Skip to content

Instantly share code, notes, and snippets.

@Hackerjef
Created July 13, 2022 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hackerjef/c44a5fb688631d7c9c9c9ecb5b151967 to your computer and use it in GitHub Desktop.
Save Hackerjef/c44a5fb688631d7c9c9c9ecb5b151967 to your computer and use it in GitHub Desktop.
x_pos = 0
y_pos = 0
z_pos = 0
orientation = 0
function orient_to(target_orientation)
delta_orientation = ((target_orientation % 4) - orientation) % 4
if delta_orientation == 3 then
delta_orientation = -1
end
if delta_orientation == -3 then
delta_orientation = 1
end
while delta_orientation > 0 do
turtle.turnRight()
delta_orientation = delta_orientation - 1
end
while delta_orientation < 0 do
turtle.turnLeft()
delta_orientation = delta_orientation + 1
end
orientation = target_orientation
serialize_current_pos()
end
function move_up()
turtle.digUp()
turtle.attackUp()
turtle.suckUp()
r = turtle.up()
if r then
y_pos = y_pos + 1
end
serialize_current_pos()
return r
end
function move_down()
turtle.digDown()
turtle.attackDown()
turtle.suckDown()
r = turtle.down()
if r then
y_pos = y_pos - 1
end
serialize_current_pos()
return r
end
function move_forward()
turtle.dig()
turtle.attack()
turtle.suck()
r = turtle.forward()
if r then
if orientation == 0 then
z_pos = z_pos + 1
elseif orientation == 1 then
x_pos = x_pos + 1
elseif orientation == 2 then
z_pos = z_pos - 1
elseif orientation == 3 then
x_pos = x_pos - 1
end
end
serialize_current_pos()
return r
end
function move_to_pos_step(target_x, target_z, target_y)
-- prioritize y over x over z
-- we're under the target point
if y_pos < target_y then
move_up()
end
-- we're above the target point
if y_pos > target_y then
move_down()
end
-- we're to the west of the target point
if x_pos < target_x then
orient_to(1)
move_forward()
end
-- we're to the east of the target point
if x_pos > target_x then
orient_to(3)
move_forward()
end
-- we're to the south of the target point
if z_pos < target_z then
orient_to(0)
move_forward()
end
-- we're to the north of the target point
if z_pos > target_z then
orient_to(2)
move_forward()
end
if x_pos == target_x and y_pos == target_y and z_pos == target_z then
return true
else
return false
end
end
function move_to_pos(target_x, target_y, target_z)
while not move_to_pos_step(target_x, target_y, target_z) do
end
end
function inventory_has_empty_slot()
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
return true
end
end
return false
end
function serialize_current_pos()
file = fs.open("current_pos", "w");
file.write(string.format("x_pos=%f;y_pos=%f;z_pos=%f;orientation=%f;", x_pos, y_pos, z_pos, orientation));
file.close();
end
function serialize_progress(width, length, height)
file = fs.open("current_progress", "w");
file.write(string.format("target_x=%f;target_y=%f;target_z=%f;target_orientation=%f;width=%f;length=%f;height=%f;", x_pos, y_pos, z_pos, orientation, width, length, height));
file.close();
end
function load_state()
dofile("current_progress");
dofile("current_pos");
move_to_pos(target_x, target_z, target_y);
orient_to(target_orientation);
excavate(width, length, height);
end
function check_fuel()
if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
saved_x_pos = x_pos
saved_y_pos = y_pos
saved_z_pos = z_pos
saved_orientation = orientation
move_to_pos(0, 0, 0)
-- empty items
orient_to(2)
for i = 1, 16 do
turtle.select(i)
while turtle.drop() == false do end
end
-- refuel :)
orient_to(3)
while turtle.getFuelLevel < 5000 do
turtle.suck()
turtle.refuel()
end
--return
move_to_pos(saved_x_pos, saved_z_pos, saved_y_pos)
orient_to(saved_orientation)
end
end
function excavate(width, length, height)
if height < 3 then
height = 3
end
if width < 1 then
width = 1
end
height = height + (height % 3)
toright = true
while true do
while inventory_has_empty_slot() do
check_fuel()
local should_move_forward = true
serialize_progress(width, length, height)
-- prioritize width over length
if toright then
if x_pos >= (width - 1) then
toright = false
if z_pos >= (length - 1) then
if y_pos < (-height) then
-- We're done, pack up and go
move_to_pos(0, 0, 0)
orient_to(0)
return
end
-- Move to the next level
move_to_pos(0, 0, y_pos - 3)
turtle.digDown()
toright = true
should_move_forward = false
end
orient_to(0)
else
orient_to(1)
end
else
if x_pos <= 0 then
toright = true
if z_pos >= (length - 1) then
if y_pos < (-height) then
move_to_pos(0, 0, 0)
orient_to(0)
return
end
move_to_pos(0, 0, y_pos - 3)
turtle.digDown()
toright = true
should_move_forward = false
end
orient_to(0)
else
orient_to(3)
end
end
if should_move_forward == true then
while not move_forward() do end
end
turtle.digUp()
turtle.digDown()
end
saved_x_pos = x_pos
saved_y_pos = y_pos
saved_z_pos = z_pos
saved_orientation = orientation
move_to_pos(0, 0, 0)
-- drop to south
orient_to(2)
for i = 1, 16 do
turtle.select(i)
while turtle.drop() == false do end
end
move_to_pos(saved_x_pos, saved_z_pos, saved_y_pos)
orient_to(saved_orientation)
end
move_to_pos(0, 0, 0)
orient_to(0)
end
function main()
term.clear()
term.setCursorPos(1, 1)
print("----- Excavate program -----")
print("")
print("What dimensions?")
print("")
term.write("Width (to the right of the turtle): ")
z = math.abs(tonumber(io.read()))
term.write("Length (in front of the turtle): ")
x = math.abs(tonumber(io.read()))
term.write("Height (multiple of 3): ")
y = math.abs(tonumber(io.read()))
print("Excavating...")
move_to_pos(0, 0, -1)
excavate(z, x, y)
print("Done!")
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment