]> git.mxchange.org Git - flightgear.git/commitdiff
Aircraft-states feature.
authorJames Turner <zakalawe@mac.com>
Fri, 17 Jun 2016 16:53:35 +0000 (17:53 +0100)
committerRoland Haeder <roland@mxchange.org>
Thu, 22 Sep 2016 21:27:36 +0000 (23:27 +0200)
src/Aircraft/CMakeLists.txt
src/Aircraft/initialstate.cxx [new file with mode: 0644]
src/Aircraft/initialstate.hxx [new file with mode: 0644]
src/Main/fg_init.cxx
src/Main/options.cxx

index 5c379f0149d73a819d3f8799c8b0bc4e3d0ef20f..419923663828cf9a67f4e0987b724b3a3362f1ef 100644 (file)
@@ -5,6 +5,7 @@ set(SOURCES
        replay.cxx
        flightrecorder.cxx
     FlightHistory.cxx
+               initialstate.cxx
        )
 
 set(HEADERS
@@ -12,6 +13,7 @@ set(HEADERS
        replay.hxx
        flightrecorder.hxx
     FlightHistory.hxx
+               initialstate.hxx
        )
 
 
diff --git a/src/Aircraft/initialstate.cxx b/src/Aircraft/initialstate.cxx
new file mode 100644 (file)
index 0000000..240539a
--- /dev/null
@@ -0,0 +1,100 @@
+// initialstate.cxx -- setup initial state of the aircraft
+//
+// Written by James Turner
+//
+// Copyright (C) 2016 James Turner <zakalawe@mac.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+// $Id$
+
+#include "config.h"
+
+#include "initialstate.hxx"
+
+#include <algorithm>
+
+#include <simgear/debug/logstream.hxx>
+#include <simgear/props/props_io.hxx>
+
+#include <Main/fg_props.hxx>
+#include <GUI/MessageBox.hxx>
+
+using namespace simgear;
+
+namespace {
+
+    class NodeValue
+    {
+    public:
+        NodeValue(const std::string& s) : v(s) {}
+        bool operator()(const SGPropertyNode_ptr n) const
+        {
+            return (v == n->getStringValue());
+        }
+    private:
+        std::string v;
+    };
+
+    SGPropertyNode_ptr nodeForState(const std::string& nm)
+    {
+        SGPropertyNode_ptr sim = fgGetNode("/sim");
+        const PropertyList& states = sim->getChildren("state");
+        PropertyList::const_iterator it;
+        for (it = states.begin(); it != states.end(); ++it) {
+            const PropertyList& names = (*it)->getChildren("name");
+            if (std::find_if(names.begin(), names.end(), NodeValue(nm)) != names.end()) {
+                return *it;
+            }
+        }
+
+        return SGPropertyNode_ptr();
+    }
+
+} // of anonymous namespace
+
+namespace flightgear
+{
+
+bool isInitialStateName(const std::string& name)
+{
+    SGPropertyNode_ptr n = nodeForState(name);
+    return n.valid();
+}
+
+void applyInitialState()
+{
+    std::string nm = fgGetString("/sim/aircraft-state");
+    if (nm.empty()) {
+        return;
+    }
+
+    SGPropertyNode_ptr stateNode = nodeForState(nm);
+    if (!stateNode) {
+        SG_LOG(SG_AIRCRAFT, SG_WARN, "missing state node for:" << nm);
+        std::string aircraft = fgGetString("/sim/aircraft");
+        modalMessageBox("Unknown aircraft state",
+                                    "The selected aircraft (" + aircraft + ") does not have a stage '" + nm + "')");
+
+        return;
+    }
+
+    SG_LOG(SG_AIRCRAFT, SG_INFO, "Applying aircraft state:" << nm);
+
+    // copy all overlay properties to the tree
+    copyProperties(stateNode->getChild("overlay"), globals->get_props());
+}
+
+} // of namespace flightgear
\ No newline at end of file
diff --git a/src/Aircraft/initialstate.hxx b/src/Aircraft/initialstate.hxx
new file mode 100644 (file)
index 0000000..d7371c5
--- /dev/null
@@ -0,0 +1,41 @@
+// initialstate.hxx -- setup initial state of the aircraft
+//
+// Written by James Turner
+//
+// Copyright (C) 2016 James Turner <zakalawe@mac.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+//
+// $Id$
+
+
+#ifndef FG_AIRCRAFT_INITIAL_STATE_HXX
+#define FG_AIRCRAFT_INITIAL_STATE_HXX
+
+#include <string>
+
+namespace flightgear
+{
+
+/**
+ * @brief is the supplied name a defined initial-state, or alias of one
+ */
+bool isInitialStateName(const std::string& name);
+
+    void applyInitialState();
+
+} // of namespace flightgear
+
+#endif // FG_AIRCRAFT_INITIAL_STATE_HXX
index 118dd1c12a0f5a448e92f3e5f6e269e081c1f03c..8b724d134a6a059ea328e2738806ba008ca26c9d 100644 (file)
@@ -78,6 +78,8 @@
 #include <Aircraft/controls.hxx>
 #include <Aircraft/replay.hxx>
 #include <Aircraft/FlightHistory.hxx>
+#include <Aircraft/initialstate.hxx>
+
 #include <Airports/runways.hxx>
 #include <Airports/airport.hxx>
 #include <Airports/dynamics.hxx>
@@ -216,7 +218,8 @@ public:
                                         e.getFormattedMessage());
           return false;
         }
-        
+          // apply state after the -set.xml, but before any options are are set
+          flightgear::applyInitialState();
         return true;
       } else {
         SG_LOG(SG_GENERAL, SG_ALERT, "aircraft '" << _searchAircraft << 
@@ -265,7 +268,11 @@ public:
                                     e.getFormattedMessage());
       return false;
     }
-    
+
+      // apply state after the -set.xml, but before any options are are set
+      flightgear::applyInitialState();
+
+
     return true;
   }
   
index 77cba345cb36dd7247fbd0199a789f8950ea9c84..659c052c1b979d0bb9bad24cc25d29b1faa1f985 100644 (file)
@@ -52,6 +52,7 @@
 #include <simgear/misc/strutils.hxx>
 #include <Autopilot/route_mgr.hxx>
 #include <Aircraft/replay.hxx>
+#include <Aircraft/initialstate.hxx>
 
 #include <GUI/gui.h>
 #include <GUI/MessageBox.hxx>
@@ -1601,6 +1602,7 @@ struct OptionDesc {
     {"fdm",                          true,  OPTION_STRING, "/sim/flight-model", false, "", 0 },
     {"aero",                         true,  OPTION_STRING, "/sim/aero", false, "", 0 },
     {"aircraft-dir",                 true,  OPTION_IGNORE,   "", false, "", 0 },
+    {"state",                        true,  OPTION_IGNORE,   "", false, "", 0 },
     {"model-hz",                     true,  OPTION_INT,    "/sim/model-hz", false, "", 0 },
     {"max-fps",                      true,  OPTION_DOUBLE, "/sim/frame-rate-throttle-hz", false, "", 0 },
     {"speed",                        true,  OPTION_DOUBLE, "/sim/speed-up", false, "", 0 },
@@ -2087,6 +2089,12 @@ void Options::initAircraft()
     // or a scenery dir).
     fgSetString("/sim/aircraft-dir", aircraftDirPath.realpath().c_str());
   }
+
+    if (isOptionSet("state")) {
+        std::string stateName = valueForOption("state");
+        // can't validate this until the -set.xml is parsed
+        fgSetString("/sim/aircraft-state", stateName);
+    }
 }
   
 void Options::processArgResult(int result)