Advertisement
Guest User

mine.lua

a guest
Apr 5th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. --digs 2x2 with 3d search
  2. --collects all ores
  3. --ejects cobble and 'ites'
  4.  
  5. --globals
  6. local input = {...}
  7. DISTANCE = input[1]
  8. moves = {}
  9. index = 1
  10. DIR = 1
  11. M_vectors = {3, 4, 1, 2, 6, 5}
  12.  
  13. --main functions
  14.  
  15. function tunnel()
  16.     turtle.dig()
  17.     dig()
  18.     turtle.forward()
  19.     inspect_d(1)
  20.     change_d('left')
  21.     turtle.dig()
  22.     dig()
  23.     turtle.forward()
  24.     inspect_d(1)
  25.     inspect_d(2)
  26.     turtle.digUp()
  27.     digUp()
  28.     turtle.up()
  29.     inspect_d(2)
  30.     inspect_d(3)
  31.     change_d('right')
  32.     change_d('right')
  33.     turtle.dig()
  34.     dig()
  35.     turtle.forward()
  36.     inspect_d(3)
  37.     inspect_d(2)
  38.     turtle.down()
  39.     inspect_d(2)
  40.     change_d('left')
  41. end
  42.  
  43. function dig()
  44.     while turtle.detect() == true do
  45.         turtle.dig()
  46.     end
  47. end
  48.  
  49. function digUp()
  50.     while turtle.detectUp() == true do
  51.         turtle.digUp()
  52.     end
  53. end
  54.  
  55. function check(dir)
  56.     local found = false
  57.     if dir == 1 then
  58.         is_block, data = turtle.inspectDown()
  59.     elseif dir == 2 then
  60.         is_block, data = turtle.inspect()
  61.     elseif dir == 3 then
  62.         is_block, data = turtle.inspectUp()
  63.     end
  64.     local name = data.name
  65.     if is_block and string.sub(name, -3) == 'ore' then
  66.         found = true
  67.     end
  68.     return found
  69. end
  70.  
  71. function change_d(dir)
  72.     if dir == 'left' then
  73.         turtle.turnLeft()
  74.         if DIR == 1 then
  75.             DIR = 4
  76.         else
  77.             DIR = DIR -1
  78.         end
  79.     elseif dir == 'right' then
  80.         turtle.turnRight()
  81.         if DIR == 4 then
  82.             DIR = 1
  83.         else
  84.             DIR = DIR + 1
  85.         end
  86.     end
  87. end
  88.  
  89. function inspect_base(dir)
  90.     local success = check(dir)
  91.     if success and dir == 2 then
  92.         turtle.dig()
  93.         turtle.forward()
  94.         moves[index] = DIR
  95.         index = index + 1
  96.     elseif success and dir == 1 then
  97.         turtle.digDown()
  98.         turtle.down()
  99.         moves[index] = 5
  100.         index = index + 1
  101.     elseif success and dir == 3 then
  102.         turtle.digUp()
  103.         turtle.up()
  104.         moves[index] = 6
  105.         index = index + 1
  106.     end
  107.     return success
  108. end
  109.  
  110. function inspect_loop()
  111.     i = 1
  112.     j = 2
  113.     while moves[1] ~= nil do
  114.         local found = inspect_base(j)
  115.         if not found and j == 2 then
  116.             change_d('right')
  117.             i = i + 1
  118.             if i == 5 then
  119.                 j = 1
  120.             end
  121.         elseif not found and j == 1 then
  122.             j = 3
  123.         elseif not found and j == 3 then
  124.             reverse()
  125.             i = 1
  126.             j = 2
  127.         else
  128.             i = 1
  129.             j = 2
  130.         end
  131.     end
  132. end
  133.  
  134. function turn(goal)
  135.     while DIR ~= goal do
  136.         change_d('right')
  137.     end
  138. end
  139.  
  140. function reverse()
  141.     index = index - 1
  142.     i = moves[index]
  143.     goal = M_vectors[i]
  144.     if goal < 5 then
  145.         turn(goal)
  146.         turtle.forward()
  147.         moves[index] = nil
  148.     else
  149.         if goal == 5 then
  150.             turtle.down()
  151.             moves[index] = nil
  152.         elseif goal == 6 then
  153.             turtle.up()
  154.             moves[index] = nil
  155.         end
  156.     end
  157. end
  158.  
  159. function inspect_d(dir)
  160.     local current_d = DIR
  161.     local found = inspect_base(dir)
  162.     if found then
  163.         inspect_loop()
  164.         turn(current_d)
  165.     end
  166. end
  167.  
  168. --aux functions
  169.  
  170. function refuel()
  171.     if turtle.getFuelLevel() < 128 then
  172.         turtle.select(1)
  173.         turtle.refuel(1)
  174.     end
  175. end
  176.  
  177. function eject()
  178.     for i = 1, 16 do
  179.         turtle.select(i)
  180.         count = turtle.getItemCount()
  181.         if count > 0 then
  182.             local data = turtle.getItemDetail()
  183.             local name = data.name
  184.             if string.sub(name, -3) == ('ite' or 'one') then
  185.                  turtle.drop()
  186.             end
  187.         end
  188.     end
  189. end
  190.  
  191. --main
  192.  
  193. function main()
  194.     refuel()
  195.     for i = 1, DISTANCE do
  196.         tunnel()
  197.         if (i % 10) == 0 then
  198.             refuel()
  199.             eject()
  200.         end
  201.         print(i)
  202.     end
  203.     turtle.turnRight()
  204.     turtle.turnRight()
  205.     eject()
  206.     refuel()
  207.     for i = 1, DISTANCE do
  208.         turtle.forward()
  209.     end
  210. end
  211.  
  212. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement