]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atc_mgr.cxx
Avoid compiler warnings.
[flightgear.git] / src / ATC / atc_mgr.cxx
1 /******************************************************************************
2  * atc_mgr.cxx
3  * Written by Durk Talsma, started August 1, 2010.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  *
20  **************************************************************************/
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <iostream>
28
29 #include <simgear/math/SGMath.hxx>
30 #include <Airports/dynamics.hxx>
31 #include <Airports/simple.hxx>
32 #include <Scenery/scenery.hxx>
33 #include "atc_mgr.hxx"
34
35
36 FGATCManager::FGATCManager() {
37     controller = 0;
38     prevController = 0;
39     networkVisible = false;
40     initSucceeded  = false;
41 }
42
43 FGATCManager::~FGATCManager() {
44
45 }
46
47 void FGATCManager::init() {
48     SGSubsystem::init();
49
50     int leg = 0;
51
52     // find a reasonable controller for our user's aircraft..
53     // Let's start by working out the following three scenarios: 
54     // Starting on ground at a parking position
55     // Starting on ground at the runway.
56     // Starting in the Air
57     bool onGround  = fgGetBool("/sim/presets/onground");
58     string runway  = fgGetString("/sim/atc/runway");
59     string airport = fgGetString("/sim/presets/airport-id");
60     string parking = fgGetString("/sim/presets/parkpos");
61     
62
63     // Create an (invisible) AIAircraft represenation of the current
64     // Users, aircraft, that mimicks the user aircraft's behavior.
65     string callsign= fgGetString("/sim/multiplay/callsign");
66     double longitude = fgGetDouble("/position/longitude-deg");
67     double latitude  = fgGetDouble("/position/latitude-deg");
68     double altitude  = fgGetDouble("/position/altitude-ft");
69     double heading   = fgGetDouble("/orientation/heading-deg");
70     double speed     = fgGetDouble("/velocities/groundspeed-kt");
71     double aircraftRadius = 40; // note that this is currently hardcoded to a one-size-fits all JumboJet value. Should change later;
72
73
74     ai_ac.setCallSign ( callsign  );
75     ai_ac.setLongitude( longitude );
76     ai_ac.setLatitude ( latitude  );
77     ai_ac.setAltitude ( altitude  );
78     ai_ac.setPerformance("jet_transport");
79
80     // NEXT UP: Create a traffic Schedule and fill that with appropriate information. This we can use to flight planning.
81     // Note that these are currently only defaults. 
82     FGAISchedule *trafficRef = new FGAISchedule;
83     trafficRef->setFlightType("gate");
84
85     FGScheduledFlight *flight =  new FGScheduledFlight;
86     flight->setDepartureAirport(airport);
87     flight->setArrivalAirport(airport);
88     flight->initializeAirports();
89     flight->setFlightRules("IFR");
90     flight->setCallSign(callsign);
91     
92     trafficRef->assign(flight);
93     FGAIFlightPlan *fp = 0; 
94     ai_ac.setTrafficRef(trafficRef);
95     
96     string flightPlanName = airport + "-" + airport + ".xml";
97     //double cruiseAlt = 100; // Doesn't really matter right now.
98     //double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though...
99     //time_t deptime = 0;        // just make sure how flightplan processing is affected by this...
100
101
102     FGAirport *apt = FGAirport::findByIdent(airport); 
103     if (apt && onGround) {// && !runway.empty()) {
104         FGAirportDynamics* dcs = apt->getDynamics();
105         int park_index = dcs->getNrOfParkings() - 1;
106         //cerr << "found information: " << runway << " " << airport << ": parking = " << parking << endl;
107         fp = new FGAIFlightPlan;
108         while (park_index >= 0 && dcs->getParkingName(park_index) != parking) park_index--;
109         // No valid parking location, so either at the runway or at a random location.
110         if (parking.empty() || (park_index < 0)) {
111             if (!runway.empty()) {
112                 controller = apt->getDynamics()->getTowerController();
113                 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
114                 if (stationFreq > 0)
115                 {
116                     //cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
117                     fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
118                 }
119                 leg = 3;
120                 string fltType = "ga";
121                 fp->setRunway(runway);
122                 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
123                 ai_ac.setTakeOffStatus(2);
124             } else {
125                 // We're on the ground somewhere. Handle this case later.
126             }
127         } else {
128             controller = apt->getDynamics()->getStartupController();
129             int stationFreq = apt->getDynamics()->getGroundFrequency(1);
130             if (stationFreq > 0)
131             {
132                 //cerr << "Setting radio frequency to : " << stationFreq << endl;
133                 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
134             }
135             leg = 1;
136             //double, lat, lon, head; // Unused variables;
137             //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
138             FGParking* parking = dcs->getParking(park_index);
139             aircraftRadius = parking->getRadius();
140             string fltType = parking->getType(); // gate / ramp, ga, etc etc. 
141             string aircraftType; // Unused.
142             string airline;      // Currently used for gate selection, but a fallback mechanism will apply when not specified.
143             fp->setGate(park_index);
144             if (!(fp->createPushBack(&ai_ac,
145                                false, 
146                                apt, 
147                                latitude,
148                                longitude,
149                                aircraftRadius,
150                                fltType,
151                                aircraftType,
152                                airline))) {
153                 controller = 0;
154                 return;
155             }
156
157         }
158         fp->getLastWaypoint()->setName( fp->getLastWaypoint()->getName() + string("legend")); 
159      } else {
160         controller = 0;
161      }
162
163     // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
164     // keep the ATC code happy. 
165     if (fp) {
166         fp->restart();
167         fp->setLeg(leg);
168         ai_ac.SetFlightPlan(fp);
169     }
170     if (controller) {
171         controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
172                                       ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
173                                       aircraftRadius, leg, &ai_ac);
174
175     //dialog.init();
176
177    //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
178    //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
179    //globals->get_scenery()->get_scene_graph()->addChild(node);
180    }
181    initSucceeded = true;
182 }
183
184 void FGATCManager::addController(FGATCController *controller) {
185     activeStations.push_back(controller);
186 }
187
188 void FGATCManager::update ( double time ) {
189     //cerr << "ATC update code is running at time: " << time << endl;
190     // Test code: let my virtual co-pilot handle ATC:
191    
192     
193
194     FGAIFlightPlan *fp = ai_ac.GetFlightPlan();
195         
196     /* test code : find out how the routing develops */
197     if (fp) {
198         int size = fp->getNrOfWayPoints();
199         //cerr << "Setting pos" << pos << " ";
200         //cerr << "setting intentions " ;
201         // This indicates that we have run out of waypoints: Im future versions, the
202         // user should be able to select a new route, but for now just shut down the
203         // system. 
204         if (size < 3) {
205             //cerr << "Shutting down the atc_mgr" << endl;
206             return;
207         }
208         //cerr << "Size of waypoint cue " << size << " ";
209         //for (int i = 0; i < size; i++) {
210         //    int val = fp->getRouteIndex(i);
211             //cerr << fp->getWayPoint(i)->getName() << " ";
212             //if ((val) && (val != pos)) {
213                 //intentions.push_back(val);
214                 //cerr << "[done ] " << endl;
215             //}
216         //}
217         //cerr << "[done ] " << endl;
218     }
219     if (fp) {
220         //cerr << "Currently at leg : " << fp->getLeg() << endl;
221     }
222     double longitude = fgGetDouble("/position/longitude-deg");
223     double latitude  = fgGetDouble("/position/latitude-deg");
224     double heading   = fgGetDouble("/orientation/heading-deg");
225     double speed     = fgGetDouble("/velocities/groundspeed-kt");
226     double altitude  = fgGetDouble("/position/altitude-ft");
227     
228     /*
229     SGGeod me(SGGeod::fromDegM(longitude,
230                                latitude,
231                                altitude));
232     SGGeod wpt1(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(), 
233                                 fp->getWayPoint(1)->getLatitude(),
234                                 fp->getWayPoint(1)->getAltitude()));
235     SGGeod wpt2(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(), 
236                                 fp->getWayPoint(1)->getLatitude(),
237                                 fp->getWayPoint(1)->getAltitude()));
238         
239     double course1, az1, dist1;
240     double course2, az2, dist2;
241     SGGeodesy::inverse(me, wpt1, course1, az1, dist1);
242     
243     cerr << "Bearing to nearest waypoint : " << course1 << " " << dist1 << ". (course " << course2 << ")." <<  endl;
244     */
245     ai_ac.setLatitude(latitude);
246     ai_ac.setLongitude(longitude);
247     ai_ac.setAltitude(altitude);
248     ai_ac.setHeading(heading);
249     ai_ac.setSpeed(speed);
250     ai_ac.update(time);
251     controller = ai_ac.getATCController();
252     FGATCDialogNew::instance()->update(time);
253     if (controller) {
254        //cerr << "name of previous waypoint : " << fp->getPreviousWaypoint()->getName() << endl;
255
256         //cerr << "Running FGATCManager::update()" << endl;
257         //cerr << "Currently under control of " << controller->getName() << endl;
258         controller->updateAircraftInformation(ai_ac.getID(),
259                                               latitude,
260                                               longitude,
261                                               heading,
262                                               speed,
263                                               altitude, time);
264         //string airport = fgGetString("/sim/presets/airport-id");
265         //FGAirport *apt = FGAirport::findByIdent(airport); 
266         // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading. 
267        static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
268             int n = trans_num->getIntValue();
269         if (n == 1) {
270             //cerr << "Toggling ground network visibility " << networkVisible << endl;
271             networkVisible = !networkVisible;
272             trans_num->setIntValue(-1);
273         }
274         if ((controller != prevController) && (prevController)) {
275             prevController->render(false);
276         }
277         controller->render(networkVisible);
278
279         //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
280         prevController = controller;
281    }
282    for (AtcVecIterator atc = activeStations.begin(); atc != activeStations.end(); atc++) {
283        (*atc)->update(time);
284    }
285 }