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>
27 #include "AIManager.hxx"
28 #include "AIAircraft.hxx"
30 #include "AIBallistic.hxx"
31 #include "AIStorm.hxx"
32 #include "AIThermal.hxx"
37 FGAIManager::FGAIManager() {
39 for (int i=0; i < FGAIBase::MAX_OBJECTS; i++)
43 scenario_filename = "";
48 FGAIManager::~FGAIManager() {
49 ai_list_itr = ai_list.begin();
50 while(ai_list_itr != ai_list.end()) {
51 delete (*ai_list_itr);
59 void FGAIManager::init() {
60 root = fgGetNode("sim/ai", true);
62 enabled = root->getNode("enabled", true)->getBoolValue();
66 wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
68 scenario_filename = root->getNode("scenario", true)->getStringValue();
70 if (scenario_filename != "") processScenario( scenario_filename );
75 void FGAIManager::bind() {
76 root = globals->get_props()->getNode("ai/models", true);
77 root->tie("count", SGRawValuePointer<int>(&numObjects[0]));
81 void FGAIManager::unbind() {
86 void FGAIManager::update(double dt) {
88 // initialize these for finding nearest thermals
89 range_nearest = 10000.0;
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()];
104 if ( ai_list_itr == ai_list.begin() ) {
105 ai_list.erase(ai_list_itr);
106 ai_list_itr = ai_list.begin();
109 ai_list.erase(ai_list_itr--);
113 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
114 processThermal((FGAIThermal*)*ai_list_itr);
116 (*ai_list_itr)->update(_dt);
121 wind_from_down_node->setDoubleValue( strength );
126 // This function returns the next available ID
127 int FGAIManager::assignID() {
131 for (x=1; x<maxint; x++) {
133 id_itr = ids.begin();
134 while( id_itr != ids.end() ) {
135 if ((*id_itr) == x) used = true;
143 return -1; // no available ID's
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() ) {
160 int FGAIManager::createAircraft( FGAIModelEntity *entity ) {
162 FGAIAircraft* ai_plane = new FGAIAircraft(this);
163 ai_list.push_back(ai_plane);
164 ai_plane->setID( assignID() );
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);
179 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
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);
190 ai_plane->SetFlightPlan(entity->fp);
195 return ai_plane->getID();
198 int FGAIManager::createShip( FGAIModelEntity *entity ) {
200 FGAIShip* ai_ship = new FGAIShip(this);
201 ai_list.push_back(ai_ship);
202 ai_ship->setID( assignID() );
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);
214 ai_ship->setFlightPlan(entity->fp);
219 return ai_ship->getID();
222 int FGAIManager::createBallistic( FGAIModelEntity *entity ) {
224 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
225 ai_list.push_back(ai_ballistic);
226 ai_ballistic->setID( assignID() );
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();
247 int FGAIManager::createStorm( FGAIModelEntity *entity ) {
249 FGAIStorm* ai_storm = new FGAIStorm(this);
250 ai_list.push_back(ai_storm);
251 ai_storm->setID( assignID() );
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);
262 return ai_storm->getID();
265 int FGAIManager::createThermal( FGAIModelEntity *entity ) {
267 FGAIThermal* ai_thermal = new FGAIThermal(this);
268 ai_list.push_back(ai_thermal);
269 ai_thermal->setID( assignID() );
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);
278 return ai_thermal->getID();
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) {
287 --numObjects[(*ai_list_itr)->getType()];
288 delete (*ai_list_itr);
289 ai_list.erase(ai_list_itr);
297 // fetch the user's state every 10 sim cycles
298 void FGAIManager::fetchUserState( void ) {
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;
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();
323 void FGAIManager::processScenario( string filename ) {
324 FGAIScenario* s = new FGAIScenario( filename );
326 for (int i=0;i<s->nEntries();i++) {
327 FGAIModelEntity* en = s->getNextEntry();
331 if (en->flightplan != ""){
332 en->fp = new FGAIFlightPlan( en->flightplan );
335 if (en->m_class == "aircraft") {
336 createAircraft( en );
338 } else if (en->m_class == "ship") {
341 } else if (en->m_class == "storm") {
344 } else if (en->m_class == "thermal") {
347 } else if (en->m_class == "ballistic") {
348 createBallistic( en );
356 int FGAIManager::getNum( FGAIBase::object_type ot ) {
357 return numObjects[ot];