Niseg

polygon builder debug

May 30th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.65 KB | None | 0 0
  1. local count=0
  2. local slot =1    
  3. local count=0
  4. local breakMode=true
  5. local args = {...}
  6. local version="0.7.6"
  7.      --             1                   2               3               4                   5
  8. local ftbl= {
  9.                 {turtle.placeDown,  turtle.down,    turtle.digDown, turtle.detectDown,  turtle.compareDown  },
  10.                 {turtle.place,      turtle.forward, turtle.dig  ,   turtle.detect   ,   turtle.compare      },
  11.                 {turtle.placeUp,    turtle.up,      turtle.digUp,   turtle.detectUp,    turtle.compareUp    }
  12.                
  13.             }
  14.            
  15.        
  16. local function ensureSlot()
  17. --write("e")
  18.     if count<=0 then
  19.         slot=slot+1;
  20.         if(slot>16) then slot=1 end
  21.         count=turtle.getItemCount(slot)
  22.         if count==0 then print("not enough blocks"); error() end
  23.         turtle.select(slot)
  24.     end
  25. end
  26.  
  27.            
  28. local function digAndThrow(dir)
  29.     if dir== nil  then dir=0 end
  30.     local dir2=dir+2
  31. for i=1,16 do
  32.     if turtle.getItemCount(i)==0 then
  33.     turtle.select(i)
  34.     break
  35.     end
  36.     if(i==16) then
  37.         --print("failed to place block - not critical")
  38.         return false
  39.     end
  40.    
  41. end
  42.    
  43.     if not ftbl[dir2][3]() then turtle.select(slot);return false end
  44.     turtle.drop()
  45.     turtle.select(slot)
  46.     return true
  47. end
  48. --[[
  49. function fCirc(x,r)
  50. return math.sqrt(r*r-x*x)
  51. end
  52.  
  53. function followCirclePath()
  54. local i=1
  55. while true do
  56.  
  57.  
  58. end
  59. end
  60. ]]--
  61. function backSpecial()
  62.     while not turtle.back() do
  63.         if breakMode then
  64.         turtle.turnRight();turtle.turnRight();
  65.             if not digAndThrow(0) then
  66.                 print("obsticle found:waiting...");sleep(1)
  67.             else
  68.                 --return false
  69.             end
  70.         turtle.turnRight();turtle.turnRight();
  71.         else
  72.             print("obsticle found:waiting...");sleep(1)
  73.         end
  74.     end
  75.     return true
  76. end
  77.  
  78. function upSpecial()
  79. while not turtle.up() do
  80.     if not digAndThrow(1) then
  81.         print("can't move up")
  82.         sleep(1)
  83.     end
  84. end
  85. end
  86.  
  87.  
  88. local function tp(dir)
  89.     if dir== nil  then dir=0 end
  90.     local dir2=dir+2
  91.    
  92.     if ftbl[dir2][1]() then
  93.         count=count-1
  94.         ensureSlot()
  95.     else
  96.         if  digAndThrow(dir) then
  97.             if ftbl[dir2][1]() then
  98.                 count=count-1
  99.                 ensureSlot()
  100.             end
  101.         else
  102.             print("failed to place block - not critical")
  103.         end
  104.     end
  105. end
  106.  
  107. local function findDims(inclusive)
  108. local x,y=1,1
  109. local xr,yr=0,0
  110. if inclusive== nil then inclusive=true end
  111. while turtle.forward() do
  112. x=x+1
  113. end
  114. if inclusive then
  115. digAndThrow(0)
  116. turtle.forward()
  117. x=x+1
  118. end
  119. xr=x
  120. turtle.turnLeft()
  121. while turtle.forward() do
  122. y=y+1
  123. end
  124. if inclusive then
  125. digAndThrow(0)
  126. turtle.forward()
  127. y=y+1
  128. end
  129. yr=y
  130. while y>1 do turtle.back();y=y-1 end
  131. turtle.turnRight()
  132. while x>1 do turtle.back();x=x-1 end
  133.  
  134. return xr,yr
  135. end
  136.  
  137.  
  138.  
  139.  
  140. local function BuildSingleWall(x,z)
  141.     local zr=0;
  142.     zr=z %3
  143.     z=math.floor(z/3)
  144.     if (zr~=0) then z=z+1 end
  145.     local buildUp=true;
  146.     local buildForward=true;
  147.     for k=1,z do
  148.         if(k<z) then buildUp=true;buildForward=true;
  149.         elseif zr==1 then   buildUp=false;buildForward=false;
  150.         elseif zr==2 then   buildUp=false;buildForward=true;
  151.         else                buildUp=true ;buildForward=true;
  152.         end
  153.         upSpecial()
  154.        
  155.                 for i=1,x-1 do
  156.                         tp(-1)
  157.                         if buildUp then
  158.                             tp(1)
  159.                         end
  160.                         backSpecial()
  161.                         if buildForward then
  162.                             tp(0)
  163.                         end
  164.                 end
  165.        
  166.         tp(-1)
  167.            
  168.         if buildForward then
  169.                 upSpecial()
  170.                 tp(-1)
  171.         end
  172.         if buildUp then
  173.                 upSpecial()
  174.                 tp(-1)
  175.  
  176.         end
  177.         turtle.turnRight()
  178.         turtle.turnRight()
  179.     --  turtle.back()
  180.     --  if buildForward and j==1 then
  181.     --      tp(0)
  182.     --  end
  183.     end
  184.  
  185. end
  186. -------------------------------------------------------------
  187. local function buildWallPolygon(ar,len,r,z,connected,startDiagonal)
  188. local i=1
  189. local fin=false;
  190. local diagonal=false;
  191. local turnleft=true
  192. local firstz = true
  193. local pb=0;
  194. if connected== nil then connected=true end
  195. if startDiagonal== nil then startDiagonal=false end
  196. diagonal=startDiagonal
  197. local lx=0
  198.     zr=z %3
  199.     z=math.floor(z/3)
  200.     if (zr~=0) then z=z+1 end
  201.     local buildUp=true;
  202.     local buildForward=true;
  203. --print(ar[1].." "..len.." "..r.." "..z)
  204. for k=1,z do
  205. --write("z")
  206. firstz=true
  207.     if(k<z) then buildUp=true;buildForward=true;
  208.     elseif zr==1 then   buildUp=false;buildForward=false;
  209.     elseif zr==2 then   buildUp=false;buildForward=true;
  210.     else                buildUp=true ;buildForward=true;
  211.     end
  212.     upSpecial()
  213.     fin=false
  214.     for l=1,r  do
  215.         for i=1,len,2 do
  216.         pb=0
  217. --  write (i..">"..ar[i])
  218.             lx=ar[i]
  219.             --[[if(lx<0) then lx=-lx; turnleft=false
  220.             else
  221.                 turnleft = true
  222.             end
  223.             --]]
  224.             for x=1,lx do
  225.         --  write("x")
  226.             if not connected or not firstz then
  227.                 tp(-1)
  228.                 pb=pb+1
  229.                     if buildUp then
  230.                         tp(1)
  231.                     end
  232.             end
  233.                 --write("b")
  234.                 backSpecial()
  235.                 if (not connected or not firstz) and buildForward then
  236.             --  write("-")
  237.                     tp(0)
  238.                 end
  239.                 firstz=false
  240.                 if diagonal then
  241.                     --if pb>0 then
  242.                         if turnleft then turtle.turnRight() else turtle.turnLeft() end
  243.                         backSpecial()
  244.                         if not turnleft then turtle.turnRight() else turtle.turnLeft() end
  245.                         --turtle.turnLeft()
  246.                     --end
  247.                 end
  248.            
  249.         --      write(">"..x)
  250.             end
  251.             lx=ar[i+1]
  252.             if lx<0 then turnleft= false; lx=-lx
  253.             else
  254.             turnleft=true
  255.             end
  256.             for i=1,lx do
  257.                 if not diagonal then
  258.                     if not turnleft then turtle.turnRight() else turtle.turnLeft() end
  259.                 end
  260.                 diagonal= not diagonal
  261.             end
  262.         --  print("out of x loop")
  263.         if i==len and l==r then fin= true end
  264.         --[[
  265.             if not diagonal then
  266.             --turtle.turnLeft()
  267.             if not turnleft then turtle.turnRight() else turtle.turnLeft() end
  268.            
  269.             end
  270.             diagonal=not diagonal;
  271.         --]]
  272.         end
  273.  
  274.     end
  275.            
  276.         tp(-1)
  277.        
  278.         if buildForward and j==lim then
  279.                 upSpecial()
  280.                 tp(-1)
  281.         end
  282.         if buildUp then
  283.             if j~=lim then
  284.                 tp(1)
  285.             else
  286.                 upSpecial()
  287.                 tp(-1)
  288.             end
  289.         end
  290. --[[            if not octagon then
  291.         turtle.turnLeft()
  292.         end--]]
  293.         --backSpecial()
  294. --      if buildForward and k~=z then
  295.     --      tp(0)
  296.     --  end
  297.  
  298.  
  299. end
  300. end
  301. --
  302.  
  303.  
  304.  
  305. local function buildWall(x,y,z,octagon)
  306.    
  307.     local lim=2;
  308.     local zr=0;
  309.     local lx=x-2
  310.     local ly=y-2
  311.    
  312.     if octagon==nil then octagon=false end
  313.    
  314.     if octagon then
  315. --[[    y=math.floor(x/3)
  316.     x=x-y*2
  317.     -- calculate diameter
  318.     --]]
  319.     lim=4
  320.     end
  321.    
  322.    
  323.     zr=z %3
  324.     z=math.floor(z/3)
  325.     if (zr~=0) then z=z+1 end
  326.     local buildUp=true;
  327.     local buildForward=true;
  328.     for k=1,z do
  329.     if(k<z) then buildUp=true;buildForward=true;
  330.     elseif zr==1 then   buildUp=false;buildForward=false;
  331.     elseif zr==2 then   buildUp=false;buildForward=true;
  332.     else                buildUp=true ;buildForward=true;
  333.     end
  334.    
  335.      
  336.     upSpecial()
  337.     if octagon and k== 1 then
  338.     backSpecial()
  339.     end
  340.     for j=1,lim do
  341.             for i=1,x-2 do
  342.                     tp(-1)
  343.                     if buildUp then
  344.                         tp(1)
  345.                     end
  346.                     backSpecial()
  347.                     if buildForward then
  348.                         tp(0)
  349.                     end
  350.             end
  351.             tp(-1)
  352.             if buildUp then
  353.                 tp(1)
  354.             end
  355.             -- length
  356.             turtle.turnLeft()
  357.             backSpecial()
  358.             if buildForward  then
  359.                 tp(0)
  360.                
  361.             end
  362.             if octagon then
  363.                     turtle.turnRight()
  364.                     backSpecial()
  365.                     turtle.turnLeft()
  366.                     end
  367.             -- width
  368.         for i=1,y-2 do
  369.                     tp(-1)
  370.                     if buildUp then
  371.                     tp(1)
  372.                     end
  373.                     backSpecial()
  374.                     if buildForward then
  375.                     tp(0)
  376.                
  377.                     end
  378.                     if octagon then
  379.                     turtle.turnRight()
  380.                     backSpecial()
  381.                     turtle.turnLeft()
  382.                     end
  383.             end
  384.             tp(-1)
  385.            
  386.             if buildForward and j==lim then
  387.                     upSpecial()
  388.                     tp(-1)
  389.             end
  390.             if buildUp then
  391.                 if j~=lim then
  392.                     tp(1)
  393.                 else
  394.                     upSpecial()
  395.                     tp(-1)
  396.                 end
  397.             end
  398.             if not octagon then
  399.             turtle.turnLeft()
  400.             end
  401.             backSpecial()
  402.             if buildForward and j~=lim then
  403.                 tp(0)
  404.             end
  405.      
  406.         end
  407.     end
  408.    
  409.     turtle.forward()
  410.     if octagon then
  411.     turtle.turnLeft()
  412.     turtle.turnLeft()
  413.     end
  414.     end
  415.     local function buildPlate(x,y)
  416.     upSpecial()
  417.     for j=1,y do
  418.         for i=1,x-1 do
  419.             tp(-1)
  420.            backSpecial()
  421.         end
  422.             tp(-1)
  423.             if(j<y) then
  424.             if(j%2== 1) then turtle.turnLeft() else turtle.turnRight() end
  425.             backSpecial()
  426.             if(j%2== 1) then turtle.turnLeft() else turtle.turnRight() end
  427.             end
  428.             --turtle.back()
  429.     end
  430.     end
  431. -----------------------------------------------------------------------
  432. print ("Rectangular "..version.. " room builder by Niseg")
  433.     local sel=0
  434.     local x1,y1,z1
  435.     local argmode=false
  436.  
  437.  
  438.  
  439.     local fuel=turtle.getFuelLevel();
  440. if(fuel<10) then
  441.     print(fuel.." fuel is probobly not enough ");
  442.     print("put in more fuel and refuel all")
  443.     print("then you can start the program")
  444.     error()
  445. end
  446. --[[
  447.     local foundfuel=false;
  448.     for i=1,16 do
  449.         if not foundfuel and turtle.getItemCount(i)>0 then
  450.             trutle.select(i)
  451.             if turtle.refuel(turtle.getItemCount(i))  then foundfuel=true  end
  452.         if foundfuel and i<16 then
  453.         --condense
  454.         turtle.select(i+1)
  455.         turtle.transferTo(i)
  456.         end
  457.     end
  458. ]]
  459. --[[
  460.     if #args==4 then
  461.         argmode=true
  462.         local tempnum
  463.         for i=1,4 do
  464.             tempnum=tonumber(args[i])
  465.             if(tempnum==nil) then
  466.             print("invalid argument #"..i.."="..args[i])
  467.             print("expecting a number")
  468.            
  469.             error()
  470.             end
  471.             args[i]=tempnum;
  472.         end
  473.         x1,y1,z1,sel=args[1],args[2],args[3],args[4]
  474.     else
  475.         print ("cmd line syntax(skip UI): ")
  476.         print("build <length> <width> <height> <0-5>")
  477.         --print("0 = walls only 1= adds floor 2= adds roof 3= adds both" )
  478.         print("")
  479.         --print("no arguments running UI")
  480.         if #args~=0 then error() end
  481.         end
  482.         --]]
  483.     for i=1,16 do
  484.         if turtle.getItemCount(i)>0 then
  485.             turtle.select(i);
  486.             slot=i
  487.             count=turtle.getItemCount(slot)
  488.             break
  489.             end
  490.     end
  491. local arshape={}
  492. local con=true
  493. local rpt
  494. local sd=false
  495. if args[1]=="1" then con=false
  496. elseif args[1]=="2" then sd=true
  497. elseif args[1]=="3" then con=false;sd=true;
  498. end
  499.  
  500. z1=tonumber(args[2])
  501. rpt=tonumber(args[3])
  502. for i=4,#args  do
  503. arshape[i-3]=tonumber(args[i])
  504. end
  505. buildWallPolygon(arshape,#arshape,rpt,z1,con,sd )
  506.  
  507. --[[
  508.     if sel>=0 and sel<=3 then
  509.     arshape[1]=x1-1
  510.     arshape[2]=2
  511.     arshape[3]=y1-1
  512.     arshape[4]=2
  513.     buildWallPolygon(arshape,4,2,z1)
  514.     elseif sel==4 then
  515.     arshape[1]=x1-1
  516.     arshape[2]=4
  517.     arshape[3]=0
  518.     arshape[4]=0
  519.     arshape[5]=0
  520.     arshape[6]=0
  521.     buildWallPolygon(arshape,2,1,z1,false)
  522.     elseif sel==5 then
  523.     arshape[1]=x1-1
  524.     arshape[2]=1
  525.     arshape[3]=y1-1
  526.     arshape[4]=1
  527.     buildWallPolygon(arshape,4,4,z1)
  528.     elseif sel==6 then
  529.     arshape[1]=x1-1
  530.     arshape[2]=1
  531.     arshape[3]=y1-1
  532.     arshape[4]=2
  533.     arshape[5]=y1-1
  534.     arshape[6]=1
  535.     buildWallPolygon(arshape,6,2,z1)
  536.    
  537.     elseif sel==7 then
  538.     arshape[1]=x1-1
  539.     arshape[2]=0
  540.     arshape[3]=-(y1-1)
  541.     arshape[4]=y1-1
  542.     buildWallPolygon(arshape,4,2,z1)
  543.     end]]--
  544. error()
  545. if not argmode then
  546.     --print("make sure the turtle is refueled")
  547.     print("Load the blocks in contiguous slots")
  548.     print("Place turtle at bottom right corner")
  549. --  print("(builds in place)")
  550.     print("Hold CTRL-T to terminate")
  551.     local blockcount=0
  552.     local constraint=0
  553.     local floorcount=0
  554.     local wallcount=0
  555.  
  556.  
  557.     for i=1,16 do blockcount=blockcount + turtle.getItemCount(i) end
  558.     print ("Block count= "..blockcount)
  559. --  print("")
  560.     repeat
  561.     print("0/-1 length or width will start auto detection")
  562.     print("0 will build over the block -1 will build by it")
  563.     write("Length(1st)/flat side? ")
  564.     x1=tonumber(read())
  565.     until x1~= nil;
  566.     repeat
  567.     write("Width(2nd)/diagonal side? ")
  568.     y1=tonumber(read())
  569.     until y1~= nil;
  570. end
  571. if y1==0 or x1==0 then
  572. print("finding room dimensions including markers")
  573. if not argmode then
  574. print("press enter to continue")
  575. read()
  576. end
  577. x1,y1=findDims(true)
  578. print("building "..x1.." long and "..y1.." wide" )
  579. elseif y1==-1 or x1==-1 then
  580. print("finding room dimensions excluding markers")
  581. if not argmode then
  582. print("press enter to continue")
  583. read()
  584. end
  585. x1,y1=findDims(false)
  586. print("building "..x1.." long and "..y1.." wide" )
  587. end
  588.     blockcount=0
  589.     for i=1,16 do blockcount=blockcount + turtle.getItemCount(i) end
  590.     wallcount=x1*y1 -(x1-2)*(y1-2)
  591.     floorcount=x1*y1
  592.     --local ox,oy
  593.     --oy=math.floor(x1/3)
  594.     --ox=x1-(oy*2)
  595.    
  596. --  local wc_oct=ox*4 +math.max(0,(oy-2))*4
  597.     local wc_oct=x1*4 +math.max(0,(y1-2))*4
  598. if not argmode then
  599.     print ("Block count= "..blockcount)
  600.     print("Cost per wall= "..wallcount.." floor/roof= "..floorcount)
  601.     print("max heights:")
  602.     write("Walls= " ..math.floor(blockcount/wallcount))
  603.     write(" with floor/roof= "..math.max(0,math.floor((blockcount-floorcount)/wallcount)))
  604.     print(" both= "..math.max(0,math.floor((blockcount-floorcount)/wallcount)))
  605.     write("single wall= "..math.floor(blockcount/x1).." ")
  606.     print("octagon ="..math.floor(blockcount/wc_oct))
  607.     repeat
  608.     write("Height? ")
  609.     z1=tonumber(read())
  610.     until z1~= nil;
  611.  end
  612.    
  613.     local fuel_cost;
  614. if not argmode then
  615.     repeat  
  616.     print("0  walls only")
  617.     print("1  floor and walls")
  618.     print("2  walls and roof ")
  619.     print("3  floor walls and roof" )
  620.     print("4  a single wall" )
  621.     print("5  octagon" )
  622.     sel=tonumber(read())
  623.     until(sel~= nil and sel>=0 and sel<=5)
  624. end
  625.  
  626.     if sel==0 then
  627.         constraint=z1*wallcount;
  628.         fuel_cost=math.ceil(constraint/3)+z1+10;
  629.     elseif sel==1 or sel==2 then
  630.         constraint=(z1-1)*wallcount +floorcount
  631.         fuel_cost=math.ceil((constraint-floorcount)/3)+z1+10+floorcount;
  632.     elseif sel==3 then
  633.         constraint=(z1-2)*wallcount +2*floorcount
  634.         fuel_cost=math.ceil((constraint-floorcount)/3)+z1+10+2*floorcount;
  635.     elseif sel== 4 then
  636.         constraint=z1*x1;
  637.         fuel_cost=math.ceil(constraint/3)+z1+10;
  638.     elseif sel==5 then
  639.     --y1=x1
  640.     local y2=math.floor(x1/3)
  641.     local x2=x1-(y2*2)
  642.     constraint=z1*(x2*4+y2*4);
  643.     fuel_cost=math.ceil((constraint+y2*4)/3)+z1+10;
  644.     end
  645.     print("min fuel cost = "..fuel_cost);
  646.     if fuel<fuel_cost then print(fuel.." fuel is not enough!");
  647.     error()
  648.     end
  649.    
  650.     if(blockcount<constraint)  then
  651.         print("you will have to resupply the turtle while it's working")
  652.         print (" needed blocks = "..(constraint-blockcount).." ( "..math.ceil((constraint-blockcount)/64).."~ stacks)" )
  653.         if not argmode then
  654.                 print(" press enter to start or hold CTRL-T to quit")
  655.                 read()
  656.         end
  657.     end
  658.  
  659.    
  660.     --turtle.select(1)
  661.     --- starts building
  662.     turtle.turnRight();turtle.turnRight();
  663.     if sel ==4 then
  664.         BuildSingleWall(x1,z1)
  665.         turtle.turnRight();turtle.turnRight();
  666.     elseif  sel==5 then
  667.             buildWall(x1,y1,z1,true)
  668.     else
  669.        
  670.         if(z1==1 and (sel==2 or sel==3)) then sel=1 end
  671.        
  672.         if((sel==1 or sel==3) and z1>0) then
  673.             buildPlate(x1,y1);
  674.             z1=z1-1;
  675.             if(y1%2==1) then
  676.        
  677.                     turtle.turnRight();turtle.turnRight();
  678.             else
  679.                     turtle.turnLeft();
  680.                
  681.             end
  682.         end
  683.         if z1>0  then
  684.             backSpecial()
  685.         else
  686.         --turtle.turnRight();turtle.turnRight();
  687.         end
  688.        
  689.         if ((sel==2 or sel==3 )and z1>1) then
  690.             buildWall(x1,y1,z1-1)
  691.             buildPlate(x1,y1)
  692.             -- reset to valid position
  693.             if(y1%2==0) then    turtle.turnRight() end
  694.         else
  695.             if(z1>0) then buildWall(x1,y1,z1) end
  696.             -- reset to valid position
  697.             turtle.turnRight();turtle.turnRight();
  698.         end
  699.     end
  700.     print("done. cost was "..(fuel-turtle.getFuelLevel()).." fuel");
  701.     print("Fuel left : "..turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment