]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
b4019554b31d7bdd679b6a6f6a1e6a1dee0ec5c9
[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                    ai_list.erase(ai_list_itr);
151                    --ai_list_itr;
152                    --numObjects;
153                 } else {
154                    fetchUserState();
155                    if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
156                        processThermal((FGAIThermal*)*ai_list_itr); 
157                    } else { 
158                       (*ai_list_itr)->update(_dt);
159                    }
160                 }
161                 ++ai_list_itr;
162         }
163         wind_from_down->setDoubleValue( strength );
164 }
165
166
167 // This function returns the next available ID
168 int FGAIManager::assignID() {
169   int maxint = 30000;
170   int x; 
171   bool used;
172   for (x=0; x<maxint; x++) {
173      used = false;
174      id_itr = ids.begin();
175      while( id_itr != ids.end() ) {
176        if ((*id_itr) == x) used = true;
177        ++id_itr;
178      }
179      if (!used) {
180        ids.push_back(x);
181        return x;
182      } 
183   }
184   return -1;  // no available ID's
185 }
186
187
188 // This function removes an ID from the ID array, making it
189 // available for assignment to another AI object
190 void FGAIManager::freeID( int ID ) {
191     id_itr = ids.begin();
192     while( id_itr != ids.end() ) {
193       if (*id_itr == ID) {
194         ids.erase( id_itr );
195         return;
196       }
197       ++id_itr;
198     }  
199 }
200
201 int FGAIManager::createAircraft( string model_class, string path,
202               double latitude, double longitude, double altitude,
203               double heading, double speed, double pitch, double roll ) {
204      
205         FGAIAircraft* ai_plane = new FGAIAircraft(this);
206         ai_list.push_back(ai_plane);
207         ai_plane->setID( assignID() );
208         ++numObjects;
209         if (model_class == "light") {
210           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
211         } else if (model_class == "ww2_fighter") {
212           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
213         } else if (model_class ==  "jet_transport") {
214           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
215         } else if (model_class == "jet_fighter") {
216           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
217         } else {
218           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
219         }
220         ai_plane->setHeading(heading);
221         ai_plane->setSpeed(speed);
222         ai_plane->setPath(path.c_str());
223         ai_plane->setAltitude(altitude);
224         ai_plane->setLongitude(longitude);
225         ai_plane->setLatitude(latitude);
226         ai_plane->setBank(roll);
227         ai_plane->init();
228         ai_plane->bind();
229         return ai_plane->getID();
230 }
231
232
233 int FGAIManager::createAircraft( string model_class, string path,
234               FGAIFlightPlan* flightplan ) {
235      
236         FGAIAircraft* ai_plane = new FGAIAircraft(this);
237         ai_list.push_back(ai_plane);
238         ai_plane->setID( assignID() );
239         ++numObjects;
240         if (model_class == "light") {
241           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
242         } else if (model_class == "ww2_fighter") {
243           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
244         } else if (model_class ==  "jet_transport") {
245           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
246         } else if (model_class == "jet_fighter") {
247           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
248         } else {
249           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
250         }
251         ai_plane->setPath(path.c_str());
252         ai_plane->SetFlightPlan(flightplan);
253         ai_plane->init();
254         ai_plane->bind();
255         return ai_plane->getID();
256 }
257
258
259 int FGAIManager::createShip( string path, double latitude, double longitude,
260                              double altitude, double heading, double speed,
261                              double rudder ) {
262
263         FGAIShip* ai_ship = new FGAIShip(this);
264         ai_list.push_back(ai_ship);
265         ai_ship->setID( assignID() );
266         ++numObjects;
267         ai_ship->setHeading(heading);
268         ai_ship->setSpeed(speed);
269         ai_ship->setPath(path.c_str());
270         ai_ship->setAltitude(altitude);
271         ai_ship->setLongitude(longitude);
272         ai_ship->setLatitude(latitude);
273         ai_ship->setBank(rudder);
274         ai_ship->init();
275         ai_ship->bind();
276         return ai_ship->getID();
277 }
278
279
280 int FGAIManager::createBallistic( string path, double latitude, double longitude,
281                                   double altitude, double azimuth, double elevation,
282                                   double speed ) {
283
284         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
285         ai_list.push_back(ai_ballistic);
286         ai_ballistic->setID( assignID() );    
287         ++numObjects;
288         ai_ballistic->setAzimuth(azimuth);
289         ai_ballistic->setElevation(elevation);
290         ai_ballistic->setSpeed(speed);
291         ai_ballistic->setPath(path.c_str());
292         ai_ballistic->setAltitude(altitude);
293         ai_ballistic->setLongitude(longitude);
294         ai_ballistic->setLatitude(latitude);
295         ai_ballistic->init();
296         ai_ballistic->bind();
297         return ai_ballistic->getID();
298 }
299
300 int FGAIManager::createStorm( string path, double latitude, double longitude,
301                              double altitude, double heading, double speed ) {
302
303         FGAIStorm* ai_storm = new FGAIStorm(this);
304         ai_list.push_back(ai_storm);
305         ai_storm->setID( assignID() );
306         ++numObjects;
307         ai_storm->setHeading(heading);
308         ai_storm->setSpeed(speed);
309         ai_storm->setPath(path.c_str());
310         ai_storm->setAltitude(altitude);
311         ai_storm->setLongitude(longitude);
312         ai_storm->setLatitude(latitude);
313         ai_storm->init();
314         ai_storm->bind();
315         return ai_storm->getID();
316 }
317
318 int FGAIManager::createThermal( double latitude, double longitude,
319                                 double strength, double diameter ) {
320
321         FGAIThermal* ai_thermal = new FGAIThermal(this);
322         ai_list.push_back(ai_thermal);
323         ai_thermal->setID( assignID() );
324         ++numObjects;
325         ai_thermal->setLongitude(longitude);
326         ai_thermal->setLatitude(latitude);
327         ai_thermal->setMaxStrength(strength);
328         ai_thermal->setDiameter(diameter / 6076.11549);
329         ai_thermal->init();
330         ai_thermal->bind();
331         return ai_thermal->getID();
332 }
333
334 void FGAIManager::destroyObject( int ID ) {
335         ai_list_itr = ai_list.begin();
336         while(ai_list_itr != ai_list.end()) {
337             if ((*ai_list_itr)->getID() == ID) {
338               freeID( ID );
339               delete (*ai_list_itr);
340               ai_list.erase(ai_list_itr);
341               --ai_list_itr;
342               --numObjects;
343               return;
344             }
345             ++ai_list_itr;
346         }
347 }
348
349 // fetch the user's state every 10 sim cycles
350 void FGAIManager::fetchUserState( void ) {
351    ++dt_count;
352    if (dt_count == 10) {
353      user_latitude  = fgGetDouble("/position/latitude-deg");
354      user_longitude = fgGetDouble("/position/longitude-deg");
355      user_altitude  = fgGetDouble("/position/altitude-ft");
356      user_heading   = fgGetDouble("/orientation/heading-deg");
357      user_pitch     = fgGetDouble("/orientation/pitch-deg");
358      user_yaw       = fgGetDouble("/orientation/side-slip-deg");
359      user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
360      dt_count = 0;
361    }
362 }
363
364
365 // only keep the results from the nearest thermal
366 void FGAIManager::processThermal( FGAIThermal* thermal ) {
367   thermal->update(_dt);
368   if ( thermal->_getRange() < range_nearest ) {
369      range_nearest = thermal->_getRange();
370      strength = thermal->getStrength();
371   }
372 }
373
374
375 void FGAIManager::processScenario( string filename ) {
376   //cout << "AIManager: creating a scenario." << endl;
377   FGAIScenario* s = new FGAIScenario( filename );
378   for (int i=0;i<s->nEntries();i++) {
379     FGAIScenario::entry* en = s->getNextEntry();
380     if (en) {
381       FGAIFlightPlan* f = new FGAIFlightPlan( en->flightplan );
382       createAircraft("jet_transport", "Aircraft/737/Models/boeing733.xml", f);
383     }
384   }
385   delete s;
386 }
387