nil_core/military/unit/stats/
haul.rs1use crate::infrastructure::storage::StorageCapacity;
5use crate::military::army::Army;
6use crate::military::army::personnel::ArmyPersonnel;
7use crate::military::squad::Squad;
8use crate::military::squad::size::SquadSize;
9use crate::resources::prelude::*;
10use derive_more::{From, Into};
11use nil_num::mul_ceil::MulCeil;
12use nil_util::ConstDeref;
13use serde::{Deserialize, Serialize};
14use std::cmp::Ordering;
15use std::iter::Sum;
16use std::ops::{Add, AddAssign, Mul, MulAssign};
17
18#[derive(Copy, Debug, From, Into, Deserialize, Serialize, ConstDeref)]
19#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
20#[from(u32, Food, Iron, Stone, Wood)]
21#[into(u32, f64, Food, Iron, Stone, Wood)]
22#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
23pub struct Haul(u32);
24
25impl Haul {
26 pub const SILO_RATIO: f64 = 0.75;
27 pub const WAREHOUSE_RATIO: f64 = 0.25;
28
29 #[inline]
30 pub const fn new(value: u32) -> Self {
31 Self(value)
32 }
33}
34
35impl const PartialEq<u32> for Haul {
36 fn eq(&self, other: &u32) -> bool {
37 self.0.eq(other)
38 }
39}
40
41impl const PartialOrd<u32> for Haul {
42 fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
43 Some(self.0.cmp(other))
44 }
45}
46
47impl const PartialEq<StorageCapacity> for Haul {
48 fn eq(&self, other: &StorageCapacity) -> bool {
49 self.0.eq(&**other)
50 }
51}
52
53impl const PartialOrd<StorageCapacity> for Haul {
54 fn partial_cmp(&self, other: &StorageCapacity) -> Option<Ordering> {
55 Some(self.0.cmp(&**other))
56 }
57}
58
59impl const From<StorageCapacity> for Haul {
60 fn from(value: StorageCapacity) -> Self {
61 Haul::new(*value)
62 }
63}
64
65impl<'a> Sum<&'a Squad> for Haul {
66 fn sum<I>(iter: I) -> Self
67 where
68 I: Iterator<Item = &'a Squad>,
69 {
70 iter.fold(Haul::default(), |mut acc, squad| {
71 acc += squad.haul();
72 acc
73 })
74 }
75}
76
77impl<'a> Sum<&'a ArmyPersonnel> for Haul {
78 fn sum<I>(iter: I) -> Self
79 where
80 I: Iterator<Item = &'a ArmyPersonnel>,
81 {
82 iter.flat_map(ArmyPersonnel::iter).sum()
83 }
84}
85
86impl<'a> Sum<&'a Army> for Haul {
87 fn sum<I>(iter: I) -> Self
88 where
89 I: Iterator<Item = &'a Army>,
90 {
91 iter.flat_map(Army::iter).sum()
92 }
93}
94
95impl const Add for Haul {
96 type Output = Haul;
97
98 fn add(self, rhs: Self) -> Self::Output {
99 Self(self.0.saturating_add(rhs.0))
100 }
101}
102
103impl const AddAssign for Haul {
104 fn add_assign(&mut self, rhs: Self) {
105 *self = *self + rhs;
106 }
107}
108
109impl const Mul<SquadSize> for Haul {
110 type Output = Haul;
111
112 fn mul(self, rhs: SquadSize) -> Self::Output {
113 Self(self.0.saturating_mul(*rhs))
114 }
115}
116
117impl const MulAssign<SquadSize> for Haul {
118 fn mul_assign(&mut self, rhs: SquadSize) {
119 *self = *self * rhs;
120 }
121}
122
123impl const Mul<f64> for Haul {
124 type Output = Haul;
125
126 fn mul(self, rhs: f64) -> Self::Output {
127 Self(f64::from(self.0).mul_ceil(rhs) as u32)
128 }
129}
130
131const _: () = {
132 let mut base: f64 = 0.0;
133 base += Haul::SILO_RATIO;
134 base += Haul::WAREHOUSE_RATIO;
135 assert!((base - 1.0).abs() < 0.001);
136};