Advertisement
jaklsfjlsak

激光制导护盾系统723

Jul 23rd, 2023
859
1
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. local rangeUpgrade = 8 --Amount of Range Upgrades in Network, Including Relays
  6. local OffsetValue = 0 --How much you want your shield to be configured forward
  7. local shield = peripheral.find("warpdriveForceFieldProjector")
  8. local laser = peripheral.find("warpdriveLaserCamera")
  9. local lever = "front" -- format: front,back,left,right,top,bottom ONLY
  10. local safedist = 15 --Set Safe Distance
  11. -- Sets the frequency of the laser to 1420
  12. laser.beamFrequency(1420)
  13.  
  14. -- initial message
  15. print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
  16.  
  17.  
  18. local _, upgrades = shield.getUpgrades()
  19.  
  20. Size = rangeUpgrade * 16
  21.  
  22. while true do
  23.     os.sleep(0.5)
  24.     local event, key = os.pullEvent()    
  25.        
  26.     if event == "key" then
  27.         -- keys are represented by numbers, the number for 'C' is 46
  28.         if key == 46 then
  29.             print("C key pressed, running 'conf' script...")
  30.             shell.run("conf")
  31.         end
  32.     elseif event == "laserScanning" then
  33.         -- Laser scan target position
  34. local lx, ly, lz = laser.getScanResult()
  35.  
  36. -- Shield position
  37. local fx, fy, fz = shield.getLocalPosition()
  38.  
  39. function shieldOffset()
  40. -- Calculate direction vector from shield position to laser scan target position
  41. local dx = lx - fx
  42. local dy = ly - fy
  43. local dz = lz - fz
  44.  
  45. -- Calculate the length of the direction vector
  46. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  47.  
  48. -- Normalize the direction vector to have a length of 1 (unit vector)
  49. dx = dx / distance
  50. dy = dy / distance
  51. dz = dz / distance
  52.  
  53. -- Calculate the final target position by extending the direction vector by OffsetValue
  54. local t1x = lx + dx * OffsetValue
  55. local t1y = ly + dy * OffsetValue
  56. local t1z = lz + dz * OffsetValue
  57. end
  58.        
  59.         shieldOffset()
  60.        
  61.         tx = (t1x-fx) / Size
  62.         ty = (t1y-fy) / Size
  63.         tz = (t1z-fz) / Size
  64.  
  65.  
  66.        
  67.            
  68.         if distance < safedist then
  69.             print("Target is too Close! Shield Disabled!")
  70.             shield.enable(false) --disable shield if too close
  71.         elseif distance > safedist then
  72.             shield.translation(tx, ty, tz)
  73.         end
  74.     elseif event == "redstone" then
  75.         dis()
  76.         local on = redstone.getAnalogInput(lever)
  77.         if on > 6 and distance > safedist then
  78.             shield.enable(true)
  79.         elseif on > 6 and distance < safedist then
  80.             shield.enable(false)
  81.              print("Target is too Close! Shield Disabled!")
  82.         elseif on < 5 then
  83.             shield.enable(false)
  84.         end
  85.     end
  86. end
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement