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