]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
MSVC doesn't have a round function
[flightgear.git] / src / ATC / trafficcontrol.hxx
1 // trafficcontrol.hxx - classes to manage AIModels based air traffic control
2 // Written by Durk Talsma, started September 2006.
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //
18 // $Id$
19
20
21 #ifndef _TRAFFIC_CONTROL_HXX_
22 #define _TRAFFIC_CONTROL_HXX_
23
24
25 #ifndef __cplusplus
26 # error This library requires C++
27 #endif
28
29
30 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32
33
34
35 #include STL_STRING
36 #include <vector>
37
38 SG_USING_STD(string);
39 SG_USING_STD(vector);
40
41
42 typedef vector<int> intVec;
43 typedef vector<int>::iterator intVecIterator;
44
45
46 class FGAIFlightPlan;  // forward reference
47 class FGGroundNetwork; // forward reference
48 //class FGAISchedule;    // forward reference
49 class FGAIAircraft;    // forward reference
50
51 /**************************************************************************************
52  * class FGATCInstruction
53  * like class FGATC Controller, this class definition should go into its own file
54  * and or directory... For now, just testing this stuff out though...
55  *************************************************************************************/
56 class FGATCInstruction
57 {
58 private:
59   bool holdPattern;
60   bool holdPosition;
61   bool changeSpeed;
62   bool changeHeading;
63   bool changeAltitude;
64   bool resolveCircularWait;
65
66   double speed;
67   double heading;
68   double alt;
69 public:
70
71   FGATCInstruction();
72   bool hasInstruction   ();
73   bool getHoldPattern   () { return holdPattern;    };
74   bool getHoldPosition  () { return holdPosition;   };
75   bool getChangeSpeed   () { return changeSpeed;    };
76   bool getChangeHeading () { return changeHeading;  };
77   bool getChangeAltitude() { return changeAltitude; };
78
79   double getSpeed       () { return speed; };
80   double getHeading     () { return heading; };
81   double getAlt         () { return alt; };
82
83   bool getCheckForCircularWait() { return resolveCircularWait; };
84
85   void setHoldPattern   (bool val) { holdPattern    = val; };
86   void setHoldPosition  (bool val) { holdPosition   = val; };
87   void setChangeSpeed   (bool val) { changeSpeed    = val; };
88   void setChangeHeading (bool val) { changeHeading  = val; };
89   void setChangeAltitude(bool val) { changeAltitude = val; };
90
91   void setResolveCircularWait (bool val) { resolveCircularWait = val; }; 
92
93   void setSpeed       (double val) { speed   = val; };
94   void setHeading     (double val) { heading = val; };
95   void setAlt         (double val) { alt     = val; };
96 };
97
98
99
100
101
102 /**************************************************************************************
103  * class FGTrafficRecord
104  *************************************************************************************/
105 class FGTrafficRecord
106 {
107 private:
108   int id, waitsForId;
109   int currentPos;
110   int leg;
111   int state;
112   time_t timer;
113   intVec intentions;
114   FGATCInstruction instruction;
115   double latitude, longitude, heading, speed, altitude, radius;
116   string runway;
117   //FGAISchedule *trafficRef;
118   FGAIAircraft *aircraft;
119   
120   
121 public:
122   FGTrafficRecord();
123   
124   void setId(int val)  { id = val; };
125   void setRadius(double rad) { radius = rad;};
126   void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
127   void setRunway(string rwy) { runway = rwy;};
128   void setLeg(int lg) { leg = lg;};
129   int getId() { return id;};
130   int getState() { return state;};
131   FGATCInstruction getInstruction() { return instruction;};
132   bool hasInstruction() { return instruction.hasInstruction(); };
133   void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
134   bool checkPositionAndIntentions(FGTrafficRecord &other);
135   int  crosses                   (FGGroundNetwork *, FGTrafficRecord &other); 
136   bool isOpposing                (FGGroundNetwork *, FGTrafficRecord &other, int node);
137
138   bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
139
140   bool getSpeedAdjustment() { return instruction.getChangeSpeed(); };
141   
142   double getLatitude () { return latitude ; };
143   double getLongitude() { return longitude; };
144   double getHeading  () { return heading  ; };
145   double getSpeed    () { return speed    ; };
146   double getAltitude () { return altitude ; };
147   double getRadius   () { return radius   ; };
148
149   int getWaitsForId  () { return waitsForId; };
150
151   void setSpeedAdjustment(double spd);
152   void setHeadingAdjustment(double heading);
153   void clearSpeedAdjustment  () { instruction.setChangeSpeed  (false); };
154   void clearHeadingAdjustment() { instruction.setChangeHeading(false); };
155
156   bool hasHeadingAdjustment() { return instruction.getChangeHeading(); };
157   bool hasHoldPosition() { return instruction.getHoldPosition(); };
158   void setHoldPosition (bool inst) { instruction.setHoldPosition(inst); };
159
160   void setWaitsForId(int id) { waitsForId = id; };
161
162   void setResolveCircularWait()   { instruction.setResolveCircularWait(true);  };
163   void clearResolveCircularWait() { instruction.setResolveCircularWait(false); };
164
165   string getRunway() { return runway; };
166   //void setCallSign(string clsgn) { callsign = clsgn; };
167   void setAircraft(FGAIAircraft *ref) { aircraft = ref;};
168   void updateState() { state++;};
169   //string getCallSign() { return callsign; };
170   FGAIAircraft *getAircraft() { return aircraft;};
171   int getTime() { return timer; };
172   int getLeg() { return leg; };
173   void setTime(time_t time) { timer = time; };
174 };
175
176 typedef vector<FGTrafficRecord> TrafficVector;
177 typedef vector<FGTrafficRecord>::iterator TrafficVectorIterator;
178
179
180 /***********************************************************************
181  * Active runway, a utility class to keep track of which aircraft has
182  * clearance for a given runway.
183  **********************************************************************/
184 class ActiveRunway
185 {
186 private:
187   string rwy;
188   int currentlyCleared;
189 public:
190   ActiveRunway(string r, int cc) { rwy = r; currentlyCleared = cc; };
191   
192   string getRunwayName() { return rwy; };
193   int    getCleared   () { return currentlyCleared; };
194 };
195
196 typedef vector<ActiveRunway> ActiveRunwayVec;
197 typedef vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
198
199 /**
200  * class FGATCController
201  * NOTE: this class serves as an abstraction layer for all sorts of ATC controller. 
202  *************************************************************************************/
203 class FGATCController
204 {
205 private:
206   double dt_count;
207 public:
208   typedef enum {
209       MSG_ANNOUNCE_ENGINE_START,
210       MSG_REQUEST_ENGINE_START, 
211       MSG_PERMIT_ENGINE_START,
212       MSG_DENY_ENGINE_START,
213       MSG_ACKNOWLEDGE_ENGINE_START } AtcMsgId;
214   typedef enum {
215       ATC_AIR_TO_GROUND,
216       ATC_GROUND_TO_AIR } AtcMsgDir;
217   FGATCController() { dt_count = 0;};
218   virtual ~FGATCController() {};
219   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
220                                 double lat, double lon,
221                                 double hdg, double spd, double alt, double radius, int leg,
222                                 FGAIAircraft *aircraft) = 0;
223   virtual void             signOff(int id) = 0;
224   virtual void             update(int id, double lat, double lon, 
225                                   double heading, double speed, double alt, double dt) = 0;
226   virtual bool             hasInstruction(int id) = 0;
227   virtual FGATCInstruction getInstruction(int id) = 0;
228
229   double getDt() { return dt_count; };
230   void   setDt(double dt) { dt_count = dt;};
231   void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir);
232   string getGateName(FGAIAircraft *aircraft);
233 };
234
235 /******************************************************************************
236  * class FGTowerControl
237  *****************************************************************************/
238 class FGTowerController : public FGATCController
239 {
240 private:
241   TrafficVector activeTraffic;
242   ActiveRunwayVec activeRunways;
243   
244 public:
245   FGTowerController();
246   virtual ~FGTowerController() {};
247   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
248                                 double lat, double lon,
249                                 double hdg, double spd, double alt, double radius, int leg,
250                                 FGAIAircraft *aircraft);
251   virtual void             signOff(int id);
252   virtual void             update(int id, double lat, double lon, 
253                                   double heading, double speed, double alt, double dt);
254   virtual bool             hasInstruction(int id);
255   virtual FGATCInstruction getInstruction(int id);
256
257   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
258   TrafficVector &getActiveTraffic() { return activeTraffic; };
259 };
260
261 /******************************************************************************
262  * class FGStartupController
263  * handle 
264  *****************************************************************************/
265
266 class FGStartupController : public FGATCController
267 {
268 private:
269   TrafficVector activeTraffic;
270   bool available;
271   time_t lastTransmission;
272   //ActiveRunwayVec activeRunways;
273   
274 public:
275   FGStartupController();
276   virtual ~FGStartupController() {};
277   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
278                                 double lat, double lon,
279                                 double hdg, double spd, double alt, double radius, int leg,
280                                 FGAIAircraft *aircraft);
281   virtual void             signOff(int id);
282   virtual void             update(int id, double lat, double lon, 
283                                   double heading, double speed, double alt, double dt);
284   virtual bool             hasInstruction(int id);
285   virtual FGATCInstruction getInstruction(int id);
286
287   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
288   TrafficVector &getActiveTraffic() { return activeTraffic; };
289
290 }; 
291
292 #endif // _TRAFFIC_CONTROL_HXX