Skip to main content

nil_core/military/squad/
size.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::military::unit::stats::power::Power;
5use crate::ranking::score::Score;
6use crate::resources::maintenance::Maintenance;
7use derive_more::{Deref, Display, From, Into};
8use serde::{Deserialize, Serialize};
9use std::cmp::Ordering;
10use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
11
12#[derive(
13  Clone,
14  Copy,
15  Debug,
16  Default,
17  Deref,
18  Display,
19  From,
20  Into,
21  PartialEq,
22  Eq,
23  PartialOrd,
24  Ord,
25  Deserialize,
26  Serialize,
27)]
28#[into(u32, f64)]
29pub struct SquadSize(u32);
30
31impl SquadSize {
32  #[inline]
33  pub const fn new(size: u32) -> Self {
34    Self(size)
35  }
36
37  #[inline]
38  pub fn random() -> Self {
39    Self::new(rand::random())
40  }
41
42  #[inline]
43  pub fn checked_sub(self, rhs: Self) -> Option<Self> {
44    self.0.checked_sub(rhs.0).map(Self::new)
45  }
46}
47
48impl PartialEq<u32> for SquadSize {
49  fn eq(&self, other: &u32) -> bool {
50    self.0.eq(other)
51  }
52}
53
54impl PartialOrd<u32> for SquadSize {
55  fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
56    self.0.partial_cmp(other)
57  }
58}
59
60impl Add for SquadSize {
61  type Output = SquadSize;
62
63  fn add(self, rhs: Self) -> Self::Output {
64    Self(self.0.saturating_add(rhs.0))
65  }
66}
67
68impl Add<u32> for SquadSize {
69  type Output = SquadSize;
70
71  fn add(self, rhs: u32) -> Self::Output {
72    Self(self.0.saturating_add(rhs))
73  }
74}
75
76impl AddAssign for SquadSize {
77  fn add_assign(&mut self, rhs: Self) {
78    *self = *self + rhs;
79  }
80}
81
82impl AddAssign<u32> for SquadSize {
83  fn add_assign(&mut self, rhs: u32) {
84    *self = *self + rhs;
85  }
86}
87
88impl From<f64> for SquadSize {
89  fn from(value: f64) -> Self {
90    Self::new(value as u32)
91  }
92}
93
94impl Sub for SquadSize {
95  type Output = SquadSize;
96
97  fn sub(self, rhs: Self) -> Self::Output {
98    Self(self.0.saturating_sub(rhs.0))
99  }
100}
101
102impl Sub<u32> for SquadSize {
103  type Output = SquadSize;
104
105  fn sub(self, rhs: u32) -> Self::Output {
106    Self(self.0.saturating_sub(rhs))
107  }
108}
109
110impl SubAssign for SquadSize {
111  fn sub_assign(&mut self, rhs: Self) {
112    *self = *self - rhs;
113  }
114}
115
116impl SubAssign<u32> for SquadSize {
117  fn sub_assign(&mut self, rhs: u32) {
118    *self = *self - rhs;
119  }
120}
121
122impl Mul<Maintenance> for SquadSize {
123  type Output = Maintenance;
124
125  fn mul(self, rhs: Maintenance) -> Self::Output {
126    let rhs = u32::from(rhs);
127    Maintenance::new(self.0.saturating_mul(rhs))
128  }
129}
130
131impl Mul<Power> for SquadSize {
132  type Output = Power;
133
134  fn mul(self, rhs: Power) -> Self::Output {
135    rhs * self.0
136  }
137}
138
139impl Mul<SquadSize> for Power {
140  type Output = Power;
141
142  fn mul(self, rhs: SquadSize) -> Self::Output {
143    self * rhs.0
144  }
145}
146
147impl Mul<Score> for SquadSize {
148  type Output = Score;
149
150  fn mul(self, rhs: Score) -> Self::Output {
151    let rhs = u32::from(rhs);
152    Score::new(self.0.saturating_mul(rhs))
153  }
154}
155
156impl Mul<f64> for SquadSize {
157  type Output = SquadSize;
158
159  fn mul(self, rhs: f64) -> Self::Output {
160    debug_assert!(rhs.is_finite());
161    debug_assert!(rhs.is_sign_positive());
162    debug_assert!(!rhs.is_subnormal());
163    Self((f64::from(self.0) * rhs).floor() as u32)
164  }
165}
166
167impl MulAssign<f64> for SquadSize {
168  fn mul_assign(&mut self, rhs: f64) {
169    *self = *self * rhs;
170  }
171}