]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AILocalTraffic.hxx
Fix a situation where the AI aircraft sometimes wouldn't be visible when they should
[flightgear.git] / src / ATC / AILocalTraffic.hxx
1 // FGAILocalTraffic - AIEntity derived class with enough logic to
2 // fly and interact with the traffic pattern.
3 //
4 // Written by David Luff, started March 2002.
5 //
6 // Copyright (C) 2002  David C. Luff - david.luff@nottingham.ac.uk
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 #ifndef _FG_AILocalTraffic_HXX
23 #define _FG_AILocalTraffic_HXX
24
25 #include <plib/sg.h>
26 #include <plib/ssg.h>
27 #include <simgear/math/point3d.hxx>
28 #include <Main/fg_props.hxx>
29
30 #include "tower.hxx"
31 #include "AIPlane.hxx"
32 #include "ATCProjection.hxx"
33 #include "ground.hxx"
34
35 #include <string>
36 SG_USING_STD(string);
37
38 enum TaxiState {
39         TD_INBOUND,
40         TD_OUTBOUND,
41         TD_NONE,
42         TD_LINING_UP
43 };
44
45 enum OperatingState {
46         IN_PATTERN,
47         TAXIING,
48         PARKED,
49         EN_ROUTE
50 };
51
52 struct StartOfDescent {
53         PatternLeg leg;
54         double x;       // Runway aligned orthopos
55         double y;       // ditto
56 };
57
58 class FGAILocalTraffic : public FGAIPlane {
59         
60 public:
61         
62         // At the moment we expect the expanded short form callsign - eventually we will just want the reg + type.
63         FGAILocalTraffic();
64         ~FGAILocalTraffic();
65         
66         // Initialise
67         bool Init(const string& callsign, string ICAO, OperatingState initialState = PARKED, PatternLeg initialLeg = DOWNWIND);
68         
69         // Run the internal calculations
70         void Update(double dt);
71         
72         // Go out and practice circuits
73         void FlyCircuits(int numCircuits, bool tag);
74         
75         // Return what type of landing we're doing on this circuit
76         LandingType GetLandingOption();
77         
78         // TODO - this will get more complex and moved into the main class
79         // body eventually since the position approved to taxi to will have
80         // to be passed.
81         inline void ApproveTaxiRequest() {taxiRequestCleared = true;}
82         
83         inline void DenyTaxiRequest() {taxiRequestCleared = false;}
84         
85         void RegisterTransmission(int code);
86         
87         // Process callbacks sent by base class
88         // (These codes are not related to the codes above)
89         void ProcessCallback(int code);
90         
91         // This is a hack and will probably go eventually
92         inline bool AtHoldShort() {return holdingShort;}
93         
94 protected:
95         
96         // Attempt to enter the traffic pattern in a reasonably intelligent manner
97         void EnterTrafficPattern(double dt);
98         
99         // Set up the internal state to be consistent for a downwind entry.
100         void DownwindEntry();
101         
102         // Ditto for straight-in
103         void StraightInEntry(bool des = false);
104         
105         // Do what is necessary to land and parkup at home airport
106         void ReturnToBase(double dt);
107         
108         // Airport/runway/pattern details
109         string airportID;       // The ICAO code of the airport that we're operating around
110         double aptElev;         // Airport elevation
111         FGGround* ground;       // A pointer to the ground control.
112         FGTower* tower; // A pointer to the tower control.
113         bool _controlled;       // Set true if we find tower control working for the airport, false otherwise.
114         RunwayDetails rwy;
115         double patternDirection;        // 1 for right, -1 for left (This is double because we multiply/divide turn rates
116         // with it to get RH/LH turns - DON'T convert it to int under ANY circumstances!!
117         double glideAngle;              // Assumed to be visual glidepath angle for FGAILocalTraffic - can be found at www.airnav.com
118         // Its conceivable that patternDirection and glidePath could be moved into the RunwayDetails structure.
119         
120         // Its possible that this might be moved out to the ground/airport class at some point.
121         FGATCAlignedProjection ortho;   // Orthogonal mapping of the local area with the threshold at the origin
122         // and the runway aligned with the y axis.
123         
124         void GetAirportDetails(string id);
125         
126         void GetRwyDetails(string id);
127         
128         double responseCounter;         // timer in seconds to allow response to requests to be a little while after them
129         // Will almost certainly get moved to FGAIPlane.        
130         
131 private:
132         FGATCMgr* ATC;  
133         // This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
134
135         // High-level stuff
136         OperatingState operatingState;
137         int circuitsToFly;      //Number of circuits still to do in this session NOT INCLUDING THE CURRENT ONE
138         bool touchAndGo;        //True if circuits should be flown touch and go, false for full stop
139         bool transmitted;       // Set true when a position report for the current leg has been transmitted.
140         
141         // Performance characteristics of the plane in knots and ft/min - some of this might get moved out into FGAIPlane
142         double Vr;
143         double best_rate_of_climb_speed;
144         double best_rate_of_climb;
145         double nominal_climb_speed;
146         double nominal_climb_rate;
147         double nominal_circuit_speed;
148         double min_circuit_speed;
149         double max_circuit_speed;
150         double nominal_descent_rate;
151         double nominal_approach_speed;
152         double nominal_final_speed;
153         double stall_speed_landing_config;
154         double nominal_taxi_speed;
155         
156         // Physical/rendering stuff
157         double wheelOffset;             // Height above ground at which we need to render the plane whilst taxiing
158         bool elevInitGood;              // We have had at least one good elev reading
159         bool inAir;                             // True when off the ground 
160         
161         // environment - some of this might get moved into FGAIPlane
162         SGPropertyNode* wind_from_hdg;  //degrees
163         SGPropertyNode* wind_speed_knots;               //knots
164         
165         // Pattern details that (may) change
166         int numInPattern;               // Number of planes in the pattern (this might get more complicated if high performance GA aircraft fly a higher pattern eventually)
167         int numAhead;           // More importantly - how many of them are ahead of us?
168         double distToNext;              // And even more importantly, how near are we getting to the one immediately ahead?
169         //PatternLeg leg;               // Our current position in the pattern - now moved to FGAIPlane
170         StartOfDescent SoD;             // Start of descent calculated wrt wind, pattern size & altitude, glideslope etc
171         bool descending;                // We're in the coming down phase of the pattern
172         double targetDescentRate;       // m/s
173
174         // Taxiing details
175         // At the moment this assumes that all taxiing in is to gates (a loose term that includes
176         // any permitted parking spot) and that all taxiing out is to runways.
177         bool parked;
178         bool taxiing;
179         bool taxiRequestPending;
180         bool taxiRequestCleared;
181         TaxiState taxiState;
182         double desiredTaxiHeading;
183         double taxiTurnRadius;
184         double nominalTaxiSpeed;
185         Gate* ourGate;
186         ground_network_path_type path;  // a path through the ground network for the plane to taxi
187         unsigned int taxiPathPos;       // position of iterator in taxi path when applicable
188         node* nextTaxiNode;     // next node in taxi path
189         node* holdShortNode;
190         //Runway out_dest; //FIXME - implement this
191         bool holdingShort;
192         bool reportReadyForDeparture;   // set true when ATC has requested that the plane report when ready for departure
193         bool clearedToLineUp;
194         bool clearedToTakeOff;
195         bool _clearedToLand;    // also implies cleared for the option.
196         bool liningUp;  // Set true when the turn onto the runway heading is commenced when taxiing out
197         bool goAround;  // Set true if need to go-around
198         bool goAroundCalled;    // Set true during go-around only after we have called our go-around on the radio
199         bool contactTower;      // we have been told to contact tower
200         bool contactGround;     // we have been told to contact ground
201         bool changeFreq;        // true when we need to change frequency
202         bool _taxiToGA;         // Temporary mega-hack indicating we are to taxi to the GA parking and disconnect from tower control.
203         atc_type changeFreqType;        // the service we need to change to
204         bool freeTaxi;  // False if the airport has a facilities file with a logical taxi network defined, true if we need to calculate our own taxiing points.
205         
206         // Hack for getting close to the runway when atan can go pear-shaped
207         double _savedSlope;
208
209         void FlyTrafficPattern(double dt);
210
211         // TODO - need to add something to define what option we are flying - Touch and go / Stop and go / Landing properly / others?
212
213         void TransmitPatternPositionReport();
214
215         void CalculateSoD(double base_leg_pos, double downwind_leg_pos, bool pattern_direction);
216
217         void ExitRunway(Point3D orthopos);
218
219         void StartTaxi();
220
221         void Taxi(double dt);
222
223         void GetNextTaxiNode();
224         
225         void DoGroundElev();
226         
227         // Set when the plane should be invisible *regardless of distance from user*.
228         bool _invisible;
229 };
230
231 #endif  // _FG_AILocalTraffic_HXX