aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-09-16 15:33:52 +0200
committermetamuffin <metamuffin@disroot.org>2024-09-16 15:34:01 +0200
commita24dd4b81ad8f87d34f1c55efa17cd3840d2ac5e (patch)
tree7ffdf92a44f73d6f7c5db4a30f2254117dc8f49b /server
parent8f949e36143eabd8b176dbd16c3d90045d256a74 (diff)
downloadhurrycurry-a24dd4b81ad8f87d34f1c55efa17cd3840d2ac5e.tar
hurrycurry-a24dd4b81ad8f87d34f1c55efa17cd3840d2ac5e.tar.bz2
hurrycurry-a24dd4b81ad8f87d34f1c55efa17cd3840d2ac5e.tar.zst
generate index for passive recipes by input for faster lookup
Diffstat (limited to 'server')
-rw-r--r--server/src/data/index.rs25
-rw-r--r--server/src/data/mod.rs1
-rw-r--r--server/src/interaction.rs51
-rw-r--r--server/src/server.rs13
4 files changed, 64 insertions, 26 deletions
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<ItemIndex, Vec<RecipeIndex>>,
+}
+
+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<TileIndex>,
slot: &mut Option<Item>,
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<PacketC>,
pub connections: HashMap<ConnectionID, HashSet<PlayerID>>,
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<Duration>,
) {
- 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),