use crate::hash::hash_bytes;
use crate::palette::Theme;
use serde::{Deserialize, Serialize};
pub const GENERATOR_VERSION: u32 = 4;
pub const OLDEST_GENERATOR_VERSION: u32 = 3;
pub const ARCHETYPES: [&str; crate::archetype::ARCHETYPE_INFO.len()] = {
let mut ids = [""; crate::archetype::ARCHETYPE_INFO.len()];
let mut i = 0;
while i < ids.len() {
ids[i] = crate::archetype::ARCHETYPE_INFO[i].id;
i += 1;
}
ids
};
pub const LOOKS: [&str; 2] = ["line", "dither"];
pub const CONTRASTS: [&str; 3] = ["soft", "normal", "high"];
pub const ZOOMS: [&str; 3] = ["close", "wide", "vast"];
pub fn zoom_factor(zoom: &str) -> f64 {
match zoom {
"wide" => 0.5,
"vast" => 0.25,
_ => 1.0,
}
}
fn close() -> String {
"close".into()
}
fn is_close(v: &str) -> bool {
v == "close"
}
fn line() -> String {
"line".into()
}
fn is_line(v: &str) -> bool {
v == "line"
}
fn normal() -> String {
"normal".into()
}
fn is_normal(v: &str) -> bool {
v == "normal"
}
#[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,
#[serde(default, skip_serializing_if = "is_zero")]
pub fly_lift_pm: u32,
#[serde(default, skip_serializing_if = "is_zero")]
pub journey_ms: u32,
#[serde(default, skip_serializing_if = "is_zero")]
pub journey_pace_c: u32,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub landmark: String,
}
fn is_zero(v: &u32) -> bool {
*v == 0
}
#[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,
#[serde(default = "line", skip_serializing_if = "is_line")]
pub look: String,
#[serde(default = "normal", skip_serializing_if = "is_normal")]
pub contrast: String,
#[serde(default = "close", skip_serializing_if = "is_close")]
pub zoom: String,
}
impl SceneRecipe {
pub fn zoom_factor(&self) -> f64 {
zoom_factor(&self.zoom)
}
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 hash_hex(&self) -> String {
format!("{:016x}", hash_bytes(self.to_json().as_bytes()))
}
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)
}
pub fn is_journey(&self) -> bool {
self.composition.journey_ms > 0 || self.composition.journey_pace_c > 0
}
}
#[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,
#[serde(default)]
pub enabled_looks: Vec<String>,
#[serde(default = "normal")]
pub contrast: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub theme: Option<Theme>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub enabled_zooms: Vec<String>,
}
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(),
enabled_looks: LOOKS.iter().map(|s| s.to_string()).collect(),
contrast: normal(),
theme: None,
enabled_zooms: Vec::new(),
}
}
}
impl ContentContext {
pub fn hash(&self) -> u64 {
hash_bytes(&serde_json::to_vec(self).expect("context serialises"))
}
}