1
0
Fork 0

Make PerformanceDB a real subsystem

This commit is contained in:
James Turner 2015-12-19 00:29:00 -08:00
parent e80fc563e5
commit 83ea6d32d0
5 changed files with 100 additions and 30 deletions

View file

@ -93,7 +93,11 @@ FGAIAircraft::FGAIAircraft(FGAISchedule *ref) :
needsTaxiClearance = false; needsTaxiClearance = false;
_needsGroundElevation = true; _needsGroundElevation = true;
_performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB PerformanceDB* perfDB = globals->get_subsystem<PerformanceDB>();
if (perfDB) {
_performance = perfDB->getDefaultPerformance();
}
dt = 0; dt = 0;
takeOffStatus = 0; takeOffStatus = 0;
@ -144,8 +148,10 @@ void FGAIAircraft::unbind()
void FGAIAircraft::setPerformance(const std::string& acType, const std::string& acclass) void FGAIAircraft::setPerformance(const std::string& acType, const std::string& acclass)
{ {
static PerformanceDB perfdb; //TODO make it a global service PerformanceDB* perfDB = globals->get_subsystem<PerformanceDB>();
_performance = perfdb.getDataFor(acType, acclass); if (perfDB) {
_performance = perfDB->getDataFor(acType, acclass);
}
} }
#if 0 #if 0

View file

@ -6,10 +6,11 @@
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <simgear/sg_inlines.h>
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/props/props.hxx> #include <simgear/props/props.hxx>
#include <simgear/props/props_io.hxx> #include <simgear/props/props_io.hxx>
#include <simgear/xml/easyxml.hxx> #include <simgear/structure/exception.hxx>
#include <Main/globals.hxx> #include <Main/globals.hxx>
#include <iostream> #include <iostream>
@ -22,17 +23,41 @@ using std::cerr;
PerformanceDB::PerformanceDB() PerformanceDB::PerformanceDB()
{ {
SGPath dbpath( globals->get_fg_root() );
dbpath.append( "/AI/Aircraft/" );
dbpath.append( "performancedb.xml");
load(dbpath);
} }
PerformanceDB::~PerformanceDB() PerformanceDB::~PerformanceDB()
{} {
}
void PerformanceDB::init()
{
SGPath dbpath( globals->get_fg_root() );
dbpath.append( "/AI/Aircraft/" );
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;
}
_db.clear();
_aliases.clear();
}
void PerformanceDB::update(double dt)
{
SG_UNUSED(dt);
suspend();
}
void PerformanceDB::registerPerformanceData(const std::string& id, PerformanceData* data) { void PerformanceDB::registerPerformanceData(const std::string& id, PerformanceData* data) {
//TODO if key exists already replace data "inplace", i.e. copy to existing PerfData instance //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; _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 // first, try with the specific aircraft type, such as 738 or A322
if (_db.find(acType) != _db.end()) { PerformanceDataDict::const_iterator it;
return _db[acType]; it = _db.find(acType);
if (it != _db.end()) {
return it->second;
} }
const string& alias = findAlias(acType); const string& alias = findAlias(acType);
if (_db.find(alias) != _db.end()) { it = _db.find(alias);
return _db[alias]; if (it != _db.end()) {
return it->second;
} }
SG_LOG(SG_AI, SG_INFO, "no performance data for " << acType); SG_LOG(SG_AI, SG_INFO, "no performance data for " << acType);
if (_db.find(acClass) == _db.end()) { it = _db.find(acClass);
return _db["jet_transport"]; 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) void PerformanceDB::load(const SGPath& filename)

View file

@ -8,6 +8,8 @@
class PerformanceData; class PerformanceData;
class SGPath; class SGPath;
#include <simgear/structure/subsystem_mgr.hxx>
/** /**
* Registry for performance data. * Registry for performance data.
* *
@ -17,24 +19,36 @@ class SGPath;
* @author Thomas F<EFBFBD>rster <t.foerster@biologie.hu-berlin.de> * @author Thomas F<EFBFBD>rster <t.foerster@biologie.hu-berlin.de>
*/ */
//TODO provide std::map interface? //TODO provide std::map interface?
class PerformanceDB class PerformanceDB : public SGSubsystem
{ {
public: public:
PerformanceDB(); PerformanceDB();
~PerformanceDB(); virtual ~PerformanceDB();
void registerPerformanceData(const std::string& id, PerformanceData* data); virtual void init();
void registerPerformanceData(const std::string& id, const std::string& filename); 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 * get performance data for an aircraft type / class. Type is specific, eg
* '738' or 'A319'. Class is more generic, such as 'jet_transport'. * '738' or 'A319'. Class is more generic, such as 'jet_transport'.
*/ */
PerformanceData* getDataFor(const std::string& acType, const std::string& acClass); PerformanceData* getDataFor(const std::string& acType, const std::string& acClass) const;
PerformanceData* getDefaultPerformance() const;
static const char* subsystemName() { return "aircraft-performance-db"; }
private:
void load(const SGPath& path); void load(const SGPath& path);
private: void registerPerformanceData(const std::string& id, PerformanceData* data);
std::map<std::string, PerformanceData*> _db;
typedef std::map<std::string, PerformanceData*> PerformanceDataDict;
PerformanceDataDict _db;
const std::string& findAlias(const std::string& acType) const; const std::string& findAlias(const std::string& acType) const;

View file

@ -43,7 +43,6 @@
#include <AIModel/AIAircraft.hxx> #include <AIModel/AIAircraft.hxx>
#include <AIModel/AIFlightPlan.hxx> #include <AIModel/AIFlightPlan.hxx>
#include <AIModel/performancedata.hxx> #include <AIModel/performancedata.hxx>
#include <AIModel/performancedb.hxx>
#include <ATC/atc_mgr.hxx> #include <ATC/atc_mgr.hxx>
#include <Traffic/TrafficMgr.hxx> #include <Traffic/TrafficMgr.hxx>
#include <Airports/groundnetwork.hxx> #include <Airports/groundnetwork.hxx>

View file

@ -103,6 +103,7 @@
#include <Model/modelmgr.hxx> #include <Model/modelmgr.hxx>
#include <AIModel/submodel.hxx> #include <AIModel/submodel.hxx>
#include <AIModel/AIManager.hxx> #include <AIModel/AIManager.hxx>
#include <AIModel/performancedb.hxx>
#include <Navaids/navdb.hxx> #include <Navaids/navdb.hxx>
#include <Navaids/navlist.hxx> #include <Navaids/navlist.hxx>
#include <Scenery/scenery.hxx> #include <Scenery/scenery.hxx>
@ -815,6 +816,8 @@ void fgCreateSubsystems(bool duringReset) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Initialize the ATC subsystem // Initialize the ATC subsystem
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
globals->add_new_subsystem<PerformanceDB>(SGSubsystemMgr::POST_FDM);
globals->add_subsystem("ATC", new FGATCManager, 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 // 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("ai-model", new FGAIManager, SGSubsystemMgr::POST_FDM);
globals->add_subsystem("submodel-mgr", new FGSubmodelMgr, SGSubsystemMgr::POST_FDM); globals->add_subsystem("submodel-mgr", new FGSubmodelMgr, SGSubsystemMgr::POST_FDM);