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;
5use crate::military::unit::stats::speed::Speed;
6use derive_more::{Deref, Into};
7use serde::{Deserialize, Serialize};
8use std::cmp::Ordering;
9use std::ops::{Sub, SubAssign};
10
11#[derive(Clone, Copy, Debug, Deref, Into, Deserialize, Serialize)]
12pub struct ManeuverDistance(f64);
13
14impl PartialEq for ManeuverDistance {
15  fn eq(&self, other: &Self) -> bool {
16    matches!(self.0.total_cmp(&other.0), Ordering::Equal)
17  }
18}
19
20impl Eq for ManeuverDistance {}
21
22impl PartialOrd for ManeuverDistance {
23  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
24    Some(self.cmp(other))
25  }
26}
27
28impl Ord for ManeuverDistance {
29  fn cmp(&self, other: &Self) -> Ordering {
30    self.0.total_cmp(&other.0)
31  }
32}
33
34impl PartialEq<f64> for ManeuverDistance {
35  fn eq(&self, other: &f64) -> bool {
36    matches!(self.0.total_cmp(other), Ordering::Equal)
37  }
38}
39
40impl PartialOrd<f64> for ManeuverDistance {
41  fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
42    Some(self.0.total_cmp(other))
43  }
44}
45
46impl Sub for ManeuverDistance {
47  type Output = ManeuverDistance;
48
49  fn sub(mut self, rhs: Self) -> Self::Output {
50    self -= rhs;
51    self
52  }
53}
54
55impl Sub<Speed> for ManeuverDistance {
56  type Output = ManeuverDistance;
57
58  fn sub(mut self, rhs: Speed) -> Self::Output {
59    self -= rhs;
60    self
61  }
62}
63
64impl SubAssign for ManeuverDistance {
65  fn sub_assign(&mut self, rhs: Self) {
66    *self = Self(self.0 - rhs.0);
67  }
68}
69
70impl SubAssign<Speed> for ManeuverDistance {
71  fn sub_assign(&mut self, rhs: Speed) {
72    *self = Self(self.0 - f64::from(rhs));
73  }
74}
75
76impl From<Distance> for ManeuverDistance {
77  fn from(distance: Distance) -> Self {
78    Self(f64::from(distance))
79  }
80}