Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lib = {}
- lib.vectorFacings = {
- [0] = vector.new(0, 0, -1), -- north (-Z)
- [1] = vector.new(1, 0, 0), -- east (+X)
- [2] = vector.new(0, 0, 1), -- south (+Z)
- [3] = vector.new(-1, 0, 0), -- west (-X)
- }
- function lib.forward()
- moved = turtle.forward()
- if moved then
- position = position + lib.vectorFacings[facing]
- else
- turtle.dig()
- lib.forward()
- end
- end
- function lib.backward()
- moved = turtle.backward()
- if moved then
- position = position + vector.new(0, 1, 0)
- else
- lib.face(math.abs(facing-2)%4)
- turtle.dig()
- lib.face(math.abs(facing-2)%4)
- lib.backward()
- end
- end
- function lib.upward()
- moved = turtle.up()
- if moved then
- position = position + vector.new(0, 1, 0)
- else
- turtle.digUp()
- lib.upward()
- end
- end
- function lib.downward()
- moved = turtle.down()
- if moved then
- position = position + vector.new(0, -1, 0)
- else
- turtle.digDown()
- lib.downward()
- end
- end
- function lib.turn_right()
- turtle.turnRight()
- facing = (facing + 1) % 4
- end
- function lib.turn_left()
- turtle.turnLeft()
- facing = (facing - 1) % 4
- if facing < 0 then
- facing = 3
- end
- end
- function lib.face(direction)
- if facing == direction then return end
- local turn
- if (facing + 1) % 4 == direction then
- turn = lib.turn_right
- else
- turn = lib.turn_left
- end
- repeat
- turn()
- until facing == direction
- end
- function lib.goto(newPosition)
- -- Align on the y-axis
- while position.y < newPosition.y do
- lib.upward()
- end
- while position.y > newPosition.y do
- lib.downward()
- end
- -- Align on the z-axis
- if position.z > newPosition.z then
- lib.face(0) -- towards the negative direction
- elseif position.z < newPosition.z then
- lib.face(2)
- end
- while position.z ~= newPosition.z do
- lib.forward()
- end
- -- Align on the x-axis
- if position.x > newPosition.x then
- lib.face(3) -- towards the negative direction
- elseif position.x < newPosition.x then
- lib.face(1)
- end
- while position.x ~= newPosition.x do
- lib.forward()
- end
- end
- function lib.tunnelBore(length, width, height)
- startPose = position
- startFacing = facing
- lib.forward()
- for i = 1, ((length) * width) do
- gotoHeight = (i % 2 == 1) and position.y + height - 1 or startPose.y
- lib.goto(vector.new(position.x, gotoHeight, position.z))
- if i % width == 0 or i == 1 then
- dir = (((i / width) % 2 == 1)) and lib.vectorFacings[(startFacing - 1) % 4] or lib.vectorFacings[(startFacing + 1) % 4]
- if i == 1 then
- lib.goto(position:add(dir))
- elseif i~= length * width then
- lib.face(startFacing)
- lib.forward()
- end
- else
- lib.goto(position:add(dir))
- end
- end
- lib.goto(startPose)
- lib.face(startFacing)
- end
- function lib.quarryBore(length, width, height)
- startPose = position
- startFacing = facing
- for i = 1, ((length) * height) do
- gotoWidth = (i % 2 == 1) and position:add(lib.vectorFacings[startFacing]:mul(width - 1)) or position:sub(lib.vectorFacings[startFacing]:mul(width - 1))
- lib.goto(gotoWidth)
- if i % width == 0 or i == 1 then
- dir = (((i / width) % 2 == 1)) and lib.vectorFacings[(startFacing - 1) % 4] or lib.vectorFacings[(startFacing + 1) % 4]
- if i == 1 then
- lib.goto(position:add(dir))
- elseif i~= length * height then
- lib.face(startFacing)
- lib.downward()
- end
- else
- lib.goto(position:add(dir))
- end
- end
- lib.goto(startPose)
- lib.face(startFacing)
- end
- return lib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement