Lemur

digger2.lua

Sep 10th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. --[[
  2. Digger2: A simple program to dig a cross pattern
  3.  
  4. Author: Brandon Weaver (keystonelemur / baweaver)
  5.  
  6. Notes:
  7. --]]
  8.  
  9. local tArgs = { ... }
  10.  
  11. if tArgs[1] == 'help' then
  12.   print('Usage:')
  13.   print('  lava [distance] [drop]')
  14.   return
  15. end
  16.  
  17. -- Distance to mine
  18. local distance = tArgs[1] or 1
  19.  
  20. -- Shorten the turtle name
  21. local t = turtle
  22.  
  23. -- Finds the shortage of fuel for the round trip
  24. local function checkFuelDeficit()
  25.   return (2 * distance - t.getFuelLevel()) + 2
  26. end
  27.  
  28. -- Confirm that we have adequate fuel for the trip
  29. local level = checkFuelDeficit()
  30. if level > 0 then
  31.   -- Attempt to correct the deficit
  32.   print('Insufficient fuel of'..level..' units, attempting refuel...')
  33.   t.refuel()
  34.  
  35.   -- See if that worked, or end the program
  36.   local newLevel = checkFuelDeficit()
  37.   if newLevel > 0 then
  38.     print('Cannot make trip, fuel is short by'..newLevel..' units. Goodbye')
  39.     return
  40.   end
  41. end
  42.  
  43. print('Mining '..distance..' squares')
  44. print('')
  45.  
  46. local function turnAround()
  47.   t.turnRight()
  48.   t.turnRight()
  49. end
  50.  
  51. local function until_clear(checkFn, digFn)
  52.   while checkFn() do
  53.     digFn()
  54.     sleep(0.1)
  55.   end
  56. end
  57.  
  58. -- Clear a forward path
  59. local function clearDown()
  60.   -- Make sure to deal with sand and friends
  61.   until_clear(t.detect, t.dig)
  62.   until_clear(t.detectDown, t.digDown)
  63.   until_clear(t.detectUp, t.digUp)
  64.  
  65.   t.turnRight()
  66.   until_clear(t.detect, t.dig)
  67.  
  68.   turnAround()
  69.   until_clear(t.detect, t.dig)
  70.  
  71.   t.turnRight()
  72.  
  73.   t.digDown()
  74.   t.forward()
  75. end
  76.  
  77. -- Make it come back to the origin
  78. local function returnHome()
  79.   turnAround()
  80.  
  81.   for returnLocation = 0, distance do
  82.      -- Make sure to deal with sand and friends
  83.     until_clear(t.detect, t.dig)
  84.    
  85.     t.forward()
  86.   end
  87. end
  88.  
  89. -- Main program loop
  90. for currentLocation = 0, distance do
  91.   -- Do you REALLY want it every block notifications?
  92.   if currentLocation % 5 == 0 then
  93.     print('Mined '..currentLocation..' blocks')
  94.   end
  95.  
  96.   clearDown()
  97. end
  98.  
  99. returnHome()
Advertisement
Add Comment
Please, Sign In to add comment