From e01ef3d10d44734e77c7311390e900e707fd3eeb Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Sun, 17 Dec 2023 16:03:10 -0600 Subject: [PATCH] factored out Vec3 --- server/src/blocks.rs | 45 +++++++++++++++++-- server/src/main.rs | 104 +++---------------------------------------- server/src/paths.rs | 4 +- server/src/turtle.rs | 41 +++++++++++++---- 4 files changed, 82 insertions(+), 112 deletions(-) diff --git a/server/src/blocks.rs b/server/src/blocks.rs index 44ff8f6..53f1877 100644 --- a/server/src/blocks.rs +++ b/server/src/blocks.rs @@ -1,15 +1,13 @@ -use pathfinding::prelude::astar; +use nalgebra::Vector3; use rstar::{self, PointDistance, RTree, RTreeObject, AABB}; use serde::{Deserialize, Serialize}; -use crate::Vec3; - pub type World = RTree; #[derive(Serialize, Deserialize)] pub struct Block { pub name: String, - pub pos: super::Vec3, + pub pos: Vec3, } impl RTreeObject for Block { @@ -25,3 +23,42 @@ impl PointDistance for Block { (self.pos - Vec3::from(*point)).abs().sum() } } + +pub type Vec3 = Vector3; +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), + } + } +} diff --git a/server/src/main.rs b/server/src/main.rs index 10894f9..a3e3510 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -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 axum::{ @@ -7,13 +7,11 @@ use axum::{ routing::{get, post}, Json, Router, }; -use blocks::World; +use blocks::{World, Position}; use rstar::{self, AABB}; -mod names; use const_format::formatcp; use hyper::body::Incoming; -use hyper_util::rt::TokioIo; use nalgebra::Vector3; use names::Name; use serde::{Deserialize, Serialize}; @@ -24,47 +22,13 @@ use tokio::sync::{ use tower::Service; use crate::{blocks::Block, paths::route}; + mod blocks; - +mod names; +mod mine; mod paths; - -pub type Vec3 = Vector3; - -#[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), - } - } -} +mod safe_kill; +mod turtle; #[derive(Serialize, Deserialize)] struct ControlState { @@ -114,7 +78,6 @@ async fn main() -> Result<(), Error> { Ok(()) } -mod safe_kill; async fn write_to_disk(state: SharedControl) -> anyhow::Result<()> { 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 { - 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, - method: TurtleMineMethod, - refuel: Position, - storage: Position, -} - -#[derive(Serialize, Deserialize)] -struct TurtleMineJob { - to_mine: VecDeque, - mined: AABB<[i32; 3]>, - params: TurtleMineJobParams, -} - -#[derive(Serialize, Deserialize)] -enum TurtleMineMethod { - Clear, - Strip, -} async fn client() -> &'static str { formatcp!( "local ipaddr = {}\n{}", @@ -269,4 +180,3 @@ async fn client() -> &'static str { ) } -mod turtle; diff --git a/server/src/paths.rs b/server/src/paths.rs index 619a28d..494205b 100644 --- a/server/src/paths.rs +++ b/server/src/paths.rs @@ -1,12 +1,10 @@ use std::rc::Rc; use crate::{ - blocks::{Block, World}, - Direction, Position, + blocks::{Block, World, Position, Direction, Vec3}, }; use pathfinding::prelude::astar; -use super::Vec3; pub fn route(from: Position, to: Position, world: &World) -> Option> { // attempt at not crashing by looking infinitely into the abyss diff --git a/server/src/turtle.rs b/server/src/turtle.rs index 1538256..fe33f60 100644 --- a/server/src/turtle.rs +++ b/server/src/turtle.rs @@ -1,8 +1,9 @@ -use super::TurtleMineJob; - -use super::difference; use crate::blocks::Block; +use crate::blocks::Direction; +use crate::blocks::Position; +use crate::blocks::Vec3; +use crate::mine::TurtleMineJob; use anyhow::Ok; @@ -11,13 +12,8 @@ use anyhow::Context; use super::ControlState; -use super::Direction; - use std::collections::VecDeque; -use super::Position; - -use super::Vec3; use super::names::Name; @@ -215,3 +211,32 @@ pub(crate) struct TurtleResponse { pub(crate) id: u32, pub(crate) command: TurtleCommand, } + +/// Get a turtle command to map two adjacent positions +fn difference(from: Position, to: Position) -> Option { + 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 + } +}