Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- A module providing:
- - Relative and absolute movement
- - Refuelling support
- -
- ]]--
- --[[
- dig: Digs a space before moving forward. NOTE: Dig is highly inefficient for moving backwards.
- tunnel: Ensures to dig space above and below for players.
- direct: UNIMPLEMENTED - Take straight line to location.
- ]]--
- local self = {}
- self.mode = {dig = true, tunnel = true, direct = false}
- self.dirPreference = {"x","y","z"}
- pos = vector.new(0,0,0)
- -- heading = 0: +x
- -- heading = 1: +y
- heading = 0
- function self.getHeading()
- return heading
- end
- function getPos()
- return pos
- end
- function self.faceDir(dir)
- self.turn(dir - heading)
- end
- function self.zeroHeading(heading)
- local oldHeading = heading
- heading = 0
- return oldHeading
- end
- function self.turn(dir)
- dir = dir % 4
- if dir == 3 then
- turtle.turnRight()
- else
- for i = 1,dir do
- turtle.turnRight()
- end
- end
- heading = (heading + dir) % 4
- end
- function forward()
- if self.mode.dig then
- turtle.dig()
- end
- if turtle.forward() then
- if self.mode.tunnel then
- turtle.digUp()
- turtle.digDown()
- end
- return true
- else
- return false
- end
- end
- function up()
- if self.mode.dig then
- turtle.digUp()
- end
- return turtle.up()
- end
- function down()
- if self.mode.dig then
- turtle.digDown()
- end
- return turtle.down()
- end
- function self.moveForward(n)
- n = n or 1
- local dist = 0
- local oldHeading = heading
- -- Face backwards and move forward.
- if n < 0 then
- self.turn(2)
- n = -n
- end
- for i = 1,n do
- -- Takes care of direction and sign.
- if forward() then
- if heading % 2 == 0 then
- pos.x = pos.x + heading - 1 -- heading = 0 or 2
- else
- pos.y = pos.y + heading - 2 -- heading = 1 or 3
- end
- dist = dist + 1
- else
- return dist
- end
- end
- face(oldHeading)
- end
- function self.moveVert(n)
- n = n or 1
- local dist = 0
- if n > 0 then
- for i = 1,n do
- if up() then
- pos.z = pos.z + 1
- dist = dist + 1
- else
- return dist
- end
- end
- else
- for i = 1,-n do
- if down() then
- pos.z = pos.z - 1
- dist = dist - 1
- else
- return dist
- end
- end
- end
- end
- function self.moveTo(vect)
- oldHeading = heading
- for dir = 1,#self.dirPreference do
- if self.dirPreference[dir] == "x" then
- self.faceDir(0)
- self.moveForward(vect.x)
- elseif self.dirPreference[dir] == "y" then
- self.faceDir(1)
- self.moveForward(vect.y)
- elseif self.dirPreference[dir] == "z" then
- self.moveVert(vect.z)
- else
- print("Error! self.dirPreference is corrupted")
- os.exit()
- end
- end
- self.faceDir(oldHeading)
- end
- return self
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement