Advertisement
Machuga14

ComputerCraft Fill Blood Alter 1 at a time

May 8th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.44 KB | None | 0 0
  1. local altarSide = "front"
  2. local altarInputChestSide = "WEST"
  3. local altar = peripheral.wrap(altarSide)
  4. local redstoneSignal = "back"
  5. local altarFuelLevelToFillAt = .50 -- The percentage of life pool to begin filling the turtle at.
  6. local modem = peripheral.wrap("left") -- Wrap the modem
  7. local modemPort =25566 -- The port to communicate to
  8. local status = " Running"
  9.  
  10.  
  11. function pullItem()
  12.   altar.pullItem(altarInputChestSide, 1, 1, 1)
  13. end
  14.  
  15. function getItemInAltar()
  16.   return altar.getStackInSlot(1)
  17. end
  18.  
  19. function printItemInAltar()
  20.   local stack = altar.getStackInSlot(1)
  21.  
  22.   for k, v in pairs(stack) do
  23.     print(k.." - ")
  24.     print(v)
  25.   end
  26. end
  27.  
  28. function checkIfTurnedOff()
  29.   if rs.getInput(redstoneSignal) then
  30.     return true
  31.   else
  32.     return false
  33.   end
  34. end
  35.  
  36. function isTable(t) return type(t) == 'table' end
  37.  
  38. function printTankInfos()
  39. local tankinfos = altar.getTankInfo()
  40. for k, v in pairs(tankinfos) do
  41.   write("["..k.."][")
  42.   if isTable(v) then
  43.     write("Table { \r\n")
  44.     for kk, vv in pairs(v) do
  45.       write("  ["..kk.."]")
  46.       if isTable(vv) then
  47.         write("[Table {\r\n")
  48.           for kkk, vvv in pairs(vv) do
  49.             write("    ["..kkk.."][")
  50.             write(vvv)
  51.             write("]\r\n")
  52.           end
  53.         write("    }]")
  54.       else
  55.         write("["..vv.."]")
  56.       end
  57.       write("\r\n")
  58.     end
  59.     write("  }]")
  60.   else
  61.     write(v.."\r\n")
  62.   end
  63. end
  64. write("\r\n")
  65. end -- End Function
  66.  
  67. function getTankLevel() -- Calculates the amount of life pool in the tank
  68.   tankInfos = altar.getTankInfo()
  69.   return tankInfos[1]["contents"]["amount"]
  70. end
  71.  
  72. function getTankCapacity() -- Calculates the capacity of the tank
  73.   tankInfos = altar.getTankInfo()
  74.   return tankInfos[1]["capacity"]
  75. end
  76.  
  77. function printTankStatus()
  78.   print(getTankLevel().." / "..getTankCapacity().."  - "..100 * (getTankLevel() / getTankCapacity()).."%")
  79. end
  80.  
  81. function updateStatus()
  82.   modem.transmit(modemPort, 100, getTankLevel().." / "..getTankCapacity().."  - "..100 * (getTankLevel() / getTankCapacity()).."%    "..status)
  83.  
  84.   local stack = altar.getStackInSlot(1)
  85.   local stackMessage = ""
  86.  
  87.   if stack then
  88.   stackMessage = "Item In Altar: "..stack["display_name"]
  89.   else
  90.   stackMessage = "Altar is Empty"
  91.   end
  92.   modem.transmit(modemPort, 102, stackMessage)
  93. end
  94.  
  95. function sendMessage(message)
  96.   modem.transmit(modemPort, 101, message)
  97.   print(message)
  98. end
  99.  
  100. function checkIfTankCapacityFullEnoughByPercent(percent)
  101.   if getTankLevel() / getTankCapacity() > percent then
  102.     --print("Detected we are full enough"..getTankLevel() / getTankCapacity())
  103.     return true
  104.   else
  105.     --print("Detected we don't have enough fuel"..getTankLevel() / getTankCapacity())
  106.     return false
  107.   end
  108. end
  109.  
  110. function waitForEmptyAltar()
  111.   while getItemInAltar() do
  112.     status = "Altar Currently Infusing"
  113.     updateStatus()
  114.     os.sleep(1)
  115.     updateStatus()
  116.     os.sleep(1)
  117.     updateStatus()
  118.     os.sleep(1)
  119.     updateStatus()
  120.     os.sleep(1)
  121.     updateStatus()
  122.     os.sleep(1)
  123.     print("Detected an item in the alter. Sleeping 5 seconds.")
  124.   end
  125. end
  126.  
  127. function run()
  128.   while true do
  129.     printTankStatus()
  130.     updateStatus()
  131.     while not checkIfTurnedOff() do
  132.       sendMessage("Detected we are turned off. waiting for 5 seconds")
  133.       status = "BloodyTurtle Turned Off"
  134.       updateStatus()
  135.       os.sleep(1)
  136.       updateStatus()
  137.       os.sleep(1)
  138.       updateStatus()
  139.       os.sleep(1)
  140.       updateStatus()
  141.       os.sleep(1)
  142.       updateStatus()
  143.       os.sleep(1)
  144.     end
  145.  
  146.     waitForEmptyAltar() -- Wait until the altar is empty
  147.     updateStatus()
  148.  
  149.     while not checkIfTankCapacityFullEnoughByPercent(altarFuelLevelToFillAt) do
  150.       sendMessage("detected the altar isn't full enough. waiting.")
  151.       status = "Altar Not Full Enough ["..(100 * altarFuelLevelToFillAt).."%] Required"
  152.       updateStatus()
  153.       os.sleep(1)
  154.       updateStatus()
  155.       os.sleep(1)
  156.       updateStatus()
  157.       os.sleep(1)
  158.       updateStatus()
  159.       os.sleep(1)
  160.       updateStatus()
  161.       os.sleep(1)
  162.     end
  163.  
  164.     pullItem() -- Pull the 1st thing in the Altar.
  165.     status = "Running"
  166.     sendMessage("Placed an item in the altar")
  167.     updateStatus()
  168.     os.sleep(1)
  169.     updateStatus()
  170.     os.sleep(1)
  171.     updateStatus()
  172.     os.sleep(1)
  173.     updateStatus()
  174.     os.sleep(1)
  175.     updateStatus()
  176.     os.sleep(1)
  177.   end
  178. end
  179.  
  180. run()
  181.  
  182. write("\r\nPress any key to continue")
  183. os.pullEvent("key")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement