]> git.mxchange.org Git - flightgear.git/blob - src/Main/save.cxx
Added code to put aircraft at the end of the runway closest to the desired
[flightgear.git] / src / Main / save.cxx
1 // save.cxx -- class to save and restore a flight.
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  David Megginson - david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 /*
25 TODO:
26 - use a separate options object so that we can roll back on error
27 - use proper FGFS logging
28 - add view direction, and other stuff
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif
34
35 #include <iostream>
36
37 #include <simgear/constants.h>
38 #include <simgear/math/fg_types.hxx>
39
40 #include "bfi.hxx"
41
42 FG_USING_NAMESPACE(std);
43
44 #define SAVE(name, value) { output << name << ": " << value << endl; }
45
46 /**
47  * Save the current state of the simulator to a stream.
48  */
49 bool
50 fgSaveFlight (ostream &output)
51 {
52   output << "#!fgfs" << endl;
53
54   //
55   // Simulation
56   //
57   SAVE("flight-model", FGBFI::getFlightModel());
58   if (FGBFI::getAircraft().length() > 0)
59     SAVE("aircraft", FGBFI::getAircraft());
60   if (FGBFI::getAircraftDir().length() > 0)
61     SAVE("aircraft-dir", FGBFI::getAircraftDir());
62   SAVE("time", FGBFI::getTimeGMT());
63   SAVE("hud", FGBFI::getHUDVisible());
64   SAVE("panel", FGBFI::getPanelVisible());
65
66   // 
67   // Location
68   //
69   SAVE("latitude", FGBFI::getLatitude());
70   SAVE("longitude", FGBFI::getLongitude());
71
72                                 // KLUDGE: deal with gear wierdness
73 //   if (FGBFI::getAGL() < 6) {
74 //     SAVE("altitude", FGBFI::getAltitude() - FGBFI::getAGL());
75 //   } else {
76     SAVE("altitude", FGBFI::getAltitude());
77 //   }
78
79   //
80   // Orientation
81   //
82   SAVE("heading", FGBFI::getHeading());
83   SAVE("pitch", FGBFI::getPitch());
84   SAVE("roll", FGBFI::getRoll());
85
86   //
87   // Velocities
88   //
89   SAVE("speed-north", FGBFI::getSpeedNorth());
90   SAVE("speed-east", FGBFI::getSpeedEast());
91   SAVE("speed-down", FGBFI::getSpeedDown());
92
93   //
94   // Primary controls
95   //
96   SAVE("elevator", FGBFI::getElevator());
97   SAVE("aileron", FGBFI::getAileron());
98   SAVE("rudder", FGBFI::getRudder());
99                                 // FIXME: save each throttle separately
100   SAVE("throttle", FGBFI::getThrottle());
101
102   //
103   // Secondary controls
104   //
105   SAVE("elevator-trim", FGBFI::getElevatorTrim());
106   SAVE("flaps", FGBFI::getFlaps());
107   SAVE("brake", FGBFI::getBrakes());
108
109   //
110   // Radio navigation
111   //
112   SAVE("nav1-active-frequency", FGBFI::getNAV1Freq());
113   SAVE("nav1-standby-frequency", FGBFI::getNAV1AltFreq());
114   SAVE("nav1-selected-radial", FGBFI::getNAV1SelRadial());
115   SAVE("nav2-active-frequency", FGBFI::getNAV2Freq());
116   SAVE("nav2-standby-frequency", FGBFI::getNAV2AltFreq());
117   SAVE("nav2-selected-radial", FGBFI::getNAV2SelRadial());
118   SAVE("adf-active-frequency", FGBFI::getADFFreq());
119   SAVE("adf-standby-frequency", FGBFI::getADFAltFreq());
120   SAVE("adf-rotation", FGBFI::getADFRotation());
121
122   //
123   // Autopilot and GPS
124   //
125   if (FGBFI::getTargetAirport().length() > 0)
126     SAVE("target-airport", FGBFI::getTargetAirport());
127   SAVE("autopilot-altitude-lock", FGBFI::getAPAltitudeLock());
128   SAVE("autopilot-altitude", FGBFI::getAPAltitude());
129   SAVE("autopilot-heading-lock", FGBFI::getAPHeadingLock());
130   SAVE("autopilot-heading", FGBFI::getAPHeadingMag());
131   SAVE("autopilot-gps-lock", FGBFI::getGPSLock());
132   SAVE("autopilot-gps-lat", FGBFI::getGPSTargetLatitude());
133   SAVE("autopilot-gps-lon", FGBFI::getGPSTargetLongitude());
134
135   //
136   // Environment.
137   //
138   SAVE("visibility", FGBFI::getVisibility());
139   SAVE("clouds", FGBFI::getClouds());
140   SAVE("clouds-asl", FGBFI::getCloudsASL());
141
142   return true;
143 }
144
145
146 /**
147  * Restore the current state of the simulator from a stream.
148  */
149 bool
150 fgLoadFlight (istream &input)
151 {
152 //   FGInterface * f = current_aircraft.fdm_state;
153   string text;
154   double n;
155   long int i;
156
157   if (!input.good() || input.eof()) {
158     cout << "Stream is no good!\n";
159     return false;
160   }
161
162   input >> text;
163   if (text != "#!fgfs") {
164     cerr << "Bad save file format!\n";
165     return false;
166   }
167
168
169   while (input.good() && !input.eof()) {
170     input >> text;
171
172     //
173     // Simulation.
174     //
175
176     if (text == "flight-model:") {
177       input >> i;
178       cout << "flight model is " << i << endl;
179       FGBFI::setFlightModel(i);
180     }
181
182     else if (text == "aircraft:") {
183       input >> text;
184       cout << "aircraft is " << text << endl;
185       FGBFI::setAircraft(text);
186     }
187
188     else if (text == "aircraft-dir:") {
189       input >> text;
190       cout << "aircraft-dir is " << text << endl;
191       FGBFI::setAircraftDir(text);
192     }
193
194     else if (text == "time:") {
195       input >> i;
196       cout << "saved time is " << i << endl;
197       FGBFI::setTimeGMT(i);
198     }
199
200     else if (text == "hud:") {
201       input >> i;
202       cout << "hud status is " << i << endl;
203       FGBFI::setHUDVisible(i);
204     }
205
206     else if (text == "panel:") {
207       input >> i;
208       cout << "panel status is " << i << endl;
209       FGBFI::setPanelVisible(i);
210     }
211
212     //
213     // Location
214     //
215       
216     else if (text == "latitude:") {
217       input >> n;
218       cout << "latitude is " << n << endl;
219       FGBFI::setLatitude(n);
220     } 
221
222     else if (text == "longitude:") {
223       input >> n;
224       cout << "longitude is " << n << endl;
225       FGBFI::setLongitude(n);
226     } 
227
228     else if (text == "altitude:") {
229       input >> n;
230       cout << "altitude is " << n << endl;
231       FGBFI::setAltitude(n);
232     } 
233
234     //
235     // Orientation
236     //
237
238     else if (text == "heading:") {
239       input >> n;
240       cout << "heading is " << n << endl;
241       FGBFI::setHeading(n);
242     } 
243
244     else if (text == "pitch:") {
245       input >> n;
246       cout << "pitch is " << n << endl;
247       FGBFI::setPitch(n);
248     } 
249
250     else if (text == "roll:") {
251       input >> n;
252       cout << "roll is " << n << endl;
253       FGBFI::setRoll(n);
254     } 
255
256     //
257     // Velocities
258     //
259
260     else if (text == "speed-north:") {
261       input >> n;
262       cout << "speed north is " << n << endl;
263       FGBFI::setSpeedNorth(n);
264     } 
265
266     else if (text == "speed-east:") {
267       input >> n;
268       cout << "speed east is " << n << endl;
269       FGBFI::setSpeedEast(n);
270     } 
271
272     else if (text == "speed-down:") {
273       input >> n;
274       cout << "speed down is " << n << endl;
275       FGBFI::setSpeedDown(n);
276     } 
277
278     //
279     // Primary controls
280     //
281
282     else if (text == "elevator:") {
283       input >> n;
284       cout << "elevator is " << n << endl;
285       FGBFI::setElevator(n);
286     }
287
288     else if (text == "aileron:") {
289       input >> n;
290       cout << "aileron is " << n << endl;
291       FGBFI::setAileron(n);
292     }
293
294     else if (text == "rudder:") {
295       input >> n;
296       cout << "rudder is " << n << endl;
297       FGBFI::setRudder(n);
298     }
299
300                                 // FIXME: assumes single engine
301     else if (text == "throttle:") {
302       input >> n;
303       cout << "throttle is " << n << endl;
304       FGBFI::setThrottle(n);
305     }
306
307     //
308     // Secondary controls
309
310     else if (text == "elevator-trim:") {
311       input >> n;
312       cout << "elevator trim is " << n << endl;
313       FGBFI::setElevatorTrim(n);
314     }
315
316     else if (text == "flaps:") {
317       input >> n;
318       cout << "flaps are " << n << endl;
319       FGBFI::setFlaps(n);
320     }
321
322     else if (text == "brake:") {
323       input >> n;
324       cout << "brake is " << n << endl;
325       FGBFI::setBrakes(n);
326     }
327
328
329     //
330     // Radio navigation
331     //
332
333     else if (text == "nav1-active-frequency:") {
334       input >> n;
335       FGBFI::setNAV1Freq(n);
336     }
337
338     else if (text == "nav1-standby-frequency:") {
339       input >> n;
340       FGBFI::setNAV1AltFreq(n);
341     }
342
343     else if (text == "nav1-selected-radial:") {
344       input >> n;
345       FGBFI::setNAV1SelRadial(n);
346     }
347
348     else if (text == "nav2-active-frequency:") {
349       input >> n;
350       FGBFI::setNAV2Freq(n);
351     }
352
353     else if (text == "nav2-standby-frequency:") {
354       input >> n;
355       FGBFI::setNAV2AltFreq(n);
356     }
357
358     else if (text == "nav2-selected-radial:") {
359       input >> n;
360       FGBFI::setNAV2SelRadial(n);
361     }
362
363     else if (text == "adf-active-frequency:") {
364       input >> n;
365       FGBFI::setADFFreq(n);
366     }
367
368     else if (text == "adf-standby-frequency:") {
369       input >> n;
370       FGBFI::setADFAltFreq(n);
371     }
372
373     else if (text == "adf-rotation:") {
374       input >> n;
375       FGBFI::setADFRotation(n);
376     }
377
378
379     //
380     // Autopilot and GPS
381     //
382
383     else if (text == "target-airport:") {
384       input >> text;
385       cout << "target airport is " << text << endl;
386       FGBFI::setTargetAirport(text);
387     }
388
389     else if (text == "autopilot-altitude-lock:") {
390       input >> i;
391       cout << "autopilot altitude lock is " << i << endl;
392       FGBFI::setAPAltitudeLock(i);
393     }
394
395     else if (text == "autopilot-altitude:") {
396       input >> n;
397       cout << "autopilot altitude is " << n << endl;
398       FGBFI::setAPAltitude(n);
399     }
400
401     else if (text == "autopilot-heading-lock:") {
402       input >> i;
403       cout << "autopilot heading lock is " << i << endl;
404       FGBFI::setAPHeadingLock(i);
405     }
406
407     else if (text == "autopilot-heading:") {
408       input >> n;
409       cout << "autopilot heading is " << n << endl;
410       FGBFI::setAPHeadingMag(n);
411     }
412
413     else if (text == "autopilot-gps-lock:") {
414       input >> i;
415       cout << "autopilot GPS lock is " << i << endl;
416       FGBFI::setGPSLock(i);
417     }
418
419     else if (text == "autopilot-gps-lat:") {
420       input >> n;
421       cout << "GPS target latitude is " << n << endl;
422       FGBFI::setGPSTargetLatitude(n);
423     }
424
425     else if (text == "autopilot-gps-lon:") {
426       input >> n;
427       cout << "GPS target longitude is " << n << endl;
428       FGBFI::setGPSTargetLongitude(n);
429     }
430
431     //
432     // Environment.
433     //
434
435     else if (text == "visibility:") {
436       input >> n;
437       cout << "visibility is " << n << endl;
438       FGBFI::setVisibility(n);
439     }
440
441     else if (text == "clouds:") {
442       input >> i;
443       cout << "clouds is " << i << endl;
444       FGBFI::setClouds(i);
445     }
446
447     else if (text == "clouds-asl:") {
448       input >> n;
449       cout << "clouds-asl is " << n << endl;
450       FGBFI::setCloudsASL(n);
451     }
452
453     //
454     // Don't die if we don't recognize something
455     //
456     
457     else {
458       cerr << "Skipping unknown field: " << text << endl;
459       input >> text;
460     }
461   }
462
463   FGBFI::update();
464
465   return true;
466 }
467
468 // end of save.cxx