Compare commits

...

3 commits

View file

@ -24,6 +24,10 @@ FUEL_LOW = 1000
SHAFT_DEPTH = 100 SHAFT_DEPTH = 100
-- Start at this position to find the next shaft
next_shaft_side = 0
next_shaft_height = 0
function distance_to_drop() function distance_to_drop()
return math.abs(depth - DROP_POINT_D) return math.abs(depth - DROP_POINT_D)
+ math.abs(height - DROP_POINT_H) + math.abs(height - DROP_POINT_H)
@ -51,15 +55,49 @@ function turn_rel(dir)
turn_abs((facing + dir) % 4) turn_abs((facing + dir) % 4)
end end
-- Dumps the current turtle position
function _dump_pos()
print("Current position: H "..height.." S "..side.." D "..depth)
end
-- Updates the turtle position for a move in the given direction
function _count_move_dir(dir)
dir = dir % 4
if dir == 0 then depth = depth + 1
elseif dir == 1 then side = side + 1
elseif dir == 2 then depth = depth - 1
elseif dir == 3 then side = side - 1
end
end
function move_forward()
rc, desc = turtle.forward()
if not rc then abort("Move: "..desc) end
_count_move_dir(facing)
end
function move_back()
rc, desc = turtle.back()
if not rc then abort("Move: "..desc) end
_count_move_dir(facing + 2)
end
function move_up()
rc, desc = turtle.up()
if not rc then abort("Move: "..desc) end
height = height + 1
end
function move_down()
rc, desc = turtle.down()
if not rc then abort("Move: "..desc) end
height = height - 1
end
function go_to_depth(dd) function go_to_depth(dd)
turn_abs(0) turn_abs(0)
while depth > dd do while depth > dd do
turtle.back() move_back()
depth = depth - 1
end end
while depth < dd do while depth < dd do
turtle.forward() move_forward()
depth = depth + 1
end end
end end
@ -68,24 +106,24 @@ end
function go_to(dh, dd, ds) function go_to(dh, dd, ds)
go_to_depth(0) go_to_depth(0)
while height > dh do if height ~= dh then
turtle.down() while height > dh do
height = height - 1 move_down()
end end
while height < dh do while height < dh do
turtle.up() move_up()
height = height + 1 end
end end
turn_abs(1) if side ~= ds then
turn_abs(1)
while side > ds do while side > ds do
turtle.back() move_back()
side = side - 1 end
end while side < ds do
while side < ds do move_forward()
turtle.forward() end
side = side + 1
end end
go_to_depth(dd) go_to_depth(dd)
@ -116,7 +154,7 @@ function refuel_turtle()
return false return false
end end
go_to(DROP_POINT_H, DROP_POINT_D, DROP_POINT_S) go_to(FUEL_POINT_H, FUEL_POINT_D, FUEL_POINT_S)
while turtle.getFuelLevel() < FUEL_EXPECTATION do while turtle.getFuelLevel() < FUEL_EXPECTATION do
local rc, desc = turtle.suckUp() local rc, desc = turtle.suckUp()
if not rc then if not rc then
@ -131,57 +169,57 @@ end
-- Find the next unmined shaft and end up in front of it -- Find the next unmined shaft and end up in front of it
function find_next_shaft() function find_next_shaft()
go_to(0, 0, 0) function is_shaft_pos()
if side % 2 == 1 then
return false
elseif side % 4 == 0 then
return height % 3 == 0
elseif side % 4 == 2 then
return height % 3 ~= 0
end
end
function is_shaft()
return is_shaft_pos() and turtle.detect()
end
go_to(next_shaft_height, 0, next_shaft_side)
while true do while true do
print("Climb") if side % 2 == 0 then
for hei=0,MAX_HEIGHT-1 do while height < MAX_HEIGHT - 1 do
if (hei % 3) == 0 then if is_shaft() then
if turtle.detect() then
return return
end end
end
if turtle.detectUp() then if turtle.detectUp() then
turtle.digUp() turtle.digUp()
end
move_up()
end
if is_shaft() then
return
end
else
while height > 0 do
if is_shaft() then
return
end
if turtle.detectDown() then
turtle.digDown()
end
move_down()
end
if is_shaft() then
return
end end
if not turtle.up() then abort("Misshaped lobby") end
height = height + 1
end end
turn_abs(1) turn_abs(1)
for i=0,1 do if turtle.detect() then
if turtle.detect() then turtle.dig()
turtle.dig()
end
if not turtle.forward() then abort("Misshaped lobby") end
side = side + 1
end
turn_abs(0)
print("Down")
for hei=MAX_HEIGHT-1,0,-1 do
if (hei % 3) ~= 0 then
if turtle.detect() then
return
end
end
if turtle.detectDown() then
turtle.digDown()
end
if not turtle.down() then abort("Misshaped lobby") end
height = height - 1
end
turn_abs(1)
for i=0,1 do
if turtle.detect() then
turtle.dig()
end
if not turtle.forward() then abort("Misshaped lobby") end
side = side + 1
end end
move_forward()
turn_abs(0) turn_abs(0)
end end
end end
@ -193,17 +231,20 @@ end
-- Main function -- Main function
function main() function main()
if turtle.getFuelLevel() < FUEL_LOW then if turtle.getFuelLevel() < distance_to_refuel() then
refuel_turtle() print("Not enough fuel to begin with.")
else
print("NO FUEL AT ALL.")
return return
elseif turtle.getFuelLevel() < FUEL_LOW then
refuel_turtle()
end end
while true do while true do
print("Finding shaft") print("Finding shaft")
find_next_shaft() find_next_shaft()
print("Mining shaft") next_shaft_height = height
next_shaft_side = side
print("Mining shaft at position:")
_dump_pos()
mine_shaft() mine_shaft()
print("Discharging") print("Discharging")