Advertisement
SilentWarrior

altar

Nov 23rd, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.69 KB | None | 0 0
  1. -- https://github.com/chuesler/computercraft-programs/blob/master/bloodmagic-altar/altar
  2.  
  3. local fromAltarToTurtleDirection = "east"
  4. local fromChestToTurtleDirection = "west"
  5.  
  6. local altar = peripheral.find("tealtar")
  7. local chest = peripheral.find("chest") or peripheral.find("iron")
  8.  
  9. if not altar then
  10.   error("Altar not found")
  11. end
  12.  
  13. if not chest then
  14.   error("Chest not found")
  15. end
  16.  
  17. local bOrbString = "BloodOrb"
  18. ---------------------------------------------------------------------------------------------------------------
  19. local recipe = {
  20. -- default recipe
  21.   { output = 'AWWayofTime:blankSlate',              amount =  64,   essence =  1000, input = 'minecraft:stone' },
  22. -- FTB Infinity Evolved (Expert)
  23. --  { output = 'AWWayofTime:blankSlate',                amount =  64,   essence =  1000, input = 'Thaumcraft:blockCosmeticSolid:6' },
  24.   { output = 'AWWayofTime:reinforcedSlate',         amount = 128,   essence =  2000, input = 'AWWayofTime:blankSlate' },
  25.   { output = 'AWWayofTime:imbuedSlate',             amount = 128,   essence =  5000, input = 'AWWayofTime:reinforcedSlate' },
  26.   { output  = 'AWWayofTime:demonicSlate',           amount =  64,   essence = 15000, input = 'AWWayofTime:imbuedSlate' },
  27.   { output = 'AWWayofTime:bloodMagicBaseItems:27',  amount =  64,   essence = 30000, input = 'AWWayofTime:demonicSlate' },
  28.   { output = bOrbString,                            amount = -1,    essence =   0, input = bOrbString  }
  29. }
  30. ---------------------------------------------------------------------------------------------------------------
  31. -- vvvv ##################### Altar related functions ###################### vvvv
  32.  
  33. function getAltarBloodLevel()
  34.   local tank = altar.getTankInfo()
  35.   return tank[1]["contents"]["amount"]
  36. end
  37.  
  38. function getItemInAltar()
  39.   return altar.getStackInSlot(1) or nil
  40. end
  41.  
  42. function altarHasOrb()
  43.   local item = getItemInAltar()
  44.   if item ~= nil and isOrb(item) then
  45.     return true
  46.   else
  47.     return false
  48.   end
  49. end
  50.  
  51. -- ^^^^ ##################### Altar related functions ###################### ^^^^
  52. -- vvvv ##################### Chest related functions ###################### vvvv
  53. local chestContent = {}
  54.  
  55. function updateChestContent()
  56.   chestContent = {}
  57.   for slot = 1, chest.getInventorySize() do
  58.     local stack = chest.getStackInSlot(slot)
  59.     local id = getItemId(stack)
  60.  
  61.     if stack then
  62.       if chestContent[id] then
  63.         chestContent[id].amount = chestContent[id].amount + stack.qty
  64.       else
  65.         chestContent[id] = { amount = stack.qty, slots={} }
  66.       end
  67.       table.insert(chestContent[id].slots, { slot=i, size=stack.qty })
  68.     end
  69.   end
  70. end
  71.  
  72. function showChestContent()
  73.   for itemId, db in pairs(chestContent) do
  74.     print(itemId.. " | "..tostring(db.amount))
  75.   end
  76. end
  77.  
  78. function getAmountInChest(itemId)
  79.   if chestContent[itemId] then
  80.     return chestContent[itemId].amount
  81.   else
  82.     return 0
  83.   end
  84. end
  85.  
  86. function getItemSlotInChest(partOfItemID)
  87.   for i=1, chest.getInventorySize() do
  88.     local stack = chest.getStackInSlot(i)
  89.     if stack and string.find(getItemId(stack), partOfItemID) then
  90.       return i
  91.     end
  92.   end
  93.   return 0
  94. end
  95.  
  96. function chestHasOrb()
  97.   return getItemSlotInChest(bOrbString) ~= 0
  98. end
  99.  
  100. -- ^^^^ ##################### Chest related functions ###################### ^^^^
  101. -- vvvv ##################### Misc functions ############################### vvvv
  102. function debug(text)
  103.   local debug = true
  104.   if text ~= nil and debug then
  105.     term.setTextColor(colors.blue)
  106.     print(">>> "..text)
  107.     term.setTextColor(colors.white)
  108.   end
  109. end
  110.  
  111. function pstatus(text)
  112.   if text ~= nil then
  113.     term.setTextColor(colors.yellow)
  114.     print(" > "..text)
  115.     term.setTextColor(colors.white)
  116.   end
  117. end
  118.  
  119. function getItemId(stack)
  120.   if stack then
  121.     if stack.dmg ~= nil and stack.dmg ~= 0 then
  122.       return stack.id .. ":" .. stack.dmg
  123.     else
  124.       return stack.id
  125.     end
  126.   else
  127.     return nil
  128.   end
  129. end
  130.  
  131. function isOrb(item)
  132.   return string.find(getItemId(item), bOrbString)
  133. end
  134.  
  135. function moveItemFromChestToAltar(slot, amount)
  136.   chest.pushItem(fromChestToTurtleDirection, slot, amount or 1)
  137.   altar.pullItem(fromAltarToTurtleDirection, 1)
  138. end
  139.  
  140. function moveItemFromAltarToChest()
  141.   altar.pushItem(fromAltarToTurtleDirection, 1)
  142.   chest.pullItem(fromChestToTurtleDirection, 1)
  143. end
  144.  
  145. -- ^^^^ ##################### Misc functions ###################### ^^^^
  146.  
  147. function getNextRecipe()
  148.   for _,r in pairs(recipe) do
  149.     --debug(r.output.."|"..r.amount)
  150.     debug(r.input .." in Chest:"..tostring(getAmountInChest(r.input)))
  151.     if getAmountInChest(r.input) > 0 then
  152.       if r.amount > getAmountInChest(r.output) then
  153.         return r
  154.       end
  155.     end
  156.   end
  157.   return nil
  158. end
  159.  
  160. local c = 0
  161. function dummy()
  162.   c = c + 1
  163.   if c == 1 then
  164.     return "|"
  165.   end
  166.   if c == 2 then
  167.     return "/"
  168.   end
  169.   if c == 3 then
  170.     return "-"
  171.   end
  172.   if c == 4 then
  173.     c = 0
  174.     return "\\"
  175.   end
  176. end
  177.  
  178. local crafting  = false
  179. local recipe =  nil
  180.  
  181. function showStatus()
  182.   term.clear()
  183.   term.setCursorPos(1,1)
  184.   print("BloodAltar automation "..dummy())
  185.  
  186.   if recipe then
  187.     pstatus("Blood Level: ("..tostring(getAltarBloodLevel()).."/"..tostring(recipe.essence)..")")
  188.     pstatus("Input: "..recipe.input)
  189.     pstatus("  -> ("..tostring(getAmountInChest(recipe.input)).." remaining)")
  190.     pstatus("Output: "..recipe.output)
  191.     pstatus("  -> ("..tostring(getAmountInChest(recipe.output)).."/"..tostring(recipe.amount)..")")
  192.   else
  193.     pstatus("no valid crafting recipe with ingredients found")
  194.   end
  195.  
  196.   if crafting then
  197.     pstatus("Crafting in progress")
  198.   else
  199.     pstatus("NOT crafting")
  200.   end
  201. end
  202.  
  203. function loop()
  204.   updateChestContent()
  205.   if not crafting then
  206.     recipe = getNextRecipe()
  207.   end
  208.  
  209.   showStatus()
  210.  
  211.   if recipe ~= nil then
  212.     -- "valid" recipe - check altar
  213.     if getItemInAltar() then -- ### Altar not empty
  214.    
  215.        if getItemId(getItemInAltar()) == recipe.output then
  216.          -- ### Item in altar equals output in recipe -> take it out
  217.          moveItemFromAltarToChest()
  218.          crafting = false
  219.        else
  220.          if recipe.input ~= bOrbString and isOrb(getItemInAltar()) then
  221.            -- ### recipe is not orb but orb in altar -> remove it
  222.            moveItemFromAltarToChest()
  223.          end
  224.        end
  225.     else -- ### Altar empty
  226.       if getAltarBloodLevel() >= recipe.essence then
  227.          moveItemFromChestToAltar( getItemSlotInChest(recipe.input) )
  228.          crafting = true
  229.       end
  230.     end  
  231.   end
  232. end
  233.  
  234. function run()
  235.   os.queueEvent("timer")
  236.   local stop = false
  237.   while not stop do
  238.     local ev, p1, p2, p3 = os.pullEvent()
  239.     if ev == "timer" then
  240.       os.startTimer(0.25)
  241.       loop()
  242.     end
  243.     if ev == "char" and p1 == "t" then
  244.       stop = true
  245.     end
  246.   end
  247. end
  248. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement