229 lines
6.7 KiB
Lua
229 lines
6.7 KiB
Lua
-- Credit/Sources/Inspiration
|
|
-- https://tweaked.cc/
|
|
-- https://www.reddit.com/r/ComputerCraft/comments/v7vdur/im_confused_with_vectors/
|
|
|
|
-- Variable declarations for movement.
|
|
local currentPosition = vector.new(0, 0, 0)
|
|
local chestPosition = vector.new(0, 0, 0)
|
|
-- Direction turtle is facing on placement is considered facing direction 0.
|
|
local facing = 0
|
|
local facingDirections = {
|
|
[0] = "forwards", -- Right
|
|
[1] = "right", -- Left
|
|
[2] = "backwards", -- Forwards
|
|
[3] = "left" -- Backwards
|
|
}
|
|
local checkpointPosition = vector.new(0, 0, 0)
|
|
local movementDirections = {
|
|
["right"] = vector.new(1, 0, 0), -- Right
|
|
["left"] = vector.new(-1, 0, 0), -- Left
|
|
["up"] = vector.new(0, 1, 0), -- Up
|
|
["down"] = vector.new(0, -1, 0), -- Down
|
|
["forwards"] = vector.new(0, 0, 1), -- Forwards
|
|
["backwards"] = vector.new(0, 0, -1) -- Backwards
|
|
}
|
|
local isGoingHome = false
|
|
-- Variable declaration for tunnel dimensions.
|
|
local tunnelHeight = 0
|
|
local tunnelWidth = 0
|
|
local tunnelDepth = 0
|
|
local realTunnelHeight = 0 -- Because we are starting at 0 which is technically height "1" we have to subtract 1 from the height.
|
|
local realTunnelWidth = 0 -- Because we are starting at 0 which is technically width "0" we have to subtract 1 from the width.
|
|
|
|
-- Function declarations.
|
|
|
|
-- Prints out the current position/vector.
|
|
local function getCurrentPosition()
|
|
write("Current position: " .. tostring(currentPosition) .. "\n")
|
|
end
|
|
|
|
-- Moves forward once.
|
|
local function forward()
|
|
write("Moving forward...\n")
|
|
if(turtle.detect()) then
|
|
write("Block in front of me. Digging...\n")
|
|
turtle.dig()
|
|
end
|
|
turtle.forward()
|
|
local direction = facingDirections[facing]
|
|
currentPosition = currentPosition + movementDirections[direction]
|
|
getCurrentPosition()
|
|
end
|
|
|
|
-- Turns right once.
|
|
local function turnRight()
|
|
write("Turning right...\n")
|
|
turtle.turnRight()
|
|
facing = (facing + 1) % 4
|
|
write("I am facing "..facingDirections[facing]..".\n")
|
|
end
|
|
|
|
-- Turns left once.
|
|
local function turnLeft()
|
|
write("Turning left...\n")
|
|
turtle.turnLeft()
|
|
facing = (facing - 1) % 4
|
|
write("I am facing "..facingDirections[facing]..".\n")
|
|
end
|
|
|
|
-- Moves up once.
|
|
local function up()
|
|
write("Moving up...\n")
|
|
if(turtle.detectUp()) then
|
|
write("Block above me. Digging...\n")
|
|
turtle.digUp()
|
|
end
|
|
turtle.up()
|
|
currentPosition = currentPosition + movementDirections["up"]
|
|
getCurrentPosition()
|
|
end
|
|
|
|
-- Moves down once.
|
|
local function down()
|
|
write("Moving down...\n")
|
|
if(turtle.detectDown()) then
|
|
write("Block below me. Digging...\n")
|
|
turtle.digDown()
|
|
end
|
|
turtle.down()
|
|
currentPosition = currentPosition + movementDirections["down"]
|
|
getCurrentPosition()
|
|
end
|
|
|
|
-- Turns left or right until facing the correct direction.
|
|
local function face(direction)
|
|
if facingDirections[facing] == direction then return end
|
|
local turn
|
|
if facingDirections[(facing + 1) % 4] == direction then
|
|
turn = turnRight
|
|
else
|
|
turn = turnLeft
|
|
end
|
|
repeat
|
|
turn()
|
|
until facingDirections[facing] == direction
|
|
end
|
|
|
|
local function buildFloor()
|
|
-- If we are not on the floor or we are going home, we don't need to do anything.
|
|
if((currentPosition.y ~= 0) or isGoingHome) then
|
|
return
|
|
end
|
|
if not turtle.detectDown() then
|
|
turtle.select(1)
|
|
turtle.placeDown()
|
|
elseif(currentPosition.z ~= 0) then
|
|
-- Only start floor pattern after we moved forward.
|
|
if((currentPosition.z % 2 > 0 and currentPosition.x % 2 > 0) or (currentPosition.z % 2 == 0 and currentPosition.x % 2 == 0)) then
|
|
-- Chess Pattern for floor.
|
|
turtle.digDown()
|
|
turtle.select(1)
|
|
turtle.placeDown()
|
|
end
|
|
end
|
|
end
|
|
|
|
local function buildCeiling()
|
|
if(currentPosition.y == realTunnelHeight) then
|
|
if not turtle.detectUp() then
|
|
turtle.select(1)
|
|
turtle.placeUp()
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Uses some functions above combined to move to a specified point relative to the turtles position.
|
|
local function goTo(newPosition)
|
|
-- Align on the X axis
|
|
if currentPosition.x > newPosition.x then
|
|
face("left")
|
|
elseif currentPosition.x < newPosition.x then
|
|
face("right")
|
|
end
|
|
while currentPosition.x ~= newPosition.x do
|
|
buildCeiling()
|
|
buildFloor()
|
|
forward()
|
|
end
|
|
|
|
-- Align on the Y axis
|
|
while currentPosition.y > newPosition.y do -- we're above where we want to go
|
|
buildCeiling()
|
|
buildFloor()
|
|
down()
|
|
end
|
|
while currentPosition.y < newPosition.y do -- we're below where we want to go
|
|
buildCeiling()
|
|
buildFloor()
|
|
up()
|
|
end
|
|
|
|
-- Align on the Z axis
|
|
if currentPosition.z > newPosition.z then
|
|
face("backwards")
|
|
elseif currentPosition.z < newPosition.z then
|
|
face("forwards")
|
|
end
|
|
while currentPosition.z ~= newPosition.z do
|
|
buildCeiling()
|
|
buildFloor()
|
|
forward()
|
|
end
|
|
end
|
|
|
|
local function digColumn()
|
|
-- If we on the bottom row, dig up to specified height. Otherwise dig down to Y = 0 (in Vector not Minecraft).
|
|
if(currentPosition.y < realTunnelHeight) then
|
|
repeat
|
|
goTo(currentPosition + movementDirections["up"])
|
|
until currentPosition.y == realTunnelHeight
|
|
else
|
|
repeat
|
|
goTo(currentPosition + movementDirections["down"])
|
|
until currentPosition.y == 0
|
|
end
|
|
end
|
|
|
|
local function digTunnel()
|
|
-- Starting position is always bottom left.
|
|
-- Reapeat as long was we have not reached the specified depth.
|
|
while currentPosition.z < tunnelDepth do
|
|
-- Always move forward for the next tunnel layer.
|
|
goTo(currentPosition + movementDirections["forwards"])
|
|
digColumn()
|
|
-- If we are on the left, dig to the right and vice versa.
|
|
if(currentPosition.x < realTunnelWidth) then
|
|
repeat
|
|
goTo(currentPosition + movementDirections["right"])
|
|
digColumn()
|
|
until currentPosition.x == realTunnelWidth
|
|
else
|
|
repeat
|
|
goTo(currentPosition + movementDirections["left"])
|
|
digColumn()
|
|
until currentPosition.x == 0
|
|
end
|
|
end
|
|
end
|
|
|
|
-- "Main"
|
|
print("How high do you want the tunnel to be?")
|
|
write("Height: ")
|
|
tunnelHeight = tonumber(io.read())
|
|
realTunnelHeight = tunnelHeight-1
|
|
print("How wide do you want the tunnel to be?")
|
|
write("Width: ")
|
|
tunnelWidth = tonumber(io.read())
|
|
realTunnelWidth = tunnelWidth-1
|
|
print("How deep do you want the tunnel to be?")
|
|
write("Depth: ")
|
|
tunnelDepth = tonumber(io.read())
|
|
digTunnel()
|
|
--- Go back "home"/to starting position.
|
|
isGoingHome = true
|
|
goTo(vector.new(0, 0, 0))
|
|
face("forwards")
|
|
|
|
|
|
|