From 058e12ef23ff7e894294549ae9d670f32bdc0545 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Fri, 22 Dec 2023 09:51:14 -0600 Subject: [PATCH] removed trait tasks --- server/src/blocks.rs | 6 +- server/src/main.rs | 23 +++----- server/src/mine.rs | 129 +------------------------------------------ server/src/paths.rs | 4 +- server/src/turtle.rs | 27 +-------- 5 files changed, 14 insertions(+), 175 deletions(-) diff --git a/server/src/blocks.rs b/server/src/blocks.rs index cb06e44..49c4153 100644 --- a/server/src/blocks.rs +++ b/server/src/blocks.rs @@ -1,9 +1,9 @@ -use std::{sync::Arc, ops::Index}; +use std::sync::Arc; use nalgebra::Vector3; -use rstar::{self, PointDistance, RTree, RTreeObject, AABB}; +use rstar::{PointDistance, RTree, RTreeObject, AABB}; use serde::{Deserialize, Serialize}; -use tokio::sync::{RwLock, RwLockReadGuard, OwnedRwLockReadGuard}; +use tokio::sync::{RwLock, OwnedRwLockReadGuard}; use crate::{turtle::TurtleCommand, paths::{self, TRANSPARENT}}; diff --git a/server/src/main.rs b/server/src/main.rs index a46d201..5553388 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,32 +2,25 @@ use std::{collections::VecDeque, io::ErrorKind, sync::Arc, env::args, path}; -use anyhow::{Context, Error, Ok}; +use anyhow::{Error, Ok}; use axum::{ extract::{Path, State}, - http::request, routing::{get, post}, Json, Router, }; use blocks::{World, Position, Vec3}; -use indoc::formatdoc; use log::info; -use mine::TurtleMineJob; -use rstar::{self, AABB, RTree}; +use rstar::RTree; -use const_format::formatcp; -use hyper::body::Incoming; -use nalgebra::Vector3; use names::Name; -use serde::{Deserialize, Serialize}; use tokio::{sync::{ - watch::{self}, - Mutex, RwLock, mpsc, OnceCell + RwLock, mpsc, OnceCell }, fs}; -use tower::Service; -use turtle::{TurtleTask, Iota, Receiver, Sender, Turtle, TurtleUpdate, TurtleInfo, TurtleCommand, TurtleCommander, TurtleCommandResponse}; +use turtle::{Turtle, TurtleInfo, TurtleCommand, TurtleCommander, TurtleCommandResponse}; +use serde::{Deserialize, Serialize}; +use indoc::formatdoc; -use crate::{blocks::Block, paths::route}; +use crate::blocks::Block; mod blocks; mod names; @@ -46,7 +39,7 @@ struct SavedState { struct LiveState { turtles: Vec>>, - tasks: Vec>, + tasks: Vec>, world: blocks::World, } diff --git a/server/src/mine.rs b/server/src/mine.rs index 4f1e7ed..087d1f0 100644 --- a/server/src/mine.rs +++ b/server/src/mine.rs @@ -1,133 +1,6 @@ -use hilbert_index::FromHilbertIndex; use log::{info, warn}; -use nalgebra::SimdValue; -use serde::{Deserialize, Serialize}; -use rstar::{self, AABB}; -use std::collections::VecDeque; - -use crate::{blocks::{Position, Vec3, Block, Direction}, turtle::{TurtleTask, Iota, self, Turtle, TurtleCommand, TurtleCommander, TurtleCommandResponse, InventorySlot}, paths::TRANSPARENT}; - -#[derive(Serialize, Deserialize)] -pub struct TurtleMineJobParams { - pub chunks: Vec, - pub method: TurtleMineMethod, - pub refuel: Position, - pub storage: Position, -} - -#[derive(Serialize, Deserialize)] -pub struct TurtleMineJob { - to_mine: VecDeque, - mined: u32, - mined_chunks: u32, - params: TurtleMineJobParams, - state: State, -} - -#[derive(Serialize, Deserialize, Debug)] -enum State { - Mining, - Refueling, - Storing, -} - -#[derive(Serialize, Deserialize)] -pub enum TurtleMineMethod { - Clear, - Hilbert, -} - -fn next_hilbert_chunk(n: i32, min: Vec3, half: bool) -> Option { - let max = min + Vec3::new(16, 16, 16); - let level = if half { - 3 - } else { 4 }; - - let point: [usize; 3] = FromHilbertIndex::from_hilbert_index(&(n as usize), level); - - let mut point = Vec3::new(point[0] as i32, point[1] as i32, point[2] as i32); - - dbg!(point); - - point = if half { - point * 2 - } else { - point - }; - - point += min; - if point > max { - None - } else { - Some(dbg!(point)) - } -} - -impl TurtleMineJob { - pub fn chunk(point: Vec3) -> Self { - //let chunk_min = Vec3::new( - // point.x - point.x % 16, - // point.y - point.y % 16, - // point.z - point.z % 16); - let chunk_min = point; - TurtleMineJob { to_mine: VecDeque::new(), mined: 0, mined_chunks: 0, - params: TurtleMineJobParams { chunks: vec![chunk_min], method: TurtleMineMethod::Hilbert, refuel: - Position::new(Vec3::new(-30,65,-44), Direction::South), - storage: Position::new(Vec3::new(-29,65,-44), Direction::South) - }, - state: State::Mining } - } - -} - -impl TurtleTask for TurtleMineJob { - fn handle_block(&mut self, block: Block) { - // TODO: more logic - if block.name.contains("ore") { - self.to_mine.push_back(block.pos); - } - } - - fn next(&mut self, turtle: &Turtle) -> Iota { - if (turtle.fuel as i32) < (turtle.position.pos - self.params.refuel.pos).abs().sum() * 2 { - self.state = State::Refueling; - } - println!("{:?}", self.state); - println!("{}m to depot",(turtle.position.pos - self.params.refuel.pos).abs().sum()); - - match self.state { - State::Mining => { - if let Some(block) = self.to_mine.pop_back() { - return Iota::Mine(block.into()); - } - - if let Some(block) = next_hilbert_chunk(self.mined as i32, self.params.chunks[self.mined_chunks as usize], false) { - self.mined += 1; - return Iota::Mine(block.into()); - } - - Iota::End - }, - State::Refueling => { - if turtle.position != self.params.refuel { - Iota::Goto(self.params.refuel) - } else { - self.state = State::Mining; - Iota::Execute(TurtleCommand::Refuel) - } - }, - State::Storing => { - if turtle.position != self.params.storage { - Iota::Goto(self.params.storage) - } else { - self.state = State::Mining; - Iota::Execute(TurtleCommand::Poweroff) - } - }, - } - } -} +use crate::{blocks::{Position, Vec3}, turtle::{TurtleCommand, TurtleCommander, TurtleCommandResponse, InventorySlot}, paths::TRANSPARENT}; use TurtleCommand::*; /// Things to leave in the field (not worth fuel) diff --git a/server/src/paths.rs b/server/src/paths.rs index 44bb54b..3589e99 100644 --- a/server/src/paths.rs +++ b/server/src/paths.rs @@ -1,7 +1,5 @@ -use std::rc::Rc; - use crate::{ - blocks::{Block, World, Position, Direction, Vec3, WorldReadLock}, turtle::TurtleCommand, + blocks::{World, Position, Direction, Vec3, WorldReadLock}, }; use pathfinding::prelude::astar; diff --git a/server/src/turtle.rs b/server/src/turtle.rs index 2bb367e..fa9bf76 100644 --- a/server/src/turtle.rs +++ b/server/src/turtle.rs @@ -1,13 +1,8 @@ - -use crate::SharedControl; use crate::blocks::Block; use crate::blocks::Direction; use crate::blocks::Position; use crate::blocks::Vec3; use crate::blocks::World; -use crate::blocks::nearest; -use crate::mine::TurtleMineJob; -use crate::paths; use crate::paths::route_facing; use anyhow::Ok; @@ -21,13 +16,11 @@ use tokio::sync::OnceCell; use tokio::sync::RwLock; use tokio::sync::mpsc; use tokio::sync::oneshot; -use tokio::sync::oneshot::channel; + use tokio::time::timeout; use super::LiveState; -use std::collections::VecDeque; -use std::future::Ready; use std::sync::Arc; use std::sync::atomic::AtomicUsize; use std::time::Duration; @@ -53,7 +46,6 @@ pub(crate) struct Turtle { /// movement vector of last given command pub(crate) queued_movement: Vec3, pub(crate) position: Position, - pub(crate) goal: Option, pub(crate) pending_update: bool, #[serde(skip)] callback: Option>, @@ -94,7 +86,6 @@ impl Default for Turtle { fuel_limit: Default::default(), queued_movement: Default::default(), position: Position::new(Vec3::zeros(), Direction::North), - goal: None, pending_update: Default::default(), callback: None, sender: Some(Arc::new(sender)), @@ -466,19 +457,3 @@ pub(crate) struct TurtleResponse { pub(crate) id: u32, pub(crate) command: TurtleCommand, } - -#[derive(Serialize, Deserialize, Clone)] -pub enum Iota { - End, - Goto(Position), - Mine(Vec3), - Execute(TurtleCommand), -} - -pub trait TurtleTask: erased_serde::Serialize { - fn handle_block(&mut self, _: Block) { } - fn next(&mut self, turtle: &Turtle) -> Iota - { Iota::End } -} - -erased_serde::serialize_trait_object!(TurtleTask);