]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
Migrate FlightGear code to use "#include SG_GL*" defined in
[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 #include "AICarrier.hxx"
34
35 SG_USING_STD(list);
36
37
38 FGAIManager::FGAIManager() {
39   initDone = false;
40   for (int i=0; i < FGAIBase::MAX_OBJECTS; i++)
41      numObjects[i] = 0;
42   _dt = 0.0;
43   dt_count = 9;
44   scenario_filename = "";
45   ai_list.clear();
46 }
47
48 FGAIManager::~FGAIManager() {
49   ai_list_itr = ai_list.begin();
50   while(ai_list_itr != ai_list.end()) {
51       (*ai_list_itr)->unbind();
52       delete (*ai_list_itr);
53       ++ai_list_itr;
54     }
55   ai_list.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   if (scenario_filename != "") processScenario( scenario_filename );
70
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                    --numObjects[(*ai_list_itr)->getType()];
101                    --numObjects[0];
102                    (*ai_list_itr)->unbind();
103                    delete (*ai_list_itr);
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 void*
127 FGAIManager::createAircraft( FGAIModelEntity *entity ) {
128      
129         FGAIAircraft* ai_plane = new FGAIAircraft(this);
130         ai_list.push_back(ai_plane);
131         ++numObjects[0];
132         ++numObjects[FGAIBase::otAircraft];
133         if (entity->m_class == "light") {
134           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
135         } else if (entity->m_class == "ww2_fighter") {
136           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
137         } else if (entity->m_class ==  "jet_transport") {
138           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
139         } else if (entity->m_class == "jet_fighter") {
140           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
141         } else if (entity->m_class ==  "tanker") {
142           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
143           ai_plane->SetTanker(true);
144         } else {
145           ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
146         }
147         ai_plane->setHeading(entity->heading);
148         ai_plane->setSpeed(entity->speed);
149         ai_plane->setPath(entity->path.c_str());
150         ai_plane->setAltitude(entity->altitude);
151         ai_plane->setLongitude(entity->longitude);
152         ai_plane->setLatitude(entity->latitude);
153         ai_plane->setBank(entity->roll);
154
155         if ( entity->fp ) {
156           ai_plane->SetFlightPlan(entity->fp);
157         }
158
159         ai_plane->init();
160         ai_plane->bind();
161         return ai_plane;
162 }
163
164 void*
165 FGAIManager::createShip( FGAIModelEntity *entity ) {
166     
167      //cout << "creating ship" << endl;    
168
169         FGAIShip* ai_ship = new FGAIShip(this);
170         ai_list.push_back(ai_ship);
171         ++numObjects[0];
172         ++numObjects[FGAIBase::otShip];
173         ai_ship->setHeading(entity->heading);
174         ai_ship->setSpeed(entity->speed);
175         ai_ship->setPath(entity->path.c_str());
176         ai_ship->setAltitude(entity->altitude);
177         ai_ship->setLongitude(entity->longitude);
178         ai_ship->setLatitude(entity->latitude);
179         ai_ship->setBank(entity->rudder);
180
181         if ( entity->fp ) {
182            ai_ship->setFlightPlan(entity->fp);
183         }
184
185         ai_ship->init();
186         ai_ship->bind();
187         return ai_ship;
188 }
189
190 void*
191 FGAIManager::createCarrier( FGAIModelEntity *entity ) {
192     
193     //cout << "creating carrier" << endl;
194
195         FGAIShip* ai_carrier = new FGAICarrier(this);
196         ai_list.push_back(ai_carrier);
197         ++numObjects[0];
198         ++numObjects[FGAIBase::otShip];
199         ai_carrier->setHeading(entity->heading);
200         ai_carrier->setSpeed(entity->speed);
201         ai_carrier->setPath(entity->path.c_str());
202         ai_carrier->setAltitude(entity->altitude);
203         ai_carrier->setLongitude(entity->longitude);
204         ai_carrier->setLatitude(entity->latitude);
205         ai_carrier->setBank(entity->rudder);
206         ai_carrier->setRadius(entity->radius);
207
208         if ( entity->fp ) {
209            ai_carrier->setFlightPlan(entity->fp);
210         }
211
212         ai_carrier->init();
213         ai_carrier->bind();
214         return ai_carrier;
215 }
216
217 void*
218 FGAIManager::createBallistic( FGAIModelEntity *entity ) {
219
220         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
221         ai_list.push_back(ai_ballistic);
222         ++numObjects[0];
223         ++numObjects[FGAIBase::otBallistic];
224         ai_ballistic->setAzimuth(entity->azimuth);
225         ai_ballistic->setElevation(entity->elevation);
226         ai_ballistic->setSpeed(entity->speed);
227         ai_ballistic->setPath(entity->path.c_str());
228         ai_ballistic->setAltitude(entity->altitude);
229         ai_ballistic->setLongitude(entity->longitude);
230         ai_ballistic->setLatitude(entity->latitude);
231         ai_ballistic->setDragArea(entity->eda);
232         ai_ballistic->setLife(entity->life);
233         ai_ballistic->setBuoyancy(entity->buoyancy);
234         ai_ballistic->setWind_from_east(entity->wind_from_east);
235         ai_ballistic->setWind_from_north(entity->wind_from_north);
236         ai_ballistic->setWind(entity->wind);
237         ai_ballistic->setRoll(entity->roll);
238         ai_ballistic->setCd(entity->cd);
239         ai_ballistic->setMass(entity->mass);
240         ai_ballistic->setStabilisation(entity->aero_stabilised);
241         ai_ballistic->init();
242         ai_ballistic->bind();
243         return ai_ballistic;
244 }
245
246 void*
247 FGAIManager::createStorm( FGAIModelEntity *entity ) {
248
249         FGAIStorm* ai_storm = new FGAIStorm(this);
250         ++numObjects[0];
251         ++numObjects[FGAIBase::otStorm];
252         ai_storm->setHeading(entity->heading);
253         ai_storm->setSpeed(entity->speed);
254         ai_storm->setPath(entity->path.c_str());
255         ai_storm->setAltitude(entity->altitude);
256         ai_storm->setLongitude(entity->longitude);
257         ai_storm->setLatitude(entity->latitude);
258         ai_storm->init();
259         ai_storm->bind();
260         ai_list.push_back(ai_storm);
261         return ai_storm;
262 }
263
264 void*
265 FGAIManager::createThermal( FGAIModelEntity *entity ) {
266
267         FGAIThermal* ai_thermal = new FGAIThermal(this);
268         ++numObjects[0];
269         ++numObjects[FGAIBase::otThermal];
270         ai_thermal->setLongitude(entity->longitude);
271         ai_thermal->setLatitude(entity->latitude);
272         ai_thermal->setMaxStrength(entity->strength);
273         ai_thermal->setDiameter(entity->diameter / 6076.11549);
274         ai_thermal->init();
275         ai_thermal->bind();
276         ai_list.push_back(ai_thermal);
277         return ai_thermal;
278 }
279
280 void FGAIManager::destroyObject( void* ID ) {
281         ai_list_itr = ai_list.begin();
282         while(ai_list_itr != ai_list.end()) {
283             if ((*ai_list_itr)->getID() == ID) {
284               --numObjects[0];
285               --numObjects[(*ai_list_itr)->getType()];
286               (*ai_list_itr)->unbind();
287               delete (*ai_list_itr);
288               ai_list.erase(ai_list_itr);
289
290               break;
291             }
292             ++ai_list_itr;
293         }
294 }
295
296 // fetch the user's state every 10 sim cycles
297 void FGAIManager::fetchUserState( void ) {
298    ++dt_count;
299    if (dt_count == 10) {
300      user_latitude  = fgGetDouble("/position/latitude-deg");
301      user_longitude = fgGetDouble("/position/longitude-deg");
302      user_altitude  = fgGetDouble("/position/altitude-ft");
303      user_heading   = fgGetDouble("/orientation/heading-deg");
304      user_pitch     = fgGetDouble("/orientation/pitch-deg");
305      user_yaw       = fgGetDouble("/orientation/side-slip-deg");
306      user_speed     = fgGetDouble("/velocities/uBody-fps") * 0.592484;
307      dt_count = 0;
308    }
309 }
310
311
312 // only keep the results from the nearest thermal
313 void FGAIManager::processThermal( FGAIThermal* thermal ) {
314   thermal->update(_dt);
315   if ( thermal->_getRange() < range_nearest ) {
316      range_nearest = thermal->_getRange();
317      strength = thermal->getStrength();
318   }
319 }
320
321
322 void FGAIManager::processScenario( string &filename ) {
323   FGAIScenario* s = new FGAIScenario( filename );
324   for (int i=0;i<s->nEntries();i++) {
325     FGAIModelEntity* en = s->getNextEntry();
326
327     if (en) {
328       if ( en->m_type == "aircraft") {
329         createAircraft( en );
330
331       } else if ( en->m_type == "ship") {
332         createShip( en );
333
334       } else if ( en->m_type == "carrier") {
335         createCarrier( en );
336
337       } else if ( en->m_type == "thunderstorm") {
338         createStorm( en );
339
340       } else if ( en->m_type == "thermal") {
341         createThermal( en );
342
343       } else if ( en->m_type == "ballistic") {
344         createBallistic( en );
345       }      
346     }
347   }
348
349   delete s;
350 }
351
352 //end AIManager.cxx