]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIFlightPlanCreate.cxx
Interim windows build fix
[flightgear.git] / src / AIModel / AIFlightPlanCreate.cxx
index 7419552de7a0e05e11f8b6417d6e5ff1da93e262..59d4422601fe900a7b4a2c12b9f131097d942762 100644 (file)
 #endif
 
 #include <cstdlib>
+#include <cstdio>
 
 #include "AIFlightPlan.hxx"
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
 
-#include <Airports/simple.hxx>
+#include <Airports/airport.hxx>
 #include <Airports/runways.hxx>
 #include <Airports/dynamics.hxx>
+#include <Airports/groundnetwork.hxx>
+
 #include "AIAircraft.hxx"
 #include "performancedata.hxx"
 
+#include <Main/fg_props.hxx>
 #include <Environment/environment_mgr.hxx>
 #include <Environment/environment.hxx>
 #include <FDM/LaRCsim/basic_aero.h>
 #include <Navaids/navrecord.hxx>
+#include <Traffic/Schedule.hxx>
 
+using std::string;
 
 /* FGAIFlightPlan::create()
  * dynamically create a flight plan for AI traffic, based on data provided by the
@@ -103,6 +109,7 @@ bool FGAIFlightPlan::create(FGAIAircraft * ac, FGAirport * dep,
         SG_LOG(SG_AI, SG_ALERT,
                "AIFlightPlan::create() attempting to create unknown leg"
                " this is probably an internal program error");
+        break;
     }
     wpt_iterator = waypoints.begin() + currWpt;
     //don't  increment leg right away, but only once we pass the actual last waypoint that was created.
@@ -208,7 +215,7 @@ bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
                                        const string & acType,
                                        const string & airline)
 {
-
+    int route;
     // If this function is called during initialization,
     // make sure we obtain a valid gate ID first
     // and place the model at the location of the gate.
@@ -225,7 +232,7 @@ bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
       }
     }
 
-    string rwyClass = getRunwayClassFromTrafficType(fltType);
+    const string& rwyClass = getRunwayClassFromTrafficType(fltType);
 
     // Only set this if it hasn't been set by ATC already.
     if (activeRunway.empty()) {
@@ -244,12 +251,11 @@ bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
         return true;
     }
 
-    intVec ids;
-    PositionedID runwayId = 0;
+    FGTaxiNodeRef runwayNode;
     if (gn->getVersion() > 0) {
-        runwayId = gn->findNearestNodeOnRunway(runwayTakeoff);
+        FGTaxiNodeRef runwayNode = gn->findNearestNodeOnRunway(runwayTakeoff);
     } else {
-        runwayId = gn->findNearestNode(runwayTakeoff);
+        FGTaxiNodeRef runwayNode = gn->findNearestNode(runwayTakeoff);
     }
 
     // A negative gateId indicates an overflow parking, use a
@@ -260,25 +266,24 @@ bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
   //  taxiRoute = new FGTaxiRoute;
 
     // Determine which node to start from.
-    PositionedID node = 0;
+    FGTaxiNodeRef node;
     // Find out which node to start from
     FGParking *park = gate.parking();
     if (park) {
         node = park->getPushBackPoint();
-        if (node == -1) {
-            node = park->guid();
-        } else if (node == 0) {
-            // HAndle case where parking doens't have a node
+        if (node == 0) {
+            // Handle case where parking doesn't have a node
             if (firstFlight) {
-                node = park->guid();
+                node = park;
             } else {
                 node = lastNodeVisited;
             }
         }
     }
     
-    FGTaxiRoute taxiRoute = gn->findShortestRoute(node, runwayId);
-    intVecIterator i;
+       FGTaxiRoute taxiRoute;
+    if ( runwayNode )
+        taxiRoute = gn->findShortestRoute(node, runwayNode);
 
     if (taxiRoute.empty()) {
         createDefaultTakeoffTaxi(ac, apt, rwy);
@@ -286,6 +291,8 @@ bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
     }
 
     taxiRoute.first();
+    FGTaxiNodeRef skipNode;
+
     //bool isPushBackPoint = false;
     if (firstFlight) {
         // If this is called during initialization, randomly
@@ -295,28 +302,30 @@ bool FGAIFlightPlan::createTakeoffTaxi(FGAIAircraft * ac, bool firstFlight,
         // but make sure we always keep two active waypoints
         // to prevent a segmentation fault
         for (int i = 0; i < nrWaypointsToSkip - 3; i++) {
-            taxiRoute.next(&node);
+            taxiRoute.next(skipNode, &route);
         }
         
         gate.release(); // free up our gate as required
     } else {
         if (taxiRoute.size() > 1) {
-            taxiRoute.next(&node);     // chop off the first waypoint, because that is already the last of the pushback route
+            taxiRoute.next(skipNode, &route);     // chop off the first waypoint, because that is already the last of the pushback route
         }
     }
 
     // push each node on the taxi route as a waypoint
-  //  int route;
+    
     //cerr << "Building taxi route" << endl;
-    while (taxiRoute.next(&node)) {
+    
+    // Note that the line wpt->setRouteIndex was commented out by revision [afcdbd] 2012-01-01,
+    // which breaks the rendering functions. 
+    // These can probably be generated on the fly however. 
+    while (taxiRoute.next(node, &route)) {
         char buffer[10];
-        snprintf(buffer, 10, "%ld", node);
-        FGTaxiNode *tn =
-            apt->getDynamics()->getGroundNetwork()->findNode(node);
+        snprintf(buffer, 10, "%d", node->getIndex());
         FGAIWaypoint *wpt =
-            createOnGround(ac, buffer, tn->geod(), apt->getElevation(),
+            createOnGround(ac, buffer, node->geod(), apt->getElevation(),
                            ac->getPerformance()->vTaxi());
-       // wpt->setRouteIndex(route);
+        wpt->setRouteIndex(route);
         //cerr << "Nodes left " << taxiRoute->nodesLeft() << " ";
         if (taxiRoute.nodesLeft() == 1) {
             // Note that we actually have hold points in the ground network, but this is just an initial test.
@@ -368,6 +377,7 @@ bool FGAIFlightPlan::createLandingTaxi(FGAIAircraft * ac, FGAirport * apt,
                                        const string & acType,
                                        const string & airline)
 {
+    int route;
     gate = apt->getDynamics()->getAvailableParking(radius, fltType,
                                             acType, airline);
 
@@ -380,47 +390,40 @@ bool FGAIFlightPlan::createLandingTaxi(FGAIAircraft * ac, FGAirport * apt,
         return true;
     }
 
-    intVec ids;
-    PositionedID runwayId = 0;
+    FGTaxiNodeRef runwayNode;
     if (gn->getVersion() == 1) {
-        runwayId = gn->findNearestNodeOnRunway(lastWptPos);
+        runwayNode = gn->findNearestNodeOnRunway(lastWptPos);
     } else {
-        runwayId = gn->findNearestNode(lastWptPos);
+        runwayNode = gn->findNearestNode(lastWptPos);
     }
     //cerr << "Using network node " << runwayId << endl;
     // A negative gateId indicates an overflow parking, use a
     // fallback mechanism for this. 
-    // Starting from gate 0 is a bit of a hack...
-    //FGTaxiRoute route;
-  //  delete taxiRoute;
-   // taxiRoute = new FGTaxiRoute;
+    // Starting from gate 0 doesn't work, so don't try it
     FGTaxiRoute taxiRoute;
     if (gate.isValid())
-        taxiRoute = gn->findShortestRoute(runwayId, gate.parking()->guid());
-    else
-        taxiRoute = gn->findShortestRoute(runwayId, 0);
-    intVecIterator i;
+        taxiRoute = gn->findShortestRoute(runwayNode, gate.parking());
 
     if (taxiRoute.empty()) {
         createDefaultLandingTaxi(ac, apt);
         return true;
     }
 
-    PositionedID node;
+    FGTaxiNodeRef node;
     taxiRoute.first();
     int size = taxiRoute.size();
     // Omit the last two waypoints, as 
     // those are created by createParking()
    // int route;
     for (int i = 0; i < size - 2; i++) {
-        taxiRoute.next(&node);
+        taxiRoute.next(node, &route);
         char buffer[10];
-        snprintf(buffer, 10, "%ld", node);
-        FGTaxiNode *tn = gn->findNode(node);
+        snprintf(buffer, 10, "%d",  node->getIndex());
         FGAIWaypoint *wpt =
-            createOnGround(ac, buffer, tn->geod(), apt->getElevation(),
+            createOnGround(ac, buffer, node->geod(), apt->getElevation(),
                            ac->getPerformance()->vTaxi());
-       // wpt->setRouteIndex(route);
+        
+        wpt->setRouteIndex(route);
         pushBackWaypoint(wpt);
     }
     return true;
@@ -478,7 +481,7 @@ bool FGAIFlightPlan::createTakeOff(FGAIAircraft * ac, bool firstFlight,
     // NOTE: DT (2009-01-18: IIRC, this is currently already the case, 
     // because the getActive runway function takes care of that.
     if (firstFlight) {
-        string rwyClass = getRunwayClassFromTrafficType(fltType);
+        const string& rwyClass = getRunwayClassFromTrafficType(fltType);
         double heading = ac->getTrafficRef()->getCourse();
         apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
                                             heading);
@@ -539,7 +542,7 @@ bool FGAIFlightPlan::createClimb(FGAIAircraft * ac, bool firstFlight,
     double vClimb = ac->getPerformance()->vClimb();
   
     if (firstFlight) {
-        string rwyClass = getRunwayClassFromTrafficType(fltType);
+        const string& rwyClass = getRunwayClassFromTrafficType(fltType);
         double heading = ac->getTrafficRef()->getCourse();
         apt->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
                                             heading);
@@ -592,7 +595,7 @@ bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt,
     double vApproach = ac->getPerformance()->vApproach();
 
     //Beginning of Descent 
-    string rwyClass = getRunwayClassFromTrafficType(fltType);
+    const string& rwyClass = getRunwayClassFromTrafficType(fltType);
     double heading = ac->getTrafficRef()->getCourse();
     apt->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway,
                                         heading);
@@ -636,10 +639,9 @@ bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt,
     }
 
     double dAlt = 0; //  = alt - (apt->getElevation() + 2000);
-    FGTaxiNode * tn = 0;
+    FGTaxiNodeRef tn;
     if (apt->getDynamics()->getGroundNetwork()) {
-        int node = apt->getDynamics()->getGroundNetwork()->findNearestNode(refPoint);
-        tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
+        tn = apt->getDynamics()->getGroundNetwork()->findNearestNode(refPoint);
     }
   
     if (tn) {
@@ -844,7 +846,7 @@ bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt,
     if (reposition) {
         double tempDistance;
         //double minDistance = HUGE_VAL;
-        string wptName;
+        //string wptName;
         tempDistance = SGGeodesy::distanceM(current, initialTarget);
         time_t eta =
             tempDistance / ((vDescent * SG_NM_TO_METER) / 3600.0) + now;
@@ -965,14 +967,13 @@ bool FGAIFlightPlan::createLanding(FGAIAircraft * ac, FGAirport * apt,
     }
   
     coord = rwy->pointOnCenterline(mindist);
-    int nodeId = 0;
+    FGTaxiNodeRef tn;
     if (gn->getVersion() > 0) {
-        nodeId = gn->findNearestNodeOnRunway(coord, rwy);
+        tn = gn->findNearestNodeOnRunway(coord, rwy);
     } else {
-        nodeId = gn->findNearestNode(coord);
+        tn = gn->findNearestNode(coord);
     }
       
-    FGTaxiNode* tn = gn->findNode(nodeId);
     if (tn) {
         wpt = createOnGround(ac, buffer, tn->geod(), currElev, vTaxi);
         pushBackWaypoint(wpt);
@@ -1044,21 +1045,21 @@ bool FGAIFlightPlan::createParking(FGAIAircraft * ac, FGAirport * apt,
  * - ul (ultralight: I can imagine that these may share a runway with ga on some airports)
  * - mil (all military traffic)
  */
-string FGAIFlightPlan::getRunwayClassFromTrafficType(string fltType)
+const char* FGAIFlightPlan::getRunwayClassFromTrafficType(const string& fltType)
 {
     if ((fltType == "gate") || (fltType == "cargo")) {
-        return string("com");
+        return "com";
     }
     if (fltType == "ga") {
-        return string("gen");
+        return "gen";
     }
     if (fltType == "ul") {
-        return string("ul");
+        return "ul";
     }
     if ((fltType == "mil-fighter") || (fltType == "mil-transport")) {
-        return string("mil");
+        return "mil";
     }
-    return string("com");
+    return "com";
 }