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 simgear::BVHMaterial *material;
898 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
899 if(!globals->get_scenery()->get_elevation_m(geod, elev, &material))
901 const SGMaterial *mat = dynamic_cast<const SGMaterial *>(material);
902 naRef vec = naNewVector(c);
903 naVec_append(vec, naNum(elev));
904 naRef matdata = naNil();
906 matdata = naNewHash(c);
907 naRef names = naNewVector(c);
908 BOOST_FOREACH(const std::string& n, mat->get_names())
909 naVec_append(names, stringToNasal(c, n));
911 HASHSET("names", 5, names);
912 HASHSET("solid", 5, naNum(mat->get_solid()));
913 HASHSET("friction_factor", 15, naNum(mat->get_friction_factor()));
914 HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction()));
915 HASHSET("load_resistance", 15, naNum(mat->get_load_resistance()));
916 HASHSET("bumpiness", 9, naNum(mat->get_bumpiness()));
917 HASHSET("light_coverage", 14, naNum(mat->get_light_coverage()));
919 naVec_append(vec, matdata);
925 class AirportInfoFilter : public FGAirport::AirportFilter
928 AirportInfoFilter() : type(FGPositioned::AIRPORT) {
929 minRunwayLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
932 bool fromArg(naRef arg)
934 const char *s = naStr_data(arg);
935 if(!strcmp(s, "airport")) type = FGPositioned::AIRPORT;
936 else if(!strcmp(s, "seaport")) type = FGPositioned::SEAPORT;
937 else if(!strcmp(s, "heliport")) type = FGPositioned::HELIPORT;
944 virtual FGPositioned::Type minType() const {
948 virtual FGPositioned::Type maxType() const {
952 virtual bool pass(FGPositioned* aPos) const
954 FGAirport* apt = (FGAirport*) aPos;
955 if ((apt->type() == FGPositioned::AIRPORT) &&
956 !apt->hasHardRunwayOfLengthFt(minRunwayLengthFt))
964 FGPositioned::Type type;
965 double minRunwayLengthFt;
968 // Returns data hash for particular or nearest airport of a <type>, or nil
971 // airportinfo(<id>); e.g. "KSFO"
972 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
973 // airportinfo() same as airportinfo("airport")
974 // airportinfo(<lat>, <lon> [, <type>]);
975 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
977 SGGeod pos = globals->get_aircraft_position();
978 FGAirport* apt = NULL;
980 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
981 pos = SGGeod::fromDeg(args[1].num, args[0].num);
986 double maxRange = 10000.0; // expose this? or pick a smaller value?
988 AirportInfoFilter filter; // defaults to airports only
991 // fall through and use AIRPORT
992 } else if(argc == 1 && naIsString(args[0])) {
993 if (filter.fromArg(args[0])) {
996 // user provided an <id>, hopefully
997 apt = FGAirport::findByIdent(naStr_data(args[0]));
999 // return nil here, but don't raise a runtime error; this is a
1000 // legitamate way to validate an ICAO code, for example in a
1001 // dialog box or similar.
1006 naRuntimeError(c, "airportinfo() with invalid function arguments");
1011 apt = FGAirport::findClosest(pos, maxRange, &filter);
1012 if(!apt) return naNil();
1015 return ghostForAirport(c, apt);
1018 static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
1021 SGGeod pos = globals->get_aircraft_position();
1022 argOffset += geodFromArgs(args, 0, argc, pos);
1024 if (!naIsNum(args[argOffset])) {
1025 naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset);
1028 AirportInfoFilter filter; // defaults to airports only
1029 double rangeNm = args[argOffset++].num;
1030 if (argOffset < argc) {
1031 filter.fromArg(args[argOffset++]);
1034 naRef r = naNewVector(c);
1036 FGPositioned::List apts = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1037 FGPositioned::sortByRange(apts, pos);
1039 BOOST_FOREACH(FGPositionedRef a, apts) {
1040 FGAirport* apt = (FGAirport*) a.get();
1041 naVec_append(r, ghostForAirport(c, apt));
1047 static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
1049 if (!naIsString(args[0])) {
1050 naRuntimeError(c, "findAirportsByICAO expects string as arg 0");
1054 string prefix(naStr_data(args[argOffset++]));
1055 AirportInfoFilter filter; // defaults to airports only
1056 if (argOffset < argc) {
1057 filter.fromArg(args[argOffset++]);
1060 naRef r = naNewVector(c);
1062 FGPositioned::List apts = FGPositioned::findAllWithIdent(prefix, &filter, false);
1064 BOOST_FOREACH(FGPositionedRef a, apts) {
1065 FGAirport* apt = (FGAirport*) a.get();
1066 naVec_append(r, ghostForAirport(c, apt));
1072 static naRef f_airport_tower(naContext c, naRef me, int argc, naRef* args)
1074 FGAirport* apt = airportGhost(me);
1076 naRuntimeError(c, "airport.tower called on non-airport object");
1079 // build a hash for the tower position
1080 SGGeod towerLoc = apt->getTowerLocation();
1081 naRef tower = naNewHash(c);
1082 hashset(c, tower, "lat", naNum(towerLoc.getLatitudeDeg()));
1083 hashset(c, tower, "lon", naNum(towerLoc.getLongitudeDeg()));
1084 hashset(c, tower, "elevation", naNum(towerLoc.getElevationM()));
1088 static naRef f_airport_comms(naContext c, naRef me, int argc, naRef* args)
1090 FGAirport* apt = airportGhost(me);
1092 naRuntimeError(c, "airport.comms called on non-airport object");
1094 naRef comms = naNewVector(c);
1096 // if we have an explicit type, return a simple vector of frequencies
1097 if (argc > 0 && naIsScalar(args[0])) {
1098 std::string commName = naStr_data(args[0]);
1099 FGPositioned::Type commType = FGPositioned::typeFromName(commName);
1101 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStationsOfType(commType)) {
1102 naVec_append(comms, naNum(comm->freqMHz()));
1105 // otherwise return a vector of hashes, one for each comm station.
1106 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStations()) {
1107 naRef commHash = naNewHash(c);
1108 hashset(c, commHash, "frequency", naNum(comm->freqMHz()));
1109 hashset(c, commHash, "ident", stringToNasal(c, comm->ident()));
1110 naVec_append(comms, commHash);
1117 static naRef f_airport_runway(naContext c, naRef me, int argc, naRef* args)
1119 FGAirport* apt = airportGhost(me);
1121 naRuntimeError(c, "airport.runway called on non-airport object");
1124 if ((argc < 1) || !naIsString(args[0])) {
1125 naRuntimeError(c, "airport.runway expects a runway ident argument");
1128 std::string ident(naStr_data(args[0]));
1129 boost::to_upper(ident);
1130 if (!apt->hasRunwayWithIdent(ident)) {
1134 return ghostForRunway(c, apt->getRunwayByIdent(ident));
1137 static naRef f_airport_sids(naContext c, naRef me, int argc, naRef* args)
1139 FGAirport* apt = airportGhost(me);
1141 naRuntimeError(c, "airport.sids called on non-airport object");
1144 naRef sids = naNewVector(c);
1146 FGRunway* rwy = NULL;
1147 if (argc > 0 && naIsString(args[0])) {
1148 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1152 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1153 } else if (argc > 0) {
1154 rwy = runwayGhost(args[0]);
1158 BOOST_FOREACH(flightgear::SID* sid, rwy->getSIDs()) {
1159 naRef procId = stringToNasal(c, sid->ident());
1160 naVec_append(sids, procId);
1163 for (unsigned int s=0; s<apt->numSIDs(); ++s) {
1164 flightgear::SID* sid = apt->getSIDByIndex(s);
1165 naRef procId = stringToNasal(c, sid->ident());
1166 naVec_append(sids, procId);
1173 static naRef f_airport_stars(naContext c, naRef me, int argc, naRef* args)
1175 FGAirport* apt = airportGhost(me);
1177 naRuntimeError(c, "airport.stars called on non-airport object");
1180 naRef stars = naNewVector(c);
1182 FGRunway* rwy = NULL;
1183 if (argc > 0 && naIsString(args[0])) {
1184 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1188 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1189 } else if (argc > 0) {
1190 rwy = runwayGhost(args[0]);
1194 BOOST_FOREACH(flightgear::STAR* s, rwy->getSTARs()) {
1195 naRef procId = stringToNasal(c, s->ident());
1196 naVec_append(stars, procId);
1199 for (unsigned int s=0; s<apt->numSTARs(); ++s) {
1200 flightgear::STAR* star = apt->getSTARByIndex(s);
1201 naRef procId = stringToNasal(c, star->ident());
1202 naVec_append(stars, procId);
1209 static naRef f_airport_approaches(naContext c, naRef me, int argc, naRef* args)
1211 FGAirport* apt = airportGhost(me);
1213 naRuntimeError(c, "airport.getApproachList called on non-airport object");
1216 naRef approaches = naNewVector(c);
1218 ProcedureType ty = PROCEDURE_INVALID;
1219 if ((argc > 1) && naIsString(args[1])) {
1220 std::string u(naStr_data(args[1]));
1222 if (u == "NDB") ty = PROCEDURE_APPROACH_NDB;
1223 if (u == "VOR") ty = PROCEDURE_APPROACH_VOR;
1224 if (u == "ILS") ty = PROCEDURE_APPROACH_ILS;
1225 if (u == "RNAV") ty = PROCEDURE_APPROACH_RNAV;
1228 FGRunway* rwy = NULL;
1229 if (argc > 0 && (rwy = runwayGhost(args[0]))) {
1231 } else if (argc > 0 && naIsString(args[0])) {
1232 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1236 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1240 BOOST_FOREACH(Approach* s, rwy->getApproaches()) {
1241 if ((ty != PROCEDURE_INVALID) && (s->type() != ty)) {
1245 naRef procId = stringToNasal(c, s->ident());
1246 naVec_append(approaches, procId);
1249 // no runway specified, report them all
1250 for (unsigned int s=0; s<apt->numApproaches(); ++s) {
1251 Approach* app = apt->getApproachByIndex(s);
1252 if ((ty != PROCEDURE_INVALID) && (app->type() != ty)) {
1256 naRef procId = stringToNasal(c, app->ident());
1257 naVec_append(approaches, procId);
1264 static naRef f_airport_parking(naContext c, naRef me, int argc, naRef* args)
1266 FGAirport* apt = airportGhost(me);
1268 naRuntimeError(c, "airport.parking called on non-airport object");
1271 naRef r = naNewVector(c);
1273 bool onlyAvailable = false;
1275 if (argc > 0 && naIsString(args[0])) {
1276 type = naStr_data(args[0]);
1279 if ((argc > 1) && naIsNum(args[1])) {
1280 onlyAvailable = (args[1].num != 0.0);
1283 FGAirportDynamics* dynamics = apt->getDynamics();
1284 for (int i=0; i<dynamics->getNrOfParkings(); ++i) {
1285 FGParking* park = dynamics->getParking(i);
1286 // filter out based on availability and type
1287 if (onlyAvailable && !park->isAvailable()) {
1291 if (!type.empty() && (park->getType() != type)) {
1295 const SGGeod& parkLoc = park->getGeod();
1296 naRef ph = naNewHash(c);
1297 hashset(c, ph, "name", stringToNasal(c, park->getName()));
1298 hashset(c, ph, "lat", naNum(parkLoc.getLatitudeDeg()));
1299 hashset(c, ph, "lon", naNum(parkLoc.getLongitudeDeg()));
1300 hashset(c, ph, "elevation", naNum(parkLoc.getElevationM()));
1301 naVec_append(r, ph);
1307 static naRef f_airport_getSid(naContext c, naRef me, int argc, naRef* args)
1309 FGAirport* apt = airportGhost(me);
1311 naRuntimeError(c, "airport.getSid called on non-airport object");
1314 if ((argc != 1) || !naIsString(args[0])) {
1315 naRuntimeError(c, "airport.getSid passed invalid argument");
1318 string ident = naStr_data(args[0]);
1319 return ghostForProcedure(c, apt->findSIDWithIdent(ident));
1322 static naRef f_airport_getStar(naContext c, naRef me, int argc, naRef* args)
1324 FGAirport* apt = airportGhost(me);
1326 naRuntimeError(c, "airport.getStar called on non-airport object");
1329 if ((argc != 1) || !naIsString(args[0])) {
1330 naRuntimeError(c, "airport.getStar passed invalid argument");
1333 string ident = naStr_data(args[0]);
1334 return ghostForProcedure(c, apt->findSTARWithIdent(ident));
1337 static naRef f_airport_getApproach(naContext c, naRef me, int argc, naRef* args)
1339 FGAirport* apt = airportGhost(me);
1341 naRuntimeError(c, "airport.getIAP called on non-airport object");
1344 if ((argc != 1) || !naIsString(args[0])) {
1345 naRuntimeError(c, "airport.getIAP passed invalid argument");
1348 string ident = naStr_data(args[0]);
1349 return ghostForProcedure(c, apt->findApproachWithIdent(ident));
1352 static naRef f_airport_toString(naContext c, naRef me, int argc, naRef* args)
1354 FGAirport* apt = airportGhost(me);
1356 naRuntimeError(c, "airport.tostring called on non-airport object");
1359 return stringToNasal(c, "an airport " + apt->ident());
1362 // Returns vector of data hash for navaid of a <type>, nil on error
1363 // navaids sorted by ascending distance
1364 // navinfo([<lat>,<lon>],[<type>],[<id>])
1365 // lat/lon (numeric): use latitude/longitude instead of ac position
1366 // type: ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
1367 // id: (partial) id of the fix
1369 // navinfo("vor") returns all vors
1370 // navinfo("HAM") return all navaids who's name start with "HAM"
1371 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
1372 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
1373 // sorted by distance relative to lat=34, lon=48
1374 static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
1378 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1379 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1383 pos = globals->get_aircraft_position();
1386 FGPositioned::Type type = FGPositioned::INVALID;
1387 nav_list_type navlist;
1388 const char * id = "";
1390 if(argc > 0 && naIsString(args[0])) {
1391 const char *s = naStr_data(args[0]);
1392 if(!strcmp(s, "any")) type = FGPositioned::INVALID;
1393 else if(!strcmp(s, "fix")) type = FGPositioned::FIX;
1394 else if(!strcmp(s, "vor")) type = FGPositioned::VOR;
1395 else if(!strcmp(s, "ndb")) type = FGPositioned::NDB;
1396 else if(!strcmp(s, "ils")) type = FGPositioned::ILS;
1397 else if(!strcmp(s, "dme")) type = FGPositioned::DME;
1398 else if(!strcmp(s, "tacan")) type = FGPositioned::TACAN;
1399 else id = s; // this is an id
1404 if(argc > 0 && naIsString(args[0])) {
1406 naRuntimeError(c, "navinfo() called with navaid id");
1409 id = naStr_data(args[0]);
1415 naRuntimeError(c, "navinfo() called with too many arguments");
1419 navlist = globals->get_navlist()->findByIdentAndFreq( pos, id, 0.0, type );
1421 naRef reply = naNewVector(c);
1422 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
1423 naVec_append( reply, ghostForNavaid(c, *it) );
1428 static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
1431 SGGeod pos = globals->get_aircraft_position();
1432 argOffset += geodFromArgs(args, 0, argc, pos);
1434 if (!naIsNum(args[argOffset])) {
1435 naRuntimeError(c, "findNavaidsWithinRange expected range (in nm) as arg %d", argOffset);
1438 FGPositioned::Type type = FGPositioned::INVALID;
1439 double rangeNm = args[argOffset++].num;
1440 if (argOffset < argc) {
1441 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1444 naRef r = naNewVector(c);
1445 FGNavList::TypeFilter filter(type);
1446 FGPositioned::List navs = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1447 FGPositioned::sortByRange(navs, pos);
1449 BOOST_FOREACH(FGPositionedRef a, navs) {
1450 FGNavRecord* nav = (FGNavRecord*) a.get();
1451 naVec_append(r, ghostForNavaid(c, nav));
1457 static naRef f_findNavaidByFrequency(naContext c, naRef me, int argc, naRef* args)
1460 SGGeod pos = globals->get_aircraft_position();
1461 argOffset += geodFromArgs(args, 0, argc, pos);
1463 if (!naIsNum(args[argOffset])) {
1464 naRuntimeError(c, "findNavaidByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1467 FGPositioned::Type type = FGPositioned::INVALID;
1468 double freqMhz = args[argOffset++].num;
1469 if (argOffset < argc) {
1470 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1473 nav_list_type navs = globals->get_navlist()->findAllByFreq(freqMhz, pos, type);
1478 return ghostForNavaid(c, navs.front().ptr());
1481 static naRef f_findNavaidsByFrequency(naContext c, naRef me, int argc, naRef* args)
1484 SGGeod pos = globals->get_aircraft_position();
1485 argOffset += geodFromArgs(args, 0, argc, pos);
1487 if (!naIsNum(args[argOffset])) {
1488 naRuntimeError(c, "findNavaidsByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1491 FGPositioned::Type type = FGPositioned::INVALID;
1492 double freqMhz = args[argOffset++].num;
1493 if (argOffset < argc) {
1494 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1497 naRef r = naNewVector(c);
1498 nav_list_type navs = globals->get_navlist()->findAllByFreq(freqMhz, pos, type);
1500 BOOST_FOREACH(nav_rec_ptr a, navs) {
1501 naVec_append(r, ghostForNavaid(c, a.ptr()));
1507 static naRef f_findNavaidsByIdent(naContext c, naRef me, int argc, naRef* args)
1510 SGGeod pos = globals->get_aircraft_position();
1511 argOffset += geodFromArgs(args, 0, argc, pos);
1513 if (!naIsString(args[argOffset])) {
1514 naRuntimeError(c, "findNavaidsByIdent expectes ident string as arg %d", argOffset);
1517 FGPositioned::Type type = FGPositioned::INVALID;
1518 string ident = naStr_data(args[argOffset++]);
1519 if (argOffset < argc) {
1520 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1523 naRef r = naNewVector(c);
1524 nav_list_type navs = globals->get_navlist()->findByIdentAndFreq(pos, ident, 0.0, type);
1526 BOOST_FOREACH(nav_rec_ptr a, navs) {
1527 naVec_append(r, ghostForNavaid(c, a.ptr()));
1533 static naRef f_findFixesByIdent(naContext c, naRef me, int argc, naRef* args)
1536 SGGeod pos = globals->get_aircraft_position();
1537 argOffset += geodFromArgs(args, 0, argc, pos);
1539 if (!naIsString(args[argOffset])) {
1540 naRuntimeError(c, "findFixesByIdent expectes ident string as arg %d", argOffset);
1543 string ident(naStr_data(args[argOffset]));
1544 naRef r = naNewVector(c);
1546 FGPositioned::TypeFilter filter(FGPositioned::FIX);
1547 FGPositioned::List fixes = FGPositioned::findAllWithIdent(ident, &filter);
1548 FGPositioned::sortByRange(fixes, pos);
1550 BOOST_FOREACH(FGPositionedRef f, fixes) {
1551 naVec_append(r, ghostForFix(c, (FGFix*) f.ptr()));
1557 // Convert a cartesian point to a geodetic lat/lon/altitude.
1558 static naRef f_magvar(naContext c, naRef me, int argc, naRef* args)
1560 SGGeod pos = globals->get_aircraft_position();
1562 // fine, use aircraft position
1563 } else if (geodFromArgs(args, 0, argc, pos)) {
1566 naRuntimeError(c, "magvar() expects no arguments, a positioned hash or lat,lon pair");
1569 double jd = globals->get_time_params()->getJD();
1570 double magvarDeg = sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
1571 return naNum(magvarDeg);
1574 static naRef f_courseAndDistance(naContext c, naRef me, int argc, naRef* args)
1576 SGGeod from = globals->get_aircraft_position(), to, p;
1577 int argOffset = geodFromArgs(args, 0, argc, p);
1578 if (geodFromArgs(args, argOffset, argc, to)) {
1579 from = p; // we parsed both FROM and TO args, so first was from
1581 to = p; // only parsed one arg, so FROM is current
1584 if (argOffset == 0) {
1585 naRuntimeError(c, "invalid arguments to courseAndDistance");
1588 double course, course2, d;
1589 SGGeodesy::inverse(from, to, course, course2, d);
1591 naRef result = naNewVector(c);
1592 naVec_append(result, naNum(course));
1593 naVec_append(result, naNum(d * SG_METER_TO_NM));
1597 static naRef f_greatCircleMove(naContext c, naRef me, int argc, naRef* args)
1599 SGGeod from = globals->get_aircraft_position(), to;
1602 // complication - don't inerpret two doubles (as the only args)
1603 // as a lat,lon pair - only do so if we have at least three args.
1605 argOffset = geodFromArgs(args, 0, argc, from);
1608 if ((argOffset + 1) >= argc) {
1609 naRuntimeError(c, "isufficent arguments to greatCircleMove");
1612 if (!naIsNum(args[argOffset]) || !naIsNum(args[argOffset+1])) {
1613 naRuntimeError(c, "invalid arguments %d and %d to greatCircleMove",
1614 argOffset, argOffset + 1);
1617 double course = args[argOffset].num, course2;
1618 double distanceNm = args[argOffset + 1].num;
1619 SGGeodesy::direct(from, course, distanceNm * SG_NM_TO_METER, to, course2);
1622 naRef coord = naNewHash(c);
1623 hashset(c, coord, "lat", naNum(to.getLatitudeDeg()));
1624 hashset(c, coord, "lon", naNum(to.getLongitudeDeg()));
1628 static naRef f_tilePath(naContext c, naRef me, int argc, naRef* args)
1630 SGGeod pos = globals->get_aircraft_position();
1631 geodFromArgs(args, 0, argc, pos);
1633 return stringToNasal(c, b.gen_base_path());
1636 static naRef f_tileIndex(naContext c, naRef me, int argc, naRef* args)
1638 SGGeod pos = globals->get_aircraft_position();
1639 geodFromArgs(args, 0, argc, pos);
1641 return naNum(b.gen_index());
1644 static naRef f_route(naContext c, naRef me, int argc, naRef* args)
1647 FGRouteMgr* rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
1648 return ghostForFlightPlan(c, rm->flightPlan());
1651 if ((argc > 0) && naIsString(args[0])) {
1652 flightgear::FlightPlan* fp = new flightgear::FlightPlan;
1653 SGPath path(naStr_data(args[0]));
1654 if (!path.exists()) {
1655 naRuntimeError(c, "flightplan, no file at path %s", path.c_str());
1658 if (!fp->load(path)) {
1659 SG_LOG(SG_NASAL, SG_WARN, "failed to load flight-plan from " << path);
1664 return ghostForFlightPlan(c, fp);
1667 naRuntimeError(c, "bad arguments to flightplan()");
1671 class NasalFPDelegate : public FlightPlan::Delegate
1674 NasalFPDelegate(FlightPlan* fp, FGNasalSys* sys, naRef ins) :
1679 SG_LOG(SG_NASAL, SG_INFO, "created Nasal delegate for " << fp);
1680 _gcSaveKey = _nasal->gcSave(ins);
1683 virtual ~NasalFPDelegate()
1685 SG_LOG(SG_NASAL, SG_INFO, "destroying Nasal delegate for " << _plan);
1686 _nasal->gcRelease(_gcSaveKey);
1689 virtual void departureChanged()
1691 callDelegateMethod("departureChanged");
1694 virtual void arrivalChanged()
1696 callDelegateMethod("arrivalChanged");
1699 virtual void waypointsChanged()
1701 callDelegateMethod("waypointsChanged");
1704 virtual void currentWaypointChanged()
1706 callDelegateMethod("currentWaypointChanged");
1710 void callDelegateMethod(const char* method)
1713 naMember_cget(_nasal->context(), _instance, method, &f);
1715 return; // no method on the delegate
1719 arg[0] = ghostForFlightPlan(_nasal->context(), _plan);
1720 _nasal->callMethod(f, _instance, 1, arg, naNil());
1729 class NasalFPDelegateFactory : public FlightPlan::DelegateFactory
1732 NasalFPDelegateFactory(naRef code)
1734 _nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1736 _gcSaveKey = _nasal->gcSave(_func);
1739 ~NasalFPDelegateFactory()
1741 _nasal->gcRelease(_gcSaveKey);
1744 virtual FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp)
1747 args[0] = ghostForFlightPlan(_nasal->context(), fp);
1748 naRef instance = _nasal->call(_func, 1, args, naNil());
1749 if (naIsNil(instance)) {
1753 return new NasalFPDelegate(fp, _nasal, instance);
1761 static naRef f_registerFPDelegate(naContext c, naRef me, int argc, naRef* args)
1763 if ((argc < 1) || !naIsFunc(args[0])) {
1764 naRuntimeError(c, "non-function argument to registerFlightPlanDelegate");
1767 NasalFPDelegateFactory* factory = new NasalFPDelegateFactory(args[0]);
1768 FlightPlan::registerDelegateFactory(factory);
1773 static WayptRef wayptFromArg(naRef arg)
1775 WayptRef r = wayptGhost(arg);
1780 FGPositioned* pos = positionedGhost(arg);
1782 // let's check if the arg is hash, coudl extra a geod and hence build
1783 // a simple waypoint
1788 // special-case for runways
1789 if (pos->type() == FGPositioned::RUNWAY) {
1790 return new RunwayWaypt((FGRunway*) pos, NULL);
1793 return new NavaidWaypoint(pos, NULL);
1796 static naRef convertWayptVecToNasal(naContext c, const WayptVec& wps)
1798 naRef result = naNewVector(c);
1799 BOOST_FOREACH(WayptRef wpt, wps) {
1800 naVec_append(result, ghostForWaypt(c, wpt.get()));
1805 static naRef f_airwaySearch(naContext c, naRef me, int argc, naRef* args)
1808 naRuntimeError(c, "airwaysSearch needs at least two arguments");
1811 WayptRef start = wayptFromArg(args[0]),
1812 end = wayptFromArg(args[1]);
1814 if (!start || !end) {
1815 SG_LOG(SG_NASAL, SG_WARN, "airwaysSearch: start or end points are invalid");
1819 bool highLevel = true;
1820 if ((argc > 2) && naIsString(args[2])) {
1821 if (!strcmp(naStr_data(args[2]), "lowlevel")) {
1828 Airway::highLevel()->route(start, end, route);
1830 Airway::lowLevel()->route(start, end, route);
1833 return convertWayptVecToNasal(c, route);
1836 static naRef f_createWP(naContext c, naRef me, int argc, naRef* args)
1839 int argOffset = geodFromArgs(args, 0, argc, pos);
1841 if (((argc - argOffset) < 1) || !naIsString(args[argOffset])) {
1842 naRuntimeError(c, "createWP: no identifier supplied");
1845 string ident = naStr_data(args[argOffset++]);
1846 WayptRef wpt = new BasicWaypt(pos, ident, NULL);
1848 // set waypt flags - approach, departure, pseudo, etc
1849 if (argc > argOffset) {
1850 WayptFlag f = wayptFlagFromString(naStr_data(args[argOffset++]));
1854 return ghostForWaypt(c, wpt);
1857 static naRef f_createWPFrom(naContext c, naRef me, int argc, naRef* args)
1860 naRuntimeError(c, "createWPFrom: need at least one argument");
1863 FGPositioned* positioned = positionedGhost(args[0]);
1865 naRuntimeError(c, "createWPFrom: couldn;t convert arg[0] to FGPositioned");
1869 if (positioned->type() == FGPositioned::RUNWAY) {
1870 wpt = new RunwayWaypt((FGRunway*) positioned, NULL);
1872 wpt = new NavaidWaypoint(positioned, NULL);
1875 // set waypt flags - approach, departure, pseudo, etc
1877 WayptFlag f = wayptFlagFromString(naStr_data(args[1]));
1881 return ghostForWaypt(c, wpt);
1884 static naRef f_flightplan_getWP(naContext c, naRef me, int argc, naRef* args)
1886 FlightPlan* fp = flightplanGhost(me);
1888 naRuntimeError(c, "flightplan.getWP called on non-flightplan object");
1893 index = fp->currentIndex();
1895 index = (int) naNumValue(args[0]).num;
1898 if ((index < 0) || (index >= fp->numLegs())) {
1902 return ghostForLeg(c, fp->legAtIndex(index));
1905 static naRef f_flightplan_currentWP(naContext c, naRef me, int argc, naRef* args)
1907 FlightPlan* fp = flightplanGhost(me);
1909 naRuntimeError(c, "flightplan.currentWP called on non-flightplan object");
1911 return ghostForLeg(c, fp->currentLeg());
1914 static naRef f_flightplan_nextWP(naContext c, naRef me, int argc, naRef* args)
1916 FlightPlan* fp = flightplanGhost(me);
1918 naRuntimeError(c, "flightplan.nextWP called on non-flightplan object");
1920 return ghostForLeg(c, fp->nextLeg());
1923 static naRef f_flightplan_numWaypoints(naContext c, naRef me, int argc, naRef* args)
1925 FlightPlan* fp = flightplanGhost(me);
1927 naRuntimeError(c, "flightplan.numWaypoints called on non-flightplan object");
1929 return naNum(fp->numLegs());
1932 static naRef f_flightplan_appendWP(naContext c, naRef me, int argc, naRef* args)
1934 FlightPlan* fp = flightplanGhost(me);
1936 naRuntimeError(c, "flightplan.appendWP called on non-flightplan object");
1939 WayptRef wp = wayptGhost(args[0]);
1940 int index = fp->numLegs();
1941 fp->insertWayptAtIndex(wp.get(), index);
1942 return naNum(index);
1945 static naRef f_flightplan_insertWP(naContext c, naRef me, int argc, naRef* args)
1947 FlightPlan* fp = flightplanGhost(me);
1949 naRuntimeError(c, "flightplan.insertWP called on non-flightplan object");
1952 WayptRef wp = wayptGhost(args[0]);
1953 int index = -1; // append
1954 if ((argc > 1) && naIsNum(args[1])) {
1955 index = (int) args[1].num;
1958 fp->insertWayptAtIndex(wp.get(), index);
1962 static naRef f_flightplan_insertWPAfter(naContext c, naRef me, int argc, naRef* args)
1964 FlightPlan* fp = flightplanGhost(me);
1966 naRuntimeError(c, "flightplan.insertWPAfter called on non-flightplan object");
1969 WayptRef wp = wayptGhost(args[0]);
1970 int index = -1; // append
1971 if ((argc > 1) && naIsNum(args[1])) {
1972 index = (int) args[1].num;
1975 fp->insertWayptAtIndex(wp.get(), index + 1);
1979 static naRef f_flightplan_insertWaypoints(naContext c, naRef me, int argc, naRef* args)
1981 FlightPlan* fp = flightplanGhost(me);
1983 naRuntimeError(c, "flightplan.insertWaypoints called on non-flightplan object");
1987 if (!naIsVector(args[0])) {
1988 naRuntimeError(c, "flightplan.insertWaypoints expects vector as first arg");
1991 int count = naVec_size(args[0]);
1992 for (int i=0; i<count; ++i) {
1993 Waypt* wp = wayptGhost(naVec_get(args[0], i));
1999 int index = -1; // append
2000 if ((argc > 1) && naIsNum(args[1])) {
2001 index = (int) args[1].num;
2004 fp->insertWayptsAtIndex(wps, index);
2008 static naRef f_flightplan_deleteWP(naContext c, naRef me, int argc, naRef* args)
2010 FlightPlan* fp = flightplanGhost(me);
2012 naRuntimeError(c, "flightplan.deleteWP called on non-flightplan object");
2015 if ((argc < 1) || !naIsNum(args[0])) {
2016 naRuntimeError(c, "bad argument to flightplan.deleteWP");
2019 int index = (int) args[0].num;
2020 fp->deleteIndex(index);
2024 static naRef f_flightplan_clearPlan(naContext c, naRef me, int argc, naRef* args)
2026 FlightPlan* fp = flightplanGhost(me);
2028 naRuntimeError(c, "flightplan.clearPlan called on non-flightplan object");
2035 static naRef f_flightplan_clearWPType(naContext c, naRef me, int argc, naRef* args)
2037 FlightPlan* fp = flightplanGhost(me);
2039 naRuntimeError(c, "flightplan.clearWPType called on non-flightplan object");
2043 naRuntimeError(c, "insufficent args to flightplan.clearWPType");
2046 WayptFlag flag = wayptFlagFromString(naStr_data(args[0]));
2047 fp->clearWayptsWithFlag(flag);
2051 static naRef f_flightplan_clone(naContext c, naRef me, int argc, naRef* args)
2053 FlightPlan* fp = flightplanGhost(me);
2055 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2058 return ghostForFlightPlan(c, fp->clone());
2061 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
2063 FlightPlan::Leg* leg = fpLegGhost(me);
2065 naRuntimeError(c, "leg.setSpeed called on non-flightplan-leg object");
2069 naRuntimeError(c, "bad arguments to leg.setSpeed");
2072 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2073 leg->setSpeed(rr, args[0].num);
2077 static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
2079 FlightPlan::Leg* leg = fpLegGhost(me);
2081 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2085 naRuntimeError(c, "bad arguments to leg.setAltitude");
2088 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2089 leg->setAltitude(rr, args[0].num);
2093 static naRef f_waypoint_navaid(naContext c, naRef me, int argc, naRef* args)
2095 flightgear::Waypt* w = wayptGhost(me);
2097 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2100 FGPositioned* pos = w->source();
2105 switch (pos->type()) {
2106 case FGPositioned::VOR:
2107 case FGPositioned::NDB:
2108 case FGPositioned::ILS:
2109 case FGPositioned::LOC:
2110 case FGPositioned::GS:
2111 case FGPositioned::DME:
2112 case FGPositioned::TACAN: {
2113 FGNavRecord* nav = (FGNavRecord*) pos;
2114 return ghostForNavaid(c, nav);
2122 static naRef f_waypoint_airport(naContext c, naRef me, int argc, naRef* args)
2124 flightgear::Waypt* w = wayptGhost(me);
2126 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2129 FGPositioned* pos = w->source();
2130 if (!pos || FGAirport::isAirportType(pos)) {
2134 return ghostForAirport(c, (FGAirport*) pos);
2137 static naRef f_waypoint_runway(naContext c, naRef me, int argc, naRef* args)
2139 flightgear::Waypt* w = wayptGhost(me);
2141 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2144 FGPositioned* pos = w->source();
2145 if (!pos || (pos->type() != FGPositioned::RUNWAY)) {
2149 return ghostForRunway(c, (FGRunway*) pos);
2152 static naRef f_procedure_transition(naContext c, naRef me, int argc, naRef* args)
2154 Procedure* proc = procedureGhost(me);
2156 naRuntimeError(c, "procedure.transition called on non-procedure object");
2159 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2160 naRuntimeError(c, "procedure.transition called on non-SID or -STAR");
2163 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2164 Transition* trans = ad->findTransitionByName(naStr_data(args[0]));
2166 return ghostForProcedure(c, trans);
2169 static naRef f_procedure_route(naContext c, naRef me, int argc, naRef* args)
2171 Procedure* proc = procedureGhost(me);
2173 naRuntimeError(c, "procedure.route called on non-procedure object");
2176 // wrapping up tow different routines here - approach routing from the IAF
2177 // to the associated runway, and SID/STAR routing via an enroute transition
2178 // and possibly a runway transition or not.
2179 if (Approach::isApproach(proc->type())) {
2182 iaf = wayptFromArg(args[0]);
2186 Approach* app = (Approach*) proc;
2187 if (!app->route(iaf, r)) {
2188 SG_LOG(SG_NASAL, SG_WARN, "procedure.route failed for Approach somehow");
2192 return convertWayptVecToNasal(c, r);
2193 } else if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2194 naRuntimeError(c, "procedure.route called on unsuitable procedure type");
2198 FGRunway* rwy = runwayGhost(args[0]);
2199 if (rwy) ++argOffset;
2201 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2202 Transition* trans = NULL;
2203 if (argOffset < argc) {
2204 trans = (Transition*) procedureGhost(args[argOffset]);
2207 // note either runway or trans may be NULL - that's ok
2209 if (!ad->route(rwy, trans, r)) {
2210 SG_LOG(SG_NASAL, SG_WARN, "prcoedure.route failed for ArrvialDeparture somehow");
2214 return convertWayptVecToNasal(c, r);
2218 // Table of extension functions. Terminate with zeros.
2219 static struct { const char* name; naCFunction func; } funcs[] = {
2220 { "carttogeod", f_carttogeod },
2221 { "geodtocart", f_geodtocart },
2222 { "geodinfo", f_geodinfo },
2223 { "airportinfo", f_airportinfo },
2224 { "findAirportsWithinRange", f_findAirportsWithinRange },
2225 { "findAirportsByICAO", f_findAirportsByICAO },
2226 { "navinfo", f_navinfo },
2227 { "findNavaidsWithinRange", f_findNavaidsWithinRange },
2228 { "findNavaidByFrequency", f_findNavaidByFrequency },
2229 { "findNavaidsByFrequency", f_findNavaidsByFrequency },
2230 { "findNavaidsByID", f_findNavaidsByIdent },
2231 { "findFixesByID", f_findFixesByIdent },
2232 { "flightplan", f_route },
2233 { "registerFlightPlanDelegate", f_registerFPDelegate },
2234 { "createWP", f_createWP },
2235 { "createWPFrom", f_createWPFrom },
2236 { "airwaysRoute", f_airwaySearch },
2237 { "magvar", f_magvar },
2238 { "courseAndDistance", f_courseAndDistance },
2239 { "greatCircleMove", f_greatCircleMove },
2240 { "tileIndex", f_tileIndex },
2241 { "tilePath", f_tilePath },
2246 naRef initNasalPositioned(naRef globals, naContext c, naRef gcSave)
2248 airportPrototype = naNewHash(c);
2249 hashset(c, gcSave, "airportProto", airportPrototype);
2251 hashset(c, airportPrototype, "runway", naNewFunc(c, naNewCCode(c, f_airport_runway)));
2252 hashset(c, airportPrototype, "tower", naNewFunc(c, naNewCCode(c, f_airport_tower)));
2253 hashset(c, airportPrototype, "comms", naNewFunc(c, naNewCCode(c, f_airport_comms)));
2254 hashset(c, airportPrototype, "sids", naNewFunc(c, naNewCCode(c, f_airport_sids)));
2255 hashset(c, airportPrototype, "stars", naNewFunc(c, naNewCCode(c, f_airport_stars)));
2256 hashset(c, airportPrototype, "getApproachList", naNewFunc(c, naNewCCode(c, f_airport_approaches)));
2257 hashset(c, airportPrototype, "parking", naNewFunc(c, naNewCCode(c, f_airport_parking)));
2258 hashset(c, airportPrototype, "getSid", naNewFunc(c, naNewCCode(c, f_airport_getSid)));
2259 hashset(c, airportPrototype, "getStar", naNewFunc(c, naNewCCode(c, f_airport_getStar)));
2260 hashset(c, airportPrototype, "getIAP", naNewFunc(c, naNewCCode(c, f_airport_getApproach)));
2261 hashset(c, airportPrototype, "tostring", naNewFunc(c, naNewCCode(c, f_airport_toString)));
2263 flightplanPrototype = naNewHash(c);
2264 hashset(c, gcSave, "flightplanProto", flightplanPrototype);
2266 hashset(c, flightplanPrototype, "getWP", naNewFunc(c, naNewCCode(c, f_flightplan_getWP)));
2267 hashset(c, flightplanPrototype, "currentWP", naNewFunc(c, naNewCCode(c, f_flightplan_currentWP)));
2268 hashset(c, flightplanPrototype, "nextWP", naNewFunc(c, naNewCCode(c, f_flightplan_nextWP)));
2269 hashset(c, flightplanPrototype, "getPlanSize", naNewFunc(c, naNewCCode(c, f_flightplan_numWaypoints)));
2270 hashset(c, flightplanPrototype, "appendWP", naNewFunc(c, naNewCCode(c, f_flightplan_appendWP)));
2271 hashset(c, flightplanPrototype, "insertWP", naNewFunc(c, naNewCCode(c, f_flightplan_insertWP)));
2272 hashset(c, flightplanPrototype, "deleteWP", naNewFunc(c, naNewCCode(c, f_flightplan_deleteWP)));
2273 hashset(c, flightplanPrototype, "insertWPAfter", naNewFunc(c, naNewCCode(c, f_flightplan_insertWPAfter)));
2274 hashset(c, flightplanPrototype, "insertWaypoints", naNewFunc(c, naNewCCode(c, f_flightplan_insertWaypoints)));
2275 hashset(c, flightplanPrototype, "cleanPlan", naNewFunc(c, naNewCCode(c, f_flightplan_clearPlan)));
2276 hashset(c, flightplanPrototype, "clearWPType", naNewFunc(c, naNewCCode(c, f_flightplan_clearWPType)));
2277 hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone)));
2279 waypointPrototype = naNewHash(c);
2280 hashset(c, gcSave, "wayptProto", waypointPrototype);
2282 hashset(c, waypointPrototype, "navaid", naNewFunc(c, naNewCCode(c, f_waypoint_navaid)));
2283 hashset(c, waypointPrototype, "runway", naNewFunc(c, naNewCCode(c, f_waypoint_runway)));
2284 hashset(c, waypointPrototype, "airport", naNewFunc(c, naNewCCode(c, f_waypoint_airport)));
2286 procedurePrototype = naNewHash(c);
2287 hashset(c, gcSave, "procedureProto", procedurePrototype);
2288 hashset(c, procedurePrototype, "transition", naNewFunc(c, naNewCCode(c, f_procedure_transition)));
2289 hashset(c, procedurePrototype, "route", naNewFunc(c, naNewCCode(c, f_procedure_route)));
2291 fpLegPrototype = naNewHash(c);
2292 hashset(c, gcSave, "fpLegProto", fpLegPrototype);
2293 hashset(c, fpLegPrototype, "setSpeed", naNewFunc(c, naNewCCode(c, f_leg_setSpeed)));
2294 hashset(c, fpLegPrototype, "setAltitude", naNewFunc(c, naNewCCode(c, f_leg_setAltitude)));
2296 for(int i=0; funcs[i].name; i++) {
2297 hashset(c, globals, funcs[i].name,
2298 naNewFunc(c, naNewCCode(c, funcs[i].func)));
2304 void postinitNasalPositioned(naRef globals, naContext c)
2306 naRef geoModule = naHash_cget(globals, (char*) "geo");
2307 if (naIsNil(geoModule)) {
2308 SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalPositioned: geo.nas not loaded");
2312 geoCoordClass = naHash_cget(geoModule, (char*) "Coord");