Guest User

Untitled

a guest
Aug 30th, 2012
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. --[[
  2.  
  3.     Vertical shaft miner 0.1
  4.         Copyleft Blastbot 2012
  5.        
  6.     Description:
  7.    
  8.         Digs a vertical shaft.
  9.        
  10.         To start place it in an area that you would like to mine.
  11.        
  12.         Type the name of the program, and the turtle will start
  13.         searching forward for the deposit location.
  14.        
  15.         The deposit location will be where the turtle bumps
  16.         into something. From here he will head back to the "starting tile".
  17.        
  18.         At the "starting tile" the turtle will start mining down and unload
  19.         at the deposit location if he can't carry anymore.
  20.        
  21.         It will send a redstone pulse for every item it drops, so any
  22.         transposer or filter can pick it up.
  23.        
  24.         It will stop when all the tiles have been traversed (from start to deposit).
  25.        
  26. ]]--
  27.  
  28. local data = {
  29.     params = {...},
  30.     currentDepth = 1,
  31.     currentTile = 1,
  32.     lastTile = 1,
  33.     lastSlot = 9,
  34.     dug = 0,
  35.     unload = false,
  36.     msg = "%s @ %d-%d"
  37. }
  38.  
  39. local function report(msg)
  40.     rednet.broadcast(msg)
  41.     print(msg)
  42. end
  43.  
  44. local function forward()
  45.     if not turtle.forward() then
  46.         return false
  47.     end
  48.     data.currentTile = data.currentTile - 1
  49.     return true
  50. end
  51.  
  52. local function back()
  53.     if not turtle.back() then
  54.         return false
  55.     end
  56.     data.currentTile = data.currentTile + 1
  57.     return true
  58. end
  59.  
  60. local function up()
  61.     if not turtle.up() then
  62.         return false
  63.     end
  64.     data.currentDepth = data.currentDepth - 1
  65.     return true
  66. end
  67.  
  68. local function down()
  69.     if not turtle.down() then
  70.         return false
  71.     end
  72.     data.currentDepth = data.currentDepth + 1
  73.     return true
  74. end
  75.  
  76. local function getCap()
  77.     local capacity = 0
  78.     for i = 1, data.lastSlot do
  79.         capacity = capacity + turtle.getItemSpace(i)
  80.     end
  81.     return capacity
  82. end
  83.  
  84. local function unloadInventory()
  85.     local itemCount = 0
  86.     local slotItemCount = nil
  87.     for i = 1, data.lastSlot do
  88.         slotItemCount = turtle.getItemCount(i)
  89.         if slotItemCount > 0 then
  90.             redstone.setOutput("front", true)
  91.             turtle.select(i)
  92.             turtle.drop()
  93.             sleep(0.2)
  94.             redstone.setOutput("front", false)
  95.             sleep(0.2)
  96.             itemCount = itemCount + slotItemCount
  97.         end
  98.     end
  99.     report("Deposited " .. itemCount  .. " items")
  100. end
  101.  
  102. local function canMine()
  103.     local slotsTaken = 0
  104.     for i = 1, data.lastSlot do
  105.         if turtle.getItemCount(i) > 0 then
  106.             turtle.select(i)
  107.             if turtle.compareDown() then
  108.                 return true
  109.             else
  110.                 slotsTaken = slotsTaken + 1
  111.             end
  112.         end
  113.     end
  114.     turtle.select(1)
  115.     return slotsTaken ~= data.lastSlot
  116. end
  117.  
  118. local function surface()
  119.     while data.currentDepth ~= 1 do
  120.         if not up() then
  121.             return false
  122.         end
  123.         sleep(0.2)
  124.     end
  125.     return true
  126. end
  127.  
  128. local function moveToDeposit()
  129.     if not surface() then
  130.         return false
  131.     end
  132.     while data.currentTile ~= 1 do
  133.         if not forward() then
  134.             return false
  135.         end
  136.     end
  137.     return true
  138. end
  139.  
  140. local function moveToNextTile()
  141.     if not surface() then
  142.         --report("moveToNextTile, could not surface")
  143.         return false
  144.     end
  145.     if forward() then
  146.         data.lastTile = data.lastTile - 1
  147.     else
  148.         --report("moveToNextTile, could not move forward")
  149.         return false
  150.     end
  151.     return true
  152. end
  153.  
  154. local function moveToLastTile()
  155.     if not surface() then
  156.         return false
  157.     end
  158.     while data.currentTile < data.lastTile do
  159.         if not back() then
  160.             return false
  161.         end
  162.     end
  163.     return true
  164. end
  165.  
  166. -- returns busy, full, moveerror or digerror
  167.  
  168. local function shaft()
  169.     if 0 == getCap() then
  170.         return "full"
  171.     end
  172.    
  173.     if turtle.detectDown() then
  174.         if canMine() then
  175.             if not turtle.digDown() then
  176.                 return "digerror"
  177.             end
  178.             data.dug = data.dug + 1
  179.             report("Dug " .. data.dug .. " blocks so far")
  180.             return "busy"
  181.         else
  182.             return "full"
  183.         end
  184.     elseif not down() then
  185.         return "moveerror"
  186.     else
  187.         return "busy"
  188.     end
  189. end
  190.  
  191. local function init()
  192.     -- Enable rednet
  193.     rednet.open("right")
  194.    
  195.     -- Detect params
  196.     if 1 == #data.params then
  197.         data.lastSlot = 16
  198.     end
  199.    
  200.     -- Find deposit location
  201.     while not turtle.detect() do
  202.         turtle.forward()
  203.         data.lastTile = data.lastTile + 1
  204.     end
  205.    
  206.     if data.lastTile < 2 then
  207.         report("Invalid mining area")
  208.         return
  209.     end
  210.    
  211.     report("Found deposit location")
  212.    
  213.     report("Gathering...")
  214.    
  215.     -- Go back to the last tile
  216.     if not moveToLastTile() then
  217.         report("Error moving to mining area")
  218.         return
  219.     end
  220.    
  221.     local status = shaft()
  222.    
  223.     report(data.msg:format(status, data.currentDepth, data.currentTile))
  224.    
  225.     -- Work
  226.     while "busy" == status do
  227.        
  228.         status = shaft()
  229.         report(data.msg:format(status, data.currentDepth, data.currentTile))
  230.        
  231.         if "full" == status then
  232.             if not moveToDeposit() then
  233.                 if not surface() then
  234.                     status = "dpfullstuck"
  235.                 else
  236.                     status = "dpfullsurfaced"
  237.                 end
  238.             else
  239.                 unloadInventory()
  240.                 if not moveToLastTile() then
  241.                     if not surface() then
  242.                         status = "ltfullstuck"
  243.                     else
  244.                         status = "ltfullsurfaced"
  245.                     end
  246.                 else
  247.                     status = "busy"
  248.                 end
  249.             end
  250.        
  251.         elseif status ~= "busy" then
  252.             if 1 == data.currentTile then
  253.                 if not moveToDeposit() then
  254.                     if not surface() then
  255.                         status = "!fulldpstuck"
  256.                     else
  257.                         status = "!fulldpsurfaced"
  258.                     end
  259.                 else
  260.                     status = "done"
  261.                 end
  262.             else
  263.                 if not moveToNextTile() then
  264.                     if not surface() then
  265.                         status = "!fullltstuck"
  266.                     else
  267.                         status = "!fullltsurfaced"
  268.                     end
  269.                 else
  270.                     status = "busy"
  271.                 end
  272.             end
  273.         end
  274.         report(data.msg:format(status, data.currentDepth, data.currentTile))
  275.     end
  276.    
  277.     rednet.close("right")
  278. end
  279.  
  280. init()
Advertisement
Add Comment
Please, Sign In to add comment