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
|
||||
|
||||
@@ -79,6 +79,9 @@ function ADCourseCache.without(records, vehicleId)
|
||||
return result
|
||||
end
|
||||
|
||||
-- Spilleren har lavet et nyt kursus på maskinen (i CP's HUD): så hentes heller intet fra biblioteket
|
||||
ADCourseCache.STALE_REASON = "kurset på maskinen er ændret"
|
||||
|
||||
-- Kursus der kan genbruges: egen maskines først ("own"), ellers en anden maskines ("copy", laveste id først).
|
||||
-- isValid(record) (valgfri) tjekker at kurset stadig sidder på maskinen. Intet: nil + grunden.
|
||||
function ADCourseCache.findReusable(records, request, vehicleId, isValid)
|
||||
@@ -103,7 +106,7 @@ function ADCourseCache.findReusable(records, request, vehicleId, isValid)
|
||||
for _, id in ipairs(ids) do
|
||||
if usable(records[id]) then return records[id], "copy" end
|
||||
end
|
||||
if anyStale then return nil, "kurset på maskinen er ændret" end
|
||||
if anyStale then return nil, ADCourseCache.STALE_REASON end
|
||||
return nil, ADCourseCache.mismatchReason(own, request)
|
||||
end
|
||||
|
||||
@@ -206,7 +209,15 @@ local function prepareUnsafe(adEnv, field, info, assignment, opts)
|
||||
result.course = source.course
|
||||
log(field.id, "kursus kopieret fra '%s' til '%s' — ingen ny beregning", ADRuns.vehicleLabel(source.vehicle), ADRuns.vehicleLabel(vehicle))
|
||||
else
|
||||
log(field.id, "nyt kursus beregnes til '%s' (%s)", ADRuns.vehicleLabel(vehicle), tostring(how))
|
||||
-- biblioteket husker kurser som maskinerne har fået skiftet ud (adCourseLibrary.lua)
|
||||
local saved = how ~= ADCourseCache.STALE_REASON and ADCourseLibrary ~= nil and ADCourseLibrary.get(ADCourseCache.key(request)) or nil
|
||||
if saved ~= nil then
|
||||
result.course = saved
|
||||
log(field.id, "kursus hentet fra biblioteket til '%s' — ingen ny beregning", ADRuns.vehicleLabel(vehicle))
|
||||
how = "library"
|
||||
else
|
||||
log(field.id, "nyt kursus beregnes til '%s' (%s)", ADRuns.vehicleLabel(vehicle), tostring(how))
|
||||
end
|
||||
end
|
||||
return result, {request = request, how = how}
|
||||
end
|
||||
@@ -232,6 +243,11 @@ function ADCourseCache.afterStart(vehicle, vehicleId, context)
|
||||
local r = context.request
|
||||
local record = ADCourseCache.newRecord({vehicle = vehicleId, field = r.field, width = r.width, sig = r.sig,
|
||||
settings = r.settings, waypoints = waypointCount(course)})
|
||||
-- biblioteket: nyberegnede kurser afløser det gemte; kun kurser med forager (som genbrugsreglen i ADCpBridge)
|
||||
local fresh = context.how ~= "own" and context.how ~= "copy" and context.how ~= "library"
|
||||
if ADCourseLibrary ~= nil and (ADCpBridge.courseHasHeadlands == nil or ADCpBridge.courseHasHeadlands(course)) then
|
||||
ADCourseLibrary.put(ADCourseCache.key(r), course, fresh)
|
||||
end
|
||||
local existing = records[record.vehicle]
|
||||
if existing ~= nil and ADCourseCache.matches(existing, r) and existing.waypoints == record.waypoints then return end
|
||||
setRecords(ADCourseCache.withRecord(records, record))
|
||||
@@ -243,6 +259,7 @@ end
|
||||
function ADCourseCache.checkField(fieldId, sig)
|
||||
local records = getRecords()
|
||||
local kept, removed = ADCourseCache.invalidateField(records, fieldId, sig)
|
||||
if sig ~= nil and ADCourseLibrary ~= nil then removed = removed + ADCourseLibrary.forget(fieldId, sig) end
|
||||
if removed > 0 then
|
||||
setRecords(kept)
|
||||
log(fieldId, "markens form er ændret — %d gemt(e) kursus(er) glemt, næste start beregner nyt", removed)
|
||||
@@ -254,6 +271,7 @@ end
|
||||
function ADCourseCache:consoleForget(arg)
|
||||
local fieldId = tonumber((tostring(arg or ""):gsub("^[Ff]", "")))
|
||||
local kept, removed = ADCourseCache.forget(getRecords(), fieldId)
|
||||
if ADCourseLibrary ~= nil then removed = removed + ADCourseLibrary.forget(fieldId) end
|
||||
setRecords(kept)
|
||||
local text = string.format("ADSmartPickup: %d gemt(e) kursus(er) glemt (%s) — næste start beregner nyt", removed,
|
||||
fieldId ~= nil and ("F" .. tostring(fieldId)) or "alle marker")
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
-- AD Course Library
|
||||
-- Bibliotek af beregnede Courseplay-kurser pr. "mark|bredde|form|indstillinger" (ADCourseCache.key). Registret i
|
||||
-- adCourseCache husker kun hver maskines SENESTE kursus: får maskinen en anden mark, er kurset væk, og næste gang
|
||||
-- beregnes det forfra (savegame8 27/10: 6 nye beregninger på 20 min, hver en frysning på 1,3-4 s). Biblioteket
|
||||
-- beholder en kopi af hvert kursus og gemmes i savegamet (ADSmartPickup_courses.xml, Courseplays eget kursusformat).
|
||||
-- Rene regler øverst (tests/test_adCourseLibrary.lua); spil-nær lim nederst (alt pcall'et).
|
||||
|
||||
-- info-log bag "Detaljeret log" (ADLog i adSettings.lua; uden ADLog — tests — direkte til Logging)
|
||||
local function logInfo(...) return (ADLog or Logging).info(...) end
|
||||
|
||||
ADCourseLibrary = {}
|
||||
|
||||
ADCourseLibrary.FILE_NAME = "ADSmartPickup_courses.xml"
|
||||
ADCourseLibrary.ROOT = "ADSmartPickupCourses"
|
||||
ADCourseLibrary.MAX_ENTRIES = 40
|
||||
ADCourseLibrary.LOG_PREFIX = "ADSmartPickup: "
|
||||
|
||||
-- key -> {course = Course, used = løbenummer}; used stiger ved hver brug (mindst brugte smides ud først)
|
||||
ADCourseLibrary.entries = {}
|
||||
ADCourseLibrary.counter = 0
|
||||
ADCourseLibrary.loaded = false
|
||||
ADCourseLibrary.writable = false -- filen må skrives (læst uden fejl, eller fandtes ikke)
|
||||
ADCourseLibrary.dirty = false -- noget er ændret siden sidste gem
|
||||
|
||||
-- ---- rene regler ----
|
||||
|
||||
-- Marken i en nøgle ("41|50.0|sig|off=..." -> 41); nil når nøglen ikke har formen.
|
||||
function ADCourseLibrary.fieldOfKey(key)
|
||||
if type(key) ~= "string" then return nil end
|
||||
return tonumber(key:match("^([^|]+)|"))
|
||||
end
|
||||
|
||||
-- Nøglerne der skal ud for at holde biblioteket på max poster: de mindst brugte (laveste used), ældste først.
|
||||
function ADCourseLibrary.evictions(entries, max)
|
||||
local keys = {}
|
||||
for key in pairs(entries or {}) do table.insert(keys, key) end
|
||||
if #keys <= (max or 0) then return {} end
|
||||
table.sort(keys, function(a, b)
|
||||
local ua, ub = entries[a].used or 0, entries[b].used or 0
|
||||
if ua ~= ub then return ua < ub end
|
||||
return a < b
|
||||
end)
|
||||
local drop = {}
|
||||
for index = 1, #keys - max do table.insert(drop, keys[index]) end
|
||||
return drop
|
||||
end
|
||||
|
||||
-- Nøglerne for en mark (fieldId) eller alle (nil); keepSig = behold poster med netop den formsignatur.
|
||||
function ADCourseLibrary.keysFor(entries, fieldId, keepSig)
|
||||
local keys = {}
|
||||
for key in pairs(entries or {}) do
|
||||
local sig = key:match("^[^|]+|[^|]+|([^|]+)|")
|
||||
if (fieldId == nil or ADCourseLibrary.fieldOfKey(key) == fieldId) and (keepSig == nil or sig ~= keepSig) then
|
||||
table.insert(keys, key)
|
||||
end
|
||||
end
|
||||
table.sort(keys)
|
||||
return keys
|
||||
end
|
||||
|
||||
-- ---- spil-nær lim ----
|
||||
|
||||
local function touch(entry)
|
||||
ADCourseLibrary.counter = ADCourseLibrary.counter + 1
|
||||
entry.used = ADCourseLibrary.counter
|
||||
end
|
||||
|
||||
local function isSingleVehicle(course)
|
||||
return course ~= nil and (course.nVehicles == nil or course.nVehicles == 1)
|
||||
end
|
||||
|
||||
local materialize -- (entry) -> course|nil; pakker et gemt kursus ud første gang det bruges (spil-lim nedenfor)
|
||||
|
||||
-- Kurset til nøglen (en kopi gives videre af ADCpBridge via cpCopyCourse), eller nil.
|
||||
function ADCourseLibrary.get(key)
|
||||
if key == nil then return nil end
|
||||
ADCourseLibrary.load()
|
||||
local entry = ADCourseLibrary.entries[key]
|
||||
if entry == nil then return nil end
|
||||
local course = materialize(entry)
|
||||
if course == nil then
|
||||
ADCourseLibrary.entries[key] = nil
|
||||
ADCourseLibrary.dirty = true
|
||||
return nil
|
||||
end
|
||||
touch(entry)
|
||||
return course
|
||||
end
|
||||
|
||||
-- Gem en kopi af kurset under nøglen. Findes nøglen, tæller det kun som brug — medmindre replace (et nyberegnet
|
||||
-- kursus afløser det gemte). Kun kurser til én maskine.
|
||||
function ADCourseLibrary.put(key, course, replace)
|
||||
if key == nil or not isSingleVehicle(course) or course.copy == nil then return end
|
||||
ADCourseLibrary.load()
|
||||
local entry = ADCourseLibrary.entries[key]
|
||||
if entry ~= nil and not replace then
|
||||
touch(entry)
|
||||
return
|
||||
end
|
||||
local ok, copy = pcall(course.copy, course)
|
||||
if not ok or copy == nil then
|
||||
Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek: kunne ikke kopiere kursus: %s", tostring(copy))
|
||||
return
|
||||
end
|
||||
-- Course:copy tager ikke disse med: compacted = lige rækker gemmes som start+slut (ellers bliver filen stor)
|
||||
copy.compacted, copy.headlandClockwise, copy.islandHeadlandClockwise = course.compacted, course.headlandClockwise, course.islandHeadlandClockwise
|
||||
entry = {course = copy}
|
||||
touch(entry)
|
||||
ADCourseLibrary.entries[key] = entry
|
||||
ADCourseLibrary.dirty = true
|
||||
for _, drop in ipairs(ADCourseLibrary.evictions(ADCourseLibrary.entries, ADCourseLibrary.MAX_ENTRIES)) do
|
||||
ADCourseLibrary.entries[drop] = nil
|
||||
end
|
||||
end
|
||||
|
||||
-- Glem kurser for en mark (nil = alle); keepSig = behold dem med markens nuværende form. Returnerer antal.
|
||||
function ADCourseLibrary.forget(fieldId, keepSig)
|
||||
ADCourseLibrary.load()
|
||||
local keys = ADCourseLibrary.keysFor(ADCourseLibrary.entries, fieldId, keepSig)
|
||||
for _, key in ipairs(keys) do ADCourseLibrary.entries[key] = nil end
|
||||
if #keys > 0 then ADCourseLibrary.dirty = true end
|
||||
return #keys
|
||||
end
|
||||
|
||||
local function getPath()
|
||||
return ADRunsStorage ~= nil and ADRunsStorage.getPath ~= nil and ADRunsStorage.getPath() ~= nil
|
||||
and (ADRunsStorage.getPath():gsub("[^/\\]+$", ADCourseLibrary.FILE_NAME)) or nil
|
||||
end
|
||||
|
||||
-- Courseplays klasser ligger i dens mod-miljø, den globale tabel FS25_Courseplay (zip-navnet) — som FS25_AutoDrive
|
||||
local function cpClass(name)
|
||||
local env = FS25_Courseplay
|
||||
return type(env) == "table" and env[name] or nil
|
||||
end
|
||||
|
||||
local schema = nil
|
||||
local function getSchema()
|
||||
if schema ~= nil then return schema end
|
||||
local courseManager = cpClass("CpCourseManager")
|
||||
if XMLSchema == nil or courseManager == nil or courseManager.registerXmlSchemaValues == nil then return nil end
|
||||
schema = XMLSchema.new("ADSmartPickupCourses")
|
||||
local key = ADCourseLibrary.ROOT .. ".course(?)"
|
||||
courseManager.registerXmlSchemaValues(schema, key)
|
||||
schema:register(XMLValueType.STRING, key .. "#adspKey", "ADSmartPickup: mark|bredde|form|indstillinger")
|
||||
schema:register(XMLValueType.INT, key .. "#adspUsed", "ADSmartPickup: senest brugt (løbenummer)")
|
||||
return schema
|
||||
end
|
||||
|
||||
-- Den gemte fil holdes åben, og et kursus pakkes først ud når det skal bruges: at læse alle kurser på én gang
|
||||
-- (Waypoint() slår terrænhøjden op for hvert punkt) ville selv give den frysning biblioteket skal fjerne (review 27/10).
|
||||
local openXml = nil
|
||||
|
||||
local function closeXml()
|
||||
if openXml ~= nil then
|
||||
openXml:delete()
|
||||
openXml = nil
|
||||
end
|
||||
end
|
||||
|
||||
materialize = function(entry)
|
||||
if entry.course ~= nil then return entry.course end
|
||||
local courseClass = cpClass("Course")
|
||||
if entry.xmlKey == nil or openXml == nil or courseClass == nil or courseClass.createFromXml == nil then return nil end
|
||||
local ok, course = pcall(courseClass.createFromXml, nil, openXml, entry.xmlKey)
|
||||
if not ok or not isSingleVehicle(course) then
|
||||
if not ok then Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek: et gemt kursus kunne ikke læses: %s", tostring(course)) end
|
||||
return nil
|
||||
end
|
||||
entry.course, entry.xmlKey = course, nil
|
||||
return course
|
||||
end
|
||||
|
||||
-- Kun nøglerne læses; nil = filen kunne ikke åbnes/læses (så må den heller ikke overskrives).
|
||||
local function loadUnsafe()
|
||||
local path, xmlSchema = getPath(), getSchema()
|
||||
if path == nil or xmlSchema == nil or cpClass("Course") == nil then return nil end
|
||||
if fileExists ~= nil and not fileExists(path) then return 0 end
|
||||
local xmlFile = XMLFile.loadIfExists("adspCourses", path, xmlSchema)
|
||||
if xmlFile == nil then return nil end
|
||||
openXml = xmlFile
|
||||
local count = 0
|
||||
xmlFile:iterate(ADCourseLibrary.ROOT .. ".course", function(_, key)
|
||||
local libraryKey = xmlFile:getValue(key .. "#adspKey")
|
||||
if libraryKey ~= nil then
|
||||
local used = xmlFile:getValue(key .. "#adspUsed") or 0
|
||||
ADCourseLibrary.entries[libraryKey] = {xmlKey = key, used = used}
|
||||
ADCourseLibrary.counter = math.max(ADCourseLibrary.counter, used)
|
||||
count = count + 1
|
||||
end
|
||||
end)
|
||||
return count
|
||||
end
|
||||
|
||||
-- Indlæs biblioteket fra savegamet første gang det bruges. Fejler læsningen, skrives filen aldrig over (writable).
|
||||
function ADCourseLibrary.load()
|
||||
if ADCourseLibrary.loaded then return end
|
||||
ADCourseLibrary.loaded = true
|
||||
local ok, count = pcall(loadUnsafe)
|
||||
ADCourseLibrary.writable = ok and count ~= nil
|
||||
if not ok then
|
||||
Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek kunne ikke indlæses: %s", tostring(count))
|
||||
elseif count ~= nil and count > 0 then
|
||||
logInfo(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek: %d gemte kurser fundet", count)
|
||||
end
|
||||
end
|
||||
|
||||
local function saveUnsafe()
|
||||
local path, xmlSchema = getPath(), getSchema()
|
||||
if path == nil or xmlSchema == nil then return end
|
||||
-- filen skrives om: kurser der endnu ikke er pakket ud, læses først (kun når noget er ændret)
|
||||
local keys = {}
|
||||
for key, entry in pairs(ADCourseLibrary.entries) do
|
||||
if materialize(entry) ~= nil then table.insert(keys, key) end
|
||||
end
|
||||
table.sort(keys)
|
||||
closeXml()
|
||||
local xmlFile = XMLFile.create("adspCourses", path, ADCourseLibrary.ROOT, xmlSchema)
|
||||
if xmlFile == nil then return end
|
||||
local ok, err = pcall(function()
|
||||
for index, libraryKey in ipairs(keys) do
|
||||
local entry = ADCourseLibrary.entries[libraryKey]
|
||||
local key = string.format("%s.course(%d)", ADCourseLibrary.ROOT, index - 1)
|
||||
entry.course:saveToXml(xmlFile, key)
|
||||
xmlFile:setValue(key .. "#adspKey", libraryKey)
|
||||
xmlFile:setValue(key .. "#adspUsed", entry.used or 0)
|
||||
end
|
||||
xmlFile:save()
|
||||
end)
|
||||
xmlFile:delete()
|
||||
if not ok then error(err, 0) end
|
||||
ADCourseLibrary.dirty = false
|
||||
end
|
||||
|
||||
-- Skriv biblioteket i savegamet (når spillet gemmer) — kun når noget er ændret, og aldrig hvis læsningen fejlede.
|
||||
function ADCourseLibrary.save()
|
||||
if not ADCourseLibrary.loaded or not ADCourseLibrary.writable or not ADCourseLibrary.dirty then return end
|
||||
local ok, err = pcall(saveUnsafe)
|
||||
if not ok then Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek kunne ikke gemmes: %s", tostring(err)) end
|
||||
end
|
||||
|
||||
-- Spillet lukker kortet: den åbne fil frigives
|
||||
function ADCourseLibrary.close()
|
||||
pcall(closeXml)
|
||||
end
|
||||
|
||||
function ADCourseLibrary.installSaveHook()
|
||||
if ItemSystem == nil or ItemSystem.save == nil or Utils == nil then return end
|
||||
ItemSystem.save = Utils.prependedFunction(ItemSystem.save, function() ADCourseLibrary.save() end)
|
||||
if FSBaseMission ~= nil and FSBaseMission.delete ~= nil then
|
||||
FSBaseMission.delete = Utils.prependedFunction(FSBaseMission.delete, function() ADCourseLibrary.close() end)
|
||||
end
|
||||
end
|
||||
@@ -139,6 +139,11 @@ function ADProfiler.install()
|
||||
{ADSmartPickup, "shouldWaitForLoading", "vent ved pålæsning"},
|
||||
{ADSmartPickup, "choosePickup", "forsyning kildevalg"},
|
||||
{ADSmartPickup, "findSupplyPickup", "forsyning kildesøgning"},
|
||||
-- 27/10: kildesøgningen tog 40-45 ms pr. kald — delene måles hver for sig
|
||||
{ADSmartPickup, "restrictSupplySources", "forsyning kildefilter"},
|
||||
{ADSmartPickup, "getStableSiloFillTypes", "forsyning staldsilo"},
|
||||
{ADSmartPickup, "factoryMayGet", "forsyning fabriksreserve"},
|
||||
{ADSmartPickup, "buyFeed", "forsyning købefoder"},
|
||||
{ADRunsController, "applyVehicleNames", "traktornavne"},
|
||||
{ADFieldJobs, "tick", "markarbejde tick"},
|
||||
{ADFields, "scanStep", "markscanning"},
|
||||
@@ -151,6 +156,7 @@ function ADProfiler.install()
|
||||
{ADCpBridge, "start", "Courseplay-start"},
|
||||
{ADCourseCache, "prepare", "kursus-cache"},
|
||||
{ADBuildings, "list", "bygningsliste"},
|
||||
{ADNames, "autoName", "bygningsnavne"},
|
||||
{ADSources, "getFarmInventory", "gårdens lagre"},
|
||||
{ADWaitPool, "hasRouteTo", "rutetjek"},
|
||||
{ADWaitPool, "hasRouteBothWays", "rutetjek frem+tilbage"},
|
||||
|
||||
@@ -428,8 +428,9 @@ end
|
||||
local function collectMarkerStations(adEnv, candidateMarkers, farmId, excludedPlaceable)
|
||||
local markerStations = {}
|
||||
for _, marker in pairs(candidateMarkers) do
|
||||
local wayPoint = ADSmartPickup.isSourceMarker(marker) and adEnv.ADGraphManager:getWayPointById(marker.id) or nil
|
||||
local station = wayPoint ~= nil and getLoadStationAtMarker(adEnv, wayPoint, farmId) or nil
|
||||
-- cachet opslag (30 s, spredt udløb): kildesøgningen kører for hver vogn og gennemgik alle markører x alle
|
||||
-- stationer x triggere hver gang (savegame8 27/10: 20 søgninger/min á 40-45 ms, op til 214 ms)
|
||||
local station = ADSmartPickup.isSourceMarker(marker) and ADSmartPickup.getLoadStationAtWayPointId(adEnv, marker.id, farmId) or nil
|
||||
local isExcluded = station ~= nil and excludedPlaceable ~= nil and station.owningPlaceable == excludedPlaceable
|
||||
if station ~= nil and not isExcluded then
|
||||
table.insert(markerStations, {marker = marker, station = station})
|
||||
@@ -2023,6 +2024,7 @@ function ADSmartPickup:update(dt)
|
||||
if ADRunsController ~= nil then
|
||||
pcall(ADRunsController.load, adEnv)
|
||||
pcall(ADRunsController.installSaveHook)
|
||||
if ADCourseLibrary ~= nil then pcall(ADCourseLibrary.installSaveHook) end
|
||||
end
|
||||
end
|
||||
return
|
||||
|
||||
@@ -374,6 +374,11 @@ function ADWaitPool.install(adEnv)
|
||||
local original = graph.markChanges
|
||||
graph.markChanges = function(self, ...)
|
||||
ADWaitPool.resetRouteCache()
|
||||
-- markør -> station-svarene er nøglet på vejpunkt-id, og AD omnummererer ved sletning (review 27/10)
|
||||
if ADCache ~= nil and ADCache.clear ~= nil then
|
||||
ADCache.clear("loadStation")
|
||||
ADCache.clear("unloadStation")
|
||||
end
|
||||
return original(self, ...)
|
||||
end
|
||||
return true
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<modDesc descVersion="107">
|
||||
<author>masterdraco</author>
|
||||
<version>1.62.416.1</version>
|
||||
<version>1.62.417.1</version>
|
||||
<title>
|
||||
<en>AD Smart Pickup</en>
|
||||
<de>AD Smart Pickup</de>
|
||||
@@ -85,6 +85,7 @@ Menu: ESC-menuen, siden "Smart Pickup". Statusvindue: numpad *. Kræver AutoDriv
|
||||
<sourceFile filename="adFields.lua"/>
|
||||
<sourceFile filename="adCpBridge.lua"/>
|
||||
<sourceFile filename="adCourseCache.lua"/>
|
||||
<sourceFile filename="adCourseLibrary.lua"/>
|
||||
<sourceFile filename="adFieldStorage.lua"/>
|
||||
<sourceFile filename="adManualTransport.lua"/>
|
||||
<sourceFile filename="adPalletRefill.lua"/>
|
||||
|
||||
Reference in New Issue
Block a user