Files
fs25-adsmartpickup/FS25_ADSmartPickup/adUtil.lua
T

85 lines
3.8 KiB
Lua

-- AD Util
-- Små fælles hjælpere, samlet 17/10-2026 fra 20+ enslydende lokale kopier i modulerne. Indlæses først (modDesc),
-- og i tests med dofile før de moduler der bruger dem. Ingen spil-afhængighed ved indlæsning.
ADUtil = {}
-- Spiltid i ms (g_currentMission.time); uden spil (tests) processorens tid.
function ADUtil.now()
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then return g_currentMission.time end
return os.clock() * 1000
end
function ADUtil.contains(list, value)
for _, item in ipairs(list or {}) do if item == value then return true end end
return false
end
function ADUtil.copyList(list)
local copy = {}
for index, value in ipairs(list or {}) do copy[index] = value end
return copy
end
function ADUtil.vehicleName(vehicle)
return vehicle ~= nil and vehicle.getName ~= nil and vehicle:getName() or "?"
end
function ADUtil.fillTypeName(fillType)
return g_fillTypeManager ~= nil and fillType ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
end
-- x, z for et objekt med rootNode (nil, nil uden).
function ADUtil.positionOf(object)
if object == nil or object.rootNode == nil then return nil, nil end
local x, _, z = getWorldTranslation(object.rootNode)
return x, z
end
-- Sti til en af modens filer i savegamet, ved siden af ADRunsStorage's fil (nil før savegamet kendes).
function ADUtil.savePath(fileName)
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath ~= nil and ADRunsStorage.getPath() or nil
if path == nil then return nil end
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", fileName))
end
-- Diagnose én gang pr. placeable: bistader-spawner uden honning-vare (HONEY ukendt) -> ikke en kilde.
local loggedBeehive = setmetatable({}, {__mode = "k"})
function ADUtil.logBeehiveOnce(placeable)
if placeable == nil or loggedBeehive[placeable] then return end
loggedBeehive[placeable] = true
if Logging ~= nil and Logging.info ~= nil then
local ok, name = pcall(function() return placeable.getName ~= nil and placeable:getName() or nil end)
Logging.info("ADSmartPickup: bistader '%s' har en pallespawner, men varen HONEY kunne ikke slås op — springes over som kilde",
tostring(ok and name or "?"))
end
end
-- Bistadernes pallespawner (spillets PlaceableBeehivePalletSpawner; også FS25_moreHoneyPalletSpawnerOptions): honning
-- fra gårdens bistader kommer som paller ved spawneren -> spawner, fillType (HONEY) | nil. Spillet sætter spec.fillType
-- i onLoad; mangler den, slås HONEY op. (in-game 19/10: 29 bistader, men spawneren stod ikke i Fra-listen)
function ADUtil.beehivePalletSpawner(placeable)
local spec = placeable ~= nil and placeable.spec_beehivePalletSpawner or nil
local spawner = spec ~= nil and spec.palletSpawner or nil
if spawner == nil then return nil end
local fillType = spec.fillType
if fillType == nil and g_fillTypeManager ~= nil and g_fillTypeManager.getFillTypeIndexByName ~= nil then
local ok, index = pcall(g_fillTypeManager.getFillTypeIndexByName, g_fillTypeManager, "HONEY")
fillType = ok and index or nil
end
if fillType == nil then
ADUtil.logBeehiveOnce(placeable)
return nil
end
return spawner, fillType
end
-- En pallespawners pladser for varen, som spillet selv vælger (PalletSpawner:getOrSpawnPallet): varens egne pladser
-- (spawnPlace#fillTypes -> fillTypeToSpawnPlaces), ellers de fælles. Uden vare: kun de fælles (som før — fabrikker).
function ADUtil.spawnPlaces(spawner, fillType)
if spawner == nil then return {} end
local byFillType = spawner.fillTypeToSpawnPlaces
if fillType ~= nil and byFillType ~= nil and byFillType[fillType] ~= nil then return byFillType[fillType] end
return spawner.spawnPlaces or {}
end