]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
Roy Vegard Ovesen:
[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   initDone = true;
117 }
118
119
120 void FGAIManager::bind() {
121    root = globals->get_props()->getNode("ai/models", true);
122    root->tie("count", SGRawValuePointer<int>(&numObjects));
123 }
124
125
126 void FGAIManager::unbind() {
127     root->untie("count");
128 }
129
130
131 void FGAIManager::update(double dt) {
132
133         // initialize these for finding nearest thermals
134         range_nearest = 10000.0;
135         strength = 0.0;
136
137         _dt = dt;       
138
139         ai_list_itr = ai_list.begin();
140         while(ai_list_itr != ai_list.end()) {
141                 if ((*ai_list_itr)->getDie()) {      
142                    freeID((*ai_list_itr)->getID());
143                    delete (*ai_list_itr);
144                    ai_list.erase(ai_list_itr);
145                    --ai_list_itr;
146                    --numObjects;
147                 } else {
148                    fetchUserState();
149                    if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
150                        processThermal((FGAIThermal*)*ai_list_itr); 
151                    } else { 
152                       (*ai_list_itr)->update(_dt);
153                    }
154                 }
155                 ++ai_list_itr;
156         }
157         wind_from_down->setDoubleValue( strength );
158 }
159
160
161 // This function returns the next available ID
162 int FGAIManager::assignID() {
163   int maxint = 30000;
164   int x; 
165   bool used;
166   for (x=0; x<maxint; x++) {
167      used = false;
168      id_itr = ids.begin();
169      while( id_itr != ids.end() ) {
170        if ((*id_itr) == x) used = true;
171        ++id_itr;
172      }
173      if (!used) {
174        ids.push_back(x);
175        return x;
176      } 
177   }
178   return -1;  // no available ID's
179 }
180
181
182 // This function removes an ID from the ID array, making it
183 // available for assignment to another AI object
184 void FGAIManager::freeID( int ID ) {
185     id_itr = ids.begin();
186     while( id_itr != ids.end() ) {
187       if (*id_itr == ID) {
188         ids.erase( id_itr );
189         return;
190       }
191       ++id_itr;
192     }  
193 }
194
195 int FGAIManager::createAircraft( string model_class, string path,
196               double latitude, double longitude, double altitude,
197               double heading, double speed, double pitch, double roll ) {
198      
199         FGAIAircraft* ai_plane = new FGAIAircraft(this);
200         ai_list.push_back(ai_plane);
201         ai_plane->setID( assignID() );
202         ++numObjects;
203         if (model_class == "light") {
204           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
205         } else if (model_class == "ww2_fighter") {
206           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
207         } else if (model_class ==  "jet_transport") {
208           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
209         } else if (model_class == "jet_fighter") {
210           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
211         }
212         ai_plane->setHeading(heading);
213         ai_plane->setSpeed(speed);
214         ai_plane->setPath(path.c_str());
215         ai_plane->setAltitude(altitude);
216         ai_plane->setLongitude(longitude);
217         ai_plane->setLatitude(latitude);
218         ai_plane->setBank(roll);
219         ai_plane->init();
220         ai_plane->bind();
221         return ai_plane->getID();
222 }
223
224
225 int FGAIManager::createShip( string path, double latitude, double longitude,
226                              double altitude, double heading, double speed,
227                              double rudder ) {
228
229         FGAIShip* ai_ship = new FGAIShip(this);
230         ai_list.push_back(ai_ship);
231         ai_ship->setID( assignID() );
232         ++numObjects;
233         ai_ship->setHeading(heading);
234         ai_ship->setSpeed(speed);
235         ai_ship->setPath(path.c_str());
236         ai_ship->setAltitude(altitude);
237         ai_ship->setLongitude(longitude);
238         ai_ship->setLatitude(latitude);
239         ai_ship->setBank(rudder);
240         ai_ship->init();
241         ai_ship->bind();
242         return ai_ship->getID();
243 }
244
245
246 int FGAIManager::createBallistic( string path, double latitude, double longitude,
247                                   double altitude, double azimuth, double elevation,
248                                   double speed ) {
249
250         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
251         ai_list.push_back(ai_ballistic);
252         ai_ballistic->setID( assignID() );    
253         ++numObjects;
254         ai_ballistic->setAzimuth(azimuth);
255         ai_ballistic->setElevation(elevation);
256         ai_ballistic->setSpeed(speed);
257         ai_ballistic->setPath(path.c_str());
258         ai_ballistic->setAltitude(altitude);
259         ai_ballistic->setLongitude(longitude);
260         ai_ballistic->setLatitude(latitude);
261         ai_ballistic->init();
262         ai_ballistic->bind();
263         return ai_ballistic->getID();
264 }
265
266 int FGAIManager::createStorm( string path, double latitude, double longitude,
267                              double altitude, double heading, double speed ) {
268
269         FGAIStorm* ai_storm = new FGAIStorm(this);
270         ai_list.push_back(ai_storm);
271         ai_storm->setID( assignID() );
272         ++numObjects;
273         ai_storm->setHeading(heading);
274         ai_storm->setSpeed(speed);
275         ai_storm->setPath(path.c_str());
276         ai_storm->setAltitude(altitude);
277         ai_storm->setLongitude(longitude);
278         ai_storm->setLatitude(latitude);
279         ai_storm->init();
280         ai_storm->bind();
281         return ai_storm->getID();
282 }
283
284 int FGAIManager::createThermal( double latitude, double longitude,
285                                 double strength, double diameter ) {
286
287         FGAIThermal* ai_thermal = new FGAIThermal(this);
288         ai_list.push_back(ai_thermal);
289         ai_thermal->setID( assignID() );
290         ++numObjects;
291         ai_thermal->setLongitude(longitude);
292         ai_thermal->setLatitude(latitude);
293         ai_thermal->setMaxStrength(strength);
294         ai_thermal->setDiameter(diameter / 6076.11549);
295         ai_thermal->init();
296         ai_thermal->bind();
297         return ai_thermal->getID();
298 }
299
300 void FGAIManager::destroyObject( int ID ) {
301         ai_list_itr = ai_list.begin();
302         while(ai_list_itr != ai_list.end()) {
303             if ((*ai_list_itr)->getID() == ID) {
304               freeID( ID );
305               delete (*ai_list_itr);
306               ai_list.erase(ai_list_itr);
307               --ai_list_itr;
308               --numObjects;
309               return;
310             }
311             ++ai_list_itr;
312         }
313 }
314
315 // fetch the user's state every 10 sim cycles
316 void FGAIManager::fetchUserState( void ) {
317    ++dt_count;
318    if (dt_count == 10) {
319      user_latitude  = fgGetDouble("/position/latitude-deg");
320      user_longitude = fgGetDouble("/position/longitude-deg");
321      user_altitude  = fgGetDouble("/position/altitude-ft");
322      user_heading   = fgGetDouble("/orientation/heading-deg");
323      user_pitch     = fgGetDouble("/orientation/pitch-deg");
324      user_yaw       = fgGetDouble("/orientation/side-slip-deg");
325      user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
326      dt_count = 0;
327    }
328 }
329
330
331 // only keep the results from the nearest thermal
332 void FGAIManager::processThermal( FGAIThermal* thermal ) {
333   thermal->update(_dt);
334   if ( thermal->_getRange() < range_nearest ) {
335      range_nearest = thermal->_getRange();
336      strength = thermal->getStrength();
337   }
338 }