]> git.mxchange.org Git - flightgear.git/commitdiff
Make PerformanceDB a real subsystem
authorJames Turner <zakalawe@mac.com>
Sat, 19 Dec 2015 08:29:00 +0000 (00:29 -0800)
committerJames Turner <zakalawe@mac.com>
Sat, 19 Dec 2015 08:29:00 +0000 (00:29 -0800)
src/AIModel/AIAircraft.cxx
src/AIModel/performancedb.cxx
src/AIModel/performancedb.hxx
src/ATC/trafficcontrol.cxx
src/Main/fg_init.cxx

index 285c3fee80f6efe85c3288ac85ae12db8dec696a..c3c1e36af3178de7775b721a6806199d09ac93b3 100644 (file)
@@ -93,7 +93,11 @@ FGAIAircraft::FGAIAircraft(FGAISchedule *ref) :
     needsTaxiClearance = false;
     _needsGroundElevation = true;
 
-    _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB
+    PerformanceDB* perfDB = globals->get_subsystem<PerformanceDB>();
+    if (perfDB) {
+        _performance = perfDB->getDefaultPerformance();
+    }
+
     dt = 0;
     takeOffStatus = 0;
 
@@ -144,8 +148,10 @@ void FGAIAircraft::unbind()
 
 void FGAIAircraft::setPerformance(const std::string& acType, const std::string& acclass)
 {
-  static PerformanceDB perfdb; //TODO make it a global service
-  _performance = perfdb.getDataFor(acType, acclass);
+    PerformanceDB* perfDB = globals->get_subsystem<PerformanceDB>();
+    if (perfDB) {
+        _performance = perfDB->getDataFor(acType, acclass);
+    }
 }
 
 #if 0
index 274e8a103462df49c4efc9feeb8a2115a83761f2..464cd5d5a4036377aa7704c38df749ea705b8c58 100644 (file)
@@ -6,10 +6,11 @@
 
 #include <boost/foreach.hpp>
 
+#include <simgear/sg_inlines.h>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
-#include <simgear/xml/easyxml.hxx>
+#include <simgear/structure/exception.hxx>
 
 #include <Main/globals.hxx>
 #include <iostream>
@@ -22,17 +23,41 @@ using std::cerr;
 
 PerformanceDB::PerformanceDB()
 {
-    SGPath dbpath( globals->get_fg_root() );
-    
+}
+
+
+PerformanceDB::~PerformanceDB()
+{
+}
 
+void PerformanceDB::init()
+{
+    SGPath dbpath( globals->get_fg_root() );
     dbpath.append( "/AI/Aircraft/" );
-    dbpath.append( "performancedb.xml"); 
+    dbpath.append( "performancedb.xml");
     load(dbpath);
+
+    if (getDefaultPerformance() == 0) {
+        SG_LOG(SG_AI, SG_WARN, "PerformanceDB: no default performance data found/loaded");
+    }
 }
 
