]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
3667d15568d7606779349176575442f052ecebd1
[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   root = fgGetNode("sim/ai", true);
58
59   enabled = root->getNode("enabled", true)->getBoolValue();
60   if (!enabled)
61       return;
62
63   wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
64  
65   scenario_filename = root->getNode("scenario", true)->getStringValue();
66
67   if (scenario_filename != "") processScenario( scenario_filename );
68   initDone = true;
69 }
70
71
72 void FGAIManager::bind() {
73    root = globals->get_props()->getNode("ai/models", true);
74    root->tie("count", SGRawValuePointer<int>(&numObjects));
75 }
76
77
78 void FGAIManager::unbind() {
79     root->untie("count");
80 }
81
82
83 void FGAIManager::update(double dt) {
84
85         // initialize these for finding nearest thermals
86         range_nearest = 10000.0;
87         strength = 0.0;
88
89         if (!enabled)
90             return;
91
92         _dt = dt;       
93
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);
99                    --numObjects;
100                    if ( ai_list_itr == ai_list.begin() ) {
101                        ai_list.erase(ai_list_itr);
102                        ai_list_itr = ai_list.begin();
103                        continue;
104                    } else {
105                        ai_list.erase(ai_list_itr--);
106                    }
107                 } else {
108                    fetchUserState();
109                    if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
110                        processThermal((FGAIThermal*)*ai_list_itr); 
111                    } else { 
112                       (*ai_list_itr)->update(_dt);
113                    }
114                 }
115                 ++ai_list_itr;
116         }
117         wind_from_down->setDoubleValue( strength );
118 }
119
120
121 // This function returns the next available ID
122 int FGAIManager::assignID() {
123   int maxint = 30000;
124   int x; 
125   bool used;
126   for (x=1; x<maxint; x++) {
127      used = false;
128      id_itr = ids.begin();
129      while( id_itr != ids.end() ) {
130        if ((*id_itr) == x) used = true;
131        ++id_itr;
132      }
133      if (!used) {
134        ids.push_back(x);
135        return x;
136      } 
137   }
138   return -1;  // no available ID's
139 }
140
141
142 // This function removes an ID from the ID array, making it
143 // available for assignment to another AI object
144 void FGAIManager::freeID( int ID ) {
145     id_itr = ids.begin();
146     while( id_itr != ids.end() ) {
147       if (*id_itr == ID) {
148         ids.erase( id_itr );
149         return;
150       }
151       ++id_itr;
152     }  
153 }
154
155 int FGAIManager::createAircraft( string model_class, string path,
156               double latitude, double longitude, double altitude,
157               double heading, double speed, double roll ) {
158      
159         FGAIAircraft* ai_plane = new FGAIAircraft(this);
160         ai_list.push_back(ai_plane);
161         ai_plane->setID( assignID() );
162         ++numObjects;
163         if (model_class == "light") {
164           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
165         } else if (model_class == "ww2_fighter") {
166           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
167         } else if (model_class ==  "jet_transport") {
168           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
169         } else if (model_class == "jet_fighter") {
170           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
171         } else {
172           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
173         }
174         ai_plane->setHeading(heading);
175         ai_plane->setSpeed(speed);
176         ai_plane->setPath(path.c_str());
177         ai_plane->setAltitude(altitude);
178         ai_plane->setLongitude(longitude);
179         ai_plane->setLatitude(latitude);
180         ai_plane->setBank(roll);
181         ai_plane->init();
182         ai_plane->bind();
183         return ai_plane->getID();
184 }
185
186
187 int FGAIManager::createAircraft( string model_class, string path,
188               FGAIFlightPlan* flightplan ) {
189      
190         FGAIAircraft* ai_plane = new FGAIAircraft(this);
191         ai_list.push_back(ai_plane);
192         ai_plane->setID( assignID() );
193         ++numObjects;
194         if (model_class == "light") {
195           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
196         } else if (model_class == "ww2_fighter") {
197           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
198         } else if (model_class ==  "jet_transport") {
199           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
200         } else if (model_class == "jet_fighter") {
201           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
202         } else {
203           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
204         }
205         ai_plane->setPath(path.c_str());
206         ai_plane->SetFlightPlan(flightplan);
207         ai_plane->init();
208         ai_plane->bind();
209         return ai_plane->getID();
210 }
211
212
213 int FGAIManager::createShip( string path, double latitude, double longitude,
214                              double altitude, double heading, double speed,
215                              double rudder ) {
216
217         FGAIShip* ai_ship = new FGAIShip(this);
218         ai_list.push_back(ai_ship);
219         ai_ship->setID( assignID() );
220         ++numObjects;
221         ai_ship->setHeading(heading);
222         ai_ship->setSpeed(speed);
223         ai_ship->setPath(path.c_str());
224         ai_ship->setAltitude(altitude);
225         ai_ship->setLongitude(longitude);
226         ai_ship->setLatitude(latitude);
227         ai_ship->setBank(rudder);
228         ai_ship->init();
229         ai_ship->bind();
230         return ai_ship->getID();
231 }
232
233 int FGAIManager::createShip( string path, FGAIFlightPlan* flightplan ) {
234
235         FGAIShip* ai_ship = new FGAIShip(this);
236         ai_list.push_back(ai_ship);
237         ai_ship->setID( assignID() );
238         ++numObjects;
239         ai_ship->setPath(path.c_str());
240         ai_ship->setFlightPlan(flightplan); 
241         ai_ship->init();
242         ai_ship->bind();
243         return ai_ship->getID();
244 }
245
246
247 int FGAIManager::createBallistic( string path, double latitude, double longitude,
248                                   double altitude, double azimuth, double elevation,
249                                   double speed ) {
250
251         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
252         ai_list.push_back(ai_ballistic);
253         ai_ballistic->setID( assignID() );    
254         ++numObjects;
255         ai_ballistic->setAzimuth(azimuth);
256         ai_ballistic->setElevation(elevation);
257         ai_ballistic->setSpeed(speed);
258         ai_ballistic->setPath(path.c_str());
259         ai_ballistic->setAltitude(altitude);
260         ai_ballistic->setLongitude(longitude);
261         ai_ballistic->setLatitude(latitude);
262         ai_ballistic->init();
263         ai_ballistic->bind();
264         return ai_ballistic->getID();
265 }
266
267 int FGAIManager::createStorm( string path, double latitude, double longitude,
268                              double altitude, double heading, double speed ) {
269
270         FGAIStorm* ai_storm = new FGAIStorm(this);
271         ai_list.push_back(ai_storm);
272         ai_storm->setID( assignID() );
273         ++numObjects;
274         ai_storm->setHeading(heading);
275         ai_storm->setSpeed(speed);
276         ai_storm->setPath(path.c_str());
277         ai_storm->setAltitude(altitude);
278         ai_storm->setLongitude(longitude);
279         ai_storm->setLatitude(latitude);
280         ai_storm->init();
281         ai_storm->bind();
282         return ai_storm->getID();
283 }
284
285 int FGAIManager::createThermal( double latitude, double longitude,
286                                 double strength, double diameter ) {
287
288         FGAIThermal* ai_thermal = new FGAIThermal(this);
289         ai_list.push_back(ai_thermal);
290         ai_thermal->setID( assignID() );
291         ++numObjects;
292         ai_thermal->setLongitude(longitude);
293         ai_thermal->setLatitude(latitude);
294         ai_thermal->setMaxStrength(strength);
295         ai_thermal->setDiameter(diameter / 6076.11549);
296         ai_thermal->init();
297         ai_thermal->bind();
298         return ai_thermal->getID();
299 }
300
301 void FGAIManager::destroyObject( int ID ) {
302         ai_list_itr = ai_list.begin();
303         while(ai_list_itr != ai_list.end()) {
304             if ((*ai_list_itr)->getID() == ID) {
305               freeID( ID );
306               delete (*ai_list_itr);
307               ai_list.erase(ai_list_itr);
308               --ai_list_itr;
309               --numObjects;
310               return;
311             }
312             ++ai_list_itr;
313         }
314 }
315
316 // fetch the user's state every 10 sim cycles
317 void FGAIManager::fetchUserState( void ) {
318    ++dt_count;
319    if (dt_count == 10) {
320      user_latitude  = fgGetDouble("/position/latitude-deg");
321      user_longitude = fgGetDouble("/position/longitude-deg");
322      user_altitude  = fgGetDouble("/position/altitude-ft");
323      user_heading   = fgGetDouble("/orientation/heading-deg");
324      user_pitch     = fgGetDouble("/orientation/pitch-deg");
325      user_yaw       = fgGetDouble("/orientation/side-slip-deg");
326      user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
327      dt_count = 0;
328    }
329 }
330
331
332 // only keep the results from the nearest thermal
333 void FGAIManager::processThermal( FGAIThermal* thermal ) {
334   thermal->update(_dt);
335   if ( thermal->_getRange() < range_nearest ) {
336      range_nearest = thermal->_getRange();
337      strength = thermal->getStrength();
338   }
339 }
340
341
342 void FGAIManager::processScenario( string filename ) {
343   FGAIScenario* s = new FGAIScenario( filename );
344   FGAIFlightPlan* f;
345
346   for (int i=0;i<s->nEntries();i++) {
347     FGAIScenario::entry* en = s->getNextEntry();
348     f = 0;
349     if (en) {
350       if (en->flightplan != ""){
351         f = new FGAIFlightPlan( en->flightplan );
352       }  
353       if (en->aitype == "aircraft"){
354          if (f){
355            createAircraft( en->aircraft_class, en->model_path, f );
356          } else {
357            createAircraft( en->aircraft_class, en->model_path, en->latitude,
358                            en->longitude, en->altitude, en->heading,
359                            en->speed, en->roll );
360          } 
361       } else if (en->aitype == "ship"){
362          if (f){
363            createShip( en->model_path, f );
364          } else {
365            createShip( en->model_path, en->latitude,
366                            en->longitude, en->altitude, en->heading,
367                            en->speed, en->rudder );
368          } 
369
370       } else if (en->aitype == "storm"){
371         createStorm( en->model_path, en->latitude, en->longitude,
372                      en->altitude, en->heading, en->speed ); 
373       } else if (en->aitype == "thermal"){
374         createThermal( en->latitude, en->longitude, en->strength, 
375                        en->diameter );
376       } else if (en->aitype == "ballistic"){
377         createBallistic( en->model_path, en->latitude, en->longitude,
378                          en->altitude, en->azimuth, en->elevation, en->speed );
379       }      
380     }
381   }
382
383   delete s;
384 }
385
386 int FGAIManager::getNum( FGAIBase::object_type ot ) {
387   ai_list_iterator itr = ai_list.begin();
388   int count = 0;
389   while(itr != ai_list.end()) {
390       if ((*itr)->getType() == ot) {
391          ++count;
392       }
393       ++itr;
394   }  
395   return count;
396 }
397