]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atc_mgr.cxx
Overhaul the ground-net / parking code.
[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         fp = new FGAIFlightPlan;
105         ParkingAssignment pk(dcs->getParkingByName(parking));
106       
107         // No valid parking location, so either at the runway or at a random location.
108         if (!pk.isValid()) {
109             if (!runway.empty()) {
110                 controller = apt->getDynamics()->getTowerController();
111                 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
112                 if (stationFreq > 0)
113                 {
114                     //cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
115                     fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
116                 }
117                 leg = 3;
118                 string fltType = "ga";
119                 fp->setRunway(runway);
120                 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
121                 ai_ac.setTakeOffStatus(2);
122             } else {
123                 // We're on the ground somewhere. Handle this case later.
124             }
125         } else {
126             controller = apt->getDynamics()->getStartupController();
127             int stationFreq = apt->getDynamics()->getGroundFrequency(1);
128             if (stationFreq > 0)
129             {
130                 //cerr << "Setting radio frequency to : " << stationFreq << endl;
131                 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
132             }
133             leg = 1;
134             //double, lat, lon, head; // Unused variables;
135             //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
136             aircraftRadius = pk.parking()->getRadius();
137             string fltType = pk.parking()->getType(); // gate / ramp, ga, etc etc.
138             string aircraftType; // Unused.
139             string airline;      // Currently used for gate selection, but a fallback mechanism will apply when not specified.
140             fp->setGate(pk);
141             if (!(fp->createPushBack(&ai_ac,
142                                false, 
143                                apt, 
144                                aircraftRadius,
145                                fltType,
146                                aircraftType,
147                                airline))) {
148                 controller = 0;
149                 return;
150             }
151
152         }
153         fp->getLastWaypoint()->setName( fp->getLastWaypoint()->getName() + string("legend")); 
154      } else {
155         controller = 0;
156      }
157
158     // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
159     // keep the ATC code happy. 
160     if (fp) {
161         fp->restart();
162         fp->setLeg(leg);
163         ai_ac.SetFlightPlan(fp);
164     }
165     if (controller) {
166         controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
167                                       ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
168                                       aircraftRadius, leg, &ai_ac);
169
170     //dialog.init();
171
172    //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
173    //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
174    //globals->get_scenery()->get_scene_graph()->addChild(node);
175    }
176    initSucceeded = true;
177 }
178
179 void FGATCManager::addController(FGATCController *controller) {
180     activeStations.push_back(controller);
181 }
182
183 void FGATCManager::update ( double time ) {
184     //cerr << "ATC update code is running at time: " << time << endl;
185     // Test code: let my virtual co-pilot handle ATC:
186    
187     
188
189     FGAIFlightPlan *fp = ai_ac.GetFlightPlan();
190         
191     /* test code : find out how the routing develops */
192     if (fp) {
193         int size = fp->getNrOfWayPoints();
194         //cerr << "Setting pos" << pos << " ";
195         //cerr << "setting intentions " ;
196         // This indicates that we have run out of waypoints: Im future versions, the
197         // user should be able to select a new route, but for now just shut down the
198         // system. 
199         if (size < 3) {
200             //cerr << "Shutting down the atc_mgr" << endl;
201             return;
202         }
203         //cerr << "Size of waypoint cue " << size << " ";
204         //for (int i = 0; i < size; i++) {
205         //    int val = fp->getRouteIndex(i);
206             //cerr << fp->getWayPoint(i)->getName() << " ";
207             //if ((val) && (val != pos)) {
208                 //intentions.push_back(val);
209                 //cerr << "[done ] " << endl;
210             //}
211         //}
212         //cerr << "[done ] " << endl;
213     }
214     if (fp) {
215         //cerr << "Currently at leg : " << fp->getLeg() << endl;
216     }
217     double longitude = fgGetDouble("/position/longitude-deg");
218     double latitude  = fgGetDouble("/position/latitude-deg");
219     double heading   = fgGetDouble("/orientation/heading-deg");
220     double speed     = fgGetDouble("/velocities/groundspeed-kt");
221     double altitude  = fgGetDouble("/position/altitude-ft");
222     
223     /*
224     SGGeod me(SGGeod::fromDegM(longitude,
225                                latitude,
226                                altitude));
227     SGGeod wpt1(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(), 
228                                 fp->getWayPoint(1)->getLatitude(),
229                                 fp->getWayPoint(1)->getAltitude()));
230     SGGeod wpt2(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(), 
231                                 fp->getWayPoint(1)->getLatitude(),
232                                 fp->getWayPoint(1)->getAltitude()));
233         
234     double course1, az1, dist1;
235     double course2, az2, dist2;
236     SGGeodesy::inverse(me, wpt1, course1, az1, dist1);
237     
238     cerr << "Bearing to nearest waypoint : " << course1 << " " << dist1 << ". (course " << course2 << ")." <<  endl;
239     */
240     ai_ac.setLatitude(latitude);
241     ai_ac.setLongitude(longitude);
242     ai_ac.setAltitude(altitude);
243     ai_ac.setHeading(heading);
244     ai_ac.setSpeed(speed);
245     ai_ac.update(time);
246     controller = ai_ac.getATCController();
247     FGATCDialogNew::instance()->update(time);
248     if (controller) {
249        //cerr << "name of previous waypoint : " << fp->getPreviousWaypoint()->getName() << endl;
250
251         //cerr << "Running FGATCManager::update()" << endl;
252         //cerr << "Currently under control of " << controller->getName() << endl;
253         controller->updateAircraftInformation(ai_ac.getID(),
254                                               latitude,
255                                               longitude,
256                                               heading,
257                                               speed,
258                                               altitude, time);
259         //string airport = fgGetString("/sim/presets/airport-id");
260         //FGAirport *apt = FGAirport::findByIdent(airport); 
261         // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading. 
262        static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
263             int n = trans_num->getIntValue();
264         if (n == 1) {
265             //cerr << "Toggling ground network visibility " << networkVisible << endl;
266             networkVisible = !networkVisible;
267             trans_num->setIntValue(-1);
268         }
269         if ((controller != prevController) && (prevController)) {
270             prevController->render(false);
271         }
272         controller->render(networkVisible);
273
274         //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
275         prevController = controller;
276    }
277    for (AtcVecIterator atc = activeStations.begin(); atc != activeStations.end(); atc++) {
278        (*atc)->update(time);
279    }
280 }