Compare commits
17 Commits
level-9
...
abc0fa7784
| Author | SHA1 | Date | |
|---|---|---|---|
| abc0fa7784 | |||
| 9b2f6d4731 | |||
| 2bf412e65b | |||
| 5bbf7ca107 | |||
| bcdd487eec | |||
| 736c6bd564 | |||
| 3f8ea45ef7 | |||
| a8b91f1645 | |||
| 040bec6c80 | |||
| 9f8e5caaa8 | |||
| 424ef3e8fd | |||
| b2f9f24b68 | |||
| 4d06100300 | |||
| 5b73daee4c | |||
| c0e89ca1a1 | |||
| b805a49aaa | |||
| 07f82a7086 |
71
src/levels/l04_list.rs
Normal file
71
src/levels/l04_list.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
//! Level 4 — lists. A chest of loot lies open before you.
|
||||
//!
|
||||
//! Paired design note: `l04.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 List;
|
||||
|
||||
const ITEMS: &[&str] = &[
|
||||
"sword", "torch", "rope", "bread", "dagger", "scroll", "gem", "coin", "potion", "shield",
|
||||
];
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct DescCtx {
|
||||
items: Vec<String>,
|
||||
}
|
||||
|
||||
impl Level for List {
|
||||
fn id(&self) -> u8 {
|
||||
4
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"The Chest"
|
||||
}
|
||||
|
||||
fn generate(&self, seed: u64) -> Generated {
|
||||
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_0004);
|
||||
let n = rng.gen_range(3..=5);
|
||||
let items: Vec<&'static str> = ITEMS.choose_multiple(&mut rng, n).copied().collect();
|
||||
|
||||
let seq: Sequence = items
|
||||
.iter()
|
||||
.map(|i| Value::String((*i).to_string()))
|
||||
.collect();
|
||||
|
||||
let mut top = Mapping::new();
|
||||
top.insert(Value::String("chest".to_string()), Value::Sequence(seq));
|
||||
let target_yaml =
|
||||
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
|
||||
|
||||
let mut d = Describer::new();
|
||||
d.register(
|
||||
"l04",
|
||||
"A chest lies open. Inside:\n{% for it in items %}- {{ it }}\n{% endfor %}\n💡 Wrap the items as a YAML list under a `chest:` key.",
|
||||
)
|
||||
.expect("register template");
|
||||
let description = d
|
||||
.render(
|
||||
"l04",
|
||||
&DescCtx {
|
||||
items: items.iter().map(|s| s.to_string()).collect(),
|
||||
},
|
||||
)
|
||||
.expect("render template");
|
||||
|
||||
Generated {
|
||||
target_yaml,
|
||||
description,
|
||||
flavor: "📦 A chest of loot lies open before you.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
120
src/levels/l05_dict_list.rs
Normal file
120
src/levels/l05_dict_list.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
//! 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 {
|
||||
sentences: Vec<String>,
|
||||
}
|
||||
|
||||
/// Pick "a" or "an" based on the first letter — keeps the prose reading
|
||||
/// naturally without giving away that chamber names are YAML keys.
|
||||
fn article(word: &str) -> &'static str {
|
||||
let first = word.chars().next().map(|c| c.to_ascii_lowercase());
|
||||
if matches!(first, Some('a') | Some('e') | Some('i') | Some('o') | Some('u')) {
|
||||
"an"
|
||||
} else {
|
||||
"a"
|
||||
}
|
||||
}
|
||||
|
||||
/// Join the item list as English: `a sword`, `a sword and a shield`,
|
||||
/// `a sword, a shield, and a potion` (Oxford comma for 3+).
|
||||
fn join_items(items: &[&str]) -> String {
|
||||
let parts: Vec<String> = items
|
||||
.iter()
|
||||
.map(|i| format!("{} {}", article(i), i))
|
||||
.collect();
|
||||
match parts.as_slice() {
|
||||
[] => String::new(),
|
||||
[one] => one.clone(),
|
||||
[a, b] => format!("{a} and {b}"),
|
||||
rest => {
|
||||
let (last, head) = rest.split_last().unwrap();
|
||||
format!("{}, and {}", head.join(", "), last)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 sentences = 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));
|
||||
|
||||
let be = if items.len() == 1 { "is" } else { "are" };
|
||||
sentences.push(format!(
|
||||
"There {be} {} inside {} {name}.",
|
||||
join_items(&items),
|
||||
article(name),
|
||||
));
|
||||
}
|
||||
|
||||
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 contents:\n\
|
||||
{% for s in sentences %}\n {{ s }}{% endfor %}\n\n\
|
||||
💡 Wrap the whole tree under a `chambers:` key — a dict of lists.",
|
||||
)
|
||||
.expect("register template");
|
||||
let description = d
|
||||
.render("l05", &DescCtx { sentences })
|
||||
.expect("render template");
|
||||
|
||||
Generated {
|
||||
target_yaml,
|
||||
description,
|
||||
flavor: "🏛 You enter a hall. Doors lead to many chambers.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
113
src/levels/l06_anchors.rs
Normal file
113
src/levels/l06_anchors.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
//! Level 6 — anchors. Two rooms share the same trap — define it once.
|
||||
//!
|
||||
//! Paired design note: `l06.md`.
|
||||
//!
|
||||
//! Note: serde_yaml resolves aliases at parse time, so the target is
|
||||
//! emitted **expanded** (the trap dict appears in each room). Players
|
||||
//! who use anchors/aliases will produce the same parsed `Value` and
|
||||
//! pass via the semantic short-circuit. Players who paste the dict
|
||||
//! verbatim also pass.
|
||||
|
||||
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 Anchors;
|
||||
|
||||
const ROOM_NAMES: &[&str] = &["north", "south", "east", "west"];
|
||||
const TRAP_TYPES: &[&str] = &["pit", "snare", "dart", "rune"];
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct DescCtx {
|
||||
trap_type: String,
|
||||
trap_depth: i64,
|
||||
trap_spikes: bool,
|
||||
rooms: Vec<String>,
|
||||
}
|
||||
|
||||
impl Level for Anchors {
|
||||
fn id(&self) -> u8 {
|
||||
6
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Anchors"
|
||||
}
|
||||
|
||||
fn generate(&self, seed: u64) -> Generated {
|
||||
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_0006);
|
||||
let trap_type = *TRAP_TYPES.choose(&mut rng).expect("non-empty");
|
||||
let trap_depth = rng.gen_range(10..=30i64);
|
||||
let trap_spikes = rng.gen_bool(0.5);
|
||||
let n_rooms = rng.gen_range(2..=3);
|
||||
let rooms: Vec<&'static str> = ROOM_NAMES
|
||||
.choose_multiple(&mut rng, n_rooms)
|
||||
.copied()
|
||||
.collect();
|
||||
|
||||
let mut trap = Mapping::new();
|
||||
trap.insert(
|
||||
Value::String("type".to_string()),
|
||||
Value::String(trap_type.to_string()),
|
||||
);
|
||||
trap.insert(Value::String("depth".to_string()), Value::from(trap_depth));
|
||||
trap.insert(
|
||||
Value::String("spikes".to_string()),
|
||||
Value::Bool(trap_spikes),
|
||||
);
|
||||
|
||||
let mut rooms_map = Mapping::new();
|
||||
for r in &rooms {
|
||||
rooms_map.insert(
|
||||
Value::String((*r).to_string()),
|
||||
Value::Mapping(trap.clone()),
|
||||
);
|
||||
}
|
||||
|
||||
let mut top = Mapping::new();
|
||||
top.insert(Value::String("trap".to_string()), Value::Mapping(trap));
|
||||
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(
|
||||
"l06",
|
||||
"A single trap recurs through these halls:\n\
|
||||
- type: {{ trap_type }}\n\
|
||||
- depth: {{ trap_depth }}\n\
|
||||
- spikes: {{ trap_spikes }}\n\
|
||||
\n\
|
||||
Reuse it for these rooms: {% for r in rooms %}{{ r }}{% if not loop.last %}, {% endif %}{% endfor %}.\n\
|
||||
💡 Define `trap: &name` once and reference it as `*name` in every room.",
|
||||
)
|
||||
.expect("register template");
|
||||
let description = d
|
||||
.render(
|
||||
"l06",
|
||||
&DescCtx {
|
||||
trap_type: trap_type.to_string(),
|
||||
trap_depth,
|
||||
trap_spikes,
|
||||
rooms: rooms.iter().map(|s| s.to_string()).collect(),
|
||||
},
|
||||
)
|
||||
.expect("render template");
|
||||
|
||||
Generated {
|
||||
target_yaml,
|
||||
description,
|
||||
flavor: "🪤 A trap recurs through these halls.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
142
src/levels/l07_complex.rs
Normal file
142
src/levels/l07_complex.rs
Normal file
@@ -0,0 +1,142 @@
|
||||
//! 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
111
src/levels/l08_tags.rs
Normal file
111
src/levels/l08_tags.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
//! Level 8 — tags and newlines. The scroll preserves its lines and
|
||||
//! demands explicit types.
|
||||
//!
|
||||
//! Paired design note: `l08.md`.
|
||||
//!
|
||||
//! The target value uses:
|
||||
//! - a multi-line string for `scroll:` (block scalar `|` round-trips through
|
||||
//! serde_yaml as a `Value::String` with embedded newlines),
|
||||
//! - a fractional float for `weight:` (forces float type without needing the
|
||||
//! `!!float` tag in the target text — but the player can use either),
|
||||
//! - a string of digits for `title:` (player needs quotes or `!!str` to
|
||||
//! avoid the integer interpretation).
|
||||
|
||||
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 Tags;
|
||||
|
||||
const VERBS: &[&str] = &["Beware", "Avoid", "Heed", "Mark"];
|
||||
const NOUNS: &[&str] = &[
|
||||
"the path that turns twice",
|
||||
"the third spring",
|
||||
"the silent statue",
|
||||
"the moonlit door",
|
||||
];
|
||||
const TITLES: &[&str] = &["1024", "2048", "4096", "8192"];
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct DescCtx {
|
||||
scroll_lines: Vec<String>,
|
||||
weight: f64,
|
||||
title: String,
|
||||
}
|
||||
|
||||
impl Level for Tags {
|
||||
fn id(&self) -> u8 {
|
||||
8
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Scroll of Tags"
|
||||
}
|
||||
|
||||
fn generate(&self, seed: u64) -> Generated {
|
||||
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_0008);
|
||||
|
||||
let line_n = 2;
|
||||
let scroll_lines: Vec<String> = (0..line_n)
|
||||
.map(|_| {
|
||||
let v = VERBS.choose(&mut rng).unwrap();
|
||||
let n = NOUNS.choose(&mut rng).unwrap();
|
||||
format!("{v} {n}.")
|
||||
})
|
||||
.collect();
|
||||
// Block scalar `|` produces a string ending in `\n` after the last line.
|
||||
let scroll_text = format!("{}\n", scroll_lines.join("\n"));
|
||||
|
||||
// Fractional weight so serde_yaml's float serialisation keeps the
|
||||
// `.5` and matches the player's submission unambiguously.
|
||||
let weight = rng.gen_range(1..=20) as f64 + 0.5;
|
||||
let title = TITLES.choose(&mut rng).unwrap().to_string();
|
||||
|
||||
let mut top = Mapping::new();
|
||||
top.insert(
|
||||
Value::String("scroll".to_string()),
|
||||
Value::String(scroll_text),
|
||||
);
|
||||
top.insert(Value::String("weight".to_string()), Value::from(weight));
|
||||
top.insert(
|
||||
Value::String("title".to_string()),
|
||||
Value::String(title.clone()),
|
||||
);
|
||||
|
||||
let target_yaml =
|
||||
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
|
||||
|
||||
let mut d = Describer::new();
|
||||
d.register(
|
||||
"l08",
|
||||
"A scroll demands its types. Match exactly:\n\
|
||||
scroll: multi-line text{% for line in scroll_lines %}\n {{ line }}{% endfor %}\n\
|
||||
weight: must parse as the float {{ weight }}\n\
|
||||
title: must parse as the string \"{{ title }}\"\n\
|
||||
💡 Block scalar `|` preserves newlines; `!!str` (or quotes) forces a digit-only string.",
|
||||
)
|
||||
.expect("register template");
|
||||
let description = d
|
||||
.render(
|
||||
"l08",
|
||||
&DescCtx {
|
||||
scroll_lines,
|
||||
weight,
|
||||
title,
|
||||
},
|
||||
)
|
||||
.expect("render template");
|
||||
|
||||
Generated {
|
||||
target_yaml,
|
||||
description,
|
||||
flavor: "📜 An ancient scroll insists on its precise form.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
101
src/levels/l10_dynamic.rs
Normal file
101
src/levels/l10_dynamic.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
//! Level 10 — dynamic values. The vault ledger accepts numbers and
|
||||
//! dates in many forms.
|
||||
//!
|
||||
//! Paired design note: `l10.md`.
|
||||
//!
|
||||
//! The target is canonical (decimal ints, decimal float, ISO date
|
||||
//! string). The lesson is that the player can write equivalent values
|
||||
//! in hex / octal / exponent forms — all parse to the same number.
|
||||
|
||||
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 Dynamic;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct DescCtx {
|
||||
gold_dec: i64,
|
||||
gold_hex: String,
|
||||
silver_dec: i64,
|
||||
silver_oct: String,
|
||||
experience: f64,
|
||||
date: String,
|
||||
}
|
||||
|
||||
impl Level for Dynamic {
|
||||
fn id(&self) -> u8 {
|
||||
10
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Vault Ledger"
|
||||
}
|
||||
|
||||
fn generate(&self, seed: u64) -> Generated {
|
||||
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_000A);
|
||||
|
||||
let gold = rng.gen_range(0x100..=0xFFFi64);
|
||||
let silver = rng.gen_range(0o100..=0o777i64);
|
||||
// Fractional float so the canonical serialisation keeps the `.5`.
|
||||
let experience = rng.gen_range(10..=99) as f64 + 0.5;
|
||||
let year = rng.gen_range(1100..=2050i64);
|
||||
let month = rng.gen_range(1..=12i64);
|
||||
let day = rng.gen_range(1..=28i64);
|
||||
let date = format!("{year:04}-{month:02}-{day:02}");
|
||||
|
||||
let mut vault = Mapping::new();
|
||||
vault.insert(Value::String("gold".to_string()), Value::from(gold));
|
||||
vault.insert(Value::String("silver".to_string()), Value::from(silver));
|
||||
vault.insert(
|
||||
Value::String("experience".to_string()),
|
||||
Value::from(experience),
|
||||
);
|
||||
vault.insert(
|
||||
Value::String("date".to_string()),
|
||||
Value::String(date.clone()),
|
||||
);
|
||||
|
||||
let mut top = Mapping::new();
|
||||
top.insert(Value::String("vault".to_string()), Value::Mapping(vault));
|
||||
|
||||
let target_yaml =
|
||||
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
|
||||
|
||||
let mut d = Describer::new();
|
||||
d.register(
|
||||
"l10",
|
||||
"The vault ledger demands its values:\n\
|
||||
gold: {{ gold_dec }} (try hex: 0x{{ gold_hex }})\n\
|
||||
silver: {{ silver_dec }} (try octal: 0o{{ silver_oct }})\n\
|
||||
experience: {{ experience }} (any equivalent float form passes)\n\
|
||||
date: {{ date }} (an ISO-8601 date string)\n\
|
||||
💡 Any equivalent numeric form passes — what matters is the parsed value.",
|
||||
)
|
||||
.expect("register template");
|
||||
let description = d
|
||||
.render(
|
||||
"l10",
|
||||
&DescCtx {
|
||||
gold_dec: gold,
|
||||
gold_hex: format!("{:X}", gold),
|
||||
silver_dec: silver,
|
||||
silver_oct: format!("{:o}", silver),
|
||||
experience,
|
||||
date,
|
||||
},
|
||||
)
|
||||
.expect("render template");
|
||||
|
||||
Generated {
|
||||
target_yaml,
|
||||
description,
|
||||
flavor: "🪙 A vault ledger awaits in many ciphers.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
141
src/levels/l11_adv_anchors.rs
Normal file
141
src/levels/l11_adv_anchors.rs
Normal file
@@ -0,0 +1,141 @@
|
||||
//! 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,14 @@
|
||||
pub mod l01_minimum;
|
||||
pub mod l02_kv;
|
||||
pub mod l03_dict;
|
||||
pub mod l04_list;
|
||||
pub mod l05_dict_list;
|
||||
pub mod l06_anchors;
|
||||
pub mod l07_complex;
|
||||
pub mod l08_tags;
|
||||
pub mod l09_operators;
|
||||
pub mod l10_dynamic;
|
||||
pub mod l11_adv_anchors;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -84,5 +91,7 @@ pub fn registry() -> Vec<Box<dyn Level>> {
|
||||
Box::new(l01_minimum::Minimum),
|
||||
Box::new(l02_kv::KeyValue),
|
||||
Box::new(l03_dict::Dict),
|
||||
Box::new(l04_list::List),
|
||||
Box::new(l05_dict_list::DictList),
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user