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