This commit is contained in:
2026-07-14 23:48:50 -05:00
parent 03185c9c68
commit 5146cc9bcc
25 changed files with 380 additions and 272 deletions
+68 -71
View File
@@ -5,8 +5,9 @@ use crate::glyph::Glyph;
use crate::log::LogLine;
use crate::object_def::ObjectDef;
use crate::utils::Direction;
use crate::utils::{Behavior, ObjectId, PlayerPos, PortalDef, Pushable, RegistryValue, Solid};
use crate::utils::{ObjectId, PlayerPos, PortalDef, Pushable, RegistryValue, Solid};
use std::collections::{BTreeMap, HashMap, HashSet};
use crate::tile::{EnterResponse, Sensor};
/// The result of [`Board::apply_shift`]: any error lines to log, plus the
/// `(from, to)` cell relocations the shift actually performed.
@@ -84,6 +85,8 @@ pub struct Board {
/// Non-solid things placed off the main grid, drawn only where the grid cell is
/// empty (see [`Decoration`]). Normally empty; populated by save files.
pub(crate) decorations: Vec<Decoration>,
/// Non-solid things placed off the main grid, can't affect movement but see other hooks
pub sensors: Vec<Sensor>,
/// Current player position on this board. See [`PlayerPos`] for caveats
/// about its future. Game-global player *stats* live in [`crate::player::Player`].
pub player: PlayerPos,
@@ -176,7 +179,7 @@ impl Board {
// non-solid object (lets invisible trigger objects exist).
let mut nonsolid: Option<Glyph> = None;
for o in self.objects.values().filter(|o| o.x == x && o.y == y) {
if o.solid {
if o.behavior.solid {
return o.glyph;
}
if nonsolid.is_none() && o.glyph.tile != 0 {
@@ -245,17 +248,7 @@ impl Board {
// A solid object shadows the cell it sits on; capture its behavior now.
if let Some(id) = self.solid_object_id_at(x, y) {
let obj = &self.objects[&id];
let behavior = Behavior {
solid: obj.solid,
opaque: obj.opaque,
// ObjectDef stores pushability as a bool meaning "any direction".
pushable: if obj.pushable {
Pushable::Any
} else {
Pushable::No
},
grab: obj.grab,
};
let behavior = obj.behavior;
return Some(Solid::object_at(x, y, id, behavior));
}
// Otherwise the grid cell's terrain archetype may be solid (e.g. a wall).
@@ -282,7 +275,7 @@ impl Board {
/// Panics if `x` or `y` are out of bounds.
pub fn is_opaque_at(&self, x: usize, y: usize) -> bool {
self.get(x, y).1.behavior().opaque
|| self.objects.values().any(|o| o.x == x && o.y == y && o.opaque)
|| self.objects.values().any(|o| o.x == x && o.y == y && o.behavior.opaque)
}
/// Computes lighting + line-of-sight for the player on this board.
@@ -327,8 +320,8 @@ impl Board {
}
// Light-emitting objects: color = their own glyph foreground.
for o in self.objects.values() {
if o.light > 0 {
add_source(&mut lighting, o.x, o.y, o.light, color_to_rgb(o.glyph.fg));
if o.behavior.glow > 0 {
add_source(&mut lighting, o.x, o.y, o.behavior.glow, color_to_rgb(o.glyph.fg));
}
}
// Glowing terrain (e.g. a `Torch` cell): color = the cell's glyph foreground.
@@ -377,7 +370,16 @@ impl Board {
let solid = self.solid_at(cx, cy)?;
// A solid object — directly, or at the end of a crate chain — is the target.
if let Some(id) = solid.object_id() {
return Some(id);
let obj = self.objects.get(&id)?;
let resp = EnterResponse::from(obj.behavior);
if let EnterResponse::Push(p) = resp && !p.allows(dir) {
// This object can't be pushed that way, so, this is a bump. If it's pushable, the
// default push behavior happens (it just gets pushed)
return Some(id);
} else if resp == EnterResponse::Block {
// It's just a wall, block everything
return Some(id);
}
}
// The player is never itself "bumped"; a wall (non-pushable terrain) stops
// the chain with no object behind it. Only a pushable crate is walked
@@ -528,7 +530,7 @@ impl Board {
pub fn non_solid_object_ids_at(&self, x: usize, y: usize) -> Vec<ObjectId> {
self.objects
.iter()
.filter(|(_, o)| o.x == x && o.y == y && !o.solid)
.filter(|(_, o)| o.x == x && o.y == y && !o.behavior.solid)
.map(|(&id, _)| id)
.collect()
}
@@ -536,7 +538,7 @@ impl Board {
/// Returns a borrow of the actual object at `(x, y)` if any
pub fn solid_object_id_at(&self, x: usize, y: usize) -> Option<ObjectId> {
self.objects.iter().find_map(|(&id, o)| {
if o.x == x && o.y == y && o.solid {
if o.x == x && o.y == y && o.behavior.solid {
Some(id)
} else {
None
@@ -551,7 +553,7 @@ impl Board {
/// the object's `grab()` hook fires instead.
pub fn grab_object_at(&self, x: usize, y: usize) -> Option<ObjectId> {
self.objects.iter().find_map(|(&id, o)| {
if o.x == x && o.y == y && o.solid && o.grab {
if o.x == x && o.y == y && EnterResponse::from(o.behavior) == EnterResponse::Grab {
Some(id)
} else {
None
@@ -645,13 +647,7 @@ impl Board {
let beh = b.behavior();
let mut obj = ObjectDef::new(x, y);
obj.glyph = glyph;
obj.solid = beh.solid;
obj.opaque = beh.opaque;
// Carry the archetype's pushability/grab onto the object (pushers and
// spinners are Pushable::No, so they stay unpushable; gems are pushable
// and grabbable).
obj.pushable = beh.pushable != crate::utils::Pushable::No;
obj.grab = beh.grab;
obj.behavior = beh;
obj.builtin_script = Some(b.script());
// The compile-cache key and the BUILTIN_* tag are both derived from the
// alias (e.g. "BUILTIN_pusher_east"), so each alias gets its own cached
@@ -758,7 +754,7 @@ pub(crate) mod tests {
use crate::floor::Floor;
use crate::glyph::Glyph;
use crate::object_def::ObjectDef;
use crate::utils::Direction;
use crate::utils::{Direction, Pushable};
use crate::utils::{ObjectId, PlayerPos};
use color::Rgba8;
use std::collections::{BTreeMap, HashMap};
@@ -789,6 +785,7 @@ pub(crate) mod tests {
grid: vec![(Glyph::transparent(), Archetype::Empty); w * h],
floor: Floor::Blank,
decorations: Vec::new(),
sensors: Vec::new(),
player: PlayerPos {
x: player.0,
y: player.1,
@@ -811,12 +808,12 @@ pub(crate) mod tests {
/// Stamps a crate cell onto the grid.
pub(crate) fn crate_at(board: &mut Board, x: usize, y: usize) {
*board.get_mut(x, y) = (Archetype::Crate.default_glyph(), Archetype::Crate);
*board.get_mut(x, y) = (Archetype::Builtin(Builtin::Crate, "crate").default_glyph(), Archetype::Builtin(Builtin::Crate, "crate"));
}
/// Stamps a wall cell onto the grid.
pub(crate) fn wall_at(board: &mut Board, x: usize, y: usize) {
*board.get_mut(x, y) = (Archetype::Wall.default_glyph(), Archetype::Wall);
*board.get_mut(x, y) = (Builtin::Wall.default_glyph_for("wall"), Archetype::Builtin(Builtin::Wall, "wall"));
}
/// Stamps an arbitrary archetype cell onto the grid.
@@ -824,7 +821,7 @@ pub(crate) mod tests {
*board.get_mut(x, y) = (arch.default_glyph(), arch);
}
#[test]
#[test]
fn solid_at_reports_wall_object_and_empty() {
let mut board = open_board(4, 1, (3, 0), vec![]);
wall_at(&mut board, 1, 0);
@@ -834,7 +831,7 @@ pub(crate) mod tests {
assert!(board.is_passable(0, 0));
let wall = board.solid_at(1, 0).expect("a wall is solid");
assert_eq!(wall.archetype(), Some(Archetype::Wall));
assert_eq!(wall.archetype(), Some(Archetype::Builtin(Builtin::Wall, "wall")));
assert!(!board.is_passable(1, 0));
let obj = board.solid_at(2, 0).expect("an object is solid");
@@ -847,8 +844,8 @@ pub(crate) mod tests {
fn grab_object_at_detects_a_gem() {
// A grabbable gem object at (1,0); the player at (2,0).
let mut gem = ObjectDef::new(1, 0);
gem.grab = true;
gem.pushable = true;
gem.behavior.grab = true;
gem.behavior.pushable = Pushable::Any;
let board = open_board(3, 1, (2, 0), vec![gem]);
// grab_object_at finds the gem on its own cell, nowhere else.
@@ -859,7 +856,7 @@ pub(crate) mod tests {
#[test]
fn non_solid_object_does_not_block() {
let mut obj = ObjectDef::new(1, 0);
obj.solid = false;
obj.behavior.solid = false;
let board = open_board(3, 1, (0, 0), vec![obj]);
assert!(board.solid_at(1, 0).is_none());
assert!(board.is_passable(1, 0));
@@ -891,7 +888,7 @@ pub(crate) mod tests {
let mut board = open_board(3, 1, (0, 0), vec![]);
crate_at(&mut board, 1, 0);
assert!(board.can_push(1, 0, Direction::East));
assert_eq!(board.get(1, 0).1, Archetype::Crate); // no mutation
assert_eq!(board.get(1, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // no mutation
assert_eq!(board.get(2, 0).1, Archetype::Empty);
let mut board = open_board(3, 1, (0, 0), vec![]);
@@ -909,7 +906,7 @@ pub(crate) mod tests {
// Crate with open space ahead: shiftable.
crate_at(&mut board, 0, 0);
assert!(board.can_shift(0, 0, Direction::East));
assert_eq!(board.get(0, 0).1, Archetype::Crate); // read-only
assert_eq!(board.get(0, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // read-only
// Crate with another pushable crate ahead: still shiftable (unlike can_push,
// which would follow the chain to the wall and fail).
@@ -937,8 +934,8 @@ pub(crate) mod tests {
// The player is always a blocker for a shift — even a grab gem may not shift
// onto it (grab now fires only on player movement, not on being shifted in).
let mut gem = ObjectDef::new(0, 0);
gem.grab = true;
gem.pushable = true;
gem.behavior.grab = true;
gem.behavior.pushable = Pushable::Any;
let board = open_board(2, 1, (1, 0), vec![gem]);
assert!(!board.can_shift(0, 0, Direction::East));
@@ -956,7 +953,7 @@ pub(crate) mod tests {
assert!(board.can_push(1, 0, Direction::East));
board.push(1, 0, Direction::East);
assert_eq!(board.get(1, 0).1, Archetype::Empty);
assert_eq!(board.get(2, 0).1, Archetype::Crate);
assert_eq!(board.get(2, 0).1, Archetype::Builtin(Builtin::Crate, "crate"));
assert_eq!((board.player.x, board.player.y), (3, 0));
}
@@ -968,7 +965,7 @@ pub(crate) mod tests {
wall_at(&mut board, 3, 0);
assert!(!board.can_push(1, 0, Direction::East));
board.push(1, 0, Direction::East); // no-op
assert_eq!(board.get(1, 0).1, Archetype::Crate);
assert_eq!(board.get(1, 0).1, Archetype::Builtin(Builtin::Crate, "crate"));
assert_eq!((board.player.x, board.player.y), (2, 0));
}
@@ -995,7 +992,7 @@ pub(crate) mod tests {
add_floor(&mut board, floor_glyph);
wall_at(&mut board, 0, 0);
// The wall (solid) draws over the floor; the empty cell reveals the floor.
assert_eq!(board.glyph_at(0, 0), Archetype::Wall.default_glyph());
assert_eq!(board.glyph_at(0, 0), Builtin::Wall.default_glyph_for("wall"));
assert_eq!(board.glyph_at(1, 0), floor_glyph);
}
@@ -1009,11 +1006,11 @@ pub(crate) mod tests {
};
add_floor(&mut board, floor);
let wall = Archetype::Wall.default_glyph();
board.place_archetype(1, 0, Archetype::Wall, wall);
let wall = Builtin::Wall.default_glyph_for("wall");
board.place_archetype(1, 0, Archetype::Builtin(Builtin::Wall, "wall"), wall);
// The wall landed on the grid; the floor attribute is untouched.
assert_eq!(board.get(1, 0), &(wall, Archetype::Wall));
assert_eq!(board.get(1, 0), &(wall, Archetype::Builtin(Builtin::Wall, "wall")));
// The solid object that was there is gone.
assert!(board.object_ids_at(1, 0).is_empty());
assert_eq!(board.glyph_at(1, 0), wall);
@@ -1024,16 +1021,16 @@ pub(crate) mod tests {
// A crate already occupies the grid at (1,0).
let mut board = open_board(3, 1, (2, 0), vec![]);
crate_at(&mut board, 1, 0);
let wall = Archetype::Wall.default_glyph();
board.place_archetype(1, 0, Archetype::Wall, wall);
assert_eq!(board.get(1, 0), &(wall, Archetype::Wall));
let wall = Builtin::Wall.default_glyph_for("wall");
board.place_archetype(1, 0, Archetype::Builtin(Builtin::Wall, "wall"), wall);
assert_eq!(board.get(1, 0), &(wall, Archetype::Builtin(Builtin::Wall, "wall")));
}
#[test]
fn erase_removes_terrain_and_objects_but_keeps_floor() {
// A fixed floor attribute, a wall on the grid, and a (non-solid) object at (1,0).
let mut obj = ObjectDef::new(1, 0);
obj.solid = false;
obj.behavior.solid = false;
let mut board = open_board(3, 1, (2, 0), vec![obj]);
let floor = Glyph {
tile: '.' as u32,
@@ -1072,7 +1069,7 @@ pub(crate) mod tests {
assert_eq!(board.get(0, 0).1, Archetype::Empty);
let obj = board.objects.values().next().expect("spinner object");
assert_eq!((obj.x, obj.y), (0, 0));
assert!(obj.solid);
assert!(obj.behavior.solid);
assert!(obj.builtin_script.is_some());
assert!(obj.tags.contains("BUILTIN_spinner_cw"));
@@ -1098,7 +1095,7 @@ pub(crate) mod tests {
crate_at(&mut board, 0, 0);
let errs = board.apply_shift(&[(0, 0), (9, 0)]);
assert_eq!(errs.errors.len(), 1);
assert_eq!(board.get(0, 0).1, Archetype::Crate); // unchanged
assert_eq!(board.get(0, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // unchanged
}
#[test]
@@ -1122,11 +1119,11 @@ pub(crate) mod tests {
// Crate(3,0)→(4,0), Crate(4,0)→(0,0) wrap
let _errs = board.apply_shift(&[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0)]);
assert_eq!(board.get(0, 0).1, Archetype::Crate); // wrapped from (4,0)
assert_eq!(board.get(1, 0).1, Archetype::Crate); // moved from (0,0)
assert_eq!(board.get(2, 0).1, Archetype::Wall); // blocked, immobile
assert_eq!(board.get(0, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // wrapped from (4,0)
assert_eq!(board.get(1, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // moved from (0,0)
assert_eq!(board.get(2, 0).1, Archetype::Builtin(Builtin::Wall, "wall")); // blocked, immobile
assert_eq!(board.get(3, 0).1, Archetype::Empty); // cleared, crate moved
assert_eq!(board.get(4, 0).1, Archetype::Crate); // moved from (3,0)
assert_eq!(board.get(4, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // moved from (3,0)
}
#[test]
@@ -1139,21 +1136,21 @@ pub(crate) mod tests {
// Subcase A: HCrate moves
{
let mut board = open_board(4, 1, (3, 0), vec![]);
stamp(&mut board, 0, 0, Archetype::HCrate);
stamp(&mut board, 0, 0, Archetype::Builtin(Builtin::HCrate, "hcrate"));
crate_at(&mut board, 1, 0);
// (2,0) empty
let _errs = board.apply_shift(&[(0, 0), (1, 0), (2, 0)]);
assert_eq!(board.get(0, 0).1, Archetype::Empty); // HCrate moved out
assert_eq!(board.get(1, 0).1, Archetype::HCrate); // moved from (0,0)
assert_eq!(board.get(2, 0).1, Archetype::Crate); // moved from (1,0)
assert_eq!(board.get(1, 0).1, Archetype::Builtin(Builtin::HCrate, "hcrate")); // moved from (0,0)
assert_eq!(board.get(2, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // moved from (1,0)
}
// Subcase B: VCrate stays; Crate behind it still moves
{
let mut board = open_board(4, 1, (3, 0), vec![]);
stamp(&mut board, 0, 0, Archetype::VCrate);
stamp(&mut board, 0, 0, Archetype::Builtin(Builtin::VCrate, "vcrate"));
crate_at(&mut board, 1, 0);
// (2,0) empty
@@ -1163,9 +1160,9 @@ pub(crate) mod tests {
// Crate at (1,0) is NOT in blocked, so it moves.
let _errs = board.apply_shift(&[(0, 0), (1, 0), (2, 0)]);
assert_eq!(board.get(0, 0).1, Archetype::VCrate); // immobile
assert_eq!(board.get(0, 0).1, Archetype::Builtin(Builtin::VCrate, "vcrate")); // immobile
assert_eq!(board.get(1, 0).1, Archetype::Empty); // crate moved out
assert_eq!(board.get(2, 0).1, Archetype::Crate); // moved from (1,0)
assert_eq!(board.get(2, 0).1, Archetype::Builtin(Builtin::Crate, "crate")); // moved from (1,0)
}
}
@@ -1178,22 +1175,22 @@ pub(crate) mod tests {
// Subcase A: VCrate moves
{
let mut board = open_board(1, 4, (0, 3), vec![]);
stamp(&mut board, 0, 0, Archetype::VCrate);
stamp(&mut board, 0, 1, Archetype::Crate);
stamp(&mut board, 0, 0, Archetype::Builtin(Builtin::VCrate, "vcrate"));
stamp(&mut board, 0, 1, Archetype::Builtin(Builtin::Crate, "crate"));
// (0,2) empty
let _errs = board.apply_shift(&[(0, 0), (0, 1), (0, 2)]);
assert_eq!(board.get(0, 0).1, Archetype::Empty); // VCrate moved out
assert_eq!(board.get(0, 1).1, Archetype::VCrate); // moved from (0,0)
assert_eq!(board.get(0, 2).1, Archetype::Crate); // moved from (0,1)
assert_eq!(board.get(0, 1).1, Archetype::Builtin(Builtin::VCrate, "vcrate")); // moved from (0,0)
assert_eq!(board.get(0, 2).1, Archetype::Builtin(Builtin::Crate, "crate")); // moved from (0,1)
}
// Subcase B: HCrate stays; Crate behind it still moves
{
let mut board = open_board(1, 4, (0, 3), vec![]);
stamp(&mut board, 0, 0, Archetype::HCrate);
stamp(&mut board, 0, 1, Archetype::Crate);
stamp(&mut board, 0, 0, Archetype::Builtin(Builtin::HCrate, "hcrate"));
stamp(&mut board, 0, 1, Archetype::Builtin(Builtin::Crate, "crate"));
// (0,2) empty
// HCrate (idx 0): target (0,1), origin (0,0), vmove=true.
@@ -1202,9 +1199,9 @@ pub(crate) mod tests {
// Crate at (0,1) is NOT in blocked, so it moves.
let _errs = board.apply_shift(&[(0, 0), (0, 1), (0, 2)]);
assert_eq!(board.get(0, 0).1, Archetype::HCrate); // immobile
assert_eq!(board.get(0, 0).1, Archetype::Builtin(Builtin::HCrate, "hcrate")); // immobile
assert_eq!(board.get(0, 1).1, Archetype::Empty); // crate moved out
assert_eq!(board.get(0, 2).1, Archetype::Crate); // moved from (0,1)
assert_eq!(board.get(0, 2).1, Archetype::Builtin(Builtin::Crate, "crate")); // moved from (0,1)
}
}
@@ -1261,8 +1258,8 @@ pub(crate) mod tests {
let mut board = open_board(3, 1, (1, 0), vec![]);
board.dark = true;
let mut lamp = ObjectDef::new(1, 0);
lamp.solid = false;
lamp.light = 4;
lamp.behavior.solid = false;
lamp.behavior.glow = 4;
lamp.glyph = Glyph { tile: 1, fg: Rgba8 { r: 255, g: 0, b: 0, a: 255 }, bg: Rgba8 { r: 0, g: 0, b: 0, a: 255 } };
board.add_object(lamp);