+ virtual void waypointsChanged()
+ {
+ callDelegateMethod("waypointsChanged");
+ }
+
+ virtual void currentWaypointChanged()
+ {
+ callDelegateMethod("currentWaypointChanged");
+ }
+private:
+
+ void callDelegateMethod(const char* method)
+ {
+ naRef f;
+ naMember_cget(_nasal->context(), _instance, method, &f);
+ if (naIsNil(f)) {
+ return; // no method on the delegate
+ }
+
+ naRef arg[1];
+ arg[0] = ghostForFlightPlan(_nasal->context(), _plan);
+ _nasal->callMethod(f, _instance, 1, arg, naNil());
+ }
+
+ FGNasalSys* _nasal;
+ FlightPlan* _plan;
+ naRef _instance;
+ int _gcSaveKey;
+};
+
+class NasalFPDelegateFactory : public FlightPlan::DelegateFactory
+{
+public:
+ NasalFPDelegateFactory(naRef code)
+ {
+ _nasal = (FGNasalSys*) globals->get_subsystem("nasal");
+ _func = code;
+ _gcSaveKey = _nasal->gcSave(_func);
+ }
+
+ ~NasalFPDelegateFactory()
+ {
+ _nasal->gcRelease(_gcSaveKey);
+ }
+
+ virtual FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp)
+ {
+ naRef args[1];
+ args[0] = ghostForFlightPlan(_nasal->context(), fp);
+ naRef instance = _nasal->call(_func, 1, args, naNil());
+ if (naIsNil(instance)) {
+ return NULL;
+ }
+
+ return new NasalFPDelegate(fp, _nasal, instance);
+ }
+private:
+ FGNasalSys* _nasal;
+ naRef _func;
+ int _gcSaveKey;
+};
+
+static naRef f_registerFPDelegate(naContext c, naRef me, int argc, naRef* args)
+{
+ if ((argc < 1) || !naIsFunc(args[0])) {
+ naRuntimeError(c, "non-function argument to registerFlightPlanDelegate");
+ }
+
+ NasalFPDelegateFactory* factory = new NasalFPDelegateFactory(args[0]);
+ FlightPlan::registerDelegateFactory(factory);
+
+ return naNil();
+}
+
+static WayptRef wayptFromArg(naRef arg)
+{
+ WayptRef r = wayptGhost(arg);
+ if (r.valid()) {
+ return r;
+ }
+
+ FGPositioned* pos = positionedGhost(arg);
+ if (!pos) {
+ // let's check if the arg is hash, coudl extra a geod and hence build
+ // a simple waypoint
+
+ return WayptRef();
+ }
+
+// special-case for runways
+ if (pos->type() == FGPositioned::RUNWAY) {
+ return new RunwayWaypt((FGRunway*) pos, NULL);
+ }
+
+ return new NavaidWaypoint(pos, NULL);
+}
+
+static naRef convertWayptVecToNasal(naContext c, const WayptVec& wps)
+{
+ naRef result = naNewVector(c);
+ BOOST_FOREACH(WayptRef wpt, wps) {
+ naVec_append(result, ghostForWaypt(c, wpt.get()));
+ }
+ return result;
+}
+
+static naRef f_airwaySearch(naContext c, naRef me, int argc, naRef* args)
+{
+ if (argc < 2) {
+ naRuntimeError(c, "airwaysSearch needs at least two arguments");
+ }
+
+ WayptRef start = wayptFromArg(args[0]),
+ end = wayptFromArg(args[1]);
+