Advertisement
Guest User

pump.lua

a guest
Nov 15th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. local rbt = require("robot")
  2. local cmp = require("component")
  3. local evt = require("event")
  4. local os = require("os")
  5. local math = require("math")
  6. local computer = require("computer")
  7. local coroutine = require("coroutine")
  8.  
  9. -- slot 1: tesseract
  10. -- slot 2: charger
  11. -- slot 3: redstone block
  12.  
  13. rechargeThreshold = 0.50
  14. fullRatio = 0.95
  15. path = {}
  16. x, y, z = 0, 0, 0
  17.  
  18. -- move robot forward n spaces, track motion for RTLS
  19. function forward(n)
  20.   if n == nil then n=1 end
  21.   for i = 1,n do
  22.     if rbt.forward() then
  23.       path[#path+1] = "forward"
  24.     end
  25.   end
  26. end
  27.  
  28. -- move robot up n w/ rtls
  29. function up(n)
  30.   if n == nil then n=1 end
  31.   for i = 1,n do
  32.     if rbt.up() then
  33.       path[#path+1] = "up"
  34.     end
  35.   end
  36. end
  37.  
  38. -- move robot down n w/ rtls
  39. function down(n)
  40.   if n == nil then n=1 end
  41.   for i = 1,n do
  42.     if rbt.down() then
  43.       path[#path+1] = "down"
  44.     end
  45.   end
  46. end
  47.  
  48. -- turn robot n times, track motion for RTLS
  49. function left(n)
  50.   if n == nil then n=1 end
  51.   for i = 1,n do
  52.     if rbt.turnLeft() then
  53.       path[#path+1] = "left"
  54.     end
  55.   end
  56. end
  57.  
  58. -- turn robot n times, track motion for RTLS
  59. function right(n)
  60.   if n == nil then n=1 end
  61.   for i = 1,n do
  62.     if rbt.turnRight() then
  63.       path[#path+1] = "right"
  64.     end
  65.   end
  66. end
  67.  
  68. undo = {right = rbt.turnLeft, left = rbt.turnRight,
  69. up = rbt.down, down = rbt.up,
  70. forward = rbt.back}
  71.  
  72. -- Return To Launch Site, move robot back along
  73. -- path traveled; return to starting location
  74. function RTLS()
  75.   while #path > 0 do
  76.     while undo[path[#path]]()==false do
  77.       rbt.turnAround()
  78.       rbt.swing()
  79.       rbt.turnAround()
  80.     end
  81.     path[#path] = nil
  82.   end
  83. end
  84.  
  85. -- reset RTLS, clear RTLS stack; set new starting
  86. -- location
  87. function rRTLS()
  88.   path = {}
  89. end
  90.  
  91. function forwardBreak()
  92.   if rbt.detect() then
  93.     rbt.swing()
  94.   end
  95.   rbt.forward()
  96. end
  97.  
  98.  
  99. -- ensure XxYxZ free space, with robot in the 0, 0, 0
  100. -- corner, xd being in front and zd being to the right
  101. function ensureEmpty(xd, yd, zd)
  102.   xd = xd - 1
  103.   for y = 1,yd do
  104.     for r = 1,zd do
  105.       for c = 1,xd do
  106.         forwardBreak()
  107.       end
  108.       rbt.turnLeft()
  109.       rbt.turnLeft()
  110.       for c = 1,xd do
  111.         rbt.forward()
  112.       end
  113.       rbt.turnLeft()
  114.       forwardBreak()
  115.       rbt.turnLeft()
  116.     end
  117.    
  118.     rbt.turnLeft()
  119.     for r = 1,zd do
  120.       rbt.forward()
  121.     end
  122.     rbt.turnRight()
  123.  
  124.     if rbt.detectUp() then
  125.       rbt.swingUp()
  126.     end
  127.     rbt.up()  
  128.   end
  129.   for y = 1,yd do
  130.     rbt.down()
  131.   end
  132. end
  133.  
  134. -- Get level in tanks
  135. function tankLevel()
  136.   return cmp.tank_controller.getTankLevelInSlot(4)+cmp.tank_controller.getTankLevelInSlot(8)
  137. end
  138.  
  139. -- Get tank capacity
  140. function tankCapacity()
  141.   return cmp.tank_controller.getTankCapacityInSlot(4)+cmp.tank_controller.getTankCapacityInSlot(8)
  142. end
  143.  
  144. -- RTLS, empty tanks, get power
  145. function reset()
  146.   RTLS()
  147.   rRTLS()
  148.   rbt.turnAround()
  149.   while tankLevel() > 0 do
  150.     rbt.select(4)
  151.     if cmp.tank_controller.getTankLevelInSlot(4) == 0 then
  152.       rbt.select(8)
  153.     end
  154.     cmp.tank_controller.drain()
  155.     rbt.fill()
  156.   end
  157.   while computer.energy()/computer.maxEnergy() < fullRatio do
  158.     os.sleep(3)
  159.   end
  160.   rbt.turnAround()
  161. end
  162.  
  163. -- BEGIN NON FUNCTION CODE --
  164. cmp.chunkloader.setActive(true)
  165. rbt.selectTank(1)
  166.  
  167. -- Move to get lava
  168. while true do
  169.   print(tankLevel().." mB of lava in tank")
  170.   -- check tank level and empty if necesary
  171.   if tankLevel() > tankCapacity()*0.95 then
  172.     -- empty tank into tesseract
  173.     print("Dumping tank")
  174.     reset()
  175.   end
  176.   -- power level check
  177.   if computer.energy()/computer.maxEnergy() < rechargeThreshold then
  178.     -- Wait for charging
  179.     reset()
  180.   end
  181.   -- check for obstacle before moving
  182.   if rbt.detect() or math.random() < 0.025 then
  183.     -- deal with the obstacle - this happens as soon as we hit the other side of the lake
  184.     --
  185.     if math.random() < 0.5 then
  186.       left()
  187.     else
  188.       right()
  189.     end
  190.     goto loopEnd
  191.   end
  192.   forward()
  193.   rbt.drainDown()
  194.   rbt.select(4)
  195.   if cmp.tank_controller.getTankLevelInSlot(4) == cmp.tank_controller.getTankCapacityInSlot(4) then
  196.     rbt.select(8)
  197.   end
  198.   cmp.tank_controller.fill()
  199.   ::loopEnd::
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement