1 // FGTower - a class to provide tower control at towered airports.
3 // Written by David Luff, started March 2002.
5 // Copyright (C) 2002 David C. Luff - david.luff@nottingham.ac.uk
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.
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.
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.
24 #include <simgear/compiler.h>
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/misc/sgstream.hxx>
36 #include "ATCProjection.hxx"
37 #include "AIPlane.hxx"
42 //DCL - a complete guess for now.
43 #define FG_TOWER_DEFAULT_RANGE 30
45 enum tower_traffic_type {
47 INBOUND, // CIRCUIT traffic gets changed to INBOUND when on final of the full-stop circuit.
49 TTT_UNKNOWN, // departure, but we don't know if for circuits or leaving properly
53 ostream& operator << (ostream& os, tower_traffic_type ttt);
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 USER_REQUEST_TAKE_OFF = 9
67 // TODO - need some differentiation of IFR and VFR traffic in order to give the former priority.
69 // Structure for holding details of a plane under tower control.
70 // Not fixed yet - may include more stuff later.
76 TowerPlaneRec(const PlaneRec& p);
77 TowerPlaneRec(const Point3D& pt);
78 TowerPlaneRec(const PlaneRec& p, const Point3D& pt);
80 FGAIPlane* planePtr; // This might move to the planeRec eventually
84 double eta; // seconds
85 double dist_out; // meters from theshold
88 bool clearedToTakeOff;
89 // ought to add time cleared to depart so we can nag if necessary
90 bool holdShortReported;
92 bool downwindReported;
93 bool longFinalReported;
94 bool longFinalAcknowledged;
96 bool finalAcknowledged;
97 bool rwyVacatedReported;
98 bool rwyVacatedAcknowledged;
99 bool goAroundReported; // set true if plane informs tower that it's going around.
100 bool instructedToGoAround; // set true if plane told by tower to go around.
101 bool onRwy; // is physically on the runway
102 bool nextOnRwy; // currently projected by tower to be the next on the runway
103 bool gearWasUp; // Tell to ATC about gear
104 bool gearUpReported; // Tell to pilot about landing gear
106 bool vfrArrivalReported;
107 bool vfrArrivalAcknowledged;
109 // Type of operation the plane is doing
110 tower_traffic_type opType;
112 // Whereabouts in circuit if doing circuits
115 LandingType landingType;
116 bool isUser; // true if this plane is the user
120 typedef list < TowerPlaneRec* > tower_plane_rec_list_type;
121 typedef tower_plane_rec_list_type::iterator tower_plane_rec_list_iterator;
122 typedef tower_plane_rec_list_type::const_iterator tower_plane_rec_list_const_iterator;
125 class FGTower : public FGATC {
134 void Update(double dt);
136 void ReceiveUserCallback(int code);
138 // Contact tower for VFR approach
139 // eg "Cessna Charlie Foxtrot Golf Foxtrot Sierra eight miles South of the airport for full stop with Bravo"
140 // This function probably only called via user interaction - AI planes will have an overloaded function taking a planerec.
141 void VFRArrivalContact(const string& ID, const LandingType& opt = AIP_LT_UNKNOWN);
142 // For the AI planes...
143 void VFRArrivalContact(const PlaneRec& plane, FGAIPlane* requestee, const LandingType& lt = AIP_LT_UNKNOWN);
145 void RequestDepartureClearance(const string& ID);
146 void RequestTakeOffClearance(const string& ID);
147 void ReportFinal(const string& ID);
148 void ReportLongFinal(const string& ID);
149 void ReportOuterMarker(const string& ID);
150 void ReportMiddleMarker(const string& ID);
151 void ReportInnerMarker(const string& ID);
152 void ReportRunwayVacated(const string& ID);
153 void ReportReadyForDeparture(const string& ID);
154 void ReportDownwind(const string& ID);
155 void ReportGoingAround(const string& ID);
157 // Contact tower when at a hold short for departure - for now we'll assume plane - maybe vehicles might want to cross runway eventually?
158 void ContactAtHoldShort(const PlaneRec& plane, FGAIPlane* requestee, tower_traffic_type operation);
160 // Register the presence of an AI plane at a point where contact would already have been made in real life
161 // CAUTION - currently it is assumed that this plane's callsign is unique - it is up to AIMgr to generate unique callsigns.
162 void RegisterAIPlane(const PlaneRec& plane, FGAIPlane* ai, const tower_traffic_type& op, const PatternLeg& lg = LEG_UNKNOWN);
164 // Deregister and remove an AI plane.
165 void DeregisterAIPlane(const string& id);
167 // Public interface to the active runway - this will get more complex
168 // in the future and consider multi-runway use, airplane weight etc.
169 inline const string& GetActiveRunway() const { return activeRwy; }
170 inline const RunwayDetails& GetActiveRunwayDetails() const { return rwy; }
171 // Get the pattern direction of the active rwy.
172 inline int GetPatternDirection() const { return rwy.patternDirection; }
174 inline const string& get_trans_ident() const { return trans_ident; }
176 inline FGGround* const GetGroundPtr() const { return ground; }
178 // Returns true if positions of crosswind/downwind/base leg turns should be constrained by previous traffic
179 // plus the constraint position as a rwy orientated orthopos (meters)
180 bool GetCrosswindConstraint(double& cpos);
181 bool GetDownwindConstraint(double& dpos);
182 bool GetBaseConstraint(double& bpos);
184 string GenText(const string& m, int c);
190 // This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
192 // Respond to a transmission
195 void ProcessRunwayVacatedReport(TowerPlaneRec* t);
196 void ProcessDownwindReport(TowerPlaneRec* t);
198 // Remove all options from the user dialog choice
199 void RemoveAllUserDialogOptions();
201 // Periodic checks on the various traffic.
202 void CheckHoldList(double dt);
203 void CheckCircuitList(double dt);
204 void CheckRunwayList(double dt);
205 void CheckApproachList(double dt);
206 void CheckDepartureList(double dt);
208 // Currently this assumes we *are* next on the runway and doesn't check for planes about to land -
209 // this should be done prior to calling this function.
210 void ClearHoldingPlane(TowerPlaneRec* t);
212 // Find a pointer to plane of callsign ID within the internal data structures
213 TowerPlaneRec* FindPlane(const string& ID);
215 // Remove and delete all instances of a plane with a given ID
216 void RemovePlane(const string& ID);
218 // Figure out if a given position lies on the active runway
219 // Might have to change when we consider more than one active rwy.
220 bool OnActiveRunway(const Point3D& pt);
222 // Figure out if a given position lies on a runway or not
223 bool OnAnyRunway(const Point3D& pt, bool onGround);
225 // Calculate the eta of a plane to the threshold.
226 // For ground traffic this is the fastest they can get there.
227 // For air traffic this is the middle approximation.
228 void CalcETA(TowerPlaneRec* tpr, bool printout = false);
230 // Iterate through all the lists and call CalcETA for all the planes.
231 void doThresholdETACalc();
233 // Order the list of traffic as per expected threshold use and flag any conflicts
234 bool doThresholdUseOrder();
236 // Calculate the crow-flys distance of a plane to the threshold in meters
237 double CalcDistOutM(TowerPlaneRec* tpr);
239 // Calculate the crow-flys distance of a plane to the threshold in miles
240 double CalcDistOutMiles(TowerPlaneRec* tpr);
243 void doCommunication();
246 void IssueLandingClearance(TowerPlaneRec* tpr);
247 void IssueGoAround(TowerPlaneRec* tpr);
248 void IssueDepartureClearance(TowerPlaneRec* tpr);
250 unsigned int update_count; // Convienince counter for speading computational load over several updates
251 unsigned int update_count_max; // ditto.
253 double timeSinceLastDeparture; // Time in seconds since last departure from active rwy.
254 bool departed; // set true when the above needs incrementing with time, false when it doesn't.
256 // environment - need to make sure we're getting the surface winds and not winds aloft.
257 SGPropertyNode_ptr wind_from_hdg; //degrees
258 SGPropertyNode_ptr wind_speed_knots; //knots
260 double aptElev; // Airport elevation
261 string activeRwy; // Active runway number - For now we'll disregard multiple / alternate runway operation.
262 RunwayDetails rwy; // Assumed to be the active one for now.
263 bool rwyOccupied; // Active runway occupied flag. For now we'll disregard land-and-hold-short operations.
264 FGATCAlignedProjection ortho; // Orthogonal mapping of the local area with the active runway threshold at the origin
265 FGATCAlignedProjection ortho_temp; // Ortho for any runway (needed to get plane position in airport)
267 // Figure out which runways are active.
268 // For now we'll just be simple and do one active runway - eventually this will get much more complex
269 // This is a private function - public interface to the results of this is through GetActiveRunway
272 // Need a data structure to hold details of the various active planes
273 // or possibly another data structure with the positions of the inactive planes.
274 // Need a data structure to hold outstanding communications from aircraft.
276 // Linked-list of planes on approach to active rwy ordered with nearest first (timewise).
277 // Includes planes that have landed but not yet vacated runway.
278 // Somewhat analagous to the paper strips used (used to be used?) in real life.
279 // Doesn't include planes in circuit until they turn onto base/final?
280 // TODO - may need to alter this for operation to more than one active rwy.
281 tower_plane_rec_list_type appList;
282 tower_plane_rec_list_iterator appListItr;
284 // What should we do with planes approaching the airport to join the circuit somewhere
285 // but not on straight-in though? - put them in here for now.
286 tower_plane_rec_list_type circuitAppList;
287 tower_plane_rec_list_iterator circuitAppListItr;
289 // List of departed planes (planes doing circuits go into circuitList not depList after departure)
290 tower_plane_rec_list_type depList;
291 tower_plane_rec_list_iterator depListItr;
293 // List of planes in the circuit (ordered by nearest to landing first)
294 tower_plane_rec_list_type circuitList;
295 tower_plane_rec_list_iterator circuitListItr;
297 // List of planes waiting to depart
298 tower_plane_rec_list_type holdList;
299 tower_plane_rec_list_iterator holdListItr;
301 // List of planes on rwy
302 tower_plane_rec_list_type rwyList;
303 tower_plane_rec_list_iterator rwyListItr;
305 // List of all planes due to use a given rwy arranged in projected order of rwy use
306 tower_plane_rec_list_type trafficList; // TODO - needs to be expandable to more than one rwy
307 tower_plane_rec_list_iterator trafficListItr;
309 // List of planes that have vacated the runway inbound but not yet handed off to ground
310 tower_plane_rec_list_type vacatedList;
311 tower_plane_rec_list_iterator vacatedListItr;
313 // Returns true if successful
314 bool RemoveFromTrafficList(const string& id);
315 bool RemoveFromAppList(const string& id);
316 bool RemoveFromRwyList(const string& id);
318 // Return the ETA of plane no. list_pos (1-based) in the traffic list.
319 // i.e. list_pos = 1 implies next to use runway.
320 double GetTrafficETA(unsigned int list_pos, bool printout = false);
322 // Add a tower plane rec with ETA to the traffic list in the correct position ETA-wise.
323 // Returns true if this could cause a threshold ETA conflict with other traffic, false otherwise.
324 bool AddToTrafficList(TowerPlaneRec* t, bool holding = false);
326 bool AddToCircuitList(TowerPlaneRec* t);
328 // Add to vacated list only if not already present
329 void AddToVacatedList(TowerPlaneRec* t);
331 void AddToHoldingList(TowerPlaneRec* t);
333 // Ground can be separate or handled by tower in real life.
334 // In the program we will always use a separate FGGround class, but we need to know
335 // whether it is supposed to be separate or not to give the correct instructions.
336 bool separateGround; // true if ground control is separate
337 FGGround* ground; // The ground control associated with this airport.
339 bool _departureControlled; // true if we need to hand off departing traffic to departure control
340 //FGDeparture* _departure; // The relevant departure control (once we've actually written it!)
342 // for failure modeling
343 string trans_ident; // transmitted ident
344 bool tower_failed; // tower failed?
346 // Pointers to current users position and orientation
347 SGPropertyNode_ptr user_lon_node;
348 SGPropertyNode_ptr user_lat_node;
349 SGPropertyNode_ptr user_elev_node;
350 SGPropertyNode_ptr user_hdg_node;
352 // Details of the general traffic flow etc in the circuit
353 double crosswind_leg_pos; // Distance from threshold crosswind leg is being turned to in meters (actual operation - *not* ideal circuit)
354 double downwind_leg_pos; // Actual offset distance in meters from the runway that planes are flying the downwind leg
355 // Currently not sure whether the above should be always +ve or just take the natural orthopos sign (+ve for RH circuit, -ve for LH).
356 double base_leg_pos; // Actual offset distance from the threshold (-ve) that planes are turning to base leg.
358 double nominal_crosswind_leg_pos;
359 double nominal_downwind_leg_pos;
360 double nominal_base_leg_pos;
362 friend istream& operator>> ( istream&, FGTower& );
365 #endif //_FG_TOWER_HXX