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);
59 wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
61 for (int i = 0; i < root->nChildren(); i++) {
62 const SGPropertyNode * entry = root->getChild(i);
64 if (!strcmp(entry->getName(), "scenario")){
65 scenario_filename = entry->getStringValue();
68 if (!strcmp(entry->getName(), "entry")) {
69 if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
71 rval = createAircraft( entry->getStringValue("class", ""),
72 entry->getStringValue("path"),
73 entry->getDoubleValue("latitude"),
74 entry->getDoubleValue("longitude"),
75 entry->getDoubleValue("altitude-ft"),
76 entry->getDoubleValue("heading"),
77 entry->getDoubleValue("speed-KTAS"),
79 entry->getDoubleValue("bank") );
81 } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
83 rval = createShip( entry->getStringValue("path"),
84 entry->getDoubleValue("latitude"),
85 entry->getDoubleValue("longitude"),
86 entry->getDoubleValue("altitude-ft"),
87 entry->getDoubleValue("heading"),
88 entry->getDoubleValue("speed-KTAS"),
89 entry->getDoubleValue("rudder") );
91 } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
93 rval = createBallistic( entry->getStringValue("path"),
94 entry->getDoubleValue("latitude"),
95 entry->getDoubleValue("longitude"),
96 entry->getDoubleValue("altitude-ft"),
97 entry->getDoubleValue("azimuth"),
98 entry->getDoubleValue("elevation"),
99 entry->getDoubleValue("speed") );
101 } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
103 rval = createStorm( entry->getStringValue("path"),
104 entry->getDoubleValue("latitude"),
105 entry->getDoubleValue("longitude"),
106 entry->getDoubleValue("altitude-ft"),
107 entry->getDoubleValue("heading"),
108 entry->getDoubleValue("speed-KTAS") );
110 } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
112 rval = createThermal( entry->getDoubleValue("latitude"),
113 entry->getDoubleValue("longitude"),
114 entry->getDoubleValue("strength-fps"),
115 entry->getDoubleValue("diameter-ft") );
121 if (scenario_filename != "") processScenario( scenario_filename );
126 void FGAIManager::bind() {
127 root = globals->get_props()->getNode("ai/models", true);
128 root->tie("count", SGRawValuePointer<int>(&numObjects));
132 void FGAIManager::unbind() {
133 root->untie("count");
137 void FGAIManager::update(double dt) {
139 // initialize these for finding nearest thermals
140 range_nearest = 10000.0;
145 ai_list_itr = ai_list.begin();
146 while(ai_list_itr != ai_list.end()) {
147 if ((*ai_list_itr)->getDie()) {
148 freeID((*ai_list_itr)->getID());
149 delete (*ai_list_itr);
150 ai_list.erase(ai_list_itr);
155 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
156 processThermal((FGAIThermal*)*ai_list_itr);
158 (*ai_list_itr)->update(_dt);
163 wind_from_down->setDoubleValue( strength );
167 // This function returns the next available ID
168 int FGAIManager::assignID() {
172 for (x=0; x<maxint; x++) {
174 id_itr = ids.begin();
175 while( id_itr != ids.end() ) {
176 if ((*id_itr) == x) used = true;
184 return -1; // no available ID's
188 // This function removes an ID from the ID array, making it
189 // available for assignment to another AI object
190 void FGAIManager::freeID( int ID ) {
191 id_itr = ids.begin();
192 while( id_itr != ids.end() ) {
201 int FGAIManager::createAircraft( string model_class, string path,
202 double latitude, double longitude, double altitude,
203 double heading, double speed, double pitch, double roll ) {
205 FGAIAircraft* ai_plane = new FGAIAircraft(this);
206 ai_list.push_back(ai_plane);
207 ai_plane->setID( assignID() );
209 if (model_class == "light") {
210 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
211 } else if (model_class == "ww2_fighter") {
212 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
213 } else if (model_class == "jet_transport") {
214 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
215 } else if (model_class == "jet_fighter") {
216 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
218 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
220 ai_plane->setHeading(heading);
221 ai_plane->setSpeed(speed);
222 ai_plane->setPath(path.c_str());
223 ai_plane->setAltitude(altitude);
224 ai_plane->setLongitude(longitude);
225 ai_plane->setLatitude(latitude);
226 ai_plane->setBank(roll);
229 return ai_plane->getID();
233 int FGAIManager::createAircraft( string model_class, string path,
234 FGAIFlightPlan* flightplan ) {
236 FGAIAircraft* ai_plane = new FGAIAircraft(this);
237 ai_list.push_back(ai_plane);
238 ai_plane->setID( assignID() );
240 if (model_class == "light") {
241 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
242 } else if (model_class == "ww2_fighter") {
243 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
244 } else if (model_class == "jet_transport") {
245 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
246 } else if (model_class == "jet_fighter") {
247 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
249 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
251 ai_plane->setPath(path.c_str());
252 ai_plane->SetFlightPlan(flightplan);
255 return ai_plane->getID();
259 int FGAIManager::createShip( string path, double latitude, double longitude,
260 double altitude, double heading, double speed,
263 FGAIShip* ai_ship = new FGAIShip(this);
264 ai_list.push_back(ai_ship);
265 ai_ship->setID( assignID() );
267 ai_ship->setHeading(heading);
268 ai_ship->setSpeed(speed);
269 ai_ship->setPath(path.c_str());
270 ai_ship->setAltitude(altitude);
271 ai_ship->setLongitude(longitude);
272 ai_ship->setLatitude(latitude);
273 ai_ship->setBank(rudder);
276 return ai_ship->getID();
280 int FGAIManager::createBallistic( string path, double latitude, double longitude,
281 double altitude, double azimuth, double elevation,
284 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
285 ai_list.push_back(ai_ballistic);
286 ai_ballistic->setID( assignID() );
288 ai_ballistic->setAzimuth(azimuth);
289 ai_ballistic->setElevation(elevation);
290 ai_ballistic->setSpeed(speed);
291 ai_ballistic->setPath(path.c_str());
292 ai_ballistic->setAltitude(altitude);
293 ai_ballistic->setLongitude(longitude);
294 ai_ballistic->setLatitude(latitude);
295 ai_ballistic->init();
296 ai_ballistic->bind();
297 return ai_ballistic->getID();
300 int FGAIManager::createStorm( string path, double latitude, double longitude,
301 double altitude, double heading, double speed ) {
303 FGAIStorm* ai_storm = new FGAIStorm(this);
304 ai_list.push_back(ai_storm);
305 ai_storm->setID( assignID() );
307 ai_storm->setHeading(heading);
308 ai_storm->setSpeed(speed);
309 ai_storm->setPath(path.c_str());
310 ai_storm->setAltitude(altitude);
311 ai_storm->setLongitude(longitude);
312 ai_storm->setLatitude(latitude);
315 return ai_storm->getID();
318 int FGAIManager::createThermal( double latitude, double longitude,
319 double strength, double diameter ) {
321 FGAIThermal* ai_thermal = new FGAIThermal(this);
322 ai_list.push_back(ai_thermal);
323 ai_thermal->setID( assignID() );
325 ai_thermal->setLongitude(longitude);
326 ai_thermal->setLatitude(latitude);
327 ai_thermal->setMaxStrength(strength);
328 ai_thermal->setDiameter(diameter / 6076.11549);
331 return ai_thermal->getID();
334 void FGAIManager::destroyObject( int ID ) {
335 ai_list_itr = ai_list.begin();
336 while(ai_list_itr != ai_list.end()) {
337 if ((*ai_list_itr)->getID() == ID) {
339 delete (*ai_list_itr);
340 ai_list.erase(ai_list_itr);
349 // fetch the user's state every 10 sim cycles
350 void FGAIManager::fetchUserState( void ) {
352 if (dt_count == 10) {
353 user_latitude = fgGetDouble("/position/latitude-deg");
354 user_longitude = fgGetDouble("/position/longitude-deg");
355 user_altitude = fgGetDouble("/position/altitude-ft");
356 user_heading = fgGetDouble("/orientation/heading-deg");
357 user_pitch = fgGetDouble("/orientation/pitch-deg");
358 user_yaw = fgGetDouble("/orientation/side-slip-deg");
359 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
365 // only keep the results from the nearest thermal
366 void FGAIManager::processThermal( FGAIThermal* thermal ) {
367 thermal->update(_dt);
368 if ( thermal->_getRange() < range_nearest ) {
369 range_nearest = thermal->_getRange();
370 strength = thermal->getStrength();
375 void FGAIManager::processScenario( string filename ) {
376 //cout << "AIManager: creating a scenario." << endl;
377 FGAIScenario* s = new FGAIScenario( filename );
378 for (int i=0;i<s->nEntries();i++) {
379 FGAIScenario::entry* en = s->getNextEntry();
381 FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
382 createAircraft("jet_transport", "Aircraft/737/Models/boeing733.xml", f);