]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATCDCL/ATCDialog.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / src / ATCDCL / ATCDialog.cxx
index 385e1f7356f5faa863b7cbded01d1cd1c385b812..026a6544928ef7b524535df8022d091d02147fa0 100644 (file)
@@ -34,9 +34,9 @@
 #include "ATCDialog.hxx"
 #include "ATC.hxx"
 #include "ATCmgr.hxx"
-#include "commlist.hxx"
 #include "ATCutils.hxx"
 #include <Airports/simple.hxx>
+#include <ATC/CommStation.hxx>
 
 #include <sstream>
 
@@ -46,6 +46,7 @@ FGATCDialog *current_atcdialog;
 
 // For the command manager - maybe eventually this should go in the built in command list
 static bool do_ATC_dialog(const SGPropertyNode* arg) {
+        cerr << "Running ATCDCL do_ATC_dialog" << endl;
        current_atcdialog->PopupDialog();
        return(true);
 }
@@ -66,7 +67,7 @@ ATCMenuEntry::ATCMenuEntry() {
 ATCMenuEntry::~ATCMenuEntry() {
 }
 
-static void atcUppercase(string &s) {
+void atcUppercase(string &s) {
        for(unsigned int i=0; i<s.size(); ++i) {
                s[i] = toupper(s[i]);
        }
@@ -101,39 +102,39 @@ FGATCDialog::~FGATCDialog() {
 
 void FGATCDialog::Init() {
        // Add ATC-dialog to the command list
-       globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
+       //globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
        // Add ATC-freq-search to the command list
-       globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
+       //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
 
        // initialize properties polled in Update()
-       globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
-       globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
+       //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
+       //globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
 }
 
 void FGATCDialog::Update(double dt) {
-       static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
-       string s = airport->getStringValue();
-       if (!s.empty()) {
-               airport->setStringValue("");
-               FreqDisplay(s);
-       }
-
-       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);
-       }
-
-       if(_callbackPending) {
-               if(_callbackTimer > _callbackWait) {
-                       _callbackPtr->ReceiveUserCallback(_callbackCode);
-                       _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
-                       _callbackPending = false;
-               } else {
-                       _callbackTimer += dt;
-               }
-       }
+       //static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
+       //string s = airport->getStringValue();
+       //if (!s.empty()) {
+       //      airport->setStringValue("");
+       //      FreqDisplay(s);
+       //}
+
+       //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);
+       //}
+
+       //if(_callbackPending) {
+       //      if(_callbackTimer > _callbackWait) {
+       //              _callbackPtr->ReceiveUserCallback(_callbackCode);
+       //              _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
+       //              _callbackPending = false;
+       //      } else {
+       //              _callbackTimer += dt;
+       //      }
+       //}
 }
 
 // Add an entry
@@ -266,7 +267,7 @@ void FGATCDialog::PopupCallback(int num) {
                //cout << "TOWER " << endl;
                //cout << "ident is " << atcptr->get_ident() << endl;
                atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
-               unsigned int size = atcmlist.size();
+               int size = atcmlist.size();
                if(size && num < size) {
                        //cout << "Doing callback...\n";
                        ATCMenuEntry a = atcmlist[num];
@@ -286,21 +287,17 @@ void FGATCDialog::PopupCallback(int num) {
        }
 }
 
-// map() key data type (removes duplicates and sorts by distance)
-struct atcdata {
-       atcdata() {}
-       atcdata(const string i, const string n, const double d) {
-               id = i, name = n, distance = d;
-       }
-       bool operator<(const atcdata& a) const {
-               return id != a.id && distance < a.distance;
-       }
-       bool operator==(const atcdata& a) const {
-               return id == a.id && distance == a.distance;
-       }
-       string id;
-       string name;
-       double distance;
+class AirportsWithATC : public FGAirport::AirportFilter
+{
+public:
+    virtual FGPositioned::Type maxType() const {
+      return FGPositioned::SEAPORT;
+    }
+  
+    virtual bool passAirport(FGAirport* aApt) const
+    {
+      return (!aApt->commStations().empty());
+    }
 };
 
 void FGATCDialog::FreqDialog() {
@@ -315,41 +312,25 @@ void FGATCDialog::FreqDialog() {
        // remove all dynamic airport/ATC buttons
        button_group->removeChildren("button", false);
 
-       // Find the ATC stations within a reasonable range
-       comm_list_type atc_stations;
-       comm_list_iterator atc_stat_itr;
-       
-       double lon = fgGetDouble("/position/longitude-deg");
-       double lat = fgGetDouble("/position/latitude-deg");
-       double elev = fgGetDouble("/position/altitude-ft");
-       Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
-               lat * SGD_DEGREES_TO_RADIANS, elev));
-
-       // search stations in range
-       int num_stat = current_commlist->FindByPos(lon, lat, elev, 50.0, &atc_stations);
-       if (num_stat != 0) {
-               map<atcdata, bool> uniq;
-               // fill map (sorts by distance and removes duplicates)
-               comm_list_iterator itr = atc_stations.begin();
-               for (; itr != atc_stations.end(); ++itr) {
-                       Point3D station = Point3D(itr->x, itr->y, itr->z);
-                       double distance = aircraft.distance3Dsquared(station);
-                       uniq[atcdata(itr->ident, itr->name, distance)] = true;
-               }
-               // create button per map entry (modified copy of <button-template>)
-               map<atcdata, bool>::iterator uit = uniq.begin();
-               for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
-                       SGPropertyNode *entry = button_group->getNode("button", n, true);
-                       copyProperties(button_group->getNode("button-template", true), entry);
-                       entry->removeChildren("enabled", true);
-                       entry->setStringValue("legend", uit->first.id.c_str());
-                       entry->setStringValue("binding[0]/value", uit->first.id.c_str());
-               }
-       }
 
+  SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
+    fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
+
+    AirportsWithATC filt;
+    FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
+    FGPositioned::sortByRange(results, geod);
+    for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
+      
+        SGPropertyNode *entry = button_group->getNode("button", r, true);
+               copyProperties(button_group->getNode("button-template", true), entry);
+               entry->removeChildren("enabled", true);
+               entry->setStringValue("legend", results[r]->ident());
+               entry->setStringValue("binding[0]/value", results[r]->ident());
+    }
+    
        // (un)hide message saying no things in range
        SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
-       range_error->setBoolValue("enabled", !num_stat);
+       range_error->setBoolValue("enabled", !results.empty());
 
        _gui->showDialog(dialog_name);
 }
@@ -380,45 +361,33 @@ void FGATCDialog::FreqDisplay(string& ident) {
        label = ident + " Frequencies";
        dlg->setStringValue("text/label", label.c_str());
 
-       int n = 0;      // Number of ATC frequencies at this airport
-
-       comm_list_type stations;
-       int found = current_commlist->FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 20.0, &stations);
-       if(found) {
-               ostringstream ostr;
-               comm_list_iterator itr = stations.begin();
-               for (n = 0; itr != stations.end(); ++itr) {
-                       if(itr->ident != ident)
-                               continue;
-
-                       if(itr->type == INVALID)
-                               continue;
-
-                       // add frequency line (modified copy of <group-template>)
-                       SGPropertyNode *entry = freq_group->getNode("group", n, true);
-                       copyProperties(freq_group->getNode("group-template", true), entry);
-                       entry->removeChildren("enabled", true);
-
-                       ostr << itr->type;
-                       entry->setStringValue("text[0]/label", ostr.str().c_str());
+    const flightgear::CommStationList& comms(a->commStations());
+    if (comms.empty()) {
+        label = "No frequencies found for airport " + ident;
+               mkDialog(label.c_str());
+               return;
+    }
+    
+    int n = 0;
+    for (unsigned int c=0; c < comms.size(); ++c) {
+        flightgear::CommStation* comm = comms[c];
+        
+        // add frequency line (modified copy of <group-template>)
+               SGPropertyNode *entry = freq_group->getNode("group", n, true);
+               copyProperties(freq_group->getNode("group-template", true), entry);
+               entry->removeChildren("enabled", true);
 
-                       char buf[8];
-                       snprintf(buf, 8, "%.2f", (itr->freq / 100.0));  // Convert from KHz to MHz
-                       if(buf[5] == '3') buf[5] = '2';
-                       if(buf[5] == '8') buf[5] = '7';
-                       buf[7] = '\0';
+        entry->setStringValue("text[0]/label", comm->ident());
 
-                       entry->setStringValue("text[1]/label", buf);
+               char buf[8];
+               snprintf(buf, 8, "%.2f", comm->freqMHz());
+               if(buf[5] == '3') buf[5] = '2';
+               if(buf[5] == '8') buf[5] = '7';
+               buf[7] = '\0';
 
-                       ostr.seekp(0);
-                       n++;
-               }
-       }
-       if(n == 0) {
-               label = "No frequencies found for airport " + ident;
-               mkDialog(label.c_str());
-               return;
-       }
+               entry->setStringValue("text[1]/label", buf);
+        ++n;
+    }
 
        _gui->showDialog(dialog_name);
 }