Ion_Silverbolt

waterFerret

May 20th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.75 KB | None | 0 0
  1. --[[
  2.  
  3. The Water Ferret - by Paul Olinger
  4.  
  5. Ever been deep under the ocean and wish there was an easier way to
  6. go up and back down? Is your nearest stronghold buried deep under
  7. the ocean? Or maybe you just want to have a deep underwater secret
  8. base?
  9.  
  10. Introducing the water ferret program. This program will go down to
  11. the deep ocean depths and build an access pipe straight up for you.
  12. Ideal for trying to reach underground mineshafts, strongholds, or
  13. other hard to reach places deep under the ocean. As an added bonus,
  14. it also works on land, and even in lava.
  15.  
  16. The program starts out by plummeting down until it detects a block
  17. which determines where the bottom of the ocean floor is. From there
  18. , the turtle pierces the ground and builds underground. This is so
  19. you can mine straight off your new tunnel without hitting water.
  20.  
  21. Just running the program without arguments works fine, but there
  22. are some commands that help improve the building process...
  23.  
  24.  
  25. ARGUMENTS:
  26.  
  27. [goto] [x] [y]
  28. The goto argument tells your turtle where to go to potentially
  29. start drilling. Placing a turtle down in the middle of the ocean
  30. isn't that easy unless you have a lilly pad, or some other way of
  31. getting a block there to place the turtle down. This argument
  32. exists so you can tell your turtle where to move to.  
  33.  
  34. If you use the goto argument, the next two arguments MUST be
  35. numeric values for the coordinates. The turtle's starting location
  36. is considered x: 0 y: 0, so you determine where you want to move
  37. from there.
  38.  
  39. Example: waterFerret goto 140 -15
  40. This will move the turtle forward 140 places, then left 15 places.
  41. If you use the goto argument, the  next two arguments must be
  42. numeric values for the x and y locations. If you don't wish to move
  43. x or y, then enter a value of zero for that coordinate.
  44.  
  45.  
  46.  
  47. [force]
  48. The force argument tells the turtle to immediately start tunneling
  49. after it reaches the entered goto x and y coordinates.
  50.  
  51. Example: waterFerret goto 10 5 force
  52. This will move the turtle forward 10 places, then right 5 places,
  53. then it will start tunneling.
  54.  
  55.  
  56.  
  57. [dock]
  58. The dock option will append a small area next to the access pipe
  59. where you can dock your boat. Nothing fancy, but enough to keep
  60. your boat from drifting away.
  61.  
  62. Example: waterFerret dock
  63.  
  64.  
  65.  
  66. [pierce] [z]
  67. Using the pierce argument allows you to change the penetration
  68. depth. The default value is 6. A value of zero would build it right
  69. at the ocean floor surface. The argument after pierce determines
  70. your depth. A negative value here also works, which will build
  71. above the ocean surface somewhere. Not sure how that might be
  72. useful, but it works. Setting a high value for pierceDepth is
  73. useful if you're wanting to use this program for land penetration.
  74.  
  75. Example: waterFerret dock pierce 10
  76. This would build a dock, then burrow down under the ocean floor 10
  77. blocks.
  78.  
  79.  
  80. SLOT PLACEMENT:
  81.  
  82. Slots 1 - 13 are for structure blocks.
  83. Slot 14 is reserved for torches.
  84. Slot 15 and 16 are for ladders.
  85.  
  86.  
  87.  
  88. PROGRAM HIGHLIGHTS:
  89.  
  90. I have implemented a lot of failsafes for the program so that the
  91. turtle does not stall at the bottom of an abyss somewhere. Whenever
  92. the turtle needs something, it will make it's way to the surface.
  93.  
  94. The turtle will come to the surface to be refueled, or to get new
  95. blocks if it happens to run out. Once refueled or restocked, the
  96. turtle will resume building.
  97.  
  98. This program handles obstacles fine, including sand, mobs, etc. If
  99. the turtle happens to hit bedrock, it will return home and abort
  100. the mission. This will happen early on, as the turtle starts from
  101. the bottom up.
  102.  
  103.  
  104. PITFALLS:
  105.  
  106. Do not leave any block slot empty. Doing so, the turtle may pick up
  107. rubble from the ocean floor and then start using that for building.
  108.  
  109. This program does not do item checking for torches and ladders. I
  110. will probably add this soon, as it's fairly trivial to add, but for
  111. now, make sure the turtle has enough. Even so, this isn't a huge
  112. deal, as the pipe is pretty much done by the time the program gets
  113. this far.
  114.  
  115. Bedrock if you happen to hit it. If the turtle hits bedrock, it
  116. will return to the surface and shut down. This will happen early
  117. in the program if you're going to hit bedrock.
  118.  
  119.  
  120. Happy tunneling!
  121.  
  122. ]]--
  123.  
  124.  
  125.  
  126. local arg = { ... }
  127.  
  128. --defaults
  129. gotoX = nil
  130. gotoY = nil
  131.  
  132. length = 5
  133. width = 5
  134. slot = 1
  135. ctr = 0
  136. check = 1
  137. ladderDetect = false
  138. dock = false
  139. goto = false
  140. force = false
  141. pierceDepth = 6
  142. fuelAlert = false
  143.  
  144. for i = 1, 7 do
  145.     if arg[i] == "dock" then
  146.         dock = true
  147.     end
  148.    
  149.     if arg[i] == "goto" then
  150.         goto = true
  151.         gotoX = tonumber(arg[i +1])
  152.         gotoY = tonumber(arg[i +2])
  153.     end
  154.    
  155.     if arg[i] == "pierce" then
  156.         pierceDepth = tonumber(arg[i +1])
  157.     end
  158.  
  159.     if arg[i] == "force" then
  160.         force = true
  161.     end    
  162. end
  163.        
  164. --home coordinates
  165. homeF = 1   -- 1 == north, 2 east, etc  
  166. homeX = 0
  167. homeY = 0
  168. homeZ = 0
  169.  
  170. --away coordinates
  171. awayF = 1
  172. awayX = 0
  173. awayY = 0
  174. awayZ = 0
  175.  
  176. --current coordinates
  177. curF = 1
  178. curX = 0
  179. curY = 0
  180. curZ = 0
  181.  
  182. --inner coordinates
  183. innerF = 0
  184. innerX = 1
  185. innerY = 1
  186. innerZ = -1
  187.  
  188. --sweep coordinates
  189. sweepZ = 0
  190.  
  191. --Emergency refill coordinates
  192. emerF = 0
  193. emerX = 0
  194. emerY = 0
  195. emerZ = 0
  196.  
  197.  
  198. function pushForward()
  199.     local deathcount = 0
  200.     while not turtle.forward() do
  201.         turtle.dig()
  202.         turtle.attack()
  203.         deathcount = deathcount + 1
  204.         if deathcount == 20 then
  205.             goHome()
  206.             print("Bedrock detected. Program aborted.")
  207.             error()
  208.         end
  209.     end
  210.    
  211.     if curF == 1 then
  212.         curX = curX + 1
  213.     end
  214.    
  215.     if curF == 2 then
  216.         curY = curY + 1
  217.     end
  218.    
  219.     if curF == 3 then
  220.         curX = curX - 1
  221.     end
  222.    
  223.     if curF == 4 then
  224.         curY = curY - 1
  225.     end
  226.     if not fuelAlert then
  227.         checkFuelLevel()
  228.     end
  229. end
  230. function pushUpward()  
  231.     local deathcount = 0
  232.    
  233.     while not turtle.up() do
  234.         turtle.digUp()
  235.         turtle.attackUp()
  236.         deathcount = deathcount + 1
  237.        
  238.         if deathcount == 20 then
  239.             goHome()
  240.             print("Bedrock detected. Program aborted.")
  241.             error()
  242.         end
  243.     end
  244.     curZ = curZ + 1
  245.     if not fuelAlert then
  246.         checkFuelLevel()
  247.     end
  248. end
  249. function pushDownward()
  250. local deathcount = 0
  251.     while not turtle.down() do
  252.         turtle.digDown()
  253.         turtle.attackDown()
  254.         deathcount = deathcount + 1
  255.                
  256.         if deathcount == 20 then
  257.             goHome()
  258.             print("Bedrock detected. Program aborted.")
  259.             error()
  260.         end
  261.     end
  262.     curZ = curZ - 1
  263.    
  264.     if not fuelAlert then
  265.         checkFuelLevel()
  266.     end
  267. end
  268. function left()
  269.     turtle.turnLeft()
  270.     if curF == 1 then
  271.         curF = 4
  272.  
  273.     elseif curF == 2 then
  274.         curF = 1
  275.  
  276.     elseif curF == 3 then
  277.         curF = 2
  278.  
  279.     elseif curF == 4 then
  280.         curF = 3
  281.     end
  282. end
  283. function right()
  284.     turtle.turnRight()
  285.     if curF == 1 then
  286.         curF = 2
  287.    
  288.     elseif curF == 2 then
  289.         curF = 3
  290.    
  291.     elseif curF == 3 then
  292.         curF = 4
  293.    
  294.     elseif curF == 4 then
  295.         curF = 1
  296.     end
  297. end
  298. function recordAway()
  299.     awayF = curF
  300.     awayX = curX
  301.     awayY = curY
  302.     awayZ = curZ
  303. end
  304. function recordEmergency()
  305.     emerF = curF
  306.     emerX = curX
  307.     emerY = curY
  308.     emerZ = curZ
  309. end
  310. function goHome()
  311.     --Go to z axis center
  312.     while curZ < (homeZ) do
  313.         pushUpward()
  314.     end
  315.            
  316.     -- Go to y axis center
  317.     while curY < homeY do
  318.         while curF ~= 2 do
  319.             left()
  320.         end
  321.         pushForward()
  322.     end
  323.    
  324.     while curY > homeY do
  325.         while curF ~= 4 do
  326.             left()
  327.         end
  328.         pushForward()
  329.     end
  330.    
  331.     --Go to x axis center
  332.     while curX < homeX do
  333.         while curF ~= 1 do
  334.             left()
  335.         end
  336.         pushForward()
  337.     end
  338.    
  339.     while curX > homeX do
  340.         while curF ~= 3 do
  341.             left()
  342.         end
  343.         pushForward()
  344.     end    
  345.            
  346.     while curF ~= 3 do
  347.         left()
  348.     end
  349.  
  350.     while curZ > homeZ do
  351.         pushDownward()
  352.     end
  353.     right()
  354.     right()
  355. end
  356. function widthEven()
  357.     left()
  358.     left()
  359.     for i=2, width do
  360.         pushForward()
  361.     end
  362.     right()
  363. end
  364. function widthOdd()
  365.     right()
  366. end
  367. function waterPlunge()
  368.     print("Looking for the ocean bottom...")
  369.     while not turtle.detectDown() do
  370.         pushDownward()
  371.     end
  372.     print("Depth determined. Penetrating " .. pierceDepth .. " blocks deep.")
  373.     awayZ = (curZ - pierceDepth)
  374.     goAway()
  375. end
  376. function goAway()
  377.     --Go to z axis center
  378.     while curZ < awayZ do
  379.         pushUpward()
  380.     end    
  381.    
  382.     while curZ > awayZ do
  383.         pushDownward()
  384.     end
  385.    
  386.     -- Go to y axis center
  387.     while curY < awayY do
  388.         while curF ~= 2 do
  389.             left()
  390.         end
  391.         pushForward()
  392.     end
  393.    
  394.     while curY > awayY do
  395.         while curF ~= 4 do
  396.             left()
  397.         end
  398.         pushForward()
  399.     end
  400.    
  401.     --Go to x axis center
  402.     while curX < awayX do
  403.         while curF ~= 1 do
  404.             left()
  405.         end
  406.         pushForward()
  407.     end
  408.            
  409.     while curX > awayX do
  410.         while curF ~= 3 do
  411.             left()
  412.         end
  413.         pushForward()
  414.     end    
  415.            
  416.     while curF ~= awayF do
  417.         left()
  418.     end
  419.  
  420.     while curZ > awayZ do
  421.         pushDownward()
  422.     end
  423. end
  424. function goInner()    
  425.     pushDownward()
  426.     -- Go to y axis center
  427.     while curY < innerY do
  428.         while curF ~= 2 do
  429.             left()
  430.         end
  431.         pushForward()
  432.     end
  433.    
  434.     while curY > innerY do
  435.         while curF ~= 4 do
  436.             left()
  437.         end
  438.         pushForward()
  439.     end
  440.    
  441.     --Go to x axis center
  442.     while curX < innerX do
  443.         while curF ~= 1 do
  444.             left()
  445.         end
  446.         pushForward()
  447.     end
  448.            
  449.     while curX > innerX do
  450.         while curF ~= 3 do
  451.             left()
  452.         end
  453.         pushForward()
  454.     end    
  455.            
  456.     while curF ~= innerF do
  457.         left()
  458.     end
  459.     --Only ever runs once
  460.     if check == 1 then
  461.         pushDownward()
  462.         check = nil
  463.     end
  464.    
  465. end
  466. function goEmergency()
  467. print("Returning to work...")
  468.     -- Go to y axis center
  469.     while curY < emerY do
  470.         while curF ~= 2 do
  471.             left()
  472.         end
  473.         pushForward()
  474.     end
  475.    
  476.     while curY > emerY do
  477.         while curF ~= 4 do
  478.             left()
  479.         end
  480.         pushForward()
  481.     end
  482.    
  483.     --Go to x axis center
  484.     while curX < emerX do
  485.         while curF ~= 1 do
  486.             left()
  487.         end
  488.         pushForward()
  489.     end
  490.    
  491.     while curX > emerX do
  492.         while curF ~= 3 do
  493.             left()
  494.         end
  495.         pushForward()
  496.     end    
  497.            
  498.     while curF ~= 3 do
  499.         left()
  500.     end
  501.  
  502.     --Go to z axis center
  503.     while curZ < emerZ do
  504.         pushUpward()
  505.     end
  506.            
  507.     while curZ > emerZ do
  508.         pushDownward()
  509.     end
  510.    
  511.     while curF ~= emerF do
  512.         right()
  513.     end
  514. end
  515. function buildWall()
  516.     turtle.select(slot)        
  517.     local depth = awayZ
  518.     while depth ~= homeZ + 1 do  
  519.        
  520.         --Every 4 passes will reset slot to 1 in case the turtle
  521.         -- is filled with blocks while running        
  522.         if depth % 4 == 1 then
  523.             slot = 1
  524.             turtle.select(slot)
  525.         end
  526.         --repeats the next for loops twice to make a complete pass
  527.         for squareCount = 1, 2 do              
  528.             for count = 2, length do
  529.                 pushForward()
  530.                 checkInventorySlot()
  531.                 if turtle.detectDown() then
  532.                     turtle.digDown()
  533.                 end
  534.                 --This skips the corners to save blocks
  535.                 if count ~= length then
  536.                         turtle.placeDown()
  537.                 end
  538.             end
  539.             right()
  540.            
  541.             for count = 2, width do
  542.                 pushForward()
  543.                 checkInventorySlot()
  544.                 if turtle.detectDown() then
  545.                     turtle.digDown()
  546.                 end
  547.                 if count ~= width then
  548.                     turtle.placeDown()
  549.                 end
  550.             end
  551.             right()
  552.         end
  553.         pushUpward()
  554.         depth = depth + 1
  555.     end        
  556. end
  557. function clearBottom()
  558.     turtle.select(slot)    
  559.     local widthCounter = 1
  560.     local directionCounter = 2
  561.     local innerWidth = width - 2
  562.     local innerLength = length - 2        
  563.    
  564.     while widthCounter <= innerWidth do
  565.         --Every 4 passes will reset slot to 1 if turtle is filled
  566.         --with blocks while running
  567.         if widthCounter % 4 == 1 then
  568.             slot = 1
  569.             turtle.select(slot)
  570.         end
  571.                                                                        
  572.         --for loop goes forward to the value entered for length
  573.         for i=2, innerLength do
  574.             if ctr == 0  and (not ladderDetect) then
  575.                 turtle.digDown()
  576.             end
  577.            
  578.             checkInventorySlot()
  579.             if ctr == 0 then
  580.                 turtle.placeDown()
  581.             end
  582.             pushForward()
  583.         end
  584.                                                                    
  585.         if directionCounter % 2 ~= 1 then
  586.             right()
  587.             if ctr == 0 then
  588.                 if (not ladderDetect) then
  589.                     turtle.digDown()
  590.                 end
  591.                 checkInventorySlot()
  592.                 turtle.placeDown()
  593.             end
  594.  
  595.             if widthCounter < innerWidth then
  596.                 pushForward()
  597.                 right()
  598.             end
  599.    
  600.         else
  601.             left()
  602.             if ctr == 0 then
  603.                 if not ladderDetect then
  604.                     turtle.digDown()
  605.                 end
  606.                 checkInventorySlot()
  607.                 turtle.placeDown()
  608.             end
  609.                
  610.             if widthCounter < innerWidth then
  611.                 pushForward()
  612.                 left()
  613.             end
  614.         end
  615.         widthCounter = widthCounter + 1
  616.         directionCounter = directionCounter + 1
  617.     end
  618.  
  619.     if innerWidth % 2 == 0 then
  620.             widthEven()
  621.     else
  622.             widthOdd()
  623.     end
  624.     ctr = nil
  625. end
  626. function recordBottom()
  627.     innerF = curF
  628.     innerX = curX
  629.     innerY = curY
  630. end
  631. function checkInventorySlot()
  632.            
  633.     local toggle --Gatekeeper. Must be set to "go" before turtle will continue.
  634.            
  635.     while turtle.getItemCount(slot) == 1 do
  636.         slot = slot + 1
  637.         --Turtle is empty. Fill the slots and reset slot to 1.
  638.         if slot == 14 then
  639.             recordEmergency()
  640.             goHome()
  641.             print("\nTurtle needs more blocks! Please reload.")
  642.             print("Type 'go' to continue after reloading")
  643.             toggle = io.read()
  644.                        
  645.             while toggle ~= "go" do
  646.                 print("Please type 'go' and hit enter")
  647.                 toggle = io.read()
  648.             end
  649.            
  650.             --Setting slot back to one after turtle is refilled and returning slot back to 2
  651.             slot = 1
  652.             turtle.select(slot)
  653.             goEmergency()
  654.         end
  655.  
  656.         --switches to the next slot    
  657.         turtle.select(slot)
  658.     end    
  659.    
  660. end
  661. function checkFuelLevel()
  662.     if turtle.getFuelLevel() < 150 then
  663.         recordEmergency()
  664.         fuelAlert = true
  665.         goHome()
  666.     end
  667.    
  668.     while turtle.getFuelLevel() < 150 do
  669.         print("\nTurtle needs more Fuel! Insert fuel sources in any slot. Then,")
  670.         print("replace your slots with building materials when you're done.")
  671.        
  672.         print("Type 'go' to begin refueling.")
  673.         toggle = io.read()
  674.        
  675.         while toggle ~= "go" do
  676.             print("Please type 'go' and hit enter")
  677.             toggle = io.read()
  678.         end
  679.         refuelTurtle()
  680.     end    
  681.    
  682.     if turtle.getFuelLevel() > 150 and fuelAlert then                  
  683.              
  684.         print("Turtle refilled. Type 'go' to return to the depths ")
  685.         print("after reloading building materials.")
  686.         toggle = io.read()
  687.                        
  688.         while toggle ~= "go" do
  689.             print("Please type 'go' and hit enter")
  690.             toggle = io.read()
  691.         end
  692.         fuelAlert = false
  693.         goEmergency()
  694.     end
  695. end
  696. function refuelTurtle()
  697.     for i = 1, 16 do
  698.         turtle.select(i)
  699.         turtle.refuel()
  700.     end
  701.     turtle.select(slot)
  702. end
  703. function buildBoatDock()
  704.     print("Building dock...")
  705.     left()
  706.     pushForward()
  707.     turtle.placeDown()
  708.     pushForward()
  709.     turtle.placeDown()
  710.     pushForward()
  711.     turtle.placeDown()
  712.     pushForward()
  713.     turtle.placeDown()
  714.     right()
  715.     pushUpward()
  716.     turtle.select(14)
  717.     turtle.placeDown()
  718.     turtle.select(slot)
  719.     pushForward()
  720.     pushDownward()
  721.     pushForward()
  722.     pushForward()
  723.     pushForward()
  724.     right()
  725.     turtle.placeDown()
  726.     pushUpward()
  727.     turtle.select(14)
  728.     turtle.placeDown()
  729.     pushForward()
  730.     pushDownward()
  731.     turtle.select(slot)
  732.     turtle.placeDown()
  733.     pushForward()
  734.     turtle.placeDown()
  735.     pushForward()
  736.     turtle.placeDown()
  737.     pushForward()
  738.     print("Dock completed.")
  739.     goHome()
  740. end
  741. function buildLadder()
  742.     local num = 3
  743.     pushForward()
  744.     right()
  745.     pushForward()
  746.     if (curZ + 200) % 2 == 1 then
  747.         right()
  748.         right()
  749.     end
  750.            
  751.     while curZ ~= (homeZ) do
  752.         turtle.select(15)
  753.         if turtle.select(15) == 0 then
  754.             turtle.select(16)
  755.         end
  756.         turtle.place()
  757.         pushUpward()
  758.         if num % 5 == 0 and (curZ ~= homeZ - 1) then
  759.             right()
  760.             right()
  761.             turtle.select(14)
  762.             turtle.place()
  763.             right()
  764.             right()
  765.         end
  766.         num = num + 1
  767.     end
  768. end
  769. function findStartingPoint()
  770.     --Go to x axis center
  771.     while curX < gotoX do
  772.         while curF ~= 1 do
  773.             left()
  774.         end
  775.         pushForward()
  776.     end
  777.    
  778.     while curX > gotoX do
  779.         while curF ~= 3 do
  780.             left()
  781.         end
  782.         pushForward()
  783.     end    
  784.            
  785.     while curF ~= 3 do
  786.         left()
  787.     end
  788.            
  789.     -- Go to y axis center
  790.     while curY < gotoY do
  791.         while curF ~= 2 do
  792.             left()
  793.         end
  794.         pushForward()
  795.     end
  796.    
  797.     while curY > gotoY do
  798.         while curF ~= 4 do
  799.             left()
  800.         end
  801.         pushForward()
  802.     end
  803. end
  804.  
  805.  
  806. --******************************* Main
  807.  
  808.  
  809. if goto then
  810.     findStartingPoint()
  811.     while curF ~= 1 do
  812.         right()
  813.     end
  814.     if force then
  815.         curX = 0
  816.         curY = 0
  817.     end
  818. end
  819.  
  820.  
  821. --Remove initial marker block just in case it was left.
  822. if turtle.detectDown() then
  823.         turtle.digDown()
  824. end
  825.  
  826.  
  827.  
  828. -- Will not run with the goto option unless force is true
  829. if not goto  or force then
  830.     print("Turtle is on a mission...")
  831.     turtle.select(slot)
  832.     if dock then
  833.         buildBoatDock()
  834.     end
  835.    
  836.     waterPlunge()
  837.     right()
  838.     pushForward()
  839.     left()
  840.     pushForward()
  841.     recordBottom()
  842.     clearBottom()
  843.     goAway()
  844.     buildWall()
  845.     goInner()
  846.  
  847.     while curZ ~= awayZ do
  848.         clearBottom()
  849.         pushDownward()
  850.     end
  851.     clearBottom()
  852.     buildLadder()
  853.     goHome()
  854.     pushForward()
  855.     right()
  856.     pushForward()
  857.     left()
  858.    
  859.     --New values for clearBottom()
  860.     ctr = 0
  861.     ladderDetect = true
  862.     clearBottom()
  863.    
  864.     --Just a few remaining details with torches
  865.     turtle.select(14)
  866.     right()
  867.     right()
  868.     turtle.place()
  869.     right()
  870.     right()
  871.     pushForward()
  872.     pushForward()
  873.     turtle.place()
  874.     right()
  875.     pushForward()
  876.     goHome()
  877.     print("Mission Completed!")
  878.     print(turtle.getFuelLevel() .. " units of fuel remaining.")
  879. end
Add Comment
Please, Sign In to add comment