Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- simulationResults = {}
- round_robin = false
- virtualInventory = {}
- function loadRecipes(filename)
- print("Lade Rezepte aus Datei:", filename)
- local recipes = {}
- for line in io.lines(filename) do
- local name, inputs, outputs = line:match("([^/]+)/(.+)/(.+)")
- if name and inputs and outputs then
- local recipe = {name = name, inputs = {}, outputs = {}}
- print("Gefundenes Rezept:", name)
- for input in inputs:gmatch("[^;]+") do
- local item, count = input:match("([^,]+),(%d+)")
- table.insert(recipe.inputs, {name = item, count = tonumber(count)})
- print(" Benötigt:", item, "x", count)
- end
- for output in outputs:gmatch("[^;]+") do
- local item, count = output:match("([^,]+),(%d+)")
- table.insert(recipe.outputs, {name = item, count = tonumber(count)})
- print(" Ergibt:", item, "x", count)
- end
- table.insert(recipes, recipe)
- else
- print("Ungültige Rezeptzeile übersprungen:", line)
- end
- end
- return recipes
- end
- function calculateTotalItemCount(itemName, inventory)
- local totalCount = 0
- for slot, item in pairs(inventory) do
- if item ~= nil and item.name == itemName then
- totalCount = totalCount + item.count
- end
- end
- return totalCount
- end
- function createVirtualInventory()
- print("Erstelle virtuelles Inventar basierend auf der Truhe...")
- local virtualInventory = {}
- for i = 1, chest.size() do
- local item = chest.getItemDetail(i)
- if item ~= nil then
- virtualInventory[#virtualInventory + 1] = item
- print(" Slot", i, ":", item.name, "x", item.count)
- end
- end
- return virtualInventory
- end
- function takeVirtualItem(inventory, item_id, amount, remove)
- local found_amount = 0
- for i = 1, #inventory do
- if inventory[i] ~= nil and inventory[i]["name"] == item_id then
- found_amount = found_amount + inventory[i]["count"]
- end
- end
- if found_amount >= amount then
- local required_amount = amount
- if remove then
- for i = 1, #inventory do
- if inventory[i] ~= nil and inventory[i]["name"] == item_id then
- if inventory[i]["count"] >= required_amount then
- inventory[i]["count"] = inventory[i]["count"] - required_amount
- required_amount = 0
- elseif inventory[i]["count"] > 0 then
- required_amount = required_amount - inventory[i]["count"]
- inventory[i]["count"] = 0
- end
- if inventory[i]["count"] == 0 then
- inventory[i] = nil
- end
- if required_amount == 0 then
- break
- end
- end
- end
- end
- return true
- else
- return false
- end
- end
- function initializeSimulationResults(recipes)
- print("Initialisiere Simulationsergebnisse...")
- for _, recipe in ipairs(recipes) do
- simulationResults[recipe.name] = {executed = 0, max = 0}
- print(" Rezept hinzugefügt:", recipe.name)
- end
- printResults()
- end
- function runSimulation(recipes)
- print("Starte Simulation...")
- virtualInventory = createVirtualInventory()
- local added_count = 1
- while added_count > 0 do
- added_count = 0
- for _, recipe in ipairs(recipes) do
- has_required_sources = true
- while has_required_sources do
- for _, input in ipairs(recipe.inputs) do
- if takeVirtualItem(virtualInventory, input.name, input.count, false) == false then
- has_required_sources = false
- break
- end
- end
- if has_required_sources then
- for _, input in ipairs(recipe.inputs) do
- takeVirtualItem(virtualInventory, input.name, input.count, true)
- end
- simulationResults[recipe.name].max = simulationResults[recipe.name].max + 1
- -- print(" Rezept ausführbar:", recipe.name, "x", simulationResults[recipe.name].max)
- -- printResults()
- added_count = added_count + 1
- if round_robin then
- break
- end
- end
- end
- end
- end
- print("Simulation abgeschlossen.")
- printResults()
- end
- function executeRecipe(recipe)
- print("Führe Rezept aus:", recipe.name)
- for _, input in ipairs(recipe.inputs) do
- local remaining = input.count
- for i = 1, chest.size() do
- local item = chest.getItemDetail(i)
- if item ~= nil and item.name == input.name then
- local transferAmount = math.min(item.count, remaining)
- chest.pushItems("left", i, transferAmount)
- print(" Verschiebe:", transferAmount, "von", input.name)
- remaining = remaining - transferAmount
- if remaining <= 0 then
- break
- end
- end
- end
- end
- end
- function waitForAndStoreOutput(recipe, executed)
- print("Warte auf Ergebnis für Rezept:", recipe.name)
- while true do
- local slot1results = machine.getItemDetail(1)
- if slot1results ~= nil and slot1results.name == recipe.outputs[1].name and slot1results.count >= recipe.outputs[1].count then
- chest.pullItems("left", 1, slot1results.count)
- executed = executed + 1
- simulationResults[recipe.name].executed = executed
- print(" Ergebnis erhalten:", slot1results.name, "x", slot1results.count)
- printResults()
- return executed
- end
- os.sleep(0.1)
- end
- end
- function printResults()
- -- term.setCursorPos(1, 1)
- for name, result in pairs(simulationResults) do
- print(name .. " " .. trail(4, result.executed) .. " / " .. trail(4, result.max))
- end
- end
- function trail(count, value)
- local str = tostring(value)
- return string.rep(" ", count - #str) .. str
- end
- function main()
- print("Starte Programm...")
- local recipes = loadRecipes("recipes.lst")
- initializeSimulationResults(recipes)
- runSimulation(recipes)
- print("Going to main loop")
- round_robin = false
- if round_robin ~= false then
- print("Round Robin Mode")
- for _, recipe in ipairs(recipes) do
- print("Main-Iteration")
- local maxExecutions = simulationResults[recipe.name].max
- local executed = 0
- for _ = 1, maxExecutions do
- executeRecipe(recipe)
- executed = waitForAndStoreOutput(recipe, executed)
- end
- end
- else
- print("Stack Mode")
- local active = true
- local executionCounts = {}
- for _, recipe in ipairs(recipes) do
- executionCounts[recipe.name] = 0
- end
- print(#recipes, "Recipes")
- while active do
- print("Main-Iteration")
- active = false
- for _, recipe in ipairs(recipes) do
- print("Checking Recipe: ", recipe.name)
- local maxExecutions = simulationResults[recipe.name].max
- if executionCounts[recipe.name] < maxExecutions then
- print("Executing Recipe: ", recipe.name)
- executeRecipe(recipe)
- executionCounts[recipe.name] = waitForAndStoreOutput(recipe, executionCounts[recipe.name])
- active = true
- end
- end
- end
- end
- print("Programm abgeschlossen.")
- end
- machine = peripheral.wrap("left")
- chest = peripheral.wrap("right")
- main()
Advertisement
Add Comment
Please, Sign In to add comment