ComputerCraft/digroom.lua
2022-07-02 16:42:07 +02:00

74 lines
1.3 KiB
Lua

local tArgs = { ... }
if table.maxn(tArgs) ~= 3 then
print("This program digs a room in front of the turtle to the right.")
print("Arguments: width, length, height.")
return
end
local width = tArgs[1]
local length = tArgs[2]
local height = tArgs[3]
local up = true
local fw = false
print("Digging room with", width, "width,", length, "length and", height, "height.")
local function digUp()
for i = 2, height do
while not turtle.up() do
turtle.digUp()
end
end
end
local function digDown()
for i = 2, height do
while not turtle.down() do
turtle.digDown()
end
end
end
local function digVertical()
if up then
digUp()
else
digDown()
end
up = not up
end
local function digFw()
while not turtle.forward() do
turtle.dig()
end
end
local function turn()
if fw then
turtle.turnLeft()
else
turtle.turnRight()
end
end
local function refuel()
if turtle.getFuelLevel() <= 4 * height then
if not turtle.refuel() then
print("No fuel!")
end
end
end
refuel()
for i = 1, width do
digFw()
turn()
fw = not fw
for j = 2, length do
digVertical()
digFw()
refuel()
end
digVertical()
turn()
end