1use crate::hash::hash_bytes;
6use crate::palette::Theme;
7use serde::{Deserialize, Serialize};
8
9pub const GENERATOR_VERSION: u32 = 3;
14
15pub const ARCHETYPES: [&str; 9] = {
17 let mut ids = [""; 9];
18 let mut i = 0;
19 while i < 9 {
20 ids[i] = crate::archetype::ARCHETYPE_INFO[i].id;
21 i += 1;
22 }
23 ids
24};
25
26#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
28pub struct Aspect {
29 pub w: u32,
30 pub h: u32,
31}
32
33impl Aspect {
34 pub fn from_size(width: u32, height: u32) -> Self {
36 assert!(width > 0 && height > 0);
37 let g = gcd(width, height);
38 Self {
39 w: width / g,
40 h: height / g,
41 }
42 }
43
44 pub fn ratio(self) -> f64 {
45 f64::from(self.w) / f64::from(self.h)
46 }
47}
48
49fn gcd(mut a: u32, mut b: u32) -> u32 {
50 while b != 0 {
51 (a, b) = (b, a % b);
52 }
53 a
54}
55
56#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
58#[serde(rename_all = "snake_case")]
59pub enum TimeOfDay {
60 Morning,
61 Noon,
62 GoldenHour,
63 Dusk,
64 DeepDusk,
65 Night,
66}
67
68impl TimeOfDay {
69 pub const LIGHT: [TimeOfDay; 3] = [TimeOfDay::Morning, TimeOfDay::Noon, TimeOfDay::GoldenHour];
70 pub const DARK: [TimeOfDay; 3] = [TimeOfDay::Dusk, TimeOfDay::DeepDusk, TimeOfDay::Night];
71
72 pub fn theme(self) -> Theme {
73 match self {
74 TimeOfDay::Morning | TimeOfDay::Noon | TimeOfDay::GoldenHour => Theme::Light,
75 TimeOfDay::Dusk | TimeOfDay::DeepDusk | TimeOfDay::Night => Theme::Dark,
76 }
77 }
78}
79
80#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
83pub struct Composition {
84 pub horizon_pm: u32,
86 pub focus_x_pm: u32,
88 pub gap_pm: u32,
90 pub shape_pow_pm: u32,
92 pub platform_margin_pm: u32,
94 pub cliff_layers: u32,
96 pub prop_left: bool,
98 pub motion_speed_cc: u32,
100 pub pace_half_pm: u32,
102}
103
104#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
106pub struct PalettePick {
107 pub light: String,
108 pub dark: String,
109}
110
111#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
113pub struct SceneRecipe {
114 pub generator_version: u32,
115 pub seed: u64,
116 pub aspect: Aspect,
117 pub content_ctx_hash: String,
119 pub activity: Option<String>,
121 pub archetype: String,
122 pub light_tod: Option<TimeOfDay>,
124 pub dark_tod: Option<TimeOfDay>,
126 pub platform: String,
127 pub props: Vec<String>,
128 pub weather: String,
129 pub composition: Composition,
130 pub palette: PalettePick,
131}
132
133impl SceneRecipe {
134 pub fn tod_for(&self, theme: Theme) -> Option<TimeOfDay> {
136 match theme {
137 Theme::Light => self.light_tod,
138 Theme::Dark => self.dark_tod,
139 }
140 }
141
142 pub fn is_theme_locked(&self) -> bool {
144 self.light_tod.is_none() || self.dark_tod.is_none()
145 }
146
147 pub fn hash_hex(&self) -> String {
149 format!("{:016x}", hash_bytes(self.to_json().as_bytes()))
150 }
151
152 pub fn to_json(&self) -> String {
154 serde_json::to_string_pretty(self).expect("recipe serialises")
155 }
156
157 pub fn from_json(s: &str) -> Result<Self, serde_json::Error> {
158 serde_json::from_str(s)
159 }
160}
161
162#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
164pub struct HistoryEntry {
165 pub activity: Option<String>,
166 pub archetype: String,
167 pub platform: String,
168 pub prop: Option<String>,
169}
170
171#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
173pub struct ContentContext {
174 pub enabled_archetypes: Vec<String>,
175 pub enabled_weather: Vec<String>,
176 pub enabled_activities: Vec<String>,
177 pub recent: Vec<HistoryEntry>,
179 pub data_hash: u64,
181}
182
183impl Default for ContentContext {
184 fn default() -> Self {
185 Self {
186 enabled_archetypes: ARCHETYPES.iter().map(|s| s.to_string()).collect(),
187 enabled_weather: crate::archetype::WEATHERS
188 .iter()
189 .map(|s| s.to_string())
190 .collect(),
191 enabled_activities: crate::activity::Catalog::builtin()
192 .activities
193 .iter()
194 .map(|a| a.id.clone())
195 .collect(),
196 recent: Vec::new(),
197 data_hash: crate::activity::builtin_data_hash(),
198 }
199 }
200}
201
202impl ContentContext {
203 pub fn hash(&self) -> u64 {
205 hash_bytes(&serde_json::to_vec(self).expect("context serialises"))
206 }
207}