]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atc_mgr.cxx
828431674e46eaf46c83d5078619921090e0f3fe
[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
33 #include "atc_mgr.hxx"
34
35
36 FGATCManager::FGATCManager() {
37
38 }
39
40 FGATCManager::~FGATCManager() {
41
42 }
43
44 void FGATCManager::init() {
45     SGSubsystem::init();
46     currentATCDialog = new FGATCDialogNew;
47     currentATCDialog->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     // Next, 
73
74     ai_ac.setCallSign ( callsign  );
75     ai_ac.setLongitude( longitude );
76     ai_ac.setLatitude ( latitude  );
77     ai_ac.setAltitude ( altitude  );
78     ai_ac.setPerformance("jet_transport");
79
80     FGAIFlightPlan *fp = new FGAIFlightPlan;
81     
82     string flightPlanName = airport + "-" + airport + ".xml";
83     double cruiseAlt = 100; // Doesn't really matter right now.
84     double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though...
85     time_t deptime = 0;        // just make sure how flightplan processing is affected by this...
86
87
88     FGAirport *apt = FGAirport::findByIdent(airport); 
89     FGAirportDynamics* dcs = apt->getDynamics();
90     int park_index = dcs->getNrOfParkings() - 1;
91     cerr << "found information: " << runway << " " << airport << ": parking = " << parking << endl;
92     if (onGround) {
93         while (park_index >= 0 && dcs->getParkingName(park_index) != parking) park_index--;
94             if (park_index < 0) {
95                   SG_LOG( SG_GENERAL, SG_ALERT,
96                           "Failed to find parking position " << parking <<
97                            " at airport " << airport );
98              }
99         if (parking.empty() || (park_index < 0)) {
100             controller = apt->getDynamics()->getTowerController();
101             int stationFreq = apt->getDynamics()->getTowerFrequency(2);
102             cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
103             fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
104             leg = 4;
105             string fltType = "ga";
106             fp->createTakeOff(&ai_ac, false, apt, 0, fltType);
107         } else {
108             controller = apt->getDynamics()->getGroundNetwork();
109             int stationFreq = apt->getDynamics()->getGroundFrequency(2);
110             cerr << "Setting radio frequency to : " << stationFreq << endl;
111             fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
112             leg = 2;
113             //double, lat, lon, head; // Unused variables;
114             //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head);
115             FGParking* parking = dcs->getParking(park_index);
116             aircraftRadius = parking->getRadius();
117             string fltType = parking->getType(); // gate / ramp, ga, etc etc. 
118             string aircraftType; // Unused.
119             string airline;      // Currently used for gate selection, but a fallback mechanism will apply when not specified.
120             fp->setGate(park_index);
121             fp->createPushBack(&ai_ac,
122                                false, 
123                                apt, 
124                                latitude,
125                                longitude,
126                                aircraftRadius,
127                                fltType,
128                                aircraftType,
129                                airline);
130          } 
131      } else {
132         controller = 0;
133      }
134
135     // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to
136     // keep the ATC code happy. 
137     
138     
139     fp->restart();
140     ai_ac.SetFlightPlan(fp);
141     if (controller) {
142         controller->announcePosition(ai_ac.getID(), fp, fp->getCurrentWaypoint()->routeIndex,
143                                       ai_ac._getLatitude(), ai_ac._getLongitude(), heading, speed, altitude,
144                                       aircraftRadius, leg, &ai_ac);
145
146     //dialog.init();
147    }
148 }
149
150 void FGATCManager::addController(FGATCController *controller) {
151     activeStations.push_back(controller);
152 }
153
154
155
156 void FGATCManager::update ( double time ) {
157     //cerr << "ATC update code is running at time: " << time << endl;
158    currentATCDialog->update(time);
159
160
161 }