rwr.nas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Radar2 and RWR routines.
  2. # Alexis Bory (xiii)
  3. # Every 0.05 seconde:
  4. # [1] Scans /AI/models for (aircrafts), (carriers), multiplayers. Creates a list of
  5. # these targets, whenever they are in radar overall range and are valid.
  6. # [2] RWR (Radar Warning Receiver) signals are then computed. RWR signal values
  7. # are writen under /instrumentation/radar2/targets for interoperabilty purposes.
  8. # [3] At each loop the targets list is scanned and each target bearing is checked
  9. # against the radar beam heading. If the target is within the radar beam, its
  10. # display properties are updated. Two different displays are possible:
  11. # B-scan like and PPI like.
  12. # The target distance is then scored so the radar system can autotrack the
  13. # nearest target.
  14. # Every 0.1 seconde:
  15. # [4] Computes HUD marker position for the nearest target.
  16. var OurAlt = props.globals.getNode("position/altitude-ft");
  17. var OurHdg = props.globals.getNode("orientation/heading-deg");
  18. var EcmOn = props.globals.getNode("instrumentation/ecm/on-off", 1);
  19. var EcmAlert1 = props.globals.getNode("instrumentation/ecm/alert-type1", 1);
  20. var EcmAlert2 = props.globals.getNode("instrumentation/ecm/alert-type2", 1);
  21. var our_alt = 0;
  22. var Mp = props.globals.getNode("ai/models");
  23. var tgts_list = [];
  24. var ecm_alert1 = 0;
  25. var ecm_alert2 = 0;
  26. var ecm_alert1_last = 0;
  27. var ecm_alert2_last = 0;
  28. var u_ecm_signal = 0;
  29. var u_ecm_signal_norm = 0;
  30. var u_radar_standby = 0;
  31. var u_ecm_type_num = 0;
  32. init = func() {
  33. radardist.init();
  34. settimer(rwr_loop, 0.5);
  35. }
  36. # Main loop ###############
  37. var rwr_loop = func() {
  38. ecm_on = EcmOn.getBoolValue();
  39. if ( ecm_on) {
  40. our_alt = OurAlt.getValue();
  41. tgts_list = [];
  42. var raw_list = Mp.getChildren();
  43. foreach( var c; raw_list ) {
  44. var type = c.getName();
  45. if (!c.getNode("valid", 1).getValue()) {
  46. continue;
  47. }
  48. var HaveRadarNode = c.getNode("radar");
  49. if (type == "multiplayer" or type == "tanker" and HaveRadarNode != nil) {
  50. var u = Threat.new(c);
  51. u_ecm_signal = 0;
  52. u_ecm_signal_norm = 0;
  53. u_radar_standby = 0;
  54. u_ecm_type_num = 0;
  55. if ( u.Range != nil) {
  56. # Test if target has a radar. Compute if we are illuminated. This propery used by ECM
  57. # over MP, should be standardized, like "ai/models/multiplayer[0]/radar/radar-standby".
  58. var u_name = radardist.get_aircraft_name(u.string);
  59. var u_maxrange = radardist.my_maxrange(u_name); # in kilometer, 0 is unknown or no radar.
  60. var horizon = u.get_horizon( our_alt );
  61. var u_rng = u.get_range();
  62. var u_carrier = u.check_carrier_type();
  63. if ( u.get_rdr_standby() == 0 and u_maxrange > 0 and u_rng < horizon ) {
  64. # Test if we are in its radar field (hard coded 74°) or if we have a MPcarrier.
  65. # Compute the signal strength.
  66. var our_deviation_deg = deviation_normdeg(u.get_heading(), u.get_reciprocal_bearing());
  67. if ( our_deviation_deg < 0 ) { our_deviation_deg *= -1 }
  68. if ( our_deviation_deg < 37 or u_carrier == 1 ) {
  69. u_ecm_signal = (((-our_deviation_deg/20)+2.5)*(!u_carrier )) + (-u_rng/20) + 2.6 + (u_carrier*1.8);
  70. u_ecm_type_num = radardist.get_ecm_type_num(u_name);
  71. }
  72. } else {
  73. u_ecm_signal = 0;
  74. }
  75. # Compute global threat situation for undiscriminant warning lights
  76. # and discrete (normalized) definition of threat strength.
  77. if ( u_ecm_signal > 1 and u_ecm_signal < 3 ) {
  78. EcmAlert1.setBoolValue(1);
  79. ecm_alert1 = 1;
  80. u_ecm_signal_norm = 2;
  81. } elsif ( u_ecm_signal >= 3 ) {
  82. EcmAlert2.setBoolValue(1);
  83. ecm_alert2 = 1;
  84. u_ecm_signal_norm = 1;
  85. }
  86. u.EcmSignal.setValue(u_ecm_signal);
  87. u.EcmSignalNorm.setIntValue(u_ecm_signal_norm);
  88. u.EcmTypeNum.setIntValue(u_ecm_type_num);
  89. }
  90. }
  91. }
  92. # Summarize ECM alerts.
  93. if ( ecm_alert1 == 0 and ecm_alert1_last == 0 ) { EcmAlert1.setBoolValue(0) }
  94. if ( ecm_alert2 == 0 and ecm_alert1_last == 0 ) { EcmAlert2.setBoolValue(0) }
  95. ecm_alert1_last = ecm_alert1; # And avoid alert blinking at each loop.
  96. ecm_alert2_last = ecm_alert2;
  97. ecm_alert1 = 0;
  98. ecm_alert2 = 0;
  99. } elsif ( size(tgts_list) > 0 ) {
  100. foreach( u; tgts_list ) {
  101. u.EcmSignal.setValue(0);
  102. u.EcmSignalNorm.setIntValue(0);
  103. u.EcmTypeNum.setIntValue(0);
  104. }
  105. }
  106. settimer(rwr_loop, 0.05);
  107. }
  108. # Utilities.
  109. var deviation_normdeg = func(our_heading, target_bearing) {
  110. var dev_norm = our_heading - target_bearing;
  111. while (dev_norm < -180) dev_norm += 360;
  112. while (dev_norm > 180) dev_norm -= 360;
  113. return(dev_norm);
  114. }
  115. setlistener("sim/signals/fdm-initialized", init);
  116. # Target class
  117. var Threat = {
  118. new : func (c) {
  119. var obj = { parents : [Threat]};
  120. obj.RdrProp = c.getNode("radar");
  121. obj.Heading = c.getNode("orientation/true-heading-deg");
  122. obj.Alt = c.getNode("position/altitude-ft");
  123. obj.AcType = c.getNode("sim/model/ac-type");
  124. obj.type = c.getName();
  125. obj.index = c.getIndex();
  126. obj.string = "ai/models/" ~ obj.type ~ "[" ~ obj.index ~ "]";
  127. obj.shortstring = obj.type ~ "[" ~ obj.index ~ "]";
  128. obj.InstrTgts = props.globals.getNode("instrumentation/radar2/targets", 1);
  129. obj.TgtsFiles = obj.InstrTgts.getNode(obj.shortstring, 1);
  130. obj.Range = obj.RdrProp.getNode("range-nm");
  131. obj.Bearing = obj.RdrProp.getNode("bearing-deg");
  132. obj.Elevation = obj.RdrProp.getNode("elevation-deg");
  133. obj.BBearing = obj.TgtsFiles.getNode("bearing-deg", 1);
  134. obj.BHeading = obj.TgtsFiles.getNode("true-heading-deg", 1);
  135. obj.RangeScore = obj.TgtsFiles.getNode("range-score", 1);
  136. obj.RelBearing = obj.TgtsFiles.getNode("ddd-relative-bearing", 1);
  137. obj.Carrier = obj.TgtsFiles.getNode("carrier", 1);
  138. obj.EcmSignal = obj.TgtsFiles.getNode("ecm-signal", 1);
  139. obj.EcmSignalNorm = obj.TgtsFiles.getNode("ecm-signal-norm", 1);
  140. obj.EcmTypeNum = obj.TgtsFiles.getNode("ecm_type_num", 1);
  141. obj.RadarStandby = c.getNode("sim/multiplay/generic/int[6]");
  142. obj.deviation = nil;
  143. return obj;
  144. },
  145. get_heading : func {
  146. var n = me.Heading.getValue();
  147. me.BHeading.setValue(n);
  148. return n;
  149. },
  150. get_bearing : func {
  151. var n = me.Bearing.getValue();
  152. me.BBearing.setValue(n);
  153. return n;
  154. },
  155. set_relative_bearing : func(n) {
  156. me.RelBearing.setValue(n);
  157. },
  158. get_reciprocal_bearing : func {
  159. return geo.normdeg(me.get_bearing() + 180);
  160. },
  161. get_deviation : func(true_heading_ref) {
  162. me.deviation = - deviation_normdeg(true_heading_ref, me.get_bearing());
  163. return me.deviation;
  164. },
  165. get_altitude : func {
  166. return me.Alt.getValue();
  167. },
  168. get_range : func {
  169. return me.Range.getValue();
  170. },
  171. get_horizon : func(own_alt) {
  172. var tgt_alt = me.get_altitude();
  173. if ( tgt_alt != nil ) {
  174. if ( own_alt < 0 ) { own_alt = 0.001 }
  175. if ( debug.isnan(tgt_alt)) {
  176. return(0);
  177. }
  178. if ( tgt_alt < 0 ) { tgt_alt = 0.001 }
  179. return radardist.radar_horizon( own_alt, tgt_alt );
  180. } else {
  181. return(0);
  182. }
  183. },
  184. check_carrier_type : func {
  185. var type = "none";
  186. var carrier = 0;
  187. if ( me.AcType != nil ) { type = me.AcType.getValue() }
  188. if ( type == "MP-Nimitz" or type == "MP-Eisenhower" or type == "MP-Vinson") { carrier = 1 }
  189. # This works only after the mp-carrier model has been loaded. Before that it is seen like a common aircraft.
  190. me.Carrier.setBoolValue(carrier);
  191. return carrier;
  192. },
  193. get_rdr_standby : func {
  194. var s = 0;
  195. if ( me.RadarStandby != nil ) {
  196. s = me.RadarStandby.getValue();
  197. if (s == nil) { s = 0 } elsif (s != 1) { s = 0 }
  198. }
  199. return s;
  200. },
  201. list : [],
  202. };