]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
dad75ddac63232ab953e82c079b68efc54639d25
[flightgear.git] / src / AIModel / AIManager.cxx
1 // AIManager.cxx  Based on David Luff's AIMgr:
2 // - a global management class for AI objects
3 //
4 // Written by David Culp, started October 2003.
5 // - davidculp2@comcast.net
6 //
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.
11 //
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.
16 //
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.
20
21 #include <simgear/misc/sg_path.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Main/globals.hxx>
24
25 #include <list>
26
27 #include "AIManager.hxx"
28 #include "AIAircraft.hxx"
29 #include "AIShip.hxx"
30 #include "AIBallistic.hxx"
31 #include "AIStorm.hxx"
32 #include "AIThermal.hxx"
33
34 SG_USING_STD(list);
35
36
37 FGAIManager::FGAIManager() {
38   initDone = false;
39   numObjects = 0;
40   _dt = 0.0;
41   dt_count = 9;
42   scenario_filename = "";
43 }
44
45 FGAIManager::~FGAIManager() {
46   ai_list_itr = ai_list.begin();
47   while(ai_list_itr != ai_list.end()) {
48       delete (*ai_list_itr);
49       ++ai_list_itr;
50     }
51   ai_list.clear();
52   ids.clear();
53 }
54
55
56 void FGAIManager::init() {
57   int rval;
58   root = fgGetNode("sim/ai", true);
59
60   enabled = root->getNode("enabled", true)->getBoolValue();
61   if (!enabled)
62       return;
63
64
65   wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
66
67   for (int i = 0; i < root->nChildren(); i++) {
68     const SGPropertyNode * entry = root->getChild(i);
69
70     if (!strcmp(entry->getName(), "scenario")){
71       scenario_filename = entry->getStringValue();
72     }
73
74     if (!strcmp(entry->getName(), "entry")) {
75       if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { 
76
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"),
84                                0.0, 
85                                entry->getDoubleValue("bank") );
86
87       } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
88
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") );
96
97       } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
98
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") );
106
107       } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
108
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") );
115
116       } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
117
118         rval = createThermal( entry->getDoubleValue("latitude"),
119                               entry->getDoubleValue("longitude"),
120                               entry->getDoubleValue("strength-fps"),
121                               entry->getDoubleValue("diameter-ft") );
122
123       }       
124     }
125   }
126
127   if (scenario_filename != "") processScenario( scenario_filename );
128   initDone = true;
129 }
130
131
132 void FGAIManager::bind() {
133    root = globals->get_props()->getNode("ai/models", true);
134    root->tie("count", SGRawValuePointer<int>(&numObjects));
135 }
136
137
138 void FGAIManager::unbind() {
139     root->untie("count");
140 }
141
142
143 void FGAIManager::update(double dt) {
144
145         // initialize these for finding nearest thermals
146         range_nearest = 10000.0;
147         strength = 0.0;
148
149         if (!enabled)
150             return;
151
152         _dt = dt;       
153
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);
159                    --numObjects;
160                    if ( ai_list_itr == ai_list.begin() ) {
161                        ai_list.erase(ai_list_itr);
162                        ai_list_itr = ai_list.begin();
163                        continue;
164                    } else {
165                        ai_list.erase(ai_list_itr--);
166                    }
167                 } else {
168                    fetchUserState();
169                    if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
170                        processThermal((FGAIThermal*)*ai_list_itr); 
171                    } else { 
172                       (*ai_list_itr)->update(_dt);
173                    }
174                 }
175                 ++ai_list_itr;
176         }
177         wind_from_down->setDoubleValue( strength );
178 }
179
180
181 // This function returns the next available ID
182 int FGAIManager::assignID() {
183   int maxint = 30000;
184   int x; 
185   bool used;
186   for (x=0; x<maxint; x++) {
187      used = false;
188      id_itr = ids.begin();
189      while( id_itr != ids.end() ) {
190        if ((*id_itr) == x) used = true;
191        ++id_itr;
192      }
193      if (!used) {
194        ids.push_back(x);
195        return x;
196      } 
197   }
198   return -1;  // no available ID's
199 }
200
201
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() ) {
207       if (*id_itr == ID) {
208         ids.erase( id_itr );
209         return;
210       }
211       ++id_itr;
212     }  
213 }
214
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 ) {
218      
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() );
223         ++numObjects;
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]);
232         } else {
233           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
234         }
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);
242         ai_plane->init();
243         ai_plane->bind();
244         return ai_plane->getID();
245 }
246
247
248 int FGAIManager::createAircraft( string model_class, string path,
249               FGAIFlightPlan* flightplan ) {
250      
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() );
255         ++numObjects;
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]);
264         } else {
265           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
266         }
267         ai_plane->setPath(path.c_str());
268         ai_plane->SetFlightPlan(flightplan);
269         ai_plane->init();
270         ai_plane->bind();
271         return ai_plane->getID();
272 }
273
274
275 int FGAIManager::createShip( string path, double latitude, double longitude,
276                              double altitude, double heading, double speed,
277                              double rudder ) {
278
279         FGAIShip* ai_ship = new FGAIShip(this);
280         ai_list.push_back(ai_ship);
281         ai_ship->setID( assignID() );
282         ++numObjects;
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);
290         ai_ship->init();
291         ai_ship->bind();
292         return ai_ship->getID();
293 }
294
295
296 int FGAIManager::createBallistic( string path, double latitude, double longitude,
297                                   double altitude, double azimuth, double elevation,
298                                   double speed ) {
299
300         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
301         ai_list.push_back(ai_ballistic);
302         ai_ballistic->setID( assignID() );    
303         ++numObjects;
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();
314 }
315
316 int FGAIManager::createStorm( string path, double latitude, double longitude,
317                              double altitude, double heading, double speed ) {
318
319         FGAIStorm* ai_storm = new FGAIStorm(this);
320         ai_list.push_back(ai_storm);
321         ai_storm->setID( assignID() );
322         ++numObjects;
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);
329         ai_storm->init();
330         ai_storm->bind();
331         return ai_storm->getID();
332 }
333
334 int FGAIManager::createThermal( double latitude, double longitude,
335                                 double strength, double diameter ) {
336
337         FGAIThermal* ai_thermal = new FGAIThermal(this);
338         ai_list.push_back(ai_thermal);
339         ai_thermal->setID( assignID() );
340         ++numObjects;
341         ai_thermal->setLongitude(longitude);
342         ai_thermal->setLatitude(latitude);
343         ai_thermal->setMaxStrength(strength);
344         ai_thermal->setDiameter(diameter / 6076.11549);
345         ai_thermal->init();
346         ai_thermal->bind();
347         return ai_thermal->getID();
348 }
349
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) {
354               freeID( ID );
355               delete (*ai_list_itr);
356               ai_list.erase(ai_list_itr);
357               --ai_list_itr;
358               --numObjects;
359               return;
360             }
361             ++ai_list_itr;
362         }
363 }
364
365 // fetch the user's state every 10 sim cycles
366 void FGAIManager::fetchUserState( void ) {
367    ++dt_count;
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;
376      dt_count = 0;
377    }
378 }
379
380
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();
387   }
388 }
389
390
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();
396     if (en) {
397       FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
398       if (en->aitype == "aircraft"){
399         createAircraft( en->aircraft_class, en->model_path, f);
400       }
401     }
402   }
403   delete s;
404 }
405
406 int FGAIManager::getNum( FGAIBase::object_type ot ) {
407   ai_list_iterator itr = ai_list.begin();
408   int count = 0;
409   while(itr != ai_list.end()) {
410       if ((*itr)->getType() == ot) {
411          ++count;
412       }
413       ++itr;
414   }  
415   return count;
416 }
417