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::military::maneuver::Maneuver;
10use crate::ruler::Ruler;
11use crate::world::World;
12use itertools::Itertools;
13use nil_util::ops::TryExt;
14use tap::Pipe;
15
16impl World {
17  pub fn cheat_get_idle_armies_at(&self, coord: Coord) -> Result<Vec<Army>> {
18    bail_if_cheats_are_not_allowed!(self);
19    self
20      .military
21      .idle_armies_at(coord)
22      .cloned()
23      .collect_vec()
24      .pipe(Ok)
25  }
26
27  pub fn cheat_get_idle_personnel_at(&self, coord: Coord) -> Result<ArmyPersonnel> {
28    bail_if_cheats_are_not_allowed!(self);
29    self
30      .military
31      .fold_idle_personnel_at(coord)
32      .pipe(Ok)
33  }
34
35  pub fn cheat_get_maneuvers(&self) -> Result<Vec<Maneuver>> {
36    bail_if_cheats_are_not_allowed!(self);
37    self
38      .military
39      .maneuvers()
40      .cloned()
41      .collect_vec()
42      .pipe(Ok)
43  }
44
45  pub fn cheat_get_maneuvers_of(&self, ruler: Ruler) -> Result<Vec<Maneuver>> {
46    bail_if_cheats_are_not_allowed!(self);
47
48    let mut maneuvers = Vec::new();
49    for coord in self.continent.coords_of(ruler) {
50      maneuvers.extend(self.military.maneuvers_at(coord));
51    }
52
53    maneuvers
54      .into_iter()
55      .unique_by(|it| it.id())
56      .sorted_by_key(|it| it.id())
57      .cloned()
58      .collect_vec()
59      .pipe(Ok)
60  }
61
62  pub fn cheat_spawn_personnel(
63    &mut self,
64    coord: Coord,
65    personnel: ArmyPersonnel,
66    ruler: Option<Ruler>,
67  ) -> Result<()> {
68    bail_if_cheats_are_not_allowed!(self);
69
70    let ruler = ruler.unwrap_or_try_else(|| {
71      let city = self.city(coord)?;
72      Ok(city.owner().clone())
73    })?;
74
75    let player = ruler.player().cloned();
76    self.military.spawn(coord, ruler, personnel);
77
78    if let Some(player) = player {
79      self.emit_military_updated(player)?;
80    }
81
82    Ok(())
83  }
84}