1
Fork 0
This commit is contained in:
Andy Killorin 2023-12-22 10:58:10 -06:00
parent 85c32e48af
commit 8660daad1d
Signed by: ank
GPG key ID: B6241CA3B552BCA4
2 changed files with 40 additions and 10 deletions

View file

@ -1,4 +1,4 @@
use std::sync::Arc; use std::{sync::Arc, ops::Sub};
use nalgebra::Vector3; use nalgebra::Vector3;
use rstar::{PointDistance, RTree, RTreeObject, AABB}; use rstar::{PointDistance, RTree, RTreeObject, AABB};
@ -124,7 +124,9 @@ impl Position {
}) })
} }
pub fn manhattan(self, other: Self) -> i32 {
self.pos.sub(other.pos).abs().sum()
}
} }
#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Copy, Debug)] #[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Copy, Debug)]

View file

@ -8,13 +8,13 @@ use axum::{
routing::{get}, routing::{get},
Router, Router,
}; };
use blocks::{World, }; use blocks::{World, Position, };
use log::info; use log::info;
use rstar::RTree; use rstar::RTree;
use names::Name; use names::Name;
use tokio::{sync::{ use tokio::{sync::{
RwLock, mpsc, OnceCell RwLock, mpsc, OnceCell, Mutex
}, fs}; }, fs};
use turtle::{Turtle, TurtleCommander}; use turtle::{Turtle, TurtleCommander};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -36,6 +36,7 @@ mod tasks;
struct SavedState { struct SavedState {
turtles: Vec<turtle::Turtle>, turtles: Vec<turtle::Turtle>,
world: RTree<Block>, world: RTree<Block>,
depots: Vec<Position>,
//chunkloaders: unimplemented!(), //chunkloaders: unimplemented!(),
} }
@ -43,6 +44,7 @@ struct LiveState {
turtles: Vec<Arc<RwLock<turtle::Turtle>>>, turtles: Vec<Arc<RwLock<turtle::Turtle>>>,
tasks: Vec<VecDeque<()>>, tasks: Vec<VecDeque<()>>,
world: blocks::World, world: blocks::World,
depots: Mutex<Vec<Arc<Mutex<Position>>>>,
} }
impl LiveState { impl LiveState {
@ -51,7 +53,11 @@ impl LiveState {
for turtle in self.turtles.iter() { for turtle in self.turtles.iter() {
turtles.push(turtle.read().await.info()); turtles.push(turtle.read().await.info());
}; };
SavedState { turtles, world: self.world.tree().await } let mut depots = Vec::new();
for depot in self.depots.lock().await.iter() {
depots.push(*depot.lock().await);
};
SavedState { turtles, world: self.world.tree().await, depots }
} }
fn from_save(save: SavedState) -> Self { fn from_save(save: SavedState) -> Self {
@ -60,7 +66,13 @@ impl LiveState {
let (tx, rx) = mpsc::channel(1); let (tx, rx) = mpsc::channel(1);
turtles.push(Turtle::with_channel(turtle.name.to_num(), turtle.position, turtle.fuel, turtle.fuel_limit, tx, rx)); turtles.push(Turtle::with_channel(turtle.name.to_num(), turtle.position, turtle.fuel, turtle.fuel_limit, tx, rx));
}; };
Self { turtles: turtles.into_iter().map(|t| Arc::new(RwLock::new(t))).collect(), tasks: Vec::new(), world: World::from_tree(save.world) } let mut depots = Vec::new();
for depot in save.depots {
depots.push(Arc::new(Mutex::new(depot)));
}
Self { turtles: turtles.into_iter().map(|t| Arc::new(RwLock::new(t))).collect(), tasks: Vec::new(), world: World::from_tree(save.world),
depots: Mutex::new(depots)
}
} }
async fn get_turtle(&self, name: u32) -> Option<TurtleCommander> { async fn get_turtle(&self, name: u32) -> Option<TurtleCommander> {
@ -120,10 +132,13 @@ async fn flush(State(state): State<SharedControl>) -> &'static str {
} }
async fn write_to_disk(state: SavedState) -> anyhow::Result<()> { async fn write_to_disk(state: SavedState) -> anyhow::Result<()> {
let json = serde_json::to_string_pretty(&state.turtles)?; let turtles = serde_json::to_string_pretty(&state.turtles)?;
let bincode = bincode::serialize(&state.world)?; let world = bincode::serialize(&state.world)?;
tokio::fs::write(SAVE.get().unwrap().join("turtles.json"), json).await?; let depots = serde_json::to_string_pretty(&state.depots)?;
tokio::fs::write(SAVE.get().unwrap().join("world.bin"), bincode).await?; let path = &SAVE.get().unwrap();
tokio::fs::write(path.join("turtles.json"), turtles).await?;
tokio::fs::write(path.join("depots.json"), depots).await?;
tokio::fs::write(path.join("world.bin"), world).await?;
Ok(()) Ok(())
} }
@ -140,6 +155,18 @@ async fn read_from_disk() -> anyhow::Result<SavedState> {
}, },
}; };
let depots = match tokio::fs::OpenOptions::new()
.read(true)
.open(SAVE.get().unwrap().join("turtles.json"))
.await
{
tokio::io::Result::Ok(file) => serde_json::from_reader(file.into_std().await)?,
tokio::io::Result::Err(e) => match e.kind() {
ErrorKind::NotFound => Vec::new(),
_ => panic!(),
},
};
let world = match tokio::fs::OpenOptions::new() let world = match tokio::fs::OpenOptions::new()
.read(true).open(SAVE.get().unwrap().join("world.bin")).await { .read(true).open(SAVE.get().unwrap().join("world.bin")).await {
tokio::io::Result::Ok(file) => bincode::deserialize_from(file.into_std().await)?, tokio::io::Result::Ok(file) => bincode::deserialize_from(file.into_std().await)?,
@ -153,5 +180,6 @@ async fn read_from_disk() -> anyhow::Result<SavedState> {
Ok(SavedState { Ok(SavedState {
turtles, turtles,
world, world,
depots,
}) })
} }