1 Commits

Author SHA1 Message Date
b805a49aaa Add level 5: Chambers (dictionaries + lists)
Top-level `chambers:` mapping; each chamber name maps to an item list.
2-3 chambers, 2-3 items each, drawn from typed pools. Deterministic per
seed via ChaCha8Rng XOR'd with 0x..05.

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:12:23 +03:00
3 changed files with 100 additions and 143 deletions

View File

@@ -0,0 +1,99 @@
//! 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(),
}
}
}

View File

@@ -1,142 +0,0 @@
//! Level 7 — complex data structures. The full map of a single floor.
//!
//! Paired design note: `l07.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 Complex;
const EXITS: &[&str] = &["north", "south", "east", "west", "up", "down"];
const CONTENTS: &[&str] = &[
"torch", "bench", "gold", "ruby", "scroll", "tome", "dagger", "shield",
];
const ROOMS: &[(&str, &str)] = &[
("entrance", "hall"),
("treasury", "vault"),
("library", "study"),
("kitchen", "scullery"),
];
#[derive(Serialize)]
struct DescCtx {
floor: i64,
rooms: Vec<RoomDesc>,
}
#[derive(Serialize)]
struct RoomDesc {
name: String,
kind: String,
locked: bool,
exits: Vec<String>,
contents: Vec<String>,
}
impl Level for Complex {
fn id(&self) -> u8 {
7
}
fn name(&self) -> &'static str {
"The Floor Map"
}
fn generate(&self, seed: u64) -> Generated {
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_0007);
let floor = rng.gen_range(1..=5i64);
let chosen: Vec<&(&'static str, &'static str)> =
ROOMS.choose_multiple(&mut rng, 2).collect();
let mut rooms_map = Mapping::new();
let mut desc_rooms = Vec::new();
for (name, kind) in &chosen {
let exits_n = rng.gen_range(1..=3);
let exits: Vec<&'static str> =
EXITS.choose_multiple(&mut rng, exits_n).copied().collect();
let contents_n = rng.gen_range(1..=3);
let contents: Vec<&'static str> = CONTENTS
.choose_multiple(&mut rng, contents_n)
.copied()
.collect();
let locked = rng.gen_bool(0.5);
let mut room = Mapping::new();
room.insert(
Value::String("type".to_string()),
Value::String((*kind).to_string()),
);
room.insert(Value::String("locked".to_string()), Value::Bool(locked));
let exits_seq: Sequence = exits
.iter()
.map(|e| Value::String((*e).to_string()))
.collect();
let contents_seq: Sequence = contents
.iter()
.map(|c| Value::String((*c).to_string()))
.collect();
room.insert(
Value::String("exits".to_string()),
Value::Sequence(exits_seq),
);
room.insert(
Value::String("contents".to_string()),
Value::Sequence(contents_seq),
);
rooms_map.insert(Value::String((*name).to_string()), Value::Mapping(room));
desc_rooms.push(RoomDesc {
name: (*name).to_string(),
kind: (*kind).to_string(),
locked,
exits: exits.iter().map(|s| s.to_string()).collect(),
contents: contents.iter().map(|s| s.to_string()).collect(),
});
}
let mut top = Mapping::new();
top.insert(Value::String("floor".to_string()), Value::from(floor));
top.insert(
Value::String("rooms".to_string()),
Value::Mapping(rooms_map),
);
let target_yaml =
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
let mut d = Describer::new();
d.register(
"l07",
"Floor {{ floor }}.\n\
{% for r in rooms %}\n\
{{ r.name }} — a {{ r.kind }} (locked: {{ r.locked }})\n\
\texits: {% for e in r.exits %}{{ e }}{% if not loop.last %}, {% endif %}{% endfor %}\n\
\tcontents: {% for c in r.contents %}{{ c }}{% if not loop.last %}, {% endif %}{% endfor %}\n\
{% endfor %}\n\
💡 Combine maps, lists, and scalars — `floor:` is an int, each room is a dict with two lists.",
)
.expect("register template");
let description = d
.render(
"l07",
&DescCtx {
floor,
rooms: desc_rooms,
},
)
.expect("render template");
Generated {
target_yaml,
description,
flavor: "🗺 The map of this floor is rich with detail.".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 l07_complex; pub mod l05_dict_list;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};