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 derive_more::Display;
5use nil_util::{ConstDeref, F64Math};
6use serde::{Deserialize, Serialize};
7
8/// Political stability of the city.
9#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref, F64Math)]
10#[derive_const(Clone, PartialEq, PartialOrd)]
11#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
12pub struct Stability(f64);
13
14impl Stability {
15  pub const MIN: Stability = Stability(0.0);
16  pub const MAX: Stability = Stability(1.0);
17
18  #[inline]
19  pub const fn new(value: f64) -> Self {
20    debug_assert!(value.is_finite());
21    debug_assert!(!value.is_subnormal());
22    Self(value.clamp(Self::MIN.0, Self::MAX.0))
23  }
24}
25
26impl const Default for Stability {
27  fn default() -> Self {
28    Self::MAX
29  }
30}
31
32impl const From<f64> for Stability {
33  fn from(value: f64) -> Self {
34    Self::new(value)
35  }
36}
37
38impl const From<Stability> for f64 {
39  fn from(value: Stability) -> Self {
40    value.0
41  }
42}