paramus

Auto Miner V2.0

Feb 26th, 2025 (edited)
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.04 KB | Gaming | 0 0
  1. -- West = Negative x
  2. -- Top = Positive y
  3. -- North = Negative z
  4.  
  5. local x,y,z = 0,0,0 --Relative to First position
  6. local Orien = 0 --0:Forward; 1:Right; 2:Back; 3:Left
  7. local Heading = {x=0,y=0,z=0}
  8. local Query
  9. local IsFull = false
  10.  
  11. function MoveUp(amount)
  12.     if (amount==0) then return end
  13.     if (amount<0) then amount=amount*-1 end
  14.    
  15.     for i=1,amount,1 do
  16.         turtle.digUp()
  17.         if (turtle.up()) then
  18.             y=y+1
  19.             term.setCursorPos(1,2)
  20.             term.write("I am at: " .. x .. " " .. y .. " " .. z)
  21.             term.setCursorPos(1,3)
  22.             term.write("Fuel: " .. turtle.getFuelLevel())
  23.         end
  24.     end
  25. end
  26. function MoveDown(amount)
  27.     if (amount==0) then return end
  28.     if (amount<0) then amount=amount*-1 end
  29.    
  30.     for i=1,amount,1 do
  31.         turtle.digDown()
  32.         if (turtle.down()) then
  33.             y=y-1
  34.             term.setCursorPos(1,2)
  35.             term.write("I am at: " .. x .. " " .. y .. " " .. z)
  36.             term.setCursorPos(1,3)
  37.             term.write("Fuel: " .. turtle.getFuelLevel())
  38.         end
  39.     end
  40. end
  41.  
  42. function MoveForward(amount)
  43.     if (amount==0) then return end
  44.     if (amount<0) then amount=amount*-1 end
  45.    
  46.     for i=1,amount,1 do
  47.         turtle.dig()
  48.         turtle.digDown()
  49.         if (turtle.forward()) then
  50.             if (Orien==0)then
  51.                 z=z-1
  52.             elseif (Orien==1)then
  53.                 x=x+1
  54.             elseif (Orien==2)then
  55.                 z=z+1
  56.             elseif (Orien==3)then
  57.                 x=x-1
  58.             end
  59.             term.setCursorPos(1,2)
  60.             term.write("I am at: " .. x .. " " .. y .. " " .. z)
  61.             term.setCursorPos(1,3)
  62.             term.write("Fuel: " .. turtle.getFuelLevel())
  63.         end
  64.     end
  65. end
  66.  
  67. function SetFacing(direction)
  68.     if (direction==3 and Orien==0) then
  69.         turtle.turnLeft()
  70.         Orien=Orien-1
  71.         return
  72.     end
  73.    
  74.     while(true) do
  75.         if (direction==Orien) then
  76.             break
  77.         end
  78.        
  79.         if (direction>Orien) then
  80.             if (turtle.turnRight()) then
  81.                 Orien = Orien+1
  82.                 if (Orien>3) then Orien=0 end
  83.             end
  84.         elseif (direction<Orien) then
  85.             if (turtle.turnLeft()) then
  86.                 Orien = Orien-1
  87.                 if (Orien<-1) then Orien=3 end
  88.             end
  89.         end
  90.     end
  91. end
  92.  
  93. function Movement()
  94.     --X
  95.     if (x>Heading.x) then
  96.         SetFacing(3)
  97.     elseif (x<Heading.x) then
  98.         SetFacing(1)
  99.     end
  100.     MoveForward(Heading.x - x)
  101.    
  102.     --Y
  103.     if (y>Heading.y) then
  104.         MoveDown(Heading.y - y)
  105.     elseif (y<Heading.y) then
  106.         MoveUp(Heading.y - y)
  107.     end
  108.    
  109.     --Z
  110.     if (z>Heading.z) then
  111.         SetFacing(0)
  112.     elseif (z<Heading.z) then
  113.         SetFacing(2)
  114.     end
  115.     MoveForward(Heading.z - z)
  116. end
  117.  
  118. function Search()
  119.     turtle.select(2)
  120.     turtle.equipRight() --Equip scanner
  121.    
  122.     local Scanner = peripheral.wrap("right")
  123.     local scan = Scanner.scan(5)
  124.     if (scan ~= nil) then
  125.         for i, data in ipairs(scan) do
  126.             if (string.match(data.name, Query)) then
  127.                 Heading.x = data.x+x
  128.                 Heading.y = data.y+y
  129.                 Heading.z = data.z+z
  130.                 turtle.equipRight(2) --UnEquip scanner
  131.                 return
  132.             end
  133.         end
  134.         --Wander North
  135.         Heading.x = 0
  136.         Heading.y = 0
  137.         Heading.z = z-5
  138.         turtle.equipRight(2) --UnEquip scanner
  139.     else
  140.         --Wander North
  141.         Heading.x = 0
  142.         Heading.y = 0
  143.         Heading.z = z-5
  144.         turtle.equipRight(2) --UnEquip scanner
  145.     end
  146. end
  147.  
  148. function Refuel()
  149.     turtle.select(1) --Select bucket
  150.    
  151.     local success, data = turtle.inspect()
  152.     if (success and data.name=="minecraft:lava") then
  153.         turtle.place()
  154.     end
  155.    
  156.     success, data = turtle.inspectUp()
  157.     if (success and data.name=="minecraft:lava") then
  158.         turtle.placeUp()
  159.     end
  160.    
  161.     success, data = turtle.inspectDown()
  162.     if (success and data.name=="minecraft:lava") then
  163.         turtle.placeDown()
  164.     end
  165. end
  166.  
  167. function Inventory()
  168.     --1 and 2 are the bucket and the scanner, so DON'T drop them
  169.     IsFull = true
  170.     for i=3,16,1 do
  171.         local data = turtle.getItemDetail(i)
  172.         if (data~=nil) then
  173.             if (string.match(data.name, Query)==nil) then -- Missing to count if items are full stacks
  174.                 turtle.select(i)
  175.                 turtle.drop()
  176.                 IsFull = false
  177.             elseif (data.count<64) then --Will not work if stack is 16
  178.                 IsFull = false
  179.             end
  180.         end
  181.     end
  182. end
  183.  
  184. function GoHome()
  185.     local FuelNeeded = ((x+y+z)*2)+100 -- Imagine is a straight line and then think it has to dig all the way
  186.                                        -- (+100 just in case there is gravel)
  187.    
  188.     if (turtle.getFuelLevel()<=FuelNeeded or IsFull==true) then
  189.         term.clear()
  190.         term.setCursorPos(1,1)
  191.         term.write("Heading home.")
  192.        
  193.         Heading.x=0
  194.         Heading.y=0
  195.         Heading.z=0
  196.         Movement()
  197.         return true
  198.     end
  199.    
  200.     return false
  201. end
  202.  
  203. -- Main --
  204. term.clear()
  205. term.setCursorPos(1,1)
  206. term.setTextColor(colors.orange)
  207. term.write("Place turtle facing North.")
  208. term.setCursorPos(1,2)
  209. term.setTextColor(colors.white)
  210. term.write("Place a bucket in the first slot,")
  211. term.setCursorPos(1,3)
  212. term.write("then a geo scanner in the second,")
  213. term.setCursorPos(1,4)
  214. term.write("then equip a pickaxe on the right side")
  215. term.setCursorPos(1,5)
  216. term.write("and a chunk loader on the left.")
  217.  
  218. term.setTextColor(colors.green)
  219. term.setCursorPos(1,7)
  220. term.write("Search for: ")
  221. Query = read()
  222. while(true) do
  223.     term.clear()
  224.     term.setCursorPos(1,1)
  225.     term.write(Query .. " at: " .. Heading.x .. " " .. Heading.y .. " " .. Heading.z)
  226.     term.setCursorPos(1,2)
  227.     term.write("I am at: " .. x .. " " .. y .. " " .. z)
  228.     term.setCursorPos(1,3)
  229.     term.write("Fuel: " .. turtle.getFuelLevel())
  230.    
  231.     if (GoHome()==false) then
  232.         Search()
  233.         Movement()
  234.         Refuel()
  235.         Inventory()
  236.     else
  237.         break
  238.     end
  239. end
Advertisement
Add Comment
Please, Sign In to add comment