pilot-dual-control.nas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ###############################################################################
  2. ## $Id: pilot-dual-control.nas,v 1.3 2009/05/24 12:52:12 mfranz Exp $
  3. ##
  4. ## Nasal for main pilot for dual control over the multiplayer network.
  5. ##
  6. ## Copyright (C) 2007 - 2009 Anders Gidenstam (anders(at)gidenstam.org)
  7. ## This file is licensed under the GPL license version 2 or later.
  8. ##
  9. ###############################################################################
  10. # Renaming (almost :)
  11. var DCT = dual_control_tools;
  12. var ADC = aircraft_dual_control;
  13. # NOTE: By loading the aircraft specific dual control module
  14. # as <aircraft_dual_control> this file is generic.
  15. # The aircraft specific modul must set the variables
  16. # pilot_type and copilot_type to the name (with full path) of
  17. # main 3d model XML for the pilot and copilot aircraft.
  18. # This module should be loades under the name dual_control.
  19. ######################################################################
  20. # Connect to new copilot
  21. var process_data = 0;
  22. var connect = func (copilot) {
  23. print("Trying connect copilot to pilot (pilot-dual-control.nas)");
  24. # Tweak MP/AI filters
  25. copilot.getNode("controls/allow-extrapolation").setBoolValue(0);
  26. copilot.getNode("controls/lag-adjust-system-speed").setValue(5);
  27. process_data = ADC.pilot_connect_copilot(copilot);
  28. print("Dual control ... copilot connected.");
  29. setprop("/sim/messages/copilot", "Hi. I'm your copilot !");
  30. }
  31. ######################################################################
  32. # Main loop singleton class.
  33. var main = {
  34. init : func {
  35. me.loopid = 0;
  36. me.active = 0;
  37. setlistener("/ai/models/model-added", func {
  38. settimer(func { me.activate(); }, 2);
  39. });
  40. settimer(func { me.activate(); }, 5);
  41. print("Pilot dual control ... initialized");
  42. },
  43. reset : func {
  44. me.active = 0;
  45. me.loopid += 1;
  46. me._loop_(me.loopid);
  47. },
  48. activate : func {
  49. if (!me.active) {
  50. me.reset();
  51. }
  52. },
  53. update : func {
  54. var mpplayers =
  55. props.globals.getNode("/ai/models").getChildren("multiplayer");
  56. var r_callsign = getprop("/sim/remote/pilot-callsign");
  57. foreach (var copilot; mpplayers) {
  58. if ((copilot.getChild("valid").getValue()) and
  59. (copilot.getChild("callsign") != nil) and
  60. (copilot.getChild("callsign").getValue() == r_callsign)) {
  61. if (me.active == 0) {
  62. # Note: sim/model/path tells which XML file of the model.
  63. if ((copilot.getNode("sim/model/path") != nil) and
  64. (copilot.getNode("sim/model/path").getValue() ==
  65. ADC.copilot_type)) {
  66. connect(copilot);
  67. me.active = 1;
  68. } else {
  69. print("Dual control ... copilot rejected - wrong aircraft type.");
  70. me.loopid += 1;
  71. return;
  72. }
  73. }
  74. # Mess with the MP filters. Highly experimental.
  75. if (copilot.getNode("controls/lag-time-offset") != nil) {
  76. var v = copilot.getNode("controls/lag-time-offset").getValue();
  77. copilot.getNode("controls/lag-time-offset").setValue(0.97 * v);
  78. }
  79. foreach (var w; process_data) {
  80. w.update();
  81. }
  82. return;
  83. }
  84. }
  85. if (me.active) {
  86. print("Dual control ... copilot disconnected.");
  87. ADC.pilot_disconnect_copilot();
  88. }
  89. me.loopid += 1;
  90. me.active = 0;
  91. },
  92. _loop_ : func(id) {
  93. id == me.loopid or return;
  94. me.update();
  95. settimer(func { me._loop_(id); }, 0);
  96. }
  97. };
  98. ######################################################################
  99. # Initialization.
  100. setlistener("/sim/signals/fdm-initialized", func {
  101. main.init();
  102. });