Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local count=0
- local slot =1
- local count=0
- local breakMode=true
- local args = {...}
- local version="0.8.5"
- -- 1 2 3 4 5
- local ftbl= {
- {turtle.placeDown, mdt, turtle.digDown, turtle.detectDown, turtle.compareDown },
- {turtle.place, mft, turtle.dig , turtle.detect , turtle.compare },
- {turtle.placeUp, mut, turtle.digUp, turtle.detectUp, turtle.compareUp }
- }
- local tx,ty,tz=0,0,0
- local face=2;
- local dirinc = { {0,-1},{1,0},{0,1},{-1,0} }
- local function tlt()
- turtle.turnLeft()
- face=(face+1)%4
- end
- local function trt()
- turtle.turnRight()
- face=(face+4-1)%4
- end
- local function mut()
- if not turtle.up() then return false end
- tz=tz+1
- return true
- end
- local function mdt()
- if not turtle.down() then return false end
- tz=tz-1
- return true
- end
- local function mft()
- local idx = (face)%4+1
- if not turtle.forward() then return false end
- tx=tx+dirinc[idx][1];
- ty=ty+dirinc[idx][2];
- return true
- end
- local function mbt()
- local idx = (face+2)%4+1
- if not turtle.back() then return false end
- tx=tx+dirinc[idx][1];
- ty=ty+dirinc[idx][2];
- return true
- end
- local function ensureSlot()
- --write("e")
- if count<=0 then
- slot=slot+1;
- if(slot>16) then slot=1 end
- count=turtle.getItemCount(slot)
- if count==0 then print("not enough blocks"); error() end
- turtle.select(slot)
- end
- end
- local function digAndThrow(dir)
- if dir== nil then dir=0 end
- local dir2=dir+2
- local full=false;
- local inv={}
- for i=1,16 do
- inv[i]=turtle.getItemCount(i)
- if inv==0 then
- turtle.select(i)
- break
- end
- if(i==16) then
- --print("failed to place block - not critical")
- -- return false
- full = true
- end
- end
- -- if all slots are
- if not ftbl[dir2][3]() then turtle.select(slot);return false end
- if not full then
- turtle.drop()
- turtle.select(slot)
- return true
- else
- for i=1,16 do
- if inv[i]~=turtle.getItemCount(i) then
- turtle.select(i)
- turtle.drop(turtle.getItemCount(i)-inv[i])
- turtle.select(slot)
- return true
- end
- end
- end
- return true
- end
- --[[
- function fCirc(x,r)
- return math.sqrt(r*r-x*x)
- end
- function followCirclePath()
- local i=1
- while true do
- end
- end
- ]]--
- function backSpecial()
- while not mbt() do
- if breakMode then
- trt();trt();
- if not digAndThrow(0) then
- print("obsticle found:waiting...");sleep(1)
- else
- --return false
- end
- trt();trt();
- else
- print("obsticle found:waiting...");sleep(1)
- end
- end
- return true
- end
- function upSpecial()
- while not mut() do
- if not digAndThrow(1) then
- print("can't move up")
- sleep(1)
- end
- end
- end
- local function tp(dir)
- if dir== nil then dir=0 end
- local dir2=dir+2
- if ftbl[dir2][1]() then
- count=count-1
- ensureSlot()
- else
- if digAndThrow(dir) then
- if ftbl[dir2][1]() then
- count=count-1
- ensureSlot()
- end
- else
- print("failed to place block - not critical")
- end
- end
- end
- local function findDims(inclusive)
- local x,y=1,1
- local xr,yr=0,0
- if inclusive== nil then inclusive=true end
- while mft() do
- x=x+1
- end
- if inclusive then
- digAndThrow(0)
- mft()
- x=x+1
- end
- xr=x
- tlt()
- while mft() do
- y=y+1
- end
- if inclusive then
- digAndThrow(0)
- mft()
- y=y+1
- end
- yr=y
- while y>1 do mbt();y=y-1 end
- trt()
- while x>1 do mbt();x=x-1 end
- return xr,yr
- end
- -------------------------------------------------------------
- local function buildWallPolygon(ar,len,r,z,connected,startDiagonal)
- local i=1
- local fin=false;
- local diagonal=false;
- local turnleft=true
- local firstz = true
- local pb=0;
- if connected== nil then connected=true end
- if startDiagonal== nil then startDiagonal=false end
- diagonal=startDiagonal
- local lx=0
- zr=z %3
- z=math.floor(z/3)
- if (zr~=0) then z=z+1 end
- local buildUp=true;
- local buildForward=true;
- --print(ar[1].." "..len.." "..r.." "..z)
- for k=1,z do
- --write("z")
- firstz=true
- if(k<z) then buildUp=true;buildForward=true;
- elseif zr==1 then buildUp=false;buildForward=false;
- elseif zr==2 then buildUp=false;buildForward=true;
- else buildUp=true ;buildForward=true;
- end
- upSpecial()
- fin=false
- for l=1,r do
- for i=1,len,2 do
- pb=0
- -- write (i..">"..ar[i])
- lx=ar[i]
- --[[if(lx<0) then lx=-lx; turnleft=false
- else
- turnleft = true
- end
- --]]
- for x=1,lx do
- -- write("x")
- if not connected or not firstz then
- tp(-1)
- pb=pb+1
- if buildUp then
- tp(1)
- end
- end
- --write("b")
- backSpecial()
- if (not connected or not firstz) and buildForward then
- -- write("-")
- tp(0)
- end
- firstz=false
- if diagonal then
- --if pb>0 then
- if turnleft then trt() else tlt() end
- backSpecial()
- if not turnleft then trt() else tlt() end
- --tlt()
- --end
- end
- -- write(">"..x)
- end
- lx=ar[i+1]
- if lx<0 then turnleft= false; lx=-lx
- else
- turnleft=true
- end
- for i=1,lx do
- if not diagonal then
- if not turnleft then trt() else tlt() end
- end
- diagonal= not diagonal
- end
- -- print("out of x loop")
- if i==len and l==r then fin= true end
- --[[
- if not diagonal then
- --tlt()
- if not turnleft then trt() else tlt() end
- end
- diagonal=not diagonal;
- --]]
- end
- end
- tp(-1)
- if buildForward and j==lim then
- upSpecial()
- tp(-1)
- end
- if buildUp then
- if j~=lim then
- tp(1)
- else
- upSpecial()
- tp(-1)
- end
- end
- --[[ if not octagon then
- tlt()
- end--]]
- --backSpecial()
- -- if buildForward and k~=z then
- -- tp(0)
- -- end
- end
- end
- --
- local function buildPlate(x,y)
- upSpecial()
- for j=1,y do
- for i=1,x-1 do
- tp(-1)
- backSpecial()
- end
- tp(-1)
- if(j<y) then
- if(j%2== 1) then tlt() else trt() end
- backSpecial()
- if(j%2== 1) then tlt() else trt() end
- end
- --turtle.back()
- end
- if x%2==1 then
- trt();trt()
- else
- tlt()
- end
- end
- -----------------------------------------------------------------------
- print ("Rectangular "..version.. " room builder by Niseg")
- local sel=0
- local x1,y1,z1
- local argmode=false
- local fuel=turtle.getFuelLevel();
- if(fuel<10) then
- print(fuel.." fuel is probobly not enough ");
- print("put in more fuel and refuel all")
- print("then you can start the program")
- error()
- end
- --[[
- local foundfuel=false;
- for i=1,16 do
- if not foundfuel and turtle.getItemCount(i)>0 then
- trutle.select(i)
- if turtle.refuel(turtle.getItemCount(i)) then foundfuel=true end
- if foundfuel and i<16 then
- --condense
- turtle.select(i+1)
- turtle.transferTo(i)
- end
- end
- ]]
- if #args==4 then
- argmode=true
- local tempnum
- for i=1,4 do
- tempnum=tonumber(args[i])
- if(tempnum==nil) then
- print("invalid argument #"..i.."="..args[i])
- print("expecting a number")
- error()
- end
- args[i]=tempnum;
- end
- x1,y1,z1,sel=args[1],args[2],args[3],args[4]
- else
- print ("cmd line syntax(skip UI): ")
- print("build <length> <width> <height> <0-5>")
- --print("0 = walls only 1= adds floor 2= adds roof 3= adds both" )
- print("")
- --print("no arguments running UI")
- if #args~=0 then error() end
- end
- for i=1,16 do
- if turtle.getItemCount(i)>0 then
- turtle.select(i);
- slot=i
- count=turtle.getItemCount(slot)
- break
- end
- end
- local arshape={}
- --[[
- local con=true
- local rpt
- local sd=false
- if args[1]=="1" then con=false
- elseif args[1]=="2" then sd=true
- elseif args[1]=="3" then con=false;sd=true;
- end
- z1=tonumber(args[2])
- rpt=tonumber(args[3])
- for i=4,#args do
- arshape[i-3]=tonumber(args[i])
- end
- buildWallPolygon(arshape,#arshape,rpt,z1,con,sd )
- --]]
- --ttr();ttr();
- --error()
- if not argmode then
- --print("make sure the turtle is refueled")
- print("Load the blocks in contiguous slots")
- print("Place turtle at bottom right corner")
- -- print("(builds in place)")
- print("Hold CTRL-T to terminate")
- local blockcount=0
- local constraint=0
- local floorcount=0
- local wallcount=0
- for i=1,16 do blockcount=blockcount + turtle.getItemCount(i) end
- print ("Block count= "..blockcount)
- -- print("")
- repeat
- print("0/-1 length or width will start auto detection")
- print("0 will build over the block -1 will build by it")
- write("Length(1st)/flat side? ")
- x1=tonumber(read())
- until x1~= nil;
- repeat
- write("Width(2nd)/diagonal side? ")
- y1=tonumber(read())
- until y1~= nil;
- end
- if y1==0 or x1==0 then
- print("finding room dimensions including markers")
- if not argmode then
- print("press enter to continue")
- read()
- end
- x1,y1=findDims(true)
- print("building "..x1.." long and "..y1.." wide" )
- elseif y1==-1 or x1==-1 then
- print("finding room dimensions excluding markers")
- if not argmode then
- print("press enter to continue")
- read()
- end
- x1,y1=findDims(false)
- print("building "..x1.." long and "..y1.." wide" )
- end
- blockcount=0
- for i=1,16 do blockcount=blockcount + turtle.getItemCount(i) end
- wallcount=x1*y1 -(x1-2)*(y1-2)
- floorcount=x1*y1
- --local ox,oy
- --oy=math.floor(x1/3)
- --ox=x1-(oy*2)
- -- local wc_oct=ox*4 +math.max(0,(oy-2))*4
- local wc_oct=x1*4 +math.max(0,(y1-2))*4
- if not argmode then
- print ("Block count= "..blockcount)
- print("Cost per wall= "..wallcount.." floor/roof= "..floorcount)
- print("max heights:")
- write("Walls= " ..math.floor(blockcount/wallcount))
- write(" with floor/roof= "..math.max(0,math.floor((blockcount-floorcount)/wallcount)))
- print(" both= "..math.max(0,math.floor((blockcount-floorcount)/wallcount)))
- write("single wall= "..math.floor(blockcount/x1).." ")
- print("octagon ="..math.floor(blockcount/wc_oct))
- repeat
- write("Height? ")
- z1=tonumber(read())
- until z1~= nil;
- end
- local fuel_cost;
- if not argmode then
- repeat
- print("0 walls only")
- print("1 floor and walls")
- print("2 walls and roof ")
- print("3 floor walls and roof" )
- print("4 a single wall ".."5 octagon " )
- print("6 hexagon 7 Parallelogram 8 digonal wall")
- print("9 cross 10 cross diagonal corner")
- print("11 cross start long side")
- sel=tonumber(read())
- until(sel~= nil and sel>=0 and sel<=11)
- end
- if sel==0 then
- constraint=z1*wallcount;
- fuel_cost=math.ceil(constraint/3)+z1+10;
- elseif sel==1 or sel==2 then
- constraint=(z1-1)*wallcount +floorcount
- fuel_cost=math.ceil((constraint-floorcount)/3)+z1+10+floorcount;
- elseif sel==3 then
- constraint=(z1-2)*wallcount +2*floorcount
- fuel_cost=math.ceil((constraint-floorcount)/3)+z1+10+2*floorcount;
- elseif sel== 4 then
- constraint=z1*x1;
- fuel_cost=math.ceil(constraint/3)+z1+10;
- --elseif sel==5 then
- --y1=x1
- else
- constraint=0;
- fuel_cost=0;
- --[[
- local y2=math.floor(x1/3)
- local x2=x1-(y2*2)
- constraint=z1*(x2*4+y2*4);
- fuel_cost=math.ceil((constraint+y2*4)/3)+z1+10;]]--
- end
- print("min fuel cost = "..fuel_cost);
- if fuel<fuel_cost then print(fuel.." fuel is not enough!");
- error()
- end
- if(blockcount<constraint) then
- print("you will have to resupply the turtle while it's working")
- print (" needed blocks = "..(constraint-blockcount).." ( "..math.ceil((constraint-blockcount)/64).."~ stacks)" )
- if not argmode then
- print(" press enter to start or hold CTRL-T to quit")
- read()
- end
- end
- --turtle.select(1)
- --- starts building
- trt();trt();
- if sel>=0 and sel<=3 then
- if sel==1 or sel ==3 then
- buildPlate(x1,y1)
- z1=z1-1
- end
- arshape[1]=x1-1
- arshape[2]=2
- arshape[3]=y1-1
- arshape[4]=2
- if z1>1 and (sel==2 or sel==3) then z1=z1-1 end
- if z1>0 then buildWallPolygon(arshape,4,2,z1,true,false) end
- if sel==2 or sel ==3 then
- buildPlate(x1,y1)
- end
- elseif sel==4 then -- wall
- arshape[1]=x1-1
- arshape[2]=4
- arshape[3]=0
- arshape[4]=0
- arshape[5]=0
- arshape[6]=0
- buildWallPolygon(arshape,2,1,z1,false)
- elseif sel==5 then -- octagon
- arshape[1]=x1-1
- arshape[2]=1
- arshape[3]=y1-1
- arshape[4]=1
- buildWallPolygon(arshape,4,4,z1)
- elseif sel==6 then -- hexagon
- arshape[1]=x1-1
- arshape[2]=1
- arshape[3]=y1-1
- arshape[4]=2
- arshape[5]=y1-1
- arshape[6]=1
- buildWallPolygon(arshape,6,2,z1)
- elseif sel==7 then -- Parallelogram
- arshape[1]=x1-1
- arshape[2]=2
- arshape[3]=y1-1
- arshape[4]=2
- buildWallPolygon(arshape,4,2,z1,true,true)
- elseif sel==8 then -- diagonal wall
- arshape[1]=x1-1
- arshape[2]=4
- arshape[3]=0
- arshape[4]=0
- arshape[5]=0
- arshape[6]=0
- buildWallPolygon(arshape,6,1,z1,false,true)
- elseif sel==9 then -- cross
- arshape[1]=x1-1
- arshape[2]=-2
- arshape[3]=y1-1
- arshape[4]=2
- arshape[5]=y1-1
- arshape[6]=-2
- buildWallPolygon(arshape,6,4,z1,true,false)
- elseif sel==10 then -- cross with diagonal corner
- arshape[1]=x1-2
- arshape[2]=-1
- arshape[3]=1
- arshape[4]=-1
- arshape[5]=y1-2
- arshape[6]=1
- arshape[7]=1
- arshape[8]=1
- arshape[9]=y1-2
- arshape[10]=-1
- arshape[11]=1
- arshape[12]=-1
- buildWallPolygon(arshape,12,4,z1,true,false)
- elseif sel==11 then -- cross start long side
- arshape[1]=y1-1
- arshape[2]=2
- arshape[3]=x1-1
- arshape[4]=2
- arshape[5]=y1-1
- arshape[6]=-2
- buildWallPolygon(arshape,6,4,z1,true,false)
- else
- print("invalid shape")
- error()
- end
- trt();trt()
- print("done. cost was "..(fuel-turtle.getFuelLevel()).." fuel");
- print("Fuel left : "..turtle.getFuelLevel())
- print("position relative to start = "..tx..","..ty..","..tz)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement