Advertisement
jaklsfjlsak

激光测距挖矿

Aug 13th, 2023 (edited)
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  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. -- Loop to wait for key events
  26. while true do
  27.   -- Wait for a key event
  28.   local event, key = os.pullEvent("key")
  29.  
  30.   -- Check if the "M" key was pressed (key code 50)
  31.   if key == 50 then
  32.     -- Get the laser's own position
  33.     local _, laserY, _ = laser.getLocalPosition()
  34.  
  35.     -- Emit a laser scan in the Y- direction (0, -1, 0)
  36.     laser.emitBeam(0, -1, 0)
  37.  
  38.     -- Get the scan result
  39.     local _, _, targetY = laser.getScanResult()
  40.  
  41.     -- Calculate the layerOffset
  42.     local mineTarget = laserY - targetY - 1
  43.  
  44.     -- Print the target
  45.     print("Target is: " .. mineTarget .. " blocks below")
  46.  
  47.     -- Configure the mining lasers to use the mineTarget as the layerOffset
  48.     for _, mininglaser in pairs(mininglasers) do
  49.       mininglaser.offset(mineTarget)
  50.       mininglaser.enable(true)
  51.     end
  52.   end
  53. end
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement