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 <simgear/math/SGMath.hxx>
30 #include <Airports/dynamics.hxx>
31 #include <Airports/simple.hxx>
32 #include <Scenery/scenery.hxx>
33 #include "atc_mgr.hxx"
36 FGATCManager::FGATCManager() {
40 FGATCManager::~FGATCManager() {
44 void FGATCManager::init() {
46 currentATCDialog = new FGATCDialogNew;
47 currentATCDialog->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 plannign.
80 FGAISchedule *trafficRef = new FGAISchedule;
81 trafficRef->setFlightType("gate");
83 FGScheduledFlight *flight = new FGScheduledFlight;
84 flight->setDepartureAirport(airport);
85 flight->setArrivalAirport(airport);
86 flight->initializeAirports();
87 flight->setFlightRules("IFR");
88 flight->setCallSign(callsign);
90 trafficRef->assign(flight);
91 FGAIFlightPlan *fp = new FGAIFlightPlan;
92 ai_ac.setTrafficRef(trafficRef);
94 string flightPlanName = airport + "-" + airport + ".xml";
95 double cruiseAlt = 100; // Doesn't really matter right now.
96 double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though...
97 time_t deptime = 0; // just make sure how flightplan processing is affected by this...
100 FGAirport *apt = FGAirport::findByIdent(airport);
101 FGAirportDynamics* dcs = apt->getDynamics();
102 int park_index = dcs->getNrOfParkings() - 1;
103 cerr << "found information: " << runway << " " << airport << ": parking = " << parking << endl;
105 while (park_index >= 0 && dcs->getParkingName(park_index) != parking) park_index--;
106 if (park_index < 0) {
107 SG_LOG( SG_GENERAL, SG_ALERT,
108 "Failed to find parking position " << parking <<
109 " at airport " << airport );
111 if (parking.empty() || (park_index < 0)) {
112 controller = apt->getDynamics()->getTowerController();
113 int stationFreq = apt->getDynamics()->getTowerFrequency(2);
114 cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
115 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
117 string fltType = "ga";
118 fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
120 controller = apt->getDynamics()->getStartupController();
121 int stationFreq = apt->getDynamics()->getGroundFrequency(2);
122 cerr << "Setting radio frequency to : " << stationFreq << endl;
123 fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
125 //double, lat, lon, head; // Unused variables;
126 //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
127 FGParking* parking = dcs->getParking(park_index);
128 aircraftRadius = parking->getRadius();
129 string fltType = parking->getType(); // gate / ramp, ga, etc etc.
130 string aircraftType; // Unused.
131 string airline; // Currently used for gate selection, but a fallback mechanism will apply when not specified.
132 fp->setGate(park_index);
133 fp->createPushBack(&ai_ac,
147 // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
148 // keep the ATC code happy.
152 ai_ac.SetFlightPlan(fp);
154 controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->routeIndex,
155 ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
156 aircraftRadius, leg, &ai_ac);
160 //osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
161 //cerr << "Adding groundnetWork to the scenegraph::init" << endl;
162 //globals->get_scenery()->get_scene_graph()->addChild(node);
166 void FGATCManager::addController(FGATCController *controller) {
167 activeStations.push_back(controller);
170 void FGATCManager::update ( double time ) {
171 //cerr << "ATC update code is running at time: " << time << endl;
172 currentATCDialog->update(time);
174 double longitude = fgGetDouble("/position/longitude-deg");
175 double latitude = fgGetDouble("/position/latitude-deg");
176 double heading = fgGetDouble("/orientation/heading-deg");
177 double speed = fgGetDouble("/velocities/groundspeed-kt");
178 double altitude = fgGetDouble("/position/altitude-ft");
180 //cerr << "Running FGATCManager::update()" << endl;
181 controller->updateAircraftInformation(ai_ac.getID(),
188 string airport = fgGetString("/sim/presets/airport-id");
189 FGAirport *apt = FGAirport::findByIdent(airport);
190 osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
191 //cerr << "Adding groundnetWork to the scenegraph::update" << endl;
193 globals->get_scenery()->get_scene_graph()->addChild(node);