fix: lange vogntog ruller frem til risten; kobling via båsens bak-stik (v1.62.33)

This commit is contained in:
2026-10-06 09:40:35 +02:00
parent be2630457f
commit e641c8e4d6
5 changed files with 343 additions and 1 deletions
+140
View File
@@ -0,0 +1,140 @@
-- AD Hitch Approach
-- Bak-ind-båse (solhallerne i savegame6): banen fra Connect-markøren ud til fordelersporet har tovejs
-- tværspor, så AD kan køre NÆSEN FØRST frem til Connect — krogen vender væk fra vognen, og koblingen
-- fejler med 'noAttachable' (in-game 6/10: Tippevogn 5 og 6). Har båsen et bak-stik (en kant S => J der
-- kun køres baglæns: J står i S.out, men S mangler i J.incoming), køres traktoren først til S og derfra
-- baglæns op ad banen til Connect — samme vej som AD selv parkerer i båsen.
ADHitchApproach = {}
ADHitchApproach.LOG_PREFIX = "ADSmartPickup: kobling: "
ADHitchApproach.MAX_DISTANCE = 40 -- m ad nettet fra Connect til bak-stikkets startknude
ADHitchApproach.ALREADY_THERE = 3 -- m: står traktoren allerede ved S, springes trinnet over
-- ---- ren logik ----
local function contains(list, value)
for _, item in ipairs(list or {}) do
if item == value then return true end
end
return false
end
-- graph = {out(id) -> {id}, incoming(id) -> {id}, pos(id) -> x, z}; avoidId = båsens P-knude (søg ikke den vej).
-- -> startknude S for den nærmeste bak-kant, eller nil
function ADHitchApproach.findBackEntry(graph, connectId, avoidId, maxDistance)
maxDistance = maxDistance or ADHitchApproach.MAX_DISTANCE
local function step(a, b)
local ax, az = graph.pos(a)
local bx, bz = graph.pos(b)
if ax == nil or bx == nil then return math.huge end
return math.sqrt((ax - bx) ^ 2 + (az - bz) ^ 2)
end
local dist, done = {[connectId] = 0}, {}
local best, bestDistance = nil, math.huge
while true do
local current, currentDistance = nil, math.huge
for id, d in pairs(dist) do
if not done[id] and d < currentDistance then current, currentDistance = id, d end
end
if current == nil then break end
done[current] = true
if current ~= connectId then
for _, target in ipairs(graph.out(current) or {}) do
if not contains(graph.incoming(target), current) and currentDistance < bestDistance then
best, bestDistance = current, currentDistance
end
end
end
local neighbours = {}
for _, id in ipairs(graph.out(current) or {}) do table.insert(neighbours, id) end
for _, id in ipairs(graph.incoming(current) or {}) do table.insert(neighbours, id) end
for _, id in ipairs(neighbours) do
if id ~= avoidId and not done[id] then
local d = currentDistance + step(current, id)
if d <= maxDistance and (dist[id] == nil or d < dist[id]) then dist[id] = d end
end
end
end
return best
end
-- "Connect P Tippevogn 5" -> "P Tippevogn 5"
function ADHitchApproach.parkNameOf(connectName)
if type(connectName) ~= "string" then return nil end
return connectName:match("^[Cc]onnect (.+)$")
end
-- ---- spil ----
local function graphOf(adEnv)
local manager = adEnv.ADGraphManager
local function wayPoint(id) return manager:getWayPointById(id) end
return {
out = function(id) local w = wayPoint(id) return w ~= nil and w.out or {} end,
incoming = function(id) local w = wayPoint(id) return w ~= nil and w.incoming or {} end,
pos = function(id) local w = wayPoint(id) if w == nil then return nil end return w.x, w.z end,
}
end
local function parkWayPointId(adEnv, connectName)
local parkName = ADHitchApproach.parkNameOf(connectName)
if parkName == nil then return nil end
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
if marker.name == parkName then return marker.id end
end
return nil
end
local function vehicleName(vehicle)
return vehicle ~= nil and vehicle.getName ~= nil and vehicle:getName() or "?"
end
-- Bak-stikkets startknude for traktorens kobl-på-tur, eller nil (ingen tur, intet bak-stik, står der allerede).
local function backEntryFor(adEnv, vehicle, destinationId)
local job = ADHitch ~= nil and ADHitch.pendingAttach ~= nil and ADHitch.pendingAttach(vehicle) or nil
if job == nil or job.markerId ~= destinationId then return nil end
local marker = adEnv.ADGraphManager:getMapMarkerByWayPointId(destinationId)
local entry = ADHitchApproach.findBackEntry(graphOf(adEnv), destinationId,
parkWayPointId(adEnv, marker ~= nil and marker.name or nil))
if entry == nil then return nil end
local wayPoint = adEnv.ADGraphManager:getWayPointById(entry)
if wayPoint ~= nil and vehicle.rootNode ~= nil then
local x, _, z = getWorldTranslation(vehicle.rootNode)
if math.sqrt((x - wayPoint.x) ^ 2 + (z - wayPoint.z) ^ 2) <= ADHitchApproach.ALREADY_THERE then return nil end
end
return entry, marker ~= nil and marker.name or tostring(destinationId)
end
local isInstalled = false
-- DriveToMode:start lægger DriveToDestinationTask(Connect) i køen; et trin til bak-stikket lægges foran.
-- Trinnet melder sig færdigt uden at give besked til mode'en (ellers stopper DriveToMode AD dér).
function ADHitchApproach.install(adEnv)
local mode, taskClass = adEnv ~= nil and adEnv.DriveToMode or nil, adEnv ~= nil and adEnv.DriveToDestinationTask or nil
if isInstalled or mode == nil or mode.start == nil or taskClass == nil or taskClass.new == nil then return end
isInstalled = true
local original = mode.start
mode.start = function(self, ...)
local result = original(self, ...)
local ok, err = pcall(function()
if self.driveToDestinationTask == nil or self.destinationID == nil then return end
local entry, name = backEntryFor(adEnv, self.vehicle, self.destinationID)
if entry == nil then return end
local stage = setmetatable({}, {__index = taskClass:new(self.vehicle, entry)})
local taskModule = adEnv.ADTaskModule
stage.finished = function(task)
task.vehicle.ad.taskModule:setCurrentTaskFinished(taskModule ~= nil and taskModule.DONT_PROPAGATE or 1)
end
local queue = self.vehicle.ad.taskModule.tasks
local items = {}
for _, item in ipairs(queue:GetItems() or {}) do table.insert(items, item) end
queue:Clear()
queue:Enqueue(stage)
for _, item in ipairs(items) do queue:Enqueue(item) end
Logging.info(ADHitchApproach.LOG_PREFIX .. "'%s' kører via båsens bak-stik (vejpunkt %s) og bakker ind til '%s'",
vehicleName(self.vehicle), tostring(entry), tostring(name))
end)
if not ok then Logging.warning(ADHitchApproach.LOG_PREFIX .. "bak-stik-tur: %s", tostring(err)) end
return result
end
end