Skip to main content

nil_core/world/cheat/
infrastructure.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::infrastructure::Infrastructure;
8use crate::infrastructure::building::academy::recruit_queue::AcademyRecruitQueue;
9use crate::infrastructure::building::prefecture::build_queue::PrefectureBuildQueue;
10use crate::infrastructure::building::stable::recruit_queue::StableRecruitQueue;
11use crate::infrastructure::building::{BuildingId, BuildingLevel};
12use crate::infrastructure::queue::InfrastructureQueue;
13use crate::infrastructure::storage::OverallStorageCapacity;
14use crate::ruler::Ruler;
15use crate::world::World;
16use itertools::Itertools;
17use strum::IntoEnumIterator;
18use tap::Pipe;
19
20impl World {
21  pub fn cheat_get_academy_recruit_queue(&self, coord: Coord) -> Result<AcademyRecruitQueue> {
22    bail_if_cheats_are_not_allowed!(self);
23    self
24      .city(coord)?
25      .infrastructure()
26      .academy()
27      .recruit_queue()
28      .clone()
29      .pipe(Ok)
30  }
31
32  pub fn cheat_get_academy_recruit_queues(
33    &self,
34    coords: &[Coord],
35    filter_empty: bool,
36  ) -> Result<Vec<(Coord, AcademyRecruitQueue)>> {
37    coords
38      .iter()
39      .copied()
40      .map(|coord| Ok((coord, self.cheat_get_academy_recruit_queue(coord)?)))
41      .filter_ok(|(_, queue)| !filter_empty || !queue.is_empty())
42      .try_collect()
43  }
44
45  pub fn cheat_get_all_academy_recruit_queues(
46    &self,
47    filter_empty: bool,
48  ) -> Result<Vec<(Coord, AcademyRecruitQueue)>> {
49    self
50      .continent
51      .city_coords()
52      .map(|coord| Ok((coord, self.cheat_get_academy_recruit_queue(coord)?)))
53      .filter_ok(|(_, queue)| !filter_empty || !queue.is_empty())
54      .try_collect()
55  }
56
57  pub fn cheat_get_all_prefecture_build_queues(
58    &self,
59    filter_empty: bool,
60  ) -> Result<Vec<(Coord, PrefectureBuildQueue)>> {
61    self
62      .continent
63      .city_coords()
64      .map(|coord| Ok((coord, self.cheat_get_prefecture_build_queue(coord)?)))
65      .filter_ok(|(_, queue)| !filter_empty || !queue.is_empty())
66      .try_collect()
67  }
68
69  pub fn cheat_get_all_stable_recruit_queues(
70    &self,
71    filter_empty: bool,
72  ) -> Result<Vec<(Coord, StableRecruitQueue)>> {
73    self
74      .continent
75      .city_coords()
76      .map(|coord| Ok((coord, self.cheat_get_stable_recruit_queue(coord)?)))
77      .filter_ok(|(_, queue)| !filter_empty || !queue.is_empty())
78      .try_collect()
79  }
80
81  pub fn cheat_get_infrastructure(&self, coord: Coord) -> Result<Infrastructure> {
82    bail_if_cheats_are_not_allowed!(self);
83    self
84      .city(coord)?
85      .infrastructure()
86      .clone()
87      .pipe(Ok)
88  }
89
90  pub fn cheat_get_prefecture_build_queue(&self, coord: Coord) -> Result<PrefectureBuildQueue> {
91    bail_if_cheats_are_not_allowed!(self);
92    self
93      .city(coord)?
94      .infrastructure()
95      .prefecture()
96      .build_queue()
97      .clone()
98      .pipe(Ok)
99  }
100
101  pub fn cheat_get_prefecture_build_queues(
102    &self,
103    coords: &[Coord],
104    filter_empty: bool,
105  ) -> Result<Vec<(Coord, PrefectureBuildQueue)>> {
106    coords
107      .iter()
108      .copied()
109      .map(|coord| Ok((coord, self.cheat_get_prefecture_build_queue(coord)?)))
110      .filter_ok(|(_, queue)| !filter_empty || !queue.is_empty())
111      .try_collect()
112  }
113
114  pub fn cheat_get_stable_recruit_queue(&self, coord: Coord) -> Result<StableRecruitQueue> {
115    bail_if_cheats_are_not_allowed!(self);
116    self
117      .city(coord)?
118      .infrastructure()
119      .stable()
120      .recruit_queue()
121      .clone()
122      .pipe(Ok)
123  }
124
125  pub fn cheat_get_stable_recruit_queues(
126    &self,
127    coords: &[Coord],
128    filter_empty: bool,
129  ) -> Result<Vec<(Coord, StableRecruitQueue)>> {
130    coords
131      .iter()
132      .copied()
133      .map(|coord| Ok((coord, self.cheat_get_stable_recruit_queue(coord)?)))
134      .filter_ok(|(_, queue)| !filter_empty || !queue.is_empty())
135      .try_collect()
136  }
137
138  pub fn cheat_get_storage_capacity(&self, ruler: Ruler) -> Result<OverallStorageCapacity> {
139    bail_if_cheats_are_not_allowed!(self);
140    self.get_storage_capacity(ruler)
141  }
142
143  pub fn cheat_set_max_infrastructure(&mut self, coord: Coord) -> Result<()> {
144    bail_if_cheats_are_not_allowed!(self);
145
146    let infrastructure = self.city_mut(coord)?.infrastructure_mut();
147
148    for id in BuildingId::iter() {
149      let building = infrastructure.building_mut(id);
150      building.set_level(building.max_level());
151    }
152
153    self.emit_city_updated(coord);
154
155    Ok(())
156  }
157
158  pub fn cheat_set_building_level(
159    &mut self,
160    coord: Coord,
161    id: BuildingId,
162    level: BuildingLevel,
163  ) -> Result<()> {
164    bail_if_cheats_are_not_allowed!(self);
165    self
166      .city_mut(coord)?
167      .infrastructure_mut()
168      .building_mut(id)
169      .set_level(level);
170
171    self.emit_city_updated(coord);
172
173    Ok(())
174  }
175}