1 // AIMgr.cxx - implementation of FGAIMgr
2 // - a global management class for FlightGear generated AI traffic
4 // Written by David Luff, started March 2002.
6 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <Main/fgfs.hxx>
23 #include <Main/fg_props.hxx>
24 #include <Main/globals.hxx>
25 #include <simgear/misc/sg_path.hxx>
32 # include <sys/types.h> // for directory reading
33 # include <dirent.h> // for directory reading
37 #include "AILocalTraffic.hxx"
38 #include "ATCutils.hxx"
44 ATC = globals->get_ATC_mgr();
50 void FGAIMgr::init() {
51 // Pointers to user's position
52 lon_node = fgGetNode("/position/longitude-deg", true);
53 lat_node = fgGetNode("/position/latitude-deg", true);
54 elev_node = fgGetNode("/position/altitude-ft", true);
56 lon = lon_node->getDoubleValue();
57 lat = lat_node->getDoubleValue();
58 elev = elev_node->getDoubleValue();
60 // go through the $FG_ROOT/ATC directory and find all *.taxi files
61 SGPath path(globals->get_fg_root());
63 string dir = path.dir();
68 // WARNING - I (DCL) haven't tested this on MSVC - this is simply cribbed from TerraGear
71 struct _finddata_t de;
74 path_str = dir + "\\*.*";
76 if ( ( hfile = _findfirst( path.c_str(), &de ) ) == -1 ) {
77 cout << "cannot open directory " << dir << "\n";
79 // load all .taxi files
83 ext = file.substr(pos + 1);
85 cout << "TAXI FILE FOUND!!!\n";
86 f_ident = file.substr(0, pos);
88 if(dclFindAirportID(f_ident, &a)) {
89 SGBucket sgb(a.longitude, a.latitude);
90 int idx = sgb.gen_index();
91 if(airports.find(idx) != airports.end()) {
92 airports[idx]->push_back(f_ident);
94 aptID_list_type* apts = new aptID_list_type;
95 apts->push_back(f_ident);
98 cout << "Mapping " << f_ident << " to bucket " << idx << '\n';
101 } while ( _findnext( hfile, &de ) == 0 );
108 if ( (d = opendir( dir.c_str() )) == NULL ) {
109 cout << "cannot open directory " << dir << "\n";
111 cout << "Opened directory " << dir << " OK :-)\n";
112 cout << "Contents are:\n";
113 // load all .taxi files
114 while ( (de = readdir(d)) != NULL ) {
116 pos = file.find(".");
117 cout << file << '\n';
119 ext = file.substr(pos + 1);
121 cout << "TAXI FILE FOUND!!!\n";
122 f_ident = file.substr(0, pos);
124 if(dclFindAirportID(f_ident, &a)) {
125 SGBucket sgb(a.longitude, a.latitude);
126 int idx = sgb.gen_index();
127 if(airports.find(idx) != airports.end()) {
128 airports[idx]->push_back(f_ident);
130 aptID_list_type* apts = new aptID_list_type;
131 apts->push_back(f_ident);
132 airports[idx] = apts;
134 cout << "Mapping " << f_ident << " to bucket " << idx << '\n';
142 // See if are in range at startup and activate if necessary
146 void FGAIMgr::bind() {
149 void FGAIMgr::unbind() {
152 void FGAIMgr::update(double dt) {
156 // Don't update any planes for first 50 runs through - this avoids some possible initialisation anomalies
157 // Might not need it now we have fade-in though?
170 // TODO - need to add a check of if any activated airports have gone out of range
172 // Traverse the list of active planes and run all their update methods
173 // TODO - spread the load - not all planes should need updating every frame.
174 // Note that this will require dt to be calculated for each plane though
175 // since they rely on it to calculate distance travelled.
176 ai_list_itr = ai_list.begin();
177 while(ai_list_itr != ai_list.end()) {
178 (*ai_list_itr)->Update(dt);
184 // Activate AI traffic at an airport
185 void FGAIMgr::ActivateAirport(string ident) {
186 ATC->AIRegisterAirport(ident);
187 // TODO - need to start the traffic more randomly
188 FGAILocalTraffic* local_traffic = new FGAILocalTraffic;
189 //local_traffic->Init(ident, IN_PATTERN, TAKEOFF_ROLL);
190 local_traffic->Init(ident);
191 local_traffic->FlyCircuits(1, true); // Fly 2 circuits with touch & go in between
192 ai_list.push_back(local_traffic);
193 activated[ident] = 1;
197 // Search for valid airports in the vicinity of the user and activate them if necessary
198 void FGAIMgr::SearchByPos(double range)
200 //cout << "In SearchByPos(...)" << endl;
202 // get bucket number for plane position
203 lon = lon_node->getDoubleValue();
204 lat = lat_node->getDoubleValue();
205 SGBucket buck(lon, lat);
207 // get neigboring buckets
208 int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
209 //cout << "bx = " << bx << endl;
210 int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
211 //cout << "by = " << by << endl;
213 // loop over bucket range
214 for ( int i=-bx; i<=bx; i++) {
215 //cout << "i loop\n";
216 for ( int j=-by; j<=by; j++) {
217 //cout << "j loop\n";
218 buck = sgBucketOffset(lon, lat, i, j);
219 long int bucket = buck.gen_index();
220 //cout << "bucket is " << bucket << endl;
221 if(airports.find(bucket) != airports.end()) {
222 aptID_list_type* apts = airports[bucket];
223 aptID_list_iterator current = apts->begin();
224 aptID_list_iterator last = apts->end();
226 //cout << "Size of apts is " << apts->size() << endl;
228 //double rlon = lon * SGD_DEGREES_TO_RADIANS;
229 //double rlat = lat * SGD_DEGREES_TO_RADIANS;
230 //Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
232 for(; current != last; ++current) {
233 //cout << "Found " << *current << endl;;
234 if(activated.find(*current) == activated.end()) {
235 //cout << "Activating " << *current << endl;
237 //if(dclFindAirportID(*current, &a)) {
238 // // We can do something here based on distance from the user if we wish.
240 ActivateAirport(*current);
241 //cout << "Activation done" << endl;
243 //cout << *current << " already activated" << endl;