Advertisement
Alakazard12

Miner

Jun 11th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. local blacklist = {
  2.     "minecraft:stone";
  3. }
  4.  
  5. local MODE_START = 0
  6. local MODE_VEIN = 1
  7. local MOVE_SHAFT = 2
  8.  
  9. local SIDE_LEFT = 0
  10. local SIDE_RIGHT = 1
  11.  
  12. local PASS_FIRST = 0
  13. local PASS_SECOND = 1
  14.  
  15. local VEIN_LEN = 16;
  16.  
  17. local state = {
  18.     -- x, y, z
  19.     relativePos = {0, 0, 0};
  20.  
  21.     -- 0: forward
  22.     -- 1: left
  23.     -- 2: back
  24.     -- 3: right
  25.     orientation = 0;
  26.  
  27.     mode = MODE_START;
  28.  
  29.     vein_len = 0;
  30.     vein_side = SIDE_LEFT;
  31.     vein_pass = PASS_FIRST;
  32.     shaft_len = 0;
  33. }
  34.  
  35. local function blacklisted(detail)
  36.     if not detail or not detail.name then return false end
  37.     for i,v in ipairs(blacklist) do
  38.         if v == detail.name then
  39.             return true
  40.         end
  41.     end
  42.     return false
  43. end
  44.  
  45. local function load_state()
  46.     local file = fs.open("state", "r")
  47.     if not file then return end
  48.     print("Loading previous state")
  49.     local t_state = textutils.unserialize(file.readAll())
  50.     if not t_state then
  51.         print("Failed to deserialize state")
  52.     else
  53.         state = t_state
  54.     end
  55.     file.close()
  56. end
  57.  
  58. local function save_state()
  59.     local file = fs.open("state", "w")
  60.     file.write(textutils.serialize(state))
  61.     file.close()
  62. end
  63.  
  64.  
  65. local function tryOp(op, force, force_op)
  66.     if force then
  67.         if not force_op then
  68.             force_op = function() end
  69.         end
  70.         while not op() do force_op() end
  71.         return true
  72.     end
  73.  
  74.     return op()
  75. end
  76.  
  77. local function left(force)
  78.     if tryOp(turtle.turnLeft, force) then
  79.         state.orientation = state.orientation + 1
  80.         if state.orientation == 4 then
  81.             state.orientation = 0
  82.         end
  83.         return true
  84.     end
  85.     return false
  86. end
  87.  
  88. local function right(force)
  89.     if tryOp(turtle.turnRight, force) then
  90.         state.orientation = state.orientation - 1
  91.         if state.orientation == -1 then
  92.             state.orientation = 3
  93.         end
  94.         return true
  95.     end
  96.     return false
  97. end
  98.  
  99. local function up(force)
  100.     if tryOp(turtle.up, force, turtle.digUp) then
  101.         state.relativePos[1] = state.relativePos[1] + 1
  102.         return true
  103.     end
  104.     return false
  105. end
  106.  
  107. local function down(force)
  108.     local success
  109.     if tryOp(turtle.down, force, turtle.digDown) then
  110.         state.relativePos[1] = state.relativePos[1] - 1
  111.         return true
  112.     end
  113.     return false
  114. end
  115.  
  116. local function dig(force)
  117.     return tryOp(turtle.dig, force)
  118. end
  119.  
  120. local function digUp(force)
  121.     return tryOp(turtle.digUp, force)
  122. end
  123.  
  124. local function digDown(force)
  125.     return tryOp(turtle.digDown, force)
  126. end
  127.  
  128. local function forward(force)
  129.     local slot, amount
  130.     if state.orientation == 0 then
  131.         slot = 1
  132.         amount = 1
  133.     elseif state.orientation == 1 then
  134.         slot = 3
  135.         amount = 1
  136.     elseif state.orientation == 2 then
  137.         slot = 1
  138.         amount = 1
  139.     elseif state.orientation == 3 then
  140.         slot = 3
  141.         amount = -1
  142.     end
  143.    
  144.     if tryOp(turtle.forward, force, turtle.dig) then
  145.         state.relativePos[slot] = state.relativePos[slot] + amount
  146.         return true
  147.     end
  148.     return false
  149. end
  150.  
  151. local function back(force)
  152.     local slot, amount
  153.     if state.orientation == 0 then
  154.         slot = 1
  155.         amount = 1
  156.     elseif state.orientation == 1 then
  157.         slot = 3
  158.         amount = 1
  159.     elseif state.orientation == 2 then
  160.         slot = 1
  161.         amount = 1
  162.     elseif state.orientation == 3 then
  163.         slot = 3
  164.         amount = -1
  165.     end
  166.    
  167.     if tryOp(turtle.back, force, function() left(true) left(true) dig() left(true) left(true) end) then
  168.         state.relativePos[slot] = state.relativePos[slot] - amount
  169.     end
  170. end
  171.  
  172. load_state()
  173.  
  174. while true do
  175.     if state.mode == MODE_START then
  176.         print("Starting miner...")
  177.         state.mode = MODE_SHAFT
  178.     elseif state.mode == MODE_SHAFT then
  179.         forward(true)
  180.         digUp()
  181.         digDown()
  182.  
  183.         state.shaft_len = state.shaft_len + 1
  184.         if state.shaft_len == 3 then
  185.             state.mode = MODE_VEIN
  186.             left(true)
  187.             down(true)
  188.             forward(true)
  189.             state.shaft_len = 0
  190.             state.vein_side = SIDE_LEFT
  191.             state.vein_pass = PASS_FIRST
  192.         end
  193.     elseif state.mode == MODE_VEIN then
  194.         dig()
  195.         forward(true)
  196.         -- Check top/bottom...
  197.         if not blacklisted(table.pack(turtle.inspectUp())[2]) then
  198.             digUp()
  199.         end
  200.         if not blacklisted(table.pack(turtle.inspectDown())[2]) then
  201.             digDown()
  202.         end
  203.  
  204.         state.vein_len = state.vein_len + 1
  205.         if state.vein_len == VEIN_LEN then
  206.             state.vein_len = 0
  207.             if state.vein_pass == PASS_FIRST then
  208.                 print("Performing second vein pass")
  209.                 up(true)
  210.                 up(true)
  211.                 up(true)
  212.                 left(true)
  213.                 left(true)
  214.                 state.vein_pass = PASS_SECOND
  215.             else
  216.                 if state.vein_side == SIDE_LEFT then
  217.                     print("Switching to next side")
  218.                     down(true)
  219.                     down(true)
  220.                     down(true)
  221.                     --forward(true)
  222.                     state.vein_side = SIDE_RIGHT
  223.                 else
  224.                     print("Switching to shaft mode")
  225.                     right(true)
  226.                     down(true)
  227.                     down(true)
  228.                     right(true)
  229.                     state.mode = MODE_SHAFT
  230.                 end
  231.             end
  232.         end
  233.     end
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement