diff --git a/kiln-core/src/action.rs b/kiln-core/src/action.rs index 81d4ffb..3cd1004 100644 --- a/kiln-core/src/action.rs +++ b/kiln-core/src/action.rs @@ -6,7 +6,6 @@ use std::fmt::Debug; use crate::log::LogLine; -use crate::map_file::color_to_hex; use crate::utils::{Direction, ObjectId}; use color::Rgba8; use rhai::Dynamic; @@ -151,128 +150,6 @@ impl Debug for Action { } } -// ── 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())); - match arg { - SendArg::Int(i) => m.insert("arg".into(), Dynamic::from(*i)), - SendArg::Float(f) => m.insert("arg".into(), Dynamic::from(*f)), - SendArg::String(s) => m.insert("arg".into(), Dynamic::from(s.clone())), - SendArg::None => m.insert("arg".into(), Dynamic::UNIT) - }; - } - // Scroll lines are not inspectable via the queue map API; emit type only. - Action::Scroll(_) => { - m.insert("type".into(), ds("Scroll")); - } - Action::Teleport { x, y } => { - m.insert("type".into(), ds("Teleport")); - m.insert("x".into(), Dynamic::from(*x)); - m.insert("y".into(), Dynamic::from(*y)); - } - Action::Push { x, y, dir } => { - m.insert("type".into(), ds("Push")); - m.insert("x".into(), Dynamic::from(*x)); - m.insert("y".into(), Dynamic::from(*y)); - m.insert("dir".into(), ds(dir_to_str(*dir))); - } - // Shift cells are not inspectable via the queue map API; emit type only. - Action::Shift(_) => { - m.insert("type".into(), ds("Shift")); - } - Action::AddGems(n) => { - m.insert("type".into(), ds("AddGems")); - m.insert("n".into(), Dynamic::from(*n)); - } - Action::AlterHealth(dh) => { - m.insert("type".into(), ds("AlterHealth")); - m.insert("dh".into(), Dynamic::from(*dh)); - } - Action::SetKey(color, present) => { - m.insert("type".into(), ds("SetKey")); - m.insert("color".into(), Dynamic::from(color.clone())); - m.insert("present".into(), Dynamic::from(*present)); - } - Action::Die => { - m.insert("type".into(), ds("Die")); - } - } - m -} - /// An action promoted from an object's output queue onto the board queue, tagged /// with the object that issued it. pub struct BoardAction { diff --git a/kiln-core/src/api/queue.rs b/kiln-core/src/api/queue.rs index a3b6488..94bf0c1 100644 --- a/kiln-core/src/api/queue.rs +++ b/kiln-core/src/api/queue.rs @@ -6,7 +6,7 @@ use std::cell::RefCell; use std::collections::VecDeque; use std::rc::Rc; use rhai::{Dynamic, Engine}; -use crate::action::{action_to_map, Action, BoardAction}; +use crate::action::{Action, BoardAction}; use crate::script::{BoardQueue, Registerable}; use crate::utils::{ErrorSink, ObjectId}; @@ -81,23 +81,6 @@ impl Registerable for ObjQueue { engine.register_get("length", |q: &mut ObjQueue| q.0.borrow().len() as i64); engine.register_fn("clear", |q: &mut ObjQueue| q.0.borrow_mut().clear()); - // peek(): front action as a Rhai map, or () if empty. - engine.register_fn("peek", |q: &mut ObjQueue| -> Dynamic { - q.0.borrow() - .front() - .map(|a| Dynamic::from(action_to_map(a))) - .unwrap_or(Dynamic::UNIT) - }); - - // pop(): same, but removes the front. - engine.register_fn("pop", |q: &mut ObjQueue| -> Dynamic { - q.0.borrow_mut() - .pop_front() - .as_ref() - .map(|a| Dynamic::from(action_to_map(a))) - .unwrap_or(Dynamic::UNIT) - }); - // Appends a [`Action::Delay`] to the back of `queue`, merging with an existing // trailing delay to prevent adjacent delays from accumulating. engine.register_fn("delay", move |q: &mut ObjQueue, secs: Dynamic| {