factored out Vec3
This commit is contained in:
parent
93f7befa7a
commit
e01ef3d10d
4 changed files with 82 additions and 112 deletions
|
@ -1,15 +1,13 @@
|
||||||
use pathfinding::prelude::astar;
|
use nalgebra::Vector3;
|
||||||
use rstar::{self, PointDistance, RTree, RTreeObject, AABB};
|
use rstar::{self, PointDistance, RTree, RTreeObject, AABB};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::Vec3;
|
|
||||||
|
|
||||||
pub type World = RTree<Block>;
|
pub type World = RTree<Block>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub pos: super::Vec3,
|
pub pos: Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RTreeObject for Block {
|
impl RTreeObject for Block {
|
||||||
|
@ -25,3 +23,42 @@ impl PointDistance for Block {
|
||||||
(self.pos - Vec3::from(*point)).abs().sum()
|
(self.pos - Vec3::from(*point)).abs().sum()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type Vec3 = Vector3<i32>;
|
||||||
|
pub type Position = (Vec3, Direction);
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Copy, Debug)]
|
||||||
|
pub enum Direction {
|
||||||
|
North,
|
||||||
|
South,
|
||||||
|
East,
|
||||||
|
West,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Direction {
|
||||||
|
pub fn left(self) -> Self {
|
||||||
|
match self {
|
||||||
|
Direction::North => Direction::West,
|
||||||
|
Direction::South => Direction::East,
|
||||||
|
Direction::East => Direction::North,
|
||||||
|
Direction::West => Direction::South,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn right(self) -> Self {
|
||||||
|
match self {
|
||||||
|
Direction::North => Direction::East,
|
||||||
|
Direction::South => Direction::West,
|
||||||
|
Direction::East => Direction::South,
|
||||||
|
Direction::West => Direction::North,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn unit(self) -> Vec3 {
|
||||||
|
match self {
|
||||||
|
Direction::North => Vec3::new(0, 0, -1),
|
||||||
|
Direction::South => Vec3::new(0, 0, 1),
|
||||||
|
Direction::East => Vec3::new(1, 0, 0),
|
||||||
|
Direction::West => Vec3::new(-1, 0, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::VecDeque, env::args, fs, io::ErrorKind, sync::Arc};
|
use std::{collections::VecDeque, io::ErrorKind, sync::Arc};
|
||||||
|
|
||||||
use anyhow::{Context, Error, Ok};
|
use anyhow::{Context, Error, Ok};
|
||||||
use axum::{
|
use axum::{
|
||||||
|
@ -7,13 +7,11 @@ use axum::{
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Json, Router,
|
Json, Router,
|
||||||
};
|
};
|
||||||
use blocks::World;
|
use blocks::{World, Position};
|
||||||
use rstar::{self, AABB};
|
use rstar::{self, AABB};
|
||||||
|
|
||||||
mod names;
|
|
||||||
use const_format::formatcp;
|
use const_format::formatcp;
|
||||||
use hyper::body::Incoming;
|
use hyper::body::Incoming;
|
||||||
use hyper_util::rt::TokioIo;
|
|
||||||
use nalgebra::Vector3;
|
use nalgebra::Vector3;
|
||||||
use names::Name;
|
use names::Name;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -24,47 +22,13 @@ use tokio::sync::{
|
||||||
use tower::Service;
|
use tower::Service;
|
||||||
|
|
||||||
use crate::{blocks::Block, paths::route};
|
use crate::{blocks::Block, paths::route};
|
||||||
|
|
||||||
mod blocks;
|
mod blocks;
|
||||||
|
mod names;
|
||||||
|
mod mine;
|
||||||
mod paths;
|
mod paths;
|
||||||
|
mod safe_kill;
|
||||||
pub type Vec3 = Vector3<i32>;
|
mod turtle;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Copy, Debug)]
|
|
||||||
enum Direction {
|
|
||||||
North,
|
|
||||||
South,
|
|
||||||
East,
|
|
||||||
West,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Direction {
|
|
||||||
fn left(self) -> Self {
|
|
||||||
match self {
|
|
||||||
Direction::North => Direction::West,
|
|
||||||
Direction::South => Direction::East,
|
|
||||||
Direction::East => Direction::North,
|
|
||||||
Direction::West => Direction::South,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn right(self) -> Self {
|
|
||||||
match self {
|
|
||||||
Direction::North => Direction::East,
|
|
||||||
Direction::South => Direction::West,
|
|
||||||
Direction::East => Direction::South,
|
|
||||||
Direction::West => Direction::North,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn unit(self) -> Vec3 {
|
|
||||||
match self {
|
|
||||||
Direction::North => Vec3::new(0, 0, -1),
|
|
||||||
Direction::South => Vec3::new(0, 0, 1),
|
|
||||||
Direction::East => Vec3::new(1, 0, 0),
|
|
||||||
Direction::West => Vec3::new(-1, 0, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct ControlState {
|
struct ControlState {
|
||||||
|
@ -114,7 +78,6 @@ async fn main() -> Result<(), Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
mod safe_kill;
|
|
||||||
|
|
||||||
async fn write_to_disk(state: SharedControl) -> anyhow::Result<()> {
|
async fn write_to_disk(state: SharedControl) -> anyhow::Result<()> {
|
||||||
let json = serde_json::to_string_pretty(&(*state.read().await))?;
|
let json = serde_json::to_string_pretty(&(*state.read().await))?;
|
||||||
|
@ -209,58 +172,6 @@ async fn command(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Position = (Vec3, Direction);
|
|
||||||
|
|
||||||
/// Get a turtle command to map two adjacent positions
|
|
||||||
fn difference(from: Position, to: Position) -> Option<turtle::TurtleCommand> {
|
|
||||||
use turtle::TurtleCommand::*;
|
|
||||||
|
|
||||||
if from.0 == to.0 {
|
|
||||||
if to.1 == from.1.left() {
|
|
||||||
Some(Left)
|
|
||||||
} else if to.1 == from.1.right() {
|
|
||||||
Some(Right)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else if to.1 == from.1 {
|
|
||||||
if to.0 == from.0 + from.1.unit() {
|
|
||||||
Some(Forward(1))
|
|
||||||
} else if to.0 == from.0 - from.1.unit() {
|
|
||||||
Some(Backward(1))
|
|
||||||
} else if to.0 == from.0 + Vec3::y() {
|
|
||||||
Some(Up(1))
|
|
||||||
} else if to.0 == from.0 - Vec3::y() {
|
|
||||||
Some(Down(1))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
struct TurtleMineJobParams {
|
|
||||||
region: AABB<[i32; 3]>,
|
|
||||||
to_mine: Vec<Vec3>,
|
|
||||||
method: TurtleMineMethod,
|
|
||||||
refuel: Position,
|
|
||||||
storage: Position,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
struct TurtleMineJob {
|
|
||||||
to_mine: VecDeque<Vec3>,
|
|
||||||
mined: AABB<[i32; 3]>,
|
|
||||||
params: TurtleMineJobParams,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
enum TurtleMineMethod {
|
|
||||||
Clear,
|
|
||||||
Strip,
|
|
||||||
}
|
|
||||||
async fn client() -> &'static str {
|
async fn client() -> &'static str {
|
||||||
formatcp!(
|
formatcp!(
|
||||||
"local ipaddr = {}\n{}",
|
"local ipaddr = {}\n{}",
|
||||||
|
@ -269,4 +180,3 @@ async fn client() -> &'static str {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
mod turtle;
|
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
blocks::{Block, World},
|
blocks::{Block, World, Position, Direction, Vec3},
|
||||||
Direction, Position,
|
|
||||||
};
|
};
|
||||||
use pathfinding::prelude::astar;
|
use pathfinding::prelude::astar;
|
||||||
|
|
||||||
use super::Vec3;
|
|
||||||
|
|
||||||
pub fn route(from: Position, to: Position, world: &World) -> Option<Vec<Position>> {
|
pub fn route(from: Position, to: Position, world: &World) -> Option<Vec<Position>> {
|
||||||
// attempt at not crashing by looking infinitely into the abyss
|
// attempt at not crashing by looking infinitely into the abyss
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use super::TurtleMineJob;
|
|
||||||
|
|
||||||
use super::difference;
|
|
||||||
|
|
||||||
use crate::blocks::Block;
|
use crate::blocks::Block;
|
||||||
|
use crate::blocks::Direction;
|
||||||
|
use crate::blocks::Position;
|
||||||
|
use crate::blocks::Vec3;
|
||||||
|
use crate::mine::TurtleMineJob;
|
||||||
|
|
||||||
use anyhow::Ok;
|
use anyhow::Ok;
|
||||||
|
|
||||||
|
@ -11,13 +12,8 @@ use anyhow::Context;
|
||||||
|
|
||||||
use super::ControlState;
|
use super::ControlState;
|
||||||
|
|
||||||
use super::Direction;
|
|
||||||
|
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use super::Position;
|
|
||||||
|
|
||||||
use super::Vec3;
|
|
||||||
|
|
||||||
use super::names::Name;
|
use super::names::Name;
|
||||||
|
|
||||||
|
@ -215,3 +211,32 @@ pub(crate) struct TurtleResponse {
|
||||||
pub(crate) id: u32,
|
pub(crate) id: u32,
|
||||||
pub(crate) command: TurtleCommand,
|
pub(crate) command: TurtleCommand,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a turtle command to map two adjacent positions
|
||||||
|
fn difference(from: Position, to: Position) -> Option<TurtleCommand> {
|
||||||
|
use TurtleCommand::*;
|
||||||
|
|
||||||
|
if from.0 == to.0 {
|
||||||
|
if to.1 == from.1.left() {
|
||||||
|
Some(Left)
|
||||||
|
} else if to.1 == from.1.right() {
|
||||||
|
Some(Right)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else if to.1 == from.1 {
|
||||||
|
if to.0 == from.0 + from.1.unit() {
|
||||||
|
Some(Forward(1))
|
||||||
|
} else if to.0 == from.0 - from.1.unit() {
|
||||||
|
Some(Backward(1))
|
||||||
|
} else if to.0 == from.0 + Vec3::y() {
|
||||||
|
Some(Up(1))
|
||||||
|
} else if to.0 == from.0 - Vec3::y() {
|
||||||
|
Some(Down(1))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue