public
Created

ComputerCraft turtle script for leveling a rectangular area

  • Download Gist
level.lua
Lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
-- Helper function for looping over a range
function range(limit)
local function seq(state, x)
if (x >= limit) then
return nil
else
return x + 1
end
end
return seq, nil, 0
end
 
-- Cover an area
function coverArea(w)
local X,Y = w.size.x,w.size.y
 
-- Prepare for first row
w.forward()
w.right()
 
-- Loop over each row
for y in range(Y) do
-- Loop over each column
for x in range(X) do
-- Do the callback for each point
w.callback()
 
-- Unless we have completed the row
if x ~= X then
-- Move forward
w.forward()
end
end
-- Unless we have completed all rows
if y ~= Y then
-- Move to next row
if y%2 == 1 then
w.left()
w.forward()
w.left()
else
w.right()
w.forward()
w.right()
end
end
end
 
-- Move back to starting point
if Y%2 == 1 then
w.left()
w.left()
for x in range(X-1) do w.forward() end
end
w.left()
for y in range(Y) do w.forward() end
w.right()
w.right()
end
 
-- Persistent forward function
function forward()
while not turtle.forward() do
sleep(0.5)
turtle.dig()
end
end
 
-- Level wherever the turtle is
function level()
-- How far down we have gone
local depth = 0
 
-- Make sure we have an empty slot for picking up junk
-- This is an attempt to not mess up the convention by
-- the blocks we dig that are picked up. Not 100%
-- reliable still...
turtle.select(9)
turtle.drop()
 
-- Move down to first block we can find
while not turtle.detectDown() and turtle.down() do
depth = depth + 1
end
 
-- Remove top block
turtle.digDown()
turtle.down()
depth = depth + 1
 
-- Dig down to first non-dirt block we can find
turtle.select(1)
while (turtle.compareDown() and turtle.digDown() and turtle.down()) or turtle.down() do
depth = depth + 1
end
 
-- Start filling
while depth > 0 do
-- Until we move up 1 successfully
while not turtle.up() do
-- Try dig upwards and wait a bit
turtle.digUp()
sleep(0.5)
end
depth = depth - 1
-- As long as we have something to place
if selectSource(depth) > 0 then
-- Place it below us
turtle.placeDown()
end
end
end
 
-- Returns the count of an appropriate slot
function selectSource(depth)
-- If we're at the top
if depth==1 then
-- Select slot 1 and return its count
turtle.select(1)
return turtle.getItemCount(1)
-- Otherwise
else
-- Move through the rest of the slots
for i=2,9 do
turtle.select(i)
local count = turtle.getItemCount(i)
-- Until we find one with something in it
if count > 0 then
return count
end
end
end
return 0
end
 
-- Program starter
local arg = {...}
if #arg == 2 then
coverArea({
size = {
x = tonumber(arg[1]),
y = tonumber(arg[2]),
},
forward = forward,
left = turtle.turnLeft,
right = turtle.turnRight,
callback = level,
})
else
print("Usage: level x y")
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.