Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f0a7992626 |
135
src/levels/l09_operators.rs
Normal file
135
src/levels/l09_operators.rs
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
//! Level 9 — special operators. The merge key (`<<`) lets a door
|
||||||
|
//! inherit a template and override one field.
|
||||||
|
//!
|
||||||
|
//! Paired design note: `l09.md`.
|
||||||
|
//!
|
||||||
|
//! `serde_yaml` treats `<<` as a literal mapping key (it does NOT
|
||||||
|
//! perform YAML 1.1 merge-key resolution). The target therefore carries
|
||||||
|
//! a `<<` key whose value is the defaults dict, exactly as the player's
|
||||||
|
//! `<<: *defaults` parses.
|
||||||
|
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
use rand::{Rng, SeedableRng};
|
||||||
|
use rand_chacha::ChaCha8Rng;
|
||||||
|
use serde::Serialize;
|
||||||
|
use serde_yaml::{Mapping, Value};
|
||||||
|
|
||||||
|
use crate::describe::Describer;
|
||||||
|
|
||||||
|
use super::{Generated, Level};
|
||||||
|
|
||||||
|
pub struct Operators;
|
||||||
|
|
||||||
|
const MATERIALS: &[&str] = &["oak", "iron", "stone", "silver", "bone"];
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct DescCtx {
|
||||||
|
default_material: String,
|
||||||
|
default_locked: bool,
|
||||||
|
north_locked: bool,
|
||||||
|
south_material: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Level for Operators {
|
||||||
|
fn id(&self) -> u8 {
|
||||||
|
9
|
||||||
|
}
|
||||||
|
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"Merge Keys"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate(&self, seed: u64) -> Generated {
|
||||||
|
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_0009);
|
||||||
|
let default_material = (*MATERIALS.choose(&mut rng).unwrap()).to_string();
|
||||||
|
let default_locked = rng.gen_bool(0.5);
|
||||||
|
// The north door overrides locked; choose the opposite so the
|
||||||
|
// override is meaningful.
|
||||||
|
let north_locked = !default_locked;
|
||||||
|
// The south door overrides material; pick anything but default.
|
||||||
|
let other_materials: Vec<&str> = MATERIALS
|
||||||
|
.iter()
|
||||||
|
.filter(|m| **m != default_material)
|
||||||
|
.copied()
|
||||||
|
.collect();
|
||||||
|
let south_material = (*other_materials.choose(&mut rng).unwrap()).to_string();
|
||||||
|
|
||||||
|
// Build the defaults mapping (referenced by all doors).
|
||||||
|
let mut defaults = Mapping::new();
|
||||||
|
defaults.insert(
|
||||||
|
Value::String("material".to_string()),
|
||||||
|
Value::String(default_material.clone()),
|
||||||
|
);
|
||||||
|
defaults.insert(
|
||||||
|
Value::String("locked".to_string()),
|
||||||
|
Value::Bool(default_locked),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut north_door = Mapping::new();
|
||||||
|
north_door.insert(
|
||||||
|
Value::String("<<".to_string()),
|
||||||
|
Value::Mapping(defaults.clone()),
|
||||||
|
);
|
||||||
|
north_door.insert(
|
||||||
|
Value::String("locked".to_string()),
|
||||||
|
Value::Bool(north_locked),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut south_door = Mapping::new();
|
||||||
|
south_door.insert(
|
||||||
|
Value::String("<<".to_string()),
|
||||||
|
Value::Mapping(defaults.clone()),
|
||||||
|
);
|
||||||
|
south_door.insert(
|
||||||
|
Value::String("material".to_string()),
|
||||||
|
Value::String(south_material.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut top = Mapping::new();
|
||||||
|
top.insert(
|
||||||
|
Value::String("door_defaults".to_string()),
|
||||||
|
Value::Mapping(defaults),
|
||||||
|
);
|
||||||
|
top.insert(
|
||||||
|
Value::String("north_door".to_string()),
|
||||||
|
Value::Mapping(north_door),
|
||||||
|
);
|
||||||
|
top.insert(
|
||||||
|
Value::String("south_door".to_string()),
|
||||||
|
Value::Mapping(south_door),
|
||||||
|
);
|
||||||
|
|
||||||
|
let target_yaml =
|
||||||
|
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
|
||||||
|
|
||||||
|
let mut d = Describer::new();
|
||||||
|
d.register(
|
||||||
|
"l09",
|
||||||
|
"Two doors share a template:\n\
|
||||||
|
default material: {{ default_material }}\n\
|
||||||
|
default locked: {{ default_locked }}\n\
|
||||||
|
\n\
|
||||||
|
north_door overrides locked → {{ north_locked }}\n\
|
||||||
|
south_door overrides material → {{ south_material }}\n\
|
||||||
|
💡 Anchor the defaults (`door_defaults: &name`) and merge with `<<: *name` in each door.",
|
||||||
|
)
|
||||||
|
.expect("register template");
|
||||||
|
let description = d
|
||||||
|
.render(
|
||||||
|
"l09",
|
||||||
|
&DescCtx {
|
||||||
|
default_material,
|
||||||
|
default_locked,
|
||||||
|
north_locked,
|
||||||
|
south_material,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("render template");
|
||||||
|
|
||||||
|
Generated {
|
||||||
|
target_yaml,
|
||||||
|
description,
|
||||||
|
flavor: "🚪 Two doors echo a single template.".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
//! Level 11 — advanced anchors. Anchor shapes inside a list, alias
|
|
||||||
//! them elsewhere.
|
|
||||||
//!
|
|
||||||
//! Paired design note: `l11.md`.
|
|
||||||
//!
|
|
||||||
//! Like L6, serde_yaml expands aliases on parse, so the emitted target
|
|
||||||
//! is the fully-inlined form. Players who use `&anchor` / `*alias`
|
|
||||||
//! produce the same `Value` and pass via the semantic short-circuit.
|
|
||||||
|
|
||||||
use rand::seq::SliceRandom;
|
|
||||||
use rand::{Rng, SeedableRng};
|
|
||||||
use rand_chacha::ChaCha8Rng;
|
|
||||||
use serde::Serialize;
|
|
||||||
use serde_yaml::{Mapping, Sequence, Value};
|
|
||||||
|
|
||||||
use crate::describe::Describer;
|
|
||||||
|
|
||||||
use super::{Generated, Level};
|
|
||||||
|
|
||||||
pub struct AdvAnchors;
|
|
||||||
|
|
||||||
const SHAPES: &[(&str, i64)] = &[
|
|
||||||
("triangle", 3),
|
|
||||||
("square", 4),
|
|
||||||
("pentagon", 5),
|
|
||||||
("hexagon", 6),
|
|
||||||
("heptagon", 7),
|
|
||||||
("octagon", 8),
|
|
||||||
];
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
struct DescCtx {
|
|
||||||
shapes: Vec<ShapeDesc>,
|
|
||||||
copies: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
struct ShapeDesc {
|
|
||||||
name: String,
|
|
||||||
sides: i64,
|
|
||||||
interior_angle_sum: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn shape_value(name: &str, sides: i64) -> Value {
|
|
||||||
let mut m = Mapping::new();
|
|
||||||
m.insert(
|
|
||||||
Value::String("name".to_string()),
|
|
||||||
Value::String(name.to_string()),
|
|
||||||
);
|
|
||||||
m.insert(Value::String("sides".to_string()), Value::from(sides));
|
|
||||||
m.insert(
|
|
||||||
Value::String("interior".to_string()),
|
|
||||||
Value::from((sides - 2) * 180),
|
|
||||||
);
|
|
||||||
Value::Mapping(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Level for AdvAnchors {
|
|
||||||
fn id(&self) -> u8 {
|
|
||||||
11
|
|
||||||
}
|
|
||||||
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"Advanced Anchors"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate(&self, seed: u64) -> Generated {
|
|
||||||
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_000B);
|
|
||||||
|
|
||||||
let n = rng.gen_range(2..=3);
|
|
||||||
let picked: Vec<(&'static str, i64)> = SHAPES
|
|
||||||
.choose_multiple(&mut rng, n)
|
|
||||||
.copied()
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// The defining `shapes:` list.
|
|
||||||
let shapes_seq: Sequence = picked
|
|
||||||
.iter()
|
|
||||||
.map(|(name, sides)| shape_value(name, *sides))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// `copies:` — random selections with possible repetition.
|
|
||||||
let m = rng.gen_range(3..=4);
|
|
||||||
let mut copies_seq = Sequence::new();
|
|
||||||
let mut copy_names = Vec::new();
|
|
||||||
for _ in 0..m {
|
|
||||||
let (name, sides) = picked.choose(&mut rng).unwrap();
|
|
||||||
copies_seq.push(shape_value(name, *sides));
|
|
||||||
copy_names.push((*name).to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut top = Mapping::new();
|
|
||||||
top.insert(
|
|
||||||
Value::String("shapes".to_string()),
|
|
||||||
Value::Sequence(shapes_seq),
|
|
||||||
);
|
|
||||||
top.insert(
|
|
||||||
Value::String("copies".to_string()),
|
|
||||||
Value::Sequence(copies_seq),
|
|
||||||
);
|
|
||||||
|
|
||||||
let target_yaml =
|
|
||||||
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
|
|
||||||
|
|
||||||
let shape_descs: Vec<ShapeDesc> = picked
|
|
||||||
.iter()
|
|
||||||
.map(|(name, sides)| ShapeDesc {
|
|
||||||
name: (*name).to_string(),
|
|
||||||
sides: *sides,
|
|
||||||
interior_angle_sum: (sides - 2) * 180,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut d = Describer::new();
|
|
||||||
d.register(
|
|
||||||
"l11",
|
|
||||||
"Shapes are defined once and reused.\n\
|
|
||||||
Definitions:\n\
|
|
||||||
{% for s in shapes %}- {{ s.name }}: sides={{ s.sides }}, interior={{ s.interior_angle_sum }}\n\
|
|
||||||
{% endfor %}\n\
|
|
||||||
Copies, in order: {% for c in copies %}{{ c }}{% if not loop.last %}, {% endif %}{% endfor %}\n\
|
|
||||||
💡 Anchor each shape in the list with `- &name`, then alias by `*name` in copies.",
|
|
||||||
)
|
|
||||||
.expect("register template");
|
|
||||||
let description = d
|
|
||||||
.render(
|
|
||||||
"l11",
|
|
||||||
&DescCtx {
|
|
||||||
shapes: shape_descs,
|
|
||||||
copies: copy_names,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.expect("render template");
|
|
||||||
|
|
||||||
Generated {
|
|
||||||
target_yaml,
|
|
||||||
description,
|
|
||||||
flavor: "🔺 The geometry chamber repeats its forms.".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
pub mod l01_minimum;
|
pub mod l01_minimum;
|
||||||
pub mod l02_kv;
|
pub mod l02_kv;
|
||||||
pub mod l03_dict;
|
pub mod l03_dict;
|
||||||
pub mod l11_adv_anchors;
|
pub mod l09_operators;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user