Advertisement
jaklsfjlsak

SGP 测试

Oct 27th, 2023 (edited)
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. local freq = 34900
  2.  
  3. -- Get the list of all peripherals
  4. local peripherals = peripheral.getNames()
  5.  
  6. -- Filter and wrap all warpdriveLasers, setting their beam frequency
  7. local lasers = {}
  8. for _, name in ipairs(peripherals) do
  9.     if peripheral.getType(name) == "warpdriveLaser" then
  10.         local laser = peripheral.wrap(name)
  11.         laser.beamFrequency(freq)
  12.         table.insert(lasers, laser)
  13.     end
  14. end
  15.  
  16. -- Filter and wrap all warpdriveLaserCameras, setting their beam frequency
  17. local scanlasers = {}
  18. for _, name in ipairs(peripherals) do
  19.     if peripheral.getType(name) == "warpdriveLaserCamera" then
  20.         local scanlaser = peripheral.wrap(name)
  21.         scanlaser.beamFrequency(1420)
  22.         scanlasers[name] = scanlaser -- Use the name as the key for easy access
  23.     end
  24. end
  25.  
  26. -- Main event loop
  27. while true do
  28.     -- Wait for laserScanning event
  29.     local event, peripheralName, lx, ly, lz, block = os.pullEvent("laserScanning")
  30.     -- Check if it's from a known scanlaser
  31.     if event == "laserScanning" and scanlasers[peripheralName] then
  32.         print("Received laser scanning event from: "..peripheralName)
  33.         local lastScanLaser = scanlasers[peripheralName]
  34.         local scanResult = {lastScanLaser.getScanResult()}
  35.         lx, ly, lz = tonumber(scanResult[2]), tonumber(scanResult[3]), tonumber(scanResult[4])
  36.        
  37.         -- Fire all lasers at the target coordinates
  38.         for _, laser in ipairs(lasers) do
  39.             local laserX, laserY, laserZ = laser.getLocalPosition()
  40.             local dx = lx - laserX
  41.             local dy = ly - laserY
  42.             local dz = lz - laserZ
  43.             laser.emitBeam(dx, dy, dz)
  44.             os.sleep(0) -- yield to allow the coroutine scheduler to run
  45.         end
  46.     end
  47. end
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement