McBaron

drone

Aug 22nd, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.38 KB | None | 0 0
  1. -- drone program used in building. written by jonathan ortiz
  2. slot=1
  3. choice=3
  4. isFull=false
  5. builder=true
  6. gatherer=false
  7. patternString="Line"
  8. pattern=1
  9. sideLength=0
  10. sideHeight=0
  11. sideWidth=0
  12. builderString="Building"
  13. drawinglength=false
  14. drawingheight=false
  15. drawingwidth=false
  16. running=false
  17. --checks if the turtle has material in any of the first 14 slots (last two slots are for fuel) will place a block below the turtle.--
  18. --It is possible to script a pattern of 14 different stages by placing specific quantities into the turtle in from top left across then down.--
  19. drawWorking=function()
  20. term.setCursorPos(2,3)
  21. term.write(" Currently:")term.write(builderString)term.write(" a ")term.write(patternString)
  22. end
  23. boarder=function()
  24. term.clear()
  25. term.setCursorPos(1,1)
  26. print("===========Turtle Builder==============")
  27. print("|                                     |")
  28. print("|                                     |")
  29. print("|                                     |")
  30. print("|                                     |")
  31. print("|                                     |")
  32. print("|                                     |")
  33. print("|                                     |")
  34. print("|                                     |")
  35. print("|                                     |")
  36. print("|                                     |")
  37. print("===========Turtle Builder==============")
  38. end
  39. mainhud=function()
  40. term.setCursorPos(2,3)
  41. term.write(" 1  Build/Break:")
  42. term.setCursorPos(2,4)
  43. term.write(" 2  Gather:")
  44. term.setCursorPos(2,5)
  45. term.write(" 3  Pattern:")
  46. term.setCursorPos(2,6)
  47. term.write(" 4  Run!")
  48. term.setCursorPos(2,7)
  49. term.write(" 5  Exit!")
  50. if pattern>1 then
  51. term.setCursorPos(1,8)
  52. term.write("< 7  > Side Length: ")term.write(tostring(sideLength))
  53. drawinglength=true
  54. else
  55. drawinglength=false
  56. end
  57. if pattern==4 or pattern==2 or pattern==3 then
  58. term.setCursorPos(1,9)
  59. term.write("< 8  > Side Height: ")term.write(tostring(sideHeight))
  60. drawingheight=true
  61. else
  62. drawingheight=false
  63. end
  64. if pattern==1 and not builder then
  65. term.setCursorPos(1,8)
  66. term.write("< 7  > Side Length: ")term.write(tostring(sideLength))
  67. end
  68. if pattern==3 then
  69. term.setCursorPos(1,10)
  70. term.write("< 9  > Side Width: ")term.write(tostring(sideWidth))
  71. drawingwidth=true
  72. else
  73. drawingwidth=false
  74. end
  75. end
  76. drawStatus=function(builder,gatherer,patternString)
  77. term.setCursorPos(18,3)
  78. if builder then
  79. builderString="Building"
  80. else
  81. builderString="Breaking"
  82. end
  83. term.write(builderString)
  84. term.setCursorPos(13,4)
  85. term.write(gatherer)
  86. term.setCursorPos(14,5)
  87.  if pattern==1 then patternString="Line"
  88.  elseif pattern==2 then patternString="Square"
  89.  elseif pattern==3 then patternString="Floor"
  90.  elseif pattern==4 then patternString="Wall"
  91.  end
  92. term.write(patternString)
  93. end
  94. drawChoice=function(choiceNum)
  95.     term.setCursorPos(2,choiceNum)
  96.     term.write("[")
  97.     term.setCursorPos(4,choiceNum)
  98.     term.write("]")
  99. end
  100. place=function()
  101.     counted=turtle.getItemCount(slot)
  102.     turtle.select(slot)
  103.     if counted<1 then
  104.         empty=true
  105.     end
  106.    
  107.     while empty do
  108.         slot=slot+1
  109.         if slot>14 then
  110.             error("out of blocks")
  111.         end
  112.        
  113.     turtle.select(slot)
  114.     counted=turtle.getItemCount(slot)
  115.    
  116.         if counted>0 then
  117.             empty=false
  118.         end
  119.    
  120.         term.write("still empty")
  121.    
  122.     end
  123.    
  124.     turtle.placeDown()
  125.  
  126. end
  127. --IMPORTANT! when a turtle is being used to collect blocks with this program the first slot must remain open as a buffer for new items coming in--
  128. sort=function()
  129.     for i=2,14,1 do
  130.         turtle.select(1)
  131.         sames=turtle.compareTo(i)
  132.  
  133.         if sames then
  134.             turtle.select(1)
  135.             turtle.transferTo(i)
  136.         end
  137. end
  138.         if turtle.getItemCount(1)>0 then
  139.             for j=2,14,1 do
  140.                 empty=turtle.getItemCount(j)
  141.                 if empty==0 and turtle.getItemCount(1)>0 then
  142.                     turtle.select(1)
  143.                     turtle.transferTo(j)
  144.                 end
  145.             end
  146.             if turtle.getItemCount(1)>0 then
  147.                 isFull=true
  148.                 return
  149.             end
  150.         end
  151.     end
  152. --takes up blocks around the turtle making use of the sort function to store them--
  153. gather=function()
  154.     turtle.drop()
  155. end
  156. --Breaks the block below the turtle and gathers it optionaly. depends on boolean 'shouldGather'--
  157. dig=function()
  158.     turtle.digDown()
  159.     if not gatherer==true then
  160.     gather()
  161.     end
  162. end
  163. digFront=function()
  164. turtle.dig()
  165. if gatherer==true then
  166.     --gather()
  167.     end
  168. end
  169. --breaks the block in front of the turtle and gathers it optionally. depends on the boolean 'shouldGather'--
  170. --will check for fuel and the need for it then take the appropriate action.--
  171. fuel=function()
  172.     fuelSlot=15
  173.     hasFuel=turtle.getFuelLevel()
  174.     if hasFuel<2 then
  175.         turtle.select(fuelSlot)
  176.         counted=turtle.getItemCount(fuelSlot)
  177.         if counted>0 then
  178.             turtle.refuel(1)
  179.         else
  180.             fuelSlot=fuelSlot+1
  181.             turtle.select(fuelSlot)
  182.             counted=turtle.getItemCount(fuelSlot)
  183.             if counted>0 then
  184.                 turtle.refuel(1)
  185.             end
  186.         end
  187.         hasFuel=turtle.getFuelLevel()
  188.         if hasFuel<2 then
  189.             error("Out of Fuel")
  190.         end
  191.         term.write("fuel complete")
  192.     end
  193. end
  194. -- will place blocks in a line untill the turtle discovers an obstruction.--
  195. -- It is possible to controll the length of the line without an obstruction by limiting the building materials located in the first 14 slots of the turtle.--
  196. bLine=function()
  197.     runsline=true
  198.     term.clear()
  199.     boarder()
  200.     term.setCursorPos(2,3)
  201.     term.write("Starting")
  202.    
  203.     while runsline do
  204.         if turtle.getFuelLevel()<2 then
  205.             fuel()
  206.             end
  207.        
  208.         isBlocked=turtle.detect()
  209.         hasPlaced=turtle.detectDown()
  210.        
  211.             if hasPlaced and isBlocked then
  212.                 term.write("Operation Stopped due to obstruction")
  213.                 runsline=false
  214.             elseif isBlocked and not hasPlaced then
  215.                 place()
  216.                 runsline=false
  217.             elseif not isBlocked and not hasPlaced then
  218.                 place()
  219.             end
  220.             turtle.forward()
  221.     end
  222.     term.write("Finshed")
  223.  
  224.     end
  225. brLine=function(sideLength)
  226.     term.clear()
  227.     boarder()
  228.     term.setCursorPos(2,3)
  229.     term.write("Starting")
  230.             for i=0,sideLength,1 do
  231.             if turtle.getFuelLevel()<2 then
  232.             fuel()
  233.             end
  234.             turtle.dig()
  235.             if gatherer==true then
  236.             term.write("stupid gatherer")
  237.             --gather()
  238.             end
  239.             turtle.forward()
  240.             end
  241.     end
  242. bSquare=function(sideLength,sideHeight)
  243. term.clear()
  244.     boarder()
  245.     term.setCursorPos(2,3)
  246.     term.write("Starting")
  247.     building=true
  248.     while building do
  249.         place()
  250.         for i=1,sideHeight do
  251.             for j=2,sideLength do
  252.                 if turtle.getFuelLevel()<2 then
  253.                 fuel()
  254.                 end
  255.                 place()
  256.                 isBlocked=turtle.detect()
  257.                 if isBlocked then
  258.                     error("path blocked")
  259.                 else
  260.                     turtle.forward()
  261.                 end
  262.             end
  263.             turtle.turnRight()
  264.             for j=2,sideLength do
  265.                 if turtle.getFuelLevel()<2 then
  266.                 fuel()
  267.                 end
  268.                 place()
  269.                 isBlocked=turtle.detect()
  270.                 if isBlocked then
  271.                     error("path blocked")
  272.                 else
  273.                     turtle.forward()
  274.                 end
  275.             end
  276.             turtle.turnRight()
  277.             for j=2,sideLength do
  278.                 if turtle.getFuelLevel()<2 then
  279.                 fuel()
  280.                 end
  281.                 place()
  282.                 isBlocked=turtle.detect()
  283.                 if isBlocked then
  284.                     error("path blocked")
  285.                 else
  286.                     turtle.forward()
  287.                 end
  288.             end
  289.             turtle.turnRight()
  290.             for j=3,sideLength do
  291.                 if turtle.getFuelLevel()<2 then
  292.                 fuel()
  293.                 end
  294.                 place()
  295.                 isBlocked=turtle.detect()
  296.                 if isBlocked then
  297.                     error("path blocked")
  298.                 else
  299.                     turtle.forward()
  300.                 end
  301.             end
  302.             place()
  303.             turtle.up()
  304.             isBlocked=turtle.detect()
  305.                 if isBlocked then
  306.                     error("path blocked")
  307.                 else
  308.                     turtle.forward()
  309.                 end
  310.                 place()
  311.                 turtle.turnRight()
  312.             end
  313.     end
  314.     running=false
  315. end
  316. brSquare=function(sideLength,sideHeight)
  317. term.clear()
  318.     boarder()
  319.     term.setCursorPos(2,3)
  320.     term.write("Starting")
  321. building=true
  322.     while building do
  323.         dig()
  324.         for i=1,sideHeight do
  325.             for j=2,sideLength do
  326.                 if turtle.getFuelLevel()<2 then
  327.                 fuel()
  328.                 end
  329.                 dig()
  330.                 isBlocked=turtle.detect()
  331.                 if isBlocked then
  332.                     error("path blocked")
  333.                 else
  334.                     turtle.forward()
  335.                 end
  336.             end
  337.             turtle.turnRight()
  338.             for j=2,sideLength do
  339.                 if turtle.getFuelLevel()<2 then
  340.                 fuel()
  341.                 end
  342.                 dig()
  343.                 isBlocked=turtle.detect()
  344.                 if isBlocked then
  345.                     error("path blocked")
  346.                 else
  347.                     turtle.forward()
  348.                 end
  349.             end
  350.             turtle.turnRight()
  351.             for j=2,sideLength do
  352.                 if turtle.getFuelLevel()<2 then
  353.                 fuel()
  354.                 end
  355.                 dig()
  356.                 isBlocked=turtle.detect()
  357.                 if isBlocked then
  358.                     error("path blocked")
  359.                 else
  360.                     turtle.forward()
  361.                 end
  362.             end
  363.             turtle.turnRight()
  364.             for j=3,sideLength do
  365.                 if turtle.getFuelLevel()<2 then
  366.                 fuel()
  367.                 end
  368.                 dig()
  369.                 isBlocked=turtle.detect()
  370.                 if isBlocked then
  371.                     error("path blocked")
  372.                 else
  373.                     turtle.forward()
  374.                 end
  375.             end
  376.             dig()
  377.             turtle.down()
  378.             isBlocked=turtle.detect()
  379.                 if isBlocked then
  380.                     error("path blocked")
  381.                 else
  382.                     turtle.forward()
  383.                 end
  384.                 dig()
  385.                 turtle.turnRight()
  386.             end
  387.             building=false
  388.     end
  389.     running=false
  390. end
  391. bFloor=function(sideLength,sideWidth,sideHeight)
  392. term.clear()
  393.     boarder()
  394.     term.setCursorPos(2,3)
  395.     term.write("Starting")
  396.     building=true
  397.     turnRight=true
  398.     while building do
  399.     for i=1,sideHeight,1 do
  400.         for i=1,sideWidth,1 do
  401.             for i=2,sideLength,1 do
  402.             --walking forward placing blocks
  403.             if turtle.getFuelLevel()<2 then
  404.             fuel()
  405.             end
  406.             place()
  407.             isBlocked=turtle.detect()
  408.                 if isBlocked then
  409.                     error("path blocked")
  410.                 else
  411.                     turtle.forward()
  412.                 end
  413.             place()        
  414.             end
  415.             --decides to zig or zag then does it placing it facing back the opposite way over an empty space
  416.             if turnRight then
  417.             turtle.turnRight()
  418.             isBlocked=turtle.detect()
  419.                     if isBlocked then
  420.                         error("path blocked")
  421.                     else
  422.                         turtle.forward()
  423.                         end
  424.             turtle.turnRight()
  425.             end
  426.             if not turnRight then
  427.                 turtle.turnLeft()
  428.                 isBlocked=turtle.detect()
  429.                     if isBlocked then
  430.                         error("path blocked")
  431.                     else
  432.                         turtle.forward()
  433.                     end
  434.                         turtle.turnLeft()
  435.                     end
  436.                 turnRight= not turnRight   
  437.             end
  438.             --moves turtle up then checks to see if it just went left or right
  439.             turtle.up()
  440.             if not turnRight then
  441.             --if the turtle just went right then it should
  442.                 turtle.turnRight()
  443.                 isBlocked=turtle.detect()
  444.                     if isBlocked then
  445.                         error("path blocked")
  446.                     else
  447.                         turtle.forward()
  448.                         end
  449.                         turtle.turnLeft()
  450.                         end
  451.             if turnRight then
  452.               turtle.turnLeft()
  453.               isBlocked=turtle.detect()
  454.                     if isBlocked then
  455.                         error("path blocked")
  456.                     else
  457.                         turtle.forward()
  458.                         end
  459.                         turtle.turnRight()
  460.                         end
  461.                         turnRight=not turnRight
  462.         end
  463.         building=false
  464.     end
  465. end
  466. brFloor=function()
  467. term.clear()
  468.     boarder()
  469.     term.setCursorPos(2,3)
  470.     term.write("Starting")
  471.     building=true
  472.     turnRight=true
  473.     while building do
  474.     for i=1,sideHeight,1 do
  475.         for j=1,sideWidth,1 do
  476.             for i=2,sideLength,1 do
  477.             --walking forward placing blocks
  478.             if turtle.getFuelLevel()<2 then
  479.             fuel()
  480.             end
  481.             dig()
  482.             isBlocked=turtle.detect()
  483.                 if isBlocked and i<sideLength then
  484.                     digFront()
  485.                         turtle.forward()
  486.                 else
  487.                     turtle.forward()
  488.                 end
  489.             dig()  
  490.             end
  491.             --decides to zig or zag then does it placing it facing back the opposite way over an empty space
  492.             if turnRight and j<sideWidth then
  493.             turtle.turnRight()
  494.             isBlocked=turtle.detect()
  495.                     if isBlocked then
  496.                         dig()
  497.                         turtle.forward()
  498.                     else
  499.                         turtle.forward()
  500.                         end
  501.             turtle.turnRight()
  502.             end
  503.             if not turnRight and j<sideWidth then
  504.                 turtle.turnLeft()
  505.                 isBlocked=turtle.detect()
  506.                     if isBlocked then
  507.                         dig()
  508.                         turtle.forward()
  509.                     else
  510.                         turtle.forward()
  511.                     end
  512.                         turtle.turnLeft()
  513.                     end
  514.                 turnRight= not turnRight   
  515.             end
  516.         --after completing a whole floor turtle turns about and descends and contunues on.
  517.         turtle.turnRight()
  518.         turtle.turnRight()
  519.         turtle.down()
  520.         turnRight=not turnRight
  521.             end
  522.         building=false
  523.     end
  524.     end
  525. bWall=function(sideLength,sideHeight)
  526. term.clear()
  527.     boarder()
  528.     term.setCursorPos(2,3)
  529.     term.write("Starting")
  530.     for i=1,sideHeight,1 do
  531.         for j=2,sideLength,1 do
  532.         if turtle.getFuelLevel()<2 then
  533.             fuel()
  534.             end
  535.             place()
  536.             isBlocked=turtle.detect()
  537.                 if isBlocked and i<sideLength then
  538.                     digFront()
  539.                         turtle.forward()
  540.                 else
  541.                     turtle.forward()
  542.                 end
  543.             place()
  544.         end
  545.         turtle.turnRight()
  546.         turtle.turnRight()
  547.         turtle.up()
  548.     end
  549. end
  550. brWall=function()
  551. term.clear()
  552.     boarder()
  553.     term.setCursorPos(2,3)
  554.     term.write("Starting")
  555.     for i=1,sideHeight,1 do
  556.         for j=2,sideLength,1 do
  557.         if turtle.getFuelLevel()<2 then
  558.             fuel()
  559.             end
  560.             dig()
  561.             isBlocked=turtle.detect()
  562.                 if isBlocked and i<sideLength then
  563.                     digFront()
  564.                         turtle.forward()
  565.                 else
  566.                     turtle.forward()
  567.                 end
  568.             dig()  
  569.         end
  570.         turtle.turnRight()
  571.         turtle.turnRight()
  572.         turtle.down()
  573.     end
  574. end
  575.  
  576. while true do
  577. boarder()
  578. mainhud()
  579. drawStatus(builder,gatherer,patternString)
  580. drawChoice(choice)
  581. event,param1=os.pullEvent("key")
  582.     if event== "key" and param1==200 then
  583.     choice = choice-1
  584.         if choice<3 and drawingwidth then
  585.             choice=10
  586.         end
  587.         if choice<3 and drawingheight and not drawingwidth then
  588.         choice=9
  589.         end
  590.         if choice<3 and drawinglength and not drawingheight then
  591.         choice=8
  592.         end
  593.         if choice<3 and not drawinglength then
  594.         choice=7
  595.         end
  596.     end
  597.     if event== "key" and param1==208 then
  598.     choice=choice+1
  599.         if choice>10 then
  600.             choice=3
  601.             end
  602.             if choice>7 and not drawinglength and not drawingwidth then
  603.             choice=3
  604.             end
  605.             if choice>8 and not drawingheight then
  606.             choice=3
  607.             end
  608.             if choice>9 and not drawingwidth then
  609.             choice=3
  610.             end
  611.             end
  612.     if event== "key" and param1==28 then
  613.         if choice==3 then
  614.             builder=not builder
  615.             end
  616.         if choice==4 then
  617.         gatherer= not gatherer
  618.         end
  619.         if choice==5 then
  620.         pattern=pattern+1
  621.             if pattern>4 then
  622.             pattern=1
  623.             end
  624.         end
  625.         if choice==6 and param1==28 then
  626.         running=true
  627.         --primary loop when all choices have been made--
  628.             while running do
  629.             --line order--
  630.                 if pattern==1 then
  631.                     if builder and running then
  632.                     bLine()
  633.                     elseif not builder then
  634.                     brLine(sideLength)
  635.                     end
  636.             --square order--
  637.                 elseif pattern==2 then
  638.                 if builder then
  639.                 bSquare(sideLength,sideHeight)
  640.                 end
  641.                 if not builder then
  642.                 brSquare(sideLength,sideHeight)
  643.                 end
  644.             --floor order--
  645.                 elseif pattern==3 then
  646.                 if builder then
  647.                 bFloor(sideLength,sideWidth,sideHeight)
  648.                 end
  649.                 if not builder then
  650.                 brFloor(sideLength,sideWidth,sideHeight)
  651.                 end
  652.             --wall order--
  653.                 elseif pattern==4 then
  654.                 if builder then
  655.                 bWall(sideLength,sideHeight)
  656.                 end
  657.                 if not builder then
  658.                 brWall(sideLength,sideHeight)
  659.                 end
  660.                 end
  661.                 term.setCursorPos(2,6)
  662.                 term.write("Operation Finished")
  663.                 os.sleep(1)
  664.                 running=false              
  665.             end
  666.         --end of the main loop area--
  667.         end
  668.         if choice==7 then
  669.         term.clear()
  670.             error("thank you for using Turtle Builder!")
  671.         end
  672.    
  673.         end
  674.     if event== "key" and param1==205 and choice==8 then
  675.      sideLength=sideLength+1
  676.      end
  677.     if event== "key" and param1==203 and choice==8 then
  678.      sideLength=sideLength-1
  679.      if sideLength<=0 then
  680.         sideLength=0
  681.         end
  682.     end
  683.     if event== "key" and param1==205 and choice==9 then
  684.      sideHeight=sideHeight+1
  685.      end
  686.     if event== "key" and param1==203 and choice==9 then
  687.      sideHeight=sideHeight-1
  688.      if sideHeight<=0 then
  689.         sideHeight=0
  690.         end
  691.         end
  692.     if event== "key" and param1==205 and choice==10 then
  693.      sideWidth=sideWidth+1
  694.      end
  695.     if event== "key" and param1==203 and choice==10 then
  696.      sideWidth=sideWidth-1
  697.      if sideWidth<=0 then
  698.         sideWidth=0
  699.         end
  700.     end
  701.     end
Add Comment
Please, Sign In to add comment