Skip to main content

nil_core/city/
stability.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use nil_util::{ConstDeref, F64Math};
5use serde::{Deserialize, Serialize};
6
7/// Political stability of the city.
8#[derive(Copy, Debug, Deserialize, Serialize, ConstDeref, F64Math)]
9#[derive_const(Clone, PartialEq, PartialOrd)]
10#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
11pub struct Stability(f64);
12
13impl Stability {
14  pub const MIN: Stability = Stability(0.0);
15  pub const MAX: Stability = Stability(1.0);
16
17  #[inline]
18  pub const fn new(value: f64) -> Self {
19    Self(value.clamp(Self::MIN.0, Self::MAX.0))
20  }
21}
22
23impl const Default for Stability {
24  fn default() -> Self {
25    Self::MAX
26  }
27}
28
29impl const From<Stability> for f64 {
30  fn from(value: Stability) -> Self {
31    value.0
32  }
33}