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/routePath.hxx>
54 #include <Navaids/procedure.hxx>
55 #include <Navaids/airways.hxx>
57 using namespace flightgear;
59 static void positionedGhostDestroy(void* g);
60 static void wayptGhostDestroy(void* g);
61 static void legGhostDestroy(void* g);
62 static void routeBaseGhostDestroy(void* g);
64 naGhostType PositionedGhostType = { positionedGhostDestroy, "positioned" };
66 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out);
67 naGhostType AirportGhostType = { positionedGhostDestroy, "airport", airportGhostGetMember, 0 };
69 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out);
70 naGhostType NavaidGhostType = { positionedGhostDestroy, "navaid", navaidGhostGetMember, 0 };
72 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out);
73 naGhostType RunwayGhostType = { positionedGhostDestroy, "runway", runwayGhostGetMember, 0 };
75 static const char* taxiwayGhostGetMember(naContext c, void* g, naRef field, naRef* out);
76 naGhostType TaxiwayGhostType = { positionedGhostDestroy, "taxiway", taxiwayGhostGetMember, 0 };
78 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out);
79 naGhostType FixGhostType = { positionedGhostDestroy, "fix", fixGhostGetMember, 0 };
81 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out);
82 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value);
84 naGhostType WayptGhostType = { wayptGhostDestroy,
87 waypointGhostSetMember};
89 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out);
90 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value);
92 naGhostType FPLegGhostType = { legGhostDestroy,
97 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out);
98 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value);
100 naGhostType FlightPlanGhostType = { routeBaseGhostDestroy,
102 flightplanGhostGetMember,
103 flightplanGhostSetMember
106 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out);
107 naGhostType ProcedureGhostType = { routeBaseGhostDestroy,
109 procedureGhostGetMember,
112 static void hashset(naContext c, naRef hash, const char* key, naRef val)
114 naRef s = naNewString(c);
115 naStr_fromdata(s, (char*)key, strlen(key));
116 naHash_set(hash, s, val);
119 static naRef stringToNasal(naContext c, const std::string& s)
121 return naStr_fromdata(naNewString(c),
122 const_cast<char *>(s.c_str()),
126 static WayptFlag wayptFlagFromString(const char* s)
128 if (!strcmp(s, "sid")) return WPT_DEPARTURE;
129 if (!strcmp(s, "star")) return WPT_ARRIVAL;
130 if (!strcmp(s, "approach")) return WPT_APPROACH;
131 if (!strcmp(s, "missed")) return WPT_MISS;
132 if (!strcmp(s, "pseudo")) return WPT_PSEUDO;
134 return (WayptFlag) 0;
137 static naRef wayptFlagToNasal(naContext c, unsigned int flags)
139 if (flags & WPT_PSEUDO) return stringToNasal(c, "pseudo");
140 if (flags & WPT_DEPARTURE) return stringToNasal(c, "sid");
141 if (flags & WPT_ARRIVAL) return stringToNasal(c, "star");
142 if (flags & WPT_MISS) return stringToNasal(c, "missed");
143 if (flags & WPT_APPROACH) return stringToNasal(c, "approach");
147 static FGPositioned* positionedGhost(naRef r)
149 if ((naGhost_type(r) == &AirportGhostType) ||
150 (naGhost_type(r) == &NavaidGhostType) ||
151 (naGhost_type(r) == &RunwayGhostType) ||
152 (naGhost_type(r) == &FixGhostType))
154 return (FGPositioned*) naGhost_ptr(r);
160 static FGAirport* airportGhost(naRef r)
162 if (naGhost_type(r) == &AirportGhostType)
163 return (FGAirport*) naGhost_ptr(r);
167 static FGNavRecord* navaidGhost(naRef r)
169 if (naGhost_type(r) == &NavaidGhostType)
170 return (FGNavRecord*) naGhost_ptr(r);
174 static FGRunway* runwayGhost(naRef r)
176 if (naGhost_type(r) == &RunwayGhostType)
177 return (FGRunway*) naGhost_ptr(r);
181 static FGTaxiway* taxiwayGhost(naRef r)
183 if (naGhost_type(r) == &TaxiwayGhostType)
184 return (FGTaxiway*) naGhost_ptr(r);
188 static FGFix* fixGhost(naRef r)
190 if (naGhost_type(r) == &FixGhostType)
191 return (FGFix*) naGhost_ptr(r);
196 static void positionedGhostDestroy(void* g)
198 FGPositioned* pos = (FGPositioned*)g;
199 if (!FGPositioned::put(pos)) // unref
203 static Waypt* wayptGhost(naRef r)
205 if (naGhost_type(r) == &WayptGhostType)
206 return (Waypt*) naGhost_ptr(r);
208 if (naGhost_type(r) == &FPLegGhostType) {
209 FlightPlan::Leg* leg = (FlightPlan::Leg*) naGhost_ptr(r);
210 return leg->waypoint();
216 static void wayptGhostDestroy(void* g)
218 Waypt* wpt = (Waypt*)g;
219 if (!Waypt::put(wpt)) // unref
223 static void legGhostDestroy(void* g)
229 static FlightPlan::Leg* fpLegGhost(naRef r)
231 if (naGhost_type(r) == &FPLegGhostType)
232 return (FlightPlan::Leg*) naGhost_ptr(r);
236 static Procedure* procedureGhost(naRef r)
238 if (naGhost_type(r) == &ProcedureGhostType)
239 return (Procedure*) naGhost_ptr(r);
243 static FlightPlan* flightplanGhost(naRef r)
245 if (naGhost_type(r) == &FlightPlanGhostType)
246 return (FlightPlan*) naGhost_ptr(r);
250 static void routeBaseGhostDestroy(void* g)
255 static naRef airportPrototype;
256 static naRef flightplanPrototype;
257 static naRef waypointPrototype;
258 static naRef geoCoordClass;
259 static naRef fpLegPrototype;
260 static naRef procedurePrototype;
262 naRef ghostForAirport(naContext c, const FGAirport* apt)
268 FGPositioned::get(apt); // take a ref
269 return naNewGhost2(c, &AirportGhostType, (void*) apt);
272 naRef ghostForNavaid(naContext c, const FGNavRecord* n)
278 FGPositioned::get(n); // take a ref
279 return naNewGhost2(c, &NavaidGhostType, (void*) n);
282 naRef ghostForRunway(naContext c, const FGRunway* r)
288 FGPositioned::get(r); // take a ref
289 return naNewGhost2(c, &RunwayGhostType, (void*) r);
292 naRef ghostForTaxiway(naContext c, const FGTaxiway* r)
298 FGPositioned::get(r); // take a ref
299 return naNewGhost2(c, &TaxiwayGhostType, (void*) r);
302 naRef ghostForFix(naContext c, const FGFix* r)
308 FGPositioned::get(r); // take a ref
309 return naNewGhost2(c, &FixGhostType, (void*) r);
313 naRef ghostForWaypt(naContext c, const Waypt* wpt)
319 Waypt::get(wpt); // take a ref
320 return naNewGhost2(c, &WayptGhostType, (void*) wpt);
323 naRef ghostForLeg(naContext c, const FlightPlan::Leg* leg)
329 return naNewGhost2(c, &FPLegGhostType, (void*) leg);
332 naRef ghostForFlightPlan(naContext c, const FlightPlan* fp)
338 return naNewGhost2(c, &FlightPlanGhostType, (void*) fp);
341 naRef ghostForProcedure(naContext c, const Procedure* proc)
347 return naNewGhost2(c, &ProcedureGhostType, (void*) proc);
350 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out)
352 const char* fieldName = naStr_data(field);
353 FGAirport* apt = (FGAirport*) g;
355 if (!strcmp(fieldName, "parents")) {
356 *out = naNewVector(c);
357 naVec_append(*out, airportPrototype);
358 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, apt->ident());
359 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, apt->name());
360 else if (!strcmp(fieldName, "lat")) *out = naNum(apt->getLatitude());
361 else if (!strcmp(fieldName, "lon")) *out = naNum(apt->getLongitude());
362 else if (!strcmp(fieldName, "elevation")) {
363 *out = naNum(apt->getElevation() * SG_FEET_TO_METER);
364 } else if (!strcmp(fieldName, "has_metar")) {
365 *out = naNum(apt->getMetar());
366 } else if (!strcmp(fieldName, "runways")) {
368 double minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft");
369 for(unsigned int r=0; r<apt->numRunways(); ++r) {
370 FGRunway* rwy(apt->getRunwayByIndex(r));
372 // ignore unusably short runways
373 if (rwy->lengthFt() < minLengthFt) {
377 naRef rwyid = stringToNasal(c, rwy->ident());
378 naRef rwydata = ghostForRunway(c, rwy);
379 naHash_set(*out, rwyid, rwydata);
382 } else if (!strcmp(fieldName, "taxiways")) {
383 *out = naNewVector(c);
384 for(unsigned int r=0; r<apt->numTaxiways(); ++r) {
385 FGTaxiway* taxi(apt->getTaxiwayByIndex(r));
386 naRef taxidata = ghostForTaxiway(c, taxi);
387 naVec_append(*out, taxidata);
397 static const char* waypointCommonGetMember(naContext c, Waypt* wpt, const char* fieldName, naRef* out)
399 if (!strcmp(fieldName, "wp_name")) *out = stringToNasal(c, wpt->ident());
400 else if (!strcmp(fieldName, "wp_type")) *out = stringToNasal(c, wpt->type());
401 else if (!strcmp(fieldName, "wp_role")) *out = wayptFlagToNasal(c, wpt->flags());
402 else if (!strcmp(fieldName, "wp_lat")) *out = naNum(wpt->position().getLatitudeDeg());
403 else if (!strcmp(fieldName, "wp_lon")) *out = naNum(wpt->position().getLongitudeDeg());
404 else if (!strcmp(fieldName, "wp_parent_name")) {
405 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
406 *out = proc ? stringToNasal(c, proc->ident()) : naNil();
407 } else if (!strcmp(fieldName, "wp_parent")) {
408 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
409 *out = ghostForProcedure(c, proc);
410 } else if (!strcmp(fieldName, "fly_type")) {
411 if (wpt->type() == "hold") {
412 *out = stringToNasal(c, "Hold");
414 *out = stringToNasal(c, wpt->flag(WPT_OVERFLIGHT) ? "flyOver" : "flyBy");
417 return NULL; // member not found
423 static void waypointCommonSetMember(naContext c, Waypt* wpt, const char* fieldName, naRef value)
425 if (!strcmp(fieldName, "wp_role")) {
426 if (!naIsString(value)) naRuntimeError(c, "wp_role must be a string");
427 if (wpt->owner() != NULL) naRuntimeError(c, "cannot override wp_role on waypoint with parent");
428 WayptFlag f = wayptFlagFromString(naStr_data(value));
430 naRuntimeError(c, "unrecognized wp_role value %s", naStr_data(value));
433 wpt->setFlag(f, true);
437 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out)
439 const char* fieldName = naStr_data(field);
440 Waypt* wpt = (flightgear::Waypt*) g;
441 return waypointCommonGetMember(c, wpt, fieldName, out);
444 static RouteRestriction routeRestrictionFromString(const char* s)
448 if (u == "computed") return RESTRICT_COMPUTED;
449 if (u == "at") return RESTRICT_AT;
450 if (u == "mach") return SPEED_RESTRICT_MACH;
451 if (u == "computed-mach") return SPEED_COMPUTED_MACH;
452 if (u == "delete") return RESTRICT_DELETE;
453 return RESTRICT_NONE;
456 naRef routeRestrictionToNasal(naContext c, RouteRestriction rr)
459 case RESTRICT_NONE: return naNil();
460 case RESTRICT_AT: return stringToNasal(c, "at");
461 case RESTRICT_ABOVE: return stringToNasal(c, "above");
462 case RESTRICT_BELOW: return stringToNasal(c, "below");
463 case SPEED_RESTRICT_MACH: return stringToNasal(c, "mach");
464 case RESTRICT_COMPUTED: return stringToNasal(c, "computed");
465 case SPEED_COMPUTED_MACH: return stringToNasal(c, "computed-mach");
466 case RESTRICT_DELETE: return stringToNasal(c, "delete");
472 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out)
474 const char* fieldName = naStr_data(field);
475 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
476 Waypt* wpt = leg->waypoint();
478 if (!strcmp(fieldName, "parents")) {
479 *out = naNewVector(c);
480 naVec_append(*out, fpLegPrototype);
481 } else if (!strcmp(fieldName, "index")) {
482 *out = naNum(leg->index());
483 } else if (!strcmp(fieldName, "alt_cstr")) {
484 *out = naNum(leg->altitudeFt());
485 } else if (!strcmp(fieldName, "alt_cstr_type")) {
486 *out = routeRestrictionToNasal(c, leg->altitudeRestriction());
487 } else if (!strcmp(fieldName, "speed_cstr")) {
488 double s = isMachRestrict(leg->speedRestriction()) ? leg->speedMach() : leg->speedKts();
490 } else if (!strcmp(fieldName, "speed_cstr_type")) {
491 *out = routeRestrictionToNasal(c, leg->speedRestriction());
492 } else if (!strcmp(fieldName, "leg_distance")) {
493 *out = naNum(leg->distanceNm());
494 } else if (!strcmp(fieldName, "leg_bearing")) {
495 *out = naNum(leg->courseDeg());
496 } else if (!strcmp(fieldName, "distance_along_route")) {
497 *out = naNum(leg->distanceAlongRoute());
498 } else { // check for fields defined on the underlying waypoint
499 return waypointCommonGetMember(c, wpt, fieldName, out);
502 return ""; // success
505 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value)
507 const char* fieldName = naStr_data(field);
508 Waypt* wpt = (Waypt*) g;
509 waypointCommonSetMember(c, wpt, fieldName, value);
512 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value)
514 const char* fieldName = naStr_data(field);
515 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
517 waypointCommonSetMember(c, leg->waypoint(), fieldName, value);
520 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out)
522 const char* fieldName = naStr_data(field);
523 FlightPlan* fp = (FlightPlan*) g;
525 if (!strcmp(fieldName, "parents")) {
526 *out = naNewVector(c);
527 naVec_append(*out, flightplanPrototype);
528 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fp->ident());
529 else if (!strcmp(fieldName, "departure")) *out = ghostForAirport(c, fp->departureAirport());
530 else if (!strcmp(fieldName, "destination")) *out = ghostForAirport(c, fp->destinationAirport());
531 else if (!strcmp(fieldName, "departure_runway")) *out = ghostForRunway(c, fp->departureRunway());
532 else if (!strcmp(fieldName, "destination_runway")) *out = ghostForRunway(c, fp->destinationRunway());
533 else if (!strcmp(fieldName, "sid")) *out = ghostForProcedure(c, fp->sid());
534 else if (!strcmp(fieldName, "sid_trans")) *out = ghostForProcedure(c, fp->sidTransition());
535 else if (!strcmp(fieldName, "star")) *out = ghostForProcedure(c, fp->star());
536 else if (!strcmp(fieldName, "star_trans")) *out = ghostForProcedure(c, fp->starTransition());
537 else if (!strcmp(fieldName, "approach")) *out = ghostForProcedure(c, fp->approach());
538 else if (!strcmp(fieldName, "current")) *out = naNum(fp->currentIndex());
546 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value)
548 const char* fieldName = naStr_data(field);
549 FlightPlan* fp = (FlightPlan*) g;
551 if (!strcmp(fieldName, "id")) {
552 if (!naIsString(value)) naRuntimeError(c, "flightplan.id must be a string");
553 fp->setIdent(naStr_data(value));
554 } else if (!strcmp(fieldName, "current")) {
555 int index = value.num;
556 if ((index < 0) || (index >= fp->numLegs())) {
559 fp->setCurrentIndex(index);
560 } else if (!strcmp(fieldName, "departure")) {
561 FGAirport* apt = airportGhost(value);
563 fp->setDeparture(apt);
567 FGRunway* rwy = runwayGhost(value);
569 fp->setDeparture(rwy);
573 naRuntimeError(c, "bad argument type setting departure");
574 } else if (!strcmp(fieldName, "destination")) {
575 FGAirport* apt = airportGhost(value);
577 fp->setDestination(apt);
581 FGRunway* rwy = runwayGhost(value);
583 fp->setDestination(rwy);
587 naRuntimeError(c, "bad argument type setting destination");
588 } else if (!strcmp(fieldName, "departure_runway")) {
589 FGRunway* rwy = runwayGhost(value);
591 fp->setDeparture(rwy);
595 naRuntimeError(c, "bad argument type setting departure");
596 } else if (!strcmp(fieldName, "destination_runway")) {
597 FGRunway* rwy = runwayGhost(value);
599 fp->setDestination(rwy);
603 naRuntimeError(c, "bad argument type setting departure");
604 } else if (!strcmp(fieldName, "sid")) {
605 Procedure* proc = procedureGhost(value);
606 if (proc && (proc->type() == PROCEDURE_SID)) {
607 fp->setSID((flightgear::SID*) proc);
610 // allow a SID transition to be set, implicitly include the SID itself
611 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
612 fp->setSID((Transition*) proc);
616 if (naIsString(value)) {
617 FGAirport* apt = fp->departureAirport();
618 fp->setSID(apt->findSIDWithIdent(naStr_data(value)));
622 naRuntimeError(c, "bad argument type setting SID");
623 } else if (!strcmp(fieldName, "star")) {
624 Procedure* proc = procedureGhost(value);
625 if (proc && (proc->type() == PROCEDURE_STAR)) {
626 fp->setSTAR((STAR*) proc);
630 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
631 fp->setSTAR((Transition*) proc);
635 if (naIsString(value)) {
636 FGAirport* apt = fp->destinationAirport();
637 fp->setSTAR(apt->findSTARWithIdent(naStr_data(value)));
641 naRuntimeError(c, "bad argument type setting STAR");
642 } else if (!strcmp(fieldName, "approach")) {
643 Procedure* proc = procedureGhost(value);
644 if (proc && Approach::isApproach(proc->type())) {
645 fp->setApproach((Approach*) proc);
649 if (naIsString(value)) {
650 FGAirport* apt = fp->destinationAirport();
651 fp->setApproach(apt->findApproachWithIdent(naStr_data(value)));
655 naRuntimeError(c, "bad argument type setting approach");
660 static naRef procedureTpType(naContext c, ProcedureType ty)
663 case PROCEDURE_SID: return stringToNasal(c, "sid");
664 case PROCEDURE_STAR: return stringToNasal(c, "star");
665 case PROCEDURE_APPROACH_VOR:
666 case PROCEDURE_APPROACH_ILS:
667 case PROCEDURE_APPROACH_RNAV:
668 case PROCEDURE_APPROACH_NDB:
669 return stringToNasal(c, "IAP");
675 static naRef procedureRadioType(naContext c, ProcedureType ty)
678 case PROCEDURE_APPROACH_VOR: return stringToNasal(c, "VOR");
679 case PROCEDURE_APPROACH_ILS: return stringToNasal(c, "ILS");
680 case PROCEDURE_APPROACH_RNAV: return stringToNasal(c, "RNAV");
681 case PROCEDURE_APPROACH_NDB: return stringToNasal(c, "NDB");
687 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out)
689 const char* fieldName = naStr_data(field);
690 Procedure* proc = (Procedure*) g;
692 if (!strcmp(fieldName, "parents")) {
693 *out = naNewVector(c);
694 naVec_append(*out, procedurePrototype);
695 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, proc->ident());
696 else if (!strcmp(fieldName, "airport")) *out = ghostForAirport(c, proc->airport());
697 else if (!strcmp(fieldName, "tp_type")) *out = procedureTpType(c, proc->type());
698 else if (!strcmp(fieldName, "radio")) *out = procedureRadioType(c, proc->type());
699 else if (!strcmp(fieldName, "runways")) {
700 *out = naNewVector(c);
701 BOOST_FOREACH(FGRunwayRef rwy, proc->runways()) {
702 naVec_append(*out, stringToNasal(c, rwy->ident()));
704 } else if (!strcmp(fieldName, "transitions")) {
705 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
710 ArrivalDeparture* ad = static_cast<ArrivalDeparture*>(proc);
711 *out = naNewVector(c);
712 BOOST_FOREACH(string id, ad->transitionIdents()) {
713 naVec_append(*out, stringToNasal(c, id));
722 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
724 const char* fieldName = naStr_data(field);
725 FGRunway* rwy = (FGRunway*) g;
727 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, rwy->ident());
728 else if (!strcmp(fieldName, "lat")) *out = naNum(rwy->latitude());
729 else if (!strcmp(fieldName, "lon")) *out = naNum(rwy->longitude());
730 else if (!strcmp(fieldName, "heading")) *out = naNum(rwy->headingDeg());
731 else if (!strcmp(fieldName, "length")) *out = naNum(rwy->lengthM());
732 else if (!strcmp(fieldName, "width")) *out = naNum(rwy->widthM());
733 else if (!strcmp(fieldName, "threshold")) *out = naNum(rwy->displacedThresholdM());
734 else if (!strcmp(fieldName, "stopway")) *out = naNum(rwy->stopwayM());
735 else if (!strcmp(fieldName, "surface")) *out = naNum(rwy->surface());
736 else if (!strcmp(fieldName, "ils_frequency_mhz")) {
737 *out = rwy->ILS() ? naNum(rwy->ILS()->get_freq() / 100.0) : naNil();
738 } else if (!strcmp(fieldName, "ils")) {
739 *out = ghostForNavaid(c, rwy->ILS());
747 static const char* taxiwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
749 const char* fieldName = naStr_data(field);
750 FGTaxiway* taxi = (FGTaxiway*) g;
752 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, taxi->ident());
753 else if (!strcmp(fieldName, "lat")) *out = naNum(taxi->latitude());
754 else if (!strcmp(fieldName, "lon")) *out = naNum(taxi->longitude());
755 else if (!strcmp(fieldName, "heading")) *out = naNum(taxi->headingDeg());
756 else if (!strcmp(fieldName, "length")) *out = naNum(taxi->lengthM());
757 else if (!strcmp(fieldName, "width")) *out = naNum(taxi->widthM());
758 else if (!strcmp(fieldName, "surface")) *out = naNum(taxi->surface());
764 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out)
766 const char* fieldName = naStr_data(field);
767 FGNavRecord* nav = (FGNavRecord*) g;
769 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, nav->ident());
770 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, nav->name());
771 else if (!strcmp(fieldName, "lat")) *out = naNum(nav->get_lat());
772 else if (!strcmp(fieldName, "lon")) *out = naNum(nav->get_lon());
773 else if (!strcmp(fieldName, "elevation")) {
774 *out = naNum(nav->get_elev_ft() * SG_FEET_TO_METER);
775 } else if (!strcmp(fieldName, "type")) {
776 *out = stringToNasal(c, nav->nameForType(nav->type()));
777 } else if (!strcmp(fieldName, "frequency")) {
778 *out = naNum(nav->get_freq());
779 } else if (!strcmp(fieldName, "range_nm")) {
780 *out = naNum(nav->get_range());
781 } else if (!strcmp(fieldName, "course")) {
782 if ((nav->type() == FGPositioned::ILS) || (nav->type() == FGPositioned::LOC)) {
783 double radial = nav->get_multiuse();
784 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
785 *out = naNum(radial);
796 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out)
798 const char* fieldName = naStr_data(field);
799 FGFix* fix = (FGFix*) g;
801 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fix->ident());
802 else if (!strcmp(fieldName, "lat")) *out = naNum(fix->get_lat());
803 else if (!strcmp(fieldName, "lon")) *out = naNum(fix->get_lon());
811 static bool hashIsCoord(naRef h)
813 naRef parents = naHash_cget(h, (char*) "parents");
814 if (!naIsVector(parents)) {
818 return naEqual(naVec_get(parents, 0), geoCoordClass);
821 bool geodFromHash(naRef ref, SGGeod& result)
823 if (!naIsHash(ref)) {
828 // check for manual latitude / longitude names
829 naRef lat = naHash_cget(ref, (char*) "lat");
830 naRef lon = naHash_cget(ref, (char*) "lon");
831 if (naIsNum(lat) && naIsNum(lon)) {
832 result = SGGeod::fromDeg(naNumValue(lon).num, naNumValue(lat).num);
836 if (hashIsCoord(ref)) {
837 naRef lat = naHash_cget(ref, (char*) "_lat");
838 naRef lon = naHash_cget(ref, (char*) "_lon");
839 if (naIsNum(lat) && naIsNum(lon)) {
840 result = SGGeod::fromRad(naNumValue(lon).num, naNumValue(lat).num);
845 // check for any synonyms?
846 // latitude + longitude?
851 static int geodFromArgs(naRef* args, int offset, int argc, SGGeod& result)
853 if (offset >= argc) {
857 if (naIsGhost(args[offset])) {
858 naGhostType* gt = naGhost_type(args[offset]);
859 if (gt == &AirportGhostType) {
860 result = airportGhost(args[offset])->geod();
864 if (gt == &NavaidGhostType) {
865 result = navaidGhost(args[offset])->geod();
869 if (gt == &RunwayGhostType) {
870 result = runwayGhost(args[offset])->geod();
874 if (gt == &TaxiwayGhostType) {
875 result = taxiwayGhost(args[offset])->geod();
879 if (gt == &FixGhostType) {
880 result = fixGhost(args[offset])->geod();
884 if (gt == &WayptGhostType) {
885 result = wayptGhost(args[offset])->position();
890 if (geodFromHash(args[offset], result)) {
894 if (((argc - offset) >= 2) && naIsNum(args[offset]) && naIsNum(args[offset + 1])) {
895 double lat = naNumValue(args[0]).num,
896 lon = naNumValue(args[1]).num;
897 result = SGGeod::fromDeg(lon, lat);
904 // Convert a cartesian point to a geodetic lat/lon/altitude.
905 static naRef f_carttogeod(naContext c, naRef me, int argc, naRef* args)
907 double lat, lon, alt, xyz[3];
908 if(argc != 3) naRuntimeError(c, "carttogeod() expects 3 arguments");
909 for(int i=0; i<3; i++)
910 xyz[i] = naNumValue(args[i]).num;
911 sgCartToGeod(xyz, &lat, &lon, &alt);
912 lat *= SG_RADIANS_TO_DEGREES;
913 lon *= SG_RADIANS_TO_DEGREES;
914 naRef vec = naNewVector(c);
915 naVec_append(vec, naNum(lat));
916 naVec_append(vec, naNum(lon));
917 naVec_append(vec, naNum(alt));
921 // Convert a geodetic lat/lon/altitude to a cartesian point.
922 static naRef f_geodtocart(naContext c, naRef me, int argc, naRef* args)
924 if(argc != 3) naRuntimeError(c, "geodtocart() expects 3 arguments");
925 double lat = naNumValue(args[0]).num * SG_DEGREES_TO_RADIANS;
926 double lon = naNumValue(args[1]).num * SG_DEGREES_TO_RADIANS;
927 double alt = naNumValue(args[2]).num;
929 sgGeodToCart(lat, lon, alt, xyz);
930 naRef vec = naNewVector(c);
931 naVec_append(vec, naNum(xyz[0]));
932 naVec_append(vec, naNum(xyz[1]));
933 naVec_append(vec, naNum(xyz[2]));
937 // For given geodetic point return array with elevation, and a material data
938 // hash, or nil if there's no information available (tile not loaded). If
939 // information about the material isn't available, then nil is returned instead
941 static naRef f_geodinfo(naContext c, naRef me, int argc, naRef* args)
943 #define HASHSET(s,l,n) naHash_set(matdata, naStr_fromdata(naNewString(c),s,l),n)
944 if(argc < 2 || argc > 3)
945 naRuntimeError(c, "geodinfo() expects 2 or 3 arguments: lat, lon [, maxalt]");
946 double lat = naNumValue(args[0]).num;
947 double lon = naNumValue(args[1]).num;
948 double elev = argc == 3 ? naNumValue(args[2]).num : 10000;
949 const simgear::BVHMaterial *material;
950 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
951 if(!globals->get_scenery()->get_elevation_m(geod, elev, &material))
953 const SGMaterial *mat = dynamic_cast<const SGMaterial *>(material);
954 naRef vec = naNewVector(c);
955 naVec_append(vec, naNum(elev));
956 naRef matdata = naNil();
958 matdata = naNewHash(c);
959 naRef names = naNewVector(c);
960 BOOST_FOREACH(const std::string& n, mat->get_names())
961 naVec_append(names, stringToNasal(c, n));
963 HASHSET("names", 5, names);
964 HASHSET("solid", 5, naNum(mat->get_solid()));
965 HASHSET("friction_factor", 15, naNum(mat->get_friction_factor()));
966 HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction()));
967 HASHSET("load_resistance", 15, naNum(mat->get_load_resistance()));
968 HASHSET("bumpiness", 9, naNum(mat->get_bumpiness()));
969 HASHSET("light_coverage", 14, naNum(mat->get_light_coverage()));
971 naVec_append(vec, matdata);
977 class AirportInfoFilter : public FGAirport::AirportFilter
980 AirportInfoFilter() : type(FGPositioned::AIRPORT) {
981 minRunwayLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
984 bool fromArg(naRef arg)
986 const char *s = naStr_data(arg);
987 if(!strcmp(s, "airport")) type = FGPositioned::AIRPORT;
988 else if(!strcmp(s, "seaport")) type = FGPositioned::SEAPORT;
989 else if(!strcmp(s, "heliport")) type = FGPositioned::HELIPORT;
996 virtual FGPositioned::Type minType() const {
1000 virtual FGPositioned::Type maxType() const {
1004 virtual bool pass(FGPositioned* aPos) const
1006 FGAirport* apt = (FGAirport*) aPos;
1007 if ((apt->type() == FGPositioned::AIRPORT) &&
1008 !apt->hasHardRunwayOfLengthFt(minRunwayLengthFt))
1016 FGPositioned::Type type;
1017 double minRunwayLengthFt;
1020 // Returns data hash for particular or nearest airport of a <type>, or nil
1023 // airportinfo(<id>); e.g. "KSFO"
1024 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
1025 // airportinfo() same as airportinfo("airport")
1026 // airportinfo(<lat>, <lon> [, <type>]);
1027 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
1029 SGGeod pos = globals->get_aircraft_position();
1030 FGAirport* apt = NULL;
1032 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1033 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1038 double maxRange = 10000.0; // expose this? or pick a smaller value?
1040 AirportInfoFilter filter; // defaults to airports only
1043 // fall through and use AIRPORT
1044 } else if(argc == 1 && naIsString(args[0])) {
1045 if (filter.fromArg(args[0])) {
1048 // user provided an <id>, hopefully
1049 apt = FGAirport::findByIdent(naStr_data(args[0]));
1051 // return nil here, but don't raise a runtime error; this is a
1052 // legitamate way to validate an ICAO code, for example in a
1053 // dialog box or similar.
1058 naRuntimeError(c, "airportinfo() with invalid function arguments");
1063 apt = FGAirport::findClosest(pos, maxRange, &filter);
1064 if(!apt) return naNil();
1067 return ghostForAirport(c, apt);
1070 static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
1073 SGGeod pos = globals->get_aircraft_position();
1074 argOffset += geodFromArgs(args, 0, argc, pos);
1076 if (!naIsNum(args[argOffset])) {
1077 naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset);
1080 AirportInfoFilter filter; // defaults to airports only
1081 double rangeNm = args[argOffset++].num;
1082 if (argOffset < argc) {
1083 filter.fromArg(args[argOffset++]);
1086 naRef r = naNewVector(c);
1088 FGPositioned::List apts = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1089 FGPositioned::sortByRange(apts, pos);
1091 BOOST_FOREACH(FGPositionedRef a, apts) {
1092 FGAirport* apt = (FGAirport*) a.get();
1093 naVec_append(r, ghostForAirport(c, apt));
1099 static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
1101 if (!naIsString(args[0])) {
1102 naRuntimeError(c, "findAirportsByICAO expects string as arg 0");
1106 string prefix(naStr_data(args[argOffset++]));
1107 AirportInfoFilter filter; // defaults to airports only
1108 if (argOffset < argc) {
1109 filter.fromArg(args[argOffset++]);
1112 naRef r = naNewVector(c);
1114 FGPositioned::List apts = FGPositioned::findAllWithIdent(prefix, &filter, false);
1116 BOOST_FOREACH(FGPositionedRef a, apts) {
1117 FGAirport* apt = (FGAirport*) a.get();
1118 naVec_append(r, ghostForAirport(c, apt));
1124 static naRef f_airport_tower(naContext c, naRef me, int argc, naRef* args)
1126 FGAirport* apt = airportGhost(me);
1128 naRuntimeError(c, "airport.tower called on non-airport object");
1131 // build a hash for the tower position
1132 SGGeod towerLoc = apt->getTowerLocation();
1133 naRef tower = naNewHash(c);
1134 hashset(c, tower, "lat", naNum(towerLoc.getLatitudeDeg()));
1135 hashset(c, tower, "lon", naNum(towerLoc.getLongitudeDeg()));
1136 hashset(c, tower, "elevation", naNum(towerLoc.getElevationM()));
1140 static naRef f_airport_comms(naContext c, naRef me, int argc, naRef* args)
1142 FGAirport* apt = airportGhost(me);
1144 naRuntimeError(c, "airport.comms called on non-airport object");
1146 naRef comms = naNewVector(c);
1148 // if we have an explicit type, return a simple vector of frequencies
1149 if (argc > 0 && naIsScalar(args[0])) {
1150 std::string commName = naStr_data(args[0]);
1151 FGPositioned::Type commType = FGPositioned::typeFromName(commName);
1153 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStationsOfType(commType)) {
1154 naVec_append(comms, naNum(comm->freqMHz()));
1157 // otherwise return a vector of hashes, one for each comm station.
1158 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStations()) {
1159 naRef commHash = naNewHash(c);
1160 hashset(c, commHash, "frequency", naNum(comm->freqMHz()));
1161 hashset(c, commHash, "ident", stringToNasal(c, comm->ident()));
1162 naVec_append(comms, commHash);
1169 static naRef f_airport_runway(naContext c, naRef me, int argc, naRef* args)
1171 FGAirport* apt = airportGhost(me);
1173 naRuntimeError(c, "airport.runway called on non-airport object");
1176 if ((argc < 1) || !naIsString(args[0])) {
1177 naRuntimeError(c, "airport.runway expects a runway ident argument");
1180 std::string ident(naStr_data(args[0]));
1181 boost::to_upper(ident);
1182 if (!apt->hasRunwayWithIdent(ident)) {
1186 return ghostForRunway(c, apt->getRunwayByIdent(ident));
1189 static naRef f_airport_taxiway(naContext c, naRef me, int argc, naRef* args)
1191 FGAirport* apt = airportGhost(me);
1193 naRuntimeError(c, "airport.taxiway called on non-airport object");
1196 if ((argc < 1) || !naIsString(args[0])) {
1197 naRuntimeError(c, "airport.taxiway expects a taxiway ident argument");
1200 std::string ident(naStr_data(args[0]));
1201 boost::to_upper(ident);
1202 if (!apt->hasTaxiwayWithIdent(ident)) {
1206 return ghostForTaxiway(c, apt->getTaxiwayByIdent(ident));
1209 static naRef f_airport_sids(naContext c, naRef me, int argc, naRef* args)
1211 FGAirport* apt = airportGhost(me);
1213 naRuntimeError(c, "airport.sids called on non-airport object");
1216 naRef sids = naNewVector(c);
1218 FGRunway* rwy = NULL;
1219 if (argc > 0 && naIsString(args[0])) {
1220 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1224 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1225 } else if (argc > 0) {
1226 rwy = runwayGhost(args[0]);
1230 BOOST_FOREACH(flightgear::SID* sid, rwy->getSIDs()) {
1231 naRef procId = stringToNasal(c, sid->ident());
1232 naVec_append(sids, procId);
1235 for (unsigned int s=0; s<apt->numSIDs(); ++s) {
1236 flightgear::SID* sid = apt->getSIDByIndex(s);
1237 naRef procId = stringToNasal(c, sid->ident());
1238 naVec_append(sids, procId);
1245 static naRef f_airport_stars(naContext c, naRef me, int argc, naRef* args)
1247 FGAirport* apt = airportGhost(me);
1249 naRuntimeError(c, "airport.stars called on non-airport object");
1252 naRef stars = naNewVector(c);
1254 FGRunway* rwy = NULL;
1255 if (argc > 0 && naIsString(args[0])) {
1256 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1260 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1261 } else if (argc > 0) {
1262 rwy = runwayGhost(args[0]);
1266 BOOST_FOREACH(flightgear::STAR* s, rwy->getSTARs()) {
1267 naRef procId = stringToNasal(c, s->ident());
1268 naVec_append(stars, procId);
1271 for (unsigned int s=0; s<apt->numSTARs(); ++s) {
1272 flightgear::STAR* star = apt->getSTARByIndex(s);
1273 naRef procId = stringToNasal(c, star->ident());
1274 naVec_append(stars, procId);
1281 static naRef f_airport_approaches(naContext c, naRef me, int argc, naRef* args)
1283 FGAirport* apt = airportGhost(me);
1285 naRuntimeError(c, "airport.getApproachList called on non-airport object");
1288 naRef approaches = naNewVector(c);
1290 ProcedureType ty = PROCEDURE_INVALID;
1291 if ((argc > 1) && naIsString(args[1])) {
1292 std::string u(naStr_data(args[1]));
1294 if (u == "NDB") ty = PROCEDURE_APPROACH_NDB;
1295 if (u == "VOR") ty = PROCEDURE_APPROACH_VOR;
1296 if (u == "ILS") ty = PROCEDURE_APPROACH_ILS;
1297 if (u == "RNAV") ty = PROCEDURE_APPROACH_RNAV;
1300 FGRunway* rwy = NULL;
1301 if (argc > 0 && (rwy = runwayGhost(args[0]))) {
1303 } else if (argc > 0 && naIsString(args[0])) {
1304 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1308 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1312 BOOST_FOREACH(Approach* s, rwy->getApproaches()) {
1313 if ((ty != PROCEDURE_INVALID) && (s->type() != ty)) {
1317 naRef procId = stringToNasal(c, s->ident());
1318 naVec_append(approaches, procId);
1321 // no runway specified, report them all
1322 for (unsigned int s=0; s<apt->numApproaches(); ++s) {
1323 Approach* app = apt->getApproachByIndex(s);
1324 if ((ty != PROCEDURE_INVALID) && (app->type() != ty)) {
1328 naRef procId = stringToNasal(c, app->ident());
1329 naVec_append(approaches, procId);
1336 static naRef f_airport_parking(naContext c, naRef me, int argc, naRef* args)
1338 FGAirport* apt = airportGhost(me);
1340 naRuntimeError(c, "airport.parking called on non-airport object");
1343 naRef r = naNewVector(c);
1345 bool onlyAvailable = false;
1347 if (argc > 0 && naIsString(args[0])) {
1348 type = naStr_data(args[0]);
1351 if ((argc > 1) && naIsNum(args[1])) {
1352 onlyAvailable = (args[1].num != 0.0);
1355 FGAirportDynamics* dynamics = apt->getDynamics();
1356 for (int i=0; i<dynamics->getNrOfParkings(); ++i) {
1357 FGParking* park = dynamics->getParking(i);
1358 // filter out based on availability and type
1359 if (onlyAvailable && !park->isAvailable()) {
1363 if (!type.empty() && (park->getType() != type)) {
1367 const SGGeod& parkLoc = park->getGeod();
1368 naRef ph = naNewHash(c);
1369 hashset(c, ph, "name", stringToNasal(c, park->getName()));
1370 hashset(c, ph, "lat", naNum(parkLoc.getLatitudeDeg()));
1371 hashset(c, ph, "lon", naNum(parkLoc.getLongitudeDeg()));
1372 hashset(c, ph, "elevation", naNum(parkLoc.getElevationM()));
1373 naVec_append(r, ph);
1379 static naRef f_airport_getSid(naContext c, naRef me, int argc, naRef* args)
1381 FGAirport* apt = airportGhost(me);
1383 naRuntimeError(c, "airport.getSid called on non-airport object");
1386 if ((argc != 1) || !naIsString(args[0])) {
1387 naRuntimeError(c, "airport.getSid passed invalid argument");
1390 string ident = naStr_data(args[0]);
1391 return ghostForProcedure(c, apt->findSIDWithIdent(ident));
1394 static naRef f_airport_getStar(naContext c, naRef me, int argc, naRef* args)
1396 FGAirport* apt = airportGhost(me);
1398 naRuntimeError(c, "airport.getStar called on non-airport object");
1401 if ((argc != 1) || !naIsString(args[0])) {
1402 naRuntimeError(c, "airport.getStar passed invalid argument");
1405 string ident = naStr_data(args[0]);
1406 return ghostForProcedure(c, apt->findSTARWithIdent(ident));
1409 static naRef f_airport_getApproach(naContext c, naRef me, int argc, naRef* args)
1411 FGAirport* apt = airportGhost(me);
1413 naRuntimeError(c, "airport.getIAP called on non-airport object");
1416 if ((argc != 1) || !naIsString(args[0])) {
1417 naRuntimeError(c, "airport.getIAP passed invalid argument");
1420 string ident = naStr_data(args[0]);
1421 return ghostForProcedure(c, apt->findApproachWithIdent(ident));
1424 static naRef f_airport_toString(naContext c, naRef me, int argc, naRef* args)
1426 FGAirport* apt = airportGhost(me);
1428 naRuntimeError(c, "airport.tostring called on non-airport object");
1431 return stringToNasal(c, "an airport " + apt->ident());
1434 // Returns vector of data hash for navaid of a <type>, nil on error
1435 // navaids sorted by ascending distance
1436 // navinfo([<lat>,<lon>],[<type>],[<id>])
1437 // lat/lon (numeric): use latitude/longitude instead of ac position
1438 // type: ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
1439 // id: (partial) id of the fix
1441 // navinfo("vor") returns all vors
1442 // navinfo("HAM") return all navaids who's name start with "HAM"
1443 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
1444 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
1445 // sorted by distance relative to lat=34, lon=48
1446 static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
1450 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1451 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1455 pos = globals->get_aircraft_position();
1458 FGPositioned::Type type = FGPositioned::INVALID;
1459 nav_list_type navlist;
1460 const char * id = "";
1462 if(argc > 0 && naIsString(args[0])) {
1463 const char *s = naStr_data(args[0]);
1464 if(!strcmp(s, "any")) type = FGPositioned::INVALID;
1465 else if(!strcmp(s, "fix")) type = FGPositioned::FIX;
1466 else if(!strcmp(s, "vor")) type = FGPositioned::VOR;
1467 else if(!strcmp(s, "ndb")) type = FGPositioned::NDB;
1468 else if(!strcmp(s, "ils")) type = FGPositioned::ILS;
1469 else if(!strcmp(s, "dme")) type = FGPositioned::DME;
1470 else if(!strcmp(s, "tacan")) type = FGPositioned::TACAN;
1471 else id = s; // this is an id
1476 if(argc > 0 && naIsString(args[0])) {
1478 naRuntimeError(c, "navinfo() called with navaid id");
1481 id = naStr_data(args[0]);
1487 naRuntimeError(c, "navinfo() called with too many arguments");
1491 FGNavList::TypeFilter filter(type);
1492 navlist = FGNavList::findByIdentAndFreq( pos, id, 0.0, &filter );
1494 naRef reply = naNewVector(c);
1495 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
1496 naVec_append( reply, ghostForNavaid(c, *it) );
1501 static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
1504 SGGeod pos = globals->get_aircraft_position();
1505 argOffset += geodFromArgs(args, 0, argc, pos);
1507 if (!naIsNum(args[argOffset])) {
1508 naRuntimeError(c, "findNavaidsWithinRange expected range (in nm) as arg %d", argOffset);
1511 FGPositioned::Type type = FGPositioned::INVALID;
1512 double rangeNm = args[argOffset++].num;
1513 if (argOffset < argc) {
1514 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1517 naRef r = naNewVector(c);
1518 FGNavList::TypeFilter filter(type);
1519 FGPositioned::List navs = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1520 FGPositioned::sortByRange(navs, pos);
1522 BOOST_FOREACH(FGPositionedRef a, navs) {
1523 FGNavRecord* nav = (FGNavRecord*) a.get();
1524 naVec_append(r, ghostForNavaid(c, nav));
1530 static naRef f_findNavaidByFrequency(naContext c, naRef me, int argc, naRef* args)
1533 SGGeod pos = globals->get_aircraft_position();
1534 argOffset += geodFromArgs(args, 0, argc, pos);
1536 if (!naIsNum(args[argOffset])) {
1537 naRuntimeError(c, "findNavaidByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1540 FGPositioned::Type type = FGPositioned::INVALID;
1541 double freqMhz = args[argOffset++].num;
1542 if (argOffset < argc) {
1543 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1546 FGNavList::TypeFilter filter(type);
1547 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1552 return ghostForNavaid(c, navs.front().ptr());
1555 static naRef f_findNavaidsByFrequency(naContext c, naRef me, int argc, naRef* args)
1558 SGGeod pos = globals->get_aircraft_position();
1559 argOffset += geodFromArgs(args, 0, argc, pos);
1561 if (!naIsNum(args[argOffset])) {
1562 naRuntimeError(c, "findNavaidsByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1565 FGPositioned::Type type = FGPositioned::INVALID;
1566 double freqMhz = args[argOffset++].num;
1567 if (argOffset < argc) {
1568 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1571 naRef r = naNewVector(c);
1573 FGNavList::TypeFilter filter(type);
1574 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1576 BOOST_FOREACH(nav_rec_ptr a, navs) {
1577 naVec_append(r, ghostForNavaid(c, a.ptr()));
1583 static naRef f_findNavaidsByIdent(naContext c, naRef me, int argc, naRef* args)
1586 SGGeod pos = globals->get_aircraft_position();
1587 argOffset += geodFromArgs(args, 0, argc, pos);
1589 if (!naIsString(args[argOffset])) {
1590 naRuntimeError(c, "findNavaidsByIdent expectes ident string as arg %d", argOffset);
1593 FGPositioned::Type type = FGPositioned::INVALID;
1594 string ident = naStr_data(args[argOffset++]);
1595 if (argOffset < argc) {
1596 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1599 FGNavList::TypeFilter filter(type);
1600 naRef r = naNewVector(c);
1601 nav_list_type navs = FGNavList::findByIdentAndFreq(pos, ident, 0.0, &filter);
1603 BOOST_FOREACH(nav_rec_ptr a, navs) {
1604 naVec_append(r, ghostForNavaid(c, a.ptr()));
1610 static naRef f_findFixesByIdent(naContext c, naRef me, int argc, naRef* args)
1613 SGGeod pos = globals->get_aircraft_position();
1614 argOffset += geodFromArgs(args, 0, argc, pos);
1616 if (!naIsString(args[argOffset])) {
1617 naRuntimeError(c, "findFixesByIdent expectes ident string as arg %d", argOffset);
1620 string ident(naStr_data(args[argOffset]));
1621 naRef r = naNewVector(c);
1623 FGPositioned::TypeFilter filter(FGPositioned::FIX);
1624 FGPositioned::List fixes = FGPositioned::findAllWithIdent(ident, &filter);
1625 FGPositioned::sortByRange(fixes, pos);
1627 BOOST_FOREACH(FGPositionedRef f, fixes) {
1628 naVec_append(r, ghostForFix(c, (FGFix*) f.ptr()));
1634 // Convert a cartesian point to a geodetic lat/lon/altitude.
1635 static naRef f_magvar(naContext c, naRef me, int argc, naRef* args)
1637 SGGeod pos = globals->get_aircraft_position();
1639 // fine, use aircraft position
1640 } else if (geodFromArgs(args, 0, argc, pos)) {
1643 naRuntimeError(c, "magvar() expects no arguments, a positioned hash or lat,lon pair");
1646 double jd = globals->get_time_params()->getJD();
1647 double magvarDeg = sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
1648 return naNum(magvarDeg);
1651 static naRef f_courseAndDistance(naContext c, naRef me, int argc, naRef* args)
1653 SGGeod from = globals->get_aircraft_position(), to, p;
1654 int argOffset = geodFromArgs(args, 0, argc, p);
1655 if (geodFromArgs(args, argOffset, argc, to)) {
1656 from = p; // we parsed both FROM and TO args, so first was from
1658 to = p; // only parsed one arg, so FROM is current
1661 if (argOffset == 0) {
1662 naRuntimeError(c, "invalid arguments to courseAndDistance");
1665 double course, course2, d;
1666 SGGeodesy::inverse(from, to, course, course2, d);
1668 naRef result = naNewVector(c);
1669 naVec_append(result, naNum(course));
1670 naVec_append(result, naNum(d * SG_METER_TO_NM));
1674 static naRef f_greatCircleMove(naContext c, naRef me, int argc, naRef* args)
1676 SGGeod from = globals->get_aircraft_position(), to;
1679 // complication - don't inerpret two doubles (as the only args)
1680 // as a lat,lon pair - only do so if we have at least three args.
1682 argOffset = geodFromArgs(args, 0, argc, from);
1685 if ((argOffset + 1) >= argc) {
1686 naRuntimeError(c, "isufficent arguments to greatCircleMove");
1689 if (!naIsNum(args[argOffset]) || !naIsNum(args[argOffset+1])) {
1690 naRuntimeError(c, "invalid arguments %d and %d to greatCircleMove",
1691 argOffset, argOffset + 1);
1694 double course = args[argOffset].num, course2;
1695 double distanceNm = args[argOffset + 1].num;
1696 SGGeodesy::direct(from, course, distanceNm * SG_NM_TO_METER, to, course2);
1699 naRef coord = naNewHash(c);
1700 hashset(c, coord, "lat", naNum(to.getLatitudeDeg()));
1701 hashset(c, coord, "lon", naNum(to.getLongitudeDeg()));
1705 static naRef f_tilePath(naContext c, naRef me, int argc, naRef* args)
1707 SGGeod pos = globals->get_aircraft_position();
1708 geodFromArgs(args, 0, argc, pos);
1710 return stringToNasal(c, b.gen_base_path());
1713 static naRef f_tileIndex(naContext c, naRef me, int argc, naRef* args)
1715 SGGeod pos = globals->get_aircraft_position();
1716 geodFromArgs(args, 0, argc, pos);
1718 return naNum(b.gen_index());
1721 static naRef f_route(naContext c, naRef me, int argc, naRef* args)
1724 FGRouteMgr* rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
1725 return ghostForFlightPlan(c, rm->flightPlan());
1728 if ((argc > 0) && naIsString(args[0])) {
1729 flightgear::FlightPlan* fp = new flightgear::FlightPlan;
1730 SGPath path(naStr_data(args[0]));
1731 if (!path.exists()) {
1732 naRuntimeError(c, "flightplan, no file at path %s", path.c_str());
1735 if (!fp->load(path)) {
1736 SG_LOG(SG_NASAL, SG_WARN, "failed to load flight-plan from " << path);
1741 return ghostForFlightPlan(c, fp);
1744 naRuntimeError(c, "bad arguments to flightplan()");
1748 class NasalFPDelegate : public FlightPlan::Delegate
1751 NasalFPDelegate(FlightPlan* fp, FGNasalSys* sys, naRef ins) :
1756 SG_LOG(SG_NASAL, SG_INFO, "created Nasal delegate for " << fp);
1757 _gcSaveKey = _nasal->gcSave(ins);
1760 virtual ~NasalFPDelegate()
1762 SG_LOG(SG_NASAL, SG_INFO, "destroying Nasal delegate for " << _plan);
1763 _nasal->gcRelease(_gcSaveKey);
1766 virtual void departureChanged()
1768 callDelegateMethod("departureChanged");
1771 virtual void arrivalChanged()
1773 callDelegateMethod("arrivalChanged");
1776 virtual void waypointsChanged()
1778 callDelegateMethod("waypointsChanged");
1781 virtual void currentWaypointChanged()
1783 callDelegateMethod("currentWaypointChanged");
1787 void callDelegateMethod(const char* method)
1790 naMember_cget(_nasal->context(), _instance, method, &f);
1792 return; // no method on the delegate
1796 arg[0] = ghostForFlightPlan(_nasal->context(), _plan);
1797 _nasal->callMethod(f, _instance, 1, arg, naNil());
1806 class NasalFPDelegateFactory : public FlightPlan::DelegateFactory
1809 NasalFPDelegateFactory(naRef code)
1811 _nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1813 _gcSaveKey = _nasal->gcSave(_func);
1816 ~NasalFPDelegateFactory()
1818 _nasal->gcRelease(_gcSaveKey);
1821 virtual FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp)
1824 args[0] = ghostForFlightPlan(_nasal->context(), fp);
1825 naRef instance = _nasal->call(_func, 1, args, naNil());
1826 if (naIsNil(instance)) {
1830 return new NasalFPDelegate(fp, _nasal, instance);
1838 static naRef f_registerFPDelegate(naContext c, naRef me, int argc, naRef* args)
1840 if ((argc < 1) || !naIsFunc(args[0])) {
1841 naRuntimeError(c, "non-function argument to registerFlightPlanDelegate");
1844 NasalFPDelegateFactory* factory = new NasalFPDelegateFactory(args[0]);
1845 FlightPlan::registerDelegateFactory(factory);
1850 static WayptRef wayptFromArg(naRef arg)
1852 WayptRef r = wayptGhost(arg);
1857 FGPositioned* pos = positionedGhost(arg);
1859 // let's check if the arg is hash, coudl extra a geod and hence build
1860 // a simple waypoint
1865 // special-case for runways
1866 if (pos->type() == FGPositioned::RUNWAY) {
1867 return new RunwayWaypt((FGRunway*) pos, NULL);
1870 return new NavaidWaypoint(pos, NULL);
1873 static naRef convertWayptVecToNasal(naContext c, const WayptVec& wps)
1875 naRef result = naNewVector(c);
1876 BOOST_FOREACH(WayptRef wpt, wps) {
1877 naVec_append(result, ghostForWaypt(c, wpt.get()));
1882 static naRef f_airwaySearch(naContext c, naRef me, int argc, naRef* args)
1885 naRuntimeError(c, "airwaysSearch needs at least two arguments");
1888 WayptRef start = wayptFromArg(args[0]),
1889 end = wayptFromArg(args[1]);
1891 if (!start || !end) {
1892 SG_LOG(SG_NASAL, SG_WARN, "airwaysSearch: start or end points are invalid");
1896 bool highLevel = true;
1897 if ((argc > 2) && naIsString(args[2])) {
1898 if (!strcmp(naStr_data(args[2]), "lowlevel")) {
1905 Airway::highLevel()->route(start, end, route);
1907 Airway::lowLevel()->route(start, end, route);
1910 return convertWayptVecToNasal(c, route);
1913 static naRef f_createWP(naContext c, naRef me, int argc, naRef* args)
1916 int argOffset = geodFromArgs(args, 0, argc, pos);
1918 if (((argc - argOffset) < 1) || !naIsString(args[argOffset])) {
1919 naRuntimeError(c, "createWP: no identifier supplied");
1922 string ident = naStr_data(args[argOffset++]);
1923 WayptRef wpt = new BasicWaypt(pos, ident, NULL);
1925 // set waypt flags - approach, departure, pseudo, etc
1926 if (argc > argOffset) {
1927 WayptFlag f = wayptFlagFromString(naStr_data(args[argOffset++]));
1931 return ghostForWaypt(c, wpt);
1934 static naRef f_createWPFrom(naContext c, naRef me, int argc, naRef* args)
1937 naRuntimeError(c, "createWPFrom: need at least one argument");
1940 FGPositioned* positioned = positionedGhost(args[0]);
1942 naRuntimeError(c, "createWPFrom: couldn;t convert arg[0] to FGPositioned");
1946 if (positioned->type() == FGPositioned::RUNWAY) {
1947 wpt = new RunwayWaypt((FGRunway*) positioned, NULL);
1949 wpt = new NavaidWaypoint(positioned, NULL);
1952 // set waypt flags - approach, departure, pseudo, etc
1954 WayptFlag f = wayptFlagFromString(naStr_data(args[1]));
1958 return ghostForWaypt(c, wpt);
1961 static naRef f_flightplan_getWP(naContext c, naRef me, int argc, naRef* args)
1963 FlightPlan* fp = flightplanGhost(me);
1965 naRuntimeError(c, "flightplan.getWP called on non-flightplan object");
1970 index = fp->currentIndex();
1972 index = (int) naNumValue(args[0]).num;
1975 if ((index < 0) || (index >= fp->numLegs())) {
1979 return ghostForLeg(c, fp->legAtIndex(index));
1982 static naRef f_flightplan_currentWP(naContext c, naRef me, int argc, naRef* args)
1984 FlightPlan* fp = flightplanGhost(me);
1986 naRuntimeError(c, "flightplan.currentWP called on non-flightplan object");
1988 return ghostForLeg(c, fp->currentLeg());
1991 static naRef f_flightplan_nextWP(naContext c, naRef me, int argc, naRef* args)
1993 FlightPlan* fp = flightplanGhost(me);
1995 naRuntimeError(c, "flightplan.nextWP called on non-flightplan object");
1997 return ghostForLeg(c, fp->nextLeg());
2000 static naRef f_flightplan_numWaypoints(naContext c, naRef me, int argc, naRef* args)
2002 FlightPlan* fp = flightplanGhost(me);
2004 naRuntimeError(c, "flightplan.numWaypoints called on non-flightplan object");
2006 return naNum(fp->numLegs());
2009 static naRef f_flightplan_appendWP(naContext c, naRef me, int argc, naRef* args)
2011 FlightPlan* fp = flightplanGhost(me);
2013 naRuntimeError(c, "flightplan.appendWP called on non-flightplan object");
2016 WayptRef wp = wayptGhost(args[0]);
2017 int index = fp->numLegs();
2018 fp->insertWayptAtIndex(wp.get(), index);
2019 return naNum(index);
2022 static naRef f_flightplan_insertWP(naContext c, naRef me, int argc, naRef* args)
2024 FlightPlan* fp = flightplanGhost(me);
2026 naRuntimeError(c, "flightplan.insertWP called on non-flightplan object");
2029 WayptRef wp = wayptGhost(args[0]);
2030 int index = -1; // append
2031 if ((argc > 1) && naIsNum(args[1])) {
2032 index = (int) args[1].num;
2035 fp->insertWayptAtIndex(wp.get(), index);
2039 static naRef f_flightplan_insertWPAfter(naContext c, naRef me, int argc, naRef* args)
2041 FlightPlan* fp = flightplanGhost(me);
2043 naRuntimeError(c, "flightplan.insertWPAfter called on non-flightplan object");
2046 WayptRef wp = wayptGhost(args[0]);
2047 int index = -1; // append
2048 if ((argc > 1) && naIsNum(args[1])) {
2049 index = (int) args[1].num;
2052 fp->insertWayptAtIndex(wp.get(), index + 1);
2056 static naRef f_flightplan_insertWaypoints(naContext c, naRef me, int argc, naRef* args)
2058 FlightPlan* fp = flightplanGhost(me);
2060 naRuntimeError(c, "flightplan.insertWaypoints called on non-flightplan object");
2064 if (!naIsVector(args[0])) {
2065 naRuntimeError(c, "flightplan.insertWaypoints expects vector as first arg");
2068 int count = naVec_size(args[0]);
2069 for (int i=0; i<count; ++i) {
2070 Waypt* wp = wayptGhost(naVec_get(args[0], i));
2076 int index = -1; // append
2077 if ((argc > 1) && naIsNum(args[1])) {
2078 index = (int) args[1].num;
2081 fp->insertWayptsAtIndex(wps, index);
2085 static naRef f_flightplan_deleteWP(naContext c, naRef me, int argc, naRef* args)
2087 FlightPlan* fp = flightplanGhost(me);
2089 naRuntimeError(c, "flightplan.deleteWP called on non-flightplan object");
2092 if ((argc < 1) || !naIsNum(args[0])) {
2093 naRuntimeError(c, "bad argument to flightplan.deleteWP");
2096 int index = (int) args[0].num;
2097 fp->deleteIndex(index);
2101 static naRef f_flightplan_clearPlan(naContext c, naRef me, int argc, naRef* args)
2103 FlightPlan* fp = flightplanGhost(me);
2105 naRuntimeError(c, "flightplan.clearPlan called on non-flightplan object");
2112 static naRef f_flightplan_clearWPType(naContext c, naRef me, int argc, naRef* args)
2114 FlightPlan* fp = flightplanGhost(me);
2116 naRuntimeError(c, "flightplan.clearWPType called on non-flightplan object");
2120 naRuntimeError(c, "insufficent args to flightplan.clearWPType");
2123 WayptFlag flag = wayptFlagFromString(naStr_data(args[0]));
2124 fp->clearWayptsWithFlag(flag);
2128 static naRef f_flightplan_clone(naContext c, naRef me, int argc, naRef* args)
2130 FlightPlan* fp = flightplanGhost(me);
2132 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2135 return ghostForFlightPlan(c, fp->clone());
2138 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
2140 FlightPlan::Leg* leg = fpLegGhost(me);
2142 naRuntimeError(c, "leg.setSpeed called on non-flightplan-leg object");
2146 naRuntimeError(c, "bad arguments to leg.setSpeed");
2149 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2150 leg->setSpeed(rr, args[0].num);
2154 static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
2156 FlightPlan::Leg* leg = fpLegGhost(me);
2158 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2162 naRuntimeError(c, "bad arguments to leg.setAltitude");
2165 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2166 leg->setAltitude(rr, args[0].num);
2170 static naRef f_leg_path(naContext c, naRef me, int argc, naRef* args)
2172 FlightPlan::Leg* leg = fpLegGhost(me);
2174 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2177 RoutePath path(leg->owner());
2178 SGGeodVec gv(path.pathForIndex(leg->index()));
2180 naRef result = naNewVector(c);
2181 BOOST_FOREACH(SGGeod p, gv) {
2182 // construct a geo.Coord!
2183 naRef coord = naNewHash(c);
2184 hashset(c, coord, "lat", naNum(p.getLatitudeDeg()));
2185 hashset(c, coord, "lon", naNum(p.getLongitudeDeg()));
2186 naVec_append(result, coord);
2192 static naRef f_waypoint_navaid(naContext c, naRef me, int argc, naRef* args)
2194 flightgear::Waypt* w = wayptGhost(me);
2196 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2199 FGPositioned* pos = w->source();
2204 switch (pos->type()) {
2205 case FGPositioned::VOR:
2206 case FGPositioned::NDB:
2207 case FGPositioned::ILS:
2208 case FGPositioned::LOC:
2209 case FGPositioned::GS:
2210 case FGPositioned::DME:
2211 case FGPositioned::TACAN: {
2212 FGNavRecord* nav = (FGNavRecord*) pos;
2213 return ghostForNavaid(c, nav);
2221 static naRef f_waypoint_airport(naContext c, naRef me, int argc, naRef* args)
2223 flightgear::Waypt* w = wayptGhost(me);
2225 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2228 FGPositioned* pos = w->source();
2229 if (!pos || FGAirport::isAirportType(pos)) {
2233 return ghostForAirport(c, (FGAirport*) pos);
2236 static naRef f_waypoint_runway(naContext c, naRef me, int argc, naRef* args)
2238 flightgear::Waypt* w = wayptGhost(me);
2240 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2243 FGPositioned* pos = w->source();
2244 if (!pos || (pos->type() != FGPositioned::RUNWAY)) {
2248 return ghostForRunway(c, (FGRunway*) pos);
2251 static naRef f_procedure_transition(naContext c, naRef me, int argc, naRef* args)
2253 Procedure* proc = procedureGhost(me);
2255 naRuntimeError(c, "procedure.transition called on non-procedure object");
2258 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2259 naRuntimeError(c, "procedure.transition called on non-SID or -STAR");
2262 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2263 Transition* trans = ad->findTransitionByName(naStr_data(args[0]));
2265 return ghostForProcedure(c, trans);
2268 static naRef f_procedure_route(naContext c, naRef me, int argc, naRef* args)
2270 Procedure* proc = procedureGhost(me);
2272 naRuntimeError(c, "procedure.route called on non-procedure object");
2275 // wrapping up tow different routines here - approach routing from the IAF
2276 // to the associated runway, and SID/STAR routing via an enroute transition
2277 // and possibly a runway transition or not.
2278 if (Approach::isApproach(proc->type())) {
2281 iaf = wayptFromArg(args[0]);
2285 Approach* app = (Approach*) proc;
2286 if (!app->route(iaf, r)) {
2287 SG_LOG(SG_NASAL, SG_WARN, "procedure.route failed for Approach somehow");
2291 return convertWayptVecToNasal(c, r);
2292 } else if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2293 naRuntimeError(c, "procedure.route called on unsuitable procedure type");
2297 FGRunway* rwy = runwayGhost(args[0]);
2298 if (rwy) ++argOffset;
2300 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2301 Transition* trans = NULL;
2302 if (argOffset < argc) {
2303 trans = (Transition*) procedureGhost(args[argOffset]);
2306 // note either runway or trans may be NULL - that's ok
2308 if (!ad->route(rwy, trans, r)) {
2309 SG_LOG(SG_NASAL, SG_WARN, "prcoedure.route failed for ArrvialDeparture somehow");
2313 return convertWayptVecToNasal(c, r);
2317 // Table of extension functions. Terminate with zeros.
2318 static struct { const char* name; naCFunction func; } funcs[] = {
2319 { "carttogeod", f_carttogeod },
2320 { "geodtocart", f_geodtocart },
2321 { "geodinfo", f_geodinfo },
2322 { "airportinfo", f_airportinfo },
2323 { "findAirportsWithinRange", f_findAirportsWithinRange },
2324 { "findAirportsByICAO", f_findAirportsByICAO },
2325 { "navinfo", f_navinfo },
2326 { "findNavaidsWithinRange", f_findNavaidsWithinRange },
2327 { "findNavaidByFrequency", f_findNavaidByFrequency },
2328 { "findNavaidsByFrequency", f_findNavaidsByFrequency },
2329 { "findNavaidsByID", f_findNavaidsByIdent },
2330 { "findFixesByID", f_findFixesByIdent },
2331 { "flightplan", f_route },
2332 { "registerFlightPlanDelegate", f_registerFPDelegate },
2333 { "createWP", f_createWP },
2334 { "createWPFrom", f_createWPFrom },
2335 { "airwaysRoute", f_airwaySearch },
2336 { "magvar", f_magvar },
2337 { "courseAndDistance", f_courseAndDistance },
2338 { "greatCircleMove", f_greatCircleMove },
2339 { "tileIndex", f_tileIndex },
2340 { "tilePath", f_tilePath },
2345 naRef initNasalPositioned(naRef globals, naContext c, naRef gcSave)
2347 airportPrototype = naNewHash(c);
2348 hashset(c, gcSave, "airportProto", airportPrototype);
2350 hashset(c, airportPrototype, "runway", naNewFunc(c, naNewCCode(c, f_airport_runway)));
2351 hashset(c, airportPrototype, "taxiway", naNewFunc(c, naNewCCode(c, f_airport_taxiway)));
2352 hashset(c, airportPrototype, "tower", naNewFunc(c, naNewCCode(c, f_airport_tower)));
2353 hashset(c, airportPrototype, "comms", naNewFunc(c, naNewCCode(c, f_airport_comms)));
2354 hashset(c, airportPrototype, "sids", naNewFunc(c, naNewCCode(c, f_airport_sids)));
2355 hashset(c, airportPrototype, "stars", naNewFunc(c, naNewCCode(c, f_airport_stars)));
2356 hashset(c, airportPrototype, "getApproachList", naNewFunc(c, naNewCCode(c, f_airport_approaches)));
2357 hashset(c, airportPrototype, "parking", naNewFunc(c, naNewCCode(c, f_airport_parking)));
2358 hashset(c, airportPrototype, "getSid", naNewFunc(c, naNewCCode(c, f_airport_getSid)));
2359 hashset(c, airportPrototype, "getStar", naNewFunc(c, naNewCCode(c, f_airport_getStar)));
2360 hashset(c, airportPrototype, "getIAP", naNewFunc(c, naNewCCode(c, f_airport_getApproach)));
2361 hashset(c, airportPrototype, "tostring", naNewFunc(c, naNewCCode(c, f_airport_toString)));
2363 flightplanPrototype = naNewHash(c);
2364 hashset(c, gcSave, "flightplanProto", flightplanPrototype);
2366 hashset(c, flightplanPrototype, "getWP", naNewFunc(c, naNewCCode(c, f_flightplan_getWP)));
2367 hashset(c, flightplanPrototype, "currentWP", naNewFunc(c, naNewCCode(c, f_flightplan_currentWP)));
2368 hashset(c, flightplanPrototype, "nextWP", naNewFunc(c, naNewCCode(c, f_flightplan_nextWP)));
2369 hashset(c, flightplanPrototype, "getPlanSize", naNewFunc(c, naNewCCode(c, f_flightplan_numWaypoints)));
2370 hashset(c, flightplanPrototype, "appendWP", naNewFunc(c, naNewCCode(c, f_flightplan_appendWP)));
2371 hashset(c, flightplanPrototype, "insertWP", naNewFunc(c, naNewCCode(c, f_flightplan_insertWP)));
2372 hashset(c, flightplanPrototype, "deleteWP", naNewFunc(c, naNewCCode(c, f_flightplan_deleteWP)));
2373 hashset(c, flightplanPrototype, "insertWPAfter", naNewFunc(c, naNewCCode(c, f_flightplan_insertWPAfter)));
2374 hashset(c, flightplanPrototype, "insertWaypoints", naNewFunc(c, naNewCCode(c, f_flightplan_insertWaypoints)));
2375 hashset(c, flightplanPrototype, "cleanPlan", naNewFunc(c, naNewCCode(c, f_flightplan_clearPlan)));
2376 hashset(c, flightplanPrototype, "clearWPType", naNewFunc(c, naNewCCode(c, f_flightplan_clearWPType)));
2377 hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone)));
2379 waypointPrototype = naNewHash(c);
2380 hashset(c, gcSave, "wayptProto", waypointPrototype);
2382 hashset(c, waypointPrototype, "navaid", naNewFunc(c, naNewCCode(c, f_waypoint_navaid)));
2383 hashset(c, waypointPrototype, "runway", naNewFunc(c, naNewCCode(c, f_waypoint_runway)));
2384 hashset(c, waypointPrototype, "airport", naNewFunc(c, naNewCCode(c, f_waypoint_airport)));
2386 procedurePrototype = naNewHash(c);
2387 hashset(c, gcSave, "procedureProto", procedurePrototype);
2388 hashset(c, procedurePrototype, "transition", naNewFunc(c, naNewCCode(c, f_procedure_transition)));
2389 hashset(c, procedurePrototype, "route", naNewFunc(c, naNewCCode(c, f_procedure_route)));
2391 fpLegPrototype = naNewHash(c);
2392 hashset(c, gcSave, "fpLegProto", fpLegPrototype);
2393 hashset(c, fpLegPrototype, "setSpeed", naNewFunc(c, naNewCCode(c, f_leg_setSpeed)));
2394 hashset(c, fpLegPrototype, "setAltitude", naNewFunc(c, naNewCCode(c, f_leg_setAltitude)));
2395 hashset(c, fpLegPrototype, "path", naNewFunc(c, naNewCCode(c, f_leg_path)));
2397 for(int i=0; funcs[i].name; i++) {
2398 hashset(c, globals, funcs[i].name,
2399 naNewFunc(c, naNewCCode(c, funcs[i].func)));
2405 void postinitNasalPositioned(naRef globals, naContext c)
2407 naRef geoModule = naHash_cget(globals, (char*) "geo");
2408 if (naIsNil(geoModule)) {
2409 SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalPositioned: geo.nas not loaded");
2413 geoCoordClass = naHash_cget(geoModule, (char*) "Coord");