Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local rbt = require("robot")
- local cmp = require("component")
- local evt = require("event")
- local os = require("os")
- local math = require("math")
- local computer = require("computer")
- local coroutine = require("coroutine")
- -- slot 1: tesseract
- -- slot 2: charger
- -- slot 3: redstone block
- rechargeThreshold = 0.50
- fullRatio = 0.95
- path = {}
- x, y, z = 0, 0, 0
- -- move robot forward n spaces, track motion for RTLS
- function forward(n)
- if n == nil then n=1 end
- for i = 1,n do
- if rbt.forward() then
- path[#path+1] = "forward"
- end
- end
- end
- -- move robot up n w/ rtls
- function up(n)
- if n == nil then n=1 end
- for i = 1,n do
- if rbt.up() then
- path[#path+1] = "up"
- end
- end
- end
- -- move robot down n w/ rtls
- function down(n)
- if n == nil then n=1 end
- for i = 1,n do
- if rbt.down() then
- path[#path+1] = "down"
- end
- end
- end
- -- turn robot n times, track motion for RTLS
- function left(n)
- if n == nil then n=1 end
- for i = 1,n do
- if rbt.turnLeft() then
- path[#path+1] = "left"
- end
- end
- end
- -- turn robot n times, track motion for RTLS
- function right(n)
- if n == nil then n=1 end
- for i = 1,n do
- if rbt.turnRight() then
- path[#path+1] = "right"
- end
- end
- end
- undo = {right = rbt.turnLeft, left = rbt.turnRight,
- up = rbt.down, down = rbt.up,
- forward = rbt.back}
- -- Return To Launch Site, move robot back along
- -- path traveled; return to starting location
- function RTLS()
- while #path > 0 do
- while undo[path[#path]]()==false do
- rbt.turnAround()
- rbt.swing()
- rbt.turnAround()
- end
- path[#path] = nil
- end
- end
- -- reset RTLS, clear RTLS stack; set new starting
- -- location
- function rRTLS()
- path = {}
- end
- function forwardBreak()
- if rbt.detect() then
- rbt.swing()
- end
- rbt.forward()
- end
- -- ensure XxYxZ free space, with robot in the 0, 0, 0
- -- corner, xd being in front and zd being to the right
- function ensureEmpty(xd, yd, zd)
- xd = xd - 1
- for y = 1,yd do
- for r = 1,zd do
- for c = 1,xd do
- forwardBreak()
- end
- rbt.turnLeft()
- rbt.turnLeft()
- for c = 1,xd do
- rbt.forward()
- end
- rbt.turnLeft()
- forwardBreak()
- rbt.turnLeft()
- end
- rbt.turnLeft()
- for r = 1,zd do
- rbt.forward()
- end
- rbt.turnRight()
- if rbt.detectUp() then
- rbt.swingUp()
- end
- rbt.up()
- end
- for y = 1,yd do
- rbt.down()
- end
- end
- -- Get level in tanks
- function tankLevel()
- return cmp.tank_controller.getTankLevelInSlot(4)+cmp.tank_controller.getTankLevelInSlot(8)
- end
- -- Get tank capacity
- function tankCapacity()
- return cmp.tank_controller.getTankCapacityInSlot(4)+cmp.tank_controller.getTankCapacityInSlot(8)
- end
- -- RTLS, empty tanks, get power
- function reset()
- RTLS()
- rRTLS()
- rbt.turnAround()
- while tankLevel() > 0 do
- rbt.select(4)
- if cmp.tank_controller.getTankLevelInSlot(4) == 0 then
- rbt.select(8)
- end
- cmp.tank_controller.drain()
- rbt.fill()
- end
- while computer.energy()/computer.maxEnergy() < fullRatio do
- os.sleep(3)
- end
- rbt.turnAround()
- end
- -- BEGIN NON FUNCTION CODE --
- cmp.chunkloader.setActive(true)
- rbt.selectTank(1)
- -- Move to get lava
- while true do
- print(tankLevel().." mB of lava in tank")
- -- check tank level and empty if necesary
- if tankLevel() > tankCapacity()*0.95 then
- -- empty tank into tesseract
- print("Dumping tank")
- reset()
- end
- -- power level check
- if computer.energy()/computer.maxEnergy() < rechargeThreshold then
- -- Wait for charging
- reset()
- end
- -- check for obstacle before moving
- if rbt.detect() or math.random() < 0.025 then
- -- deal with the obstacle - this happens as soon as we hit the other side of the lake
- --
- if math.random() < 0.5 then
- left()
- else
- right()
- end
- goto loopEnd
- end
- forward()
- rbt.drainDown()
- rbt.select(4)
- if cmp.tank_controller.getTankLevelInSlot(4) == cmp.tank_controller.getTankCapacityInSlot(4) then
- rbt.select(8)
- end
- cmp.tank_controller.fill()
- ::loopEnd::
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement