-- 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. -- info-log bag "Detaljeret log" (ADLog i adSettings.lua; uden ADLog — tests — direkte til Logging) local function logInfo(...) return (ADLog or Logging).info(...) end ADUtil = {} -- Gården ADSmartPickup kører for (multiplayer etape 1; én fælles gård, brugerens valg 25/10). Med lokal spiller -- (singleplayer/vært) spillerens gård. På en dedikeret server er der ingen spiller (tilskuer-gården): så den -- indstillede gård (serverFarmId), ellers gården med flest AutoDrive-køretøjer, ellers 1. -- Server/klient (multiplayer): singleplayer og værten er server; en MP-klient er klient uden server. function ADUtil.isServer() return g_server ~= nil end function ADUtil.isMpClient() return g_client ~= nil and g_server == nil end ADUtil.FARM_CACHE_MS = 60000 local farmCache = {id = nil, atMs = -math.huge} function ADUtil.resetFarmCache() farmCache = {id = nil, atMs = -math.huge} end function ADUtil.configuredFarmId() local value = ADSettings ~= nil and ADSettings.get ~= nil and tonumber(ADSettings.get("serverFarmId")) or 0 return math.floor(value) end local function spectatorFarmId() return FarmManager ~= nil and FarmManager.SPECTATOR_FARM_ID or 0 end local function dominantFarmId() local counts, best, bestCount = {}, nil, 0 local system = g_currentMission ~= nil and g_currentMission.vehicleSystem or nil for _, vehicle in ipairs(system ~= nil and system.vehicles or {}) do if vehicle.ad ~= nil and vehicle.getOwnerFarmId ~= nil then local farm = vehicle:getOwnerFarmId() if farm ~= nil and farm ~= spectatorFarmId() then counts[farm] = (counts[farm] or 0) + 1 if counts[farm] > bestCount or (counts[farm] == bestCount and farm < best) then best, bestCount = farm, counts[farm] end end end end return best end function ADUtil.farmId() local mission = g_currentMission if mission ~= nil and mission.getFarmId ~= nil then local own = mission:getFarmId() if own ~= nil and own ~= spectatorFarmId() then return own end end local configured = ADUtil.configuredFarmId() if configured ~= nil and configured > 0 then return configured end if mission == nil then return 1 end local nowMs = ADUtil.now() if farmCache.id == nil or nowMs - farmCache.atMs > ADUtil.FARM_CACHE_MS or nowMs < farmCache.atMs then farmCache = {id = dominantFarmId() or 1, atMs = nowMs} end return farmCache.id end -- 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) logInfo("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 -- 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