local empty={}
local x,y,z,d= 0,0,0,0
local FUEL,FRAME,FILLER,WINDOW,FLOOR,TRASH = 1,2,3,4,5,6
print "Welcome to TeddyJ's turtle builder"
print "Please ensure your chests and turtles are"
print "arranged as follows:"
print " [T]"
print "[5][4][3][2][1]"
print "T=turtle, 1..5 chests containing Floor/Window/Filler/Frame/Fuel materials"
print ""
print "How big is the gap between chests (0 for no gap)?"
local chest_gap=io.read()
print "How many panels long would you like the building?"
local length=io.read()
print "How many panels wide would you like the building?"
local width=io.read()
print "How many storeys high would you like the building?"
local storeys=io.read()
function goUp(n)
checkFuel()
if n==nil then n=1 end
y=y+n
for _=1,n do
turtle.digUp()
turtle.up()
end
end
function placeDown(n)
if turtle.detectDown() then
turtle.select(TRASH)
turtle.digDown()
turtle.drop()
end
turtle.select(n)
if turtle.getItemCount(n)==0 then
refill(n)
end
turtle.placeDown()
end
function refill(n)
if not(empty[n]) then
goToChestAndRestock(n)
goToLastPlace()
end
end
function goToChestAndRestock(n)
for _=1,6 do
turtle.digUp()
turtle.up()
end
for _=1,d+3 do
turtle.turnLeft()
end
for _=1,x do
turtle.dig()
turtle.forward()
end
turtle.turnRight()
for _=0,z do
turtle.dig()
turtle.forward()
end
for _=1,(y+5) do
turtle.down()
end
turtle.turnRight()
if n>1 then
for _=2,n do
for i=0,chest_gap do
turtle.forward()
end
end
content=turtle.suckDown()
for _=2,n do
for i=0,chest_gap do
turtle.back()
end
end
else
content=turtle.suckDown()
end
if not(content) then empty[n]=true end
turtle.turnRight()
end
function goToLastPlace()
for _=1,(y+5) do
turtle.up()
end
for _=0,z do
turtle.forward()
end
turtle.turnLeft()
for _=1,x do
turtle.forward()
end
for _=1,6 do
turtle.down()
end
for _=1,d+5 do
turtle.turnRight()
end
end
function goDown(n)
checkFuel()
if n==nil then n=1 end
y=y-n
for _=1,n do
turtle.digDown()
turtle.down()
end
end
function turnLeft()
d=d-1
if d==-1 then d=3 end
turtle.turnLeft()
end
function turnRight()
d=d+1
if d==4 then d=0 end
turtle.turnRight()
end
function checkFuel()
if tonumber(turtle.getFuelLevel())~= nil and turtle.getFuelLevel()<80 then
print "Fuel low"
if turtle.getItemCount(FUEL)==0 then
print "No fuel available, restocking"
refill(FUEL)
end
print "Refueling"
turtle.select(FUEL)
if not(turtle.refuel()) then
print "No fuel in slot 1! Press enter when filled"
io.read()
end
end
end
function goForward(n)
checkFuel()
if n==nil then n=1 end
if d==0 then z=z+n end
if d==1 then x=x-n end
if d==2 then z=z-n end
if d==3 then x=x+n end
for _=1,n do
turtle.dig()
turtle.forward()
end
end
function goBack(n)
if n==nil then n=1 end
if d==0 then z=z-n end
if d==1 then x=x+n end
if d==2 then z=z+n end
if d==3 then x=x-n end
for _=1,n do
turtle.back()
end
end
function getBlockType(i,j)
if i==5 or j==5 then
return FRAME
end
if j==2 or j==3 then
if (i==2 or i==3) then
return WINDOW
else
return FILLER
end
end
return FILLER
end
function doSide(panels, isFinalSide)
for _=1,panels do
doPanel()
end
if not(isFinalSide) then
turnLeft()
goForward()
turnLeft()
goForward()
turnRight()
end
end
function doPanel()
checkFuel()
for i=1,5 do
goUp()
for j=1,5 do
b=getBlockType(i,j)
placeDown(b)
goForward()
end
if i<5 then
goBack(5)
end
end
goDown(5)
end
function doFloor()
turnLeft()
goForward()
turnRight()
floor_width=(width*5)-1
floor_length=(length*5)-1
for i=1,floor_width do
for i=1,floor_length do
placeDown(FLOOR)
goForward()
end
goBack(floor_length)
turnLeft()
goForward()
turnRight()
end
turnRight()
goForward(floor_width+1)
turnLeft()
end
function doStorey()
goForward()
doFloor()
doSide(length)
doSide(width)
doSide(length)
doSide(width,true)
goUp(6)
goBack()
turnLeft()
goDown()
end
goForward()
for _=1,storeys do
doStorey()
end
goForward()
doFloor()