Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- directions={
- NORTH={0, 1},
- EAST={1, 0},
- SOUTH={0, -1},
- WEST={-1, 0},
- ORDER={"NORTH", "EAST", "SOUTH", "WEST"},
- indexOf=function(self, direction)
- for i, value in ipairs(self.ORDER) do
- if value==direction then
- return i
- end
- end
- end
- }
- directionX, directionY=unpack(directions.NORTH)
- currentDirection=1 --NORTH=1, EAST=2, SOUTH=3, WEST=4
- currentX, currentY, currentZ=0, 0, 0
- function getCurrentCoordinates()
- return currentX, currentY, currentZ
- end
- function getCurrentDirection()
- return directions.ORDER[currentDirection]
- end
- function Stack()
- local stack={
- push=function(self, ...)
- for i, value in ipairs{...} do
- self[#self+1]=value
- end
- end,
- pop=function(self, count)
- local count = count or 1
- if count > #self then
- error("Stack underflow!")
- end
- local values={}
- for i=count, 1, -1 do
- values[#values+1]=table.remove(self)
- end
- return unpack(values)
- end
- }
- return setmetatable({}, {__index = stack})
- end
- function turnRight()
- turtle.turnRight()
- currentDirection=currentDirection+1
- if currentDirection > 4 then
- currentDirection=1
- end
- directionX, directionY=unpack(directions[directions.ORDER[currentDirection]])
- return turnLeft
- end
- function turnLeft()
- turtle.turnLeft()
- currentDirection=currentDirection-1
- if currentDirection < 1 then
- currentDirection=4
- end
- directionX, directionY=unpack(directions[directions.ORDER[currentDirection]])
- return turnRight
- end
- function forward()
- local success=turtle.forward()
- if success==false then
- return false
- end
- currentX=currentX+directionX
- currentY=currentY+directionY
- return true
- end
- function up()
- local success=turtle.up()
- if success==false then
- return false
- end
- currentZ=currentZ+1
- return true
- end
- function down()
- local success=turtle.down()
- if success==false then
- return false
- end
- currentZ=currentZ-1
- return true
- end
- turnAround=function()
- turnRight()
- turnRight()
- end
- function face(direction)
- local destinationDirection=directions:indexOf(direction)
- local offset=destinationDirection-currentDirection
- local turnFunction=turnRight
- if (offset < 1 and offset~=-3) or (offset > 1 and offset==3) then
- turnFunction=turnLeft
- end
- while currentDirection~=destinationDirection do
- turnFunction()
- end
- end
- function move(destinationX, destinationY, destinationZ)
- local dx=destinationX-currentX
- local dy=destinationY-currentY
- local dz=destinationZ-currentZ
- face("NORTH")
- local xDirection="WEST"
- if dx>0 then
- xDirection="EAST"
- end
- local yDirection="SOUTH"
- if dy>0 then
- yDirection="NORTH"
- end
- local zFunction=down
- if dz>0 then
- zFunction=up
- end
- while currentZ~=destinationZ do
- zFunction()
- end
- if dx~=0 then
- face(xDirection)
- while currentX~=destinationX do
- forward()
- end
- end
- if dy~=0 then
- face(yDirection)
- while currentY~=destinationY do
- forward()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement