//! The [`Action`] enum and its Rhai-facing conversions. //! //! This module owns the `Action` type (which scripts enqueue via write host //! functions), the rate-limiting delay constant, and [`action_to_map`] which //! converts an `Action` to a Rhai map for `Queue.peek()` / `Queue.pop()`. use crate::log::LogLine; use crate::map_file::color_to_hex; use crate::utils::{Direction, ObjectId, ScriptArg}; use color::Rgba8; use rhai::Dynamic; /// How long a move occupies an object before it can act again, in seconds. pub(crate) const MOVE_COST: f64 = 0.25; /// 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 }, } /// 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 `duration` seconds. Say(String, f64), /// 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, bg: Option }, /// Call `fn_name` on `target` object, optionally passing `arg`. Send { target: ObjectId, fn_name: String, arg: Option }, /// Open a scrollable text overlay. Pauses game ticks while shown; a choice /// selection dispatches [`Send`](Action::Send) to the source object. Scroll(Vec), } // ── Direction helper ────────────────────────────────────────────────────────── 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 ───────────────────────────────────────────────────────────── /// Converts an [`Action`] to a Rhai map for `Queue.peek()` / `Queue.pop()`. /// /// 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 { // Dynamic::from(String) avoids rhai 1.x's From<&str> lifetime restriction. let ds = |v: &'static str| Dynamic::from(v.to_string()); let mut m = rhai::Map::new(); match action { Action::Move(dir) => { m.insert("type".into(), ds("Move")); m.insert("dir".into(), ds(dir_to_str(*dir))); } Action::SetTile(n) => { m.insert("type".into(), ds("SetTile")); 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(); m.insert("type".into(), ds("Log")); m.insert("msg".into(), Dynamic::from(text)); } Action::SetTag { target, tag, present } => { m.insert("type".into(), ds("SetTag")); 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, dur) => { m.insert("type".into(), ds("Say")); m.insert("msg".into(), Dynamic::from(text.clone())); m.insert("duration".into(), Dynamic::from(*dur)); } Action::Delay(secs) => { m.insert("type".into(), ds("Delay")); m.insert("secs".into(), Dynamic::from(*secs)); } Action::SetColor { fg, bg } => { m.insert("type".into(), ds("SetColor")); 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 } => { m.insert("type".into(), ds("Send")); 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); } } // Scroll lines are not inspectable via the queue map API; emit type only. Action::Scroll(_) => { m.insert("type".into(), ds("Scroll")); } } m }