]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIManager.cxx
b3d3060da74ce6772e3d96dc4ec53fbc8c656ea3
[flightgear.git] / src / AIModel / AIManager.cxx
1 // AIManager.cxx  Based on David Luff's AIMgr:
2 // - a global management class for AI objects
3 //
4 // Written by David Culp, started October 2003.
5 // - davidculp2@comcast.net
6 //
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.
11 //
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.
16 //
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.
20
21 #include <simgear/misc/sg_path.hxx>
22 #include <Main/fg_props.hxx>
23 #include <Main/globals.hxx>
24
25 #include <list>
26
27 #include "AIManager.hxx"
28 #include "AIAircraft.hxx"
29 #include "AIShip.hxx"
30 #include "AIBallistic.hxx"
31
32 SG_USING_STD(list);
33
34
35 FGAIManager::FGAIManager() {
36   initDone = false;
37   numObjects = 0;
38   dt_count = 9;
39 }
40
41 FGAIManager::~FGAIManager() {
42   ai_list_itr = ai_list.begin();
43   while(ai_list_itr != ai_list.end()) {
44       delete (*ai_list_itr);
45       ++ai_list_itr;
46     }
47   ai_list.clear();
48   ids.clear();
49 }
50
51
52 void FGAIManager::init() {
53   int rval;
54   root = fgGetNode("sim/ai", true);
55
56   for (int i = 0; i < root->nChildren(); i++) {
57     const SGPropertyNode * entry = root->getChild(i);
58
59     if (!strcmp(entry->getName(), "entry")) {
60       if (!strcmp(entry->getStringValue("type", ""), "aircraft")) { 
61
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"),
69                                0.0, 
70                                entry->getDoubleValue("bank") );
71
72       } else if (!strcmp(entry->getStringValue("type", ""), "ship")) {
73
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") );
81
82       } else if (!strcmp(entry->getStringValue("type", ""), "ballistic")) {
83
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") );
91
92       } 
93     }
94   }
95
96   initDone = true;
97 }
98
99
100 void FGAIManager::bind() {
101    root = globals->get_props()->getNode("ai/models", true);
102    root->tie("count", SGRawValuePointer<int>(&numObjects));
103 }
104
105
106 void FGAIManager::unbind() {
107     root->untie("count");
108 }
109
110
111 void FGAIManager::update(double dt) {
112         
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);
119                    --ai_list_itr;
120                    --numObjects;
121                 } else {
122                    fetchUserState();
123                    (*ai_list_itr)->update(dt);
124                 }
125                 ++ai_list_itr;
126         }
127 }
128
129
130 // This function returns the next available ID
131 int FGAIManager::assignID() {
132   int maxint = 30000;
133   int x; 
134   bool used;
135   for (x=0; x<maxint; x++) {
136      used = false;
137      id_itr = ids.begin();
138      while( id_itr != ids.end() ) {
139        if ((*id_itr) == x) used = true;
140        ++id_itr;
141      }
142      if (!used) {
143        ids.push_back(x);
144        return x;
145      } 
146   }
147   return -1;  // no available ID's
148 }
149
150
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() ) {
156       if (*id_itr == ID) {
157         ids.erase( id_itr );
158         return;
159       }
160       ++id_itr;
161     }  
162 }
163
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 ) {
167      
168         FGAIAircraft* ai_plane = new FGAIAircraft(this);
169         ai_list.push_back(ai_plane);
170         ai_plane->setID( assignID() );
171         ++numObjects;
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]);
180         }
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);
188         ai_plane->init();
189         ai_plane->bind();
190         return ai_plane->getID();
191 }
192
193
194 int FGAIManager::createShip( string path, double latitude, double longitude,
195                              double altitude, double heading, double speed,
196                              double rudder ) {
197
198         FGAIShip* ai_ship = new FGAIShip(this);
199         ai_list.push_back(ai_ship);
200         ai_ship->setID( assignID() );
201         ++numObjects;
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);
209         ai_ship->init();
210         ai_ship->bind();
211         return ai_ship->getID();
212 }
213
214
215 int FGAIManager::createBallistic( string path, double latitude, double longitude,
216                                   double altitude, double azimuth, double elevation,
217                                   double speed ) {
218
219         FGAIBallistic* ai_ballistic = new FGAIBallistic(this);
220         ai_list.push_back(ai_ballistic);
221         ai_ballistic->setID( assignID() );    
222         ++numObjects;
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();
233 }
234
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) {
239               freeID( ID );
240               delete (*ai_list_itr);
241               ai_list.erase(ai_list_itr);
242               --ai_list_itr;
243               --numObjects;
244               return;
245             }
246             ++ai_list_itr;
247         }
248 }
249
250 // fetch the user's state every 10 sim cycles
251 void FGAIManager::fetchUserState( void ) {
252    ++dt_count;
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;
261      dt_count = 0;
262    }
263 }