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