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