Board layers
This commit is contained in:
@@ -0,0 +1,290 @@
|
||||
//! Palette layers: the unit of the map-file format and the board's draw stack.
|
||||
//!
|
||||
//! A board is an ordered list of **layers**, drawn bottom (index 0) to top. Each
|
||||
//! layer is a character grid plus a palette mapping each character to one *kind*
|
||||
//! of thing — an archetype (terrain), a floor glyph, a scripted object, a portal,
|
||||
//! or the player. "Put two things on one cell" simply means "use two layers".
|
||||
//!
|
||||
//! This module owns the per-layer serde types ([`LayerData`], [`PaletteEntry`]),
|
||||
//! the runtime [`Layer`] (a grid of `(Glyph, Archetype)` cells, where a cell with
|
||||
//! a transparent glyph lets lower layers show through), and the load-time
|
||||
//! conversion ([`build_layer`]) that turns one `LayerData` into a `Layer` plus a
|
||||
//! list of [`Placement`]s (objects/portals/player) for the map loader to resolve
|
||||
//! across layers. Cross-layer validation (one solid per cell, unique names, the
|
||||
//! player winning its cell) lives in [`crate::map_file`].
|
||||
|
||||
use crate::archetype::Archetype;
|
||||
use crate::floor::FloorGenerator;
|
||||
use crate::glyph::Glyph;
|
||||
use crate::log::LogLine;
|
||||
use crate::map_file::{TileIndex, parse_color};
|
||||
use crate::object_def::ObjectDef;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use tinyrand::StdRand;
|
||||
|
||||
/// A single draw layer: a row-major grid of `(Glyph, Archetype)` cells.
|
||||
///
|
||||
/// A cell whose `glyph.tile == 0` (see [`Glyph::transparent`]) is transparent —
|
||||
/// it draws nothing and lets the layer beneath show through. Solidity comes from
|
||||
/// the archetype and is independent of transparency. The grid is `width * height`
|
||||
/// long; index it via the owning [`Board`](crate::board::Board)'s dimensions.
|
||||
pub(crate) struct Layer {
|
||||
/// Row-major `(Glyph, Archetype)` cells for this layer.
|
||||
pub(crate) cells: Vec<(Glyph, Archetype)>,
|
||||
}
|
||||
|
||||
/// Serde representation of one `[[layers]]` entry: a grid string and its palette.
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub(crate) struct LayerData {
|
||||
/// Multi-line grid string; one char per cell, looked up in `palette`.
|
||||
pub content: String,
|
||||
/// Char (as a one-character string key) → palette entry.
|
||||
#[serde(default)]
|
||||
pub palette: HashMap<String, PaletteEntry>,
|
||||
}
|
||||
|
||||
/// One palette entry, discriminated by [`kind`](PaletteEntry::kind).
|
||||
///
|
||||
/// A single flat struct (rather than an enum) because `kind` is open-ended: it is
|
||||
/// any archetype name (`"wall"`, `"crate"`, `"pusher_east"`, …) *or* one of the
|
||||
/// meta-kinds `empty`, `floor`, `object`, `portal`, `player`. Only the fields
|
||||
/// relevant to a given kind are read; the rest stay `None`. See [`resolve_entry`].
|
||||
#[derive(Deserialize, Serialize, Default, Clone)]
|
||||
pub(crate) struct PaletteEntry {
|
||||
/// What this entry is: an archetype name, or `empty`/`floor`/`object`/`portal`/`player`.
|
||||
pub kind: String,
|
||||
/// Tile index (int or single-char string). Used by archetype/floor/object kinds.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub tile: Option<TileIndex>,
|
||||
/// Foreground `"#RRGGBB"`. Used by archetype/floor/object kinds.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub fg: Option<String>,
|
||||
/// Background `"#RRGGBB"`. Used by archetype/floor/object kinds.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub bg: Option<String>,
|
||||
/// Floor generator name (`"grass"`/`"dirt"`/`"stone"`). Only for `kind = "floor"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub generator: Option<String>,
|
||||
/// Object solidity (defaults `true`). Only for `kind = "object"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub solid: Option<bool>,
|
||||
/// Object opacity (defaults `true`). Only for `kind = "object"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub opaque: Option<bool>,
|
||||
/// Object pushability (defaults `false`). Only for `kind = "object"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub pushable: Option<bool>,
|
||||
/// Rhai script name. Only for `kind = "object"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub script_name: Option<String>,
|
||||
/// Open-ended labels. Only for `kind = "object"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub tags: Option<Vec<String>>,
|
||||
/// Unique name. For `kind = "object"` (optional) or `kind = "portal"` (required).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
/// Target board key. Required for `kind = "portal"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub target_map: Option<String>,
|
||||
/// Target portal name on the destination board. Required for `kind = "portal"`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub target_entry: Option<String>,
|
||||
}
|
||||
|
||||
/// A non-terrain thing a layer places at a grid cell, resolved by the map loader.
|
||||
///
|
||||
/// Terrain/floor go straight into [`Layer::cells`]; these need cross-layer
|
||||
/// handling (ids, name uniqueness, the single player), so [`build_layer`] returns
|
||||
/// them separately with their `(x, y)` for [`crate::map_file`] to finish.
|
||||
pub(crate) enum Placement {
|
||||
/// A scripted object to spawn at `(x, y)`.
|
||||
Object(ObjectTemplate, usize, usize),
|
||||
/// A portal at `(x, y)`.
|
||||
Portal(PortalTemplate, usize, usize),
|
||||
/// The player's start cell `(x, y)`.
|
||||
Player(usize, usize),
|
||||
}
|
||||
|
||||
/// A resolved object definition minus board-assigned fields (`id`, `z`).
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ObjectTemplate {
|
||||
pub glyph: Glyph,
|
||||
pub solid: bool,
|
||||
pub opaque: bool,
|
||||
pub pushable: bool,
|
||||
pub script_name: Option<String>,
|
||||
pub tags: Vec<String>,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
/// A resolved portal definition minus `(x, y, z)`.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct PortalTemplate {
|
||||
pub name: String,
|
||||
pub target_map: String,
|
||||
pub target_entry: String,
|
||||
}
|
||||
|
||||
/// What a palette character resolves to during the grid walk.
|
||||
#[derive(Clone)]
|
||||
enum Resolved {
|
||||
/// A fixed cell written straight into the layer (terrain, fixed floor, empty, error block).
|
||||
Cell(Glyph, Archetype),
|
||||
/// A procedural floor: roll a fresh glyph per occurrence.
|
||||
FloorGen(FloorGenerator),
|
||||
/// A scripted object placed per occurrence.
|
||||
Object(ObjectTemplate),
|
||||
/// A portal placed per occurrence.
|
||||
Portal(PortalTemplate),
|
||||
/// The player's start cell.
|
||||
Player,
|
||||
}
|
||||
|
||||
/// Resolves one palette entry to a [`Resolved`], recording any nonfatal problem.
|
||||
///
|
||||
/// Unknown archetype names become a visible [`Archetype::ErrorBlock`]; a `floor`
|
||||
/// with an unknown generator or a `portal` missing required fields falls back to a
|
||||
/// transparent cell (the grid char is still consumed). Object/floor/archetype
|
||||
/// glyphs fall back to the relevant default for any absent visual field.
|
||||
fn resolve_entry(ch: char, e: &PaletteEntry, errors: &mut Vec<LogLine>) -> Resolved {
|
||||
// Builds a glyph from the entry's tile/fg/bg, each falling back to `default`.
|
||||
let glyph_with_default = |default: Glyph| Glyph {
|
||||
tile: e.tile.map(TileIndex::into_u32).unwrap_or(default.tile),
|
||||
fg: e.fg.as_deref().map(parse_color).unwrap_or(default.fg),
|
||||
bg: e.bg.as_deref().map(parse_color).unwrap_or(default.bg),
|
||||
};
|
||||
|
||||
match e.kind.as_str() {
|
||||
// Transparent: lower layers show through here.
|
||||
"empty" => Resolved::Cell(Glyph::transparent(), Archetype::Empty),
|
||||
"floor" => match &e.generator {
|
||||
Some(name) => match FloorGenerator::from_name(name) {
|
||||
Some(g) => Resolved::FloorGen(g),
|
||||
None => {
|
||||
errors.push(LogLine::error(format!(
|
||||
"floor palette '{ch}' names unknown generator '{name}'; using empty floor"
|
||||
)));
|
||||
Resolved::Cell(Glyph::transparent(), Archetype::Empty)
|
||||
}
|
||||
},
|
||||
// A fixed floor glyph: a visual-only cell (no archetype behavior).
|
||||
None => Resolved::Cell(
|
||||
glyph_with_default(Glyph {
|
||||
tile: 32,
|
||||
..Glyph::transparent()
|
||||
}),
|
||||
Archetype::Empty,
|
||||
),
|
||||
},
|
||||
"object" => Resolved::Object(ObjectTemplate {
|
||||
glyph: glyph_with_default(ObjectDef::default_glyph()),
|
||||
solid: e.solid.unwrap_or(true),
|
||||
opaque: e.opaque.unwrap_or(true),
|
||||
pushable: e.pushable.unwrap_or(false),
|
||||
script_name: e.script_name.clone(),
|
||||
tags: e.tags.clone().unwrap_or_default(),
|
||||
name: e.name.clone(),
|
||||
}),
|
||||
"portal" => match (e.name.clone(), e.target_map.clone(), e.target_entry.clone()) {
|
||||
(Some(name), Some(target_map), Some(target_entry)) => {
|
||||
Resolved::Portal(PortalTemplate {
|
||||
name,
|
||||
target_map,
|
||||
target_entry,
|
||||
})
|
||||
}
|
||||
_ => {
|
||||
errors.push(LogLine::error(format!(
|
||||
"portal palette '{ch}' needs name, target_map and target_entry; skipping"
|
||||
)));
|
||||
Resolved::Cell(Glyph::transparent(), Archetype::Empty)
|
||||
}
|
||||
},
|
||||
"player" => Resolved::Player,
|
||||
// Any other kind must be an archetype name.
|
||||
other => match Archetype::try_from(other) {
|
||||
Ok(a) => Resolved::Cell(glyph_with_default(a.default_glyph()), a),
|
||||
Err(msg) => {
|
||||
errors.push(LogLine::error(format!(
|
||||
"palette '{ch}': {msg}; using error block"
|
||||
)));
|
||||
Resolved::Cell(Archetype::ErrorBlock.default_glyph(), Archetype::ErrorBlock)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds one [`Layer`] from its [`LayerData`], plus the non-terrain placements it
|
||||
/// contains (with their `(x, y)`).
|
||||
///
|
||||
/// `rng` is the shared, deterministically-seeded floor PRNG (one per board build),
|
||||
/// threaded through so procedural floors depend only on map content. Returns
|
||||
/// `Err` only on a grid-dimension mismatch (the single hard error, matching the
|
||||
/// pre-layers loader); every other problem is recorded on `errors`.
|
||||
pub(crate) fn build_layer(
|
||||
data: &LayerData,
|
||||
width: usize,
|
||||
height: usize,
|
||||
rng: &mut StdRand,
|
||||
errors: &mut Vec<LogLine>,
|
||||
) -> Result<(Layer, Vec<Placement>), String> {
|
||||
// Resolve each palette entry once (per-cell work like generators happens below).
|
||||
let resolved: HashMap<char, Resolved> = data
|
||||
.palette
|
||||
.iter()
|
||||
.map(|(key, entry)| {
|
||||
let ch = key.chars().next().unwrap_or(' ');
|
||||
(ch, resolve_entry(ch, entry, errors))
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Validate dimensions before walking — the only fatal error.
|
||||
let rows: Vec<&str> = data.content.lines().collect();
|
||||
if rows.len() != height {
|
||||
return Err(format!(
|
||||
"layer grid has {} rows but the board is {height} tall",
|
||||
rows.len()
|
||||
));
|
||||
}
|
||||
for (i, line) in rows.iter().enumerate() {
|
||||
let cols = line.chars().count();
|
||||
if cols != width {
|
||||
return Err(format!(
|
||||
"layer grid row {i} has {cols} characters but the board is {width} wide"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Walk the grid, filling cells and collecting placements.
|
||||
let mut cells: Vec<(Glyph, Archetype)> = Vec::with_capacity(width * height);
|
||||
let mut placements: Vec<Placement> = Vec::new();
|
||||
for (y, line) in rows.iter().enumerate() {
|
||||
for (x, ch) in line.chars().enumerate() {
|
||||
match resolved.get(&ch) {
|
||||
Some(Resolved::Cell(g, a)) => cells.push((*g, *a)),
|
||||
Some(Resolved::FloorGen(g)) => cells.push((g.generate(rng), Archetype::Empty)),
|
||||
Some(Resolved::Object(t)) => {
|
||||
cells.push((Glyph::transparent(), Archetype::Empty));
|
||||
placements.push(Placement::Object(t.clone(), x, y));
|
||||
}
|
||||
Some(Resolved::Portal(t)) => {
|
||||
cells.push((Glyph::transparent(), Archetype::Empty));
|
||||
placements.push(Placement::Portal(t.clone(), x, y));
|
||||
}
|
||||
Some(Resolved::Player) => {
|
||||
cells.push((Glyph::transparent(), Archetype::Empty));
|
||||
placements.push(Placement::Player(x, y));
|
||||
}
|
||||
None => {
|
||||
errors.push(LogLine::error(format!(
|
||||
"unknown grid character '{ch}' at ({x}, {y}); using error block"
|
||||
)));
|
||||
cells.push((Archetype::ErrorBlock.default_glyph(), Archetype::ErrorBlock));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((Layer { cells }, placements))
|
||||
}
|
||||
Reference in New Issue
Block a user