Board layers

This commit is contained in:
2026-06-15 23:35:18 -05:00
parent d1d0824d37
commit 01cd73ca3b
29 changed files with 2166 additions and 2051 deletions
+44 -70
View File
@@ -12,79 +12,53 @@ fn load_board(toml: &str) -> Board {
Board::try_from(mf).expect("convert to board")
}
/// A 3×1 map (`empty` palette `.`) whose grid is `grid`, plus the given
/// `[[objects]]` TOML appended. Keeps placement tests compact.
fn map_3x1(grid: &str, objects: &str) -> String {
format!(
r##"
[map]
name = "Test"
width = 3
height = 1
player_start = [2, 0]
[palette]
"." = {{ archetype = "empty", tile = 46, fg = "#000000", bg = "#000000" }}
"#" = {{ archetype = "wall", tile = 35, fg = "#808080", bg = "#606060" }}
[grid]
content = """
{grid}
"""
{objects}
"##
)
/// Builds one `[[layers]]` block: a triple-quoted `content` grid plus an inline
/// `palette` table. Each palette entry is `(char_key, inline-body)` where the
/// body is the inside of the entry's `{ ... }` (e.g. `kind = "wall"`).
fn layer(content: &str, palette: &[(&str, &str)]) -> String {
let pal = palette
.iter()
.map(|(k, body)| format!("\"{k}\" = {{ {body} }}"))
.collect::<Vec<_>>()
.join(", ");
format!("\n[[layers]]\ncontent = \"\"\"\n{content}\n\"\"\"\npalette = {{ {pal} }}\n")
}
/// A standard `[[objects]]` block placed by palette char `ch`.
fn obj_palette(ch: &str, extra: &str) -> String {
format!(
"[[objects]]\npalette = \"{ch}\"\ntile = 64\nfg = \"#00FFFF\"\nbg = \"#000000\"\n{extra}"
)
/// Wraps a `[map]` header of the given size around the supplied `[[layers]]`
/// blocks (each produced by [`layer`]).
fn map(width: usize, height: usize, layers: &[String]) -> String {
let mut s = format!("[map]\nname = \"Test\"\nwidth = {width}\nheight = {height}\n");
for l in layers {
s.push_str(l);
}
s
}
/// A 1-row map of the given `width` with a raw `player_start` TOML value
/// (`start`, e.g. `[1, 0]` or `"@"`), grid, and objects block.
fn player_map(width: usize, start: &str, grid: &str, objects: &str) -> String {
format!(
r##"
[map]
name = "Test"
width = {width}
height = 1
player_start = {start}
[palette]
"." = {{ archetype = "empty", tile = 46, fg = "#000000", bg = "#000000" }}
"#" = {{ archetype = "wall", tile = 35, fg = "#808080", bg = "#606060" }}
[grid]
content = """
{grid}
"""
{objects}
"##
)
}
/// Minimal valid TOML with a configurable grid, used as a base for dimension tests.
fn minimal_toml(width: usize, height: usize, grid_rows: &[&str]) -> String {
let content = grid_rows.join("\n");
format!(
r##"
[map]
name = "Test"
width = {width}
height = {height}
player_start = [0, 0]
[palette]
"." = {{ archetype = "empty", tile = 46, fg = "#000000", bg = "#000000" }}
[grid]
content = """
{content}
"""
"##
/// A 3×1 single-layer map: an `empty`/`wall` palette plus one `object` entry
/// placed by char `ch` (cyan `@` glyph), with `extra` appended to its body. The
/// player is placed at the far-right cell via a second char where room allows;
/// callers that need the player elsewhere build the map directly.
fn map_3x1_object(grid: &str, ch: &str, extra: &str) -> String {
let body = if extra.is_empty() {
"kind = \"object\", tile = 64, fg = \"#00FFFF\", bg = \"#000000\"".to_string()
} else {
format!("kind = \"object\", tile = 64, fg = \"#00FFFF\", bg = \"#000000\", {extra}")
};
map(
3,
1,
&[layer(
grid,
&[
(" ", "kind = \"empty\""),
(".", "kind = \"empty\""),
(
"#",
"kind = \"wall\", tile = 35, fg = \"#808080\", bg = \"#606060\"",
),
("@", "kind = \"player\""),
(ch, &body),
],
)],
)
}