NeoGriever

Untitled

Nov 6th, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. simulationResults = {}
  2. round_robin = false
  3. virtualInventory = {}
  4.  
  5. function loadRecipes(filename)
  6. print("Lade Rezepte aus Datei:", filename)
  7. local recipes = {}
  8. for line in io.lines(filename) do
  9. local name, inputs, outputs = line:match("([^/]+)/(.+)/(.+)")
  10. if name and inputs and outputs then
  11. local recipe = {name = name, inputs = {}, outputs = {}}
  12. print("Gefundenes Rezept:", name)
  13. for input in inputs:gmatch("[^;]+") do
  14. local item, count = input:match("([^,]+),(%d+)")
  15. table.insert(recipe.inputs, {name = item, count = tonumber(count)})
  16. print(" Benötigt:", item, "x", count)
  17. end
  18. for output in outputs:gmatch("[^;]+") do
  19. local item, count = output:match("([^,]+),(%d+)")
  20. table.insert(recipe.outputs, {name = item, count = tonumber(count)})
  21. print(" Ergibt:", item, "x", count)
  22. end
  23. table.insert(recipes, recipe)
  24. else
  25. print("Ungültige Rezeptzeile übersprungen:", line)
  26. end
  27. end
  28. return recipes
  29. end
  30.  
  31. function calculateTotalItemCount(itemName, inventory)
  32. local totalCount = 0
  33. for slot, item in pairs(inventory) do
  34. if item ~= nil and item.name == itemName then
  35. totalCount = totalCount + item.count
  36. end
  37. end
  38. return totalCount
  39. end
  40.  
  41. function createVirtualInventory()
  42. print("Erstelle virtuelles Inventar basierend auf der Truhe...")
  43. local virtualInventory = {}
  44. for i = 1, chest.size() do
  45. local item = chest.getItemDetail(i)
  46. if item ~= nil then
  47. virtualInventory[#virtualInventory + 1] = item
  48. print(" Slot", i, ":", item.name, "x", item.count)
  49. end
  50. end
  51. return virtualInventory
  52. end
  53.  
  54. function takeVirtualItem(inventory, item_id, amount, remove)
  55. local found_amount = 0
  56. for i = 1, #inventory do
  57. if inventory[i] ~= nil and inventory[i]["name"] == item_id then
  58. found_amount = found_amount + inventory[i]["count"]
  59. end
  60. end
  61. if found_amount >= amount then
  62. local required_amount = amount
  63. if remove then
  64. for i = 1, #inventory do
  65. if inventory[i] ~= nil and inventory[i]["name"] == item_id then
  66. if inventory[i]["count"] >= required_amount then
  67. inventory[i]["count"] = inventory[i]["count"] - required_amount
  68. required_amount = 0
  69. elseif inventory[i]["count"] > 0 then
  70. required_amount = required_amount - inventory[i]["count"]
  71. inventory[i]["count"] = 0
  72. end
  73. if inventory[i]["count"] == 0 then
  74. inventory[i] = nil
  75. end
  76. if required_amount == 0 then
  77. break
  78. end
  79. end
  80. end
  81. end
  82. return true
  83. else
  84. return false
  85. end
  86. end
  87.  
  88. function initializeSimulationResults(recipes)
  89. print("Initialisiere Simulationsergebnisse...")
  90. for _, recipe in ipairs(recipes) do
  91. simulationResults[recipe.name] = {executed = 0, max = 0}
  92. print(" Rezept hinzugefügt:", recipe.name)
  93. end
  94. printResults()
  95. end
  96.  
  97. function runSimulation(recipes)
  98. print("Starte Simulation...")
  99. virtualInventory = createVirtualInventory()
  100. local added_count = 1
  101. while added_count > 0 do
  102. added_count = 0
  103. for _, recipe in ipairs(recipes) do
  104. has_required_sources = true
  105. while has_required_sources do
  106. for _, input in ipairs(recipe.inputs) do
  107. if takeVirtualItem(virtualInventory, input.name, input.count, false) == false then
  108. has_required_sources = false
  109. break
  110. end
  111. end
  112. if has_required_sources then
  113. for _, input in ipairs(recipe.inputs) do
  114. takeVirtualItem(virtualInventory, input.name, input.count, true)
  115. end
  116. simulationResults[recipe.name].max = simulationResults[recipe.name].max + 1
  117. -- print(" Rezept ausführbar:", recipe.name, "x", simulationResults[recipe.name].max)
  118. -- printResults()
  119. added_count = added_count + 1
  120. if round_robin then
  121. break
  122. end
  123. end
  124. end
  125. end
  126. end
  127. print("Simulation abgeschlossen.")
  128. printResults()
  129. end
  130.  
  131. function executeRecipe(recipe)
  132. print("Führe Rezept aus:", recipe.name)
  133. for _, input in ipairs(recipe.inputs) do
  134. local remaining = input.count
  135. for i = 1, chest.size() do
  136. local item = chest.getItemDetail(i)
  137. if item ~= nil and item.name == input.name then
  138. local transferAmount = math.min(item.count, remaining)
  139. chest.pushItems("left", i, transferAmount)
  140. print(" Verschiebe:", transferAmount, "von", input.name)
  141. remaining = remaining - transferAmount
  142. if remaining <= 0 then
  143. break
  144. end
  145. end
  146. end
  147. end
  148. end
  149.  
  150. function waitForAndStoreOutput(recipe, executed)
  151. print("Warte auf Ergebnis für Rezept:", recipe.name)
  152. while true do
  153. local slot1results = machine.getItemDetail(1)
  154. if slot1results ~= nil and slot1results.name == recipe.outputs[1].name and slot1results.count >= recipe.outputs[1].count then
  155. chest.pullItems("left", 1, slot1results.count)
  156. executed = executed + 1
  157. simulationResults[recipe.name].executed = executed
  158. print(" Ergebnis erhalten:", slot1results.name, "x", slot1results.count)
  159. printResults()
  160. return executed
  161. end
  162. os.sleep(0.1)
  163. end
  164. end
  165.  
  166. function printResults()
  167. -- term.setCursorPos(1, 1)
  168. for name, result in pairs(simulationResults) do
  169. print(name .. " " .. trail(4, result.executed) .. " / " .. trail(4, result.max))
  170. end
  171. end
  172.  
  173. function trail(count, value)
  174. local str = tostring(value)
  175. return string.rep(" ", count - #str) .. str
  176. end
  177.  
  178. function main()
  179. print("Starte Programm...")
  180. local recipes = loadRecipes("recipes.lst")
  181. initializeSimulationResults(recipes)
  182. runSimulation(recipes)
  183. print("Going to main loop")
  184. round_robin = false
  185. if round_robin ~= false then
  186. print("Round Robin Mode")
  187. for _, recipe in ipairs(recipes) do
  188. print("Main-Iteration")
  189. local maxExecutions = simulationResults[recipe.name].max
  190. local executed = 0
  191. for _ = 1, maxExecutions do
  192. executeRecipe(recipe)
  193. executed = waitForAndStoreOutput(recipe, executed)
  194. end
  195. end
  196. else
  197. print("Stack Mode")
  198. local active = true
  199. local executionCounts = {}
  200. for _, recipe in ipairs(recipes) do
  201. executionCounts[recipe.name] = 0
  202. end
  203. print(#recipes, "Recipes")
  204. while active do
  205. print("Main-Iteration")
  206. active = false
  207. for _, recipe in ipairs(recipes) do
  208. print("Checking Recipe: ", recipe.name)
  209. local maxExecutions = simulationResults[recipe.name].max
  210. if executionCounts[recipe.name] < maxExecutions then
  211. print("Executing Recipe: ", recipe.name)
  212. executeRecipe(recipe)
  213. executionCounts[recipe.name] = waitForAndStoreOutput(recipe, executionCounts[recipe.name])
  214. active = true
  215. end
  216. end
  217. end
  218. end
  219. print("Programm abgeschlossen.")
  220. end
  221.  
  222. machine = peripheral.wrap("left")
  223. chest = peripheral.wrap("right")
  224.  
  225. main()
  226.  
Advertisement
Add Comment
Please, Sign In to add comment