]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/dynamics.cxx
Expose more things to Nasal for FMSs in particular - still work in progress.
[flightgear.git] / src / Airports / dynamics.cxx
index 11cb454b97c5f8bdf851e6aa3ac81308d78b78d7..5a077968d1cad8f7bbc1d9ff456fd7b6e108e452 100644 (file)
@@ -36,6 +36,7 @@
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 #include <Airports/runways.hxx>
+#include <ATCDCL/ATCutils.hxx>
 
 #include <string>
 #include <vector>
@@ -49,33 +50,15 @@ using std::random_shuffle;
 #include "dynamics.hxx"
 
 FGAirportDynamics::FGAirportDynamics(FGAirport * ap):
-_ap(ap), rwyPrefs(ap), SIDs(ap)
-{
-    lastUpdate = 0;
-
-    // For testing only. This needs to be refined when we move ATIS functionality over.
-    atisInformation = "Sierra";
-}
+    _ap(ap), rwyPrefs(ap), SIDs(ap),
+        startupController    (this),
+    towerController      (this),
+    approachController   (this),
+    atisSequenceIndex(-1),
+    atisSequenceTimeStamp(0.0)
 
-// Note that the ground network should also be copied
-FGAirportDynamics::
-FGAirportDynamics(const FGAirportDynamics & other):rwyPrefs(other.
-                                                            rwyPrefs),
-SIDs(other.SIDs)
 {
-    for (FGParkingVecConstIterator ip = other.parkings.begin();
-         ip != other.parkings.end(); ip++)
-        parkings.push_back(*(ip));
-    // rwyPrefs = other.rwyPrefs;
-    lastUpdate = other.lastUpdate;
-
-    stringVecConstIterator il;
-    for (il = other.landing.begin(); il != other.landing.end(); il++)
-        landing.push_back(*il);
-    for (il = other.takeoff.begin(); il != other.takeoff.end(); il++)
-        takeoff.push_back(*il);
-    lastUpdate = other.lastUpdate;
-    atisInformation = other.atisInformation;
+    lastUpdate = 0;
 }
 
 // Destructor
@@ -96,10 +79,11 @@ void FGAirportDynamics::init()
     random_shuffle(parkings.begin(), parkings.end());
     sort(parkings.begin(), parkings.end());
     // add the gate positions to the ground network. 
+    groundNetwork.setParent(_ap);
     groundNetwork.addNodes(&parkings);
     groundNetwork.init();
     groundNetwork.setTowerController(&towerController);
-    groundNetwork.setParent(_ap);
+    
 }
 
 bool FGAirportDynamics::getAvailableParking(double *lat, double *lon,
@@ -118,6 +102,7 @@ bool FGAirportDynamics::getAvailableParking(double *lat, double *lon,
         //cerr << "Could not find parking spot at " << _ap->getId() << endl;
         *lat = _ap->getLatitude();
         *lon = _ap->getLongitude();
+        * gateId = -1;
         *heading = 0;
         found = true;
     } else {
@@ -512,31 +497,86 @@ const string & FGAirportDynamics::getId() const
 int FGAirportDynamics::getGroundFrequency(unsigned leg)
 {
     //return freqGround.size() ? freqGround[0] : 0; };
+    //cerr << "Getting frequency for : " << leg << endl;
     int groundFreq = 0;
-    if (leg < 2) {
+    if (leg < 1) {
         SG_LOG(SG_ATC, SG_ALERT,
-               "Leg value is smaller than two at " << SG_ORIGIN);
+               "Leg value is smaller than one at " << SG_ORIGIN);
     }
     if (freqGround.size() == 0) {
         return 0;
     }
-    if ((freqGround.size() > leg - 1) && (leg > 1)) {
-        groundFreq = freqGround[leg - 1];
-    }
-    if ((freqGround.size() < leg - 1) && (leg > 1)) {
+
+    if ((freqGround.size() < leg) && (leg > 0)) {
         groundFreq =
-            (freqGround.size() <
+            (freqGround.size() <=
              (leg - 1)) ? freqGround[freqGround.size() -
-                                     1] : freqGround[leg - 2];
+                                     1] : freqGround[leg - 1];
     }
-    if ((freqGround.size() >= leg - 1) && (leg > 1)) {
-        groundFreq = freqGround[leg - 2];
+    if ((freqGround.size() >= leg) && (leg > 0)) {
+        groundFreq = freqGround[leg - 1];
     }
     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)
 {
     return SIDs.getBest(activeRunway, heading);
 }
+
+const std::string FGAirportDynamics::getAtisSequence()
+{
+   if (atisSequenceIndex == -1) {
+       updateAtisSequence(1, false);
+   }
+   
+   return GetPhoneticLetter(atisSequenceIndex);
+}
+
+int FGAirportDynamics::updateAtisSequence(int interval, bool forceUpdate)
+{
+    double now = globals->get_sim_time_sec();
+    if (atisSequenceIndex == -1) {
+        // first computation
+        atisSequenceTimeStamp = now;
+        atisSequenceIndex = rand() % LTRS; // random initial sequence letters
+        return atisSequenceIndex;
+    }
+
+    int steps = static_cast<int>((now - atisSequenceTimeStamp) / interval);
+    atisSequenceTimeStamp += (interval * steps);
+    if (forceUpdate && (steps == 0)) {
+        ++steps; // a "special" ATIS update is required
+    } 
+    
+    atisSequenceIndex = (atisSequenceIndex + steps) % LTRS;
+    // return a huge value if no update occurred
+    return (atisSequenceIndex + (steps ? 0 : LTRS*1000));
+}