Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Minevein
- os.loadAPI("vector.lua")
- function getBlockName()
- local success, data = turtle.inspect()
- return success and data.name or nil
- end
- local target = getBlockName()
- if target == nil then return end
- print("Target: " .. target)
- local DIRECTIONS = {
- [0] = {label="north", vector=vector.new(0, 0, 1)},
- [1] = {label="east", vector=vector.new(1, 0, 0)},
- [2] = {label="south", vector=vector.new(0, 0, -1)},
- [3] = {label="west", vector=vector.new(-1, 0, 0)}
- }
- local startPos = vector.new(0, 0, 0)
- local pos = vector.new(0, 0, 0)
- local facing = 0
- function getFacingVector()
- local direction = DIRECTIONS[facing]
- if direction == nil then
- error("Unknown Direction ("..facing..")")
- end
- return direction.vector
- end
- function forward(times)
- times = times or 1
- if times < 1 then return true, 0 end
- local facingDir = getFacingVector()
- for i=1, times do
- local success = turtle.forward()
- if not success then return false, i-1 end
- pos = pos + facingDir
- end
- return true, times
- end
- function up(times)
- times = times or 1
- if times < 1 then return true, 0 end
- for i=1, times do
- local success = turtle.up()
- if not success then return false, i-1 end
- pos = pos + vector.new(0, 1, 0)
- end
- return true, times
- end
- function down(times)
- times = times or 1
- if times < 1 then return true, 0 end
- for i=1, times do
- local success = turtle.down()
- if not success then return false, i-1 end
- pos = pos + vector.new(0, -1, 0)
- end
- return true, times
- end
- function turnLeft(times)
- times = times or 1
- if times < 1 then return end
- for i=1, times do
- turtle.turnLeft()
- -- print("facing BEFORE TL: "..facing)
- facing = (facing - 1) % 4
- -- print("facing AFTER TL: "..facing)
- end
- end
- function turnRight(times)
- times = times or 1
- if times < 1 then return end
- for i=1, times do
- turtle.turnRight()
- -- print("facing BEFORE TR: "..facing)
- facing = (facing + 1) % 4
- -- print("facing AFTER TR: "..facing)
- end
- end
- function turnToDirectionIndex(index)
- local rightTurns = (index - facing) % 4
- local leftTurns = (facing - index) % 4
- if rightTurns < leftTurns then
- turnRight(rightTurns)
- else
- turnLeft(leftTurns)
- end
- end
- function turnToDirectionVector(dir)
- if math.abs(dir.x) > math.abs(dir.z) then
- -- East or West
- if dir.x < 0 then
- turnToDirectionIndex(3)
- else
- turnToDirectionIndex(1)
- end
- else
- -- North or South
- if dir.z < 0 then
- turnToDirectionIndex(2)
- else
- turnToDirectionIndex(0)
- end
- end
- end
- print("Facing: "..facing..", "..getFacingVector():tostring())
- local firstBlock = pos + getFacingVector()
- print("First Block: "..tostring(firstBlock))
- local matchedBlocks = {firstBlock}
- function getNextBlock()
- local bestDistance = math.huge
- local bestIndex = -1
- local best = nil
- for i, v in ipairs(matchedBlocks) do
- -- print(i, v)
- local distance = (
- math.abs(pos.x - v.x) +
- math.abs(pos.y - v.y) +
- math.abs(pos.z - v.z)
- )
- -- print("Distance: "..distance)
- if distance < bestDistance then
- bestDistance = distance
- bestIndex = i
- best = v
- -- print("Was Better")
- end
- end
- -- print("Best "..tostring(best)..", Index: "..bestIndex..", Distance: "..bestDistance)
- return best, bestIndex, bestDistance
- end
- function mineTo(targetPos, onMove)
- local delta = targetPos - pos
- local xMoves, yMoves, zMoves = delta.x, delta.y, delta.z
- print("Moves:", delta)
- -- Mine in X Direction
- if math.abs(xMoves) > 0 then
- for x=1, math.abs(xMoves) do
- turnToDirectionVector(vector.new(xMoves, 0, 0))
- local blockMined = nil
- while not forward() do
- local success, data = turtle.inspect()
- if success and blockMined == nil then blockMined = data end
- turtle.dig()
- end
- if onMove then onMove(blockMined) end
- end
- end
- -- Mine in Y Direction
- if math.abs(yMoves) > 0 then
- local dig = yMoves < 0 and turtle.digDown or turtle.digUp
- local move = yMoves < 0 and down or up
- local inspect = yMoves < 0 and turtle.inspectDown or turtle.inspectUp
- for y=1, math.abs(yMoves) do
- local blockMined = nil
- while not move() do
- local success, data = inspect()
- if success and blockMined == nil then blockMined = data end
- dig()
- end
- if onMove then onMove(blockMined) end
- end
- end
- -- Mine in Z Direction
- if math.abs(zMoves) > 0 then
- for z=1, math.abs(zMoves) do
- turnToDirectionVector(vector.new(0, 0, zMoves))
- local blockMined = nil
- while not forward() do
- local success, data = turtle.inspect()
- if success and blockMined == nil then blockMined = data end
- turtle.dig()
- end
- if onMove then onMove(blockMined) end
- end
- end
- end
- function scanForTarget()
- local detectedBlocks = {}
- -- Inspect Forward
- local success, data = turtle.inspect()
- if success and data.name == target then
- table.insert(detectedBlocks, pos + getFacingVector())
- end
- -- Inspect Right
- turnRight()
- success, data = turtle.inspect()
- if success and data.name == target then
- table.insert(detectedBlocks, pos + getFacingVector())
- end
- -- Inspect Back
- turnRight()
- local success, data = turtle.inspect()
- if success and data.name == target then
- table.insert(detectedBlocks, pos + getFacingVector())
- end
- -- Inspect Left
- turnRight()
- success, data = turtle.inspect()
- if success and data.name == target then
- table.insert(detectedBlocks, pos + getFacingVector())
- end
- -- Inspect Up
- success, data = turtle.inspectUp()
- if success and data.name == target then
- table.insert(detectedBlocks, pos + vector.new(0, 1, 0))
- end
- -- Inspect Down
- success, data = turtle.inspectDown()
- if success and data.name == target then
- table.insert(detectedBlocks, pos + vector.new(0, -1, 0))
- end
- return detectedBlocks
- end
- function handleOnMove(blockMined)
- if blockMined == nil then return end
- print(target, blockMined, target == blockMined)
- if blockMined.name ~= target then return end
- print("Moved and Mined:", blockMined.name)
- print("Start Scanning...")
- local detectedBlocks = scanForTarget()
- print("- Scan detected "..#detectedBlocks.." Blocks matching the target.")
- for _, newBlock in ipairs(detectedBlocks) do
- local exists = false
- for _, oldBlock in ipairs(matchedBlocks) do
- if newBlock == oldBlock then
- exists = true
- break
- end
- end
- if not exists then
- table.insert(matchedBlocks, newBlock)
- end
- end
- end
- while #matchedBlocks > 0 do
- local next, nextIndex = getNextBlock()
- table.remove(matchedBlocks, nextIndex)
- print(next, nextIndex)
- mineTo(next, handleOnMove)
- end
- -- turnToDirectionIndex(3) -- Will turn left once
- -- turnToDirectionIndex(0) -- Will turn right once
- -- turnToDirectionVector(vector.new(0, 0, 0))
- print("Finished")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement