]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
Performance optimization: empty() instead of size()>0
[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 #include <Airports/airports_fwd.hxx>
25
26 #include <osg/Geode>
27 #include <osg/Geometry>
28 #include <osg/MatrixTransform>
29 #include <osg/Shape>
30
31 #include <simgear/compiler.h>
32 // There is probably a better include than sg_geodesy to get the SG_NM_TO_METER...
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/structure/SGReferenced.hxx>
36 #include <simgear/structure/SGSharedPtr.hxx>
37
38 class FGAIAircraft;
39 typedef std::vector<FGAIAircraft*> AircraftVec;
40 typedef std::vector<FGAIAircraft*>::iterator AircraftVecIterator;
41
42 class FGAIFlightPlan;
43 typedef std::vector<FGAIFlightPlan*>           FlightPlanVec;
44 typedef std::vector<FGAIFlightPlan*>::iterator FlightPlanVecIterator;
45 typedef std::map<std::string, FlightPlanVec>   FlightPlanVecMap;
46
47 class FGTrafficRecord;
48 typedef std::list<FGTrafficRecord> TrafficVector;
49 typedef std::list<FGTrafficRecord>::iterator TrafficVectorIterator;
50
51 class ActiveRunway;
52 typedef std::vector<ActiveRunway> ActiveRunwayVec;
53 typedef std::vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
54
55 typedef std::vector<int> intVec;
56 typedef std::vector<int>::iterator intVecIterator;
57
58 /**************************************************************************************
59  * class FGATCInstruction
60  * like class FGATC Controller, this class definition should go into its own file
61  * and or directory... For now, just testing this stuff out though...
62  *************************************************************************************/
63 class FGATCInstruction
64 {
65 private:
66     bool holdPattern;
67     bool holdPosition;
68     bool changeSpeed;
69     bool changeHeading;
70     bool changeAltitude;
71     bool resolveCircularWait;
72
73     double speed;
74     double heading;
75     double alt;
76 public:
77
78     FGATCInstruction();
79     bool hasInstruction   () const;
80     bool getHoldPattern   () const {
81         return holdPattern;
82     };
83     bool getHoldPosition  () const {
84         return holdPosition;
85     };
86     bool getChangeSpeed   () const {
87         return changeSpeed;
88     };
89     bool getChangeHeading () const {
90         return changeHeading;
91     };
92     bool getChangeAltitude() const {
93         return changeAltitude;
94     };
95
96     double getSpeed       () const {
97         return speed;
98     };
99     double getHeading     () const {
100         return heading;
101     };
102     double getAlt         () const {
103         return alt;
104     };
105
106     bool getCheckForCircularWait() const {
107         return resolveCircularWait;
108     };
109
110     void setHoldPattern   (bool val) {
111         holdPattern    = val;
112     };
113     void setHoldPosition  (bool val) {
114         holdPosition   = val;
115     };
116     void setChangeSpeed   (bool val) {
117         changeSpeed    = val;
118     };
119     void setChangeHeading (bool val) {
120         changeHeading  = val;
121     };
122     void setChangeAltitude(bool val) {
123         changeAltitude = val;
124     };
125
126     void setResolveCircularWait (bool val) {
127         resolveCircularWait = val;
128     };
129
130     void setSpeed       (double val) {
131         speed   = val;
132     };
133     void setHeading     (double val) {
134         heading = val;
135     };
136     void setAlt         (double val) {
137         alt     = val;
138     };
139 };
140
141
142
143
144
145 /**************************************************************************************
146  * class FGTrafficRecord
147  *************************************************************************************/
148 class FGTrafficRecord
149 {
150 private:
151     int id, waitsForId;
152     int currentPos;
153     int leg;
154     int frequencyId;
155     int state;
156     bool allowTransmission;
157     bool allowPushback;
158     int priority;
159     time_t timer;
160     intVec intentions;
161     FGATCInstruction instruction;
162     double latitude, longitude, heading, speed, altitude, radius;
163     std::string runway;
164     //FGAISchedule *trafficRef;
165     FGAIAircraft *aircraft;
166
167
168 public:
169     FGTrafficRecord();
170
171     void setId(int val)  {
172         id = val;
173     };
174     void setRadius(double rad) {
175         radius = rad;
176     };
177     void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
178     void setRunway(const std::string& rwy) {
179         runway = rwy;
180     };
181     void setLeg(int lg) {
182         leg = lg;
183     };
184     int getId() {
185         return id;
186     };
187     int getState() {
188         return state;
189     };
190     void setState(int s) {
191         state = s;
192     }
193     FGATCInstruction getInstruction() {
194         return instruction;
195     };
196     bool hasInstruction() {
197         return instruction.hasInstruction();
198     };
199     void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
200     bool checkPositionAndIntentions(FGTrafficRecord &other);
201     int  crosses                   (FGGroundNetwork *, FGTrafficRecord &other);
202     bool isOpposing                (FGGroundNetwork *, FGTrafficRecord &other, int node);
203     
204     bool isActive(int margin) const;
205
206     bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
207
208     bool getSpeedAdjustment() const {
209         return instruction.getChangeSpeed();
210     };
211
212     double getLatitude () const {
213         return latitude ;
214     };
215     double getLongitude() const {
216         return longitude;
217     };
218     double getHeading  () const {
219         return heading  ;
220     };
221     double getSpeed    () const {
222         return speed    ;
223     };
224     double getAltitude () const {
225         return altitude ;
226     };
227     double getRadius   () const {
228         return radius   ;
229     };
230
231     int getWaitsForId  () const {
232         return waitsForId;
233     };
234
235     void setSpeedAdjustment(double spd);
236     void setHeadingAdjustment(double heading);
237     void clearSpeedAdjustment  () {
238         instruction.setChangeSpeed  (false);
239     };
240     void clearHeadingAdjustment() {
241         instruction.setChangeHeading(false);
242     };
243
244     bool hasHeadingAdjustment() const {
245         return instruction.getChangeHeading();
246     };
247     bool hasHoldPosition() const {
248         return instruction.getHoldPosition();
249     };
250     void setHoldPosition (bool inst) {
251         instruction.setHoldPosition(inst);
252     };
253
254     void setWaitsForId(int id) {
255         waitsForId = id;
256     };
257
258     void setResolveCircularWait()   {
259         instruction.setResolveCircularWait(true);
260     };
261     void clearResolveCircularWait() {
262         instruction.setResolveCircularWait(false);
263     };
264
265     const std::string& getRunway() const {
266         return runway;
267     };
268     //void setCallSign(string clsgn) { callsign = clsgn; };
269     void setAircraft(FGAIAircraft *ref) {
270         aircraft = ref;
271     };
272     void updateState() {
273         state++;
274         allowTransmission=true;
275     };
276     //string getCallSign() { return callsign; };
277     FGAIAircraft *getAircraft() const {
278         return aircraft;
279     };
280     int getTime() const {
281         return timer;
282     };
283     int getLeg() const {
284         return leg;
285     };
286     void setTime(time_t time) {
287         timer = time;
288     };
289
290     bool pushBackAllowed() const;
291     bool allowTransmissions() const {
292         return allowTransmission;
293     };
294     void allowPushBack() { allowPushback =true;};
295     void denyPushBack () { allowPushback = false;};
296     void suppressRepeatedTransmissions () {
297         allowTransmission=false;
298     };
299     void allowRepeatedTransmissions () {
300         allowTransmission=true;
301     };
302     void nextFrequency() {
303         frequencyId++;
304     };
305     int  getNextFrequency() const {
306         return frequencyId;
307     };
308     intVec& getIntentions() {
309         return intentions;
310     };
311     int getCurrentPosition() const {
312         return currentPos;
313     };
314     void setPriority(int p) { priority = p; };
315     int getPriority() const { return priority; };
316 };
317
318 /***********************************************************************
319  * Active runway, a utility class to keep track of which aircraft has
320  * clearance for a given runway.
321  **********************************************************************/
322 class ActiveRunway
323 {
324 private:
325     std::string rwy;
326     int currentlyCleared;
327     double distanceToFinal;
328     TimeVector estimatedArrivalTimes;
329     AircraftVec departureCue;
330
331 public:
332     ActiveRunway(const std::string& r, int cc) {
333         rwy = r;
334         currentlyCleared = cc;
335         distanceToFinal = 6.0 * SG_NM_TO_METER;
336     };
337
338     std::string getRunwayName() {
339         return rwy;
340     };
341     int    getCleared   () {
342         return currentlyCleared;
343     };
344     double getApproachDistance() {
345         return distanceToFinal;
346     };
347     //time_t getEstApproachTime() { return estimatedArrival; };
348
349     //void setEstApproachTime(time_t time) { estimatedArrival = time; };
350     void addToDepartureCue(FGAIAircraft *ac) {
351         departureCue.push_back(ac);
352     };
353     void setCleared(int number) {
354         currentlyCleared = number;
355     };
356     time_t requestTimeSlot(time_t eta);
357
358     int getDepartureCueSize() {
359         return departureCue.size();
360     };
361     FGAIAircraft* getFirstAircraftInDepartureCue() {
362         return departureCue.size() ? *(departureCue.begin()) : NULL;
363     };
364     FGAIAircraft* getFirstOfStatus(int stat);
365     void updateDepartureCue() {
366         departureCue.erase(departureCue.begin());
367     }
368     void printDepartureCue();
369 };
370
371 /**
372  * class FGATCController
373  * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers.
374  *************************************************************************************/
375 class FGATCController
376 {
377 private:
378
379
380 protected:
381     bool initialized;
382     bool available;
383     time_t lastTransmission;
384
385     double dt_count;
386     osg::Group* group;
387
388     std::string formatATCFrequency3_2(int );
389     std::string genTransponderCode(const std::string& fltRules);
390     bool isUserAircraft(FGAIAircraft*);
391
392 public:
393     typedef enum {
394         MSG_ANNOUNCE_ENGINE_START,
395         MSG_REQUEST_ENGINE_START,
396         MSG_PERMIT_ENGINE_START,
397         MSG_DENY_ENGINE_START,
398         MSG_ACKNOWLEDGE_ENGINE_START,
399         MSG_REQUEST_PUSHBACK_CLEARANCE,
400         MSG_PERMIT_PUSHBACK_CLEARANCE,
401         MSG_HOLD_PUSHBACK_CLEARANCE,
402         MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
403         MSG_INITIATE_CONTACT,
404         MSG_ACKNOWLEDGE_INITIATE_CONTACT,
405         MSG_REQUEST_TAXI_CLEARANCE,
406         MSG_ISSUE_TAXI_CLEARANCE,
407         MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
408         MSG_HOLD_POSITION,
409         MSG_ACKNOWLEDGE_HOLD_POSITION,
410         MSG_RESUME_TAXI,
411         MSG_ACKNOWLEDGE_RESUME_TAXI,
412         MSG_REPORT_RUNWAY_HOLD_SHORT,
413         MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT,
414         MSG_SWITCH_TOWER_FREQUENCY,
415         MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY
416     } AtcMsgId;
417
418     typedef enum {
419         ATC_AIR_TO_GROUND,
420         ATC_GROUND_TO_AIR
421     } AtcMsgDir;
422     FGATCController();
423     virtual ~FGATCController();
424     void init();
425
426     virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
427                                   double lat, double lon,
428                                   double hdg, double spd, double alt, double radius, int leg,
429                                   FGAIAircraft *aircraft) = 0;
430     virtual void             signOff(int id) = 0;
431     virtual void             updateAircraftInformation(int id, double lat, double lon,
432             double heading, double speed, double alt, double dt) = 0;
433     virtual bool             hasInstruction(int id) = 0;
434     virtual FGATCInstruction getInstruction(int id) = 0;
435
436     double getDt() {
437         return dt_count;
438     };
439     void   setDt(double dt) {
440         dt_count = dt;
441     };
442     void transmit(FGTrafficRecord *rec, FGAirportDynamics *parent, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
443     std::string getGateName(FGAIAircraft *aircraft);
444     virtual void render(bool) = 0;
445     virtual std::string getName()  = 0;
446
447     virtual void update(double) = 0;
448
449
450 private:
451
452     AtcMsgDir lastTransmissionDirection;
453 };
454
455 /******************************************************************************
456  * class FGTowerControl
457  *****************************************************************************/
458 class FGTowerController : public FGATCController
459 {
460 private:
461     TrafficVector activeTraffic;
462     ActiveRunwayVec activeRunways;
463     FGAirportDynamics *parent;
464
465 public:
466     FGTowerController(FGAirportDynamics *parent);
467     virtual ~FGTowerController() {};
468     virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
469                                   double lat, double lon,
470                                   double hdg, double spd, double alt, double radius, int leg,
471                                   FGAIAircraft *aircraft);
472     virtual void             signOff(int id);
473     virtual void             updateAircraftInformation(int id, double lat, double lon,
474             double heading, double speed, double alt, double dt);
475     virtual bool             hasInstruction(int id);
476     virtual FGATCInstruction getInstruction(int id);
477
478     virtual void render(bool);
479     virtual std::string getName();
480     virtual void update(double dt);
481     bool hasActiveTraffic() {
482         return ! activeTraffic.empty();
483     };
484     TrafficVector &getActiveTraffic() {
485         return activeTraffic;
486     };
487 };
488
489 /******************************************************************************
490  * class FGStartupController
491  * handle
492  *****************************************************************************/
493
494 class FGStartupController : public FGATCController
495 {
496 private:
497     TrafficVector activeTraffic;
498     //ActiveRunwayVec activeRunways;
499     FGAirportDynamics *parent;
500
501 public:
502     FGStartupController(FGAirportDynamics *parent);
503     virtual ~FGStartupController() {};
504     virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
505                                   double lat, double lon,
506                                   double hdg, double spd, double alt, double radius, int leg,
507                                   FGAIAircraft *aircraft);
508     virtual void             signOff(int id);
509     virtual void             updateAircraftInformation(int id, double lat, double lon,
510             double heading, double speed, double alt, double dt);
511     virtual bool             hasInstruction(int id);
512     virtual FGATCInstruction getInstruction(int id);
513
514     virtual void render(bool);
515     virtual std::string getName();
516     virtual void update(double dt);
517
518     bool hasActiveTraffic() {
519         return ! activeTraffic.empty();
520     };
521     TrafficVector &getActiveTraffic() {
522         return activeTraffic;
523     };
524
525     // Hpoefully, we can move this function to the base class, but I need to verify what is needed for the other controllers before doing so.
526     bool checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
527                                 AtcMsgDir msgDir);
528
529 };
530
531 /******************************************************************************
532  * class FGTowerControl
533  *****************************************************************************/
534 class FGApproachController : public FGATCController
535 {
536 private:
537     TrafficVector activeTraffic;
538     ActiveRunwayVec activeRunways;
539     FGAirportDynamics *parent;
540
541 public:
542     FGApproachController(FGAirportDynamics * parent);
543     virtual ~FGApproachController() { };
544     virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
545                                   double lat, double lon,
546                                   double hdg, double spd, double alt, double radius, int leg,
547                                   FGAIAircraft *aircraft);
548     virtual void             signOff(int id);
549     virtual void             updateAircraftInformation(int id, double lat, double lon,
550             double heading, double speed, double alt, double dt);
551     virtual bool             hasInstruction(int id);
552     virtual FGATCInstruction getInstruction(int id);
553
554     virtual void render(bool);
555     virtual std::string getName();
556     virtual void update(double dt);
557
558     ActiveRunway* getRunway(const std::string& name);
559
560     bool hasActiveTraffic() {
561         return ! activeTraffic.empty();
562     };
563     TrafficVector &getActiveTraffic() {
564         return activeTraffic;
565     };
566 };
567
568
569 #endif // _TRAFFIC_CONTROL_HXX