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