adriannic

Mine.lua

Jan 16th, 2021 (edited)
1,993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.38 KB | None | 0 0
  1. modules=peripheral.wrap("left")
  2. scannerRange=8
  3. scannerWidth=scannerRange*2+1
  4. ores={}
  5. testBlock={
  6.     ["name"]="enderstorage:ender_storage"
  7. }
  8. desiredOres={
  9.     "minecraft:diamond_ore",
  10.     "minecraft:emerald_ore",
  11.     "minecraft:iron_ore",
  12.     "minecraft:gold_ore",
  13.     "minecraft:coal_ore",
  14.     "minecraft:lapis_ore",
  15.     "minecraft:redstone_ore",
  16.     "minecraft:quartz_ore",
  17.     "minecraft:quartz_block",
  18.     "thermalfoundation:ore",
  19.     "railcraft:ore_metal",
  20.     "aroma1997sdimension:miningore",
  21.     "astralsorcery:blockcustomore",
  22.     "thaumcraft:ore_quartz",
  23.     "appliedenergistics2:quartz_ore",
  24.     "appliedenergistics2:charged_quartz_ore",
  25.     "deepresonance:resonating_ore",
  26.     "bigreactors:oreanglesite",
  27.     "bigreactors:orebenitoite",
  28.     "bigreactors:oreyellorite",
  29.     "forestry:resources",
  30.     "ic2:resource",
  31.     "projectred-exploration:ore",
  32.     "rftools:dimensional_shard_ore",
  33.     "railcraft:ore_magic",
  34.     "thaumcraft:ore_cinnabar",
  35.     "woot:stygianironore",
  36.     "evilcraft:dark_ore",
  37.     "thaumcraft:crystal_aer",
  38.     "thaumcraft:crystal_ignis",
  39.     "thaumcraft:crystal_aqua",
  40.     "thaumcraft:crystal_terra",
  41.     "thaumcraft:crystal_ordo",
  42.     "thaumcraft:crystal_perditio",
  43.     "thaumcraft:crystal_vitium",
  44.     "bewitchment:opal_ore",
  45.     "bewitchment:salt_ore",
  46.     "galacticraftcore:basic_block_core",
  47.     ""
  48. }
  49. blacklist={
  50.     "minecraft:cobblestone",
  51.     "minecraft:dirt",
  52.     "minecraft:gravel",
  53.     "minecraft:stone"
  54. }
  55. x=0
  56. y=0
  57. z=0
  58. direction=90 --in degrees
  59. baseDirection=direction
  60.  
  61. local function move()
  62.     while not turtle.forward() do
  63.         turtle.dig()
  64.         sleep(.5)
  65.     end
  66. end
  67.  
  68. local function moveUp()
  69.     while not turtle.up() do
  70.         turtle.digUp()
  71.         sleep(.5)
  72.     end
  73. end
  74.  
  75. local function moveDown()
  76.     while not turtle.down() do
  77.         turtle.digDown()
  78.         sleep(.5)
  79.     end
  80. end
  81.  
  82. local function dig()
  83.     while turtle.detect() do
  84.         turtle.dig()
  85.         sleep(.5)
  86.     end
  87. end
  88.  
  89. local function digUp()
  90.     while turtle.detectUp() do
  91.         turtle.digUp()
  92.         sleep(.5)
  93.     end
  94. end
  95.  
  96. local function digDown()
  97.     while turtle.detectDown() do
  98.         turtle.digDown()
  99.         sleep(.5)
  100.     end
  101. end
  102.  
  103. local function findNearestOre()--done
  104.     if #ores>0 then
  105.         minDist=15
  106.         minI=1
  107.         for i=1,#ores do
  108.             dist=math.sqrt((ores[i].x-x)^2+(ores[i].y-y)^2+(ores[i].z-z)^2)
  109.             if dist<minDist then
  110.                 minDist=dist
  111.                 minI=i
  112.             end
  113.         end
  114.         return minI
  115.     end
  116. end
  117.  
  118. local function scan()--done
  119.     local scanned_blocks = modules.scan()
  120.     ores={}
  121.     for x=-scannerRange, scannerRange do
  122.         for z=-scannerRange, scannerRange do
  123.             for y=-scannerRange, scannerRange do
  124.                 local scanned=scanned_blocks[scannerWidth ^ 2 * (x + scannerRange) + scannerWidth * (y + scannerRange) + (z + scannerRange) + 1]
  125.                 if scanned then
  126.                     for i=1,#desiredOres do
  127.                         if scanned.name == desiredOres[i] then
  128.                             ores[#ores+1]={["x"]=x,["y"]=y,["z"]=z}
  129.                         elseif scanned.name == testBlock.name then
  130.                             testBlock.x=x
  131.                             testBlock.y=y
  132.                             testBlock.z=z
  133.                             break
  134.                         end
  135.                     end
  136.                 end
  137.             end
  138.         end
  139.     end
  140. end
  141.  
  142. local function checkDirection()--done
  143.     turtle.select(16)
  144.     while turtle.detectUp() do
  145.         turtle.digUp()
  146.         sleep(1)
  147.     end
  148.     turtle.placeUp()
  149.     scan()
  150.     oldX=testBlock.x
  151.     oldZ=testBlock.z
  152.     turtle.select(1)
  153.     move()
  154.     scan()
  155.     distX=testBlock.x-oldX
  156.     distZ=testBlock.z-oldZ
  157.     if distX==1 then
  158.         direction=270
  159.     elseif distX==-1 then
  160.         direction=90
  161.     elseif distZ==1 then
  162.         direction=180
  163.     elseif distZ==-1 then
  164.         direction=0
  165.     end
  166.     baseDirection=direction
  167.     turtle.back()
  168.     turtle.select(16)
  169.     turtle.digUp()
  170. end
  171.  
  172. local function turn(dir)--done
  173.     local turns = ((dir-direction)%360)/90
  174.     if turns>0 then
  175.         for i=1,turns do
  176.             turtle.turnLeft()
  177.         end
  178.     end
  179.     direction=dir
  180. end
  181.  
  182. local function store()--done
  183.     turtle.select(16)
  184.     digDown()
  185.     turtle.placeDown()
  186.     for i=1, 15 do
  187.         turtle.select(i)
  188.         if #blacklist>0 then
  189.             for j=1,#blacklist do
  190.                 if turtle.getItemDetail() then
  191.                     if turtle.getItemDetail().name == blacklist[j] then
  192.                         turtle.dropUp()
  193.                     end
  194.                 end
  195.             end
  196.         end
  197.         turtle.dropDown()
  198.     end
  199.     turtle.select(16)
  200.     digDown()
  201. end
  202.  
  203. local function goToNearestOre()--done
  204.     while #ores>0 do
  205.         if #ores%8 == 7 then
  206.             store()
  207.         end
  208.         local i=findNearestOre()
  209.         if ores[i].y > y then
  210.             for i=1,ores[i].y-y do
  211.                 moveUp()
  212.             end
  213.         elseif ores[i].y < y then
  214.             for i=1,y-ores[i].y do
  215.                 while not turtle.down() do
  216.                     digDown()
  217.                     sleep(.5)
  218.                 end
  219.             end
  220.         end
  221.         if ores[i].x > x then
  222.             for i=1,ores[i].x-x do
  223.                 move()
  224.             end
  225.         elseif ores[i].x < x then
  226.             turtle.turnLeft()
  227.             turtle.turnLeft()
  228.             for i=1,x-ores[i].x do
  229.                 move()
  230.             end
  231.             turtle.turnLeft()
  232.             turtle.turnLeft()
  233.         end
  234.         if ores[i].z > z then
  235.             turtle.turnRight()
  236.             for i=1,ores[i].z-z do
  237.                 move()
  238.             end
  239.             turtle.turnLeft()
  240.         elseif ores[i].z < z then
  241.             turtle.turnLeft()
  242.             for i=1,z-ores[i].z do
  243.                 move()
  244.             end
  245.             turtle.turnRight()
  246.         end
  247.         x=ores[i].x
  248.         y=ores[i].y
  249.         z=ores[i].z
  250.         table.remove(ores,i)
  251.     end
  252.  
  253.     if 0 > y then
  254.             for i=1,0-y do
  255.                 moveUp()
  256.             end
  257.     elseif 0 < y then
  258.         for i=1,y-0 do
  259.             while not turtle.down() do
  260.                 digDown()
  261.                 sleep(.5)
  262.             end
  263.         end
  264.     end
  265.     if 0 > x then
  266.         for i=1,0-x do
  267.             move()
  268.         end
  269.     elseif 0 < x then
  270.         turtle.turnLeft()
  271.         turtle.turnLeft()
  272.         for i=1,x-0 do
  273.             move()
  274.         end
  275.         turtle.turnLeft()
  276.         turtle.turnLeft()
  277.     end
  278.     if 0 > z then
  279.         turtle.turnRight()
  280.         for i=1,0-z do
  281.             move()
  282.         end
  283.         turtle.turnLeft()
  284.     elseif 0 < z then
  285.         turtle.turnLeft()
  286.         for i=1,z-0 do
  287.             move()
  288.         end
  289.         turtle.turnRight()
  290.     end
  291.     x=0
  292.     y=0
  293.     z=0
  294. end
  295.  
  296. local function tunnel(n)--done
  297.     for j=1,n do
  298.         for i=1,scannerWidth do
  299.             move()
  300.             digUp()
  301.             digDown()
  302.         end
  303.         for i=1,scannerRange do
  304.             turtle.back()
  305.         end
  306.         turn(90)
  307.         scan()
  308.         goToNearestOre()
  309.         turn(baseDirection)
  310.         direction=baseDirection
  311.         store()
  312.         for i=1,scannerRange do
  313.             turtle.forward()
  314.         end
  315.     end
  316. end
  317.  
  318. checkDirection()
  319. tunnel(4)
Add Comment
Please, Sign In to add comment