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() {
42 scenario_filename = "";
47 FGAIManager::~FGAIManager() {
48 ai_list_itr = ai_list.begin();
49 while(ai_list_itr != ai_list.end()) {
50 delete (*ai_list_itr);
58 void FGAIManager::init() {
59 root = fgGetNode("sim/ai", true);
61 enabled = root->getNode("enabled", true)->getBoolValue();
65 wind_from_down_node = fgGetNode("/environment/wind-from-down-fps", true);
67 scenario_filename = root->getNode("scenario", true)->getStringValue();
69 if (scenario_filename != "") processScenario( scenario_filename );
74 void FGAIManager::bind() {
75 root = globals->get_props()->getNode("ai/models", true);
76 root->tie("count", SGRawValuePointer<int>(&numObjects));
80 void FGAIManager::unbind() {
85 void FGAIManager::update(double dt) {
87 // initialize these for finding nearest thermals
88 range_nearest = 10000.0;
96 ai_list_itr = ai_list.begin();
97 while(ai_list_itr != ai_list.end()) {
98 if ((*ai_list_itr)->getDie()) {
99 freeID((*ai_list_itr)->getID());
100 delete (*ai_list_itr);
102 if ( ai_list_itr == ai_list.begin() ) {
103 ai_list.erase(ai_list_itr);
104 ai_list_itr = ai_list.begin();
107 ai_list.erase(ai_list_itr--);
111 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
112 processThermal((FGAIThermal*)*ai_list_itr);
114 (*ai_list_itr)->update(_dt);
119 wind_from_down_node->setDoubleValue( strength );
124 // This function returns the next available ID
125 int FGAIManager::assignID() {
129 for (x=1; x<maxint; x++) {
131 id_itr = ids.begin();
132 while( id_itr != ids.end() ) {
133 if ((*id_itr) == x) used = true;
141 return -1; // no available ID's
145 // This function removes an ID from the ID array, making it
146 // available for assignment to another AI object
147 void FGAIManager::freeID( int ID ) {
148 id_itr = ids.begin();
149 while( id_itr != ids.end() ) {
158 int FGAIManager::createAircraft( FGAIModelEntity *entity ) {
160 FGAIAircraft* ai_plane = new FGAIAircraft(this);
161 ai_list.push_back(ai_plane);
162 ai_plane->setID( assignID() );
164 if (entity->m_class == "light") {
165 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
166 } else if (entity->m_class == "ww2_fighter") {
167 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
168 } else if (entity->m_class == "jet_transport") {
169 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
170 } else if (entity->m_class == "jet_fighter") {
171 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
172 } else if (entity->m_class == "tanker") {
173 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
174 ai_plane->SetTanker(true);
176 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
178 ai_plane->setHeading(entity->heading);
179 ai_plane->setSpeed(entity->speed);
180 ai_plane->setPath(entity->path);
181 ai_plane->setAltitude(entity->altitude);
182 ai_plane->setLongitude(entity->longitude);
183 ai_plane->setLatitude(entity->latitude);
184 ai_plane->setBank(entity->roll);
187 ai_plane->SetFlightPlan(entity->fp);
192 return ai_plane->getID();
195 int FGAIManager::createShip( FGAIModelEntity *entity ) {
197 FGAIShip* ai_ship = new FGAIShip(this);
198 ai_list.push_back(ai_ship);
199 ai_ship->setID( assignID() );
201 ai_ship->setHeading(entity->heading);
202 ai_ship->setSpeed(entity->speed);
203 ai_ship->setPath(entity->path);
204 ai_ship->setAltitude(entity->altitude);
205 ai_ship->setLongitude(entity->longitude);
206 ai_ship->setLatitude(entity->latitude);
207 ai_ship->setBank(entity->rudder);
210 ai_ship->setFlightPlan(entity->fp);
215 return ai_ship->getID();
218 int FGAIManager::createBallistic( FGAIModelEntity *entity ) {
220 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
221 ai_list.push_back(ai_ballistic);
222 ai_ballistic->setID( assignID() );
224 ai_ballistic->setAzimuth(entity->azimuth);
225 ai_ballistic->setElevation(entity->elevation);
226 ai_ballistic->setSpeed(entity->speed);
227 ai_ballistic->setPath(entity->path);
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->init();
238 ai_ballistic->bind();
239 return ai_ballistic->getID();
242 int FGAIManager::createStorm( FGAIModelEntity *entity ) {
244 FGAIStorm* ai_storm = new FGAIStorm(this);
245 ai_list.push_back(ai_storm);
246 ai_storm->setID( assignID() );
248 ai_storm->setHeading(entity->heading);
249 ai_storm->setSpeed(entity->speed);
250 ai_storm->setPath(entity->path);
251 ai_storm->setAltitude(entity->altitude);
252 ai_storm->setLongitude(entity->longitude);
253 ai_storm->setLatitude(entity->latitude);
256 return ai_storm->getID();
259 int FGAIManager::createThermal( FGAIModelEntity *entity ) {
261 FGAIThermal* ai_thermal = new FGAIThermal(this);
262 ai_list.push_back(ai_thermal);
263 ai_thermal->setID( assignID() );
265 ai_thermal->setLongitude(entity->longitude);
266 ai_thermal->setLatitude(entity->latitude);
267 ai_thermal->setMaxStrength(entity->strength);
268 ai_thermal->setDiameter(entity->diameter / 6076.11549);
271 return ai_thermal->getID();
274 void FGAIManager::destroyObject( int ID ) {
275 ai_list_itr = ai_list.begin();
276 while(ai_list_itr != ai_list.end()) {
277 if ((*ai_list_itr)->getID() == ID) {
279 delete (*ai_list_itr);
280 ai_list.erase(ai_list_itr);
289 // fetch the user's state every 10 sim cycles
290 void FGAIManager::fetchUserState( void ) {
292 if (dt_count == 10) {
293 user_latitude = fgGetDouble("/position/latitude-deg");
294 user_longitude = fgGetDouble("/position/longitude-deg");
295 user_altitude = fgGetDouble("/position/altitude-ft");
296 user_heading = fgGetDouble("/orientation/heading-deg");
297 user_pitch = fgGetDouble("/orientation/pitch-deg");
298 user_yaw = fgGetDouble("/orientation/side-slip-deg");
299 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
305 // only keep the results from the nearest thermal
306 void FGAIManager::processThermal( FGAIThermal* thermal ) {
307 thermal->update(_dt);
308 if ( thermal->_getRange() < range_nearest ) {
309 range_nearest = thermal->_getRange();
310 strength = thermal->getStrength();
315 void FGAIManager::processScenario( string filename ) {
316 FGAIScenario* s = new FGAIScenario( filename );
318 for (int i=0;i<s->nEntries();i++) {
319 FGAIModelEntity* en = s->getNextEntry();
323 if (en->flightplan != ""){
324 en->fp = new FGAIFlightPlan( en->flightplan );
327 if (en->m_class == "aircraft") {
328 createAircraft( en );
330 } else if (en->m_class == "ship") {
333 } else if (en->m_class == "storm") {
336 } else if (en->m_class == "thermal") {
339 } else if (en->m_class == "ballistic") {
340 createBallistic( en );
348 int FGAIManager::getNum( FGAIBase::object_type ot ) {
349 ai_list_iterator itr = ai_list.begin();
351 while(itr != ai_list.end()) {
352 if ((*itr)->getType() == ot) {