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