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::Display;
8use nil_util::ConstDeref;
9use serde::{Deserialize, Serialize};
10use std::cmp::Ordering;
11use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
12
13#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref)]
14#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
15#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
16#[cfg_attr(feature = "typescript", ts(export))]
17pub struct SquadSize(u32);
18
19impl SquadSize {
20  #[inline]
21  pub const fn new(size: u32) -> Self {
22    Self(size)
23  }
24
25  #[inline]
26  pub fn random() -> Self {
27    Self::new(rand::random())
28  }
29
30  #[inline]
31  pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
32    self.0.checked_sub(rhs.0).map(Self::new)
33  }
34}
35
36impl const PartialEq<u32> for SquadSize {
37  fn eq(&self, other: &u32) -> bool {
38    self.0.eq(other)
39  }
40}
41
42impl const PartialOrd<u32> for SquadSize {
43  fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
44    self.0.partial_cmp(other)
45  }
46}
47
48impl const From<u32> for SquadSize {
49  fn from(value: u32) -> Self {
50    Self::new(value)
51  }
52}
53
54impl const From<SquadSize> for u32 {
55  fn from(value: SquadSize) -> Self {
56    value.0
57  }
58}
59
60impl const From<SquadSize> for f64 {
61  fn from(value: SquadSize) -> Self {
62    f64::from(value.0)
63  }
64}
65
66impl const Add for SquadSize {
67  type Output = SquadSize;
68
69  fn add(self, rhs: Self) -> Self::Output {
70    Self(self.0.saturating_add(rhs.0))
71  }
72}
73
74impl const Add<u32> for SquadSize {
75  type Output = SquadSize;
76
77  fn add(self, rhs: u32) -> Self::Output {
78    Self(self.0.saturating_add(rhs))
79  }
80}
81
82impl const AddAssign for SquadSize {
83  fn add_assign(&mut self, rhs: Self) {
84    *self = *self + rhs;
85  }
86}
87
88impl const AddAssign<u32> for SquadSize {
89  fn add_assign(&mut self, rhs: u32) {
90    *self = *self + rhs;
91  }
92}
93
94impl const From<f64> for SquadSize {
95  fn from(value: f64) -> Self {
96    debug_assert!(value >= 0.0);
97    debug_assert!(value.is_finite());
98    Self::new(value as u32)
99  }
100}
101
102impl const Sub for SquadSize {
103  type Output = SquadSize;
104
105  fn sub(self, rhs: Self) -> Self::Output {
106    Self(self.0.saturating_sub(rhs.0))
107  }
108}
109
110impl const Sub<u32> for SquadSize {
111  type Output = SquadSize;
112
113  fn sub(self, rhs: u32) -> Self::Output {
114    Self(self.0.saturating_sub(rhs))
115  }
116}
117
118impl const SubAssign for SquadSize {
119  fn sub_assign(&mut self, rhs: Self) {
120    *self = *self - rhs;
121  }
122}
123
124impl const SubAssign<u32> for SquadSize {
125  fn sub_assign(&mut self, rhs: u32) {
126    *self = *self - rhs;
127  }
128}
129
130impl const Mul<Maintenance> for SquadSize {
131  type Output = Maintenance;
132
133  fn mul(self, rhs: Maintenance) -> Self::Output {
134    let rhs = u32::from(rhs);
135    Maintenance::new(self.0.saturating_mul(rhs))
136  }
137}
138
139impl const Mul<Power> for SquadSize {
140  type Output = Power;
141
142  fn mul(self, rhs: Power) -> Self::Output {
143    rhs * self.0
144  }
145}
146
147impl const 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 const 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 const MulAssign<f64> for SquadSize {
168  fn mul_assign(&mut self, rhs: f64) {
169    *self = *self * rhs;
170  }
171}