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