Niseg

polygon build debug tracked movement

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