t8rt0t

mineAPI.lua

Mar 13th, 2024 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.54 KB | None | 0 0
  1. if not refuel then os.loadAPI("apis/refuel.lua") end
  2. if not stats then os.loadAPI("apis/stats.lua") end
  3. if not turt then os.loadAPI("apis/turt.lua") end
  4. if not docs then os.loadAPI("apis/docs.lua") end
  5.  
  6. --Type registry
  7. local types = {
  8.     ["VectorND"]={colors.lightBlue,"A vector with an arbitrary number of entries"},
  9.     ["Vector2D"]={colors.lightBlue,"A vector with 2 entries"},
  10.     ["Vector3D"]={colors.lightBlue,"A vector with 3 entries"}
  11. }
  12. for k,v in pairs(types) do
  13.     docs.registerType(k,v[1],v[2])
  14. end
  15.  
  16. --Adds two vectors together
  17. --@Arg VectorND v1: The first vector to be added
  18. --@Arg VectorND v2: the second vector to be added
  19. --@Returns VectorND v: The sum of the two vectors
  20. --@Returns boolean false: Returns false if the two vectors are different lengths or if the vectors arent numeric
  21. function add(v1,v2)
  22.     if type(v1) ~= "table" or type(v2) ~= "table" then return false end
  23.     if #v1 ~= #v2 then return false end
  24.     local t = {}
  25.     for k,v in pairs(v1) do
  26.         if not tonumber(v2[k]) or not tonumber(v) then return false end
  27.         t[k] = tonumber(v)+tonumber(v2[k])
  28.     end
  29.     return t
  30. end
  31.  
  32. --Multiplies a vector by a constant
  33. --@Arg VectorND v1: The vector to be scaled
  34. --@Arg number c: The constant to scale the vector by
  35. --@Returns VectorND v: The scaled vector
  36. --@Returns boolean false: If the vector had non-numeric entries
  37. function multiply(v1,c)
  38.     c = tonumber(c)
  39.     if type(v1) ~= "table" or not c then return false end
  40.     local t = {}
  41.     for k,v in pairs(v1) do
  42.         if not tonumber(v) then return false end
  43.         t[k] = tonumber(v)*c
  44.     end
  45.     return t
  46. end
  47. --Moves a turtle to a position
  48. --@Arg number x: The x position the turtle should move to (x is right/left in the original, rot=0 reference frame)
  49. --@Arg number y: The y position the turtle should move to (y is up/down)
  50. --@Arg number z: the z position the turtle should move to (z is forward/back in the original reference frame)
  51. --@OptionalArg number rot: The turtle's horizontal rotation from the original reference frame, measured in number of right turns
  52. --@Returns boolean success: Whether the turtle successfully moved to its target position
  53. function navToVec(x,y,z,rot)
  54.     if not x or not y or not z then return false end
  55.     rot = (tonumber(rot) or 0)%4
  56.     for i=1,rot do
  57.         turt.turnLeft()
  58.     end
  59.     while y ~= 0 do
  60.         if y > 0 then
  61.             turt.digDown()
  62.             turt.down()
  63.             y = y-1
  64.         else
  65.             turt.digUp()
  66.             turt.up()
  67.             y = y+1
  68.         end
  69.     end
  70.     local flag = false
  71.     if z < 0 then
  72.         turt.turnLeft(2)
  73.         flag = true
  74.     end
  75.     while z ~= 0 do
  76.         turt.dig()
  77.         turt.forward()
  78.         if z < 0 then z = z -1 else z = z+1 end
  79.     end
  80.     if flag then turt.turnLeft(2) end
  81.     flag = 0
  82.     if x > 0 then
  83.         turt.turnRight()
  84.         flag = 1
  85.     elseif x < 0 then
  86.         turt.turnLeft()
  87.         flag = 2
  88.     end
  89.     while x ~= 0 do
  90.         turt.dig()
  91.         turt.forward()
  92.         if x > 0 then x = x-1 else x= x+1 end
  93.     end
  94.     if flag == 1 then turt.turnLeft()
  95.     elseif flag == 2 then turt.turnRight() end
  96.     return true
  97. end
  98. --Reads Blockdata tags (from turtle.inspect) to get its ore tag (more specific ore tags preferred)
  99. --@Arg table tags: A list of all of the block tags
  100. --@Returns string tag: The block's longest ore tag in the format "forge:ores/*"
  101. --@Returns boolean false: If no ore tag was found
  102. function getOreTag(tags)
  103.     if not tags then return false end
  104.     local tag = nil
  105.     for k,v in pairs(tags) do
  106.         local tempTag = string.match(k,"forge:ores/?%w*")
  107.         if not tag and tempTag then tag = tempTag end
  108.         if tempTag and #tempTag > #tag then tag = tempTag end
  109.     end
  110.     return tag or false
  111. end
  112. --Gets the priority value of an ore
  113. --@Arg string ore: The name of the ore, eg "minecraft:diamond_ore"
  114. --@OptionalArg table tags: The block tags of the block to inspect
  115. --@Returns number priority: The priority value of the ore, with lower numbers being prioritized
  116. --@Returns boolean false: If the block had no ore priority defined (likely not an ore)
  117. function getOrePriority(ore,tags)
  118.     if fs.exists("ore_priority.json") then
  119.         local file = fs.open("ore_priority.json","r")
  120.         local data = textutils.unserialiseJSON(file.readAll())
  121.         file.close()
  122.         if data and data[ore] then return data[ore] end
  123.         local tag = getOreTag(tags)
  124.         if data and tag and data[tag] then return data[tag] end
  125.     end
  126.     if getOreTag(tags) then return 5 end
  127.     return false
  128. end
  129. --Basic, cheap ore detection to know whether or not to trigger a vein mine
  130. --@Returns boolean foundOre: If there was an ore found in front of, above, or below the turtle (takes no moves to check these)
  131. function detectOres()
  132.     local s,d = turtle.inspect()
  133.     if d and d.tags and d.tags["forge:ores"] then return true end
  134.     local s,d = turtle.inspectUp()
  135.     if d and d.tags and d.tags["forge:ores"] then return true end
  136.     local s,d = turtle.inspectDown()
  137.     if d and d.tags and d.tags["forge:ores"] then return true end
  138.     return false
  139. end
  140. --Direction is local rotation, rot is absolute rotation
  141. --@Arg string direction: ["up","down","left","right","forward","back"], the local rotation of the turtle
  142. --@OptionalArg number rot: The absolute rotation of the reference frame
  143. --@Returns Vector3D v: The absolute displacement of the turtle if it were to move in that direction, in the original reference frame
  144. function displacementToVec(direction,rot)
  145.     if direction == "up" then return {0,1,0} end
  146.     if direction == "down" then return {0,-1,0} end
  147.     rot = tonumber(rot) or 0
  148.     local directions = {
  149.         ["forward"]= 0,
  150.         ["right"]= 1,
  151.         ["back"]= 2,
  152.         ["left"]= 3
  153.     }
  154.     rot = (rot+directions[direction])%4
  155.     return {math.floor(math.sin(rot*math.pi/2)+0.5),0,math.floor(math.cos(rot*math.pi/2)+0.5)}
  156. end
  157. -- ExploreLimit also affects how much each priority of ore will increase the explore limit
  158. -- At its default of 2, mining a diamond will increase the exploreLimit by 2,
  159. -- osmium will increase it by 1, iron will not affect the exploreLimit, and copper will actually decrease it by 2
  160. --Highly prioritized ores like diamonds or ancient debris will have the turtle explore more blocks to try to find the whole vein
  161. --@OptionalArg number limit: An upper limit on the number of ores the turtle can mine. Higher priority ores have a higher limit
  162. --@OptionalArg Vector3D displacement: The displacement of the turtle from its original position before mining ores
  163. --@OptionalArg number rot: The rotation of the turtle from its original reference frame
  164. --@OptionalArg number exploreLimit: The number of extra blocks the turtle can break to search for hidden ore pockets
  165. --@OptionalArg number exploreFactor: How much mining ores increases the exploreLimit
  166. --@Returns Vector3D displacement, number rot, number exploreLimit: The turtle's final displacement and rotation, as well as the exploreLimit
  167. function mineOres(limit,displacement,rot,exploreLimit,exploreFactor)
  168.     limit = tonumber(limit) or 200
  169.     exploreLimit = tonumber(exploreLimit) or 0
  170.     exploreFactor = tonumber(exploreFactor) or 2
  171.     displacement = displacement or {0,0,0}
  172.     rot = rot or 0
  173.     if limit <= 0 then return displacement,rot end
  174.     local canExplore = true
  175.     local success,data = turtle.inspect()
  176.     --Foward highest priority, since least costly
  177.     if success then
  178.         local priority = getOrePriority(data.name,data.tags)
  179.         if priority then
  180.             turt.dig()
  181.             turt.forward()
  182.             limit = limit - priority
  183.             displacement = add(displacement,displacementToVec("forward",rot))
  184.             displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+exploreFactor-priority,exploreFactor)
  185.             turt.back()
  186.             displacement = add(displacement,displacementToVec("back",rot))
  187.             canExplore = false --Higher priority ores, like diamonds, will expand exploreLimit
  188.         end
  189.     end
  190.     --Up also not costly
  191.     if limit <= 0 then return displacement,rot,exploreLimit end
  192.     local success,data = turtle.inspectUp()
  193.     if success then
  194.         local priority = getOrePriority(data.name,data.tags)
  195.         if priority then
  196.             turt.digUp()
  197.             turt.up()
  198.             limit = limit-priority
  199.             displacement = add(displacement,displacementToVec("up",rot))
  200.             displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
  201.             turt.down()
  202.             displacement = add(displacement,displacementToVec("down",rot))
  203.             canExplore = false
  204.         end
  205.     end
  206.     --Down also cheap since turtles can mine up and down
  207.     if limit <= 0 then return displacement,rot,exploreLimit end
  208.     local success,data = turtle.inspectDown()
  209.     if success then
  210.         local priority = getOrePriority(data.name,data.tags)
  211.         if priority then
  212.             turt.digDown()
  213.             turt.down()
  214.             limit = limit-priority
  215.             displacement = add(displacement,displacementToVec("down",rot))
  216.             displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
  217.             turt.up()
  218.             displacement = add(displacement,displacementToVec("up",rot))
  219.             canExplore = false
  220.         end
  221.     end
  222.     --Now the more costly actions
  223.     if limit <= 0 then return displacement,rot,exploreLimit end
  224.     turt.turnRight()
  225.     rot = (rot+1)%4
  226.     local success,data = turtle.inspect()
  227.     if success then
  228.         local priority = getOrePriority(data.name,data.tags)
  229.         if priority then
  230.             turt.dig()
  231.             turt.forward()
  232.             limit = limit-priority
  233.             displacement = add(displacement,displacementToVec("forward",rot))
  234.             displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
  235.             turt.back()
  236.             displacement = add(displacement,displacementToVec("back",rot))
  237.             canExplore = false
  238.         end
  239.     end
  240.     turt.turnLeft()
  241.     rot = (rot+3)%4
  242.     if limit <= 0 then return displacement,rot,exploreLimit end
  243.     turt.turnLeft()
  244.     rot = (rot+3)%4
  245.     local success,data = turtle.inspect()
  246.     if success then
  247.         local priority = getOrePriority(data.name,data.tags)
  248.         if priority then
  249.             turt.dig()
  250.             turt.forward()
  251.             limit = limit-priority
  252.             displacement = add(displacement,displacementToVec("forward",rot))
  253.             displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
  254.             turt.back()
  255.             displacement = add(displacement,displacementToVec("back",rot))
  256.             canExplore = false
  257.         end
  258.     end
  259.     turt.turnRight()
  260.     rot = (rot+1)%4
  261.     if canExplore and exploreLimit > 0 then
  262.         turt.dig()
  263.         turt.forward()
  264.         displacement = add(displacement,displacementToVec("forward",rot))
  265.         displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit - 1)
  266.         turt.back()
  267.         displacement = add(displacement,displacementToVec("back",rot))
  268.     end
  269.     return displacement,rot,exploreLimit
  270. end
  271. --Recursively mines an ore vein
  272. --@OptionalArg number limit: A weight to how many ores the turtle can mine before quitting
  273. function veinMine(limit)
  274.     local displacement,rot = mineOres(limit)
  275.     stats.add("Ores Mined",1)
  276.     navToVec(displacement[1],displacement[2],displacement[3],rot)
  277. end
  278. --Digs a strip above and/or below the turtle
  279. --@Arg number length: How far the strip should be dug
  280. --@OptionalArg string mode: If the turtle should only dig a strip "down", only "up", or "both"
  281. function digStripBlw(length,mode)
  282.     mode = mode or "down"
  283.     if mode == "down" then
  284.         turt.digDown()
  285.     elseif mode == "up" then
  286.         turt.digUp()
  287.     else
  288.         turt.digUp()
  289.         turt.digDown()
  290.     end
  291.     for i=2,length do
  292.         turt.dig()
  293.         turt.forward()
  294.         if mode == "down" then
  295.             turt.digDown()
  296.         elseif mode == "up" then
  297.             turt.digUp()
  298.         else
  299.             turt.digDown()
  300.             turt.digUp()
  301.         end
  302.     end
  303.     if turt.countFreeSlots() <= 4 then
  304.         turt.storeItems()
  305.     end
  306. end
  307. --Digs the x y plane in front of it, starting in top left corner
  308. --@Arg number x: How wide the rectangle it digs should be
  309. --@Arg number y: How high the rectangle it digs should be
  310. function digDeprecatedXY(x,y)
  311.     turt.dig()
  312.     turt.forward()
  313.     turt.turnRight()
  314.     for i=2,x do
  315.         turt.dig()
  316.         turt.forward()
  317.     end
  318.     for i=2,y do
  319.         turt.turnLeft(2)
  320.         --turtle.turnLeft()
  321.         --turtle.turnLeft()
  322.         digStripBlw(x)
  323.         turt.down()
  324.     end
  325.     if y%2==1 then
  326.         turt.turnLeft(2)
  327.         --turtle.turnLeft()
  328.         --turtle.turnLeft()
  329.         for i=1,x-1 do
  330.             turt.forward()
  331.         end
  332.     end
  333.     turt.turnRight()
  334. end
  335. --Digs a rectangle in front of the turtle, starting from the corner specified
  336. --@Arg number w: How wide the rectangle should be
  337. --@Arg number h: How high the rectangle should be
  338. --@OptionalArg number corner: The corner it should start from, with 0 being top left, 1 being top right, etc
  339. --@Returns boolean success: Whether it succeeded or not
  340. function digXY(w,h,corner)
  341.     corner = corner or 0
  342.     corner = corner%4
  343.     turt.dig()
  344.     turt.forward()
  345.     if w <= 1 then
  346.         for i=2,h do
  347.             if corner <= 1 then
  348.                 turt.digDown()
  349.                 turt.down()
  350.             else
  351.                 turt.digUp()
  352.                 turt.up()
  353.             end
  354.         end
  355.         return true
  356.     end
  357.     if corner == 0 or corner == 3 then
  358.         turt.turnRight()
  359.         if h <= 1 then
  360.             for i=2,w do
  361.                 turt.dig()
  362.                 turt.forward()
  363.             end
  364.             turt.turnLeft()
  365.             return true
  366.         end
  367.     else
  368.         turt.turnLeft()
  369.         if h <=1 then
  370.             for i=2,w do
  371.                 turt.dig()
  372.                 turt.forward()
  373.             end
  374.             turt.turnRight()
  375.             return true
  376.         end
  377.     end
  378.     for i=2,h do
  379.         if i%2 == 1 then
  380.             if corner <= 1 and i > 1 then
  381.                 turt.digDown()
  382.                 turt.down()
  383.             elseif i > 1 then
  384.                 turt.digUp()
  385.                 turt.up()
  386.             end
  387.         else
  388.             if i == h then
  389.                 if corner <= 1 then
  390.                     turt.digDown()
  391.                     turt.down()
  392.                     if i ~= 2 then
  393.                         turt.turnRight(2)
  394.                     end
  395.                     digStripBlw(w,"up")
  396.                 else
  397.                     turt.digUp()
  398.                     turt.up()
  399.                     if i ~= 2 then
  400.                         turt.turnRight(2)
  401.                     end
  402.                     digStripBlw(w,"down")
  403.                 end
  404.             elseif corner <=1 then
  405.                 turt.digDown()
  406.                 turt.down()
  407.                 if i ~=2 then
  408.                     turt.turnLeft(2)
  409.                 end
  410.                 digStripBlw(w,"both")
  411.             else
  412.                 turt.digUp()
  413.                 turt.up()
  414.                 if i ~= 2 then
  415.                     turt.turnLeft(2)
  416.                 end
  417.                 digStripBlw(w,"both")
  418.             end
  419.         end
  420.     end
  421.     local a = math.floor(h/2)%2
  422.     local b = math.floor((corner+1)/2)%2
  423.     local c = (a+b)%2
  424.     if c == 0 then
  425.         turt.turnRight()
  426.     else
  427.         turt.turnLeft()
  428.     end
  429. end
  430. --Wrapper for a wrapper for a fuel function :D
  431. --Refuels the turtle, printing more information
  432. function fuel()
  433.     term.setTextColor(colors.green)
  434.     term.clearLine()
  435.     print(refuel.refuel())
  436.     term.setTextColor(colors.white)
  437. end
  438. --New tunnel, efficient
  439. --Mines starting from the top left corner
  440. --@OptionalArg number w: How wide the tunnel should be
  441. --@OptionalArg number h: How high the tunnel should be
  442. --@OptionalArg number d: How deep the tunnel should extend
  443. --@OptionalArg boolean shouldFuel: If the turtle should attempt to refuel
  444. function mineTunnel(w,h,d,shouldFuel)
  445.     term.setCursorPos(1,1)
  446.     term.clear()
  447.     width = tonumber(width) or 3
  448.     height = tonumber(height) or 3
  449.     depth = tonumber(depth) or 30
  450.     term.setTextColor(colors.green)
  451.     print("Mining",w.."x"..h,"tunnel")
  452.     term.setTextColor(colors.white)
  453.     local a = math.floor(h/2)%2
  454.     for i=1,d do
  455.         term.clear()
  456.         term.setCursorPos(1,2)
  457.         print("Tunnel [" .. i.. "/" .. d .. "]")
  458.         if i%2==1 then
  459.             digXY(w,h,0)
  460.         else
  461.             if a==0 then
  462.                 digXY(w,h,3)
  463.             else
  464.                 digXY(w,h,2)
  465.             end
  466.         end
  467.         if shouldFuel then fuel() end
  468.     end
  469. end
  470. --Mines a staircase, good for setting up mining operations
  471. --TODO: Not finished
  472. --@Arg number w: The width of the staircase
  473. --@Arg number h: How high the staircase should be
  474. --@OptionalArg number count: How many stairs should be dug
  475. function mineStairs(w,h,count)
  476.     term.setCursorPos(1,1)
  477.     term.clear()
  478.     if not w or not h then return end
  479.     if h <= 1 then return end
  480.     count = count or 10
  481.     term.setTextColor(colors.green)
  482.     print("Mining",w.."x"..h,"stairs")
  483.     term.setTextColor(colors.white)
  484. end
  485.  
  486. --Old stairs
  487. --Mines a staircase with specified dimensions
  488. --@Arg number stairWidth: How many blocks wide the stairs are
  489. --@Arg number stairHeight: How many blocks tall the stairs are
  490. --@OptionalArg number steps: How many stairs should be created
  491. function mineDeprecatedStairs(stairWidth,stairHeight,steps)
  492.     term.setCursorPos(1,1)
  493.     term.clear()
  494.     if not stairWidth or not stairHeight then return end--or not steps then return end
  495.     if stairHeight <= 2 then return end
  496.     steps = steps or 10
  497.     term.setTextColor(colors.green)
  498.     print("Running Stairs with height",stairHeight,"and width",stairWidth)
  499.     term.setTextColor(colors.white)
  500.     local step = 1
  501.     while step <= steps do
  502.         term.setCursorPos(1,2)
  503.         print("Stairs ["..step.."/"..steps.."]")
  504.         turt.fuel()
  505.         digXY(stairWidth,stairHeight)
  506.         for i=1,stairHeight-2 do
  507.             turt.up()
  508.         end
  509.         step = step+1
  510.     end
  511. end
  512. --An inefficient tunnel digger, but otherwise the same as mineTunnel()
  513. --@OptionalArg number width: How wide the tunnel should be
  514. --@OptionalArg number height: How high the tunnel should be
  515. --@OptionalArg number depth: How deep the tunnel should be
  516. function mineDeprecatedTunnel(width,height,depth)
  517.     term.setCursorPos(1,1)
  518.     term.clear()
  519.     width = width or 3
  520.     height = height or 3
  521.     depth = depth or 30
  522.     term.setTextColor(colors.green)
  523.     print("Mining",width.."x"..height,"tunnel")
  524.     term.setTextColor(colors.white)
  525.     for i=1,depth do
  526.         term.setCursorPos(1,2)
  527.         print("Tunnel [" .. i.. "/" .. depth .. "]")
  528.         turt.fuel()
  529.         digXY(width,height)
  530.         for i=1,height-1 do
  531.             turt.up()
  532.         end
  533.     end
  534. end
  535. --Creates a 1x2 high mining tunnel, with the turtle moving in the bottom position, while searching for ore veins
  536. --@Arg number depth: How deep the tunnel should extend
  537. function createMiningTunnel(depth)
  538.     for i=1,depth do
  539.         if detectOres() then
  540.             veinMine(100)
  541.         end
  542.         turt.dig()
  543.         turt.forward()
  544.         turt.digUp()
  545.     end
  546. end
  547. --Creates a strip mine
  548. --@OptionalArg number depth: How deep the tunnels should go from the main tunnel
  549. --@OptionalArg number spacing: How far apart the tunnels should be from each other (gap in between = spacing-1)
  550. --@OptionalArg number count: How many mining tunnels should it makes
  551. --@OptionalArg boolean goRight: Whether the turtle should move right along the tunnel, or left
  552. function createMiningTunnels(depth,spacing,count,goRight)
  553.     depth = depth or 45
  554.     count = count or 10
  555.     if type(goRight) ~= "boolean" then goRight=false end
  556.     spacing = spacing or 3
  557.     if count%2 == 1 then count = count+1 end
  558.     for i=1,count do
  559.         if i%2 == 0 then
  560.             if goRight then turt.turnRight() else turt.turnLeft() end
  561.             createMiningTunnel(spacing)
  562.             if goRight then turt.turnRight() else turt.turnLeft() end
  563.         elseif i > 1 then
  564.             if goRight then turt.turnLeft() else turt.turnRight() end
  565.             createMiningTunnel(spacing)
  566.             turt.storeItems()
  567.             if goRight then turt.turnLeft() else turt.turnRight() end
  568.         end
  569.         createMiningTunnel(depth)
  570.     end
  571. end
Advertisement
Add Comment
Please, Sign In to add comment