]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Preliminary support for AI planes from Dave Luff. This works only at
[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 //#include "groundlist.hxx"
26 #include "towerlist.hxx"
27 #include "approachlist.hxx"
28
29 /*
30 // periodic radio station search wrapper
31 static void fgATCSearch( void ) {
32     globals->get_ATC_mgr()->Search();
33 }
34 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
35
36 FGATCMgr::FGATCMgr() {
37     comm1_ident = "";
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 = "";
45     approach_ident = "";
46     last_in_range = false;
47     comm1_atis_valid = false;
48     comm1_tower_valid = false;
49     comm1_approach_valid = false;
50 }
51
52 FGATCMgr::~FGATCMgr() {
53 }
54
55 void FGATCMgr::bind() {
56 }
57
58 void FGATCMgr::unbind() {
59 }
60
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.
72
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;
75     a->lon = -118.034719;
76     a->lat = 34.086114;
77     a->elev = 296.0;
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;
84
85     //a->set_by_AI = true;
86     //a->set_by_comm_search = false;
87
88     airport_atc_map[(string)"KEMT"] = a;
89 }
90
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';
96     if(atc_list.size()) {
97         if(atc_list_itr == atc_list.end()) {
98             atc_list_itr = atc_list.begin();
99         }
100         (*atc_list_itr)->Update();
101         ++atc_list_itr;
102     }
103
104     // Search the tuned frequencies every now and then - this should be done with the event scheduler
105     static int i = 0;
106     if(i == 30) {
107         Search();
108         i = 0;
109     }
110     ++i;
111 }
112 /*
113 // Remove from list only if not needed by the AI system
114 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp) {
115     AirportATC a;
116     if(GetAirportATCDetails((string)id, &a)) {
117         if(a.set_by_AI) {
118             // Don't remove
119             a.set_by_comm_search = false;
120             airport_atc_map[(string)id] = a;
121             return;
122         } else {
123             // remove
124             
125 */    
126
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);
143             break;
144         }  // Note that that can upset where we are in the list but that doesn't really matter
145         ++atc_list_itr;
146     }
147 }
148
149 //DCL - this routine untested so far.
150 // Find in list - return a currently active ATC pointer given ICAO code and type
151 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
152     atc_list_itr = atc_list.begin();
153     while(atc_list_itr != atc_list.end()) {
154         if( (!strcmp((*atc_list_itr)->GetIdent(), id))
155             && ((*atc_list_itr)->GetType() == tp) ) {
156             return(*atc_list_itr);
157         }       // Note that that can upset where we are in the list but that shouldn't really matter
158         ++atc_list_itr;
159     }
160     // We need a fallback position
161     cout << "*** Failed to find FGATC* in FGATCMgr::FindInList - this should not happen!" << endl;
162     return(NULL);
163 }
164
165 // Returns true if the airport is found in the map
166 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
167     if(airport_atc_map.find(icao) != airport_atc_map.end()) {
168         *a = *airport_atc_map[icao];
169         return(true);
170     } else {
171         return(false);
172     }
173 }
174
175
176 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
177 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
178 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
179     AirportATC *a = airport_atc_map[icao];
180     //cout << "a->lon = " << a->lon << '\n';
181     //cout << "a->elev = " << a->elev << '\n';
182     //cout << "a->tower_freq = " << a->tower_freq << '\n';
183     switch(type) {
184     case TOWER:
185         if(a->tower_active) {
186             // Get the pointer from the list
187             return(FindInList(icao.c_str(), type));     // DCL - this untested so far.
188         } else {
189             FGTower* t = new FGTower;
190             if(current_towerlist->query(a->lon, a->lat, a->elev, a->tower_freq, &tower)) {
191                 *t = tower;
192                 atc_list.push_back(t);
193                 a->tower_active = true;
194                 airport_atc_map[icao] = a;
195                 return(t);
196             } else {
197                 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
198             }
199         }
200         break;
201     // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
202     case APPROACH:
203         break;
204     case ATIS:
205         cout << "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer" << endl;
206         break;
207     case GROUND:
208         break;
209     case INVALID:
210         break;
211     case ENROUTE:
212         break;
213     case DEPARTURE:
214         break;
215     }
216
217     cout << "ERROR IN FGATCMgr - reached end of GetATCPointer\n";
218
219     return(NULL);
220 }
221
222
223
224 void FGATCMgr::Search() {
225
226     ////////////////////////////////////////////////////////////////////////
227     // Comm1.
228     ////////////////////////////////////////////////////////////////////////
229     //cout << "In FGATCMgr::Search() - atc_list.size = " << atc_list.size() << '\n';
230
231     comm1_freq = comm1_node->getDoubleValue();
232     //cout << "************* comm1_freq = " << comm1_freq << '\n';
233     double lon = lon_node->getDoubleValue();
234     double lat = lat_node->getDoubleValue();
235     double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
236
237     // Store the comm1_type
238     //atc_type old_comm1_type = comm1_type;
239
240 // We must be able to generalise some of the repetetive searching below!
241
242     //Search for ATIS first
243     if(current_atislist->query(lon, lat, elev, comm1_freq, &atis)) {
244         //cout << "atis found in radiostack search !!!!" << endl;
245         //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
246         //cout << "comm1_type " << comm1_type << '\n';
247         comm1_atis_ident = atis.GetIdent();
248         comm1_atis_valid = true;
249         if(last_comm1_atis_ident != comm1_atis_ident) {
250             if(last_comm1_atis_ident != "") {
251                 RemoveFromList(last_comm1_atis_ident, ATIS);
252             }
253             last_comm1_atis_ident = comm1_atis_ident;
254             //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
255             comm1_type = ATIS;
256             comm1_elev = atis.get_elev();
257             comm1_range = FG_ATIS_DEFAULT_RANGE;
258             comm1_effective_range = comm1_range;
259             comm1_x = atis.get_x();
260             comm1_y = atis.get_y();
261             comm1_z = atis.get_z();
262             FGATIS* a = new FGATIS;
263             *a = atis;
264             a->SetDisplay();
265             atc_list.push_back(a);
266             //cout << "Found a new atis station in range" << endl;
267             //cout << " id = " << atis.GetIdent() << endl;
268             return;  //This rather assumes that we never have more than one type of station in range.
269         }
270     } else {
271         if(comm1_atis_valid) {
272             //cout << "Removing ATIS " << comm1_atis_ident << " from list\n";
273             RemoveFromList(comm1_atis_ident, ATIS);
274             comm1_atis_valid = false;
275             if(comm1_type == ATIS) {
276                 comm1_type = INVALID;
277             }
278             comm1_atis_ident = "";
279             //comm1_trans_ident = "";
280             last_comm1_atis_ident = "";
281         }
282         //cout << "not picking up atis" << endl;
283     }
284
285     //Next search for tower
286     //cout << "comm1_freq = " << comm1_freq << '\n';
287     if(current_towerlist->query(lon, lat, elev, comm1_freq, &tower)) {
288         //cout << "tower found in radiostack search !!!!" << endl;
289         comm1_tower_ident = tower.GetIdent();
290         //cout << "comm1_tower_ident = " << comm1_tower_ident << '\n';
291         comm1_tower_valid = true;
292         if(last_comm1_tower_ident != comm1_tower_ident) {
293             if(last_comm1_tower_ident != "") {
294                 RemoveFromList(last_comm1_tower_ident, TOWER);
295             }
296             last_comm1_tower_ident = comm1_tower_ident;
297             comm1_type = TOWER;
298             comm1_elev = tower.get_elev();
299             comm1_range = FG_TOWER_DEFAULT_RANGE;
300             comm1_effective_range = comm1_range;
301             comm1_x = tower.get_x();
302             comm1_y = tower.get_y();
303             comm1_z = tower.get_z();
304             FGTower* t = new FGTower;
305             *t = tower;
306             t->SetDisplay();
307             atc_list.push_back(t);
308             //cout << "Found a new tower station in range" << endl;
309             //cout << " id = " << tower.GetIdent() << endl;
310             return;  //This rather assumes that we never have more than one type of station in range.
311         }
312     } else {
313         if(comm1_tower_valid) {
314             //cout << "removing tower\n";
315             RemoveFromList(comm1_tower_ident, TOWER);
316             //comm1_valid = false;
317             if(comm1_type == TOWER) {
318                 comm1_type = INVALID;   // Only invalidate if we haven't switched it to something else
319             }
320             comm1_tower_valid = false;
321             comm1_tower_ident = "";
322             last_comm1_tower_ident = "";
323             //comm1_ident = "";
324             //comm1_trans_ident = "";
325             //last_comm1_ident = "";
326         }
327         //cout << "not picking up tower" << endl;
328     }
329 /*
330     //Next search for Ground control
331     if(current_groundlist->query(lon, lat, elev, comm1_freq, &ground)) {
332         //cout << "Ground Control found in radiostack search !!!!" << endl;
333         comm1_ident = ground.GetIdent();
334         comm1_valid = true;
335         if((last_comm1_ident != comm1_ident) || (comm1_type != GROUND)) {
336             if(last_comm1_ident != "") {
337                 RemoveFromList(last_comm1_ident, GROUND);
338             }
339             last_comm1_ident = comm1_ident;
340             comm1_type = GROUND;
341             comm1_elev = ground.get_elev();
342             comm1_range = FG_GROUND_DEFAULT_RANGE;
343             comm1_effective_range = comm1_range;
344             comm1_x = ground.get_x();
345             comm1_y = ground.get_y();
346             comm1_z = ground.get_z();
347             FGGround* g = new FGGround;
348             *g = ground;
349             g->SetDisplay();
350             atc_list.push_back(g);
351             // For now we will automatically make contact with ground when the radio is tuned.
352             // This rather assumes that the user tunes the radio at the appropriate place
353             // (ie. having just turned off the runway) and only uses ground control on arrival
354             // but its a start!
355             g->NewArrival(current_plane);
356             //cout << "Found a new ground station in range" << endl;
357             //cout << " id = " << ground.GetIdent() << endl;
358             return;  //This rather assumes that we never have more than one type of station in range.
359         }
360     } else {
361         if((comm1_valid) && (comm1_type == GROUND)) {
362             RemoveFromList(comm1_ident, GROUND);
363             comm1_valid = false;
364             comm1_type = INVALID;
365             comm1_ident = "";
366             //comm1_trans_ident = "";
367             last_comm1_ident = "";
368         }
369         //cout << "not picking up ground control" << endl;
370     }
371 */
372 // ================================================================================
373 // Search for Approach stations
374 // ================================================================================
375     // init number of approach stations reachable by plane
376     int  num_app = 0;
377
378     // search stations in range
379     current_approachlist->query_bck(lon, lat, elev, approaches, max_app, num_app);
380     if (num_app != 0) {
381       //cout << num_app << " approaches found in radiostack search !!!!" << endl;
382
383       for ( int i=0; i<num_app; i++ ) {
384         bool new_app = true;
385         approach_ident = approaches[i].GetIdent();
386
387         // check if station already exists on ATC stack
388         atc_list_itr = atc_list.begin();
389         while(atc_list_itr != atc_list.end()) {
390           //cout << "ATC list: " << (*atc_list_itr)->GetIdent() << endl;
391           if((!strcmp((*atc_list_itr)->GetIdent(), approach_ident))
392              && ((*atc_list_itr)->GetType() == APPROACH) ) {
393             new_app = false;
394             string pid = "Player";
395             (*atc_list_itr)->AddPlane(pid);
396             (*atc_list_itr)->Update();
397             break;
398           }
399           ++atc_list_itr;
400         }
401         // generate new Approach on ATC stack
402         if (new_app) {
403           FGApproach* a = new FGApproach;
404           *a = approaches[i];
405           string pid = "Player";
406           a->AddPlane(pid);
407           a->Update();
408           a->SetDisplay();
409           atc_list.push_back(a);
410           //cout << "Found a new approach station in range: Id = " 
411           //     << approaches[i].GetIdent() << endl;
412         }
413       }
414     }
415
416     // remove planes which are out of range
417     atc_list_itr = atc_list.begin();
418     while(atc_list_itr != atc_list.end()) {
419       if((*atc_list_itr)->GetType() == APPROACH ) {
420         int np = (*atc_list_itr)->RemovePlane();
421         // if approach has no planes left remove it from ATC list
422         if ( np == 0) {
423           (*atc_list_itr)->SetNoDisplay();
424           (*atc_list_itr)->Update();
425           delete (*atc_list_itr);
426           atc_list_itr = atc_list.erase(atc_list_itr);
427           break;     // the other stations will be checked next time
428         }
429       }
430       ++atc_list_itr;
431     }
432 }