Debian Codegraph @

Aircraft/Generic/DualControl/pilot-dual-control.nas

raw
1 ###############################################################################
2 ##
3 ## Nasal for main pilot for dual control over the multiplayer network.
4 ##
5 ## Copyright (C) 2007 - 2012 Anders Gidenstam (anders(at)gidenstam.org)
6 ## This file is licensed under the GPL license version 2 or later.
7 ##
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 ######################################################################
21 # Connect new copilot
22 var process_data = 0;
23
24 var connect = func (copilot) {
25 # Tweak MP/AI filters
26 copilot.getNode("controls/allow-extrapolation").setBoolValue(0);
27 copilot.getNode("controls/lag-adjust-system-speed").setValue(5);
28
29 process_data = ADC.pilot_connect_copilot(copilot);
30
31 print("Dual control ... copilot connected.");
32 setprop("/sim/messages/copilot", "Hi. I'm your copilot " ~
33 copilot.getNode("callsign").getValue() ~ ".");
34 }
35
36 ######################################################################
37 # Main loop singleton class.
38 var main = {
39 _initialized : 0,
40 init : func {
41 if (!me._initialized) {
42 me.loopid = 0;
43 setlistener("/ai/models/model-added", func {
44 settimer(func { me.activate(); }, 2);
45 });
46 me._initialized = 1;
47 }
48 me.active = 0;
49 settimer(func { me.activate(); }, 5);
50 print("Pilot dual control ... initialized");
51 },
52 reset : func {
53 if (me.active) {
54 print("Dual control ... copilot disconnected.");
55 ADC.pilot_disconnect_copilot();
56 }
57 me.active = 0;
58 me.loopid += 1;
59 me._loop_(me.loopid);
60 },
61 activate : func {
62 if (!me.active) {
63 me.reset();
64 }
65 },
66 update : func {
67 var mpplayers =
68 props.globals.getNode("/ai/models").getChildren("multiplayer");
69 var r_callsign = getprop("/sim/remote/pilot-callsign");
70
71 foreach (var copilot; mpplayers) {
72 if ((copilot.getChild("valid").getValue()) and
73 (copilot.getChild("callsign") != nil) and
74 (copilot.getChild("callsign").getValue() == r_callsign)) {
75
76 if (me.active == 0) {
77 # Note: sim/model/path tells the 3d XML file of the model.
78 if ((copilot.getNode("sim/model/path") != nil) and
79 (copilot.getNode("sim/model/path").getValue() ==
80 ADC.copilot_type)) {
81 connect(copilot);
82 me.active = 1;
83 } else {
84 print("Dual control ... copilot rejected - wrong aircraft type.");
85 me.loopid += 1;
86 return;
87 }
88 }
89
90 # Mess with the MP filters. Highly experimental.
91 if (copilot.getNode("controls/lag-time-offset") != nil) {
92 var v = copilot.getNode("controls/lag-time-offset").getValue();
93 copilot.getNode("controls/lag-time-offset").setValue(0.97 * v);
94 }
95
96 foreach (var w; process_data) {
97 w.update();
98 }
99 return;
100 }
101 }
102 if (me.active) {
103 print("Dual control ... copilot disconnected.");
104 ADC.pilot_disconnect_copilot();
105 }
106 me.loopid += 1;
107 me.active = 0;
108 },
109 _loop_ : func(id) {
110 id == me.loopid or return;
111 me.update();
112 settimer(func { me._loop_(id); }, 0);
113 }
114 };
115
116 ######################################################################
117 # Initialization.
118 setlistener("/sim/signals/fdm-initialized", func {
119 main.init();
120 });