diff --git a/server/src/blocks.rs b/server/src/blocks.rs index 25cfc3d..e416a23 100644 --- a/server/src/blocks.rs +++ b/server/src/blocks.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; pub type World = RTree; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] pub struct Block { pub name: String, pub pos: Vec3, diff --git a/server/src/main.rs b/server/src/main.rs index 9de8521..0433702 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -35,6 +35,7 @@ mod turtle; #[derive(Serialize, Deserialize)] struct ControlState { turtles: Vec, + tasks: Vec>, world: blocks::World, //chunkloaders: unimplemented!(), } @@ -53,6 +54,7 @@ async fn main() -> Result<(), Error> { ErrorKind::NotFound => ControlState { turtles: Vec::new(), world: World::new(), + tasks: Vec::new(), }, _ => panic!(), }, @@ -97,9 +99,12 @@ async fn create_turtle( State(state): State, Json(req): Json, ) -> Json { - let turtles = &mut state.write().await.turtles; + let state = &mut state.write().await; + let turtles = &mut state.turtles; let id = turtles.len() as u32; turtles.push(turtle::Turtle::new(id, req.position, req.facing, req.fuel)); + state.tasks.push(VecDeque::new()); + println!("turt {id}"); @@ -115,7 +120,7 @@ async fn set_goal( State(state): State, Json(req): Json, ) -> &'static str { - state.write().await.turtles[id as usize].add_task( + state.write().await.tasks[id as usize].push_back( TurtleMineJob::chunk(req.0) ); @@ -139,8 +144,6 @@ async fn turtle_info( let state = &mut state.read().await; let turtle = &state.turtles[id as usize]; - let tasks: VecDeque> = VecDeque::new(); - let cloned = turtle::Turtle { name: turtle.name.clone(), fuel: turtle.fuel, @@ -148,7 +151,6 @@ async fn turtle_info( position: turtle.position.clone(), goal: turtle.goal.clone(), pending_update: turtle.pending_update, - tasks, }; Json(cloned) diff --git a/server/src/turtle.rs b/server/src/turtle.rs index 46bdc8e..b2806ad 100644 --- a/server/src/turtle.rs +++ b/server/src/turtle.rs @@ -34,8 +34,6 @@ pub(crate) struct Turtle { pub(crate) position: Position, pub(crate) goal: Option, pub(crate) pending_update: bool, - #[serde(skip)] - pub(crate) tasks: VecDeque>>, } impl Default for Turtle { @@ -47,7 +45,6 @@ impl Default for Turtle { position: (Vec3::zeros(), Direction::North), goal: Default::default(), pending_update: Default::default(), - tasks: VecDeque::new(), } } } @@ -61,13 +58,8 @@ impl Turtle { position: (position, facing), goal: None, pending_update: true, - tasks: VecDeque::new(), } } - - pub fn add_task(&mut self, task: impl TurtleTask + Send + Sync) { - self.tasks.push_back(Arc::new(task)); - } } pub(crate) fn process_turtle_update( @@ -79,6 +71,10 @@ pub(crate) fn process_turtle_update( .turtles .get_mut(id as usize) .context("nonexisting turtle")?; + let tasks = state + .tasks + .get_mut(id as usize) + .context("state gone?????").unwrap(); let world = &mut state.world; if turtle.pending_update { @@ -98,32 +94,32 @@ pub(crate) fn process_turtle_update( pos: turtle.position.0 + Vec3::y(), }; world.remove_at_point(&above.pos.into()); - world.insert(above); + world.insert(above.clone()); let ahead = Block { name: update.ahead, pos: turtle.position.0 + turtle.position.1.clone().unit(), }; world.remove_at_point(&ahead.pos.into()); - world.insert(ahead); + world.insert(ahead.clone()); let below = Block { name: update.below, pos: turtle.position.0 - Vec3::y(), }; world.remove_at_point(&below.pos.into()); - world.insert(below); + world.insert(below.clone()); - if let Some(task) = turtle.tasks.front() { + if let Some(task) = tasks.front_mut() { task.handle_block(above); task.handle_block(below); task.handle_block(ahead); } - if let Some(goal) = turtle.tasks.front().map(|t| t.next(&turtle)) { + if let Some(goal) = tasks.front_mut().map(|t| t.next(&turtle)) { let command = match goal { Iota::End => { - turtle.tasks.pop_front(); + tasks.pop_front(); TurtleCommand::Wait(0) // TODO: fix }, Iota::Goto(pos) => { @@ -143,6 +139,7 @@ pub(crate) fn process_turtle_update( }, }; + println!("Order: {:?}", command); return Ok(command); };