nil_core/military/squad/
size.rs1use 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 debug_assert!(value >= 0.0);
91 debug_assert!(value.is_finite());
92 Self::new(value as u32)
93 }
94}
95
96impl Sub for SquadSize {
97 type Output = SquadSize;
98
99 fn sub(self, rhs: Self) -> Self::Output {
100 Self(self.0.saturating_sub(rhs.0))
101 }
102}
103
104impl Sub<u32> for SquadSize {
105 type Output = SquadSize;
106
107 fn sub(self, rhs: u32) -> Self::Output {
108 Self(self.0.saturating_sub(rhs))
109 }
110}
111
112impl SubAssign for SquadSize {
113 fn sub_assign(&mut self, rhs: Self) {
114 *self = *self - rhs;
115 }
116}
117
118impl SubAssign<u32> for SquadSize {
119 fn sub_assign(&mut self, rhs: u32) {
120 *self = *self - rhs;
121 }
122}
123
124impl Mul<Maintenance> for SquadSize {
125 type Output = Maintenance;
126
127 fn mul(self, rhs: Maintenance) -> Self::Output {
128 let rhs = u32::from(rhs);
129 Maintenance::new(self.0.saturating_mul(rhs))
130 }
131}
132
133impl Mul<Power> for SquadSize {
134 type Output = Power;
135
136 fn mul(self, rhs: Power) -> Self::Output {
137 rhs * self.0
138 }
139}
140
141impl Mul<SquadSize> for Power {
142 type Output = Power;
143
144 fn mul(self, rhs: SquadSize) -> Self::Output {
145 self * rhs.0
146 }
147}
148
149impl Mul<Score> for SquadSize {
150 type Output = Score;
151
152 fn mul(self, rhs: Score) -> Self::Output {
153 let rhs = u32::from(rhs);
154 Score::new(self.0.saturating_mul(rhs))
155 }
156}
157
158impl Mul<f64> for SquadSize {
159 type Output = SquadSize;
160
161 fn mul(self, rhs: f64) -> Self::Output {
162 debug_assert!(rhs.is_finite());
163 debug_assert!(rhs.is_sign_positive());
164 debug_assert!(!rhs.is_subnormal());
165 Self((f64::from(self.0) * rhs).floor() as u32)
166 }
167}
168
169impl MulAssign<f64> for SquadSize {
170 fn mul_assign(&mut self, rhs: f64) {
171 *self = *self * rhs;
172 }
173}