]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ground.cxx
f713a69e71b758112c06dc0c7080c3dba708a08c
[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
33 SG_USING_STD(ifstream);
34 SG_USING_STD(cout);
35
36 FGGround::FGGround() {
37         display = false;
38         networkLoadOK = false;
39 }
40
41 FGGround::~FGGround() {
42 }
43
44 void FGGround::ParseRwyExits(node* np, char* es) {
45         char* token;
46         char estr[20];
47         strcpy(estr, es);
48         const char delimiters[] = "-";
49         token = strtok(estr, delimiters);
50         while(token != NULL) {
51                 int i = atoi(token);
52                 //cout << "token = " << token << endl;
53                 //cout << "rwy number = " << i << endl;
54                 //runways[(atoi(token))].exits.push_back(np);
55                 runways[i].exits.push_back(np);
56                 //cout << "token = " << token << '\n';
57                 token = strtok(NULL, delimiters);
58         }
59 }
60         
61
62 // Load the ground logical network of the current instances airport
63 // Return true if successfull.
64 // TODO - currently the file is assumed to reside in the base/ATC directory.
65 // This might change to something more thought out in the future.
66 bool FGGround::LoadNetwork() {
67         node* np;
68         arc* ap;
69         Gate* gp;
70         
71         ifstream fin;
72         SGPath path = globals->get_fg_root();
73         //string taxiPath = "ATC/" + ident + ".taxi";
74         string taxiPath = "ATC/KEMT.taxi";      // FIXME - HARDWIRED FOR TESTING
75         path.append(taxiPath);
76         
77         SG_LOG(SG_GENERAL, SG_INFO, "Trying to read taxiway data for " << ident << "...");
78         //cout << "Trying to read taxiway data for " << ident << "..." << endl;
79         fin.open(path.c_str(), ios::in);
80         if(!fin) {
81                 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open taxiway data input file " << path.c_str());
82                 //cout << "Unable to open taxiway data input file " << path.c_str() << endl;
83                 return(false);
84         }
85         
86         char ch;
87         char buf[30];
88         while(!fin.eof()) {
89                 fin >> buf;
90                 // Node, arc, or [End]?
91                 //cout << "Read in ground network element type = " << buf << endl;
92                 if(!strcmp(buf, "[End]")) {             // TODO - maybe make this more robust to spelling errors by just looking for '['
93                         SG_LOG(SG_GENERAL, SG_INFO, "Done reading " << path.c_str() << endl);
94                         break;
95                 } else if(!strcmp(buf, "N")) {
96                         // Node
97                         np = new node;
98                         np->struct_type = NODE;
99                         fin >> buf;
100                         np->nodeID = atoi(buf);
101                         fin >> buf;
102                         np->pos.setlon(atof(buf));
103                         fin >> buf;
104                         np->pos.setlat(atof(buf));
105                         fin >> buf;
106                         np->pos.setelev(atof(buf));
107                         fin >> buf;             // node type
108                         if(!strcmp(buf, "J")) {
109                                 np->type = JUNCTION;
110                         } else if(!strcmp(buf, "T")) {
111                                 np->type = TJUNCTION;
112                         } else if(!strcmp(buf, "H")) {
113                                 np->type = HOLD;
114                         } else {
115                                 cout << "**** ERROR ***** Unknown node type in taxi network...\n";
116                                 delete np;
117                                 return(false);
118                         }
119                         fin >> buf;             // rwy exit information - gets parsed later - FRAGILE - will break if buf is reused.
120                         // Now the name
121                         np->name = "";
122                         while(1) {
123                                 fin.unsetf(ios::skipws);
124                                 fin >> ch;
125                                 np->name += ch;
126                                 if((ch == '"') || (ch == 0x0A)) {
127                                         break;
128                                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
129                         }
130                         fin.setf(ios::skipws);
131                         network.push_back(np);
132                         // FIXME - fragile - replies on buf not getting modified from exits read to here
133                         // see if we also need to push it onto the runway exit list
134                         cout << "strlen(buf) = " << strlen(buf) << endl;
135                         if(strlen(buf) > 2) {
136                                 cout << "Calling ParseRwyExits for " << buf << endl;
137                                 ParseRwyExits(np, buf);
138                         }
139                 } else if(!strcmp(buf, "A")) {
140                         ap = new arc;
141                         ap->struct_type = ARC;
142                         fin >> buf;
143                         ap->n1 = atoi(buf);
144                         fin >> buf;
145                         ap->n2 = atoi(buf);
146                         fin >> buf;
147                         if(!strcmp(buf, "R")) {
148                                 ap->type = RUNWAY;
149                         } else if(!strcmp(buf, "T")) {
150                                 ap->type = TAXIWAY;
151                         } else {
152                                 cout << "**** ERROR ***** Unknown arc type in taxi network...\n";
153                                 delete ap;
154                                 return(false);
155                         }
156                         // directed?
157                         fin >> buf;
158                         if(!strcmp(buf, "Y")) {
159                                 ap->directed = true;
160                         } else if(!strcmp(buf, "N")) {
161                                 ap->directed = false;
162                         } else {
163                                 cout << "**** ERROR ***** Unknown arc directed value in taxi network - should be Y/N !!!\n";
164                                 delete ap;
165                                 return(false);
166                         }                       
167                         // Now the name
168                         ap->name = "";
169                         while(1) {
170                                 fin.unsetf(ios::skipws);
171                                 fin >> ch;
172                                 ap->name += ch;
173                                 if((ch == '"') || (ch == 0x0A)) {
174                                         break;
175                                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
176                         }
177                         fin.setf(ios::skipws);
178                         network[ap->n1]->arcs.push_back(ap);
179                         network[ap->n2]->arcs.push_back(ap);
180                 } else if(!strcmp(buf, "G")) {
181                         gp = new Gate;
182                         gp->struct_type = NODE;
183                         gp->type = GATE;
184                         fin >> buf;
185                         gp->nodeID = atoi(buf);
186                         fin >> buf;
187                         gp->pos.setlon(atof(buf));
188                         fin >> buf;
189                         gp->pos.setlat(atof(buf));
190                         fin >> buf;
191                         gp->pos.setelev(atof(buf));
192                         fin >> buf;             // gate type - ignore this for now
193                         fin >> buf;             // gate heading
194                         gp->heading = atoi(buf);
195                         // Now the name
196                         gp->name = "";
197                         while(1) {
198                                 fin.unsetf(ios::skipws);
199                                 fin >> ch;
200                                 gp->name += ch;
201                                 if((ch == '"') || (ch == 0x0A)) {
202                                         break;
203                                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
204                         }
205                         fin.setf(ios::skipws);
206                         network.push_back(gp);
207                 } else {
208                         // Something has gone seriously pear-shaped
209                         cout << "********* ERROR - unknown ground network element type... aborting read of " << path.c_str() << '\n';
210                         return(false);
211                 }
212                 
213                 fin >> skipeol;         
214         }
215         return(true);
216 }
217
218 void FGGround::Init() {
219         display = false;
220         
221         // For now we'll hardwire the threshold end
222         Point3D P010(-118.037483, 34.081358, 296 * SG_FEET_TO_METER);
223         double hdg = 25.32;
224         ortho.Init(P010, hdg);
225         
226         networkLoadOK = LoadNetwork();
227 }
228
229 void FGGround::Update() {
230         // Each time step, what do we need to do?
231         // We need to go through the list of outstanding requests and acknowedgements
232         // and process at least one of them.
233         // We need to go through the list of planes under our control and check if
234         // any need to be addressed.
235         // We need to check for planes not under our control coming within our 
236         // control area and address if necessary.
237         
238         // Lets take the example of a plane which has just contacted ground
239         // following landing - presumably requesting where to go?
240         // First we need to establish the position of the plane within the logical network.
241         // Next we need to decide where its going. 
242 }
243
244 // FIXME - at the moment this assumes there is at least one gate and crashes if none
245 // FIXME - In fact, at the moment this routine doesn't work at all and hence is munged to always return Gate 1 !!!!
246 int FGGround::GetRandomGateID() {
247         //cout << "GetRandomGateID called" << endl;
248         return(1);
249
250         gate_vec_type gateVec;
251         //gate_vec_iterator gateVecItr;
252         int num = 0;
253         int thenum;
254         int ID;
255         
256         gatesItr = gates.begin();
257         while(gatesItr != gates.end()) {
258                 if(gatesItr->second.used == false) {
259                         gateVec.push_back(gatesItr->second);
260                         num++;
261                 }
262                 ++gatesItr;
263         }
264         
265         // Randomly select one from the list
266         thenum = (int)(sg_random() * gateVec.size());
267         ID = gateVec[thenum].id;
268         //cout << "Returning gate ID " << ID << " from GetRandomGateID" << endl;
269         return(ID);
270 }
271
272 // Return a pointer to a gate node based on the gate ID
273 Gate* FGGround::GetGateNode(int gateID) {
274         //TODO - ought to add some sanity checking here - ie does a gate of this ID exist?!
275         return(&(gates[gateID]));
276 }
277
278 // Get a path from a point on a runway to a gate
279
280 // Get a path from a node to another node
281 // Eventually we will need complex algorithms for this taking other traffic,
282 // shortest path and suitable paths into accout.  For now we're going to hardwire for KEMT!!!!
283 ground_network_path_type FGGround::GetPath(node* A, node* B) {
284         ground_network_path_type path;
285         //arc_array_iterator arcItr;
286         //bool found;
287
288         // VERY HARDWIRED - this hardwires a path from the far end of R01 to Gate 1.
289         // In fact in real life the area between R01/19 and Taxiway Alpha at KEMT is tarmaced and planes
290         // are supposed to exit the rwy asap.
291         // OK - for now very hardwire this for testing
292         path.push_back(network[1]);
293         path.push_back(network[1]->arcs[1]);    // ONLY BECAUSE WE KNOW THIS IS THE ONE !!!!!
294         path.push_back(network[3]);
295         path.push_back(network[3]->arcs[1]);
296         path.push_back(network[5]);
297         path.push_back(network[5]->arcs[0]);
298         path.push_back(network[4]);
299         path.push_back(network[4]->arcs[2]);
300         path.push_back(network[6]);
301         path.push_back(network[6]->arcs[2]);
302         path.push_back(network[7]);                             // THE GATE!!  Note that for now we're not even looking at the requested exit and gate passed in !!!!!
303
304 #if 0   
305         // In this hardwired scheme there are two possibilities - taxiing from rwy to gate or gate to rwy.
306         if(B->type == GATE) {
307                 //return an inward path
308                 path.push_back(A);
309                 // In this hardwired scheme we know A is a rwy exit and should have one taxiway arc only
310                 // THIS WILL NOT HOLD TRUE IN THE GENERAL CASE
311                 arcItr = A->arcs.begin();
312                 found = false;
313                 while(arcItr != A->arcs.end()) {
314                         if(arcItr->type == TAXIWAY) {
315                                 path.push_back(&(*arcItr));
316                                 found = true;
317                                 break;
318                         }
319                 }
320                 if(found == false) {
321                         //cout << "AI/ATC SUBSYSTEM ERROR - no taxiway from runway exit in airport.cxx" << endl;
322                 }
323                 // Then push back the start of taxiway node
324                 // Then push back the taxiway arc
325                 arcItr = A->arcs.begin();
326                 found = false;
327                 while(arcItr != A->arcs.end()) {
328                         if(arcItr->type == TAXIWAY) { // FIXME - OOPS - two taxiways go off this node
329                                 // How are we going to differentiate, apart from one called Alpha.
330                                 // I suppose eventually the traversal algorithms will select.
331                                 path.push_back(&(*arcItr));
332                                 found = true;
333                                 break;
334                 }
335                 }
336                 if(found == false) {
337                 //cout << "AI/ATC SUBSYSTEM ERROR - no taxiway from runway exit in airport.cxx" << endl;
338                 }
339                 // Then push back the junction node
340                 // Planes always face one way in the parking, so depending on which parking exit we have either take it or push back another taxiway node
341                 // Repeat if necessary
342                 // Then push back the gate B
343                 path.push_back(B);
344         } else {
345                 //return an outward path
346         }       
347
348         // WARNING TODO FIXME - this is VERY FRAGILE - eg taxi to apron!!! but should be enough to
349         // see an AI plane physically taxi.
350 #endif  // 0
351         
352         return(path);
353 };
354
355
356 // Randomly or otherwise populate some of the gates with parked planes
357 // (might eventually be done by the AIMgr if and when lots of AI traffic is generated)
358
359 // Return a list of exits from a given runway
360 // It is up to the calling function to check for non-zero size of returned array before use
361 node_array_type FGGround::GetExits(int rwyID) {
362         return(runways[rwyID].exits);
363 }
364
365 #if 0
366 void FGGround::NewArrival(plane_rec plane) {
367         // What are we going to do here?
368         // We need to start a new ground_rec and add the plane_rec to it
369         // We need to decide what gate we are going to clear it to.
370         // Then we need to add clearing it to that gate to the pending transmissions queue? - or simply transmit?
371         // Probably simply transmit for now and think about a transmission queue later if we need one.
372         // We might need one though in order to add a little delay for response time.
373         ground_rec* g = new ground_rec;
374         g->plane_rec = plane;
375         g->current_pos = ConvertWGS84ToXY(plane.pos);
376         g->node = GetNode(g->current_pos);  // TODO - might need to sort out node/arc here
377         AssignGate(g);
378         g->cleared = false;
379         ground_traffic.push_back(g);
380         NextClearance(g);
381 }
382
383 void FGGround::NewContact(plane_rec plane) {
384         // This is a bit of a convienience function at the moment and is likely to change.
385         if(at a gate or apron)
386                 NewDeparture(plane);
387         else
388                 NewArrival(plane);
389 }
390
391 void FGGround::NextClearance(ground_rec &g) {
392         // Need to work out where we can clear g to.
393         // Assume the pilot doesn't need progressive instructions
394         // We *should* already have a gate or holding point assigned by the time we get here
395         // but it wouldn't do any harm to check.
396         
397         // For now though we will hardwire it to clear to the final destination.
398 }
399
400 void FGGround::AssignGate(ground_rec &g) {
401         // We'll cheat for now - since we only have the user's aircraft and a couple of airports implemented
402         // we'll hardwire the gate!
403         // In the long run the logic of which gate or area to send the plane to could be somewhat non-trivial.
404 }
405 #endif //0