-- AD Chain Model (ren logik) -- Sporer baglæns fra de produktioner spilleren selv har tændt (rødder) gennem gårdens egne fabrikker og stalde -- til råvarerne. Alle mængder er liter pr. måned; varer og afgrøder er NAVNE. Se spec 2026-09-30-produktionskaeder. ADChainModel = {} ADChainModel.MAX_DEPTH = 8 local function byKey(a, b) return tostring(a.key) < tostring(b.key) end -- producenter pr. vare: tændte først, så stabil rækkefølge local function indexProducers(recipes) local index = {} for _, recipe in ipairs(recipes or {}) do for name, amount in pairs(recipe.outputs or {}) do if (amount or 0) > 0 and (recipe.cyclesPerMonth or 0) > 0 then index[name] = index[name] or {} table.insert(index[name], recipe) end end end for _, list in pairs(index) do table.sort(list, function(a, b) if (a.enabled == true) ~= (b.enabled == true) then return a.enabled == true end return byKey(a, b) end) end return index end local function indexHerds(husbandry) local index = {} for _, herd in ipairs(husbandry or {}) do for name, perMonth in pairs(herd.outputs or {}) do if (perMonth or 0) > 0 then index[name] = index[name] or {} table.insert(index[name], {building = herd.building, buildingName = herd.buildingName, left = perMonth}) end end end return index end local function sortedNames(map) local names = {} for name in pairs(map or {}) do table.insert(names, name) end table.sort(names) return names end -- fieldGoods = {[vare] = true}: varer der høstes/laves på marken uden at være en afgrødes hovedvare (hø, hakkelse, -- halm …). Uden producent/stald er kilden "field" uden afgrøde: intet markbehov og intet problem. -- goodCrops = {[vare] = {fruit, product}}: markvarer der kommer fra en bestemt afgrøde med et produkt (hø = græs med -- produkt hø, græsvindue = græs med ensilage) — de får markbehov pr. vare (fieldDemand[vare], fieldProducts[vare]). -- animalDemand (valgfri): {[vare] = liter/md} dyrenes foder — regnes som et behov efter fabrikkernes rødder, så -- afgrødeplanen også lægger frie marker om til det dyrene mangler (brugerønske 8/10: "dyrene først"). function ADChainModel.build(recipes, husbandry, crops, fieldGoods, goodCrops, animalDemand) local chain = {roots = {}, animals = {}, demand = {}, fieldDemand = {}, fieldProducts = {}, needRecipes = {}, problems = {}} local producers, herds = indexProducers(recipes), indexHerds(husbandry) -- Rødderne kører for sig selv med fuld fart: deres egne input kræves af deres eget træ. Som leverandør til en -- anden rod har de derfor fuld kapacitet, men deres input regnes ikke med igen (standalone = ingen rekursion). -- En pauset rod (recipe.paused) kører også for sig selv, men er ikke rod: kæden skaber intet nyt for den. local capacity, roots, standalone = {}, {}, {} for _, recipe in ipairs(recipes or {}) do capacity[recipe.key] = recipe.cyclesPerMonth or 0 if recipe.enabled and not recipe.byChain then if not recipe.paused and not recipe.notChosen then table.insert(roots, recipe) end standalone[recipe.key] = true end end table.sort(roots, byKey) -- detail = manglende liter/md (flaskehals/køb), need = varens behov pr. måned i den gren local function problem(kind, fillType, rootKey, detail, need) table.insert(chain.problems, {kind = kind, fillType = fillType, rootKey = rootKey, detail = detail, need = need}) end local need local function inputsOf(recipe, cycles, depth, path, rootKey) local children = {} for _, name in ipairs(sortedNames(recipe.inputs)) do local amount = recipe.inputs[name] or 0 if amount > 0 and cycles > 0 then local child = need(name, amount * cycles, depth + 1, path, rootKey) child.consumer, child.consumerName = recipe.building, recipe.buildingName table.insert(children, child) end end return children end need = function(name, perMonth, depth, path, rootKey) local node = {fillType = name, perMonth = perMonth, coveredPerMonth = 0, producers = {}, herds = {}, children = {}} chain.demand[name] = (chain.demand[name] or 0) + perMonth if depth > ADChainModel.MAX_DEPTH or path[name] then node.source = "cycle" problem("cycle", name, rootKey, nil, perMonth) return node end path[name] = true local rest = perMonth -- a) gårdens fabrikker local makers = producers[name] if makers ~= nil then node.source = "production" for _, recipe in ipairs(makers) do local perCycle = recipe.outputs[name] local cycles = math.min(rest / perCycle, capacity[recipe.key] or 0) if cycles > 0 then capacity[recipe.key] = capacity[recipe.key] - cycles rest = rest - cycles * perCycle table.insert(node.producers, {key = recipe.key, building = recipe.building, buildingName = recipe.buildingName, cycles = cycles}) if not standalone[recipe.key] then chain.needRecipes[recipe.key] = (chain.needRecipes[recipe.key] or 0) + cycles for _, child in ipairs(inputsOf(recipe, cycles, depth, path, rootKey)) do table.insert(node.children, child) end end end if rest <= 1e-9 then break end end if rest > 1e-9 and #node.producers > 0 then problem("bottleneck", name, rootKey, rest, perMonth) node.shortPerMonth = rest end if rest > 1e-9 and #node.producers == 0 then node.source = nil end end -- b) stalde if rest > 1e-9 and #node.producers == 0 and herds[name] ~= nil then node.source = "husbandry" for _, herd in ipairs(herds[name]) do local take = math.min(rest, herd.left) if take > 0 then herd.left = herd.left - take rest = rest - take table.insert(node.herds, {building = herd.building, buildingName = herd.buildingName, perMonth = take}) end end end -- c0) markvare fra en afgrøde med produkt (hø = græs/hø): markbehov pr. vare, så kæden planlægger marken; -- før afgrøde-stien, så et vindue der også er en afgrødes hovedvare (eng: GRASS_WINDROW) får sit produkt local goodCrop = goodCrops ~= nil and goodCrops[name] or nil if rest > 1e-9 and #node.producers == 0 and #node.herds == 0 and goodCrop ~= nil then node.source, node.fruit, node.product, node.shortKey = "field", goodCrop.fruit, goodCrop.product, name chain.fieldDemand[name] = (chain.fieldDemand[name] or 0) + rest chain.fieldProducts[name] = {fruit = goodCrop.fruit, product = goodCrop.product, fruits = goodCrop.fruits} rest = 0 end -- c) mark if rest > 1e-9 and #node.producers == 0 and #node.herds == 0 and crops ~= nil and crops[name] ~= nil then node.source, node.fruit = "field", crops[name] chain.fieldDemand[node.fruit] = (chain.fieldDemand[node.fruit] or 0) + rest rest = 0 end -- c3) markvare uden egen afgrøde (fx hakkelse, halm): kommer fra markerne, ingen afgrødeplan if rest > 1e-9 and #node.producers == 0 and #node.herds == 0 and fieldGoods ~= nil and fieldGoods[name] then node.source, node.fruit = "field", nil rest = 0 end -- d) ingen kilde (eller rest efter stald), eller exhausted producers if rest > 1e-9 and makers ~= nil and #node.producers == 0 and #node.herds == 0 then node.source = "production" node.shortPerMonth = rest problem("bottleneck", name, rootKey, rest, perMonth) elseif rest > 1e-9 and (node.source == nil or node.source == "husbandry") then if node.source == nil then node.source = "buy" end node.shortPerMonth = rest problem("buy", name, rootKey, rest, perMonth) end node.coveredPerMonth = perMonth - (node.shortPerMonth or 0) path[name] = nil return node end -- 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 -- 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 end