1 Commits

Author SHA1 Message Date
f0a7992626 Add level 9: Merge Keys (special operators)
`door_defaults` plus `north_door` and `south_door`, each containing a
`<<:` key whose value is the defaults dict. north overrides locked,
south overrides material. serde_yaml treats `<<` as a literal mapping
key, so the target and a player's `<<: *defaults` form parse to the
same Value.

Default material/locked and which side overrides which are randomised
per seed (ChaCha8Rng XOR'd with 0x..09).

Not wired into levels::registry() yet — integration belongs to a follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 22:56:44 +03:00
3 changed files with 136 additions and 100 deletions

View File

@@ -1,99 +0,0 @@
//! Level 5 — dictionaries AND lists. Each chamber keeps its own inventory.
//!
//! Paired design note: `l05.md`.
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 DictList;
const CHAMBERS: &[&str] = &[
"armory", "pantry", "library", "vault", "kitchen", "cellar",
];
const ITEMS: &[&str] = &[
"sword", "shield", "bread", "water", "tome", "scroll", "gem", "coin", "dagger", "potion",
];
#[derive(Serialize)]
struct DescCtx {
chambers: Vec<ChamberDesc>,
}
#[derive(Serialize)]
struct ChamberDesc {
name: String,
items: Vec<String>,
}
impl Level for DictList {
fn id(&self) -> u8 {
5
}
fn name(&self) -> &'static str {
"Chambers"
}
fn generate(&self, seed: u64) -> Generated {
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_0005);
let n = rng.gen_range(2..=3);
let chamber_names: Vec<&'static str> =
CHAMBERS.choose_multiple(&mut rng, n).copied().collect();
let mut inner = Mapping::new();
let mut desc_chambers = Vec::new();
for name in &chamber_names {
let item_n = rng.gen_range(2..=3);
let items: Vec<&'static str> =
ITEMS.choose_multiple(&mut rng, item_n).copied().collect();
let seq: Sequence = items
.iter()
.map(|i| Value::String((*i).to_string()))
.collect();
inner.insert(Value::String((*name).to_string()), Value::Sequence(seq));
desc_chambers.push(ChamberDesc {
name: (*name).to_string(),
items: items.iter().map(|s| s.to_string()).collect(),
});
}
let mut top = Mapping::new();
top.insert(
Value::String("chambers".to_string()),
Value::Mapping(inner),
);
let target_yaml =
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
let mut d = Describer::new();
d.register(
"l05",
"Several chambers branch off, each with its own inventory:\n\
{% for c in chambers %}\n{{ c.name }}:{% for it in c.items %}\n - {{ it }}{% endfor %}\n{% endfor %}\n\
💡 Wrap the whole tree under a `chambers:` key — a dict of lists.",
)
.expect("register template");
let description = d
.render(
"l05",
&DescCtx {
chambers: desc_chambers,
},
)
.expect("render template");
Generated {
target_yaml,
description,
flavor: "🏛 You enter a hall. Doors lead to many chambers.".to_string(),
}
}
}

135
src/levels/l09_operators.rs Normal file
View 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(),
}
}
}

View File

@@ -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 l05_dict_list; pub mod l09_operators;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};