Advertisement
gelatine87

Energized Orb Autocrafting

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