Compare commits
4 Commits
level-11
...
040bec6c80
| Author | SHA1 | Date | |
|---|---|---|---|
| 040bec6c80 | |||
| b2f9f24b68 | |||
| 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
99
src/levels/l05_dict_list.rs
Normal file
99
src/levels/l05_dict_list.rs
Normal 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,8 @@
|
||||
pub mod l01_minimum;
|
||||
pub mod l02_kv;
|
||||
pub mod l03_dict;
|
||||
pub mod l11_adv_anchors;
|
||||
pub mod l04_list;
|
||||
pub mod l05_dict_list;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user