]> git.mxchange.org Git - flightgear.git/blob - src/Main/save.cxx
Added bfi.[ch]xx (Big Flat Interface) to give a consistant access point
[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   SAVE("time", FGBFI::getTimeGMT());
59   SAVE("hud", FGBFI::getHUDVisible());
60   SAVE("panel", FGBFI::getPanelVisible());
61
62   // 
63   // Location
64   //
65   SAVE("latitude", FGBFI::getLatitude());
66   SAVE("longitude", FGBFI::getLongitude());
67   SAVE("altitude", FGBFI::getAltitude());
68
69   //
70   // Orientation
71   //
72   SAVE("heading", FGBFI::getHeading());
73   SAVE("pitch", FGBFI::getPitch());
74   SAVE("roll", FGBFI::getRoll());
75
76   //
77   // Velocities
78   //
79   SAVE("speed-north", FGBFI::getSpeedNorth());
80   SAVE("speed-east", FGBFI::getSpeedEast());
81   SAVE("speed-down", FGBFI::getSpeedDown());
82
83   //
84   // Primary controls
85   //
86   SAVE("elevator", FGBFI::getElevator());
87   SAVE("aileron", FGBFI::getAileron());
88   SAVE("rudder", FGBFI::getRudder());
89                                 // FIXME: save each throttle separately
90   SAVE("throttle", FGBFI::getThrottle());
91
92   //
93   // Secondary controls
94   //
95   SAVE("elevator-trim", FGBFI::getElevatorTrim());
96   SAVE("flaps", FGBFI::getFlaps());
97                                 // FIXME: save each brake separately
98   SAVE("brake", FGBFI::getBrake());
99
100   //
101   // Navigation.
102   //
103   if (FGBFI::getTargetAirport().length() > 0) {
104     SAVE("target-airport", FGBFI::getTargetAirport());
105   }
106   SAVE("autopilot-altitude-lock", FGBFI::getAPAltitudeLock());
107   SAVE("autopilot-altitude", FGBFI::getAPAltitude());
108   SAVE("autopilot-heading-lock", FGBFI::getAPHeadingLock());
109   SAVE("autopilot-heading", FGBFI::getAPHeading());
110   SAVE("autopilot-gps-lock", FGBFI::getGPSLock());
111   SAVE("autopilot-gps-lat", FGBFI::getGPSTargetLatitude());
112   SAVE("autopilot-gps-lon", FGBFI::getGPSTargetLongitude());
113
114   //
115   // Environment.
116   //
117   SAVE("visibility", FGBFI::getVisibility());
118
119   return true;
120 }
121
122
123 /**
124  * Restore the current state of the simulator from a stream.
125  */
126 bool
127 fgLoadFlight (istream &input)
128 {
129 //   FGInterface * f = current_aircraft.fdm_state;
130   string text;
131   double n;
132   long int i;
133
134   if (!input.good() || input.eof()) {
135     cout << "Stream is no good!\n";
136     return false;
137   }
138
139   input >> text;
140   if (text != "#!fgfs") {
141     cerr << "Bad save file format!\n";
142     return false;
143   }
144
145
146   while (input.good() && !input.eof()) {
147     input >> text;
148
149     //
150     // Simulation.
151     //
152
153     if (text == "flight-model:") {
154       input >> i;
155       cout << "flight model is " << i << endl;
156       FGBFI::setFlightModel(i);
157     }
158
159     else if (text == "time:") {
160       input >> i;
161       cout << "saved time is " << i << endl;
162       FGBFI::setTimeGMT(i);
163     }
164
165     else if (text == "hud:") {
166       input >> i;
167       cout << "hud status is " << i << endl;
168       FGBFI::setHUDVisible(i);
169     }
170
171     else if (text == "panel:") {
172       input >> i;
173       cout << "panel status is " << i << endl;
174       FGBFI::setPanelVisible(i);
175     }
176
177     //
178     // Location
179     //
180       
181     else if (text == "latitude:") {
182       input >> n;
183       cout << "latitude is " << n << endl;
184       FGBFI::setLatitude(n);
185     } 
186
187     else if (text == "longitude:") {
188       input >> n;
189       cout << "longitude is " << n << endl;
190       FGBFI::setLongitude(n);
191     } 
192
193     else if (text == "altitude:") {
194       input >> n;
195       cout << "altitude is " << n << endl;
196       FGBFI::setAltitude(n);
197     } 
198
199     //
200     // Orientation
201     //
202
203     else if (text == "heading:") {
204       input >> n;
205       cout << "heading is " << n << endl;
206       FGBFI::setHeading(n);
207     } 
208
209     else if (text == "pitch:") {
210       input >> n;
211       cout << "pitch is " << n << endl;
212       FGBFI::setPitch(n);
213     } 
214
215     else if (text == "roll:") {
216       input >> n;
217       cout << "roll is " << n << endl;
218       FGBFI::setRoll(n);
219     } 
220
221     //
222     // Velocities
223     //
224
225     else if (text == "speed-north:") {
226       input >> n;
227       cout << "speed north is " << n << endl;
228       FGBFI::setSpeedNorth(n);
229     } 
230
231     else if (text == "speed-east:") {
232       input >> n;
233       cout << "speed east is " << n << endl;
234       FGBFI::setSpeedEast(n);
235     } 
236
237     else if (text == "speed-down:") {
238       input >> n;
239       cout << "speed down is " << n << endl;
240       FGBFI::setSpeedDown(n);
241     } 
242
243     //
244     // Primary controls
245     //
246
247     else if (text == "elevator:") {
248       input >> n;
249       cout << "elevator is " << n << endl;
250       FGBFI::setElevator(n);
251     }
252
253     else if (text == "aileron:") {
254       input >> n;
255       cout << "aileron is " << n << endl;
256       FGBFI::setAileron(n);
257     }
258
259     else if (text == "rudder:") {
260       input >> n;
261       cout << "rudder is " << n << endl;
262       FGBFI::setRudder(n);
263     }
264
265                                 // FIXME: assumes single engine
266     else if (text == "throttle:") {
267       input >> n;
268       cout << "throttle is " << n << endl;
269       FGBFI::setThrottle(n);
270     }
271
272     //
273     // Secondary controls
274
275     else if (text == "elevator-trim:") {
276       input >> n;
277       cout << "elevator trim is " << n << endl;
278       FGBFI::setElevatorTrim(n);
279     }
280
281     else if (text == "flaps:") {
282       input >> n;
283       cout << "flaps are " << n << endl;
284       FGBFI::setFlaps(n);
285     }
286
287     else if (text == "brake:") {
288       input >> n;
289       cout << "brake is " << n << endl;
290       FGBFI::setBrake(n);
291     }
292
293     //
294     // Navigation.
295     //
296
297     else if (text == "target-airport:") {
298       input >> text;
299       cout << "target airport is " << text << endl;
300       FGBFI::setTargetAirport(text);
301     }
302
303     else if (text == "autopilot-altitude-lock:") {
304       input >> i;
305       cout << "autopilot altitude lock is " << i << endl;
306       FGBFI::setAPAltitudeLock(i);
307     }
308
309     else if (text == "autopilot-altitude:") {
310       input >> n;
311       cout << "autopilot altitude is " << n << endl;
312       FGBFI::setAPAltitude(n);
313     }
314
315     else if (text == "autopilot-heading-lock:") {
316       input >> i;
317       cout << "autopilot heading lock is " << i << endl;
318       FGBFI::setAPHeadingLock(i);
319     }
320
321     else if (text == "autopilot-heading:") {
322       input >> n;
323       cout << "autopilot heading is " << n << endl;
324       FGBFI::setAPHeading(n);
325     }
326
327     else if (text == "autopilot-gps-lock:") {
328       input >> i;
329       cout << "autopilot GPS lock is " << i << endl;
330       FGBFI::setGPSLock(i);
331     }
332
333     else if (text == "autopilot-gps-lat:") {
334       input >> n;
335       cout << "GPS target latitude is " << n << endl;
336       FGBFI::setGPSTargetLatitude(n);
337     }
338
339     else if (text == "autopilot-gps-lon:") {
340       input >> n;
341       cout << "GPS target longitude is " << n << endl;
342       FGBFI::setGPSTargetLongitude(n);
343     }
344
345     //
346     // Environment.
347     //
348
349     else if (text == "visibility:") {
350       input >> n;
351       cout << "visibility is " << n << endl;
352       FGBFI::setVisibility(n);
353     }
354
355     //
356     // Don't die if we don't recognize something
357     //
358     
359     else {
360       cerr << "Skipping unknown field: " << text << endl;
361       input >> text;
362     }
363   }
364
365   FGBFI::update();
366
367   return true;
368 }
369
370 // end of save.cxx