use crate::hash::hash_bytes;
use crate::palette::Theme;
use serde::{Deserialize, Serialize};
pub const GENERATOR_VERSION: u32 = 3;
pub const ARCHETYPES: [&str; 9] = {
let mut ids = [""; 9];
let mut i = 0;
while i < 9 {
ids[i] = crate::archetype::ARCHETYPE_INFO[i].id;
i += 1;
}
ids
};
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Aspect {
pub w: u32,
pub h: u32,
}
impl Aspect {
pub fn from_size(width: u32, height: u32) -> Self {
assert!(width > 0 && height > 0);
let g = gcd(width, height);
Self {
w: width / g,
h: height / g,
}
}
pub fn ratio(self) -> f64 {
f64::from(self.w) / f64::from(self.h)
}
}
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
(a, b) = (b, a % b);
}
a
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum TimeOfDay {
Morning,
Noon,
GoldenHour,
Dusk,
DeepDusk,
Night,
}
impl TimeOfDay {
pub const LIGHT: [TimeOfDay; 3] = [TimeOfDay::Morning, TimeOfDay::Noon, TimeOfDay::GoldenHour];
pub const DARK: [TimeOfDay; 3] = [TimeOfDay::Dusk, TimeOfDay::DeepDusk, TimeOfDay::Night];
pub fn theme(self) -> Theme {
match self {
TimeOfDay::Morning | TimeOfDay::Noon | TimeOfDay::GoldenHour => Theme::Light,
TimeOfDay::Dusk | TimeOfDay::DeepDusk | TimeOfDay::Night => Theme::Dark,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Composition {
pub horizon_pm: u32,
pub focus_x_pm: u32,
pub gap_pm: u32,
pub shape_pow_pm: u32,
pub platform_margin_pm: u32,
pub cliff_layers: u32,
pub prop_left: bool,
pub motion_speed_cc: u32,
pub pace_half_pm: u32,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PalettePick {
pub light: String,
pub dark: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct SceneRecipe {
pub generator_version: u32,
pub seed: u64,
pub aspect: Aspect,
pub content_ctx_hash: String,
pub activity: Option<String>,
pub archetype: String,
pub light_tod: Option<TimeOfDay>,
pub dark_tod: Option<TimeOfDay>,
pub platform: String,
pub props: Vec<String>,
pub weather: String,
pub composition: Composition,
pub palette: PalettePick,
}
impl SceneRecipe {
pub fn tod_for(&self, theme: Theme) -> Option<TimeOfDay> {
match theme {
Theme::Light => self.light_tod,
Theme::Dark => self.dark_tod,
}
}
pub fn is_theme_locked(&self) -> bool {
self.light_tod.is_none() || self.dark_tod.is_none()
}
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(self).expect("recipe serialises")
}
pub fn from_json(s: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(s)
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct HistoryEntry {
pub activity: Option<String>,
pub archetype: String,
pub platform: String,
pub prop: Option<String>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct ContentContext {
pub enabled_archetypes: Vec<String>,
pub enabled_weather: Vec<String>,
pub enabled_activities: Vec<String>,
pub recent: Vec<HistoryEntry>,
pub data_hash: u64,
}
impl Default for ContentContext {
fn default() -> Self {
Self {
enabled_archetypes: ARCHETYPES.iter().map(|s| s.to_string()).collect(),
enabled_weather: crate::archetype::WEATHERS
.iter()
.map(|s| s.to_string())
.collect(),
enabled_activities: crate::activity::Catalog::builtin()
.activities
.iter()
.map(|a| a.id.clone())
.collect(),
recent: Vec::new(),
data_hash: crate::activity::builtin_data_hash(),
}
}
}
impl ContentContext {
pub fn hash(&self) -> u64 {
hash_bytes(&serde_json::to_vec(self).expect("context serialises"))
}
}