Skip to main content

nil_core/battle/
luck.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#[derive(Copy, Debug, Deserialize, Serialize, ConstDeref, F64Math)]
8#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
9#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
10pub struct Luck(i8);
11
12impl Luck {
13  pub const MIN: Luck = Luck(-20);
14  pub const MAX: Luck = Luck(20);
15
16  #[inline]
17  pub const fn new(value: i8) -> Self {
18    Self(value.clamp(Self::MIN.0, Self::MAX.0))
19  }
20
21  pub fn random() -> Self {
22    Self(rand::random_range(Self::MIN.0..=Self::MAX.0))
23  }
24}
25
26impl const From<i8> for Luck {
27  fn from(value: i8) -> Self {
28    Self::new(value)
29  }
30}
31
32impl const From<Luck> for f64 {
33  fn from(luck: Luck) -> Self {
34    f64::from(luck.0) / 100.0
35  }
36}