diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-10-19 23:50:23 +0200 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-10-19 23:50:23 +0200 |
| commit | ab83f982601d93b2399102c4d030fd6e13c4c735 (patch) | |
| tree | c0536ca9e328707d6b4f4cfc7a2307713466a5be /server/src/entity | |
| parent | 231a5ce21fcee9195fcc504ee672e4464d627c47 (diff) | |
| download | hurrycurry-ab83f982601d93b2399102c4d030fd6e13c4c735.tar hurrycurry-ab83f982601d93b2399102c4d030fd6e13c4c735.tar.bz2 hurrycurry-ab83f982601d93b2399102c4d030fd6e13c4c735.tar.zst | |
Refactor and move interaction code
Diffstat (limited to 'server/src/entity')
| -rw-r--r-- | server/src/entity/bot.rs | 3 | ||||
| -rw-r--r-- | server/src/entity/campaign.rs | 4 | ||||
| -rw-r--r-- | server/src/entity/conveyor.rs | 31 | ||||
| -rw-r--r-- | server/src/entity/customers.rs | 9 | ||||
| -rw-r--r-- | server/src/entity/environment_effect.rs | 3 | ||||
| -rw-r--r-- | server/src/entity/item_portal.rs | 32 | ||||
| -rw-r--r-- | server/src/entity/mod.rs | 4 | ||||
| -rw-r--r-- | server/src/entity/pedestrians.rs | 3 | ||||
| -rw-r--r-- | server/src/entity/player_portal.rs | 7 | ||||
| -rw-r--r-- | server/src/entity/tag_minigame.rs | 3 | ||||
| -rw-r--r-- | server/src/entity/tram.rs | 3 | ||||
| -rw-r--r-- | server/src/entity/tutorial.rs | 4 |
12 files changed, 42 insertions, 64 deletions
diff --git a/server/src/entity/bot.rs b/server/src/entity/bot.rs index 9627bb76..51a09b62 100644 --- a/server/src/entity/bot.rs +++ b/server/src/entity/bot.rs @@ -18,6 +18,7 @@ use super::{Entity, EntityContext}; use anyhow::Result; use hurrycurry_bot::{BotAlgo, DynBotAlgo}; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{Character, Hand, ItemLocation, PacketS, PlayerClass, PlayerID}; use log::debug; use std::any::Any; @@ -47,7 +48,7 @@ impl<T: BotAlgo + Any> Entity for BotDriver<T> { fn finished(&self) -> bool { self.left } - fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + fn tick(&mut self, c: EntityContext<'_>) -> Result<(), TrError> { if let Some((name, character, class)) = self.join_data.take() { self.id = c.game.get_unused_player_id(); // TODO clashes when multiple bots join in the same tick debug!("join {}", self.id); diff --git a/server/src/entity/campaign.rs b/server/src/entity/campaign.rs index 1966fc01..b7d7d8db 100644 --- a/server/src/entity/campaign.rs +++ b/server/src/entity/campaign.rs @@ -32,7 +32,7 @@ pub struct Map { } impl Entity for Map { - fn tick(&mut self, c: EntityContext) -> Result<()> { + fn tick(&mut self, c: EntityContext) -> Result<(), TrError> { let mut activate = false; c.game .players_spatial_index @@ -55,7 +55,7 @@ pub struct Gate { pub condition: GateCondition, } impl Entity for Gate { - fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + fn tick(&mut self, c: EntityContext<'_>) -> Result<(), TrError> { if self.active { self.active = false; self.unlocked = self.condition.check(c.scoreboard); diff --git a/server/src/entity/conveyor.rs b/server/src/entity/conveyor.rs index 9534b045..6757ed43 100644 --- a/server/src/entity/conveyor.rs +++ b/server/src/entity/conveyor.rs @@ -16,8 +16,8 @@ */ use super::{Entity, EntityContext}; -use crate::interaction::interact; -use anyhow::{Result, anyhow, bail}; +use anyhow::Result; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{ItemLocation, glam::IVec2}; #[derive(Debug, Clone)] @@ -29,12 +29,12 @@ pub struct Conveyor { } impl Entity for Conveyor { - fn tick(&mut self, c: EntityContext) -> Result<()> { + fn tick(&mut self, c: EntityContext) -> Result<(), TrError> { let from = c .game .tiles .get(&self.from) - .ok_or(anyhow!("conveyor from missing"))?; + .ok_or(TrError::Plain("conveyor from missing".into()))?; if from.item.is_some() { self.cooldown += c.dt; @@ -43,28 +43,11 @@ impl Entity for Conveyor { } self.cooldown = 0.; - if self.from == self.to { - bail!("conveyor does ends in itself") - } - let [Some(from), Some(to)] = c.game.tiles.get_disjoint_mut([&self.from, &self.to]) - else { - bail!("at least one conveyor end not on map"); - }; - - interact( - &c.game.data, - true, - Some(to.kind), - None, - &mut to.item, - ItemLocation::Tile(self.to), - &mut from.item, + c.game.interact( ItemLocation::Tile(self.from), - &mut c.game.score, - c.score_changed, + ItemLocation::Tile(self.to), true, - c.packet_out, - ); + )?; } Ok(()) diff --git a/server/src/entity/customers.rs b/server/src/entity/customers.rs index 1e795ac5..36afe24a 100644 --- a/server/src/entity/customers.rs +++ b/server/src/entity/customers.rs @@ -1,7 +1,3 @@ -use std::random::random; - -use crate::random_float; - /* Hurry Curry! - a game about cooking Copyright (C) 2025 Hurry Curry! Contributors @@ -20,9 +16,12 @@ use crate::random_float; */ use super::{Entity, EntityContext, bot::BotDriver}; +use crate::random_float; use anyhow::Result; use hurrycurry_bot::algos::{Customer, CustomerConfig}; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{Character, PlayerClass}; +use std::random::random; pub struct Customers { customers: Vec<BotDriver<Customer>>, @@ -43,7 +42,7 @@ impl Customers { } impl Entity for Customers { - fn tick(&mut self, c: EntityContext) -> Result<()> { + fn tick(&mut self, c: EntityContext) -> Result<(), TrError> { let chairs = *self.chair_count.get_or_insert_with(|| { c.game .tiles diff --git a/server/src/entity/environment_effect.rs b/server/src/entity/environment_effect.rs index b5344a27..e50a65ff 100644 --- a/server/src/entity/environment_effect.rs +++ b/server/src/entity/environment_effect.rs @@ -19,6 +19,7 @@ use crate::random_float; */ use super::{Entity, EntityContext}; use hurrycurry_data::entities::EnvironmentEffect; +use hurrycurry_locale::TrError; use hurrycurry_protocol::PacketC; use std::time::{Duration, Instant}; @@ -39,7 +40,7 @@ impl EnvironmentEffectController { } } impl Entity for EnvironmentEffectController { - fn tick(&mut self, c: EntityContext) -> anyhow::Result<()> { + fn tick(&mut self, c: EntityContext) -> Result<(), TrError> { if self.next_transition < Instant::now() { if self.active { self.next_transition += diff --git a/server/src/entity/item_portal.rs b/server/src/entity/item_portal.rs index f0472802..4335a659 100644 --- a/server/src/entity/item_portal.rs +++ b/server/src/entity/item_portal.rs @@ -16,8 +16,8 @@ */ use super::{Entity, EntityContext}; -use crate::interaction::interact; -use anyhow::{Result, bail}; +use anyhow::Result; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{ItemLocation, glam::IVec2}; #[derive(Debug, Default, Clone)] @@ -27,29 +27,19 @@ pub struct ItemPortal { } impl Entity for ItemPortal { - fn tick(&mut self, c: EntityContext) -> Result<()> { - if self.from == self.to { - bail!("item portal ends in itself") - } - let [Some(from), Some(to)] = c.game.tiles.get_disjoint_mut([&self.from, &self.to]) else { - bail!("at least one item portal end not on map"); - }; + fn tick(&mut self, c: EntityContext) -> Result<(), TrError> { + let from = c + .game + .tiles + .get(&self.from) + .ok_or(TrError::Plain("portal from missing".into()))?; if from.item.is_some() { - interact( - &c.game.data, - true, - Some(to.kind), - None, - &mut to.item, - ItemLocation::Tile(self.to), - &mut from.item, + c.game.interact( ItemLocation::Tile(self.from), - &mut c.game.score, - c.score_changed, + ItemLocation::Tile(self.to), true, - c.packet_out, - ); + )?; } Ok(()) diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 34815d0d..2e235c95 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -38,8 +38,8 @@ use campaign::{Gate, Map}; use conveyor::Conveyor; use customers::Customers; use environment_effect::{EnvironmentController, EnvironmentEffectController}; -use hurrycurry_game_core::Game; use hurrycurry_data::{Serverdata, entities::EntityDecl}; +use hurrycurry_game_core::Game; use hurrycurry_locale::TrError; use hurrycurry_protocol::{Character, ItemLocation, PacketC, PacketS, PlayerID}; use item_portal::ItemPortal; @@ -66,7 +66,7 @@ pub struct EntityContext<'a> { } pub trait Entity: Any { - fn tick(&mut self, _c: EntityContext<'_>) -> Result<()> { + fn tick(&mut self, _c: EntityContext<'_>) -> Result<(), TrError> { Ok(()) } fn finished(&self) -> bool { diff --git a/server/src/entity/pedestrians.rs b/server/src/entity/pedestrians.rs index edfe5ca3..0d33bfd0 100644 --- a/server/src/entity/pedestrians.rs +++ b/server/src/entity/pedestrians.rs @@ -19,6 +19,7 @@ use crate::random_gauss; */ use super::{Entity, EntityContext}; use anyhow::Result; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{Character, PacketS, PlayerClass, PlayerID, glam::Vec2}; use std::{collections::HashMap, random::random}; @@ -35,7 +36,7 @@ impl Entity for Pedestrians { fn finished(&self) -> bool { false } - fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + fn tick(&mut self, c: EntityContext<'_>) -> Result<(), TrError> { self.cooldown -= c.dt; if self.cooldown <= 0. && self.players.len() < 32 { let id = c.game.get_unused_player_id(); diff --git a/server/src/entity/player_portal.rs b/server/src/entity/player_portal.rs index 1717002a..a2653900 100644 --- a/server/src/entity/player_portal.rs +++ b/server/src/entity/player_portal.rs @@ -16,7 +16,8 @@ */ use super::{Entity, EntityContext}; -use anyhow::{Result, anyhow}; +use anyhow::Result; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{PacketC, glam::Vec2}; #[derive(Debug, Default, Clone)] @@ -26,7 +27,7 @@ pub struct PlayerPortal { } impl Entity for PlayerPortal { - fn tick(&mut self, c: EntityContext) -> Result<()> { + fn tick(&mut self, c: EntityContext) -> Result<(), TrError> { let mut players = Vec::new(); c.game .players_spatial_index @@ -37,7 +38,7 @@ impl Entity for PlayerPortal { .game .players .get_mut(&pid) - .ok_or(anyhow!("Player is missing"))?; + .ok_or(TrError::Plain("Player is missing".to_string()))?; p.movement.position = self.to; c.packet_out .push_back(PacketC::MovementSync { player: pid }); diff --git a/server/src/entity/tag_minigame.rs b/server/src/entity/tag_minigame.rs index a76b0511..089fd49e 100644 --- a/server/src/entity/tag_minigame.rs +++ b/server/src/entity/tag_minigame.rs @@ -18,6 +18,7 @@ use super::{Entity, EntityContext}; use anyhow::Result; use hurrycurry_game_core::{Item, Tile}; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{ Hand, ItemIndex, ItemLocation, Message, PacketC, PlayerID, TileIndex, glam::IVec2, }; @@ -49,7 +50,7 @@ impl TagMinigame { } } impl Entity for TagMinigame { - fn tick(&mut self, c: EntityContext) -> Result<()> { + fn tick(&mut self, c: EntityContext) -> Result<(), TrError> { if !self.init_done { self.init_done = true; // Hand out the item to some player(s) diff --git a/server/src/entity/tram.rs b/server/src/entity/tram.rs index 2d6aa8c1..20fe8120 100644 --- a/server/src/entity/tram.rs +++ b/server/src/entity/tram.rs @@ -17,6 +17,7 @@ */ use super::{Entity, EntityContext}; use anyhow::Result; +use hurrycurry_locale::TrError; use hurrycurry_protocol::{Character, PacketS, PlayerClass, PlayerID, glam::Vec2}; pub struct Tram { @@ -33,7 +34,7 @@ impl Entity for Tram { fn finished(&self) -> bool { false } - fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + fn tick(&mut self, c: EntityContext<'_>) -> Result<(), TrError> { if self.ids.len() < self.length { let id = c.game.get_unused_player_id(); c.packet_in.push_back(PacketS::Join { diff --git a/server/src/entity/tutorial.rs b/server/src/entity/tutorial.rs index 8cc896ac..46b2a0db 100644 --- a/server/src/entity/tutorial.rs +++ b/server/src/entity/tutorial.rs @@ -17,7 +17,7 @@ */ use super::{Entity, EntityContext}; use anyhow::Result; -use hurrycurry_locale::trm; +use hurrycurry_locale::{TrError, trm}; use hurrycurry_protocol::{ ItemIndex, Message, PacketC, PlayerID, Recipe, RecipeIndex, TileIndex, glam::IVec2, }; @@ -67,7 +67,7 @@ impl Entity for Tutorial { success: false, }); } - fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + fn tick(&mut self, c: EntityContext<'_>) -> Result<(), TrError> { if self.ended { return Ok(()); } |