]> git.mxchange.org Git - flightgear.git/blob - src/Main/save.cxx
464c0de54db2c6af15919680ec24b2d8f08512ee
[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() - 6);
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                                 // FIXME: save each brake separately
108   SAVE("brake", FGBFI::getBrake());
109
110   //
111   // Radio navigation
112   //
113   SAVE("nav1-active-frequency", FGBFI::getNAV1Freq());
114   SAVE("nav1-standby-frequency", FGBFI::getNAV1AltFreq());
115   SAVE("nav1-selected-radial", FGBFI::getNAV1SelRadial());
116   SAVE("nav2-active-frequency", FGBFI::getNAV2Freq());
117   SAVE("nav2-standby-frequency", FGBFI::getNAV2AltFreq());
118   SAVE("nav2-selected-radial", FGBFI::getNAV2SelRadial());
119   SAVE("adf-active-frequency", FGBFI::getADFFreq());
120   SAVE("adf-standby-frequency", FGBFI::getADFAltFreq());
121   SAVE("adf-rotation", FGBFI::getADFRotation());
122
123   //
124   // Autopilot and GPS
125   //
126   if (FGBFI::getTargetAirport().length() > 0)
127     SAVE("target-airport", FGBFI::getTargetAirport());
128   SAVE("autopilot-altitude-lock", FGBFI::getAPAltitudeLock());
129   SAVE("autopilot-altitude", FGBFI::getAPAltitude());
130   SAVE("autopilot-heading-lock", FGBFI::getAPHeadingLock());
131   SAVE("autopilot-heading", FGBFI::getAPHeading());
132   SAVE("autopilot-gps-lock", FGBFI::getGPSLock());
133   SAVE("autopilot-gps-lat", FGBFI::getGPSTargetLatitude());
134   SAVE("autopilot-gps-lon", FGBFI::getGPSTargetLongitude());
135
136   //
137   // Environment.
138   //
139   SAVE("visibility", FGBFI::getVisibility());
140   SAVE("clouds", FGBFI::getClouds());
141   SAVE("clouds-asl", FGBFI::getCloudsASL());
142
143   return true;
144 }
145
146
147 /**
148  * Restore the current state of the simulator from a stream.
149  */
150 bool
151 fgLoadFlight (istream &input)
152 {
153 //   FGInterface * f = current_aircraft.fdm_state;
154   string text;
155   double n;
156   long int i;
157
158   if (!input.good() || input.eof()) {
159     cout << "Stream is no good!\n";
160     return false;
161   }
162
163   input >> text;
164   if (text != "#!fgfs") {
165     cerr << "Bad save file format!\n";
166     return false;
167   }
168
169
170   while (input.good() && !input.eof()) {
171     input >> text;
172
173     //
174     // Simulation.
175     //
176
177     if (text == "flight-model:") {
178       input >> i;
179       cout << "flight model is " << i << endl;
180       FGBFI::setFlightModel(i);
181     }
182
183     else if (text == "aircraft:") {
184       input >> text;
185       cout << "aircraft is " << text << endl;
186       FGBFI::setAircraft(text);
187     }
188
189     else if (text == "aircraft-dir:") {
190       input >> text;
191       cout << "aircraft-dir is " << text << endl;
192       FGBFI::setAircraftDir(text);
193     }
194
195     else if (text == "time:") {
196       input >> i;
197       cout << "saved time is " << i << endl;
198       FGBFI::setTimeGMT(i);
199     }
200
201     else if (text == "hud:") {
202       input >> i;
203       cout << "hud status is " << i << endl;
204       FGBFI::setHUDVisible(i);
205     }
206
207     else if (text == "panel:") {
208       input >> i;
209       cout << "panel status is " << i << endl;
210       FGBFI::setPanelVisible(i);
211     }
212
213     //
214     // Location
215     //
216       
217     else if (text == "latitude:") {
218       input >> n;
219       cout << "latitude is " << n << endl;
220       FGBFI::setLatitude(n);
221     } 
222
223     else if (text == "longitude:") {
224       input >> n;
225       cout << "longitude is " << n << endl;
226       FGBFI::setLongitude(n);
227     } 
228
229     else if (text == "altitude:") {
230       input >> n;
231       cout << "altitude is " << n << endl;
232       FGBFI::setAltitude(n);
233     } 
234
235     //
236     // Orientation
237     //
238
239     else if (text == "heading:") {
240       input >> n;
241       cout << "heading is " << n << endl;
242       FGBFI::setHeading(n);
243     } 
244
245     else if (text == "pitch:") {
246       input >> n;
247       cout << "pitch is " << n << endl;
248       FGBFI::setPitch(n);
249     } 
250
251     else if (text == "roll:") {
252       input >> n;
253       cout << "roll is " << n << endl;
254       FGBFI::setRoll(n);
255     } 
256
257     //
258     // Velocities
259     //
260
261     else if (text == "speed-north:") {
262       input >> n;
263       cout << "speed north is " << n << endl;
264       FGBFI::setSpeedNorth(n);
265     } 
266
267     else if (text == "speed-east:") {
268       input >> n;
269       cout << "speed east is " << n << endl;
270       FGBFI::setSpeedEast(n);
271     } 
272
273     else if (text == "speed-down:") {
274       input >> n;
275       cout << "speed down is " << n << endl;
276       FGBFI::setSpeedDown(n);
277     } 
278
279     //
280     // Primary controls
281     //
282
283     else if (text == "elevator:") {
284       input >> n;
285       cout << "elevator is " << n << endl;
286       FGBFI::setElevator(n);
287     }
288
289     else if (text == "aileron:") {
290       input >> n;
291       cout << "aileron is " << n << endl;
292       FGBFI::setAileron(n);
293     }
294
295     else if (text == "rudder:") {
296       input >> n;
297       cout << "rudder is " << n << endl;
298       FGBFI::setRudder(n);
299     }
300
301                                 // FIXME: assumes single engine
302     else if (text == "throttle:") {
303       input >> n;
304       cout << "throttle is " << n << endl;
305       FGBFI::setThrottle(n);
306     }
307
308     //
309     // Secondary controls
310
311     else if (text == "elevator-trim:") {
312       input >> n;
313       cout << "elevator trim is " << n << endl;
314       FGBFI::setElevatorTrim(n);
315     }
316
317     else if (text == "flaps:") {
318       input >> n;
319       cout << "flaps are " << n << endl;
320       FGBFI::setFlaps(n);
321     }
322
323     else if (text == "brake:") {
324       input >> n;
325       cout << "brake is " << n << endl;
326       FGBFI::setBrake(n);
327     }
328
329
330     //
331     // Radio navigation
332     //
333
334     else if (text == "nav1-active-frequency:") {
335       input >> n;
336       FGBFI::setNAV1Freq(n);
337     }
338
339     else if (text == "nav1-standby-frequency:") {
340       input >> n;
341       FGBFI::setNAV1AltFreq(n);
342     }
343
344     else if (text == "nav1-selected-radial:") {
345       input >> n;
346       FGBFI::setNAV1SelRadial(n);
347     }
348
349     else if (text == "nav2-active-frequency:") {
350       input >> n;
351       FGBFI::setNAV2Freq(n);
352     }
353
354     else if (text == "nav2-standby-frequency:") {
355       input >> n;
356       FGBFI::setNAV2AltFreq(n);
357     }
358
359     else if (text == "nav2-selected-radial:") {
360       input >> n;
361       FGBFI::setNAV2SelRadial(n);
362     }
363
364     else if (text == "adf-active-frequency:") {
365       input >> n;
366       FGBFI::setADFFreq(n);
367     }
368
369     else if (text == "adf-standby-frequency:") {
370       input >> n;
371       FGBFI::setADFAltFreq(n);
372     }
373
374     else if (text == "adf-rotation:") {
375       input >> n;
376       FGBFI::setADFRotation(n);
377     }
378
379
380     //
381     // Autopilot and GPS
382     //
383
384     else if (text == "target-airport:") {
385       input >> text;
386       cout << "target airport is " << text << endl;
387       FGBFI::setTargetAirport(text);
388     }
389
390     else if (text == "autopilot-altitude-lock:") {
391       input >> i;
392       cout << "autopilot altitude lock is " << i << endl;
393       FGBFI::setAPAltitudeLock(i);
394     }
395
396     else if (text == "autopilot-altitude:") {
397       input >> n;
398       cout << "autopilot altitude is " << n << endl;
399       FGBFI::setAPAltitude(n);
400     }
401
402     else if (text == "autopilot-heading-lock:") {
403       input >> i;
404       cout << "autopilot heading lock is " << i << endl;
405       FGBFI::setAPHeadingLock(i);
406     }
407
408     else if (text == "autopilot-heading:") {
409       input >> n;
410       cout << "autopilot heading is " << n << endl;
411       FGBFI::setAPHeading(n);
412     }
413
414     else if (text == "autopilot-gps-lock:") {
415       input >> i;
416       cout << "autopilot GPS lock is " << i << endl;
417       FGBFI::setGPSLock(i);
418     }
419
420     else if (text == "autopilot-gps-lat:") {
421       input >> n;
422       cout << "GPS target latitude is " << n << endl;
423       FGBFI::setGPSTargetLatitude(n);
424     }
425
426     else if (text == "autopilot-gps-lon:") {
427       input >> n;
428       cout << "GPS target longitude is " << n << endl;
429       FGBFI::setGPSTargetLongitude(n);
430     }
431
432     //
433     // Environment.
434     //
435
436     else if (text == "visibility:") {
437       input >> n;
438       cout << "visibility is " << n << endl;
439       FGBFI::setVisibility(n);
440     }
441
442     else if (text == "clouds:") {
443       input >> i;
444       cout << "clouds is " << i << endl;
445       FGBFI::setClouds(i);
446     }
447
448     else if (text == "clouds-asl:") {
449       input >> n;
450       cout << "clouds-asl is " << n << endl;
451       FGBFI::setCloudsASL(n);
452     }
453
454     //
455     // Don't die if we don't recognize something
456     //
457     
458     else {
459       cerr << "Skipping unknown field: " << text << endl;
460       input >> text;
461     }
462   }
463
464   FGBFI::update();
465
466   return true;
467 }
468
469 // end of save.cxx