]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/trafficcontrol.cxx
Make PerformanceDB a real subsystem
[flightgear.git] / src / ATC / trafficcontrol.cxx
index c71dd034dcd6664ffddfb1b01c21941279890461..f27ca86a1f1c663d798284751f2a175a030d7be0 100644 (file)
@@ -25,6 +25,7 @@
 #endif
 
 #include <algorithm>
+#include <cstdio>
 
 #include <osg/Geode>
 #include <osg/Geometry>
 #include <AIModel/AIAircraft.hxx>
 #include <AIModel/AIFlightPlan.hxx>
 #include <AIModel/performancedata.hxx>
-#include <AIModel/performancedb.hxx>
 #include <ATC/atc_mgr.hxx>
 #include <Traffic/TrafficMgr.hxx>
 #include <Airports/groundnetwork.hxx>
 #include <Airports/dynamics.hxx>
-#include <Airports/simple.hxx>
+#include <Airports/airport.hxx>
 #include <Radio/radio.hxx>
 #include <signal.h>
 
 using std::sort;
 using std::string;
+using std::cout;
+using std::endl;
 
 /***************************************************************************
  * ActiveRunway
@@ -62,7 +64,7 @@ time_t ActiveRunway::requestTimeSlot(time_t eta)
     time_t newEta;
     time_t separation = 90;
     bool found = false;
-    if (estimatedArrivalTimes.size() == 0) {
+    if (estimatedArrivalTimes.empty()) {
         estimatedArrivalTimes.push_back(eta);
         return eta;
     } else {
@@ -177,7 +179,9 @@ FGTrafficRecord::FGTrafficRecord():
         allowTransmission(true),
         allowPushback(true),
         priority(0),
-        latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0)
+        timer(0),
+        latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0),
+        aircraft(NULL)
 {
 }
 
@@ -186,7 +190,7 @@ void FGTrafficRecord::setPositionAndIntentions(int pos,
 {
 
     currentPos = pos;
-    if (intentions.size()) {
+    if (! intentions.empty()) {
         intVecIterator i = intentions.begin();
         if ((*i) != pos) {
             SG_LOG(SG_ATC, SG_ALERT,
@@ -218,7 +222,7 @@ bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
         //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
         result = true;
     }
-    //  else if (other.intentions.size())
+    //  else if (! other.intentions.empty())
     //     {
     //       cerr << "Start check 2" << endl;
     //       intVecIterator i = other.intentions.begin();
@@ -228,7 +232,7 @@ bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
     //     cerr << "Check Position and intentions: current matches other.intentions" << endl;
     //     result = true;
     //       }
-    else if (intentions.size()) {
+    else if (! intentions.empty()) {
         //cerr << "Start check 3" << endl;
         intVecIterator i = intentions.begin();
         //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
@@ -272,7 +276,7 @@ int FGTrafficRecord::crosses(FGGroundNetwork * net,
         otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex();     // OKAY,...
     if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
         return currentTargetNode;
-    if (intentions.size()) {
+    if (! intentions.empty()) {
         for (i = intentions.begin(); i != intentions.end(); i++) {
             if ((*i) > 0) {
                 if (currentTargetNode ==
@@ -283,7 +287,7 @@ int FGTrafficRecord::crosses(FGGroundNetwork * net,
             }
         }
     }
-    if (other.intentions.size()) {
+    if (! other.intentions.empty()) {
         for (i = other.intentions.begin(); i != other.intentions.end();
                 i++) {
             if ((*i) > 0) {
@@ -295,7 +299,7 @@ int FGTrafficRecord::crosses(FGGroundNetwork * net,
             }
         }
     }
-    if (intentions.size() && other.intentions.size()) {
+    if (! intentions.empty() && ! other.intentions.empty()) {
         for (i = intentions.begin(); i != intentions.end(); i++) {
             for (j = other.intentions.begin(); j != other.intentions.end();
                     j++) {
@@ -327,7 +331,7 @@ bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
             net->findSegment(other.currentPos)->getEnd()->getIndex();
     if ((node == othernode) && (node != -1))
         return true;
-    if (other.intentions.size()) {
+    if (! other.intentions.empty()) {
         for (intVecIterator i = other.intentions.begin();
                 i != other.intentions.end(); i++) {
             if (*i > 0) {
@@ -339,7 +343,7 @@ bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
     }
     //if (other.currentPos > 0)
     //  othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
-    //if (intentions.size())
+    //if (! intentions.empty())
     //  {
     //    for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
     //    {
@@ -383,7 +387,7 @@ bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
                         }
                     }
             }
-            if (other.intentions.size()) {
+            if (! other.intentions.empty()) {
                 for (intVecIterator j = other.intentions.begin();
                         j != other.intentions.end(); j++) {
                     // cerr << "Current segment 1 " << (*i) << endl;
@@ -411,7 +415,7 @@ bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
     return false;
 }
 
-bool FGTrafficRecord::isActive(int margin)
+bool FGTrafficRecord::isActive(int margin) const
 {
     time_t now = time(NULL) + fgGetLong("/sim/time/warp");
     time_t deptime = aircraft->getTrafficRef()->getDepartureTime();
@@ -431,7 +435,7 @@ void FGTrafficRecord::setHeadingAdjustment(double heading)
     instruction.setHeading(heading);
 }
 
-bool FGTrafficRecord::pushBackAllowed()
+bool FGTrafficRecord::pushBackAllowed() const
 {
     return allowPushback;
 }
@@ -458,7 +462,7 @@ FGATCInstruction::FGATCInstruction()
 }
 
 
-bool FGATCInstruction::hasInstruction()
+bool FGATCInstruction::hasInstruction() const
 {
     return (holdPattern || holdPosition || changeSpeed || changeHeading
             || changeAltitude || resolveCircularWait);
@@ -479,11 +483,14 @@ FGATCController::FGATCController()
     available = true;
     lastTransmission = 0;
     initialized = false;
+    lastTransmissionDirection = ATC_AIR_TO_GROUND;
+    group = NULL;
 }
 
 FGATCController::~FGATCController()
 {
-    //cerr << "running FGATController destructor" << endl;
+    FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
+    mgr->removeController(this);
 }
 
 string FGATCController::getGateName(FGAIAircraft * ref)
@@ -813,7 +820,7 @@ void FGTowerController::announcePosition(int id,
     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
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -823,7 +830,7 @@ void FGTowerController::announcePosition(int id,
         }
     }
     // Add a new TrafficRecord if no one exsists for this aircraft.
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || (activeTraffic.empty())) {
         FGTrafficRecord rec;
         rec.setId(id);
 
@@ -836,7 +843,7 @@ void FGTowerController::announcePosition(int id,
         activeTraffic.push_back(rec);
         // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
         ActiveRunwayVecIterator rwy = activeRunways.begin();
-        if (activeRunways.size()) {
+        if (! activeRunways.empty()) {
             while (rwy != activeRunways.end()) {
                 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
                     break;
@@ -867,7 +874,7 @@ void FGTowerController::updateAircraftInformation(int id, double lat, double lon
     // Search whether the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
     TrafficVectorIterator current, closest;
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -877,7 +884,7 @@ void FGTowerController::updateAircraftInformation(int id, double lat, double lon
         }
     }
 //    // update position of the current aircraft
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || (activeTraffic.empty())) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
     } else {
@@ -950,7 +957,7 @@ void FGTowerController::signOff(int id)
     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
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -961,7 +968,7 @@ void FGTowerController::signOff(int id)
     }
     // If this aircraft has left the runway, we can clear the departure record for this runway
     ActiveRunwayVecIterator rwy = activeRunways.begin();
-    if (activeRunways.size()) {
+    if (! activeRunways.empty()) {
         //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
         while (rwy != activeRunways.end()) {
             if (rwy->getRunwayName() == i->getRunway()) {
@@ -977,7 +984,7 @@ void FGTowerController::signOff(int id)
                    "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff at " << SG_ORIGIN);
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || (activeTraffic.empty())) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
     } else {
@@ -998,7 +1005,7 @@ bool FGTowerController::hasInstruction(int id)
     TrafficVectorIterator i = activeTraffic.begin();
     // Search search if the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1007,7 +1014,7 @@ bool FGTowerController::hasInstruction(int id)
             i++;
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
     } else {
@@ -1022,7 +1029,7 @@ FGATCInstruction FGTowerController::getInstruction(int id)
     TrafficVectorIterator i = activeTraffic.begin();
     // Search search if the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1031,7 +1038,7 @@ FGATCInstruction FGTowerController::getInstruction(int id)
             i++;
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
     } else {
@@ -1041,7 +1048,7 @@ FGATCInstruction FGTowerController::getInstruction(int id)
 }
 
 void FGTowerController::render(bool visible) {
-    //cerr << "FGTowerController::render function not yet implemented" << endl;
+    //std::cerr << "FGTowerController::render function not yet implemented" << std::endl;
 }
 
 string FGTowerController::getName() {
@@ -1077,7 +1084,7 @@ void FGStartupController::announcePosition(int id,
     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
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1087,7 +1094,7 @@ void FGStartupController::announcePosition(int id,
         }
     }
     // Add a new TrafficRecord if no one exsists for this aircraft.
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         FGTrafficRecord rec;
         rec.setId(id);
 
@@ -1117,7 +1124,7 @@ bool FGStartupController::hasInstruction(int id)
     TrafficVectorIterator i = activeTraffic.begin();
     // Search search if the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1126,7 +1133,7 @@ bool FGStartupController::hasInstruction(int id)
             i++;
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
     } else {
@@ -1141,7 +1148,7 @@ FGATCInstruction FGStartupController::getInstruction(int id)
     TrafficVectorIterator i = activeTraffic.begin();
     // Search search if the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1150,7 +1157,7 @@ FGATCInstruction FGStartupController::getInstruction(int id)
             i++;
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
     } else {
@@ -1164,7 +1171,7 @@ void FGStartupController::signOff(int id)
     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
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1173,7 +1180,7 @@ void FGStartupController::signOff(int id)
             i++;
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
     } else {
@@ -1190,7 +1197,7 @@ bool FGStartupController::checkTransmissionState(int st, time_t now, time_t star
         if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
 
             //cerr << "Checking state " << st << " for " << i->getAircraft()->getCallSign() << endl;
-            static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
+            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);
@@ -1223,7 +1230,7 @@ void FGStartupController::updateAircraftInformation(int id, double lat, double l
     // Search search if the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
     TrafficVectorIterator current, closest;
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1308,7 +1315,7 @@ static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
 
 void FGStartupController::render(bool visible)
 {
-
+    //std::cerr << "Rendering startup controller" << std::endl;
     SGMaterialLib *matlib = globals->get_matlib();
     if (group) {
         //int nr = ;
@@ -1363,7 +1370,7 @@ void FGStartupController::render(bool visible)
                     } else {
                         elevationStart = ((i)->getAircraft()->_getAltitude() * SG_FEET_TO_METER);
                     }
-                    double elevationEnd   = segment->getEnd()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
+                    double elevationEnd   = segment->getEnd()->getElevationM();
                     if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
                         SGGeod center2 = end;
                         center2.setElevationM(SG_MAX_ELEVATION_M);
@@ -1402,9 +1409,9 @@ void FGStartupController::render(bool visible)
                     //osg::Node *custom_obj;
                     SGMaterial *mat;
                     if (segment->hasBlock(now)) {
-                        mat = matlib->find("UnidirectionalTaperRed");
+                        mat = matlib->find("UnidirectionalTaperRed", center);
                     } else {
-                        mat = matlib->find("UnidirectionalTaperGreen");
+                        mat = matlib->find("UnidirectionalTaperGreen", center);
                     }
                     if (mat)
                         geode->setEffect(mat->get_effect());
@@ -1425,8 +1432,8 @@ void FGStartupController::render(bool visible)
                         obj_trans->setDataVariance(osg::Object::STATIC);
                         FGTaxiSegment *segment  = parent->getGroundNetwork()->findSegment(k);
 
-                        double elevationStart = segment->getStart()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
-                        double elevationEnd   = segment->getEnd  ()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
+                        double elevationStart = segment->getStart()->getElevationM();
+                        double elevationEnd   = segment->getEnd  ()->getElevationM();
                         if ((elevationStart == 0) || (elevationStart == parent->getElevation())) {
                             SGGeod center2 = segment->getStart()->geod();
                             center2.setElevationM(SG_MAX_ELEVATION_M);
@@ -1480,9 +1487,9 @@ void FGStartupController::render(bool visible)
                         //osg::Node *custom_obj;
                         SGMaterial *mat;
                         if (segment->hasBlock(now)) {
-                            mat = matlib->find("UnidirectionalTaperRed");
+                            mat = matlib->find("UnidirectionalTaperRed", segCenter);
                         } else {
-                            mat = matlib->find("UnidirectionalTaperGreen");
+                            mat = matlib->find("UnidirectionalTaperGreen", segCenter);
                         }
                         if (mat)
                             geode->setEffect(mat->get_effect());
@@ -1535,7 +1542,7 @@ void FGApproachController::announcePosition(int id,
     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
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1545,7 +1552,7 @@ void FGApproachController::announcePosition(int id,
         }
     }
     // Add a new TrafficRecord if no one exsists for this aircraft.
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         FGTrafficRecord rec;
         rec.setId(id);
 
@@ -1568,7 +1575,7 @@ void FGApproachController::updateAircraftInformation(int id, double lat, double
     // Search search if the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
     TrafficVectorIterator current, closest;
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1578,7 +1585,7 @@ void FGApproachController::updateAircraftInformation(int id, double lat, double
         }
     }
 //    // update position of the current aircraft
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
     } else {
@@ -1617,7 +1624,7 @@ void FGApproachController::signOff(int id)
     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
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1626,7 +1633,7 @@ void FGApproachController::signOff(int id)
             i++;
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: Aircraft without traffic record is signing off from approach at " << SG_ORIGIN);
     } else {
@@ -1646,7 +1653,7 @@ bool FGApproachController::hasInstruction(int id)
     TrafficVectorIterator i = activeTraffic.begin();
     // Search search if the current id has an entry
     // This might be faster using a map instead of a vector, but let's start by taking a safe route
-    if (activeTraffic.size()) {
+    if (! activeTraffic.empty()) {
         //while ((i->getId() != id) && i != activeTraffic.end()) {
         while (i != activeTraffic.end()) {
             if (i->getId() == id) {
@@ -1655,7 +1662,7 @@ bool FGApproachController::hasInstruction(int id)
             i++;
         }
     }
-    if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
+    if (i == activeTraffic.end() || activeTraffic.empty()) {
         SG_LOG(SG_ATC, SG_ALERT,
                "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
     } else {
@@ -1709,7 +1716,7 @@ ActiveRunway *FGApproachController::getRunway(const string& name)
 }
 
 void FGApproachController::render(bool visible) {
-    //cerr << "FGApproachController::render function not yet implemented" << endl;
+    //std::cerr << "FGApproachController::render function not yet implemented" << std::endl;
 }