Advertisement
Bunny_bt

Mining Turtle

Feb 7th, 2023 (edited)
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.23 KB | None | 0 0
  1. ---@diagnostic disable: undefined-global
  2. local OreType = { "coal", "iron", "lapis", "gold", "redstone", "diamond", "emerald", "nether", "quartz", "uranium", "sulfur", "ruby", "sapphire", "peridot", "tin", "lead", "blockmetal", "copper", "basic_block_core", "nickel", "apatite", "amber", "crystal", "exploration:ore"}
  3. local position = vector.new(0, 0, 0)
  4. local isExplored = false
  5. local Visited = {}
  6. local Stack = {}
  7. local facing = 1
  8. local dirVec
  9.  
  10. function Nav(faceDir, digDir)
  11.     -- faceDir 1:North, 2:East, 3:South, 4:West
  12.     if faceDir - facing == 1 or faceDir - facing == -3 then
  13.         shell.run('turn', 'right', 1)
  14.         facing = facing + 1
  15.     elseif faceDir - facing == 2 or faceDir - facing == -2 then
  16.         shell.run('turn', 'right', 2)
  17.         facing = facing + 2
  18.     elseif faceDir - facing == 3 or faceDir - facing == -1 then
  19.         shell.run('turn', 'left', 1)
  20.         facing = facing - 1
  21.     end
  22.     --Correct facing
  23.     if facing == 5 then facing = 1
  24.     elseif facing == 6 then facing = 2
  25.     elseif facing == 0 then facing = 4
  26.     elseif facing == -1 then facing = 3
  27.     end
  28.  
  29.     if digDir == 'forward' then
  30.         while not turtle.forward() do
  31.             turtle.dig()
  32.         end
  33.         print('moved forward')
  34.     elseif digDir == 'back' then
  35.         if not turtle.back() then
  36.             shell.run('turn', 'right', 2)
  37.             while not turtle.forward() do
  38.                 turtle.dig()
  39.             end
  40.             shell.run('turn', 'right', 2)
  41.         end
  42.         print('moved back')
  43.     elseif digDir == 'up' then
  44.         while not turtle.up() do
  45.             turtle.digUp()
  46.         end
  47.         print('moved up')
  48.     elseif digDir == 'down' then
  49.         while not turtle.down() do
  50.             turtle.digDown()
  51.         end
  52.         print('moved down')
  53.     end
  54. end
  55.  
  56. function Move()
  57.     --Set direction vector to nearby ore
  58.     if Stack[#Stack] ~= nil and (Stack[#Stack]:sub(position)):length() == 1 then
  59.         isExplored = false
  60.         dirVec = Stack[#Stack]:sub(position)
  61.         table.insert(Visited, position)
  62.         table.remove(Stack, #Stack)
  63.         print('directed to nearby ore')
  64.     elseif Visited[#Visited] ~= nil then --Ore in stack too far or Stack depleted
  65.         print('directing to POI')
  66.         isExplored = true
  67.         dirVec = Visited[#Visited]:sub(position)
  68.         table.remove(Visited, #Visited)
  69.     end
  70.  
  71.     if dirVec ~= nil then
  72.         if dirVec:tostring() == vector.new(0, 1, 0):tostring() then
  73.             Nav(facing, 'up')
  74.             position = position:add(vector.new(0, 1, 0))
  75.         elseif dirVec:tostring() == vector.new(0, -1, 0):tostring() then
  76.             Nav(facing, 'down')
  77.             position = position:add(vector.new(0, -1, 0))
  78.         elseif dirVec:tostring() == vector.new(0, 0, 1):tostring() then
  79.             if isExplored == true then
  80.                 Nav(3, 'back')
  81.             else
  82.                 Nav(1, 'forward')
  83.             end
  84.             position = position:add(vector.new(0, 0, 1))
  85.         elseif dirVec:tostring() == vector.new(1, 0, 0):tostring() then
  86.             if isExplored == true then
  87.                 Nav(4, 'back')
  88.             else
  89.                 Nav(2, 'forward')
  90.             end
  91.             position = position:add(vector.new(1, 0, 0))
  92.         elseif dirVec:tostring() == vector.new(0, 0, -1):tostring() then
  93.             if isExplored == true then
  94.                 Nav(1, 'back')
  95.             else
  96.                 Nav(3, 'forward')
  97.             end
  98.             position = position:add(vector.new(0, 0, -1))
  99.         elseif dirVec:tostring() == vector.new(-1, 0, 0):tostring() then
  100.             if isExplored == true then
  101.                 Nav(2, 'back')
  102.             else
  103.                 Nav(4, 'forward')
  104.             end
  105.             position = position:add(vector.new(-1, 0, 0))
  106.         end
  107.     end
  108. end
  109.  
  110. function DFS()
  111.     print('Running DFS...')
  112.     while (Stack[#Stack] ~= nil) or (Visited[#Visited] ~= nil) do
  113.         for k, v in pairs(Stack) do
  114.             if (k < #Stack) and (v:tostring() == Stack[#Stack]:tostring()) then
  115.                 table.remove(Stack, k)
  116.             end
  117.         end
  118.         Move()
  119.         if isExplored == false then Inspect() end
  120.     end
  121. end
  122.  
  123. function Inspect()
  124.     local isBlock, blockInfo
  125.  
  126.     --Up
  127.     isBlock, blockInfo = turtle.inspectUp()
  128.     for i = 1, #OreType, 1 do
  129.         if isBlock and string.find(blockInfo.name, OreType[i]) then
  130.             table.insert(Stack, position:add(vector.new(0, 1, 0)))
  131.         end
  132.     end
  133.     --Down
  134.     isBlock, blockInfo = turtle.inspectDown()
  135.     for i = 1, #OreType, 1 do
  136.         if isBlock and string.find(blockInfo.name, OreType[i]) then
  137.             table.insert(Stack, position:add(vector.new(0, -1, 0)))
  138.         end
  139.     end
  140.     --Forward, Right, Back, Left
  141.     for f = 1, 4, 1 do
  142.         Nav(facing + 1)
  143.         isBlock, blockInfo = turtle.inspect()
  144.         for i = 1, #OreType, 1 do
  145.             if isBlock and string.find(blockInfo.name, OreType[i]) then
  146.                 print(facing, ": ", OreType[i])
  147.                 if facing == 1 then table.insert(Stack, position:add(vector.new(0, 0, 1))) end
  148.                 if facing == 2 then table.insert(Stack, position:add(vector.new(1, 0, 0))) end
  149.                 if facing == 3 then table.insert(Stack, position:add(vector.new(0, 0, -1))) end
  150.                 if facing == 4 then table.insert(Stack, position:add(vector.new(-1, 0, 0))) end
  151.             end
  152.         end
  153.     end
  154.  
  155.     if Stack[#Stack] ~= nil then DFS() end
  156. end
  157.  
  158. write('Distance: ')
  159. local distance = tonumber(read())
  160. local distTrack = 0
  161. for i = 1, distance/2,1 do
  162.     --Deposit when if half full
  163.     if turtle.getItemCount(8) > 0 then
  164.         turtle.digDown()
  165.         turtle.placeDown()
  166.         for i = 2, 16, 1 do
  167.             turtle.select(i)
  168.             turtle.dropDown()
  169.         end
  170.         turtle.select(1)
  171.         local itemData = turtle.getItemDetail(1)
  172.         if (itemData == nil or string.find(itemData.name, "chest") == nil) then
  173.             return
  174.         end
  175.     end
  176.  
  177.     Inspect()
  178.     Nav(1, 'up')
  179.     Inspect()
  180.     Nav(1, 'forward')
  181.     Inspect()
  182.     Nav(1,'down')
  183.     Inspect()
  184.     Nav(1, 'forward')
  185.     distTrack = distTrack + 2
  186. end
  187.  
  188. for i = 1, distTrack, 1 do
  189.     Nav(3, 'forward')
  190. end
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement