1 // AIManager.cxx Based on David Luff's AIMgr:
2 // - a global management class 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() {
43 FGAIManager::~FGAIManager() {
44 ai_list_itr = ai_list.begin();
45 while(ai_list_itr != ai_list.end()) {
46 delete (*ai_list_itr);
54 void FGAIManager::init() {
56 root = fgGetNode("sim/ai", true);
58 for (int i = 0; i < root->nChildren(); i++) {
59 const SGPropertyNode * entry = root->getChild(i);
61 if (!strcmp(entry->getName(), "entry")) {
62 if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
64 rval = createAircraft( entry->getStringValue("class", ""),
65 entry->getStringValue("path"),
66 entry->getDoubleValue("latitude"),
67 entry->getDoubleValue("longitude"),
68 entry->getDoubleValue("altitude-ft"),
69 entry->getDoubleValue("heading"),
70 entry->getDoubleValue("speed-KTAS"),
72 entry->getDoubleValue("bank") );
74 } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
76 rval = createShip( entry->getStringValue("path"),
77 entry->getDoubleValue("latitude"),
78 entry->getDoubleValue("longitude"),
79 entry->getDoubleValue("altitude-ft"),
80 entry->getDoubleValue("heading"),
81 entry->getDoubleValue("speed-KTAS"),
82 entry->getDoubleValue("rudder") );
84 } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
86 rval = createBallistic( entry->getStringValue("path"),
87 entry->getDoubleValue("latitude"),
88 entry->getDoubleValue("longitude"),
89 entry->getDoubleValue("altitude-ft"),
90 entry->getDoubleValue("azimuth"),
91 entry->getDoubleValue("elevation"),
92 entry->getDoubleValue("speed") );
94 } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
96 rval = createStorm( entry->getStringValue("path"),
97 entry->getDoubleValue("latitude"),
98 entry->getDoubleValue("longitude"),
99 entry->getDoubleValue("altitude-ft"),
100 entry->getDoubleValue("heading"),
101 entry->getDoubleValue("speed-KTAS") );
103 } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
105 rval = createThermal( entry->getDoubleValue("latitude"),
106 entry->getDoubleValue("longitude"),
107 entry->getDoubleValue("strength-fps"),
108 entry->getDoubleValue("diameter-ft") );
118 void FGAIManager::bind() {
119 root = globals->get_props()->getNode("ai/models", true);
120 root->tie("count", SGRawValuePointer<int>(&numObjects));
124 void FGAIManager::unbind() {
125 root->untie("count");
129 void FGAIManager::update(double dt) {
131 ai_list_itr = ai_list.begin();
132 while(ai_list_itr != ai_list.end()) {
133 if ((*ai_list_itr)->getDie()) {
134 freeID((*ai_list_itr)->getID());
135 delete (*ai_list_itr);
136 ai_list.erase(ai_list_itr);
141 (*ai_list_itr)->update(dt);
148 // This function returns the next available ID
149 int FGAIManager::assignID() {
153 for (x=0; x<maxint; x++) {
155 id_itr = ids.begin();
156 while( id_itr != ids.end() ) {
157 if ((*id_itr) == x) used = true;
165 return -1; // no available ID's
169 // This function removes an ID from the ID array, making it
170 // available for assignment to another AI object
171 void FGAIManager::freeID( int ID ) {
172 id_itr = ids.begin();
173 while( id_itr != ids.end() ) {
182 int FGAIManager::createAircraft( string model_class, string path,
183 double latitude, double longitude, double altitude,
184 double heading, double speed, double pitch, double roll ) {
186 FGAIAircraft* ai_plane = new FGAIAircraft(this);
187 ai_list.push_back(ai_plane);
188 ai_plane->setID( assignID() );
190 if (model_class == "light") {
191 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
192 } else if (model_class == "ww2_fighter") {
193 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
194 } else if (model_class == "jet_transport") {
195 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
196 } else if (model_class == "jet_fighter") {
197 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
199 ai_plane->setHeading(heading);
200 ai_plane->setSpeed(speed);
201 ai_plane->setPath(path.c_str());
202 ai_plane->setAltitude(altitude);
203 ai_plane->setLongitude(longitude);
204 ai_plane->setLatitude(latitude);
205 ai_plane->setBank(roll);
208 return ai_plane->getID();
212 int FGAIManager::createShip( string path, double latitude, double longitude,
213 double altitude, double heading, double speed,
216 FGAIShip* ai_ship = new FGAIShip(this);
217 ai_list.push_back(ai_ship);
218 ai_ship->setID( assignID() );
220 ai_ship->setHeading(heading);
221 ai_ship->setSpeed(speed);
222 ai_ship->setPath(path.c_str());
223 ai_ship->setAltitude(altitude);
224 ai_ship->setLongitude(longitude);
225 ai_ship->setLatitude(latitude);
226 ai_ship->setBank(rudder);
229 return ai_ship->getID();
233 int FGAIManager::createBallistic( string path, double latitude, double longitude,
234 double altitude, double azimuth, double elevation,
237 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
238 ai_list.push_back(ai_ballistic);
239 ai_ballistic->setID( assignID() );
241 ai_ballistic->setAzimuth(azimuth);
242 ai_ballistic->setElevation(elevation);
243 ai_ballistic->setSpeed(speed);
244 ai_ballistic->setPath(path.c_str());
245 ai_ballistic->setAltitude(altitude);
246 ai_ballistic->setLongitude(longitude);
247 ai_ballistic->setLatitude(latitude);
248 ai_ballistic->init();
249 ai_ballistic->bind();
250 return ai_ballistic->getID();
253 int FGAIManager::createStorm( string path, double latitude, double longitude,
254 double altitude, double heading, double speed ) {
256 FGAIStorm* ai_storm = new FGAIStorm(this);
257 ai_list.push_back(ai_storm);
258 ai_storm->setID( assignID() );
260 ai_storm->setHeading(heading);
261 ai_storm->setSpeed(speed);
262 ai_storm->setPath(path.c_str());
263 ai_storm->setAltitude(altitude);
264 ai_storm->setLongitude(longitude);
265 ai_storm->setLatitude(latitude);
268 return ai_storm->getID();
271 int FGAIManager::createThermal( double latitude, double longitude,
272 double strength, double diameter ) {
274 FGAIThermal* ai_thermal = new FGAIThermal(this);
275 ai_list.push_back(ai_thermal);
276 ai_thermal->setID( assignID() );
278 ai_thermal->setLongitude(longitude);
279 ai_thermal->setLatitude(latitude);
280 ai_thermal->setStrength(strength);
281 ai_thermal->setDiameter(diameter / 6076.11549);
284 return ai_thermal->getID();
287 void FGAIManager::destroyObject( int ID ) {
288 ai_list_itr = ai_list.begin();
289 while(ai_list_itr != ai_list.end()) {
290 if ((*ai_list_itr)->getID() == ID) {
292 delete (*ai_list_itr);
293 ai_list.erase(ai_list_itr);
302 // fetch the user's state every 10 sim cycles
303 void FGAIManager::fetchUserState( void ) {
305 if (dt_count == 10) {
306 user_latitude = fgGetDouble("/position/latitude-deg");
307 user_longitude = fgGetDouble("/position/longitude-deg");
308 user_altitude = fgGetDouble("/position/altitude-ft");
309 user_heading = fgGetDouble("/orientation/heading-deg");
310 user_pitch = fgGetDouble("/orientation/pitch-deg");
311 user_yaw = fgGetDouble("/orientation/side-slip-deg");
312 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;