Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function DemandLocation()
- local pos = nil
- while (pos == nil) do
- pos = vector.new(gps.locate())
- end
- return pos
- end
- function DirString()
- if (Move.Dir == Move.DIR_NORTH) then
- return "north"
- elseif (Move.Dir == Move.DIR_EAST) then
- return "east"
- elseif (Move.Dir == Move.DIR_SOUTH) then
- return "south"
- elseif (Move.Dir == Move.DIR_WEST) then
- return "west"
- else
- return nil
- end
- end
- function Face(direction)
- while (Move.Dir ~= direction) do
- TurnRight()
- end
- end
- function ForceDown()
- while (TryDown() ~= true) do
- turtle.digDown()
- end
- end
- function ForceForward()
- while (TryForward() ~= true) do
- turtle.dig()
- end
- end
- function ForceUp()
- while (TryUp() ~= true) do
- turtle.digUp()
- end
- end
- function Initialize()
- Move.DIR_NORTH = 0
- Move.DIR_EAST = 1
- Move.DIR_SOUTH = 2
- Move.DIR_WEST = 3
- local pos1 = DemandLocation()
- ForceForward()
- local pos2 = DemandLocation()
- local delta = pos2 - pos1
- if (delta.z == -1) then
- Move.Dir = Move.DIR_NORTH
- elseif (delta.x == 1) then
- Move.Dir = Move.DIR_EAST
- elseif (delta.z == 1) then
- Move.Dir = Move.DIR_SOUTH
- elseif (delta.x == -1) then
- Move.Dir = Move.DIR_WEST
- end
- MoveTo(pos1)
- end
- function MoveTo(newPos)
- local currPos = DemandLocation()
- local delta = newPos - currPos
- alignEastWest(delta)
- alignNorthSouth(delta)
- alignUpDown(delta)
- end
- function TryDown()
- return turtle.down()
- end
- function TryForward()
- return turtle.forward()
- end
- function TryUp()
- return turtle.up()
- end
- function TurnLeft()
- turtle.turnLeft()
- Move.Dir = (Move.Dir + 3) % 4
- end
- function TurnRight()
- turtle.turnRight()
- Move.Dir = (Move.Dir + 1) % 4
- end
- function alignEastWest(delta)
- if (delta.x < 0) then
- Face(Move.DIR_WEST)
- doMultiple(ForceForward, 0 - delta.x)
- elseif (delta.x > 0) then
- Face(Move.DIR_EAST)
- doMultiple(ForceForward, delta.x)
- end
- end
- function alignNorthSouth(delta)
- if (delta.z < 0) then
- Face(Move.DIR_NORTH)
- doMultiple(ForceForward, 0 - delta.z)
- elseif (delta.z > 0) then
- Face(Move.DIR_SOUTH)
- doMultiple(ForceForward, delta.z)
- end
- end
- function alignUpDown(delta)
- if (delta.y < 0) then
- doMultiple(ForceDown, 0 - delta.y)
- elseif (delta.y > 0) then
- doMultiple(ForceUp, delta.y)
- end
- end
- function doMultiple(func, count)
- for i = 1, count do
- func()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement