Files
kiln/kiln-core/src/game.rs
T

446 lines
19 KiB
Rust
Raw Normal View History

2026-06-09 22:38:17 -05:00
use crate::action::Action;
2026-06-13 21:33:38 -05:00
use crate::archetype::Archetype;
2026-06-13 01:25:58 -05:00
use crate::board::Board;
2026-06-03 22:46:54 -05:00
use crate::log::LogLine;
2026-06-08 22:15:44 -05:00
use crate::script::ScriptHost;
2026-06-13 01:25:58 -05:00
use crate::world::World;
use std::cell::{Ref, RefMut};
2026-06-04 20:11:55 -05:00
use std::time::Duration;
2026-06-13 16:24:29 -05:00
use crate::utils::{Direction, ObjectId, Player, ScriptArg, Solid};
2026-06-08 22:15:44 -05:00
/// How long a `say()` speech bubble stays on screen, in seconds.
pub const SAY_DURATION: f64 = 3.0;
2026-06-13 21:33:38 -05:00
/// How often pusher archetypes fire, in seconds.
const PUSHER_INTERVAL: f64 = 0.5;
2026-06-10 01:42:33 -05:00
// Re-export ScrollLine so kiln-tui can pattern-match scroll content without
// accessing the private `action` module directly.
pub use crate::action::ScrollLine;
/// An active scroll overlay opened by a scripted object via `scroll()`.
///
/// While a `Scroll` is present on [`GameState`], the front-end should pause ticks
/// and display the overlay. Closed via [`GameState::close_scroll`].
pub struct Scroll {
/// The object whose `scroll()` call opened this overlay.
pub source: ObjectId,
/// The lines of content to display.
pub lines: Vec<ScrollLine>,
}
2026-06-08 22:15:44 -05:00
/// An active speech bubble emitted by a scripted object via `say()`.
pub struct SpeechBubble {
/// The object whose `say()` call created this bubble.
pub object_id: ObjectId,
/// The text to display.
pub text: String,
/// Seconds remaining before the bubble disappears.
pub remaining: f64,
}
2026-06-04 23:52:33 -05:00
/// Holds the active game world and provides game-logic operations.
///
/// `GameState` is the boundary between the engine (rendering, input) and the
2026-06-13 01:25:58 -05:00
/// game data. It owns a [`World`] (all boards + world scripts) and tracks which
/// board is currently active. Front-ends reach the active board through
/// [`board`](GameState::board) / [`board_mut`](GameState::board_mut). Scripts
/// read the board and queue mutations via commands applied by `resolve` after
/// each script batch.
pub struct GameState {
2026-06-13 01:25:58 -05:00
/// All boards and world-level scripts.
world: World,
/// Key of the currently active board in [`world.boards`](World::boards).
current_board_name: String,
2026-06-03 22:46:54 -05:00
/// The in-game message log, oldest first (newest pushed at the end).
pub log: Vec<LogLine>,
2026-06-13 01:25:58 -05:00
/// The Rhai scripting runtime for the active board's objects.
2026-06-04 20:11:55 -05:00
scripts: ScriptHost,
2026-06-08 22:15:44 -05:00
/// Active speech bubbles from `say()` calls, displayed by the front-end over the board.
pub speech_bubbles: Vec<SpeechBubble>,
2026-06-10 01:42:33 -05:00
/// An active scroll overlay opened by `scroll()`. `Some` while the overlay is
/// visible; the front-end pauses ticks and closes it via [`close_scroll`](GameState::close_scroll).
pub active_scroll: Option<Scroll>,
2026-06-13 16:24:29 -05:00
/// Remaining seconds for the board-entry transition animation, set to `1.0`
/// by [`enter_board`](GameState::enter_board). Front-ends tick this down and
/// may block input or show a visual effect while it is `Some(t)` where `t > 0`.
pub board_transition: Option<f64>,
2026-06-13 21:33:38 -05:00
/// Countdown to the next global pusher heartbeat. When it reaches zero all
/// [`Archetype::Pusher`] cells fire and the timer resets to [`PUSHER_INTERVAL`].
pusher_timer: f64,
}
impl GameState {
2026-06-13 01:25:58 -05:00
/// Creates a [`GameState`] from a loaded [`World`], starting on `world.start`.
2026-06-04 20:11:55 -05:00
///
2026-06-13 01:25:58 -05:00
/// The starting board's objects are compiled and registered with the
/// [`ScriptHost`] using `world.scripts`. No lifecycle hooks are run here;
/// call [`run_init`](GameState::run_init) once the game is ready to start.
/// Compile errors are surfaced into the log immediately.
pub fn from_world(world: World) -> Self {
let name = world.start.clone();
let host = ScriptHost::new(
world.boards.get(&name).expect("world::load guarantees start board exists"),
&world.scripts,
);
2026-06-04 20:11:55 -05:00
let mut state = Self {
2026-06-13 01:25:58 -05:00
world,
current_board_name: name,
2026-06-03 22:46:54 -05:00
log: Vec::new(),
2026-06-13 01:25:58 -05:00
scripts: host,
2026-06-08 22:15:44 -05:00
speech_bubbles: Vec::new(),
2026-06-10 01:42:33 -05:00
active_scroll: None,
2026-06-13 16:24:29 -05:00
board_transition: None,
2026-06-13 21:33:38 -05:00
pusher_timer: PUSHER_INTERVAL,
2026-06-04 20:11:55 -05:00
};
2026-06-06 17:36:00 -05:00
state.drain_errors();
2026-06-04 20:11:55 -05:00
state
2026-06-03 22:46:54 -05:00
}
2026-06-13 01:25:58 -05:00
/// Creates a `GameState` from a single [`Board`] with no scripts.
///
/// Test-only convenience. Use [`from_world`](GameState::from_world) in production.
#[cfg(test)]
pub fn new(board: Board) -> Self {
Self::with_scripts(board, std::collections::HashMap::new())
}
/// Creates a `GameState` from a single [`Board`] and an explicit script pool.
///
/// Test-only convenience. Use [`from_world`](GameState::from_world) in production.
#[cfg(test)]
pub fn with_scripts(board: Board, scripts: std::collections::HashMap<String, String>) -> Self {
// Wrap the bare board in Rc<RefCell<Board>> to match World::boards storage.
let board_ref = std::rc::Rc::new(std::cell::RefCell::new(board));
let world = World {
name: String::new(),
start: "board".to_string(),
scripts,
boards: std::collections::HashMap::from([("board".to_string(), board_ref)]),
};
Self::from_world(world)
}
2026-06-04 23:52:33 -05:00
/// Borrows the active board for reading (e.g. by a front-end renderer).
pub fn board(&self) -> Ref<'_, Board> {
2026-06-13 01:25:58 -05:00
self.world.boards[&self.current_board_name].borrow()
2026-06-04 23:52:33 -05:00
}
/// Borrows the active board for mutation.
pub fn board_mut(&self) -> RefMut<'_, Board> {
2026-06-13 01:25:58 -05:00
self.world.boards[&self.current_board_name].borrow_mut()
}
/// The name (key) of the currently active board within the world.
pub fn current_board_name(&self) -> &str {
&self.current_board_name
2026-06-04 23:52:33 -05:00
}
2026-06-13 18:43:29 -05:00
/// Returns a clone of the `Rc` for the active board.
///
/// Lets a front-end hold a reference to the current board across a board
/// transition — the old board stays alive in `world.boards`, so the clone
/// keeps it reachable even after `current_board_name` changes.
pub fn board_rc(&self) -> std::rc::Rc<std::cell::RefCell<Board>> {
self.world.boards[&self.current_board_name].clone()
}
2026-06-03 22:46:54 -05:00
/// Appends a styled message to the log.
pub fn log(&mut self, line: LogLine) {
self.log.push(line);
}
2026-06-06 17:36:00 -05:00
/// Runs the `init()` hook of every scripted object, pumps their queues, and
/// resolves the resulting actions. Call once, after the whole map is loaded and
/// the game is about to start — never during map deserialization, since a script
/// may inspect the board.
2026-06-04 20:11:55 -05:00
pub fn run_init(&mut self) {
self.scripts.run_init();
2026-06-06 17:36:00 -05:00
self.resolve();
2026-06-04 20:11:55 -05:00
}
2026-06-06 17:36:00 -05:00
/// Advances real-time game state by `dt` (the elapsed time since the last tick).
/// Called once per frame by the front-end's game loop. Counts down object
/// cooldowns, drives every scripted object's `tick(dt)` hook (pumping each queue),
/// then resolves the actions that were promoted onto the board queue.
2026-06-03 23:21:05 -05:00
pub fn tick(&mut self, dt: Duration) {
2026-06-06 17:36:00 -05:00
let secs = dt.as_secs_f64();
self.scripts.run_tick(secs);
2026-06-08 22:15:44 -05:00
// Expire speech bubbles before resolving new actions so a fresh say()
// this frame isn't immediately culled.
self.speech_bubbles.retain_mut(|b| { b.remaining -= secs; b.remaining > 0.0 });
2026-06-13 21:33:38 -05:00
self.tick_pushers(secs);
2026-06-06 17:36:00 -05:00
self.resolve();
}
2026-06-13 21:33:38 -05:00
/// Fires the global pusher heartbeat: every [`PUSHER_INTERVAL`] seconds each
/// [`Archetype::Pusher`] cell shoves the occupant directly in front of it.
///
/// Two-phase to avoid holding the board borrow while calling `push()` (which
/// needs `&mut Board`): phase A collects target coordinates, phase B applies them.
fn tick_pushers(&mut self, secs: f64) {
self.pusher_timer -= secs;
if self.pusher_timer > 0.0 {
return;
}
self.pusher_timer += PUSHER_INTERVAL;
let pushers: Vec<(usize, usize)> = {
let board = self.board();
let mut v = Vec::new();
for y in 0..board.height {
for x in 0..board.width {
if matches!(board.get(x, y).1, Archetype::Pusher(_)) {
v.push((x, y));
}
}
}
v
};
let mut board = self.board_mut();
for (x, y) in pushers {
board.advance_pusher(x, y);
}
}
2026-06-06 17:36:00 -05:00
/// Drains errors collected by the script host into the game log.
fn drain_errors(&mut self) {
// TODO: errors are only logged for now. This is the place to halt execution /
// set an error state when a script faults.
let errors = self.scripts.take_errors();
self.log.extend(errors);
}
/// Resolves the board queue: applies each promoted action to the board, then fires
2026-06-09 22:38:17 -05:00
/// the `bump` and `send` hooks the moves triggered.
2026-06-06 17:36:00 -05:00
///
2026-06-09 22:38:17 -05:00
/// Done in two phases so no `board_mut` borrow is held while scripts run
2026-06-06 17:36:00 -05:00
/// (they read the board through its getters): phase A mutates the board and records
2026-06-09 22:38:17 -05:00
/// `(bumped, bumper)` and `(send_target, fn_name, arg)` tuples; phase B fires the
/// hooks after the borrow drops.
2026-06-06 17:36:00 -05:00
fn resolve(&mut self) {
let actions = self.scripts.take_board_queue();
// Logs are collected here rather than pushed inline, since the board borrow
// below also borrows `self`.
let mut logs: Vec<LogLine> = Vec::new();
2026-06-06 20:01:07 -05:00
let mut bumps: Vec<(ObjectId, i64)> = Vec::new();
2026-06-09 22:38:17 -05:00
let mut sends: Vec<(ObjectId, String, Option<ScriptArg>)> = Vec::new();
2026-06-08 22:15:44 -05:00
let mut new_bubbles: Vec<SpeechBubble> = Vec::new();
2026-06-10 01:42:33 -05:00
// Collected outside the board borrow so we can assign to self.active_scroll.
let mut new_scroll: Option<Scroll> = None;
2026-06-06 17:36:00 -05:00
{
let mut board = self.board_mut();
for ba in actions {
match ba.action {
Action::Move(dir) => {
if let Some(bumped) = step_object(&mut board, ba.source, dir) {
bumps.push((bumped, ba.source as i64));
}
}
Action::SetTile(tile) => {
2026-06-06 20:01:07 -05:00
if let Some(obj) = board.objects.get_mut(&ba.source) {
2026-06-06 17:36:00 -05:00
obj.glyph.tile = tile;
}
2026-06-04 23:52:33 -05:00
}
2026-06-06 17:36:00 -05:00
Action::Log(line) => logs.push(line),
2026-06-07 01:26:18 -05:00
Action::SetTag { target, tag, present } => {
if let Some(obj) = board.objects.get_mut(&target) {
if present { obj.tags.insert(tag); } else { obj.tags.remove(&tag); }
}
}
2026-06-08 22:15:44 -05:00
// Replace any existing bubble from this object so repeated say() calls
// don't stack visually — the new text resets the timer.
2026-06-13 15:33:04 -05:00
Action::Say(text, duration) => new_bubbles.push(SpeechBubble {
2026-06-08 22:15:44 -05:00
object_id: ba.source,
text,
2026-06-13 15:33:04 -05:00
remaining: duration,
2026-06-08 22:15:44 -05:00
}),
// Delays are consumed by ScriptHost::drain and never reach the board queue.
Action::Delay(_) => {}
2026-06-09 22:38:17 -05:00
Action::SetColor { fg, bg } => {
if let Some(obj) = board.objects.get_mut(&ba.source) {
if let Some(c) = fg { obj.glyph.fg = c; }
if let Some(c) = bg { obj.glyph.bg = c; }
}
}
// Collected and fired after the board borrow drops, like bumps.
Action::Send { target, fn_name, arg } => {
sends.push((target, fn_name, arg));
}
2026-06-10 01:42:33 -05:00
// Later scrolls overwrite earlier ones from the same tick.
Action::Scroll(lines) => {
new_scroll = Some(Scroll { source: ba.source, lines });
}
2026-06-13 17:58:04 -05:00
Action::Teleport { x, y } => {
if !board.in_bounds((x, y)) {
logs.push(LogLine::error(format!(
"teleport({x},{y}): out of bounds"
)));
} else {
let (ux, uy) = (x as usize, y as usize);
let source_solid = board.objects.get(&ba.source)
.map(|o| o.solid)
.unwrap_or(false);
if source_solid && board.solid_at(ux, uy).is_some() {
logs.push(LogLine::error(format!(
"teleport({x},{y}): destination is solid"
)));
} else if let Some(obj) = board.objects.get_mut(&ba.source) {
obj.x = ux;
obj.y = uy;
}
}
}
2026-06-04 23:52:33 -05:00
}
}
}
2026-06-06 17:36:00 -05:00
self.log.extend(logs);
2026-06-08 22:15:44 -05:00
for bubble in new_bubbles {
// One bubble per object: replace the existing one if present.
self.speech_bubbles.retain(|b| b.object_id != bubble.object_id);
self.speech_bubbles.push(bubble);
}
2026-06-10 01:42:33 -05:00
if let Some(scroll) = new_scroll {
self.active_scroll = Some(scroll);
}
2026-06-06 17:36:00 -05:00
for (bumped, bumper) in bumps {
self.scripts.run_bump(bumped, bumper);
2026-06-04 23:52:33 -05:00
}
2026-06-09 22:38:17 -05:00
for (target, fn_name, arg) in sends {
self.scripts.run_send(target, &fn_name, arg);
}
2026-06-06 17:36:00 -05:00
self.drain_errors();
2026-06-03 23:21:05 -05:00
}
2026-06-10 01:42:33 -05:00
/// Closes the active scroll overlay, optionally dispatching a named choice
/// back to the object that opened it via `send`.
///
/// Call from the front-end when the player dismisses the scroll or selects a
/// choice. If `choice` is `Some`, the source object receives a `send` call with
/// that string as the function name (e.g. `"eat"` triggers `fn eat()`).
pub fn close_scroll(&mut self, choice: Option<&str>) {
let source = self.active_scroll.take().map(|s| s.source);
if let (Some(src), Some(ch)) = (source, choice) {
self.scripts.run_send(src, ch, None);
self.drain_errors();
}
}
2026-06-13 16:24:29 -05:00
/// Switches the active board, placing the player at the named arrival portal,
/// rebuilding the script host, and running `init()` hooks on the new board's objects.
///
/// Called automatically by [`try_move`](GameState::try_move) when the player
/// steps onto a portal. Front-ends may check [`board_transition`](GameState::board_transition)
/// to play a visual effect during the switch.
pub fn enter_board(&mut self, target_map: &str, target_entry: &str) {
if !self.world.boards.contains_key(target_map) {
self.log.push(LogLine::error(format!("portal target board {target_map:?} not found")));
return;
}
// Find the named arrival portal on the target board (borrow then release).
let arrival = self.world.boards[target_map].borrow()
.portals.iter()
.find(|p| p.name == target_entry)
.map(|p| (p.x, p.y));
let (ax, ay) = match arrival {
Some(pos) => pos,
None => {
self.log.push(LogLine::error(format!(
"portal entry {target_entry:?} not found on board {target_map:?}"
)));
return;
}
};
// Clear per-board transient state.
self.speech_bubbles.clear();
self.active_scroll = None;
// Switch to the new board and place the player at the arrival portal.
self.current_board_name = target_map.to_string();
self.board_mut().player = Player { x: ax as i32, y: ay as i32 };
// Rebuild the script host for the new board's objects.
self.scripts = ScriptHost::new(
&self.world.boards[&self.current_board_name],
&self.world.scripts,
);
// Stub hook for the front-end transition animation (1 second).
self.board_transition = Some(1.0);
// Run init hooks and resolve their queued actions.
self.run_init();
}
2026-06-06 14:11:20 -05:00
/// Attempts to move the player one cell in `dir`.
///
2026-06-06 14:11:20 -05:00
/// The move is ignored if the target cell is out of bounds, or it is neither
2026-06-06 17:36:00 -05:00
/// passable nor a pushable solid that can be shoved aside. No-ops silently (the
/// caller does not need to check). If the target cell holds a solid object, that
/// object's `bump(-1)` hook fires (whether or not the player ends up moving).
2026-06-06 14:11:20 -05:00
pub fn try_move(&mut self, dir: Direction) {
2026-06-06 17:36:00 -05:00
let bumped;
2026-06-13 16:24:29 -05:00
let portal_target;
2026-06-06 17:36:00 -05:00
{
let (dx, dy): (i32, i32) = dir.into();
let mut board = self.board_mut();
let target = (board.player.x + dx, board.player.y + dy);
if !board.in_bounds(target) {
return;
}
let (nx, ny) = (target.0 as usize, target.1 as usize);
// A solid object in the way is bumped by the player (id -1).
bumped = match board.solid_at(nx, ny) {
2026-06-06 20:01:07 -05:00
Some(Solid::Object(_)) => board.solid_object_id_at(nx, ny),
2026-06-06 17:36:00 -05:00
_ => None,
};
if board.is_passable(nx, ny) || board.can_push(nx, ny, dir) {
board.push(nx, ny, dir); // shoves a crate/object out of the way; no-op otherwise
board.player.x = nx as i32;
board.player.y = ny as i32;
2026-06-13 16:24:29 -05:00
// Check for a portal at the new position; clone strings to release the borrow.
portal_target = board.portals.iter()
.find(|p| p.x == nx && p.y == ny)
.map(|p| (p.target_map.clone(), p.target_entry.clone()));
} else {
portal_target = None;
2026-06-06 17:36:00 -05:00
}
2026-06-06 14:11:20 -05:00
}
2026-06-13 16:24:29 -05:00
// A portal takes priority: board transitions skip the bump hook.
if let Some((target_map, target_entry)) = portal_target {
self.enter_board(&target_map, &target_entry);
return;
}
2026-06-06 17:36:00 -05:00
if let Some(idx) = bumped {
self.scripts.run_bump(idx, -1);
self.drain_errors();
}
}
2026-05-19 00:07:04 -05:00
}
2026-06-03 23:21:05 -05:00
2026-06-06 20:01:07 -05:00
/// Moves object `id` one cell in `dir` on `board`, returning the [`ObjectId`] of a
/// solid object it bumped (its target cell was occupied by that object), if any.
2026-06-06 17:36:00 -05:00
///
/// The move itself proceeds only if the target is in bounds and either passable or a
/// pushable solid the object can shove out of the way (see [`Board::can_push`]). The
/// bump is recorded whenever a solid object occupies the target — whether it gets
/// pushed aside or blocks the move — since something tried to move into it. Walls and
/// crates carry no script, so only solid objects yield a bump.
2026-06-06 20:01:07 -05:00
fn step_object(board: &mut Board, id: ObjectId, dir: Direction) -> Option<ObjectId> {
2026-06-06 17:36:00 -05:00
let (dx, dy): (i32, i32) = dir.into();
2026-06-06 20:01:07 -05:00
let (ox, oy) = board.objects.get(&id).map(|o| (o.x, o.y))?;
2026-06-06 17:36:00 -05:00
let target = (ox as i32 + dx, oy as i32 + dy);
if !board.in_bounds(target) {
return None;
}
let (nx, ny) = (target.0 as usize, target.1 as usize);
2026-06-06 20:01:07 -05:00
// Capture the bumped object before any push relocates it (its id is stable).
2026-06-06 17:36:00 -05:00
let bumped = match board.solid_at(nx, ny) {
2026-06-06 20:01:07 -05:00
Some(Solid::Object(_)) => board.solid_object_id_at(nx, ny),
2026-06-06 17:36:00 -05:00
_ => None,
};
if board.is_passable(nx, ny) || board.can_push(nx, ny, dir) {
board.push(nx, ny, dir); // shoves a crate/object out of the way; no-op otherwise
2026-06-06 20:01:07 -05:00
let obj = board.objects.get_mut(&id).expect("id checked above");
2026-06-06 17:36:00 -05:00
obj.x = nx;
obj.y = ny;
}
bumped
}