]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/performancedb.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / src / AIModel / performancedb.cxx
1 #include <simgear/math/SGMath.hxx>
2 #include <simgear/misc/sg_path.hxx>
3 #include <simgear/props/props.hxx>
4 #include <simgear/props/props_io.hxx>
5 #include <simgear/xml/easyxml.hxx>
6
7 #include <Main/globals.hxx>
8 #include <iostream>
9 #include <fstream>
10
11 #include "performancedb.hxx"
12
13 using std::string;
14 using std::cerr;
15
16 PerformanceDB::PerformanceDB()
17 {
18     SGPath dbpath( globals->get_fg_root() );
19     
20
21     dbpath.append( "/AI/Aircraft/" );
22     dbpath.append( "performancedb.xml"); 
23     load(dbpath);
24 }
25
26
27 PerformanceDB::~PerformanceDB()
28 {}
29
30 void PerformanceDB::registerPerformanceData(const std::string& id, PerformanceData* data) {
31     //TODO if key exists already replace data "inplace", i.e. copy to existing PerfData instance
32     // this updates all aircraft currently using the PerfData instance.
33     _db[id] = data;
34 }
35
36 void PerformanceDB::registerPerformanceData(const std::string& id, const std::string& filename) {
37     registerPerformanceData(id, new PerformanceData(filename));
38 }
39
40 PerformanceData* PerformanceDB::getDataFor(const std::string& id) {
41     if (_db.find(id) == _db.end()) // id not found -> return jet_transport data
42         return _db["jet_transport"];
43
44     return _db[id];
45 }
46
47 void PerformanceDB::load(SGPath filename) {
48     string name;
49     double acceleration;
50     double deceleration;
51     double climbRate;
52     double descentRate;
53     double vRotate;
54     double vTakeOff;
55     double vClimb;
56     double vCruise;
57     double vDescent;
58     double vApproach;
59     double vTouchdown;
60     double vTaxi;
61     SGPropertyNode root;
62     try {
63         readProperties(filename.str(), &root);
64     } catch (const sg_exception &) {
65         SG_LOG(SG_GENERAL, SG_ALERT,
66             "Error reading AI aircraft performance database: " << filename.str());
67         return;
68     }
69
70     SGPropertyNode * node = root.getNode("performancedb");
71     for (int i = 0; i < node->nChildren(); i++) { 
72         SGPropertyNode * db_node = node->getChild(i);
73             name         = db_node->getStringValue("type", "heavy_jet");
74             acceleration = db_node->getDoubleValue("acceleration-kts-hour", 4.0);
75             deceleration = db_node->getDoubleValue("deceleration-kts-hour", 2.0);
76             climbRate    = db_node->getDoubleValue("climbrate-fpm", 3000.0);
77             descentRate  = db_node->getDoubleValue("decentrate-fpm", 1500.0);
78             vRotate      = db_node->getDoubleValue("rotate-speed-kts", 150.0);
79             vTakeOff     = db_node->getDoubleValue("takeoff-speed-kts", 160.0);
80             vClimb       = db_node->getDoubleValue("climb-speed-kts", 300.0);
81             vCruise      = db_node->getDoubleValue("cruise-speed-kts", 430.0);
82             vDescent     = db_node->getDoubleValue("decent-speed-kts", 300.0);
83             vApproach    = db_node->getDoubleValue("approach-speed-kts", 170.0);
84             vTouchdown   = db_node->getDoubleValue("touchdown-speed-kts", 150.0);
85             vTaxi        = db_node->getDoubleValue("taxi-speed-kts", 15.0);
86
87             registerPerformanceData(name, new PerformanceData(
88                 acceleration, deceleration, climbRate, descentRate, vRotate, vTakeOff, vClimb, vCruise, vDescent, vApproach, vTouchdown, vTaxi));
89     }
90 }
91