Advertisement
Tri11Paragon

Powah Autocrafter V8

Apr 18th, 2024 (edited)
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.11 KB | Gaming | 0 0
  1. -- state enums
  2. local STATE_NONE = "NONE"
  3. local STATE_STEEL = "Hardened Steel"
  4. local STATE_NITRO = "Nitro Crystal"
  5. local STATE_ENDER = "Ender Core"
  6. local STATE_BLAZE = "Blazing Crystal Block"
  7. local STATE_NIOTIC = "Niotic Crystal"
  8. local STATE_SPIRIT = "Spirited Crystal"
  9.  
  10. -- item -> state table
  11. -- this table will be used to determine what item we are currently crafting
  12. -- and what items need to be transfered in order to craft that item
  13. local state_from_item_table = {
  14.     -- energized steel
  15.     ["minecraft:iron_ingot"]=STATE_STEEL,
  16.     ["minecraft:gold_ingot"]=STATE_STEEL,
  17.  
  18.     -- nitro
  19.     ["minecraft:redstone_block"]=STATE_NITRO,
  20.     ["minecraft:nether_star"]=STATE_NITRO,
  21.     ["powah:blazing_crystal_block"]=STATE_NITRO,
  22.  
  23.     -- ender core
  24.     ["minecraft:ender_eye"]=STATE_ENDER,
  25.     ["powah:dielectric_casing"]=STATE_ENDER,
  26.     ["powah:capacitor_basic_tiny"]=STATE_ENDER,
  27.  
  28.     -- blaze crystal
  29.     ["botania:blaze_block"]=STATE_BLAZE,
  30.  
  31.     -- niotic crystal
  32.     ["minecraft:diamond"]=STATE_NIOTIC,
  33.  
  34.     -- spirited crystal
  35.     ["minecraft:emerald"]=STATE_SPIRIT
  36. }
  37.  
  38. -- item table describing what items are required to craft in each state
  39. local state_item_recipies = {
  40.     [STATE_STEEL] = {
  41.         ["minecraft:gold_ingot"] = 1,
  42.         ["minecraft:iron_ingot"] = 1
  43.     },
  44.     [STATE_NITRO] = {
  45.         ["minecraft:redstone_block"] = 2,
  46.         ["powah:blazing_crystal_block"] = 1,
  47.         ["minecraft:nether_star"] = 1
  48.     },
  49.     [STATE_ENDER] = {
  50.         ["minecraft:ender_eye"] = 1,
  51.         ["powah:dielectric_casing"] = 1,
  52.         ["powah:capacitor_basic_tiny"] = 1
  53.     },
  54.     [STATE_BLAZE] = {
  55.         ["botania:blaze_block"] = 1
  56.     },
  57.     [STATE_NIOTIC] = {
  58.         ["minecraft:diamond"] = 1
  59.     },
  60.     [STATE_SPIRIT] = {
  61.         ["minecraft:emerald"] = 1
  62.     }
  63. }
  64.  
  65. ---------------------------------
  66. -- DO NOT EDIT BELOW THIS LINE --
  67. ---------------------------------
  68.  
  69. -- the current state of the system
  70. cur_state = STATE_NONE
  71.  
  72. -- helper function for cloning the item table
  73. function table.clone(org)
  74.     local tbl = {}
  75.     for k,v in pairs(org) do
  76.         tbl[k] = v
  77.     end
  78.     return tbl
  79. end
  80.  
  81. -- helper function for len of table
  82. function table.count(tbl)
  83.     local count = 0
  84.     for _ in pairs(tbl) do
  85.         count = count + 1
  86.     end
  87.     return count
  88. end
  89.  
  90. -- determine what state we should enter
  91. -- based on what item we find in the chest
  92. function determineStateFromItem(item)
  93.     local state = state_from_item_table[item]
  94.     if state == nil then
  95.         return STATE_NONE
  96.     else
  97.         return state
  98.     end
  99. end
  100.  
  101. -- find where the chest is
  102. local chest = peripheral.find("minecraft:chest")
  103. local orb = peripheral.find("powah:energizing_orb")
  104.  
  105. -- waits for the orb's inventory to be empty then
  106. -- signals that the orb is ready to craft another item
  107. function waitForReset()
  108.     while true do
  109.         if table.count(orb.list()) == 0 then
  110.             break
  111.         end
  112.     end
  113.     cur_state = STATE_NONE
  114. end
  115.  
  116. -- transfers the requested number of items from the slot in the chest to the orb,
  117. -- if there is not enough items in the stack to meet the demands of the recipe
  118. -- the value in the recipe will be decremented by the amount we could transfer
  119. function transferItem(itemList, slot, item)
  120.     -- # items we need to transfer
  121.     local count = itemList[item.name]
  122.     -- try to transfer the items
  123.     local itemsTransfered = chest.pushItems(peripheral.getName(orb), slot, count)
  124.     -- number of items left required to craft the recipe
  125.     local itemsLeft = count - itemsTransfered
  126.     -- if 0 then we have enough of this item and can stop searching for it,
  127.     -- otherwise set to the amount left required to craft
  128.     if itemsLeft <= 0 then
  129.         itemList[item.name] = nil
  130.     else
  131.         itemList[item.name] = itemsLeft
  132.     end
  133. end
  134.  
  135. -- transfers the list of items from the chest to the orb
  136. -- this will remove items from the list
  137. function transferItems(itemList)
  138.     -- loop until all items are transfered
  139.     while next(itemList) ~= nil do
  140.         -- check every slot in the chest for any of the items we need to craft the current recipe
  141.         for slot, item in pairs(chest.list()) do
  142.             if itemList[item.name] ~= nil then
  143.                 transferItem(itemList, slot, item)
  144.             end
  145.         end
  146.     end
  147. end
  148.  
  149. -- constantly check the chest for items, 1 second sleeps between
  150. function waitForItems()
  151.     while cur_state == STATE_NONE do
  152.         for slot, item in pairs(chest.list()) do
  153.             cur_state = determineStateFromItem(item.name)
  154.             if cur_state ~= STATE_NONE then
  155.                 break
  156.             end
  157.         end
  158.         -- prevent looping forever with zero delay!
  159.         if cur_state == STATE_NONE then
  160.             os.sleep(1)
  161.         end
  162.     end
  163. end
  164.  
  165. -- print out program info
  166. print("Running with recipies:")
  167. printed_recipes = {}
  168.  
  169. for k, v in pairs(state_from_item_table) do
  170.     if printed_recipes[v] == nil then
  171.         printed_recipes[v] = true
  172.         print(("\t%s = {"):format(v))
  173.         for item, count in pairs(state_item_recipies[v]) do
  174.             print(("\t\t%d\t%s"):format(count, item))
  175.         end
  176.         print("\t}")
  177.     end
  178. end
  179. print("System started successfully!")
  180. print("")
  181. print("--------------------------------------")
  182. print("-- Mr. Fancy Dan's Crafting Manager --")
  183. print("--------------------------------------")
  184. print("")
  185.  
  186. -- main loop
  187. while true do
  188.     if state_item_recipies[cur_state] ~= nil then
  189.         -- if we are in a state which has a recipe
  190.         local items = table.clone(state_item_recipies[cur_state])
  191.         -- transfer the required items
  192.         transferItems(items)
  193.         -- then wait for the reset signal which tells us we are done crafting that item
  194.         waitForReset()
  195.     else
  196.         if cur_state ~= STATE_NONE then
  197.             print(("Unknown state '%d' detected"):format(cur_state))
  198.         else
  199.             -- if we don't have a recipe we are in the none state, so we should wait for the chest to
  200.             -- have an item which we can use to craft
  201.             waitForItems()
  202.         end
  203.     end
  204.     os.sleep(1)
  205. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement