1 // AIManager.cxx Based on David Luff's AIMgr:
2 // - a global management class 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>
27 #include "AIManager.hxx"
28 #include "AIAircraft.hxx"
30 #include "AIBallistic.hxx"
31 #include "AIStorm.hxx"
32 #include "AIThermal.hxx"
37 FGAIManager::FGAIManager() {
42 scenario_filename = "";
45 FGAIManager::~FGAIManager() {
46 ai_list_itr = ai_list.begin();
47 while(ai_list_itr != ai_list.end()) {
48 delete (*ai_list_itr);
56 void FGAIManager::init() {
58 root = fgGetNode("sim/ai", true);
60 enabled = root->getNode("enabled", true)->getBoolValue();
65 wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
67 for (int i = 0; i < root->nChildren(); i++) {
68 const SGPropertyNode * entry = root->getChild(i);
70 if (!strcmp(entry->getName(), "scenario")){
71 scenario_filename = entry->getStringValue();
74 if (!strcmp(entry->getName(), "entry")) {
75 if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
77 rval = createAircraft( entry->getStringValue("class", ""),
78 entry->getStringValue("path"),
79 entry->getDoubleValue("latitude"),
80 entry->getDoubleValue("longitude"),
81 entry->getDoubleValue("altitude-ft"),
82 entry->getDoubleValue("heading"),
83 entry->getDoubleValue("speed-KTAS"),
85 entry->getDoubleValue("bank") );
87 } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
89 rval = createShip( entry->getStringValue("path"),
90 entry->getDoubleValue("latitude"),
91 entry->getDoubleValue("longitude"),
92 entry->getDoubleValue("altitude-ft"),
93 entry->getDoubleValue("heading"),
94 entry->getDoubleValue("speed-KTAS"),
95 entry->getDoubleValue("rudder") );
97 } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
99 rval = createBallistic( entry->getStringValue("path"),
100 entry->getDoubleValue("latitude"),
101 entry->getDoubleValue("longitude"),
102 entry->getDoubleValue("altitude-ft"),
103 entry->getDoubleValue("azimuth"),
104 entry->getDoubleValue("elevation"),
105 entry->getDoubleValue("speed") );
107 } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
109 rval = createStorm( entry->getStringValue("path"),
110 entry->getDoubleValue("latitude"),
111 entry->getDoubleValue("longitude"),
112 entry->getDoubleValue("altitude-ft"),
113 entry->getDoubleValue("heading"),
114 entry->getDoubleValue("speed-KTAS") );
116 } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
118 rval = createThermal( entry->getDoubleValue("latitude"),
119 entry->getDoubleValue("longitude"),
120 entry->getDoubleValue("strength-fps"),
121 entry->getDoubleValue("diameter-ft") );
127 if (scenario_filename != "") processScenario( scenario_filename );
132 void FGAIManager::bind() {
133 root = globals->get_props()->getNode("ai/models", true);
134 root->tie("count", SGRawValuePointer<int>(&numObjects));
138 void FGAIManager::unbind() {
139 root->untie("count");
143 void FGAIManager::update(double dt) {
145 // initialize these for finding nearest thermals
146 range_nearest = 10000.0;
154 ai_list_itr = ai_list.begin();
155 while(ai_list_itr != ai_list.end()) {
156 if ((*ai_list_itr)->getDie()) {
157 freeID((*ai_list_itr)->getID());
158 delete (*ai_list_itr);
160 if ( ai_list_itr == ai_list.begin() ) {
161 ai_list.erase(ai_list_itr);
162 ai_list_itr = ai_list.begin();
165 ai_list.erase(ai_list_itr--);
169 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
170 processThermal((FGAIThermal*)*ai_list_itr);
172 (*ai_list_itr)->update(_dt);
177 wind_from_down->setDoubleValue( strength );
181 // This function returns the next available ID
182 int FGAIManager::assignID() {
186 for (x=0; x<maxint; x++) {
188 id_itr = ids.begin();
189 while( id_itr != ids.end() ) {
190 if ((*id_itr) == x) used = true;
198 return -1; // no available ID's
202 // This function removes an ID from the ID array, making it
203 // available for assignment to another AI object
204 void FGAIManager::freeID( int ID ) {
205 id_itr = ids.begin();
206 while( id_itr != ids.end() ) {
215 int FGAIManager::createAircraft( string model_class, string path,
216 double latitude, double longitude, double altitude,
217 double heading, double speed, double pitch, double roll ) {
219 FGAIAircraft* ai_plane = new FGAIAircraft(this);
220 cout << "ai_plane: " << ai_plane << endl;
221 ai_list.push_back(ai_plane);
222 ai_plane->setID( assignID() );
224 if (model_class == "light") {
225 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
226 } else if (model_class == "ww2_fighter") {
227 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
228 } else if (model_class == "jet_transport") {
229 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
230 } else if (model_class == "jet_fighter") {
231 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
233 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
235 ai_plane->setHeading(heading);
236 ai_plane->setSpeed(speed);
237 ai_plane->setPath(path.c_str());
238 ai_plane->setAltitude(altitude);
239 ai_plane->setLongitude(longitude);
240 ai_plane->setLatitude(latitude);
241 ai_plane->setBank(roll);
244 return ai_plane->getID();
248 int FGAIManager::createAircraft( string model_class, string path,
249 FGAIFlightPlan* flightplan ) {
251 FGAIAircraft* ai_plane = new FGAIAircraft(this);
252 cout << "ai_plane1: " << ai_plane << endl;
253 ai_list.push_back(ai_plane);
254 ai_plane->setID( assignID() );
256 if (model_class == "light") {
257 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
258 } else if (model_class == "ww2_fighter") {
259 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
260 } else if (model_class == "jet_transport") {
261 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
262 } else if (model_class == "jet_fighter") {
263 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
265 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
267 ai_plane->setPath(path.c_str());
268 ai_plane->SetFlightPlan(flightplan);
271 return ai_plane->getID();
275 int FGAIManager::createShip( string path, double latitude, double longitude,
276 double altitude, double heading, double speed,
279 FGAIShip* ai_ship = new FGAIShip(this);
280 ai_list.push_back(ai_ship);
281 ai_ship->setID( assignID() );
283 ai_ship->setHeading(heading);
284 ai_ship->setSpeed(speed);
285 ai_ship->setPath(path.c_str());
286 ai_ship->setAltitude(altitude);
287 ai_ship->setLongitude(longitude);
288 ai_ship->setLatitude(latitude);
289 ai_ship->setBank(rudder);
292 return ai_ship->getID();
296 int FGAIManager::createBallistic( string path, double latitude, double longitude,
297 double altitude, double azimuth, double elevation,
300 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
301 ai_list.push_back(ai_ballistic);
302 ai_ballistic->setID( assignID() );
304 ai_ballistic->setAzimuth(azimuth);
305 ai_ballistic->setElevation(elevation);
306 ai_ballistic->setSpeed(speed);
307 ai_ballistic->setPath(path.c_str());
308 ai_ballistic->setAltitude(altitude);
309 ai_ballistic->setLongitude(longitude);
310 ai_ballistic->setLatitude(latitude);
311 ai_ballistic->init();
312 ai_ballistic->bind();
313 return ai_ballistic->getID();
316 int FGAIManager::createStorm( string path, double latitude, double longitude,
317 double altitude, double heading, double speed ) {
319 FGAIStorm* ai_storm = new FGAIStorm(this);
320 ai_list.push_back(ai_storm);
321 ai_storm->setID( assignID() );
323 ai_storm->setHeading(heading);
324 ai_storm->setSpeed(speed);
325 ai_storm->setPath(path.c_str());
326 ai_storm->setAltitude(altitude);
327 ai_storm->setLongitude(longitude);
328 ai_storm->setLatitude(latitude);
331 return ai_storm->getID();
334 int FGAIManager::createThermal( double latitude, double longitude,
335 double strength, double diameter ) {
337 FGAIThermal* ai_thermal = new FGAIThermal(this);
338 ai_list.push_back(ai_thermal);
339 ai_thermal->setID( assignID() );
341 ai_thermal->setLongitude(longitude);
342 ai_thermal->setLatitude(latitude);
343 ai_thermal->setMaxStrength(strength);
344 ai_thermal->setDiameter(diameter / 6076.11549);
347 return ai_thermal->getID();
350 void FGAIManager::destroyObject( int ID ) {
351 ai_list_itr = ai_list.begin();
352 while(ai_list_itr != ai_list.end()) {
353 if ((*ai_list_itr)->getID() == ID) {
355 delete (*ai_list_itr);
356 ai_list.erase(ai_list_itr);
365 // fetch the user's state every 10 sim cycles
366 void FGAIManager::fetchUserState( void ) {
368 if (dt_count == 10) {
369 user_latitude = fgGetDouble("/position/latitude-deg");
370 user_longitude = fgGetDouble("/position/longitude-deg");
371 user_altitude = fgGetDouble("/position/altitude-ft");
372 user_heading = fgGetDouble("/orientation/heading-deg");
373 user_pitch = fgGetDouble("/orientation/pitch-deg");
374 user_yaw = fgGetDouble("/orientation/side-slip-deg");
375 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
381 // only keep the results from the nearest thermal
382 void FGAIManager::processThermal( FGAIThermal* thermal ) {
383 thermal->update(_dt);
384 if ( thermal->_getRange() < range_nearest ) {
385 range_nearest = thermal->_getRange();
386 strength = thermal->getStrength();
391 void FGAIManager::processScenario( string filename ) {
392 //cout << "AIManager: creating a scenario." << endl;
393 FGAIScenario* s = new FGAIScenario( filename );
394 for (int i=0;i<s->nEntries();i++) {
395 FGAIScenario::entry* en = s->getNextEntry();
397 FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
398 if (en->aitype == "aircraft"){
399 createAircraft( en->aircraft_class, en->model_path, f);
406 int FGAIManager::getNum( FGAIBase::object_type ot ) {
407 ai_list_iterator itr = ai_list.begin();
409 while(itr != ai_list.end()) {
410 if ((*itr)->getType() == ot) {