de-traited tasks
This commit is contained in:
parent
6d77acff8a
commit
5f00bb9526
3 changed files with 19 additions and 20 deletions
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub type World = RTree<Block>;
|
pub type World = RTree<Block>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub pos: Vec3,
|
pub pos: Vec3,
|
||||||
|
|
|
@ -35,6 +35,7 @@ mod turtle;
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct ControlState {
|
struct ControlState {
|
||||||
turtles: Vec<turtle::Turtle>,
|
turtles: Vec<turtle::Turtle>,
|
||||||
|
tasks: Vec<VecDeque<TurtleMineJob>>,
|
||||||
world: blocks::World,
|
world: blocks::World,
|
||||||
//chunkloaders: unimplemented!(),
|
//chunkloaders: unimplemented!(),
|
||||||
}
|
}
|
||||||
|
@ -53,6 +54,7 @@ async fn main() -> Result<(), Error> {
|
||||||
ErrorKind::NotFound => ControlState {
|
ErrorKind::NotFound => ControlState {
|
||||||
turtles: Vec::new(),
|
turtles: Vec::new(),
|
||||||
world: World::new(),
|
world: World::new(),
|
||||||
|
tasks: Vec::new(),
|
||||||
},
|
},
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
},
|
},
|
||||||
|
@ -97,9 +99,12 @@ async fn create_turtle(
|
||||||
State(state): State<SharedControl>,
|
State(state): State<SharedControl>,
|
||||||
Json(req): Json<turtle::TurtleRegister>,
|
Json(req): Json<turtle::TurtleRegister>,
|
||||||
) -> Json<turtle::TurtleResponse> {
|
) -> Json<turtle::TurtleResponse> {
|
||||||
let turtles = &mut state.write().await.turtles;
|
let state = &mut state.write().await;
|
||||||
|
let turtles = &mut state.turtles;
|
||||||
let id = turtles.len() as u32;
|
let id = turtles.len() as u32;
|
||||||
turtles.push(turtle::Turtle::new(id, req.position, req.facing, req.fuel));
|
turtles.push(turtle::Turtle::new(id, req.position, req.facing, req.fuel));
|
||||||
|
state.tasks.push(VecDeque::new());
|
||||||
|
|
||||||
|
|
||||||
println!("turt {id}");
|
println!("turt {id}");
|
||||||
|
|
||||||
|
@ -115,7 +120,7 @@ async fn set_goal(
|
||||||
State(state): State<SharedControl>,
|
State(state): State<SharedControl>,
|
||||||
Json(req): Json<Position>,
|
Json(req): Json<Position>,
|
||||||
) -> &'static str {
|
) -> &'static str {
|
||||||
state.write().await.turtles[id as usize].add_task(
|
state.write().await.tasks[id as usize].push_back(
|
||||||
TurtleMineJob::chunk(req.0)
|
TurtleMineJob::chunk(req.0)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -139,8 +144,6 @@ async fn turtle_info(
|
||||||
let state = &mut state.read().await;
|
let state = &mut state.read().await;
|
||||||
let turtle = &state.turtles[id as usize];
|
let turtle = &state.turtles[id as usize];
|
||||||
|
|
||||||
let tasks: VecDeque<Arc<dyn TurtleTask + Send + Sync>> = VecDeque::new();
|
|
||||||
|
|
||||||
let cloned = turtle::Turtle {
|
let cloned = turtle::Turtle {
|
||||||
name: turtle.name.clone(),
|
name: turtle.name.clone(),
|
||||||
fuel: turtle.fuel,
|
fuel: turtle.fuel,
|
||||||
|
@ -148,7 +151,6 @@ async fn turtle_info(
|
||||||
position: turtle.position.clone(),
|
position: turtle.position.clone(),
|
||||||
goal: turtle.goal.clone(),
|
goal: turtle.goal.clone(),
|
||||||
pending_update: turtle.pending_update,
|
pending_update: turtle.pending_update,
|
||||||
tasks,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Json(cloned)
|
Json(cloned)
|
||||||
|
|
|
@ -34,8 +34,6 @@ pub(crate) struct Turtle {
|
||||||
pub(crate) position: Position,
|
pub(crate) position: Position,
|
||||||
pub(crate) goal: Option<Iota>,
|
pub(crate) goal: Option<Iota>,
|
||||||
pub(crate) pending_update: bool,
|
pub(crate) pending_update: bool,
|
||||||
#[serde(skip)]
|
|
||||||
pub(crate) tasks: VecDeque<RwLock<Arc<dyn TurtleTask + Send + Sync>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Turtle {
|
impl Default for Turtle {
|
||||||
|
@ -47,7 +45,6 @@ impl Default for Turtle {
|
||||||
position: (Vec3::zeros(), Direction::North),
|
position: (Vec3::zeros(), Direction::North),
|
||||||
goal: Default::default(),
|
goal: Default::default(),
|
||||||
pending_update: Default::default(),
|
pending_update: Default::default(),
|
||||||
tasks: VecDeque::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,13 +58,8 @@ impl Turtle {
|
||||||
position: (position, facing),
|
position: (position, facing),
|
||||||
goal: None,
|
goal: None,
|
||||||
pending_update: true,
|
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(
|
pub(crate) fn process_turtle_update(
|
||||||
|
@ -79,6 +71,10 @@ pub(crate) fn process_turtle_update(
|
||||||
.turtles
|
.turtles
|
||||||
.get_mut(id as usize)
|
.get_mut(id as usize)
|
||||||
.context("nonexisting turtle")?;
|
.context("nonexisting turtle")?;
|
||||||
|
let tasks = state
|
||||||
|
.tasks
|
||||||
|
.get_mut(id as usize)
|
||||||
|
.context("state gone?????").unwrap();
|
||||||
let world = &mut state.world;
|
let world = &mut state.world;
|
||||||
|
|
||||||
if turtle.pending_update {
|
if turtle.pending_update {
|
||||||
|
@ -98,32 +94,32 @@ pub(crate) fn process_turtle_update(
|
||||||
pos: turtle.position.0 + Vec3::y(),
|
pos: turtle.position.0 + Vec3::y(),
|
||||||
};
|
};
|
||||||
world.remove_at_point(&above.pos.into());
|
world.remove_at_point(&above.pos.into());
|
||||||
world.insert(above);
|
world.insert(above.clone());
|
||||||
|
|
||||||
let ahead = Block {
|
let ahead = Block {
|
||||||
name: update.ahead,
|
name: update.ahead,
|
||||||
pos: turtle.position.0 + turtle.position.1.clone().unit(),
|
pos: turtle.position.0 + turtle.position.1.clone().unit(),
|
||||||
};
|
};
|
||||||
world.remove_at_point(&ahead.pos.into());
|
world.remove_at_point(&ahead.pos.into());
|
||||||
world.insert(ahead);
|
world.insert(ahead.clone());
|
||||||
|
|
||||||
let below = Block {
|
let below = Block {
|
||||||
name: update.below,
|
name: update.below,
|
||||||
pos: turtle.position.0 - Vec3::y(),
|
pos: turtle.position.0 - Vec3::y(),
|
||||||
};
|
};
|
||||||
world.remove_at_point(&below.pos.into());
|
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(above);
|
||||||
task.handle_block(below);
|
task.handle_block(below);
|
||||||
task.handle_block(ahead);
|
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 {
|
let command = match goal {
|
||||||
Iota::End => {
|
Iota::End => {
|
||||||
turtle.tasks.pop_front();
|
tasks.pop_front();
|
||||||
TurtleCommand::Wait(0) // TODO: fix
|
TurtleCommand::Wait(0) // TODO: fix
|
||||||
},
|
},
|
||||||
Iota::Goto(pos) => {
|
Iota::Goto(pos) => {
|
||||||
|
@ -143,6 +139,7 @@ pub(crate) fn process_turtle_update(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
println!("Order: {:?}", command);
|
||||||
return Ok(command);
|
return Ok(command);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue