287 lines
12 KiB
Lua
287 lines
12 KiB
Lua
-- AD Buildings
|
|
-- Gårdens bygninger som brugeren vælger imellem (i stedet for markører): fabrik, silo, stald,
|
|
-- objektlager og salgssted, hver med den AD-markør der står nærmest bygningens læsse- og
|
|
-- aflæsningstrigger (≤ MAX_MARKER_DISTANCE; Wait-markører aldrig; siloers læssemarkør via det
|
|
-- strenge opslag, så tætte rækker ikke blandes). Spil-adapter med 1 s cache.
|
|
|
|
ADBuildings = {}
|
|
|
|
ADBuildings.LOG_PREFIX = "ADSmartPickup: "
|
|
ADBuildings.MAX_MARKER_DISTANCE = 40
|
|
ADBuildings.CACHE_MS = 1000
|
|
|
|
local cache = {list = nil, farmId = nil, time = -math.huge}
|
|
|
|
local function now()
|
|
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then
|
|
return g_currentMission.time
|
|
end
|
|
return os.clock() * 1000
|
|
end
|
|
|
|
local function getNodePosition(node)
|
|
if node == nil or (entityExists ~= nil and not entityExists(node)) then
|
|
return nil
|
|
end
|
|
local x, _, z = getWorldTranslation(node)
|
|
return x, z
|
|
end
|
|
|
|
local function getName(placeable, fallback)
|
|
if placeable ~= nil and placeable.getName ~= nil then
|
|
local ok, name = pcall(placeable.getName, placeable)
|
|
if ok and name ~= nil then return tostring(name) end
|
|
end
|
|
return fallback
|
|
end
|
|
|
|
function ADBuildings.getId(placeable)
|
|
if placeable.uniqueId ~= nil then
|
|
return tostring(placeable.uniqueId)
|
|
end
|
|
local x, z = getNodePosition(placeable.rootNode)
|
|
if x == nil then
|
|
return nil
|
|
end
|
|
return string.format("pos:%d:%d", math.floor(x + 0.5), math.floor(z + 0.5))
|
|
end
|
|
|
|
local function getProductionPoint(placeable)
|
|
return placeable.spec_productionPoint ~= nil and placeable.spec_productionPoint.productionPoint or nil
|
|
end
|
|
|
|
local function isOwnedBy(placeable, farmId)
|
|
if placeable.getOwnerFarmId ~= nil then
|
|
local ok, owner = pcall(placeable.getOwnerFarmId, placeable)
|
|
return ok and owner == farmId
|
|
end
|
|
return placeable.ownerFarmId == farmId
|
|
end
|
|
|
|
local function getKind(placeable)
|
|
if getProductionPoint(placeable) ~= nil then return "factory" end
|
|
if placeable.spec_silo ~= nil then return "silo" end
|
|
if placeable.getHusbandryFillLevel ~= nil then return "husbandry" end
|
|
if placeable.spec_objectStorage ~= nil then return "objectStorage" end
|
|
return nil
|
|
end
|
|
|
|
-- Trigger-noder pr. bygning: læsse (loadTriggers.triggerNode, pallespawner, objektlager) og
|
|
-- aflæsning (unloadTriggers exactFillRootNode/triggerNode/aiNode).
|
|
-- loadGroups: én gruppe noder pr. læssepunkt (mælketank, gyllebeholder ...), så hvert punkt får sin
|
|
-- egen markør; pallespawner/objektlager er en gruppe for sig.
|
|
local function collectNodes(placeable, loadStations, unloadStations)
|
|
local loadGroups, unloadNodes = {}, {}
|
|
-- én gruppe pr. LÆSSEPUNKT: en stald har ofte én station med både mælke- og gyllepunkt
|
|
for _, station in pairs(loadStations) do
|
|
if station.owningPlaceable == placeable then
|
|
for _, trigger in pairs(station.loadTriggers or {}) do
|
|
if trigger.triggerNode ~= nil then table.insert(loadGroups, {trigger.triggerNode}) end
|
|
end
|
|
end
|
|
end
|
|
for _, station in pairs(unloadStations) do
|
|
if station.owningPlaceable == placeable then
|
|
for _, trigger in pairs(station.unloadTriggers or {}) do
|
|
for _, field in ipairs({"exactFillRootNode", "triggerNode", "aiNode"}) do
|
|
if trigger[field] ~= nil then table.insert(unloadNodes, trigger[field]) end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local productionPoint = getProductionPoint(placeable)
|
|
local spawner = productionPoint ~= nil and productionPoint.palletSpawner or nil
|
|
if spawner ~= nil then
|
|
local nodes = {}
|
|
for _, place in ipairs(spawner.spawnPlaces or {}) do
|
|
if place.startNode ~= nil then table.insert(nodes, place.startNode) end
|
|
end
|
|
if #nodes > 0 then table.insert(loadGroups, nodes) end
|
|
end
|
|
if placeable.spec_objectStorage ~= nil and placeable.rootNode ~= nil then
|
|
table.insert(loadGroups, {placeable.rootNode})
|
|
table.insert(unloadNodes, placeable.rootNode)
|
|
end
|
|
return loadGroups, unloadNodes
|
|
end
|
|
|
|
-- Naturlig sortering: tal i navne sammenlignes som tal ("Silo 9" < "Silo 10", "25 Silo" < "26 Silo").
|
|
local function naturalKey(text)
|
|
local parts = {}
|
|
for chunk in string.gmatch(tostring(text or ""), "%d+%.?%d*[^%d]*") do
|
|
local number, rest = string.match(chunk, "^(%d+%.?%d*)(.*)$")
|
|
table.insert(parts, {tonumber(number), string.lower(rest)})
|
|
end
|
|
local leading = string.match(tostring(text or ""), "^[^%d]*")
|
|
if leading ~= nil and leading ~= "" then
|
|
table.insert(parts, 1, {nil, string.lower(leading)})
|
|
end
|
|
return parts
|
|
end
|
|
|
|
function ADBuildings.naturalLess(a, b)
|
|
local ka, kb = naturalKey(a), naturalKey(b)
|
|
for index = 1, math.max(#ka, #kb) do
|
|
local pa, pb = ka[index], kb[index]
|
|
if pa == nil then return true end
|
|
if pb == nil then return false end
|
|
if pa[1] ~= nil and pb[1] ~= nil and pa[1] ~= pb[1] then return pa[1] < pb[1] end
|
|
if (pa[1] == nil) ~= (pb[1] == nil) then return pa[1] == nil end
|
|
if pa[2] ~= pb[2] then return pa[2] < pb[2] end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- Bygninger sorteres efter navn (naturligt), dernæst første læssemarkørs navn (ens navne, fx 40 Farma-siloer).
|
|
function ADBuildings.compare(a, b)
|
|
if a.name ~= b.name then return ADBuildings.naturalLess(a.name, b.name) end
|
|
local ma = a.loadMarkers ~= nil and a.loadMarkers[1] ~= nil and a.loadMarkers[1].name or (a.unloadMarker ~= nil and a.unloadMarker.name or "")
|
|
local mb = b.loadMarkers ~= nil and b.loadMarkers[1] ~= nil and b.loadMarkers[1].name or (b.unloadMarker ~= nil and b.unloadMarker.name or "")
|
|
return ADBuildings.naturalLess(ma, mb)
|
|
end
|
|
|
|
-- Nærmeste ikke-Wait-markør til en af noderne, ≤ MAX_MARKER_DISTANCE.
|
|
local function nearestMarker(adEnv, nodes)
|
|
local best, bestDistance = nil, ADBuildings.MAX_MARKER_DISTANCE
|
|
if #nodes == 0 then
|
|
return nil
|
|
end
|
|
local positions = {}
|
|
for _, node in ipairs(nodes) do
|
|
local x, z = getNodePosition(node)
|
|
if x ~= nil then table.insert(positions, {x = x, z = z}) end
|
|
end
|
|
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
|
|
if not ADSupplyPlanner.isWaitMarker(marker) then
|
|
local wayPoint = adEnv.ADGraphManager:getWayPointById(marker.id)
|
|
if wayPoint ~= nil then
|
|
for _, position in ipairs(positions) do
|
|
local dx, dz = wayPoint.x - position.x, wayPoint.z - position.z
|
|
local distance = math.sqrt(dx * dx + dz * dz)
|
|
if distance < bestDistance then
|
|
best, bestDistance = marker, distance
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if best == nil then
|
|
return nil
|
|
end
|
|
return {id = best.id, name = tostring(best.name), distance = math.floor(bestDistance + 0.5)}
|
|
end
|
|
|
|
-- Silo: den markør hvis STRENGE opslag (trigger bag næsen) rammer siloens læssestation.
|
|
local function siloLoadMarker(adEnv, farmId, placeable, loadStations)
|
|
local own = {}
|
|
for _, station in pairs(loadStations) do
|
|
if station.owningPlaceable == placeable then own[station] = true end
|
|
end
|
|
local best = nil
|
|
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
|
|
if not ADSupplyPlanner.isWaitMarker(marker) then
|
|
local station = ADSmartPickup.getLoadStationAtWayPointId(adEnv, marker.id, farmId)
|
|
if station ~= nil and own[station] then
|
|
best = {id = marker.id, name = tostring(marker.name), distance = 0}
|
|
break
|
|
end
|
|
end
|
|
end
|
|
return best
|
|
end
|
|
|
|
local function isSellPlaceable(placeable, unloadStations)
|
|
for _, station in pairs(unloadStations) do
|
|
if station.owningPlaceable == placeable and ADPrices.isSellingStation(station) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
local function build(adEnv, farmId)
|
|
local placeables = g_currentMission ~= nil and g_currentMission.placeableSystem ~= nil and g_currentMission.placeableSystem.placeables or {}
|
|
local storage = g_currentMission ~= nil and g_currentMission.storageSystem or nil
|
|
local loadStations = storage ~= nil and storage:getLoadingStations() or {}
|
|
local unloadStations = storage ~= nil and storage:getUnloadingStations() or {}
|
|
local result = {}
|
|
for _, placeable in pairs(placeables) do
|
|
local kind = getKind(placeable)
|
|
local isSell = kind == nil and isSellPlaceable(placeable, unloadStations)
|
|
if isSell then kind = "sell" end
|
|
if kind ~= nil and (kind == "sell" or isOwnedBy(placeable, farmId)) then
|
|
local id = ADBuildings.getId(placeable)
|
|
if id ~= nil then
|
|
local loadGroups, unloadNodes = collectNodes(placeable, loadStations, unloadStations)
|
|
local loadMarkers, seen = {}, {}
|
|
if kind == "silo" then
|
|
local marker = siloLoadMarker(adEnv, farmId, placeable, loadStations)
|
|
if marker ~= nil then table.insert(loadMarkers, marker) end
|
|
else
|
|
for _, nodes in ipairs(loadGroups) do
|
|
local marker = nearestMarker(adEnv, nodes)
|
|
if marker ~= nil and not seen[marker.id] then
|
|
seen[marker.id] = true
|
|
table.insert(loadMarkers, marker)
|
|
end
|
|
end
|
|
table.sort(loadMarkers, function(a, b) return ADBuildings.naturalLess(a.name, b.name) end)
|
|
end
|
|
table.insert(result, {
|
|
id = id, name = getName(placeable, id), kind = kind, placeable = placeable,
|
|
loadMarker = loadMarkers[1],
|
|
loadMarkers = loadMarkers,
|
|
unloadMarker = nearestMarker(adEnv, unloadNodes),
|
|
canBeSource = kind ~= "sell",
|
|
canBeTarget = kind == "husbandry" or kind == "factory",
|
|
canSell = kind == "sell",
|
|
})
|
|
end
|
|
end
|
|
end
|
|
table.sort(result, ADBuildings.compare)
|
|
return result
|
|
end
|
|
|
|
function ADBuildings.invalidate()
|
|
cache.list, cache.time = nil, -math.huge
|
|
end
|
|
|
|
function ADBuildings.list(adEnv, farmId)
|
|
local time = now()
|
|
if cache.list ~= nil and cache.farmId == farmId and (time - cache.time) < ADBuildings.CACHE_MS then
|
|
return cache.list
|
|
end
|
|
cache.list, cache.farmId, cache.time = build(adEnv, farmId), farmId, time
|
|
return cache.list
|
|
end
|
|
|
|
function ADBuildings.find(adEnv, farmId, id)
|
|
for _, building in ipairs(ADBuildings.list(adEnv, farmId)) do
|
|
if building.id == id then return building end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- role "load": bygningen hvis læssemarkør er wayPointId; "unload": aflæsningsmarkør.
|
|
function ADBuildings.byMarker(adEnv, farmId, wayPointId, role)
|
|
for _, building in ipairs(ADBuildings.list(adEnv, farmId)) do
|
|
if role == "unload" then
|
|
if building.unloadMarker ~= nil and building.unloadMarker.id == wayPointId then return building end
|
|
else
|
|
for _, marker in ipairs(building.loadMarkers or {}) do
|
|
if marker.id == wayPointId then return building end
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- Alle markører for en rolle: læsning = én pr. læssestation; aflæsning = én.
|
|
function ADBuildings.getMarkers(building, role)
|
|
if role == "unload" then
|
|
return building.unloadMarker ~= nil and {building.unloadMarker} or {}
|
|
end
|
|
return building.loadMarkers or (building.loadMarker ~= nil and {building.loadMarker} or {})
|
|
end
|