From 8660daad1d0934b2f7193df88f2fe3af878e3f83 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Fri, 22 Dec 2023 10:58:10 -0600 Subject: [PATCH] depots --- server/src/blocks.rs | 6 ++++-- server/src/main.rs | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/server/src/blocks.rs b/server/src/blocks.rs index 49c4153..aa444e4 100644 --- a/server/src/blocks.rs +++ b/server/src/blocks.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{sync::Arc, ops::Sub}; use nalgebra::Vector3; 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)] diff --git a/server/src/main.rs b/server/src/main.rs index 83cfd94..7e8cb00 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -8,13 +8,13 @@ use axum::{ routing::{get}, Router, }; -use blocks::{World, }; +use blocks::{World, Position, }; use log::info; use rstar::RTree; use names::Name; use tokio::{sync::{ - RwLock, mpsc, OnceCell + RwLock, mpsc, OnceCell, Mutex }, fs}; use turtle::{Turtle, TurtleCommander}; use serde::{Deserialize, Serialize}; @@ -36,6 +36,7 @@ mod tasks; struct SavedState { turtles: Vec, world: RTree, + depots: Vec, //chunkloaders: unimplemented!(), } @@ -43,6 +44,7 @@ struct LiveState { turtles: Vec>>, tasks: Vec>, world: blocks::World, + depots: Mutex>>>, } impl LiveState { @@ -51,7 +53,11 @@ impl LiveState { for turtle in self.turtles.iter() { 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 { @@ -60,7 +66,13 @@ impl LiveState { let (tx, rx) = mpsc::channel(1); 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 { @@ -120,10 +132,13 @@ async fn flush(State(state): State) -> &'static str { } async fn write_to_disk(state: SavedState) -> anyhow::Result<()> { - let json = serde_json::to_string_pretty(&state.turtles)?; - let bincode = bincode::serialize(&state.world)?; - tokio::fs::write(SAVE.get().unwrap().join("turtles.json"), json).await?; - tokio::fs::write(SAVE.get().unwrap().join("world.bin"), bincode).await?; + let turtles = serde_json::to_string_pretty(&state.turtles)?; + let world = bincode::serialize(&state.world)?; + let depots = serde_json::to_string_pretty(&state.depots)?; + 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(()) } @@ -140,6 +155,18 @@ async fn read_from_disk() -> anyhow::Result { }, }; + 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() .read(true).open(SAVE.get().unwrap().join("world.bin")).await { tokio::io::Result::Ok(file) => bincode::deserialize_from(file.into_std().await)?, @@ -153,5 +180,6 @@ async fn read_from_disk() -> anyhow::Result { Ok(SavedState { turtles, world, + depots, }) }