Advertisement
Goldman92

mine

Sep 20th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.78 KB | None | 0 0
  1. --==================================================================--
  2. --==================================================================--
  3. --  Holds the basics to be used in future turtle projects           --
  4. --  Includes:                                                       --
  5. --  1. movement in the six directions                               --
  6. --  2. picking up and uses fuel from chest                          --
  7. --  3. empty entire turtle contents to chest in front of it         --
  8. --                                                                  --
  9. --                                                                  --
  10. --                                                                  --
  11. --                                                                  --
  12. --                                                                  --
  13. --                                                                  --
  14. --  author: jhurst      created:            edited:                 --
  15. --==================================================================--
  16. --==================================================================--
  17. local d, x, y, z = 1, 0, 0, 0
  18.  
  19. --===============================================================--
  20. --1. movement in the six directions                              --
  21. --===============================================================--
  22. function moveForward(count)
  23. --
  24. --moves forward a number of blocks
  25. --CAN handle sand/gravel
  26. --counts the position
  27. --
  28.  
  29.        
  30. check()
  31. for a = 1, count, 1 do
  32.  
  33.     if d == 1 then
  34.         y = y + 1
  35.     end
  36.     if d == 2 then
  37.         x = x + 1
  38.     end
  39.     if d == 3 then
  40.         y = y - 1
  41.     end
  42.    
  43.     if d == 4 then
  44.         x = x - 1
  45.     end
  46.    
  47.     turtle.dig()
  48.     while not turtle.forward() do
  49.     turtle.dig()
  50.     turtle.attack()
  51.     os.sleep(1)
  52.     end
  53. end
  54. end
  55. function moveBack(count)
  56. --
  57. --moves back a number of blocks
  58. --CAN handle sand/gravel
  59. --
  60. for b = 1, count, 1 do
  61.     if not turtle.back() then
  62.         if d == 1 then
  63.         y = y - 1
  64.         end
  65.         if d == 3 then
  66.         y = y + 1
  67.         end
  68.         if d == 2 then
  69.         x = x - 1
  70.         end
  71.         if d == 4 then
  72.         x = x + 1
  73.         end
  74.    
  75.         turtle.turnRight()
  76.         turtle.turnRight()
  77.         while not turtle.forward() do
  78.         turtle.dig()
  79.         turtle.attack()
  80.         os.sleep(1)
  81.         end
  82.         turtle.turnRight()
  83.         turtle.turnRight()
  84.        
  85.     end
  86.     end
  87. end
  88. function moveR(count)
  89. --
  90. --turns and moves right a number of blocks
  91. --CAN handle sand/gravel
  92. --counts the direction: N=1 and so forth
  93. --
  94.  
  95.  
  96. turtle.turnRight()
  97.     d = d + 1
  98.     if d == 5 then
  99.     d = 1
  100.     end
  101.  
  102.     for c = 1, count, 1 do
  103.        
  104.         while turtle.detect() do
  105.         turtle.dig()
  106.         os.sleep(1)
  107.         end
  108.         moveForward(1)
  109.    
  110.     end
  111. end
  112. function moveL(count)
  113. --
  114. --turns and moves left a number of blocks
  115. --CAN handle sand/gravel
  116. --
  117. turtle.turnLeft()
  118.     d = d - 1
  119.     if d == 0 then
  120.     d = 4
  121.     end
  122.     for aa = 1, count, 1 do
  123.        
  124.    
  125.         while turtle.detect() do
  126.         turtle.dig()
  127.         os.sleep(1)
  128.         end
  129.         moveForward(1)
  130.     end
  131. end
  132. function moveDown(count)
  133. --
  134. --moves down by destroying the block underneath it
  135. --
  136. for e = 1, count, 1 do
  137.  
  138.     z = z - 1
  139.  
  140.     while not turtle.down() do
  141.     turtle.digDown()
  142.     turtle.attackDown()
  143.     os.sleep(1)
  144.     end
  145.     end
  146. end
  147. function moveUp(count)
  148. --
  149. --moves up by destroying block above it
  150. --does not handle sand/gravel on top
  151. --
  152. for f = 1, count, 1 do
  153.     z = z + 1
  154.  
  155.     while not turtle.up() do
  156.     turtle.digUp()
  157.     turtle.attackUp()
  158.     os.sleep(1)
  159.     end
  160. end
  161. end
  162. --===============================================================--
  163. --2. picking up fuel from chest and fuelling from any slot       --
  164. --===============================================================--
  165. function fuelChest()
  166. --
  167. --picks up fuel from chest in FRONT of it and refuels
  168. --
  169.     turtle.select(1)
  170.     turtle.suck()
  171.     fuel()
  172. end
  173. function fuel()
  174. --
  175. --fuels from first burnable item in inventory
  176. --
  177.     fuelSlot = 1
  178.     turtle.select(fuelSlot)
  179.    
  180.     while not turtle.refuel() do
  181.         fuelSlot = fuelSlot + 1
  182.         turtle.select(fuelSlot)
  183.     end
  184.     turtle.select(1)
  185. end
  186. --===============================================================--
  187. --3. empty entire turtle contents to chest in front of it        --
  188. --===============================================================--
  189. function emptyToChest()
  190. --
  191. --empties entire contents of turtle to chest in FRONT of it
  192. --
  193.     for slot = 2, 16, 1 do
  194.     turtle.select(slot)
  195.         turtle.drop()
  196.     end
  197.     turtle.select(1)
  198.     if turtle.getItemCount(1) >1 then
  199.     turtle.drop(turtle.getItemCount(1)-1)
  200.     end
  201. end
  202. --===============================================================--
  203. --4. pitStop() bringing turtle to original position              --
  204. --===============================================================--
  205. function pitStop(num)
  206.    
  207.     --keeps track of original coordinates
  208.     local oldx = x
  209.     local oldy = y
  210.     local oldz = z
  211.     local oldd = d
  212.    
  213.    
  214.     --turns turtle to point south or d = 3
  215.     if d ~= 3 then
  216.         if d == 1 then
  217.             turtle.turnRight()
  218.             turtle.turnRight()
  219.            
  220.         end
  221.         if d == 2 then
  222.             turtle.turnRight()
  223.         end
  224.         if d == 4 then
  225.             turtle.turnLeft()
  226.         end
  227.     end
  228.     d = 3
  229.     --moves turtle original starting position
  230.     moveForward(y)
  231.     turtle.turnRight()
  232.     d = 4
  233.     moveForward(x)
  234.     turtle.turnRight()
  235.     d = 1
  236.     moveUp(math.abs(z))
  237.    
  238.     --if low on fuel, num will be 0
  239.     --empties inventory then refuels
  240.     if num == 0 then
  241.     turtle.turnLeft()
  242.     emptyToChest()
  243.     turtle.turnLeft()
  244.     fuelChest()
  245.     turtle.turnLeft()
  246.     turtle.turnLeft()
  247.     end
  248.    
  249.     --if inventory full, num will be 1
  250.     --empties inventory
  251.     if num == 1 then
  252.     turtle.turnLeft()
  253.     emptyToChest()
  254.     turtle.turnRight()
  255.     end
  256.    
  257.     --returns turtle to spot to resume digging
  258.     moveDown(math.abs(oldz))
  259.     turtle.turnLeft()
  260.     d = 4
  261.     moveBack(oldx)
  262.     turtle.turnLeft()
  263.     d = 3
  264.     moveBack(oldy)
  265.    
  266.     --faces turtle back to original direction
  267.     if oldd ~= 3 then
  268.         if oldd == 1 then
  269.             turtle.turnRight()
  270.             turtle.turnRight()
  271.         end
  272.         if oldd == 2 then
  273.             turtle.turnLeft()
  274.         end
  275.         if oldd == 4 then
  276.             turtle.turnRight()
  277.         end
  278.     end
  279.    
  280.     --resets coordinates to original values
  281.     d = oldd
  282.     y = oldy
  283.     x = oldx
  284.     z = oldz
  285. end
  286. function pitStopCheck()
  287. --returns to start if low fuel
  288.  
  289.             if (y + (x*2) - (z*2)) > turtle.getFuelLevel() - 10 then
  290.                 pitStop(0)
  291.             end
  292.             --returns to start if inventory is full
  293.             --total is total number of blocks in inventory
  294.             --totalOcc is number of used inventory slots in turtle
  295.             local total = 0
  296.             local totalOcc = 0
  297.             for check = 2, 16, 1 do
  298.                 total = total + turtle.getItemCount(check)
  299.                 if turtle.getItemCount(check) > 0 then
  300.                     totalOcc = totalOcc + 1
  301.                 end
  302.             end
  303.             if total > 950 or totalOcc >= 14 then
  304.                 pitStop(1)
  305.             end
  306. end
  307. function check()
  308.     turtle.select(1)
  309.     if not turtle.compareUp() then
  310.         turtle.digUp()
  311.     end
  312.     if not turtle.compareDown() then
  313.         turtle.digDown()
  314.     end
  315.     turtle.turnRight()
  316.     if not turtle.compare() then
  317.         turtle.dig()
  318.     end
  319.         turtle.turnLeft()
  320.         turtle.turnLeft()
  321.     if not turtle.compare() then
  322.         turtle.dig()
  323.     end
  324.     turtle.turnRight()
  325. end
  326. function mine(length, width, height)
  327. --unloads anything in inventory and fuels
  328. turtle.select(2)
  329. turtle.turnLeft()
  330. emptyToChest()
  331. turtle.turnLeft()
  332. fuelChest()
  333. turtle.turnLeft()
  334. turtle.turnLeft()
  335. moveDown(1)
  336.  
  337.     for h = 1, height, 1 do
  338.         for w = 1, width, 1 do
  339.             for l = 1, length - 1, 1 do
  340.             pitStopCheck()
  341.                 moveForward(1)
  342.             end
  343.             --turns turtle at the end of each row
  344.             --if row is odd, turn right; left otherwise
  345.             if math.fmod(h, 2) == 1 then
  346.                 if math.fmod(w, 2) == 1 then
  347.                     if w ~= width then
  348.                     moveR(3)
  349.                     turtle.turnRight()
  350.                         d = d + 1
  351.                         if d == 5 then
  352.                         d = 1
  353.                         end
  354.                     end
  355.                 else
  356.                     if w ~= width then
  357.                     moveL(3)
  358.                     turtle.turnLeft()
  359.                         d = d - 1
  360.                         if d == 0 then
  361.                         d = 4
  362.                         end
  363.                     end
  364.                 end
  365.             else
  366.                 if math.fmod(w, 2) == 0 then
  367.                     if w ~= width then
  368.                     moveR(3)
  369.                     turtle.turnRight()
  370.                         d = d + 1
  371.                         if d == 5 then
  372.                         d = 1
  373.                         end
  374.                     end
  375.                 else
  376.                     if w ~= width then
  377.                     moveL(3)
  378.                     turtle.turnLeft()
  379.                         d = d - 1
  380.                         if d == 0 then
  381.                         d = 4
  382.                         end
  383.                     end
  384.                 end
  385.             end
  386.         end
  387.         if h~=height then
  388.             if math.fmod(h, 2) == 1 then
  389.                 moveL(1)
  390.                 turtle.turnLeft()
  391.                     d = d + 1
  392.                     if d == 5 then
  393.                     d = 1
  394.                     end
  395.                 moveDown(2)
  396.                 else
  397.                 moveR(1)
  398.                 turtle.turnRight()
  399.                     d = d + 1
  400.                     if d == 5 then
  401.                     d = 1
  402.                     end
  403.                 moveDown(2)
  404.             end
  405.         else
  406.            
  407.         end
  408.     end
  409.     --returns turtle to original location
  410.     moveR(1)
  411.     turtle.turnRight()
  412.         d = d + 1
  413.         if d == 5 then
  414.         d = 1
  415.         end
  416.     moveUp(height*2 -1)
  417.     turtle.turnLeft()
  418.     emptyToChest()
  419.     turtle.turnRight()
  420. end
  421.  
  422. --prompts user for dimensions of quarry
  423. print("How many mines on each level?")
  424. print("(Even numbers only please:)")
  425. local ansWidth = tonumber(io.read())
  426. while math.fmod(ansWidth, 2) == 1 do
  427.     print("Please pick an even number:")
  428.     ansWidth = tonumber(io.read())
  429. end
  430. print("How many levels?")
  431. print("(Even numbers only please:)")
  432. local ansHeight = tonumber(io.read())
  433. while math.fmod(ansHeight, 2) == 1 do
  434.     print("Please pick an even number:")
  435.     ansHeight = tonumber(io.read())
  436. end
  437. print("How long for each mine?")
  438. local ansLength = tonumber(io.read())
  439. print("Please put any block you would like to ignore in the first turtle slot.")
  440. print("Type any key to continue and start the program:")
  441. io.read()
  442.  
  443.  
  444. mine(ansLength, ansWidth, ansHeight)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement