From 9426ac01dd666df75a34fa61e0a3fea1648510aa Mon Sep 17 00:00:00 2001 From: Vanic Date: Mon, 9 Feb 2026 22:19:20 +0300 Subject: [PATCH] `Creature`: minor fixes --- mods/CORE/Creature/src/Creature.lua | 61 ++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/mods/CORE/Creature/src/Creature.lua b/mods/CORE/Creature/src/Creature.lua index 0cd41c6..7619a77 100644 --- a/mods/CORE/Creature/src/Creature.lua +++ b/mods/CORE/Creature/src/Creature.lua @@ -17,41 +17,64 @@ local Creature = { } } ----The alias `table.merge` for EmmyLua ----@type fun(...):(EntityDefinition) -local defMerge = function(...) - return tableMerge(...) -end - ---@param entityDef Creature.CreatureDefinition ---@return EntityDefinition local function entityDefToLuantiDef(entityDef) entityDef = tableCopy(entityDef) local s = entityDef.settings - local c = entityDef.callbacks + local c = entityDef.callbacks or {} - local luantiDef = defMerge(s, c) + ---@type EntityDefinition + local luantiDef = tableMerge(s, c) return luantiDef end ----The alias `table.merge` for EmmyLua ----@type fun(...):(Creature.CreatureDefinition) -local defMerge = function(...) - return tableMerge(...) -end - ---@param entityDef Creature.CreatureDefinition ---@return Creature function Creature:new(entityDef) - -- Adding default parameters - entityDef = defMerge(entityDef, self.defaultDef, true) - local s = entityDef.settings + -- Adding default parameters. + ---@type Creature.CreatureDefinition + entityDef = tableMerge(entityDef, self.defaultDef, true) + -- The callback table may not be specified. + if entityDef.callbacks == nil then + ---@type Creature.Callbacks + entityDef.callbacks = {} + end + + local s = entityDef.settings + local c = entityDef.callbacks + + -- Add the short mod name prefix to the entity name. if s._add_mod_name then s._name = Mod.getInfo().shortName .. ':' .. s._name end + local onActivate = c.on_activate or function() end + + -- We rewrite the entity initialization function so that custom fields + -- can be passed to it directly via `core.add_entity(pos, name, [staticdata])`. + c.on_activate = function(self, staticdata, dtime) + local initCustomFields = {} + + if staticdata and staticdata ~= '' then + initCustomFields = core.deserialize(staticdata) or {} + end + + -- We reset the fields that should not be overwritten. + initCustomFields.name = nil + initCustomFields.object = nil + + ---@type LuaEntity + self = tableMerge(self, initCustomFields) + + -- If an initialization callback was specified for the entity, then we call it. + if c.on_activate ~= nil then + onActivate(self, staticdata, dtime) + end + end + ---@type Creature local instance = getInstance(self, {}) @@ -71,7 +94,9 @@ end ---@param defaultDef Creature.CreatureDefinition ---@return Creature function Creature:getExtended(defaultDef) - defaultDef = defMerge(self.defaultDef, defaultDef) + -- Adding default parameters. + ---@type Creature.CreatureDefinition + defaultDef = tableMerge(self.defaultDef, defaultDef) ---@type Creature local ChildClass = extend(self, {