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