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