From 0f94e292bde8b9614aa48a6ba87f1a8d927b8133 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 13 Aug 2024 14:50:10 +0200 Subject: replace customers with bots and refactor some more server code. --- server/bot/src/algos/customer.rs | 219 ++++++++++++++++++ server/bot/src/algos/mod.rs | 3 + server/bot/src/algos/simple.rs | 1 + server/bot/src/algos/test.rs | 1 + server/bot/src/algos/waiter.rs | 1 + server/bot/src/lib.rs | 3 + server/bot/src/main.rs | 13 +- server/protocol/src/lib.rs | 2 + server/src/data.rs | 354 ----------------------------- server/src/data/demands.rs | 89 ++++++++ server/src/data/mod.rs | 341 +++++++++++++++++++++++++++ server/src/entity/bot.rs | 82 ++++++- server/src/entity/customers.rs | 62 +++++ server/src/entity/customers/demands.rs | 89 -------- server/src/entity/customers/mod.rs | 276 ---------------------- server/src/entity/customers/pathfinding.rs | 96 -------- server/src/entity/mod.rs | 22 +- server/src/server.rs | 40 +++- 18 files changed, 844 insertions(+), 850 deletions(-) create mode 100644 server/bot/src/algos/customer.rs delete mode 100644 server/src/data.rs create mode 100644 server/src/data/demands.rs create mode 100644 server/src/data/mod.rs create mode 100644 server/src/entity/customers.rs delete mode 100644 server/src/entity/customers/demands.rs delete mode 100644 server/src/entity/customers/mod.rs delete mode 100644 server/src/entity/customers/pathfinding.rs (limited to 'server') diff --git a/server/bot/src/algos/customer.rs b/server/bot/src/algos/customer.rs new file mode 100644 index 00000000..167fc0d7 --- /dev/null +++ b/server/bot/src/algos/customer.rs @@ -0,0 +1,219 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +use crate::{pathfinding::Path, BotAlgo, BotInput}; +use hurrycurry_protocol::{glam::IVec2, DemandIndex}; + +#[derive(Debug, Clone)] +pub enum Customer { + New, + Entering { + path: Path, + chair: IVec2, + }, + Waiting { + demand: DemandIndex, + chair: IVec2, + timeout: f32, + }, + Eating { + demand: DemandIndex, + target: IVec2, + progress: f32, + chair: IVec2, + }, + Exiting { + path: Path, + }, +} + +impl Default for Customer { + fn default() -> Self { + Customer::New + } +} + +impl BotAlgo for Customer { + fn tick( + &mut self, + me: hurrycurry_protocol::PlayerID, + game: &hurrycurry_client_lib::Game, + dt: f32, + ) -> BotInput { + let _ = (me, game, dt); + BotInput::default() + // let Some(playerdata) = game.players.get_mut(&player) else { + // return BotInput::default(); + // }; + // match state { + // let chair = self.select_chair().ok_or(anyhow!("no free chair found"))?; + // let from = game.data.customer_spawn.as_ivec2(); + // let path = find_path(&game.walkable, from, chair) + // .ok_or(anyhow!("no path from {from} to {chair}"))?; + // info!("{id:?} -> entering"); + + // CustomerState::Entering { path, chair } => { + // playerdata + // .movement + // .input(path.next_direction(playerdata.position()), false); + // if path.is_done() { + // let demand = DemandIndex(random::() as usize % self.demands.len()); + // self.cpackets.push_back(PacketS::Communicate { + // message: Some(Message::Item(self.demands[demand.0].from)), + // persist: true, + // player, + // }); + // info!("{player:?} -> waiting"); + // *state = CustomerState::Waiting { + // chair: *chair, + // timeout: 90. + random::() * 60., + // demand, + // }; + // } + // } + // CustomerState::Waiting { + // chair, + // demand, + // timeout, + // } => { + // playerdata + // .movement + // .input((chair.as_vec2() + 0.5) - playerdata.position(), false); + // *timeout -= dt; + // if *timeout <= 0. { + // self.cpackets.push_back(PacketS::Communicate { + // message: None, + // persist: true, + // player, + // }); + // self.cpackets.push_back(PacketS::Communicate { + // message: Some(Message::Effect("angry".to_string())), + // persist: false, + // player, + // }); + // let path = find_path( + // &game.walkable, + // playerdata.position().as_ivec2(), + // game.data.customer_spawn.as_ivec2(), + // ) + // .expect("no path to exit"); + // *self.chairs.get_mut(chair).unwrap() = true; + // game.score.demands_failed += 1; + // game.score.points -= 1; + // game.score_changed = true; + // info!("{player:?} -> exiting"); + // *state = CustomerState::Exiting { path } + // } else { + // let demand_data = &self.demands[demand.0]; + // let demand_pos = [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y] + // .into_iter() + // .find_map(|off| { + // let pos = *chair + off; + // if game + // .tiles + // .get(&pos) + // .map(|t| { + // t.item + // .as_ref() + // .map(|i| i.kind == demand_data.from) + // .unwrap_or_default() + // }) + // .unwrap_or_default() + // { + // Some(pos) + // } else { + // None + // } + // }); + // if let Some(pos) = demand_pos { + // self.cpackets.push_back(PacketS::Communicate { + // persist: true, + // message: None, + // player, + // }); + // self.cpackets.push_back(PacketS::Communicate { + // message: Some(Message::Effect("satisfied".to_string())), + // persist: false, + // player, + // }); + // self.cpackets.push_back(PacketS::Interact { + // pos: Some(pos), + // player, + // }); + // self.cpackets + // .push_back(PacketS::Interact { pos: None, player }); + // info!("{player:?} -> eating"); + // *state = CustomerState::Eating { + // demand: *demand, + // target: pos, + // progress: 0., + // chair: *chair, + // } + // } + // } + // } + // CustomerState::Eating { + // demand, + // target, + // progress, + // chair, + // } => { + // playerdata + // .movement + // .input((chair.as_vec2() + 0.5) - playerdata.position(), false); + // let demand = &self.demands[demand.0]; + // *progress += dt / demand.duration; + // if *progress >= 1. { + // self.cpackets.push_back(PacketS::ReplaceHand { + // player, + // item: demand.to, + // }); + // if demand.to.is_some() { + // self.cpackets.push_back(PacketS::Interact { + // player, + // pos: Some(*target), + // }); + // self.cpackets + // .push_back(PacketS::Interact { player, pos: None }); + // } + // let path = find_path( + // &game.walkable, + // playerdata.position().as_ivec2(), + // game.data.customer_spawn.as_ivec2(), + // ) + // .ok_or(anyhow!("no path to exit"))?; + // *self.chairs.get_mut(chair).unwrap() = true; + // game.score.demands_completed += 1; + // game.score.points += demand.points; + // game.score_changed = true; + // info!("{player:?} -> exiting"); + // *state = CustomerState::Exiting { path } + // } + // } + // CustomerState::Exiting { path } => { + // playerdata + // .movement + // .input(path.next_direction(playerdata.position()), false); + // if path.is_done() { + // info!("{player:?} -> leave"); + // self.cpackets.push_back(PacketS::Leave { player }); + // customers_to_remove.push(player); + // } + // } + // } + } +} diff --git a/server/bot/src/algos/mod.rs b/server/bot/src/algos/mod.rs index 7b165da4..d3aba8f4 100644 --- a/server/bot/src/algos/mod.rs +++ b/server/bot/src/algos/mod.rs @@ -15,10 +15,12 @@ along with this program. If not, see . */ +mod customer; mod simple; mod test; mod waiter; +pub use customer::Customer; pub use simple::Simple; pub use test::Test; pub use waiter::Waiter; @@ -27,4 +29,5 @@ pub const ALGO_CONSTRUCTORS: &'static [(&'static str, fn() -> Box, + pub leave: bool, } + +pub type DynBotAlgo = Box; pub trait BotAlgo { fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput; } diff --git a/server/bot/src/main.rs b/server/bot/src/main.rs index 9ecd0a84..cf115358 100644 --- a/server/bot/src/main.rs +++ b/server/bot/src/main.rs @@ -57,6 +57,7 @@ fn main() -> Result<()> { network.queue_out.push_back(PacketS::Join { name: format!("{}-bot", args.username.clone().unwrap_or(args.algo.clone())), character: args.character, + id: None, }); let mut bots = Vec::new(); @@ -85,12 +86,19 @@ fn main() -> Result<()> { game.apply_packet(packet); } - for b in &mut bots { + bots.retain_mut(|b| { let BotInput { direction, boost, interact, + leave, } = b.state.tick(b.id, &game, dt); + + if leave { + network.queue_out.push_back(PacketS::Leave { player: b.id }); + return false; + } + if interact.is_some() != b.interacting { b.interacting = interact.is_some(); network.queue_out.push_back(PacketS::Interact { @@ -104,7 +112,8 @@ fn main() -> Result<()> { boost, pos: None, }); - } + true + }); sleep(Duration::from_secs_f32(dt)); } diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 05561a12..695d45cf 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -87,6 +87,8 @@ pub enum PacketS { Join { name: String, character: i32, + #[serde(skip)] + id: Option, // used entity bots that cant receive a response }, Leave { player: PlayerID, diff --git a/server/src/data.rs b/server/src/data.rs deleted file mode 100644 index 021de525..00000000 --- a/server/src/data.rs +++ /dev/null @@ -1,354 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright 2024 metamuffin - Copyright 2024 nokoe - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, version 3 of the License only. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -*/ -use crate::entity::{construct_entity, Entities, EntityDecl}; -use anyhow::{anyhow, bail, Result}; -use hurrycurry_protocol::{ - glam::{IVec2, Vec2}, - Gamedata, ItemIndex, MapMetadata, Recipe, TileIndex, -}; -use serde::{Deserialize, Serialize}; -use std::{ - collections::{HashMap, HashSet}, - fs::File, - path::PathBuf, - str::FromStr, - sync::{Mutex, RwLock}, -}; -use tokio::fs::read_to_string; - -#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)] -#[serde(rename_all = "snake_case")] -pub enum Action { - #[default] - Never, - Passive, - Active, - Instant, - Demand, -} - -#[derive(Debug, Clone, Deserialize, Serialize)] -pub struct RecipeDecl { - #[serde(default)] - tile: Option, - #[serde(default)] - inputs: Vec, - #[serde(default)] - outputs: Vec, - #[serde(default)] - action: Action, - #[serde(default)] - warn: bool, - #[serde(default)] - revert_duration: Option, - #[serde(default)] - duration: Option, - #[serde(default)] - points: Option, -} - -#[derive(Debug, Clone, Deserialize)] -pub struct InitialMap { - map: Vec, - tiles: HashMap, - #[serde(default)] - items: HashMap, - collider: Vec, - walkable: Vec, - chef_spawn: char, - customer_spawn: char, - #[serde(default)] - entities: Vec, - #[serde(default)] - tile_entities: HashMap, - #[serde(default)] - score_baseline: i64, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DemandDecl { - from: String, - to: Option, - duration: f32, - points: i64, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Demand { - pub from: ItemIndex, - pub to: Option, - pub duration: f32, - pub points: i64, -} - -#[derive(Debug,Clone, Default)] -#[rustfmt::skip] -pub struct Serverdata { - pub spec: String, - pub initial_map: HashMap)>, - pub chef_spawn: Vec2, - pub customer_spawn: Vec2, - pub score_baseline: i64, -} - -#[derive(Debug, Deserialize, Default)] -pub struct DataIndex { - pub maps: HashMap, - pub recipes: HashSet, -} - -pub static DATA_DIR: Mutex> = Mutex::new(None); -fn data_dir() -> PathBuf { - DATA_DIR - .lock() - .unwrap() - .to_owned() - .unwrap_or_else(|| PathBuf::from_str("data").unwrap()) -} - -impl DataIndex { - pub fn reload(&mut self) -> Result<()> { - *self = serde_yml::from_reader(File::open(data_dir().join("index.yaml"))?)?; - Ok(()) - } - - pub async fn read_map(&self, name: &str) -> Result { - if !self.maps.contains_key(name) { - bail!("unknown map: {name:?}"); - } - let path = data_dir().join(format!("maps/{name}.yaml")); - Ok(read_to_string(path).await?) - } - pub async fn read_recipes(&self, name: &str) -> Result { - if !self.recipes.contains(name) { - bail!("unknown recipes: {name:?}"); - } - let path = data_dir().join(format!("recipes/{name}.yaml")); - Ok(read_to_string(path).await?) - } - - pub async fn generate(&self, spec: String) -> Result<(Gamedata, Serverdata, Entities)> { - let (map, recipes) = spec.split_once("-").unwrap_or((spec.as_str(), "default")); - - let map_in = serde_yml::from_str(&self.read_map(map).await?)?; - let recipes_in = serde_yml::from_str(&self.read_recipes(recipes).await?)?; - - Ok(build_data( - self.maps.clone(), - spec.clone(), - map.to_string(), - map_in, - recipes_in, - )?) - } -} - -pub fn build_data( - maps: HashMap, - spec: String, - map_name: String, - map_in: InitialMap, - recipes_in: Vec, -) -> Result<(Gamedata, Serverdata, Entities)> { - let reg = ItemTileRegistry::default(); - let mut recipes = Vec::new(); - let mut entities = Vec::new(); - let mut raw_demands = Vec::new(); - - for mut r in recipes_in { - let r2 = r.clone(); - let mut inputs = r.inputs.into_iter().map(|i| reg.register_item(i)); - let mut outputs = r.outputs.into_iter().map(|o| reg.register_item(o)); - let tile = r.tile.map(|t| reg.register_tile(t)); - match r.action { - Action::Never => {} - Action::Passive => recipes.push(Recipe::Passive { - duration: r.duration.ok_or(anyhow!("duration for passive missing"))?, - warn: r.warn, - tile, - revert_duration: r.revert_duration, - input: inputs - .next() - .ok_or(anyhow!("passive recipe without input"))?, - output: outputs.next(), - }), - Action::Active => recipes.push(Recipe::Active { - duration: r.duration.ok_or(anyhow!("duration for active missing"))?, - tile, - input: inputs - .next() - .ok_or(anyhow!("active recipe without input"))?, - outputs: [outputs.next(), outputs.next()], - }), - Action::Instant => { - recipes.push(Recipe::Instant { - points: r.points.take().unwrap_or(0), - tile, - inputs: [inputs.next(), inputs.next()], - outputs: [outputs.next(), outputs.next()], - }); - } - Action::Demand => raw_demands.push(( - inputs.next().ok_or(anyhow!("demand needs inputs"))?, - outputs.next(), - r.duration.unwrap_or(10.), - )), - } - assert_eq!(inputs.next(), None, "{r2:?} inputs left over"); - assert_eq!(outputs.next(), None, "{r2:?} outputs left over"); - assert_eq!(r.points, None, "points specified where not possible") - } - - // TODO - // for d in demands_in { - // demands.push(Demand { - // from: reg.register_item(d.from), - // to: d.to.map(|to| reg.register_item(to)), - // duration: d.duration, - // points: d.points, - // }) - // } - - let mut chef_spawn = Vec2::new(0., 0.); - let mut customer_spawn = Vec2::new(0., 0.); - let mut initial_map = HashMap::new(); - let mut tiles_used = HashSet::new(); - let mut items_used = HashSet::new(); - for (y, line) in map_in.map.iter().enumerate() { - for (x, tile) in line.chars().enumerate() { - if tile == ' ' { - continue; // space is empty space - } - let pos = IVec2::new(x as i32, y as i32); - if tile == map_in.chef_spawn { - chef_spawn = pos.as_vec2() + Vec2::splat(0.5); - } - if tile == map_in.customer_spawn { - customer_spawn = pos.as_vec2() + Vec2::splat(0.5); - } - let tilename = map_in - .tiles - .get(&tile) - .ok_or(anyhow!("tile {tile} is undefined"))? - .clone(); - - let itemname = map_in.items.get(&tile).cloned(); - let tile = reg.register_tile(tilename); - let item = itemname.map(|i| reg.register_item(i)); - tiles_used.insert(tile); - if let Some(i) = item { - items_used.insert(i); - }; - initial_map.insert(pos, (tile, item)); - } - } - - for (y, line) in map_in.map.iter().enumerate() { - for (x, tile) in line.trim().chars().enumerate() { - let pos = IVec2::new(x as i32, y as i32); - if let Some(ent) = map_in.tile_entities.get(&tile) { - entities.push(construct_entity( - Some(pos), - ent, - ®, - &tiles_used, - &items_used, - &raw_demands, - &recipes, - &initial_map, - )?); - } - } - } - - entities.extend( - map_in - .entities - .iter() - .map(|decl| { - construct_entity( - None, - decl, - ®, - &tiles_used, - &items_used, - &raw_demands, - &recipes, - &initial_map, - ) - }) - .try_collect::>()?, - ); - - let item_names = reg.items.into_inner().unwrap(); - let tile_names = reg.tiles.into_inner().unwrap(); - let tile_collide = tile_names - .iter() - .map(|i| !map_in.walkable.contains(i)) - .collect(); - let tile_interact = tile_names - .iter() - .map(|i| !map_in.collider.contains(i) && !map_in.walkable.contains(i)) - .collect(); - - Ok(( - Gamedata { - current_map: map_name, - maps, - tile_collide, - tile_interact, - recipes, - item_names, - tile_names, - }, - Serverdata { - spec, - initial_map, - chef_spawn, - customer_spawn, - score_baseline: map_in.score_baseline, - }, - entities, - )) -} - -#[derive(Default)] -pub struct ItemTileRegistry { - tiles: RwLock>, - items: RwLock>, -} - -impl ItemTileRegistry { - pub fn register_tile(&self, name: String) -> TileIndex { - TileIndex(Self::register(&self.tiles, name)) - } - pub fn register_item(&self, name: String) -> ItemIndex { - ItemIndex(Self::register(&self.items, name)) - } - fn register(db: &RwLock>, name: String) -> usize { - let mut db = db.write().unwrap(); - if let Some(index) = db.iter().position(|e| e == &name) { - index - } else { - let index = db.len(); - db.push(name); - index - } - } -} diff --git a/server/src/data/demands.rs b/server/src/data/demands.rs new file mode 100644 index 00000000..176ca232 --- /dev/null +++ b/server/src/data/demands.rs @@ -0,0 +1,89 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +use super::Demand; +use hurrycurry_protocol::{ItemIndex, Recipe, TileIndex}; +use std::collections::{HashMap, HashSet}; + +pub fn generate_demands( + tiles: &HashSet, + items: &HashSet, + raw_demands: &[(ItemIndex, Option, f32)], + recipes: &[Recipe], +) -> Vec { + let recipes = recipes + .iter() + .filter(|r| r.tile().map(|t| tiles.contains(&t)).unwrap_or(true)) + .collect::>(); + + let mut producable = HashMap::new(); + + for i in items { + producable.insert(*i, 0.0); + } + + loop { + let prod_count = producable.len(); + + for r in &recipes { + let output_count = r.outputs().iter().filter(|o| !items.contains(o)).count(); + let Some(ingred_cost) = r + .inputs() + .iter() + .map(|i| producable.get(i).copied()) + .reduce(|a, b| { + if let (Some(a), Some(b)) = (a, b) { + Some(a + b) + } else { + None + } + }) + .unwrap_or(Some(0.)) + else { + continue; + }; + + let base_cost = match r { + Recipe::Passive { duration, .. } => 2. + duration * 0.1, + Recipe::Active { duration, .. } => 2. + duration, + Recipe::Instant { .. } => 1., + }; + + let output_cost = (ingred_cost + base_cost) / output_count as f32; + for o in r.outputs() { + let cost = producable.entry(o).or_insert(f32::INFINITY); + *cost = cost.min(output_cost); + } + } + + if prod_count == producable.len() { + break; + } + } + + raw_demands + .iter() + .filter_map(|(i, o, d)| { + producable.get(i).map(|cost| Demand { + from: *i, + to: *o, + duration: *d, + points: *cost as i64, + }) + }) + .collect() +} diff --git a/server/src/data/mod.rs b/server/src/data/mod.rs new file mode 100644 index 00000000..6555fbc4 --- /dev/null +++ b/server/src/data/mod.rs @@ -0,0 +1,341 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + Copyright 2024 nokoe + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +pub mod demands; + +use crate::entity::{construct_entity, Entities, EntityDecl}; +use anyhow::{anyhow, bail, Result}; +use demands::generate_demands; +use hurrycurry_protocol::{ + glam::{IVec2, Vec2}, + Gamedata, ItemIndex, MapMetadata, Recipe, TileIndex, +}; +use serde::{Deserialize, Serialize}; +use std::{ + collections::{HashMap, HashSet}, + fs::File, + path::PathBuf, + str::FromStr, + sync::{Mutex, RwLock}, +}; +use tokio::fs::read_to_string; + +#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)] +#[serde(rename_all = "snake_case")] +pub enum Action { + #[default] + Never, + Passive, + Active, + Instant, + Demand, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct RecipeDecl { + #[serde(default)] + tile: Option, + #[serde(default)] + inputs: Vec, + #[serde(default)] + outputs: Vec, + #[serde(default)] + action: Action, + #[serde(default)] + warn: bool, + #[serde(default)] + revert_duration: Option, + #[serde(default)] + duration: Option, + #[serde(default)] + points: Option, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct InitialMap { + map: Vec, + tiles: HashMap, + #[serde(default)] + items: HashMap, + collider: Vec, + walkable: Vec, + chef_spawn: char, + customer_spawn: char, + #[serde(default)] + entities: Vec, + #[serde(default)] + tile_entities: HashMap, + #[serde(default)] + score_baseline: i64, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DemandDecl { + from: String, + to: Option, + duration: f32, + points: i64, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Demand { + pub from: ItemIndex, + pub to: Option, + pub duration: f32, + pub points: i64, +} + +#[derive(Debug,Clone, Default)] +#[rustfmt::skip] +pub struct Serverdata { + pub demands: Vec, + pub spec: String, + pub initial_map: HashMap)>, + pub chef_spawn: Vec2, + pub customer_spawn: Vec2, + pub score_baseline: i64, +} + +#[derive(Debug, Deserialize, Default)] +pub struct DataIndex { + pub maps: HashMap, + pub recipes: HashSet, +} + +pub static DATA_DIR: Mutex> = Mutex::new(None); +fn data_dir() -> PathBuf { + DATA_DIR + .lock() + .unwrap() + .to_owned() + .unwrap_or_else(|| PathBuf::from_str("data").unwrap()) +} + +impl DataIndex { + pub fn reload(&mut self) -> Result<()> { + *self = serde_yml::from_reader(File::open(data_dir().join("index.yaml"))?)?; + Ok(()) + } + + pub async fn read_map(&self, name: &str) -> Result { + if !self.maps.contains_key(name) { + bail!("unknown map: {name:?}"); + } + let path = data_dir().join(format!("maps/{name}.yaml")); + Ok(read_to_string(path).await?) + } + pub async fn read_recipes(&self, name: &str) -> Result { + if !self.recipes.contains(name) { + bail!("unknown recipes: {name:?}"); + } + let path = data_dir().join(format!("recipes/{name}.yaml")); + Ok(read_to_string(path).await?) + } + + pub async fn generate(&self, spec: String) -> Result<(Gamedata, Serverdata, Entities)> { + let (map, recipes) = spec.split_once("-").unwrap_or((spec.as_str(), "default")); + + let map_in = serde_yml::from_str(&self.read_map(map).await?)?; + let recipes_in = serde_yml::from_str(&self.read_recipes(recipes).await?)?; + + Ok(build_data( + self.maps.clone(), + spec.clone(), + map.to_string(), + map_in, + recipes_in, + )?) + } +} + +pub fn build_data( + maps: HashMap, + spec: String, + map_name: String, + map_in: InitialMap, + recipes_in: Vec, +) -> Result<(Gamedata, Serverdata, Entities)> { + let reg = ItemTileRegistry::default(); + let mut recipes = Vec::new(); + let mut entities = Vec::new(); + let mut raw_demands = Vec::new(); + + for mut r in recipes_in { + let r2 = r.clone(); + let mut inputs = r.inputs.into_iter().map(|i| reg.register_item(i)); + let mut outputs = r.outputs.into_iter().map(|o| reg.register_item(o)); + let tile = r.tile.map(|t| reg.register_tile(t)); + match r.action { + Action::Never => {} + Action::Passive => recipes.push(Recipe::Passive { + duration: r.duration.ok_or(anyhow!("duration for passive missing"))?, + warn: r.warn, + tile, + revert_duration: r.revert_duration, + input: inputs + .next() + .ok_or(anyhow!("passive recipe without input"))?, + output: outputs.next(), + }), + Action::Active => recipes.push(Recipe::Active { + duration: r.duration.ok_or(anyhow!("duration for active missing"))?, + tile, + input: inputs + .next() + .ok_or(anyhow!("active recipe without input"))?, + outputs: [outputs.next(), outputs.next()], + }), + Action::Instant => { + recipes.push(Recipe::Instant { + points: r.points.take().unwrap_or(0), + tile, + inputs: [inputs.next(), inputs.next()], + outputs: [outputs.next(), outputs.next()], + }); + } + Action::Demand => raw_demands.push(( + inputs.next().ok_or(anyhow!("demand needs inputs"))?, + outputs.next(), + r.duration.unwrap_or(10.), + )), + } + assert_eq!(inputs.next(), None, "{r2:?} inputs left over"); + assert_eq!(outputs.next(), None, "{r2:?} outputs left over"); + assert_eq!(r.points, None, "points specified where not possible") + } + + // TODO + // for d in demands_in { + // demands.push(Demand { + // from: reg.register_item(d.from), + // to: d.to.map(|to| reg.register_item(to)), + // duration: d.duration, + // points: d.points, + // }) + // } + + let mut chef_spawn = Vec2::new(0., 0.); + let mut customer_spawn = Vec2::new(0., 0.); + let mut initial_map = HashMap::new(); + let mut tiles_used = HashSet::new(); + let mut items_used = HashSet::new(); + for (y, line) in map_in.map.iter().enumerate() { + for (x, tile) in line.chars().enumerate() { + if tile == ' ' { + continue; // space is empty space + } + let pos = IVec2::new(x as i32, y as i32); + if tile == map_in.chef_spawn { + chef_spawn = pos.as_vec2() + Vec2::splat(0.5); + } + if tile == map_in.customer_spawn { + customer_spawn = pos.as_vec2() + Vec2::splat(0.5); + } + let tilename = map_in + .tiles + .get(&tile) + .ok_or(anyhow!("tile {tile} is undefined"))? + .clone(); + + let itemname = map_in.items.get(&tile).cloned(); + let tile = reg.register_tile(tilename); + let item = itemname.map(|i| reg.register_item(i)); + tiles_used.insert(tile); + if let Some(i) = item { + items_used.insert(i); + }; + initial_map.insert(pos, (tile, item)); + } + } + + for (y, line) in map_in.map.iter().enumerate() { + for (x, tile) in line.trim().chars().enumerate() { + let pos = IVec2::new(x as i32, y as i32); + if let Some(ent) = map_in.tile_entities.get(&tile) { + entities.push(construct_entity(Some(pos), ent, ®)?); + } + } + } + + entities.extend( + map_in + .entities + .iter() + .map(|decl| construct_entity(None, decl, ®)) + .try_collect::>()?, + ); + + let demands = generate_demands(&tiles_used, &items_used, &raw_demands, &recipes); + + let item_names = reg.items.into_inner().unwrap(); + let tile_names = reg.tiles.into_inner().unwrap(); + let tile_collide = tile_names + .iter() + .map(|i| !map_in.walkable.contains(i)) + .collect(); + let tile_interact = tile_names + .iter() + .map(|i| !map_in.collider.contains(i) && !map_in.walkable.contains(i)) + .collect(); + + Ok(( + Gamedata { + current_map: map_name, + maps, + tile_collide, + tile_interact, + recipes, + item_names, + tile_names, + }, + Serverdata { + spec, + demands, + initial_map, + chef_spawn, + customer_spawn, + score_baseline: map_in.score_baseline, + }, + entities, + )) +} + +#[derive(Default)] +pub struct ItemTileRegistry { + tiles: RwLock>, + items: RwLock>, +} + +impl ItemTileRegistry { + pub fn register_tile(&self, name: String) -> TileIndex { + TileIndex(Self::register(&self.tiles, name)) + } + pub fn register_item(&self, name: String) -> ItemIndex { + ItemIndex(Self::register(&self.items, name)) + } + fn register(db: &RwLock>, name: String) -> usize { + let mut db = db.write().unwrap(); + if let Some(index) = db.iter().position(|e| e == &name) { + index + } else { + let index = db.len(); + db.push(name); + index + } + } +} diff --git a/server/src/entity/bot.rs b/server/src/entity/bot.rs index 06477e8a..301dbca3 100644 --- a/server/src/entity/bot.rs +++ b/server/src/entity/bot.rs @@ -1,13 +1,85 @@ -use super::{EntityContext, Entity}; +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +use super::{Entity, EntityContext}; use anyhow::Result; -use hurrycurry_bot::BotAlgo; +use hurrycurry_bot::{BotAlgo, DynBotAlgo}; +use hurrycurry_protocol::{PacketS, PlayerID}; +use log::info; +use rand::random; + +pub type DynBotDriver = BotDriver; -pub struct BotDriver { - algo: Box, +pub struct BotDriver { + algo: T, + join_data: Option<(String, i32)>, + id: PlayerID, + interacting: bool, + pub left: bool, } -impl Entity for BotDriver { +impl BotDriver { + pub fn new(name: String, character: i32, algo: T) -> Self { + Self { + algo, + id: PlayerID(0), + interacting: false, + join_data: Some((name, character)), + left: false, + } + } +} +impl Entity for BotDriver { fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + if let Some((name, character)) = self.join_data.take() { + self.id = PlayerID(random()); // TODO bad code, can collide + info!("spawn {:?} ({name:?}, {character})", self.id); + c.packet_in.push_back(PacketS::Join { + name, + character, + id: Some(self.id), + }) + } + + let input = self.algo.tick(self.id, &c.game, c.dt); + if input.leave { + info!("leave {:?}", self.id); + c.packet_in.push_back(PacketS::Leave { player: self.id }); + self.left = true; + return Ok(()); + } + if input.interact.is_some() != self.interacting { + self.interacting = input.interact.is_some(); + c.packet_in.push_back(PacketS::Interact { + player: self.id, + pos: input.interact, + }) + } + c.packet_in.push_back(PacketS::Movement { + player: self.id, + dir: input.direction, + boost: input.boost, + pos: None, + }); Ok(()) } + fn destructor(&mut self, c: EntityContext<'_>) { + if self.join_data.is_none() && !self.left { + c.packet_in.push_back(PacketS::Leave { player: self.id }) + } + } } diff --git a/server/src/entity/customers.rs b/server/src/entity/customers.rs new file mode 100644 index 00000000..a89d5687 --- /dev/null +++ b/server/src/entity/customers.rs @@ -0,0 +1,62 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +use super::{bot::BotDriver, Entity, EntityContext}; +use anyhow::Result; +use hurrycurry_bot::algos::Customer; +use rand::random; + +pub struct Customers { + customers: Vec>, + spawn_cooldown: f32, +} + +impl Customers { + pub fn new() -> Result { + Ok(Self { + customers: Default::default(), + spawn_cooldown: 0., + }) + } +} + +impl Entity for Customers { + fn tick(&mut self, c: EntityContext) -> Result<()> { + self.spawn_cooldown -= c.dt; + self.spawn_cooldown = self.spawn_cooldown.max(0.); + if self.customers.len() < 5 && self.spawn_cooldown <= 0. { + self.spawn_cooldown = 10. + random::() * 10.; + let bot = BotDriver::new( + fake::Fake::fake(&fake::faker::name::fr_fr::Name()), + -1 - (random::() as i32), + Customer::default(), + ); + self.customers.push(bot) + } + for bot in &mut self.customers { + bot.tick(EntityContext { + game: c.game, + packet_out: c.packet_out, + packet_in: c.packet_in, + score_changed: c.score_changed, + dt: c.dt, + })?; + } + self.customers.retain(|c| !c.left); + Ok(()) + } +} diff --git a/server/src/entity/customers/demands.rs b/server/src/entity/customers/demands.rs deleted file mode 100644 index 176ca232..00000000 --- a/server/src/entity/customers/demands.rs +++ /dev/null @@ -1,89 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright 2024 metamuffin - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, version 3 of the License only. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -*/ -use super::Demand; -use hurrycurry_protocol::{ItemIndex, Recipe, TileIndex}; -use std::collections::{HashMap, HashSet}; - -pub fn generate_demands( - tiles: &HashSet, - items: &HashSet, - raw_demands: &[(ItemIndex, Option, f32)], - recipes: &[Recipe], -) -> Vec { - let recipes = recipes - .iter() - .filter(|r| r.tile().map(|t| tiles.contains(&t)).unwrap_or(true)) - .collect::>(); - - let mut producable = HashMap::new(); - - for i in items { - producable.insert(*i, 0.0); - } - - loop { - let prod_count = producable.len(); - - for r in &recipes { - let output_count = r.outputs().iter().filter(|o| !items.contains(o)).count(); - let Some(ingred_cost) = r - .inputs() - .iter() - .map(|i| producable.get(i).copied()) - .reduce(|a, b| { - if let (Some(a), Some(b)) = (a, b) { - Some(a + b) - } else { - None - } - }) - .unwrap_or(Some(0.)) - else { - continue; - }; - - let base_cost = match r { - Recipe::Passive { duration, .. } => 2. + duration * 0.1, - Recipe::Active { duration, .. } => 2. + duration, - Recipe::Instant { .. } => 1., - }; - - let output_cost = (ingred_cost + base_cost) / output_count as f32; - for o in r.outputs() { - let cost = producable.entry(o).or_insert(f32::INFINITY); - *cost = cost.min(output_cost); - } - } - - if prod_count == producable.len() { - break; - } - } - - raw_demands - .iter() - .filter_map(|(i, o, d)| { - producable.get(i).map(|cost| Demand { - from: *i, - to: *o, - duration: *d, - points: *cost as i64, - }) - }) - .collect() -} diff --git a/server/src/entity/customers/mod.rs b/server/src/entity/customers/mod.rs deleted file mode 100644 index 5038eaf2..00000000 --- a/server/src/entity/customers/mod.rs +++ /dev/null @@ -1,276 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright 2024 metamuffin - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, version 3 of the License only. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -*/ -pub mod demands; -mod pathfinding; - -use super::{EntityContext, Entity}; -use crate::{data::Demand, server::Server}; -use anyhow::{anyhow, bail, Result}; -use fake::{faker, Fake}; -use hurrycurry_protocol::{glam::IVec2, DemandIndex, Message, PacketC, PacketS, PlayerID}; -use log::{info, warn}; -use pathfinding::{find_path, Path}; -use rand::{random, thread_rng}; -use std::collections::{HashMap, VecDeque}; - -#[derive(Debug, Clone)] -pub struct Customers { - demands: Vec, - cpackets: VecDeque, - chairs: HashMap, - customers: HashMap, - spawn_cooldown: f32, -} - -#[derive(Debug, Clone)] -enum CustomerState { - Entering { - path: Path, - chair: IVec2, - }, - Waiting { - demand: DemandIndex, - chair: IVec2, - timeout: f32, - }, - Eating { - demand: DemandIndex, - target: IVec2, - progress: f32, - chair: IVec2, - }, - Exiting { - path: Path, - }, -} - -impl Customers { - pub fn new(chairs: HashMap, demands: Vec) -> Result { - if demands.is_empty() { - bail!("one or more demands required for customers entity") - } - Ok(Self { - chairs, - customers: Default::default(), - demands, - spawn_cooldown: 0., - cpackets: VecDeque::new(), - }) - } -} - -impl Entity for Customers { - fn tick(&mut self, c: EntityContext) -> Result<()> { - // self.spawn_cooldown -= dt; - // self.spawn_cooldown = self.spawn_cooldown.max(0.); - // if self.customers.len() < 5 && self.spawn_cooldown <= 0. { - // self.spawn_cooldown = 10. + random::() * 10.; - // let id = game.join_player( - // faker::name::fr_fr::Name().fake(), - // -1 - (random::() as i32), - // packet_out, - // ); - - // let chair = self.select_chair().ok_or(anyhow!("no free chair found"))?; - // let from = game.data.customer_spawn.as_ivec2(); - // let path = find_path(&game.walkable, from, chair) - // .ok_or(anyhow!("no path from {from} to {chair}"))?; - // info!("{id:?} -> entering"); - // self.customers - // .insert(id, CustomerState::Entering { path, chair }); - // } - // let mut customers_to_remove = Vec::new(); - // for (&player, state) in &mut self.customers { - // let Some(playerdata) = game.players.get_mut(&player) else { - // continue; - // }; - - // match state { - // CustomerState::Entering { path, chair } => { - // playerdata - // .movement - // .input(path.next_direction(playerdata.position()), false); - // if path.is_done() { - // let demand = DemandIndex(random::() as usize % self.demands.len()); - // self.cpackets.push_back(PacketS::Communicate { - // message: Some(Message::Item(self.demands[demand.0].from)), - // persist: true, - // player, - // }); - // info!("{player:?} -> waiting"); - // *state = CustomerState::Waiting { - // chair: *chair, - // timeout: 90. + random::() * 60., - // demand, - // }; - // } - // } - // CustomerState::Waiting { - // chair, - // demand, - // timeout, - // } => { - // playerdata - // .movement - // .input((chair.as_vec2() + 0.5) - playerdata.position(), false); - // *timeout -= dt; - // if *timeout <= 0. { - // self.cpackets.push_back(PacketS::Communicate { - // message: None, - // persist: true, - // player, - // }); - // self.cpackets.push_back(PacketS::Communicate { - // message: Some(Message::Effect("angry".to_string())), - // persist: false, - // player, - // }); - // let path = find_path( - // &game.walkable, - // playerdata.position().as_ivec2(), - // game.data.customer_spawn.as_ivec2(), - // ) - // .expect("no path to exit"); - // *self.chairs.get_mut(chair).unwrap() = true; - // game.score.demands_failed += 1; - // game.score.points -= 1; - // game.score_changed = true; - // info!("{player:?} -> exiting"); - // *state = CustomerState::Exiting { path } - // } else { - // let demand_data = &self.demands[demand.0]; - // let demand_pos = [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y] - // .into_iter() - // .find_map(|off| { - // let pos = *chair + off; - // if game - // .tiles - // .get(&pos) - // .map(|t| { - // t.item - // .as_ref() - // .map(|i| i.kind == demand_data.from) - // .unwrap_or_default() - // }) - // .unwrap_or_default() - // { - // Some(pos) - // } else { - // None - // } - // }); - // if let Some(pos) = demand_pos { - // self.cpackets.push_back(PacketS::Communicate { - // persist: true, - // message: None, - // player, - // }); - // self.cpackets.push_back(PacketS::Communicate { - // message: Some(Message::Effect("satisfied".to_string())), - // persist: false, - // player, - // }); - // self.cpackets.push_back(PacketS::Interact { - // pos: Some(pos), - // player, - // }); - // self.cpackets - // .push_back(PacketS::Interact { pos: None, player }); - // info!("{player:?} -> eating"); - // *state = CustomerState::Eating { - // demand: *demand, - // target: pos, - // progress: 0., - // chair: *chair, - // } - // } - // } - // } - // CustomerState::Eating { - // demand, - // target, - // progress, - // chair, - // } => { - // playerdata - // .movement - // .input((chair.as_vec2() + 0.5) - playerdata.position(), false); - // let demand = &self.demands[demand.0]; - // *progress += dt / demand.duration; - // if *progress >= 1. { - // self.cpackets.push_back(PacketS::ReplaceHand { - // player, - // item: demand.to, - // }); - // if demand.to.is_some() { - // self.cpackets.push_back(PacketS::Interact { - // player, - // pos: Some(*target), - // }); - // self.cpackets - // .push_back(PacketS::Interact { player, pos: None }); - // } - // let path = find_path( - // &game.walkable, - // playerdata.position().as_ivec2(), - // game.data.customer_spawn.as_ivec2(), - // ) - // .ok_or(anyhow!("no path to exit"))?; - // *self.chairs.get_mut(chair).unwrap() = true; - // game.score.demands_completed += 1; - // game.score.points += demand.points; - // game.score_changed = true; - // info!("{player:?} -> exiting"); - // *state = CustomerState::Exiting { path } - // } - // } - // CustomerState::Exiting { path } => { - // playerdata - // .movement - // .input(path.next_direction(playerdata.position()), false); - // if path.is_done() { - // info!("{player:?} -> leave"); - // self.cpackets.push_back(PacketS::Leave { player }); - // customers_to_remove.push(player); - // } - // } - // } - // } - // for c in customers_to_remove { - // self.customers.remove(&c).unwrap(); - // } - // for packet in self.cpackets.drain(..) { - // if let Err(err) = game.packet_in(packet, &mut vec![], packet_out) { - // warn!("demand packet {err}"); - // } - // } - Ok(()) - } -} -impl Customers { - fn select_chair(&mut self) -> Option { - use rand::seq::IteratorRandom; - let (chosen, free) = self - .chairs - .iter_mut() - .filter(|(_p, free)| **free) - .choose(&mut thread_rng())?; - *free = false; - Some(*chosen) - } -} diff --git a/server/src/entity/customers/pathfinding.rs b/server/src/entity/customers/pathfinding.rs deleted file mode 100644 index 87ccf391..00000000 --- a/server/src/entity/customers/pathfinding.rs +++ /dev/null @@ -1,96 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright 2024 metamuffin - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, version 3 of the License only. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -*/ -use hurrycurry_protocol::glam::{IVec2, Vec2}; -use log::trace; -use std::{ - cmp::Ordering, - collections::{BinaryHeap, HashMap, HashSet}, -}; - -#[derive(Debug, Clone)] -pub struct Path(Vec); - -impl Path { - pub fn next_direction(&mut self, position: Vec2) -> Vec2 { - if let Some(next) = self.0.last().copied() { - trace!("next {next}"); - if next.distance(position) < if self.0.len() == 1 { 0.1 } else { 0.6 } { - self.0.pop(); - } - (next - position).normalize_or_zero() * 0.5 - } else { - Vec2::ZERO - } - } - pub fn is_done(&self) -> bool { - self.0.is_empty() - } -} - -pub fn find_path(walkable: &HashSet, from: IVec2, to: IVec2) -> Option { - #[derive(Debug, PartialEq, Eq)] - struct Open(i32, IVec2, IVec2, i32); - impl PartialOrd for Open { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.0.cmp(&other.0)) - } - } - impl Ord for Open { - fn cmp(&self, other: &Self) -> Ordering { - self.0.cmp(&other.0) - } - } - - let mut visited = HashMap::new(); - let mut open = BinaryHeap::new(); - open.push(Open(1, from, from, 0)); - - loop { - let Open(_, pos, f, distance) = open.pop()?; - if visited.contains_key(&pos) { - continue; - } - visited.insert(pos, f); - if pos == to { - break; - } - for dir in [IVec2::NEG_X, IVec2::NEG_Y, IVec2::X, IVec2::Y] { - let next = pos + dir; - if walkable.contains(&next) { - open.push(Open( - -(distance + next.distance_squared(to).isqrt()), - next, - pos, - distance + 1, - )); - } - } - } - - let mut path = Vec::new(); - let mut c = to; - loop { - path.push(c.as_vec2() + 0.5); - let cn = visited[&c]; - if cn == c { - break; - } - c = cn - } - Some(Path(path)) -} diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index efee6a6d..20370d0c 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -25,17 +25,17 @@ pub mod player_portal; use crate::data::ItemTileRegistry; use anyhow::{anyhow, Result}; use conveyor::Conveyor; -use customers::{demands::generate_demands, Customers}; +use customers::Customers; use environment_effect::{EnvironmentController, EnvironmentEffect, EnvironmentEffectController}; use hurrycurry_client_lib::Game; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, - ItemIndex, PacketC, PacketS, Recipe, TileIndex, + PacketC, PacketS, }; use item_portal::ItemPortal; use player_portal::PlayerPortal; use serde::{Deserialize, Serialize}; -use std::collections::{HashMap, HashSet, VecDeque}; +use std::collections::VecDeque; pub type DynEntity = Box; pub type Entities = Vec; @@ -104,11 +104,6 @@ pub fn construct_entity( pos: Option, decl: &EntityDecl, reg: &ItemTileRegistry, - tiles_used: &HashSet, - items_used: &HashSet, - raw_demands: &[(ItemIndex, Option, f32)], - recipes: &[Recipe], - initial_map: &HashMap)>, ) -> Result { Ok(match decl.to_owned() { EntityDecl::ItemPortal { from, to } => Box::new(ItemPortal { @@ -142,16 +137,7 @@ pub fn construct_entity( cooldown: 0., }) } - EntityDecl::Customers {} => { - let demands = generate_demands(tiles_used, items_used, raw_demands, recipes); - let chair = reg.register_tile("chair".to_string()); - let chairs = initial_map - .iter() - .filter(|(_, (tile, _))| *tile == chair) - .map(|(e, _)| (*e, true)) - .collect(); - Box::new(Customers::new(chairs, demands)?) - } + EntityDecl::Customers {} => Box::new(Customers::new()?), EntityDecl::EnvironmentEffect(config) => Box::new(EnvironmentEffectController::new(config)), EntityDecl::Environment(names) => Box::new(EnvironmentController(names)), }) diff --git a/server/src/server.rs b/server/src/server.rs index faac080f..49d0b461 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -40,6 +40,7 @@ pub struct ServerState { pub lobby: bool, pub player_id_counter: PlayerID, pub score_changed: bool, + pub packet_loopback: VecDeque, } pub struct Server<'a> { @@ -218,6 +219,7 @@ impl ServerState { entities: vec![], player_id_counter: PlayerID(1), score_changed: false, + packet_loopback: VecDeque::new(), } } } @@ -230,17 +232,25 @@ impl Server<'_> { ) { self.game.load(gamedata, &serverdata, timer, packet_out); self.state.data = serverdata.into(); + for mut e in self.state.entities.drain(..) { + e.destructor(EntityContext { + game: self.game, + packet_out, + packet_in: &mut self.state.packet_loopback, + score_changed: &mut self.state.score_changed, + dt: 0., + }); + } self.state.entities = entities; } pub fn join_player( &mut self, + id: PlayerID, name: String, character: i32, packet_out: &mut VecDeque, - ) -> PlayerID { - let id = self.state.player_id_counter; - self.state.player_id_counter.0 += 1; + ) { let position = if character < 0 { self.state.data.customer_spawn } else { @@ -274,7 +284,6 @@ impl Server<'_> { position, character, }); - id } pub fn packet_in( @@ -284,8 +293,17 @@ impl Server<'_> { packet_out: &mut VecDeque, ) -> Result<()> { match packet { - PacketS::Join { name, character } => { - let id = self.join_player(name, character, packet_out); + PacketS::Join { + name, + character, + id, + } => { + let id = id.unwrap_or_else(|| { + let id = self.state.player_id_counter; + self.state.player_id_counter.0 += 1; + id + }); + self.join_player(id, name, character, packet_out); replies.push(PacketC::Joined { id }) } PacketS::Leave { player } => { @@ -586,20 +604,22 @@ impl Server<'_> { ); } - let mut packet_in = VecDeque::new(); for entity in self.state.entities.iter_mut() { if let Err(e) = entity.tick(EntityContext { game: self.game, packet_out, score_changed: &mut self.state.score_changed, - packet_in: &mut packet_in, + packet_in: &mut self.state.packet_loopback, dt, }) { warn!("entity tick failed: {e}") } } - for p in packet_in.drain(..) { - let _ = self.packet_in(p, &mut vec![], packet_out); + + while let Some(p) = self.state.packet_loopback.pop_front() { + if let Err(e) = self.packet_in(p, &mut vec![], packet_out) { + warn!("internal packet errored: {e}"); + } } let now = Instant::now(); -- cgit v1.2.3-70-g09d2