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.
This commit is contained in:
@@ -8,6 +8,7 @@ står i [docs/BRUGERMANUAL.md](docs/BRUGERMANUAL.md) (dansk) og [docs/USER_MANUA
|
||||
|
||||
## 1.62 — Kæder slås selv til
|
||||
|
||||
- **1.62.285** Kodeoprydning, ingen ændret opførsel (brugerønske 17/10: "samlet kodeoptimering"). **adFieldWork.lua delt i tre**: lager/salg -> `adFieldStore.lua`, påfyldningssteder -> `adFieldRefill.lua` (3.087 -> 2.598 + 203 + 440 linjer). De tre længste funktioner (`follow` 296, `dispatch` 232, `beginWork` 164 linjer) er delt op i én funktion pr. fase/trin; ingen funktion i filen er nu over 120 linjer. Ny `adUtil.lua` med de små hjælpere (`now`, `contains`, `copyList`, `vehicleName`, `fillTypeName`, `positionOf`, `savePath`), der før lå som 28 enslydende kopier i 22 filer. Alle 93 testfiler grønne.
|
||||
- **1.62.284** Rettet: **græs/kløver/lucerne regnet med ét slæt om året** (in-game 17/10: hø-behov 207 ha -> 20 af 28 marker til græs). Afgrøder der gror igen, regnes nu med 3 slæt om året — både i Alt automatisk og i kædens markskift (ellers ville kæden tage salgsmarkerne tilbage). Varige afgrøder (vin, oliven, poppel) høstes stadig én gang.
|
||||
- **1.62.283** "Alt automatisk" er nu en **hel markplan** (brugerønsker 17/10): gårdens forbrug beregnes forfra (12 mdr. + indstillingen **Overskud til gårdens forbrug**, standard 10 %) og dækkes med de marker der **passer bedst i størrelse** (marker der allerede har afgrøden først); resten fordeles jævnt i ha på de 3 mest indbringende salgsafgrøder. **Græsmarker er med** (overskydende græs sås om efter sidste slæt; eng er fast). Nyt link **[Alt automatisk + nulstil mine valg]**. Planen **laves om ved markkøb/-salg** (Til/Fra i Indstillinger). Ingen afgrøder ændres, hvis gården ikke kan læses helt.
|
||||
- **1.62.282** "Alt automatisk" udvidet (brugerønsker 17/10): **gårdens forbrug dækkes først** — kædeplanen (stalde/fabrikker, fx græs/kløver/lucerne til køerne) sætter de marker der skal til, uden kædens loft på skift pr. runde; spillerens egne mål røres aldrig. **Resten spredes skiftevis på de 3 mest indbringende afgrøder** (spredt høst). Afgrøder vurderes over **hele året** (i november kan intet sås før marts). Knappen venter, til markscanningen er færdig. Ny markering **[Gård]** i Auto-kolonnen for marker der dyrker til gårdens eget forbrug.
|
||||
|
||||
@@ -15,12 +15,7 @@ ADBuildings.CACHE_MS = 30000
|
||||
|
||||
local cache = {list = nil, farmId = nil, time = -math.huge}
|
||||
|
||||
local function now()
|
||||
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then
|
||||
return g_currentMission.time
|
||||
end
|
||||
return os.clock() * 1000
|
||||
end
|
||||
local now = ADUtil.now
|
||||
|
||||
local function getNodePosition(node)
|
||||
if node == nil or (entityExists ~= nil and not entityExists(node)) then
|
||||
|
||||
@@ -36,10 +36,7 @@ ADChain.problemKeys = {}
|
||||
-- hele data). nil efter indlæsning: det der allerede er slukket ved første tjek, advares der ikke om.
|
||||
ADChain.depPrevEnabled = nil
|
||||
|
||||
local function contains(list, value)
|
||||
for _, item in ipairs(list or {}) do if item == value then return true end end
|
||||
return false
|
||||
end
|
||||
local contains = ADUtil.contains
|
||||
|
||||
local function sortedKeys(map)
|
||||
local keys = {}
|
||||
@@ -606,11 +603,7 @@ end
|
||||
|
||||
-- ---- gem / indlæs (samme sti-opslag som runs; skrives kun fra ADRunsController.save) ----
|
||||
|
||||
local function getPath()
|
||||
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath() or nil
|
||||
if path == nil then return nil end
|
||||
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", ADChain.FILE_NAME))
|
||||
end
|
||||
local function getPath() return ADUtil.savePath(ADChain.FILE_NAME) end
|
||||
|
||||
function ADChain.save()
|
||||
local path = getPath()
|
||||
|
||||
@@ -72,10 +72,7 @@ function ADChainActions.edges(chain, recipes)
|
||||
return list
|
||||
end
|
||||
|
||||
local function contains(list, value)
|
||||
for _, item in ipairs(list or {}) do if item == value then return true end end
|
||||
return false
|
||||
end
|
||||
local contains = ADUtil.contains
|
||||
|
||||
function ADChainActions.isCovered(edge, runs)
|
||||
for _, run in ipairs(runs or {}) do
|
||||
|
||||
@@ -107,11 +107,7 @@ function ADCpResume.takeToStart(adEnv)
|
||||
return ids, byId
|
||||
end
|
||||
|
||||
local function getPath()
|
||||
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath() or nil
|
||||
if path == nil then return nil end
|
||||
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", ADCpResume.FILE_NAME))
|
||||
end
|
||||
local function getPath() return ADUtil.savePath(ADCpResume.FILE_NAME) end
|
||||
|
||||
function ADCpResume.save(adEnv)
|
||||
local path = getPath()
|
||||
|
||||
@@ -8,10 +8,7 @@ ADDependency = {}
|
||||
ADDependency.STOCK_DAYS_OK = 60 -- lager til mere end så mange døgn: ingen advarsel
|
||||
ADDependency.MAX_USERS = 2 -- brugere nævnt i beskeden; resten "+k"
|
||||
|
||||
local function contains(list, value)
|
||||
for _, item in ipairs(list or {}) do if item == value then return true end end
|
||||
return false
|
||||
end
|
||||
local contains = ADUtil.contains
|
||||
|
||||
local function sortedKeys(map)
|
||||
local keys = {}
|
||||
|
||||
@@ -55,11 +55,7 @@ local STRAW_STAGES = {collect = {"strawCollecting"}, bale = {"baling", "baleColl
|
||||
local SOIL_VALUES = {auto = true, cultivate = true, plow = true, none = true}
|
||||
local PRODUCTS = {auto = true, silage = true, hay = true, harvest = true}
|
||||
|
||||
local function copyList(list)
|
||||
local copy = {}
|
||||
for index, value in ipairs(list or {}) do copy[index] = value end
|
||||
return copy
|
||||
end
|
||||
local copyList = ADUtil.copyList
|
||||
|
||||
local function copyPlan(plan)
|
||||
local copy = {}
|
||||
|
||||
@@ -0,0 +1,440 @@
|
||||
-- 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.
|
||||
|
||||
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
|
||||
|
||||
-- Stationens læssetriggere i verden (buying-/loading-station) -> {{x, z}}.
|
||||
local function loadTriggerPoints(station)
|
||||
local points = {}
|
||||
for _, trigger in pairs(station.loadTriggers or {}) 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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
local id, step = markerAtStationRaw(adEnv, vehicle, station, placeable)
|
||||
pcall(function()
|
||||
local key = placeable or station
|
||||
if key ~= nil and loggedStationMarker[key] ~= tostring(id) then
|
||||
loggedStationMarker[key] = tostring(id)
|
||||
local marker = id ~= nil and adEnv.ADGraphManager:getMapMarkerByWayPointId(id) or nil
|
||||
local name = placeable ~= nil and placeable.getName ~= nil and placeable:getName() or "?"
|
||||
Logging.info("ADSmartPickup: påfyldning: station '%s' -> markør '%s' (trin %s)", tostring(name),
|
||||
tostring(marker ~= nil and marker.name or id), tostring(step))
|
||||
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
|
||||
function ADFieldWork.findRefillMarker(adEnv, vehicle, fillType, x, z, skipRoute, exclude)
|
||||
local farmId = vehicle:getOwnerFarmId()
|
||||
local best, bestDistance = nil, math.huge
|
||||
local function consider(wayPointId)
|
||||
if exclude ~= nil and wayPointId ~= nil and exclude[wayPointId] then return end
|
||||
local wayPoint = wayPointId ~= nil and wayPointId > 0 and adEnv.ADGraphManager:getWayPointById(wayPointId) or nil
|
||||
if wayPoint == nil then return end
|
||||
local distance = (wayPoint.x - x) ^ 2 + (wayPoint.z - z) ^ 2
|
||||
if distance < bestDistance and (skipRoute or ADUnloadWait.hasRouteTo(adEnv, vehicle, wayPointId)) 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)
|
||||
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
|
||||
if not isShop and g_currentMission.accessHandler:canFarmAccess(farmId, station) and supported[fillType]
|
||||
and (station:getFillLevel(fillType, farmId) or 0) >= ADFieldWork.MIN_REFILL_STOCK_L then
|
||||
consider(markerAtStation(adEnv, vehicle, station, station.owningPlaceable))
|
||||
end
|
||||
end
|
||||
end)
|
||||
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))
|
||||
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)
|
||||
Logging.info("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)
|
||||
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
|
||||
@@ -706,11 +706,7 @@ end
|
||||
|
||||
-- ---- gem / indlæs (samme sti-opslag som runs) ----
|
||||
|
||||
local function getPath()
|
||||
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath() or nil
|
||||
if path == nil then return nil end
|
||||
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", ADFieldSilos.FILE_NAME))
|
||||
end
|
||||
local function getPath() return ADUtil.savePath(ADFieldSilos.FILE_NAME) end
|
||||
|
||||
function ADFieldSilos.save()
|
||||
local path = getPath()
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
-- AD Field Store
|
||||
-- Markarbejdets lager og salg (udskilt fra adFieldWork.lua 17/10-2026): hvor markens høst/skår køres hen —
|
||||
-- gæringssiloer, siloer, kontraktens salgssted eller bedst betalende salgssted — og ballesamlerens mål.
|
||||
-- Funktionerne ligger på ADFieldWork, så kaldere og tests er uændrede. Indlæses efter adFieldWork.lua.
|
||||
|
||||
local function log(...) ADFieldJobs.log(...) end
|
||||
|
||||
|
||||
-- Gårdens gæringssiloer for markens skår, nærmest marken først (produktioner skår -> SILAGE).
|
||||
function ADFieldWork.getFermenters(adEnv, farmId, windrowFillType, x, z)
|
||||
local silage = g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeIndexByName("SILAGE") or nil
|
||||
local result = {}
|
||||
for _, building in ipairs(ADBuildings.list(adEnv, farmId)) do
|
||||
local spec = building.kind == "factory" and building.placeable ~= nil and building.placeable.spec_productionPoint or nil
|
||||
local point = spec ~= nil and spec.productionPoint or nil
|
||||
if point ~= nil and ADFieldModel.isFermenter(point.productions, windrowFillType, silage) then
|
||||
local bx, _, bz = getWorldTranslation(building.placeable.rootNode)
|
||||
table.insert(result, {building = building, distance = (bx - (x or bx)) ^ 2 + (bz - (z or bz)) ^ 2})
|
||||
end
|
||||
end
|
||||
table.sort(result, function(a, b) return a.distance < b.distance end)
|
||||
local buildings = {}
|
||||
for _, entry in ipairs(result) do table.insert(buildings, entry.building) end
|
||||
return buildings
|
||||
end
|
||||
|
||||
local function unloadMarkerIds(buildings)
|
||||
local markerIds = {}
|
||||
for _, building in ipairs(buildings) do
|
||||
for _, marker in ipairs(ADBuildings.getMarkers(building, "unload") or {}) do
|
||||
table.insert(markerIds, marker.id)
|
||||
end
|
||||
end
|
||||
return markerIds
|
||||
end
|
||||
|
||||
-- Hvilken vare markens trin fører til lager: skår (ensilage), tørskår (hø) eller høstet afgrøde.
|
||||
function ADFieldWork.storeFillType(field, info)
|
||||
if field.stage == "harvesting" then return info.harvestFillType end
|
||||
if field.stage == "baleCollecting" and ADFieldModel.isBaleHarvest(field.lastFruit) then
|
||||
return g_fillTypeManager:getFillTypeIndexByName(string.upper(field.lastFruit))
|
||||
end
|
||||
if field.stage == "strawCollecting" or field.stage == "baling" or field.stage == "baleCollecting" then
|
||||
return g_fillTypeManager:getFillTypeIndexByName("STRAW")
|
||||
end
|
||||
local product = ADFieldModel.product(field.plan, field.kind or info.kind)
|
||||
return product == "hay" and info.hay or info.windrow
|
||||
end
|
||||
|
||||
-- Tvunget lager -> de valgte; ensilage -> gæringssiloer før almindelige siloer (in-game 25/9: græs endte
|
||||
-- i en Farma-silo); hø og høst -> kun siloer (gæringssiloer laver også hø om til ensilage).
|
||||
local saleLogged = {} -- fieldId -> salgsmarkør der sidst blev logget (log kun ved skift)
|
||||
|
||||
function ADFieldWork.chooseStore(adEnv, field, info, vehicle, fillType)
|
||||
local isReachable = function(markerId) return ADUnloadWait.hasRouteTo(adEnv, vehicle, markerId) end
|
||||
local farmId = vehicle:getOwnerFarmId()
|
||||
-- høstkontrakt: læssene skal til kontraktens salgssted (ADContracts), aldrig gårdens lager
|
||||
if field.contract and field.contractSellIds ~= nil then
|
||||
if #field.contractSellIds == 0 then return nil, "kontraktens salgssted har ingen AutoDrive-markør" end
|
||||
return ADLoadSwap.findForcedDumpMarker(adEnv, vehicle, fillType, 1, field.contractSellIds, isReachable)
|
||||
end
|
||||
if #field.plan.storeBuildingIds > 0 then
|
||||
local buildings = {}
|
||||
for _, buildingId in ipairs(field.plan.storeBuildingIds) do
|
||||
local building = ADBuildings.find(adEnv, farmId, buildingId)
|
||||
if building ~= nil then table.insert(buildings, building) end
|
||||
end
|
||||
return ADLoadSwap.findForcedDumpMarker(adEnv, vehicle, fillType, 1, unloadMarkerIds(buildings), isReachable)
|
||||
end
|
||||
local product = ADFieldModel.product(field.plan, field.kind or info.kind)
|
||||
if field.stage == "collecting" and product == "silage" then
|
||||
local fermenters = ADFieldWork.getFermenters(adEnv, farmId, fillType, info.x, info.z)
|
||||
if #fermenters > 0 then
|
||||
local store, why = ADLoadSwap.findForcedDumpMarker(adEnv, vehicle, fillType, 1, unloadMarkerIds(fermenters), isReachable)
|
||||
if store ~= nil then return store end
|
||||
log(field.id, "ingen gæringssilo kan tage græsset (%s) — bruger almindelig silo", tostring(why))
|
||||
end
|
||||
end
|
||||
local store, why = ADLoadSwap.findDumpMarker(adEnv, vehicle, fillType, 1, info.markerId, isReachable, nil, nil, false)
|
||||
if store ~= nil then return store end
|
||||
-- intet lager tager afgrøden: sælg den ved det bedst betalende salgssted med AD-markør og vej (in-game 16/10:
|
||||
-- sukkerroer fra F36 — gården har intet roelager, og tømmevognene blev sluppet igen og igen)
|
||||
-- kun når INTET lager tager varen — en fuld eller utilgængelig silo betyder vent, ikke sælg (review 16/10)
|
||||
local okAny, anyStore = pcall(ADLoadSwap.anyStoreAccepts, adEnv, vehicle, fillType, info.markerId)
|
||||
if okAny and anyStore then return nil, why end
|
||||
local sale, price = ADFieldWork.findSaleMarker(adEnv, vehicle, fillType, info.markerId)
|
||||
if sale ~= nil then
|
||||
if saleLogged[field.id] ~= sale.id then
|
||||
saleLogged[field.id] = sale.id
|
||||
local name = g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
|
||||
log(field.id, "intet lager tager %s — høsten sælges ved %s (%.0f kr/1000 l)", tostring(name), tostring(sale.name), (price or 0) * 1000)
|
||||
end
|
||||
return sale
|
||||
end
|
||||
return nil, why
|
||||
end
|
||||
|
||||
-- Ren: salgstilbud {price, wayPointId, name, routed} -> det bedst betalende der kan nås (nil hvis intet).
|
||||
function ADFieldWork.pickSaleQuote(quotes)
|
||||
local best = nil
|
||||
for _, quote in ipairs(quotes or {}) do
|
||||
if quote.routed and (best == nil or quote.price > best.price or (quote.price == best.price and quote.wayPointId < best.wayPointId)) then
|
||||
best = quote
|
||||
end
|
||||
end
|
||||
return best
|
||||
end
|
||||
|
||||
-- Salgssted for høst uden lager: alle salgsmarkører (som udkørselsflowene: ikke spærrede, ikke P/Connect/Wait) hvis
|
||||
-- station tager varen; højeste pris med vej fra marken og tilbage. -> AD-markør, pris pr. liter | nil
|
||||
-- stationOk(station) (valgfri): ekstra krav til stationen (baller: ballestation, ADFieldWork.stationTakesBales)
|
||||
function ADFieldWork.findSaleMarker(adEnv, vehicle, fillType, fieldMarkerId, stationOk)
|
||||
if fillType == nil or ADSources == nil or ADSources.getSellMarkers == nil or ADPrices == nil then return nil end
|
||||
local farmId = vehicle:getOwnerFarmId()
|
||||
local quotes = {}
|
||||
for _, entry in ipairs(ADSources.getSellMarkers(adEnv, farmId) or {}) do
|
||||
if ADSources.stationAccepts(entry.station, fillType) and (stationOk == nil or stationOk(entry.station)) then
|
||||
local quote = ADPrices.getQuote(entry.station, fillType)
|
||||
if quote ~= nil and quote.price > 0 then
|
||||
table.insert(quotes, {price = quote.price, wayPointId = entry.marker.id, name = entry.marker.name, routed = false})
|
||||
end
|
||||
end
|
||||
end
|
||||
-- rutetjek kun for de bedste (dyrt): højeste pris først, stop ved første der kan nås
|
||||
table.sort(quotes, function(a, b)
|
||||
if a.price ~= b.price then return a.price > b.price end
|
||||
return a.wayPointId < b.wayPointId
|
||||
end)
|
||||
for _, quote in ipairs(quotes) do
|
||||
local ok, routed = pcall(function()
|
||||
if fieldMarkerId ~= nil then return ADWaitPool.hasRouteBothWays(adEnv, vehicle, quote.wayPointId, fieldMarkerId) end
|
||||
return ADUnloadWait.hasRouteTo(adEnv, vehicle, quote.wayPointId)
|
||||
end)
|
||||
quote.routed = ok and routed == true
|
||||
if quote.routed then break end
|
||||
end
|
||||
local best = ADFieldWork.pickSaleQuote(quotes)
|
||||
if best == nil then return nil end
|
||||
return adEnv.ADGraphManager:getMapMarkerByWayPointId(best.wayPointId), best.price
|
||||
end
|
||||
|
||||
-- Plads der skal være i fabrikken til et ballelæs (et bomuldsmodul er flere tusinde liter; 1 l sendte vognen til en
|
||||
-- fuld fabrik — review 16/10)
|
||||
ADFieldWork.BALE_MIN_FREE_L = 20000
|
||||
|
||||
-- Tager stationen baller af varen? Spillets egen regel (UnloadingStation: "does not have a BaleUnloadTrigger"): en
|
||||
-- BaleUnloadTrigger for varen, eller en almindelig UnloadTrigger uden exactFillRootNode. In-game 16/10: bomulds-
|
||||
-- modulerne blev sendt til TMR Silo Farma (silo der tager COTTON løst, men ingen ballestation).
|
||||
function ADFieldWork.stationTakesBales(station, fillType)
|
||||
if station == nil or fillType == nil then return false end
|
||||
for _, trigger in pairs(station.unloadTriggers or {}) do
|
||||
if trigger.fillTypes ~= nil and trigger.fillTypes[fillType] ~= nil then
|
||||
local class = ClassUtil ~= nil and ClassUtil.getClassObjectByObject(trigger) or nil
|
||||
if BaleUnloadTrigger ~= nil and class == BaleUnloadTrigger then return true end
|
||||
if UnloadTrigger ~= nil and class == UnloadTrigger and trigger.exactFillRootNode == nil then return true end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Som ADLoadSwap.findDumpMarker (silo eller egen fabrik der bruger varen), men kun stationer med ballestation til
|
||||
-- varen: stationer uden springes over, og der søges igen (højst MAX_BALE_TRIES gange).
|
||||
ADFieldWork.MAX_BALE_TRIES = 20
|
||||
function ADFieldWork.findBaleDumpMarker(adEnv, vehicle, fillType, rest, homeMarkerId, isReachable, farmId)
|
||||
local excluded = {}
|
||||
for _ = 1, ADFieldWork.MAX_BALE_TRIES do
|
||||
local marker = ADLoadSwap.findDumpMarker(adEnv, vehicle, fillType, rest, homeMarkerId, isReachable, excluded, nil, true)
|
||||
if marker == nil then return nil end
|
||||
-- samme gård som findDumpMarker selv slår op med (review: getUnloadStationAtWayPointId caches pr. gård)
|
||||
local station = ADSmartPickup.getUnloadStationAtWayPointId(adEnv, marker.id, ADSmartPickup.getFarmId(vehicle))
|
||||
if station == nil then return nil end
|
||||
if ADFieldWork.stationTakesBales(station, fillType) then return marker end
|
||||
-- findDumpMarker udelukker på owningPlaceable; uden en kan stationen ikke springes over
|
||||
if station.owningPlaceable == nil then return nil end
|
||||
excluded[station.owningPlaceable] = true
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Mål for ballesamlerens læs -> {wayPointId, name} | nil. Halm: balle-/palleskur (som før). Bomuldsmoduler
|
||||
-- (brugerønske 16/10): egen fabrik der bruger dem (spinderiet), ellers skur, ellers salgssted der køber dem.
|
||||
function ADFieldWork.chooseBaleStore(adEnv, field, info, vehicle, fillType, isReachable, farmId)
|
||||
if not ADFieldModel.isBaleHarvest(field.lastFruit) then
|
||||
return ADSources.findPalletStore(adEnv, vehicle, fillType, nil, isReachable, farmId)
|
||||
end
|
||||
local takesBales = function(station) return ADFieldWork.stationTakesBales(station, fillType) end
|
||||
local okFactory, factory = pcall(ADFieldWork.findBaleDumpMarker, adEnv, vehicle, fillType, ADFieldWork.BALE_MIN_FREE_L,
|
||||
info.markerId, isReachable, farmId)
|
||||
if not okFactory then log(field.id, "ballemål: fabriksopslag fejlede: %s", tostring(factory)) end
|
||||
if okFactory and factory ~= nil then return {wayPointId = factory.id, name = factory.name} end
|
||||
local shed = ADSources.findPalletStore(adEnv, vehicle, fillType, nil, isReachable, farmId)
|
||||
if shed ~= nil then return shed end
|
||||
-- en fabrik der bruger varen, men er fuld lige nu: vent, sælg ikke (som chooseStore, review 16/10)
|
||||
local okAny, anyFactory = pcall(ADFieldWork.findBaleDumpMarker, adEnv, vehicle, fillType, 0, info.markerId, isReachable, farmId)
|
||||
if okAny and anyFactory ~= nil then
|
||||
log(field.id, "ballemål: %s har ikke plads lige nu — modulerne venter", tostring(anyFactory.name))
|
||||
return nil
|
||||
end
|
||||
local okSale, sale = pcall(ADFieldWork.findSaleMarker, adEnv, vehicle, fillType, info.markerId, takesBales)
|
||||
if not okSale then log(field.id, "ballemål: salgsopslag fejlede: %s", tostring(sale)) end
|
||||
if okSale and sale ~= nil then return {wayPointId = sale.id, name = sale.name} end
|
||||
return nil
|
||||
end
|
||||
+428
-917
File diff suppressed because it is too large
Load Diff
@@ -234,11 +234,7 @@ function ADFlowSleep.hasWork(adEnv, run, byId, onlyTractorId)
|
||||
return false, nil
|
||||
end
|
||||
|
||||
local function positionOf(vehicle)
|
||||
if vehicle == nil or vehicle.rootNode == nil then return nil, nil end
|
||||
local x, _, z = getWorldTranslation(vehicle.rootNode)
|
||||
return x, z
|
||||
end
|
||||
local positionOf = ADUtil.positionOf
|
||||
|
||||
local function isOwnFree(vehicle, tractorId, run)
|
||||
if vehicle == nil then return false end
|
||||
|
||||
@@ -167,9 +167,7 @@ local function liveSwap(combine)
|
||||
return swap
|
||||
end
|
||||
|
||||
local function vehicleName(vehicle)
|
||||
return vehicle ~= nil and vehicle.getName ~= nil and vehicle:getName() or "?"
|
||||
end
|
||||
local vehicleName = ADUtil.vehicleName
|
||||
|
||||
local function jointTypes()
|
||||
if AttacherJoints == nil then return nil, nil end
|
||||
|
||||
@@ -16,12 +16,7 @@ ADHitchApproach.ENABLED = false
|
||||
|
||||
-- ---- 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
|
||||
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
|
||||
@@ -89,9 +84,7 @@ local function parkWayPointId(adEnv, connectName)
|
||||
return nil
|
||||
end
|
||||
|
||||
local function vehicleName(vehicle)
|
||||
return vehicle ~= nil and vehicle.getName ~= nil and vehicle:getName() or "?"
|
||||
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)
|
||||
|
||||
@@ -97,10 +97,7 @@ ADHitchCreep.REVERSE_OUT_TIMEOUT_MS = 90000
|
||||
ADHitchCreep.REVERSE_OUT_STUCK_MS = 10000
|
||||
ADHitchCreep.STILL_MOVE_M = 0.5 -- m: så langt skal den være kommet, før "står stille"-uret nulstilles
|
||||
|
||||
local function contains(list, value)
|
||||
for _, item in ipairs(list or {}) do if item == value then return true end end
|
||||
return false
|
||||
end
|
||||
local contains = ADUtil.contains
|
||||
|
||||
-- Bakkekant: AD's konvention — fra står i fra.out, men ikke i til.incoming.
|
||||
function ADHitchCreep.isReverseEdge(fromId, toWp)
|
||||
@@ -174,9 +171,7 @@ local function worldPos(node)
|
||||
return {x = x, y = y, z = z}
|
||||
end
|
||||
|
||||
local function vehicleName(vehicle)
|
||||
return vehicle ~= nil and vehicle.getName ~= nil and vehicle:getName() or "?"
|
||||
end
|
||||
local vehicleName = ADUtil.vehicleName
|
||||
|
||||
-- Traktorens ledige krog-noder på den ene side: bag (lz < 0) eller front (lz > 0, frontslåmaskine 8/10).
|
||||
local function freeJoints(vehicle, front)
|
||||
|
||||
@@ -40,9 +40,7 @@ end
|
||||
|
||||
-- ---- spil ----
|
||||
|
||||
local function fillTypeName(fillType)
|
||||
return g_fillTypeManager ~= nil and fillType ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
|
||||
end
|
||||
local fillTypeName = ADUtil.fillTypeName
|
||||
|
||||
local function inBox(trigger, trailer)
|
||||
for key, entry in pairs(trigger.fillableObjects or {}) do
|
||||
|
||||
@@ -18,11 +18,7 @@ function ADNames.fromRecords(records)
|
||||
return byId
|
||||
end
|
||||
|
||||
local function getPath()
|
||||
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath() or nil
|
||||
if path == nil then return nil end
|
||||
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", ADNames.FILE_NAME))
|
||||
end
|
||||
local function getPath() return ADUtil.savePath(ADNames.FILE_NAME) end
|
||||
|
||||
function ADNames.load()
|
||||
ADNames.byId = {}
|
||||
|
||||
@@ -21,10 +21,7 @@ function ADNaming.nextNumber(names, prefix)
|
||||
return highest + 1
|
||||
end
|
||||
|
||||
local function contains(names, wanted)
|
||||
for _, name in ipairs(names) do if name == wanted then return true end end
|
||||
return false
|
||||
end
|
||||
local contains = ADUtil.contains
|
||||
|
||||
-- Et fast navn der kan være brugt: "X", ellers "X 2", "X 3" ...
|
||||
local function uniqueFixed(names, base)
|
||||
|
||||
@@ -83,12 +83,7 @@ function ADOutbound.resetEmptySources()
|
||||
emptyUntil = {}
|
||||
end
|
||||
|
||||
local function now()
|
||||
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then
|
||||
return g_currentMission.time
|
||||
end
|
||||
return os.clock() * 1000
|
||||
end
|
||||
local now = ADUtil.now
|
||||
|
||||
local function getRun(vehicle)
|
||||
return ADRunsController ~= nil and ADRunsController.getRunForVehicle ~= nil and activeRunOf(vehicle) or nil
|
||||
@@ -99,9 +94,7 @@ function ADOutbound.isOutboundVehicle(vehicle)
|
||||
return run ~= nil and run.kind == "outbound"
|
||||
end
|
||||
|
||||
local function fillTypeName(fillType)
|
||||
return g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
|
||||
end
|
||||
local fillTypeName = ADUtil.fillTypeName
|
||||
|
||||
-- Lagrene en kildes læssestation trækker fra (FS25: sourceStorages = {[storage] = storage}); nil = ukendt.
|
||||
local function sourceStorages(source)
|
||||
@@ -456,11 +449,7 @@ end
|
||||
|
||||
-- ---- tur ----
|
||||
|
||||
local function copyList(list)
|
||||
local copy = {}
|
||||
for index, value in ipairs(list or {}) do copy[index] = value end
|
||||
return copy
|
||||
end
|
||||
local copyList = ADUtil.copyList
|
||||
|
||||
local function choiceFor(marker, source, trip)
|
||||
return {
|
||||
|
||||
@@ -82,11 +82,7 @@ end
|
||||
|
||||
-- ---- spil ----
|
||||
|
||||
local function positionOf(object)
|
||||
if object == nil or object.rootNode == nil then return nil end
|
||||
local x, _, z = getWorldTranslation(object.rootNode)
|
||||
return x, z
|
||||
end
|
||||
local positionOf = ADUtil.positionOf
|
||||
|
||||
local function isPallet(vehicle)
|
||||
return vehicle.spec_pallet ~= nil or vehicle.typeName == "pallet"
|
||||
|
||||
@@ -58,10 +58,7 @@ end
|
||||
|
||||
-- ---- spil-adapter ----
|
||||
|
||||
local function now()
|
||||
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then return g_currentMission.time end
|
||||
return os.clock() * 1000
|
||||
end
|
||||
local now = ADUtil.now
|
||||
|
||||
local function label(vehicle)
|
||||
return tostring(ADRuns ~= nil and ADRuns.vehicleLabel(vehicle) or (vehicle.getName ~= nil and vehicle:getName()) or "?")
|
||||
|
||||
@@ -397,10 +397,7 @@ local function parkMarker(adEnv, vehicle)
|
||||
return adEnv.ADGraphManager:getMapMarkerById(index)
|
||||
end
|
||||
|
||||
local function positionOf(vehicle)
|
||||
local x, _, z = getWorldTranslation(vehicle.rootNode)
|
||||
return x, z
|
||||
end
|
||||
local positionOf = ADUtil.positionOf
|
||||
|
||||
-- Butiksdata: redskabets krævede effekt (hk). specs indlæses først, når butikken har vist varen.
|
||||
local neededHpCache = {} -- configFileName -> hk eller false (butiksdata ændrer sig ikke i et spil)
|
||||
|
||||
@@ -24,13 +24,7 @@ ADRuns.DEFAULT_SELL_ABOVE_PERCENT, ADRuns.MIN_SELL_ABOVE_PERCENT, ADRuns.MAX_SEL
|
||||
-- "hent først ved X % i kilden" (0 = fra): salg venter til kilden er så fuld eller et fuldt læs ligger klar
|
||||
ADRuns.DEFAULT_MIN_SOURCE_PERCENT, ADRuns.MIN_MIN_SOURCE_PERCENT, ADRuns.MAX_MIN_SOURCE_PERCENT = 0, 0, 95
|
||||
|
||||
local function copyList(list)
|
||||
local copy = {}
|
||||
for index, value in ipairs(list or {}) do
|
||||
copy[index] = value
|
||||
end
|
||||
return copy
|
||||
end
|
||||
local copyList = ADUtil.copyList
|
||||
|
||||
local function copyProducts(products)
|
||||
local copy = {}
|
||||
|
||||
@@ -16,13 +16,7 @@ ADRunsController.MIN_LOAD_CAPACITY = 100
|
||||
-- traktorerne tilfældigvis kører AutoDrive — en manuelt startet traktor må ikke vende knappen).
|
||||
ADRunsController.runningRuns = {}
|
||||
|
||||
local function copyList(list)
|
||||
local copy = {}
|
||||
for index, value in ipairs(list or {}) do
|
||||
copy[index] = value
|
||||
end
|
||||
return copy
|
||||
end
|
||||
local copyList = ADUtil.copyList
|
||||
|
||||
-- Stabil nøgle: FS25's uniqueId ("vehicle…"), ellers savegame-id. Navne bruges aldrig.
|
||||
function ADRunsController.getVehicleId(vehicle)
|
||||
|
||||
@@ -113,11 +113,7 @@ function ADSellBlock:consoleSellBlock(...)
|
||||
return table.concat(lines, "\n")
|
||||
end
|
||||
|
||||
local function getPath()
|
||||
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath() or nil
|
||||
if path == nil then return nil end
|
||||
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", ADSellBlock.FILE_NAME))
|
||||
end
|
||||
local function getPath() return ADUtil.savePath(ADSellBlock.FILE_NAME) end
|
||||
|
||||
function ADSellBlock.save()
|
||||
local path = getPath()
|
||||
|
||||
@@ -96,11 +96,7 @@ function ADSettings.restore(snapshot) values = snapshot or {global = {}, byBuild
|
||||
|
||||
-- ---- lagring ----
|
||||
|
||||
function ADSettings.getPath()
|
||||
local runsPath = ADRunsStorage ~= nil and ADRunsStorage.getPath ~= nil and ADRunsStorage.getPath() or nil
|
||||
if runsPath == nil then return nil end
|
||||
return (string.gsub(runsPath, ADRunsStorage.FILE_NAME .. "$", ADSettings.FILE_NAME))
|
||||
end
|
||||
function ADSettings.getPath() return ADUtil.savePath(ADSettings.FILE_NAME) end
|
||||
|
||||
function ADSettings.save()
|
||||
local path = ADSettings.getPath()
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
-- AD Util
|
||||
-- Små fælles hjælpere, samlet 17/10-2026 fra 20+ enslydende lokale kopier i modulerne. Indlæses først (modDesc),
|
||||
-- og i tests med dofile før de moduler der bruger dem. Ingen spil-afhængighed ved indlæsning.
|
||||
|
||||
ADUtil = {}
|
||||
|
||||
-- Spiltid i ms (g_currentMission.time); uden spil (tests) processorens tid.
|
||||
function ADUtil.now()
|
||||
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then return g_currentMission.time end
|
||||
return os.clock() * 1000
|
||||
end
|
||||
|
||||
function ADUtil.contains(list, value)
|
||||
for _, item in ipairs(list or {}) do if item == value then return true end end
|
||||
return false
|
||||
end
|
||||
|
||||
function ADUtil.copyList(list)
|
||||
local copy = {}
|
||||
for index, value in ipairs(list or {}) do copy[index] = value end
|
||||
return copy
|
||||
end
|
||||
|
||||
function ADUtil.vehicleName(vehicle)
|
||||
return vehicle ~= nil and vehicle.getName ~= nil and vehicle:getName() or "?"
|
||||
end
|
||||
|
||||
function ADUtil.fillTypeName(fillType)
|
||||
return g_fillTypeManager ~= nil and fillType ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
|
||||
end
|
||||
|
||||
-- x, z for et objekt med rootNode (nil, nil uden).
|
||||
function ADUtil.positionOf(object)
|
||||
if object == nil or object.rootNode == nil then return nil, nil end
|
||||
local x, _, z = getWorldTranslation(object.rootNode)
|
||||
return x, z
|
||||
end
|
||||
|
||||
-- Sti til en af modens filer i savegamet, ved siden af ADRunsStorage's fil (nil før savegamet kendes).
|
||||
function ADUtil.savePath(fileName)
|
||||
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath ~= nil and ADRunsStorage.getPath() or nil
|
||||
if path == nil then return nil end
|
||||
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", fileName))
|
||||
end
|
||||
@@ -12,9 +12,7 @@ function SmartPickupFrame.tabOfKind(kind)
|
||||
return SmartPickupFrame.TAB_OVERVIEW
|
||||
end
|
||||
|
||||
local function fillTypeName(fillType)
|
||||
return g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeNameByIndex(fillType) or tostring(fillType)
|
||||
end
|
||||
local fillTypeName = ADUtil.fillTypeName
|
||||
|
||||
-- Forsyningsflow der allerede leverer varen til en af forbrugerne -> run eller nil.
|
||||
local function existingSupplyRun(consumerIds, name)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<modDesc descVersion="107">
|
||||
<author>masterdraco</author>
|
||||
<version>1.62.284.0</version>
|
||||
<version>1.62.285.0</version>
|
||||
<title>
|
||||
<en>AD Smart Pickup</en>
|
||||
</title>
|
||||
@@ -12,6 +12,7 @@
|
||||
<multiplayer supported="true"/>
|
||||
<l10n filenamePrefix="l10n/l10n"/>
|
||||
<extraSourceFiles>
|
||||
<sourceFile filename="adUtil.lua"/>
|
||||
<sourceFile filename="adCache.lua"/>
|
||||
<sourceFile filename="adNaming.lua"/>
|
||||
<sourceFile filename="adNames.lua"/>
|
||||
@@ -50,6 +51,8 @@
|
||||
<sourceFile filename="adManualTransport.lua"/>
|
||||
<sourceFile filename="adPalletRefill.lua"/>
|
||||
<sourceFile filename="adFieldWork.lua"/>
|
||||
<sourceFile filename="adFieldStore.lua"/>
|
||||
<sourceFile filename="adFieldRefill.lua"/>
|
||||
<sourceFile filename="adFillFold.lua"/>
|
||||
<sourceFile filename="adPlayerTransport.lua"/>
|
||||
<sourceFile filename="adFieldSiloPlanner.lua"/>
|
||||
|
||||
Reference in New Issue
Block a user