aboutsummaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-09-30 01:19:01 +0200
committermetamuffin <metamuffin@disroot.org>2025-09-30 01:19:09 +0200
commit5033c326094edc1ff4234b994e95d987cb937fc4 (patch)
tree5fa426a77109722df163c15ce8d647170cd8fcea /server/src
parent727752b87bbe7146adb0f9e9e27d6e64b785ec2f (diff)
downloadhurrycurry-5033c326094edc1ff4234b994e95d987cb937fc4.tar
hurrycurry-5033c326094edc1ff4234b994e95d987cb937fc4.tar.bz2
hurrycurry-5033c326094edc1ff4234b994e95d987cb937fc4.tar.zst
Implement tile placeable items for server-side (#433)
Diffstat (limited to 'server/src')
-rw-r--r--server/src/data/index.rs39
-rw-r--r--server/src/data/mod.rs31
-rw-r--r--server/src/interaction.rs179
-rw-r--r--server/src/server.rs19
4 files changed, 125 insertions, 143 deletions
diff --git a/server/src/data/index.rs b/server/src/data/index.rs
deleted file mode 100644
index a9ffbb81..00000000
--- a/server/src/data/index.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- Hurry Curry! - a game about cooking
- Copyright (C) 2025 Hurry Curry! Contributors
-
- 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 <https://www.gnu.org/licenses/>.
-
-*/
-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() {
- if let Recipe::Passive { input, .. } = r {
- 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 d19b32a3..e5cd4552 100644
--- a/server/src/data/mod.rs
+++ b/server/src/data/mod.rs
@@ -16,7 +16,6 @@
*/
pub mod demands;
-pub mod index;
use crate::entity::{construct_entity, Entities, EntityDecl};
use anyhow::{anyhow, bail, Context, Result};
@@ -29,7 +28,7 @@ use hurrycurry_protocol::{
};
use serde::{Deserialize, Serialize};
use std::{
- collections::{HashMap, HashSet},
+ collections::{BTreeMap, HashMap, HashSet},
fs::{read_to_string, File},
path::PathBuf,
str::FromStr,
@@ -307,16 +306,24 @@ pub fn build_data(
maps.sort_unstable_by_key(|(_, m)| m.difficulty);
maps.sort_by_key(|(_, m)| m.players);
+ let mut tile_placeable_items = BTreeMap::new();
+ for tile_name in map_in.collider.iter().chain(map_in.walkable.iter()) {
+ let tile = reg.register_tile(tile_name.to_string());
+ let whitelist = recipes
+ .iter()
+ .filter(|r| r.tile() == Some(tile))
+ .flat_map(|e| e.inputs())
+ .collect();
+ tile_placeable_items.insert(tile, whitelist);
+ }
+ let tile_walkable = map_in
+ .walkable
+ .into_iter()
+ .map(|name| reg.register_tile(name))
+ .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();
let default_timer = if map_name.ends_with("lobby") {
None
@@ -329,8 +336,8 @@ pub fn build_data(
bot_algos,
current_map: map_name,
maps,
- tile_collide,
- tile_interact,
+ tile_walkable,
+ tile_placeable_items,
recipes,
item_names,
demands,
diff --git a/server/src/interaction.rs b/server/src/interaction.rs
index 624d9893..8f591db1 100644
--- a/server/src/interaction.rs
+++ b/server/src/interaction.rs
@@ -15,8 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use crate::data::index::GamedataIndex;
-use hurrycurry_client_lib::{Involvement, Item};
+use hurrycurry_client_lib::{gamedata_index::GamedataIndex, Involvement, Item};
use hurrycurry_protocol::{Gamedata, ItemLocation, PacketC, PlayerID, Recipe, Score, TileIndex};
use log::info;
use std::collections::VecDeque;
@@ -36,11 +35,11 @@ pub fn interact(
automated: bool,
packet_out: &mut VecDeque<PacketC>,
) {
- let interactable = automated
- || tile
- .map(|tile| data.is_tile_interactable(tile))
- .unwrap_or(true);
- if interactable && other.is_none() {
+ // let interactable = automated
+ // || tile
+ // .map(|tile| data.is_tile_interactable(tile))
+ // .unwrap_or(true);
+ if other.is_none() {
if let Some(item) = this {
if let Some(active) = &mut item.active {
let recipe = &data.recipe(active.recipe);
@@ -85,96 +84,102 @@ pub fn interact(
if !edge {
return;
}
- if interactable {
- for (ri, recipe) in data.recipes() {
- if !recipe.supports_tile(tile) {
- continue;
- }
- match recipe {
- Recipe::Active { input, speed, .. } => {
- if other.is_none() {
- if let Some(item) = this {
- if item.kind == *input && item.active.is_none() {
- info!("start active recipe {ri:?}");
- item.active = Some(Involvement {
- player,
- recipe: ri,
- speed: *speed,
- position: 0.,
- warn: false,
- });
- }
+ for (ri, recipe) in data.recipes() {
+ if !recipe.supports_tile(tile) {
+ continue;
+ }
+ match recipe {
+ Recipe::Active { input, speed, .. } => {
+ if other.is_none() {
+ if let Some(item) = this {
+ if item.kind == *input && item.active.is_none() {
+ info!("start active recipe {ri:?}");
+ item.active = Some(Involvement {
+ player,
+ recipe: ri,
+ speed: *speed,
+ position: 0.,
+ warn: false,
+ });
}
}
- if this.is_none() {
- if let Some(item) = &other {
- if item.kind == *input && item.active.is_none() {
- let mut item = other.take().unwrap();
- info!("start active recipe {ri:?}");
- item.active = Some(Involvement {
- player,
- recipe: ri,
- speed: *speed,
- position: 0.,
- warn: false,
- });
- *this = Some(item);
- score.active_recipes += 1;
- packet_out.push_back(PacketC::MoveItem {
- from: other_loc,
- to: this_loc,
- });
- packet_out.push_back(PacketC::SetProgress {
- player,
- item: this_loc,
- position: 0.,
- speed: *speed,
- warn: false,
- });
- return;
- }
+ }
+ if this.is_none() {
+ if let Some(item) = &other {
+ if item.kind == *input && item.active.is_none() {
+ let mut item = other.take().unwrap();
+ info!("start active recipe {ri:?}");
+ item.active = Some(Involvement {
+ player,
+ recipe: ri,
+ speed: *speed,
+ position: 0.,
+ warn: false,
+ });
+ *this = Some(item);
+ score.active_recipes += 1;
+ packet_out.push_back(PacketC::MoveItem {
+ from: other_loc,
+ to: this_loc,
+ });
+ packet_out.push_back(PacketC::SetProgress {
+ player,
+ item: this_loc,
+ position: 0.,
+ speed: *speed,
+ warn: false,
+ });
+ return;
}
}
}
- Recipe::Instant {
- inputs,
- outputs,
- points: pd,
- ..
- } => {
- let on_tile = this.as_ref().map(|i| i.kind);
- let in_hand = other.as_ref().map(|i| i.kind);
- let ok = inputs[0] == on_tile && inputs[1] == in_hand;
- let ok_rev = inputs[1] == on_tile && inputs[0] == in_hand;
- if ok || ok_rev {
- info!("instant recipe {ri:?} reversed={ok_rev}");
- let ok_rev = ok_rev as usize;
- let this_had_item = this.is_some();
- let other_had_item = other.is_some();
- *other = outputs[1 - ok_rev].map(|kind| Item { kind, active: None });
- *this = outputs[ok_rev].map(|kind| Item { kind, active: None });
- score.points += pd;
- score.instant_recipes += 1;
- *score_changed = true;
- produce(
- this_had_item,
- other_had_item,
- this,
- this_loc,
- other,
- other_loc,
- score_changed,
- packet_out,
- );
- return;
- }
+ }
+ Recipe::Instant {
+ inputs,
+ outputs,
+ points: pd,
+ ..
+ } => {
+ let on_tile = this.as_ref().map(|i| i.kind);
+ let in_hand = other.as_ref().map(|i| i.kind);
+ let ok = inputs[0] == on_tile && inputs[1] == in_hand;
+ let ok_rev = inputs[1] == on_tile && inputs[0] == in_hand;
+ if ok || ok_rev {
+ info!("instant recipe {ri:?} reversed={ok_rev}");
+ let ok_rev = ok_rev as usize;
+ let this_had_item = this.is_some();
+ let other_had_item = other.is_some();
+ *other = outputs[1 - ok_rev].map(|kind| Item { kind, active: None });
+ *this = outputs[ok_rev].map(|kind| Item { kind, active: None });
+ score.points += pd;
+ score.instant_recipes += 1;
+ *score_changed = true;
+ produce(
+ this_had_item,
+ other_had_item,
+ this,
+ this_loc,
+ other,
+ other_loc,
+ score_changed,
+ packet_out,
+ );
+ return;
}
- _ => (),
}
+ _ => (),
}
}
- if interactable && this.is_none() {
+ let can_place = tile.map_or(true, |tile| {
+ other.as_ref().map_or(false, |other| {
+ data.tile_placeable_items
+ .get(&tile)
+ .map_or(true, |pl| pl.contains(&other.kind))
+ })
+ });
+
+ if can_place && this.is_none() {
if let Some(item) = other.take() {
*this = Some(item);
packet_out.push_back(PacketC::MoveItem {
diff --git a/server/src/server.rs b/server/src/server.rs
index 57b26122..14244394 100644
--- a/server/src/server.rs
+++ b/server/src/server.rs
@@ -16,7 +16,7 @@
*/
use crate::{
- data::{index::GamedataIndex, DataIndex, Serverdata},
+ data::{DataIndex, Serverdata},
entity::{Entities, EntityContext},
interaction::{interact, tick_slot},
message::TrError,
@@ -24,7 +24,7 @@ use crate::{
tre, ConnectionID,
};
use anyhow::{Context, Result};
-use hurrycurry_client_lib::{Game, Involvement, Item, Player, Tile};
+use hurrycurry_client_lib::{gamedata_index::GamedataIndex, Game, Involvement, Item, Player, Tile};
use hurrycurry_protocol::{
glam::{IVec2, Vec2},
movement::MovementBase,
@@ -137,6 +137,7 @@ impl GameServerExt for Game {
self.lobby = gamedata.current_map == "lobby";
self.data = gamedata.into();
+ self.data_index.update(&self.data);
self.score = Score {
time_remaining: timer.map(|dur| dur.as_secs_f64()).unwrap_or(0.),
..Default::default()
@@ -154,7 +155,7 @@ impl GameServerExt for Game {
}),
},
);
- if !self.data.tile_collide[tile.0] {
+ if !self.data_index.tile_collide[tile.0] {
self.walkable.insert(p);
}
}
@@ -304,7 +305,7 @@ impl GameServerExt for Game {
) {
if let Some(kind) = kind {
self.tiles.insert(tile, Tile::from(kind));
- if self.data.is_tile_colliding(kind) {
+ if self.data_index.tile_collide[kind.0] {
self.walkable.remove(&tile);
} else {
self.walkable.insert(tile);
@@ -547,7 +548,15 @@ impl Server {
player.interacting = if edge { Some((pos, hand)) } else { None };
- let other_pid = if !self.game.data.is_tile_interactable(tile.kind) {
+ // Dont try interacting with player it tile is interactable
+ let other_pid = if !self
+ .game
+ .data
+ .tile_placeable_items
+ .get(&tile.kind)
+ .map_or(false, |p| !p.is_empty())
+ // TODO check for hand item
+ {
self.game
.players
.iter()