SirSheepe

turtleboi

Jan 9th, 2021 (edited)
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.90 KB | None | 0 0
  1.  
  2. local Vector3 = {}
  3. Vector3.__index = Vector3
  4. Vector3.__add = function(a, b)
  5.     return Vector3.new(
  6.         a.x + b.x,
  7.         a.y + b.y,
  8.         a.z + b.z
  9.     )
  10. end
  11. Vector3.__sub = function(a, b)
  12.     return Vector3.new(
  13.         a.x - b.x,
  14.         a.y - b.y,
  15.         a.z - b.z
  16.     )
  17. end
  18. Vector3.__eq = function(a, b)
  19.     return a.x == b.x and a.y == b.y and a.z == b.z
  20. end
  21. Vector3.__tostring = function(self)
  22.     return self.x .. ", " .. self.y .. ", " .. self.z
  23. end
  24.  
  25. function Vector3.new(x, y, z)
  26.     return setmetatable({
  27.         x = x or 0,
  28.         y = y or 0,
  29.         z = z or 0
  30.     }, Vector3)
  31. end
  32.  
  33. function Vector3:length()
  34.     return math.abs(self.x) + math.abs(self.y) + math.abs(self.z)
  35. end
  36.  
  37. function Vector3:clone()
  38.     return Vector3.new(self.x, self.y, self.z)
  39. end
  40.  
  41. local dir = Vector3.new(0, 0, 1)
  42. local pos = Vector3.new()
  43.  
  44. local pathcost = 0
  45. local debounce = false
  46. local maxSteps = 63
  47. local steps = 0
  48. local maxTunnels = 16
  49. local tunnels = 1
  50.  
  51. local fuel = turtle.getFuelLevel()
  52.  
  53. local wantedOres = {
  54.     "minecraft:diamond_ore",
  55.     "minecraft:gold_ore",
  56.     "minecraft:iron_ore",
  57.     "minecraft:lapis_ore",
  58.     "minecraft:redstone_ore",
  59.     "minecraft:coal_ore",
  60.     "minecraft:emerald_ore",
  61.     "appliedenergistics2:quartz_ore",
  62.     "appliedenergistics2:charged_quartz_ore",
  63.     "thermal:apatite_ore",
  64.     "thermal:cinnabar_ore",
  65.     "thermal:niter_ore",
  66.     "thermal:sulfur_ore",
  67.     "thermal:copper_ore",
  68.     "thermal:tin_ore",
  69.     "thermal:lead_ore",
  70.     "thermal:silver_ore",
  71.     "thermal:nickel_ore",
  72.     "mekanism:osmium_ore",
  73.     "mekanism:uranium_ore",
  74.     "mekanism:fluorite_ore"
  75. }
  76.  
  77. local inventoryWhitelist = {
  78.     "minecraft:diamond",
  79.     "minecraft:gold_ore",
  80.     "minecraft:iron_ore",
  81.     "minecraft:lapis_lazuli",
  82.     "minecraft:redstone",
  83.     "minecraft:coal",
  84.     "minecraft:emerald",
  85.     "appliedenergistics2:certus_quartz_crystal",
  86.     "appliedenergistics2:charged_certus_quartz_crystal",
  87.     "thermal:apatite",
  88.     "thermal:cinnabar",
  89.     "thermal:niter",
  90.     "thermal:sulfur",
  91.     "thermal:copper_ore",
  92.     "thermal:tin_ore",
  93.     "thermal:lead_ore",
  94.     "thermal:silver_ore",
  95.     "thermal:nickel_ore",
  96.     "mekanism:osmium_ore",
  97.     "mekanism:uranium_ore",
  98.     "mekanism:fluorite_gem"
  99. }
  100.  
  101. local function contains(t, item)
  102.     for i = 1, #t do
  103.         if item == t[i] then return true end
  104.     end
  105.     return false
  106. end
  107.  
  108. local function refuel()
  109.     for i = 1, 16 do
  110.         local detail = turtle.getItemDetail(i)
  111.         if detail then
  112.             if detail.name == "minecraft:coal" then
  113.                 if turtle.getFuelLevel() < 1000 then
  114.                     turtle.select(i)
  115.                     repeat
  116.                         turtle.refuel(1)
  117.  
  118.                         if not turtle.getItemDetail(i) then
  119.                             break
  120.                         end
  121.                     until turtle.getFuelLevel() > 1000
  122.                 end
  123.             elseif not contains(inventoryWhitelist, detail.name) then
  124.                 turtle.select(i)
  125.                 turtle.dropDown()
  126.             end
  127.         end
  128.     end
  129.     fuel = turtle.getFuelLevel()
  130.     turtle.select(1)
  131. end
  132.  
  133. local function shouldReturn()
  134.     if debounce then return true end
  135.     debounce = fuel <= pathcost
  136.     refuel()
  137.     return fuel <= pathcost
  138. end
  139.  
  140. local function forward()
  141.     if shouldReturn() then return false end
  142.     if turtle.forward() then
  143.         pos = pos + dir
  144.         pathcost = pathcost + 1
  145.         fuel = fuel - 1
  146.         return true
  147.     end
  148.     return false
  149. end
  150.  
  151. local function back()
  152.     if shouldReturn() then return false end
  153.     if turtle.back() then
  154.         pos = pos - dir
  155.         pathcost = pathcost + 1
  156.         fuel = fuel - 1
  157.         return true
  158.     end
  159.     return false
  160. end
  161.  
  162. local function up()
  163.     if shouldReturn() then return false end
  164.     if turtle.up() then
  165.         pos.y = pos.y + 1
  166.         pathcost = pathcost + 1
  167.         fuel = fuel - 1
  168.         return true
  169.     end
  170.     return false
  171. end
  172.  
  173. local function down()
  174.     if shouldReturn() then return false end
  175.     if turtle.down() then
  176.         pos.y = pos.y - 1
  177.         pathcost = pathcost + 1
  178.         fuel = fuel - 1
  179.         return true
  180.     end
  181.     return false
  182. end
  183.  
  184. local function turnRight()
  185.     turtle.turnRight()
  186.     dir = Vector3.new(dir.z, 0, -dir.x)
  187. end
  188.  
  189. local function turnLeft()
  190.     turtle.turnLeft()
  191.     dir = Vector3.new(-dir.z, 0, dir.x)
  192. end
  193.  
  194. local function mineUp()
  195.     if shouldReturn() then return false end
  196.     if turtle.digUp() then
  197.         fuel = fuel - 1
  198.         return true
  199.     end
  200.     return false
  201. end
  202.  
  203. local function mineDown()
  204.     if shouldReturn() then return false end
  205.     if turtle.digDown() then
  206.         fuel = fuel - 1
  207.         return true
  208.     end
  209.     return false
  210. end
  211.  
  212. local function mineForward()
  213.     if shouldReturn() then return false end
  214.     if turtle.dig() then
  215.         fuel = fuel - 1
  216.         return true
  217.     end
  218.     return false
  219. end
  220.  
  221. local function dump()
  222.     for i = 16, 1, -1 do
  223.         turtle.select(i)
  224.         turtle.drop(64)
  225.     end
  226. end
  227.  
  228. local function mineVein()
  229.     local upsuc, updat = turtle.inspectUp()
  230.    
  231.     if upsuc and contains(wantedOres, updat.name) then
  232.         if mineUp() then
  233.             up()
  234.  
  235.             if shouldReturn() then
  236.                 pathcost = pathcost - 1
  237.                 down()
  238.                 return false
  239.             end
  240.  
  241.             if not mineVein() then
  242.                 return false
  243.             end
  244.  
  245.             pathcost = pathcost - 1
  246.             down()
  247.         else
  248.             return false
  249.         end
  250.     end
  251.  
  252.     local dwsuc, dwdat = turtle.inspectDown()
  253.  
  254.     if dwsuc and contains(wantedOres, dwdat.name) then
  255.         if mineDown() then
  256.             down()
  257.  
  258.             if shouldReturn() then
  259.                 pathcost = pathcost - 1
  260.                 up()
  261.                 return false
  262.             end
  263.  
  264.             if not mineVein() then
  265.                 return false
  266.             end
  267.  
  268.             pathcost = pathcost - 1
  269.             up()
  270.         else
  271.             return false
  272.         end
  273.     end
  274.  
  275.     local d0suc, d0dat = turtle.inspect()
  276.  
  277.     if d0suc and contains(wantedOres, d0dat.name) then
  278.         if mineForward() then
  279.             forward()
  280.  
  281.             if shouldReturn() then
  282.                 pathcost = pathcost - 1
  283.                 back()
  284.                 return false
  285.             end
  286.  
  287.             if not mineVein() then
  288.                 return false
  289.             end
  290.  
  291.             pathcost = pathcost - 1
  292.             back()
  293.         else
  294.             return false
  295.         end
  296.     end
  297.  
  298.     turnRight()
  299.  
  300.     local d1suc, d1dat = turtle.inspect()
  301.  
  302.     if d1suc and contains(wantedOres, d1dat.name) then
  303.         if mineForward() then
  304.             forward()
  305.  
  306.             if shouldReturn() then
  307.                 pathcost = pathcost - 1
  308.                 back()
  309.                 return false
  310.             end
  311.  
  312.             if not mineVein() then
  313.                 return false
  314.             end
  315.  
  316.             pathcost = pathcost - 1
  317.             back()
  318.         else
  319.             return false
  320.         end
  321.     end
  322.  
  323.     turnRight()
  324.  
  325.     local d2suc, d2dat = turtle.inspect()
  326.  
  327.     if d2suc and contains(wantedOres, d2dat.name) then
  328.         if mineForward() then
  329.             forward()
  330.  
  331.             if shouldReturn() then
  332.                 pathcost = pathcost - 1
  333.                 back()
  334.                 return false
  335.             end
  336.  
  337.             if not mineVein() then
  338.                 return false
  339.             end
  340.  
  341.             pathcost = pathcost - 1
  342.             back()
  343.         else
  344.             return false
  345.         end
  346.     end
  347.  
  348.     turnRight()
  349.  
  350.     local d3suc, d3dat = turtle.inspect()
  351.  
  352.     if d3suc and contains(wantedOres, d3dat.name) then
  353.         if mineForward() then
  354.             forward()
  355.  
  356.             if shouldReturn() then
  357.                 pathcost = pathcost - 1
  358.                 back()
  359.                 return false
  360.             end
  361.  
  362.             if not mineVein() then
  363.                 return false
  364.             end
  365.  
  366.             pathcost = pathcost - 1
  367.             back()
  368.         else
  369.             return false
  370.         end
  371.     end
  372.  
  373.     turnRight()
  374.  
  375.     return true
  376. end
  377.  
  378. local function step()
  379.     print("Path: ", pathcost, "Fuel: ", fuel)
  380.     if shouldReturn() then return false end
  381.     if forward() then
  382.         local canContinue = mineVein()
  383.         if not canContinue then
  384.             print("Forced to exit mining vein...")
  385.             return false
  386.         end
  387.     else
  388.         if mineForward() then
  389.             if shouldReturn() then return false end
  390.             return step()
  391.         else
  392.             return false
  393.         end
  394.     end
  395.     return true
  396. end
  397.  
  398. repeat
  399.     while true do
  400.         if not step() then break end
  401.         steps = steps + 1
  402.         if (steps >= maxSteps) then
  403.             break
  404.         end
  405.     end
  406.    
  407.     if debounce then
  408.         print("Ran out of fuel, returning...")
  409.  
  410.         repeat
  411.             turnRight()
  412.             print(dir)
  413.         until dir == Vector3.new(0, 0, -1)
  414.        
  415.         while pos.z > 0 do forward() end
  416.  
  417.         break
  418.     else
  419.         print("Reached max steps, returning to start")
  420.     end
  421.    
  422.     repeat
  423.         turnRight()
  424.         print(dir)
  425.     until dir == Vector3.new(0, 0, -1)
  426.    
  427.     while pos.z > 0 do forward() end
  428.    
  429.     print("Tunnel complete")
  430.  
  431.     dump()
  432.  
  433.     if tunnels >= maxTunnels then break end
  434.  
  435.     turnRight()
  436.  
  437.     mineForward()
  438.     forward()
  439.     mineForward()
  440.     forward()
  441.     mineForward()
  442.     forward()
  443.    
  444.     turnRight()
  445.  
  446.     debounce = false
  447.    
  448.     if shouldReturn() then break end
  449.  
  450.     steps = 0
  451.     pathcost = tunnels * 3
  452.  
  453.     tunnels = tunnels + 1
  454.  
  455.     print("Tunnel: ", tunnels, "/", maxTunnels)
  456.  
  457. until tunnels > maxTunnels
  458.  
  459. if tunnels > 1 then
  460.     turnLeft()
  461.  
  462.     while pos.x ~= 0 do forward() end
  463. end
  464.  
  465. turnRight()
  466.  
  467. print("Process completed")
Add Comment
Please, Sign In to add comment