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