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