SamzFerg

Computercraft 1.7.10 Alchemy Automation

May 8th, 2019
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. local TURTLE_ID           = 22                    -- ID of Turtle that changes what item is being crafted by the ritual
  2. local WIRELESS_MODEM_SIDE = "top"                 -- Side of Computer Wireless Modem is Placed On
  3. local RITUAL_OUTPUT       = "ender_chest_0"       -- Address of Ritual Output Inventory
  4.  
  5. rednet.open(WIRELESS_MODEM_SIDE)                  -- Opens rednet communication with the wireless modem
  6. local EnderChest = peripheral.wrap(RITUAL_OUTPUT) -- Wraps the Ritual Output as an Inventory
  7.  
  8. local OuputSize  = EnderChest.getInventorySize()  -- Size of output Inventory
  9. local ItemCounts = {}                             -- Table with amount of each item in the inventory
  10.  
  11. local ONE_X_CRAFT_LIMIT  = 64                     -- Lower Bound of Items That Craft 1 at a Time Before They Need To Be Crafted
  12. local TWO_X_CRAFT_LIMIT  = 63                     -- Lower Bound of items That Craft 2 at a Time Before They Need To Be Crafted
  13. local FOUR_X_CRAFT_LIMIT = 61                     -- Lower Bound of items That Craft 4 at a Time Before They Need To Be Crafted
  14.  
  15. --List of Items to Craft
  16. ItemCounts["Simple Catalyst"] = 0
  17. ItemCounts["Strengthened Catalyst"] = 0
  18. ItemCounts["Concentrated Catalyst"] = 0
  19. ItemCounts["Fractured Bone"] = 0
  20. ItemCounts["Magicales"] = 0
  21. ItemCounts["Tenebrae"] = 0
  22. ItemCounts["Terrae"] = 0
  23. ItemCounts["Aether"] = 0
  24. ItemCounts["Sanctus"] = 0
  25.  
  26. OutputSize = 54
  27.  
  28. --Gets the aount of the specified item in the ritual output
  29. function getItemCount(name)
  30.     local count = 0
  31.     for i=1,OutputSize,1 do
  32.         local tmp = EnderChest.getStackInSlot(i)
  33.         if(tmp ~= nil) then
  34.             if(tmp.display_name == name) then
  35.                 count = count + tmp.qty
  36.             end
  37.         end
  38.     end
  39.     return count
  40. end
  41.  
  42.  
  43. --Updates each of the items with how much of each are in the ritual output
  44. function refreshItemCount()
  45.     for k,v in pairs(ItemCounts) do
  46.         ItemCounts[k] = getItemCount(k)
  47.     end
  48. end
  49.  
  50. --Tells Turtle to Stop Crafting Whatever Item is Being Crafted
  51. function stopCraft()
  52.     local msg = {"stop"}
  53.     rednet.send(TURTLE_ID, textutils.serialize(msg))
  54. end
  55.  
  56.  
  57. --Requests Turtle to being Crafting specified item
  58. function requestCraft(item)
  59.     local msg = {"craft", item}
  60.     rednet.send(TURTLE_ID, textutils.serialize(msg))
  61. end
  62.  
  63. --Checks if specified item is below the limit or now
  64. function isBelowLimit(item)
  65.     if(ItemCounts[item] == nil) then                        --returns if the item doesn't exist in list
  66.         return
  67.     end
  68.    
  69.     if(item == "Strengthened Catalyst") then                --Grabs Strengthened Catalyst Seperatly becuase it crafts 2 at once.
  70.         return(ItemCounts[item] < TWO_X_CRAFT_LIMIT)        --Returns if QTY of Strengthened Catalyst is Below Limit For 2x Crafts
  71.     elseif (item == "Fractured Bone") then
  72.         return(ItemCounts[item] < FOUR_X_CRAFT_LIMIT)
  73.     else
  74.         return(ItemCounts[item] < ONE_X_CRAFT_LIMIT)        --Returns if QTY of Strengthened Catalyst is Below Limit For 1x Crafts
  75.     end
  76. end
  77.  
  78.  
  79. local currentCraft = ""  --Holds Name of Currently Crafting Item
  80.  
  81. --Main Program Loop
  82. while (true) do
  83.     refreshItemCount()                       --At the beginning of every loop, refreshes the item count table
  84.  
  85.     if (isBelowLimit(currentCraft) == false) then --if the current crafting item has reached its limists
  86.         print("Restocked ", currentCraft)
  87.         currentCraft = ""
  88.         stopCraft()
  89.         sleep(2)                                  --Has Progam Sleep To give Turtle Time to Remove Item From Altar
  90.     end
  91.    
  92.     if(currentCraft == "") then                   --If there we aren't crafting anything currently
  93.         for k,v in pairs(ItemCounts) do           --Loops Through ItemCount Table to Check Each Item
  94.             if(isBelowLimit(k)) then              --If Item is Below the Limit
  95.                 currentCraft = k                  --Sets Current Craft Variable
  96.                 requestCraft(k)                   --Request Craft From Turtle
  97.                 print("Requesting ", k)
  98.                 break                             --leaves the loops so no more crafts are requested
  99.             end
  100.         end
  101.     end
  102.  
  103.     sleep(1)
  104. end
Advertisement
Add Comment
Please, Sign In to add comment