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