Advertisement
KaosKlaus

Mine

Apr 14th, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.57 KB | None | 0 0
  1. local t = require("libs.tlib")
  2.  
  3. local direction
  4. local tunnelHeight = 3
  5. local numRepeats = 8
  6. local tunnelLength
  7. local continueWork = false
  8.  
  9. local tFuel = {
  10.     'charcoal',
  11.     'coal',
  12. }
  13.  
  14. tProtectedBlocks = {
  15.     ['minecraft:diamond_ore'] = true,
  16.     ['minecraft:lapis_ore'] = true,
  17.     ['minecraft:redstone_ore'] = true,
  18. }
  19.  
  20. local tkeepItems = {
  21.     ['minecraft:torch'] = true,
  22.     ['minecraft:chest'] = true,
  23. }
  24.  
  25. local tDropTable = {
  26.     -- 'cobblestone',
  27.     'granite',
  28.     'andesite',
  29.     'diorite',
  30.     'dirt',
  31.     'gravel',
  32. }
  33.  
  34. local tTurnRightFacing = {
  35.     ['north'] = 'east',
  36.     ['east'] = 'south',
  37.     ['south'] = 'west',
  38.     ['west'] = 'north',
  39. }
  40.  
  41. local tTurnLeftFacing = {
  42.     ['north'] = 'west',
  43.     ['east'] = 'north',
  44.     ['south'] = 'east',
  45.     ['west'] = 'south',
  46. }
  47.  
  48. local function isProtectedBlock(success, data)
  49.     if success and tProtectedBlocks[data.name] then
  50.         if data.name == 'minecraft:diamond_ore' then
  51.             print('I found diamonds!')
  52.         end
  53.         return true
  54.     end
  55.     return false
  56. end
  57.  
  58. local function determineFacing()
  59.     t.forward()
  60.     if not turtle.detect() then
  61.         if not t.existItem('cobblestone') then
  62.             print('add cobblestone')
  63.             while not t.existItem('cobblestone') do
  64.                 sleep(1)
  65.             end
  66.         end
  67.         if not (t.selectItem('cobblestone') and turtle.place()) then
  68.             print('ERROR: could not place cobblestone')
  69.             t.back()
  70.             return false
  71.         end
  72.     end
  73.     t.back()
  74.     if not (t.selectItem('torch') and turtle.place()) then
  75.         print('ERROR: could not place torch')
  76.         return false
  77.     end
  78.     success, data = turtle.inspect()
  79.     if not (success and data.state.facing) then
  80.         return false
  81.     else
  82.         local direction = t.tInverseDirection[data.state.facing]
  83.         print('Turtle is facing ' .. direction .. '.')
  84.         return direction
  85.     end
  86. end
  87.  
  88. local function saveDig()
  89.     if not isProtectedBlock(turtle.inspect()) then
  90.         while turtle.dig() do
  91.             --
  92.         end
  93.     end
  94. end
  95.  
  96. local function torchTime(step)
  97.     torchPlacement = {
  98.         [2] = true,     [11] = true,
  99.         [20] = true,  [29] = true,
  100.         [38] = true,
  101.       }
  102.     return torchPlacement[step]
  103. end
  104.  
  105. local function placeTorch()
  106.     if not t.selectItem('torch') then
  107.         print('out of torches!')
  108.         t.turnLeft()
  109.         return
  110.     end
  111.     turtle.place()
  112.     success, data = turtle.inspect()
  113.     if not t.tInverseDirection[direction] then
  114.         print(direction)
  115.         print(t.tInverseDirection[direction])
  116.         error('ERROR in placeTorch(): no direction value')
  117.     end
  118.     if not success or (data.name == 'minecraft:wall_torch' and data.state.facing ~= t.tInverseDirection[direction]) then
  119.         t.forward()
  120.         if t.selectItem('cobblestone') and (turtle.placeDown() or turtle.place()) then
  121.             t.back()
  122.             if t.selectItem('torch') then
  123.                 turtle.place()
  124.             end
  125.         else
  126.             t.turnAround()
  127.             t.forward()
  128.             if t.selectItem('torch') then
  129.                 turtle.place()
  130.             end
  131.             t.turnRight()
  132.             return
  133.         end
  134.     elseif isProtectedBlock(turtle.inspect()) then
  135.         t.turnAround()
  136.         if t.selectItem('torch') then
  137.             turtle.place()
  138.         end
  139.         t.turnRight()
  140.         return
  141.     end
  142.     t.turnLeft()
  143.     turtle.select(1)
  144. end
  145.  
  146. local function dig3x3(torch)
  147.     --recursive digging function:
  148.     local function digOutLevel(level, max)
  149.         saveDig()
  150.         if (level < max) and (not isProtectedBlock(turtle.inspectUp())) then
  151.             t.up()
  152.             digOutLevel(level + 1, max)
  153.             t.down()
  154.         else
  155.             t.turnAround()
  156.         end
  157.         saveDig()
  158.     end
  159.  
  160.     if (not turtle.detectDown()) and t.selectItem('cobblestone') then
  161.         turtle.placeDown()
  162.     end
  163.     t.turnLeft()
  164.     digOutLevel(1, tunnelHeight)
  165.     if torch then
  166.         placeTorch()
  167.     else
  168.         t.turnLeft()
  169.     end
  170. end
  171.  
  172. local function emptySlots()
  173.     local numEmptySlots = 0
  174.     for slot=1, 16 do
  175.         if turtle.getItemCount(slot) == 0 then
  176.             numEmptySlots = numEmptySlots + 1
  177.         end
  178.     end
  179.     return numEmptySlots
  180. end
  181.  
  182.  
  183.  
  184. local function cleanUpInventory()
  185.     for slot_n = 16, 2, -1 do
  186.         if turtle.getItemCount(slot_n) > 0 then
  187.             item_source = turtle.getItemDetail(slot_n)
  188.             for slot_m = 1, slot_n-1 do
  189.                 if turtle.getItemSpace(slot_m) > 0 then
  190.                     item_target = turtle.getItemDetail(slot_m)
  191.                     if (item_source and item_target) and (item_source.name == item_target.name) then
  192.                         turtle.select(slot_n)
  193.                         turtle.transferTo(slot_m)
  194.                         if turtle.getItemCount(slot_n) == 0 then
  195.                             break
  196.                         end
  197.                     end
  198.                 end
  199.             end
  200.         end
  201.     end
  202.     for slot_n = 16, 2, -1 do
  203.         if turtle.getItemCount(slot_n) > 0 then
  204.             for slot_m = 1, slot_n-1 do
  205.                 if turtle.getItemCount(slot_m) == 0 then
  206.                     turtle.select(slot_n)
  207.                     turtle.transferTo(slot_m)
  208.                     break
  209.                 end
  210.             end
  211.         end
  212.     end
  213. end
  214.  
  215. local function inventoryManagement()
  216.     cleanUpInventory()
  217.     if emptySlots() <= 2 then
  218.         t.back()
  219.         t.turnAround()
  220.         if t.selectItem('chest') and turtle.place() then
  221.             print('Turtle placed chest.')
  222.             for i, v in ipairs(tDropTable) do
  223.                 t.dropAll(v)
  224.             end
  225.         end
  226.         t.dropAllButOneStack('cobblestone')
  227.         cleanUpInventory()
  228.         t.turnAround()
  229.         t.forward()
  230.     end
  231.     turtle.select(1)
  232. end
  233.  
  234. local function stepAround(stepsLeft)
  235.     local level = 1
  236.     while stepsLeft > 0 do
  237.         if isProtectedBlock(turtle.inspectUp()) then
  238.             -- case 1
  239.             t.back()
  240.             stepsLeft = stepsLeft + 1
  241.             if not isProtectedBlock(turtle.inspectUp()) then
  242.                 t.up()
  243.                 level = level + 1
  244.             end
  245.         elseif isProtectedBlock(turtle.inspect()) then
  246.             -- case 2
  247.             t.up()
  248.             level = level + 1
  249.             if not isProtectedBlock(turtle.inspect()) then
  250.                 t.forward()
  251.                 stepsLeft = stepsLeft - 1
  252.             end
  253.         elseif isProtectedBlock(turtle.inspectDown()) then
  254.             -- case 3
  255.             t.forward()
  256.             stepsLeft = stepsLeft - 1
  257.         elseif level > 1 then
  258.             -- get down
  259.             t.down()
  260.             level = level - 1
  261.         else
  262.             return stepsLeft
  263.         end
  264.     end
  265.     while level > 1 do
  266.         t.down()
  267.         level = level - 1
  268.     end
  269.     return stepsLeft
  270. end
  271.  
  272. local function tunnel(stepsLeft)
  273.     local torchCounter = 7
  274.     while stepsLeft > 0 do
  275.        
  276.         if isProtectedBlock(turtle.inspect()) then
  277.             local temp = stepAround(stepsLeft)
  278.             local diff = stepsLeft - temp
  279.             torchCounter = (torchCounter + diff) % 9
  280.             stepsLeft = temp
  281.         else
  282.             t.forward()
  283.             stepsLeft = stepsLeft - 1
  284.             torchCounter = (torchCounter + 1) % 9
  285.         end
  286.         dig3x3(torchCounter == 0)
  287.         inventoryManagement()
  288.         sleep(0)
  289.     end
  290. end
  291.  
  292. local function uTurnRight()
  293.     t.back()
  294.     t.turnRight()
  295.     direction = tTurnRightFacing[direction]
  296.     t.forward()
  297.     tunnel(6)
  298.     t.back()
  299.     t.turnRight()
  300.     direction = tTurnRightFacing[direction]
  301.     t.forward()
  302. end
  303.  
  304. local function uTurnLeft()
  305.     t.back()
  306.     t.turnLeft()
  307.     direction = tTurnLeftFacing[direction]
  308.     t.forward()
  309.     tunnel(6)
  310.     t.back()
  311.     t.turnLeft()
  312.     direction = tTurnLeftFacing[direction]
  313.     t.forward()
  314. end
  315.  
  316. local function unload()
  317.     for slot=1, 16 do
  318.         item = turtle.getItemDetail(slot)
  319.         if item and not tkeepItems[item.name] then
  320.             turtle.select(slot)
  321.             turtle.drop()
  322.         end
  323.     end
  324.     turtle.select(1)
  325. end
  326.  
  327. local function gotoNextTunnel()
  328.     t.forward()
  329.     t.forward()
  330.     t.turnLeft()
  331.     if t.selectItem('chest') and turtle.place() then
  332.         unload()
  333.     end
  334.     t.turnRight()
  335.     t.forward()
  336. end
  337.  
  338. -- **** MAIN ********
  339.  
  340. -- SETUP
  341. local tArgs = { ... }
  342. if #tArgs == 0 then
  343.     tunnelLength = false
  344. elseif #tArgs ~= 1 then
  345.     print( 'Usage: [mine] OR [mine <length>]' )
  346.     return
  347. else
  348.     tunnelLength = tonumber(tArgs[1])
  349.     if not (tunnelLength ~= nil and tunnelLength > 0 and tunnelLength <= 256) then
  350.         print('Argument "' .. tArgs[1] ..  '" is not a valid number (between 1 and 256)')
  351.         return
  352.     end
  353. end
  354.  
  355. t.setFuel(tFuel)
  356. if not t.existItem('torch') then
  357.     print('need torches...')
  358.     while not t.existItem('torch') do
  359.         sleep(1)
  360.     end
  361. end
  362. direction = determineFacing()
  363. if direction then
  364.     t.setDirection(direction)
  365.     direction = tTurnRightFacing[direction]
  366. else
  367.     error('ERROR: no Facing value')
  368. end
  369.  
  370. print('Going Mining...')
  371. print()
  372. -- SETUP END
  373.  
  374. if tunnelLength then
  375.     tunnel(tunnelLength)
  376. else
  377.     for n=1, numRepeats do
  378.         -- first tunnel
  379.         tunnel(39+3)
  380.         -- U-Turn
  381.         if (n % 2 == 0) then
  382.             uTurnLeft()
  383.         else
  384.             uTurnRight()
  385.         end    
  386.         -- second tunnel
  387.         tunnel(39)
  388.         -- cross the road
  389.         gotoNextTunnel()
  390.     end
  391. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement