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