]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIScenario.cxx
Replace the data/Airports/basic.dat.gz and data/Airports/runways.dat.gz with
[flightgear.git] / src / AIModel / AIScenario.cxx
1 // FGAIScenario.cxx - class for loading an AI scenario
2 // Written by David Culp, started May 2004
3 // - davidculp2@comcast.net
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 #include <cstdio>
20
21 #include <simgear/misc/sg_path.hxx>
22 #include <simgear/debug/logstream.hxx>
23 #include <simgear/structure/exception.hxx>
24 #include <simgear/constants.h>
25 #ifdef __BORLANDC__
26 #  define exception c_exception
27 #endif
28 #include <simgear/props/props.hxx>
29
30 #include <Main/globals.hxx>
31 #include <Main/fg_props.hxx>
32
33 #include "AIScenario.hxx"
34 #include "AIFlightPlan.hxx"
35
36 static list<string>
37 getAllNodeVals(const char* name, SGPropertyNode * entry_node);
38
39 FGAIScenario::FGAIScenario(string &filename)
40 {
41   int i;
42   SGPath path( globals->get_fg_root() );
43   
44   cout << "/Data/AI/" << filename << endl;
45   
46   path.append( ("/Data/AI/" + filename + ".xml").c_str() );
47   SGPropertyNode root;
48   readProperties(path.str(), &root);
49   
50   cout <<"path " << path.str() << endl;
51   
52   try {
53       readProperties(path.str(), &root);
54   } catch (const sg_exception &e) {
55       SG_LOG(SG_GENERAL, SG_ALERT,
56        "Incorrect path specified for AI scenario: ");
57        
58        cout << path.str() << endl;
59       
60       return;
61   }
62
63   entries.clear();
64   SGPropertyNode * node = root.getNode("scenario");
65   for (i = 0; i < node->nChildren(); i++) { 
66      
67      cout << "Reading entity data entry " << i << endl;        
68      
69      SGPropertyNode * entry_node = node->getChild(i);
70
71      FGAIModelEntity* en = new FGAIModelEntity;
72      en->callsign       = entry_node->getStringValue("callsign", "none");
73      en->m_type         = entry_node->getStringValue("type", "aircraft");
74      en->m_class        = entry_node->getStringValue("class", "jet_transport");
75      en->path           = entry_node->getStringValue("model", "Models/Geometry/glider.ac");
76      en->flightplan     = entry_node->getStringValue("flightplan", "");
77      en->repeat         = entry_node->getDoubleValue("repeat", 0.0); 
78      en->latitude       = entry_node->getDoubleValue("latitude", 0.0); 
79      en->longitude      = entry_node->getDoubleValue("longitude", 0.0); 
80      en->altitude       = entry_node->getDoubleValue("altitude", 0.0); 
81      en->speed          = entry_node->getDoubleValue("speed", 0.0); 
82      en->heading        = entry_node->getDoubleValue("heading", 0.0); 
83      en->roll           = entry_node->getDoubleValue("roll", 0.0); 
84      en->azimuth        = entry_node->getDoubleValue("azimuth", 0.0); 
85      en->elevation      = entry_node->getDoubleValue("elevation", 0.0); 
86      en->rudder         = entry_node->getDoubleValue("rudder", 0.0);
87      en->strength       = entry_node->getDoubleValue("strength-fps", 0.0);
88      en->diameter       = entry_node->getDoubleValue("diameter-ft", 0.0);
89      en->height_msl     = entry_node->getDoubleValue("height-msl", 5000.0);
90      en->eda            = entry_node->getDoubleValue("eda", 0.007);
91      en->life           = entry_node->getDoubleValue("life", 900.0);
92      en->buoyancy       = entry_node->getDoubleValue("buoyancy", 0);
93      en->wind_from_east = entry_node->getDoubleValue("wind_from_east", 0);
94      en->wind_from_north = entry_node->getDoubleValue("wind_from_north", 0);
95      en->wind            = entry_node->getBoolValue  ("wind", false);
96      en->cd              = entry_node->getDoubleValue("cd", 0.029); 
97      en->mass            = entry_node->getDoubleValue("mass", 0.007); 
98      en->radius          = entry_node->getDoubleValue("turn-radius-ft", 2000);
99      en->x_offset        = entry_node->getDoubleValue("x-offset-m", 5.5); 
100      en->y_offset        = entry_node->getDoubleValue("y-offset-m", 1.0); 
101      en->z_offset        = entry_node->getDoubleValue("z-offset-m", 1.0); 
102  /*  en->name            = entry_node->getStringValue("name", "");*/
103      en->wire_objects     = getAllNodeVals("wire", entry_node);
104      en->catapult_objects = getAllNodeVals("catapult", entry_node);
105      en->solid_objects    = getAllNodeVals("solid", entry_node);
106
107      en->fp             = NULL;
108      if (en->flightplan != ""){
109         en->fp = new FGAIFlightPlan( en->flightplan );
110      }
111      entries.push_back( en );
112    }
113
114   entry_iterator = entries.begin();
115   //cout << entries.size() << " entries read." << endl;
116 }
117
118
119 FGAIScenario::~FGAIScenario()
120 {
121   entries.clear();
122 }
123
124
125 FGAIModelEntity*
126 FGAIScenario::getNextEntry( void )
127 {
128   if (entries.size() == 0) return 0;
129   if (entry_iterator != entries.end()) {
130     return *entry_iterator++;
131   } else {
132     return 0;
133   }
134 }
135
136 int FGAIScenario::nEntries( void )
137 {
138   return entries.size();
139 }
140
141 static list<string>
142 getAllNodeVals(const char* name, SGPropertyNode * entry_node)
143 {
144   list<string> retval;
145   int i=0;
146   do {
147     char nodename[100];
148     snprintf(nodename, sizeof(nodename), "%s[%d]", name, i);
149     const char* objname = entry_node->getStringValue(nodename, 0);
150     if (objname == 0)
151       return retval;
152
153     retval.push_back(string(objname));
154     ++i;
155   } while (1);
156
157   return retval;
158 }
159
160
161 // end scenario.cxx
162