1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
3 // Written by David Luff, started February 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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //#include <Time/event.hxx>
24 #include "atislist.hxx"
25 //#include "groundlist.hxx"
26 #include "towerlist.hxx"
27 #include "approachlist.hxx"
30 // periodic radio station search wrapper
31 static void fgATCSearch( void ) {
32 globals->get_ATC_mgr()->Search();
34 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
36 FGATCMgr::FGATCMgr() {
38 comm1_atis_ident = "";
39 comm1_tower_ident = "";
40 comm1_approach_ident = "";
41 last_comm1_ident = "";
42 last_comm1_atis_ident = "";
43 last_comm1_tower_ident = "";
44 last_comm1_approach_ident = "";
46 last_in_range = false;
47 comm1_atis_valid = false;
48 comm1_tower_valid = false;
49 comm1_approach_valid = false;
52 FGATCMgr::~FGATCMgr() {
55 void FGATCMgr::bind() {
58 void FGATCMgr::unbind() {
61 void FGATCMgr::init() {
62 comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
63 comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
64 lon_node = fgGetNode("/position/longitude-deg", true);
65 lat_node = fgGetNode("/position/latitude-deg", true);
66 elev_node = fgGetNode("/position/altitude-ft", true);
67 atc_list_itr = atc_list.begin();
68 // Search for connected ATC stations once per 0.8 seconds or so
69 // global_events.Register( "fgATCSearch()", fgATCSearch,
70 // fgEVENT::FG_EVENT_READY, 800);
71 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
73 // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
74 AirportATC *a = new AirportATC;
78 a->atis_freq = 118.75;
79 a->atis_active = false;
80 a->tower_freq = 121.2;
81 a->tower_active = false;
82 a->ground_freq = 125.9;
83 a->ground_active = false;
85 //a->set_by_AI = true;
86 //a->set_by_comm_search = false;
88 airport_atc_map[(string)"KEMT"] = a;
91 void FGATCMgr::update(double dt) {
92 //Traverse the list of active stations.
93 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
94 //Eventually we should only update every so many steps.
95 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
97 if(atc_list_itr == atc_list.end()) {
98 atc_list_itr = atc_list.begin();
100 (*atc_list_itr)->Update();
104 // Search the tuned frequencies every now and then - this should be done with the event scheduler
113 // Remove from list only if not needed by the AI system
114 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp) {
116 if(GetAirportATCDetails((string)id, &a)) {
119 a.set_by_comm_search = false;
120 airport_atc_map[(string)id] = a;
127 // Remove from list - should only be called from above or similar
128 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
129 //cout << "Requested type = " << tp << '\n';
130 //cout << "id = " << id << '\n';
131 atc_list_itr = atc_list.begin();
132 while(atc_list_itr != atc_list.end()) {
133 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
134 //cout << "Ident = " << (*atc_list_itr)->GetIdent() << '\n';
135 if( (!strcmp((*atc_list_itr)->GetIdent(), id))
136 && ((*atc_list_itr)->GetType() == tp) ) {
137 //Before removing it stop it transmitting!!
138 //cout << "OBLITERATING FROM LIST!!!\n";
139 (*atc_list_itr)->SetNoDisplay();
140 (*atc_list_itr)->Update();
141 delete (*atc_list_itr);
142 atc_list_itr = atc_list.erase(atc_list_itr);
144 } // Note that that can upset where we are in the list but that doesn't really matter
149 // Returns true if the airport is found in the map
150 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
151 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
152 *a = *airport_atc_map[icao];
160 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
161 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
162 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
163 AirportATC *a = airport_atc_map[icao];
164 //cout << "a->lon = " << a->lon << '\n';
165 //cout << "a->elev = " << a->elev << '\n';
166 //cout << "a->tower_freq = " << a->tower_freq << '\n';
169 if(a->tower_active) {
170 // Get the pointer from the list
172 FGTower* t = new FGTower;
173 if(current_towerlist->query(a->lon, a->lat, a->elev, a->tower_freq, &tower)) {
175 atc_list.push_back(t);
176 a->tower_active = true;
177 airport_atc_map[icao] = a;
180 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
184 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
188 cout << "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer" << endl;
200 cout << "ERROR IN FGATCMgr - reached end of GetATCPointer\n";
207 void FGATCMgr::Search() {
209 ////////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////////
212 //cout << "In FGATCMgr::Search() - atc_list.size = " << atc_list.size() << '\n';
214 comm1_freq = comm1_node->getDoubleValue();
215 //cout << "************* comm1_freq = " << comm1_freq << '\n';
216 double lon = lon_node->getDoubleValue();
217 double lat = lat_node->getDoubleValue();
218 double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
220 // Store the comm1_type
221 //atc_type old_comm1_type = comm1_type;
223 // We must be able to generalise some of the repetetive searching below!
225 //Search for ATIS first
226 if(current_atislist->query(lon, lat, elev, comm1_freq, &atis)) {
227 //cout << "atis found in radiostack search !!!!" << endl;
228 //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
229 //cout << "comm1_type " << comm1_type << '\n';
230 comm1_atis_ident = atis.GetIdent();
231 comm1_atis_valid = true;
232 if(last_comm1_atis_ident != comm1_atis_ident) {
233 if(last_comm1_atis_ident != "") {
234 RemoveFromList(last_comm1_atis_ident, ATIS);
236 last_comm1_atis_ident = comm1_atis_ident;
237 //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
239 comm1_elev = atis.get_elev();
240 comm1_range = FG_ATIS_DEFAULT_RANGE;
241 comm1_effective_range = comm1_range;
242 comm1_x = atis.get_x();
243 comm1_y = atis.get_y();
244 comm1_z = atis.get_z();
245 FGATIS* a = new FGATIS;
248 atc_list.push_back(a);
249 //cout << "Found a new atis station in range" << endl;
250 //cout << " id = " << atis.GetIdent() << endl;
251 return; //This rather assumes that we never have more than one type of station in range.
254 if(comm1_atis_valid) {
255 //cout << "Removing ATIS " << comm1_atis_ident << " from list\n";
256 RemoveFromList(comm1_atis_ident, ATIS);
257 comm1_atis_valid = false;
258 if(comm1_type == ATIS) {
259 comm1_type = INVALID;
261 comm1_atis_ident = "";
262 //comm1_trans_ident = "";
263 last_comm1_atis_ident = "";
265 //cout << "not picking up atis" << endl;
268 //Next search for tower
269 //cout << "comm1_freq = " << comm1_freq << '\n';
270 if(current_towerlist->query(lon, lat, elev, comm1_freq, &tower)) {
271 //cout << "tower found in radiostack search !!!!" << endl;
272 comm1_tower_ident = tower.GetIdent();
273 //cout << "comm1_tower_ident = " << comm1_tower_ident << '\n';
274 comm1_tower_valid = true;
275 if(last_comm1_tower_ident != comm1_tower_ident) {
276 if(last_comm1_tower_ident != "") {
277 RemoveFromList(last_comm1_tower_ident, TOWER);
279 last_comm1_tower_ident = comm1_tower_ident;
281 comm1_elev = tower.get_elev();
282 comm1_range = FG_TOWER_DEFAULT_RANGE;
283 comm1_effective_range = comm1_range;
284 comm1_x = tower.get_x();
285 comm1_y = tower.get_y();
286 comm1_z = tower.get_z();
287 FGTower* t = new FGTower;
290 atc_list.push_back(t);
291 //cout << "Found a new tower station in range" << endl;
292 //cout << " id = " << tower.GetIdent() << endl;
293 return; //This rather assumes that we never have more than one type of station in range.
296 if(comm1_tower_valid) {
297 //cout << "removing tower\n";
298 RemoveFromList(comm1_tower_ident, TOWER);
299 //comm1_valid = false;
300 if(comm1_type == TOWER) {
301 comm1_type = INVALID; // Only invalidate if we haven't switched it to something else
303 comm1_tower_valid = false;
304 comm1_tower_ident = "";
305 last_comm1_tower_ident = "";
307 //comm1_trans_ident = "";
308 //last_comm1_ident = "";
310 //cout << "not picking up tower" << endl;
313 //Next search for Ground control
314 if(current_groundlist->query(lon, lat, elev, comm1_freq, &ground)) {
315 //cout << "Ground Control found in radiostack search !!!!" << endl;
316 comm1_ident = ground.GetIdent();
318 if((last_comm1_ident != comm1_ident) || (comm1_type != GROUND)) {
319 if(last_comm1_ident != "") {
320 RemoveFromList(last_comm1_ident, GROUND);
322 last_comm1_ident = comm1_ident;
324 comm1_elev = ground.get_elev();
325 comm1_range = FG_GROUND_DEFAULT_RANGE;
326 comm1_effective_range = comm1_range;
327 comm1_x = ground.get_x();
328 comm1_y = ground.get_y();
329 comm1_z = ground.get_z();
330 FGGround* g = new FGGround;
333 atc_list.push_back(g);
334 // For now we will automatically make contact with ground when the radio is tuned.
335 // This rather assumes that the user tunes the radio at the appropriate place
336 // (ie. having just turned off the runway) and only uses ground control on arrival
338 g->NewArrival(current_plane);
339 //cout << "Found a new ground station in range" << endl;
340 //cout << " id = " << ground.GetIdent() << endl;
341 return; //This rather assumes that we never have more than one type of station in range.
344 if((comm1_valid) && (comm1_type == GROUND)) {
345 RemoveFromList(comm1_ident, GROUND);
347 comm1_type = INVALID;
349 //comm1_trans_ident = "";
350 last_comm1_ident = "";
352 //cout << "not picking up ground control" << endl;
355 // ================================================================================
356 // Search for Approach stations
357 // ================================================================================
358 // init number of approach stations reachable by plane
361 // search stations in range
362 current_approachlist->query_bck(lon, lat, elev, approaches, max_app, num_app);
364 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
366 for ( int i=0; i<num_app; i++ ) {
368 approach_ident = approaches[i].GetIdent();
370 // check if station already exists on ATC stack
371 atc_list_itr = atc_list.begin();
372 while(atc_list_itr != atc_list.end()) {
373 //cout << "ATC list: " << (*atc_list_itr)->GetIdent() << endl;
374 if((!strcmp((*atc_list_itr)->GetIdent(), approach_ident))
375 && ((*atc_list_itr)->GetType() == APPROACH) ) {
377 string pid = "Player";
378 (*atc_list_itr)->AddPlane(pid);
379 (*atc_list_itr)->Update();
384 // generate new Approach on ATC stack
386 FGApproach* a = new FGApproach;
388 string pid = "Player";
392 atc_list.push_back(a);
393 //cout << "Found a new approach station in range: Id = "
394 // << approaches[i].GetIdent() << endl;
399 // remove planes which are out of range
400 atc_list_itr = atc_list.begin();
401 while(atc_list_itr != atc_list.end()) {
402 if((*atc_list_itr)->GetType() == APPROACH ) {
403 int np = (*atc_list_itr)->RemovePlane();
404 // if approach has no planes left remove it from ATC list
406 (*atc_list_itr)->SetNoDisplay();
407 (*atc_list_itr)->Update();
408 delete (*atc_list_itr);
409 atc_list_itr = atc_list.erase(atc_list_itr);
410 break; // the other stations will be checked next time