Advertisement
jaklsfjlsak

试制护盾采矿723 v4 多激光 简化

Jul 25th, 2023
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if fs.exists("conf") == false then
  2.     shell.run("pastebin get 38M5cNbZ conf")
  3.     term.clear()
  4. end
  5.  
  6. local shield = peripheral.find("warpdriveForceFieldProjector")
  7. local lasers = peripheral.getNames() -- get all peripherals
  8. local lever = "front"
  9. local safedist = 15
  10. local distance -- Initialize here
  11.  
  12. -- filter only warpdriveLaserCamera peripherals
  13. for i=#lasers, 1, -1 do
  14.     if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then
  15.         table.remove(lasers, i)
  16.     else
  17.         peripheral.wrap(lasers[i]).beamFrequency(1420)
  18.     end
  19. end
  20.  
  21. print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
  22.  
  23. local _, upgrades = shield.getUpgrades()
  24.  
  25. function getRange()
  26.     if upgrades:match("1/4 x Range") then
  27.         return 1
  28.     elseif upgrades:match("2/4 x Range") then
  29.         return 2
  30.     elseif upgrades:match("3/4 x Range") then
  31.         return 3
  32.     elseif upgrades:match("4/4 x Range") then
  33.         return 4
  34.     elseif upgrades:match("0/4 x Range") then
  35.         return 0
  36.     end
  37. end
  38.  
  39. Size = getRange() * 16
  40.  
  41. function dis(lx, ly, lz, fx, fy, fz)
  42.     local dx = lx - fx
  43.     local dy = ly - fy
  44.     local dz = lz - fz
  45.     local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  46.     return distance
  47. end
  48.        
  49. while true do
  50.     for _, laserName in ipairs(lasers) do
  51.         local laser = peripheral.wrap(laserName)
  52.         local event, key = os.pullEvent()
  53.         os.sleep(0.5)
  54.  
  55.         if event == "key" then
  56.             if key == 46 then
  57.                 print("C key pressed, running 'conf' script...")
  58.                 shell.run("conf")
  59.             end
  60.         end
  61.  
  62.         if event == "laserScanning" then
  63.             local type, lx, ly, lz, block = laser.getScanResult()
  64.             lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz)
  65.             local fx, fy, fz = shield.getLocalPosition()
  66.            
  67.             tx = (lx - fx) / Size
  68.             ty = (ly - fy) / Size
  69.             tz = (lz - fz) / Size
  70.  
  71.             distance = dis(lx, ly, lz, fx, fy, fz) -- Update the value here
  72.  
  73.             if distance < safedist then
  74.                 print("Target is too Close! Shield Disabled!")
  75.                 shield.enable(false)
  76.             elseif distance > safedist then
  77.                 shield.translation(tx, ty, tz)
  78.             end
  79.         elseif event == "redstone" then
  80.             local on = redstone.getAnalogInput(lever)
  81.             if on > 6 and distance > safedist then
  82.                 shield.enable(true)
  83.             elseif on > 6 and distance < safedist then
  84.                 shield.enable(false)
  85.                 print("Target is too Close! Shield Disabled!")
  86.             elseif on < 5 then
  87.                 shield.enable(false)
  88.             end
  89.         end
  90.     end
  91. end
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement