257 lines
5.8 KiB
Lua
Raw Normal View History

2023-05-29 02:39:58 +02:00
compareSlotMax = 1 -- all slots up to this one are used for comparing, order the blocks in decreasing probability
2023-05-28 14:07:39 +02:00
xRelPos = 0 -- "positive = forward"
yRelPos = 0 -- "positive = right"
2023-05-29 02:39:58 +02:00
zRelPos = 0 -- "positive = upwards"
2023-05-28 14:07:39 +02:00
facing = {
x = 1,
y = 0
}
holeCounter = {
line = 0,
hole = 0
}
function printPos()
print("At (", xRelPos, ", ", yRelPos, ") facing (", facing.x, ", ", facing.y, ")")
end
-- helper functions for movement
function forward()
if turtle.forward() then
xRelPos = xRelPos + facing.x
yRelPos = yRelPos + facing.y
else
if not turtle.dig() then
turtle.attack()
end
end
end
2023-05-29 02:39:58 +02:00
function upwards()
if turtle.up() then
zRelPos = zRelPos + 1
return true
end
return false
end
function downwards()
if turtle.down() then
zRelPos = zRelPos - 1
return true
end
return false
2023-05-28 14:07:39 +02:00
function turnRight()
turtle.turnRight()
if facing.x == 1 and facing.y == 0 then -- facing forward
facing.x = 0
facing.y = 1
elseif facing.x == 0 and facing.y == 1 then -- facing right
facing.x = -1
facing.y = 0
elseif facing.x == -1 and facing.y == 0 then -- facing backward
facing.x = 0
facing.y = -1
elseif facing.x == 0 and facing.y == -1 then -- facing left
facing.x = 1
facing.y = 0
else
print("turnRight: This should never happen")
end
end
function turnLeft()
turtle.turnLeft()
if facing.x == 1 and facing.y == 0 then -- facing forward
facing.x = 0
facing.y = -1
elseif facing.x == 0 and facing.y == 1 then -- facing right
facing.x = 1
facing.y = 0
elseif facing.x == -1 and facing.y == 0 then -- facing backward
facing.x = 0
facing.y = 1
elseif facing.x == 0 and facing.y == -1 then -- facing left
facing.x = -1
facing.y = 0
else
print("turnLeft: This should never happen")
end
end
function faceTo(x, y)
if (x == facing.x) and (y == facing.y) then
return
end
if (x == 1 and facing.x == -1) or (x == -1 and facing.x == 1) or (y == 1 and facing.y == -1) or (y == -1 and facing.y == 1) then
turnRight()
turnRight()
elseif (facing.y == -1 and x == 1) or (facing.y == 1 and x == -1) or (facing.x == 1 and y == 1) or (facing.x == -1 and y == -1) then
turnRight()
elseif (facing.y == -1 and x == -1) or (facing.y == 1 and x == 1) or (facing.x == 1 and y == -1) or (facing.x == -1 and y == 1) then
turnLeft()
else
print("faceTo: This should never happen")
end
end
function moveTo(x, y)
if (yRelPos > y) then
faceTo(0, -1)
while yRelPos > y do
forward()
end
elseif (yRelPos < y) then
faceTo(0, 1)
while yRelPos < y do
forward()
end
end
if (xRelPos > x) then
faceTo(-1, 0)
while xRelPos > x do
forward()
end
elseif (xRelPos < x) then
faceTo(1, 0)
while xRelPos < x do
forward()
end
end
end
function blockIsWorth()
2023-05-29 02:39:58 +02:00
for i = 1, compareSlotMax do
2023-05-28 14:07:39 +02:00
turtle.select(i)
if turtle.compare() then
return false
end
end
2023-05-29 02:39:58 +02:00
return true
2023-05-28 14:07:39 +02:00
end
function checkForItems(z)
dir = math.fmod(z, 2)
-- alternatingly check relative facing (left, forward, right) and (right, forward, left) blocks, so at the end we can face backwards when going up and check without turning at all
-- -> two turns per mined level
if (dir == 0) then
faceTo(0, -1) -- just to be safe, but this should never really do anything except for the first level
if blockIsWorth() then
turtle.dig()
end
turnRight()
if blockIsWorth() then
turtle.dig()
end
turnRight()
if blockIsWorth() then
turtle.dig()
end
else
faceTo(0, 1)
if blockIsWorth() then
turtle.dig()
end
turnLeft()
if blockIsWorth() then
turtle.dig()
end
turnLeft()
if blockIsWorth() then
turtle.dig()
end
end
end
-- Digs straight down right where it is, taking resources with it
function digHole()
z = 0
-- go down
turtle.digDown()
2023-05-29 02:39:58 +02:00
while downwards() do
2023-05-28 14:07:39 +02:00
z = z + 1
checkForItems(z)
turtle.digDown()
end
-- go back up
-- look at the remaining direction (-1, 0) and check blocks
faceTo(-1, 0)
2023-05-29 02:39:58 +02:00
while zRelPos < 0 do
2023-05-28 14:07:39 +02:00
if blockIsWorth() then
turtle.dig()
end
2023-05-29 02:39:58 +02:00
upwards()
2023-05-28 14:07:39 +02:00
end
end
function returnItems()
-- chest coordinates are -1, 0, so go to 0, 0 facing "backwards"
moveTo(0, 0)
faceTo(-1, 0)
-- already facing right direction, put items
2023-05-29 02:39:58 +02:00
for slot = compareSlotMax + 1, 16 do
2023-05-28 14:07:39 +02:00
turtle.select(slot)
turtle.drop()
end
end
function moveToNext()
xPos = 1
yPos = 1
-- find out where the next hole should be
for i = 1, holeCounter.line do
if math.fmod(i, 2) == 0 then
xPos = xPos + 2
yPos = 1
else
xPos = xPos + 3
yPos = 0
end
end
for i = 1, holeCounter.hole do
xPos = xPos - 1
yPos = yPos + 2
end
moveTo(xPos, yPos)
-- update holeCounter
-- if the xPos we calculated is 1 that means we need to start the next line
if xPos == 0 then
holeCounter.line = holeCounter.line + 1
holeCounter.hole = 0
else
holeCounter.hole = holeCounter.hole + 1
end
end
print("Starting...")
-- startup
while not xRelPos == 1 do
forward()
end
while true do
print("Digging Hole (", holeCounter.line, ", ", holeCounter.hole, ")")
moveToNext()
digHole()
returnItems()
end