]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIScenario.cxx
8f4e22b8c7c8cc114b17b9e8056c6a171ffe9b41
[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 getAllStringNodeVals(const char* name, SGPropertyNode * entry_node);
38 static list<ParkPosition>
39 getAllOffsetNodeVals(const char* name, SGPropertyNode * entry_node);
40
41 FGAIScenario::FGAIScenario(const string &filename)
42 {
43   int i;
44   SGPath path( globals->get_fg_root() );
45   
46 //   cout << "/Data/AI/" << filename << endl;
47   
48   path.append( ("/Data/AI/" + filename + ".xml").c_str() );
49   SGPropertyNode root;
50   
51 //   cout <<"path " << path.str() << endl;
52   
53   try {
54       readProperties(path.str(), &root);
55   } catch (const sg_exception &e) {
56       SG_LOG(SG_GENERAL, SG_ALERT,
57        "Incorrect path specified for AI scenario: ");
58        
59        cout << path.str() << endl;
60       
61       return;
62   }
63
64   entries.clear();
65   SGPropertyNode * node = root.getNode("scenario");
66   for (i = 0; i < node->nChildren(); i++) { 
67      
68 //      cout << "Reading entity data entry " << i << endl;        
69      
70      SGPropertyNode * entry_node = node->getChild(i);
71
72      FGAIModelEntity* en = new FGAIModelEntity;
73      en->callsign       = entry_node->getStringValue("callsign", "none");
74      en->m_type         = entry_node->getStringValue("type", "aircraft");
75      en->m_class        = entry_node->getStringValue("class", "jet_transport");
76      en->path           = entry_node->getStringValue("model", "Models/Geometry/glider.ac");
77      en->flightplan     = entry_node->getStringValue("flightplan", "");
78      en->repeat         = entry_node->getDoubleValue("repeat", 0.0); 
79      en->latitude       = entry_node->getDoubleValue("latitude", 0.0); 
80      en->longitude      = entry_node->getDoubleValue("longitude", 0.0); 
81      en->altitude       = entry_node->getDoubleValue("altitude", 0.0); 
82      en->speed          = entry_node->getDoubleValue("speed", 0.0); 
83      en->heading        = entry_node->getDoubleValue("heading", 0.0); 
84      en->roll           = entry_node->getDoubleValue("roll", 0.0); 
85      en->azimuth        = entry_node->getDoubleValue("azimuth", 0.0); 
86      en->elevation      = entry_node->getDoubleValue("elevation", 0.0); 
87      en->rudder         = entry_node->getDoubleValue("rudder", 0.0);
88      en->strength       = entry_node->getDoubleValue("strength-fps", 8.0);
89      en->strength       = entry_node->getDoubleValue("strength-norm", 1.0);  
90      en->diameter       = entry_node->getDoubleValue("diameter-ft", 0.0);
91      en->height_msl     = entry_node->getDoubleValue("height-msl", 5000.0);
92      en->eda            = entry_node->getDoubleValue("eda", 0.007);
93      en->life           = entry_node->getDoubleValue("life", 900.0);
94      en->buoyancy       = entry_node->getDoubleValue("buoyancy", 0);
95      en->wind_from_east = entry_node->getDoubleValue("wind_from_east", 0);
96      en->wind_from_north = entry_node->getDoubleValue("wind_from_north", 0);
97      en->wind            = entry_node->getBoolValue  ("wind", false);
98      en->cd              = entry_node->getDoubleValue("cd", 0.029); 
99      en->mass            = entry_node->getDoubleValue("mass", 0.007); 
100      en->radius          = entry_node->getDoubleValue("turn-radius-ft", 2000);
101      en->name            = entry_node->getStringValue("name", "");
102      en->pennant_number  = entry_node->getStringValue("pennant-number", "");
103      en->wire_objects     = getAllStringNodeVals("wire", entry_node);
104      en->catapult_objects = getAllStringNodeVals("catapult", entry_node);
105      en->solid_objects    = getAllStringNodeVals("solid", entry_node);
106      en->ppositions       = getAllOffsetNodeVals("parking-pos", entry_node);
107      list<ParkPosition> flolspos = getAllOffsetNodeVals("flols-pos", entry_node);
108      en->flols_offset     = flolspos.front().offset;
109
110      en->fp             = NULL;
111      if (en->flightplan != ""){
112         en->fp = new FGAIFlightPlan( en->flightplan );
113      }
114      entries.push_back( en );
115    }
116
117   entry_iterator = entries.begin();
118   //cout << entries.size() << " entries read." << endl;
119 }
120
121
122 FGAIScenario::~FGAIScenario()
123 {
124   entries.clear();
125 }
126
127
128 FGAIModelEntity*
129 FGAIScenario::getNextEntry( void )
130 {
131   if (entries.size() == 0) return 0;
132   if (entry_iterator != entries.end()) {
133     return *entry_iterator++;
134   } else {
135     return 0;
136   }
137 }
138
139 int FGAIScenario::nEntries( void )
140 {
141   return entries.size();
142 }
143
144 static list<string>
145 getAllStringNodeVals(const char* name, SGPropertyNode * entry_node)
146 {
147   list<string> retval;
148   int i=0;
149   do {
150     char nodename[100];
151     snprintf(nodename, sizeof(nodename), "%s[%d]", name, i);
152     const char* objname = entry_node->getStringValue(nodename, 0);
153     if (objname == 0)
154       return retval;
155
156     retval.push_back(string(objname));
157     ++i;
158   } while (1);
159
160   return retval;
161 }
162
163 static list<ParkPosition>
164 getAllOffsetNodeVals(const char* name, SGPropertyNode * entry_node)
165 {
166   list<ParkPosition> retval;
167
168   vector<SGPropertyNode_ptr>::iterator it;
169   vector<SGPropertyNode_ptr> children = entry_node->getChildren(name);
170   for (it = children.begin(); it != children.end(); ++it) {
171     string name = (*it)->getStringValue("name", "unnamed");
172     double offset_x = (*it)->getDoubleValue("x-offset-m", 0);
173     double offset_y = (*it)->getDoubleValue("y-offset-m", 0);
174     double offset_z = (*it)->getDoubleValue("z-offset-m", 0);
175     double hd = (*it)->getDoubleValue("heading-offset-deg", 0);
176     ParkPosition pp(name, Point3D(offset_x, offset_y, offset_z), hd);
177     retval.push_back(pp);
178   }
179
180   return retval;
181 }
182
183 // end scenario.cxx
184