]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ground.hxx
fix another crash on exit by finally converting the rest of unguarded
[flightgear.git] / src / ATC / ground.hxx
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifndef _FG_GROUND_HXX
22 #define _FG_GROUND_HXX
23
24 #include <map>
25 #include <vector>
26 #include <list>
27
28 #include <simgear/math/point3d.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31 #include <simgear/props/props.hxx>
32
33 #include "ATC.hxx"
34 #include "ATCProjection.hxx"
35
36 #include STL_IOSTREAM
37 #include STL_STRING
38
39 SG_USING_STD(string);
40 SG_USING_STD(ios);
41
42 SG_USING_STD(map);
43 SG_USING_STD(vector);
44 SG_USING_STD(list);
45
46 class FGAIEntity;
47 class FGATCMgr;
48
49 //////////////////////////////////////////////////////
50 // Types for the logical network data structure
51 enum arc_type {
52         RUNWAY,
53         TAXIWAY
54 };
55
56 enum node_type {
57         GATE,
58         APRON,
59         HOLD,
60         JUNCTION,
61         TJUNCTION
62 };
63
64 enum GateType {
65         TRANSPORT_PASSENGER,
66         TRANSPORT_PASSENGER_NARROWBODY,
67         TRANSPORT_PASSENGER_WIDEBODY,
68         TRANSPORT_CARGO,
69         GA_LOCAL,
70         GA_LOCAL_SINGLE,
71         GA_LOCAL_TWIN,
72         GA_TRANSIENT,
73         GA_TRANSIENT_SINGLE,
74         GA_TRANSIENT_TWIN,
75         OTHER   // ie. anything goes!!
76 };
77
78 enum network_element_type {
79         NODE,
80         ARC
81 };
82
83 struct ground_network_element {
84         network_element_type struct_type;
85 };
86
87 struct arc : public ground_network_element {
88         int distance;
89         string name;
90         arc_type type;
91         bool directed;  //false if 2-way, true if 1-way.  
92         //This is a can of worms since arcs might be one way in different directions under different circumstances
93         unsigned int n1;        // The nodeID of the first node
94         unsigned int n2;        // The nodeID of the second node
95         // If the arc is directed then flow is normally from n1 to n2.  See the above can of worms comment though.
96 };
97
98 typedef vector <arc*> arc_array_type;   // This was and may become again a list instead of vector
99 typedef arc_array_type::iterator arc_array_iterator;
100 typedef arc_array_type::const_iterator arc_array_const_iterator; 
101
102 struct node : public ground_network_element {
103         node();
104         ~node();
105         
106         unsigned int nodeID;    //each node in an airport needs a unique ID number - this is ZERO-BASED to match array position
107         Point3D pos;
108         Point3D orthoPos;
109         string name;
110         node_type type;
111         arc_array_type arcs;
112         double max_turn_radius;
113 };
114
115 typedef vector <node*> node_array_type;
116 typedef node_array_type::iterator node_array_iterator;
117 typedef node_array_type::const_iterator node_array_const_iterator;
118
119 struct Gate : public node {
120         GateType gateType;
121         int max_weight; //units??
122         //airline_code airline; //For the future - we don't have any airline codes ATM
123         int id; // The gate number in the logical scheme of things
124         string name;    // The real-world gate letter/number
125         //node* pNode;
126         bool used;
127         double heading; // The direction the parked-up plane should point in degrees
128 };
129
130 typedef vector < Gate* > gate_vec_type;
131 typedef gate_vec_type::iterator gate_vec_iterator;
132 typedef gate_vec_type::const_iterator gate_vec_const_iterator;
133
134 // A map of gate vs. the logical (internal FGFS) gate ID
135 typedef map < int, Gate* > gate_map_type;
136 typedef gate_map_type::iterator gate_map_iterator;
137 typedef gate_map_type::const_iterator gate_map_const_iterator;
138
139 // Runways - all the runway stuff is likely to change in the future
140 struct Rwy {
141         int id; //note this is a very simplified scheme for now - R & L are not differentiated
142         //It should work for simple one rwy airports
143         node_array_type exits;  //Array of available exits from runway
144         // should probably add an FGRunway structure here as well eventually
145         // Eventually we will also want some encoding of real-life preferred runways
146         // This will get us up and running for single runway airports though.
147 };
148
149 typedef vector < Rwy > runway_array_type;
150 typedef runway_array_type::iterator runway_array_iterator;
151 typedef runway_array_type::const_iterator runway_array_const_iterator;
152
153 // end logical network types
154 ///////////////////////////////////////////////////////
155
156 ///////////////////////////////////////////////////////
157 // Structures to use the network
158
159 // A path through the network 
160 typedef vector < ground_network_element* > ground_network_path_type;
161 typedef ground_network_path_type::iterator ground_network_path_iterator;
162 typedef ground_network_path_type::const_iterator ground_network_path_const_iterator;
163
164 //////////////////////////////////////////////////////////////////////////////////////////
165
166 ////////////////////////////////////////////////
167 //
168 // Stuff for the shortest-path algorithms
169 struct a_path {
170         a_path();
171         
172         ground_network_path_type path;
173         int cost;
174 };
175
176 // Paths mapped by nodeID reached so-far
177 typedef map < unsigned int, a_path* > shortest_path_map_type;
178 typedef shortest_path_map_type::iterator shortest_path_map_iterator;
179
180 // Nodes mapped by their ID
181 //typedef map < unsigned int, node* > node_map_type;
182 //typedef node_map_type::iterator node_map_iterator;
183 ////////////////////////////////////////////////
184
185 // Planes active within the ground network.
186
187 // A more specialist plane rec to include ground information
188 struct GroundRec {
189         FGAIEntity* planePtr;   // This might move to the planeRec eventually
190         
191     PlaneRec plane;
192     Point3D current_pos;
193     node* destination;
194     node* last_clearance;
195         bool taxiRequestOutstanding;    // Plane has requested taxi and we haven't responded yet
196         double clearanceCounter;                // Hack for communication timing - counter since clearance requested in seconds 
197         
198     bool cleared;  // set true when the plane has been cleared to somewhere
199     bool incoming; //true for arrivals, false for departures
200     // status?
201     // Almost certainly need to add more here
202 };
203
204 typedef list < GroundRec* > ground_rec_list;
205 typedef ground_rec_list::iterator ground_rec_list_itr;
206 typedef ground_rec_list::const_iterator ground_rec_list_const_itr;
207
208 //////////////////////////////////////////////////////////////////////////////////////////
209
210 // Hack
211 // perhaps we could use an FGRunway instead of this
212 struct GRunwayDetails {
213         Point3D threshold_pos;
214         Point3D end1ortho;      // ortho projection end1 (the threshold ATM)
215         Point3D end2ortho;      // ortho projection end2 (the take off end in the current hardwired scheme)
216         double hdg;             // true runway heading
217         double length;  // In *METERS*
218         string rwyID;
219 };
220
221 ///////////////////////////////////////////////////////////////////////////////
222 //
223 // FGGround
224 //
225 ///////////////////////////////////////////////////////////////////////////////
226 class FGGround : public FGATC {
227
228 public:
229         FGGround();
230         FGGround(const string& id);
231         ~FGGround();
232     void Init();
233
234     void Update(double dt);
235         
236         inline const string& get_trans_ident() { return trans_ident; }
237
238     // Contact ground control on arrival, assumed to request any gate
239     //void NewArrival(plane_rec plane);
240
241     // Contact ground control on departure, assumed to request currently active runway.
242     void RequestDeparture(const PlaneRec& plane, FGAIEntity* requestee);
243
244     // Contact ground control when the calling routine doesn't know if arrival
245     // or departure is appropriate.
246     //void NewContact(plane_rec plane);
247
248     // Make a request of ground control.
249     //void Request(ground_request request);
250         
251         // Randomly fill some of the available gates and GA parking spots with planes
252         void PopulateGates();
253         
254         // Return a suitable gate (maybe this should be a list of suitable gates so the plane or controller can choose the closest one)
255         void ReturnGate(Gate &gate, GateType type);
256         
257         // Return a pointer to an unused gate
258         Gate* GetGateNode();
259         
260         // Return a pointer to a hold short node
261         node* GetHoldShortNode(const string& rwyID);
262         
263         // Runway stuff - this might change in the future.
264         // Get a list of exits from a given runway
265         // It is up to the calling function to check for non-zero size of returned array before use
266         node_array_type GetExits(const string& rwyID);
267         
268         // Get a path from one node to another
269         ground_network_path_type GetPath(node* A, node* B);
270         
271         // Get a path from a node to a runway threshold
272         ground_network_path_type GetPath(node* A, const string& rwyID);
273         
274         // Get a path from a node to a runway hold short point
275         // Bit of a hack this at the moment!
276         ground_network_path_type GetPathToHoldShort(node* A, const string& rwyID);
277
278 private:
279         FGATCMgr* ATCmgr;       
280         // This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
281         
282     // Need a data structure to hold details of the various active planes
283     // Need a data structure to hold details of the logical network
284     // including which gates are filled - or possibly another data structure
285     // with the positions of the inactive planes.
286     // Need a data structure to hold outstanding communications from aircraft.
287     // Possibly need a data structure to hold outstanding communications to aircraft.
288
289         // The logical network
290         // NODES WILL BE STORED IN THE NETWORK IN ORDER OF nodeID NUMBER
291         // ie. NODE 5 WILL BE AT network[5]
292     node_array_type network;
293         
294         // A map of all the gates indexed against internal (FGFS) ID
295         gate_map_type gates;
296         gate_map_iterator gatesItr;
297
298         FGATCAlignedProjection ortho;
299         
300         // Planes currently active
301     //ground_rec_list ground_traffic;
302
303     // Find the shortest route through the logical network between two points.
304     //FindShortestRoute(point a, point b);
305
306     // Assign a gate or parking location to a new arrival
307     //AssignGate(ground_rec &g);
308
309     // Generate the next clearance for an airplane
310     //NextClearance(ground_rec &g);
311         
312         // environment - need to make sure we're getting the surface winds and not winds aloft.
313         SGPropertyNode_ptr wind_from_hdg;       //degrees
314         SGPropertyNode_ptr wind_speed_knots;            //knots
315         
316         // for failure modeling
317         string trans_ident;             // transmitted ident
318         bool ground_failed;             // ground failed?
319         bool networkLoadOK;             // Indicates whether LoadNetwork returned true or false at last attempt
320         
321         // Tower control
322         bool untowered;         // True if this is an untowered airport (we still need the ground class for shortest path implementation etc
323         //FGATC* tower;         // Pointer to the tower control
324
325         // Logical runway details - this might change in the future.
326         //runway_array_type runways;    // STL way
327         Rwy runways[37];        // quick hack!
328         
329         // Physical runway details
330         double aptElev;         // Airport elevation
331         string activeRwy;       // Active runway number - For now we'll disregard multiple / alternate runway operation.
332         RunwayDetails rwy;      // Assumed to be the active one for now.// Figure out which runways are active.
333         
334         // For now we'll just be simple and do one active runway - eventually this will get much more complex
335         // Copied from FGTower - TODO - it would be better to implement this just once, and have ground call tower
336         // for runway operation details, but at the moment we can't guarantee that tower control at a given airport
337         // will be initialised before ground so we can't do that.
338         void DoRwyDetails();    
339         
340         // Load the logical ground network for this airport from file.
341         // Return true if successfull.
342         bool LoadNetwork();
343         
344         // Parse a runway exit string and push the supplied node pointer onto the runway exit list
345         void ParseRwyExits(node* np, char* es);
346         
347         // Return a random gate ID of an unused gate.
348         // Two error values may be returned and must be checked for by the calling function:
349         // -2 signifies that no gates exist at this airport.
350         // -1 signifies that all gates are currently full.
351         // TODO - modify to return a suitable gate based on aircraft size/weight.
352         int GetRandomGateID();
353         
354         // Return a pointer to the node at a runway threshold
355         // Returns NULL if unsuccessful.
356         node* GetThresholdNode(const string& rwyID);
357         
358         // A shortest path algorithm from memory (I can't find the bl&*dy book again!)
359         ground_network_path_type GetShortestPath(node* A, node* B); 
360         
361         // Planes
362         ground_rec_list ground_traffic;
363         ground_rec_list_itr ground_traffic_itr;
364 };
365
366 #endif  // _FG_GROUND_HXX
367