503 lines
28 KiB
Lua
503 lines
28 KiB
Lua
-- AD Field Refill
|
|
-- Markarbejdets påfyldningssteder (udskilt fra adFieldWork.lua 17/10-2026): hvilken station en AD-markør hører til,
|
|
-- nærmeste lager/købestation med varen, og omdirigering når stedet er løbet tørt.
|
|
-- Funktionerne ligger på ADFieldWork, så kaldere og tests er uændrede. Indlæses efter adFieldWork.lua.
|
|
|
|
-- info-log bag "Detaljeret log" (ADLog i adSettings.lua; uden ADLog — tests — direkte til Logging)
|
|
local function logInfo(...) return (ADLog or Logging).info(...) end
|
|
|
|
local function log(...) ADFieldJobs.log(...) end
|
|
local function now() return ADFieldJobs.now end
|
|
local function startAd(...) return ADFieldWork.startAd(...) end
|
|
local function vehiclePosition(...) return ADFieldWork.vehiclePosition(...) end
|
|
|
|
-- Nærmeste påfyldning af varen: en læssestation gården har adgang til, med varen på lager, en AutoDrive-
|
|
-- markør ved triggeren og vej fra traktoren. (AutoDrives eget "automatiske påfyldningsmål" er slået fra i
|
|
-- AutoDrive — AutoDrive.automaticPickupTarget = false — så vi finder stedet selv, som silovalget.)
|
|
-- Sælger købestationen varen? (placeable.spec_buyingStation.buyingStation; FS25-felter varierer, så flere
|
|
-- opslag: udbudte varer, triggernes varer, AI-varer.)
|
|
function ADFieldWork.buyingSupports(station, fillType)
|
|
if station == nil or fillType == nil then return false end
|
|
local ok, result = pcall(function()
|
|
for _, key in ipairs({"providedFillTypes", "supportedFillTypes", "fillTypes"}) do
|
|
local set = station[key]
|
|
if type(set) == "table" and set[fillType] then return true end
|
|
end
|
|
for _, trigger in pairs(station.loadTriggers or {}) do
|
|
if type(trigger.fillTypes) == "table" and trigger.fillTypes[fillType] then return true end
|
|
end
|
|
if station.getAISupportedFillTypes ~= nil then
|
|
local set = station:getAISupportedFillTypes()
|
|
if type(set) == "table" and set[fillType] then return true end
|
|
end
|
|
return false
|
|
end)
|
|
return ok and result == true
|
|
end
|
|
|
|
-- Parkering, kobling og ventepladser er aldrig en læssemarkør.
|
|
local function isServiceMarker(marker)
|
|
local name = string.lower(tostring(marker.name or ""))
|
|
local group = string.lower(tostring(marker.group or ""))
|
|
return string.find(name, "^p ") ~= nil or string.find(name, "^connect ") ~= nil
|
|
or string.find(name, "%f[%a]wait%f[%A]") ~= nil or string.find(group, "%f[%a]wait%f[%A]") ~= nil
|
|
end
|
|
|
|
ADFieldWork.THROUGH_TRIGGER_M = 3 -- AD-vejen før markøren kører gennem triggeren, hvis et punkt er så tæt på
|
|
|
|
-- Kører AD-vejen ind til markøren gennem en af triggerne? marker.approach = {{x, z}} (punkter bagud fra markøren).
|
|
function ADFieldWork.drivesThrough(marker, points)
|
|
local limit = ADFieldWork.THROUGH_TRIGGER_M ^ 2
|
|
for _, p in ipairs(marker.approach or {}) do
|
|
for _, point in ipairs(points) do
|
|
if (p.x - point.x) ^ 2 + (p.z - point.z) ^ 2 <= limit then return true end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- markers = {{id, name, group, x, z, approach?}}, points = triggerpositioner {{x, z}} -> id på læssemarkøren inden
|
|
-- for maxDistance m af en af triggerne, ellers nil. En markør hvis AD-vej kører GENNEM triggeren foretrækkes for den
|
|
-- blot nærmeste (in-game 6/10: sprøjten til den blå væsketank holdt ved "Palleskur 2 load" på nabosporet, 0,5 m
|
|
-- tættere end "Buy All" lige efter tankene).
|
|
function ADFieldWork.nearestStationMarker(markers, points, maxDistance)
|
|
points = points or {}
|
|
local best, bestDistance, bestThrough = nil, maxDistance * maxDistance, false
|
|
for _, marker in ipairs(markers or {}) do
|
|
if not isServiceMarker(marker) then
|
|
local nearest = math.huge
|
|
for _, point in ipairs(points) do
|
|
nearest = math.min(nearest, (marker.x - point.x) ^ 2 + (marker.z - point.z) ^ 2)
|
|
end
|
|
if nearest <= maxDistance * maxDistance then
|
|
local through = ADFieldWork.drivesThrough(marker, points)
|
|
if (through and not bestThrough) or (through == bestThrough and nearest < bestDistance) then
|
|
best, bestDistance, bestThrough = marker.id, nearest, through
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return best, bestThrough
|
|
end
|
|
|
|
ADFieldWork.STATION_MARKER_M = 30
|
|
ADFieldWork.STATION_MATCH_M = 80 -- markører så tæt på bygningen tjekkes med markør->station-opslaget
|
|
|
|
-- Triggerne der giver varen ud: trigger.fillTypes = {[fillType] = true} eller nil (= alle varer). Uden fillType, eller
|
|
-- når ingen trigger nævner varen, alle triggere. In-game 17/10: Kostald 5 har mælke- OG gylletrigger; gyllevognen fik
|
|
-- markøren ved mælketriggeren ("Cow 5 Milk") og kørte tom til marken.
|
|
function ADFieldWork.triggersFor(triggers, fillType)
|
|
local list = {}
|
|
for _, trigger in pairs(triggers or {}) do
|
|
if fillType == nil or trigger.fillTypes == nil or trigger.fillTypes[fillType] then table.insert(list, trigger) end
|
|
end
|
|
if #list == 0 then
|
|
for _, trigger in pairs(triggers or {}) do table.insert(list, trigger) end
|
|
end
|
|
return list
|
|
end
|
|
|
|
-- Stationens læssetriggere i verden (buying-/loading-station) -> {{x, z}}; med fillType kun dem der giver varen ud.
|
|
local function loadTriggerPoints(station, fillType)
|
|
local points = {}
|
|
for _, trigger in ipairs(ADFieldWork.triggersFor(station.loadTriggers, fillType)) do
|
|
local node = trigger.triggerNode or trigger.aiNode
|
|
if node ~= nil then
|
|
local ok, x, _, z = pcall(getWorldTranslation, node)
|
|
if ok and x ~= nil then table.insert(points, {x = x, z = z}) end
|
|
end
|
|
end
|
|
return points
|
|
end
|
|
|
|
ADFieldWork.APPROACH_M = 40 -- så langt bagud ad AD-vejen ledes der efter triggeren
|
|
|
|
-- Punkterne AD kører igennem ind til markøren (bagud ad incoming, op til APPROACH_M).
|
|
function ADFieldWork.approachPoints(adEnv, startId)
|
|
local points, seen, queue = {}, {[startId] = true}, {{id = startId, d = 0}}
|
|
local head = 1
|
|
while head <= #queue and #points < 600 do
|
|
local item = queue[head]
|
|
head = head + 1
|
|
local wayPoint = adEnv.ADGraphManager:getWayPointById(item.id)
|
|
if wayPoint ~= nil then
|
|
table.insert(points, {x = wayPoint.x, z = wayPoint.z})
|
|
for _, prevId in ipairs(wayPoint.incoming or {}) do
|
|
local prev = not seen[prevId] and adEnv.ADGraphManager:getWayPointById(prevId) or nil
|
|
if prev ~= nil then
|
|
seen[prevId] = true
|
|
local step = math.sqrt((prev.x - wayPoint.x) ^ 2 + (prev.z - wayPoint.z) ^ 2)
|
|
-- et punkt pr. meter mellem knuderne (AD-knuder ligger flere meter fra hinanden)
|
|
for t = 1, math.floor(step) do
|
|
local f = t / step
|
|
table.insert(points, {x = wayPoint.x + (prev.x - wayPoint.x) * f, z = wayPoint.z + (prev.z - wayPoint.z) * f})
|
|
end
|
|
if item.d + step <= ADFieldWork.APPROACH_M then table.insert(queue, {id = prevId, d = item.d + step}) end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return points
|
|
end
|
|
|
|
-- points: kun markører inden for nearM af et af punkterne får deres AD-vej med (approach)
|
|
local function markerList(adEnv, points, nearM)
|
|
local list = {}
|
|
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
|
|
local wayPoint = adEnv.ADGraphManager:getWayPointById(marker.id)
|
|
if wayPoint ~= nil then
|
|
local entry = {id = marker.id, name = marker.name, group = marker.group, x = wayPoint.x, z = wayPoint.z}
|
|
for _, point in ipairs(points or {}) do
|
|
if (wayPoint.x - point.x) ^ 2 + (wayPoint.z - point.z) ^ 2 <= (nearM or 0) ^ 2 then
|
|
entry.approach = ADFieldWork.approachPoints(adEnv, marker.id)
|
|
break
|
|
end
|
|
end
|
|
table.insert(list, entry)
|
|
end
|
|
end
|
|
return list
|
|
end
|
|
|
|
-- Stationens markør: den der kører GENNEM en af triggerne (points), ellers den nærmeste inden for maxDistance.
|
|
-- accept(marker) filtrerer (fx væk med andre bygningers markører). -> id, through (eller nil). Til salgsstederne:
|
|
-- markør->station giver hver markør kun én station ("Buy All" gennem væske- og bulkmarkedet blev kun bulkmarkedets,
|
|
-- in-game 7/10), og uden gennemkørsel vandt nabosporets palleskur- eller koblingsmarkør.
|
|
function ADFieldWork.stationMarkerAt(adEnv, points, maxDistance, accept)
|
|
local list = {}
|
|
for _, marker in ipairs(markerList(adEnv, points, maxDistance)) do
|
|
if accept == nil or accept(marker) then table.insert(list, marker) end
|
|
end
|
|
return ADFieldWork.nearestStationMarker(list, points, maxDistance)
|
|
end
|
|
|
|
ADFieldWork.isServiceMarker = isServiceMarker
|
|
|
|
|
|
-- AD-markøren ved stationen: den nærmeste markør ved stationens EGNE læssetriggere, ellers nærmeste ikke-Wait markør
|
|
-- inden for 40 m af bygningen ("Liquid Buy Station" står ved tanken, ikke ved triggeren). AutoDrives
|
|
-- ADTriggerManager:getMarkerAtStation bruges IKKE: den vil have markøren togets længde + 3..13 m forbi triggeren (et
|
|
-- langt sæt fik en anden stations markør: Puma + Kinze 4900 ventede på frø ved "Market Price Liquid Fill Station"), og
|
|
-- finder den ingen, OPRETTER den en ny markør med stationens navn (2 ekstra "Buying Station Multifruit", in-game 30/9).
|
|
-- Alle læsse-/købsstationer med deres påfyldningstriggeres positioner (lager + købestationer).
|
|
local function allLoadStations()
|
|
local list = {}
|
|
local seen = {}
|
|
pcall(function()
|
|
for _, station in pairs(g_currentMission.storageSystem:getLoadingStations()) do
|
|
if not seen[station] then seen[station] = true; table.insert(list, station) end
|
|
end
|
|
end)
|
|
pcall(function()
|
|
for _, placeable in pairs(g_currentMission.placeableSystem.placeables) do
|
|
local spec = placeable.spec_buyingStation
|
|
local station = spec ~= nil and spec.buyingStation or nil
|
|
if station ~= nil and not seen[station] then seen[station] = true; table.insert(list, station) end
|
|
end
|
|
end)
|
|
return list
|
|
end
|
|
|
|
-- Hører markøren til DENNE station? Den station hvis påfyldningstrigger ligger nærmest markøren (over alle stationer).
|
|
-- Retningsløst: markør->station-opslaget (bagud fra næsen) koblede hverken "Solid Buy Silo" eller "Liquid Buy Silo" til
|
|
-- bulkstationen, så den gamle regel valgte Liquid Buy til frø (in-game 16/10, efter 1.62.250).
|
|
-- stationer + triggerpunkter genbruges kort (markerBelongsTo kaldes pr. kandidatmarkør — review: ellers S²·M)
|
|
local stationPointsCache = {at = -math.huge, list = nil}
|
|
local STATION_POINTS_TTL_MS = 2000
|
|
local function stationPoints()
|
|
local t = now()
|
|
if stationPointsCache.list == nil or t - stationPointsCache.at > STATION_POINTS_TTL_MS then
|
|
local list = {}
|
|
for _, candidate in ipairs(allLoadStations()) do table.insert(list, {station = candidate, points = loadTriggerPoints(candidate)}) end
|
|
stationPointsCache.list, stationPointsCache.at = list, t
|
|
end
|
|
return stationPointsCache.list
|
|
end
|
|
|
|
-- Stationen hvis påfyldningstrigger ligger nærmest markøren (inden for STATION_MATCH_M), eller nil.
|
|
local function markerOwner(adEnv, markerId)
|
|
local wayPoint = adEnv.ADGraphManager:getWayPointById(markerId)
|
|
if wayPoint == nil then return nil end
|
|
local best, bestDistance = nil, math.huge
|
|
for _, entry in ipairs(stationPoints()) do
|
|
for _, point in ipairs(entry.points) do
|
|
local d = (point.x - wayPoint.x) ^ 2 + (point.z - wayPoint.z) ^ 2
|
|
if d < bestDistance then best, bestDistance = entry.station, d end
|
|
end
|
|
end
|
|
if best == nil or bestDistance > ADFieldWork.STATION_MATCH_M * ADFieldWork.STATION_MATCH_M then return nil end
|
|
return best
|
|
end
|
|
|
|
local function sameStation(a, station, placeable)
|
|
return a ~= nil and (a == station or (placeable ~= nil and a.owningPlaceable == placeable))
|
|
end
|
|
|
|
local function markerBelongsTo(adEnv, farmId, markerId, station, placeable)
|
|
return sameStation(markerOwner(adEnv, markerId), station, placeable)
|
|
end
|
|
|
|
-- Hører markøren til en ANDEN station? (trin 3/4 må ikke låne nabostationens markør — in-game 16/10: gårdens lager
|
|
-- "Farma 500 + Obi 1000" fik "Liquid Buy Silo", og sprederne holdt dér uden at blive fyldt)
|
|
local function markerOfOther(adEnv, markerId, station, placeable)
|
|
local owner = markerOwner(adEnv, markerId)
|
|
return owner ~= nil and not sameStation(owner, station, placeable)
|
|
end
|
|
|
|
local function markerAtStationRaw(adEnv, vehicle, station, placeable, fillType)
|
|
local farmId = vehicle ~= nil and ADSmartPickup ~= nil and ADSmartPickup.getFarmId ~= nil and ADSmartPickup.getFarmId(vehicle) or nil
|
|
-- 1) markør ved stationens egne triggere, der hører til stationen. Uden filteret vandt en markør, hvis vej kører
|
|
-- GENNEM triggeren videre til nabostationen (in-game 16/10: frø fra "Bulk Fill Station" ved "Liquid Buy Silo")
|
|
local points = nil
|
|
local okOwn, ownId = pcall(function()
|
|
points = loadTriggerPoints(station, fillType)
|
|
local list = {}
|
|
for _, entry in ipairs(markerList(adEnv, points, ADFieldWork.STATION_MARKER_M)) do
|
|
if not isServiceMarker(entry) and markerBelongsTo(adEnv, farmId, entry.id, station, placeable) then table.insert(list, entry) end
|
|
end
|
|
return ADFieldWork.nearestStationMarker(list, points, ADFieldWork.STATION_MARKER_M)
|
|
end)
|
|
if okOwn and ownId ~= nil then return ownId, 1 end
|
|
local node = placeable ~= nil and placeable.rootNode or nil
|
|
local px, pz
|
|
if node ~= nil then
|
|
local x, _, z = getWorldTranslation(node); px, pz = x, z
|
|
end
|
|
-- 2) samme filter i større radius (STATION_MATCH_M) om triggerne, rangeret som trin 1 (gennemkørsel, afstand til
|
|
-- triggeren); uden kendte triggere: afstand til bygningen
|
|
local okMatch, matchId = pcall(function()
|
|
local around = (points ~= nil and #points > 0) and points or (px ~= nil and {{x = px, z = pz}} or nil)
|
|
if around == nil then return nil end
|
|
local list = {}
|
|
for _, entry in ipairs(markerList(adEnv, around, ADFieldWork.STATION_MATCH_M)) do
|
|
local near = false
|
|
for _, point in ipairs(around) do
|
|
if (entry.x - point.x) ^ 2 + (entry.z - point.z) ^ 2 <= ADFieldWork.STATION_MATCH_M ^ 2 then near = true break end
|
|
end
|
|
if near and not isServiceMarker(entry) and markerBelongsTo(adEnv, farmId, entry.id, station, placeable) then table.insert(list, entry) end
|
|
end
|
|
return ADFieldWork.nearestStationMarker(list, around, ADFieldWork.STATION_MATCH_M)
|
|
end)
|
|
if okMatch and matchId ~= nil then return matchId, 2 end
|
|
-- 3) som før: markør ved triggerne (også gennemkørsel), ellers nærmeste ikke-Wait markør inden for 40 m af bygningen
|
|
local okAny, anyId = pcall(function()
|
|
local pts = points or loadTriggerPoints(station, fillType)
|
|
local list = {}
|
|
for _, entry in ipairs(markerList(adEnv, pts, ADFieldWork.STATION_MARKER_M)) do
|
|
if not markerOfOther(adEnv, entry.id, station, placeable) then table.insert(list, entry) end
|
|
end
|
|
return ADFieldWork.nearestStationMarker(list, pts, ADFieldWork.STATION_MARKER_M)
|
|
end)
|
|
if okAny and anyId ~= nil then return anyId, 3 end
|
|
if px == nil then return nil end
|
|
local best, bestDistance = nil, 40 * 40
|
|
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
|
|
local isWait = ADSupplyPlanner ~= nil and ADSupplyPlanner.isWaitMarker ~= nil and ADSupplyPlanner.isWaitMarker(marker)
|
|
local wayPoint = not isWait and adEnv.ADGraphManager:getWayPointById(marker.id) or nil
|
|
if wayPoint ~= nil then
|
|
local d = (wayPoint.x - px) ^ 2 + (wayPoint.z - pz) ^ 2
|
|
if d < bestDistance and not markerOfOther(adEnv, marker.id, station, placeable) then best, bestDistance = marker.id, d end
|
|
end
|
|
end
|
|
return best, 4
|
|
end
|
|
|
|
-- Til loggen: valgt markør pr. station (kun når valget ændrer sig) — trin 1 = ved triggeren og hører til stationen,
|
|
-- 2 = hører til stationen (80 m), 3 = gammel regel ved triggeren, 4 = nærmest bygningen.
|
|
local loggedStationMarker = {}
|
|
local function markerAtStation(adEnv, vehicle, station, placeable, fillType)
|
|
local id, step = markerAtStationRaw(adEnv, vehicle, station, placeable, fillType)
|
|
pcall(function()
|
|
local key = placeable or station
|
|
local logged = key ~= nil and (loggedStationMarker[key] or {}) or nil
|
|
if logged ~= nil and logged[fillType or 0] ~= tostring(id) then
|
|
logged[fillType or 0] = tostring(id)
|
|
loggedStationMarker[key] = logged
|
|
local marker = id ~= nil and adEnv.ADGraphManager:getMapMarkerByWayPointId(id) or nil
|
|
local name = placeable ~= nil and placeable.getName ~= nil and placeable:getName() or "?"
|
|
local goods = fillType ~= nil and g_fillTypeManager ~= nil and g_fillTypeManager.getFillTypeNameByIndex ~= nil
|
|
and g_fillTypeManager:getFillTypeNameByIndex(fillType) or nil
|
|
logInfo("ADSmartPickup: påfyldning: station '%s' -> markør '%s' (trin %s%s)", tostring(name),
|
|
tostring(marker ~= nil and marker.name or id), tostring(step), goods ~= nil and (", " .. tostring(goods)) or "")
|
|
end
|
|
end)
|
|
return id
|
|
end
|
|
|
|
-- Nærmeste påfyldning af varen med AD-markør og vej: lagre/siloer med varen, og købestationer (købes
|
|
-- efter behov — ingen lagerbeholdning, så de fandtes ikke før; in-game 26/9 "Liquid Buy Station").
|
|
-- skipRoute: rutetjekket springes over (frakoblet puljeredskab har ingen .ad, så AD's getPathTo fejler; traktoren
|
|
-- tjekkes igen i refillPlan efter påkobling).
|
|
-- exclude (valgfri): {[wayPointId] = true} der springes over (fx et lager der er løbet tørt, mens vognen ventede)
|
|
ADFieldWork.MIN_REFILL_STOCK_L = 500 -- eget lager skal have mindst så meget af varen for at være påfyldningssted
|
|
|
|
-- Bedste eget lager: nærmeste med et HELT læs (level >= capacity); ellers nærmeste med noget (brugerønske 17/10:
|
|
-- gyllevognen skal ikke køre til en tank med 600 l og videre derfra). entries = {{id, distance, level}} -> id
|
|
function ADFieldWork.bestRefill(entries, capacity)
|
|
local bestFull, bestFullDistance, bestAny, bestAnyDistance = nil, math.huge, nil, math.huge
|
|
for _, entry in ipairs(entries or {}) do
|
|
if entry.distance < bestAnyDistance then bestAny, bestAnyDistance = entry.id, entry.distance end
|
|
if (capacity or 0) > 0 and (entry.level or 0) >= capacity and entry.distance < bestFullDistance then
|
|
bestFull, bestFullDistance = entry.id, entry.distance
|
|
end
|
|
end
|
|
return bestFull or bestAny
|
|
end
|
|
|
|
-- Sættets plads til varen (alle fyldenheder på traktor + redskaber, der tager varen)
|
|
local function loadCapacity(vehicle, fillType)
|
|
local capacity = 0
|
|
pcall(function()
|
|
local units = vehicle.getChildVehicles ~= nil and vehicle:getChildVehicles() or {vehicle}
|
|
for _, unit in ipairs(units) do
|
|
for index in ipairs(unit.getFillUnits ~= nil and unit:getFillUnits() or {}) do
|
|
if unit.getFillUnitSupportsFillType ~= nil and unit:getFillUnitSupportsFillType(index, fillType) then
|
|
capacity = capacity + (unit:getFillUnitCapacity(index) or 0)
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
return capacity
|
|
end
|
|
|
|
function ADFieldWork.findRefillMarker(adEnv, vehicle, fillType, x, z, skipRoute, exclude)
|
|
local farmId = vehicle:getOwnerFarmId()
|
|
local best, bestDistance = nil, math.huge
|
|
-- -> afstand² når markøren kan bruges (findes, ikke udelukket, vej til den), ellers nil
|
|
local function usable(wayPointId)
|
|
if exclude ~= nil and wayPointId ~= nil and exclude[wayPointId] then return nil end
|
|
local wayPoint = wayPointId ~= nil and wayPointId > 0 and adEnv.ADGraphManager:getWayPointById(wayPointId) or nil
|
|
if wayPoint == nil then return nil end
|
|
if not (skipRoute or ADUnloadWait.hasRouteTo(adEnv, vehicle, wayPointId)) then return nil end
|
|
return (wayPoint.x - x) ^ 2 + (wayPoint.z - z) ^ 2
|
|
end
|
|
local function consider(wayPointId)
|
|
local distance = usable(wayPointId)
|
|
if distance ~= nil and distance < bestDistance then
|
|
best, bestDistance = wayPointId, distance
|
|
end
|
|
end
|
|
-- gårdens egne lagre først (stalde/tanke med gylle, lager med gødning): købes kun når intet eget findes
|
|
-- (in-game 26/9: 7 mio. l gylle ved BGA'en, men påfyldningen ville købe ved købestationen).
|
|
-- Nærmeste med et helt læs først, ellers nærmeste med noget.
|
|
local own = {}
|
|
pcall(function()
|
|
for _, station in pairs(g_currentMission.storageSystem:getLoadingStations()) do
|
|
local supported = station.getAISupportedFillTypes ~= nil and station:getAISupportedFillTypes() or {}
|
|
local isShop = station.owningPlaceable ~= nil and station.owningPlaceable.spec_buyingStation ~= nil
|
|
local level = (not isShop and supported[fillType] and g_currentMission.accessHandler:canFarmAccess(farmId, station))
|
|
and (station:getFillLevel(fillType, farmId) or 0) or 0
|
|
if level >= ADFieldWork.MIN_REFILL_STOCK_L then
|
|
local id = markerAtStation(adEnv, vehicle, station, station.owningPlaceable, fillType)
|
|
local distance = usable(id)
|
|
if distance ~= nil then table.insert(own, {id = id, distance = distance, level = level}) end
|
|
end
|
|
end
|
|
end)
|
|
best = ADFieldWork.bestRefill(own, loadCapacity(vehicle, fillType))
|
|
if best ~= nil then return best end
|
|
pcall(function()
|
|
for _, placeable in pairs(g_currentMission.placeableSystem.placeables) do
|
|
local spec = placeable.spec_buyingStation
|
|
local station = spec ~= nil and spec.buyingStation or nil
|
|
if station ~= nil and ADFieldWork.buyingSupports(station, fillType) then
|
|
consider(markerAtStation(adEnv, vehicle, station, placeable, fillType))
|
|
end
|
|
end
|
|
end)
|
|
return best
|
|
end
|
|
|
|
-- Påfyldningsstedet er løbet tørt, mens vognen venter der (in-game 16/10: kalk-sprederen holdt ved "TMR Silo Farma",
|
|
-- der var tom): find næste sted uden det nuværende, og kør derhen. -> true når turen er flyttet.
|
|
function ADFieldWork.retargetEmptyRefill(adEnv, vehicle)
|
|
local module = vehicle.ad ~= nil and vehicle.ad.stateModule or nil
|
|
if module == nil or not module:isActive() or module:getMode() ~= adEnv.AutoDrive.MODE_LOAD then return false end
|
|
local first, second = module:getFirstMarker(), module:getSecondMarker()
|
|
local fillType = module.getFillType ~= nil and module:getFillType() or nil
|
|
if first == nil or second == nil or fillType == nil then return false end
|
|
-- kun når stedet BEVISELIGT ikke kan levere (review): eget lager under minimum, eller købestation der ikke sælger
|
|
-- varen. Kø ved stationen, langsom pumpe eller fuld tank flytter den ikke.
|
|
local station = markerOwner(adEnv, second.id)
|
|
if station ~= nil then
|
|
local shop = station.owningPlaceable ~= nil and station.owningPlaceable.spec_buyingStation ~= nil
|
|
if shop then
|
|
if ADFieldWork.buyingSupports(station, fillType) then return false end
|
|
else
|
|
local farmId = vehicle:getOwnerFarmId()
|
|
local okLevel, stock = pcall(station.getFillLevel, station, fillType, farmId)
|
|
if okLevel and (stock or 0) >= ADFieldWork.MIN_REFILL_STOCK_L then return false end
|
|
end
|
|
end
|
|
local x, z = vehiclePosition(vehicle)
|
|
local right = ADFieldWork.findRefillMarker(adEnv, vehicle, fillType, x, z, true, {[second.id] = true})
|
|
if right == nil then return false end
|
|
startAd(adEnv, vehicle, adEnv.AutoDrive.MODE_LOAD, first.id, right, fillType)
|
|
local marker = adEnv.ADGraphManager:getMapMarkerByWayPointId(right)
|
|
local name = g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
|
|
logInfo("ADSmartPickup: påfyldning: '%s' fik intet %s ved %s — kører til %s", tostring(ADRuns.vehicleLabel(vehicle)),
|
|
tostring(name), tostring(second.name), tostring(marker ~= nil and marker.name or right))
|
|
return true
|
|
end
|
|
|
|
-- Genoptaget AD-tur i påfyldnings-tilstand (LOAD): påfyldningsstedet blev valgt før gem — måske af en ældre version
|
|
-- (in-game 16/10: kalk og gødning ved "Liquid Buy Silo", som ikke sælger dem; sprederne holdt der). Find det rigtige
|
|
-- sted for varen nu, og genstart turen dertil, hvis det er et andet.
|
|
function ADFieldWork.fixResumedRefill(adEnv, vehicle, fieldId)
|
|
local module = vehicle.ad ~= nil and vehicle.ad.stateModule or nil
|
|
if module == nil or not module:isActive() or module:getMode() ~= adEnv.AutoDrive.MODE_LOAD then return false end
|
|
local first, second = module:getFirstMarker(), module:getSecondMarker()
|
|
local fillType = module.getFillType ~= nil and module:getFillType() or nil
|
|
if first == nil or second == nil or fillType == nil then return false end
|
|
-- allerede fyldt: turen ud til marken er i gang — rør den ikke
|
|
if ADCpBridge ~= nil and ADCpBridge.getRefillLevel ~= nil and (ADCpBridge.getRefillLevel(vehicle, fillType) or 0) > 0 then return false end
|
|
-- det gemte sted har varen (sælger den / lager med varen): spillerens eller et gyldigt valg — behold det (review)
|
|
local farmId = ADSmartPickup.getFarmId(vehicle)
|
|
local saved = ADSmartPickup.getLoadStationAtWayPointId(adEnv, second.id, farmId)
|
|
if saved ~= nil then
|
|
local shop = saved.owningPlaceable ~= nil and saved.owningPlaceable.spec_buyingStation ~= nil
|
|
and saved.owningPlaceable.spec_buyingStation.buyingStation or nil
|
|
if shop ~= nil and ADFieldWork.buyingSupports(shop, fillType) then return false end
|
|
if shop == nil and saved.getFillLevel ~= nil and (saved:getFillLevel(fillType, farmId) or 0) > 0 then return false end
|
|
end
|
|
local x, z = vehiclePosition(vehicle)
|
|
-- uden rutetjek: det kører ved indlæsning (AD finder selv vejen; undgå hak)
|
|
local right = ADFieldWork.findRefillMarker(adEnv, vehicle, fillType, x, z, true)
|
|
if right == nil or right == second.id then return false end
|
|
startAd(adEnv, vehicle, adEnv.AutoDrive.MODE_LOAD, first.id, right, fillType)
|
|
local marker = adEnv.ADGraphManager:getMapMarkerByWayPointId(right)
|
|
local name = g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
|
|
log(fieldId, "'%s': påfyldning af %s flyttet fra %s til %s (genoptaget tur)", ADRuns.vehicleLabel(vehicle), tostring(name),
|
|
tostring(second.name), tostring(marker ~= nil and marker.name or right))
|
|
return true
|
|
end
|
|
|
|
-- Kun købestationer der sælger varen (dyrefoder som sidste udvej, 8/10). -> waypoint-id | nil
|
|
-- -> waypoint-id | nil, diagnose {stations, withMarker, withRoute} (hvorfor intet købes — til loggen)
|
|
function ADFieldWork.findBuyMarker(adEnv, vehicle, fillType, x, z, skipRoute)
|
|
local best, bestDistance = nil, math.huge
|
|
local info = {stations = 0, withMarker = 0, withRoute = 0}
|
|
local ok, err = pcall(function()
|
|
for _, placeable in pairs(g_currentMission.placeableSystem.placeables) do
|
|
local spec = placeable.spec_buyingStation
|
|
local station = spec ~= nil and spec.buyingStation or nil
|
|
if station ~= nil and ADFieldWork.buyingSupports(station, fillType) then
|
|
info.stations = info.stations + 1
|
|
local wayPointId = markerAtStation(adEnv, vehicle, station, placeable, fillType)
|
|
local wayPoint = wayPointId ~= nil and wayPointId > 0 and adEnv.ADGraphManager:getWayPointById(wayPointId) or nil
|
|
if wayPoint ~= nil then
|
|
info.withMarker = info.withMarker + 1
|
|
local distance = (wayPoint.x - x) ^ 2 + (wayPoint.z - z) ^ 2
|
|
local routed = skipRoute or ADUnloadWait.hasRouteTo(adEnv, vehicle, wayPointId)
|
|
if routed then info.withRoute = info.withRoute + 1 end
|
|
if distance < bestDistance and routed then
|
|
best, bestDistance = wayPointId, distance
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
if not ok then info.error = tostring(err) end
|
|
return best, info
|
|
end
|