fix: foder-fabrikker planlægges efter dyrenes forbrug, kæden følges bagud (v1.62.106)

This commit is contained in:
2026-10-08 20:10:14 +02:00
parent 5fa7d8490b
commit 20f7d1a410
3 changed files with 50 additions and 7 deletions
+48 -6
View File
@@ -171,13 +171,55 @@ function ADChainModel.build(recipes, husbandry, crops, fieldGoods, goodCrops, an
return node
end
for _, root in ipairs(roots) do
local cycles = root.cyclesPerMonth or 0
table.insert(chain.roots, {key = root.key, name = root.name, building = root.building, buildingName = root.buildingName,
children = inputsOf(root, cycles, 0, {}, root.key)})
-- en rod der laver dyrefoder (fx TMR til køerne) planlægges efter hvad dyrene æder, ikke fuld kapacitet — ellers
-- kræver den marker man aldrig får brug for (in-game 8/10: TMR + fermentor gav "mangler 1702 ha hø / 1272 ha græs").
-- Kæden følges bagud: TMR'ens råvarer (ensilage) bliver et afledt foderbehov, så fermentoren der laver ensilagen
-- også begrænses. Sælges et af rodens output direkte, kører den fuldt. Øvrige rødder: fuld kapacitet som før.
local animalLeft, rootCycles = {}, {}
for name, perMonth in pairs(animalDemand or {}) do animalLeft[name] = perMonth end
local changed, passes = next(animalLeft) ~= nil, 0
while changed and passes < ADChainModel.MAX_DEPTH do
changed, passes = false, passes + 1
for _, root in ipairs(roots) do
if rootCycles[root.key] == nil then
local sells, feeds, feedCycles = false, false, 0
for name, perCycle in pairs(root.outputs or {}) do
if (root.outputModes or {})[name] == "sell" then sells = true end
if (animalLeft[name] or 0) > 0 and (perCycle or 0) > 0 then
feeds = true
feedCycles = math.max(feedCycles, animalLeft[name] / perCycle)
end
end
if feeds then
local cycles = sells and (root.cyclesPerMonth or 0) or math.min(root.cyclesPerMonth or 0, feedCycles)
rootCycles[root.key] = cycles
for name, perCycle in pairs(root.outputs or {}) do
if animalLeft[name] ~= nil then animalLeft[name] = math.max(0, animalLeft[name] - cycles * perCycle) end
end
-- rodens råvarer er nu også foder (afledt): den der laver dem, begrænses i næste runde
for name, amount in pairs(root.inputs or {}) do
if (amount or 0) > 0 and producers[name] ~= nil then
animalLeft[name] = (animalLeft[name] or 0) + amount * cycles
end
end
changed = true
end
end
end
end
for _, name in ipairs(sortedNames(animalDemand or {})) do
local perMonth = animalDemand[name] or 0
-- afledt behov (råvarer til foder-rødderne) er dækket af rødderne selv — kun dyrenes eget, udækkede foder tilbage
local animalRest = {}
for name, perMonth in pairs(animalDemand or {}) do
animalRest[name] = math.min(perMonth, animalLeft[name] or 0)
end
for _, root in ipairs(roots) do
local cycles = rootCycles[root.key] or root.cyclesPerMonth or 0
table.insert(chain.roots, {key = root.key, name = root.name, building = root.building, buildingName = root.buildingName,
cycles = cycles, children = inputsOf(root, cycles, 0, {}, root.key)})
end
animalLeft = animalRest
for _, name in ipairs(sortedNames(animalLeft)) do
local perMonth = animalLeft[name] or 0
if perMonth > 0 then table.insert(chain.animals, need(name, perMonth, 0, {}, "animals")) end
end
return chain