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.
23 #include <simgear/math/sg_geodesy.hxx>
24 #include <simgear/props/props_io.hxx>
25 #include <simgear/structure/exception.hxx>
27 #include <Main/globals.hxx>
29 #include <Airports/simple.hxx>
30 #include <Traffic/TrafficMgr.hxx>
32 #include "AIManager.hxx"
33 #include "AIAircraft.hxx"
35 #include "AIBallistic.hxx"
36 #include "AIStorm.hxx"
37 #include "AIThermal.hxx"
38 #include "AICarrier.hxx"
39 #include "AIStatic.hxx"
40 #include "AIMultiplayer.hxx"
41 #include "AITanker.hxx"
42 #include "AIWingman.hxx"
43 #include "AIGroundVehicle.hxx"
44 #include "AIEscort.hxx"
46 FGAIManager::FGAIManager() {
50 for (unsigned i = 0; i < FGAIBase::MAX_OBJECTS; ++i)
51 mNumAiTypeModels[i] = 0;
54 FGAIManager::~FGAIManager() {
55 ai_list_iterator ai_list_itr = ai_list.begin();
57 while(ai_list_itr != ai_list.end()) {
58 (*ai_list_itr)->unbind();
65 root = fgGetNode("sim/ai", true);
67 enabled = root->getNode("enabled", true)->getBoolValue();
72 thermal_lift_node = fgGetNode("/environment/thermal-lift-fps", true);
73 wind_from_east_node = fgGetNode("/environment/wind-from-east-fps",true);
74 wind_from_north_node = fgGetNode("/environment/wind-from-north-fps",true);
76 user_latitude_node = fgGetNode("/position/latitude-deg", true);
77 user_longitude_node = fgGetNode("/position/longitude-deg", true);
78 user_altitude_node = fgGetNode("/position/altitude-ft", true);
79 user_heading_node = fgGetNode("/orientation/heading-deg", true);
80 user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
81 user_yaw_node = fgGetNode("/orientation/side-slip-deg", true);
82 user_roll_node = fgGetNode("/orientation/roll-deg", true);
83 user_speed_node = fgGetNode("/velocities/uBody-fps", true);
87 FGAIManager::postinit() {
88 // postinit, so that it can access the Nasal subsystem
89 map<string, bool> scenarios;
90 for (int i = 0 ; i < root->nChildren() ; i++) {
91 SGPropertyNode *n = root->getChild(i);
92 if (strcmp(n->getName(), "scenario"))
95 string name = n->getStringValue();
99 if (scenarios.find(name) != scenarios.end()) {
100 SG_LOG(SG_GENERAL, SG_WARN, "won't load scenario '" << name << "' twice");
104 SG_LOG(SG_GENERAL, SG_ALERT, "loading scenario '" << name << '\'');
105 processScenario(name);
106 scenarios[name] = true;
111 FGAIManager::reinit() {
113 ai_list_iterator ai_list_itr = ai_list.begin();
115 while(ai_list_itr != ai_list.end()) {
116 (*ai_list_itr)->reinit();
122 FGAIManager::bind() {
123 root = globals->get_props()->getNode("ai/models", true);
124 root->tie("count", SGRawValueMethods<FGAIManager, int>(*this,
125 &FGAIManager::getNumAiObjects));
129 FGAIManager::unbind() {
130 root->untie("count");
134 FGAIManager::update(double dt) {
135 // initialize these for finding nearest thermals
136 range_nearest = 10000.0;
142 FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
145 ai_list_iterator ai_list_itr = ai_list.begin();
147 while(ai_list_itr != ai_list.end()) {
149 if ((*ai_list_itr)->getDie()) {
150 tmgr->release((*ai_list_itr)->getID());
152 --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
153 FGAIBase *base = (*ai_list_itr).get();
154 SGPropertyNode *props = base->_getProps();
156 props->setBoolValue("valid", false);
159 // for backward compatibility reset properties, so that aircraft,
160 // which don't know the <valid> property, keep working
161 // TODO: remove after a while
162 props->setIntValue("id", -1);
163 props->setBoolValue("radar/in-range", false);
164 props->setIntValue("refuel/tanker", false);
166 ai_list_itr = ai_list.erase(ai_list_itr);
169 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
170 FGAIBase *base = (*ai_list_itr).get();
171 processThermal((FGAIThermal*)base);
173 (*ai_list_itr)->update(_dt);
179 thermal_lift_node->setDoubleValue( strength ); // for thermals
183 FGAIManager::attach(FGAIBase *model)
185 //unsigned idx = mNumAiTypeModels[model->getType()];
186 const char* typeString = model->getTypeString();
187 SGPropertyNode* root = globals->get_props()->getNode("ai/models", true);
191 // find free index in the property tree, if we have
192 // more than 10000 mp-aircrafts in the property tree we should optimize the mp-server
193 for (i = 0; i < 10000; i++) {
194 p = root->getNode(typeString, i, false);
196 if (!p || !p->getBoolValue("valid", false))
199 if (p->getIntValue("id",-1)==model->getID()) {
200 p->setStringValue("callsign","***invalid node***"); //debug only, should never set!
204 p = root->getNode(typeString, i, true);
205 model->setManager(this, p);
206 ai_list.push_back(model);
208 ++(mNumAiTypeModels[model->getType()]);
209 model->init(model->getType()==FGAIBase::otAircraft
210 || model->getType()==FGAIBase::otMultiplayer
211 || model->getType()==FGAIBase::otStatic);
213 p->setBoolValue("valid", true);
217 FGAIManager::destroyObject( int ID ) {
218 ai_list_iterator ai_list_itr = ai_list.begin();
220 while(ai_list_itr != ai_list.end()) {
222 if ((*ai_list_itr)->getID() == ID) {
224 --(mNumAiTypeModels[(*ai_list_itr)->getType()]);
225 (*ai_list_itr)->unbind();
226 ai_list_itr = ai_list.erase(ai_list_itr);
234 FGAIManager::getNumAiObjects(void) const
240 FGAIManager::fetchUserState( void ) {
241 user_latitude = user_latitude_node->getDoubleValue();
242 user_longitude = user_longitude_node->getDoubleValue();
243 user_altitude = user_altitude_node->getDoubleValue();
244 user_heading = user_heading_node->getDoubleValue();
245 user_pitch = user_pitch_node->getDoubleValue();
246 user_yaw = user_yaw_node->getDoubleValue();
247 user_speed = user_speed_node->getDoubleValue() * 0.592484;
248 user_roll = user_roll_node->getDoubleValue();
249 wind_from_east = wind_from_east_node->getDoubleValue();
250 wind_from_north = wind_from_north_node->getDoubleValue();
253 // only keep the results from the nearest thermal
255 FGAIManager::processThermal( FGAIThermal* thermal ) {
256 thermal->update(_dt);
258 if ( thermal->_getRange() < range_nearest ) {
259 range_nearest = thermal->_getRange();
260 strength = thermal->getStrength();
268 FGAIManager::processScenario( const string &filename ) {
270 SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
275 SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
280 for (int i = 0; i < scenarios->nChildren(); i++) {
281 SGPropertyNode* scEntry = scenarios->getChild(i);
283 if (strcmp(scEntry->getName(), "entry"))
285 std::string type = scEntry->getStringValue("type", "aircraft");
287 if (type == "tanker") { // refueling scenarios
288 FGAITanker* tanker = new FGAITanker;
289 tanker->readFromScenario(scEntry);
292 } else if (type == "wingman") {
293 FGAIWingman* wingman = new FGAIWingman;
294 wingman->readFromScenario(scEntry);
297 } else if (type == "aircraft") {
298 FGAIAircraft* aircraft = new FGAIAircraft;
299 aircraft->readFromScenario(scEntry);
302 } else if (type == "ship") {
303 FGAIShip* ship = new FGAIShip;
304 ship->readFromScenario(scEntry);
307 } else if (type == "carrier") {
308 FGAICarrier* carrier = new FGAICarrier;
309 carrier->readFromScenario(scEntry);
312 } else if (type == "groundvehicle") {
313 FGAIGroundVehicle* groundvehicle = new FGAIGroundVehicle;
314 groundvehicle->readFromScenario(scEntry);
315 attach(groundvehicle);
317 } else if (type == "escort") {
318 FGAIEscort* escort = new FGAIEscort;
319 escort->readFromScenario(scEntry);
322 } else if (type == "thunderstorm") {
323 FGAIStorm* storm = new FGAIStorm;
324 storm->readFromScenario(scEntry);
327 } else if (type == "thermal") {
328 FGAIThermal* thermal = new FGAIThermal;
329 thermal->readFromScenario(scEntry);
332 } else if (type == "ballistic") {
333 FGAIBallistic* ballistic = new FGAIBallistic;
334 ballistic->readFromScenario(scEntry);
337 } else if (type == "static") {
338 FGAIStatic* aistatic = new FGAIStatic;
339 aistatic->readFromScenario(scEntry);
348 FGAIManager::loadScenarioFile(const std::string& filename)
350 SGPath path(globals->get_fg_root());
351 path.append("AI/" + filename + ".xml");
353 SGPropertyNode_ptr root = new SGPropertyNode;
354 readProperties(path.str(), root);
356 } catch (const sg_exception &) {
357 SG_LOG(SG_GENERAL, SG_DEBUG, "Incorrect path specified for AI "
358 "scenario: \"" << path.str() << "\"");
364 FGAIManager::getStartPosition(const string& id, const string& pid,
365 SGGeod& geodPos, double& hdng, SGVec3d& uvw)
368 SGPropertyNode* root = fgGetNode("sim/ai", true);
369 if (!root->getNode("enabled", true)->getBoolValue())
372 for (int i = 0 ; (!found) && i < root->nChildren() ; i++) {
373 SGPropertyNode *aiEntry = root->getChild( i );
374 if ( !strcmp( aiEntry->getName(), "scenario" ) ) {
375 string filename = aiEntry->getStringValue();
376 SGPropertyNode_ptr scenarioTop = loadScenarioFile(filename);
378 SGPropertyNode* scenarios = scenarioTop->getChild("scenario");
380 for (int i = 0; i < scenarios->nChildren(); i++) {
381 SGPropertyNode* scEntry = scenarios->getChild(i);
382 std::string type = scEntry->getStringValue("type");
383 std::string pnumber = scEntry->getStringValue("pennant-number");
384 std::string name = scEntry->getStringValue("name");
385 if (type == "carrier" && (pnumber == id || name == id)) {
386 SGSharedPtr<FGAICarrier> carrier = new FGAICarrier;
387 carrier->readFromScenario(scEntry);
389 if (carrier->getParkPosition(pid, geodPos, hdng, uvw)) {
403 FGAIManager::calcCollision(double alt, double lat, double lon, double fuse_range)
405 // we specify tgt extent (ft) according to the AIObject type
406 double tgt_ht[] = {0, 50, 100, 250, 0, 100, 0, 0, 50, 50, 20, 100, 50};
407 double tgt_length[] = {0, 100, 200, 750, 0, 50, 0, 0, 200, 100, 40, 200, 100};
408 ai_list_iterator ai_list_itr = ai_list.begin();
409 ai_list_iterator end = ai_list.end();
411 while (ai_list_itr != end) {
412 double tgt_alt = (*ai_list_itr)->_getAltitude();
413 int type = (*ai_list_itr)->getType();
414 tgt_ht[type] += fuse_range;
416 if (fabs(tgt_alt - alt) > tgt_ht[type] || type == FGAIBase::otBallistic
417 || type == FGAIBase::otStorm || type == FGAIBase::otThermal ) {
418 //SG_LOG(SG_GENERAL, SG_DEBUG, "AIManager: skipping "
419 // << fabs(tgt_alt - alt)
427 double tgt_lat = (*ai_list_itr)->_getLatitude();
428 double tgt_lon = (*ai_list_itr)->_getLongitude();
429 int id = (*ai_list_itr)->getID();
431 double range = calcRange(lat, lon, tgt_lat, tgt_lon);
433 //SG_LOG(SG_GENERAL, SG_DEBUG, "AIManager: AI list size "
435 // << " type " << type
437 // << " range " << range
438 // //<< " bearing " << bearing
439 // << " alt " << tgt_alt
442 tgt_length[type] += fuse_range;
444 if (range < tgt_length[type]){
445 SG_LOG(SG_GENERAL, SG_DEBUG, "AIManager: HIT! "
448 << " range " << range
449 << " alt " << tgt_alt
451 return (*ai_list_itr).get();
459 FGAIManager::calcRange(double lat, double lon, double lat2, double lon2) const
461 double course, az2, distance;
463 //calculate the bearing and range of the second pos from the first
464 geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
465 distance *= SG_METER_TO_FEET;