Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 424ef3e8fd |
@@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
pub mod l01_minimum;
|
||||
pub mod l02_kv;
|
||||
pub mod l03_dict;
|
||||
pub mod l07_complex;
|
||||
pub mod l10_dynamic;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user