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() {
44 FGAIManager::~FGAIManager() {
45 ai_list_itr = ai_list.begin();
46 while(ai_list_itr != ai_list.end()) {
47 delete (*ai_list_itr);
55 void FGAIManager::init() {
57 root = fgGetNode("sim/ai", true);
58 wind_from_down = fgGetNode("/environment/wind-from-down-fps", true);
60 for (int i = 0; i < root->nChildren(); i++) {
61 const SGPropertyNode * entry = root->getChild(i);
63 if (!strcmp(entry->getName(), "entry")) {
64 if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
66 rval = createAircraft( entry->getStringValue("class", ""),
67 entry->getStringValue("path"),
68 entry->getDoubleValue("latitude"),
69 entry->getDoubleValue("longitude"),
70 entry->getDoubleValue("altitude-ft"),
71 entry->getDoubleValue("heading"),
72 entry->getDoubleValue("speed-KTAS"),
74 entry->getDoubleValue("bank") );
76 } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
78 rval = createShip( entry->getStringValue("path"),
79 entry->getDoubleValue("latitude"),
80 entry->getDoubleValue("longitude"),
81 entry->getDoubleValue("altitude-ft"),
82 entry->getDoubleValue("heading"),
83 entry->getDoubleValue("speed-KTAS"),
84 entry->getDoubleValue("rudder") );
86 } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
88 rval = createBallistic( entry->getStringValue("path"),
89 entry->getDoubleValue("latitude"),
90 entry->getDoubleValue("longitude"),
91 entry->getDoubleValue("altitude-ft"),
92 entry->getDoubleValue("azimuth"),
93 entry->getDoubleValue("elevation"),
94 entry->getDoubleValue("speed") );
96 } else if (!strcmp(entry->getStringValue("type", ""), "storm")) {
98 rval = createStorm( entry->getStringValue("path"),
99 entry->getDoubleValue("latitude"),
100 entry->getDoubleValue("longitude"),
101 entry->getDoubleValue("altitude-ft"),
102 entry->getDoubleValue("heading"),
103 entry->getDoubleValue("speed-KTAS") );
105 } else if (!strcmp(entry->getStringValue("type", ""), "thermal")) {
107 rval = createThermal( entry->getDoubleValue("latitude"),
108 entry->getDoubleValue("longitude"),
109 entry->getDoubleValue("strength-fps"),
110 entry->getDoubleValue("diameter-ft") );
120 void FGAIManager::bind() {
121 root = globals->get_props()->getNode("ai/models", true);
122 root->tie("count", SGRawValuePointer<int>(&numObjects));
126 void FGAIManager::unbind() {
127 root->untie("count");
131 void FGAIManager::update(double dt) {
133 // initialize these for finding nearest thermals
134 range_nearest = 10000.0;
139 ai_list_itr = ai_list.begin();
140 while(ai_list_itr != ai_list.end()) {
141 if ((*ai_list_itr)->getDie()) {
142 freeID((*ai_list_itr)->getID());
143 delete (*ai_list_itr);
144 ai_list.erase(ai_list_itr);
149 if ((*ai_list_itr)->isa(FGAIBase::otThermal)) {
150 processThermal((FGAIThermal*)*ai_list_itr);
152 (*ai_list_itr)->update(_dt);
157 wind_from_down->setDoubleValue( strength );
161 // This function returns the next available ID
162 int FGAIManager::assignID() {
166 for (x=0; x<maxint; x++) {
168 id_itr = ids.begin();
169 while( id_itr != ids.end() ) {
170 if ((*id_itr) == x) used = true;
178 return -1; // no available ID's
182 // This function removes an ID from the ID array, making it
183 // available for assignment to another AI object
184 void FGAIManager::freeID( int ID ) {
185 id_itr = ids.begin();
186 while( id_itr != ids.end() ) {
195 int FGAIManager::createAircraft( string model_class, string path,
196 double latitude, double longitude, double altitude,
197 double heading, double speed, double pitch, double roll ) {
199 FGAIAircraft* ai_plane = new FGAIAircraft(this);
200 ai_list.push_back(ai_plane);
201 ai_plane->setID( assignID() );
203 if (model_class == "light") {
204 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
205 } else if (model_class == "ww2_fighter") {
206 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
207 } else if (model_class == "jet_transport") {
208 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
209 } else if (model_class == "jet_fighter") {
210 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
212 ai_plane->setHeading(heading);
213 ai_plane->setSpeed(speed);
214 ai_plane->setPath(path.c_str());
215 ai_plane->setAltitude(altitude);
216 ai_plane->setLongitude(longitude);
217 ai_plane->setLatitude(latitude);
218 ai_plane->setBank(roll);
221 return ai_plane->getID();
225 int FGAIManager::createShip( string path, double latitude, double longitude,
226 double altitude, double heading, double speed,
229 FGAIShip* ai_ship = new FGAIShip(this);
230 ai_list.push_back(ai_ship);
231 ai_ship->setID( assignID() );
233 ai_ship->setHeading(heading);
234 ai_ship->setSpeed(speed);
235 ai_ship->setPath(path.c_str());
236 ai_ship->setAltitude(altitude);
237 ai_ship->setLongitude(longitude);
238 ai_ship->setLatitude(latitude);
239 ai_ship->setBank(rudder);
242 return ai_ship->getID();
246 int FGAIManager::createBallistic( string path, double latitude, double longitude,
247 double altitude, double azimuth, double elevation,
250 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
251 ai_list.push_back(ai_ballistic);
252 ai_ballistic->setID( assignID() );
254 ai_ballistic->setAzimuth(azimuth);
255 ai_ballistic->setElevation(elevation);
256 ai_ballistic->setSpeed(speed);
257 ai_ballistic->setPath(path.c_str());
258 ai_ballistic->setAltitude(altitude);
259 ai_ballistic->setLongitude(longitude);
260 ai_ballistic->setLatitude(latitude);
261 ai_ballistic->init();
262 ai_ballistic->bind();
263 return ai_ballistic->getID();
266 int FGAIManager::createStorm( string path, double latitude, double longitude,
267 double altitude, double heading, double speed ) {
269 FGAIStorm* ai_storm = new FGAIStorm(this);
270 ai_list.push_back(ai_storm);
271 ai_storm->setID( assignID() );
273 ai_storm->setHeading(heading);
274 ai_storm->setSpeed(speed);
275 ai_storm->setPath(path.c_str());
276 ai_storm->setAltitude(altitude);
277 ai_storm->setLongitude(longitude);
278 ai_storm->setLatitude(latitude);
281 return ai_storm->getID();
284 int FGAIManager::createThermal( double latitude, double longitude,
285 double strength, double diameter ) {
287 FGAIThermal* ai_thermal = new FGAIThermal(this);
288 ai_list.push_back(ai_thermal);
289 ai_thermal->setID( assignID() );
291 ai_thermal->setLongitude(longitude);
292 ai_thermal->setLatitude(latitude);
293 ai_thermal->setMaxStrength(strength);
294 ai_thermal->setDiameter(diameter / 6076.11549);
297 return ai_thermal->getID();
300 void FGAIManager::destroyObject( int ID ) {
301 ai_list_itr = ai_list.begin();
302 while(ai_list_itr != ai_list.end()) {
303 if ((*ai_list_itr)->getID() == ID) {
305 delete (*ai_list_itr);
306 ai_list.erase(ai_list_itr);
315 // fetch the user's state every 10 sim cycles
316 void FGAIManager::fetchUserState( void ) {
318 if (dt_count == 10) {
319 user_latitude = fgGetDouble("/position/latitude-deg");
320 user_longitude = fgGetDouble("/position/longitude-deg");
321 user_altitude = fgGetDouble("/position/altitude-ft");
322 user_heading = fgGetDouble("/orientation/heading-deg");
323 user_pitch = fgGetDouble("/orientation/pitch-deg");
324 user_yaw = fgGetDouble("/orientation/side-slip-deg");
325 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;
331 // only keep the results from the nearest thermal
332 void FGAIManager::processThermal( FGAIThermal* thermal ) {
333 thermal->update(_dt);
334 if ( thermal->_getRange() < range_nearest ) {
335 range_nearest = thermal->_getRange();
336 strength = thermal->getStrength();