fix: find DLC-fabrikkers pallespawner uden for productionPoint (young trout ved Young Fish Breeding)
This commit is contained in:
@@ -85,3 +85,72 @@ function ADUtil.spawnPlaces(spawner, fillType)
|
||||
if fillType ~= nil and byFillType ~= nil and byFillType[fillType] ~= nil then return byFillType[fillType] end
|
||||
return spawner.spawnPlaces or {}
|
||||
end
|
||||
|
||||
-- Fabrikkens pallespawner. Spillets ProductionPoint har den i productionPoint.palletSpawner; DLC-bygninger kan have
|
||||
-- den andetsteds (in-game 20/10: Young Fish Breeding fra Highlands Fishing laver young trout-paller, men havde ingen
|
||||
-- productionPoint.palletSpawner — derfor ingen læssested og ingen vare). Så findes et spawner-objekt (har
|
||||
-- getOrSpawnPallet) i produktionspunktet eller bygningens spec_*-tabeller, højst ét niveau nede, i fast navneorden og
|
||||
-- uden stald-/bistade-/palleforhandler-spawnere. Kun for fabrikker; resultatet gemmes pr. bygning.
|
||||
local spawnerCache = setmetatable({}, {__mode = "k"})
|
||||
local loggedSpawner = setmetatable({}, {__mode = "k"})
|
||||
|
||||
local function isSpawner(value)
|
||||
return type(value) == "table" and type(value.getOrSpawnPallet) == "function"
|
||||
end
|
||||
|
||||
-- Spawnere i tabellens felter (og én liste nede), med sti -> {{spawner, path}}; nøgler sorteret, så valget er fast
|
||||
local function sortedKeys(tbl)
|
||||
local keys = {}
|
||||
for key in pairs(tbl) do table.insert(keys, key) end
|
||||
table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
|
||||
return keys
|
||||
end
|
||||
|
||||
local function scanForSpawners(tbl, depth, prefix, found)
|
||||
if type(tbl) ~= "table" then return found end
|
||||
for _, key in ipairs(sortedKeys(tbl)) do
|
||||
local value = tbl[key]
|
||||
local path = prefix .. "." .. tostring(key)
|
||||
if isSpawner(value) then
|
||||
table.insert(found, {spawner = value, path = path})
|
||||
elseif depth > 0 and type(value) == "table" and getmetatable(value) == nil then
|
||||
scanForSpawners(value, depth - 1, path, found)
|
||||
end
|
||||
end
|
||||
return found
|
||||
end
|
||||
|
||||
-- andre slags spawnere på samme bygning: stald (æg/uld), bistader (honning), palleforhandler (købte paller)
|
||||
local FOREIGN_SPECS = {spec_productionPoint = true, spec_husbandryPallets = true, spec_beehivePalletSpawner = true, spec_palletBuyingStation = true}
|
||||
|
||||
-- Produktionspunktet først, så spec_*-tabellerne i navneorden -> spawner, sti, antal fundne | nil
|
||||
local function findFallbackSpawner(productionPoint, placeable)
|
||||
local found = scanForSpawners(productionPoint, 1, "productionPoint", {})
|
||||
if #found == 0 and type(placeable) == "table" then
|
||||
for _, key in ipairs(sortedKeys(placeable)) do
|
||||
if type(key) == "string" and key:sub(1, 5) == "spec_" and not FOREIGN_SPECS[key] then
|
||||
scanForSpawners(placeable[key], 1, key, found)
|
||||
end
|
||||
end
|
||||
end
|
||||
if #found == 0 then return nil end
|
||||
return found[1].spawner, found[1].path, #found
|
||||
end
|
||||
|
||||
function ADUtil.productionPalletSpawner(productionPoint, placeable)
|
||||
if productionPoint == nil then return nil end
|
||||
if productionPoint.palletSpawner ~= nil then return productionPoint.palletSpawner end
|
||||
local key = placeable or productionPoint
|
||||
local cached = spawnerCache[key]
|
||||
if cached ~= nil then return cached or nil end
|
||||
local ok, found, path, count = pcall(findFallbackSpawner, productionPoint, placeable)
|
||||
found = ok and found or nil
|
||||
spawnerCache[key] = found or false
|
||||
if found ~= nil and not loggedSpawner[key] then
|
||||
loggedSpawner[key] = true
|
||||
local okName, name = pcall(function() return placeable ~= nil and placeable.getName ~= nil and placeable:getName() or nil end)
|
||||
logInfo("ADSmartPickup: '%s' har sin pallespawner i %s (ikke productionPoint.palletSpawner, %d fundet) — bruges som læssested",
|
||||
tostring(okName and name or "?"), tostring(path), count or 1)
|
||||
end
|
||||
return found
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user