Advertisement
ecco7777

CC Autosapper

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