some auto-refactoring
This commit is contained in:
+49
-28
@@ -9,15 +9,22 @@
|
||||
//! - `bump(id)` — run when another mover steps into this object's cell, with the
|
||||
//! bumper's object index (`-1` for the player); see [`ScriptHost::run_bump`].
|
||||
//!
|
||||
//! ## Script constants in scope
|
||||
//! ## Script constants
|
||||
//!
|
||||
//! Every object scope contains:
|
||||
//! Object-specific handles live in the per-object scope (visible to the
|
||||
//! top-level hook Rust calls):
|
||||
//!
|
||||
//! | Name | Type | Description |
|
||||
//! |---|---|---|
|
||||
//! | `Board` | `Board` | Read-only world handle. |
|
||||
//! | `Queue` | `Queue` | This object's pending-action queue. |
|
||||
//! | `Me` | `Me` | Self-reference (id, name, x, y, tags, glyph). |
|
||||
//!
|
||||
//! Direction and color constants are registered as a global Rhai module and
|
||||
//! are visible to all functions at any call depth, including Rhai-to-Rhai calls:
|
||||
//!
|
||||
//! | Name | Type | Description |
|
||||
//! |---|---|---|
|
||||
//! | `North`/`South`/`East`/`West` | `Direction` | Cardinal directions. |
|
||||
//! | `Black`..`White` | `String` | 16 EGA/VGA color hex strings. |
|
||||
//!
|
||||
@@ -46,11 +53,12 @@
|
||||
//! `Queue.length()`, `Queue.clear()`, `Queue.peek()`, `Queue.pop()`
|
||||
|
||||
use crate::action::{action_to_map, Action, ScrollLine, MOVE_COST};
|
||||
use crate::game::SAY_DURATION;
|
||||
use crate::board::Board;
|
||||
use crate::log::LogLine;
|
||||
use crate::map_file::{color_to_hex, parse_color};
|
||||
use crate::utils::{Direction, ObjectId, ScriptArg};
|
||||
use rhai::{Array, CallFnOptions, Dynamic, Engine, FuncArgs, ImmutableString, NativeCallContext, Scope, AST};
|
||||
use rhai::{Array, CallFnOptions, Dynamic, Engine, FuncArgs, ImmutableString, Module, NativeCallContext, Scope, AST};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::rc::Rc;
|
||||
@@ -179,6 +187,7 @@ impl ScriptHost {
|
||||
register_me_type(&mut engine);
|
||||
register_object_info_type(&mut engine);
|
||||
register_glyph_type(&mut engine);
|
||||
register_global_constants(&mut engine);
|
||||
|
||||
let board = board_cell.borrow();
|
||||
|
||||
@@ -612,7 +621,11 @@ fn register_write_api(engine: &mut Engine, queues: &QueueMap) {
|
||||
|
||||
let q = queues.clone();
|
||||
engine.register_fn("say", move |ctx: NativeCallContext, msg: ImmutableString| {
|
||||
emit(&q, source_of(&ctx), Action::Say(msg.to_string()));
|
||||
emit(&q, source_of(&ctx), Action::Say(msg.to_string(), SAY_DURATION));
|
||||
});
|
||||
let q = queues.clone();
|
||||
engine.register_fn("say", move |ctx: NativeCallContext, msg: ImmutableString, dur: f64| {
|
||||
emit(&q, source_of(&ctx), Action::Say(msg.to_string(), dur));
|
||||
});
|
||||
|
||||
// scroll(lines): open a full-screen scrollable overlay. Each element of `lines`
|
||||
@@ -739,36 +752,44 @@ fn register_queue_api(engine: &mut Engine) {
|
||||
});
|
||||
}
|
||||
|
||||
// ── Scope construction ────────────────────────────────────────────────────────
|
||||
// ── Scope construction & global constants ─────────────────────────────────────
|
||||
|
||||
/// Builds a fresh per-object scope with the Board view, Queue, Me, direction
|
||||
/// constants, and the 16 EGA/VGA named color constants.
|
||||
/// Registers direction and color constants as a global Rhai module so they are
|
||||
/// visible to every function at any call depth, including Rhai-to-Rhai calls.
|
||||
/// (Scope-level constants are only visible to the top-level function Rust calls.)
|
||||
fn register_global_constants(engine: &mut Engine) {
|
||||
let mut m = Module::new();
|
||||
m.set_var("North", Direction::North);
|
||||
m.set_var("South", Direction::South);
|
||||
m.set_var("East", Direction::East);
|
||||
m.set_var("West", Direction::West);
|
||||
// 16 EGA/VGA color constants as "#RRGGBB" strings.
|
||||
m.set_var("Black", "#000000");
|
||||
m.set_var("Blue", "#0000AA");
|
||||
m.set_var("Green", "#00AA00");
|
||||
m.set_var("Cyan", "#00AAAA");
|
||||
m.set_var("Red", "#AA0000");
|
||||
m.set_var("Magenta", "#AA00AA");
|
||||
m.set_var("Brown", "#AA5500");
|
||||
m.set_var("LightGray", "#AAAAAA");
|
||||
m.set_var("DarkGray", "#555555");
|
||||
m.set_var("BrightBlue", "#5555FF");
|
||||
m.set_var("BrightGreen", "#55FF55");
|
||||
m.set_var("BrightCyan", "#55FFFF");
|
||||
m.set_var("BrightRed", "#FF5555");
|
||||
m.set_var("BrightMagenta", "#FF55FF");
|
||||
m.set_var("Yellow", "#FFFF55");
|
||||
m.set_var("White", "#FFFFFF");
|
||||
engine.register_global_module(m.into());
|
||||
}
|
||||
|
||||
/// Builds a fresh per-object scope containing only the object-specific handles:
|
||||
/// the Board view, the object's Queue, and the Me self-reference.
|
||||
fn new_object_scope(board: &BoardRef, queue: &ObjQueue, object_id: ObjectId) -> Scope<'static> {
|
||||
let mut scope = Scope::new();
|
||||
scope.push_constant("Board", board.clone());
|
||||
scope.push_constant("Queue", queue.clone());
|
||||
scope.push_constant("Me", Me { object_id, board: board.clone() });
|
||||
scope.push_constant("North", Direction::North);
|
||||
scope.push_constant("South", Direction::South);
|
||||
scope.push_constant("East", Direction::East);
|
||||
scope.push_constant("West", Direction::West);
|
||||
// 16 EGA/VGA color constants as "#RRGGBB" strings.
|
||||
scope.push_constant("Black", "#000000");
|
||||
scope.push_constant("Blue", "#0000AA");
|
||||
scope.push_constant("Green", "#00AA00");
|
||||
scope.push_constant("Cyan", "#00AAAA");
|
||||
scope.push_constant("Red", "#AA0000");
|
||||
scope.push_constant("Magenta", "#AA00AA");
|
||||
scope.push_constant("Brown", "#AA5500");
|
||||
scope.push_constant("LightGray", "#AAAAAA");
|
||||
scope.push_constant("DarkGray", "#555555");
|
||||
scope.push_constant("BrightBlue", "#5555FF");
|
||||
scope.push_constant("BrightGreen", "#55FF55");
|
||||
scope.push_constant("BrightCyan", "#55FFFF");
|
||||
scope.push_constant("BrightRed", "#FF5555");
|
||||
scope.push_constant("BrightMagenta","#FF55FF");
|
||||
scope.push_constant("Yellow", "#FFFF55");
|
||||
scope.push_constant("White", "#FFFFFF");
|
||||
scope
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user