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