Advertisement
jaklsfjlsak

激光测距挖矿v2 自动停止

Aug 13th, 2023 (edited)
1,480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local laser = peripheral.find("warpdriveLaser")
  2. local mininglasers = {}
  3. local sides = peripheral.getNames()
  4. local zeroBlockCount = 0
  5.  
  6. for _, side in pairs(sides) do
  7.   if peripheral.getType(side) == "warpdriveMiningLaser" then
  8.     table.insert(mininglasers, peripheral.wrap(side))
  9.   end
  10. end
  11.  
  12. laser.beamFrequency(1420)
  13.  
  14. if not laser then
  15.   print("No warpdriveLaser detected")
  16.   os.exit()
  17. end
  18.  
  19. if #mininglasers == 0 then
  20.   print("No warpdriveMiningLaser detected")
  21.   os.exit()
  22. end
  23.  
  24. print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
  25.  
  26. -- Loop to wait for key events
  27. while true do
  28.   -- Wait for a key event
  29.   local event, key = os.pullEvent("key")
  30.  
  31.   -- Check if the "M" key was pressed (key code 50)
  32.   if key == 50 then
  33.     -- Get the laser's own position
  34.     local _, laserY, _ = laser.getLocalPosition()
  35.  
  36.     -- Emit a laser scan in the Y- direction (0, -1, 0)
  37.     laser.emitBeam(0, -1, 0)
  38.  
  39.     -- Get the scan result
  40.     local _, _, targetY = laser.getScanResult()
  41.  
  42.     -- Calculate the layerOffset
  43.     local mineTarget = laserY - targetY - 1
  44.  
  45.     -- Print the target
  46.     print("Target is: " .. mineTarget .. " blocks below")
  47.  
  48.     -- Configure the mining lasers to use the mineTarget as the layerOffset
  49.     for _, mininglaser in pairs(mininglasers) do
  50.       mininglaser.offset(mineTarget)
  51.       mininglaser.enable(true)
  52.     end
  53.  
  54.     repeat
  55.       local areActive = false
  56.       for _, mininglaser in pairs(mininglasers) do
  57.         local _, isActive, _, _, _, total = mininglaser.state()
  58.  
  59.         if total == 0 then
  60.           zeroBlockCount = zeroBlockCount + 1
  61.         else
  62.           zeroBlockCount = 0
  63.         end
  64.  
  65.         if zeroBlockCount >= 3 then
  66.           print("3 consecutive layers with 0 total blocks detected. Stopping mining.")
  67.           for _, mininglaser in pairs(mininglasers) do
  68.                shell.run("stop")
  69.           end
  70.           break
  71.         end
  72.  
  73.         if isActive then
  74.           areActive = true
  75.         end
  76.       end
  77.       os.sleep(1)
  78.     until not areActive or zeroBlockCount >= 3
  79.  
  80.     -- Reset zeroBlockCount for the next mining session
  81.     zeroBlockCount = 0
  82.   end
  83. end
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement