Advertisement
ecco7777

CC OreFinder

Nov 18th, 2022 (edited)
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. --1 Pickaxe, 2 Diamondpickaxe, turtle.turtl
  2. p=peripheral.wrap("left")
  3. s=peripheral.wrap("right")
  4. maxDmg=249
  5. whitelist="appliedenergistics2:quartz_ore/0thermalfoundation:ore/0thermalfoundation:ore/1"
  6. blocks={}
  7. nesw={"north","east","south","west"}
  8. neswNum={}
  9. neswNum["north"]=1
  10. neswNum["east"]=2
  11. neswNum["south"]=3
  12. neswNum["west"]=4
  13. neiCoords={{x=1,y=0,z=0},{x=-1,y=0,z=0},{x=0,y=1,z=0},{x=0,y=-1,z=0},{x=0,y=0,z=1},{x=0,y=0,z=-1}}
  14. neswChart={north={north=0,east=1,south=2,west=3},east={north=3,east=0,south=1,west=2},south={north=2,east=3,south=0,west=1},west={north=1,east=2,south=3,west=0}}
  15. function fingerprint(block)
  16.     return block.name.."/"..block.metadata
  17. end
  18.  
  19. function equip(slot)
  20. if turtle.getSelectedSlot()~=slot then
  21.     turtle.select(slot)
  22. end
  23. turtle.equipLeft()
  24. end
  25.  
  26. function sDig()
  27.     while turtle.detectUp() do
  28.         turtle.digUp()
  29.     end
  30.     while turtle.detect() do
  31.         --p.swing()
  32.         turtle.dig()
  33.     end
  34.     while turtle.detectDown() do
  35.         turtle.digDown()
  36.     end
  37. end
  38.  
  39. function sDigUp()
  40.     if turtle.getItemDetail(1) ~=nil then
  41.         if turtle.getItemDetail(1).damage < maxDmg and string.find(turtle.getItemDetail(1).name,"pickaxe") then
  42.             while turtle.detectUp() do
  43.                 p.swing()
  44.             end
  45.         end
  46.     end
  47. end
  48.  
  49. function getDistance(b)
  50.     return math.pow(posify(b.x)+posify(b.y)+posify(b.z),0.5)
  51. end
  52.  
  53. function sort(a,b)
  54.     return getDistance(a)<getDistance(b)
  55. end
  56.  
  57. function posify(val)
  58.     return math.sqrt(val*val)
  59. end
  60.  
  61. function getFacing()
  62.     rotate={x=1,z=1}
  63.     blocks=s.scan()
  64.     facing=blocks[#blocks/2+0.5].state.facing
  65.     if facing=="south" then rotate.z=-1 end
  66.     if facing=="west" then rotate.x=-1 end
  67.     return rotate, facing
  68. end
  69.  
  70. function getOres()
  71.     blocks=s.scan()
  72.     ores={}
  73.     for i=1,#blocks do
  74.         if string.find(whitelist,fingerprint(blocks[i]))~=nil then
  75.             table.insert(ores,blocks[i])
  76.         end
  77.     end
  78.     table.sort(ores,sort)
  79.     print(fingerprint(ores[1]))
  80.     return ores
  81. end
  82.  
  83. function goToBlock(block)
  84.     path=""
  85.     r = getFacing()
  86.     if block.x~=0 then
  87.         if block.x>0 then
  88.             path=path.."e"
  89.             path=path..string.rep("kf",block.x)
  90.         end
  91.         if block.x<0 then
  92.             path=path.."w"..string.rep("kf",posify(block.x))
  93.         end
  94.     end
  95.     if block.z~=0 then
  96.         if block.z>0 then
  97.             path=path.."s"..string.rep("kf",block.z)
  98.         end
  99.         if block.z<0 then
  100.             path=path.."n"..string.rep("kf",posify(block.z))
  101.         end
  102.     end
  103.     if block.y~=0 then
  104.         if block.y>0 then
  105.             path=path..string.rep("ju",block.y)
  106.         end
  107.         if block.y<0 then
  108.             path=path..string.rep("gd",posify(block.y))
  109.         end
  110.     end
  111.     executeDrive(path)
  112. end
  113.  
  114. function sTurn(val)
  115.     if val==1 or val=="right" then
  116.         turtle.turnRight()
  117.     end
  118.     if val==2 or val=="back" then
  119.         turtle.turnLeft()
  120.         turtle.turnLeft()
  121.     end
  122.     if val==3 or val=="left" then
  123.         turtle.turnLeft()
  124.     end
  125.     if val=="north" or val=="east" or val=="south" or val=="west" then
  126.         getFacing()
  127.         sTurn(neswChart[facing][val])
  128.     end
  129. end
  130.  
  131. function executeDrive(path)
  132.     print(path)
  133.     for ip=1,#path do
  134.         lts=string.sub(path,ip,ip)
  135.         if lts=="f" then turtle.forward() end
  136.         if lts=="u" then turtle.up() end
  137.         if lts=="d" then turtle.down() end
  138.         if lts=="b" then sForward(-1) end
  139.         if lts=="r" then sTurn(1) end
  140.         if lts=="l" then sTurn(3) end
  141.         if lts=="n" then sTurn("north") end
  142.         if lts=="s" then sTurn("south") end
  143.         if lts=="e" then sTurn("east") end
  144.         if lts=="w" then sTurn("west") end
  145.         if lts=="k" then turtle.dig() end
  146.         if lts=="j" then turtle.digUp() end
  147.         if lts=="g" then turtle.digDown() end
  148.         if lts=="p" then
  149.             while turtle.placeDown()==false do
  150.                 sleep(waitDelay)
  151.             end
  152.         end
  153.     end
  154. end
  155.  
  156. function dump()
  157.     if turtle.getItemCount(16)>0 then
  158.         turtle.select(1)
  159.         turtle.placeUp()
  160.         for ic=2,16 do
  161.             turtle.select(ic)
  162.             turtle.dropUp()
  163.         end
  164.         turtle.select(1)
  165.         turtle.digDown()
  166.     end
  167. end
  168.  
  169. function getNeighbors(block,blocks)
  170.     nei={}
  171.     for i=1,#blocks do
  172.         if blocks[i].name=="minecraft:air" then
  173.             if blocks[i].x==neiCoords[1].x and blocks[i].y==neiCoords[1].y and blocks[i].z==neiCoords[1].z or blocks[i].x==neiCoords[2].x and blocks[i].y==neiCoords[2].y and blocks[i].z==neiCoords[2].z or blocks[i].x==neiCoords[3].x and blocks[i].y==neiCoords[3].y and blocks[i].z==neiCoords[3].z or blocks[i].x==neiCoords[4].x and blocks[i].y==neiCoords[4].y and blocks[i].z==neiCoords[4].z or blocks[i].x==neiCoords[5].x and blocks[i].y==neiCoords[5].y and blocks[i].z==neiCoords[5].z or blocks[i].x==neiCoords[6].x and blocks[i].y==neiCoords[6].y and blocks[i].z==neiCoords[6].z then
  174.                 table.insert(nei,blocks[i])
  175.             end
  176.         end
  177.        
  178.     end
  179. end
  180.  
  181. function mineStuff()
  182.     while #getOres()>0 do
  183.         ores=getOres()
  184.         if #ores>0 then
  185.             print(ores[1].x.."/"..ores[1].y.."/"..ores[1].z.."/"..ores[1].name.."/"..getDistance(ores[1]))
  186.             goToBlock(ores[1])
  187.             dump()
  188.         else
  189.             executeDrive("df")
  190.         end
  191.     end
  192. end
  193. --debug
  194. mineStuff()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement