Advertisement
bcash8

PlethoraBlockScannerStripMiner

May 19th, 2022 (edited)
867
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local MINFUELLEVEL = 4000
  3. local tunnelLength = 0
  4. local torches = false
  5. local garbage = {
  6.     ["minecraft:cobblestone"] = true,
  7.     ["minecraft:dirt"] = true,
  8.     ["minecraft:gravel"] = true,
  9.     ["minecraft:stone"] = true,
  10.     ["minecraft:sand"] = true
  11. }
  12.  
  13.  
  14.  
  15.  
  16.  
  17. local args = {...}
  18.  
  19. local sides = {"left", "right"}
  20. local equipment = {left = nil, right = nil}
  21. local tools = {["minecraft:compass"] = true, ["plethora:module"] = true, ["minecraft:diamond_pickaxe"] = true}
  22. local inventory = {} -- {itemName = {slot1, slot2, slot3}}
  23. local pos = vector.new(0, 0, 0)
  24. local heading = vector.new(1, 0, 0)
  25. local directions = {NORTH = vector.new(0, 0, -1), EAST = vector.new(1, 0, 0), SOUTH = vector.new(0, 0, 1), WEST = vector.new(-1, 0, 0)}
  26. local orientations = {[vector.new(0, 0, -1):tostring()] = 0, [vector.new(1, 0, 0):tostring()] = 1, [vector.new(0, 0, 1):tostring()] = 2, [vector.new(-1, 0, 0):tostring()] = 3}
  27. local home = vector.new(0, 0, 0)
  28. local startingHeading
  29. -- init
  30. function init()
  31.  
  32.     if args then
  33.         if args[1] then
  34.             tunnelLength = args[1]
  35.         end
  36.  
  37.         if args[2] then
  38.             torches = args[2]
  39.         end
  40.     end
  41.  
  42.     heading = getFacing()
  43.     startingHeading = heading
  44.     print(heading)
  45.     for _, side in pairs(sides) do
  46.         local tool = peripheral.getType(side)
  47.         if tool then
  48.             equipment[side] = tool
  49.         end
  50.     end
  51.  
  52.     for side, tool in pairs(equipment) do
  53.         print(side .. ": " .. tool)
  54.     end
  55.  
  56.     scanInventory()
  57. end
  58.  
  59. -- inventory functions
  60. function scanInventory()
  61.     inventory = {}
  62.  
  63.     for i = 1, 16 do
  64.         local data = turtle.getItemDetail(i)
  65.         if data then
  66.             if inventory[data.name] then
  67.                 table.insert(inventory[data.name], i)
  68.             else
  69.                 inventory[data.name] = {i}
  70.             end
  71.         end
  72.     end
  73. end
  74.  
  75. function selectItem(itemName)
  76.     scanInventory()
  77.     if not inventory[itemName] then return false end
  78.  
  79.     turtle.select(inventory[itemName][1])
  80.     return true
  81. end
  82.  
  83. function equipItem(itemName, side)
  84.     side = side or "right"
  85.     scanInventory()
  86.     if not inventory[itemName] then return false end
  87.     turtle.select(inventory[itemName][1])
  88.  
  89.     if side == "right" then
  90.         turtle.equipRight()
  91.     else
  92.         turtle.equipLeft()
  93.     end
  94. end
  95.  
  96. function purgeInv()
  97.     for i = 1, 16 do
  98.         local data = turtle.getItemDetail(i)
  99.         if data then
  100.             if garbage[data.name] then
  101.                 turtle.select(i)
  102.                 turtle.dropUp()
  103.             end
  104.  
  105.             for b = 1, i-1 do
  106.                 local d = turtle.getItemDetail(b)
  107.                 if d then
  108.                     if d.name == data.name then
  109.                         turtle.select(i)
  110.                         turtle.transferTo(b)
  111.                     end
  112.                 end
  113.             end
  114.         end
  115.     end
  116. end
  117.  
  118. function refuel()
  119.     scanInventory()
  120.     if inventory["minecraft:coal"] then
  121.         turtle.select(inventory["minecraft:coal"][1])
  122.         turtle.refuel()
  123.     end
  124. end
  125.  
  126. function emptyInventory()
  127.     purgeInv()
  128.  
  129.     for i = 1, 16 do
  130.         local data = turtle.getItemDetail(i)
  131.         if data then
  132.             if not tools[data.name] then
  133.                 turtle.select(i)
  134.                 turtle.drop()
  135.             end
  136.         end
  137.     end
  138. end
  139.  
  140. function getEmptySpace()
  141.     local emptySpace = 0
  142.  
  143.     for i = 1, 16 do
  144.         local count = turtle.getItemCount(i)
  145.         if count == 0 then
  146.             emptySpace = emptySpace + 1
  147.         end
  148.     end
  149.     return emptySpace
  150. end
  151. -- navigation
  152. function getFacing()
  153.     equipItem("minecraft:compass")
  154.     local facing = peripheral.call("right", "getFacing")
  155.     return directions[string.upper(facing)]
  156. end
  157.  
  158. function compareVectors(vec1, vec2)
  159.     if vec1.x == vec2.x and vec1.y == vec2.y and vec1.z == vec2.z then
  160.         return true
  161.     else
  162.         return false
  163.     end
  164. end
  165.  
  166. function turnRight()
  167.     turtle.turnRight()
  168.  
  169.     if compareVectors(heading, vector.new(1, 0, 0)) then
  170.         heading = vector.new(0, 0, 1)
  171.     elseif compareVectors(heading, vector.new(-1, 0, 0)) then
  172.         heading = vector.new(0, 0, -1)
  173.     elseif compareVectors(heading, vector.new(0, 0, 1)) then
  174.         heading = vector.new(-1, 0, 0)
  175.     elseif compareVectors(heading, vector.new(0, 0, -1)) then
  176.         heading = vector.new(1, 0, 0)
  177.     end
  178.  
  179.     return true
  180. end
  181.  
  182. function turnLeft()
  183.     turtle.turnLeft()
  184.  
  185.     if compareVectors(heading, vector.new(1, 0, 0)) then
  186.         heading = vector.new(0, 0, -1)
  187.     elseif compareVectors(heading, vector.new(-1, 0, 0)) then
  188.         heading = vector.new(0, 0, 1)
  189.     elseif compareVectors(heading, vector.new(0, 0, 1)) then
  190.         heading = vector.new(1, 0, 0)
  191.     elseif compareVectors(heading, vector.new(0, 0, -1)) then
  192.         heading = vector.new(-1, 0, 0)
  193.     end
  194.  
  195.     return true
  196. end
  197.  
  198. function forward()
  199.     local tries = 0
  200.     repeat
  201.         if tries > 50 then return false end
  202.         tries = tries + 1
  203.         turtle.dig()
  204.     until turtle.forward()
  205.     pos = pos + heading
  206.     return true
  207. end
  208.  
  209. function back()
  210.     if not turtle.back() then
  211.         turnRight()
  212.         turnRight()
  213.         forward()
  214.         turnLeft()
  215.         turnLeft()
  216.     else
  217.         pos = pos - heading
  218.     end
  219.  
  220.     return true
  221. end
  222.  
  223. function up()
  224.     repeat turtle.digUp() until turtle.up()
  225.  
  226.     pos = pos + vector.new(0, 1, 0)
  227.     return true
  228. end
  229.  
  230. function down()
  231.     local tries = 0
  232.     repeat
  233.         if tries > 50 then return false end
  234.         tries = tries + 1
  235.         turtle.digDown()
  236.     until turtle.down()
  237.  
  238.     pos = pos + vector.new(0, -1, 0)
  239.     return true
  240. end
  241.  
  242. function turnToHeading(newHeading)
  243.     if compareVectors(newHeading, heading) then
  244.         return true
  245.     end
  246.  
  247.     if compareVectors(newHeading, -heading) then
  248.         turnRight()
  249.         turnRight()
  250.         return true
  251.     end
  252.  
  253.     if orientations[newHeading:tostring()] == 0 and orientations[heading:tostring()] == 3 then
  254.         turnRight()
  255.         return true
  256.     end
  257.  
  258.     if orientations[newHeading:tostring()] == 3 and orientations[heading:tostring()] == 0 then
  259.         turnLeft()
  260.         return true
  261.     end
  262.  
  263.  
  264.     if orientations[newHeading:tostring()] > orientations[heading:tostring()] then
  265.         turnRight()
  266.     else
  267.         turnLeft()
  268.     end
  269. end
  270.  
  271. function goto(newPos)
  272.     if newPos.x > pos.x then
  273.         turnToHeading(vector.new(1, 0, 0))
  274.         while newPos.x > pos.x do
  275.             if not forward() then return false end
  276.         end
  277.     end
  278.  
  279.     if newPos.x < pos.x then
  280.         turnToHeading(vector.new(-1, 0, 0))
  281.         while newPos.x < pos.x do
  282.             if not forward() then return false end
  283.         end
  284.     end
  285.  
  286.     if newPos.z > pos.z then
  287.         turnToHeading(vector.new(0, 0, 1))
  288.         while newPos.z > pos.z do
  289.             if not forward() then return false end
  290.         end
  291.     end
  292.  
  293.     if newPos.z < pos.z then
  294.         turnToHeading(vector.new(0, 0, -1))
  295.         while newPos.z < pos.z do
  296.             if not forward() then return false end
  297.         end
  298.     end
  299.  
  300.     while newPos.y > pos.y do
  301.         up()
  302.     end
  303.  
  304.     while newPos.y < pos.y do
  305.         if not down() then return false end
  306.     end
  307. end
  308.  
  309.  
  310. -- mining functions
  311. function isOreBlock(name)
  312.     if string.find(name, "ore")  and string.find(name, "minecraft") then return true end
  313.     if string.find(name, "ore")  and string.find(name, "thermal") then return true end
  314.     return false
  315. end
  316.  
  317. function scanArea()
  318.     if equipment.right ~= "plethora:scanner" then
  319.         equipItem("plethora:module")
  320.     end
  321.  
  322.     local data = peripheral.call("right", "scan")
  323.     local ores = {}
  324.  
  325.     for _, block in pairs(data) do
  326.         if isOreBlock(block.name) then
  327.             block.x = block.x + pos.x
  328.             block.y = block.y + pos.y
  329.             block.z = block.z + pos.z
  330.             table.insert(ores, block)
  331.         end
  332.     end
  333.  
  334.     return ores
  335. end
  336.  
  337. function getDistance(pos1, pos2)
  338.     local x = math.abs(pos1.x - pos2.x)
  339.     local y = math.abs(pos1.y - pos2.y)
  340.     local z = math.abs(pos1.z - pos2.z)
  341.     return (x + y + z)
  342. end
  343.  
  344. function createMiningPath(ores)
  345.     local unassignedOres = ores
  346.     local assignedOres = {}
  347.  
  348.     local lowestScore = 999
  349.     local lowestIndex = 1
  350.     for i = 1, #unassignedOres do
  351.         local dis = getDistance(unassignedOres[i], pos)
  352.         if dis < lowestScore then
  353.             lowestIndex = i
  354.             lowestScore = dis
  355.         end
  356.     end
  357.  
  358.     table.insert(assignedOres, unassignedOres[lowestIndex])
  359.     table.remove(unassignedOres, lowestIndex)
  360.  
  361.     while #unassignedOres > 0 do
  362.  
  363.         local lowestScore = 999
  364.         local lowestIndex = 1
  365.         for i = 1, #unassignedOres do
  366.             local dis = getDistance(unassignedOres[i], assignedOres[#assignedOres])
  367.             if dis < lowestScore then
  368.                 lowestIndex = i
  369.                 lowestScore = dis
  370.             end
  371.         end
  372.         table.insert(assignedOres, unassignedOres[lowestIndex])
  373.         table.remove(unassignedOres, lowestIndex)
  374.     end
  375.  
  376.     print(#assignedOres)
  377.     return assignedOres
  378. end
  379.  
  380. function scanAndMine()
  381.     local ores = scanArea()
  382.     equipItem("minecraft:diamond_pickaxe")
  383.     local path = createMiningPath(ores)
  384.  
  385.     print(#path)
  386.     for i = 1, #path do
  387.         if i % 5 == 0 then
  388.             purgeInv()
  389.             if getEmptySpace() < 3 then
  390.                 dumpItems()
  391.             end
  392.         end
  393.  
  394.         goto(vector.new(path[i].x, path[i].y, path[i].z))
  395.     end
  396. end
  397.  
  398. function dumpItems()
  399.     local thisPos = pos
  400.     local thisHeading = heading
  401.     goto(home)
  402.     turnToHeading(-startingHeading)
  403.     emptyInventory()
  404.  
  405.     goto(thisPos)
  406.     turnToHeading(thisHeading)
  407. end
  408.  
  409. function main()
  410.     equipItem("minecraft:diamond_pickaxe")
  411.     for i = 1, 5 do
  412.         turtle.digUp()
  413.         forward()
  414.     end
  415.     for i = 1, tunnelLength/8 do
  416.         equipItem("minecraft:diamond_pickaxe")
  417.         for b = 1, 8 do
  418.             turtle.digUp()
  419.             forward()
  420.         end
  421.  
  422.         local curPos = pos
  423.         local curHeading = heading
  424.         scanAndMine()
  425.         goto(curPos)
  426.         turnToHeading(curHeading)
  427.         purgeInv()
  428.  
  429.         if turtle.getFuelLevel() < MINFUELLEVEL then
  430.             refuel()
  431.         end
  432.     end
  433.     goto(home)
  434.     turnToHeading(-startingHeading)
  435.     emptyInventory()
  436. end
  437.  
  438. init()
  439. main()
Advertisement
RAW Paste Data Copied
Advertisement