From d806b687061c9ab545ea710576e488fed1721222 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Fri, 28 Nov 2014 22:00:38 +0000
Subject: [PATCH] find waypoints in the flight plan.

---
 src/Navaids/FlightPlan.cxx        | 11 +++++++++++
 src/Navaids/FlightPlan.hxx        |  1 +
 src/Scripting/NasalPositioned.cxx | 22 ++++++++++++++++++++++
 3 files changed, 34 insertions(+)

diff --git a/src/Navaids/FlightPlan.cxx b/src/Navaids/FlightPlan.cxx
index 4e7d7a67d..8a407c991 100644
--- a/src/Navaids/FlightPlan.cxx
+++ b/src/Navaids/FlightPlan.cxx
@@ -358,6 +358,17 @@ int FlightPlan::findWayptIndex(const SGGeod& aPos) const
   
   return -1;
 }
+  
+int FlightPlan::findWayptIndex(const FGPositionedRef aPos) const
+{
+  for (int i=0; i<numLegs(); ++i) {
+    if (_legs[i]->waypoint()->source() == aPos) {
+      return i;
+    }
+  }
+  
+  return -1;
+}
 
 FlightPlan::Leg* FlightPlan::currentLeg() const
 {
diff --git a/src/Navaids/FlightPlan.hxx b/src/Navaids/FlightPlan.hxx
index 11d98643d..93e421b3b 100644
--- a/src/Navaids/FlightPlan.hxx
+++ b/src/Navaids/FlightPlan.hxx
@@ -152,6 +152,7 @@ public:
   int findLegIndex(const Leg* l) const;
   
   int findWayptIndex(const SGGeod& aPos) const;
+  int findWayptIndex(const FGPositionedRef aPos) const;
   
   bool load(const SGPath& p);
   bool save(const SGPath& p);
diff --git a/src/Scripting/NasalPositioned.cxx b/src/Scripting/NasalPositioned.cxx
index 3e494e352..5386fc81d 100644
--- a/src/Scripting/NasalPositioned.cxx
+++ b/src/Scripting/NasalPositioned.cxx
@@ -2213,6 +2213,27 @@ static naRef f_flightplan_finish(naContext c, naRef me, int argc, naRef* args)
     return naNil();
 }
 
+static naRef f_flightplan_indexOfWp(naContext c, naRef me, int argc, naRef* args)
+{
+  FlightPlan* fp = flightplanGhost(me);
+  if (!fp) {
+    naRuntimeError(c, "flightplan.indexOfWP called on non-flightplan object");
+  }
+  
+  FGPositioned* positioned = positionedGhost(args[0]);
+  if (positioned) {
+    return naNum(fp->findWayptIndex(positioned));
+  }
+  
+  SGGeod pos;
+  int argOffset = geodFromArgs(args, 0, argc, pos);
+  if (argOffset > 0) {
+    return naNum(fp->findWayptIndex(pos));
+  }
+
+  return naNum(-1);
+}
+
 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
 {
   FlightPlan::Leg* leg = fpLegGhost(me);
@@ -2479,6 +2500,7 @@ naRef initNasalPositioned(naRef globals, naContext c)
     hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone))); 
     hashset(c, flightplanPrototype, "pathGeod", naNewFunc(c, naNewCCode(c, f_flightplan_pathGeod)));
     hashset(c, flightplanPrototype, "finish", naNewFunc(c, naNewCCode(c, f_flightplan_finish)));
+    hashset(c, flightplanPrototype, "indexOfWP", naNewFunc(c, naNewCCode(c, f_flightplan_indexOfWp)));
     
     waypointPrototype = naNewHash(c);
     naSave(c, waypointPrototype);