1
0
mirror of https://github.com/LDDestroier/CC/ synced 2024-11-17 15:24:52 +00:00
ldd-CC/cubedig.lua

148 lines
2.8 KiB
Lua
Raw Normal View History

2018-11-12 19:30:27 +00:00
-- Turtle program that digs a rectangular area.
local tArg = {...}
local dx = tonumber(tArg[1])
local dy = tonumber(tArg[2])
local dz = tonumber(tArg[3])
2019-04-25 03:13:07 +00:00
if dy == 0 then
dy = 1
end
2018-11-12 19:30:27 +00:00
-- relative position, used for printing a graphic
local pos = {
x = 0,
y = 0,
z = 0,
f = 0
}
assert(dx, "Invalid X input.")
assert(dy, "Invalid X input.")
assert(dz, "Invalid X input.")
-- done to offset the fact that the turtle occupies the rectangle it'll dig out, usually
dz = dz - 1
local dig = function(direction)
if direction == -1 then -- down
while turtle.inspectDown() do
2019-04-25 02:44:45 +00:00
turtle.digDown()
end
2018-11-12 19:30:27 +00:00
elseif direction == 1 then -- up
while turtle.inspectUp() do
2019-04-25 02:44:45 +00:00
turtle.digUp()
end
else -- forwards
2018-11-12 19:30:27 +00:00
while turtle.inspect() do
turtle.dig()
end
end
end
local move = function(direction)
if direction == "up" then
turtle.up()
pos.y = pos.y + 1
elseif direction == "down" then
turtle.down()
pos.y = pos.y - 1
elseif direction == "forward" then
turtle.forward()
pos.x = pos.x + math.cos(math.rad(pos.f * 90))
elseif direction == "back" then
2019-04-25 02:44:45 +00:00
turtle.forward()
pos.x = pos.x - math.cos(math.rad(pos.f * 90))
2018-11-12 19:30:27 +00:00
elseif direction == "left" then
turtle.turnLeft()
2018-11-12 19:30:27 +00:00
pos.f = (pos.f - 1) % 4
elseif direction == "right" then
turtle.turnRight()
2018-11-12 19:30:27 +00:00
pos.f = (pos.f + 1) % 4
end
end
2019-04-25 03:13:07 +00:00
local UDdig = function(left, check, right, doNotDigForwards)
if not doNotDigForwards then
dig( 0 )
end
2019-04-25 02:49:04 +00:00
if check > left then
dig( -1 )
end
if check < right then
dig ( 1 )
end
end
local turn = function(dir, doDig, left, check, right)
2018-11-12 19:30:27 +00:00
if dir then
move( "right" )
if doDig then
2019-04-25 02:53:31 +00:00
UDdig( left, check, right )
2018-11-12 19:30:27 +00:00
move( "forward" )
end
move( "right" )
else
move( "left" )
if doDig then
2019-04-25 02:53:31 +00:00
UDdig( left, check, right )
2018-11-12 19:30:27 +00:00
move( "forward" )
end
move( "left" )
end
end
local printData = function()
local dirs = {
[0] = "Forwards",
[1] = "Right",
[2] = "Backwards",
[3] = "Left"
}
term.clear()
term.setCursorPos(1,1)
write("Relative Pos.: (")
write(pos.x .. ", ")
write(pos.y .. ", ")
print(pos.z .. ")")
print("Relative dir.: " .. dirs[pos.f])
print("Fuel: " .. turtle.getFuelLevel())
end
local doTurn = true
2019-04-25 02:44:45 +00:00
if dy > 1 then
2019-04-25 03:13:07 +00:00
dig( 1 )
2019-04-25 02:44:45 +00:00
move( "up" )
elseif dy < 1 then
2019-04-25 03:13:07 +00:00
dig( -1 )
2019-04-25 02:44:45 +00:00
move( "down" )
end
for y = (math.abs(dy) > 1 and 2 or 1), math.abs(dy) do
2019-04-25 03:17:07 +00:00
if (y % 3 == 2) or (math.abs(dy) - y == 1) or (math.abs(dy) == 1) then
2019-04-25 02:44:45 +00:00
for x = 1, dx do
for z = 1, dz do
2019-04-25 03:13:07 +00:00
UDdig(dy / math.abs(dy), y * (dy / math.abs(dy)), dy, false)
2019-04-25 02:44:45 +00:00
move( "forward" )
2019-04-25 03:13:07 +00:00
UDdig(dy / math.abs(dy), y * (dy / math.abs(dy)), dy, true)
2019-04-25 02:44:45 +00:00
printData()
end
2019-04-25 03:13:07 +00:00
turn(doTurn, x < dx, dy / math.abs(dy), y * (dy / math.abs(dy)), dy, false)
2018-11-12 19:30:27 +00:00
printData()
2019-04-25 02:44:45 +00:00
if x < dx then
doTurn = not doTurn
end
2018-11-12 19:30:27 +00:00
end
end
if y ~= math.abs(dy) then
if dy > 0 then
dig( 1 )
move( "up" )
else
dig( -1 )
move( "down" )
end
printData()
end
end