From: James Turner Date: Sat, 19 Dec 2015 08:29:00 +0000 (-0800) Subject: Make PerformanceDB a real subsystem X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=83ea6d32d0504f5a9bd4730ea35275cfc1954392;p=flightgear.git Make PerformanceDB a real subsystem --- diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index 285c3fee8..c3c1e36af 100644 --- a/src/AIModel/AIAircraft.cxx +++ b/src/AIModel/AIAircraft.cxx @@ -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(); + 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(); + if (perfDB) { + _performance = perfDB->getDataFor(acType, acclass); + } } #if 0 diff --git a/src/AIModel/performancedb.cxx b/src/AIModel/performancedb.cxx index 274e8a103..464cd5d5a 100644 --- a/src/AIModel/performancedb.cxx +++ b/src/AIModel/performancedb.cxx @@ -6,10 +6,11 @@ #include +#include #include #include #include -#include +#include #include
#include @@ -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) diff --git a/src/AIModel/performancedb.hxx b/src/AIModel/performancedb.hxx index 43defb440..0e80b28fd 100644 --- a/src/AIModel/performancedb.hxx +++ b/src/AIModel/performancedb.hxx @@ -8,6 +8,8 @@ class PerformanceData; class SGPath; +#include + /** * Registry for performance data. * @@ -17,24 +19,36 @@ class SGPath; * @author Thomas F�rster */ //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 _db; + void load(const SGPath& path); + + void registerPerformanceData(const std::string& id, PerformanceData* data); + + + typedef std::map PerformanceDataDict; + PerformanceDataDict _db; const std::string& findAlias(const std::string& acType) const; diff --git a/src/ATC/trafficcontrol.cxx b/src/ATC/trafficcontrol.cxx index e86ad1e53..f27ca86a1 100644 --- a/src/ATC/trafficcontrol.cxx +++ b/src/ATC/trafficcontrol.cxx @@ -43,7 +43,6 @@ #include #include #include -#include #include #include #include diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index b5c30e6d6..a5abbb798 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -103,6 +103,7 @@ #include #include #include +#include #include #include #include @@ -815,6 +816,8 @@ void fgCreateSubsystems(bool duringReset) { //////////////////////////////////////////////////////////////////// // Initialize the ATC subsystem //////////////////////////////////////////////////////////////////// + + globals->add_new_subsystem(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);