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