]> git.mxchange.org Git - flightgear.git/commitdiff
Added a load/save state function contributed by David Megginson.
authorcurt <curt>
Fri, 11 Feb 2000 22:27:23 +0000 (22:27 +0000)
committercurt <curt>
Fri, 11 Feb 2000 22:27:23 +0000 (22:27 +0000)
src/Main/Makefile.am
src/Main/keyboard.cxx
src/Main/options.hxx
src/Main/save.cxx [new file with mode: 0644]
src/Main/save.hxx [new file with mode: 0644]

index 673113f346b4e6e2f668138cc0595d141f1cfdc4..5fb7798f8a6cfa04cf5463808f0ec00b3dfaa768 100644 (file)
@@ -37,6 +37,7 @@ fgfs_SOURCES = \
        keyboard.cxx keyboard.hxx \
        main.cxx \
        options.cxx options.hxx \
+       save.cxx save.hxx \
        splash.cxx splash.hxx \
        views.cxx views.hxx
 
index a1d4f401b9a38e8ec9cb6e5a103f4b466a5112f7..cdf406825e188005e7660d4e8ddd5a64321756eb 100644 (file)
@@ -63,6 +63,7 @@
 
 #include "keyboard.hxx"
 #include "options.hxx"
+#include "save.hxx"
 #include "views.hxx"
 
 extern void NewAltitude( puObject *cb );
