Advertisement
jaklsfjlsak

试制护盾采矿723 v3 多激光

Jul 25th, 2023 (edited)
709
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 OffsetValue = 3
  7. local shield = peripheral.find("warpdriveForceFieldProjector")
  8. local lasers = peripheral.getNames() -- get all peripherals
  9. local lever = "front"
  10. local safedist = 15
  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. while true do
  42.     for _, laserName in ipairs(lasers) do
  43.         local laser = peripheral.wrap(laserName)
  44.         local event, key = os.pullEvent()
  45.         os.sleep(0.5)
  46.  
  47.         if event == "key" then
  48.             if key == 46 then
  49.                 print("C key pressed, running 'conf' script...")
  50.                 shell.run("conf")
  51.             end
  52.         end
  53.  
  54.         if event == "laserScanning" then
  55.             local type, lx, ly, lz, block = laser.getScanResult()
  56.             lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz)
  57.             local fx, fy, fz = shield.getLocalPosition()
  58.  
  59.             function shieldOffset()
  60.                 local dx = lx - fx
  61.                 local dy = ly - fy
  62.                 local dz = lz - fz
  63.                 local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  64.                 dx = dx / distance
  65.                 dy = dy / distance
  66.                 dz = dz / distance
  67.                 local t1x = lx + dx * OffsetValue
  68.                 local t1y = ly + dy * OffsetValue
  69.                 local t1z = lz + dz * OffsetValue
  70.                 return t1x, t1y, t1z, distance
  71.             end
  72.  
  73.             local t1x, t1y, t1z, distance = shieldOffset()
  74.  
  75.             tx = (t1x - fx) / Size
  76.             ty = (t1y - fy) / Size
  77.             tz = (t1z - fz) / Size
  78.  
  79.             if distance < safedist then
  80.                 print("Target is too Close! Shield Disabled!")
  81.                 shield.enable(false)
  82.             elseif distance > safedist then
  83.                 shield.translation(tx, ty, tz)
  84.             end
  85.         elseif event == "redstone" then
  86.             local _, _, _, distance = shieldOffset()
  87.             local on = redstone.getAnalogInput(lever)
  88.             if on > 6 and distance > safedist then
  89.                 shield.enable(true)
  90.             elseif on > 6 and distance < safedist then
  91.                 shield.enable(false)
  92.                 print("Target is too Close! Shield Disabled!")
  93.             elseif on < 5 then
  94.                 shield.enable(false)
  95.             end
  96.         end
  97.     end
  98. end
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement