2026-06-09 22:38:17 -05:00
|
|
|
//! The [`Action`] enum and its Rhai-facing conversions.
|
|
|
|
|
//!
|
|
|
|
|
//! This module owns the `Action` type (which scripts enqueue via write host
|
2026-06-13 01:47:11 -05:00
|
|
|
//! functions), the rate-limiting delay constant, and [`action_to_map`] which
|
|
|
|
|
//! converts an `Action` to a Rhai map for `Queue.peek()` / `Queue.pop()`.
|
2026-06-09 22:38:17 -05:00
|
|
|
|
|
|
|
|
use crate::log::LogLine;
|
2026-06-13 01:47:11 -05:00
|
|
|
use crate::map_file::color_to_hex;
|
2026-06-09 22:38:17 -05:00
|
|
|
use crate::utils::{Direction, ObjectId, ScriptArg};
|
|
|
|
|
use color::Rgba8;
|
2026-06-13 01:47:11 -05:00
|
|
|
use rhai::Dynamic;
|
2026-06-09 22:38:17 -05:00
|
|
|
|
|
|
|
|
/// How long a move occupies an object before it can act again, in seconds.
|
|
|
|
|
pub(crate) const MOVE_COST: f64 = 0.25;
|
|
|
|
|
|
2026-06-10 01:42:33 -05:00
|
|
|
/// One line of content in a [`Action::Scroll`] overlay.
|
|
|
|
|
///
|
|
|
|
|
/// Each element of the array passed to the `scroll()` Rhai function becomes a
|
|
|
|
|
/// `ScrollLine`: a plain string becomes [`Text`](ScrollLine::Text); a two-element
|
|
|
|
|
/// array `[choice, display]` becomes a [`Choice`](ScrollLine::Choice).
|
|
|
|
|
pub enum ScrollLine {
|
|
|
|
|
/// Plain text, word-wrapped to the scroll window width.
|
|
|
|
|
Text(String),
|
|
|
|
|
/// A selectable option: `choice` is the event name sent back to the source
|
|
|
|
|
/// object via `send`, and `display` is the label shown to the player.
|
|
|
|
|
Choice { choice: String, display: String },
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 22:38:17 -05:00
|
|
|
/// One deferred mutation emitted by a script, applied by [`crate::game::GameState`]
|
|
|
|
|
/// after it is promoted onto the board queue.
|
|
|
|
|
///
|
|
|
|
|
/// [`Action::Delay`] is the exception: it is **never** promoted to the board queue.
|
|
|
|
|
/// It lives only in the per-object output queue and is consumed by `ScriptHost::drain`
|
|
|
|
|
/// to pace how quickly other actions are released.
|
|
|
|
|
pub(crate) enum Action {
|
|
|
|
|
/// Move the source object one cell in a direction (subject to passability).
|
|
|
|
|
Move(Direction),
|
|
|
|
|
/// Set the source object's glyph tile index.
|
|
|
|
|
SetTile(u32),
|
|
|
|
|
/// Append a styled line to the game log.
|
|
|
|
|
Log(LogLine),
|
|
|
|
|
/// Add (`present = true`) or remove (`present = false`) `tag` on `target`.
|
|
|
|
|
SetTag { target: ObjectId, tag: String, present: bool },
|
|
|
|
|
/// Display a speech bubble above the source object for [`crate::game::SAY_DURATION`] seconds.
|
|
|
|
|
Say(String),
|
|
|
|
|
/// Pause draining this object's queue for `secs` seconds. Never reaches the board queue.
|
|
|
|
|
Delay(f64),
|
|
|
|
|
/// Set the source object's foreground and/or background color.
|
|
|
|
|
SetColor { fg: Option<Rgba8>, bg: Option<Rgba8> },
|
|
|
|
|
/// Call `fn_name` on `target` object, optionally passing `arg`.
|
|
|
|
|
Send { target: ObjectId, fn_name: String, arg: Option<ScriptArg> },
|
2026-06-10 01:42:33 -05:00
|
|
|
/// Open a scrollable text overlay. Pauses game ticks while shown; a choice
|
|
|
|
|
/// selection dispatches [`Send`](Action::Send) to the source object.
|
|
|
|
|
Scroll(Vec<ScrollLine>),
|
2026-06-09 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-13 01:47:11 -05:00
|
|
|
// ── Direction helper ──────────────────────────────────────────────────────────
|
2026-06-09 22:38:17 -05:00
|
|
|
|
|
|
|
|
fn dir_to_str(d: Direction) -> &'static str {
|
|
|
|
|
match d {
|
|
|
|
|
Direction::North => "North",
|
|
|
|
|
Direction::South => "South",
|
|
|
|
|
Direction::East => "East",
|
|
|
|
|
Direction::West => "West",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── action_to_map ─────────────────────────────────────────────────────────────
|
|
|
|
|
|
2026-06-13 01:47:11 -05:00
|
|
|
/// Converts an [`Action`] to a Rhai map for `Queue.peek()` / `Queue.pop()`.
|
2026-06-09 22:38:17 -05:00
|
|
|
///
|
|
|
|
|
/// The map always has a `"type"` key naming the variant; other keys carry the
|
|
|
|
|
/// payload. [`Action::Log`] is flattened to its first span's text.
|
|
|
|
|
pub(crate) fn action_to_map(action: &Action) -> rhai::Map {
|
2026-06-13 01:47:11 -05:00
|
|
|
// Dynamic::from(String) avoids rhai 1.x's From<&str> lifetime restriction.
|
|
|
|
|
let ds = |v: &'static str| Dynamic::from(v.to_string());
|
2026-06-09 22:38:17 -05:00
|
|
|
let mut m = rhai::Map::new();
|
|
|
|
|
match action {
|
|
|
|
|
Action::Move(dir) => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("Move"));
|
|
|
|
|
m.insert("dir".into(), ds(dir_to_str(*dir)));
|
2026-06-09 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
Action::SetTile(n) => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("SetTile"));
|
2026-06-09 22:38:17 -05:00
|
|
|
m.insert("tile".into(), Dynamic::from(*n as i64));
|
|
|
|
|
}
|
|
|
|
|
Action::Log(line) => {
|
|
|
|
|
let text = line.spans.first().map(|s| s.text.as_str()).unwrap_or("").to_string();
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("Log"));
|
|
|
|
|
m.insert("msg".into(), Dynamic::from(text));
|
2026-06-09 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
Action::SetTag { target, tag, present } => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("SetTag"));
|
2026-06-09 22:38:17 -05:00
|
|
|
m.insert("target".into(), Dynamic::from(*target as i64));
|
|
|
|
|
m.insert("tag".into(), Dynamic::from(tag.clone()));
|
|
|
|
|
m.insert("present".into(), Dynamic::from(*present));
|
|
|
|
|
}
|
|
|
|
|
Action::Say(text) => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("Say"));
|
|
|
|
|
m.insert("msg".into(), Dynamic::from(text.clone()));
|
2026-06-09 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
Action::Delay(secs) => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("Delay"));
|
2026-06-09 22:38:17 -05:00
|
|
|
m.insert("secs".into(), Dynamic::from(*secs));
|
|
|
|
|
}
|
|
|
|
|
Action::SetColor { fg, bg } => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("SetColor"));
|
2026-06-09 22:38:17 -05:00
|
|
|
if let Some(c) = fg { m.insert("fg".into(), Dynamic::from(color_to_hex(*c))); }
|
|
|
|
|
if let Some(c) = bg { m.insert("bg".into(), Dynamic::from(color_to_hex(*c))); }
|
|
|
|
|
}
|
|
|
|
|
Action::Send { target, fn_name, arg } => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("Send"));
|
2026-06-09 22:38:17 -05:00
|
|
|
m.insert("target".into(), Dynamic::from(*target as i64));
|
|
|
|
|
m.insert("name".into(), Dynamic::from(fn_name.clone()));
|
|
|
|
|
if let Some(a) = arg {
|
|
|
|
|
let dyn_arg = match a {
|
|
|
|
|
ScriptArg::Str(s) => Dynamic::from(s.clone()),
|
|
|
|
|
ScriptArg::Num(n) => Dynamic::from(*n),
|
|
|
|
|
};
|
|
|
|
|
m.insert("arg".into(), dyn_arg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-13 01:47:11 -05:00
|
|
|
// Scroll lines are not inspectable via the queue map API; emit type only.
|
2026-06-10 01:42:33 -05:00
|
|
|
Action::Scroll(_) => {
|
2026-06-13 01:47:11 -05:00
|
|
|
m.insert("type".into(), ds("Scroll"));
|
2026-06-10 01:42:33 -05:00
|
|
|
}
|
2026-06-09 22:38:17 -05:00
|
|
|
}
|
|
|
|
|
m
|
|
|
|
|
}
|