Skip to main content

nil_core/resources/
gold.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::market::fee::MarketFee;
5use crate::resources::Resources;
6use derive_more::Display;
7use nil_num::impl_mul_ceil;
8use nil_num::mul_ceil::MulCeil;
9use nil_util::{ConstDeref, F64Math};
10use serde::{Deserialize, Serialize};
11use std::cmp::Ordering;
12use std::iter::Sum;
13use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
14
15/// Gold is a special resource used to trade in the market.
16#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref, F64Math)]
17#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
18#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
19pub struct Gold(u32);
20
21impl Gold {
22  pub const MIN: Self = Self::new(0);
23  pub const MAX: Self = Self::new(u32::MAX);
24
25  #[inline]
26  pub const fn new(value: u32) -> Self {
27    Self(value)
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_mul_ceil!(Gold);
37
38const impl From<u32> for Gold {
39  fn from(value: u32) -> Self {
40    Self::new(value)
41  }
42}
43
44const impl From<Gold> for u32 {
45  fn from(value: Gold) -> Self {
46    value.0
47  }
48}
49
50const impl From<f64> for Gold {
51  fn from(value: f64) -> Self {
52    debug_assert!(value.is_finite());
53    debug_assert!(value >= 0.0);
54    Self(value.trunc() as u32)
55  }
56}
57
58const impl From<Gold> for f64 {
59  fn from(value: Gold) -> Self {
60    f64::from(value.0)
61  }
62}
63
64const impl From<Resources> for Gold {
65  fn from(value: Resources) -> Self {
66    // It's better to destructure here so the compiler will warn us
67    // if we add new resources in the future and forget to update this.
68    let Resources { food, iron, stone, wood } = value;
69
70    Gold(0)
71      .add(Gold::from(food))
72      .add(Gold::from(iron))
73      .add(Gold::from(stone))
74      .add(Gold::from(wood))
75  }
76}
77
78const impl PartialEq<u32> for Gold {
79  fn eq(&self, other: &u32) -> bool {
80    self.0.eq(other)
81  }
82}
83
84const impl PartialOrd<u32> for Gold {
85  fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
86    self.0.partial_cmp(other)
87  }
88}
89
90const impl Add for Gold {
91  type Output = Gold;
92
93  fn add(self, rhs: Gold) -> Self::Output {
94    Self::new(self.0.saturating_add(rhs.0))
95  }
96}
97
98const impl Add<Resources> for Gold {
99  type Output = Gold;
100
101  fn add(self, rhs: Resources) -> Self::Output {
102    self + Gold::from(rhs)
103  }
104}
105
106const impl AddAssign for Gold {
107  fn add_assign(&mut self, rhs: Gold) {
108    *self = *self + rhs;
109  }
110}
111
112const impl AddAssign<Resources> for Gold {
113  fn add_assign(&mut self, rhs: Resources) {
114    *self = *self + rhs;
115  }
116}
117
118const impl Sub for Gold {
119  type Output = Gold;
120
121  fn sub(self, rhs: Gold) -> Self::Output {
122    Self::new(self.0.saturating_sub(rhs.0))
123  }
124}
125
126const impl SubAssign for Gold {
127  fn sub_assign(&mut self, rhs: Gold) {
128    *self = *self - rhs;
129  }
130}
131
132const impl Mul<u32> for Gold {
133  type Output = Gold;
134
135  fn mul(self, rhs: u32) -> Self::Output {
136    Self::new(self.0.saturating_mul(rhs))
137  }
138}
139
140const impl Mul<MarketFee> for Gold {
141  type Output = Gold;
142
143  fn mul(self, rhs: MarketFee) -> Self::Output {
144    Self::from(self.mul_ceil(*rhs))
145  }
146}
147
148const impl Div for Gold {
149  type Output = Gold;
150
151  fn div(self, rhs: Gold) -> Self::Output {
152    debug_assert!(rhs.0 != 0);
153    self
154      .0
155      .checked_div(rhs.0)
156      .map(Self::new)
157      .unwrap_or_default()
158  }
159}
160
161impl Sum<Gold> for Gold {
162  fn sum<I>(iter: I) -> Self
163  where
164    I: Iterator<Item = Gold>,
165  {
166    iter.fold(Gold::default(), |acc, gold| acc + gold)
167  }
168}
169
170impl Sum<Resources> for Gold {
171  fn sum<I>(iter: I) -> Self
172  where
173    I: Iterator<Item = Resources>,
174  {
175    iter.fold(Gold::default(), |acc, resources| acc + resources)
176  }
177}