]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
466f5332a87889c78bdf314c476bff260574eab6
[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)->getStringValue();
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         ai_list.push_back(ai_plane);
221         ai_plane->setID( assignID() );
222         ++numObjects;
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]);
231         } else {
232           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
233         }
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);
241         ai_plane->init();
242         ai_plane->bind();
243         return ai_plane->getID();
244 }
245
246
247 int FGAIManager::createAircraft( string model_class, string path,
248               FGAIFlightPlan* flightplan ) {
249      
250         FGAIAircraft* ai_plane = new FGAIAircraft(this);
251         ai_list.push_back(ai_plane);
252         ai_plane->setID( assignID() );
253         ++numObjects;
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]);
262         } else {
263           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
264         }
265         ai_plane->setPath(path.c_str());
266         ai_plane->SetFlightPlan(flightplan);
267         ai_plane->init();
268         ai_plane->bind();
269         return ai_plane->getID();
270 }
271
272
273 int FGAIManager::createShip( string path, double latitude, double longitude,
274                              double altitude, double heading, double speed,
275                              double rudder ) {
276
277         FGAIShip* ai_ship = new FGAIShip(this);
278         ai_list.push_back(ai_ship);
279         ai_ship->setID( assignID() );
280         ++numObjects;
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);
288         ai_ship->init();
289         ai_ship->bind();
290         return ai_ship->getID();
291 }
292
293
294 int FGAIManager::createBallistic( string path, double latitude, double longitude,
295                                   double altitude, double azimuth, double elevation,
296                                   double speed ) {
297
298         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
299         ai_list.push_back(ai_ballistic);
300         ai_ballistic->setID( assignID() );    
301         ++numObjects;
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();
312 }
313
314 int FGAIManager::createStorm( string path, double latitude, double longitude,
315                              double altitude, double heading, double speed ) {
316
317         FGAIStorm* ai_storm = new FGAIStorm(this);
318         ai_list.push_back(ai_storm);
319         ai_storm->setID( assignID() );
320         ++numObjects;
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);
327         ai_storm->init();
328         ai_storm->bind();
329         return ai_storm->getID();
330 }
331
332 int FGAIManager::createThermal( double latitude, double longitude,
333                                 double strength, double diameter ) {
334
335         FGAIThermal* ai_thermal = new FGAIThermal(this);
336         ai_list.push_back(ai_thermal);
337         ai_thermal->setID( assignID() );
338         ++numObjects;
339         ai_thermal->setLongitude(longitude);
340         ai_thermal->setLatitude(latitude);
341         ai_thermal->setMaxStrength(strength);
342         ai_thermal->setDiameter(diameter / 6076.11549);
343         ai_thermal->init();
344         ai_thermal->bind();
345         return ai_thermal->getID();
346 }
347
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) {
352               freeID( ID );
353               delete (*ai_list_itr);
354               ai_list.erase(ai_list_itr);
355               --ai_list_itr;
356               --numObjects;
357               return;
358             }
359             ++ai_list_itr;
360         }
361 }
362
363 // fetch the user's state every 10 sim cycles
364 void FGAIManager::fetchUserState( void ) {
365    ++dt_count;
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;
374      dt_count = 0;
375    }
376 }
377
378
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();
385   }
386 }
387
388
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();
394     if (en) {
395       FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
396       createAircraft("jet_transport", "Aircraft/737/Models/boeing733.xml", f);
397     }
398   }
399   delete s;
400 }
401