Files
fs25-adsmartpickup/FS25_ADSmartPickup/adHitchApproach.lua
T
masterdraco e4a3c6fea0 refactor: adFieldWork delt i adFieldStore/adFieldRefill, follow/dispatch/beginWork i faser, fælles adUtil — 1.62.285
Ingen ændret opførsel. adFieldWork.lua 3087 -> 2598 linjer; lager/salg og
ballemål i adFieldStore.lua, påfyldningssteder i adFieldRefill.lua (begge
på ADFieldWork-tabellen, indlæses efter adFieldWork). follow (296), dispatch
(232) og beginWork (164 linjer) delt i én funktion pr. fase/trin. Ny
adUtil.lua erstatter 28 enslydende lokale hjælpere i 22 filer. Alle 93
testfiler grønne; code-review uden fund over LOW.
2026-10-17 03:21:58 +02:00

139 lines
6.8 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 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
-- SLÅET FRA (in-game 6/10 15:03): bak-kanten i båsene er kun ~1,5 m; bagefter kører AutoDrive fremad igen ad banen,
-- så traktoren nåede Connect NÆSEN FØRST (trækøje 7–9 m foran krogen) ved Læssevogn 1+2, Kalkspreder 1, Gyllevogn 1.
-- Uden trinnet ankom den oftest baglæns og bakkede selv det sidste (Tippevogn 5/7, Plov 2, Vender 1).
ADHitchApproach.ENABLED = false
-- ---- ren logik ----
local contains = ADUtil.contains
-- 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 vehicleName = ADUtil.vehicleName
-- 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, ...)
if not ADHitchApproach.ENABLED then return result end
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