Advertisement
Ineentho

Smart Miner

May 18th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1. quarrySize = 0
  2. minFuelLevel = 1000
  3. fuelUsed = 0
  4.  
  5. x = 0 --Standing in positive X direction when starting
  6. y = 0
  7. z = 0
  8. direction = 0
  9.  
  10. local goHome
  11.  
  12. function printPosition()
  13.     --print("X: " .. x .. ", Y: " .. y .. ", Z: " .. z .. ", Direction: " .. direction)
  14. end
  15.  
  16. function checkFullInventory()
  17.     for i = 1, 16, 1 do
  18.         if turtle.getItemCount(i) == 0 then
  19.             return false
  20.         end
  21.     end
  22.  
  23.     return true
  24. end
  25.  
  26. function dig(careAboutInventory)
  27.     if careAboutInventory == nil then
  28.         careAboutInventory = true
  29.     end
  30.  
  31.     if careAboutInventory then
  32.         if not checkFullInventory() then
  33.             turtle.dig()
  34.         else
  35.             goHome()
  36.         end
  37.     else
  38.         turtle.dig()
  39.     end
  40. end
  41. function digDown(careAboutInventory)
  42.     if careAboutInventory == nil then
  43.         careAboutInventory = true
  44.     end
  45.  
  46.     if careAboutInventory then
  47.         if not checkFullInventory() then
  48.             turtle.digDown()
  49.         else
  50.             goHome()
  51.         end
  52.     else
  53.         turtle.digDown()
  54.     end
  55. end
  56.  
  57. function digUp(careAboutInventory)
  58.     if careAboutInventory == nil then
  59.         careAboutInventory = true
  60.     end
  61.  
  62.     if careAboutInventory then
  63.         if not checkFullInventory() then
  64.             turtle.digUp()
  65.         else
  66.             goHome()
  67.         end
  68.     else
  69.         turtle.digUp()
  70.     end
  71. end
  72.  
  73. function forward(steps, careAboutInventory)
  74.     if careAboutInventory == nil then
  75.         careAboutInventory = true
  76.     end
  77.  
  78.     if steps == nil then
  79.         steps = 1
  80.     end
  81.  
  82.     for i = 1, steps, 1 do
  83.         while not turtle.forward() do
  84.             turtle.attack()
  85.             dig(careAboutInventory)
  86.         end
  87.  
  88.         fuelUsed = fuelUsed + 1
  89.  
  90.         --Update coordinates
  91.         if direction == 0 then
  92.             x = x + 1
  93.         elseif direction == 1 then
  94.             z = z + 1
  95.         elseif direction == 2 then
  96.             x = x - 1
  97.         elseif direction == 3 then
  98.             z = z - 1
  99.         end
  100.         printPosition()
  101.  
  102.     end
  103. end
  104.  
  105. function up(steps, careAboutInventory)
  106.     if careAboutInventory == nil then
  107.         careAboutInventory = true
  108.     end
  109.  
  110.     if steps == nil then
  111.         steps = 1
  112.     end
  113.  
  114.     for i = 1, steps, 1 do
  115.         while not turtle.up() do
  116.             turtle.attackUp()
  117.             digUp(careAboutInventory)
  118.         end
  119.         fuelUsed = fuelUsed + 1
  120.         y = y + 1
  121.         printPosition()
  122.     end
  123. end
  124.  
  125.  
  126. function down(steps, careAboutInventory)
  127.     if careAboutInventory == nil then
  128.         careAboutInventory = true
  129.     end
  130.  
  131.     if steps == nil then
  132.         steps = 1
  133.     end
  134.  
  135.     for i = 1, steps, 1 do
  136.         while not turtle.down() do
  137.             turtle.attackDown()
  138.             digDown(careAboutInventory)
  139.         end
  140.         fuelUsed = fuelUsed + 1
  141.         y = y - 1
  142.         printPosition()
  143.     end
  144. end
  145.  
  146. function updateDirection(amount)
  147.     direction = direction + amount
  148.     direction = direction % 4
  149. end
  150.  
  151. function turnLeft(steps)
  152.     if steps == nil then
  153.         steps = 1
  154.     end
  155.  
  156.     for i = 1, steps, 1 do
  157.         turtle.turnLeft()
  158.         updateDirection(-1)
  159.     end
  160. end
  161.  
  162. function turnRight(steps)
  163.     if steps == nil then
  164.         steps = 1
  165.     end
  166.  
  167.     for i = 1, steps, 1 do
  168.         turtle.turnRight()
  169.         updateDirection(1)
  170.     end
  171. end
  172.  
  173. function turnTo(dir)
  174.     turnLeft((direction - dir) % 4 )
  175. end
  176.  
  177. function unloadInventory()
  178.     for i = 1, 16, 1 do
  179.         turtle.select(i)
  180.         turtle.drop()
  181.     end
  182.     turtle.select(1)
  183. end
  184.  
  185. function waitForFuel()
  186.     print("Waiting for fuel..")
  187.  
  188.     while true do
  189.         fuel = turtle.getFuelLevel()
  190.  
  191.         print(fuel .. " of " .. minFuelLevel .. " fuel.")
  192.  
  193.         if fuel >= minFuelLevel then
  194.             return
  195.         end
  196.         sleep(5)
  197.     end
  198. end
  199.  
  200. function goHome()
  201.     print("Going home...")
  202.  
  203.     resumeAtX = x
  204.     resumeAtY = y
  205.     resumeAtZ = z
  206.     resumeDirection = direction
  207.  
  208.     --Go to the home position
  209.     up(-y, false)
  210.     turnTo(2) --X-axis
  211.     forward(x, false)
  212.     turnTo(3) --Z-axis
  213.     forward(z, false)
  214.  
  215.     print(fuelUsed .. " fuel used.")
  216.  
  217.     --Unload
  218.     turnTo(2) --Face the chest
  219.     unloadInventory()
  220.     turnTo(0) --Face the quarry again
  221.  
  222.     waitForFuel()
  223.  
  224.     --Return where it left off
  225.     turnTo(0) --X-axis
  226.     forward(resumeAtX, false)
  227.     turnTo(1) --Z-axis
  228.     forward(resumeAtZ, false)
  229.     down(-resumeAtY, false)
  230.     turnTo(resumeDirection)
  231. end
  232.  
  233. function mine()
  234.     digUp()
  235.     digDown()
  236.     dig()
  237. end
  238.  
  239.  
  240. function checkArguments(args)
  241.     if #args == 0 then
  242.         print("You have to supply an argument for quarry size.")
  243.         return false
  244.     end
  245.     quarrySize = tonumber(args[1])
  246.     return true
  247. end
  248.  
  249. function nextRow(left)
  250.     if left then
  251.         turnLeft()
  252.     else
  253.         turnRight()
  254.     end
  255.  
  256.     mine()
  257.     forward()
  258.  
  259.     if left then
  260.         turnLeft()
  261.     else
  262.         turnRight()
  263.     end
  264. end
  265.  
  266. function mineLayer()
  267.     left = false
  268.     for i=1, quarrySize, 1 do
  269.         --For each row
  270.         for j=2, quarrySize, 1 do
  271.             mine()
  272.             forward()
  273.         end
  274.  
  275.         if i ~= quarrySize then
  276.             nextRow(left)
  277.             left = not left
  278.         else
  279.             if evenSize then
  280.                 turnRight()
  281.             else
  282.                 turnLeft(2)
  283.             end
  284.         end
  285.  
  286.         mine()
  287.     end
  288. end
  289.  
  290. if not checkArguments({...}) then
  291.     return --Exit the program
  292. end
  293.  
  294. done = false
  295. evenSize = (quarrySize % 2) == 0
  296.  
  297. waitForFuel()
  298.  
  299. down(2)
  300.  
  301. while true do
  302.     mineLayer()
  303.     down(3)
  304. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement