2026-05-19 00:07:04 -05:00
|
|
|
use crate::game::{Archetype, Board, FontSpec, Glyph, ObjectDef, Player, PortalDef};
|
2026-05-16 01:50:05 -05:00
|
|
|
use eframe::egui::Color32;
|
2026-05-19 00:07:04 -05:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
use std::collections::HashMap;
|
2026-05-16 01:50:05 -05:00
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
/// The top-level deserialization type for a `.toml` map file.
|
|
|
|
|
///
|
|
|
|
|
/// This struct mirrors the TOML file structure exactly and is used only
|
|
|
|
|
/// during loading — it is immediately converted into a [`Board`] via
|
|
|
|
|
/// [`From<MapFile>`] and then discarded. It is never used at runtime.
|
|
|
|
|
///
|
|
|
|
|
/// See `maps/start.toml` for a complete example of the file format.
|
2026-05-16 01:50:05 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct MapFile {
|
2026-05-17 12:48:52 -05:00
|
|
|
/// The `[map]` section: name, dimensions, player start position.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub map: MapHeader,
|
2026-05-17 12:48:52 -05:00
|
|
|
/// The `[palette]` section: maps single-character keys to tile definitions.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub palette: HashMap<String, PaletteEntry>,
|
2026-05-17 12:48:52 -05:00
|
|
|
/// The `[grid]` section: the multi-line string that defines cell layout.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub grid: GridData,
|
2026-05-19 00:07:04 -05:00
|
|
|
/// Optional `[font]` section specifying a bitmap font for this board.
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub font: Option<FontHeader>,
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Any `[[objects]]` entries. Optional; defaults to empty.
|
2026-05-16 01:50:05 -05:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub objects: Vec<ObjectDef>,
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Any `[[portals]]` entries. Optional; defaults to empty.
|
2026-05-16 01:50:05 -05:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub portals: Vec<PortalDef>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
/// The `[map]` header section of a map file.
|
2026-05-16 01:50:05 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct MapHeader {
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Human-readable name for this board, e.g. `"Opening Room"`.
|
2026-05-16 01:50:05 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
|
pub name: String,
|
2026-05-19 00:07:04 -05:00
|
|
|
/// Width of the board in cells. Must match the length of every row in `[grid] content`.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub width: usize,
|
2026-05-19 00:07:04 -05:00
|
|
|
/// Height of the board in cells. Must match the number of rows in `[grid] content`.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub height: usize,
|
2026-05-19 00:07:04 -05:00
|
|
|
/// Starting position of the player as `[x, y]` (0-indexed, origin top-left).
|
2026-05-16 01:50:05 -05:00
|
|
|
pub player_start: [i32; 2],
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 00:07:04 -05:00
|
|
|
/// The `[font]` section of a map file, specifying a bitmap font for this board.
|
|
|
|
|
///
|
|
|
|
|
/// When absent, the app's default embedded CP437 font is used.
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct FontHeader {
|
|
|
|
|
/// Path to the PNG font image, relative to the working directory.
|
|
|
|
|
pub path: String,
|
|
|
|
|
/// Width of each tile in the font image, in pixels.
|
|
|
|
|
pub tile_w: u32,
|
|
|
|
|
/// Height of each tile in the font image, in pixels.
|
|
|
|
|
pub tile_h: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A tile index in a palette entry: either a plain integer or a character literal.
|
|
|
|
|
///
|
|
|
|
|
/// Accepting both forms lets map files use `tile = 32` for new files or
|
|
|
|
|
/// `tile = " "` for convenience (the char is converted to its Unicode scalar).
|
|
|
|
|
/// This means existing maps that used `ch = "#"` can be migrated by renaming
|
|
|
|
|
/// the key to `tile`; the char form keeps working.
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
#[serde(untagged)]
|
|
|
|
|
enum TileIndex {
|
|
|
|
|
/// A direct tile index (e.g. `tile = 35`).
|
|
|
|
|
Num(u32),
|
|
|
|
|
/// A single-character shorthand (e.g. `tile = "#"`); converted to its Unicode scalar.
|
|
|
|
|
Chr(char),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TileIndex {
|
|
|
|
|
fn into_u32(self) -> u32 {
|
|
|
|
|
match self {
|
|
|
|
|
TileIndex::Num(n) => n,
|
|
|
|
|
TileIndex::Chr(c) => c as u32,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
/// One entry in the `[palette]` table.
|
|
|
|
|
///
|
2026-05-19 00:07:04 -05:00
|
|
|
/// Each entry is keyed by a single character (e.g. `"#"` or `" "`).
|
2026-05-17 12:48:52 -05:00
|
|
|
/// That character is used in the `[grid]` content string to place tiles.
|
2026-05-19 00:07:04 -05:00
|
|
|
/// The entry defines both how the tile looks (`tile`, `fg`, `bg`) and which
|
2026-05-17 15:20:46 -05:00
|
|
|
/// [`Archetype`] it uses for behavior.
|
2026-05-17 12:48:52 -05:00
|
|
|
///
|
2026-05-19 00:07:04 -05:00
|
|
|
/// `tile` accepts either an integer (`tile = 35`) or a char (`tile = "#"`).
|
2026-05-16 01:50:05 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct PaletteEntry {
|
2026-05-17 15:20:46 -05:00
|
|
|
/// The archetype name for this tile, e.g. `"wall"` or `"empty"`.
|
|
|
|
|
pub archetype: String,
|
2026-05-19 00:07:04 -05:00
|
|
|
/// The tile index into the board's bitmap font.
|
|
|
|
|
/// Accepts either an integer or a single-character string.
|
|
|
|
|
tile: TileIndex,
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Foreground color as an `"#RRGGBB"` hex string.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub fg: String,
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Background color as an `"#RRGGBB"` hex string.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub bg: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
/// The `[grid]` section of a map file.
|
|
|
|
|
///
|
|
|
|
|
/// `content` is a TOML multi-line string. Each line is one row of the board;
|
|
|
|
|
/// each character in a line is looked up in the palette to determine the
|
2026-05-19 00:07:04 -05:00
|
|
|
/// tile for that cell.
|
2026-05-16 01:50:05 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct GridData {
|
2026-05-17 12:48:52 -05:00
|
|
|
/// The raw grid content. Split by [`str::lines`] during loading.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub content: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Parses an `"#RRGGBB"` hex color string into a [`Color32`].
|
|
|
|
|
/// Returns black on any parse failure.
|
2026-05-16 01:50:05 -05:00
|
|
|
fn parse_color(hex: &str) -> Color32 {
|
|
|
|
|
let hex = hex.trim_start_matches('#');
|
|
|
|
|
if hex.len() != 6 {
|
|
|
|
|
return Color32::BLACK;
|
|
|
|
|
}
|
|
|
|
|
let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(0);
|
|
|
|
|
let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(0);
|
|
|
|
|
let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(0);
|
|
|
|
|
Color32::from_rgb(r, g, b)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Converts a parsed map file into a runtime [`Board`].
|
|
|
|
|
///
|
|
|
|
|
/// The conversion proceeds in two passes:
|
|
|
|
|
///
|
2026-05-17 15:20:46 -05:00
|
|
|
/// 1. **Palette pass** — each entry is parsed into a `(Glyph, Archetype)` pair
|
|
|
|
|
/// and stored in a temporary `HashMap` keyed by the palette character.
|
2026-05-19 00:07:04 -05:00
|
|
|
/// Unknown archetype names produce an [`Archetype::ErrorBlock`] and a logged
|
|
|
|
|
/// warning so malformed maps are visible in-game.
|
2026-05-17 12:48:52 -05:00
|
|
|
///
|
2026-05-19 00:07:04 -05:00
|
|
|
/// 2. **Grid pass** — `grid.content` is split into lines; each character is
|
|
|
|
|
/// looked up in the palette map and pushed directly into `cells`.
|
2026-05-16 01:50:05 -05:00
|
|
|
impl From<MapFile> for Board {
|
|
|
|
|
fn from(mf: MapFile) -> Self {
|
|
|
|
|
let w = mf.map.width;
|
|
|
|
|
let h = mf.map.height;
|
|
|
|
|
|
2026-05-17 15:20:46 -05:00
|
|
|
// Pass 1: build a char → (Glyph, Archetype) lookup from the palette.
|
|
|
|
|
let mut palette: HashMap<char, (Glyph, Archetype)> = HashMap::new();
|
2026-05-16 01:50:05 -05:00
|
|
|
|
2026-05-19 00:07:04 -05:00
|
|
|
for (key, entry) in mf.palette {
|
2026-05-16 01:50:05 -05:00
|
|
|
let key_char = key.chars().next().unwrap_or(' ');
|
2026-05-19 00:07:04 -05:00
|
|
|
let tile = entry.tile.into_u32();
|
2026-05-17 15:20:46 -05:00
|
|
|
match Archetype::try_from(entry.archetype.as_str()) {
|
|
|
|
|
Ok(archetype) => {
|
2026-05-19 00:07:04 -05:00
|
|
|
let glyph = Glyph {
|
|
|
|
|
tile,
|
|
|
|
|
fg: parse_color(&entry.fg),
|
|
|
|
|
bg: parse_color(&entry.bg),
|
|
|
|
|
};
|
2026-05-17 15:20:46 -05:00
|
|
|
palette.insert(key_char, (glyph, archetype));
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("{e}");
|
2026-05-19 00:07:04 -05:00
|
|
|
palette.insert(
|
|
|
|
|
key_char,
|
|
|
|
|
(Archetype::ErrorBlock.default_glyph(), Archetype::ErrorBlock),
|
|
|
|
|
);
|
2026-05-17 15:20:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-16 01:50:05 -05:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
// Pass 2: walk the grid string and build cells.
|
2026-05-17 15:20:46 -05:00
|
|
|
let mut cells: Vec<(Glyph, Archetype)> = Vec::with_capacity(w * h);
|
2026-05-16 01:50:05 -05:00
|
|
|
for line in mf.grid.content.lines() {
|
|
|
|
|
for ch in line.chars() {
|
2026-05-17 15:20:46 -05:00
|
|
|
if let Some(&cell) = palette.get(&ch) {
|
|
|
|
|
cells.push(cell);
|
2026-05-16 01:50:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 00:07:04 -05:00
|
|
|
let font = mf.font.map(|f| FontSpec {
|
|
|
|
|
path: f.path,
|
|
|
|
|
tile_w: f.tile_w,
|
|
|
|
|
tile_h: f.tile_h,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-16 01:50:05 -05:00
|
|
|
Board {
|
|
|
|
|
width: w,
|
|
|
|
|
height: h,
|
|
|
|
|
cells,
|
|
|
|
|
player: Player {
|
|
|
|
|
x: mf.map.player_start[0],
|
|
|
|
|
y: mf.map.player_start[1],
|
|
|
|
|
},
|
|
|
|
|
objects: mf.objects,
|
|
|
|
|
portals: mf.portals,
|
2026-05-19 00:07:04 -05:00
|
|
|
font,
|
2026-05-16 01:50:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 12:48:52 -05:00
|
|
|
/// Loads a map file from disk and returns a ready-to-use [`Board`].
|
|
|
|
|
///
|
|
|
|
|
/// Reads the file at `path`, deserializes it as TOML into a [`MapFile`],
|
|
|
|
|
/// then converts it via [`From<MapFile>`]. Propagates both I/O errors and
|
|
|
|
|
/// TOML parse errors through the `Box<dyn Error>` return.
|
2026-05-16 01:50:05 -05:00
|
|
|
pub fn load(path: &str) -> Result<Board, Box<dyn std::error::Error>> {
|
|
|
|
|
let content = std::fs::read_to_string(path)?;
|
|
|
|
|
let map_file: MapFile = toml::from_str(&content)?;
|
|
|
|
|
Ok(Board::from(map_file))
|
2026-05-17 16:12:03 -05:00
|
|
|
}
|