feat: add template_of getter + bump to v0.3.0

Add composition.template_of(entity) -> string|nil, returning the
template-id this entity was created from. Returns nil for
non-composition-managed entities (e.g. raw entity.create() handles)
and loud-errors on nil arg. Enables crafting and inventory consumers
to recover the template-id without round-tripping through tags.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Axel Meyer
2026-06-14 11:57:29 +02:00
parent ccc4bb5643
commit b2a6d5c170
3 changed files with 51 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
-- =====================================================================
-- lib-core.composition v0.2.0 — Template + Instantiation + Tag-Index
-- lib-core.composition v0.3.0 — Template + Instantiation + Tag-Index
-- See: meta/docs/superpowers/specs/2026-06-09-phase-A-inactive-entities-...
--
-- v0.1.0 Template-Only-Subset:
@@ -23,6 +23,11 @@
-- All constraint fields (weight_max, volume_max, grid, accepts_fluid,
-- accepts_gas, restrictions) → Loud-Error (Phase F/G deferred).
--
-- v0.3.0 additions:
-- - composition.template_of(entity) — returns the template_id this
-- entity was created from, or nil if the entity is not
-- composition-managed. Loud-Error if entity is nil.
--
-- DEFERRED (loud-error if attempted):
-- - slots → Phase D Trigger (Composite Items with Sub-Items)
-- - container constraint fields (weight_max, volume_max, grid,
@@ -353,6 +358,24 @@ function M.list_by_tag(tag)
return out
end
-- Returns the template_id this entity was created from, or nil if
-- the entity is not composition-managed (no reg_id property).
-- Loud-Error if entity is nil.
function M.template_of(target_entity)
if target_entity == nil then
error("composition.template_of: entity must not be nil", 2)
end
local reg_id = target_entity:get_property("composition.reg_id")
if not reg_id or reg_id == 0 then
return nil
end
local meta = entity_meta[reg_id]
if not meta then
return nil
end
return meta.template_id
end
-- Remove entity from indices + destroy engine-side. Idempotent.
local function remove_from_list(list, entity)
if not list then return end