Compare commits

...

3 commits

View file

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