]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atc_mgr.cxx
88f527f32347d9b261578bb3efdb46b2d31856e2
[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 "atc_mgr.hxx"
33
34
35 FGATCManager::FGATCManager() {
36
37 }
38
39 FGATCManager::~FGATCManager() {
40
41 }
42
43 void FGATCManager::init() {
44     SGSubsystem::init();
45     currentATCDialog = new FGATCDialogNew;
46     currentATCDialog->init();
47
48     int leg = 0;
49
50     // find a reasonable controller for our user's aircraft..
51     // Let's start by working out the following three scenarios: 
52     // Starting on ground at a parking position
53     // Starting on ground at the runway.
54     // Starting in the Air
55     bool onGround  = fgGetBool("/sim/presets/onground");
56     string runway  = fgGetString("/sim/atc/runway");
57     string airport = fgGetString("/sim/presets/airport-id");
58     string parking = fgGetString("/sim/presets/parkpos");
59     
60
61     // Create an (invisible) AIAircraft represenation of the current
62     // Users, aircraft, that mimicks the user aircraft's behavior.
63     string callsign= fgGetString("/sim/multiplay/callsign");
64     double longitude = fgGetDouble("/position/longitude-deg");
65     double latitude  = fgGetDouble("/position/latitude-deg");
66     double altitude  = fgGetDouble("/position/altitude-ft");
67     double heading   = fgGetDouble("/orientation/heading-deg");
68     double speed     = fgGetDouble("/velocities/groundspeed-kt");
69     double aircraftRadius = 40; // note that this is currently hardcoded to a one-size-fits all JumboJet value. Should change later;
70
71
72     ai_ac.setCallSign ( callsign  );
73     ai_ac.setLongitude( longitude );
74     ai_ac.setLatitude ( latitude  );
75     ai_ac.setAltitude ( altitude  );
76     ai_ac.setPerformance("jet_transport");
77
78     // NEXT UP: Create a traffic Schedule and fill that with appropriate information. This we can use to flight plannign.
79     FGAISchedule *trafficRef = new FGAISchedule;
80     trafficRef->setFlightType("gate");
81
82     FGScheduledFlight *flight =  new FGScheduledFlight;
83     flight->setDepartureAirport(airport);
84     flight->setArrivalAirport(airport);
85     flight->initializeAirports();
86     flight->setFlightRules("IFR");
87     flight->setCallSign(callsign);
88     
89     trafficRef->assign(flight);
90     FGAIFlightPlan *fp = new FGAIFlightPlan;
91     ai_ac.setTrafficRef(trafficRef);
92     
93     string flightPlanName = airport + "-" + airport + ".xml";
94     double cruiseAlt = 100; // Doesn't really matter right now.
95     double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though...
96     time_t deptime = 0;        // just make sure how flightplan processing is affected by this...
97
98
99     FGAirport *apt = FGAirport::findByIdent(airport); 
100     FGAirportDynamics* dcs = apt->getDynamics();
101     int park_index = dcs->getNrOfParkings() - 1;
102     cerr << "found information: " << runway << " " << airport << ": parking = " << parking << endl;
103     if (onGround) {
104         while (park_index >= 0 && dcs->getParkingName(park_index) != parking) park_index--;
105             if (park_index < 0) {
106                   SG_LOG( SG_GENERAL, SG_ALERT,
107                           "Failed to find parking position " << parking <<
108                            " at airport " << airport );
109              }
110         if (parking.empty() || (park_index < 0)) {
111             controller = apt->getDynamics()->getTowerController();
112             int stationFreq = apt->getDynamics()->getTowerFrequency(2);
113             cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
114             fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
115             leg = 4;
116             string fltType = "ga";
117             fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
118         } else {
119             controller = apt->getDynamics()->getStartupController();
120             int stationFreq = apt->getDynamics()->getGroundFrequency(2);
121             cerr << "Setting radio frequency to : " << stationFreq << endl;
122             fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
123             leg = 2;
124             //double, lat, lon, head; // Unused variables;
125             //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
126             FGParking* parking = dcs->getParking(park_index);
127             aircraftRadius = parking->getRadius();
128             string fltType = parking->getType(); // gate / ramp, ga, etc etc. 
129             string aircraftType; // Unused.
130             string airline;      // Currently used for gate selection, but a fallback mechanism will apply when not specified.
131             fp->setGate(park_index);
132             fp->createPushBack(&ai_ac,
133                                false, 
134                                apt, 
135                                latitude,
136                                longitude,
137                                aircraftRadius,
138                                fltType,
139                                aircraftType,
140                                airline);
141          } 
142      } else {
143         controller = 0;
144      }
145
146     // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
147     // keep the ATC code happy. 
148     
149     
150     fp->restart();
151     ai_ac.SetFlightPlan(fp);
152     if (controller) {
153         controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->routeIndex,
154                                       ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
155                                       aircraftRadius, leg, &ai_ac);
156
157     //dialog.init();
158    }
159 }
160
161 void FGATCManager::addController(FGATCController *controller) {
162     activeStations.push_back(controller);
163 }
164
165 void FGATCManager::update ( double time ) {
166     //cerr << "ATC update code is running at time: " << time << endl;
167     currentATCDialog->update(time);
168     if (controller) {
169         double longitude = fgGetDouble("/position/longitude-deg");
170         double latitude  = fgGetDouble("/position/latitude-deg");
171         double heading   = fgGetDouble("/orientation/heading-deg");
172         double speed     = fgGetDouble("/velocities/groundspeed-kt");
173         double altitude  = fgGetDouble("/position/altitude-ft");
174
175         cerr << "Running FGATCManager::update()" << endl;
176         controller->updateAircraftInformation(ai_ac.getID(),
177                                               latitude,
178                                               longitude,
179                                               heading,
180                                               speed,
181                                               altitude, time);
182     }
183 }