]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
394951f09b4405526dbc4054930fd8723e0a17f4
[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_count = 9;
41 }
42
43 FGAIManager::~FGAIManager() {
44   ai_list_itr = ai_list.begin();
45   while(ai_list_itr != ai_list.end()) {
46       delete (*ai_list_itr);
47       ++ai_list_itr;
48     }
49   ai_list.clear();
50   ids.clear();
51 }
52
53
54 void FGAIManager::init() {
55   int rval;
56   root = fgGetNode("sim/ai", true);
57
58   for (int i = 0; i < root->nChildren(); i++) {
59     const SGPropertyNode * entry = root->getChild(i);
60
61     if (!strcmp(entry->getName(), "entry")) {
62       if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { 
63
64         rval = createAircraft( entry->getStringValue("class", ""),
65                                entry->getStringValue("path"),
66                                entry->getDoubleValue("latitude"),
67                                entry->getDoubleValue("longitude"),
68                                entry->getDoubleValue("altitude-ft"),
69                                entry->getDoubleValue("heading"),
70                                entry->getDoubleValue("speed-KTAS"),
71                                0.0, 
72                                entry->getDoubleValue("bank") );
73
74       } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
75
76         rval = createShip( entry->getStringValue("path"),
77                            entry->getDoubleValue("latitude"),
78                            entry->getDoubleValue("longitude"),
79                            entry->getDoubleValue("altitude-ft"),
80                            entry->getDoubleValue("heading"),
81                            entry->getDoubleValue("speed-KTAS"),
82                            entry->getDoubleValue("rudder") );
83
84       } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
85
86         rval = createBallistic( entry->getStringValue("path"),
87                                 entry->getDoubleValue("latitude"),
88                                 entry->getDoubleValue("longitude"),
89                                 entry->getDoubleValue("altitude-ft"),
90                                 entry->getDoubleValue("azimuth"),
91                                 entry->getDoubleValue("elevation"),
92                                 entry->getDoubleValue("speed") );
93
94       } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
95
96         rval = createStorm( entry->getStringValue("path"),
97                             entry->getDoubleValue("latitude"),
98                             entry->getDoubleValue("longitude"),
99                             entry->getDoubleValue("altitude-ft"),
100                             entry->getDoubleValue("heading"),
101                             entry->getDoubleValue("speed-KTAS") );
102
103       } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
104
105         rval = createThermal( entry->getDoubleValue("latitude"),
106                               entry->getDoubleValue("longitude"),
107                               entry->getDoubleValue("strength-fps"),
108                               entry->getDoubleValue("diameter-ft") );
109
110       }       
111     }
112   }
113
114   initDone = true;
115 }
116
117
118 void FGAIManager::bind() {
119    root = globals->get_props()->getNode("ai/models", true);
120    root->tie("count", SGRawValuePointer<int>(&numObjects));
121 }
122
123
124 void FGAIManager::unbind() {
125     root->untie("count");
126 }
127
128
129 void FGAIManager::update(double dt) {
130         
131         ai_list_itr = ai_list.begin();
132         while(ai_list_itr != ai_list.end()) {
133                 if ((*ai_list_itr)->getDie()) {
134                    freeID((*ai_list_itr)->getID());
135                    delete (*ai_list_itr);
136                    ai_list.erase(ai_list_itr);
137                    --ai_list_itr;
138                    --numObjects;
139                 } else {
140                    fetchUserState();
141                    (*ai_list_itr)->update(dt);
142                 }
143                 ++ai_list_itr;
144         }
145 }
146
147
148 // This function returns the next available ID
149 int FGAIManager::assignID() {
150   int maxint = 30000;
151   int x; 
152   bool used;
153   for (x=0; x<maxint; x++) {
154      used = false;
155      id_itr = ids.begin();
156      while( id_itr != ids.end() ) {
157        if ((*id_itr) == x) used = true;
158        ++id_itr;
159      }
160      if (!used) {
161        ids.push_back(x);
162        return x;
163      } 
164   }
165   return -1;  // no available ID's
166 }
167
168
169 // This function removes an ID from the ID array, making it
170 // available for assignment to another AI object
171 void FGAIManager::freeID( int ID ) {
172     id_itr = ids.begin();
173     while( id_itr != ids.end() ) {
174       if (*id_itr == ID) {
175         ids.erase( id_itr );
176         return;
177       }
178       ++id_itr;
179     }  
180 }
181
182 int FGAIManager::createAircraft( string model_class, string path,
183               double latitude, double longitude, double altitude,
184               double heading, double speed, double pitch, double roll ) {
185      
186         FGAIAircraft* ai_plane = new FGAIAircraft(this);
187         ai_list.push_back(ai_plane);
188         ai_plane->setID( assignID() );
189         ++numObjects;
190         if (model_class == "light") {
191           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
192         } else if (model_class == "ww2_fighter") {
193           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
194         } else if (model_class ==  "jet_transport") {
195           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
196         } else if (model_class == "jet_fighter") {
197           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
198         }
199         ai_plane->setHeading(heading);
200         ai_plane->setSpeed(speed);
201         ai_plane->setPath(path.c_str());
202         ai_plane->setAltitude(altitude);
203         ai_plane->setLongitude(longitude);
204         ai_plane->setLatitude(latitude);
205         ai_plane->setBank(roll);
206         ai_plane->init();
207         ai_plane->bind();
208         return ai_plane->getID();
209 }
210
211
212 int FGAIManager::createShip( string path, double latitude, double longitude,
213                              double altitude, double heading, double speed,
214                              double rudder ) {
215
216         FGAIShip* ai_ship = new FGAIShip(this);
217         ai_list.push_back(ai_ship);
218         ai_ship->setID( assignID() );
219         ++numObjects;
220         ai_ship->setHeading(heading);
221         ai_ship->setSpeed(speed);
222         ai_ship->setPath(path.c_str());
223         ai_ship->setAltitude(altitude);
224         ai_ship->setLongitude(longitude);
225         ai_ship->setLatitude(latitude);
226         ai_ship->setBank(rudder);
227         ai_ship->init();
228         ai_ship->bind();
229         return ai_ship->getID();
230 }
231
232
233 int FGAIManager::createBallistic( string path, double latitude, double longitude,
234                                   double altitude, double azimuth, double elevation,
235                                   double speed ) {
236
237         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
238         ai_list.push_back(ai_ballistic);
239         ai_ballistic->setID( assignID() );    
240         ++numObjects;
241         ai_ballistic->setAzimuth(azimuth);
242         ai_ballistic->setElevation(elevation);
243         ai_ballistic->setSpeed(speed);
244         ai_ballistic->setPath(path.c_str());
245         ai_ballistic->setAltitude(altitude);
246         ai_ballistic->setLongitude(longitude);
247         ai_ballistic->setLatitude(latitude);
248         ai_ballistic->init();
249         ai_ballistic->bind();
250         return ai_ballistic->getID();
251 }
252
253 int FGAIManager::createStorm( string path, double latitude, double longitude,
254                              double altitude, double heading, double speed ) {
255
256         FGAIStorm* ai_storm = new FGAIStorm(this);
257         ai_list.push_back(ai_storm);
258         ai_storm->setID( assignID() );
259         ++numObjects;
260         ai_storm->setHeading(heading);
261         ai_storm->setSpeed(speed);
262         ai_storm->setPath(path.c_str());
263         ai_storm->setAltitude(altitude);
264         ai_storm->setLongitude(longitude);
265         ai_storm->setLatitude(latitude);
266         ai_storm->init();
267         ai_storm->bind();
268         return ai_storm->getID();
269 }
270
271 int FGAIManager::createThermal( double latitude, double longitude,
272                                 double strength, double diameter ) {
273
274         FGAIThermal* ai_thermal = new FGAIThermal(this);
275         ai_list.push_back(ai_thermal);
276         ai_thermal->setID( assignID() );
277         ++numObjects;
278         ai_thermal->setLongitude(longitude);
279         ai_thermal->setLatitude(latitude);
280         ai_thermal->setStrength(strength);
281         ai_thermal->setDiameter(diameter / 6076.11549);
282         ai_thermal->init();
283         ai_thermal->bind();
284         return ai_thermal->getID();
285 }
286
287 void FGAIManager::destroyObject( int ID ) {
288         ai_list_itr = ai_list.begin();
289         while(ai_list_itr != ai_list.end()) {
290             if ((*ai_list_itr)->getID() == ID) {
291               freeID( ID );
292               delete (*ai_list_itr);
293               ai_list.erase(ai_list_itr);
294               --ai_list_itr;
295               --numObjects;
296               return;
297             }
298             ++ai_list_itr;
299         }
300 }
301
302 // fetch the user's state every 10 sim cycles
303 void FGAIManager::fetchUserState( void ) {
304    ++dt_count;
305    if (dt_count == 10) {
306      user_latitude  = fgGetDouble("/position/latitude-deg");
307      user_longitude = fgGetDouble("/position/longitude-deg");
308      user_altitude  = fgGetDouble("/position/altitude-ft");
309      user_heading   = fgGetDouble("/orientation/heading-deg");
310      user_pitch     = fgGetDouble("/orientation/pitch-deg");
311      user_yaw       = fgGetDouble("/orientation/side-slip-deg");
312      user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
313      dt_count = 0;
314    }
315 }