Модуль:Песочница/Pok: различия между версиями

мНет описания правки
Нет описания правки
Строка 61: Строка 61:
local componentDefs = load_module_data("component.json")
local componentDefs = load_module_data("component.json")
local prototypeDefs = load_module_data("prototype.json")
local prototypeDefs = load_module_data("prototype.json")
local entities = load_module_data("entity_prototypes.json")
if not componentDefs or not prototypeDefs then
if not componentDefs or not prototypeDefs then
return ""
return ""
end
end
if not entities then
return ""
end


local entity = entities[id]
local foundComponents = {}
if not entity then
local foundPrototypes = {}
return ""
 
-- If id directly matches a prototype or component key, include it
if prototypeDefs[id] ~= nil then
foundPrototypes[id] = true
end
end
 
if componentDefs[id] ~= nil then
local foundComponents = {}
foundComponents[id] = true
-- If entity explicitly lists components use that
if type(entity.components) == "table" then
for _, v in ipairs(entity.components) do
if type(v) == "string" then
foundComponents[v] = true
elseif type(v) == "table" and v.name then
foundComponents[v.name] = true
end
end
else
-- fallback: detect component keys present in entity
for compName,_ in pairs(componentDefs) do
if entity[compName] ~= nil then
foundComponents[compName] = true
end
end
end
end


local foundPrototypes = {}
-- Support comma-separated list of ids (e.g. "absorbent,action")
if type(entity.prototypes) == "table" then
for name in string.gmatch(id, "[^,]+") do
for _, v in ipairs(entity.prototypes) do
local n = trim(name)
if type(v) == "string" then
if n ~= "" then
foundPrototypes[v] = true
if prototypeDefs[n] ~= nil then foundPrototypes[n] = true end
end
if componentDefs[n] ~= nil then foundComponents[n] = true end
end
else
for protoName,_ in pairs(prototypeDefs) do
if entity[protoName] ~= nil then
foundPrototypes[protoName] = true
end
end
end
end
end