]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
c7643a22d7d5d465adae2de1563f7118a7dd2c5f
[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
73 void FGAIManager::postinit() {
74   // postinit, so that it can access the Nasal subsystem
75   for(int i = 0 ; i < root->nChildren() ; i++) {
76     SGPropertyNode *aiEntry = root->getChild( i );
77     if( !strcmp( aiEntry->getName(), "scenario" ) ) {
78       scenario_filename = aiEntry->getStringValue();
79       if (!scenario_filename.empty())
80         processScenario( scenario_filename );
81     }
82   }
83 }
84
85
86 void FGAIManager::reinit() {
87    update(0.0);
88    ai_list_iterator ai_list_itr = ai_list.begin();
89
90    while(ai_list_itr != ai_list.end()) {
91       (*ai_list_itr)->reinit();
92       ++ai_list_itr;
93    }
94 }
95
96
97 void FGAIManager::bind() {
98    root = globals->get_props()->getNode("ai/models", true);
99    root->tie("count", SGRawValueMethods<FGAIManager, int>(*this,
100              &FGAIManager::getNumAiObjects));
101 }
102
103
104 void FGAIManager::unbind() {
105     root->untie("count");
106 }
107
108
109 void FGAIManager::update(double dt) {
110   // initialize these for finding nearest thermals
111   range_nearest = 10000.0;
112   strength = 0.0;
113   if (!enabled)
114     return;
115
116   FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
117   _dt = dt;
118
119   ai_list_iterator ai_list_itr = ai_list.begin();
120   while(ai_list_itr != ai_list.end()) {
121     if ((*ai_list_itr)->getDie()) {      
122       tmgr->release((*ai_list_itr)->getID());
123       --mNumAiModels;
124       --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
125       (*ai_list_itr)->unbind();
126       ai_list_itr = ai_list.erase(ai_list_itr);
127     } else {
128       fetchUserState();
129       if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
130         FGAIBase *base = *ai_list_itr;
131         processThermal((FGAIThermal*)base); 
132       } else { 
133         (*ai_list_itr)->update(_dt);
134       }
135       ++ai_list_itr;
136     }
137   }
138   wind_from_down_node->setDoubleValue( strength ); // for thermals
139 }
140
141 void
142 FGAIManager::attach(SGSharedPtr<FGAIBase> model)
143 {
144   unsigned idx = mNumAiTypeModels[model->getType()];
145   const char* typeString = model->getTypeString();
146   SGPropertyNode* root = globals->get_props()->getNode("ai/models", true);
147   SGPropertyNode* p;
148   int i;
149   for (i=0;i<10000;i++) //find free index in the property tree, if we have
150       //more than 10000 mp-aircrafts in the property tree we should optimize the mp-server
151   {
152     p = root->getNode(typeString, i, false);
153     if (!p) break;
154     if (p->getIntValue("id",-1)==model->getID())
155     {
156         p->setStringValue("callsign","***invalid node***"); //debug only, should never set!
157                                               
158     }
159   }
160   p = root->getNode(typeString, i, true);
161   model->setManager(this, p);
162   ai_list.push_back(model);
163   ++mNumAiModels;
164   ++(mNumAiTypeModels[model->getType()]);
165   model->init(model->getType()==FGAIBase::otAircraft
166       || model->getType()==FGAIBase::otMultiplayer
167       || model->getType()==FGAIBase::otStatic);
168   model->bind();
169 }
170
171
172 void FGAIManager::destroyObject( int ID ) {
173   ai_list_iterator ai_list_itr = ai_list.begin();
174   while(ai_list_itr != ai_list.end()) {
175     if ((*ai_list_itr)->getID() == ID) {
176       --mNumAiModels;
177       --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
178       (*ai_list_itr)->unbind();
179       ai_list_itr = ai_list.erase(ai_list_itr);
180     } else
181       ++ai_list_itr;
182   }
183 }
184
185 int
186 FGAIManager::getNumAiObjects(void) const
187 {
188   return mNumAiModels;
189 }
190
191 void FGAIManager::fetchUserState( void ) {
192      user_latitude  = user_latitude_node->getDoubleValue();
193      user_longitude = user_longitude_node->getDoubleValue();
194      user_altitude  = user_altitude_node->getDoubleValue();
195      user_heading   = user_heading_node->getDoubleValue();
196      user_pitch     = user_pitch_node->getDoubleValue();
197      user_yaw       = user_yaw_node->getDoubleValue();
198      user_speed     = user_speed_node->getDoubleValue() * 0.592484;
199      wind_from_east = wind_from_east_node->getDoubleValue();
200      wind_from_north = wind_from_north_node->getDoubleValue();
201 }
202
203
204 // only keep the results from the nearest thermal
205 void FGAIManager::processThermal( FGAIThermal* thermal ) {
206   thermal->update(_dt);
207   if ( thermal->_getRange() < range_nearest ) {
208      range_nearest = thermal->_getRange();
209      strength = thermal->getStrength();
210   }
211 }
212
213
214 void FGAIManager::processScenario( const string &filename ) {
215
216   SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
217   if (!scenarioTop)
218     return;
219   SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
220   if (!scenarios)
221     return;
222
223   for (int i = 0; i < scenarios->nChildren(); i++) { 
224     SGPropertyNode* scEntry = scenarios->getChild(i);
225     std::string type = scEntry->getStringValue("type", "aircraft");
226
227     if (type == "aircraft") {
228       FGAIAircraft* aircraft = new FGAIAircraft;
229       aircraft->readFromScenario(scEntry);
230       attach(aircraft);
231
232     } else if (type == "ship") {
233       FGAIShip* ship = new FGAIShip;
234       ship->readFromScenario(scEntry);
235       attach(ship);
236       
237     } else if (type == "carrier") {
238       FGAICarrier* carrier = new FGAICarrier;
239       carrier->readFromScenario(scEntry);
240       attach(carrier);
241       
242     } else if (type == "thunderstorm") {
243       FGAIStorm* storm = new FGAIStorm;
244       storm->readFromScenario(scEntry);
245       attach(storm);
246       
247     } else if (type == "thermal") {
248       FGAIThermal* thermal = new FGAIThermal;
249       thermal->readFromScenario(scEntry);
250       attach(thermal);
251       
252     } else if (type == "ballistic") {
253       FGAIBallistic* ballistic = new FGAIBallistic;
254       ballistic->readFromScenario(scEntry);
255       attach(ballistic);
256       
257     } else if (type == "static") {
258       FGAIStatic* aistatic = new FGAIStatic;
259       aistatic->readFromScenario(scEntry);
260       attach(aistatic);
261       
262     }
263   }
264 }
265
266 SGPropertyNode_ptr
267 FGAIManager::loadScenarioFile(const std::string& filename)
268 {
269   SGPath path(globals->get_fg_root());
270   path.append("AI/" + filename + ".xml");
271   try {
272     SGPropertyNode_ptr root = new SGPropertyNode;
273     readProperties(path.str(), root);
274     return root;
275   } catch (const sg_exception &e) {
276     SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path specified for AI "
277            "scenario: \"" << path.str() << "\"");
278     return 0;
279   }
280 }
281
282 bool FGAIManager::getStartPosition(const string& id, const string& pid,
283                                    SGGeod& geodPos, double& hdng, SGVec3d& uvw)
284 {
285   bool found = false;
286   SGPropertyNode* root = fgGetNode("sim/ai", true);
287   if (!root->getNode("enabled", true)->getBoolValue())
288       return found;
289
290   for(int i = 0 ; (!found) && i < root->nChildren() ; i++) {
291     SGPropertyNode *aiEntry = root->getChild( i );
292     if( !strcmp( aiEntry->getName(), "scenario" ) ) {
293       string filename = aiEntry->getStringValue();
294       SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
295       if (scenarioTop) {
296         SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
297         if (scenarios) {
298           for (int i = 0; i < scenarios->nChildren(); i++) { 
299             SGPropertyNode* scEntry = scenarios->getChild(i);
300             std::string type = scEntry->getStringValue("type");
301             std::string pnumber = scEntry->getStringValue("pennant-number");
302             std::string name = scEntry->getStringValue("name");
303             if (type == "carrier" && (pnumber == id || name == id)) {
304               SGSharedPtr<FGAICarrier> carrier = new FGAICarrier;
305               carrier->readFromScenario(scEntry);
306               
307               if (carrier->getParkPosition(pid, geodPos, hdng, uvw)) {
308                 found = true;
309                 break;
310               }
311             }
312           }
313         }
314       }
315     }
316   }
317   return found;
318 }
319
320 //end AIManager.cxx