Skip to main content

nil_core/military/maneuver/
distance.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::continent::distance::Distance;
5use crate::military::unit::stats::speed::Speed;
6use derive_more::Display;
7use nil_util::{ConstDeref, F64Math};
8use serde::{Deserialize, Serialize};
9use std::cmp::Ordering;
10use std::ops::{Sub, SubAssign};
11
12#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref, F64Math)]
13#[derive_const(Clone)]
14#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
15pub struct ManeuverDistance(f64);
16
17const impl From<ManeuverDistance> for f64 {
18  fn from(value: ManeuverDistance) -> Self {
19    value.0
20  }
21}
22
23const impl From<f64> for ManeuverDistance {
24  fn from(distance: f64) -> Self {
25    Self(distance)
26  }
27}
28
29const impl From<Distance> for ManeuverDistance {
30  fn from(distance: Distance) -> Self {
31    Self(f64::from(distance))
32  }
33}
34
35const impl PartialEq for ManeuverDistance {
36  fn eq(&self, other: &Self) -> bool {
37    matches!(self.0.total_cmp(&other.0), Ordering::Equal)
38  }
39}
40
41const impl Eq for ManeuverDistance {}
42
43const impl PartialOrd for ManeuverDistance {
44  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
45    Some(self.cmp(other))
46  }
47}
48
49const impl Ord for ManeuverDistance {
50  fn cmp(&self, other: &Self) -> Ordering {
51    self.0.total_cmp(&other.0)
52  }
53}
54
55const impl PartialEq<f64> for ManeuverDistance {
56  fn eq(&self, other: &f64) -> bool {
57    matches!(self.0.total_cmp(other), Ordering::Equal)
58  }
59}
60
61const impl PartialOrd<f64> for ManeuverDistance {
62  fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
63    Some(self.0.total_cmp(other))
64  }
65}
66
67const impl Sub for ManeuverDistance {
68  type Output = ManeuverDistance;
69
70  fn sub(mut self, rhs: Self) -> Self::Output {
71    self -= rhs;
72    self
73  }
74}
75
76const impl Sub<Speed> for ManeuverDistance {
77  type Output = ManeuverDistance;
78
79  fn sub(mut self, rhs: Speed) -> Self::Output {
80    self -= rhs;
81    self
82  }
83}
84
85const impl SubAssign for ManeuverDistance {
86  fn sub_assign(&mut self, rhs: Self) {
87    *self = Self(self.0 - rhs.0);
88  }
89}
90
91const impl SubAssign<Speed> for ManeuverDistance {
92  fn sub_assign(&mut self, rhs: Speed) {
93    *self = Self(self.0 - f64::from(rhs));
94  }
95}