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::index::ContinentKey;
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
16pub fn get_idle_armies_at(world: &World, key: impl ContinentKey) -> Result<Vec<Army>> {
17  bail_if_cheats_are_not_allowed!(world);
18  world
19    .military
20    .idle_armies_at(key)
21    .cloned()
22    .collect_vec()
23    .pipe(Ok)
24}
25
26pub fn get_idle_personnel_at(world: &World, key: impl ContinentKey) -> Result<ArmyPersonnel> {
27  bail_if_cheats_are_not_allowed!(world);
28  world
29    .military
30    .fold_idle_personnel_at(key)
31    .pipe(Ok)
32}
33
34pub fn get_maneuvers(world: &World) -> Result<Vec<Maneuver>> {
35  bail_if_cheats_are_not_allowed!(world);
36  world
37    .military
38    .maneuvers()
39    .cloned()
40    .collect_vec()
41    .pipe(Ok)
42}
43
44pub fn get_maneuvers_of(world: &World, ruler: impl Into<Ruler>) -> Result<Vec<Maneuver>> {
45  bail_if_cheats_are_not_allowed!(world);
46
47  let mut maneuvers = Vec::new();
48  for coord in world.continent.coords_of(ruler) {
49    maneuvers.extend(world.military.maneuvers_at(coord));
50  }
51
52  maneuvers
53    .into_iter()
54    .unique_by(|it| it.id())
55    .sorted_by_key(|it| it.id())
56    .cloned()
57    .collect_vec()
58    .pipe(Ok)
59}
60
61pub fn spawn_personnel(
62  world: &mut World,
63  key: impl ContinentKey,
64  personnel: ArmyPersonnel,
65  ruler: Option<Ruler>,
66) -> Result<()> {
67  bail_if_cheats_are_not_allowed!(world);
68
69  let coord = key.into_coord(world.continent.size())?;
70  let ruler = ruler.unwrap_or_try_else(|| {
71    let city = world.city(coord)?;
72    Ok(city.owner().clone())
73  })?;
74
75  let player = ruler.player().cloned();
76  world.military.spawn(coord, ruler, personnel);
77
78  if let Some(player) = player {
79    world.emit_military(player)?;
80  }
81
82  Ok(())
83}