thunder.nas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var splash_vec_loop = func(){
  2. var airspeed = getprop("/velocities/airspeed-kt");
  3. airspeed = airspeed + getprop("/engines/engine/rpm")*0.0191;
  4. var splash_x = -0.71 - 0.0071 * airspeed;
  5. var splash_y = 0.0;
  6. var splash_z = 0.71 + 0.0071 * airspeed;
  7. setprop("/environment/aircraft-effects/splash-vector-x", splash_x);
  8. setprop("/environment/aircraft-effects/splash-vector-y", splash_y);
  9. setprop("/environment/aircraft-effects/splash-vector-z", splash_z);
  10. settimer(func(){
  11. splash_vec_loop();
  12. }, 0.05);
  13. }
  14. settimer(func(){
  15. splash_vec_loop();
  16. }, 3);
  17. ##########################################
  18. # Click Sounds
  19. ##########################################
  20. var click = func (name, timeout=0.1, delay=0) {
  21. var sound_prop = "/sim/model/AvroVulcanB2/sound/click-" ~ name;
  22. settimer(func {
  23. # Play the sound
  24. setprop(sound_prop, 1);
  25. # Reset the property after 0.2 seconds so that the sound can be
  26. # played again.
  27. settimer(func {
  28. setprop(sound_prop, 0);
  29. }, timeout);
  30. }, delay);
  31. };
  32. ##########################################
  33. # Thunder Sound
  34. ##########################################
  35. var speed_of_sound = func (t, re) {
  36. # Compute speed of sound in m/s
  37. #
  38. # t = temperature in Celsius
  39. # re = amount of water vapor in the air
  40. # Compute virtual temperature using mixing ratio (amount of water vapor)
  41. # Ratio of gas constants of dry air and water vapor: 287.058 / 461.5 = 0.622
  42. var T = 273.15 + t;
  43. var v_T = T * (1 + re/0.622)/(1 + re);
  44. # Compute speed of sound using adiabatic index, gas constant of air,
  45. # and virtual temperature in Kelvin.
  46. return math.sqrt(1.4 * 287.058 * v_T);
  47. };
  48. var thunder = func (name) {
  49. var thunderCalls = 0;
  50. var lightning_pos_x = getprop("/environment/lightning/lightning-pos-x");
  51. var lightning_pos_y = getprop("/environment/lightning/lightning-pos-y");
  52. var lightning_distance = math.sqrt(math.pow(lightning_pos_x, 2) + math.pow(lightning_pos_y, 2));
  53. # On the ground, thunder can be heard up to 16 km. Increase this value
  54. # a bit because the aircraft is usually in the air.
  55. if (lightning_distance > 20000)
  56. return;
  57. var t = getprop("/environment/temperature-degc");
  58. var re = getprop("/environment/relative-humidity") / 100;
  59. var delay_seconds = lightning_distance / speed_of_sound(t, re);
  60. # Maximum volume at 5000 meter
  61. var lightning_distance_norm = std.min(1.0, 1 / math.pow(lightning_distance / 5000.0, 2));
  62. settimer(func {
  63. var thunder1 = getprop("/sim/model/AvroVulcanB2/sound/click-thunder1");
  64. var thunder2 = getprop("/sim/model/AvroVulcanB2/sound/click-thunder2");
  65. var thunder3 = getprop("/sim/model/AvroVulcanB2/sound/click-thunder3");
  66. if (!thunder1) {
  67. thunderCalls = 1;
  68. setprop("/sim/model/AvroVulcanB2/sound/lightning/dist1", lightning_distance_norm);
  69. }
  70. else if (!thunder2) {
  71. thunderCalls = 2;
  72. setprop("/sim/model/AvroVulcanB2/sound/lightning/dist2", lightning_distance_norm);
  73. }
  74. else if (!thunder3) {
  75. thunderCalls = 3;
  76. setprop("/sim/model/AvroVulcanB2/sound/lightning/dist3", lightning_distance_norm);
  77. }
  78. else
  79. return;
  80. # Play the sound (sound files are about 9 seconds)
  81. click("thunder" ~ thunderCalls, 9.0, 0);
  82. }, delay_seconds);
  83. };
  84. # Listening for lightning strikes
  85. setlistener("/environment/lightning/lightning-pos-y", thunder);