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() {
44 FGAIManager::~FGAIManager() {
45 ai_list_itr = ai_list.begin();
46 while(ai_list_itr != ai_list.end()) {
47 delete (*ai_list_itr);
55 void FGAIManager::init() {
57 root = fgGetNode("sim/ai", true);
58 wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
60 for (int i = 0; i < root->nChildren(); i++) {
61 const SGPropertyNode * entry = root->getChild(i);
63 if (!strcmp(entry->getName(), "entry")) {
64 if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
66 rval = createAircraft( entry->getStringValue("class", ""),
67 entry->getStringValue("path"),
68 entry->getDoubleValue("latitude"),
69 entry->getDoubleValue("longitude"),
70 entry->getDoubleValue("altitude-ft"),
71 entry->getDoubleValue("heading"),
72 entry->getDoubleValue("speed-KTAS"),
74 entry->getDoubleValue("bank") );
76 } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
78 rval = createShip( 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"),
84 entry->getDoubleValue("rudder") );
86 } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
88 rval = createBallistic( entry->getStringValue("path"),
89 entry->getDoubleValue("latitude"),
90 entry->getDoubleValue("longitude"),
91 entry->getDoubleValue("altitude-ft"),
92 entry->getDoubleValue("azimuth"),
93 entry->getDoubleValue("elevation"),
94 entry->getDoubleValue("speed") );
96 } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
98 rval = createStorm( entry->getStringValue("path"),
99 entry->getDoubleValue("latitude"),
100 entry->getDoubleValue("longitude"),
101 entry->getDoubleValue("altitude-ft"),
102 entry->getDoubleValue("heading"),
103 entry->getDoubleValue("speed-KTAS") );
105 } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
107 rval = createThermal( entry->getDoubleValue("latitude"),
108 entry->getDoubleValue("longitude"),
109 entry->getDoubleValue("strength-fps"),
110 entry->getDoubleValue("diameter-ft") );
116 //********** Flight Plan test code !!!! ****************
117 processScenario( "default_scenario" );
118 //*******************************************************
124 void FGAIManager::bind() {
125 root = globals->get_props()->getNode("ai/models", true);
126 root->tie("count", SGRawValuePointer<int>(&numObjects));
130 void FGAIManager::unbind() {
131 root->untie("count");
135 void FGAIManager::update(double dt) {
137 // initialize these for finding nearest thermals
138 range_nearest = 10000.0;
143 ai_list_itr = ai_list.begin();
144 while(ai_list_itr != ai_list.end()) {
145 if ((*ai_list_itr)->getDie()) {
146 freeID((*ai_list_itr)->getID());
147 delete (*ai_list_itr);
148 ai_list.erase(ai_list_itr);
153 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
154 processThermal((FGAIThermal*)*ai_list_itr);
156 (*ai_list_itr)->update(_dt);
161 wind_from_down->setDoubleValue( strength );
165 // This function returns the next available ID
166 int FGAIManager::assignID() {
170 for (x=0; x<maxint; x++) {
172 id_itr = ids.begin();
173 while( id_itr != ids.end() ) {
174 if ((*id_itr) == x) used = true;
182 return -1; // no available ID's
186 // This function removes an ID from the ID array, making it
187 // available for assignment to another AI object
188 void FGAIManager::freeID( int ID ) {
189 id_itr = ids.begin();
190 while( id_itr != ids.end() ) {
199 int FGAIManager::createAircraft( string model_class, string path,
200 double latitude, double longitude, double altitude,
201 double heading, double speed, double pitch, double roll ) {
203 FGAIAircraft* ai_plane = new FGAIAircraft(this);
204 ai_list.push_back(ai_plane);
205 ai_plane->setID( assignID() );
207 if (model_class == "light") {
208 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
209 } else if (model_class == "ww2_fighter") {
210 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
211 } else if (model_class == "jet_transport") {
212 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
213 } else if (model_class == "jet_fighter") {
214 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
216 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
218 ai_plane->setHeading(heading);
219 ai_plane->setSpeed(speed);
220 ai_plane->setPath(path.c_str());
221 ai_plane->setAltitude(altitude);
222 ai_plane->setLongitude(longitude);
223 ai_plane->setLatitude(latitude);
224 ai_plane->setBank(roll);
227 return ai_plane->getID();
231 int FGAIManager::createAircraft( string model_class, string path,
232 FGAIFlightPlan* flightplan ) {
234 FGAIAircraft* ai_plane = new FGAIAircraft(this);
235 ai_list.push_back(ai_plane);
236 ai_plane->setID( assignID() );
238 if (model_class == "light") {
239 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
240 } else if (model_class == "ww2_fighter") {
241 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
242 } else if (model_class == "jet_transport") {
243 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
244 } else if (model_class == "jet_fighter") {
245 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
247 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
249 ai_plane->setPath(path.c_str());
250 ai_plane->SetFlightPlan(flightplan);
253 return ai_plane->getID();
257 int FGAIManager::createShip( string path, double latitude, double longitude,
258 double altitude, double heading, double speed,
261 FGAIShip* ai_ship = new FGAIShip(this);
262 ai_list.push_back(ai_ship);
263 ai_ship->setID( assignID() );
265 ai_ship->setHeading(heading);
266 ai_ship->setSpeed(speed);
267 ai_ship->setPath(path.c_str());
268 ai_ship->setAltitude(altitude);
269 ai_ship->setLongitude(longitude);
270 ai_ship->setLatitude(latitude);
271 ai_ship->setBank(rudder);
274 return ai_ship->getID();
278 int FGAIManager::createBallistic( string path, double latitude, double longitude,
279 double altitude, double azimuth, double elevation,
282 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
283 ai_list.push_back(ai_ballistic);
284 ai_ballistic->setID( assignID() );
286 ai_ballistic->setAzimuth(azimuth);
287 ai_ballistic->setElevation(elevation);
288 ai_ballistic->setSpeed(speed);
289 ai_ballistic->setPath(path.c_str());
290 ai_ballistic->setAltitude(altitude);
291 ai_ballistic->setLongitude(longitude);
292 ai_ballistic->setLatitude(latitude);
293 ai_ballistic->init();
294 ai_ballistic->bind();
295 return ai_ballistic->getID();
298 int FGAIManager::createStorm( string path, double latitude, double longitude,
299 double altitude, double heading, double speed ) {
301 FGAIStorm* ai_storm = new FGAIStorm(this);
302 ai_list.push_back(ai_storm);
303 ai_storm->setID( assignID() );
305 ai_storm->setHeading(heading);
306 ai_storm->setSpeed(speed);
307 ai_storm->setPath(path.c_str());
308 ai_storm->setAltitude(altitude);
309 ai_storm->setLongitude(longitude);
310 ai_storm->setLatitude(latitude);
313 return ai_storm->getID();
316 int FGAIManager::createThermal( double latitude, double longitude,
317 double strength, double diameter ) {
319 FGAIThermal* ai_thermal = new FGAIThermal(this);
320 ai_list.push_back(ai_thermal);
321 ai_thermal->setID( assignID() );
323 ai_thermal->setLongitude(longitude);
324 ai_thermal->setLatitude(latitude);
325 ai_thermal->setMaxStrength(strength);
326 ai_thermal->setDiameter(diameter / 6076.11549);
329 return ai_thermal->getID();
332 void FGAIManager::destroyObject( int ID ) {
333 ai_list_itr = ai_list.begin();
334 while(ai_list_itr != ai_list.end()) {
335 if ((*ai_list_itr)->getID() == ID) {
337 delete (*ai_list_itr);
338 ai_list.erase(ai_list_itr);
347 // fetch the user's state every 10 sim cycles
348 void FGAIManager::fetchUserState( void ) {
350 if (dt_count == 10) {
351 user_latitude = fgGetDouble("/position/latitude-deg");
352 user_longitude = fgGetDouble("/position/longitude-deg");
353 user_altitude = fgGetDouble("/position/altitude-ft");
354 user_heading = fgGetDouble("/orientation/heading-deg");
355 user_pitch = fgGetDouble("/orientation/pitch-deg");
356 user_yaw = fgGetDouble("/orientation/side-slip-deg");
357 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
363 // only keep the results from the nearest thermal
364 void FGAIManager::processThermal( FGAIThermal* thermal ) {
365 thermal->update(_dt);
366 if ( thermal->_getRange() < range_nearest ) {
367 range_nearest = thermal->_getRange();
368 strength = thermal->getStrength();
373 void FGAIManager::processScenario( string filename ) {
374 //cout << "AIManager: creating a scenario." << endl;
375 FGAIScenario* s = new FGAIScenario( filename );
376 FGAIScenario::entry* en = s->getNextEntry();
378 FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
379 createAircraft("jet_transport", "Aircraft/737/Models/boeing733.xml", f);