1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::convert::TryFrom;
use std::f32::consts::PI;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde::{Deserialize, Serialize};
use strum_macros::Display;
use thiserror::Error;
use holding_kronos::{Calendar, CalendarDateTime};
use crate::{CelestialBody, PlanetId, PlanetStore};
#[derive(Clone, Deserialize, Serialize, Debug, Copy)]
pub struct Orbit {
pub parent: PlanetId,
pub body: PlanetId,
pub shift: usize,
pub eccentricity: f32,
pub period: usize,
}
impl Orbit {
pub fn from_period(
target: &CelestialBody,
parent: PlanetId,
period: usize,
shift: usize,
) -> Self {
Orbit {
parent,
body: target.id,
period: period * target.rotational_period,
shift: shift * target.rotational_period % period,
eccentricity: 0.0,
}
}
pub fn from_radius(
target: &CelestialBody,
parent: PlanetId,
semimajor_axis: f32,
shift: usize,
) -> Self {
let period = semimajor_axis.powf(3.0).sqrt() as usize;
Orbit::from_period(target, parent, period, shift)
}
pub fn get_phase(
&self,
lookup: &dyn PlanetStore,
date_time: CalendarDateTime,
) -> Option<Phase> {
if lookup.get_planet(self.body)?.is_luminous() {
return None;
}
let parent = lookup.get_planet(self.parent)?;
let parent_orbit = parent.orbit.as_ref()?;
let sun = lookup.get_planet(parent_orbit.parent)?;
if !sun.is_luminous() {
return None;
}
let theta_moon = self.get_orbit_radians(date_time.modulo(self.period as u32));
let theta_parent =
parent_orbit.get_orbit_radians(date_time.modulo(parent_orbit.period as u32));
let mut theta = theta_moon - theta_parent;
if theta.is_sign_negative() {
theta += 2.0 * PI
};
Some(Phase::try_from((theta * 4.0 / PI) as u8).expect("This should be in range."))
}
pub fn get_orbit_radians(&self, seconds: u32) -> f32 {
(seconds as usize + self.shift) as f32 / self.period as f32 % 1.0 * 2.0 * PI
}
pub fn get_distance(&self, seconds: u32) -> f32 {
let radians = self.get_orbit_radians(seconds);
self.get_semimajor_axis() * (1.0 - self.eccentricity.powf(2.0))
/ (1.0 + self.eccentricity * radians.cos())
}
pub fn get_semimajor_axis(&self) -> f32 {
(self.period as f32).powf(2.0).powf(-3.0)
}
pub fn validate_calendar(&self, calendar: &Calendar) -> Result<bool, OrbitValidationError> {
let planet_period = self.period as u32;
let calendar_period = calendar.years_to_seconds(1) as u32;
if planet_period != calendar_period {
Err(OrbitValidationError::InconsistentPeriod(
planet_period,
calendar_period,
))
} else {
Ok(true)
}
}
}
#[derive(Error, Debug, Copy, Clone)]
pub enum OrbitValidationError {
#[error("the orbial period is inconsistent. planet: {0}, calendar: {1}")]
InconsistentPeriod(u32, u32),
}
#[derive(IntoPrimitive, Clone, Debug, TryFromPrimitive, Copy, Display)]
#[repr(u8)]
#[allow(missing_docs)]
pub enum Phase {
#[strum(serialize = "a brilliant gleaming disk in the dark")]
Full,
#[strum(serialize = "in waning gibbous, beginning to retreat into darkness")]
WaningGibbous,
#[strum(serialize = "in the half-shadow of the third quarter")]
ThirdQuarter,
#[strum(serialize = "in waning crescent, nearly covered in darkness")]
WaningCrescent,
#[strum(serialize = "a silky hole in the starry sky")]
New,
#[strum(serialize = "in waxing crescent, light creeping out")]
WaxingCrescent,
#[strum(serialize = "in the half-light of the first quarter")]
FirstQuarter,
#[strum(serialize = "in waxing gibbous, nearly fully lit")]
WaxingGibbous,
}
impl Phase {
pub fn unicode(&self) -> &str {
match self {
Self::Full => "🌕",
Self::WaningGibbous => "🌖",
Self::ThirdQuarter => "🌗",
Self::WaningCrescent => "🌘",
Self::New => "🌑",
Self::WaxingCrescent => "🌒",
Self::FirstQuarter => "🌓",
Self::WaxingGibbous => "🌔",
}
}
}