Advertisement
Ltven0mI

minevein.lua

Apr 7th, 2023 (edited)
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.06 KB | None | 0 0
  1. -- Minevein
  2.  
  3. os.loadAPI("vector.lua")
  4.  
  5. function getBlockName()
  6.   local success, data = turtle.inspect()
  7.   return success and data.name or nil
  8. end
  9.  
  10. local target = getBlockName()
  11. if target == nil then return end
  12.  
  13. print("Target: " .. target)
  14.  
  15. local DIRECTIONS = {
  16.   [0] = {label="north", vector=vector.new(0, 0, 1)},
  17.   [1] = {label="east", vector=vector.new(1, 0, 0)},
  18.   [2] = {label="south", vector=vector.new(0, 0, -1)},
  19.   [3] = {label="west", vector=vector.new(-1, 0, 0)}
  20. }
  21.  
  22. local startPos = vector.new(0, 0, 0)
  23. local pos = vector.new(0, 0, 0)
  24. local facing = 0
  25.  
  26. function getFacingVector()
  27.   local direction = DIRECTIONS[facing]
  28.   if direction == nil then
  29.     error("Unknown Direction ("..facing..")")
  30.   end
  31.   return direction.vector
  32. end
  33.  
  34. function forward(times)
  35.   times = times or 1
  36.   if times < 1 then return true, 0 end
  37.  
  38.   local facingDir = getFacingVector()
  39.  
  40.   for i=1, times do
  41.     local success = turtle.forward()
  42.     if not success then return false, i-1 end
  43.     pos = pos + facingDir
  44.   end
  45.   return true, times
  46. end
  47.  
  48. function up(times)
  49.   times = times or 1
  50.   if times < 1 then return true, 0 end
  51.  
  52.   for i=1, times do
  53.     local success = turtle.up()
  54.     if not success then return false, i-1 end
  55.     pos = pos + vector.new(0, 1, 0)
  56.   end
  57.   return true, times
  58. end
  59.  
  60. function down(times)
  61.   times = times or 1
  62.   if times < 1 then return true, 0 end
  63.  
  64.   for i=1, times do
  65.     local success = turtle.down()
  66.     if not success then return false, i-1 end
  67.     pos = pos + vector.new(0, -1, 0)
  68.   end
  69.   return true, times
  70. end
  71.  
  72. function turnLeft(times)
  73.   times = times or 1
  74.   if times < 1 then return end
  75.   for i=1, times do
  76.     turtle.turnLeft()
  77.     -- print("facing BEFORE TL: "..facing)
  78.     facing = (facing - 1) % 4
  79.     -- print("facing AFTER TL: "..facing)
  80.   end
  81. end
  82.  
  83. function turnRight(times)
  84.   times = times or 1
  85.   if times < 1 then return end
  86.   for i=1, times do
  87.     turtle.turnRight()
  88.     -- print("facing BEFORE TR: "..facing)
  89.     facing = (facing + 1) % 4
  90.     -- print("facing AFTER TR: "..facing)
  91.   end
  92. end
  93.  
  94. function turnToDirectionIndex(index)
  95.   local rightTurns = (index - facing) % 4
  96.   local leftTurns = (facing - index) % 4
  97.   if rightTurns < leftTurns then
  98.     turnRight(rightTurns)
  99.   else
  100.     turnLeft(leftTurns)
  101.   end
  102. end
  103.  
  104. function turnToDirectionVector(dir)
  105.   if math.abs(dir.x) > math.abs(dir.z) then
  106.     -- East or West
  107.     if dir.x < 0 then
  108.       turnToDirectionIndex(3)
  109.     else
  110.       turnToDirectionIndex(1)
  111.     end
  112.   else
  113.     -- North or South
  114.     if dir.z < 0 then
  115.       turnToDirectionIndex(2)
  116.     else
  117.       turnToDirectionIndex(0)
  118.     end
  119.   end
  120. end
  121.  
  122. print("Facing: "..facing..", "..getFacingVector():tostring())
  123.  
  124. local firstBlock = pos + getFacingVector()
  125. print("First Block: "..tostring(firstBlock))
  126.  
  127. local matchedBlocks = {firstBlock}
  128.  
  129. function getNextBlock()
  130.   local bestDistance = math.huge
  131.   local bestIndex = -1
  132.   local best = nil
  133.   for i, v in ipairs(matchedBlocks) do
  134.     -- print(i, v)
  135.     local distance = (
  136.       math.abs(pos.x - v.x) +
  137.       math.abs(pos.y - v.y) +
  138.       math.abs(pos.z - v.z)
  139.     )
  140.     -- print("Distance: "..distance)
  141.  
  142.     if distance < bestDistance then
  143.       bestDistance = distance
  144.       bestIndex = i
  145.       best = v
  146.       -- print("Was Better")
  147.     end
  148.   end
  149.  
  150.   -- print("Best "..tostring(best)..", Index: "..bestIndex..", Distance: "..bestDistance)
  151.   return best, bestIndex, bestDistance
  152. end
  153.  
  154. function mineTo(targetPos, onMove)
  155.   local delta = targetPos - pos
  156.   local xMoves, yMoves, zMoves = delta.x, delta.y, delta.z
  157.   print("Moves:", delta)
  158.  
  159.   -- Mine in X Direction
  160.   if math.abs(xMoves) > 0 then
  161.     for x=1, math.abs(xMoves) do
  162.       turnToDirectionVector(vector.new(xMoves, 0, 0))
  163.       local blockMined = nil
  164.       while not forward() do
  165.         local success, data = turtle.inspect()
  166.         if success and blockMined == nil then blockMined = data end
  167.         turtle.dig()
  168.       end
  169.       if onMove then onMove(blockMined) end
  170.     end
  171.   end
  172.  
  173.   -- Mine in Y Direction
  174.   if math.abs(yMoves) > 0 then
  175.     local dig = yMoves < 0 and turtle.digDown or turtle.digUp
  176.     local move = yMoves < 0 and down or up
  177.     local inspect = yMoves < 0 and turtle.inspectDown or turtle.inspectUp
  178.  
  179.     for y=1, math.abs(yMoves) do
  180.       local blockMined = nil
  181.       while not move() do
  182.         local success, data = inspect()
  183.         if success and blockMined == nil then blockMined = data end
  184.         dig()
  185.       end
  186.       if onMove then onMove(blockMined) end
  187.     end
  188.   end
  189.  
  190.   -- Mine in Z Direction
  191.   if math.abs(zMoves) > 0 then
  192.     for z=1, math.abs(zMoves) do
  193.       turnToDirectionVector(vector.new(0, 0, zMoves))
  194.       local blockMined = nil
  195.       while not forward() do
  196.         local success, data = turtle.inspect()
  197.         if success and blockMined == nil then blockMined = data end
  198.         turtle.dig()
  199.       end
  200.       if onMove then onMove(blockMined) end
  201.     end
  202.   end
  203. end
  204.  
  205. function scanForTarget()
  206.   local detectedBlocks = {}
  207.  
  208.   -- Inspect Forward
  209.   local success, data = turtle.inspect()
  210.   if success and data.name == target then
  211.     table.insert(detectedBlocks, pos + getFacingVector())
  212.   end
  213.  
  214.   -- Inspect Right
  215.   turnRight()
  216.   success, data = turtle.inspect()
  217.   if success and data.name == target then
  218.     table.insert(detectedBlocks, pos + getFacingVector())
  219.   end
  220.  
  221.   -- Inspect Back
  222.   turnRight()
  223.   local success, data = turtle.inspect()
  224.   if success and data.name == target then
  225.     table.insert(detectedBlocks, pos + getFacingVector())
  226.   end
  227.  
  228.   -- Inspect Left
  229.   turnRight()
  230.   success, data = turtle.inspect()
  231.   if success and data.name == target then
  232.     table.insert(detectedBlocks, pos + getFacingVector())
  233.   end
  234.  
  235.   -- Inspect Up
  236.   success, data = turtle.inspectUp()
  237.   if success and data.name == target then
  238.     table.insert(detectedBlocks, pos + vector.new(0, 1, 0))
  239.   end
  240.  
  241.   -- Inspect Down
  242.   success, data = turtle.inspectDown()
  243.   if success and data.name == target then
  244.     table.insert(detectedBlocks, pos + vector.new(0, -1, 0))
  245.   end
  246.  
  247.   return detectedBlocks
  248. end
  249.  
  250. function handleOnMove(blockMined)
  251.   if blockMined == nil then return end
  252.   print(target, blockMined, target == blockMined)
  253.   if blockMined.name ~= target then return end
  254.   print("Moved and Mined:", blockMined.name)
  255.   print("Start Scanning...")
  256.   local detectedBlocks = scanForTarget()
  257.   print("- Scan detected "..#detectedBlocks.." Blocks matching the target.")
  258.  
  259.   for _, newBlock in ipairs(detectedBlocks) do
  260.     local exists = false
  261.     for _, oldBlock in ipairs(matchedBlocks) do
  262.       if newBlock == oldBlock then
  263.         exists = true
  264.         break
  265.       end
  266.     end
  267.     if not exists then
  268.       table.insert(matchedBlocks, newBlock)
  269.     end
  270.   end
  271. end
  272.  
  273. while #matchedBlocks > 0 do
  274.   local next, nextIndex = getNextBlock()
  275.   table.remove(matchedBlocks, nextIndex)
  276.   print(next, nextIndex)
  277.   mineTo(next, handleOnMove)
  278. end
  279.  
  280. -- turnToDirectionIndex(3) -- Will turn left once
  281. -- turnToDirectionIndex(0) -- Will turn right once
  282.  
  283. -- turnToDirectionVector(vector.new(0, 0, 0))
  284.  
  285. print("Finished")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement