Advertisement
jaklsfjlsak

726 护盾多激光 v5

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