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