1 // NasalPositioned.cxx -- expose FGPositioned classes to Nasal
3 // Written by James Turner, started 2012.
5 // Copyright (C) 2012 James Turner
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "NasalPositioned.hxx"
29 #include <boost/foreach.hpp>
30 #include <boost/algorithm/string/case_conv.hpp>
32 #include <simgear/sg_inlines.h>
33 #include <simgear/scene/material/mat.hxx>
34 #include <simgear/magvar/magvar.hxx>
35 #include <simgear/timing/sg_time.hxx>
36 #include <simgear/bucket/newbucket.hxx>
38 #include <Airports/runways.hxx>
39 #include <Airports/simple.hxx>
40 #include <Airports/dynamics.hxx>
41 #include <Airports/parking.hxx>
42 #include <Scripting/NasalSys.hxx>
43 #include <Navaids/navlist.hxx>
44 #include <Navaids/procedure.hxx>
45 #include <Main/globals.hxx>
46 #include <Main/fg_props.hxx>
47 #include <Scenery/scenery.hxx>
48 #include <ATC/CommStation.hxx>
49 #include <Navaids/FlightPlan.hxx>
50 #include <Navaids/waypoint.hxx>
51 #include <Navaids/fix.hxx>
52 #include <Autopilot/route_mgr.hxx>
53 #include <Navaids/procedure.hxx>
54 #include <Navaids/airways.hxx>
56 using namespace flightgear;
58 static void positionedGhostDestroy(void* g);
59 static void wayptGhostDestroy(void* g);
60 static void legGhostDestroy(void* g);
61 static void routeBaseGhostDestroy(void* g);
63 naGhostType PositionedGhostType = { positionedGhostDestroy, "positioned" };
65 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out);
66 naGhostType AirportGhostType = { positionedGhostDestroy, "airport", airportGhostGetMember, 0 };
68 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out);
69 naGhostType NavaidGhostType = { positionedGhostDestroy, "navaid", navaidGhostGetMember, 0 };
71 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out);
72 naGhostType RunwayGhostType = { positionedGhostDestroy, "runway", runwayGhostGetMember, 0 };
74 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out);
75 naGhostType FixGhostType = { positionedGhostDestroy, "fix", fixGhostGetMember, 0 };
77 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out);
78 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value);
80 naGhostType WayptGhostType = { wayptGhostDestroy,
83 waypointGhostSetMember};
85 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out);
86 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value);
88 naGhostType FPLegGhostType = { legGhostDestroy,
93 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out);
94 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value);
96 naGhostType FlightPlanGhostType = { routeBaseGhostDestroy,
98 flightplanGhostGetMember,
99 flightplanGhostSetMember
102 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out);
103 naGhostType ProcedureGhostType = { routeBaseGhostDestroy,
105 procedureGhostGetMember,
108 static void hashset(naContext c, naRef hash, const char* key, naRef val)
110 naRef s = naNewString(c);
111 naStr_fromdata(s, (char*)key, strlen(key));
112 naHash_set(hash, s, val);
115 static naRef stringToNasal(naContext c, const std::string& s)
117 return naStr_fromdata(naNewString(c),
118 const_cast<char *>(s.c_str()),
122 static WayptFlag wayptFlagFromString(const char* s)
124 if (!strcmp(s, "sid")) return WPT_DEPARTURE;
125 if (!strcmp(s, "star")) return WPT_ARRIVAL;
126 if (!strcmp(s, "approach")) return WPT_APPROACH;
127 if (!strcmp(s, "missed")) return WPT_MISS;
128 if (!strcmp(s, "pseudo")) return WPT_PSEUDO;
130 return (WayptFlag) 0;
133 static naRef wayptFlagToNasal(naContext c, unsigned int flags)
135 if (flags & WPT_PSEUDO) return stringToNasal(c, "pseudo");
136 if (flags & WPT_DEPARTURE) return stringToNasal(c, "sid");
137 if (flags & WPT_ARRIVAL) return stringToNasal(c, "star");
138 if (flags & WPT_MISS) return stringToNasal(c, "missed");
139 if (flags & WPT_APPROACH) return stringToNasal(c, "approach");
143 static FGPositioned* positionedGhost(naRef r)
145 if ((naGhost_type(r) == &AirportGhostType) ||
146 (naGhost_type(r) == &NavaidGhostType) ||
147 (naGhost_type(r) == &RunwayGhostType) ||
148 (naGhost_type(r) == &FixGhostType))
150 return (FGPositioned*) naGhost_ptr(r);
156 static FGAirport* airportGhost(naRef r)
158 if (naGhost_type(r) == &AirportGhostType)
159 return (FGAirport*) naGhost_ptr(r);
163 static FGNavRecord* navaidGhost(naRef r)
165 if (naGhost_type(r) == &NavaidGhostType)
166 return (FGNavRecord*) naGhost_ptr(r);
170 static FGRunway* runwayGhost(naRef r)
172 if (naGhost_type(r) == &RunwayGhostType)
173 return (FGRunway*) naGhost_ptr(r);
177 static FGFix* fixGhost(naRef r)
179 if (naGhost_type(r) == &FixGhostType)
180 return (FGFix*) naGhost_ptr(r);
185 static void positionedGhostDestroy(void* g)
187 FGPositioned* pos = (FGPositioned*)g;
188 if (!FGPositioned::put(pos)) // unref
192 static Waypt* wayptGhost(naRef r)
194 if (naGhost_type(r) == &WayptGhostType)
195 return (Waypt*) naGhost_ptr(r);
197 if (naGhost_type(r) == &FPLegGhostType) {
198 FlightPlan::Leg* leg = (FlightPlan::Leg*) naGhost_ptr(r);
199 return leg->waypoint();
205 static void wayptGhostDestroy(void* g)
207 Waypt* wpt = (Waypt*)g;
208 if (!Waypt::put(wpt)) // unref
212 static void legGhostDestroy(void* g)
218 static FlightPlan::Leg* fpLegGhost(naRef r)
220 if (naGhost_type(r) == &FPLegGhostType)
221 return (FlightPlan::Leg*) naGhost_ptr(r);
225 static Procedure* procedureGhost(naRef r)
227 if (naGhost_type(r) == &ProcedureGhostType)
228 return (Procedure*) naGhost_ptr(r);
232 static FlightPlan* flightplanGhost(naRef r)
234 if (naGhost_type(r) == &FlightPlanGhostType)
235 return (FlightPlan*) naGhost_ptr(r);
239 static void routeBaseGhostDestroy(void* g)
244 static naRef airportPrototype;
245 static naRef flightplanPrototype;
246 static naRef waypointPrototype;
247 static naRef geoCoordClass;
248 static naRef fpLegPrototype;
249 static naRef procedurePrototype;
251 naRef ghostForAirport(naContext c, const FGAirport* apt)
257 FGPositioned::get(apt); // take a ref
258 return naNewGhost2(c, &AirportGhostType, (void*) apt);
261 naRef ghostForNavaid(naContext c, const FGNavRecord* n)
267 FGPositioned::get(n); // take a ref
268 return naNewGhost2(c, &NavaidGhostType, (void*) n);
271 naRef ghostForRunway(naContext c, const FGRunway* r)
277 FGPositioned::get(r); // take a ref
278 return naNewGhost2(c, &RunwayGhostType, (void*) r);
281 naRef ghostForFix(naContext c, const FGFix* r)
287 FGPositioned::get(r); // take a ref
288 return naNewGhost2(c, &FixGhostType, (void*) r);
292 naRef ghostForWaypt(naContext c, const Waypt* wpt)
298 Waypt::get(wpt); // take a ref
299 return naNewGhost2(c, &WayptGhostType, (void*) wpt);
302 naRef ghostForLeg(naContext c, const FlightPlan::Leg* leg)
308 return naNewGhost2(c, &FPLegGhostType, (void*) leg);
311 naRef ghostForFlightPlan(naContext c, const FlightPlan* fp)
317 return naNewGhost2(c, &FlightPlanGhostType, (void*) fp);
320 naRef ghostForProcedure(naContext c, const Procedure* proc)
326 return naNewGhost2(c, &ProcedureGhostType, (void*) proc);
329 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out)
331 const char* fieldName = naStr_data(field);
332 FGAirport* apt = (FGAirport*) g;
334 if (!strcmp(fieldName, "parents")) {
335 *out = naNewVector(c);
336 naVec_append(*out, airportPrototype);
337 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, apt->ident());
338 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, apt->name());
339 else if (!strcmp(fieldName, "lat")) *out = naNum(apt->getLatitude());
340 else if (!strcmp(fieldName, "lon")) *out = naNum(apt->getLongitude());
341 else if (!strcmp(fieldName, "elevation")) {
342 *out = naNum(apt->getElevation() * SG_FEET_TO_METER);
343 } else if (!strcmp(fieldName, "has_metar")) {
344 *out = naNum(apt->getMetar());
345 } else if (!strcmp(fieldName, "runways")) {
347 double minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft");
348 for(unsigned int r=0; r<apt->numRunways(); ++r) {
349 FGRunway* rwy(apt->getRunwayByIndex(r));
351 // ignore unusably short runways
352 if (rwy->lengthFt() < minLengthFt) {
356 naRef rwyid = stringToNasal(c, rwy->ident());
357 naRef rwydata = ghostForRunway(c, rwy);
358 naHash_set(*out, rwyid, rwydata);
368 static const char* waypointCommonGetMember(naContext c, Waypt* wpt, const char* fieldName, naRef* out)
370 if (!strcmp(fieldName, "wp_name")) *out = stringToNasal(c, wpt->ident());
371 else if (!strcmp(fieldName, "wp_type")) *out = stringToNasal(c, wpt->type());
372 else if (!strcmp(fieldName, "wp_role")) *out = wayptFlagToNasal(c, wpt->flags());
373 else if (!strcmp(fieldName, "wp_lat")) *out = naNum(wpt->position().getLatitudeDeg());
374 else if (!strcmp(fieldName, "wp_lon")) *out = naNum(wpt->position().getLongitudeDeg());
375 else if (!strcmp(fieldName, "wp_parent_name")) {
376 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
377 *out = proc ? stringToNasal(c, proc->ident()) : naNil();
378 } else if (!strcmp(fieldName, "wp_parent")) {
379 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
380 *out = ghostForProcedure(c, proc);
381 } else if (!strcmp(fieldName, "fly_type")) {
382 if (wpt->type() == "hold") {
383 *out = stringToNasal(c, "Hold");
385 *out = stringToNasal(c, wpt->flag(WPT_OVERFLIGHT) ? "flyOver" : "flyBy");
388 return NULL; // member not found
394 static void waypointCommonSetMember(naContext c, Waypt* wpt, const char* fieldName, naRef value)
396 if (!strcmp(fieldName, "wp_role")) {
397 if (!naIsString(value)) naRuntimeError(c, "wp_role must be a string");
398 if (wpt->owner() != NULL) naRuntimeError(c, "cannot override wp_role on waypoint with parent");
399 WayptFlag f = wayptFlagFromString(naStr_data(value));
401 naRuntimeError(c, "unrecognized wp_role value %s", naStr_data(value));
404 wpt->setFlag(f, true);
408 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out)
410 const char* fieldName = naStr_data(field);
411 Waypt* wpt = (flightgear::Waypt*) g;
412 return waypointCommonGetMember(c, wpt, fieldName, out);
415 static RouteRestriction routeRestrictionFromString(const char* s)
419 if (u == "computed") return RESTRICT_COMPUTED;
420 if (u == "at") return RESTRICT_AT;
421 if (u == "mach") return SPEED_RESTRICT_MACH;
422 if (u == "computed-mach") return SPEED_COMPUTED_MACH;
423 if (u == "delete") return RESTRICT_DELETE;
424 return RESTRICT_NONE;
427 naRef routeRestrictionToNasal(naContext c, RouteRestriction rr)
430 case RESTRICT_NONE: return naNil();
431 case RESTRICT_AT: return stringToNasal(c, "at");
432 case RESTRICT_ABOVE: return stringToNasal(c, "above");
433 case RESTRICT_BELOW: return stringToNasal(c, "below");
434 case SPEED_RESTRICT_MACH: return stringToNasal(c, "mach");
435 case RESTRICT_COMPUTED: return stringToNasal(c, "computed");
436 case SPEED_COMPUTED_MACH: return stringToNasal(c, "computed-mach");
437 case RESTRICT_DELETE: return stringToNasal(c, "delete");
443 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out)
445 const char* fieldName = naStr_data(field);
446 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
447 Waypt* wpt = leg->waypoint();
449 if (!strcmp(fieldName, "parents")) {
450 *out = naNewVector(c);
451 naVec_append(*out, fpLegPrototype);
452 } else if (!strcmp(fieldName, "index")) {
453 *out = naNum(leg->index());
454 } else if (!strcmp(fieldName, "alt_cstr")) {
455 *out = naNum(leg->altitudeFt());
456 } else if (!strcmp(fieldName, "alt_cstr_type")) {
457 *out = routeRestrictionToNasal(c, leg->altitudeRestriction());
458 } else if (!strcmp(fieldName, "speed_cstr")) {
459 double s = isMachRestrict(leg->speedRestriction()) ? leg->speedMach() : leg->speedKts();
461 } else if (!strcmp(fieldName, "speed_cstr_type")) {
462 *out = routeRestrictionToNasal(c, leg->speedRestriction());
463 } else if (!strcmp(fieldName, "leg_distance")) {
464 *out = naNum(leg->distanceNm());
465 } else if (!strcmp(fieldName, "leg_bearing")) {
466 *out = naNum(leg->courseDeg());
467 } else if (!strcmp(fieldName, "distance_along_route")) {
468 *out = naNum(leg->distanceAlongRoute());
469 } else { // check for fields defined on the underlying waypoint
470 return waypointCommonGetMember(c, wpt, fieldName, out);
473 return ""; // success
476 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value)
478 const char* fieldName = naStr_data(field);
479 Waypt* wpt = (Waypt*) g;
480 waypointCommonSetMember(c, wpt, fieldName, value);
483 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value)
485 const char* fieldName = naStr_data(field);
486 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
488 waypointCommonSetMember(c, leg->waypoint(), fieldName, value);
491 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out)
493 const char* fieldName = naStr_data(field);
494 FlightPlan* fp = (FlightPlan*) g;
496 if (!strcmp(fieldName, "parents")) {
497 *out = naNewVector(c);
498 naVec_append(*out, flightplanPrototype);
499 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fp->ident());
500 else if (!strcmp(fieldName, "departure")) *out = ghostForAirport(c, fp->departureAirport());
501 else if (!strcmp(fieldName, "destination")) *out = ghostForAirport(c, fp->destinationAirport());
502 else if (!strcmp(fieldName, "departure_runway")) *out = ghostForRunway(c, fp->departureRunway());
503 else if (!strcmp(fieldName, "destination_runway")) *out = ghostForRunway(c, fp->destinationRunway());
504 else if (!strcmp(fieldName, "sid")) *out = ghostForProcedure(c, fp->sid());
505 else if (!strcmp(fieldName, "sid_trans")) *out = ghostForProcedure(c, fp->sidTransition());
506 else if (!strcmp(fieldName, "star")) *out = ghostForProcedure(c, fp->star());
507 else if (!strcmp(fieldName, "star_trans")) *out = ghostForProcedure(c, fp->starTransition());
508 else if (!strcmp(fieldName, "approach")) *out = ghostForProcedure(c, fp->approach());
509 else if (!strcmp(fieldName, "current")) *out = naNum(fp->currentIndex());
517 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value)
519 const char* fieldName = naStr_data(field);
520 FlightPlan* fp = (FlightPlan*) g;
522 if (!strcmp(fieldName, "id")) {
523 if (!naIsString(value)) naRuntimeError(c, "flightplan.id must be a string");
524 fp->setIdent(naStr_data(value));
525 } else if (!strcmp(fieldName, "current")) {
526 int index = value.num;
527 if ((index < 0) || (index >= fp->numLegs())) {
530 fp->setCurrentIndex(index);
531 } else if (!strcmp(fieldName, "departure")) {
532 FGAirport* apt = airportGhost(value);
534 fp->setDeparture(apt);
538 FGRunway* rwy = runwayGhost(value);
540 fp->setDeparture(rwy);
544 naRuntimeError(c, "bad argument type setting departure");
545 } else if (!strcmp(fieldName, "destination")) {
546 FGAirport* apt = airportGhost(value);
548 fp->setDestination(apt);
552 FGRunway* rwy = runwayGhost(value);
554 fp->setDestination(rwy);
558 naRuntimeError(c, "bad argument type setting destination");
559 } else if (!strcmp(fieldName, "departure_runway")) {
560 FGRunway* rwy = runwayGhost(value);
562 fp->setDeparture(rwy);
566 naRuntimeError(c, "bad argument type setting departure");
567 } else if (!strcmp(fieldName, "destination_runway")) {
568 FGRunway* rwy = runwayGhost(value);
570 fp->setDestination(rwy);
574 naRuntimeError(c, "bad argument type setting departure");
575 } else if (!strcmp(fieldName, "sid")) {
576 Procedure* proc = procedureGhost(value);
577 if (proc && (proc->type() == PROCEDURE_SID)) {
578 fp->setSID((flightgear::SID*) proc);
581 // allow a SID transition to be set, implicitly include the SID itself
582 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
583 fp->setSID((Transition*) proc);
587 if (naIsString(value)) {
588 FGAirport* apt = fp->departureAirport();
589 fp->setSID(apt->findSIDWithIdent(naStr_data(value)));
593 naRuntimeError(c, "bad argument type setting SID");
594 } else if (!strcmp(fieldName, "star")) {
595 Procedure* proc = procedureGhost(value);
596 if (proc && (proc->type() == PROCEDURE_STAR)) {
597 fp->setSTAR((STAR*) proc);
601 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
602 fp->setSTAR((Transition*) proc);
606 if (naIsString(value)) {
607 FGAirport* apt = fp->destinationAirport();
608 fp->setSTAR(apt->findSTARWithIdent(naStr_data(value)));
612 naRuntimeError(c, "bad argument type setting STAR");
613 } else if (!strcmp(fieldName, "approach")) {
614 Procedure* proc = procedureGhost(value);
615 if (proc && Approach::isApproach(proc->type())) {
616 fp->setApproach((Approach*) proc);
620 if (naIsString(value)) {
621 FGAirport* apt = fp->destinationAirport();
622 fp->setApproach(apt->findApproachWithIdent(naStr_data(value)));
626 naRuntimeError(c, "bad argument type setting approach");
631 static naRef procedureTpType(naContext c, ProcedureType ty)
634 case PROCEDURE_SID: return stringToNasal(c, "sid");
635 case PROCEDURE_STAR: return stringToNasal(c, "star");
636 case PROCEDURE_APPROACH_VOR:
637 case PROCEDURE_APPROACH_ILS:
638 case PROCEDURE_APPROACH_RNAV:
639 case PROCEDURE_APPROACH_NDB:
640 return stringToNasal(c, "IAP");
646 static naRef procedureRadioType(naContext c, ProcedureType ty)
649 case PROCEDURE_APPROACH_VOR: return stringToNasal(c, "VOR");
650 case PROCEDURE_APPROACH_ILS: return stringToNasal(c, "ILS");
651 case PROCEDURE_APPROACH_RNAV: return stringToNasal(c, "RNAV");
652 case PROCEDURE_APPROACH_NDB: return stringToNasal(c, "NDB");
658 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out)
660 const char* fieldName = naStr_data(field);
661 Procedure* proc = (Procedure*) g;
663 if (!strcmp(fieldName, "parents")) {
664 *out = naNewVector(c);
665 naVec_append(*out, procedurePrototype);
666 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, proc->ident());
667 else if (!strcmp(fieldName, "airport")) *out = ghostForAirport(c, proc->airport());
668 else if (!strcmp(fieldName, "tp_type")) *out = procedureTpType(c, proc->type());
669 else if (!strcmp(fieldName, "radio")) *out = procedureRadioType(c, proc->type());
670 else if (!strcmp(fieldName, "runways")) {
671 *out = naNewVector(c);
672 BOOST_FOREACH(FGRunwayPtr rwy, proc->runways()) {
673 naVec_append(*out, stringToNasal(c, rwy->ident()));
675 } else if (!strcmp(fieldName, "transitions")) {
676 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
681 ArrivalDeparture* ad = static_cast<ArrivalDeparture*>(proc);
682 *out = naNewVector(c);
683 BOOST_FOREACH(string id, ad->transitionIdents()) {
684 naVec_append(*out, stringToNasal(c, id));
693 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
695 const char* fieldName = naStr_data(field);
696 FGRunway* rwy = (FGRunway*) g;
698 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, rwy->ident());
699 else if (!strcmp(fieldName, "lat")) *out = naNum(rwy->latitude());
700 else if (!strcmp(fieldName, "lon")) *out = naNum(rwy->longitude());
701 else if (!strcmp(fieldName, "heading")) *out = naNum(rwy->headingDeg());
702 else if (!strcmp(fieldName, "length")) *out = naNum(rwy->lengthM());
703 else if (!strcmp(fieldName, "width")) *out = naNum(rwy->widthM());
704 else if (!strcmp(fieldName, "threshold")) *out = naNum(rwy->displacedThresholdM());
705 else if (!strcmp(fieldName, "stopway")) *out = naNum(rwy->stopwayM());
706 else if (!strcmp(fieldName, "ils_frequency_mhz")) {
707 *out = rwy->ILS() ? naNum(rwy->ILS()->get_freq() / 100.0) : naNil();
708 } else if (!strcmp(fieldName, "ils")) {
709 *out = ghostForNavaid(c, rwy->ILS());
717 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out)
719 const char* fieldName = naStr_data(field);
720 FGNavRecord* nav = (FGNavRecord*) g;
722 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, nav->ident());
723 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, nav->name());
724 else if (!strcmp(fieldName, "lat")) *out = naNum(nav->get_lat());
725 else if (!strcmp(fieldName, "lon")) *out = naNum(nav->get_lon());
726 else if (!strcmp(fieldName, "elevation")) {
727 *out = naNum(nav->get_elev_ft() * SG_FEET_TO_METER);
728 } else if (!strcmp(fieldName, "type")) {
729 *out = stringToNasal(c, nav->nameForType(nav->type()));
730 } else if (!strcmp(fieldName, "frequency")) {
731 *out = naNum(nav->get_freq());
732 } else if (!strcmp(fieldName, "range_nm")) {
733 *out = naNum(nav->get_range());
734 } else if (!strcmp(fieldName, "course")) {
735 if ((nav->type() == FGPositioned::ILS) || (nav->type() == FGPositioned::LOC)) {
736 double radial = nav->get_multiuse();
737 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
738 *out = naNum(radial);
749 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out)
751 const char* fieldName = naStr_data(field);
752 FGFix* fix = (FGFix*) g;
754 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fix->ident());
755 else if (!strcmp(fieldName, "lat")) *out = naNum(fix->get_lat());
756 else if (!strcmp(fieldName, "lon")) *out = naNum(fix->get_lon());
764 static bool hashIsCoord(naRef h)
766 naRef parents = naHash_cget(h, (char*) "parents");
767 if (!naIsVector(parents)) {
771 return naEqual(naVec_get(parents, 0), geoCoordClass);
774 bool geodFromHash(naRef ref, SGGeod& result)
776 if (!naIsHash(ref)) {
781 // check for manual latitude / longitude names
782 naRef lat = naHash_cget(ref, (char*) "lat");
783 naRef lon = naHash_cget(ref, (char*) "lon");
784 if (naIsNum(lat) && naIsNum(lon)) {
785 result = SGGeod::fromDeg(naNumValue(lon).num, naNumValue(lat).num);
789 if (hashIsCoord(ref)) {
790 naRef lat = naHash_cget(ref, (char*) "_lat");
791 naRef lon = naHash_cget(ref, (char*) "_lon");
792 if (naIsNum(lat) && naIsNum(lon)) {
793 result = SGGeod::fromRad(naNumValue(lon).num, naNumValue(lat).num);
798 // check for any synonyms?
799 // latitude + longitude?
804 static int geodFromArgs(naRef* args, int offset, int argc, SGGeod& result)
806 if (offset >= argc) {
810 if (naIsGhost(args[offset])) {
811 naGhostType* gt = naGhost_type(args[offset]);
812 if (gt == &AirportGhostType) {
813 result = airportGhost(args[offset])->geod();
817 if (gt == &NavaidGhostType) {
818 result = navaidGhost(args[offset])->geod();
822 if (gt == &RunwayGhostType) {
823 result = runwayGhost(args[offset])->geod();
827 if (gt == &FixGhostType) {
828 result = fixGhost(args[offset])->geod();
832 if (gt == &WayptGhostType) {
833 result = wayptGhost(args[offset])->position();
838 if (geodFromHash(args[offset], result)) {
842 if (((argc - offset) >= 2) && naIsNum(args[offset]) && naIsNum(args[offset + 1])) {
843 double lat = naNumValue(args[0]).num,
844 lon = naNumValue(args[1]).num;
845 result = SGGeod::fromDeg(lon, lat);
852 // Convert a cartesian point to a geodetic lat/lon/altitude.
853 static naRef f_carttogeod(naContext c, naRef me, int argc, naRef* args)
855 double lat, lon, alt, xyz[3];
856 if(argc != 3) naRuntimeError(c, "carttogeod() expects 3 arguments");
857 for(int i=0; i<3; i++)
858 xyz[i] = naNumValue(args[i]).num;
859 sgCartToGeod(xyz, &lat, &lon, &alt);
860 lat *= SG_RADIANS_TO_DEGREES;
861 lon *= SG_RADIANS_TO_DEGREES;
862 naRef vec = naNewVector(c);
863 naVec_append(vec, naNum(lat));
864 naVec_append(vec, naNum(lon));
865 naVec_append(vec, naNum(alt));
869 // Convert a geodetic lat/lon/altitude to a cartesian point.
870 static naRef f_geodtocart(naContext c, naRef me, int argc, naRef* args)
872 if(argc != 3) naRuntimeError(c, "geodtocart() expects 3 arguments");
873 double lat = naNumValue(args[0]).num * SG_DEGREES_TO_RADIANS;
874 double lon = naNumValue(args[1]).num * SG_DEGREES_TO_RADIANS;
875 double alt = naNumValue(args[2]).num;
877 sgGeodToCart(lat, lon, alt, xyz);
878 naRef vec = naNewVector(c);
879 naVec_append(vec, naNum(xyz[0]));
880 naVec_append(vec, naNum(xyz[1]));
881 naVec_append(vec, naNum(xyz[2]));
885 // For given geodetic point return array with elevation, and a material data
886 // hash, or nil if there's no information available (tile not loaded). If
887 // information about the material isn't available, then nil is returned instead
889 static naRef f_geodinfo(naContext c, naRef me, int argc, naRef* args)
891 #define HASHSET(s,l,n) naHash_set(matdata, naStr_fromdata(naNewString(c),s,l),n)
892 if(argc < 2 || argc > 3)
893 naRuntimeError(c, "geodinfo() expects 2 or 3 arguments: lat, lon [, maxalt]");
894 double lat = naNumValue(args[0]).num;
895 double lon = naNumValue(args[1]).num;
896 double elev = argc == 3 ? naNumValue(args[2]).num : 10000;
897 const SGMaterial *mat;
898 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
899 if(!globals->get_scenery()->get_elevation_m(geod, elev, &mat))
901 naRef vec = naNewVector(c);
902 naVec_append(vec, naNum(elev));
903 naRef matdata = naNil();
905 matdata = naNewHash(c);
906 naRef names = naNewVector(c);
907 BOOST_FOREACH(const std::string& n, mat->get_names())
908 naVec_append(names, stringToNasal(c, n));
910 HASHSET("names", 5, names);
911 HASHSET("solid", 5, naNum(mat->get_solid()));
912 HASHSET("friction_factor", 15, naNum(mat->get_friction_factor()));
913 HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction()));
914 HASHSET("load_resistance", 15, naNum(mat->get_load_resistance()));
915 HASHSET("bumpiness", 9, naNum(mat->get_bumpiness()));
916 HASHSET("light_coverage", 14, naNum(mat->get_light_coverage()));
918 naVec_append(vec, matdata);
924 class AirportInfoFilter : public FGAirport::AirportFilter
927 AirportInfoFilter() : type(FGPositioned::AIRPORT) {
928 minRunwayLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
931 bool fromArg(naRef arg)
933 const char *s = naStr_data(arg);
934 if(!strcmp(s, "airport")) type = FGPositioned::AIRPORT;
935 else if(!strcmp(s, "seaport")) type = FGPositioned::SEAPORT;
936 else if(!strcmp(s, "heliport")) type = FGPositioned::HELIPORT;
943 virtual FGPositioned::Type minType() const {
947 virtual FGPositioned::Type maxType() const {
951 virtual bool pass(FGPositioned* aPos) const
953 FGAirport* apt = (FGAirport*) aPos;
954 if ((apt->type() == FGPositioned::AIRPORT) &&
955 !apt->hasHardRunwayOfLengthFt(minRunwayLengthFt))
963 FGPositioned::Type type;
964 double minRunwayLengthFt;
967 // Returns data hash for particular or nearest airport of a <type>, or nil
970 // airportinfo(<id>); e.g. "KSFO"
971 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
972 // airportinfo() same as airportinfo("airport")
973 // airportinfo(<lat>, <lon> [, <type>]);
974 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
976 SGGeod pos = globals->get_aircraft_position();
977 FGAirport* apt = NULL;
979 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
980 pos = SGGeod::fromDeg(args[1].num, args[0].num);
985 double maxRange = 10000.0; // expose this? or pick a smaller value?
987 AirportInfoFilter filter; // defaults to airports only
990 // fall through and use AIRPORT
991 } else if(argc == 1 && naIsString(args[0])) {
992 if (filter.fromArg(args[0])) {
995 // user provided an <id>, hopefully
996 apt = FGAirport::findByIdent(naStr_data(args[0]));
998 // return nil here, but don't raise a runtime error; this is a
999 // legitamate way to validate an ICAO code, for example in a
1000 // dialog box or similar.
1005 naRuntimeError(c, "airportinfo() with invalid function arguments");
1010 apt = FGAirport::findClosest(pos, maxRange, &filter);
1011 if(!apt) return naNil();
1014 return ghostForAirport(c, apt);
1017 static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
1020 SGGeod pos = globals->get_aircraft_position();
1021 argOffset += geodFromArgs(args, 0, argc, pos);
1023 if (!naIsNum(args[argOffset])) {
1024 naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset);
1027 AirportInfoFilter filter; // defaults to airports only
1028 double rangeNm = args[argOffset++].num;
1029 if (argOffset < argc) {
1030 filter.fromArg(args[argOffset++]);
1033 naRef r = naNewVector(c);
1035 FGPositioned::List apts = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1036 FGPositioned::sortByRange(apts, pos);
1038 BOOST_FOREACH(FGPositionedRef a, apts) {
1039 FGAirport* apt = (FGAirport*) a.get();
1040 naVec_append(r, ghostForAirport(c, apt));
1046 static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
1048 if (!naIsString(args[0])) {
1049 naRuntimeError(c, "findAirportsByICAO expects string as arg 0");
1053 string prefix(naStr_data(args[argOffset++]));
1054 AirportInfoFilter filter; // defaults to airports only
1055 if (argOffset < argc) {
1056 filter.fromArg(args[argOffset++]);
1059 naRef r = naNewVector(c);
1061 FGPositioned::List apts = FGPositioned::findAllWithIdent(prefix, &filter, false);
1063 BOOST_FOREACH(FGPositionedRef a, apts) {
1064 FGAirport* apt = (FGAirport*) a.get();
1065 naVec_append(r, ghostForAirport(c, apt));
1071 static naRef f_airport_tower(naContext c, naRef me, int argc, naRef* args)
1073 FGAirport* apt = airportGhost(me);
1075 naRuntimeError(c, "airport.tower called on non-airport object");
1078 // build a hash for the tower position
1079 SGGeod towerLoc = apt->getTowerLocation();
1080 naRef tower = naNewHash(c);
1081 hashset(c, tower, "lat", naNum(towerLoc.getLatitudeDeg()));
1082 hashset(c, tower, "lon", naNum(towerLoc.getLongitudeDeg()));
1083 hashset(c, tower, "elevation", naNum(towerLoc.getElevationM()));
1087 static naRef f_airport_comms(naContext c, naRef me, int argc, naRef* args)
1089 FGAirport* apt = airportGhost(me);
1091 naRuntimeError(c, "airport.comms called on non-airport object");
1093 naRef comms = naNewVector(c);
1095 // if we have an explicit type, return a simple vector of frequencies
1096 if (argc > 0 && naIsScalar(args[0])) {
1097 std::string commName = naStr_data(args[0]);
1098 FGPositioned::Type commType = FGPositioned::typeFromName(commName);
1100 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStationsOfType(commType)) {
1101 naVec_append(comms, naNum(comm->freqMHz()));
1104 // otherwise return a vector of hashes, one for each comm station.
1105 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStations()) {
1106 naRef commHash = naNewHash(c);
1107 hashset(c, commHash, "frequency", naNum(comm->freqMHz()));
1108 hashset(c, commHash, "ident", stringToNasal(c, comm->ident()));
1109 naVec_append(comms, commHash);
1116 static naRef f_airport_runway(naContext c, naRef me, int argc, naRef* args)
1118 FGAirport* apt = airportGhost(me);
1120 naRuntimeError(c, "airport.runway called on non-airport object");
1123 if ((argc < 1) || !naIsString(args[0])) {
1124 naRuntimeError(c, "airport.runway expects a runway ident argument");
1127 std::string ident(naStr_data(args[0]));
1128 boost::to_upper(ident);
1129 if (!apt->hasRunwayWithIdent(ident)) {
1133 return ghostForRunway(c, apt->getRunwayByIdent(ident));
1136 static naRef f_airport_sids(naContext c, naRef me, int argc, naRef* args)
1138 FGAirport* apt = airportGhost(me);
1140 naRuntimeError(c, "airport.sids called on non-airport object");
1143 naRef sids = naNewVector(c);
1145 FGRunway* rwy = NULL;
1146 if (argc > 0 && naIsString(args[0])) {
1147 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1151 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1152 } else if (argc > 0) {
1153 rwy = runwayGhost(args[0]);
1157 BOOST_FOREACH(flightgear::SID* sid, rwy->getSIDs()) {
1158 naRef procId = stringToNasal(c, sid->ident());
1159 naVec_append(sids, procId);
1162 for (unsigned int s=0; s<apt->numSIDs(); ++s) {
1163 flightgear::SID* sid = apt->getSIDByIndex(s);
1164 naRef procId = stringToNasal(c, sid->ident());
1165 naVec_append(sids, procId);
1172 static naRef f_airport_stars(naContext c, naRef me, int argc, naRef* args)
1174 FGAirport* apt = airportGhost(me);
1176 naRuntimeError(c, "airport.stars called on non-airport object");
1179 naRef stars = naNewVector(c);
1181 FGRunway* rwy = NULL;
1182 if (argc > 0 && naIsString(args[0])) {
1183 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1187 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1188 } else if (argc > 0) {
1189 rwy = runwayGhost(args[0]);
1193 BOOST_FOREACH(flightgear::STAR* s, rwy->getSTARs()) {
1194 naRef procId = stringToNasal(c, s->ident());
1195 naVec_append(stars, procId);
1198 for (unsigned int s=0; s<apt->numSTARs(); ++s) {
1199 flightgear::STAR* star = apt->getSTARByIndex(s);
1200 naRef procId = stringToNasal(c, star->ident());
1201 naVec_append(stars, procId);
1208 static naRef f_airport_approaches(naContext c, naRef me, int argc, naRef* args)
1210 FGAirport* apt = airportGhost(me);
1212 naRuntimeError(c, "airport.getApproachList called on non-airport object");
1215 naRef approaches = naNewVector(c);
1217 ProcedureType ty = PROCEDURE_INVALID;
1218 if ((argc > 1) && naIsString(args[1])) {
1219 std::string u(naStr_data(args[1]));
1221 if (u == "NDB") ty = PROCEDURE_APPROACH_NDB;
1222 if (u == "VOR") ty = PROCEDURE_APPROACH_VOR;
1223 if (u == "ILS") ty = PROCEDURE_APPROACH_ILS;
1224 if (u == "RNAV") ty = PROCEDURE_APPROACH_RNAV;
1227 FGRunway* rwy = NULL;
1228 if (argc > 0 && (rwy = runwayGhost(args[0]))) {
1230 } else if (argc > 0 && naIsString(args[0])) {
1231 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1235 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1239 BOOST_FOREACH(Approach* s, rwy->getApproaches()) {
1240 if ((ty != PROCEDURE_INVALID) && (s->type() != ty)) {
1244 naRef procId = stringToNasal(c, s->ident());
1245 naVec_append(approaches, procId);
1248 // no runway specified, report them all
1249 for (unsigned int s=0; s<apt->numApproaches(); ++s) {
1250 Approach* app = apt->getApproachByIndex(s);
1251 if ((ty != PROCEDURE_INVALID) && (app->type() != ty)) {
1255 naRef procId = stringToNasal(c, app->ident());
1256 naVec_append(approaches, procId);
1263 static naRef f_airport_parking(naContext c, naRef me, int argc, naRef* args)
1265 FGAirport* apt = airportGhost(me);
1267 naRuntimeError(c, "airport.parking called on non-airport object");
1270 naRef r = naNewVector(c);
1272 bool onlyAvailable = false;
1274 if (argc > 0 && naIsString(args[0])) {
1275 type = naStr_data(args[0]);
1278 if ((argc > 1) && naIsNum(args[1])) {
1279 onlyAvailable = (args[1].num != 0.0);
1282 FGAirportDynamics* dynamics = apt->getDynamics();
1283 for (int i=0; i<dynamics->getNrOfParkings(); ++i) {
1284 FGParking* park = dynamics->getParking(i);
1285 // filter out based on availability and type
1286 if (onlyAvailable && !park->isAvailable()) {
1290 if (!type.empty() && (park->getType() != type)) {
1294 const SGGeod& parkLoc = park->getGeod();
1295 naRef ph = naNewHash(c);
1296 hashset(c, ph, "name", stringToNasal(c, park->getName()));
1297 hashset(c, ph, "lat", naNum(parkLoc.getLatitudeDeg()));
1298 hashset(c, ph, "lon", naNum(parkLoc.getLongitudeDeg()));
1299 hashset(c, ph, "elevation", naNum(parkLoc.getElevationM()));
1300 naVec_append(r, ph);
1306 static naRef f_airport_getSid(naContext c, naRef me, int argc, naRef* args)
1308 FGAirport* apt = airportGhost(me);
1310 naRuntimeError(c, "airport.getSid called on non-airport object");
1313 if ((argc != 1) || !naIsString(args[0])) {
1314 naRuntimeError(c, "airport.getSid passed invalid argument");
1317 string ident = naStr_data(args[0]);
1318 return ghostForProcedure(c, apt->findSIDWithIdent(ident));
1321 static naRef f_airport_getStar(naContext c, naRef me, int argc, naRef* args)
1323 FGAirport* apt = airportGhost(me);
1325 naRuntimeError(c, "airport.getStar called on non-airport object");
1328 if ((argc != 1) || !naIsString(args[0])) {
1329 naRuntimeError(c, "airport.getStar passed invalid argument");
1332 string ident = naStr_data(args[0]);
1333 return ghostForProcedure(c, apt->findSTARWithIdent(ident));
1336 static naRef f_airport_getApproach(naContext c, naRef me, int argc, naRef* args)
1338 FGAirport* apt = airportGhost(me);
1340 naRuntimeError(c, "airport.getIAP called on non-airport object");
1343 if ((argc != 1) || !naIsString(args[0])) {
1344 naRuntimeError(c, "airport.getIAP passed invalid argument");
1347 string ident = naStr_data(args[0]);
1348 return ghostForProcedure(c, apt->findApproachWithIdent(ident));
1351 static naRef f_airport_toString(naContext c, naRef me, int argc, naRef* args)
1353 FGAirport* apt = airportGhost(me);
1355 naRuntimeError(c, "airport.tostring called on non-airport object");
1358 return stringToNasal(c, "an airport " + apt->ident());
1361 // Returns vector of data hash for navaid of a <type>, nil on error
1362 // navaids sorted by ascending distance
1363 // navinfo([<lat>,<lon>],[<type>],[<id>])
1364 // lat/lon (numeric): use latitude/longitude instead of ac position
1365 // type: ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
1366 // id: (partial) id of the fix
1368 // navinfo("vor") returns all vors
1369 // navinfo("HAM") return all navaids who's name start with "HAM"
1370 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
1371 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
1372 // sorted by distance relative to lat=34, lon=48
1373 static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
1377 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1378 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1382 pos = globals->get_aircraft_position();
1385 FGPositioned::Type type = FGPositioned::INVALID;
1386 nav_list_type navlist;
1387 const char * id = "";
1389 if(argc > 0 && naIsString(args[0])) {
1390 const char *s = naStr_data(args[0]);
1391 if(!strcmp(s, "any")) type = FGPositioned::INVALID;
1392 else if(!strcmp(s, "fix")) type = FGPositioned::FIX;
1393 else if(!strcmp(s, "vor")) type = FGPositioned::VOR;
1394 else if(!strcmp(s, "ndb")) type = FGPositioned::NDB;
1395 else if(!strcmp(s, "ils")) type = FGPositioned::ILS;
1396 else if(!strcmp(s, "dme")) type = FGPositioned::DME;
1397 else if(!strcmp(s, "tacan")) type = FGPositioned::TACAN;
1398 else id = s; // this is an id
1403 if(argc > 0 && naIsString(args[0])) {
1405 naRuntimeError(c, "navinfo() called with navaid id");
1408 id = naStr_data(args[0]);
1414 naRuntimeError(c, "navinfo() called with too many arguments");
1418 navlist = globals->get_navlist()->findByIdentAndFreq( pos, id, 0.0, type );
1420 naRef reply = naNewVector(c);
1421 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
1422 naVec_append( reply, ghostForNavaid(c, *it) );
1427 static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
1430 SGGeod pos = globals->get_aircraft_position();
1431 argOffset += geodFromArgs(args, 0, argc, pos);
1433 if (!naIsNum(args[argOffset])) {
1434 naRuntimeError(c, "findNavaidsWithinRange expected range (in nm) as arg %d", argOffset);
1437 FGPositioned::Type type = FGPositioned::INVALID;
1438 double rangeNm = args[argOffset++].num;
1439 if (argOffset < argc) {
1440 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1443 naRef r = naNewVector(c);
1444 FGNavList::TypeFilter filter(type);
1445 FGPositioned::List navs = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1446 FGPositioned::sortByRange(navs, pos);
1448 BOOST_FOREACH(FGPositionedRef a, navs) {
1449 FGNavRecord* nav = (FGNavRecord*) a.get();
1450 naVec_append(r, ghostForNavaid(c, nav));
1456 static naRef f_findNavaidByFrequency(naContext c, naRef me, int argc, naRef* args)
1459 SGGeod pos = globals->get_aircraft_position();
1460 argOffset += geodFromArgs(args, 0, argc, pos);
1462 if (!naIsNum(args[argOffset])) {
1463 naRuntimeError(c, "findNavaidByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1466 FGPositioned::Type type = FGPositioned::INVALID;
1467 double freqMhz = args[argOffset++].num;
1468 if (argOffset < argc) {
1469 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1472 nav_list_type navs = globals->get_navlist()->findAllByFreq(freqMhz, pos, type);
1477 return ghostForNavaid(c, navs.front().ptr());
1480 static naRef f_findNavaidsByFrequency(naContext c, naRef me, int argc, naRef* args)
1483 SGGeod pos = globals->get_aircraft_position();
1484 argOffset += geodFromArgs(args, 0, argc, pos);
1486 if (!naIsNum(args[argOffset])) {
1487 naRuntimeError(c, "findNavaidsByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1490 FGPositioned::Type type = FGPositioned::INVALID;
1491 double freqMhz = args[argOffset++].num;
1492 if (argOffset < argc) {
1493 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1496 naRef r = naNewVector(c);
1497 nav_list_type navs = globals->get_navlist()->findAllByFreq(freqMhz, pos, type);
1499 BOOST_FOREACH(nav_rec_ptr a, navs) {
1500 naVec_append(r, ghostForNavaid(c, a.ptr()));
1506 static naRef f_findNavaidsByIdent(naContext c, naRef me, int argc, naRef* args)
1509 SGGeod pos = globals->get_aircraft_position();
1510 argOffset += geodFromArgs(args, 0, argc, pos);
1512 if (!naIsString(args[argOffset])) {
1513 naRuntimeError(c, "findNavaidsByIdent expectes ident string as arg %d", argOffset);
1516 FGPositioned::Type type = FGPositioned::INVALID;
1517 string ident = naStr_data(args[argOffset++]);
1518 if (argOffset < argc) {
1519 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1522 naRef r = naNewVector(c);
1523 nav_list_type navs = globals->get_navlist()->findByIdentAndFreq(pos, ident, 0.0, type);
1525 BOOST_FOREACH(nav_rec_ptr a, navs) {
1526 naVec_append(r, ghostForNavaid(c, a.ptr()));
1532 static naRef f_findFixesByIdent(naContext c, naRef me, int argc, naRef* args)
1535 SGGeod pos = globals->get_aircraft_position();
1536 argOffset += geodFromArgs(args, 0, argc, pos);
1538 if (!naIsString(args[argOffset])) {
1539 naRuntimeError(c, "findFixesByIdent expectes ident string as arg %d", argOffset);
1542 string ident(naStr_data(args[argOffset]));
1543 naRef r = naNewVector(c);
1545 FGPositioned::TypeFilter filter(FGPositioned::FIX);
1546 FGPositioned::List fixes = FGPositioned::findAllWithIdent(ident, &filter);
1547 FGPositioned::sortByRange(fixes, pos);
1549 BOOST_FOREACH(FGPositionedRef f, fixes) {
1550 naVec_append(r, ghostForFix(c, (FGFix*) f.ptr()));
1556 // Convert a cartesian point to a geodetic lat/lon/altitude.
1557 static naRef f_magvar(naContext c, naRef me, int argc, naRef* args)
1559 SGGeod pos = globals->get_aircraft_position();
1561 // fine, use aircraft position
1562 } else if (geodFromArgs(args, 0, argc, pos)) {
1565 naRuntimeError(c, "magvar() expects no arguments, a positioned hash or lat,lon pair");
1568 double jd = globals->get_time_params()->getJD();
1569 double magvarDeg = sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
1570 return naNum(magvarDeg);
1573 static naRef f_courseAndDistance(naContext c, naRef me, int argc, naRef* args)
1575 SGGeod from = globals->get_aircraft_position(), to, p;
1576 int argOffset = geodFromArgs(args, 0, argc, p);
1577 if (geodFromArgs(args, argOffset, argc, to)) {
1578 from = p; // we parsed both FROM and TO args, so first was from
1580 to = p; // only parsed one arg, so FROM is current
1583 if (argOffset == 0) {
1584 naRuntimeError(c, "invalid arguments to courseAndDistance");
1587 double course, course2, d;
1588 SGGeodesy::inverse(from, to, course, course2, d);
1590 naRef result = naNewVector(c);
1591 naVec_append(result, naNum(course));
1592 naVec_append(result, naNum(d * SG_METER_TO_NM));
1596 static naRef f_greatCircleMove(naContext c, naRef me, int argc, naRef* args)
1598 SGGeod from = globals->get_aircraft_position(), to;
1601 // complication - don't inerpret two doubles (as the only args)
1602 // as a lat,lon pair - only do so if we have at least three args.
1604 argOffset = geodFromArgs(args, 0, argc, from);
1607 if ((argOffset + 1) >= argc) {
1608 naRuntimeError(c, "isufficent arguments to greatCircleMove");
1611 if (!naIsNum(args[argOffset]) || !naIsNum(args[argOffset+1])) {
1612 naRuntimeError(c, "invalid arguments %d and %d to greatCircleMove",
1613 argOffset, argOffset + 1);
1616 double course = args[argOffset].num, course2;
1617 double distanceNm = args[argOffset + 1].num;
1618 SGGeodesy::direct(from, course, distanceNm * SG_NM_TO_METER, to, course2);
1621 naRef coord = naNewHash(c);
1622 hashset(c, coord, "lat", naNum(to.getLatitudeDeg()));
1623 hashset(c, coord, "lon", naNum(to.getLongitudeDeg()));
1627 static naRef f_tilePath(naContext c, naRef me, int argc, naRef* args)
1629 SGGeod pos = globals->get_aircraft_position();
1630 geodFromArgs(args, 0, argc, pos);
1632 return stringToNasal(c, b.gen_base_path());
1635 static naRef f_tileIndex(naContext c, naRef me, int argc, naRef* args)
1637 SGGeod pos = globals->get_aircraft_position();
1638 geodFromArgs(args, 0, argc, pos);
1640 return naNum(b.gen_index());
1643 static naRef f_route(naContext c, naRef me, int argc, naRef* args)
1646 FGRouteMgr* rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
1647 return ghostForFlightPlan(c, rm->flightPlan());
1650 if ((argc > 0) && naIsString(args[0])) {
1651 flightgear::FlightPlan* fp = new flightgear::FlightPlan;
1652 SGPath path(naStr_data(args[0]));
1653 if (!path.exists()) {
1654 naRuntimeError(c, "flightplan, no file at path %s", path.c_str());
1657 if (!fp->load(path)) {
1658 SG_LOG(SG_NASAL, SG_WARN, "failed to load flight-plan from " << path);
1663 return ghostForFlightPlan(c, fp);
1666 naRuntimeError(c, "bad arguments to flightplan()");
1670 class NasalFPDelegate : public FlightPlan::Delegate
1673 NasalFPDelegate(FlightPlan* fp, FGNasalSys* sys, naRef ins) :
1678 SG_LOG(SG_NASAL, SG_INFO, "created Nasal delegate for " << fp);
1679 _gcSaveKey = _nasal->gcSave(ins);
1682 virtual ~NasalFPDelegate()
1684 SG_LOG(SG_NASAL, SG_INFO, "destroying Nasal delegate for " << _plan);
1685 _nasal->gcRelease(_gcSaveKey);
1688 virtual void departureChanged()
1690 callDelegateMethod("departureChanged");
1693 virtual void arrivalChanged()
1695 callDelegateMethod("arrivalChanged");
1698 virtual void waypointsChanged()
1700 callDelegateMethod("waypointsChanged");
1703 virtual void currentWaypointChanged()
1705 callDelegateMethod("currentWaypointChanged");
1709 void callDelegateMethod(const char* method)
1712 naMember_cget(_nasal->context(), _instance, method, &f);
1714 return; // no method on the delegate
1718 arg[0] = ghostForFlightPlan(_nasal->context(), _plan);
1719 _nasal->callMethod(f, _instance, 1, arg, naNil());
1728 class NasalFPDelegateFactory : public FlightPlan::DelegateFactory
1731 NasalFPDelegateFactory(naRef code)
1733 _nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1735 _gcSaveKey = _nasal->gcSave(_func);
1738 ~NasalFPDelegateFactory()
1740 _nasal->gcRelease(_gcSaveKey);
1743 virtual FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp)
1746 args[0] = ghostForFlightPlan(_nasal->context(), fp);
1747 naRef instance = _nasal->call(_func, 1, args, naNil());
1748 if (naIsNil(instance)) {
1752 return new NasalFPDelegate(fp, _nasal, instance);
1760 static naRef f_registerFPDelegate(naContext c, naRef me, int argc, naRef* args)
1762 if ((argc < 1) || !naIsFunc(args[0])) {
1763 naRuntimeError(c, "non-function argument to registerFlightPlanDelegate");
1766 NasalFPDelegateFactory* factory = new NasalFPDelegateFactory(args[0]);
1767 FlightPlan::registerDelegateFactory(factory);
1772 static WayptRef wayptFromArg(naRef arg)
1774 WayptRef r = wayptGhost(arg);
1779 FGPositioned* pos = positionedGhost(arg);
1781 // let's check if the arg is hash, coudl extra a geod and hence build
1782 // a simple waypoint
1787 // special-case for runways
1788 if (pos->type() == FGPositioned::RUNWAY) {
1789 return new RunwayWaypt((FGRunway*) pos, NULL);
1792 return new NavaidWaypoint(pos, NULL);
1795 static naRef convertWayptVecToNasal(naContext c, const WayptVec& wps)
1797 naRef result = naNewVector(c);
1798 BOOST_FOREACH(WayptRef wpt, wps) {
1799 naVec_append(result, ghostForWaypt(c, wpt.get()));
1804 static naRef f_airwaySearch(naContext c, naRef me, int argc, naRef* args)
1807 naRuntimeError(c, "airwaysSearch needs at least two arguments");
1810 WayptRef start = wayptFromArg(args[0]),
1811 end = wayptFromArg(args[1]);
1813 if (!start || !end) {
1814 SG_LOG(SG_NASAL, SG_WARN, "airwaysSearch: start or end points are invalid");
1818 bool highLevel = true;
1819 if ((argc > 2) && naIsString(args[2])) {
1820 if (!strcmp(naStr_data(args[2]), "lowlevel")) {
1827 Airway::highLevel()->route(start, end, route);
1829 Airway::lowLevel()->route(start, end, route);
1832 return convertWayptVecToNasal(c, route);
1835 static naRef f_createWP(naContext c, naRef me, int argc, naRef* args)
1838 int argOffset = geodFromArgs(args, 0, argc, pos);
1840 if (((argc - argOffset) < 1) || !naIsString(args[argOffset])) {
1841 naRuntimeError(c, "createWP: no identifier supplied");
1844 string ident = naStr_data(args[argOffset++]);
1845 WayptRef wpt = new BasicWaypt(pos, ident, NULL);
1847 // set waypt flags - approach, departure, pseudo, etc
1848 if (argc > argOffset) {
1849 WayptFlag f = wayptFlagFromString(naStr_data(args[argOffset++]));
1853 return ghostForWaypt(c, wpt);
1856 static naRef f_createWPFrom(naContext c, naRef me, int argc, naRef* args)
1859 naRuntimeError(c, "createWPFrom: need at least one argument");
1862 FGPositioned* positioned = positionedGhost(args[0]);
1864 naRuntimeError(c, "createWPFrom: couldn;t convert arg[0] to FGPositioned");
1868 if (positioned->type() == FGPositioned::RUNWAY) {
1869 wpt = new RunwayWaypt((FGRunway*) positioned, NULL);
1871 wpt = new NavaidWaypoint(positioned, NULL);
1874 // set waypt flags - approach, departure, pseudo, etc
1876 WayptFlag f = wayptFlagFromString(naStr_data(args[1]));
1880 return ghostForWaypt(c, wpt);
1883 static naRef f_flightplan_getWP(naContext c, naRef me, int argc, naRef* args)
1885 FlightPlan* fp = flightplanGhost(me);
1887 naRuntimeError(c, "flightplan.getWP called on non-flightplan object");
1892 index = fp->currentIndex();
1894 index = (int) naNumValue(args[0]).num;
1897 if ((index < 0) || (index >= fp->numLegs())) {
1901 return ghostForLeg(c, fp->legAtIndex(index));
1904 static naRef f_flightplan_currentWP(naContext c, naRef me, int argc, naRef* args)
1906 FlightPlan* fp = flightplanGhost(me);
1908 naRuntimeError(c, "flightplan.currentWP called on non-flightplan object");
1910 return ghostForLeg(c, fp->currentLeg());
1913 static naRef f_flightplan_nextWP(naContext c, naRef me, int argc, naRef* args)
1915 FlightPlan* fp = flightplanGhost(me);
1917 naRuntimeError(c, "flightplan.nextWP called on non-flightplan object");
1919 return ghostForLeg(c, fp->nextLeg());
1922 static naRef f_flightplan_numWaypoints(naContext c, naRef me, int argc, naRef* args)
1924 FlightPlan* fp = flightplanGhost(me);
1926 naRuntimeError(c, "flightplan.numWaypoints called on non-flightplan object");
1928 return naNum(fp->numLegs());
1931 static naRef f_flightplan_appendWP(naContext c, naRef me, int argc, naRef* args)
1933 FlightPlan* fp = flightplanGhost(me);
1935 naRuntimeError(c, "flightplan.appendWP called on non-flightplan object");
1938 WayptRef wp = wayptGhost(args[0]);
1939 int index = fp->numLegs();
1940 fp->insertWayptAtIndex(wp.get(), index);
1941 return naNum(index);
1944 static naRef f_flightplan_insertWP(naContext c, naRef me, int argc, naRef* args)
1946 FlightPlan* fp = flightplanGhost(me);
1948 naRuntimeError(c, "flightplan.insertWP called on non-flightplan object");
1951 WayptRef wp = wayptGhost(args[0]);
1952 int index = -1; // append
1953 if ((argc > 1) && naIsNum(args[1])) {
1954 index = (int) args[1].num;
1957 fp->insertWayptAtIndex(wp.get(), index);
1961 static naRef f_flightplan_insertWPAfter(naContext c, naRef me, int argc, naRef* args)
1963 FlightPlan* fp = flightplanGhost(me);
1965 naRuntimeError(c, "flightplan.insertWPAfter called on non-flightplan object");
1968 WayptRef wp = wayptGhost(args[0]);
1969 int index = -1; // append
1970 if ((argc > 1) && naIsNum(args[1])) {
1971 index = (int) args[1].num;
1974 fp->insertWayptAtIndex(wp.get(), index + 1);
1978 static naRef f_flightplan_insertWaypoints(naContext c, naRef me, int argc, naRef* args)
1980 FlightPlan* fp = flightplanGhost(me);
1982 naRuntimeError(c, "flightplan.insertWaypoints called on non-flightplan object");
1986 if (!naIsVector(args[0])) {
1987 naRuntimeError(c, "flightplan.insertWaypoints expects vector as first arg");
1990 int count = naVec_size(args[0]);
1991 for (int i=0; i<count; ++i) {
1992 Waypt* wp = wayptGhost(naVec_get(args[0], i));
1998 int index = -1; // append
1999 if ((argc > 1) && naIsNum(args[1])) {
2000 index = (int) args[1].num;
2003 fp->insertWayptsAtIndex(wps, index);
2007 static naRef f_flightplan_deleteWP(naContext c, naRef me, int argc, naRef* args)
2009 FlightPlan* fp = flightplanGhost(me);
2011 naRuntimeError(c, "flightplan.deleteWP called on non-flightplan object");
2014 if ((argc < 1) || !naIsNum(args[0])) {
2015 naRuntimeError(c, "bad argument to flightplan.deleteWP");
2018 int index = (int) args[0].num;
2019 fp->deleteIndex(index);
2023 static naRef f_flightplan_clearPlan(naContext c, naRef me, int argc, naRef* args)
2025 FlightPlan* fp = flightplanGhost(me);
2027 naRuntimeError(c, "flightplan.clearPlan called on non-flightplan object");
2034 static naRef f_flightplan_clearWPType(naContext c, naRef me, int argc, naRef* args)
2036 FlightPlan* fp = flightplanGhost(me);
2038 naRuntimeError(c, "flightplan.clearWPType called on non-flightplan object");
2042 naRuntimeError(c, "insufficent args to flightplan.clearWPType");
2045 WayptFlag flag = wayptFlagFromString(naStr_data(args[0]));
2046 fp->clearWayptsWithFlag(flag);
2050 static naRef f_flightplan_clone(naContext c, naRef me, int argc, naRef* args)
2052 FlightPlan* fp = flightplanGhost(me);
2054 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2057 return ghostForFlightPlan(c, fp->clone());
2060 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
2062 FlightPlan::Leg* leg = fpLegGhost(me);
2064 naRuntimeError(c, "leg.setSpeed called on non-flightplan-leg object");
2068 naRuntimeError(c, "bad arguments to leg.setSpeed");
2071 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2072 leg->setSpeed(rr, args[0].num);
2076 static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
2078 FlightPlan::Leg* leg = fpLegGhost(me);
2080 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2084 naRuntimeError(c, "bad arguments to leg.setAltitude");
2087 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2088 leg->setAltitude(rr, args[0].num);
2092 static naRef f_waypoint_navaid(naContext c, naRef me, int argc, naRef* args)
2094 flightgear::Waypt* w = wayptGhost(me);
2096 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2099 FGPositioned* pos = w->source();
2104 switch (pos->type()) {
2105 case FGPositioned::VOR:
2106 case FGPositioned::NDB:
2107 case FGPositioned::ILS:
2108 case FGPositioned::LOC:
2109 case FGPositioned::GS:
2110 case FGPositioned::DME:
2111 case FGPositioned::TACAN: {
2112 FGNavRecord* nav = (FGNavRecord*) pos;
2113 return ghostForNavaid(c, nav);
2121 static naRef f_waypoint_airport(naContext c, naRef me, int argc, naRef* args)
2123 flightgear::Waypt* w = wayptGhost(me);
2125 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2128 FGPositioned* pos = w->source();
2129 if (!pos || FGAirport::isAirportType(pos)) {
2133 return ghostForAirport(c, (FGAirport*) pos);
2136 static naRef f_waypoint_runway(naContext c, naRef me, int argc, naRef* args)
2138 flightgear::Waypt* w = wayptGhost(me);
2140 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2143 FGPositioned* pos = w->source();
2144 if (!pos || (pos->type() != FGPositioned::RUNWAY)) {
2148 return ghostForRunway(c, (FGRunway*) pos);
2151 static naRef f_procedure_transition(naContext c, naRef me, int argc, naRef* args)
2153 Procedure* proc = procedureGhost(me);
2155 naRuntimeError(c, "procedure.transition called on non-procedure object");
2158 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2159 naRuntimeError(c, "procedure.transition called on non-SID or -STAR");
2162 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2163 Transition* trans = ad->findTransitionByName(naStr_data(args[0]));
2165 return ghostForProcedure(c, trans);
2168 static naRef f_procedure_route(naContext c, naRef me, int argc, naRef* args)
2170 Procedure* proc = procedureGhost(me);
2172 naRuntimeError(c, "procedure.route called on non-procedure object");
2175 // wrapping up tow different routines here - approach routing from the IAF
2176 // to the associated runway, and SID/STAR routing via an enroute transition
2177 // and possibly a runway transition or not.
2178 if (Approach::isApproach(proc->type())) {
2181 iaf = wayptFromArg(args[0]);
2185 Approach* app = (Approach*) proc;
2186 if (!app->route(iaf, r)) {
2187 SG_LOG(SG_NASAL, SG_WARN, "procedure.route failed for Approach somehow");
2191 return convertWayptVecToNasal(c, r);
2192 } else if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2193 naRuntimeError(c, "procedure.route called on unsuitable procedure type");
2197 FGRunway* rwy = runwayGhost(args[0]);
2198 if (rwy) ++argOffset;
2200 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2201 Transition* trans = NULL;
2202 if (argOffset < argc) {
2203 trans = (Transition*) procedureGhost(args[argOffset]);
2206 // note either runway or trans may be NULL - that's ok
2208 if (!ad->route(rwy, trans, r)) {
2209 SG_LOG(SG_NASAL, SG_WARN, "prcoedure.route failed for ArrvialDeparture somehow");
2213 return convertWayptVecToNasal(c, r);
2217 // Table of extension functions. Terminate with zeros.
2218 static struct { const char* name; naCFunction func; } funcs[] = {
2219 { "carttogeod", f_carttogeod },
2220 { "geodtocart", f_geodtocart },
2221 { "geodinfo", f_geodinfo },
2222 { "airportinfo", f_airportinfo },
2223 { "findAirportsWithinRange", f_findAirportsWithinRange },
2224 { "findAirportsByICAO", f_findAirportsByICAO },
2225 { "navinfo", f_navinfo },
2226 { "findNavaidsWithinRange", f_findNavaidsWithinRange },
2227 { "findNavaidByFrequency", f_findNavaidByFrequency },
2228 { "findNavaidsByFrequency", f_findNavaidsByFrequency },
2229 { "findNavaidsByID", f_findNavaidsByIdent },
2230 { "findFixesByID", f_findFixesByIdent },
2231 { "flightplan", f_route },
2232 { "registerFlightPlanDelegate", f_registerFPDelegate },
2233 { "createWP", f_createWP },
2234 { "createWPFrom", f_createWPFrom },
2235 { "airwaysRoute", f_airwaySearch },
2236 { "magvar", f_magvar },
2237 { "courseAndDistance", f_courseAndDistance },
2238 { "greatCircleMove", f_greatCircleMove },
2239 { "tileIndex", f_tileIndex },
2240 { "tilePath", f_tilePath },
2245 naRef initNasalPositioned(naRef globals, naContext c, naRef gcSave)
2247 airportPrototype = naNewHash(c);
2248 hashset(c, gcSave, "airportProto", airportPrototype);
2250 hashset(c, airportPrototype, "runway", naNewFunc(c, naNewCCode(c, f_airport_runway)));
2251 hashset(c, airportPrototype, "tower", naNewFunc(c, naNewCCode(c, f_airport_tower)));
2252 hashset(c, airportPrototype, "comms", naNewFunc(c, naNewCCode(c, f_airport_comms)));
2253 hashset(c, airportPrototype, "sids", naNewFunc(c, naNewCCode(c, f_airport_sids)));
2254 hashset(c, airportPrototype, "stars", naNewFunc(c, naNewCCode(c, f_airport_stars)));
2255 hashset(c, airportPrototype, "getApproachList", naNewFunc(c, naNewCCode(c, f_airport_approaches)));
2256 hashset(c, airportPrototype, "parking", naNewFunc(c, naNewCCode(c, f_airport_parking)));
2257 hashset(c, airportPrototype, "getSid", naNewFunc(c, naNewCCode(c, f_airport_getSid)));
2258 hashset(c, airportPrototype, "getStar", naNewFunc(c, naNewCCode(c, f_airport_getStar)));
2259 hashset(c, airportPrototype, "getIAP", naNewFunc(c, naNewCCode(c, f_airport_getApproach)));
2260 hashset(c, airportPrototype, "tostring", naNewFunc(c, naNewCCode(c, f_airport_toString)));
2262 flightplanPrototype = naNewHash(c);
2263 hashset(c, gcSave, "flightplanProto", flightplanPrototype);
2265 hashset(c, flightplanPrototype, "getWP", naNewFunc(c, naNewCCode(c, f_flightplan_getWP)));
2266 hashset(c, flightplanPrototype, "currentWP", naNewFunc(c, naNewCCode(c, f_flightplan_currentWP)));
2267 hashset(c, flightplanPrototype, "nextWP", naNewFunc(c, naNewCCode(c, f_flightplan_nextWP)));
2268 hashset(c, flightplanPrototype, "getPlanSize", naNewFunc(c, naNewCCode(c, f_flightplan_numWaypoints)));
2269 hashset(c, flightplanPrototype, "appendWP", naNewFunc(c, naNewCCode(c, f_flightplan_appendWP)));
2270 hashset(c, flightplanPrototype, "insertWP", naNewFunc(c, naNewCCode(c, f_flightplan_insertWP)));
2271 hashset(c, flightplanPrototype, "deleteWP", naNewFunc(c, naNewCCode(c, f_flightplan_deleteWP)));
2272 hashset(c, flightplanPrototype, "insertWPAfter", naNewFunc(c, naNewCCode(c, f_flightplan_insertWPAfter)));
2273 hashset(c, flightplanPrototype, "insertWaypoints", naNewFunc(c, naNewCCode(c, f_flightplan_insertWaypoints)));
2274 hashset(c, flightplanPrototype, "cleanPlan", naNewFunc(c, naNewCCode(c, f_flightplan_clearPlan)));
2275 hashset(c, flightplanPrototype, "clearWPType", naNewFunc(c, naNewCCode(c, f_flightplan_clearWPType)));
2276 hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone)));
2278 waypointPrototype = naNewHash(c);
2279 hashset(c, gcSave, "wayptProto", waypointPrototype);
2281 hashset(c, waypointPrototype, "navaid", naNewFunc(c, naNewCCode(c, f_waypoint_navaid)));
2282 hashset(c, waypointPrototype, "runway", naNewFunc(c, naNewCCode(c, f_waypoint_runway)));
2283 hashset(c, waypointPrototype, "airport", naNewFunc(c, naNewCCode(c, f_waypoint_airport)));
2285 procedurePrototype = naNewHash(c);
2286 hashset(c, gcSave, "procedureProto", procedurePrototype);
2287 hashset(c, procedurePrototype, "transition", naNewFunc(c, naNewCCode(c, f_procedure_transition)));
2288 hashset(c, procedurePrototype, "route", naNewFunc(c, naNewCCode(c, f_procedure_route)));
2290 fpLegPrototype = naNewHash(c);
2291 hashset(c, gcSave, "fpLegProto", fpLegPrototype);
2292 hashset(c, fpLegPrototype, "setSpeed", naNewFunc(c, naNewCCode(c, f_leg_setSpeed)));
2293 hashset(c, fpLegPrototype, "setAltitude", naNewFunc(c, naNewCCode(c, f_leg_setAltitude)));
2295 for(int i=0; funcs[i].name; i++) {
2296 hashset(c, globals, funcs[i].name,
2297 naNewFunc(c, naNewCCode(c, funcs[i].func)));
2303 void postinitNasalPositioned(naRef globals, naContext c)
2305 naRef geoModule = naHash_cget(globals, (char*) "geo");
2306 if (naIsNil(geoModule)) {
2307 SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalPositioned: geo.nas not loaded");
2311 geoCoordClass = naHash_cget(geoModule, (char*) "Coord");