]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
6269e8a268c1f311ec01aa9b6524e2ae2f553d5a
[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
122 // This function returns the next available ID
123 int FGAIManager::assignID() {
124   int maxint = 30000;
125   int x; 
126   bool used;
127   for (x=1; x<maxint; x++) {
128      used = false;
129      id_itr = ids.begin();
130      while( id_itr != ids.end() ) {
131        if ((*id_itr) == x) used = true;
132        ++id_itr;
133      }
134      if (!used) {
135        ids.push_back(x);
136        return x;
137      } 
138   }
139   return -1;  // no available ID's
140 }
141
142
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() ) {
148       if (*id_itr == ID) {
149         ids.erase( id_itr );
150         return;
151       }
152       ++id_itr;
153     }  
154 }
155
156 int FGAIManager::createAircraft( string model_class, string path,
157               double latitude, double longitude, double altitude,
158               double heading, double speed, double roll ) {
159      
160         FGAIAircraft* ai_plane = new FGAIAircraft(this);
161         ai_list.push_back(ai_plane);
162         ai_plane->setID( assignID() );
163         ++numObjects;
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);
175         } else {
176           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
177         }
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);
185         ai_plane->init();
186         ai_plane->bind();
187         return ai_plane->getID();
188 }
189
190
191 int FGAIManager::createAircraft( string model_class, string path,
192               FGAIFlightPlan* flightplan ) {
193      
194         FGAIAircraft* ai_plane = new FGAIAircraft(this);
195         ai_list.push_back(ai_plane);
196         ai_plane->setID( assignID() );
197         ++numObjects;
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);
209         } else {
210           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
211         }
212         ai_plane->setPath(path.c_str());
213         ai_plane->SetFlightPlan(flightplan);
214         ai_plane->init();
215         ai_plane->bind();
216         return ai_plane->getID();
217 }
218
219
220 int FGAIManager::createShip( string path, double latitude, double longitude,
221                              double altitude, double heading, double speed,
222                              double rudder ) {
223
224         FGAIShip* ai_ship = new FGAIShip(this);
225         ai_list.push_back(ai_ship);
226         ai_ship->setID( assignID() );
227         ++numObjects;
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);
235         ai_ship->init();
236         ai_ship->bind();
237         return ai_ship->getID();
238 }
239
240 int FGAIManager::createShip( string path, FGAIFlightPlan* flightplan ) {
241
242         FGAIShip* ai_ship = new FGAIShip(this);
243         ai_list.push_back(ai_ship);
244         ai_ship->setID( assignID() );
245         ++numObjects;
246         ai_ship->setPath(path.c_str());
247         ai_ship->setFlightPlan(flightplan); 
248         ai_ship->init();
249         ai_ship->bind();
250         return ai_ship->getID();
251 }
252
253
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
258         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
259         ai_list.push_back(ai_ballistic);
260         ai_ballistic->setID( assignID() );    
261         ++numObjects;
262         ai_ballistic->setAzimuth(azimuth);
263         ai_ballistic->setElevation(elevation);
264         ai_ballistic->setSpeed(speed);
265         ai_ballistic->setPath(path.c_str());
266         ai_ballistic->setAltitude(altitude);
267         ai_ballistic->setLongitude(longitude);
268         ai_ballistic->setLatitude(latitude);
269         ai_ballistic->setDragArea(eda);
270         ai_ballistic->setLife(life);
271         ai_ballistic->setBuoyancy(buoyancy);
272         ai_ballistic->init();
273         ai_ballistic->bind();
274         return ai_ballistic->getID();
275 }
276
277 int FGAIManager::createStorm( string path, double latitude, double longitude,
278                              double altitude, double heading, double speed ) {
279
280         FGAIStorm* ai_storm = new FGAIStorm(this);
281         ai_list.push_back(ai_storm);
282         ai_storm->setID( assignID() );
283         ++numObjects;
284         ai_storm->setHeading(heading);
285         ai_storm->setSpeed(speed);
286         ai_storm->setPath(path.c_str());
287         ai_storm->setAltitude(altitude);
288         ai_storm->setLongitude(longitude);
289         ai_storm->setLatitude(latitude);
290         ai_storm->init();
291         ai_storm->bind();
292         return ai_storm->getID();
293 }
294
295 int FGAIManager::createThermal( double latitude, double longitude,
296                                 double strength, double diameter ) {
297
298         FGAIThermal* ai_thermal = new FGAIThermal(this);
299         ai_list.push_back(ai_thermal);
300         ai_thermal->setID( assignID() );
301         ++numObjects;
302         ai_thermal->setLongitude(longitude);
303         ai_thermal->setLatitude(latitude);
304         ai_thermal->setMaxStrength(strength);
305         ai_thermal->setDiameter(diameter / 6076.11549);
306         ai_thermal->init();
307         ai_thermal->bind();
308         return ai_thermal->getID();
309 }
310
311
312 void FGAIManager::destroyObject( int ID ) {
313         ai_list_itr = ai_list.begin();
314         while(ai_list_itr != ai_list.end()) {
315             if ((*ai_list_itr)->getID() == ID) {
316               freeID( ID );
317               delete (*ai_list_itr);
318               ai_list.erase(ai_list_itr);
319               --ai_list_itr;
320               --numObjects;
321               return;
322             }
323             ++ai_list_itr;
324         }
325 }
326
327 // fetch the user's state every 10 sim cycles
328 void FGAIManager::fetchUserState( void ) {
329    ++dt_count;
330    if (dt_count == 10) {
331      user_latitude  = fgGetDouble("/position/latitude-deg");
332      user_longitude = fgGetDouble("/position/longitude-deg");
333      user_altitude  = fgGetDouble("/position/altitude-ft");
334      user_heading   = fgGetDouble("/orientation/heading-deg");
335      user_pitch     = fgGetDouble("/orientation/pitch-deg");
336      user_yaw       = fgGetDouble("/orientation/side-slip-deg");
337      user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
338      dt_count = 0;
339    }
340 }
341
342
343 // only keep the results from the nearest thermal
344 void FGAIManager::processThermal( FGAIThermal* thermal ) {
345   thermal->update(_dt);
346   if ( thermal->_getRange() < range_nearest ) {
347      range_nearest = thermal->_getRange();
348      strength = thermal->getStrength();
349   }
350 }
351
352
353 void FGAIManager::processScenario( string filename ) {
354   FGAIScenario* s = new FGAIScenario( filename );
355   FGAIFlightPlan* f;
356
357   for (int i=0;i<s->nEntries();i++) {
358     FGAIScenario::entry* en = s->getNextEntry();
359     f = 0;
360     if (en) {
361       if (en->flightplan != ""){
362         f = new FGAIFlightPlan( en->flightplan );
363       }  
364       if (en->aitype == "aircraft"){
365          if (f){
366            createAircraft( en->aircraft_class, en->model_path, f );
367          } else {
368            createAircraft( en->aircraft_class, en->model_path, en->latitude,
369                            en->longitude, en->altitude, en->heading,
370                            en->speed, en->roll );
371          } 
372       } else if (en->aitype == "ship"){
373          if (f){
374            createShip( en->model_path, f );
375          } else {
376            createShip( en->model_path, en->latitude,
377                            en->longitude, en->altitude, en->heading,
378                            en->speed, en->rudder );
379          } 
380
381       } else if (en->aitype == "storm"){
382         createStorm( en->model_path, en->latitude, en->longitude,
383                      en->altitude, en->heading, en->speed ); 
384       } else if (en->aitype == "thermal"){
385         createThermal( en->latitude, en->longitude, en->strength, 
386                        en->diameter );
387       } else if (en->aitype == "ballistic"){
388         createBallistic( en->model_path, en->latitude, en->longitude,
389                          en->altitude, en->azimuth, en->elevation, en->speed,
390                          en->eda, en->life, en->buoyancy );
391       }      
392     }
393   }
394
395   delete s;
396 }
397
398 int FGAIManager::getNum( FGAIBase::object_type ot ) {
399   ai_list_iterator itr = ai_list.begin();
400   int count = 0;
401   while(itr != ai_list.end()) {
402       if ((*itr)->getType() == ot) {
403          ++count;
404       }
405       ++itr;
406   }  
407   return count;
408 }
409