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