1 // AIManager.cxx Based on David Luff's AIMgr:
2 // - a global management type for AI objects
4 // Written by David Culp, started October 2003.
5 // - davidculp2@comcast.net
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.
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.
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.
21 #include <simgear/misc/sg_path.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Main/globals.hxx>
25 #include <Airports/simple.hxx>
26 #include <Traffic/SchedFlight.hxx>
27 #include <Traffic/Schedule.hxx>
28 #include <Traffic/TrafficMgr.hxx>
32 #include "AIManager.hxx"
33 #include "AIAircraft.hxx"
35 #include "AIBallistic.hxx"
36 #include "AIStorm.hxx"
37 #include "AIThermal.hxx"
38 #include "AICarrier.hxx"
43 FGAIManager::FGAIManager() {
45 for (int i=0; i < FGAIBase::MAX_OBJECTS; i++)
49 scenario_filename = "";
53 FGAIManager::~FGAIManager() {
54 ai_list_iterator ai_list_itr = ai_list.begin();
55 while(ai_list_itr != ai_list.end()) {
56 (*ai_list_itr)->unbind();
57 delete (*ai_list_itr);
61 ModelVecIterator i = loadedModels.begin();
62 while (i != loadedModels.end())
64 i->getModelId()->deRef();
69 void FGAIManager::init() {
70 root = fgGetNode("sim/ai", true);
72 enabled = root->getNode("enabled", true)->getBoolValue();
76 wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
77 user_latitude_node = fgGetNode("/position/latitude-deg", true);
78 user_longitude_node = fgGetNode("/position/longitude-deg", true);
79 user_altitude_node = fgGetNode("/position/altitude-ft", true);
80 user_heading_node = fgGetNode("/orientation/heading-deg", true);
81 user_pitch_node = fgGetNode("/orientation/pitch-deg", true);
82 user_yaw_node = fgGetNode("/orientation/side-slip-deg", true);
83 user_speed_node = fgGetNode("/velocities/uBody-fps", true);
85 scenario_filename = root->getNode("scenario", true)->getStringValue();
86 if (scenario_filename != "") processScenario( scenario_filename );
92 void FGAIManager::bind() {
93 root = globals->get_props()->getNode("ai/models", true);
94 root->tie("count", SGRawValuePointer<int>(&numObjects[0]));
98 void FGAIManager::unbind() {
103 void FGAIManager::update(double dt) {
105 // initialize these for finding nearest thermals
106 range_nearest = 10000.0;
108 FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
115 ai_list_iterator ai_list_itr = ai_list.begin();
116 while(ai_list_itr != ai_list.end()) {
117 if ((*ai_list_itr)->getDie()) {
118 tmgr->release((*ai_list_itr)->getID());
119 --numObjects[(*ai_list_itr)->getType()];
121 (*ai_list_itr)->unbind();
122 delete (*ai_list_itr);
123 if ( ai_list_itr == ai_list.begin() ) {
124 ai_list.erase(ai_list_itr);
125 ai_list_itr = ai_list.begin();
128 ai_list.erase(ai_list_itr--);
132 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
133 processThermal((FGAIThermal*)*ai_list_itr);
135 (*ai_list_itr)->update(_dt);
140 wind_from_down_node->setDoubleValue( strength ); // for thermals
146 FGAIManager::createAircraft( FGAIModelEntity *entity, FGAISchedule *ref) {
148 FGAIAircraft* ai_plane = new FGAIAircraft(this, ref);
149 ai_list.push_back(ai_plane);
151 ++numObjects[FGAIBase::otAircraft];
152 if (entity->m_class == "light") {
153 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
154 } else if (entity->m_class == "ww2_fighter") {
155 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
156 } else if (entity->m_class == "jet_transport") {
157 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
158 } else if (entity->m_class == "jet_fighter") {
159 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
160 } else if (entity->m_class == "tanker") {
161 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
162 ai_plane->SetTanker(true);
164 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
166 ai_plane->setAcType(entity->acType);
167 ai_plane->setCompany(entity->company);
168 ai_plane->setHeading(entity->heading);
169 ai_plane->setSpeed(entity->speed);
170 ai_plane->setPath(entity->path.c_str());
171 ai_plane->setAltitude(entity->altitude);
172 ai_plane->setLongitude(entity->longitude);
173 ai_plane->setLatitude(entity->latitude);
174 ai_plane->setBank(entity->roll);
177 ai_plane->SetFlightPlan(entity->fp);
186 FGAIManager::createShip( FGAIModelEntity *entity ) {
188 // cout << "creating ship" << endl;
190 FGAIShip* ai_ship = new FGAIShip(this);
191 ai_list.push_back(ai_ship);
193 ++numObjects[FGAIBase::otShip];
194 ai_ship->setHeading(entity->heading);
195 ai_ship->setSpeed(entity->speed);
196 ai_ship->setPath(entity->path.c_str());
197 ai_ship->setAltitude(entity->altitude);
198 ai_ship->setLongitude(entity->longitude);
199 ai_ship->setLatitude(entity->latitude);
200 ai_ship->setBank(entity->rudder);
201 ai_ship->setName(entity->name);
204 ai_ship->setFlightPlan(entity->fp);
213 FGAIManager::createCarrier( FGAIModelEntity *entity ) {
215 // cout << "creating carrier" << endl;
217 FGAICarrier* ai_carrier = new FGAICarrier(this);
218 ai_list.push_back(ai_carrier);
220 ++numObjects[FGAIBase::otCarrier];
221 ai_carrier->setHeading(entity->heading);
222 ai_carrier->setSpeed(entity->speed);
223 ai_carrier->setPath(entity->path.c_str());
224 ai_carrier->setAltitude(entity->altitude);
225 ai_carrier->setLongitude(entity->longitude);
226 ai_carrier->setLatitude(entity->latitude);
227 ai_carrier->setBank(entity->rudder);
228 ai_carrier->setSolidObjects(entity->solid_objects);
229 ai_carrier->setWireObjects(entity->wire_objects);
230 ai_carrier->setCatapultObjects(entity->catapult_objects);
231 ai_carrier->setParkingPositions(entity->ppositions);
232 ai_carrier->setRadius(entity->radius);
233 ai_carrier->setSign(entity->pennant_number);
234 ai_carrier->setName(entity->name);
235 ai_carrier->setFlolsOffset(entity->flols_offset);
238 ai_carrier->setFlightPlan(entity->fp);
247 FGAIManager::createBallistic( FGAIModelEntity *entity ) {
249 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
250 ai_list.push_back(ai_ballistic);
252 ++numObjects[FGAIBase::otBallistic];
253 ai_ballistic->setAzimuth(entity->azimuth);
254 ai_ballistic->setElevation(entity->elevation);
255 ai_ballistic->setSpeed(entity->speed);
256 ai_ballistic->setPath(entity->path.c_str());
257 ai_ballistic->setAltitude(entity->altitude);
258 ai_ballistic->setLongitude(entity->longitude);
259 ai_ballistic->setLatitude(entity->latitude);
260 ai_ballistic->setDragArea(entity->eda);
261 ai_ballistic->setLife(entity->life);
262 ai_ballistic->setBuoyancy(entity->buoyancy);
263 ai_ballistic->setWind_from_east(entity->wind_from_east);
264 ai_ballistic->setWind_from_north(entity->wind_from_north);
265 ai_ballistic->setWind(entity->wind);
266 ai_ballistic->setRoll(entity->roll);
267 ai_ballistic->setCd(entity->cd);
268 ai_ballistic->setMass(entity->mass);
269 ai_ballistic->setStabilisation(entity->aero_stabilised);
270 ai_ballistic->init();
271 ai_ballistic->bind();
276 FGAIManager::createStorm( FGAIModelEntity *entity ) {
278 FGAIStorm* ai_storm = new FGAIStorm(this);
280 ++numObjects[FGAIBase::otStorm];
281 ai_storm->setHeading(entity->heading);
282 ai_storm->setSpeed(entity->speed);
283 ai_storm->setPath(entity->path.c_str());
284 ai_storm->setAltitude(entity->altitude);
285 ai_storm->setDiameter(entity->diameter / 6076.11549);
286 ai_storm->setHeight(entity->height_msl);
287 ai_storm->setStrengthNorm(entity->strength);
288 ai_storm->setLongitude(entity->longitude);
289 ai_storm->setLatitude(entity->latitude);
292 ai_list.push_back(ai_storm);
297 FGAIManager::createThermal( FGAIModelEntity *entity ) {
299 FGAIThermal* ai_thermal = new FGAIThermal(this);
301 ++numObjects[FGAIBase::otThermal];
302 ai_thermal->setLongitude(entity->longitude);
303 ai_thermal->setLatitude(entity->latitude);
304 ai_thermal->setMaxStrength(entity->strength);
305 ai_thermal->setDiameter(entity->diameter / 6076.11549);
306 ai_thermal->setHeight(entity->height_msl);
309 ai_list.push_back(ai_thermal);
313 void FGAIManager::destroyObject( void* ID ) {
314 ai_list_iterator ai_list_itr = ai_list.begin();
315 while(ai_list_itr != ai_list.end()) {
316 if ((*ai_list_itr)->getID() == ID) {
318 --numObjects[(*ai_list_itr)->getType()];
319 (*ai_list_itr)->unbind();
320 delete (*ai_list_itr);
321 ai_list.erase(ai_list_itr);
330 void FGAIManager::fetchUserState( void ) {
331 user_latitude = user_latitude_node->getDoubleValue();
332 user_longitude = user_longitude_node->getDoubleValue();
333 user_altitude = user_altitude_node->getDoubleValue();
334 user_heading = user_heading_node->getDoubleValue();
335 user_pitch = user_pitch_node->getDoubleValue();
336 user_yaw = user_yaw_node->getDoubleValue();
337 user_speed = user_speed_node->getDoubleValue() * 0.592484;
341 // only keep the results from the nearest thermal
342 void FGAIManager::processThermal( FGAIThermal* thermal ) {
343 thermal->update(_dt);
344 if ( thermal->_getRange() < range_nearest ) {
345 range_nearest = thermal->_getRange();
346 strength = thermal->getStrength();
351 void FGAIManager::processScenario( string &filename ) {
352 FGAIScenario* s = new FGAIScenario( filename );
353 for (int i=0;i<s->nEntries();i++) {
354 FGAIModelEntity* en = s->getNextEntry();
357 if ( en->m_type == "aircraft") {
358 createAircraft( en );
360 } else if ( en->m_type == "ship") {
363 } else if ( en->m_type == "carrier") {
366 } else if ( en->m_type == "thunderstorm") {
369 } else if ( en->m_type == "thermal") {
372 } else if ( en->m_type == "ballistic") {
373 createBallistic( en );
381 // This code keeps track of models that have already been loaded
382 // Eventually we'd prbably need to find a way to keep track of models
383 // that are unloaded again
384 ssgBranch * FGAIManager::getModel(const string& path)
386 ModelVecIterator i = loadedModels.begin();
387 while (i != loadedModels.end())
389 if (i->getPath() == path)
390 return i->getModelId();
396 void FGAIManager::setModel(const string& path, ssgBranch *model)
398 loadedModels.push_back(FGModelID(path,model));