Advertisement
jaklsfjlsak

激光测距挖矿v3 自动停止

Aug 13th, 2023 (edited)
1,216
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.  
  5. for _, side in pairs(sides) do
  6.   if peripheral.getType(side) == "warpdriveMiningLaser" then
  7.     table.insert(mininglasers, peripheral.wrap(side))
  8.   end
  9. end
  10.  
  11. laser.beamFrequency(1420)
  12.  
  13. if not laser then
  14.   print("No warpdriveLaser detected")
  15.   os.exit()
  16. end
  17.  
  18. if #mininglasers == 0 then
  19.   print("No warpdriveMiningLaser detected")
  20.   os.exit()
  21. end
  22.  
  23. print("Press the 'M' key to emit a laser scan and start the mining lasers with calculated layer offset.")
  24.  
  25. local zeroBlockCount = 0
  26.  
  27. -- Loop to wait for key events
  28. while true do
  29.   -- Wait for a key event
  30.   local event, key = os.pullEvent("key")
  31.  
  32.   -- Check if the "M" key was pressed (key code 50)
  33.   if key == 50 then
  34.     -- Get the laser's own position
  35.     local _, laserY, _ = laser.getLocalPosition()
  36.  
  37.     -- Emit a laser scan in the Y- direction (0, -1, 0)
  38.     laser.emitBeam(0, -1, 0)
  39.  
  40.     -- Get the scan result
  41.     local _, _, targetY = laser.getScanResult()
  42.  
  43.     -- Calculate the layerOffset
  44.     local mineTarget = laserY - targetY - 1
  45.  
  46.     -- Print the target
  47.     print("Target is: " .. mineTarget .. " blocks below")
  48.  
  49.     -- Configure the mining lasers to use the mineTarget as the layerOffset
  50.     for _, mininglaser in pairs(mininglasers) do
  51.       mininglaser.offset(mineTarget)
  52.       mininglaser.enable(true)
  53.     end
  54.  
  55.     -- Monitor the total blocks and stop if necessary
  56.     while true do
  57.       local total = 0
  58.       for _, mininglaser in pairs(mininglasers) do
  59.         local _, _, _, _, _, laserTotal = mininglaser.state()
  60.         total = total + laserTotal
  61.       end
  62.      
  63.       if total == 0 then
  64.         zeroBlockCount = zeroBlockCount + 1
  65.         if zeroBlockCount >= 3 then
  66.           print("3 consecutive layers with 0 total blocks detected. Stopping mining.")
  67.           shell.run("stop") -- Call the stop script to stop mining
  68.           break -- Exit the loop
  69.         end
  70.       else
  71.         zeroBlockCount = 0
  72.       end
  73.  
  74.       os.sleep(1) -- Delay to give mining lasers time to process
  75.     end
  76.   end
  77. end
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement