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

40 lines
1.9 KiB
Rust
Raw Normal View History

2026-06-23 01:08:01 -05:00
//! Tag helpers for script-backed archetypes expanded from map-file keywords.
2026-06-16 14:34:42 -05:00
//!
2026-06-23 01:08:01 -05:00
//! When a [`Builtin`] archetype cell is expanded into an [`ObjectDef`] by
//! [`Board::expand_builtin_archetypes`], the object receives a `BUILTIN_<alias>`
//! tag (e.g. `"BUILTIN_pusher_north"`) so its Rhai script can read which specific
//! variant it is (via `Me.has_tag("BUILTIN_pusher_north")`).
2026-06-16 14:34:42 -05:00
//!
2026-06-23 01:08:01 -05:00
//! The save path ([`map_file`]) uses [`archetype_from_builtin_tag`] to collapse
//! an expanded object back into its original map-file keyword so worlds
//! round-trip correctly.
//!
//! The full builtin registry — which archetypes exist, their behaviors, glyphs,
//! and embedded scripts — lives in [`crate::archetype`] via the `builtins!` macro.
//!
//! [`Builtin`]: crate::archetype::Builtin
//! [`ObjectDef`]: crate::object_def::ObjectDef
//! [`Board::expand_builtin_archetypes`]: crate::board::Board::expand_builtin_archetypes
//! [`map_file`]: crate::map_file
2026-06-16 14:34:42 -05:00
use crate::archetype::Archetype;
/// Prefix for the tag that marks an object as an expanded built-in archetype and
2026-06-23 01:08:01 -05:00
/// names which alias it came from (e.g. `"BUILTIN_pusher_east"`).
2026-06-16 14:34:42 -05:00
pub(crate) const BUILTIN_TAG_PREFIX: &str = "BUILTIN_";
2026-06-23 01:08:01 -05:00
/// Returns the `BUILTIN_<alias>` tag for `arch` — e.g. `"BUILTIN_pusher_east"`.
///
/// For a `Builtin` archetype, `arch.name()` returns the alias (e.g. `"pusher_east"`).
/// For terrain archetypes (wall, crate, etc.) this is never called in practice.
2026-06-16 14:34:42 -05:00
pub(crate) fn builtin_tag(arch: Archetype) -> String {
format!("{BUILTIN_TAG_PREFIX}{}", arch.name())
}
2026-06-23 01:08:01 -05:00
/// Recovers the `Archetype` a `BUILTIN_*` tag came from, or `None` if `tag` is not
/// a built-in tag naming a known archetype. Used by the save path to round-trip.
2026-06-16 14:34:42 -05:00
pub(crate) fn archetype_from_builtin_tag(tag: &str) -> Option<Archetype> {
let name = tag.strip_prefix(BUILTIN_TAG_PREFIX)?;
Archetype::try_from(name).ok()
}