nil_core/infrastructure/building/
mod.rs1pub mod academy;
5pub mod farm;
6pub mod iron_mine;
7pub mod prefecture;
8pub mod quarry;
9pub mod sawmill;
10pub mod silo;
11pub mod stable;
12pub mod wall;
13pub mod warehouse;
14pub mod workshop;
15
16use crate::error::{Error, Result};
17use crate::infrastructure::requirements::InfrastructureRequirements;
18use crate::ranking::score::Score;
19use crate::resources::prelude::*;
20use derive_more::{Deref, Into};
21use nil_num::growth::growth;
22use serde::{Deserialize, Serialize};
23use std::collections::HashMap;
24use std::ops::{Add, AddAssign, Sub, SubAssign};
25use std::{cmp, fmt};
26use strum::{Display, EnumIs, EnumIter};
27use subenum::subenum;
28
29pub trait Building: Send + Sync {
30 fn id(&self) -> BuildingId;
31
32 fn is_enabled(&self) -> bool;
34 fn toggle(&mut self, enabled: bool);
36
37 fn level(&self) -> BuildingLevel;
39 fn min_level(&self) -> BuildingLevel;
41 fn max_level(&self) -> BuildingLevel;
43 fn set_level(&mut self, level: BuildingLevel);
45
46 fn set_min_level(&mut self) {
48 self.set_level(self.min_level());
49 }
50
51 fn set_max_level(&mut self) {
53 self.set_level(self.max_level());
54 }
55
56 fn increase_level(&mut self) {
58 self.increase_level_by(1);
59 }
60
61 fn increase_level_by(&mut self, amount: u8);
63
64 fn decrease_level(&mut self) {
66 self.decrease_level_by(1);
67 }
68
69 fn decrease_level_by(&mut self, amount: u8);
71
72 fn is_min_level(&self) -> bool {
74 self.level() == self.min_level()
75 }
76
77 fn is_max_level(&self) -> bool {
79 self.level() >= self.max_level()
80 }
81
82 fn min_cost(&self) -> Cost;
84 fn max_cost(&self) -> Cost;
86 fn wood_ratio(&self) -> ResourceRatio;
88 fn stone_ratio(&self) -> ResourceRatio;
90 fn iron_ratio(&self) -> ResourceRatio;
92
93 fn maintenance(&self, stats: &BuildingStatsTable) -> Result<Maintenance>;
95 fn maintenance_ratio(&self) -> MaintenanceRatio;
97
98 fn min_workforce(&self) -> Workforce;
100 fn max_workforce(&self) -> Workforce;
102
103 fn score(&self, stats: &BuildingStatsTable) -> Result<Score>;
105 fn min_score(&self) -> Score;
107 fn max_score(&self) -> Score;
109
110 fn infrastructure_requirements(&self) -> &InfrastructureRequirements;
112
113 fn is_civil(&self) -> bool {
114 self.id().is_civil()
115 }
116
117 fn is_military(&self) -> bool {
118 self.id().is_military()
119 }
120
121 fn is_mine(&self) -> bool {
122 self.id().is_mine()
123 }
124
125 fn is_storage(&self) -> bool {
126 self.id().is_storage()
127 }
128}
129
130#[subenum(CivilBuildingId, MilitaryBuildingId, MineId, StorageId)]
131#[derive(
132 Clone, Copy, Debug, Display, EnumIs, EnumIter, PartialEq, Eq, Hash, Deserialize, Serialize,
133)]
134#[serde(rename_all = "kebab-case")]
135#[strum(serialize_all = "kebab-case")]
136pub enum BuildingId {
137 #[subenum(MilitaryBuildingId)]
138 Academy,
139
140 #[subenum(CivilBuildingId, MineId)]
141 Farm,
142
143 #[subenum(CivilBuildingId, MineId)]
144 IronMine,
145
146 #[subenum(CivilBuildingId)]
147 Prefecture,
148
149 #[subenum(CivilBuildingId, MineId)]
150 Quarry,
151
152 #[subenum(CivilBuildingId, MineId)]
153 Sawmill,
154
155 #[subenum(CivilBuildingId, StorageId)]
156 Silo,
157
158 #[subenum(MilitaryBuildingId)]
159 Stable,
160
161 Wall,
162
163 #[subenum(CivilBuildingId, StorageId)]
164 Warehouse,
165
166 #[subenum(MilitaryBuildingId)]
167 Workshop,
168}
169
170impl BuildingId {
171 #[inline]
172 pub fn is_civil(self) -> bool {
173 CivilBuildingId::try_from(self).is_ok()
174 }
175
176 #[inline]
177 pub fn is_military(self) -> bool {
178 MilitaryBuildingId::try_from(self).is_ok()
179 }
180
181 #[inline]
182 pub fn is_mine(self) -> bool {
183 MineId::try_from(self).is_ok()
184 }
185
186 #[inline]
187 pub fn is_storage(self) -> bool {
188 StorageId::try_from(self).is_ok()
189 }
190}
191
192#[derive(Clone, Debug, Deserialize, Serialize)]
194#[serde(rename_all = "camelCase")]
195pub struct BuildingStats {
196 pub level: BuildingLevel,
197 pub cost: Cost,
198 pub resources: Resources,
199 pub maintenance: Maintenance,
200 pub workforce: Workforce,
201 pub score: Score,
202}
203
204#[derive(Clone, Debug, Deserialize, Serialize)]
205#[serde(rename_all = "camelCase")]
206pub struct BuildingStatsTable {
207 id: BuildingId,
208 min_level: BuildingLevel,
209 max_level: BuildingLevel,
210 table: HashMap<BuildingLevel, BuildingStats>,
211}
212
213impl BuildingStatsTable {
214 pub(crate) fn new(building: &dyn Building) -> Self {
215 let min_level = building.min_level();
216 let max_level = building.max_level();
217 let mut table = HashMap::with_capacity((max_level.0).into());
218
219 let mut cost = f64::from(building.min_cost());
220 let cost_growth = growth()
221 .floor(cost)
222 .ceil(building.max_cost())
223 .max_level(max_level)
224 .call();
225
226 let mut workforce = f64::from(building.min_workforce());
227 let workforce_growth = growth()
228 .floor(workforce)
229 .ceil(building.max_workforce())
230 .max_level(max_level)
231 .call();
232
233 let mut score = f64::from(building.min_score());
234 let score_growth = growth()
235 .floor(score)
236 .ceil(building.max_score())
237 .max_level(max_level)
238 .call();
239
240 let wood_ratio = *building.wood_ratio();
241 let stone_ratio = *building.stone_ratio();
242 let iron_ratio = *building.iron_ratio();
243
244 let maintenance_ratio = *building.maintenance_ratio();
245 let mut maintenance = cost * maintenance_ratio;
246
247 for level in 1..=max_level.0 {
248 let level = BuildingLevel::new(level);
249 let resources = Resources {
250 food: Food::MIN,
251 iron: Iron::from((cost * iron_ratio).round()),
252 stone: Stone::from((cost * stone_ratio).round()),
253 wood: Wood::from((cost * wood_ratio).round()),
254 };
255
256 table.insert(
257 level,
258 BuildingStats {
259 level,
260 cost: Cost::from(cost.round()),
261 resources,
262 maintenance: Maintenance::from(maintenance.round()),
263 workforce: Workforce::from(workforce.round()),
264 score: Score::from(score.round()),
265 },
266 );
267
268 debug_assert!(cost.is_normal());
269 debug_assert!(workforce.is_normal());
270
271 debug_assert!(maintenance.is_finite());
272 debug_assert!(maintenance >= 0.0);
273
274 debug_assert!(score.is_finite());
275 debug_assert!(score >= 0.0);
276
277 cost += cost * cost_growth;
278 workforce += workforce * workforce_growth;
279 score += score * score_growth;
280
281 maintenance = cost * maintenance_ratio;
282 }
283
284 table.shrink_to_fit();
285
286 Self {
287 id: building.id(),
288 min_level,
289 max_level,
290 table,
291 }
292 }
293
294 #[inline]
295 pub fn id(&self) -> BuildingId {
296 self.id
297 }
298
299 #[inline]
300 pub fn min_level(&self) -> BuildingLevel {
301 self.min_level
302 }
303
304 #[inline]
305 pub fn max_level(&self) -> BuildingLevel {
306 self.max_level
307 }
308
309 #[inline]
310 pub fn get(&self, level: BuildingLevel) -> Result<&BuildingStats> {
311 self
312 .table
313 .get(&level)
314 .ok_or(Error::BuildingStatsNotFoundForLevel(self.id, level))
315 }
316}
317
318#[derive(
319 Clone,
320 Copy,
321 Debug,
322 Default,
323 Deref,
324 Into,
325 PartialEq,
326 Eq,
327 PartialOrd,
328 Ord,
329 Hash,
330 Deserialize,
331 Serialize,
332)]
333#[into(u8, u16, u32, u64, usize, f64)]
334pub struct BuildingLevel(u8);
335
336impl BuildingLevel {
337 pub const ZERO: BuildingLevel = BuildingLevel(0);
338
339 #[inline]
340 pub const fn new(level: u8) -> Self {
341 Self(level)
342 }
343}
344
345impl PartialEq<u8> for BuildingLevel {
346 fn eq(&self, other: &u8) -> bool {
347 self.0.eq(other)
348 }
349}
350
351impl PartialOrd<u8> for BuildingLevel {
352 fn partial_cmp(&self, other: &u8) -> Option<cmp::Ordering> {
353 self.0.partial_cmp(other)
354 }
355}
356
357impl Add for BuildingLevel {
358 type Output = Self;
359
360 fn add(self, rhs: Self) -> Self {
361 Self(self.0.saturating_add(rhs.0))
362 }
363}
364
365impl Add<u8> for BuildingLevel {
366 type Output = Self;
367
368 fn add(self, rhs: u8) -> Self {
369 Self(self.0.saturating_add(rhs))
370 }
371}
372
373impl Add<i8> for BuildingLevel {
374 type Output = Self;
375
376 fn add(self, rhs: i8) -> Self {
377 Self(self.0.saturating_add_signed(rhs))
378 }
379}
380
381impl AddAssign for BuildingLevel {
382 fn add_assign(&mut self, rhs: Self) {
383 *self = *self + rhs;
384 }
385}
386
387impl AddAssign<u8> for BuildingLevel {
388 fn add_assign(&mut self, rhs: u8) {
389 *self = *self + rhs;
390 }
391}
392
393impl AddAssign<i8> for BuildingLevel {
394 fn add_assign(&mut self, rhs: i8) {
395 *self = *self + rhs;
396 }
397}
398
399impl Sub for BuildingLevel {
400 type Output = Self;
401
402 fn sub(self, rhs: Self) -> Self {
403 Self(self.0.saturating_sub(rhs.0))
404 }
405}
406
407impl Sub<u8> for BuildingLevel {
408 type Output = Self;
409
410 fn sub(self, rhs: u8) -> Self {
411 Self(self.0.saturating_sub(rhs))
412 }
413}
414
415impl Sub<i8> for BuildingLevel {
416 type Output = Self;
417
418 fn sub(self, rhs: i8) -> Self {
419 Self(self.0.saturating_sub_signed(rhs))
420 }
421}
422
423impl SubAssign for BuildingLevel {
424 fn sub_assign(&mut self, rhs: Self) {
425 *self = *self - rhs;
426 }
427}
428
429impl SubAssign<u8> for BuildingLevel {
430 fn sub_assign(&mut self, rhs: u8) {
431 *self = *self - rhs;
432 }
433}
434
435impl SubAssign<i8> for BuildingLevel {
436 fn sub_assign(&mut self, rhs: i8) {
437 *self = *self - rhs;
438 }
439}
440
441impl fmt::Display for BuildingLevel {
442 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
443 write!(f, "{}", self.0)
444 }
445}
446
447#[macro_export]
449macro_rules! lv {
450 ($level:expr) => {
451 const { $crate::infrastructure::building::BuildingLevel::new($level) }
452 };
453}
454
455#[macro_export]
457macro_rules! with_random_level {
458 ($building:ident) => {{ $crate::infrastructure::prelude::$building::with_random_level() }};
459 ($building:ident, $max:expr) => {{
460 $crate::infrastructure::prelude::$building::with_random_level_in()
461 .max($crate::lv!($max))
462 .call()
463 }};
464 ($building:ident, $min:expr, $max:expr) => {{
465 $crate::infrastructure::prelude::$building::with_random_level_in()
466 .min($crate::lv!($min))
467 .max($crate::lv!($max))
468 .call()
469 }};
470}