+void PerformanceDB::shutdown()
+{
+    PerformanceDataDict::iterator it;
+    for (it = _db.begin(); it != _db.end(); ++it) {
+        delete it->second;
+    }
 
-PerformanceDB::~PerformanceDB()
-{}
+    _db.clear();
+    _aliases.clear();
+}
+
+void PerformanceDB::update(double dt)
+{
+    SG_UNUSED(dt);
+    suspend();
+}
 
 void PerformanceDB::registerPerformanceData(const std::string& id, PerformanceData* data) {
     //TODO if key exists already replace data "inplace", i.e. copy to existing PerfData instance
@@ -40,25 +65,48 @@ void PerformanceDB::registerPerformanceData(const std::string& id, PerformanceDa
     _db[id] = data;
 }
 
-PerformanceData* PerformanceDB::getDataFor(const string& acType, const string& acClass)
+PerformanceData* PerformanceDB::getDataFor(const string& acType, const string& acClass) const
 {
   // first, try with the specific aircraft type, such as 738 or A322
-    if (_db.find(acType) != _db.end()) {
-        return _db[acType];
+    PerformanceDataDict::const_iterator it;
+    it = _db.find(acType);
+    if (it != _db.end()) {
+        return it->second;
     }
     
     const string& alias = findAlias(acType);
-    if (_db.find(alias) != _db.end()) {
-      return _db[alias];
+    it = _db.find(alias);
+    if (it != _db.end()) {
+        return it->second;
     }
   
     SG_LOG(SG_AI, SG_INFO, "no performance data for " << acType);
-  
-    if (_db.find(acClass) == _db.end()) {
-        return _db["jet_transport"];
+
+    it = _db.find(acClass);
+    if (it == _db.end()) {
+        return getDefaultPerformance();
     }
-  
-    return _db[acClass];
+
+    return it->second;
+}
+
+PerformanceData* PerformanceDB::getDefaultPerformance() const
+{
+    PerformanceDataDict::const_iterator it = _db.find("jet_transport");
+    if (it == _db.end())
+        return NULL;
+
+    return it->second;
+}
+
+bool PerformanceDB::havePerformanceDataForAircraftType(const std::string& acType) const
+{
+    PerformanceDataDict::const_iterator it = _db.find(acType);
+    if (it != _db.end())
+        return true;
+
+    const std::string alias(findAlias(acType));
+    return (_db.find(alias) != _db.end());
 }
 
 void PerformanceDB::load(const SGPath& filename)
index 43defb44020aa08fcecb31ce5cf650ee7e8f5d64..0e80b28fdacda44b934825e77c29690d120a72e1 100644 (file)
@@ -8,6 +8,8 @@
 class PerformanceData;
 class SGPath;
 
+#include <simgear/structure/subsystem_mgr.hxx>
+
 /**
  * Registry for performance data.
  *
@@ -17,24 +19,36 @@ class SGPath;
  * @author Thomas F�rster <t.foerster@biologie.hu-berlin.de>
 */
 //TODO provide std::map interface?
-class PerformanceDB
+class PerformanceDB : public SGSubsystem
 {
 public:
     PerformanceDB();
-    ~PerformanceDB();
+    virtual ~PerformanceDB();
 
-    void registerPerformanceData(const std::string& id, PerformanceData* data);
-    void registerPerformanceData(const std::string& id, const std::string& filename);
+    virtual void init();
+    virtual void shutdown();
+
+    virtual void update(double dt);
+
+    bool havePerformanceDataForAircraftType(const std::string& acType) const;
 
     /**
      * get performance data for an aircraft type / class. Type is specific, eg
      * '738' or 'A319'. Class is more generic, such as 'jet_transport'.
      */
-    PerformanceData* getDataFor(const std::string& acType, const std::string& acClass);
-    void load(const SGPath& path);
+    PerformanceData* getDataFor(const std::string& acType, const std::string& acClass) const;
+
+    PerformanceData* getDefaultPerformance() const;
 
+    static const char* subsystemName() { return "aircraft-performance-db"; }
 private:
-    std::map<std::string, PerformanceData*> _db;
+    void load(const SGPath& path);
+
+    void registerPerformanceData(const std::string& id, PerformanceData* data);
+
+
+    typedef std::map<std::string, PerformanceData*> PerformanceDataDict;
+    PerformanceDataDict _db;
     
     const std::string& findAlias(const std::string& acType) const;
   
index e86ad1e534f9d16a37d71ce0896f7b3900339a81..f27ca86a1f1c663d798284751f2a175a030d7be0 100644 (file)
@@ -43,7 +43,6 @@
 #include <AIModel/AIAircraft.hxx>
 #include <AIModel/AIFlightPlan.hxx>
 #include <AIModel/performancedata.hxx>
-#include <AIModel/performancedb.hxx>
 #include <ATC/atc_mgr.hxx>
 #include <Traffic/TrafficMgr.hxx>
 #include <Airports/groundnetwork.hxx>
index b5c30e6d663e42eb8b4d351d9fcc909470929093..a5abbb798429fecc8e236af8ec06d183121522a4 100644 (file)
 #include <Model/modelmgr.hxx>
 #include <AIModel/submodel.hxx>
 #include <AIModel/AIManager.hxx>
+#include <AIModel/performancedb.hxx>
 #include <Navaids/navdb.hxx>
 #include <Navaids/navlist.hxx>
 #include <Scenery/scenery.hxx>
@@ -815,6 +816,8 @@ void fgCreateSubsystems(bool duringReset) {
     ////////////////////////////////////////////////////////////////////
    // Initialize the ATC subsystem
     ////////////////////////////////////////////////////////////////////
+
+    globals->add_new_subsystem<PerformanceDB>(SGSubsystemMgr::POST_FDM);
     globals->add_subsystem("ATC", new FGATCManager, SGSubsystemMgr::POST_FDM);
 
     ////////////////////////////////////////////////////////////////////
@@ -826,7 +829,7 @@ void fgCreateSubsystems(bool duringReset) {
     ////////////////////////////////////////////////////////////////////
     // Initialise the AI Model Manager
     ////////////////////////////////////////////////////////////////////
-    SG_LOG(SG_GENERAL, SG_INFO, "  AI Model Manager");
+
     globals->add_subsystem("ai-model", new FGAIManager, SGSubsystemMgr::POST_FDM);
     globals->add_subsystem("submodel-mgr", new FGSubmodelMgr, SGSubsystemMgr::POST_FDM);