filloax

Untitled

Oct 9th, 2022 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. --[[
  2. Automate Powah energizing orb; requires energizing orb below, an
  3. inventory/AE input/etc above, and a redstone signal that is enabled
  4. when the orb is empty.
  5. ]]
  6.  
  7. local RUN_EVERY_SEC = 0.5
  8. local GET_ITEMS_EVERY_ITERS = 4
  9. local DEBUG_MODE = true
  10.  
  11. local Items = {
  12. IRON = "minecraft:iron_ingot",
  13. GOLD = "minecraft:gold_ingot",
  14. EMERALD = "minecraft:emerald",
  15. DIAMOND = "minecraft:diamond",
  16. STAR = "minecraft:nether_star",
  17. RED_BLOCK = "minecraft:redstone_block",
  18. BLAZING_CRYSTAL_BLOCK = "powah:blazing_crystal_block",
  19. ENDER_EYE = "minecraft:ender_eye",
  20. DIELECTRIC_CASING = "powah:dielectric_casing",
  21. CAPACITOR_BASIC_TINY = "powah:capacitor_basic_tiny",
  22. BLUE_ICE = "minecraft:blue_ice",
  23. SNOWBALL = "minecraft:snowball",
  24. URANINITE_RAW = "powah:uraninite_raw",
  25. }
  26.  
  27. local MAX_SLOT = 16
  28.  
  29. -- Add deepslate/stone variants
  30. local UraniniteVariants = {
  31. URANINITE_ORE_POOR = "uraninite_ore_poor",
  32. URANINITE_ORE_DENSE = "uraninite_ore_dense",
  33. URANINITE_ORE = "uraninite_ore",
  34. }
  35. for k, suffix in pairs(UraniniteVariants) do
  36. Items[k] = "powah:" .. suffix
  37. Items["DEEPSLATE_" .. k] = "powah:deepslate_" .. suffix
  38. end
  39.  
  40. -- Inputs that aren't just 1 unit of the item, all the ones
  41. -- that are 1 unit of the item are automatically filled
  42. local ValidInputs = {
  43. {
  44. Items.IRON,
  45. Items.GOLD,
  46. },
  47. {
  48. Items.STAR,
  49. Items.RED_BLOCK,
  50. Items.RED_BLOCK,
  51. Items.BLAZING_CRYSTAL_BLOCK,
  52. },
  53. {
  54. Items.ENDER_EYE,
  55. Items.CAPACITOR_BASIC_TINY,
  56. Items.DIELECTRIC_CASING,
  57. },
  58. {
  59. Items.BLUE_ICE,
  60. Items.BLUE_ICE,
  61. },
  62. }
  63.  
  64. local function fillRemainingInputs()
  65. -- Fill remaining 1 item recipes, as above
  66. for k, itemId in pairs(Items) do
  67. local found = false
  68.  
  69. for _, recipe in ipairs(ValidInputs) do
  70. found = includes(recipe, itemId)
  71. if found then
  72. break
  73. end
  74. end
  75.  
  76. if not found then
  77. ValidInputs[#ValidInputs+1] = {
  78. itemId
  79. }
  80. end
  81. end
  82. end
  83.  
  84. local function getItemsAndCheck()
  85. local done = false
  86. repeat
  87. local success = turtle.suckUp(64)
  88. done = not success
  89. until done
  90.  
  91. for slot = 1, MAX_SLOT do
  92. local data = turtle.getItemDetail(slot)
  93. if data
  94. and not includes(Items, data.name) then
  95. turtle.select(slot)
  96. turtle.dropUp(64)
  97. end
  98. end
  99. end
  100.  
  101. local function nextRecipe()
  102. local currentRecipe
  103. local recipeSlots
  104.  
  105. for slot = 1, MAX_SLOT do
  106. local data = turtle.getItemDetail(slot)
  107. if data then
  108. if not currentRecipe then
  109. for _, recipe in ipairs(ValidInputs) do
  110. if includes(recipe, data.name) then
  111. -- copy of the recipe, items will be removed as they are done
  112. currentRecipe = {}
  113. for _, ingredient in ipairs(recipe) do
  114. currentRecipe[#currentRecipe+1] = ingredient
  115. end
  116. recipeSlots = {}
  117. print("Detected an item for recipe " .. dump(recipe) .. ", checking if has all...")
  118. break
  119. end
  120. end
  121. end
  122.  
  123. for i = 1, data.count do
  124. local idx = indexOf(currentRecipe, data.name)
  125. if idx then
  126. table.remove(currentRecipe, idx)
  127. if recipeSlots[slot] then
  128. recipeSlots[slot] = recipeSlots[slot] + 1
  129. else
  130. recipeSlots[slot] = 1
  131. end
  132. end
  133. end
  134. end
  135.  
  136. -- if has recipe and all items for it
  137. if currentRecipe and #currentRecipe == 0 then
  138. break
  139. end
  140. end
  141.  
  142. -- if has recipe and all items for it
  143. if currentRecipe and #currentRecipe == 0 then
  144. for slot, amount in pairs(recipeSlots) do
  145. turtle.select(slot)
  146. turtle.dropDown(amount)
  147. end
  148. print("Done recipe!")
  149. end
  150. end
  151.  
  152. -- This assumes input that is true when the energizing orb is empty
  153. -- For example, an inverted comparator out of the orb
  154. -- for ease of vertical redstone transmission
  155. local function shouldDoNext()
  156. for _, side in pairs(redstone.getSides()) do
  157. if redstone.getInput(side) then
  158. return true
  159. end
  160. end
  161. return false
  162. end
  163.  
  164. local function main()
  165. fillRemainingInputs()
  166.  
  167. print("Powah automation: starting!")
  168.  
  169. local iters = 0
  170. while true do
  171. if iters == 0 then
  172. getItemsAndCheck()
  173. end
  174.  
  175. if shouldDoNext() then
  176. nextRecipe()
  177. end
  178.  
  179. iters = (iters + 1) % GET_ITEMS_EVERY_ITERS
  180. ---@diagnostic disable-next-line: undefined-field
  181. os.sleep(RUN_EVERY_SEC)
  182. end
  183. end
  184.  
  185. -- library stuff
  186.  
  187. function includes(tbl, val)
  188. for k, v in pairs(tbl) do
  189. if v == val then
  190. return true
  191. end
  192. end
  193.  
  194. return false
  195. end
  196.  
  197. function indexOf(list, val)
  198. for i, v in ipairs(list) do
  199. if v == val then
  200. return i
  201. end
  202. end
  203. end
  204.  
  205. -- If the tables have the same items,
  206. -- with eventually different order
  207. function matches(tbl1, tbl2)
  208. local tbl2copy = {}
  209. for k2, v2 in pairs(tbl2) do
  210. tbl2copy[k2] = v2
  211. end
  212. for k1, v1 in pairs(tbl1) do
  213. local found = false
  214. for k2, v2 in pairs(tbl2copy) do
  215. if v2 == v1 then
  216. found = true
  217. tbl2copy[k2] = nil
  218. break
  219. end
  220. end
  221. if not found then
  222. return false
  223. end
  224. end
  225. return true
  226. end
  227.  
  228. function dump(o)
  229. if type(o) == 'table' then
  230. local s = '{ '
  231. for k,v in pairs(o) do
  232. if type(k) ~= 'number' then k = '"'..k..'"' end
  233. s = s .. '['..k..'] = ' .. dump(v) .. ','
  234. end
  235. return s .. '} '
  236. else
  237. return tostring(o)
  238. end
  239. end
  240.  
  241. -- Actually run
  242.  
  243. main()
  244.  
Advertisement
Add Comment
Please, Sign In to add comment