Skip to main content

nil_core/resources/
workforce.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::city::stability::Stability;
5use crate::infrastructure::building::Building;
6use crate::infrastructure::building::level::BuildingLevel;
7use crate::world::config::WorldConfig;
8use nil_num::impl_mul_ceil;
9use nil_num::mul_ceil::MulCeil;
10use nil_util::{ConstDeref, F64Math};
11use serde::{Deserialize, Serialize};
12use std::cmp::Ordering;
13use std::num::NonZeroU32;
14use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
15
16/// Workforce is a special resource used to construct buildings and recruit troops.
17/// The amount generated per round will always be equal to the level of the relevant
18/// building multiplied by the world speed.
19///
20/// Unlike other resources, workforce should never accumulate for the next round.
21/// Anything that is not used should be discarded.
22#[derive(Copy, Debug, Deserialize, Serialize, ConstDeref, F64Math)]
23#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
24#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
25pub struct Workforce(u32);
26
27impl Workforce {
28  #[inline]
29  pub const fn new(value: u32) -> Self {
30    Self(value)
31  }
32}
33
34impl const From<u32> for Workforce {
35  fn from(value: u32) -> Self {
36    Workforce::new(value)
37  }
38}
39
40impl const From<Workforce> for u32 {
41  fn from(value: Workforce) -> Self {
42    value.0
43  }
44}
45
46impl const From<Workforce> for f64 {
47  fn from(value: Workforce) -> Self {
48    f64::from(value.0)
49  }
50}
51
52impl const From<BuildingLevel> for Workforce {
53  fn from(value: BuildingLevel) -> Self {
54    Workforce(u32::from(value))
55  }
56}
57
58impl const From<f64> for Workforce {
59  fn from(value: f64) -> Self {
60    debug_assert!(value >= 0.0);
61    debug_assert!(value.is_finite());
62    Self::new(value.ceil() as u32)
63  }
64}
65
66impl const PartialEq<u32> for Workforce {
67  fn eq(&self, other: &u32) -> bool {
68    self.0.eq(other)
69  }
70}
71
72impl const PartialOrd<u32> for Workforce {
73  fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
74    self.0.partial_cmp(other)
75  }
76}
77
78impl const Add for Workforce {
79  type Output = Workforce;
80
81  fn add(self, rhs: Self) -> Self::Output {
82    Self(self.0.saturating_add(rhs.0))
83  }
84}
85
86impl const AddAssign for Workforce {
87  fn add_assign(&mut self, rhs: Self) {
88    *self = *self + rhs;
89  }
90}
91
92impl const Sub for Workforce {
93  type Output = Workforce;
94
95  fn sub(self, rhs: Self) -> Self::Output {
96    Self(self.0.saturating_sub(rhs.0))
97  }
98}
99
100impl const SubAssign for Workforce {
101  fn sub_assign(&mut self, rhs: Self) {
102    *self = *self - rhs;
103  }
104}
105
106impl const Mul for Workforce {
107  type Output = Workforce;
108
109  fn mul(self, rhs: Workforce) -> Self::Output {
110    Self(self.0.saturating_mul(rhs.0))
111  }
112}
113
114impl const Mul<u32> for Workforce {
115  type Output = Workforce;
116
117  fn mul(self, rhs: u32) -> Self::Output {
118    Self(self.0.saturating_mul(rhs))
119  }
120}
121
122impl const Mul<NonZeroU32> for Workforce {
123  type Output = Workforce;
124
125  fn mul(self, rhs: NonZeroU32) -> Self::Output {
126    self * rhs.get()
127  }
128}
129
130impl const Mul<Stability> for Workforce {
131  type Output = Workforce;
132
133  fn mul(self, rhs: Stability) -> Self::Output {
134    Self::from(self.mul_ceil(*rhs))
135  }
136}
137
138impl const MulAssign for Workforce {
139  fn mul_assign(&mut self, rhs: Self) {
140    *self = *self * rhs;
141  }
142}
143
144impl const MulAssign<Stability> for Workforce {
145  fn mul_assign(&mut self, rhs: Stability) {
146    *self = *self * rhs;
147  }
148}
149
150impl_mul_ceil!(Workforce);
151
152/// A building that generates workforce, such as the prefecture.
153pub trait WorkforceSource: Building {
154  fn workforce(&self, config: &WorldConfig) -> Workforce {
155    Workforce::from(f64::from(self.level()) * config.speed())
156  }
157}