Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not refuel then os.loadAPI("apis/refuel.lua") end
- if not stats then os.loadAPI("apis/stats.lua") end
- if not turt then os.loadAPI("apis/turt.lua") end
- if not docs then os.loadAPI("apis/docs.lua") end
- --Type registry
- local types = {
- ["VectorND"]={colors.lightBlue,"A vector with an arbitrary number of entries"},
- ["Vector2D"]={colors.lightBlue,"A vector with 2 entries"},
- ["Vector3D"]={colors.lightBlue,"A vector with 3 entries"}
- }
- for k,v in pairs(types) do
- docs.registerType(k,v[1],v[2])
- end
- --Adds two vectors together
- --@Arg VectorND v1: The first vector to be added
- --@Arg VectorND v2: the second vector to be added
- --@Returns VectorND v: The sum of the two vectors
- --@Returns boolean false: Returns false if the two vectors are different lengths or if the vectors arent numeric
- function add(v1,v2)
- if type(v1) ~= "table" or type(v2) ~= "table" then return false end
- if #v1 ~= #v2 then return false end
- local t = {}
- for k,v in pairs(v1) do
- if not tonumber(v2[k]) or not tonumber(v) then return false end
- t[k] = tonumber(v)+tonumber(v2[k])
- end
- return t
- end
- --Multiplies a vector by a constant
- --@Arg VectorND v1: The vector to be scaled
- --@Arg number c: The constant to scale the vector by
- --@Returns VectorND v: The scaled vector
- --@Returns boolean false: If the vector had non-numeric entries
- function multiply(v1,c)
- c = tonumber(c)
- if type(v1) ~= "table" or not c then return false end
- local t = {}
- for k,v in pairs(v1) do
- if not tonumber(v) then return false end
- t[k] = tonumber(v)*c
- end
- return t
- end
- --Moves a turtle to a position
- --@Arg number x: The x position the turtle should move to (x is right/left in the original, rot=0 reference frame)
- --@Arg number y: The y position the turtle should move to (y is up/down)
- --@Arg number z: the z position the turtle should move to (z is forward/back in the original reference frame)
- --@OptionalArg number rot: The turtle's horizontal rotation from the original reference frame, measured in number of right turns
- --@Returns boolean success: Whether the turtle successfully moved to its target position
- function navToVec(x,y,z,rot)
- if not x or not y or not z then return false end
- rot = (tonumber(rot) or 0)%4
- for i=1,rot do
- turt.turnLeft()
- end
- while y ~= 0 do
- if y > 0 then
- turt.digDown()
- turt.down()
- y = y-1
- else
- turt.digUp()
- turt.up()
- y = y+1
- end
- end
- local flag = false
- if z < 0 then
- turt.turnLeft(2)
- flag = true
- end
- while z ~= 0 do
- turt.dig()
- turt.forward()
- if z < 0 then z = z -1 else z = z+1 end
- end
- if flag then turt.turnLeft(2) end
- flag = 0
- if x > 0 then
- turt.turnRight()
- flag = 1
- elseif x < 0 then
- turt.turnLeft()
- flag = 2
- end
- while x ~= 0 do
- turt.dig()
- turt.forward()
- if x > 0 then x = x-1 else x= x+1 end
- end
- if flag == 1 then turt.turnLeft()
- elseif flag == 2 then turt.turnRight() end
- return true
- end
- --Reads Blockdata tags (from turtle.inspect) to get its ore tag (more specific ore tags preferred)
- --@Arg table tags: A list of all of the block tags
- --@Returns string tag: The block's longest ore tag in the format "forge:ores/*"
- --@Returns boolean false: If no ore tag was found
- function getOreTag(tags)
- if not tags then return false end
- local tag = nil
- for k,v in pairs(tags) do
- local tempTag = string.match(k,"forge:ores/?%w*")
- if not tag and tempTag then tag = tempTag end
- if tempTag and #tempTag > #tag then tag = tempTag end
- end
- return tag or false
- end
- --Gets the priority value of an ore
- --@Arg string ore: The name of the ore, eg "minecraft:diamond_ore"
- --@OptionalArg table tags: The block tags of the block to inspect
- --@Returns number priority: The priority value of the ore, with lower numbers being prioritized
- --@Returns boolean false: If the block had no ore priority defined (likely not an ore)
- function getOrePriority(ore,tags)
- if fs.exists("ore_priority.json") then
- local file = fs.open("ore_priority.json","r")
- local data = textutils.unserialiseJSON(file.readAll())
- file.close()
- if data and data[ore] then return data[ore] end
- local tag = getOreTag(tags)
- if data and tag and data[tag] then return data[tag] end
- end
- if getOreTag(tags) then return 5 end
- return false
- end
- --Basic, cheap ore detection to know whether or not to trigger a vein mine
- --@Returns boolean foundOre: If there was an ore found in front of, above, or below the turtle (takes no moves to check these)
- function detectOres()
- local s,d = turtle.inspect()
- if d and d.tags and d.tags["forge:ores"] then return true end
- local s,d = turtle.inspectUp()
- if d and d.tags and d.tags["forge:ores"] then return true end
- local s,d = turtle.inspectDown()
- if d and d.tags and d.tags["forge:ores"] then return true end
- return false
- end
- --Direction is local rotation, rot is absolute rotation
- --@Arg string direction: ["up","down","left","right","forward","back"], the local rotation of the turtle
- --@OptionalArg number rot: The absolute rotation of the reference frame
- --@Returns Vector3D v: The absolute displacement of the turtle if it were to move in that direction, in the original reference frame
- function displacementToVec(direction,rot)
- if direction == "up" then return {0,1,0} end
- if direction == "down" then return {0,-1,0} end
- rot = tonumber(rot) or 0
- local directions = {
- ["forward"]= 0,
- ["right"]= 1,
- ["back"]= 2,
- ["left"]= 3
- }
- rot = (rot+directions[direction])%4
- return {math.floor(math.sin(rot*math.pi/2)+0.5),0,math.floor(math.cos(rot*math.pi/2)+0.5)}
- end
- -- ExploreLimit also affects how much each priority of ore will increase the explore limit
- -- At its default of 2, mining a diamond will increase the exploreLimit by 2,
- -- osmium will increase it by 1, iron will not affect the exploreLimit, and copper will actually decrease it by 2
- --Highly prioritized ores like diamonds or ancient debris will have the turtle explore more blocks to try to find the whole vein
- --@OptionalArg number limit: An upper limit on the number of ores the turtle can mine. Higher priority ores have a higher limit
- --@OptionalArg Vector3D displacement: The displacement of the turtle from its original position before mining ores
- --@OptionalArg number rot: The rotation of the turtle from its original reference frame
- --@OptionalArg number exploreLimit: The number of extra blocks the turtle can break to search for hidden ore pockets
- --@OptionalArg number exploreFactor: How much mining ores increases the exploreLimit
- --@Returns Vector3D displacement, number rot, number exploreLimit: The turtle's final displacement and rotation, as well as the exploreLimit
- function mineOres(limit,displacement,rot,exploreLimit,exploreFactor)
- limit = tonumber(limit) or 200
- exploreLimit = tonumber(exploreLimit) or 0
- exploreFactor = tonumber(exploreFactor) or 2
- displacement = displacement or {0,0,0}
- rot = rot or 0
- if limit <= 0 then return displacement,rot end
- local canExplore = true
- local success,data = turtle.inspect()
- --Foward highest priority, since least costly
- if success then
- local priority = getOrePriority(data.name,data.tags)
- if priority then
- turt.dig()
- turt.forward()
- limit = limit - priority
- displacement = add(displacement,displacementToVec("forward",rot))
- displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+exploreFactor-priority,exploreFactor)
- turt.back()
- displacement = add(displacement,displacementToVec("back",rot))
- canExplore = false --Higher priority ores, like diamonds, will expand exploreLimit
- end
- end
- --Up also not costly
- if limit <= 0 then return displacement,rot,exploreLimit end
- local success,data = turtle.inspectUp()
- if success then
- local priority = getOrePriority(data.name,data.tags)
- if priority then
- turt.digUp()
- turt.up()
- limit = limit-priority
- displacement = add(displacement,displacementToVec("up",rot))
- displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
- turt.down()
- displacement = add(displacement,displacementToVec("down",rot))
- canExplore = false
- end
- end
- --Down also cheap since turtles can mine up and down
- if limit <= 0 then return displacement,rot,exploreLimit end
- local success,data = turtle.inspectDown()
- if success then
- local priority = getOrePriority(data.name,data.tags)
- if priority then
- turt.digDown()
- turt.down()
- limit = limit-priority
- displacement = add(displacement,displacementToVec("down",rot))
- displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
- turt.up()
- displacement = add(displacement,displacementToVec("up",rot))
- canExplore = false
- end
- end
- --Now the more costly actions
- if limit <= 0 then return displacement,rot,exploreLimit end
- turt.turnRight()
- rot = (rot+1)%4
- local success,data = turtle.inspect()
- if success then
- local priority = getOrePriority(data.name,data.tags)
- if priority then
- turt.dig()
- turt.forward()
- limit = limit-priority
- displacement = add(displacement,displacementToVec("forward",rot))
- displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
- turt.back()
- displacement = add(displacement,displacementToVec("back",rot))
- canExplore = false
- end
- end
- turt.turnLeft()
- rot = (rot+3)%4
- if limit <= 0 then return displacement,rot,exploreLimit end
- turt.turnLeft()
- rot = (rot+3)%4
- local success,data = turtle.inspect()
- if success then
- local priority = getOrePriority(data.name,data.tags)
- if priority then
- turt.dig()
- turt.forward()
- limit = limit-priority
- displacement = add(displacement,displacementToVec("forward",rot))
- displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit+2-priority)
- turt.back()
- displacement = add(displacement,displacementToVec("back",rot))
- canExplore = false
- end
- end
- turt.turnRight()
- rot = (rot+1)%4
- if canExplore and exploreLimit > 0 then
- turt.dig()
- turt.forward()
- displacement = add(displacement,displacementToVec("forward",rot))
- displacement,rot,exploreLimit = mineOres(limit,displacement,rot,exploreLimit - 1)
- turt.back()
- displacement = add(displacement,displacementToVec("back",rot))
- end
- return displacement,rot,exploreLimit
- end
- --Recursively mines an ore vein
- --@OptionalArg number limit: A weight to how many ores the turtle can mine before quitting
- function veinMine(limit)
- local displacement,rot = mineOres(limit)
- stats.add("Ores Mined",1)
- navToVec(displacement[1],displacement[2],displacement[3],rot)
- end
- --Digs a strip above and/or below the turtle
- --@Arg number length: How far the strip should be dug
- --@OptionalArg string mode: If the turtle should only dig a strip "down", only "up", or "both"
- function digStripBlw(length,mode)
- mode = mode or "down"
- if mode == "down" then
- turt.digDown()
- elseif mode == "up" then
- turt.digUp()
- else
- turt.digUp()
- turt.digDown()
- end
- for i=2,length do
- turt.dig()
- turt.forward()
- if mode == "down" then
- turt.digDown()
- elseif mode == "up" then
- turt.digUp()
- else
- turt.digDown()
- turt.digUp()
- end
- end
- if turt.countFreeSlots() <= 4 then
- turt.storeItems()
- end
- end
- --Digs the x y plane in front of it, starting in top left corner
- --@Arg number x: How wide the rectangle it digs should be
- --@Arg number y: How high the rectangle it digs should be
- function digDeprecatedXY(x,y)
- turt.dig()
- turt.forward()
- turt.turnRight()
- for i=2,x do
- turt.dig()
- turt.forward()
- end
- for i=2,y do
- turt.turnLeft(2)
- --turtle.turnLeft()
- --turtle.turnLeft()
- digStripBlw(x)
- turt.down()
- end
- if y%2==1 then
- turt.turnLeft(2)
- --turtle.turnLeft()
- --turtle.turnLeft()
- for i=1,x-1 do
- turt.forward()
- end
- end
- turt.turnRight()
- end
- --Digs a rectangle in front of the turtle, starting from the corner specified
- --@Arg number w: How wide the rectangle should be
- --@Arg number h: How high the rectangle should be
- --@OptionalArg number corner: The corner it should start from, with 0 being top left, 1 being top right, etc
- --@Returns boolean success: Whether it succeeded or not
- function digXY(w,h,corner)
- corner = corner or 0
- corner = corner%4
- turt.dig()
- turt.forward()
- if w <= 1 then
- for i=2,h do
- if corner <= 1 then
- turt.digDown()
- turt.down()
- else
- turt.digUp()
- turt.up()
- end
- end
- return true
- end
- if corner == 0 or corner == 3 then
- turt.turnRight()
- if h <= 1 then
- for i=2,w do
- turt.dig()
- turt.forward()
- end
- turt.turnLeft()
- return true
- end
- else
- turt.turnLeft()
- if h <=1 then
- for i=2,w do
- turt.dig()
- turt.forward()
- end
- turt.turnRight()
- return true
- end
- end
- for i=2,h do
- if i%2 == 1 then
- if corner <= 1 and i > 1 then
- turt.digDown()
- turt.down()
- elseif i > 1 then
- turt.digUp()
- turt.up()
- end
- else
- if i == h then
- if corner <= 1 then
- turt.digDown()
- turt.down()
- if i ~= 2 then
- turt.turnRight(2)
- end
- digStripBlw(w,"up")
- else
- turt.digUp()
- turt.up()
- if i ~= 2 then
- turt.turnRight(2)
- end
- digStripBlw(w,"down")
- end
- elseif corner <=1 then
- turt.digDown()
- turt.down()
- if i ~=2 then
- turt.turnLeft(2)
- end
- digStripBlw(w,"both")
- else
- turt.digUp()
- turt.up()
- if i ~= 2 then
- turt.turnLeft(2)
- end
- digStripBlw(w,"both")
- end
- end
- end
- local a = math.floor(h/2)%2
- local b = math.floor((corner+1)/2)%2
- local c = (a+b)%2
- if c == 0 then
- turt.turnRight()
- else
- turt.turnLeft()
- end
- end
- --Wrapper for a wrapper for a fuel function :D
- --Refuels the turtle, printing more information
- function fuel()
- term.setTextColor(colors.green)
- term.clearLine()
- print(refuel.refuel())
- term.setTextColor(colors.white)
- end
- --New tunnel, efficient
- --Mines starting from the top left corner
- --@OptionalArg number w: How wide the tunnel should be
- --@OptionalArg number h: How high the tunnel should be
- --@OptionalArg number d: How deep the tunnel should extend
- --@OptionalArg boolean shouldFuel: If the turtle should attempt to refuel
- function mineTunnel(w,h,d,shouldFuel)
- term.setCursorPos(1,1)
- term.clear()
- width = tonumber(width) or 3
- height = tonumber(height) or 3
- depth = tonumber(depth) or 30
- term.setTextColor(colors.green)
- print("Mining",w.."x"..h,"tunnel")
- term.setTextColor(colors.white)
- local a = math.floor(h/2)%2
- for i=1,d do
- term.clear()
- term.setCursorPos(1,2)
- print("Tunnel [" .. i.. "/" .. d .. "]")
- if i%2==1 then
- digXY(w,h,0)
- else
- if a==0 then
- digXY(w,h,3)
- else
- digXY(w,h,2)
- end
- end
- if shouldFuel then fuel() end
- end
- end
- --Mines a staircase, good for setting up mining operations
- --TODO: Not finished
- --@Arg number w: The width of the staircase
- --@Arg number h: How high the staircase should be
- --@OptionalArg number count: How many stairs should be dug
- function mineStairs(w,h,count)
- term.setCursorPos(1,1)
- term.clear()
- if not w or not h then return end
- if h <= 1 then return end
- count = count or 10
- term.setTextColor(colors.green)
- print("Mining",w.."x"..h,"stairs")
- term.setTextColor(colors.white)
- end
- --Old stairs
- --Mines a staircase with specified dimensions
- --@Arg number stairWidth: How many blocks wide the stairs are
- --@Arg number stairHeight: How many blocks tall the stairs are
- --@OptionalArg number steps: How many stairs should be created
- function mineDeprecatedStairs(stairWidth,stairHeight,steps)
- term.setCursorPos(1,1)
- term.clear()
- if not stairWidth or not stairHeight then return end--or not steps then return end
- if stairHeight <= 2 then return end
- steps = steps or 10
- term.setTextColor(colors.green)
- print("Running Stairs with height",stairHeight,"and width",stairWidth)
- term.setTextColor(colors.white)
- local step = 1
- while step <= steps do
- term.setCursorPos(1,2)
- print("Stairs ["..step.."/"..steps.."]")
- turt.fuel()
- digXY(stairWidth,stairHeight)
- for i=1,stairHeight-2 do
- turt.up()
- end
- step = step+1
- end
- end
- --An inefficient tunnel digger, but otherwise the same as mineTunnel()
- --@OptionalArg number width: How wide the tunnel should be
- --@OptionalArg number height: How high the tunnel should be
- --@OptionalArg number depth: How deep the tunnel should be
- function mineDeprecatedTunnel(width,height,depth)
- term.setCursorPos(1,1)
- term.clear()
- width = width or 3
- height = height or 3
- depth = depth or 30
- term.setTextColor(colors.green)
- print("Mining",width.."x"..height,"tunnel")
- term.setTextColor(colors.white)
- for i=1,depth do
- term.setCursorPos(1,2)
- print("Tunnel [" .. i.. "/" .. depth .. "]")
- turt.fuel()
- digXY(width,height)
- for i=1,height-1 do
- turt.up()
- end
- end
- end
- --Creates a 1x2 high mining tunnel, with the turtle moving in the bottom position, while searching for ore veins
- --@Arg number depth: How deep the tunnel should extend
- function createMiningTunnel(depth)
- for i=1,depth do
- if detectOres() then
- veinMine(100)
- end
- turt.dig()
- turt.forward()
- turt.digUp()
- end
- end
- --Creates a strip mine
- --@OptionalArg number depth: How deep the tunnels should go from the main tunnel
- --@OptionalArg number spacing: How far apart the tunnels should be from each other (gap in between = spacing-1)
- --@OptionalArg number count: How many mining tunnels should it makes
- --@OptionalArg boolean goRight: Whether the turtle should move right along the tunnel, or left
- function createMiningTunnels(depth,spacing,count,goRight)
- depth = depth or 45
- count = count or 10
- if type(goRight) ~= "boolean" then goRight=false end
- spacing = spacing or 3
- if count%2 == 1 then count = count+1 end
- for i=1,count do
- if i%2 == 0 then
- if goRight then turt.turnRight() else turt.turnLeft() end
- createMiningTunnel(spacing)
- if goRight then turt.turnRight() else turt.turnLeft() end
- elseif i > 1 then
- if goRight then turt.turnLeft() else turt.turnRight() end
- createMiningTunnel(spacing)
- turt.storeItems()
- if goRight then turt.turnLeft() else turt.turnRight() end
- end
- createMiningTunnel(depth)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment