Feldherren

Miner.lua (master)

May 8th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function find(itemName)
  2.     for curSlot = 1,16 do
  3.         if turtle.getItemCount(curSlot) >= 1 then
  4.             local data = turtle.getItemDetail(curSlot)
  5.             if data.name == itemName then
  6.                 return curSlot
  7.             end
  8.         end
  9.     end
  10.     return false
  11. end
  12.  
  13. local function pickSong()
  14.     song = math.random(2)
  15.     if song == 1 then
  16.         return "hi ho"
  17.     elseif song == 2 then
  18.         return "gold"
  19.     end
  20. end
  21.  
  22. local function hi_ho(dist, voicebox)
  23.     local line = dist % 19
  24.     if line == 0 then
  25.         voicebox.speak("Hi ho, hi ho")
  26.     elseif line == 1 then
  27.         voicebox.speak("It's off to work I go")
  28.     elseif line == 2 then
  29.         voicebox.speak("I dig dig dig dig dig dig dig")
  30.     elseif line == 3 then
  31.         voicebox.speak("in a mine the whole day through")
  32.     elseif line == 4 then
  33.         voicebox.speak("To dig dig dig dig dig dig dig")
  34.     elseif line == 5 then
  35.         voicebox.speak("Is what I'm made to do")
  36.     elseif line == 6 then
  37.         voicebox.speak("It ain't no trick to get rich quick")
  38.     elseif line == 7 then
  39.         voicebox.speak("If you dig dig dig with a shovel or a pick")
  40.     elseif line == 8 then
  41.         voicebox.speak("In a mine, in a mine")
  42.     elseif line == 9 then
  43.         voicebox.speak("In a mine, in a mine")
  44.     elseif line == 10 then
  45.         voicebox.speak("Where a million diamonds shine")
  46.     elseif line == 11 then
  47.         voicebox.speak("I dig dig dig dig dig dig dig")
  48.     elseif line == 12 then
  49.         voicebox.speak("from early morn to night")
  50.     elseif line == 13 then
  51.         voicebox.speak("I dig dig dig dig dig dig dig")
  52.     elseif line == 14 then
  53.         voicebox.speak("up everything in sight")
  54.     elseif line == 15 then
  55.         voicebox.speak("I dig up diamonds by the score")
  56.     elseif line == 16 then
  57.         voicebox.speak("A thousand emeralds, sometimes more")
  58.     elseif line == 17 then
  59.         voicebox.speak("I don't know what we dig them for")
  60.     elseif line == 18 then
  61.         voicebox.speak("I dig dig digga dig dig")
  62.     end
  63. end
  64.  
  65. local function traditionaDwarfishSong(voicebox)
  66.     voicebox.speak("gold, gold, gold, gold")
  67. end
  68.  
  69. local function sing(song, dist, voicebox)
  70.     if song == "hi ho" then
  71.         hi_ho(dist,voicebox)
  72.     elseif song == "gold" then
  73.         traditionaDwarfishSong(voicebox)
  74.     end
  75. end
  76.  
  77. local tArgs = { ... }
  78. if #tArgs ~= 1 then
  79.     print( "Usage: miner <distance>" )
  80.     return
  81. end
  82.  
  83. local maxDist = tonumber( tArgs[1] )
  84. if maxDist < 1 then
  85.     print( "Distance must be positive" )
  86.     return
  87. end
  88.  
  89. local function getFreeInventorySpace()
  90.     local freeSpace = 16
  91.     for curSlot = 1,16 do
  92.         if turtle.getItemCount(curSlot) ~= 0 then
  93.             freeSpace = freeSpace - 1
  94.         end
  95.     end
  96.     return freeSpace
  97. end
  98.  
  99. local function sortInventory()
  100.     local inventory = {}
  101.     for curSlot=1,16 do
  102.         if turtle.getItemCount(curSlot) ~= 0 then
  103.             inventory[curSlot] = turtle.getItemDetail(curSlot)
  104.         else
  105.             inventory[curSlot] = "empty"
  106.         end
  107.     end
  108.     for slotA=1,16 do
  109.         if inventory[slotA] ~= "empty" then
  110.             for slotB=1,16 do
  111.                 if inventory[slotB] ~= "empty" then
  112.                     if slotA ~= slotB then
  113.                         if (inventory[slotA].name == inventory[slotB].name) and (inventory[slotA].damage == inventory[slotB].damage) then
  114.                             if inventory[slotA].count + inventory[slotB].count <= 64 then
  115.                                 turtle.select(slotB)
  116.                                 turtle.transferTo(slotA)
  117.                                 inventory[slotB] = "empty"
  118.                             end
  119.                         end
  120.                     end
  121.                 end
  122.             end
  123.         end
  124.     end
  125. end
  126.  
  127. local function digAround()
  128.     while turtle.detectUp() do
  129.         turtle.digUp()
  130.     end
  131.     if turtle.detectDown() then
  132.         local success, data = turtle.inspectDown()
  133.         if success then
  134.             if not (data.name == "minecraft:cobblestone") and not (data.name == "minecraft:stone") and not (data.name == "minecraft:sand") and not (data.name == "minecraft:dirt") and not (data.name == "minecraft:gravel") and not (data.name == "chisel:marble2") and not (data.name == "chisel:limestone2") then
  135.                 turtle.digDown()
  136.             end
  137.         end
  138.        
  139.     end
  140.     if find("minecraft:cobblestone") then
  141.         turtle.select(find("minecraft:cobblestone"))
  142.         turtle.placeDown()
  143.     elseif find("minecraft:stone") then
  144.         turtle.select(find("minecraft:stone"))
  145.         turtle.placeDown()
  146.     end
  147. end
  148.  
  149. local function checkFuel(level)
  150.     if (turtle.getFuelLevel() <= level) and (find("minecraft:coal")) then
  151.         turtle.select(find("minecraft:coal"))
  152.         turtle.refuel(1)
  153.     end
  154. end
  155.  
  156. local function returnToStart(travelled, continue)
  157.     if continue then
  158.         print("Returning to deposit items, then continuing")
  159.     else
  160.         print("Returning to deposit items; job done")
  161.     end
  162.     turtle.turnLeft()
  163.     turtle.turnLeft()
  164.     for curDist=0,travelled do
  165.         checkFuel(10)
  166.         local success, data = turtle.inspect()
  167.         if success then
  168.             if data.name ~= "minecraft:chest" then
  169.                 while not turtle.forward() do
  170.                     turtle.dig()
  171.                 end
  172.             end
  173.         else
  174.             while not turtle.forward() do
  175.                 turtle.dig()
  176.             end
  177.         end
  178.     end
  179.  
  180.     local success, data = turtle.inspect()
  181.     if success then
  182.         if data.name == "minecraft:chest" then
  183.             for curSlot = 1,16 do
  184.                 if turtle.getItemCount(curSlot) ~= 0 then
  185.                     block = turtle.getItemDetail(curSlot)
  186.                     if (block.name ~= "minecraft:coal") and (block.name ~= "minecraft:torch") and (block.name ~= "tconstruct:stone_torch") then
  187.                         turtle.select(curSlot)
  188.                         turtle.drop()
  189.                     end
  190.                 end
  191.             end
  192.         end
  193.     end
  194.  
  195.     if continue then
  196.         turtle.turnLeft()
  197.         turtle.turnLeft()
  198.         for curDist=0,travelled-1 do
  199.             checkFuel(10)
  200.             local success, data = turtle.inspect()
  201.             if success then
  202.                 if data.name ~= "minecraft:chest" then
  203.                     while not turtle.forward() do
  204.                         turtle.dig()
  205.                     end
  206.                 end
  207.             else
  208.                 while not turtle.forward() do
  209.                     turtle.dig()
  210.                 end
  211.             end
  212.         end
  213.     end
  214. end
  215.  
  216. local travelDist = 0
  217. local vocal = peripheral.find("speaker")
  218. local song = pickSong()
  219.  
  220. while travelDist <= maxDist do
  221.     sing(song,travelDist,vocal)
  222.     checkFuel(10)
  223.     if (find("minecraft:torch") == false) and (find("tconstruct:stone_torch") == false) then
  224.         break
  225.     end
  226.     sortInventory()
  227.     if getFreeInventorySpace() <= 1 then
  228.         returnToStart(travelDist,true)
  229.     end
  230.     digAround()
  231.     turtle.turnLeft()
  232.     while not turtle.forward() do
  233.         turtle.dig()
  234.     end
  235.     digAround()
  236.     turtle.turnRight()
  237.     turtle.turnRight()
  238.     turtle.forward()
  239.     while not turtle.forward() do
  240.         turtle.dig()
  241.     end
  242.     digAround()
  243.     turtle.turnLeft()
  244.     turtle.turnLeft()
  245.     turtle.forward()
  246.     if (travelDist % 6 == 0) and ((find("minecraft:torch")) or (find("tconstruct:stone_torch"))) then
  247.         if find("tconstruct:stone_torch") then
  248.             turtle.select(find("tconstruct:stone_torch"))
  249.         else
  250.             turtle.select(find("minecraft:torch"))
  251.         end
  252.         turtle.place()
  253.     end
  254.     turtle.turnRight()
  255.     if travelDist <= maxDist then
  256.         while not turtle.forward() do
  257.             turtle.dig()
  258.         end
  259.         travelDist = travelDist + 1
  260.     end
  261. end
  262.  
  263. returnToStart(travelDist,false)
Advertisement
Add Comment
Please, Sign In to add comment