]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
72f592a1eec4b2ac0cfd884cceab66b57e2190b7
[flightgear.git] / src / AIModel / AIManager.cxx
1 // AIManager.cxx  Based on David Luff's AIMgr:
2 // - a global management type for AI objects
3 //
4 // Written by David Culp, started October 2003.
5 // - davidculp2@comcast.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #include <Main/globals.hxx>
22
23 #include <Airports/simple.hxx>
24 #include <Traffic/TrafficMgr.hxx>
25
26 #include "AIManager.hxx"
27 #include "AIAircraft.hxx"
28 #include "AIShip.hxx"
29 #include "AIBallistic.hxx"
30 #include "AIStorm.hxx"
31 #include "AIThermal.hxx"
32 #include "AICarrier.hxx"
33 #include "AIStatic.hxx"
34 #include "AIMultiplayer.hxx"
35
36 FGAIManager::FGAIManager() {
37   _dt = 0.0;
38   mNumAiModels = 0;
39   for (unsigned i = 0; i < FGAIBase::MAX_OBJECTS; ++i)
40     mNumAiTypeModels[i] = 0;
41 }
42
43 FGAIManager::~FGAIManager() {
44   ai_list_iterator ai_list_itr = ai_list.begin();
45   while(ai_list_itr != ai_list.end()) {
46       (*ai_list_itr)->unbind();
47       ++ai_list_itr;
48   }
49 }
50
51
52 void FGAIManager::init() {
53   root = fgGetNode("sim/ai", true);
54
55   enabled = root->getNode("enabled", true)->getBoolValue();
56   if (!enabled)
57       return;
58
59   wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
60   wind_from_east_node  = fgGetNode("/environment/wind-from-east-fps",true);
61   wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
62
63   user_latitude_node  = fgGetNode("/position/latitude-deg", true);
64   user_longitude_node = fgGetNode("/position/longitude-deg", true);
65   user_altitude_node  = fgGetNode("/position/altitude-ft", true);
66   user_heading_node   = fgGetNode("/orientation/heading-deg", true);
67   user_pitch_node     = fgGetNode("/orientation/pitch-deg", true);
68   user_yaw_node       = fgGetNode("/orientation/side-slip-deg", true);
69   user_speed_node     = fgGetNode("/velocities/uBody-fps", true);
70
71
72   /// Move that into the constructor
73   for(int i = 0 ; i < root->nChildren() ; i++) {
74     SGPropertyNode *aiEntry = root->getChild( i );
75     if( !strcmp( aiEntry->getName(), "scenario" ) ) {
76       scenario_filename = aiEntry->getStringValue();
77       if (!scenario_filename.empty())
78         processScenario( scenario_filename );
79     }
80   }
81 }
82
83
84 void FGAIManager::reinit() {
85    update(0.0);
86 }
87
88
89 void FGAIManager::bind() {
90    root = globals->get_props()->getNode("ai/models", true);
91    root->tie("count", SGRawValueMethods<FGAIManager, int>(*this,
92              &FGAIManager::getNumAiObjects));
93 }
94
95
96 void FGAIManager::unbind() {
97     root->untie("count");
98 }
99
100
101 void FGAIManager::update(double dt) {
102   // initialize these for finding nearest thermals
103   range_nearest = 10000.0;
104   strength = 0.0;
105   if (!enabled)
106     return;
107
108   FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
109   _dt = dt;
110
111   ai_list_iterator ai_list_itr = ai_list.begin();
112   while(ai_list_itr != ai_list.end()) {
113     if ((*ai_list_itr)->getDie()) {      
114       tmgr->release((*ai_list_itr)->getID());
115       --mNumAiModels;
116       --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
117       (*ai_list_itr)->unbind();
118       ai_list_itr = ai_list.erase(ai_list_itr);
119     } else {
120       fetchUserState();
121       if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
122         FGAIBase *base = *ai_list_itr;
123         processThermal((FGAIThermal*)base); 
124       } else { 
125         (*ai_list_itr)->update(_dt);
126       }
127       ++ai_list_itr;
128     }
129   }
130   wind_from_down_node->setDoubleValue( strength ); // for thermals
131 }
132
133 void
134 FGAIManager::attach(SGSharedPtr<FGAIBase> model)
135 {
136   unsigned idx = mNumAiTypeModels[model->getType()];
137   const char* typeString = model->getTypeString();
138   SGPropertyNode* root = globals->get_props()->getNode("ai/models", true);
139   SGPropertyNode* p;
140   int i;
141   for (i=0;i<10000;i++) //find free index in the property tree, if we have
142       //more than 10000 mp-aircrafts in the property tree we should optimize the mp-server
143   {
144     p = root->getNode(typeString, i, false);
145     if (!p) break;
146     if (p->getIntValue("id",-1)==model->getID())
147     {
148         p->setStringValue("callsign","***invalid node***"); //debug only, should never set!
149                                               
150     }
151   }
152   p = root->getNode(typeString, i, true);
153   model->setManager(this, p);
154   ai_list.push_back(model);
155   ++mNumAiModels;
156   ++(mNumAiTypeModels[model->getType()]);
157   model->init(model->getType()==FGAIBase::otAircraft
158       || model->getType()==FGAIBase::otMultiplayer
159       || model->getType()==FGAIBase::otStatic);
160   model->bind();
161 }
162
163
164 void FGAIManager::destroyObject( int ID ) {
165   ai_list_iterator ai_list_itr = ai_list.begin();
166   while(ai_list_itr != ai_list.end()) {
167     if ((*ai_list_itr)->getID() == ID) {
168       --mNumAiModels;
169       --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
170       (*ai_list_itr)->unbind();
171       ai_list_itr = ai_list.erase(ai_list_itr);
172     } else
173       ++ai_list_itr;
174   }
175 }
176
177 int
178 FGAIManager::getNumAiObjects(void) const
179 {
180   return mNumAiModels;
181 }
182
183 void FGAIManager::fetchUserState( void ) {
184      user_latitude  = user_latitude_node->getDoubleValue();
185      user_longitude = user_longitude_node->getDoubleValue();
186      user_altitude  = user_altitude_node->getDoubleValue();
187      user_heading   = user_heading_node->getDoubleValue();
188      user_pitch     = user_pitch_node->getDoubleValue();
189      user_yaw       = user_yaw_node->getDoubleValue();
190      user_speed     = user_speed_node->getDoubleValue() * 0.592484;
191      wind_from_east = wind_from_east_node->getDoubleValue();
192      wind_from_north = wind_from_north_node->getDoubleValue();
193 }
194
195
196 // only keep the results from the nearest thermal
197 void FGAIManager::processThermal( FGAIThermal* thermal ) {
198   thermal->update(_dt);
199   if ( thermal->_getRange() < range_nearest ) {
200      range_nearest = thermal->_getRange();
201      strength = thermal->getStrength();
202   }
203 }
204
205
206 void FGAIManager::processScenario( const string &filename ) {
207
208   SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
209   if (!scenarioTop)
210     return;
211   SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
212   if (!scenarios)
213     return;
214
215   for (int i = 0; i < scenarios->nChildren(); i++) { 
216     SGPropertyNode* scEntry = scenarios->getChild(i);
217     std::string type = scEntry->getStringValue("type", "aircraft");
218
219     if (type == "aircraft") {
220       FGAIAircraft* aircraft = new FGAIAircraft;
221       aircraft->readFromScenario(scEntry);
222       attach(aircraft);
223
224     } else if (type == "ship") {
225       FGAIShip* ship = new FGAIShip;
226       ship->readFromScenario(scEntry);
227       attach(ship);
228       
229     } else if (type == "carrier") {
230       FGAICarrier* carrier = new FGAICarrier;
231       carrier->readFromScenario(scEntry);
232       attach(carrier);
233       
234     } else if (type == "thunderstorm") {
235       FGAIStorm* storm = new FGAIStorm;
236       storm->readFromScenario(scEntry);
237       attach(storm);
238       
239     } else if (type == "thermal") {
240       FGAIThermal* thermal = new FGAIThermal;
241       thermal->readFromScenario(scEntry);
242       attach(thermal);
243       
244     } else if (type == "ballistic") {
245       FGAIBallistic* ballistic = new FGAIBallistic;
246       ballistic->readFromScenario(scEntry);
247       attach(ballistic);
248       
249     } else if (type == "static") {
250       FGAIStatic* aistatic = new FGAIStatic;
251       aistatic->readFromScenario(scEntry);
252       attach(aistatic);
253       
254     }
255   }
256 }
257
258 SGPropertyNode_ptr
259 FGAIManager::loadScenarioFile(const std::string& filename)
260 {
261   SGPath path(globals->get_fg_root());
262   path.append("AI/" + filename + ".xml");
263   try {
264     SGPropertyNode_ptr root = new SGPropertyNode;
265     readProperties(path.str(), root);
266     return root;
267   } catch (const sg_exception &e) {
268     SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path specified for AI "
269            "scenario: \"" << path.str() << "\"");
270     return 0;
271   }
272 }
273
274 bool FGAIManager::getStartPosition(const string& id, const string& pid,
275                                    SGGeod& geodPos, double& hdng, SGVec3d& uvw)
276 {
277   bool found = false;
278   SGPropertyNode* root = fgGetNode("sim/ai", true);
279   if (!root->getNode("enabled", true)->getBoolValue())
280       return found;
281
282   for(int i = 0 ; (!found) && i < root->nChildren() ; i++) {
283     SGPropertyNode *aiEntry = root->getChild( i );
284     if( !strcmp( aiEntry->getName(), "scenario" ) ) {
285       string filename = aiEntry->getStringValue();
286       SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
287       if (scenarioTop) {
288         SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
289         if (scenarios) {
290           for (int i = 0; i < scenarios->nChildren(); i++) { 
291             SGPropertyNode* scEntry = scenarios->getChild(i);
292             std::string type = scEntry->getStringValue("type");
293             std::string pnumber = scEntry->getStringValue("pennant-number");
294             std::string name = scEntry->getStringValue("name");
295             if (type == "carrier" && (pnumber == id || name == id)) {
296               SGSharedPtr<FGAICarrier> carrier = new FGAICarrier;
297               carrier->readFromScenario(scEntry);
298               
299               if (carrier->getParkPosition(pid, geodPos, hdng, uvw)) {
300                 found = true;
301                 break;
302               }
303             }
304           }
305         }
306       }
307     }
308   }
309   return found;
310 }
311
312 //end AIManager.cxx