From a24dd4b81ad8f87d34f1c55efa17cd3840d2ac5e Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 16 Sep 2024 15:33:52 +0200 Subject: generate index for passive recipes by input for faster lookup --- server/src/data/index.rs | 25 +++++++++++++++++++++++ server/src/data/mod.rs | 1 + server/src/interaction.rs | 51 +++++++++++++++++++++++++++-------------------- server/src/server.rs | 13 ++++++++---- 4 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 server/src/data/index.rs (limited to 'server/src') diff --git a/server/src/data/index.rs b/server/src/data/index.rs new file mode 100644 index 00000000..1c206d83 --- /dev/null +++ b/server/src/data/index.rs @@ -0,0 +1,25 @@ +use hurrycurry_protocol::{Gamedata, ItemIndex, Recipe, RecipeIndex}; +use std::collections::HashMap; + +#[derive(Debug, Default)] +pub struct GamedataIndex { + pub recipe_passive_by_input: HashMap>, +} + +impl GamedataIndex { + pub fn update(&mut self, data: &Gamedata) { + self.recipe_passive_by_input.clear(); + + for (ri, r) in data.recipes() { + match r { + Recipe::Passive { input, .. } => { + self.recipe_passive_by_input + .entry(*input) + .or_default() + .push(ri); + } + _ => (), + } + } + } +} diff --git a/server/src/data/mod.rs b/server/src/data/mod.rs index 12d4f23e..246f695e 100644 --- a/server/src/data/mod.rs +++ b/server/src/data/mod.rs @@ -17,6 +17,7 @@ */ pub mod demands; +pub mod index; use crate::entity::{construct_entity, Entities, EntityDecl}; use anyhow::{anyhow, bail, Result}; diff --git a/server/src/interaction.rs b/server/src/interaction.rs index a8f80bd8..0721fa81 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -20,6 +20,8 @@ use hurrycurry_protocol::{Gamedata, ItemLocation, PacketC, PlayerID, Recipe, Sco use log::info; use std::collections::VecDeque; +use crate::data::index::GamedataIndex; + pub fn interact( data: &Gamedata, edge: bool, @@ -206,9 +208,11 @@ pub enum TickEffect { ClearProgress, Produce, } + pub fn tick_slot( dt: f32, data: &Gamedata, + data_index: &GamedataIndex, tile: Option, slot: &mut Option, slot_loc: ItemLocation, @@ -272,28 +276,31 @@ pub fn tick_slot( return; } } else { - for (ri, recipe) in data.recipes() { - if recipe.supports_tile(tile) { - if let Recipe::Passive { - input, warn, speed, .. - } = recipe - { - if *input == item.kind { - item.active = Some(Involvement { - player: None, - recipe: ri, - position: 0., - warn: *warn, - speed: *speed, - }); - packet_out.push_back(PacketC::SetProgress { - player: None, - position: 0., - speed: *speed, - warn: *warn, - item: slot_loc, - }); - return; + if let Some(recipes) = data_index.recipe_passive_by_input.get(&item.kind) { + for &ri in recipes { + let recipe = data.recipe(ri); + if recipe.supports_tile(tile) { + if let Recipe::Passive { + input, warn, speed, .. + } = recipe + { + if *input == item.kind { + item.active = Some(Involvement { + player: None, + recipe: ri, + position: 0., + warn: *warn, + speed: *speed, + }); + packet_out.push_back(PacketC::SetProgress { + player: None, + position: 0., + speed: *speed, + warn: *warn, + item: slot_loc, + }); + return; + } } } } diff --git a/server/src/server.rs b/server/src/server.rs index 6a310254..fd7db5b5 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -16,7 +16,7 @@ */ use crate::{ - data::{DataIndex, Serverdata}, + data::{index::GamedataIndex, DataIndex, Serverdata}, entity::{Entities, EntityContext}, interaction::{interact, tick_slot}, scoreboard::ScoreboardStore, @@ -52,6 +52,7 @@ pub struct Server { pub tx: Sender, pub connections: HashMap>, pub scoreboard: ScoreboardStore, + pub gamedata_index: GamedataIndex, } pub trait GameServerExt { @@ -304,7 +305,8 @@ impl Server { packet_out: VecDeque::new(), connections: HashMap::new(), data: Serverdata::default().into(), - entities: vec![], + gamedata_index: GamedataIndex::default(), + entities: Vec::new(), player_id_counter: PlayerID(1), score_changed: false, packet_loopback: VecDeque::new(), @@ -321,8 +323,6 @@ impl Server { (gamedata, serverdata, entities): (Gamedata, Serverdata, Entities), timer: Option, ) { - self.game - .load(gamedata, &serverdata, timer, &mut self.packet_out); for mut e in self.entities.drain(..) { e.destructor(EntityContext { game: &mut self.game, @@ -334,6 +334,9 @@ impl Server { load_map: &mut None, }); } + self.game + .load(gamedata, &serverdata, timer, &mut self.packet_out); + self.gamedata_index.update(&self.game.data); self.data = serverdata.into(); self.entities = entities; } @@ -579,6 +582,7 @@ impl Server { tick_slot( dt, &self.game.data, + &self.gamedata_index, Some(tile.kind), &mut tile.item, ItemLocation::Tile(pos), @@ -618,6 +622,7 @@ impl Server { tick_slot( dt, &self.game.data, + &self.gamedata_index, None, &mut player.item, ItemLocation::Player(pid), -- cgit v1.2.3-70-g09d2