Debian Codegraph @

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

raw
1 ###############################################################################
2 ##
3 ## Nasal for copilot 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 # Allow aircraft to override the copilot view name. Deprecated.
21 if (!contains(ADC, "copilot_view")) {
22 ADC.copilot_view = "Copilot View";
23 }
24
25 # Properties for position and orientation of local aircraft.
26 var l_lat = "/position/latitude-deg";
27 var l_lon = "/position/longitude-deg";
28 var l_alt = "/position/altitude-ft";
29 var l_heading = "/orientation/heading-deg";
30 var l_pitch = "/orientation/pitch-deg";
31 var l_roll = "/orientation/roll-deg";
32
33 # Replicate remote state.
34 var r_airspeed = "velocities/true-airspeed-kt";
35 var l_airspeed = "/velocities/airspeed-kt";
36 var vertspeed = "velocities/vertical-speed-fps";
37
38 # Default external views to slave to the MP pilot.
39 var views = {};
40 views["Helicopter View"] = 2;
41 views["Chase View"] = 3;
42 views["Tower View"] = 0;
43 views["Fly-By View"] = 1;
44 views["Chase View Without Yaw"] = 1;
45
46 ######################################################################
47 # Connect to new pilot
48 var process_data = 0;
49
50 var connect = func (pilot) {
51 # Set external view eye and target paths.
52 foreach (var vn; keys(views)) {
53 var view_cfg = "/sim/view[" ~ view.indexof(vn) ~ "]/config";
54 setprop(view_cfg ~ "/at-model", 0);
55
56 if (views[vn] > 0) {
57 setprop(view_cfg ~ "/eye-lat-deg-path",
58 pilot.getNode(DCT.lat_mpp).getPath());
59 setprop(view_cfg ~ "/eye-lon-deg-path",
60 pilot.getNode(DCT.lon_mpp).getPath());
61 setprop(view_cfg ~ "/eye-alt-ft-path",
62 pilot.getNode(DCT.alt_mpp).getPath());
63 }
64 if (views[vn] > 1) {
65 setprop(view_cfg ~ "/eye-heading-deg-path",
66 pilot.getNode(DCT.heading_mpp).getPath());
67 }
68 if (views[vn] > 2) {
69 setprop(view_cfg ~ "/eye-pitch-deg-path",
70 pilot.getNode(DCT.pitch_mpp).getPath());
71 setprop(view_cfg ~ "/eye-roll-deg-path",
72 pilot.getNode(DCT.roll_mpp).getPath());
73 }
74
75 setprop(view_cfg ~ "/target-lat-deg-path",
76 pilot.getNode(DCT.lat_mpp).getPath());
77 setprop(view_cfg ~ "/target-lon-deg-path",
78 pilot.getNode(DCT.lon_mpp).getPath());
79 setprop(view_cfg ~ "/target-alt-ft-path",
80 pilot.getNode(DCT.alt_mpp).getPath());
81 setprop(view_cfg ~ "/target-heading-deg-path",
82 pilot.getNode(DCT.heading_mpp).getPath());
83 setprop(view_cfg ~ "/target-pitch-deg-path",
84 pilot.getNode(DCT.pitch_mpp).getPath());
85 setprop(view_cfg ~ "/target-roll-deg-path",
86 pilot.getNode(DCT.roll_mpp).getPath());
87 }
88
89 # Tweak MP/AI filters
90 pilot.getNode("controls/allow-extrapolation").setBoolValue(1);
91 pilot.getNode("controls/lag-adjust-system-speed").setValue(5.0);
92
93 # Set up property aliases
94
95 # Set up property mappings.
96 process_data =
97 [
98 # Map /postition/*
99 DCT.Translator.new
100 (pilot.getNode(DCT.lat_mpp), props.globals.getNode(l_lat)),
101 DCT.Translator.new
102 (pilot.getNode(DCT.lon_mpp), props.globals.getNode(l_lon)),
103 DCT.Translator.new
104 (pilot.getNode(DCT.alt_mpp), props.globals.getNode(l_alt)),
105 # Map /orientation/*
106 DCT.Translator.new
107 (pilot.getNode(DCT.heading_mpp),
108 props.globals.getNode(l_heading)),
109 DCT.Translator.new
110 (pilot.getNode(DCT.pitch_mpp),
111 props.globals.getNode(l_pitch)),
112 DCT.Translator.new
113 (pilot.getNode(DCT.roll_mpp),
114 props.globals.getNode(l_roll)),
115 # Map /velocities/*
116 DCT.Translator.new
117 (pilot.getNode(r_airspeed),
118 props.globals.getNode(l_airspeed)),
119 DCT.Translator.new
120 (pilot.getNode(vertspeed),
121 props.globals.getNode(vertspeed)),
122 ] ~ ADC.copilot_connect_pilot(pilot);
123
124 print("Dual control ... connected to pilot.");
125 setprop("/sim/messages/copilot", "Welcome aboard. I'm " ~
126 pilot.getNode("callsign").getValue() ~ ".");
127 }
128
129 var disconnect = func {
130 # Reset external view eye and target paths.
131 foreach (var vn; keys(views)) {
132 var view_cfg = "/sim/view[" ~ view.indexof(vn) ~ "]/config";
133
134 if (views[vn] > 0) {
135 setprop(view_cfg ~ "/eye-lat-deg-path",
136 "/position/latitude-deg");
137 setprop(view_cfg ~ "/eye-lon-deg-path",
138 "/position/longitude-deg");
139 setprop(view_cfg ~ "/eye-alt-ft-path",
140 "/position/altitude-ft");
141 }
142 if (views[vn] > 1) {
143 setprop(view_cfg ~ "/eye-heading-deg-path",
144 "/orientation/heading-deg");
145 }
146 if (views[vn] > 2) {
147 setprop(view_cfg ~ "/eye-pitch-deg-path",
148 "/orientation/pitch-deg");
149 setprop(view_cfg ~ "/eye-roll-deg-path",
150 "/orientation/roll-deg");
151 }
152 setprop(view_cfg ~ "/target-lat-deg-path",
153 "/sim/viewer/target/latitude-deg");
154 setprop(view_cfg ~ "/target-lon-deg-path",
155 "/sim/viewer/target/longitude-deg");
156 setprop(view_cfg ~ "/target-alt-ft-path",
157 "/sim/viewer/target/altitude-ft");
158 setprop(view_cfg ~ "/target-heading-deg-path",
159 "/sim/viewer/target/heading-deg");
160 setprop(view_cfg ~ "/target-pitch-deg-path",
161 "/sim/viewer/target/pitch-deg");
162 setprop(view_cfg ~ "/target-roll-deg-path",
163 "/sim/viewer/target/roll-deg");
164 }
165 }
166
167
168 ######################################################################
169 # Main loop singleton class.
170 var main = {
171 _initialized : 0,
172 init : func {
173 if (!me._initialized) {
174 me.loopid = 0;
175 setlistener("/ai/models/model-added", func {
176 settimer(func { me.activate(); }, 2);
177 });
178 me._initialized = 1;
179 }
180 me.active = 0;
181 print("Copilot dual control ... initialized");
182 settimer(func { me.activate(); }, 5);
183 },
184 reset : func {
185 if (me.active) {
186 print("Dual control ... disconnected from pilot.");
187 disconnect();
188 ADC.copilot_disconnect_pilot();
189 }
190 me.active = 0;
191 me.loopid += 1;
192 me._loop_(me.loopid);
193 },
194 activate : func {
195 if (!me.active) {
196 me.reset();
197 }
198 },
199 update : func {
200 var mpplayers =
201 props.globals.getNode("/ai/models").getChildren("multiplayer");
202 var r_callsign = getprop("/sim/remote/pilot-callsign");
203
204 foreach (var pilot; mpplayers) {
205 if ((pilot.getChild("valid").getValue()) and
206 (pilot.getChild("callsign") != nil) and
207 (pilot.getChild("callsign").getValue() == r_callsign)) {
208
209 if (me.active == 0) {
210 # Note: sim/model/path contains the model XML file.
211 if ((pilot.getNode("sim/model/path") != nil) and
212 (pilot.getNode("sim/model/path").getValue() ==
213 ADC.pilot_type)) {
214 me.active = 1;
215 connect(pilot);
216 } else {
217 print("Dual control ... pilot rejected - wrong aircraft type.");
218 me.loopid += 1;
219 return;
220 }
221 }
222
223 # Mess with the MP filters. Highly experimental.
224 if (pilot.getNode("controls/lag-time-offset") != nil) {
225 var v = pilot.getNode("controls/lag-time-offset").getValue();
226 #pilot.getNode("controls/lag-time-offset").setValue(0.99 * v);
227 }
228
229 foreach (var w; process_data) {
230 w.update();
231 }
232 return;
233 }
234 }
235 # The pilot player is not around. Idle loop.
236 if (me.active) {
237 print("Dual control ... disconnected from pilot.");
238 disconnect();
239 ADC.copilot_disconnect_pilot();
240 }
241 me.active = 0;
242 me.loopid += 1;
243 },
244 _loop_ : func(id) {
245 id == me.loopid or return;
246 me.update();
247 settimer(func { me._loop_(id); }, 0);
248 }
249 };
250
251 ###############################################################################
252 # Initialization.
253
254 var last_view = 0;
255
256 setlistener("/sim/signals/fdm-initialized", func {
257 main.init();
258 });
259