aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornokoe <nokoe@mailbox.org>2025-12-16 14:07:46 +0100
committernokoe <nokoe@mailbox.org>2025-12-16 14:10:38 +0100
commit6419d8c8139d697af309b7db2e698effa8290582 (patch)
treedba574ed1a72a3df41677fb14faf22503c018f68
parentf5468d2b46e46cdbd72b64febd3c1844b0ac4390 (diff)
downloadhurrycurry-6419d8c8139d697af309b7db2e698effa8290582.tar
hurrycurry-6419d8c8139d697af309b7db2e698effa8290582.tar.bz2
hurrycurry-6419d8c8139d697af309b7db2e698effa8290582.tar.zst
multi hand support in ctf
-rw-r--r--server/src/entity/ctf_minigame.rs109
1 files changed, 57 insertions, 52 deletions
diff --git a/server/src/entity/ctf_minigame.rs b/server/src/entity/ctf_minigame.rs
index 842081dc..f56e5648 100644
--- a/server/src/entity/ctf_minigame.rs
+++ b/server/src/entity/ctf_minigame.rs
@@ -36,25 +36,12 @@ struct TeamData {
}
impl TeamData {
- fn clear_hands(&mut self, c: &mut EntityContext) -> Result<(), TrError> {
+ fn effect(&self, c: &mut EntityContext, name: &str) -> Result<(), TrError> {
for pid in self.players.iter() {
- let player = c
- .game
- .players
- .get_mut(pid)
- .ok_or(TrError::Plain("Player is missing".to_string()))?;
- for (hand, item) in player
- .items
- .iter_mut()
- .enumerate()
- .filter(|(_, i)| i.is_some())
- {
- *item = None;
- c.packet_out.push_back(PacketC::SetItem {
- location: ItemLocation::Player(*pid, hurrycurry_protocol::Hand(hand)),
- item: None,
- });
- }
+ c.packet_out.push_back(PacketC::Effect2 {
+ name: name.to_string(),
+ location: ItemLocation::Player(*pid, hurrycurry_protocol::Hand(0)),
+ });
}
Ok(())
}
@@ -101,23 +88,6 @@ impl CtfMinigame {
Ok(())
}
- fn new_round(&mut self, c: &mut EntityContext) -> Result<(), TrError> {
- for (&pos, tile) in c.game.tiles.iter_mut() {
- if tile.item.is_some() {
- tile.item = None;
- c.packet_out.push_back(PacketC::SetItem {
- location: ItemLocation::Tile(pos),
- item: None,
- });
- }
- }
- for (item, team) in self.teams.iter_mut() {
- team.clear_hands(c)?;
- c.game.set_item(team.spawn, Some(*item));
- }
- Ok(())
- }
-
fn get_team(&mut self, pid: PlayerID) -> Result<ItemIndex, TrError> {
Ok(*self
.teams
@@ -129,6 +99,29 @@ impl CtfMinigame {
fn return_flag(&mut self, c: &mut EntityContext, team_idx: ItemIndex) -> Result<(), TrError> {
let team = self.teams.get_mut(&team_idx).unwrap();
+ for (&pos, tile) in c.game.tiles.iter_mut() {
+ if tile.item.as_ref().is_some_and(|a| a.kind == team_idx) {
+ tile.item = None;
+ c.packet_out.push_back(PacketC::SetItem {
+ location: ItemLocation::Tile(pos),
+ item: None,
+ });
+ }
+ }
+ for (pid, player) in c.game.players.iter_mut() {
+ for (hand, item) in player
+ .items
+ .iter_mut()
+ .enumerate()
+ .filter(|(_, i)| i.as_ref().is_some_and(|a| a.kind == team_idx))
+ {
+ *item = None;
+ c.packet_out.push_back(PacketC::SetItem {
+ location: ItemLocation::Player(*pid, hurrycurry_protocol::Hand(hand)),
+ item: None,
+ });
+ }
+ }
c.game.set_item(team.spawn, Some(team_idx));
Ok(())
}
@@ -148,6 +141,23 @@ impl CtfMinigame {
Ok(())
}
+ fn player_flags(
+ &mut self,
+ c: &mut EntityContext,
+ pid: PlayerID,
+ ) -> Result<Vec<ItemIndex>, TrError> {
+ Ok(c.game
+ .players
+ .get_mut(&pid)
+ .ok_or(TrError::Plain("Player is missing".to_string()))?
+ .items
+ .iter()
+ .flatten()
+ .map(|a| a.kind)
+ .filter(|a| self.teams.contains_key(a))
+ .collect::<Vec<_>>())
+ }
+
fn send_table(&self, c: &mut EntityContext) -> Result<(), TrError> {
let mut o = String::new();
let ti = self
@@ -247,9 +257,16 @@ impl Entity for CtfMinigame {
.and_then(|a| a.item.as_ref().map(|a| a.kind))
.is_some_and(|a| a == from_team_idx)
{
- self.teams.get_mut(&from_team_idx).unwrap().score += 10;
+ for i in self.player_flags(&mut c, from)? {
+ self.teams.get(&i).unwrap().effect(&mut c, "angry")?;
+ self.return_flag(&mut c, i)?;
+ self.teams.get_mut(&from_team_idx).unwrap().score += 10;
+ }
+ self.teams
+ .get(&from_team_idx)
+ .unwrap()
+ .effect(&mut c, "satisfied")?;
self.send_table(&mut c)?;
- self.new_round(&mut c)?;
}
Ok(true)
}
@@ -261,23 +278,11 @@ impl Entity for CtfMinigame {
let from_team_idx = self.get_team(from)?;
let to_team_idx = self.get_team(to)?;
if from_team_idx != to_team_idx {
- if c.game
- .players
- .get(&to)
- .ok_or(TrError::Plain("Player is missing".to_string()))?
- .items
- .iter()
- .flatten()
- .find(|i| i.kind == from_team_idx)
- .is_some()
- {
- self.teams
- .get_mut(&to_team_idx)
- .unwrap()
- .clear_hands(&mut c)?;
- self.return_flag(&mut c, from_team_idx)?;
+ for i in self.player_flags(&mut c, to)? {
+ self.return_flag(&mut c, i)?;
self.teams.get_mut(&from_team_idx).unwrap().score += 3;
}
+ self.send_table(&mut c)?;
self.return_player(&mut c, to)?;
Ok(true)
} else {