feat: markørvagt genstarter flowkøretøjer når AutoDrive nulstiller dem til markør 1 (v1.62.63)
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
-- AD Marker Guard
|
||||
-- Slettes en AutoDrive-markør (også når spilleren flytter/genoptager en markør), sætter AutoDrive alle køretøjer der
|
||||
-- havde den som 1., 2. eller aktuelt mål til markør nr. 1 (ADGraphManager:checkResetVehicleDestinations). In-game 7/10:
|
||||
-- tre køretøjer bakkede ind i "P Sprøjte 1" efter at spilleren lavede vejnettet om ved Liquid/Solid Buy.
|
||||
-- Vagten pakker den funktion ind: FØR AutoDrive nulstiller, noteres køretøjer i kørende flows med den slettede markør,
|
||||
-- og efter RESTART_DELAY_MS genstartes de i deres flow (moden skriver flowets rigtige markører igen). Køretøjer uden
|
||||
-- for flows (markarbejde, kobling) logges kun.
|
||||
-- Ren logik med et env-objekt (testes med luajit); ADMarkerGuard.gameEnv bygger det i spillet.
|
||||
|
||||
ADMarkerGuard = {}
|
||||
ADMarkerGuard.LOG_PREFIX = "ADSmartPickup: markørvagt: "
|
||||
ADMarkerGuard.RESTART_DELAY_MS = 3000 -- spilleren er måske midt i at flytte markøren
|
||||
|
||||
local queue = setmetatable({}, {__mode = "k"}) -- køretøj -> tid (ms) den blev berørt
|
||||
local strayCount = 0
|
||||
|
||||
-- ids = {first, second, current} (AutoDrive-markørindeks) -> peger køretøjet på den slettede markør?
|
||||
function ADMarkerGuard.isAffected(ids, destination)
|
||||
if destination == nil then return false end
|
||||
return ids.first == destination or ids.second == destination or ids.current == destination
|
||||
end
|
||||
|
||||
function ADMarkerGuard.isDue(queuedAt, nowMs)
|
||||
return nowMs - queuedAt >= ADMarkerGuard.RESTART_DELAY_MS
|
||||
end
|
||||
|
||||
function ADMarkerGuard.queued() return queue end
|
||||
function ADMarkerGuard.lastStrayCount() return strayCount end
|
||||
|
||||
local function markerIds(vehicle)
|
||||
local state = vehicle.ad ~= nil and vehicle.ad.stateModule or nil
|
||||
if state == nil then return {} end
|
||||
local function read(name)
|
||||
if state[name] == nil then return nil end
|
||||
local ok, value = pcall(state[name], state)
|
||||
return ok and value or nil
|
||||
end
|
||||
return {first = read("getFirstMarkerId"), second = read("getSecondMarkerId"), current = read("getCurrentDestinationId")}
|
||||
end
|
||||
|
||||
local function noteAffected(env, destination)
|
||||
local strays = {}
|
||||
for _, vehicle in ipairs(env.vehicles()) do
|
||||
if ADMarkerGuard.isAffected(markerIds(vehicle), destination) then
|
||||
local run = env.runOf(vehicle)
|
||||
if run == nil then
|
||||
-- kun køretøjer AutoDrive kører lige nu; parkerede maskiner med gamle markører er ligegyldige
|
||||
if env.isActive == nil or env.isActive(vehicle) then table.insert(strays, vehicle) end
|
||||
elseif not run.stopped then
|
||||
queue[vehicle] = env.now()
|
||||
end
|
||||
-- stoppet flow: flowet skriver selv sine markører ved næste Start
|
||||
end
|
||||
end
|
||||
strayCount = #strays
|
||||
for _, vehicle in ipairs(strays) do
|
||||
if env.warnStray ~= nil then env.warnStray(vehicle) end
|
||||
end
|
||||
end
|
||||
|
||||
function ADMarkerGuard.install(env)
|
||||
local graph = env.ADGraphManager
|
||||
if graph == nil or graph.checkResetVehicleDestinations == nil or graph.spuMarkerGuard then return false end
|
||||
graph.spuMarkerGuard = true
|
||||
local original = graph.checkResetVehicleDestinations
|
||||
graph.checkResetVehicleDestinations = function(self, destination, ...)
|
||||
local ok, err = pcall(noteAffected, env, destination)
|
||||
if not ok and Logging ~= nil then Logging.warning(ADMarkerGuard.LOG_PREFIX .. "%s", tostring(err)) end
|
||||
return original(self, destination, ...)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function ADMarkerGuard.tick(env)
|
||||
local now = env.now()
|
||||
for vehicle, queuedAt in pairs(queue) do
|
||||
if ADMarkerGuard.isDue(queuedAt, now) then
|
||||
queue[vehicle] = nil
|
||||
local run = env.runOf(vehicle)
|
||||
if run ~= nil and not run.stopped and env.restart ~= nil then
|
||||
local ok, err = pcall(env.restart, vehicle, run)
|
||||
if not ok and Logging ~= nil then Logging.warning(ADMarkerGuard.LOG_PREFIX .. "genstart fejlede: %s", tostring(err)) end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ---- spil-adapter ----
|
||||
|
||||
local gameEnvCache = nil
|
||||
function ADMarkerGuard.gameEnv(adEnv)
|
||||
if gameEnvCache ~= nil and gameEnvCache.adEnv == adEnv then return gameEnvCache end
|
||||
local label = function(vehicle) return tostring(ADRuns ~= nil and ADRuns.vehicleLabel(vehicle) or "?") end
|
||||
gameEnvCache = {
|
||||
adEnv = adEnv,
|
||||
ADGraphManager = adEnv.ADGraphManager,
|
||||
vehicles = function() return ADRunsController.getFarmVehicles(adEnv) end,
|
||||
runOf = function(vehicle) return ADRunsController.getRunForVehicle(vehicle) end,
|
||||
isActive = function(vehicle)
|
||||
local state = vehicle.ad ~= nil and vehicle.ad.stateModule or nil
|
||||
return state ~= nil and state.isActive ~= nil and state:isActive() == true
|
||||
end,
|
||||
now = function()
|
||||
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then return g_currentMission.time end
|
||||
return os.clock() * 1000
|
||||
end,
|
||||
restart = function(vehicle, run)
|
||||
Logging.info(ADMarkerGuard.LOG_PREFIX .. "AutoDrive-markør slettet — '%s' genstartes i flowet '%s' (ellers kører den til markør nr. 1)",
|
||||
label(vehicle), tostring(run.name))
|
||||
ADRunsController.startVehicle(adEnv, run, vehicle)
|
||||
end,
|
||||
warnStray = function(vehicle)
|
||||
Logging.warning(ADMarkerGuard.LOG_PREFIX .. "AutoDrive-markør slettet — '%s' (ikke i et kørende flow) har fået markør nr. 1 som mål; tjek den",
|
||||
label(vehicle))
|
||||
end,
|
||||
}
|
||||
return gameEnvCache
|
||||
end
|
||||
@@ -1173,6 +1173,10 @@ local function installHook(adEnv)
|
||||
if ADOutbound ~= nil and ADOutbound.install ~= nil then
|
||||
ADOutbound.install(adEnv)
|
||||
end
|
||||
if ADMarkerGuard ~= nil then
|
||||
local okGuard, errGuard = pcall(ADMarkerGuard.install, ADMarkerGuard.gameEnv(adEnv))
|
||||
if not okGuard then Logging.warning(ADSmartPickup.LOG_PREFIX .. "markørvagt kunne ikke installeres: %s", tostring(errGuard)) end
|
||||
end
|
||||
if ADHitchCreep ~= nil and ADHitchCreep.install ~= nil then
|
||||
ADHitchCreep.install(adEnv)
|
||||
end
|
||||
@@ -1350,6 +1354,10 @@ function ADSmartPickup:update(dt)
|
||||
local okFlow, errFlow = pcall(ADFlowWagons.tick, getAutoDriveEnv(), dt)
|
||||
if not okFlow then Logging.warning(ADSmartPickup.LOG_PREFIX .. "flow-vogne fejlede: %s", tostring(errFlow)) end
|
||||
end
|
||||
if ADMarkerGuard ~= nil and getAutoDriveEnv() ~= nil then
|
||||
local okGuard, errGuard = pcall(ADMarkerGuard.tick, ADMarkerGuard.gameEnv(getAutoDriveEnv()))
|
||||
if not okGuard then Logging.warning(ADSmartPickup.LOG_PREFIX .. "markørvagt fejlede: %s", tostring(errGuard)) end
|
||||
end
|
||||
if ADFlowSleep ~= nil then
|
||||
local okSleep, errSleep = pcall(ADFlowSleep.tick, getAutoDriveEnv(), dt)
|
||||
if not okSleep then Logging.warning(ADSmartPickup.LOG_PREFIX .. "dvale fejlede: %s", tostring(errSleep)) end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<modDesc descVersion="107">
|
||||
<author>masterdraco</author>
|
||||
<version>1.62.62.0</version>
|
||||
<version>1.62.63.0</version>
|
||||
<title>
|
||||
<en>AD Smart Pickup</en>
|
||||
</title>
|
||||
@@ -57,6 +57,7 @@
|
||||
<sourceFile filename="adPool.lua"/>
|
||||
<sourceFile filename="adFlowWagons.lua"/>
|
||||
<sourceFile filename="adFlowSleep.lua"/>
|
||||
<sourceFile filename="adMarkerGuard.lua"/>
|
||||
<sourceFile filename="adFleetNames.lua"/>
|
||||
<sourceFile filename="adFieldJobs.lua"/>
|
||||
<sourceFile filename="adMonthView.lua"/>
|
||||
|
||||
Reference in New Issue
Block a user