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() {
57 root = fgGetNode("sim/ai", true);
59 enabled = root->getNode("enabled", true)->getBoolValue();
63 wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
65 scenario_filename = root->getNode("scenario", true)->getStringValue();
67 if (scenario_filename != "") processScenario( scenario_filename );
72 void FGAIManager::bind() {
73 root = globals->get_props()->getNode("ai/models", true);
74 root->tie("count", SGRawValuePointer<int>(&numObjects));
78 void FGAIManager::unbind() {
83 void FGAIManager::update(double dt) {
85 // initialize these for finding nearest thermals
86 range_nearest = 10000.0;
94 ai_list_itr = ai_list.begin();
95 while(ai_list_itr != ai_list.end()) {
96 if ((*ai_list_itr)->getDie()) {
97 freeID((*ai_list_itr)->getID());
98 delete (*ai_list_itr);
100 if ( ai_list_itr == ai_list.begin() ) {
101 ai_list.erase(ai_list_itr);
102 ai_list_itr = ai_list.begin();
105 ai_list.erase(ai_list_itr--);
109 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
110 processThermal((FGAIThermal*)*ai_list_itr);
112 (*ai_list_itr)->update(_dt);
117 wind_from_down->setDoubleValue( strength );
122 // This function returns the next available ID
123 int FGAIManager::assignID() {
127 for (x=1; x<maxint; x++) {
129 id_itr = ids.begin();
130 while( id_itr != ids.end() ) {
131 if ((*id_itr) == x) used = true;
139 return -1; // no available ID's
143 // This function removes an ID from the ID array, making it
144 // available for assignment to another AI object
145 void FGAIManager::freeID( int ID ) {
146 id_itr = ids.begin();
147 while( id_itr != ids.end() ) {
156 int FGAIManager::createAircraft( string model_class, string path,
157 double latitude, double longitude, double altitude,
158 double heading, double speed, double roll ) {
160 FGAIAircraft* ai_plane = new FGAIAircraft(this);
161 ai_list.push_back(ai_plane);
162 ai_plane->setID( assignID() );
164 if (model_class == "light") {
165 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
166 } else if (model_class == "ww2_fighter") {
167 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
168 } else if (model_class == "jet_transport") {
169 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
170 } else if (model_class == "jet_fighter") {
171 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
172 } else if (model_class == "tanker") {
173 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
174 ai_plane->SetTanker(true);
176 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
178 ai_plane->setHeading(heading);
179 ai_plane->setSpeed(speed);
180 ai_plane->setPath(path.c_str());
181 ai_plane->setAltitude(altitude);
182 ai_plane->setLongitude(longitude);
183 ai_plane->setLatitude(latitude);
184 ai_plane->setBank(roll);
187 return ai_plane->getID();
191 int FGAIManager::createAircraft( string model_class, string path,
192 FGAIFlightPlan* flightplan ) {
194 FGAIAircraft* ai_plane = new FGAIAircraft(this);
195 ai_list.push_back(ai_plane);
196 ai_plane->setID( assignID() );
198 if (model_class == "light") {
199 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
200 } else if (model_class == "ww2_fighter") {
201 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
202 } else if (model_class == "jet_transport") {
203 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
204 } else if (model_class == "jet_fighter") {
205 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
206 } else if (model_class == "tanker") {
207 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
208 ai_plane->SetTanker(true);
210 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
212 ai_plane->setPath(path.c_str());
213 ai_plane->SetFlightPlan(flightplan);
216 return ai_plane->getID();
220 int FGAIManager::createShip( string path, double latitude, double longitude,
221 double altitude, double heading, double speed,
224 FGAIShip* ai_ship = new FGAIShip(this);
225 ai_list.push_back(ai_ship);
226 ai_ship->setID( assignID() );
228 ai_ship->setHeading(heading);
229 ai_ship->setSpeed(speed);
230 ai_ship->setPath(path.c_str());
231 ai_ship->setAltitude(altitude);
232 ai_ship->setLongitude(longitude);
233 ai_ship->setLatitude(latitude);
234 ai_ship->setBank(rudder);
237 return ai_ship->getID();
240 int FGAIManager::createShip( string path, FGAIFlightPlan* flightplan ) {
242 FGAIShip* ai_ship = new FGAIShip(this);
243 ai_list.push_back(ai_ship);
244 ai_ship->setID( assignID() );
246 ai_ship->setPath(path.c_str());
247 ai_ship->setFlightPlan(flightplan);
250 return ai_ship->getID();
254 int FGAIManager::createBallistic( string path, double latitude, double longitude,
255 double altitude, double azimuth, double elevation,
256 double speed, double eda, double life, double buoyancy,
257 double wind_from_east, double wind_from_north, bool wind ) {
259 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
260 ai_list.push_back(ai_ballistic);
261 ai_ballistic->setID( assignID() );
263 ai_ballistic->setAzimuth(azimuth);
264 ai_ballistic->setElevation(elevation);
265 ai_ballistic->setSpeed(speed);
266 ai_ballistic->setPath(path.c_str());
267 ai_ballistic->setAltitude(altitude);
268 ai_ballistic->setLongitude(longitude);
269 ai_ballistic->setLatitude(latitude);
270 ai_ballistic->setDragArea(eda);
271 ai_ballistic->setLife(life);
272 ai_ballistic->setBuoyancy(buoyancy);
273 ai_ballistic->setWind_from_east(wind_from_east);
274 ai_ballistic->setWind_from_north(wind_from_north);
275 ai_ballistic->setWind(wind);
276 ai_ballistic->init();
277 ai_ballistic->bind();
278 return ai_ballistic->getID();
281 int FGAIManager::createStorm( string path, double latitude, double longitude,
282 double altitude, double heading, double speed ) {
284 FGAIStorm* ai_storm = new FGAIStorm(this);
285 ai_list.push_back(ai_storm);
286 ai_storm->setID( assignID() );
288 ai_storm->setHeading(heading);
289 ai_storm->setSpeed(speed);
290 ai_storm->setPath(path.c_str());
291 ai_storm->setAltitude(altitude);
292 ai_storm->setLongitude(longitude);
293 ai_storm->setLatitude(latitude);
296 return ai_storm->getID();
299 int FGAIManager::createThermal( double latitude, double longitude,
300 double strength, double diameter ) {
302 FGAIThermal* ai_thermal = new FGAIThermal(this);
303 ai_list.push_back(ai_thermal);
304 ai_thermal->setID( assignID() );
306 ai_thermal->setLongitude(longitude);
307 ai_thermal->setLatitude(latitude);
308 ai_thermal->setMaxStrength(strength);
309 ai_thermal->setDiameter(diameter / 6076.11549);
312 return ai_thermal->getID();
315 void FGAIManager::destroyObject( int ID ) {
316 ai_list_itr = ai_list.begin();
317 while(ai_list_itr != ai_list.end()) {
318 if ((*ai_list_itr)->getID() == ID) {
320 delete (*ai_list_itr);
321 ai_list.erase(ai_list_itr);
330 // fetch the user's state every 10 sim cycles
331 void FGAIManager::fetchUserState( void ) {
333 if (dt_count == 10) {
334 user_latitude = fgGetDouble("/position/latitude-deg");
335 user_longitude = fgGetDouble("/position/longitude-deg");
336 user_altitude = fgGetDouble("/position/altitude-ft");
337 user_heading = fgGetDouble("/orientation/heading-deg");
338 user_pitch = fgGetDouble("/orientation/pitch-deg");
339 user_yaw = fgGetDouble("/orientation/side-slip-deg");
340 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
346 // only keep the results from the nearest thermal
347 void FGAIManager::processThermal( FGAIThermal* thermal ) {
348 thermal->update(_dt);
349 if ( thermal->_getRange() < range_nearest ) {
350 range_nearest = thermal->_getRange();
351 strength = thermal->getStrength();
356 void FGAIManager::processScenario( string filename ) {
357 FGAIScenario* s = new FGAIScenario( filename );
360 for (int i=0;i<s->nEntries();i++) {
361 FGAIScenario::entry* en = s->getNextEntry();
364 if (en->flightplan != ""){
365 f = new FGAIFlightPlan( en->flightplan );
367 if (en->aitype == "aircraft"){
369 createAircraft( en->aircraft_class, en->model_path, f );
371 createAircraft( en->aircraft_class, en->model_path, en->latitude,
372 en->longitude, en->altitude, en->heading,
373 en->speed, en->roll );
375 } else if (en->aitype == "ship"){
377 createShip( en->model_path, f );
379 createShip( en->model_path, en->latitude,
380 en->longitude, en->altitude, en->heading,
381 en->speed, en->rudder );
384 } else if (en->aitype == "storm"){
385 createStorm( en->model_path, en->latitude, en->longitude,
386 en->altitude, en->heading, en->speed );
387 } else if (en->aitype == "thermal"){
388 createThermal( en->latitude, en->longitude, en->strength,
390 } else if (en->aitype == "ballistic"){
391 createBallistic( en->model_path, en->latitude, en->longitude,
392 en->altitude, en->azimuth, en->elevation, en->speed,
393 en->eda, en->life, en->buoyancy, en->wind_from_east,
394 en-> wind_from_north, en->wind);
402 int FGAIManager::getNum( FGAIBase::object_type ot ) {
403 ai_list_iterator itr = ai_list.begin();
405 while(itr != ai_list.end()) {
406 if ((*itr)->getType() == ot) {