Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- function dirHelper1(x,z)
- local x2, y2, z2 = gps.locate(3)
- turtle.back()
- local dx = x - x2
- local dz = z - z2
- if (dx == 0) then
- if (dz > 0) then
- print("Dir: 0")
- return 0
- else
- print("Dir: 2")
- return 2
- end
- else
- if (dx > 0) then
- print("Dir: 3")
- return 3
- else
- print("Dir: 1")
- return 1
- end
- end
- end
- function left()
- turtle.turnLeft()
- turtleDIR = math.abs(turtleDIR -1) % 4
- end
- function right()
- turtle.turnRight()
- turtleDIR = math.abs(turtleDIR +1) %4
- end
- function getDir()
- if (turtleDIR == nil) then
- print("attempting to determine direction")
- local x,y,z = gps.locate(3)
- if (turtle.forward()) then
- turtleDIR = dirHelper1(x,z)
- else
- turtle.turnLeft()
- if (turtle.forward()) then
- turtleDIR = dirHelper1(x,z)
- else
- turtle.turnLeft()
- if (turtle.forward()) then
- turtleDIR = dirHelper1(x,z)
- else
- turtle.turnLeft()
- if (turtle.forward()) then
- turtleDIR = dirHelper1(x,z)
- else
- print("I can't figure out where i'm going... craaaash")
- return
- end
- end
- end
- end
- return turtleDIR
- else
- return turtleDIR
- end
- end
- function setDir(d)
- local dir = getDir()
- while (not (d == dir)) do
- right()
- dir = getDir()
- end
- end
- -- 0 = -z, 1 = +x, 2 = +z, 3 = -x
- function deltaMove(dx,dy,dz)
- print("Checking if X direction")
- if (not (dx == 0)) then
- print("There is X")
- if (dx > 0) then
- print("Moving direction 1, +x")
- move(1,math.abs(dx))
- else
- print("Direction 3, -x")
- move(3,math.abs(dx))
- end
- end
- print("Checking if Z direction")
- if (not ( dz == 0)) then
- if (dz > 0) then
- print("Direction 2, +z")
- move(2,math.abs(dz))
- else
- print("Direction 0, -z")
- move(0,math.abs(dz))
- end
- end
- changeHeight(dy)
- end
- function move(dir, dist)
- if (dist == 0) then
- print("Exiting move(dir, dist), dist is 0")
- return
- end
- print("Setting dir to:")
- print(dir)
- setDir(dir)
- for i = 1, dist do
- while (not turtle.forward()) do
- turtle.dig()
- end
- end
- end
- function changeHeight(delta)
- if (delta == 0) then
- return
- end
- local up = true
- if (delta < 0) then
- up = false
- end
- for i = 1, math.abs(delta) do
- if (up) then
- while (not turtle.up()) do
- turtle.digUp()
- end
- else
- while (not turtle.down()) do
- turtle.digDown()
- end
- end
- end
- end
- local nx = tonumber(tArgs[1])
- local ny = tonumber(tArgs[2])
- local nz = tonumber(tArgs[3])
- rednet.open("right")
- local cx, cy, cz = gps.locate(3)
- local dx = nx - cx
- local dy = ny - cy
- local dz = nz - cz
- local fuelCost = math.abs(dx) + math.abs(dy) + math.abs(dz)
- local turtleDIR
- print("FuelCost: " .. fuelCost)
- print("delta's xyz")
- print(dx)
- print(dy)
- print(dz)
- if (turtle.getFuelLevel() < fuelCost) then
- print("Refueling")
- turtle.refuel(fuelCost)
- if (turtle.getFuelLevel() < fuelCost) then
- print("Not enough fuel")
- else
- print("Starting Movements")
- deltaMove(dx,dy,dz)
- end
- else
- print("Starting Movements")
- deltaMove(dx,dy,dz)
- end
Add Comment
Please, Sign In to add comment