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