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