feat: stald-silo som mål, retfærdig fordeling, ADSettings + spuSet (v1.62.139)
This commit is contained in:
@@ -423,6 +423,57 @@ local function mayUseSource(station, fillTypes, level, loadCapacity, need)
|
||||
return true
|
||||
end
|
||||
|
||||
-- Stald-silo som mål: de varer staldene inden for stableSiloRange af siloen bruger (foder + halm). nil når målet ikke
|
||||
-- er en ren silo, eller ingen stald står ved den (så fyldes siloen med alt, den tager — et almindeligt lager).
|
||||
function ADSmartPickup.getStableSiloFillTypes(adEnv, stations, farmId)
|
||||
local placeables = {}
|
||||
for _, entry in ipairs(stations or {}) do
|
||||
local placeable = entry.station ~= nil and entry.station.owningPlaceable or nil
|
||||
if placeable == nil or placeable.spec_silo == nil or getHusbandry(entry.station) ~= nil or getProductionPoint(entry.station) ~= nil then return nil end
|
||||
table.insert(placeables, placeable)
|
||||
end
|
||||
if #placeables == 0 or ADBuildings == nil then return nil end
|
||||
local result, any = {}, false
|
||||
local strawIndex = g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeIndexByName("STRAW") or nil
|
||||
for _, silo in ipairs(placeables) do
|
||||
local siloId = ADBuildings.getId ~= nil and ADBuildings.getId(silo) or nil
|
||||
local range = ADSettings ~= nil and ADSettings.get("stableSiloRange", siloId) or ADSupplyPlanner.STABLE_SILO_M
|
||||
local okS, sx, _, sz = pcall(getWorldTranslation, silo.rootNode)
|
||||
if okS then
|
||||
for _, building in ipairs(ADBuildings.list(adEnv, farmId) or {}) do
|
||||
local husbandry = building.kind == "husbandry" and building.placeable or nil
|
||||
if husbandry ~= nil and husbandry.rootNode ~= nil then
|
||||
local okH, hx, _, hz = pcall(getWorldTranslation, husbandry.rootNode)
|
||||
if okH and math.sqrt((hx - sx) ^ 2 + (hz - sz) ^ 2) <= range then
|
||||
for fillType in pairs(getHusbandryFoodFillTypes(husbandry) or {}) do result[fillType], any = true, true end
|
||||
if strawIndex ~= nil and husbandry.spec_husbandryStraw ~= nil then result[strawIndex], any = true, true end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return any and result or nil
|
||||
end
|
||||
|
||||
-- Behov pr. vare lige nu (til retfærdig fordeling): køretøj -> {free, at}
|
||||
local demandRegistry = {}
|
||||
ADSmartPickup.DEMAND_FRESH_MS = 10 * 60 * 1000
|
||||
local function registerDemand(fillType, driver, freeSpace)
|
||||
local now = g_currentMission ~= nil and g_currentMission.time or 0
|
||||
demandRegistry[fillType] = demandRegistry[fillType] or {}
|
||||
demandRegistry[fillType][driver] = {free = freeSpace or 0, at = now}
|
||||
local total, others = 0, 0
|
||||
for vehicle, entry in pairs(demandRegistry[fillType]) do
|
||||
if now - entry.at > ADSmartPickup.DEMAND_FRESH_MS then
|
||||
demandRegistry[fillType][vehicle] = nil
|
||||
else
|
||||
total = total + entry.free
|
||||
if vehicle ~= driver then others = others + 1 end
|
||||
end
|
||||
end
|
||||
return total, others
|
||||
end
|
||||
|
||||
-- Kun gårdens lagre eller en silo ved stalden (ADSupplyPlanner.filterSupplySources); marksiloer udelades.
|
||||
function ADSmartPickup.restrictSupplySources(adEnv, markerStations, targetPlaceable)
|
||||
local fieldSiloIds = {}
|
||||
@@ -444,7 +495,9 @@ function ADSmartPickup.restrictSupplySources(adEnv, markerStations, targetPlacea
|
||||
table.insert(entries, {marker = entry.marker, station = entry.station, isFieldSilo = id ~= nil and fieldSiloIds[id] == true,
|
||||
isSilo = placeable ~= nil and placeable.spec_silo ~= nil, distance = distance})
|
||||
end
|
||||
local near, farm = ADSupplyPlanner.filterSupplySources(entries)
|
||||
local targetId = targetPlaceable ~= nil and ADBuildings ~= nil and ADBuildings.getId ~= nil and ADBuildings.getId(targetPlaceable) or nil
|
||||
local range = ADSettings ~= nil and ADSettings.get("stableSiloRange", targetId) or nil
|
||||
local near, farm = ADSupplyPlanner.filterSupplySources(entries, range)
|
||||
local all = {}
|
||||
for _, entry in ipairs(near) do table.insert(all, entry) end
|
||||
for _, entry in ipairs(farm) do table.insert(all, entry) end
|
||||
@@ -1006,12 +1059,23 @@ function ADSmartPickup.findSupplyPickup(adEnv, driver, ingredients, targetWayPoi
|
||||
local choice = not held[need] and (ADSupplyPlanner.isFoodAllowed == nil or ADSupplyPlanner.isFoodAllowed(need, bestFood))
|
||||
and findBestSourceTiered(sourceTiers, {need.fillType}, farmId, loadCapacity, need) or nil
|
||||
if choice ~= nil then
|
||||
-- retfærdig fordeling af gårdens lager mellem alle, der mangler varen lige nu (brugerønske 10/10)
|
||||
local pending = math.min(loadCapacity or math.huge, choice.level or math.huge, need.freeSpace or math.huge)
|
||||
local totalFree, others = registerDemand(need.fillType, driver, need.freeSpace)
|
||||
if choice.isOwn and others > 0 then
|
||||
local share = ADSupplyPlanner.fairShare(choice.level or 0, need.freeSpace or 0, totalFree)
|
||||
if share < pending then
|
||||
Logging.info(ADSmartPickup.LOG_PREFIX .. "'%s': %s deles med %d andre behov — tager %.0f l af %.0f l i %s",
|
||||
tostring(ADRuns.vehicleLabel(driver)), tostring(g_fillTypeManager:getFillTypeNameByIndex(need.fillType)), others, share, choice.level or 0, tostring(choice.markerName))
|
||||
pending = math.max(share, ADSmartPickup.MIN_USEFUL_LITERS or 0)
|
||||
end
|
||||
end
|
||||
return {
|
||||
markerIndex = choice.markerIndex,
|
||||
markerId = choice.markerId,
|
||||
targetWayPointId = wayPointId,
|
||||
-- det der reelt kommer af: vognen, kildens beholdning og målets fri plads sætter grænsen
|
||||
pendingLiters = math.min(loadCapacity or math.huge, choice.level or math.huge, need.freeSpace or math.huge),
|
||||
pendingLiters = pending,
|
||||
markerName = choice.markerName,
|
||||
group = choice.group,
|
||||
level = choice.level,
|
||||
|
||||
Reference in New Issue
Block a user