Advertisement
Guest User

pump.lua

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