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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <simgear/misc/sg_path.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Main/globals.hxx>
25 #include <Airports/simple.hxx>
26 #include <Traffic/SchedFlight.hxx>
27 #include <Traffic/Schedule.hxx>
28 #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"
44 FGAIManager::FGAIManager() {
46 for (int i=0; i < FGAIBase::MAX_OBJECTS; i++)
50 scenario_filename = "";
54 FGAIManager::~FGAIManager() {
55 ai_list_iterator ai_list_itr = ai_list.begin();
56 while(ai_list_itr != ai_list.end()) {
57 (*ai_list_itr)->unbind();
58 delete (*ai_list_itr);
65 void FGAIManager::init() {
66 root = fgGetNode("sim/ai", true);
68 enabled = root->getNode("enabled", true)->getBoolValue();
72 wind_from_down_node = fgGetNode("/environment/wind-from-down-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_speed_node = fgGetNode("/velocities/uBody-fps", true);
84 for(int i = 0 ; i < root->nChildren() ; i++) {
85 SGPropertyNode *aiEntry = root->getChild( i );
86 if( !strcmp( aiEntry->getName(), "scenario" ) ) {
87 scenario_filename = aiEntry->getStringValue();
88 if (scenario_filename != "") processScenario( scenario_filename );
95 void FGAIManager::reinit() {
100 void FGAIManager::bind() {
101 root = globals->get_props()->getNode("ai/models", true);
102 root->tie("count", SGRawValuePointer<int>(&numObjects[0]));
106 void FGAIManager::unbind() {
107 root->untie("count");
111 void FGAIManager::update(double dt) {
113 // initialize these for finding nearest thermals
114 range_nearest = 10000.0;
116 FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
123 ai_list_iterator ai_list_itr = ai_list.begin();
124 while(ai_list_itr != ai_list.end()) {
125 if ((*ai_list_itr)->getDie()) {
126 tmgr->release((*ai_list_itr)->getID());
127 --numObjects[(*ai_list_itr)->getType()];
129 (*ai_list_itr)->unbind();
130 delete (*ai_list_itr);
131 if ( ai_list_itr == ai_list.begin() ) {
132 ai_list.erase(ai_list_itr);
133 ai_list_itr = ai_list.begin();
136 ai_list.erase(ai_list_itr--);
140 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
141 processThermal((FGAIThermal*)*ai_list_itr);
143 (*ai_list_itr)->update(_dt);
148 wind_from_down_node->setDoubleValue( strength ); // for thermals
154 FGAIManager::createAircraft( FGAIModelEntity *entity, FGAISchedule *ref) {
156 FGAIAircraft* ai_plane = new FGAIAircraft(this, ref);
157 ai_list.push_back(ai_plane);
159 ++numObjects[FGAIBase::otAircraft];
160 if (entity->m_class == "light") {
161 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
162 } else if (entity->m_class == "ww2_fighter") {
163 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
164 } else if (entity->m_class == "jet_transport") {
165 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
166 } else if (entity->m_class == "jet_fighter") {
167 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
168 } else if (entity->m_class == "tanker") {
169 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
170 ai_plane->SetTanker(true);
172 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
174 ai_plane->setAcType(entity->acType);
175 ai_plane->setCompany(entity->company);
176 ai_plane->setHeading(entity->heading);
177 ai_plane->setSpeed(entity->speed);
178 ai_plane->setPath(entity->path.c_str());
179 ai_plane->setAltitude(entity->altitude);
180 ai_plane->setLongitude(entity->longitude);
181 ai_plane->setLatitude(entity->latitude);
182 ai_plane->setBank(entity->roll);
185 ai_plane->SetFlightPlan(entity->fp);
187 if (entity->repeat) {
188 ai_plane->GetFlightPlan()->setRepeat(true);
196 FGAIManager::createShip( FGAIModelEntity *entity ) {
198 // cout << "creating ship" << endl;
200 FGAIShip* ai_ship = new FGAIShip(this);
201 ai_list.push_back(ai_ship);
203 ++numObjects[FGAIBase::otShip];
204 ai_ship->setHeading(entity->heading);
205 ai_ship->setSpeed(entity->speed);
206 ai_ship->setPath(entity->path.c_str());
207 ai_ship->setAltitude(entity->altitude);
208 ai_ship->setLongitude(entity->longitude);
209 ai_ship->setLatitude(entity->latitude);
210 ai_ship->setRudder(entity->rudder);
211 ai_ship->setName(entity->name);
214 ai_ship->setFlightPlan(entity->fp);
223 FGAIManager::createCarrier( FGAIModelEntity *entity ) {
225 // cout << "creating carrier" << endl;
227 FGAICarrier* ai_carrier = new FGAICarrier(this);
228 ai_list.push_back(ai_carrier);
230 ++numObjects[FGAIBase::otCarrier];
231 ai_carrier->setHeading(entity->heading);
232 ai_carrier->setSpeed(entity->speed);
233 ai_carrier->setPath(entity->path.c_str());
234 ai_carrier->setAltitude(entity->altitude);
235 ai_carrier->setLongitude(entity->longitude);
236 ai_carrier->setLatitude(entity->latitude);
237 ai_carrier->setRudder(entity->rudder);
238 ai_carrier->setSolidObjects(entity->solid_objects);
239 ai_carrier->setWireObjects(entity->wire_objects);
240 ai_carrier->setCatapultObjects(entity->catapult_objects);
241 ai_carrier->setParkingPositions(entity->ppositions);
242 ai_carrier->setRadius(entity->radius);
243 ai_carrier->setSign(entity->pennant_number);
244 ai_carrier->setName(entity->name);
245 ai_carrier->setFlolsOffset(entity->flols_offset);
246 ai_carrier->setWind_from_east(entity->wind_from_east);
247 ai_carrier->setWind_from_north(entity->wind_from_north);
248 ai_carrier->setTACANChannelID(entity->TACAN_channel_ID);
249 ai_carrier->setMaxLat(entity->max_lat);
250 ai_carrier->setMinLat(entity->min_lat);
251 ai_carrier->setMaxLong(entity->max_long);
252 ai_carrier->setMinLong(entity->min_long);
256 ai_carrier->setFlightPlan(entity->fp);
265 FGAIManager::createBallistic( FGAIModelEntity *entity ) {
267 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
268 ai_list.push_back(ai_ballistic);
270 ++numObjects[FGAIBase::otBallistic];
271 ai_ballistic->setAzimuth(entity->azimuth);
272 ai_ballistic->setElevation(entity->elevation);
273 ai_ballistic->setSpeed(entity->speed);
274 ai_ballistic->setPath(entity->path.c_str());
275 ai_ballistic->setAltitude(entity->altitude);
276 ai_ballistic->setLongitude(entity->longitude);
277 ai_ballistic->setLatitude(entity->latitude);
278 ai_ballistic->setDragArea(entity->eda);
279 ai_ballistic->setLife(entity->life);
280 ai_ballistic->setBuoyancy(entity->buoyancy);
281 ai_ballistic->setWind_from_east(entity->wind_from_east);
282 ai_ballistic->setWind_from_north(entity->wind_from_north);
283 ai_ballistic->setWind(entity->wind);
284 ai_ballistic->setRoll(entity->roll);
285 ai_ballistic->setCd(entity->cd);
286 ai_ballistic->setMass(entity->mass);
287 ai_ballistic->setStabilisation(entity->aero_stabilised);
288 ai_ballistic->init();
289 ai_ballistic->bind();
294 FGAIManager::createStorm( FGAIModelEntity *entity ) {
296 FGAIStorm* ai_storm = new FGAIStorm(this);
298 ++numObjects[FGAIBase::otStorm];
299 ai_storm->setHeading(entity->heading);
300 ai_storm->setSpeed(entity->speed);
301 ai_storm->setPath(entity->path.c_str());
302 ai_storm->setAltitude(entity->altitude);
303 ai_storm->setDiameter(entity->diameter / 6076.11549);
304 ai_storm->setHeight(entity->height_msl);
305 ai_storm->setStrengthNorm(entity->turb_strength);
306 ai_storm->setLongitude(entity->longitude);
307 ai_storm->setLatitude(entity->latitude);
310 ai_list.push_back(ai_storm);
315 FGAIManager::createThermal( FGAIModelEntity *entity ) {
317 FGAIThermal* ai_thermal = new FGAIThermal(this);
319 ++numObjects[FGAIBase::otThermal];
320 ai_thermal->setLongitude(entity->longitude);
321 ai_thermal->setLatitude(entity->latitude);
322 ai_thermal->setMaxStrength(entity->strength);
323 ai_thermal->setDiameter(entity->diameter / 6076.11549);
324 ai_thermal->setHeight(entity->height_msl);
327 ai_list.push_back(ai_thermal);
332 FGAIManager::createStatic( FGAIModelEntity *entity ) {
334 // cout << "creating static object" << endl;
336 FGAIStatic* ai_static = new FGAIStatic(this);
337 ai_list.push_back(ai_static);
339 ++numObjects[FGAIBase::otStatic];
340 ai_static->setHeading(entity->heading);
341 ai_static->setPath(entity->path.c_str());
342 ai_static->setAltitude(entity->altitude);
343 ai_static->setLongitude(entity->longitude);
344 ai_static->setLatitude(entity->latitude);
350 void FGAIManager::destroyObject( int ID ) {
351 ai_list_iterator ai_list_itr = ai_list.begin();
352 while(ai_list_itr != ai_list.end()) {
353 if ((*ai_list_itr)->getID() == ID) {
355 --numObjects[(*ai_list_itr)->getType()];
356 (*ai_list_itr)->unbind();
357 delete (*ai_list_itr);
358 ai_list.erase(ai_list_itr);
367 void FGAIManager::fetchUserState( void ) {
368 user_latitude = user_latitude_node->getDoubleValue();
369 user_longitude = user_longitude_node->getDoubleValue();
370 user_altitude = user_altitude_node->getDoubleValue();
371 user_heading = user_heading_node->getDoubleValue();
372 user_pitch = user_pitch_node->getDoubleValue();
373 user_yaw = user_yaw_node->getDoubleValue();
374 user_speed = user_speed_node->getDoubleValue() * 0.592484;
375 wind_from_east = wind_from_east_node->getDoubleValue();
376 wind_from_north = wind_from_north_node->getDoubleValue();
382 // only keep the results from the nearest thermal
383 void FGAIManager::processThermal( FGAIThermal* thermal ) {
384 thermal->update(_dt);
385 if ( thermal->_getRange() < range_nearest ) {
386 range_nearest = thermal->_getRange();
387 strength = thermal->getStrength();
392 void FGAIManager::processScenario( const string &filename ) {
393 FGAIScenario* s = new FGAIScenario( filename );
394 for (int i=0;i<s->nEntries();i++) {
395 FGAIModelEntity* const en = s->getNextEntry();
398 if ( en->m_type == "aircraft") {
399 createAircraft( en );
401 } else if ( en->m_type == "ship") {
404 } else if ( en->m_type == "carrier") {
407 } else if ( en->m_type == "thunderstorm") {
410 } else if ( en->m_type == "thermal") {
413 } else if ( en->m_type == "ballistic") {
414 createBallistic( en );
416 } else if ( en->m_type == "static") {
425 // This code keeps track of models that have already been loaded
426 // Eventually we'd prbably need to find a way to keep track of models
427 // that are unloaded again
428 ssgBranch * FGAIManager::getModel(const string& path) const
430 ModelVecIterator i = loadedModels.begin();
431 while (i != loadedModels.end())
433 if (i->getPath() == path)
434 return i->getModelId();
440 void FGAIManager::setModel(const string& path, ssgBranch *model)
442 loadedModels.push_back(FGModelID(path,model));
445 bool FGAIManager::getStartPosition(const string& id, const string& pid,
446 Point3D& geodPos, double& heading,
449 SGPropertyNode* root = fgGetNode("sim/ai", true);
450 if (!root->getNode("enabled", true)->getBoolValue())
454 for(int i = 0 ; (!found) && i < root->nChildren() ; i++) {
455 SGPropertyNode *aiEntry = root->getChild( i );
456 if( !strcmp( aiEntry->getName(), "scenario" ) ) {
457 string filename = aiEntry->getStringValue();
458 FGAIScenario* s = new FGAIScenario( filename );
459 for (int i=0; i<s->nEntries(); i++) {
460 FGAIModelEntity* en = s->getNextEntry();
461 if (en && en->m_type == "carrier" &&
462 (en->pennant_number == id || en->name == id)) {
463 FGAICarrier* ai_carrier = new FGAICarrier(0);
464 ai_carrier->setHeading(en->heading);
465 ai_carrier->setSpeed(en->speed);
466 ai_carrier->setAltitude(en->altitude);
467 ai_carrier->setLongitude(en->longitude);
468 ai_carrier->setLatitude(en->latitude);
469 ai_carrier->setBank(en->rudder);
470 ai_carrier->setParkingPositions(en->ppositions);
471 ai_carrier->setWind_from_east(en->wind_from_east);
472 ai_carrier->setWind_from_north(en->wind_from_north);
473 //ai_carrier->setTACANFreq(en->TACAN_freq);
475 if (ai_carrier->getParkPosition(pid, geodPos, heading, uvw)) {