1 Commits

Author SHA1 Message Date
4d06100300 Add level 8: Scroll of Tags (tags + newlines)
Three keys:
- scroll: multi-line string (block scalar `|`)
- weight: fractional float (e.g. 12.5) — forces float without needing
  the `!!float` tag in the target text
- title:  string of digits (player needs quotes or `!!str` to avoid int)

Per-seed deterministic via ChaCha8Rng XOR'd with 0x..08.

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:55:56 +03:00
3 changed files with 112 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(),
}
}
}

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

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 l08_tags;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};