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    debug_assert!(value.is_finite());
20    debug_assert!(!value.is_subnormal());
21    Self(value.clamp(Self::MIN.0, Self::MAX.0))
22  }
23}
24
25impl const Default for Stability {
26  fn default() -> Self {
27    Self::MAX
28  }
29}
30
31impl const From<f64> for Stability {
32  fn from(value: f64) -> Self {
33    Self::new(value)
34  }
35}
36
37impl const From<Stability> for f64 {
38  fn from(value: Stability) -> Self {
39    value.0
40  }
41}