Smiley43210

Turtle Shaft Miner

May 13th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | None | 0 0
  1. --[[ Title: Advanced Wireless Mining Turtle (TurtleSide)
  2.  
  3. Description: Mines a shaft and places ladders.
  4. Excavates a 3 wide and 16 high passage for mining.
  5. It knows the pattern to expose all blocks in a chunk.
  6. and is able to make a walk way for accessing each tunnel.
  7. It will hopefully do this while taking commands from
  8. a computer with a wireless modem.
  9.  
  10. Author: margeobur (some code is based on default programs
  11. for things like digging the mineshaft) ]]
  12.  
  13. local tArgs = { ... }
  14. if #tArgs ~= 1 then
  15.   print("Usage: TurtleMining <Monitor Computer's ID> ")
  16.   return
  17. end
  18.  
  19. local monitorID = tonumber( tArgs[1] )
  20. if monitorID < 0 then
  21.   print("Error: Cannot have negative ID")
  22. end
  23.  
  24.  
  25. -- ***********************Functions**********************************
  26.  
  27. --*******************Monitor Communication*****************
  28.  
  29. local function receive()
  30.   while true do
  31.     local sender, message, distance = rednet.receive()
  32.     if sender ~= monitorID then
  33.       rednet.send(sender, "You are not my monitor!")
  34.     end
  35.  
  36.     if message == "CheckStatus" then
  37.       local Status = getStatus()
  38.       local sStatus = textutils.serialze(Status)
  39.       rednet.send(monitorID, sStatus)
  40.  
  41.     elseif message == "StopMining" then
  42.       command = 2
  43.       break
  44.  
  45.     else
  46.       rednet.send(monitorID, "Unknown command")
  47.  
  48.     end
  49.   end
  50.   return
  51. end
  52.  
  53. local function getStatus()
  54.   local x, y, z = gps.locate(4)
  55.   if not x then
  56.     x, y, z = "Could not locate", "Could not locate", "Could not locate"
  57.   end
  58.   local fuel = turtle.getFuelLevel()
  59.   local inv_room = 0
  60.   for i = 4, 9 do
  61.     inv_room = inv_room + turtle.getItemSpace(i)
  62.   end
  63.   local tStatus = readonlytable { fuel, blocks_mined, stage, inv_room, x, y, z }
  64.  
  65.   return tStatus
  66. end
  67.  
  68.  
  69. --*********Digging Mineshaft/Making Mine Room*************
  70.  
  71.  
  72. local function collect()
  73.   blocks_mined = blocks_mined + 1
  74.  
  75.   for n=1,9 do
  76.     if turtle.getItemCount(n) == 0 then
  77.       return true
  78.     end
  79.   end
  80.  
  81.   print( "No empty slots left." )
  82.   return false
  83. end
  84.  
  85.  
  86. local function tryForwards()
  87.   while not turtle.forward() do
  88.     if turtle.dig() then
  89.       if not collect() then
  90.         return false
  91.       end
  92.     else
  93.       -- give sand a chance to fall
  94.       sleep(0.8)
  95.       if turtle.dig() then
  96.         if not collect() then
  97.           return false
  98.         end
  99.       else
  100.         return false
  101.       end
  102.     end
  103.   end
  104.   xPos = xPos + xDir
  105.   zPos = zPos + zDir
  106.   return true
  107. end
  108.  
  109.  
  110. local function tryDown()
  111.   if not turtle.down() then
  112.     if turtle.digDown() then
  113.       if not collect() then
  114.         return false
  115.       end
  116.     end
  117.     if not turtle.down() then
  118.       return false
  119.     end
  120.   end
  121.   depth = depth - 1
  122.   return true
  123. end
  124.  
  125.  
  126. local function turnLeft()
  127.   turtle.turnLeft()
  128.   xDir, zDir = -zDir, xDir
  129. end
  130.  
  131. local function turnRight()
  132.   turtle.turnRight()
  133.   xDir, zDir = zDir, -xDir
  134. end
  135.  
  136. local function placeLadders()
  137.   print("place de laddars")
  138. end
  139.  
  140. local function DigShaft()
  141.   stop = false
  142.   depth = 0
  143.   blocks_mined = 0
  144.  
  145.   xPos,zPos = 0,0
  146.   xDir,zDir = 0,1
  147.   alternate = 0
  148.  
  149.   stage = "Digging Shaft"
  150.  
  151.   print("Digging de shaft...")
  152.  
  153.   while not stop do
  154.     for n = 1,3 do
  155.       for m = 1,2 do
  156.         if not tryForwards() then
  157.           stop = true
  158.           break
  159.         end
  160.       end
  161.       if stop then
  162.         break
  163.       end
  164.       if n < 3 then
  165.         if math.fmod(n + alternate,2) == 0 then
  166.           turnLeft()
  167.           if not tryForwards() then
  168.             stop = true
  169.             break
  170.           end
  171.           turnLeft()
  172.         else
  173.           turnRight()
  174.           if not tryForwards() then
  175.             stop = true
  176.             break
  177.           end
  178.           turnRight()
  179.         end
  180.       end
  181.     end
  182.    
  183.     if stop then
  184.       break
  185.     end
  186.    
  187.    
  188.     if alternate == 0 then
  189.       turnLeft()
  190.     else
  191.       turnRight()
  192.     end
  193.    
  194.     alternate = 1 - alternate
  195.    
  196.     if not tryDown() then
  197.       stop = true
  198.       break
  199.     end
  200.    
  201.     if command == 2 then
  202.       print("Master has told us to stop. Stopping...")
  203.       stop = true
  204.       break
  205.     end
  206.    
  207.   end
  208.   return
  209. end
  210.  
  211.  
  212. --*********************Driver Program:*******************************
  213.  
  214.  
  215. print("Welcome to margeobur's Advanced Turtle ")
  216. print("mining program (turtleside interface)  ")
  217. print()
  218.  
  219. rednet.open("right")
  220. command = 0
  221.  
  222. parallel.waitForAll (DigShaft, receive)
Advertisement
Add Comment
Please, Sign In to add comment