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"
35 FGAIManager::FGAIManager() {
41 FGAIManager::~FGAIManager() {
42 ai_list_itr = ai_list.begin();
43 while(ai_list_itr != ai_list.end()) {
44 delete (*ai_list_itr);
52 void FGAIManager::init() {
54 root = fgGetNode("sim/ai", true);
56 for (int i = 0; i < root->nChildren(); i++) {
57 const SGPropertyNode * entry = root->getChild(i);
59 if (!strcmp(entry->getName(), "entry")) {
60 if (!strcmp(entry->getStringValue("type", ""), "aircraft")) {
62 rval = createAircraft( entry->getStringValue("class", ""),
63 entry->getStringValue("path"),
64 entry->getDoubleValue("latitude"),
65 entry->getDoubleValue("longitude"),
66 entry->getDoubleValue("altitude-ft"),
67 entry->getDoubleValue("heading"),
68 entry->getDoubleValue("speed-KTAS"),
70 entry->getDoubleValue("bank") );
72 } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
74 rval = createShip( entry->getStringValue("path"),
75 entry->getDoubleValue("latitude"),
76 entry->getDoubleValue("longitude"),
77 entry->getDoubleValue("altitude-ft"),
78 entry->getDoubleValue("heading"),
79 entry->getDoubleValue("speed-KTAS"),
80 entry->getDoubleValue("rudder") );
82 } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
84 rval = createBallistic( entry->getStringValue("path"),
85 entry->getDoubleValue("latitude"),
86 entry->getDoubleValue("longitude"),
87 entry->getDoubleValue("altitude-ft"),
88 entry->getDoubleValue("azimuth"),
89 entry->getDoubleValue("elevation"),
90 entry->getDoubleValue("speed") );
100 void FGAIManager::bind() {
101 root = globals->get_props()->getNode("ai/models", true);
102 root->tie("count", SGRawValuePointer<int>(&numObjects));
106 void FGAIManager::unbind() {
107 root->untie("count");
111 void FGAIManager::update(double dt) {
113 ai_list_itr = ai_list.begin();
114 while(ai_list_itr != ai_list.end()) {
115 if ((*ai_list_itr)->getDie()) {
116 freeID((*ai_list_itr)->getID());
117 delete (*ai_list_itr);
118 ai_list.erase(ai_list_itr);
123 (*ai_list_itr)->update(dt);
130 // This function returns the next available ID
131 int FGAIManager::assignID() {
135 for (x=0; x<maxint; x++) {
137 id_itr = ids.begin();
138 while( id_itr != ids.end() ) {
139 if ((*id_itr) == x) used = true;
147 return -1; // no available ID's
151 // This function removes an ID from the ID array, making it
152 // available for assignment to another AI object
153 void FGAIManager::freeID( int ID ) {
154 id_itr = ids.begin();
155 while( id_itr != ids.end() ) {
164 int FGAIManager::createAircraft( string model_class, string path,
165 double latitude, double longitude, double altitude,
166 double heading, double speed, double pitch, double roll ) {
168 FGAIAircraft* ai_plane = new FGAIAircraft(this);
169 ai_list.push_back(ai_plane);
170 ai_plane->setID( assignID() );
172 if (model_class == "light") {
173 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::LIGHT]);
174 } else if (model_class == "ww2_fighter") {
175 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::WW2_FIGHTER]);
176 } else if (model_class == "jet_transport") {
177 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_TRANSPORT]);
178 } else if (model_class == "jet_fighter") {
179 ai_plane->SetPerformance(&FGAIAircraft::settings[FGAIAircraft::JET_FIGHTER]);
181 ai_plane->setHeading(heading);
182 ai_plane->setSpeed(speed);
183 ai_plane->setPath(path.c_str());
184 ai_plane->setAltitude(altitude);
185 ai_plane->setLongitude(longitude);
186 ai_plane->setLatitude(latitude);
187 ai_plane->setBank(roll);
190 return ai_plane->getID();
194 int FGAIManager::createShip( string path, double latitude, double longitude,
195 double altitude, double heading, double speed,
198 FGAIShip* ai_ship = new FGAIShip(this);
199 ai_list.push_back(ai_ship);
200 ai_ship->setID( assignID() );
202 ai_ship->setHeading(heading);
203 ai_ship->setSpeed(speed);
204 ai_ship->setPath(path.c_str());
205 ai_ship->setAltitude(altitude);
206 ai_ship->setLongitude(longitude);
207 ai_ship->setLatitude(latitude);
208 ai_ship->setBank(rudder);
211 return ai_ship->getID();
215 int FGAIManager::createBallistic( string path, double latitude, double longitude,
216 double altitude, double azimuth, double elevation,
219 FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
220 ai_list.push_back(ai_ballistic);
221 ai_ballistic->setID( assignID() );
223 ai_ballistic->setAzimuth(azimuth);
224 ai_ballistic->setElevation(elevation);
225 ai_ballistic->setSpeed(speed);
226 ai_ballistic->setPath(path.c_str());
227 ai_ballistic->setAltitude(altitude);
228 ai_ballistic->setLongitude(longitude);
229 ai_ballistic->setLatitude(latitude);
230 ai_ballistic->init();
231 ai_ballistic->bind();
232 return ai_ballistic->getID();
235 void FGAIManager::destroyObject( int ID ) {
236 ai_list_itr = ai_list.begin();
237 while(ai_list_itr != ai_list.end()) {
238 if ((*ai_list_itr)->getID() == ID) {
240 delete (*ai_list_itr);
241 ai_list.erase(ai_list_itr);
250 // fetch the user's state every 10 sim cycles
251 void FGAIManager::fetchUserState( void ) {
253 if (dt_count == 10) {
254 user_latitude = fgGetDouble("/position/latitude-deg");
255 user_longitude = fgGetDouble("/position/longitude-deg");
256 user_altitude = fgGetDouble("/position/altitude-ft");
257 user_heading = fgGetDouble("/orientation/heading-deg");
258 user_pitch = fgGetDouble("/orientation/pitch-deg");
259 user_yaw = fgGetDouble("/orientation/side-slip-deg");
260 user_speed = fgGetDouble("/velocities/uBody-fps") * 0.592484;