Skip to content

Instantly share code, notes, and snippets.

@Orpheon
Forked from anonymous/gist:4299974
Created December 15, 2012 22:38
Show Gist options
  • Save Orpheon/4299979 to your computer and use it in GitHub Desktop.
Save Orpheon/4299979 to your computer and use it in GitHub Desktop.
-- run by calling: f=fs.open("excavate","w");f.write(http.get("https://raw.github.com/gist/4299979").readAll());f.close();dofile("excavate")
-- orientation: 0 = N, 1 = E, 2 = S, 3 = W
x_pos = 0
y_pos = 0
z_pos = 0
orientation = 0
current_slot = 1
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
end
function move_up()
turtle.digUp()
turtle.attackUp()
turtle.suckUp()
r = turtle.up()
if r then
y_pos = y_pos + 1
end
return r
end
function move_down()
turtle.digDown()
turtle.attackDown()
turtle.suckDown()
r = turtle.down()
if r then
y_pos = y_pos - 1
end
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
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 iterate_point(x, y, limit)
z1 = 0
z2 = 0
c1 = x
c2 = y
for i=0, limit do
a = z1*z1 - z2*z2 + c1
b = 2*z1*z2 + c2
z1 = a
z2 = b
if (z1*z1 + z2*z2 > 4) then
return i
else
return -1
end
end
end
function draw(width, length, height)
move_to_pos(0, 0, height+1)
for z=0, width do
move_to_pos(z, 0, height+1)
orient_to(0)
y_math = 4*(z/width) - 2
for x=0, length do
x_math = 4*(x/length) - 2
if (iterate_point(x_math, y_math, 200) == -1) then
if (turtle.getItemCount(current_slot) == 0) then
current_slot = current_slot+1
turtle.select(current_slot)
end
turtle.placeDown()
end
move_forward()
end
end
move_to_pos(0, 0, height+1)
move_to_pos(0, 0, 0)
end
function main()
turtle.select(current_slot)
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: ")
y = math.abs(tonumber(io.read()))
print("Excavating...")
draw(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