]> git.mxchange.org Git - flightgear.git/blob - src/ATC/tower.hxx
Further progress towards interactive ATC control
[flightgear.git] / src / ATC / tower.hxx
1 // FGTower - a class to provide tower control at towered 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 #ifndef _FG_TOWER_HXX
22 #define _FG_TOWER_HXX
23
24 #include <simgear/compiler.h>
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/misc/sgstream.hxx>
27 //#include <simgear/math/sg_geodesy.hxx>
28 #include <plib/sg.h>
29 //#include <Airports/runways.hxx>
30
31 #include STL_IOSTREAM
32 #include STL_STRING
33
34 SG_USING_STD(string);
35 SG_USING_STD(ios);
36
37 #include "ATC.hxx"
38 //#include "ATCmgr.hxx"
39 #include "ground.hxx"
40 #include "ATCProjection.hxx"
41 #include "AIPlane.hxx"
42
43 //DCL - a complete guess for now.
44 #define FG_TOWER_DEFAULT_RANGE 30
45
46 enum tower_traffic_type {
47         CIRCUIT,
48         INBOUND,        // CIRCUIT traffic gets changed to INBOUND when on final of the full-stop circuit.
49         OUTBOUND,
50         TTT_UNKNOWN,    // departure, but we don't know if for circuits or leaving properly
51         STRAIGHT_IN
52 };
53
54 ostream& operator << (ostream& os, tower_traffic_type ttt);
55
56 enum tower_callback_type {
57         USER_REQUEST_VFR_DEPARTURE = 1,
58         USER_REQUEST_VFR_ARRIVAL = 2,
59         USER_REQUEST_VFR_ARRIVAL_FULL_STOP = 3,
60         USER_REQUEST_VFR_ARRIVAL_TOUCH_AND_GO = 4
61 };
62
63 // TODO - need some differentiation of IFR and VFR traffic in order to give the former priority.
64
65 // Structure for holding details of a plane under tower control.
66 // Not fixed yet - may include more stuff later.
67 class TowerPlaneRec {
68         
69 public:
70         
71         TowerPlaneRec();
72         TowerPlaneRec(PlaneRec p);
73         TowerPlaneRec(Point3D pt);
74         TowerPlaneRec(PlaneRec p, Point3D pt);
75         
76         FGAIPlane* planePtr;    // This might move to the planeRec eventually
77         PlaneRec plane;
78         
79         Point3D pos;
80         double eta;             // seconds
81         double dist_out;        // meters from theshold
82         bool clearedToLand;
83         bool clearedToLineUp;
84         bool clearedToTakeOff;
85         // ought to add time cleared to depart so we can nag if necessary
86         bool holdShortReported;
87         bool downwindReported;
88         bool longFinalReported;
89         bool longFinalAcknowledged;
90         bool finalReported;
91         bool finalAcknowledged;
92         bool instructedToGoAround;      // set true if told by tower to go around
93         bool onRwy;             // is physically on the runway
94         bool nextOnRwy;         // currently projected by tower to be the next on the runway
95         
96         bool vfrArrivalReported;
97         bool vfrArrivalAcknowledged;
98
99         // Type of operation the plane is doing
100         tower_traffic_type opType;
101         
102         // Whereabouts in circuit if doing circuits
103         PatternLeg leg;
104         
105         LandingType landingType;
106         
107         bool isUser;    // true if this plane is the user
108 };
109
110
111 typedef list < TowerPlaneRec* > tower_plane_rec_list_type;
112 typedef tower_plane_rec_list_type::iterator tower_plane_rec_list_iterator;
113 typedef tower_plane_rec_list_type::const_iterator tower_plane_rec_list_const_iterator;
114
115
116 class FGTower : public FGATC {
117
118 public:
119         
120         FGTower();
121         ~FGTower();
122         
123         void Init();
124         
125         void Update(double dt);
126         
127         void ReceiveUserCallback(int code);
128
129         // Contact tower for VFR approach
130         // eg "Cessna Charlie Foxtrot Golf Foxtrot Sierra eight miles South of the airport for full stop with Bravo"
131         // This function probably only called via user interaction - AI planes will have an overloaded function taking a planerec.
132         void VFRArrivalContact(string ID, LandingType opt = AIP_LT_UNKNOWN);
133         
134         void RequestDepartureClearance(string ID);      
135         void ReportFinal(string ID);
136         void ReportLongFinal(string ID);
137         void ReportOuterMarker(string ID);
138         void ReportMiddleMarker(string ID);
139         void ReportInnerMarker(string ID);
140         void ReportGoingAround(string ID);
141         void ReportRunwayVacated(string ID);
142         void ReportReadyForDeparture(string ID);
143         void ReportDownwind(string ID);
144         
145         // Contact tower when at a hold short for departure - for now we'll assume plane - maybe vehicles might want to cross runway eventually?
146         void ContactAtHoldShort(PlaneRec plane, FGAIPlane* requestee, tower_traffic_type operation);
147         
148         // Register the presence of an AI plane at a point where contact would already have been made in real life
149         // CAUTION - currently it is assumed that this plane's callsign is unique - it is up to AIMgr to generate unique callsigns.
150         void RegisterAIPlane(PlaneRec plane, FGAIPlane* ai, tower_traffic_type op, PatternLeg lg = LEG_UNKNOWN);
151         
152         // Public interface to the active runway - this will get more complex 
153         // in the future and consider multi-runway use, airplane weight etc.
154         inline string GetActiveRunway() { return activeRwy; }
155         inline RunwayDetails GetActiveRunwayDetails() { return rwy; }
156         
157         inline void SetDisplay() { display = true; }
158         inline void SetNoDisplay() { display = false; }
159         
160         inline string get_trans_ident() { return trans_ident; }
161         inline atc_type GetType() { return TOWER; }
162         
163         inline FGGround* GetGroundPtr() { return ground; }
164         
165         // Returns true if positions of crosswind/downwind/base leg turns should be constrained by previous traffic
166         // plus the constraint position as a rwy orientated orthopos (meters)
167         bool GetCrosswindConstraint(double& cpos);
168         bool GetDownwindConstraint(double& dpos);
169         bool GetBaseConstraint(double& bpos);
170         
171         string GenText(const string& m, int c);
172
173 private:
174         FGATCMgr* ATCmgr;       
175         // This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
176         
177         // Respond to a transmission
178         void Respond();
179         
180         void CheckHoldList(double dt);
181
182         void CheckCircuitList(double dt);
183         
184         void CheckRunwayList(double dt);
185
186         void CheckApproachList(double dt);
187         
188         // Currently this assumes we *are* next on the runway and doesn't check for planes about to land - 
189         // this should be done prior to calling this function.
190         void ClearHoldingPlane(TowerPlaneRec* t);
191         
192         // Find a pointer to plane of callsign ID within the internal data structures
193         TowerPlaneRec* FindPlane(string ID);
194         
195         // Figure out if a given position lies on the active runway
196         // Might have to change when we consider more than one active rwy.
197         bool OnActiveRunway(Point3D pt);
198         
199         // Figure out if a given position lies on a runway or not
200         bool OnAnyRunway(Point3D pt);
201         
202         // Calculate the eta of a plane to the threshold.
203         // For ground traffic this is the fastest they can get there.
204         // For air traffic this is the middle approximation.
205         void CalcETA(TowerPlaneRec* tpr, bool printout = false);
206         
207         // Iterate through all the lists and call CalcETA for all the planes.
208         void doThresholdETACalc();
209         
210         // Order the list of traffic as per expected threshold use and flag any conflicts
211         bool doThresholdUseOrder();
212         
213         // Calculate the crow-flys distance of a plane to the threshold in meters
214         double CalcDistOutM(TowerPlaneRec* tpr);
215
216         // Calculate the crow-flys distance of a plane to the threshold in miles
217         double CalcDistOutMiles(TowerPlaneRec* tpr);
218         
219         /*
220         void doCommunication();
221         */
222         
223         void IssueLandingClearance(TowerPlaneRec* tpr);
224         void IssueGoAround(TowerPlaneRec* tpr);
225         void IssueDepartureClearance(TowerPlaneRec* tpr);
226         
227         unsigned int update_count;      // Convienince counter for speading computational load over several updates
228         unsigned int update_count_max;  // ditto.
229         
230         bool display;           // Flag to indicate whether we should be outputting to the ATC display.
231         bool displaying;                // Flag to indicate whether we are outputting to the ATC display.
232         
233         double timeSinceLastDeparture;  // Time in seconds since last departure from active rwy.
234         bool departed;  // set true when the above needs incrementing with time, false when it doesn't.
235         
236         // environment - need to make sure we're getting the surface winds and not winds aloft.
237         SGPropertyNode* wind_from_hdg;  //degrees
238         SGPropertyNode* wind_speed_knots;               //knots
239         
240         double aptElev;         // Airport elevation
241         string activeRwy;       // Active runway number - For now we'll disregard multiple / alternate runway operation.
242         RunwayDetails rwy;      // Assumed to be the active one for now.
243         bool rwyOccupied;       // Active runway occupied flag.  For now we'll disregard land-and-hold-short operations.
244         FGATCAlignedProjection ortho;   // Orthogonal mapping of the local area with the active runway threshold at the origin
245         
246         // Figure out which runways are active.
247         // For now we'll just be simple and do one active runway - eventually this will get much more complex
248         // This is a private function - public interface to the results of this is through GetActiveRunway
249         void DoRwyDetails();
250         
251         // Need a data structure to hold details of the various active planes
252         // or possibly another data structure with the positions of the inactive planes.
253         // Need a data structure to hold outstanding communications from aircraft.
254         
255         // Linked-list of planes on approach to active rwy ordered with nearest first (timewise).
256         // Includes planes that have landed but not yet vacated runway.
257         // Somewhat analagous to the paper strips used (used to be used?) in real life.
258         // Doesn't include planes in circuit until they turn onto base/final?
259         // TODO - may need to alter this for operation to more than one active rwy.
260         tower_plane_rec_list_type appList;
261         tower_plane_rec_list_iterator appListItr;
262
263         // What should we do with planes approaching the airport to join the circuit somewhere
264         // but not on straight-in though? - put them in here for now.   
265         tower_plane_rec_list_type circuitAppList;
266         tower_plane_rec_list_iterator circuitAppListItr;
267         
268         // List of departed planes (planes doing circuits go into circuitList not depList after departure)
269         tower_plane_rec_list_type depList;
270         tower_plane_rec_list_iterator depListItr;
271         
272         // List of planes in the circuit (ordered by nearest to landing first)
273         tower_plane_rec_list_type circuitList;
274         tower_plane_rec_list_iterator circuitListItr;
275         
276         // List of planes waiting to depart
277         tower_plane_rec_list_type holdList;
278         tower_plane_rec_list_iterator holdListItr;
279                 
280         // List of planes on rwy
281         tower_plane_rec_list_type rwyList;
282         tower_plane_rec_list_iterator rwyListItr;
283
284         // List of all planes due to use a given rwy arranged in projected order of rwy use
285         tower_plane_rec_list_type trafficList;  // TODO - needs to be expandable to more than one rwy
286         tower_plane_rec_list_iterator trafficListItr;
287         
288         // Returns true if successful
289         bool RemoveFromTrafficList(string id);
290         
291         // Return the ETA of plane no. list_pos (1-based) in the traffic list.
292         // i.e. list_pos = 1 implies next to use runway.
293         double GetTrafficETA(unsigned int list_pos, bool printout = false);
294         
295         // Add a tower plane rec with ETA to the traffic list in the correct position ETA-wise.
296         // Returns true if this could cause a threshold ETA conflict with other traffic, false otherwise.
297         bool AddToTrafficList(TowerPlaneRec* t, bool holding = false);
298         
299         bool AddToCircuitList(TowerPlaneRec* t);
300
301         // Ground can be separate or handled by tower in real life.
302         // In the program we will always use a separate FGGround class, but we need to know
303         // whether it is supposed to be separate or not to give the correct instructions.
304         bool separateGround;    // true if ground control is separate
305         FGGround* ground;       // The ground control associated with this airport.
306         
307         // for failure modeling
308         string trans_ident;             // transmitted ident
309         bool tower_failed;              // tower failed?
310         
311         // Pointers to current users position and orientation
312         SGPropertyNode* user_lon_node;
313         SGPropertyNode* user_lat_node;
314         SGPropertyNode* user_elev_node;
315         SGPropertyNode* user_hdg_node;
316         
317         // Details of the general traffic flow etc in the circuit
318         double crosswind_leg_pos;       // Distance from threshold crosswind leg is being turned to in meters (actual operation - *not* ideal circuit)
319         double downwind_leg_pos;        // Actual offset distance in meters from the runway that planes are flying the downwind leg
320         // Currently not sure whether the above should be always +ve or just take the natural orthopos sign (+ve for RH circuit, -ve for LH).
321         double base_leg_pos;            // Actual offset distance from the threshold (-ve) that planes are turning to base leg.
322         
323         double nominal_crosswind_leg_pos;
324         double nominal_downwind_leg_pos;
325         double nominal_base_leg_pos;
326         
327         friend istream& operator>> ( istream&, FGTower& );
328 };
329
330 #endif  //_FG_TOWER_HXX