]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Attached is a reasonably large patch to add a proper ATC
[flightgear.git] / src / ATC / ATCmgr.cxx
1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
2 //
3 // Written by David Luff, started February 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 //#include <Time/event.hxx>
22
23 #include "ATCmgr.hxx"
24 #include "atislist.hxx"
25
26 /*
27 // periodic radio station search wrapper
28 static void fgATCSearch( void ) {
29     globals->get_ATC_mgr()->Search();
30 }
31 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
32
33 FGATCMgr::FGATCMgr() {
34 }
35
36 FGATCMgr::~FGATCMgr() {
37 }
38
39 void FGATCMgr::bind() {
40 }
41
42 void FGATCMgr::unbind() {
43 }
44
45 void FGATCMgr::init() {
46     comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
47     comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
48     lon_node = fgGetNode("/position/longitude-deg", true);
49     lat_node = fgGetNode("/position/latitude-deg", true);
50     elev_node = fgGetNode("/position/altitude-ft", true);
51     atc_list_itr = atc_list.begin();
52     // Search for connected ATC stations once per 0.8 seconds or so
53     // global_events.Register( "fgATCSearch()", fgATCSearch,
54     //              fgEVENT::FG_EVENT_READY, 800);
55     // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
56 }
57
58 void FGATCMgr::update(int dt) {
59     //Traverse the list of active stations.
60     //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
61     //Eventually we should only update every so many steps.
62     if(atc_list.size()) {
63         if(atc_list_itr == atc_list.end()) {
64             atc_list_itr = atc_list.begin();
65         }
66         (*atc_list_itr)->Update();
67         ++atc_list_itr;
68     }
69
70     // Search the tuned frequencies every now and then - this should be done with the event scheduler
71     static int i = 0;
72     if(i == 30) {
73         Search();
74         i = 0;
75     }
76     ++i;
77 }
78
79 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
80     atc_list_itr = atc_list.begin();
81     while(atc_list_itr != atc_list.end()) {
82         if( (!strcmp((*atc_list_itr)->GetIdent(), id))
83             && ((*atc_list_itr)->GetType() == tp) ) {
84             //Before removing it stop it transmitting!!
85             (*atc_list_itr)->SetNoDisplay();
86             (*atc_list_itr)->Update();
87             delete (*atc_list_itr);
88             atc_list_itr = atc_list.erase(atc_list_itr);
89             break;
90         }  // Note that that can upset where we are in the list but that doesn't really matter
91         ++atc_list_itr;
92     }
93 }
94
95 void FGATCMgr::Search() {
96
97     ////////////////////////////////////////////////////////////////////////
98     // Comm1.
99     ////////////////////////////////////////////////////////////////////////
100
101     comm1_freq = comm1_node->getDoubleValue();
102     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
103     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
104     double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
105
106     //Search for ATIS first
107     if(current_atislist->query(lon, lat, elev, comm1_freq, &atis)) {
108         //cout << "atis found in radiostack search !!!!" << endl;
109         comm1_ident = atis.GetIdent();
110         comm1_valid = true;
111         if(last_comm1_ident != comm1_ident) {
112             if(last_comm1_ident != "") {
113                 RemoveFromList(last_comm1_ident, ATIS);
114             }
115             last_comm1_ident = comm1_ident;
116             comm1_elev = atis.get_elev();
117             comm1_range = FG_ATIS_DEFAULT_RANGE;
118             comm1_effective_range = comm1_range;
119             comm1_x = atis.get_x();
120             comm1_y = atis.get_y();
121             comm1_z = atis.get_z();
122             FGATIS* a = new FGATIS;
123             *a = atis;
124             a->SetDisplay();
125             atc_list.push_back(a);
126             //cout << "Found a new atis station in range" << endl;
127             //cout << " id = " << atis.GetIdent() << endl;
128         }
129     } else {
130         if(comm1_valid) {
131             RemoveFromList(comm1_ident, ATIS);
132             comm1_valid = false;
133             comm1_ident = "";
134             //comm1_trans_ident = "";
135             last_comm1_ident = "";
136         }
137         //cout << "not picking up atis" << endl;
138     }
139 }