Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c0e89ca1a1 |
@@ -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(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 l06_anchors;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user