Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Moving with coordinates API
- North = 0
- East = 1
- South = 2
- West = 3
- do
- local x, y, z, direction
- local startX, startY, startZ, startDirection
- local xDiff = { 1, 0, -1, 0 }
- local zDiff = { 0, 1, 0, -1 }
- -- Old for default setup
- --local xDiff = { 0, 1, 0, -1 }
- --local zDiff = { -1, 0, 1, 0 }
- -- Initialize coordinates
- function setup(newX, newY, newZ, newDirection)
- if (newX == nil and newY == nil and newZ == nil and newDirection == nil) then
- local file = fs.open("location", "r")
- x = tonumber(file.readLine())
- y = tonumber(file.readLine())
- z = tonumber(file.readLine())
- direction = tonumber(file.readLine())
- file.close()
- local file = fs.open("startlocation", "r")
- startX = tonumber(file.readLine())
- startY = tonumber(file.readLine())
- startZ = tonumber(file.readLine())
- startDirection = tonumber(file.readLine())
- file.close()
- elseif (newX == nil or newY == nil or newZ == nil or newDirection == nil) then
- logger.error("Need correct coordinates and direction")
- else
- x = 0
- y = 0
- z = 0
- direction = 0
- startX = newX
- startY = newY
- startZ = newZ
- startDirection = newDirection
- end
- end
- function writeToFile()
- local file = fs.open("location", "w")
- file.writeLine(x)
- file.writeLine(y)
- file.writeLine(z)
- file.writeLine(direction)
- file.flush()
- file.close()
- end
- function calculatePoint(toX, toY, toZ)
- newX = startX + xDiff[startDirection] * toX
- newY = startY + toY
- newZ = startZ + zDiff[startDirection] * toZ
- newDirection = (direction - startDirection + 4) % 4
- return newX, newY, newZ, newDirection
- end
- function getLocation()
- return x, y, z, direction;
- end
- function getRealLocation()
- return calculatePoint(x, y, z, direction);
- end
- function up()
- if (turtle.up()) then
- y = y + 1
- writeToFile()
- logger.debug("Moved to "..x.." "..y.." "..z)
- return true
- else
- return false
- end
- end
- function down()
- if (turtle.down()) then
- y = y - 1
- writeToFile()
- logger.debug("Moved to "..x.." "..y.." "..z)
- return true
- else
- return false
- end
- end
- function turnRight()
- if (turtle.turnRight()) then
- direction = direction + 1
- if direction == 4 then
- direction = North
- end
- writeToFile()
- logger.debug("Moved to "..x.." "..y.." "..z)
- return true
- else
- return false
- end
- end
- function turnLeft()
- if (turtle.turnLeft()) then
- direction = direction - 1
- if (direction == -1) then
- direction = West
- end
- writeToFile()
- logger.debug("Moved to "..x.." "..y.." "..z)
- return true
- else
- return false
- end
- end
- function turn(side)
- while side ~= direction do
- if side - direction == 1 or side - direction == -3 then
- turnRight()
- else
- turnLeft()
- end
- end
- end
- function forward()
- if (turtle.forward()) then
- print(xDiff[direction])
- x = x + xDiff[direction + 1]
- z = z + zDiff[direction + 1]
- writeToFile()
- logger.debug("Moved to "..x.." "..y.." "..z)
- return true
- else
- return false
- end
- end
- function back()
- if (turtle.back()) then
- x = x - xDiff[direction + 1]
- z = z - zDiff[direction + 1]
- writeToFile()
- logger.debug("Moved to "..x.." "..y.." "..z)
- return true
- else
- return false
- end
- end
- function safeMove(toX, toY, toZ)
- while toY > y do
- up()
- end
- while toY < y do
- down()
- end
- if toX > x then
- turn(North)
- while toX > x do
- forward()
- end
- end
- if toX < x then
- turn(South)
- while toX < x do
- forward()
- end
- end
- if toZ > z then
- turn(East)
- while toZ > z do
- forward()
- end
- end
- if toZ < z then
- turn(West)
- while toZ < z do
- forward()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement