Advertisement
paramus

Auto Miner V2.0

Feb 26th, 2025 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 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 amount=amount*-1 end
  13.     if (amount==0) then return end
  14.    
  15.     for i=1,amount,1 do
  16.         turtle.digUp()
  17.         if (turtle.up()) then
  18.             y=y+1
  19.         end
  20.     end
  21. end
  22. function MoveDown(amount)
  23.     if (amount<0) then amount=amount*-1 end
  24.     if (amount==0) then return end
  25.    
  26.     for i=1,amount,1 do
  27.         turtle.digDown()
  28.         if (turtle.down()) then
  29.             y=y-1
  30.         end
  31.     end
  32. end
  33.  
  34. function MoveForward(amount)
  35.     if (amount<0) then amount=amount*-1 end
  36.     if (amount==0) then return end
  37.    
  38.     for i=1,amount,1 do
  39.         turtle.dig()
  40.         if (turtle.forward()) then
  41.             if (Orien==0)then
  42.                 z=z-1
  43.             elseif (Orien==1)then
  44.                 x=x+1
  45.             elseif (Orien==2)then
  46.                 z=z+1
  47.             elseif (Orien==3)then
  48.                 x=x-1
  49.             end
  50.         end
  51.     end
  52. end
  53.  
  54. function SetFacing(direction)
  55.     while(true) do
  56.         if (direction==Orien) then
  57.             break
  58.         end
  59.        
  60.         if (turtle.turnRight()) then
  61.             Orien = Orien+1
  62.             if (Orien>3) then Orien=0 end
  63.         end
  64.     end
  65. end
  66.  
  67. function Movement()
  68.     --X
  69.     if (x>Heading.x) then
  70.         SetFacing(3)
  71.     elseif (x<Heading.x) then
  72.         SetFacing(1)
  73.     end
  74.     MoveForward(Heading.x - x)
  75.    
  76.     --Y
  77.     if (y>Heading.y) then
  78.         MoveUp(Heading.y - y)
  79.     elseif (y<Heading.y) then
  80.         MoveDown(Heading.y - y)
  81.     end
  82.    
  83.     --Z
  84.     if (z>Heading.z) then
  85.         SetFacing(0)
  86.     elseif (z<Heading.z) then
  87.         SetFacing(2)
  88.     end
  89.     MoveForward(Heading.z - z)
  90. end
  91.  
  92. function Search()
  93.     turtle.select(2)
  94.     turtle.equipRight() --Equip scanner
  95.     local Scanner = peripheral.wrap("right")
  96.     local scan = Scanner.scan(16)
  97.     if (scan ~= nil) then
  98.         for i, data in ipairs(scan) do
  99.             if (string.match(data.name, Query)) then
  100.                 Heading.x = data.x+x
  101.                 Heading.y = data.y+y
  102.                 Heading.z = data.z+z
  103.                 break
  104.             end
  105.         end
  106.     end
  107.     turtle.select(2)
  108.     turtle.equipRight(2) --UnEquip scanner
  109.    
  110.     turtle.select(1) --Equip bucket
  111. end
  112.  
  113. function Refuel()
  114.     turtle.select(1) --Equip bucket
  115.    
  116.     local success, data = turtle.inspect()
  117.     if (success and data.name=="minecraft:lava") then
  118.         turtle.place()
  119.     end
  120.    
  121.     success, data = turtle.inspectUp()
  122.     if (success and data.name=="minecraft:lava") then
  123.         turtle.placeUp()
  124.     end
  125.    
  126.     success, data = turtle.inspectDown()
  127.     if (success and data.name=="minecraft:lava") then
  128.         turtle.placeDown()
  129.     end
  130. end
  131.  
  132. function Inventory()
  133.     --1 and 2 are the bucket and the scanner, so DON'T drop them
  134.     IsFull = true
  135.     for i=3,16,1 do
  136.         local data = turtle.getItemDetail(i)
  137.         if (string.match(data.name, Query)~=false) then
  138.             turtle.select(i)
  139.             turtle.drop()
  140.             IsFull = false
  141.         end
  142.     end
  143.     turtle.select(1) --Equip bucket
  144. end
  145.  
  146. function GoHome()
  147.     local FuelNeeded = math.floor((x+y+z)*2.5) -- Imagine is a straight line and then think it has to dig all the way
  148.     if (turtle.getFuelLevel()<=FuelNeeded or ISFull==true) then
  149.         Heading.x=0
  150.         Heading.y=0
  151.         Heading.z=0
  152.         Movement()
  153.         return true
  154.     end
  155.    
  156.     return false
  157. end
  158.  
  159. -- Main --
  160. term.setTextColor(colors.green)
  161. term.write("Instructions: Place a bucket in the first slot, then a geo scanner in the seccond, then equip a pickaxe on the right side and a chunk loader on the left.")
  162.  
  163. term.setTextColor(colors.cyan)
  164. term.write("Search for: ")
  165. Query = read()
  166. while(true) do
  167.     term.clear()
  168.     term.setCursorPos(1,1)
  169.     term.write(Query .. " at: " .. Heading.x .. " " .. Heading.y .. " " .. Heading.z)
  170.    
  171.     if (GoHome()==false) then
  172.         Search()
  173.         Movement()
  174.         Refuel()
  175.         Inventory()
  176.     else
  177.         term.clear()
  178.         term.setCursorPos(1,1)
  179.         term.write("Heading home.")
  180.         break
  181.     end
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement