Advertisement
Lemur

Untitled

Sep 7th, 2014
2,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. --[[
  2. Digger: A simple program to dig a 1 x 2 shaft straight
  3.  
  4. Author: Brandon Weaver (keystonelemur / baweaver)
  5.  
  6. Notes: I guess you could use this as a bridge builder too,
  7.        but I'll make a better program for that one later.
  8.  
  9.        Some of the more common functionality such as 180s and
  10.        checking fuel levels really should be an API level concern
  11.        so I'll get to that one later.
  12. --]]
  13.  
  14. local tArgs = { ... }
  15.  
  16. if tArgs[1] == 'help' then
  17.   print('Usage:')
  18.   print('  miner [distance] [set safe blocks] [set torches]')
  19.   print('')
  20.   print('setSafeBlock - Whether to set blocks to prevent a fall')
  21.   print('setTorches   - Whether to set torches')
  22.   return
  23. end
  24.  
  25. -- Distance to mine
  26. local distance = tArgs[1] or 1
  27.  
  28. -- Whether or not to set blocks on an imminent fall
  29. local safeBlock = tArgs[2]
  30.  
  31. -- Whether or not to set torches from slot 2
  32. local torches = tArgs[3]
  33.  
  34. -- Shorten the turtle name
  35. local t = turtle
  36.  
  37. -- Finds the shortage of fuel for the round trip
  38. function checkFuelDeficit()
  39.   return (2 * distance - t.getFuelLevel()) + 2
  40. end
  41.  
  42. -- Confirm that we have adequate fuel for the trip
  43. level = checkFuelDeficit()
  44. if level > 0 then
  45.   -- Attempt to correct the deficit
  46.   print('Insufficient fuel of'..level..' units, attempting refuel...')
  47.   t.refuel(level)
  48.  
  49.   -- See if that worked, or end the program
  50.   newLevel = checkFuelDeficit()
  51.   if newLevel > 0 then
  52.     print('Cannot make trip, fuel is short by'..newLevel..' units. Goodbye')
  53.     return
  54.   end
  55. end
  56.  
  57. -- Confirm that we have enough torches to cover the span
  58. --   TODO - consider increasing the slots, while 320 is large it may
  59. --          not be enough for some.
  60. if torches then
  61.   requiredTorches = math.floor(distance / 5)
  62.   currentStock    = turtle.getItemCount(3)
  63.   deficit         = requiredTorches - currentStock
  64.  
  65.   if deficit > 0 then
  66.     print('Insufficient torches to cover the distance, '..deficit..' more torches required!')
  67.     return
  68.   end
  69. end
  70.  
  71. print('Mining '..distance..' squares')
  72. print('')
  73.  
  74. function turnAround()
  75.   t.turnRight()
  76.   t.turnRight()
  77. end
  78.  
  79. -- Set a safety block to keep from a nasty fall
  80. function setSafeBlock()
  81.   if safeBlock then
  82.     t.select(2)
  83.     t.placeDown()
  84.   end
  85. end
  86.  
  87. -- Set a torch for visibility
  88. function setTorch(currentLocation)
  89.   if torches and currentLocation % 5 == 0 then
  90.     t.select(3)
  91.     turnAround()
  92.     t.place()
  93.     turnAround()
  94.   end
  95. end
  96.  
  97. -- Clear a forward path
  98. function clearForward(currentLocation)
  99.   setSafeBlock()
  100.  
  101.   -- Make sure to deal with sand and friends
  102.   while t.detect() do t.dig() end
  103.  
  104.   t.forward()
  105.   setTorch(currentLocation)
  106.   t.digUp()
  107. end
  108.  
  109. -- Make it come back to the origin
  110. function returnHome()
  111.   turnAround()
  112.   t.up()
  113.  
  114.   for returnLocation = 0, distance do
  115.      -- Make sure to deal with sand and friends
  116.     while t.detect() do t.dig() end
  117.    
  118.     t.forward()
  119.   end
  120. end
  121.  
  122. -- Main program loop
  123. for currentLocation = 0, distance do
  124.   -- Do you REALLY want it every block notifications?
  125.   if currentLocation % 5 == 0 then
  126.     print('Mined '..currentLocation..' blocks')
  127.   end
  128.  
  129.   clearForward(currentLocation)
  130. end
  131.  
  132. returnHome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement