]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atc_mgr.cxx
Fix flightplan initialization issue.
[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     networkVisible = false;
38 }
39
40 FGATCManager::~FGATCManager() {
41
42 }
43
44 void FGATCManager::init() {
45     SGSubsystem::init();
46     currentATCDialog = new FGATCDialogNew;
47     currentATCDialog->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) {
103         FGAirportDynamics* dcs = apt->getDynamics();
104         int park_index = dcs->getNrOfParkings() - 1;
105         cerr << "found information: " << runway << " " << airport << ": parking = " << parking << endl;
106         if (onGround) {
107             fp = new FGAIFlightPlan;
108             while (park_index >= 0 && dcs->getParkingName(park_index) != parking) park_index--;
109                 if (park_index < 0) {
110                       SG_LOG( SG_GENERAL, SG_ALERT,
111                             "Failed to find parking position " << parking <<
112                             " at airport " << airport );
113                 }
114             if (parking.empty() || (park_index < 0)) {
115                 controller = apt->getDynamics()->getTowerController();
116                 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
117                 cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
118                 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
119                 leg = 4;
120                 string fltType = "ga";
121                 fp->setRunway(runway);
122                 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
123             } else {
124                 controller = apt->getDynamics()->getStartupController();
125                 int stationFreq = apt->getDynamics()->getGroundFrequency(2);
126                 cerr << "Setting radio frequency to : " << stationFreq << endl;
127                 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
128                 leg = 2;
129                 //double, lat, lon, head; // Unused variables;
130                 //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
131                 FGParking* parking = dcs->getParking(park_index);
132                 aircraftRadius = parking->getRadius();
133                 string fltType = parking->getType(); // gate / ramp, ga, etc etc. 
134                 string aircraftType; // Unused.
135                 string airline;      // Currently used for gate selection, but a fallback mechanism will apply when not specified.
136                 fp->setGate(park_index);
137                 fp->createPushBack(&ai_ac,
138                                    false, 
139                                    apt, 
140                                    latitude,
141                                    longitude,
142                                    aircraftRadius,
143                                    fltType,
144                                    aircraftType,
145                                    airline);
146             } 
147         }
148      } else {
149         controller = 0;
150      }
151
152     // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
153     // keep the ATC code happy. 
154     
155     if (fp) {
156         fp->restart();
157         fp->setLeg(leg);
158         ai_ac.SetFlightPlan(fp);
159     }
160     if (controller) {
161         controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->routeIndex,
162                                       ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
163                                       aircraftRadius, leg, &ai_ac);
164
165     //dialog.init();
166
167    //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
168    //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
169    //globals->get_scenery()->get_scene_graph()->addChild(node);
170    }
171 }
172
173 void FGATCManager::addController(FGATCController *controller) {
174     activeStations.push_back(controller);
175 }
176
177 void FGATCManager::update ( double time ) {
178     //cerr << "ATC update code is running at time: " << time << endl;
179     // Test code: let my virtual co-pilot handle ATC:
180    
181     
182
183     FGAIFlightPlan *fp = ai_ac.GetFlightPlan();
184         
185     /* test code : find out how the routing develops */
186     if (fp) {
187         int size = fp->getNrOfWayPoints();
188         //cerr << "Setting pos" << pos << " ";
189         //cerr << "setting intentions " ;
190         for (int i = 0; i < size; i++) {
191             int val = fp->getRouteIndex(i);
192             //cerr << val << " ";
193             //if ((val) && (val != pos)) {
194                 //intentions.push_back(val);
195                 //cerr << "[done ] " << endl;
196             //}
197         }
198     }
199     //cerr << "[done ] " << endl;
200     double longitude = fgGetDouble("/position/longitude-deg");
201     double latitude  = fgGetDouble("/position/latitude-deg");
202     double heading   = fgGetDouble("/orientation/heading-deg");
203     double speed     = fgGetDouble("/velocities/groundspeed-kt");
204     double altitude  = fgGetDouble("/position/altitude-ft");
205     ai_ac.setLatitude(latitude);
206     ai_ac.setLongitude(longitude);
207     ai_ac.setAltitude(altitude);
208     ai_ac.setHeading(heading);
209     ai_ac.setSpeed(speed);
210     controller = ai_ac.getATCController();
211     ai_ac.update(time);
212     currentATCDialog->update(time);
213     if (controller) {
214        
215
216         //cerr << "Running FGATCManager::update()" << endl;
217         controller->updateAircraftInformation(ai_ac.getID(),
218                                               latitude,
219                                               longitude,
220                                               heading,
221                                               speed,
222                                               altitude, time);
223         //string airport = fgGetString("/sim/presets/airport-id");
224         //FGAirport *apt = FGAirport::findByIdent(airport); 
225         // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading. 
226        static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
227             int n = trans_num->getIntValue();
228         if (n == 1) {
229             cerr << "Toggling ground network visibility " << networkVisible << endl;
230             networkVisible = !networkVisible;
231             trans_num->setIntValue(-1);
232         }
233         controller->render(networkVisible);
234
235         //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
236    }
237    //globals->get_scenery()->get_scene_graph()->addChild(node);
238 }