jille_Jr

CC: Mining program (turtle)

Nov 20th, 2013
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1.  
  2. --(( Settings ))--
  3.  
  4. local fuelcap = 50
  5.  
  6. --(( Variables ))--
  7.  
  8. local depth = 0
  9. local items = {}
  10.  
  11. --(( Functions ))--
  12.  
  13. local function claim(slot)
  14.     items[slot] = true
  15. end
  16.  
  17. -- side = "", "up", or "down"
  18. local function isjunk(side)
  19.     side = side or ""
  20.     side = side:sub(1,1):upper() .. side:sub(2):lower()
  21.     for slot,value in pairs(items) do
  22.         turtle.select(slot)
  23.         if turtle["compare"..side]() then
  24.             return true
  25.         end
  26.     end
  27.     return false
  28. end
  29.  
  30. local function refuel()
  31.     local fuel = turtle.getFuelLevel()
  32.     while fuel < fuelcap do
  33.         print("Searching for fuel..")
  34.         for slot = 1,16 do
  35.             if items[slot] ~= true then
  36.                 turtle.select(slot)
  37.                 if turtle.refuel(1) then return true end
  38.             end
  39.         end
  40.         print("No fuel found. Press SPACE to search again!")
  41.         local key
  42.         repeat _,key=os.pullEvent("key")
  43.         until key==keys.space
  44.     end
  45. end
  46.  
  47. local function dig(dir)
  48.     dir = dir or ""
  49.     dir = dir:sub(1,1):upper() .. dir:sub(2):lower()
  50.     turtle.select(1)
  51.     if not turtle["detect"..dir]() then return true end
  52.     while not turtle["dig"..dir]() do
  53.         if not turtle["attack"..dir]() then
  54.             return false
  55.         end
  56.     end
  57.     return true
  58. end
  59.  
  60. local function down()
  61.     refuel()
  62.     while not turtle.down() do
  63.         if dig("down") == false then
  64.             return false
  65.         end
  66.     end
  67.     return true
  68. end
  69.  
  70. local function up()
  71.     refuel()
  72.     while not turtle.up() do
  73.         if dig("up") == false then
  74.             return false
  75.         end
  76.     end
  77.     return true
  78. end
  79.  
  80. local function checksides()
  81.     local turns = {turtle.turnRight,turtle.turnLeft,}
  82.     local turn = turns[math.random(1,#turns)]
  83.     for count = 1,4 do
  84.         if not isjunk() then
  85.             dig()
  86.         end
  87.         turn()
  88.     end
  89. end
  90.  
  91. local function fill()
  92.     -- first try junk items
  93.     for slot,state in pairs(items) do
  94.         if state == true then
  95.             if turtle.getItemCount(slot) > 1 then
  96.                 turtle.select(slot)
  97.                 while not turtle.placeDown() do
  98.                     turtle.attackDown()
  99.                 end
  100.                 return
  101.             end
  102.         end
  103.     end
  104.     -- then use storage
  105.     for slot = 1,16 do
  106.         if items[slot] ~= true then
  107.             if turtle.getItemCount(slot) > 0 then
  108.                 turtle.select(slot)
  109.                 if turtle.placeDown() then
  110.                     return
  111.                 end
  112.             end
  113.         end
  114.     end
  115. end
  116.  
  117. local function mine()
  118.     local running = true
  119.     while running do
  120.         checksides()
  121.         if not down() then
  122.             print("Probably hit bedrock!")
  123.             running = false
  124.         else
  125.             depth = depth + 1
  126.         end
  127.     end
  128.     print("Heading back up...")
  129.     for count = 1,depth do
  130.         repeat until up()
  131.     end
  132.     fill()
  133. end
  134.  
  135. claim(1)--stone
  136. claim(2)--dirt
  137. claim(3)--cobble/gravel
  138. print("Commencing mining operation!")
  139. mine()
Advertisement
Add Comment
Please, Sign In to add comment