1 // AIManager.cxx Based on David Luff's AIMgr:
2 // - a global management type for AI objects
4 // Written by David Culp, started October 2003.
5 // - davidculp2@comcast.net
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.
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.
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.
21 #include <Main/globals.hxx>
23 #include <Airports/simple.hxx>
24 #include <Traffic/TrafficMgr.hxx>
26 #include "AIManager.hxx"
27 #include "AIAircraft.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 #include "AITanker.hxx"
37 #include <simgear/math/sg_geodesy.hxx>
40 FGAIManager::FGAIManager() {
44 for (unsigned i = 0; i < FGAIBase::MAX_OBJECTS; ++i)
45 mNumAiTypeModels[i] = 0;
48 FGAIManager::~FGAIManager() {
49 ai_list_iterator ai_list_itr = ai_list.begin();
51 while(ai_list_itr != ai_list.end()) {
52 (*ai_list_itr)->unbind();
59 root = fgGetNode("sim/ai", true);
61 enabled = root->getNode("enabled", true)->getBoolValue();
66 wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
67 wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
68 wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
70 user_latitude_node = fgGetNode("/position/latitude-deg", true);
71 user_longitude_node = fgGetNode("/position/longitude-deg", true);
72 user_altitude_node = fgGetNode("/position/altitude-ft", true);
73 user_heading_node = fgGetNode("/orientation/heading-deg", true);
74 user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
75 user_yaw_node = fgGetNode("/orientation/side-slip-deg", true);
76 user_speed_node = fgGetNode("/velocities/uBody-fps", true);
80 FGAIManager::postinit() {
81 // postinit, so that it can access the Nasal subsystem
82 map<string, bool> scenarios;
83 for (int i = 0 ; i < root->nChildren() ; i++) {
84 SGPropertyNode *n = root->getChild(i);
85 if (strcmp(n->getName(), "scenario"))
88 string name = n->getStringValue();
92 if (scenarios.find(name) != scenarios.end()) {
93 SG_LOG(SG_GENERAL, SG_WARN, "won't load scenario '" << name << "' twice");
97 SG_LOG(SG_GENERAL, SG_INFO, "loading scenario '" << name << '\'');
98 processScenario(name);
99 scenarios[name] = true;
104 FGAIManager::reinit() {
106 ai_list_iterator ai_list_itr = ai_list.begin();
108 while(ai_list_itr != ai_list.end()) {
109 (*ai_list_itr)->reinit();
115 FGAIManager::bind() {
116 root = globals->get_props()->getNode("ai/models", true);
117 root->tie("count", SGRawValueMethods<FGAIManager, int>(*this,
118 &FGAIManager::getNumAiObjects));
122 FGAIManager::unbind() {
123 root->untie("count");
127 FGAIManager::update(double dt) {
128 // initialize these for finding nearest thermals
129 range_nearest = 10000.0;
135 FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
138 ai_list_iterator ai_list_itr = ai_list.begin();
140 while(ai_list_itr != ai_list.end()) {
142 if ((*ai_list_itr)->getDie()) {
143 tmgr->release((*ai_list_itr)->getID());
145 --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
146 FGAIBase *base = *ai_list_itr;
147 SGPropertyNode *props = base->_getProps();
149 props->setBoolValue("valid", false);
152 // for backward compatibility reset properties, so that aircraft,
153 // which don't know the <valid> property, keep working
154 // TODO: remove after a while
155 props->setIntValue("id", -1);
156 props->setBoolValue("radar/in-range", false);
157 props->setIntValue("refuel/tanker", false);
158 props->setStringValue("sim/multiplay/chat", "");
159 props->setStringValue("sim/multiplay/transmission-freq-hz", "");
161 ai_list_itr = ai_list.erase(ai_list_itr);
164 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
165 FGAIBase *base = *ai_list_itr;
166 processThermal((FGAIThermal*)base);
168 (*ai_list_itr)->update(_dt);
174 wind_from_down_node->setDoubleValue( strength ); // for thermals
178 FGAIManager::attach(SGSharedPtr<FGAIBase> model)
180 //unsigned idx = mNumAiTypeModels[model->getType()];
181 const char* typeString = model->getTypeString();
182 SGPropertyNode* root = globals->get_props()->getNode("ai/models", true);
186 // find free index in the property tree, if we have
187 // more than 10000 mp-aircrafts in the property tree we should optimize the mp-server
188 for (i = 0; i < 10000; i++) {
189 p = root->getNode(typeString, i, false);
191 if (!p || !p->getBoolValue("valid", false))
194 if (p->getIntValue("id",-1)==model->getID()) {
195 p->setStringValue("callsign","***invalid node***"); //debug only, should never set!
199 p = root->getNode(typeString, i, true);
200 model->setManager(this, p);
201 ai_list.push_back(model);
203 ++(mNumAiTypeModels[model->getType()]);
204 model->init(model->getType()==FGAIBase::otAircraft
205 || model->getType()==FGAIBase::otMultiplayer
206 || model->getType()==FGAIBase::otStatic);
208 p->setBoolValue("valid", true);
212 FGAIManager::destroyObject( int ID ) {
213 ai_list_iterator ai_list_itr = ai_list.begin();
215 while(ai_list_itr != ai_list.end()) {
217 if ((*ai_list_itr)->getID() == ID) {
219 --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
220 (*ai_list_itr)->unbind();
221 ai_list_itr = ai_list.erase(ai_list_itr);
229 FGAIManager::getNumAiObjects(void) const
235 FGAIManager::fetchUserState( void ) {
236 user_latitude = user_latitude_node->getDoubleValue();
237 user_longitude = user_longitude_node->getDoubleValue();
238 user_altitude = user_altitude_node->getDoubleValue();
239 user_heading = user_heading_node->getDoubleValue();
240 user_pitch = user_pitch_node->getDoubleValue();
241 user_yaw = user_yaw_node->getDoubleValue();
242 user_speed = user_speed_node->getDoubleValue() * 0.592484;
243 wind_from_east = wind_from_east_node->getDoubleValue();
244 wind_from_north = wind_from_north_node->getDoubleValue();
247 // only keep the results from the nearest thermal
249 FGAIManager::processThermal( FGAIThermal* thermal ) {
250 thermal->update(_dt);
252 if ( thermal->_getRange() < range_nearest ) {
253 range_nearest = thermal->_getRange();
254 strength = thermal->getStrength();
260 FGAIManager::processScenario( const string &filename ) {
262 SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
267 SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
272 for (int i = 0; i < scenarios->nChildren(); i++) {
273 SGPropertyNode* scEntry = scenarios->getChild(i);
275 if (strcmp(scEntry->getName(), "entry"))
277 std::string type = scEntry->getStringValue("type", "aircraft");
279 if (type == "tanker") { // refueling scenarios
280 FGAITanker* tanker = new FGAITanker;
281 tanker->readFromScenario(scEntry);
283 } else if (type == "aircraft") {
284 FGAIAircraft* aircraft = new FGAIAircraft;
285 aircraft->readFromScenario(scEntry);
288 } else if (type == "ship") {
289 FGAIShip* ship = new FGAIShip;
290 ship->readFromScenario(scEntry);
293 } else if (type == "carrier") {
294 FGAICarrier* carrier = new FGAICarrier;
295 carrier->readFromScenario(scEntry);
298 } else if (type == "thunderstorm") {
299 FGAIStorm* storm = new FGAIStorm;
300 storm->readFromScenario(scEntry);
303 } else if (type == "thermal") {
304 FGAIThermal* thermal = new FGAIThermal;
305 thermal->readFromScenario(scEntry);
308 } else if (type == "ballistic") {
309 FGAIBallistic* ballistic = new FGAIBallistic;
310 ballistic->readFromScenario(scEntry);
313 } else if (type == "static") {
314 FGAIStatic* aistatic = new FGAIStatic;
315 aistatic->readFromScenario(scEntry);
323 FGAIManager::loadScenarioFile(const std::string& filename)
325 SGPath path(globals->get_fg_root());
326 path.append("AI/" + filename + ".xml");
328 SGPropertyNode_ptr root = new SGPropertyNode;
329 readProperties(path.str(), root);
331 } catch (const sg_exception &e) {
332 SG_LOG(SG_GENERAL, SG_DEBUG, "Incorrect path specified for AI "
333 "scenario: \"" << path.str() << "\"");
339 FGAIManager::getStartPosition(const string& id, const string& pid,
340 SGGeod& geodPos, double& hdng, SGVec3d& uvw)
343 SGPropertyNode* root = fgGetNode("sim/ai", true);
344 if (!root->getNode("enabled", true)->getBoolValue())
347 for (int i = 0 ; (!found) && i < root->nChildren() ; i++) {
348 SGPropertyNode *aiEntry = root->getChild( i );
349 if ( !strcmp( aiEntry->getName(), "scenario" ) ) {
350 string filename = aiEntry->getStringValue();
351 SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
353 SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
355 for (int i = 0; i < scenarios->nChildren(); i++) {
356 SGPropertyNode* scEntry = scenarios->getChild(i);
357 std::string type = scEntry->getStringValue("type");
358 std::string pnumber = scEntry->getStringValue("pennant-number");
359 std::string name = scEntry->getStringValue("name");
360 if (type == "carrier" && (pnumber == id || name == id)) {
361 SGSharedPtr<FGAICarrier> carrier = new FGAICarrier;
362 carrier->readFromScenario(scEntry);
364 if (carrier->getParkPosition(pid, geodPos, hdng, uvw)) {
378 FGAIManager::calcCollision(double alt, double lat, double lon, double fuse_range)
380 // we specify tgt extent (ft) according to the AIObject type
381 double tgt_ht[] = {0, 50 ,100, 250, 0, 100, 0, 0, 50, 50};
382 double tgt_length[] = {0, 100, 200, 750, 0, 50, 0, 0, 200, 100};
384 ai_list_iterator ai_list_itr = ai_list.begin();
385 ai_list_iterator end = ai_list.end();
387 while (ai_list_itr != end) {
388 double tgt_alt = (*ai_list_itr)->_getAltitude();
389 int type = (*ai_list_itr)->getType();
390 tgt_ht[type] += fuse_range;
392 if (fabs(tgt_alt - alt) > tgt_ht[type] || type == FGAIBase::otBallistic
393 || type == FGAIBase::otStorm || type == FGAIBase::otThermal) {
394 SG_LOG(SG_GENERAL, SG_DEBUG, "AIManager: skipping "
395 << fabs(tgt_alt - alt)
403 double tgt_lat = (*ai_list_itr)->_getLatitude();
404 double tgt_lon = (*ai_list_itr)->_getLongitude();
405 int id = (*ai_list_itr)->getID();
407 double range = calcRange(lat, lon, tgt_lat, tgt_lon);
409 SG_LOG(SG_GENERAL, SG_DEBUG, "AIManager: AI list size "
413 << " range " << range
414 //<< " bearing " << bearing
415 << " alt " << tgt_alt
418 tgt_length[type] += fuse_range;
420 if (range < tgt_length[type]){
421 SG_LOG(SG_GENERAL, SG_DEBUG, "AIManager: HIT! "
424 << " range " << range
425 << " alt " << tgt_alt
435 FGAIManager::calcRange(double lat, double lon, double lat2, double lon2) const
437 double course, az2, distance;
439 //calculate the bearing and range of the second pos from the first
440 geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
441 distance *= SG_METER_TO_FEET;