bindre12

Custom Command Turtles (Miner)

Dec 10th, 2012
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. -- Miner
  2.  
  3. local length = 3
  4. local unloaded = 0
  5. local collected = 0
  6. local depth = 0
  7.  
  8. local function collect()
  9.  collected = collected + 1
  10.  if math.fmod(collected, 25) == 0 then
  11.   print( "Mined "..collected.." items." )
  12.  end
  13. end
  14.  
  15. local function tryDig()
  16.  while turtle.detect() do
  17.   if turtle.dig() then
  18.    collect()
  19.    sleep(0.5)
  20.   else
  21.    return false
  22.   end
  23.  end
  24.  return true
  25. end
  26.  
  27. local function tryDigUp()
  28.  while turtle.detectUp() do
  29.   if turtle.digUp() then
  30.    collect()
  31.    sleep(0.5)
  32.   else
  33.    return false
  34.   end
  35.  end
  36.  return true
  37. end
  38.  
  39. local function refuel()
  40.  local fuelLevel = turtle.getFuelLevel()
  41.  if fuelLevel == "unlimited" or fuelLevel > 0 then
  42.   return
  43.  end
  44.  local function tryRefuel()
  45.   for n=1,16 do
  46.    if turtle.getItemCount(n) > 0 then
  47.     turtle.select(n)
  48.     if turtle.refuel(1) then
  49.      turtle.select(1)
  50.      return true
  51.     end
  52.    end
  53.   end
  54.   turtle.select(1)
  55.   return false
  56.  end
  57.  if not tryRefuel() then
  58.   print( "Add more fuel to continue." )
  59.   while not tryRefuel() do
  60.    sleep(1)
  61.   end
  62.   print( "Resuming Tunnel." )
  63.  end
  64. end
  65.  
  66. local function tryUp()
  67.  refuel()
  68.  while not turtle.up() do
  69.   if turtle.detectUp() then
  70.    if not tryDigUp() then
  71.     return false
  72.    end
  73.   elseif turtle.attackUp() then
  74.    collect()
  75.   else
  76.    sleep( 0.5 )
  77.   end
  78.  end
  79.  return true
  80. end
  81.  
  82. local function tryDown()
  83.  refuel()
  84.  while not turtle.down() do
  85.   if turtle.detectDown() then
  86.    if not tryDigDown() then
  87.     return false
  88.    end
  89.   elseif turtle.attackDown() then
  90.    collect()
  91.   else
  92.    sleep( 0.5 )
  93.   end
  94.  end
  95.  return true
  96. end
  97.  
  98. local function tryForward()
  99.  refuel()
  100.  while not turtle.forward() do
  101.   if turtle.detect() then
  102.    if not tryDig() then
  103.     return false
  104.    end
  105.   elseif turtle.attack() then
  106.    collect()
  107.   else
  108.    sleep( 0.5 )
  109.   end
  110.  end
  111.  return true
  112. end
  113. sleep(1)
  114. rednet.open("right")
  115.  
  116. while true do
  117.  term.clear()
  118.  term.setCursorPos(1, 1)
  119.  print("Ready to Receve Commands")
  120.  local id, mgs, dis = rednet.receive()
  121.  if mgs == "2Forward" then
  122.   turtle.forward()
  123.  end
  124.  if mgs == "2Back" then
  125.   turtle.back()
  126.  end
  127.  if mgs == "2TurnLeft" then
  128.   turtle.turnLeft()
  129.  end
  130.  if mgs == "2TurnRight" then
  131.   turtle.turnRight()
  132.  end
  133.  if mgs == "2Up" then
  134.   turtle.up()
  135.  end
  136.  if mgs == "2Down" then
  137.   turtle.down()
  138.  end
  139.  if mgs == "2Dig" then
  140.   turtle.dig()
  141.  end
  142.  if mgs == "2DigUp" then
  143.   turtle.digUp()
  144.  end
  145.  if mgs == "2DigDown" then
  146.   turtle.digDown()
  147.  end
  148.  if mgs == "2Place" then
  149.   turtle.place()
  150.  end
  151.  if mgs == "2PlaceDown" then
  152.   turtle.placeDown()
  153.  end
  154.  if mgs == "2PlaceUp" then
  155.   turtle.placeUp()
  156.  end
  157.  if mgs == "2Drop" then
  158.   for n=1,16 do
  159.    unloaded = unloaded + turtle.getItemCount(n)
  160.    turtle.select(n)
  161.    turtle.drop()
  162.   end
  163.   collected = 0
  164.   turtle.select(1)
  165.  end
  166.  if mgs == "2Tunnel" then
  167.   for n=1,length do
  168.    turtle.placeDown()
  169.    tryDigUp()
  170.    turtle.turnLeft()
  171.    tryDig()
  172.    tryUp()
  173.    tryDig()
  174.    turtle.turnRight()
  175.    turtle.turnRight()
  176.    tryDig()
  177.    tryDown()
  178.    tryDig()
  179.    turtle.turnLeft()
  180.    if n<length then
  181.     tryDig()
  182.      if not tryForward() then
  183.      print( "Aborting Tunnel." )
  184.      break
  185.     end
  186.    else
  187.     print( "Tunnel complete." )
  188.    end
  189.   end
  190.   print( "Returning to start..." )
  191.   turtle.turnLeft()
  192.   turtle.turnLeft()
  193.   while depth > 0 do
  194.    if turtle.forward() then
  195.     depth = depth - 1
  196.    else
  197.     turtle.dig()
  198.    end
  199.   end
  200.   turtle.turnRight()
  201.   turtle.turnRight()
  202.   rednet.broadcast("MDone")
  203.  end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment