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