]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ground.cxx
Log to SG_ATC instead of SG_GENERAL and cout
[flightgear.git] / src / ATC / ground.cxx
1 // FGGround - a class to provide ground control at larger airports.
2 //
3 // Written by David Luff, started March 2002.
4 //
5 // Copyright (C) 2002  David C. Luff - david.luff@nottingham.ac.uk
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 <simgear/math/sg_random.h>
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/misc/sgstream.hxx>
25 #include <simgear/constants.h>
26 #include <Main/globals.hxx>
27
28 #include <stdlib.h>
29 #include STL_FSTREAM
30
31 #include "ground.hxx"
32 #include "ATCmgr.hxx"
33 #include "ATCutils.hxx"
34 #include "ATCdisplay.hxx"
35 #include "AILocalTraffic.hxx"
36
37 SG_USING_STD(ifstream);
38 SG_USING_STD(cout);
39
40 node::node() {
41 }
42
43 node::~node() {
44         for(unsigned int i=0; i < arcs.size(); ++i) {
45                 delete arcs[i];
46         }
47 }
48
49 // Make sure that a_path.cost += distance is safe from the moment it's created.
50 a_path::a_path() {
51         cost = 0;
52 }
53
54 FGGround::FGGround() {
55         display = false;
56         networkLoadOK = false;
57         ground_traffic.erase(ground_traffic.begin(), ground_traffic.end());
58         ground_traffic_itr = ground_traffic.begin();
59 }
60
61 FGGround::FGGround(string id) {
62         display = false;
63         networkLoadOK = false;
64         ground_traffic.erase(ground_traffic.begin(), ground_traffic.end());
65         ground_traffic_itr = ground_traffic.begin();
66         ident = id;
67 }
68
69 FGGround::~FGGround() {
70 }
71
72 void FGGround::ParseRwyExits(node* np, char* es) {
73         char* token;
74         char estr[20];
75         strcpy(estr, es);
76         const char delimiters[] = "-";
77         token = strtok(estr, delimiters);
78         while(token != NULL) {
79                 int i = atoi(token);
80                 //cout << "token = " << token << endl;
81                 //cout << "rwy number = " << i << endl;
82                 //runways[(atoi(token))].exits.push_back(np);
83                 runways[i].exits.push_back(np);
84                 //cout << "token = " << token << '\n';
85                 token = strtok(NULL, delimiters);
86         }
87 }
88         
89
90 // Load the ground logical network of the current instances airport
91 // Return true if successfull.
92 // TODO - currently the file is assumed to reside in the base/ATC directory.
93 // This might change to something more thought out in the future.
94 // NOTE - currently it is assumed that all nodes are loaded before any arcs.
95 // It won't work ATM if this doesn't hold true.
96 bool FGGround::LoadNetwork() {
97         node* np;
98         arc* ap;
99         Gate* gp;
100         
101         int gateCount = 0;      // This is used to allocate gateID's from zero upwards
102         // This may well change in the future - probably to reading in the real-world
103         // gate numbers from file.
104         
105         ifstream fin;
106         SGPath path = globals->get_fg_root();
107         //string taxiPath = "ATC/" + ident + ".taxi";
108         string taxiPath = "ATC/KEMT.taxi";      // FIXME - HARDWIRED FOR TESTING
109         path.append(taxiPath);
110         
111         SG_LOG(SG_ATC, SG_INFO, "Trying to read taxiway data for " << ident << "...");
112         //cout << "Trying to read taxiway data for " << ident << "..." << endl;
113         fin.open(path.c_str(), ios::in);
114         if(!fin) {
115                 SG_LOG(SG_ATC, SG_ALERT, "Unable to open taxiway data input file " << path.c_str());
116                 //cout << "Unable to open taxiway data input file " << path.c_str() << endl;
117                 return(false);
118         }
119         
120         char ch;
121         char buf[30];
122         while(!fin.eof()) {
123                 fin >> buf;
124                 // Node, arc, or [End]?
125                 //cout << "Read in ground network element type = " << buf << endl;
126                 if(!strcmp(buf, "[End]")) {             // TODO - maybe make this more robust to spelling errors by just looking for '['
127                         SG_LOG(SG_ATC, SG_INFO, "Done reading " << path.c_str() << endl);
128                         break;
129                 } else if(!strcmp(buf, "N")) {
130                         // Node
131                         np = new node;
132                         np->struct_type = NODE;
133                         fin >> buf;
134                         np->nodeID = atoi(buf);
135                         fin >> buf;
136                         np->pos.setlon(atof(buf));
137                         fin >> buf;
138                         np->pos.setlat(atof(buf));
139                         fin >> buf;
140                         np->pos.setelev(atof(buf));
141                         fin >> buf;             // node type
142                         if(!strcmp(buf, "J")) {
143                                 np->type = JUNCTION;
144                         } else if(!strcmp(buf, "T")) {
145                                 np->type = TJUNCTION;
146                         } else if(!strcmp(buf, "H")) {
147                                 np->type = HOLD;
148                         } else {
149                                 SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown node type in taxi network...\n");
150                                 delete np;
151                                 return(false);
152                         }
153                         fin >> buf;             // rwy exit information - gets parsed later - FRAGILE - will break if buf is reused.
154                         // Now the name
155                         fin >> ch;      // strip the leading " off
156                         np->name = "";
157                         while(1) {
158                                 fin.unsetf(ios::skipws);
159                                 fin >> ch;
160                                 if((ch == '"') || (ch == 0x0A)) {
161                                         break;
162                                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
163                                 np->name += ch;
164                         }
165                         fin.setf(ios::skipws);
166                         network.push_back(np);
167                         // FIXME - fragile - replies on buf not getting modified from exits read to here
168                         // see if we also need to push it onto the runway exit list
169                         //cout << "strlen(buf) = " << strlen(buf) << endl;
170                         if(strlen(buf) > 2) {
171                                 //cout << "Calling ParseRwyExits for " << buf << endl;
172                                 ParseRwyExits(np, buf);
173                         }
174                 } else if(!strcmp(buf, "A")) {
175                         ap = new arc;
176                         ap->struct_type = ARC;
177                         fin >> buf;
178                         ap->n1 = atoi(buf);
179                         fin >> buf;
180                         ap->n2 = atoi(buf);
181                         fin >> buf;
182                         if(!strcmp(buf, "R")) {
183                                 ap->type = RUNWAY;
184                         } else if(!strcmp(buf, "T")) {
185                                 ap->type = TAXIWAY;
186                         } else {
187                                 SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown arc type in taxi network...\n");
188                                 delete ap;
189                                 return(false);
190                         }
191                         // directed?
192                         fin >> buf;
193                         if(!strcmp(buf, "Y")) {
194                                 ap->directed = true;
195                         } else if(!strcmp(buf, "N")) {
196                                 ap->directed = false;
197                         } else {
198                                 SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown arc directed value in taxi network - should be Y/N !!!\n");
199                                 delete ap;
200                                 return(false);
201                         }                       
202                         // Now the name
203                         ap->name = "";
204                         while(1) {
205                                 fin.unsetf(ios::skipws);
206                                 fin >> ch;
207                                 ap->name += ch;
208                                 if((ch == '"') || (ch == 0x0A)) {
209                                         break;
210                                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
211                         }
212                         fin.setf(ios::skipws);
213                         ap->distance = (int)dclGetHorizontalSeparation(network[ap->n1]->pos, network[ap->n2]->pos);
214                         //cout << "Distance  = " << ap->distance << '\n';
215                         network[ap->n1]->arcs.push_back(ap);
216                         network[ap->n2]->arcs.push_back(ap);
217                 } else if(!strcmp(buf, "G")) {
218                         gp = new Gate;
219                         gp->struct_type = NODE;
220                         gp->type = GATE;
221                         fin >> buf;
222                         gp->nodeID = atoi(buf);
223                         fin >> buf;
224                         gp->pos.setlon(atof(buf));
225                         fin >> buf;
226                         gp->pos.setlat(atof(buf));
227                         fin >> buf;
228                         gp->pos.setelev(atof(buf));
229                         fin >> buf;             // gate type - ignore this for now
230                         fin >> buf;             // gate heading
231                         gp->heading = atoi(buf);
232                         // Now the name
233                         gp->name = "";
234                         while(1) {
235                                 fin.unsetf(ios::skipws);
236                                 fin >> ch;
237                                 gp->name += ch;
238                                 if((ch == '"') || (ch == 0x0A)) {
239                                         break;
240                                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
241                         }
242                         fin.setf(ios::skipws);
243                         gp->id = gateCount;             // Warning - this will likely change in the future.
244                         gp->used = false;
245                         network.push_back(gp);
246                         gates[gateCount] = gp;
247                         gateCount++;
248                 } else {
249                         // Something has gone seriously pear-shaped
250                         SG_LOG(SG_ATC, SG_ALERT, "********* ERROR - unknown ground network element type... aborting read of " << path.c_str() << '\n');
251                         return(false);
252                 }
253                 
254                 fin >> skipeol;         
255         }
256         return(true);
257 }
258
259 void FGGround::Init() {
260         display = false;
261         
262         //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
263         // For now we'll hardwire the threshold end FIXME FIXME FIXME - use actual active rwy
264         Point3D P010(-118.037483, 34.081358, 296 * SG_FEET_TO_METER);
265         double hdg = 25.32;
266         ortho.Init(P010, hdg);
267         // FIXME TODO FIXME TODO
268         // TODO FIXME TODO FIXME
269         //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
270         
271         networkLoadOK = LoadNetwork();
272 }
273
274 void FGGround::Update(double dt) {
275         // Each time step, what do we need to do?
276         // We need to go through the list of outstanding requests and acknowedgements
277         // and process at least one of them.
278         // We need to go through the list of planes under our control and check if
279         // any need to be addressed.
280         // We need to check for planes not under our control coming within our 
281         // control area and address if necessary.
282         
283         // Lets take the example of a plane which has just contacted ground
284         // following landing - presumably requesting where to go?
285         // First we need to establish the position of the plane within the logical network.
286         // Next we need to decide where its going.
287         
288         if(ground_traffic.size()) {
289                 if(ground_traffic_itr == ground_traffic.end()) {
290                         ground_traffic_itr = ground_traffic.begin();
291                 }
292                 
293                 //Process(*ground_traffic_itr);
294                 GroundRec* g = *ground_traffic_itr;
295                 if(g->taxiRequestOutstanding) {
296                         double responseTime = 10.0;             // seconds - this should get more sophisticated at some point
297                         if(g->clearanceCounter > responseTime) {
298                                 // DO CLEARANCE
299                                 // TODO - move the mechanics of making up the transmission out of the main Update(...) routine.
300                                 string trns = "";
301                                 trns += g->plane.callsign;
302                                 trns += " taxi holding point runway ";  // TODO - add the holding point name
303                                 // eg " taxi holding point G2 runway "
304                                 //trns += "
305                                 if(display) {
306                                         globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
307                                 }
308                                 g->planePtr->RegisterTransmission(1);   // cleared to taxi
309                                 g->clearanceCounter = 0.0;
310                                 g->taxiRequestOutstanding = false;
311                         } else {
312                                 g->clearanceCounter += (dt * ground_traffic.size());
313                         }
314                 } else if(((FGAILocalTraffic*)(g->planePtr))->AtHoldShort()) {          // That's a hack - eventually we should monitor actual position
315                         // HACK ALERT - the automatic cast to AILocalTraffic has to go once we have other sorts working!!!!! FIXME TODO
316                         // NOTE - we don't need to do the contact tower bit unless we have separate tower and ground
317                         string trns = g->plane.callsign;
318                         trns += " contact Tower ";
319                         double f = globals->get_ATC_mgr()->GetFrequency(ident, TOWER);
320                         char buf[10];
321                         sprintf(buf, "%f", f);
322                         trns += buf;
323                         if(display) {
324                                 globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
325                         }
326                         g->planePtr->RegisterTransmission(2);   // contact tower
327                         delete *ground_traffic_itr;
328                         ground_traffic.erase(ground_traffic_itr);
329                         ground_traffic_itr = ground_traffic.begin();
330                 }                               
331                 ++ground_traffic_itr;
332         }
333 }
334
335 // Return a random gate ID of an unused gate.
336 // Two error values may be returned and must be checked for by the calling function:
337 // -2 signifies that no gates exist at this airport.
338 // -1 signifies that all gates are currently full.
339 int FGGround::GetRandomGateID() {
340         // Check that this airport actually has some gates!!
341         if(!gates.size()) {
342                 return(-2);
343         }
344
345         gate_vec_type gateVec;
346         int num = 0;
347         int thenum;
348         int ID;
349         
350         gatesItr = gates.begin();
351         while(gatesItr != gates.end()) {
352                 if((gatesItr->second)->used == false) {
353                         gateVec.push_back(gatesItr->second);
354                         num++;
355                 }
356                 ++gatesItr;
357         }
358
359         // Check that there are some unused gates!
360         if(!gateVec.size()) {
361                 return(-1);
362         }
363         
364         // Randomly select one from the list
365         sg_srandom_time();
366         thenum = (int)(sg_random() * gateVec.size());
367         ID = gateVec[thenum]->id;
368         
369         return(ID);
370 }
371
372 // Return a pointer to an unused gate node
373 Gate* FGGround::GetGateNode() {
374         int id = GetRandomGateID();
375         if(id < 0) {
376                 return(NULL);
377         } else {
378                 return(gates[id]);
379         }
380 }
381
382
383 // WARNING - This is hardwired to my prototype logical network format
384 // and will almost certainly change when Bernie's stuff comes on-line.
385 node* FGGround::GetThresholdNode(string rwyID) {
386         // For now go through all the nodes and parse their names
387         // Maybe in the future we'll map threshold nodes by ID
388         //cout << "Size of network is " << network.size() << '\n';
389         for(unsigned int i=0; i<network.size(); ++i) {
390                 //cout << "Name = " << network[i]->name << '\n';
391                 if(network[i]->name.size()) {
392                         string s = network[i]->name;
393                         // Warning - the next bit is fragile and dependent on my current naming scheme
394                         //cout << "substr = " << s.substr(0,3) << '\n';
395                         //cout << "size of s = " << s.size() << '\n'; 
396                         if(s.substr(0,3) == "rwy") {
397                                 //cout << "subsubstr = " << s.substr(4, s.size() - 4) << '\n';
398                                 if(s.substr(4, s.size() - 4) == rwyID) {
399                                         return network[i];
400                                 }
401                         }
402                 }
403         }
404         return NULL;
405 }
406
407
408 // Get a path from a point on a runway to a gate
409 // TODO !!
410
411 // Get a path from a node to another node
412 // Eventually we will need complex algorithms for this taking other traffic,
413 // shortest path and suitable paths into accout.
414 // For now we'll just call the shortest path algorithm.
415 ground_network_path_type FGGround::GetPath(node* A, node* B) {  
416         return(GetShortestPath(A, B));
417 };
418
419 // Get a path from a node to a runway threshold
420 ground_network_path_type FGGround::GetPath(node* A, string rwyID) {
421         node* b = GetThresholdNode(rwyID);
422         if(b == NULL) {
423                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - unable to find path to runway theshold in ground.cxx\n");
424                 ground_network_path_type emptyPath;
425                 emptyPath.erase(emptyPath.begin(), emptyPath.end());
426                 return(emptyPath);
427         }
428         return GetShortestPath(A, b);
429 }
430
431 // Get a path from a node to a runway hold short point
432 // Bit of a hack this at the moment!
433 ground_network_path_type FGGround::GetPathToHoldShort(node* A, string rwyID) {
434         ground_network_path_type path = GetPath(A, rwyID);
435         path.pop_back();        // That should be the threshold stripped of 
436         path.pop_back();        // and that should be the arc from hold short to threshold
437         // This isn't robust though - TODO - implement properly!
438         return(path);
439 }
440
441 // A shortest path algorithm from memory (ie. I can't find the bl&*dy book again!)
442 // I'm sure there must be enchancements that we can make to this, such as biasing the
443 // order in which the nodes are searched out from in favour of those geographically
444 // closer to the destination.
445 // Note that we are working with the master set of nodes and arcs so we mustn't change
446 // or delete them -  we only delete the paths that we create during the algorithm.
447 ground_network_path_type FGGround::GetShortestPath(node* A, node* B) {
448         a_path* pathPtr;
449         shortest_path_map_type pathMap;
450         node_array_type nodesLeft;
451         
452         // Debugging check
453         int pathsCreated = 0;
454         
455         // Initialise the algorithm
456         nodesLeft.push_back(A);
457         pathPtr = new a_path;
458         pathsCreated++;
459         pathPtr->path.push_back(A);
460         pathPtr->cost = 0;
461         pathMap[A->nodeID] = pathPtr;
462         bool solution_found = false;    // Flag to indicate that at least one candidate path has been found
463         int solution_cost = -1;                 // Cost of current best cost solution.  -1 indicates no solution found yet.
464         a_path solution_path;           
465                                                                                         
466         node* nPtr;     // nPtr is used to point to the node we are currently working with
467         
468         while(nodesLeft.size()) {
469                 //cout << "\n*****nodesLeft*****\n";
470                 //for(unsigned int i=0; i<nodesLeft.size(); ++i) {
471                         //cout << nodesLeft[i]->nodeID << '\n';
472                 //}
473                 //cout << "*******************\n\n";
474                 nPtr = *nodesLeft.begin();              // Thought - definate optimization possibilities here in the choice of which nodes we process first.
475                 nodesLeft.erase(nodesLeft.begin());
476                 //cout << "Walking out from node " << nPtr->nodeID << '\n';
477                 for(unsigned int i=0; i<nPtr->arcs.size(); ++i) {
478                         //cout << "ARC TO " << ((nPtr->arcs[i]->n1 == nPtr->nodeID) ? nPtr->arcs[i]->n2 : nPtr->arcs[i]->n1) << '\n';
479                 }
480                 if((solution_found) && (solution_cost <= pathMap[nPtr->nodeID]->cost)) {
481                         // Do nothing - we've already found a solution and this partial path is already more expensive
482                 } else {
483                         // This path could still be better than the current solution - check it out
484                         for(unsigned int i=0; i<(nPtr->arcs.size()); i++) {
485                                 // Map the new path against the end node, ie. *not* the one we just started with.
486                                 unsigned int end_nodeID = ((nPtr->arcs[i]->n1 == nPtr->nodeID) ? nPtr->arcs[i]->n2 : nPtr->arcs[i]->n1);
487                                 //cout << "end_nodeID = " << end_nodeID << '\n';
488                                 //cout << "pathMap size is " << pathMap.size() << '\n';
489                                 if(end_nodeID == nPtr->nodeID) {
490                                         //cout << "Circular arc!\n";
491                                         // Then its a circular arc - don't bother!!
492                                         //nPtr->arcs.erase(nPtr->arcs.begin() + i);
493                                 } else {
494                                         // see if the end node is already in the map or not
495                                         if(pathMap.find(end_nodeID) == pathMap.end()) {
496                                                 //cout << "Not in the map" << endl;;
497                                                 // Not in the map - easy!
498                                                 pathPtr = new a_path;
499                                                 pathsCreated++;
500                                                 *pathPtr = *pathMap[nPtr->nodeID];      // *copy* the path
501                                                 pathPtr->path.push_back(nPtr->arcs[i]);
502                                                 pathPtr->path.push_back(network[end_nodeID]);
503                                                 pathPtr->cost += nPtr->arcs[i]->distance;
504                                                 pathMap[end_nodeID] = pathPtr;
505                                                 nodesLeft.push_back(network[end_nodeID]);       // By definition this can't be in the list already, or
506                                                 // it would also have been in the map and hence OR'd with this one.
507                                                 if(end_nodeID == B->nodeID) {
508                                                         //cout << "Solution found!!!" << endl;
509                                                         // Since this node wasn't in the map this is by definition the first solution
510                                                         solution_cost = pathPtr->cost;
511                                                         solution_path = *pathPtr;
512                                                         solution_found = true;
513                                                 }
514                                         } else {
515                                                 //cout << "Already in the map" << endl;
516                                                 // In the map - not so easy - need to get rid of an arc from the higher cost one.
517                                                 //cout << "Current cost of node " << end_nodeID << " is " << pathMap[end_nodeID]->cost << endl;
518                                                 int newCost = pathMap[nPtr->nodeID]->cost + nPtr->arcs[i]->distance;
519                                                 //cout << "New cost is of node " << nPtr->nodeID << " is " << newCost << endl;
520                                                 if(newCost >= pathMap[end_nodeID]->cost) {
521                                                         // No need to do anything.
522                                                         //cout << "Not doing anything!" << endl;
523                                                 } else {
524                                                         delete pathMap[end_nodeID];
525                                                         pathsCreated--;
526                                                         
527                                                         pathPtr = new a_path;
528                                                         pathsCreated++;
529                                                         *pathPtr = *pathMap[nPtr->nodeID];      // *copy* the path
530                                                         pathPtr->path.push_back(nPtr->arcs[i]);
531                                                         pathPtr->path.push_back(network[end_nodeID]);
532                                                         pathPtr->cost += nPtr->arcs[i]->distance;
533                                                         pathMap[end_nodeID] = pathPtr;
534                                                         
535                                                         // We need to add this node to the list-to-do again to force a recalculation 
536                                                         // onwards from this node with the new lower cost to node cost.
537                                                         nodesLeft.push_back(network[end_nodeID]);
538                                                         
539                                                         if(end_nodeID == B->nodeID) {
540                                                                 //cout << "Solution found!!!" << endl;
541                                                                 // Need to check if there is a previous better solution
542                                                                 if((solution_cost < 0) || (pathPtr->cost < solution_cost)) {
543                                                                         solution_cost = pathPtr->cost;
544                                                                         solution_path = *pathPtr;
545                                                                         solution_found = true;
546                                                                 }
547                                                         }
548                                                 }
549                                         }
550                                 }
551                         }
552                 }
553         }
554         
555         // delete all the paths before returning
556         shortest_path_map_iterator spItr = pathMap.begin();
557         while(spItr != pathMap.end()) {
558                 if(spItr->second != NULL) {
559                         delete spItr->second;
560                         --pathsCreated;
561                 }
562                 ++spItr;
563         }
564         
565         //cout << "pathsCreated = " << pathsCreated << '\n';
566         if(pathsCreated > 0) {
567                 SG_LOG(SG_ATC, SG_ALERT, "WARNING - Possible memory leak in FGGround::GetShortestPath\n\
568                                                                           Please report to flightgear-devel@flightgear.org\n");
569         }
570         
571         //cout << (solution_found ? "Result: solution found\n" : "Result: no solution found\n");
572         return(solution_path.path);             // TODO - we really ought to have a fallback position incase a solution isn't found.
573 }
574                 
575
576
577 // Randomly or otherwise populate some of the gates with parked planes
578 // (might eventually be done by the AIMgr if and when lots of AI traffic is generated)
579
580 // Return a list of exits from a given runway
581 // It is up to the calling function to check for non-zero size of returned array before use
582 node_array_type FGGround::GetExits(string rwyID) {
583         // FIXME - get a 07L or similar in here and we're stuffed!!!
584         return(runways[atoi(rwyID.c_str())].exits);
585 }
586
587 void FGGround::RequestDeparture(PlaneRec plane, FGAIEntity* requestee) {
588         // For now we'll just automatically clear all planes to the runway hold.
589         // This communication needs to be delayed 20 sec or so from receiving the request.
590         // Even if display=false we still need to start the timer in case display=true when communication starts.
591         // We also need to bear in mind we also might have other outstanding communications, although for now we'll punt that issue!
592         // FIXME - sort the above!
593         
594         // HACK - assume that anything requesting departure is new for now - FIXME LATER
595         GroundRec* g = new GroundRec;
596         g->plane = plane;
597         g->planePtr = requestee;
598         g->taxiRequestOutstanding = true;
599         g->clearanceCounter = 0;
600         g->cleared = false;
601         g->incoming = false;
602         // TODO - need to handle the next 3 as well
603     //Point3D current_pos;
604     //node* destination;
605     //node* last_clearance;
606         
607         ground_traffic.push_back(g);
608 }
609
610 #if 0
611 void FGGround::NewArrival(plane_rec plane) {
612         // What are we going to do here?
613         // We need to start a new ground_rec and add the plane_rec to it
614         // We need to decide what gate we are going to clear it to.
615         // Then we need to add clearing it to that gate to the pending transmissions queue? - or simply transmit?
616         // Probably simply transmit for now and think about a transmission queue later if we need one.
617         // We might need one though in order to add a little delay for response time.
618         ground_rec* g = new ground_rec;
619         g->plane_rec = plane;
620         g->current_pos = ConvertWGS84ToXY(plane.pos);
621         g->node = GetNode(g->current_pos);  // TODO - might need to sort out node/arc here
622         AssignGate(g);
623         g->cleared = false;
624         ground_traffic.push_back(g);
625         NextClearance(g);
626 }
627
628 void FGGround::NewContact(plane_rec plane) {
629         // This is a bit of a convienience function at the moment and is likely to change.
630         if(at a gate or apron)
631                 NewDeparture(plane);
632         else
633                 NewArrival(plane);
634 }
635
636 void FGGround::NextClearance(ground_rec &g) {
637         // Need to work out where we can clear g to.
638         // Assume the pilot doesn't need progressive instructions
639         // We *should* already have a gate or holding point assigned by the time we get here
640         // but it wouldn't do any harm to check.
641         
642         // For now though we will hardwire it to clear to the final destination.
643 }
644
645 void FGGround::AssignGate(ground_rec &g) {
646         // We'll cheat for now - since we only have the user's aircraft and a couple of airports implemented
647         // we'll hardwire the gate!
648         // In the long run the logic of which gate or area to send the plane to could be somewhat non-trivial.
649 }
650 #endif //0
651