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