From e04d2f8ba9421fe9811e2d2e474392f010ae54b3 Mon Sep 17 00:00:00 2001 From: Durk Talsma Date: Sun, 10 Apr 2011 08:58:48 +0200 Subject: [PATCH] Assigned an ATC controller to the user's Aircraft and change the comm1 radio frequency according to the frequency relevant to the current scenario. --- src/ATC/atc_mgr.cxx | 35 ++++++++++++++++++++++++++++++++++ src/ATC/atc_mgr.hxx | 1 + src/ATC/atcdialog.cxx | 10 ++++++++++ src/ATC/trafficcontrol.cxx | 15 +++++++++++++-- src/ATC/trafficcontrol.hxx | 4 ++++ src/Airports/dynamics.cxx | 26 +++++++++++++++++++++++++ src/Airports/dynamics.hxx | 4 ++-- src/Airports/groundnetwork.cxx | 1 + 8 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/ATC/atc_mgr.cxx b/src/ATC/atc_mgr.cxx index ba71c6087..d1a182f18 100644 --- a/src/ATC/atc_mgr.cxx +++ b/src/ATC/atc_mgr.cxx @@ -27,6 +27,9 @@ #include #include +#include +#include + #include "atc_mgr.hxx" @@ -43,6 +46,37 @@ void FGATCManager::init() { currentATCDialog = new FGATCDialogNew; currentATCDialog->init(); + // find a reasonable controller for our user's aircraft.. + // Let's start by working out the following three scenarios: + // Starting on ground at a parking position + // Starting on ground at the runway. + // Starting in the Air + bool onGround = fgGetBool("/sim/presets/onground"); + string runway = fgGetString("/sim/atc/runway"); + string airport = fgGetString("/sim/presets/airport-id"); + string parking = fgGetString("/sim/presets/parkpos"); + + FGAirport *apt = FGAirport::findByIdent(airport); + cerr << "found information: " << runway << " " << airport << ": parking = " << parking << endl; + if (onGround) { + if (parking.empty()) { + controller = apt->getDynamics()->getTowerController(); + int stationFreq = apt->getDynamics()->getTowerFrequency(2); + cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl; + fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0)); + + } else { + controller = apt->getDynamics()->getGroundNetwork(); + int stationFreq = apt->getDynamics()->getGroundFrequency(2); + cerr << "Setting radio frequency to : " << stationFreq << endl; + fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0)); + + } + } else { + controller = 0; + } + //controller = + //dialog.init(); } @@ -52,4 +86,5 @@ void FGATCManager::addController(FGATCController *controller) { void FGATCManager::update ( double time ) { //cerr << "ATC update code is running at time: " << time << endl; + currentATCDialog->update(time); } diff --git a/src/ATC/atc_mgr.hxx b/src/ATC/atc_mgr.hxx index c34c82446..5aab9b64c 100644 --- a/src/ATC/atc_mgr.hxx +++ b/src/ATC/atc_mgr.hxx @@ -46,6 +46,7 @@ class FGATCManager : public SGSubsystem { private: AtcVec activeStations; + FGATCController *controller; // The ATC controller that is responsible for the user's aircraft. //FGATCDialogNew dialog; // note that this variable should really replace the ugly global "currentATCDialog(); public: diff --git a/src/ATC/atcdialog.cxx b/src/ATC/atcdialog.cxx index 9c33b04aa..3ab9e28d7 100644 --- a/src/ATC/atcdialog.cxx +++ b/src/ATC/atcdialog.cxx @@ -132,3 +132,13 @@ void FGATCDialogNew::PopupDialog() { dialogVisible = !dialogVisible; return; } + +void FGATCDialogNew::update(double dt) { + static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true); + int n = trans_num->getIntValue(); + if (n >= 0) { + trans_num->setIntValue(-1); + // PopupCallback(n); + cerr << "Selected transmission message" << n << endl; + } +} \ No newline at end of file diff --git a/src/ATC/trafficcontrol.cxx b/src/ATC/trafficcontrol.cxx index 664929bfc..b4bfb7efd 100644 --- a/src/ATC/trafficcontrol.cxx +++ b/src/ATC/trafficcontrol.cxx @@ -452,8 +452,7 @@ FGATCController::FGATCController() dt_count = 0; available = true; lastTransmission = 0; - FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC"); - mgr->addController(this); + initialized = false; } FGATCController::~FGATCController() @@ -672,6 +671,15 @@ string FGATCController::genTransponderCode(string fltRules) } } +void FGATCController::init() +{ + if (!initialized) { + FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC"); + mgr->addController(this); + initialized = true; + } +} + /*************************************************************************** * class FGTowerController * @@ -690,6 +698,7 @@ void FGTowerController::announcePosition(int id, double radius, int leg, FGAIAircraft * ref) { + init(); TrafficVectorIterator i = activeTraffic.begin(); // Search whether the current id alread has an entry // This might be faster using a map instead of a vector, but let's start by taking a safe route @@ -882,6 +891,7 @@ void FGStartupController::announcePosition(int id, double radius, int leg, FGAIAircraft * ref) { + init(); TrafficVectorIterator i = activeTraffic.begin(); // Search whether the current id alread has an entry // This might be faster using a map instead of a vector, but let's start by taking a safe route @@ -1141,6 +1151,7 @@ void FGApproachController::announcePosition(int id, double alt, double radius, int leg, FGAIAircraft * ref) { + init(); TrafficVectorIterator i = activeTraffic.begin(); // Search whether the current id alread has an entry // This might be faster using a map instead of a vector, but let's start by taking a safe route diff --git a/src/ATC/trafficcontrol.hxx b/src/ATC/trafficcontrol.hxx index 2550f7cb6..20ec9962e 100644 --- a/src/ATC/trafficcontrol.hxx +++ b/src/ATC/trafficcontrol.hxx @@ -225,6 +225,8 @@ typedef vector::iterator ActiveRunwayVecIterator; *************************************************************************************/ class FGATCController { +private: + bool initialized; protected: bool available; time_t lastTransmission; @@ -260,6 +262,8 @@ public: ATC_GROUND_TO_AIR } AtcMsgDir; FGATCController(); virtual ~FGATCController(); + void init(); + virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute, double lat, double lon, double hdg, double spd, double alt, double radius, int leg, diff --git a/src/Airports/dynamics.cxx b/src/Airports/dynamics.cxx index 11cb454b9..d608043b4 100644 --- a/src/Airports/dynamics.cxx +++ b/src/Airports/dynamics.cxx @@ -535,6 +535,32 @@ int FGAirportDynamics::getGroundFrequency(unsigned leg) return groundFreq; } +int FGAirportDynamics::getTowerFrequency(unsigned nr) +{ + int towerFreq = 0; + if (nr < 2) { + SG_LOG(SG_ATC, SG_ALERT, + "Leg value is smaller than two at " << SG_ORIGIN); + } + if (freqTower.size() == 0) { + return 0; + } + if ((freqTower.size() > nr - 1) && (nr > 1)) { + towerFreq = freqTower[nr - 1]; + } + if ((freqTower.size() < nr - 1) && (nr > 1)) { + towerFreq = + (freqTower.size() < + (nr - 1)) ? freqTower[freqTower.size() - + 1] : freqTower[nr - 2]; + } + if ((freqTower.size() >= nr - 1) && (nr > 1)) { + towerFreq = freqTower[nr - 2]; + } + return towerFreq; +} + + FGAIFlightPlan *FGAirportDynamics::getSID(string activeRunway, double heading) { diff --git a/src/Airports/dynamics.hxx b/src/Airports/dynamics.hxx index 609218218..ecadb9d62 100644 --- a/src/Airports/dynamics.hxx +++ b/src/Airports/dynamics.hxx @@ -119,8 +119,8 @@ public: FGApproachController *getApproachController() { return &approachController; }; const string& getAtisInformation() { return atisInformation; }; - int getGroundFrequency(unsigned leg); //{ return freqGround.size() ? freqGround[0] : 0; }; - + int getGroundFrequency (unsigned leg); //{ return freqGround.size() ? freqGround[0] : 0; }; + int getTowerFrequency (unsigned nr); void setRwyUse(const FGRunwayPreference& ref); }; diff --git a/src/Airports/groundnetwork.cxx b/src/Airports/groundnetwork.cxx index 3ba650158..0470e4af9 100644 --- a/src/Airports/groundnetwork.cxx +++ b/src/Airports/groundnetwork.cxx @@ -460,6 +460,7 @@ void FGGroundNetwork::announcePosition(int id, double radius, int leg, FGAIAircraft * aircraft) { + init(); TrafficVectorIterator i = activeTraffic.begin(); // Search search if the current id alread has an entry // This might be faster using a map instead of a vector, but let's start by taking a safe route -- 2.39.2