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_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);
78 scenario_filename = root->getNode("scenario", true)->getStringValue();
79 if (scenario_filename != "") processScenario( scenario_filename );
85 void FGAIManager::bind() {
86 root = globals->get_props()->getNode("ai/models", true);
87 root->tie("count", SGRawValuePointer<int>(&numObjects[0]));
91 void FGAIManager::unbind() {
96 void FGAIManager::update(double dt) {
98 // initialize these for finding nearest thermals
99 range_nearest = 10000.0;
101 FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("Traffic Manager");
108 ai_list_itr = ai_list.begin();
109 while(ai_list_itr != ai_list.end()) {
110 if ((*ai_list_itr)->getDie()) {
111 tmgr->release((*ai_list_itr)->getID());
112 --numObjects[(*ai_list_itr)->getType()];
114 (*ai_list_itr)->unbind();
115 delete (*ai_list_itr);
116 if ( ai_list_itr == ai_list.begin() ) {
117 ai_list.erase(ai_list_itr);
118 ai_list_itr = ai_list.begin();
121 ai_list.erase(ai_list_itr--);
125 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
126 processThermal((FGAIThermal*)*ai_list_itr);
128 (*ai_list_itr)->update(_dt);
133 wind_from_down_node->setDoubleValue( strength );
139 FGAIManager::createAircraft( FGAIModelEntity *entity, FGAISchedule *ref) {
141 FGAIAircraft* ai_plane = new FGAIAircraft(this, ref);
142 ai_list.push_back(ai_plane);
144 ++numObjects[FGAIBase::otAircraft];
145 if (entity->m_class == "light") {
146 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
147 } else if (entity->m_class == "ww2_fighter") {
148 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
149 } else if (entity->m_class == "jet_transport") {
150 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
151 } else if (entity->m_class == "jet_fighter") {
152 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
153 } else if (entity->m_class == "tanker") {
154 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
155 ai_plane->SetTanker(true);
157 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
159 ai_plane->setAcType(entity->acType);
160 ai_plane->setCompany(entity->company);
161 ai_plane->setHeading(entity->heading);
162 ai_plane->setSpeed(entity->speed);
163 ai_plane->setPath(entity->path.c_str());
164 ai_plane->setAltitude(entity->altitude);
165 ai_plane->setLongitude(entity->longitude);
166 ai_plane->setLatitude(entity->latitude);
167 ai_plane->setBank(entity->roll);
170 ai_plane->SetFlightPlan(entity->fp);
179 FGAIManager::createShip( FGAIModelEntity *entity ) {
181 // cout << "creating ship" << endl;
183 FGAIShip* ai_ship = new FGAIShip(this);
184 ai_list.push_back(ai_ship);
186 ++numObjects[FGAIBase::otShip];
187 ai_ship->setHeading(entity->heading);
188 ai_ship->setSpeed(entity->speed);
189 ai_ship->setPath(entity->path.c_str());
190 ai_ship->setAltitude(entity->altitude);
191 ai_ship->setLongitude(entity->longitude);
192 ai_ship->setLatitude(entity->latitude);
193 ai_ship->setBank(entity->rudder);
196 ai_ship->setFlightPlan(entity->fp);
205 FGAIManager::createCarrier( FGAIModelEntity *entity ) {
207 // cout << "creating carrier" << endl;
209 FGAICarrier* ai_carrier = new FGAICarrier(this);
210 ai_list.push_back(ai_carrier);
212 ++numObjects[FGAIBase::otShip];
213 ai_carrier->setHeading(entity->heading);
214 ai_carrier->setSpeed(entity->speed);
215 ai_carrier->setPath(entity->path.c_str());
216 ai_carrier->setAltitude(entity->altitude);
217 ai_carrier->setLongitude(entity->longitude);
218 ai_carrier->setLatitude(entity->latitude);
219 ai_carrier->setBank(entity->rudder);
220 ai_carrier->setSolidObjects(entity->solid_objects);
221 ai_carrier->setWireObjects(entity->wire_objects);
222 ai_carrier->setCatapultObjects(entity->catapult_objects);
223 ai_carrier->setRadius(entity->radius);
224 ai_carrier->setXoffset(entity->x_offset);
225 ai_carrier->setYoffset(entity->y_offset);
226 ai_carrier->setZoffset(entity->z_offset);
229 ai_carrier->setFlightPlan(entity->fp);
238 FGAIManager::createBallistic( FGAIModelEntity *entity ) {
240 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
241 ai_list.push_back(ai_ballistic);
243 ++numObjects[FGAIBase::otBallistic];
244 ai_ballistic->setAzimuth(entity->azimuth);
245 ai_ballistic->setElevation(entity->elevation);
246 ai_ballistic->setSpeed(entity->speed);
247 ai_ballistic->setPath(entity->path.c_str());
248 ai_ballistic->setAltitude(entity->altitude);
249 ai_ballistic->setLongitude(entity->longitude);
250 ai_ballistic->setLatitude(entity->latitude);
251 ai_ballistic->setDragArea(entity->eda);
252 ai_ballistic->setLife(entity->life);
253 ai_ballistic->setBuoyancy(entity->buoyancy);
254 ai_ballistic->setWind_from_east(entity->wind_from_east);
255 ai_ballistic->setWind_from_north(entity->wind_from_north);
256 ai_ballistic->setWind(entity->wind);
257 ai_ballistic->setRoll(entity->roll);
258 ai_ballistic->setCd(entity->cd);
259 ai_ballistic->setMass(entity->mass);
260 ai_ballistic->setStabilisation(entity->aero_stabilised);
261 ai_ballistic->init();
262 ai_ballistic->bind();
267 FGAIManager::createStorm( FGAIModelEntity *entity ) {
269 FGAIStorm* ai_storm = new FGAIStorm(this);
271 ++numObjects[FGAIBase::otStorm];
272 ai_storm->setHeading(entity->heading);
273 ai_storm->setSpeed(entity->speed);
274 ai_storm->setPath(entity->path.c_str());
275 ai_storm->setAltitude(entity->altitude);
276 ai_storm->setLongitude(entity->longitude);
277 ai_storm->setLatitude(entity->latitude);
280 ai_list.push_back(ai_storm);
285 FGAIManager::createThermal( FGAIModelEntity *entity ) {
287 FGAIThermal* ai_thermal = new FGAIThermal(this);
289 ++numObjects[FGAIBase::otThermal];
290 ai_thermal->setLongitude(entity->longitude);
291 ai_thermal->setLatitude(entity->latitude);
292 ai_thermal->setMaxStrength(entity->strength);
293 ai_thermal->setDiameter(entity->diameter / 6076.11549);
296 ai_list.push_back(ai_thermal);
300 void FGAIManager::destroyObject( void* ID ) {
301 ai_list_itr = ai_list.begin();
302 while(ai_list_itr != ai_list.end()) {
303 if ((*ai_list_itr)->getID() == ID) {
305 --numObjects[(*ai_list_itr)->getType()];
306 (*ai_list_itr)->unbind();
307 delete (*ai_list_itr);
308 ai_list.erase(ai_list_itr);
316 // fetch the user's state every 10 sim cycles
317 void FGAIManager::fetchUserState( void ) {
319 if (dt_count == 10) {
320 user_latitude = fgGetDouble("/position/latitude-deg");
321 user_longitude = fgGetDouble("/position/longitude-deg");
322 user_altitude = fgGetDouble("/position/altitude-ft");
323 user_heading = fgGetDouble("/orientation/heading-deg");
324 user_pitch = fgGetDouble("/orientation/pitch-deg");
325 user_yaw = fgGetDouble("/orientation/side-slip-deg");
326 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
332 // only keep the results from the nearest thermal
333 void FGAIManager::processThermal( FGAIThermal* thermal ) {
334 thermal->update(_dt);
335 if ( thermal->_getRange() < range_nearest ) {
336 range_nearest = thermal->_getRange();
337 strength = thermal->getStrength();
342 void FGAIManager::processScenario( string &filename ) {
343 FGAIScenario* s = new FGAIScenario( filename );
344 for (int i=0;i<s->nEntries();i++) {
345 FGAIModelEntity* en = s->getNextEntry();
348 if ( en->m_type == "aircraft") {
349 createAircraft( en );
351 } else if ( en->m_type == "ship") {
354 } else if ( en->m_type == "carrier") {
357 } else if ( en->m_type == "thunderstorm") {
360 } else if ( en->m_type == "thermal") {
363 } else if ( en->m_type == "ballistic") {
364 createBallistic( en );
372 // This code keeps track of models that have already been loaded
373 // Eventually we'd prbably need to find a way to keep track of models
374 // that are unloaded again
375 ssgBranch * FGAIManager::getModel(const string& path)
377 ModelVecIterator i = loadedModels.begin();
378 while (i != loadedModels.end())
380 if (i->getPath() == path)
381 return i->getModelId();
387 void FGAIManager::setModel(const string& path, ssgBranch *model)
389 loadedModels.push_back(FGModelID(path,model));