@@ -383,6 +384,27 @@ void GLUTspecialkey(int k, int x, int y) {
     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
        FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
        switch (k) {
+       case GLUT_KEY_F1: {
+           ifstream input("fgfs.sav");
+           if (input.good() && fgLoadFlight(input)) {
+               input.close();
+               FG_LOG(FG_INPUT, FG_INFO, "Restored flight from fgfs.sav");
+           } else {
+               FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from fgfs.sav");
+           }
+           return;
+       }
+       case GLUT_KEY_F2: {
+           FG_LOG(FG_INPUT, FG_INFO, "Saving flight");
+           ofstream output("fgfs.sav");
+           if (output.good() && fgSaveFlight(output)) {
+               output.close();
+               FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
+           } else {
+               FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
+           }
+           return;
+       }
        case GLUT_KEY_END: // numeric keypad 1
            v->set_goal_view_offset( FG_PI * 0.75 );
            return;
index dd32db18115389ff8b388a3264c88e6326516876..1c4d8b37d2a567c6181210d70b098a52a0d7342d 100644 (file)
@@ -271,12 +271,35 @@ public:
     inline string get_net_id() const { return net_id; }
 
     // Update functions
+    inline void set_fg_root (const string value) { fg_root = value; }
     inline void set_airport_id( const string id ) { airport_id = id; }
+    inline void set_lon (double value) { lon = value; }
+    inline void set_lat (double value) { lat = value; }
+    inline void set_altitude (double value) { altitude = value; }
+    inline void set_heading (double value) { heading = value; }
+    inline void set_roll (double value) { roll = value; }
+    inline void set_pitch (double value) { pitch = value; }
+    inline void set_uBody (double value) { uBody = value; }
+    inline void set_vBody (double value) { vBody = value; }
+    inline void set_wBody (double value) { wBody = value; }
+    inline void set_game_mode (bool value) { game_mode = value; }
+    inline void set_splash_screen (bool value) { splash_screen = value; }
+    inline void set_intro_music (bool value) { intro_music = value; }
+    inline void set_mouse_pointer (int value) { mouse_pointer = value; }
+    inline void set_pause (bool value) { pause = value; }
     inline void set_hud_status( bool status ) { hud_status = status; }
+    inline void set_sound (bool value) { sound = value; }
+    inline void set_flight_model (int value) { flight_model = value; }
+    inline void set_model_hz (int value) { model_hz = value; }
+    inline void set_fog (fgFogKind value) { fog = value; }
     inline void set_clouds( bool value ) { clouds = value; }
     inline void set_clouds_asl( double value ) { clouds_asl = value; }
     inline void set_fov( double amount ) { fov = amount; }
+    inline void set_fullscreen (bool value) { fullscreen = value; }
+    inline void set_shading (int value) { shading = value; }
+    inline void set_skyblend (bool value) { skyblend = value; }
     inline void set_textures( bool status ) { textures = status; }
+    inline void set_wireframe (bool status) { wireframe = status; }
     inline void cycle_fog( void ) { 
        if ( fog == FG_FOG_DISABLED ) {
            fog = FG_FOG_FASTEST;
@@ -291,6 +314,13 @@ public:
     void toggle_panel();
     inline void set_xsize( int x ) { xsize = x; }
     inline void set_ysize( int y ) { ysize = y; }
+    inline void set_view_mode (fgViewMode value) { view_mode = value; }
+    inline void set_tile_radius (int value) { tile_radius = value; }
+    inline void set_tile_diameter (int value) { tile_diameter = value; }
+    inline void set_units (int value) { units = value; }
+    inline void set_tris_or_culled (int value) { tris_or_culled = value; }
+    inline void set_time_offset (int value) { time_offset = value; }
+    inline void set_time_offset_type (int value) { time_offset_type = value; }
     inline void cycle_view_mode() { 
        if ( view_mode == FG_VIEW_PILOT ) {
            view_mode = FG_VIEW_FOLLOW;
diff --git a/src/Main/save.cxx b/src/Main/save.cxx
new file mode 100644 (file)
index 0000000..69f3b01
--- /dev/null
@@ -0,0 +1,428 @@
+// save.cxx -- class to save and restore a flight.
+//
+// Written by Curtis Olson, started November 1999.
+//
+// Copyright (C) 1999  David Megginson - david@megginson.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+// $Id$
+
+
+/*
+CHANGES:
+- time is now working
+- autopilot is working (even with GPS)
+
+TODO:
+- use a separate options object so that we can roll back on error
+- use proper FGFS logging
+- add view direction, and other stuff
+*/
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <iostream>
+
+#include <Include/fg_types.hxx>
+#include <Include/fg_constants.h>
+#include <Aircraft/aircraft.hxx>
+#include <Controls/controls.hxx>
+#include <Autopilot/autopilot.hxx>
+#include <Time/fg_time.hxx>
+#ifndef FG_OLD_WEATHER
+#  include <WeatherCM/FGLocalWeatherDatabase.h>
+#else
+#  include <Weather/weather.hxx>
+#endif
+
+
+#include "options.hxx"
+#include "save.hxx"
+#include "fg_init.hxx"
+
+FG_USING_NAMESPACE(std);
+
+                               // FIXME: these are not part of the
+                               // published interface!!!
+extern fgAPDataPtr APDataGlobal;
+extern void fgAPAltitudeSet (double new_altitude);
+extern void fgAPHeadingSet (double new_heading);
+
+#define SAVE(name, value) { output << name << ": " << value << endl; }
+
+/**
+ * Save the current state of the simulator to a stream.
+ */
+bool
+fgSaveFlight (ostream &output)
+{
+  char tb[100];
+  const FGInterface * f = current_aircraft.fdm_state;
+  struct tm *t = FGTime::cur_time_params->getGmt();
+
+  output << "#!fgfs" << endl;
+
+  //
+  // Simulation
+  //
+  SAVE("flight-model", current_options.get_flight_model());
+  SAVE("time", mktime(t));
+  SAVE("hud", current_options.get_hud_status());
+  SAVE("panel", current_options.get_panel_status());
+
+  // 
+  // Location
+  //
+  SAVE("latitude", (f->get_Latitude() * RAD_TO_DEG));
+  SAVE("longitude", (f->get_Longitude() * RAD_TO_DEG));
+  SAVE("altitude", f->get_Altitude());
+
+  //
+  // Orientation
+  //
+  SAVE("heading", (f->get_Psi() * RAD_TO_DEG));
+  SAVE("pitch", (f->get_Theta() * RAD_TO_DEG));
+  SAVE("roll", (f->get_Phi() * RAD_TO_DEG));
+
+  //
+  // Velocities
+  //
+  SAVE("speed-north", f->get_V_north());
+  SAVE("speed-east", f->get_V_east());
+  SAVE("speed-down", f->get_V_down());
+
+  //
+  // Primary controls
+  //
+  SAVE("elevator", controls.get_elevator());
+  SAVE("aileron", controls.get_aileron());
+  SAVE("rudder", controls.get_rudder());
+                               // FIXME: save each throttle separately
+  SAVE("throttle", controls.get_throttle(0));
+
+  //
+  // Secondary controls
+  //
+  SAVE("elevator-trim", controls.get_elevator_trim());
+  SAVE("flaps", controls.get_flaps());
+                               // FIXME: save each brake separately
+  SAVE("brake", controls.get_brake(0));
+
+  //
+  // Navigation.
+  //
+  if (current_options.get_airport_id().length() > 0) {
+    SAVE("target-airport", current_options.get_airport_id());
+  }
+  SAVE("autopilot-altitude-lock", fgAPAltitudeEnabled());
+  SAVE("autopilot-altitude", fgAPget_TargetAltitude() * METER_TO_FEET);
+  SAVE("autopilot-heading-lock", fgAPHeadingEnabled());
+  SAVE("autopilot-heading", fgAPget_TargetHeading());
+  SAVE("autopilot-gps-lock", fgAPWayPointEnabled());
+  SAVE("autopilot-gps-lat", fgAPget_TargetLatitude());
+  SAVE("autopilot-gps-lon", fgAPget_TargetLongitude());
+
+  //
+  // Environment.
+  //
+#ifndef FG_OLD_WEATHER
+  SAVE("visibility", WeatherDatabase->getWeatherVisibility());
+#else
+  SAVE("visibility", current_weather.get_visibility());
+#endif
+
+  return true;
+}
+
+
+/**
+ * Restore the current state of the simulator from a stream.
+ */
+bool
+fgLoadFlight (istream &input)
+{
+  FGInterface * f = current_aircraft.fdm_state;
+  string text;
+  double n;
+  long int i;
+
+  double elevator = controls.get_elevator();
+  double aileron = controls.get_aileron();
+  double rudder = controls.get_rudder();
+  double throttle = controls.get_throttle(0);
+  double elevator_trim = controls.get_elevator_trim();
+  double flaps = controls.get_flaps();
+  double brake =  controls.get_brake(FGControls::ALL_WHEELS);
+
+  bool ap_heading_lock = false;
+  double ap_heading = 0;
+  bool ap_altitude_lock = false;
+  double ap_altitude = 0;
+  bool ap_gps_lock = false;
+  double ap_gps_lat = 0;
+  double ap_gps_lon = 0;
+
+  string airport_id = current_options.get_airport_id();
+  current_options.set_airport_id("");
+  current_options.set_time_offset(0);
+  current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_OFFSET);
+
+  if (!input.good() || input.eof()) {
+    cout << "Stream is no good!\n";
+    return false;
+  }
+
+  input >> text;
+  if (text != "#!fgfs") {
+    printf("Bad save file format!\n");
+    return false;
+  }
+
+
+  while (input.good() && !input.eof()) {
+    input >> text;
+
+    //
+    // Simulation.
+    //
+
+    if (text == "flight-model:") {
+      input >> i;
+      cout << "flight model is " << i << endl;
+      current_options.set_flight_model(i);
+    }
+
+    else if (text == "time:") {
+      input >> i;
+      cout << "saved time is " << i << endl;
+      current_options.set_time_offset(i);
+      current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_ABSOLUTE);
+      FGTime::cur_time_params->init(*cur_fdm_state);
+      FGTime::cur_time_params->update(*cur_fdm_state);
+    }
+
+    else if (text == "hud:") {
+      input >> i;
+      cout << "hud status is " << i << endl;
+      current_options.set_hud_status(i);
+    }
+
+    else if (text == "panel:") {
+      input >> i;
+      cout << "panel status is " << i << endl;
+      if (current_options.get_panel_status() != i) {
+       current_options.toggle_panel();
+      }
+    }
+
+    //
+    // Location
+    //
+      
+    else if (text == "latitude:") {
+      input >> n;
+      cout << "latitude is " << n << endl;
+      current_options.set_lat(n);
+    } else if (text == "longitude:") {
+      input >> n;
+      cout << "longitude is " << n << endl;
+      current_options.set_lon(n);
+    } else if (text == "altitude:") {
+      input >> n;
+      cout << "altitude is " << n << endl;
+      current_options.set_altitude(n * FEET_TO_METER);
+    } 
+
+    //
+    // Orientation
+    //
+
+    else if (text == "heading:") {
+      input >> n;
+      cout << "heading is " << n << endl;
+      current_options.set_heading(n);
+    } else if (text == "pitch:") {
+      input >> n;
+      cout << "pitch is " << n << endl;
+      current_options.set_pitch(n);
+    } else if (text == "roll:") {
+      input >> n;
+      cout << "roll is " << n << endl;
+      current_options.set_roll(n);
+    } 
+
+    //
+    // Velocities
+    //
+
+    else if (text == "speed-north:") {
+      input >> n;
+      cout << "speed north is " << n << endl;
+      current_options.set_uBody(n);
+    } else if (text == "speed-east:") {
+      input >> n;
+      cout << "speed east is " << n << endl;
+      current_options.set_vBody(n);
+    } else if (text == "speed-down:") {
+      input >> n;
+      cout << "speed down is " << n << endl;
+      current_options.set_wBody(n);
+    } 
+
+    //
+    // Primary controls
+    //
+
+    else if (text == "elevator:") {
+      input >> elevator;
+      cout << "elevator is " << elevator << endl;
+    }
+
+    else if (text == "aileron:") {
+      input >> aileron;
+      cout << "aileron is " << aileron << endl;
+    }
+
+    else if (text == "rudder:") {
+      input >> rudder;
+      cout << "rudder is " << rudder << endl;
+    }
+
+                               // FIXME: assumes single engine
+    else if (text == "throttle:") {
+      input >> throttle;
+      cout << "throttle is " << throttle << endl;
+    }
+
+    //
+    // Secondary controls
+
+    else if (text == "elevator-trim:") {
+      input >> elevator_trim;
+      cout << "elevator trim is " << elevator_trim << endl;
+    }
+
+    else if (text == "flaps:") {
+      input >> flaps;
+      cout << "flaps are " << flaps << endl;
+    }
+
+    else if (text == "brake:") {
+      input >> brake;
+      cout << "brake is " << brake << endl;
+    }
+
+    //
+    // Navigation.
+    //
+
+    else if (text == "target-airport:") {
+      input >> airport_id;
+      cout << "target airport is " << airport_id << endl;
+    }
+
+    else if (text == "autopilot-altitude-lock:") {
+      input >> ap_altitude_lock;
+      cout << "autopilot altitude lock is " << ap_altitude_lock << endl;
+    }
+
+    else if (text == "autopilot-altitude:") {
+      input >> ap_altitude;
+      cout << "autopilot altitude is " << ap_altitude << endl;
+    }
+
+    else if (text == "autopilot-heading-lock:") {
+      input >> ap_heading_lock;
+      cout << "autopilot heading lock is " << ap_heading_lock << endl;
+    }
+
+    else if (text == "autopilot-heading:") {
+      input >> ap_heading;
+      cout << "autopilot heading is " << ap_heading << endl;
+    }
+
+    else if (text == "autopilot-gps-lock:") {
+      input >> ap_gps_lock;
+      cout << "autopilot GPS lock is " << ap_gps_lock << endl;
+    }
+
+    else if (text == "autopilot-gps-lat:") {
+      input >> ap_gps_lat;
+    }
+
+    else if (text == "autopilot-gps-lon:") {
+      input >> ap_gps_lon;
+    }
+
+    //
+    // Environment.
+    //
+
+    else if (text == "visibility:") {
+      input >> n;
+      cout << "visibility is " << n << endl;
+      
+#ifndef FG_OLD_WEATHER
+      WeatherDatabase->setWeatherVisibility(n);
+#else
+      current_weather.set_visibility(n);
+#endif
+    }
+
+    //
+    // Don't die if we don't recognize something
+    //
+    
+    else {
+      cerr << "Skipping unknown field: " << text << endl;
+      input >> text;
+    }
+  }
+
+  fgReInitSubsystems();
+
+                               // Set airport and controls after the
+                               // re-init.
+  current_options.set_airport_id(airport_id);
+
+                               // The controls have to be set after
+                               // the reinit
+  controls.set_elevator(elevator);
+  controls.set_aileron(aileron);
+  controls.set_rudder(rudder);
+  controls.set_throttle(FGControls::ALL_ENGINES, throttle);
+  controls.set_elevator_trim(elevator_trim);
+  controls.set_flaps(flaps);
+  controls.set_brake(FGControls::ALL_WHEELS, brake);
+
+                               // Ditto for the autopilot.
+                               // FIXME: shouldn't have to use
+                               // APDataGlobal.
+  APDataGlobal->heading_hold = ap_heading_lock;
+  APDataGlobal->altitude_hold = ap_altitude_lock;
+  fgAPHeadingSet(ap_heading);
+  fgAPAltitudeSet(ap_altitude);
+                               // GPS overrides heading
+  APDataGlobal->waypoint_hold = ap_gps_lock;
+  APDataGlobal->TargetLatitude = ap_gps_lat;
+  APDataGlobal->TargetLongitude = ap_gps_lon;
+
+  return true;
+}
+
+// end of save.cxx
diff --git a/src/Main/save.hxx b/src/Main/save.hxx
new file mode 100644 (file)
index 0000000..38624b0
--- /dev/null
@@ -0,0 +1,44 @@
+// save.hxx -- class to save and restore a flight.
+//
+// Written by Curtis Olson, started November 1999.
+//
+// Copyright (C) 1999  David Megginson - david@megginson.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+// $Id$
+
+#ifndef _SAVE_HXX
+#define _SAVE_HXX
+
+#ifndef __cplusplus
+# error This library requires C++
+#endif                                   
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <Include/fg_types.hxx>
+#include <iostream>
+
+FG_USING_NAMESPACE(std);
+
+extern bool fgSaveFlight (ostream &output);
+extern bool fgLoadFlight (istream &input);
+
+#endif __SAVE_HXX
+
+// end of save.hxx