merge main (1.62.417 færre hak: markørindeks, cachet kildesøgning, kursus-bibliotek) ind i mp — 1.62.417.1
This commit is contained in:
@@ -252,45 +252,92 @@ function ADBuildings.labelWithPlace(building)
|
||||
return tostring(building ~= nil and building.name or "?") .. ADBuildings.placeSuffix(building)
|
||||
end
|
||||
|
||||
-- Alle navngivne markører (uden P/Connect/Wait) med position -> {{name, x, z}}
|
||||
local function namedMarkers(adEnv)
|
||||
local list = {}
|
||||
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
|
||||
local wayPoint = ADBuildings.isCandidateMarker(marker) and adEnv.ADGraphManager:getWayPointById(marker.id) or nil
|
||||
if wayPoint ~= nil then table.insert(list, {name = tostring(marker.name), x = wayPoint.x, z = wayPoint.z}) end
|
||||
end
|
||||
return list
|
||||
-- Markørindeks: markørerne med position i et gitter, så et opslag kun ser på markører i nærheden af bygningen.
|
||||
-- Bygges én gang pr. opbygning af listen (savegame8 27/10: alle markører x alle noder x alle bygninger tog
|
||||
-- 190-230 ms hvert 30. s og gav et hak). entries = {{marker, x, z, order}}; order = rækkefølgen i AD's markørliste.
|
||||
ADBuildings.GRID_CELL_M = 50
|
||||
local GRID_OFFSET, GRID_STRIDE = 100000, 1000000
|
||||
|
||||
local function cellOf(cellSize, x, z)
|
||||
return math.floor(x / cellSize), math.floor(z / cellSize)
|
||||
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
|
||||
function ADBuildings.newMarkerIndex(entries, cellSize)
|
||||
local index = {cell = cellSize or ADBuildings.GRID_CELL_M, cells = {}, all = entries or {}}
|
||||
for _, entry in ipairs(index.all) do
|
||||
local cx, cz = cellOf(index.cell, entry.x, entry.z)
|
||||
local key = (cx + GRID_OFFSET) * GRID_STRIDE + (cz + GRID_OFFSET)
|
||||
local bucket = index.cells[key]
|
||||
if bucket == nil then
|
||||
bucket = {}
|
||||
index.cells[key] = bucket
|
||||
end
|
||||
table.insert(bucket, entry)
|
||||
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 ADBuildings.isCandidateMarker(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
|
||||
return index
|
||||
end
|
||||
|
||||
-- Indeksets markører i gittercellerne der dækker radius om mindst én position (et overmængde: kalderen måler selv
|
||||
-- afstanden), i AD's rækkefølge som en fuld gennemgang ville give dem.
|
||||
function ADBuildings.markersNear(index, positions, radius)
|
||||
local found, seen = {}, {}
|
||||
local span = math.ceil((radius or 0) / index.cell)
|
||||
for _, position in ipairs(positions or {}) do
|
||||
local cx, cz = cellOf(index.cell, position.x, position.z)
|
||||
for ix = cx - span, cx + span do
|
||||
for iz = cz - span, cz + span do
|
||||
local bucket = index.cells[(ix + GRID_OFFSET) * GRID_STRIDE + (iz + GRID_OFFSET)]
|
||||
for _, entry in ipairs(bucket or {}) do
|
||||
if not seen[entry] then
|
||||
seen[entry] = true
|
||||
table.insert(found, entry)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if best == nil then
|
||||
return nil
|
||||
table.sort(found, function(a, b) return a.order < b.order end)
|
||||
return found
|
||||
end
|
||||
|
||||
-- AD's markører der opfylder accept, med vejpunktets position -> {{marker, x, z, order}}
|
||||
local function scanMarkers(adEnv, accept)
|
||||
local list = {}
|
||||
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
|
||||
local wayPoint = accept(marker) and adEnv.ADGraphManager:getWayPointById(marker.id) or nil
|
||||
if wayPoint ~= nil then table.insert(list, {marker = marker, x = wayPoint.x, z = wayPoint.z, order = #list + 1}) end
|
||||
end
|
||||
return {id = best.id, name = tostring(best.name), distance = math.floor(bestDistance + 0.5)}
|
||||
return list
|
||||
end
|
||||
|
||||
local function isNonWaitMarker(marker) return not ADSupplyPlanner.isWaitMarker(marker) end
|
||||
|
||||
-- under build(): {candidates = indeks over bygnings-kandidater, nonWait = indeks over alle ikke-Wait-markører}
|
||||
local buildIndex = nil
|
||||
|
||||
-- Kandidatmarkører (ikke Wait/P/Connect/service) der kan stå inden for radius af positionerne.
|
||||
local function candidatesNear(adEnv, positions, radius)
|
||||
if buildIndex ~= nil then return ADBuildings.markersNear(buildIndex.candidates, positions, radius) end
|
||||
return scanMarkers(adEnv, ADBuildings.isCandidateMarker)
|
||||
end
|
||||
|
||||
local function nodePositions(nodes)
|
||||
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
|
||||
return positions
|
||||
end
|
||||
|
||||
-- Navngivne markører (uden P/Connect/Wait) nær et punkt -> {{name, x, z}} til nearestNamedMarker
|
||||
local function namedMarkersNear(adEnv, x, z)
|
||||
local list = {}
|
||||
if x == nil or z == nil then return list end
|
||||
for _, entry in ipairs(candidatesNear(adEnv, {{x = x, z = z}}, ADBuildings.NEAR_MARKER_M)) do
|
||||
table.insert(list, {name = tostring(entry.marker.name), x = entry.x, z = entry.z})
|
||||
end
|
||||
return list
|
||||
end
|
||||
|
||||
-- Aflæsningsmarkør: nærmeste ikke-Wait-markør ≤ MAX_MARKER_DISTANCE til aflæsningsnoderne — men ikke
|
||||
@@ -298,22 +345,17 @@ end
|
||||
-- aflæsningstriggeren end drop). Kun én fælles markør (AIO Load / Unload) bruges til begge.
|
||||
-- Alle ikke-Wait-markører ≤ MAX_MARKER_DISTANCE til nodes, nærmeste først: {{id, name, distance}}.
|
||||
local function markerCandidates(adEnv, nodes)
|
||||
local positions, found = {}, {}
|
||||
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
|
||||
local wayPoint = ADBuildings.isCandidateMarker(marker) and adEnv.ADGraphManager:getWayPointById(marker.id) or nil
|
||||
if wayPoint ~= nil then
|
||||
local nearest = nil
|
||||
for _, position in ipairs(positions) do
|
||||
local distance = math.sqrt((wayPoint.x - position.x) ^ 2 + (wayPoint.z - position.z) ^ 2)
|
||||
if nearest == nil or distance < nearest then nearest = distance end
|
||||
end
|
||||
if nearest ~= nil and nearest < ADBuildings.MAX_MARKER_DISTANCE then
|
||||
table.insert(found, {id = marker.id, name = tostring(marker.name), distance = nearest, x = wayPoint.x, z = wayPoint.z})
|
||||
end
|
||||
local positions, found = nodePositions(nodes), {}
|
||||
if #positions == 0 then return found end
|
||||
for _, entry in ipairs(candidatesNear(adEnv, positions, ADBuildings.MAX_MARKER_DISTANCE)) do
|
||||
local marker = entry.marker
|
||||
local nearest = nil
|
||||
for _, position in ipairs(positions) do
|
||||
local distance = math.sqrt((entry.x - position.x) ^ 2 + (entry.z - position.z) ^ 2)
|
||||
if nearest == nil or distance < nearest then nearest = distance end
|
||||
end
|
||||
if nearest ~= nil and nearest < ADBuildings.MAX_MARKER_DISTANCE then
|
||||
table.insert(found, {id = marker.id, name = tostring(marker.name), distance = nearest, x = entry.x, z = entry.z})
|
||||
end
|
||||
end
|
||||
table.sort(found, function(a, b) return a.distance < b.distance end)
|
||||
@@ -415,21 +457,14 @@ end
|
||||
-- (in-game 27/9: Goat Food fik "Sheep Milk"-markørerne som mål og Goat Milk Pickup "Sheep Food" som kilde)
|
||||
local diagnosed = {}
|
||||
local function closestMarkers(adEnv, nodes, count)
|
||||
local positions, found = {}, {}
|
||||
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
|
||||
local wayPoint = ADBuildings.isCandidateMarker(marker) and adEnv.ADGraphManager:getWayPointById(marker.id) or nil
|
||||
if wayPoint ~= nil then
|
||||
local nearest = nil
|
||||
for _, position in ipairs(positions) do
|
||||
local distance = math.sqrt((wayPoint.x - position.x) ^ 2 + (wayPoint.z - position.z) ^ 2)
|
||||
if nearest == nil or distance < nearest then nearest = distance end
|
||||
end
|
||||
if nearest ~= nil and nearest < ADBuildings.MAX_MARKER_DISTANCE then table.insert(found, {name = tostring(marker.name), distance = nearest}) end
|
||||
local positions, found = nodePositions(nodes), {}
|
||||
for _, entry in ipairs(#positions > 0 and candidatesNear(adEnv, positions, ADBuildings.MAX_MARKER_DISTANCE) or {}) do
|
||||
local nearest = nil
|
||||
for _, position in ipairs(positions) do
|
||||
local distance = math.sqrt((entry.x - position.x) ^ 2 + (entry.z - position.z) ^ 2)
|
||||
if nearest == nil or distance < nearest then nearest = distance end
|
||||
end
|
||||
if nearest ~= nil and nearest < ADBuildings.MAX_MARKER_DISTANCE then table.insert(found, {name = tostring(entry.marker.name), distance = nearest}) end
|
||||
end
|
||||
table.sort(found, function(a, b) return a.distance < b.distance end)
|
||||
local parts = {}
|
||||
@@ -537,27 +572,19 @@ local function unloadMarkerFor(adEnv, nodes, loadMarkers, allowSkipLoad, placeab
|
||||
end
|
||||
local isLoad = {}
|
||||
for _, marker in ipairs(loadMarkers or {}) do isLoad[marker.id] = true 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
|
||||
local positions = nodePositions(nodes)
|
||||
local best, bestOther, nearbyAll = nil, nil, {}
|
||||
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
|
||||
if ADBuildings.isCandidateMarker(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 < ADBuildings.MAX_MARKER_DISTANCE then
|
||||
local entry = {marker = marker, distance = distance, x = wayPoint.x, z = wayPoint.z}
|
||||
table.insert(nearbyAll, entry)
|
||||
if best == nil or distance < best.distance then best = entry end
|
||||
if not isLoad[marker.id] and (bestOther == nil or distance < bestOther.distance) then
|
||||
bestOther = entry
|
||||
end
|
||||
end
|
||||
for _, near in ipairs(#positions > 0 and candidatesNear(adEnv, positions, ADBuildings.MAX_MARKER_DISTANCE) or {}) do
|
||||
local marker = near.marker
|
||||
for _, position in ipairs(positions) do
|
||||
local dx, dz = near.x - position.x, near.z - position.z
|
||||
local distance = math.sqrt(dx * dx + dz * dz)
|
||||
if distance < ADBuildings.MAX_MARKER_DISTANCE then
|
||||
local entry = {marker = marker, distance = distance, x = near.x, z = near.z}
|
||||
table.insert(nearbyAll, entry)
|
||||
if best == nil or distance < best.distance then best = entry end
|
||||
if not isLoad[marker.id] and (bestOther == nil or distance < bestOther.distance) then
|
||||
bestOther = entry
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -621,22 +648,35 @@ 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 = {}
|
||||
local own, triggerPositions = {}, {}
|
||||
for _, station in pairs(loadStations) do
|
||||
if station.owningPlaceable == placeable then own[station] = true end
|
||||
if station.owningPlaceable == placeable then
|
||||
own[station] = true
|
||||
for _, trigger in pairs(station.loadTriggers or {}) do
|
||||
local x, z = getNodePosition(trigger.triggerNode)
|
||||
if x ~= nil then table.insert(triggerPositions, {x = x, z = z}) end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- det strenge opslag finder kun en station hvis triggerNode står inden for MAX_MARKER_TO_TRIGGER_DISTANCE af
|
||||
-- markøren: kun markører så tæt på siloens egne triggere spørges (uden kendte triggere: alle, som før)
|
||||
local markers
|
||||
if buildIndex ~= nil and #triggerPositions > 0 then
|
||||
local radius = (ADSmartPickup.MAX_MARKER_TO_TRIGGER_DISTANCE or ADBuildings.MAX_MARKER_DISTANCE) + 1
|
||||
markers = ADBuildings.markersNear(buildIndex.nonWait, triggerPositions, radius)
|
||||
else
|
||||
markers = scanMarkers(adEnv, isNonWaitMarker)
|
||||
end
|
||||
-- rammer flere markører stationen (AD's triggerafstand er stor, 25 m), vinder den der står tættest på
|
||||
-- læssetriggeren — ikke den tilfældigt første (in-game 9/10: marksiloen ved F9 fik markmarkøren 'F9' 19 m væk
|
||||
-- i stedet for 'F9 Silo' 11 m; vognen stod uden for boksen)
|
||||
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
|
||||
local wayPoint = adEnv.ADGraphManager:getWayPointById(marker.id)
|
||||
local distance = wayPoint ~= nil and stationTriggerDistance(station, wayPoint.x, wayPoint.z) or 0
|
||||
best = ADBuildings.closerHit(best, {id = marker.id, name = tostring(marker.name), distance = math.floor(distance + 0.5)})
|
||||
end
|
||||
for _, entry in ipairs(markers) do
|
||||
local marker = entry.marker
|
||||
local station = ADSmartPickup.getLoadStationAtWayPointId(adEnv, marker.id, farmId)
|
||||
if station ~= nil and own[station] then
|
||||
local distance = stationTriggerDistance(station, entry.x, entry.z)
|
||||
best = ADBuildings.closerHit(best, {id = marker.id, name = tostring(marker.name), distance = math.floor(distance + 0.5)})
|
||||
end
|
||||
end
|
||||
return best
|
||||
@@ -693,7 +733,7 @@ end
|
||||
|
||||
local fallbackLogged = {} -- "stald-id:markør-id" -> true
|
||||
|
||||
local function build(adEnv, farmId)
|
||||
local function buildList(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 {}
|
||||
@@ -704,8 +744,6 @@ local function build(adEnv, farmId)
|
||||
-- naboens marker kan være nærmere dem (in-game 17/10 08:05: Hønsehus 2 tog 'Coop 1 Egg Pickup' 7 m fra sin
|
||||
-- palleplads, og Hønsehus 3 stod uden markør)
|
||||
local loadOwner, prepared = {}, {}
|
||||
local okNamed, named = pcall(namedMarkers, adEnv)
|
||||
if not okNamed then named = {} end
|
||||
for _, placeable in pairs(placeables) do
|
||||
if getKind(placeable) == "husbandry" and isOwnedBy(placeable, farmId) then
|
||||
local loadGroups, unloadNodes = collectNodes(placeable, loadStations, unloadStations)
|
||||
@@ -784,7 +822,7 @@ local function build(adEnv, farmId)
|
||||
local nearX, nearZ = getNodePosition(placeable.rootNode)
|
||||
table.insert(result, {
|
||||
id = id, name = getName(placeable, id), kind = kind, placeable = placeable,
|
||||
near = ADBuildings.nearestNamedMarker(named, nearX, nearZ), -- stedet (nærmeste AD-markør) til listerne
|
||||
near = ADBuildings.nearestNamedMarker(namedMarkersNear(adEnv, nearX, nearZ), nearX, nearZ), -- stedet (nærmeste AD-markør) til listerne
|
||||
loadMarker = loadMarkers[1],
|
||||
loadMarkers = loadMarkers,
|
||||
unloadMarker = unloadMarker,
|
||||
@@ -800,6 +838,23 @@ local function build(adEnv, farmId)
|
||||
return result
|
||||
end
|
||||
|
||||
-- Listen bygges med et markørindeks (se newMarkerIndex); et indlejret kald (stationsopslag der selv beder om listen)
|
||||
-- får sit eget indeks, og det ydre gendannes bagefter.
|
||||
-- debug-biblioteket er ikke sikkert til stede i spillets release-runtime (review 27/10)
|
||||
local traceback = (type(debug) == "table" and debug.traceback) or function(err) return err end
|
||||
|
||||
local function build(adEnv, farmId)
|
||||
local previous = buildIndex
|
||||
buildIndex = {
|
||||
candidates = ADBuildings.newMarkerIndex(scanMarkers(adEnv, ADBuildings.isCandidateMarker)),
|
||||
nonWait = ADBuildings.newMarkerIndex(scanMarkers(adEnv, isNonWaitMarker)),
|
||||
}
|
||||
local ok, result = xpcall(buildList, traceback, adEnv, farmId)
|
||||
buildIndex = previous
|
||||
if not ok then error(result, 0) end
|
||||
return result
|
||||
end
|
||||
|
||||
-- Sted-endelsen for en bygning i den cachede liste (til "Vis bygning"-vælgeren): " · Silo Complex" eller ""
|
||||
function ADBuildings.placeSuffixFor(adEnv, farmId, placeable)
|
||||
local id = placeable ~= nil and ADBuildings.getId(placeable) or nil
|
||||
|
||||
Reference in New Issue
Block a user