1 /******************************************************************************
3 * Written by Durk Talsma, started August 1, 2010.
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.
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.
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.
20 **************************************************************************/
29 #include <Airports/dynamics.hxx>
30 #include <Airports/simple.hxx>
31 #include <Scenery/scenery.hxx>
32 #include "atc_mgr.hxx"
35 FGATCManager::FGATCManager() {
38 networkVisible = false;
39 initSucceeded = false;
42 FGATCManager::~FGATCManager() {
46 void FGATCManager::init() {
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");
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;
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");
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");
84 FGScheduledFlight *flight = new FGScheduledFlight;
85 flight->setDepartureAirport(airport);
86 flight->setArrivalAirport(airport);
87 flight->initializeAirports();
88 flight->setFlightRules("IFR");
89 flight->setCallSign(callsign);
91 trafficRef->assign(flight);
92 FGAIFlightPlan *fp = 0;
93 ai_ac.setTrafficRef(trafficRef);
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...
101 FGAirport *apt = FGAirport::findByIdent(airport);
102 if (apt && onGround) {// && !runway.empty()) {
103 FGAirportDynamics* dcs = apt->getDynamics();
104 int park_index = dcs->getNrOfParkings() - 1;
105 //cerr << "found information: " << runway << " " << airport << ": parking = " << parking << endl;
106 fp = new FGAIFlightPlan;
107 while (park_index >= 0 && dcs->getParkingName(park_index) != parking) park_index--;
108 // No valid parking location, so either at the runway or at a random location.
109 if (parking.empty() || (park_index < 0)) {
110 if (!runway.empty()) {
111 controller = apt->getDynamics()->getTowerController();
112 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
115 //cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
116 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
119 string fltType = "ga";
120 fp->setRunway(runway);
121 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
122 ai_ac.setTakeOffStatus(2);
124 // We're on the ground somewhere. Handle this case later.
127 controller = apt->getDynamics()->getStartupController();
128 int stationFreq = apt->getDynamics()->getGroundFrequency(1);
131 //cerr << "Setting radio frequency to : " << stationFreq << endl;
132 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
135 //double, lat, lon, head; // Unused variables;
136 //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
137 FGParking* parking = dcs->getParking(park_index);
138 aircraftRadius = parking->getRadius();
139 string fltType = parking->getType(); // gate / ramp, ga, etc etc.
140 string aircraftType; // Unused.
141 string airline; // Currently used for gate selection, but a fallback mechanism will apply when not specified.
142 fp->setGate(park_index);
143 if (!(fp->createPushBack(&ai_ac,
157 fp->getLastWaypoint()->setName( fp->getLastWaypoint()->getName() + string("legend"));
162 // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
163 // keep the ATC code happy.
167 ai_ac.SetFlightPlan(fp);
170 controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
171 ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
172 aircraftRadius, leg, &ai_ac);
176 //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
177 //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
178 //globals->get_scenery()->get_scene_graph()->addChild(node);
180 initSucceeded = true;
183 void FGATCManager::addController(FGATCController *controller) {
184 activeStations.push_back(controller);
187 void FGATCManager::update ( double time ) {
188 //cerr << "ATC update code is running at time: " << time << endl;
189 // Test code: let my virtual co-pilot handle ATC:
193 FGAIFlightPlan *fp = ai_ac.GetFlightPlan();
195 /* test code : find out how the routing develops */
197 int size = fp->getNrOfWayPoints();
198 //cerr << "Setting pos" << pos << " ";
199 //cerr << "setting intentions " ;
200 // This indicates that we have run out of waypoints: Im future versions, the
201 // user should be able to select a new route, but for now just shut down the
204 //cerr << "Shutting down the atc_mgr" << endl;
207 //cerr << "Size of waypoint cue " << size << " ";
208 //for (int i = 0; i < size; i++) {
209 // int val = fp->getRouteIndex(i);
210 //cerr << fp->getWayPoint(i)->getName() << " ";
211 //if ((val) && (val != pos)) {
212 //intentions.push_back(val);
213 //cerr << "[done ] " << endl;
216 //cerr << "[done ] " << endl;
219 //cerr << "Currently at leg : " << fp->getLeg() << endl;
221 double longitude = fgGetDouble("/position/longitude-deg");
222 double latitude = fgGetDouble("/position/latitude-deg");
223 double heading = fgGetDouble("/orientation/heading-deg");
224 double speed = fgGetDouble("/velocities/groundspeed-kt");
225 double altitude = fgGetDouble("/position/altitude-ft");
228 SGGeod me(SGGeod::fromDegM(longitude,
231 SGGeod wpt1(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
232 fp->getWayPoint(1)->getLatitude(),
233 fp->getWayPoint(1)->getAltitude()));
234 SGGeod wpt2(SGGeod::fromDegM(fp->getWayPoint(1)->getLongitude(),
235 fp->getWayPoint(1)->getLatitude(),
236 fp->getWayPoint(1)->getAltitude()));
238 double course1, az1, dist1;
239 double course2, az2, dist2;
240 SGGeodesy::inverse(me, wpt1, course1, az1, dist1);
242 cerr << "Bearing to nearest waypoint : " << course1 << " " << dist1 << ". (course " << course2 << ")." << endl;
244 ai_ac.setLatitude(latitude);
245 ai_ac.setLongitude(longitude);
246 ai_ac.setAltitude(altitude);
247 ai_ac.setHeading(heading);
248 ai_ac.setSpeed(speed);
250 controller = ai_ac.getATCController();
251 FGATCDialogNew::instance()->update(time);
253 //cerr << "name of previous waypoint : " << fp->getPreviousWaypoint()->getName() << endl;
255 //cerr << "Running FGATCManager::update()" << endl;
256 //cerr << "Currently under control of " << controller->getName() << endl;
257 controller->updateAircraftInformation(ai_ac.getID(),
263 //string airport = fgGetString("/sim/presets/airport-id");
264 //FGAirport *apt = FGAirport::findByIdent(airport);
265 // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading.
266 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
267 int n = trans_num->getIntValue();
269 //cerr << "Toggling ground network visibility " << networkVisible << endl;
270 networkVisible = !networkVisible;
271 trans_num->setIntValue(-1);
273 if ((controller != prevController) && (prevController)) {
274 prevController->render(false);
276 controller->render(networkVisible);
278 //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
279 prevController = controller;
281 for (AtcVecIterator atc = activeStations.begin(); atc != activeStations.end(); atc++) {
282 (*atc)->update(time);