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)->getStringValue();
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 ai_list.push_back(ai_plane);
221 ai_plane->setID( assignID() );
223 if (model_class == "light") {
224 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
225 } else if (model_class == "ww2_fighter") {
226 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
227 } else if (model_class == "jet_transport") {
228 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
229 } else if (model_class == "jet_fighter") {
230 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
232 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
234 ai_plane->setHeading(heading);
235 ai_plane->setSpeed(speed);
236 ai_plane->setPath(path.c_str());
237 ai_plane->setAltitude(altitude);
238 ai_plane->setLongitude(longitude);
239 ai_plane->setLatitude(latitude);
240 ai_plane->setBank(roll);
243 return ai_plane->getID();
247 int FGAIManager::createAircraft( string model_class, string path,
248 FGAIFlightPlan* flightplan ) {
250 FGAIAircraft* ai_plane = new FGAIAircraft(this);
251 ai_list.push_back(ai_plane);
252 ai_plane->setID( assignID() );
254 if (model_class == "light") {
255 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
256 } else if (model_class == "ww2_fighter") {
257 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
258 } else if (model_class == "jet_transport") {
259 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
260 } else if (model_class == "jet_fighter") {
261 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
263 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
265 ai_plane->setPath(path.c_str());
266 ai_plane->SetFlightPlan(flightplan);
269 return ai_plane->getID();
273 int FGAIManager::createShip( string path, double latitude, double longitude,
274 double altitude, double heading, double speed,
277 FGAIShip* ai_ship = new FGAIShip(this);
278 ai_list.push_back(ai_ship);
279 ai_ship->setID( assignID() );
281 ai_ship->setHeading(heading);
282 ai_ship->setSpeed(speed);
283 ai_ship->setPath(path.c_str());
284 ai_ship->setAltitude(altitude);
285 ai_ship->setLongitude(longitude);
286 ai_ship->setLatitude(latitude);
287 ai_ship->setBank(rudder);
290 return ai_ship->getID();
294 int FGAIManager::createBallistic( string path, double latitude, double longitude,
295 double altitude, double azimuth, double elevation,
298 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
299 ai_list.push_back(ai_ballistic);
300 ai_ballistic->setID( assignID() );
302 ai_ballistic->setAzimuth(azimuth);
303 ai_ballistic->setElevation(elevation);
304 ai_ballistic->setSpeed(speed);
305 ai_ballistic->setPath(path.c_str());
306 ai_ballistic->setAltitude(altitude);
307 ai_ballistic->setLongitude(longitude);
308 ai_ballistic->setLatitude(latitude);
309 ai_ballistic->init();
310 ai_ballistic->bind();
311 return ai_ballistic->getID();
314 int FGAIManager::createStorm( string path, double latitude, double longitude,
315 double altitude, double heading, double speed ) {
317 FGAIStorm* ai_storm = new FGAIStorm(this);
318 ai_list.push_back(ai_storm);
319 ai_storm->setID( assignID() );
321 ai_storm->setHeading(heading);
322 ai_storm->setSpeed(speed);
323 ai_storm->setPath(path.c_str());
324 ai_storm->setAltitude(altitude);
325 ai_storm->setLongitude(longitude);
326 ai_storm->setLatitude(latitude);
329 return ai_storm->getID();
332 int FGAIManager::createThermal( double latitude, double longitude,
333 double strength, double diameter ) {
335 FGAIThermal* ai_thermal = new FGAIThermal(this);
336 ai_list.push_back(ai_thermal);
337 ai_thermal->setID( assignID() );
339 ai_thermal->setLongitude(longitude);
340 ai_thermal->setLatitude(latitude);
341 ai_thermal->setMaxStrength(strength);
342 ai_thermal->setDiameter(diameter / 6076.11549);
345 return ai_thermal->getID();
348 void FGAIManager::destroyObject( int ID ) {
349 ai_list_itr = ai_list.begin();
350 while(ai_list_itr != ai_list.end()) {
351 if ((*ai_list_itr)->getID() == ID) {
353 delete (*ai_list_itr);
354 ai_list.erase(ai_list_itr);
363 // fetch the user's state every 10 sim cycles
364 void FGAIManager::fetchUserState( void ) {
366 if (dt_count == 10) {
367 user_latitude = fgGetDouble("/position/latitude-deg");
368 user_longitude = fgGetDouble("/position/longitude-deg");
369 user_altitude = fgGetDouble("/position/altitude-ft");
370 user_heading = fgGetDouble("/orientation/heading-deg");
371 user_pitch = fgGetDouble("/orientation/pitch-deg");
372 user_yaw = fgGetDouble("/orientation/side-slip-deg");
373 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
379 // only keep the results from the nearest thermal
380 void FGAIManager::processThermal( FGAIThermal* thermal ) {
381 thermal->update(_dt);
382 if ( thermal->_getRange() < range_nearest ) {
383 range_nearest = thermal->_getRange();
384 strength = thermal->getStrength();
389 void FGAIManager::processScenario( string filename ) {
390 //cout << "AIManager: creating a scenario." << endl;
391 FGAIScenario* s = new FGAIScenario( filename );
392 for (int i=0;i<s->nEntries();i++) {
393 FGAIScenario::entry* en = s->getNextEntry();
395 FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
396 createAircraft("jet_transport", "Aircraft/737/Models/boeing733.xml", f);