Skip to main content

nil_core/world/cheat/
military.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::bail_if_cheats_are_not_allowed;
5use crate::continent::Coord;
6use crate::error::Result;
7use crate::military::army::Army;
8use crate::military::army::personnel::ArmyPersonnel;
9use crate::ruler::Ruler;
10use crate::world::World;
11use itertools::Itertools;
12use nil_util::ops::TryElse;
13use tap::Pipe;
14
15impl World {
16  pub fn cheat_get_idle_armies_at(&self, coord: Coord) -> Result<Vec<Army>> {
17    bail_if_cheats_are_not_allowed!(self);
18    self
19      .military
20      .idle_armies_at(coord)
21      .cloned()
22      .collect_vec()
23      .pipe(Ok)
24  }
25
26  pub fn cheat_get_idle_personnel_at(&self, coord: Coord) -> Result<ArmyPersonnel> {
27    bail_if_cheats_are_not_allowed!(self);
28    self
29      .military
30      .fold_idle_personnel_at(coord)
31      .pipe(Ok)
32  }
33
34  pub fn cheat_spawn_personnel(
35    &mut self,
36    coord: Coord,
37    personnel: ArmyPersonnel,
38    ruler: Option<Ruler>,
39  ) -> Result<()> {
40    bail_if_cheats_are_not_allowed!(self);
41
42    let ruler = ruler.unwrap_or_try_else(|| {
43      let city = self.city(coord)?;
44      Ok(city.owner().clone())
45    })?;
46
47    let player = ruler.player().cloned();
48    self.military.spawn(coord, ruler, personnel);
49
50    if let Some(player) = player {
51      self.emit_military_updated(player);
52    }
53
54    Ok(())
55  }
56}