]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIMgr.cxx
Roy Vegard Ovesen:
[flightgear.git] / src / ATC / AIMgr.cxx
1 // AIMgr.cxx - implementation of FGAIMgr 
2 // - a global management class for FlightGear generated AI traffic
3 //
4 // Written by David Luff, started March 2002.
5 //
6 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
7 //
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.
12 //
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.
17 //
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.
21
22 #include <simgear/misc/sg_path.hxx>
23
24 #include <Main/fg_props.hxx>
25 #include <Main/globals.hxx>
26 #include <simgear/math/sg_random.h>
27
28 #include <list>
29
30 #ifdef _MSC_VER
31 #  include <io.h>
32 #else
33 #  include <sys/types.h>        // for directory reading
34 #  include <dirent.h>           // for directory reading
35 #endif
36
37 #ifdef FG_WEATHERCM
38 # include <WeatherCM/FGLocalWeatherDatabase.h>
39 #else
40 # include <Environment/environment_mgr.hxx>
41 # include <Environment/environment.hxx>
42 #endif
43
44 #include "AIMgr.hxx"
45 #include "AILocalTraffic.hxx"
46 #include "AIGAVFRTraffic.hxx"
47 #include "ATCutils.hxx"
48 #include "commlist.hxx"
49
50 SG_USING_STD(list);
51 SG_USING_STD(cout);
52
53 FGAIMgr::FGAIMgr() {
54         ATC = globals->get_ATC_mgr();
55         initDone = false;
56         ai_callsigns_used["CFGFS"] = 1; // so we don't inadvertently use this
57         // TODO - use the proper user callsign when it becomes user settable.
58         removalList.clear();
59         activated.clear();
60 }
61
62 FGAIMgr::~FGAIMgr() {
63 }
64
65 void FGAIMgr::init() {
66         //cout << "AIMgr::init called..." << endl;
67         
68         // Pointers to user's position
69         lon_node = fgGetNode("/position/longitude-deg", true);
70         lat_node = fgGetNode("/position/latitude-deg", true);
71         elev_node = fgGetNode("/position/altitude-ft", true);
72
73         lon = lon_node->getDoubleValue();
74         lat = lat_node->getDoubleValue();
75         elev = elev_node->getDoubleValue();
76         
77         // Load up models at the start to avoid pausing later
78         // Hack alert - Hardwired paths!!
79         string planepath = "Aircraft/c172/Models/c172-dpm.ac";
80         _defaultModel = sgLoad3DModel( globals->get_fg_root(),
81                                           planepath.c_str(),
82                                           globals->get_props(),
83                                           globals->get_sim_time_sec() );
84                                                                           
85         planepath = "Aircraft/pa28-161/Models/pa28-161.ac";
86         _piperModel = sgLoad3DModel( globals->get_fg_root(),
87                                           planepath.c_str(),
88                                           globals->get_props(),
89                                           globals->get_sim_time_sec() );
90                                                                           
91         // go through the $FG_ROOT/ATC directory and find all *.taxi files
92         SGPath path(globals->get_fg_root());
93         path.append("ATC/");
94         string dir = path.dir();
95         string ext;
96         string file, f_ident;
97         int pos;
98         
99         // WARNING - I (DCL) haven't tested this on MSVC - this is simply cribbed from TerraGear
100 #ifdef _MSC_VER 
101         long hfile;
102         struct _finddata_t de;
103         string path_str;
104         
105         path_str = dir + "\\*.*";
106         
107         if ( ( hfile = _findfirst( path.c_str(), &de ) ) == -1 ) {
108                 SG_LOG(SG_ATC, SG_WARN, "cannot open directory " << dir);
109         } else {                
110                 // load all .taxi files
111                 do {
112                         file = de.name;
113                         pos = file.find(".");
114                         ext = file.substr(pos + 1);
115                         if(ext == "taxi") {
116                                 f_ident = file.substr(0, pos);
117                                 FGAirport a;
118                                 if(dclFindAirportID(f_ident, &a)) {
119                                         SGBucket sgb(a.longitude, a.latitude);
120                                         int idx = sgb.gen_index();
121                                         if(facilities.find(idx) != facilities.end()) {
122                                                 facilities[idx]->push_back(f_ident);
123                                         } else {
124                                                 ID_list_type* apts = new ID_list_type;
125                                                 apts->push_back(f_ident);
126                                                 facilities[idx] = apts;
127                                         }
128                                         SG_LOG(SG_ATC, SG_BULK, "Mapping " << f_ident << " to bucket " << idx); 
129                                 }
130                         }
131                 } while ( _findnext( hfile, &de ) == 0 );
132         }
133 #else
134
135     DIR *d;
136     struct dirent *de;
137
138     if ( (d = opendir( dir.c_str() )) == NULL ) {
139                 SG_LOG(SG_ATC, SG_WARN, "cannot open directory " << dir);
140         } else {
141                 // load all .taxi files
142                 while ( (de = readdir(d)) != NULL ) {
143                         file = de->d_name;
144                         pos = file.find(".");
145                         ext = file.substr(pos + 1);
146                         if(ext == "taxi") {
147                                 f_ident = file.substr(0, pos);
148                                 FGAirport a;
149                                 if(dclFindAirportID(f_ident, &a)) {
150                                         SGBucket sgb(a.longitude, a.latitude);
151                                         int idx = sgb.gen_index();
152                                         if(facilities.find(idx) != facilities.end()) {
153                                                 facilities[idx]->push_back(f_ident);
154                                         } else {
155                                                 ID_list_type* apts = new ID_list_type;
156                                                 apts->push_back(f_ident);
157                                                 facilities[idx] = apts;
158                                         }
159                                         SG_LOG(SG_ATC, SG_BULK, "Mapping " << f_ident << " to bucket " << idx);
160                                 }
161                         }
162                 }               
163                 closedir(d);
164         }
165 #endif
166         
167         // See if are in range at startup and activate if necessary
168         SearchByPos(15.0);
169         
170         initDone = true;
171         
172         //cout << "AIMgr::init done..." << endl;
173         
174         /*
175         // TESTING
176         FGATCAlignedProjection ortho;
177         ortho.Init(dclGetAirportPos("KEMT"), 205.0);    // Guess of rwy19 heading
178         //Point3D ip = ortho.ConvertFromLocal(Point3D(6000, 1000, 1000));       // 90 deg entry
179         //Point3D ip = ortho.ConvertFromLocal(Point3D(-7000, 3000, 1000));      // 45 deg entry
180         Point3D ip = ortho.ConvertFromLocal(Point3D(1000, -7000, 1000));        // straight-in
181         ATC->AIRegisterAirport("KEMT");
182         FGAIGAVFRTraffic* p = new FGAIGAVFRTraffic();
183         p->SetModel(_defaultModel);
184         p->Init(ip, "KEMT", GenerateShortForm(GenerateUniqueCallsign()));
185         ai_list.push_back(p);
186         traffic[ident].push_back(p);
187         activated["KEMT"] = 1;
188         */      
189 }
190
191 void FGAIMgr::bind() {
192 }
193
194 void FGAIMgr::unbind() {
195 }
196
197 void FGAIMgr::update(double dt) {
198         if(!initDone) {
199                 init();
200                 SG_LOG(SG_ATC, SG_WARN, "Warning - AIMgr::update(...) called before AIMgr::init()");
201         }
202         
203         //cout << activated.size() << '\n';
204         
205         Point3D userPos = Point3D(lon_node->getDoubleValue(), lat_node->getDoubleValue(), elev_node->getDoubleValue());
206         
207         // TODO - make these class variables!!
208         static int i = 0;
209         static int j = 0;
210
211         // Don't update any planes for first 50 runs through - this avoids some possible initialisation anomalies
212         // Might not need it now we have fade-in though?
213         if(i < 50) {
214                 ++i;
215                 return;
216         }
217         
218         if(j == 215) {
219                 SearchByPos(25.0);
220                 j = 0;
221         } else if(j == 200) {
222                 // Go through the list of activated airports and remove those out of range
223                 //cout << "The following airports have been activated by the AI system:\n";
224                 ai_activated_map_iterator apt_itr = activated.begin();
225                 while(apt_itr != activated.end()) {
226                         //cout << "FIRST IS " << (*apt_itr).first << '\n';
227                         if(dclGetHorizontalSeparation(userPos, dclGetAirportPos((*apt_itr).first)) > (35.0 * 1600.0)) {
228                                 // Then get rid of it and make sure the iterator is left pointing to the next one!
229                                 string s = (*apt_itr).first;
230                                 if(traffic.find(s) != traffic.end()) {
231                                         //cout << "s = " << s << ", traffic[s].size() = " << traffic[s].size() << '\n';
232                                         if(traffic[s].size()) {
233                                                 apt_itr++;
234                                         } else {
235                                                 //cout << "Erasing " << (*apt_itr).first << " and traffic" << '\n';
236                                                 activated.erase(apt_itr++);
237                                                 traffic.erase(s);
238                                         }
239                                 } else {
240                                                 //cout << "Erasing " << (*apt_itr).first << ' ' << (*apt_itr).second << '\n';
241                                                 activated.erase(apt_itr++);
242                                 }
243                         } else {
244                                 apt_itr++;
245                         }
246                 }
247         } else if(j == 180) {
248                 // Go through the list of activated airports and do the random airplane generation
249                 ai_traffic_map_iterator it = traffic.begin();
250                 while(it != traffic.end()) {
251                         string s = (*it).first;
252                         //cout << "s = " << s << " size = " << (*it).second.size() << '\n';
253                         // Only generate extra traffic if within a certain distance of the user,
254                         // TODO - maybe take users's tuned freq into account as well.
255                         double d = dclGetHorizontalSeparation(userPos, dclGetAirportPos(s)); 
256                         if(d < (15.0 * 1600.0)) {
257                                 double cd = 0.0;
258                                 bool gen = false;
259                                 //cout << "Size of list is " << (*it).second.size() << " at " << s << '\n';
260                                 if((*it).second.size()) {
261                                         FGAIEntity* e = *((*it).second.rbegin());
262                                         cd = dclGetHorizontalSeparation(e->GetPos(), dclGetAirportPos(s));
263                                         if(cd < (d < 5000 ? 10000 : d + 5000)) {
264                                                 gen = true;
265                                         }
266                                 } else {
267                                         gen = true;
268                                         cd = 0.0;
269                                 }
270                                 if(gen) {
271                                         //cout << "Generating extra traffic at airport " << s << ", at least " << cd << " meters out\n";
272                                         //GenerateSimpleAirportTraffic(s, cd);
273                                         GenerateSimpleAirportTraffic(s, cd + 2000.0);   // The random seems a bit wierd - traffic could get far too bunched without the +2000.
274                                 }
275                         }
276                         ++it;
277                 }
278         }
279         
280         ++j;
281         
282         //cout << "Size of AI list is " << ai_list.size() << '\n';
283         
284         // TODO - need to add a check of if any activated airports have gone out of range
285         
286         string rs;      // plane to be removed, if one.
287         if(removalList.size()) {
288                 rs = *(removalList.begin());
289                 removalList.pop_front();
290         } else {
291                 rs = "";
292         }
293         
294         // Traverse the list of active planes and run all their update methods
295         // TODO - spread the load - not all planes should need updating every frame.
296         // Note that this will require dt to be calculated for each plane though
297         // since they rely on it to calculate distance travelled.
298         ai_list_itr = ai_list.begin();
299         while(ai_list_itr != ai_list.end()) {
300                 FGAIEntity *e = *ai_list_itr;
301                 if(rs.size() && e->GetCallsign() == rs) {
302                         //cout << "Removing " << rs << " from ai_list\n";
303                         ai_list_itr = ai_list.erase(ai_list_itr);
304                         delete e;
305                         // This is a hack - we should deref this plane from the airport count!
306                 } else {
307                         e->Update(dt);
308                         ++ai_list_itr;
309                 }
310         }
311
312         //cout << "Size of AI list is " << ai_list.size() << '\n';
313 }
314
315 void FGAIMgr::ScheduleRemoval(string s) {
316         //cout << "Scheduling removal of plane " << s << " from AIMgr\n";
317         removalList.push_back(s);
318 }
319
320 // Activate AI traffic at an airport
321 void FGAIMgr::ActivateAirport(string ident) {
322         ATC->AIRegisterAirport(ident);
323         // TODO - need to start the traffic more randomly
324         FGAILocalTraffic* local_traffic = new FGAILocalTraffic;
325         local_traffic->SetModel(_defaultModel); // currently hardwired to cessna.
326         //local_traffic->Init(ident, IN_PATTERN, TAKEOFF_ROLL);
327         local_traffic->Init(GenerateShortForm(GenerateUniqueCallsign()), ident);
328         local_traffic->FlyCircuits(1, true);    // Fly 2 circuits with touch & go in between
329         ai_list.push_back(local_traffic);
330         traffic[ident].push_back(local_traffic);
331         //cout << "******** ACTIVATING AIRPORT, ident = " << ident << '\n';
332         activated[ident] = 1;
333 }
334
335 // Hack - Generate AI traffic at an airport with no facilities file
336 void FGAIMgr::GenerateSimpleAirportTraffic(string ident, double min_dist) {
337         // Ugly hack - don't let VFR Cessnas operate at a hardwired list of major airports
338         // This will go eventually once airport .xml files specify the traffic profile
339         if(ident == "KSFO" || ident == "KDFW" || ident == "EGLL" || ident == "KORD" || ident == "KJFK" 
340                            || ident == "KMSP" || ident == "KLAX" || ident == "KBOS" || ident == "KEDW"
341                                            || ident == "KSEA" || ident == "EHAM") {
342                 return;
343         }
344         
345         /*
346         // TODO - check for military airports - this should be in the current data.
347         // UGGH - there's no point at the moment - everything is labelled civil in basic.dat!
348         FGAirport a;
349         if(dclFindAirportID(ident, &a)) {
350                 cout << "CODE IS " << a.code << '\n';
351         } else {
352                 // UG - can't find the airport!
353                 return;
354         }
355         */
356         
357         Point3D aptpos = dclGetAirportPos(ident);       // TODO - check for elev of -9999
358         //cout << "ident = " << ident << ", elev = " << aptpos.elev() << '\n';
359         
360         // Operate from airports at 3000ft and below only to avoid the default cloud layers and since we don't degrade AI performance with altitude.
361         if(aptpos.elev() > 3000) {
362                 //cout << "High alt airports not yet supported - returning\n";
363                 return;
364         }
365         
366         // Rough hack for plane type - make 70% of the planes cessnas, the rest pipers.
367         bool cessna = true;
368         
369         // Get the time and only operate VFR in the (approximate) daytime.
370         //SGTime *t = globals->get_time_params();
371         string time_str = fgGetString("sim/time/gmt-string");
372         int loc_time = atoi((time_str.substr(0,3)).c_str());
373         //cout << "gmt_time = " << loc_time << '\n';
374         loc_time += (int)((aptpos.lon() / 360.0) * 24.0);
375         while(loc_time < 0) loc_time += 24;
376         while(loc_time > 24) loc_time -= 24;
377         //cout << "loc_time = " << loc_time << '\n';
378         if(loc_time < 7 || loc_time > 19) return;
379         
380         // Check that the visibility is OK for IFR operation.
381         double visibility;
382         #ifdef FG_WEATHERCM
383         //sgVec3 position = { aptpos.lat(), aptpos.lon(), aptpos.elev() };
384         //FGPhysicalProperty stationweather = WeatherDatabase->get(position);
385         #else
386         FGEnvironment stationweather =
387             ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
388               ->getEnvironment(aptpos.lat(), aptpos.lon(), aptpos.elev());      // TODO - check whether this should take ft or m for elev.
389         #endif
390         #ifdef FG_WEATHERCM
391         visibility = fgGetDouble("/environment/visibility-m");
392         #else
393         visibility = stationweather.get_visibility_m();
394         #endif
395         // Technically we can do VFR down to 1 mile (1600m) but that's pretty murky!
396         //cout << "vis = " << visibility << '\n';
397         if(visibility < 3000) return;
398         
399         ATC->AIRegisterAirport(ident);
400         
401         // Next - get the distance from user to the airport.
402         Point3D userpos = Point3D(lon_node->getDoubleValue(), lat_node->getDoubleValue(), elev_node->getDoubleValue());
403         double d = dclGetHorizontalSeparation(userpos, aptpos); // in meters
404         
405         int lev = fgGetInt("/sim/ai-traffic/level");
406         if(lev < 1 || lev > 3) lev = 2;
407         if(visibility < 6000) lev = 1;
408         //cout << "level = " << lev << '\n';
409         
410         // Next - generate any local / circuit traffic
411
412         /*
413         // --------------------------- THIS BLOCK IS JUST FOR TESTING - COMMENT OUT BEFORE RELEASE ---------------
414         // Finally - generate VFR approaching traffic
415         //if(d > 2000) {
416         if(ident == "KPOC") {
417                 double ad = 2000.0;
418                 double avd = 3000.0;    // average spacing of arriving traffic in meters - relate to airport business and AI density setting one day!
419                 //while(ad < (d < 10000 ? 12000 : d + 2000)) {
420                 for(int i=0; i<8; ++i) {
421                         double dd = sg_random() * avd;
422                         // put a minimum spacing in for now since I don't think tower will cope otherwise!
423                         if(dd < 1500) dd = 1500; 
424                         //ad += dd;
425                         ad += dd;
426                         double dir = int(sg_random() * 36);
427                         if(dir == 36) dir--;
428                         dir *= 10;
429                         //dir = 180;
430                         if(sg_random() < 0.3) cessna = false;
431                         else cessna = true;
432                         string s = GenerateShortForm(GenerateUniqueCallsign(), (cessna ? "Cessna-" : "Piper-"));
433                         FGAIGAVFRTraffic* t = new FGAIGAVFRTraffic();
434                         t->SetModel(cessna ? _defaultModel : _piperModel);
435                         //cout << "Generating VFR traffic " << s << " inbound to " << ident << " " << ad << " meters out from " << dir << " degrees\n";
436                         Point3D tpos = dclUpdatePosition(aptpos, dir, 6.0, ad);
437                         if(tpos.elev() > (aptpos.elev() + 3000.0)) tpos.setelev(aptpos.elev() + 3000.0);
438                         t->Init(tpos, ident, s);
439                         ai_list.push_back(t);
440                 }
441         }
442         activated[ident] = 1;
443         return;
444         //---------------------------------------------------------------------------------------------------
445         */
446         
447         double ad;   // Minimum distance out of first arriving plane in meters.
448         double mind; // Minimum spacing of traffic in meters
449         double avd;  // average spacing of arriving traffic in meters - relate to airport business and AI density setting one day!
450         // Finally - generate VFR approaching traffic
451         //if(d > 2000) {
452         if(1) {
453                 if(lev == 3) {
454                         ad = 5000.0;
455                         mind = 2000.0;
456                         avd = 6000.0;
457                 } else if(lev == 2) {
458                         ad = 8000.0;
459                         mind = 4000.0;
460                         avd = 10000.0;
461                 } else {
462                         ad = 9000.0;    // Start the first aircraft at least 9K out for now.
463                         mind = 6000.0;
464                         avd = 15000.0;
465                 }
466                 /*
467                 // Check if there is already arriving traffic at this airport
468                 cout << "BING A " << ident << '\n';
469                 if(traffic.find(ident) != traffic.end()) {
470                         cout << "BING B " << ident << '\n';
471                         ai_list_type lst = traffic[ident];
472                         cout << "BING C " << ident << '\n';
473                         if(lst.size()) {
474                                 cout << "BING D " << ident << '\n';
475                                 double cd = dclGetHorizontalSeparation(aptpos, (*lst.rbegin())->GetPos());
476                                 cout << "ident = " << ident << ", cd = " << cd << '\n';
477                                 if(cd > ad) ad = cd;
478                         }
479                 }
480                 */
481                 if(min_dist != 0) ad = min_dist;
482                 //cout << "ident = " << ident << ", ad = " << ad << '\n';
483                 while(ad < (d < 5000 ? 15000 : d + 10000)) {
484                         double dd = mind + (sg_random() * (avd - mind));
485                         ad += dd;
486                         double dir = int(sg_random() * 36);
487                         if(dir == 36) dir--;
488                         dir *= 10;
489                         if(sg_random() < 0.3) cessna = false;
490                         else cessna = true;
491                         string s = GenerateShortForm(GenerateUniqueCallsign(), (cessna ? "Cessna-" : "Piper-"));
492                         FGAIGAVFRTraffic* t = new FGAIGAVFRTraffic();
493                         t->SetModel(cessna ? _defaultModel : _piperModel);
494                         //cout << "Generating VFR traffic " << s << " inbound to " << ident << " " << ad << " meters out from " << dir << " degrees\n";
495                         Point3D tpos = dclUpdatePosition(aptpos, dir, 6.0, ad);
496                         if(tpos.elev() > (aptpos.elev() + 3000.0)) tpos.setelev(aptpos.elev() + 3000.0);        // FEET yuk :-(
497                         t->Init(tpos, ident, s);
498                         ai_list.push_back(t);
499                         traffic[ident].push_back(t);
500                 }
501         }       
502 }
503
504 /*
505 // Generate a VFR arrival at airport apt, at least distance d (meters) out.
506 void FGAIMgr::GenerateVFRArrival(string apt, double d) {
507 }
508 */
509
510 // Search for valid airports in the vicinity of the user and activate them if necessary
511 void FGAIMgr::SearchByPos(double range) {
512         //cout << "In SearchByPos(...)" << endl;
513         
514         // get bucket number for plane position
515         lon = lon_node->getDoubleValue();
516         lat = lat_node->getDoubleValue();
517         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
518         SGBucket buck(lon, lat);
519
520         // get neigboring buckets
521         int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
522         //cout << "bx = " << bx << endl;
523         int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
524         //cout << "by = " << by << endl;
525         
526         // Search for airports with facitities files --------------------------
527         // loop over bucket range 
528         for ( int i=-bx; i<=bx; i++) {
529                 //cout << "i loop\n";
530                 for ( int j=-by; j<=by; j++) {
531                         //cout << "j loop\n";
532                         buck = sgBucketOffset(lon, lat, i, j);
533                         long int bucket = buck.gen_index();
534                         //cout << "bucket is " << bucket << endl;
535                         if(facilities.find(bucket) != facilities.end()) {
536                                 ID_list_type* apts = facilities[bucket];
537                                 ID_list_iterator current = apts->begin();
538                                 ID_list_iterator last = apts->end();
539                                 
540                                 //cout << "Size of apts is " << apts->size() << endl;
541                                 
542                                 //double rlon = lon * SGD_DEGREES_TO_RADIANS;
543                                 //double rlat = lat * SGD_DEGREES_TO_RADIANS;
544                                 //Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
545                                 //Point3D airport;
546                                 for(; current != last; ++current) {
547                                         //cout << "Found " << *current << endl;;
548                                         if(activated.find(*current) == activated.end()) {
549                                                 //cout << "Activating " << *current << endl;
550                                                 //FGAirport a;
551                                                 //if(dclFindAirportID(*current, &a)) {
552                                                         //      // We can do something here based on distance from the user if we wish.
553                                                 //}
554                                                 //string s = *current;
555                                                 //cout << "s = " << s << '\n';
556                                                 ActivateAirport(*current);
557                                                 //ActivateSimpleAirport(*current);      // TODO - put this back to ActivateAirport when that code is done.
558                                                 //cout << "Activation done" << endl;
559                                         } else {
560                                                 //cout << *current << " already activated" << endl;
561                                         }
562                                 }
563                         }
564                 }
565         }
566         //-------------------------------------------------------------
567         
568         // Search for any towered airports in the vicinity ------------
569         comm_list_type towered;
570         comm_list_iterator twd_itr;
571         
572         int num_twd = current_commlist->FindByPos(lon, lat, elev, range, &towered, TOWER);
573         if (num_twd != 0) {
574                 double closest = 1000000;
575                 string s = "";
576                 for(twd_itr = towered.begin(); twd_itr != towered.end(); twd_itr++) {
577                         // Only activate the closest airport not already activated each time.
578                         if(activated.find(twd_itr->ident) == activated.end()) {
579                                 double sep = dclGetHorizontalSeparation(Point3D(lon, lat, elev), dclGetAirportPos(twd_itr->ident));
580                                 if(sep < closest) {
581                                         closest = sep;
582                                         s = twd_itr->ident;
583                                 }
584                                 
585                         }
586                 }
587                 if(s.size()) {
588                         // TODO - find out why empty strings come through here when all in-range airports done.
589                         GenerateSimpleAirportTraffic(s);
590                         //cout << "**************ACTIVATING SIMPLE AIRPORT, ident = " << s << '\n';
591                         activated[s] = 1;
592                 }
593         }
594 }
595
596 string FGAIMgr::GenerateCallsign() {
597         // For now we'll just generate US callsigns until we can regionally identify airports.
598         string s = "N";
599         // Add 3 to 5 numbers and make up to 5 with letters.
600         //sg_srandom_time();
601         double d = sg_random();
602         int n = int(d * 3);
603         if(n == 3) --n;
604         //cout << "First n, n = " << n << '\n';
605         int j = 3 + n;
606         //cout << "j = " << j << '\n';
607         for(int i=0; i<j; ++i) { 
608                 int n = int(sg_random() * 10);
609                 if(n == 10) --n;
610                 s += (char)('0' + n);
611         }
612         for(int i=j; i<5; ++i) {
613                 int n = int(sg_random() * 26);
614                 if(n == 26) --n;
615                 //cout << "Alpha, n = " << n << '\n';
616                 s += (char)('A' + n);
617         }
618         //cout << "s = " << s << '\n';
619         return(s);
620 }
621
622 string FGAIMgr::GenerateUniqueCallsign() {
623         while(1) {
624                 string s = GenerateCallsign();
625                 if(!ai_callsigns_used[s]) {
626                         ai_callsigns_used[s] = 1;
627                         return(s);
628                 }
629         }
630 }
631
632 // This will be moved somewhere else eventually!!!!
633 string FGAIMgr::GenerateShortForm(string callsign, string plane_str, bool local) {
634         //cout << callsign << '\n';
635         string s;
636         if(local) s = "Trainer-";
637         else s = plane_str;
638         for(int i=3; i>0; --i) {
639                 char c = callsign[callsign.size() - i];
640                 //cout << c << '\n';
641                 string tmp = "";
642                 tmp += c;
643                 if(isalpha(c)) s += GetPhoneticIdent(c);
644                 else s += ConvertNumToSpokenDigits(tmp);
645                 if(i > 1) s += '-';
646         }
647         return(s);
648 }