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