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>
56 #include <Navaids/NavDataCache.hxx>
58 using namespace flightgear;
60 static void positionedGhostDestroy(void* g);
61 static void wayptGhostDestroy(void* g);
62 static void legGhostDestroy(void* g);
63 static void routeBaseGhostDestroy(void* g);
65 naGhostType PositionedGhostType = { positionedGhostDestroy, "positioned" };
67 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out);
68 naGhostType AirportGhostType = { positionedGhostDestroy, "airport", airportGhostGetMember, 0 };
70 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out);
71 naGhostType NavaidGhostType = { positionedGhostDestroy, "navaid", navaidGhostGetMember, 0 };
73 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out);
74 naGhostType RunwayGhostType = { positionedGhostDestroy, "runway", runwayGhostGetMember, 0 };
75 naGhostType TaxiwayGhostType = { positionedGhostDestroy, "taxiway", runwayGhostGetMember, 0 };
77 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out);
78 naGhostType FixGhostType = { positionedGhostDestroy, "fix", fixGhostGetMember, 0 };
80 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out);
81 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value);
83 naGhostType WayptGhostType = { wayptGhostDestroy,
86 waypointGhostSetMember};
88 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out);
89 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value);
91 naGhostType FPLegGhostType = { legGhostDestroy,
96 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out);
97 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value);
99 naGhostType FlightPlanGhostType = { routeBaseGhostDestroy,
101 flightplanGhostGetMember,
102 flightplanGhostSetMember
105 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out);
106 naGhostType ProcedureGhostType = { routeBaseGhostDestroy,
108 procedureGhostGetMember,
111 static void hashset(naContext c, naRef hash, const char* key, naRef val)
113 naRef s = naNewString(c);
114 naStr_fromdata(s, (char*)key, strlen(key));
115 naHash_set(hash, s, val);
118 static naRef stringToNasal(naContext c, const std::string& s)
120 return naStr_fromdata(naNewString(c),
121 const_cast<char *>(s.c_str()),
125 static WayptFlag wayptFlagFromString(const char* s)
127 if (!strcmp(s, "sid")) return WPT_DEPARTURE;
128 if (!strcmp(s, "star")) return WPT_ARRIVAL;
129 if (!strcmp(s, "approach")) return WPT_APPROACH;
130 if (!strcmp(s, "missed")) return WPT_MISS;
131 if (!strcmp(s, "pseudo")) return WPT_PSEUDO;
133 return (WayptFlag) 0;
136 static naRef wayptFlagToNasal(naContext c, unsigned int flags)
138 if (flags & WPT_PSEUDO) return stringToNasal(c, "pseudo");
139 if (flags & WPT_DEPARTURE) return stringToNasal(c, "sid");
140 if (flags & WPT_ARRIVAL) return stringToNasal(c, "star");
141 if (flags & WPT_MISS) return stringToNasal(c, "missed");
142 if (flags & WPT_APPROACH) return stringToNasal(c, "approach");
146 static FGPositioned* positionedGhost(naRef r)
148 if ((naGhost_type(r) == &AirportGhostType) ||
149 (naGhost_type(r) == &NavaidGhostType) ||
150 (naGhost_type(r) == &RunwayGhostType) ||
151 (naGhost_type(r) == &FixGhostType))
153 return (FGPositioned*) naGhost_ptr(r);
159 static FGAirport* airportGhost(naRef r)
161 if (naGhost_type(r) == &AirportGhostType)
162 return (FGAirport*) naGhost_ptr(r);
166 static FGNavRecord* navaidGhost(naRef r)
168 if (naGhost_type(r) == &NavaidGhostType)
169 return (FGNavRecord*) naGhost_ptr(r);
173 static FGRunway* runwayGhost(naRef r)
175 if (naGhost_type(r) == &RunwayGhostType)
176 return (FGRunway*) naGhost_ptr(r);
180 static FGTaxiway* taxiwayGhost(naRef r)
182 if (naGhost_type(r) == &TaxiwayGhostType)
183 return (FGTaxiway*) naGhost_ptr(r);
187 static FGFix* fixGhost(naRef r)
189 if (naGhost_type(r) == &FixGhostType)
190 return (FGFix*) naGhost_ptr(r);
195 static void positionedGhostDestroy(void* g)
197 FGPositioned* pos = (FGPositioned*)g;
198 if (!FGPositioned::put(pos)) // unref
202 static Waypt* wayptGhost(naRef r)
204 if (naGhost_type(r) == &WayptGhostType)
205 return (Waypt*) naGhost_ptr(r);
207 if (naGhost_type(r) == &FPLegGhostType) {
208 FlightPlan::Leg* leg = (FlightPlan::Leg*) naGhost_ptr(r);
209 return leg->waypoint();
215 static void wayptGhostDestroy(void* g)
217 Waypt* wpt = (Waypt*)g;
218 if (!Waypt::put(wpt)) // unref
222 static void legGhostDestroy(void* g)
228 static FlightPlan::Leg* fpLegGhost(naRef r)
230 if (naGhost_type(r) == &FPLegGhostType)
231 return (FlightPlan::Leg*) naGhost_ptr(r);
235 static Procedure* procedureGhost(naRef r)
237 if (naGhost_type(r) == &ProcedureGhostType)
238 return (Procedure*) naGhost_ptr(r);
242 static FlightPlan* flightplanGhost(naRef r)
244 if (naGhost_type(r) == &FlightPlanGhostType)
245 return (FlightPlan*) naGhost_ptr(r);
249 static void routeBaseGhostDestroy(void* g)
254 static naRef airportPrototype;
255 static naRef flightplanPrototype;
256 static naRef waypointPrototype;
257 static naRef geoCoordClass;
258 static naRef fpLegPrototype;
259 static naRef procedurePrototype;
261 naRef ghostForAirport(naContext c, const FGAirport* apt)
267 FGPositioned::get(apt); // take a ref
268 return naNewGhost2(c, &AirportGhostType, (void*) apt);
271 naRef ghostForNavaid(naContext c, const FGNavRecord* n)
277 FGPositioned::get(n); // take a ref
278 return naNewGhost2(c, &NavaidGhostType, (void*) n);
281 naRef ghostForRunway(naContext c, const FGRunway* r)
287 FGPositioned::get(r); // take a ref
288 return naNewGhost2(c, &RunwayGhostType, (void*) r);
291 naRef ghostForTaxiway(naContext c, const FGTaxiway* r)
297 FGPositioned::get(r); // take a ref
298 return naNewGhost2(c, &TaxiwayGhostType, (void*) r);
301 naRef ghostForFix(naContext c, const FGFix* r)
307 FGPositioned::get(r); // take a ref
308 return naNewGhost2(c, &FixGhostType, (void*) r);
312 naRef ghostForWaypt(naContext c, const Waypt* wpt)
318 Waypt::get(wpt); // take a ref
319 return naNewGhost2(c, &WayptGhostType, (void*) wpt);
322 naRef ghostForLeg(naContext c, const FlightPlan::Leg* leg)
328 return naNewGhost2(c, &FPLegGhostType, (void*) leg);
331 naRef ghostForFlightPlan(naContext c, const FlightPlan* fp)
337 return naNewGhost2(c, &FlightPlanGhostType, (void*) fp);
340 naRef ghostForProcedure(naContext c, const Procedure* proc)
346 return naNewGhost2(c, &ProcedureGhostType, (void*) proc);
349 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out)
351 const char* fieldName = naStr_data(field);
352 FGAirport* apt = (FGAirport*) g;
354 if (!strcmp(fieldName, "parents")) {
355 *out = naNewVector(c);
356 naVec_append(*out, airportPrototype);
357 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, apt->ident());
358 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, apt->name());
359 else if (!strcmp(fieldName, "lat")) *out = naNum(apt->getLatitude());
360 else if (!strcmp(fieldName, "lon")) *out = naNum(apt->getLongitude());
361 else if (!strcmp(fieldName, "elevation")) {
362 *out = naNum(apt->getElevation() * SG_FEET_TO_METER);
363 } else if (!strcmp(fieldName, "has_metar")) {
364 *out = naNum(apt->getMetar());
365 } else if (!strcmp(fieldName, "runways")) {
367 double minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft");
368 for(unsigned int r=0; r<apt->numRunways(); ++r) {
369 FGRunway* rwy(apt->getRunwayByIndex(r));
371 // ignore unusably short runways
372 if (rwy->lengthFt() < minLengthFt) {
376 naRef rwyid = stringToNasal(c, rwy->ident());
377 naRef rwydata = ghostForRunway(c, rwy);
378 naHash_set(*out, rwyid, rwydata);
381 } else if (!strcmp(fieldName, "taxiways")) {
382 *out = naNewVector(c);
383 for(unsigned int r=0; r<apt->numTaxiways(); ++r) {
384 FGTaxiway* taxi(apt->getTaxiwayByIndex(r));
385 naRef taxidata = ghostForTaxiway(c, taxi);
386 naVec_append(*out, taxidata);
396 static const char* waypointCommonGetMember(naContext c, Waypt* wpt, const char* fieldName, naRef* out)
398 if (!strcmp(fieldName, "wp_name")) *out = stringToNasal(c, wpt->ident());
399 else if (!strcmp(fieldName, "wp_type")) *out = stringToNasal(c, wpt->type());
400 else if (!strcmp(fieldName, "wp_role")) *out = wayptFlagToNasal(c, wpt->flags());
401 else if (!strcmp(fieldName, "wp_lat")) *out = naNum(wpt->position().getLatitudeDeg());
402 else if (!strcmp(fieldName, "wp_lon")) *out = naNum(wpt->position().getLongitudeDeg());
403 else if (!strcmp(fieldName, "wp_parent_name")) {
404 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
405 *out = proc ? stringToNasal(c, proc->ident()) : naNil();
406 } else if (!strcmp(fieldName, "wp_parent")) {
407 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
408 *out = ghostForProcedure(c, proc);
409 } else if (!strcmp(fieldName, "fly_type")) {
410 if (wpt->type() == "hold") {
411 *out = stringToNasal(c, "Hold");
413 *out = stringToNasal(c, wpt->flag(WPT_OVERFLIGHT) ? "flyOver" : "flyBy");
416 return NULL; // member not found
422 static void waypointCommonSetMember(naContext c, Waypt* wpt, const char* fieldName, naRef value)
424 if (!strcmp(fieldName, "wp_role")) {
425 if (!naIsString(value)) naRuntimeError(c, "wp_role must be a string");
426 if (wpt->owner() != NULL) naRuntimeError(c, "cannot override wp_role on waypoint with parent");
427 WayptFlag f = wayptFlagFromString(naStr_data(value));
429 naRuntimeError(c, "unrecognized wp_role value %s", naStr_data(value));
432 wpt->setFlag(f, true);
436 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out)
438 const char* fieldName = naStr_data(field);
439 Waypt* wpt = (flightgear::Waypt*) g;
440 return waypointCommonGetMember(c, wpt, fieldName, out);
443 static RouteRestriction routeRestrictionFromString(const char* s)
447 if (u == "computed") return RESTRICT_COMPUTED;
448 if (u == "at") return RESTRICT_AT;
449 if (u == "mach") return SPEED_RESTRICT_MACH;
450 if (u == "computed-mach") return SPEED_COMPUTED_MACH;
451 if (u == "delete") return RESTRICT_DELETE;
452 return RESTRICT_NONE;
455 naRef routeRestrictionToNasal(naContext c, RouteRestriction rr)
458 case RESTRICT_NONE: return naNil();
459 case RESTRICT_AT: return stringToNasal(c, "at");
460 case RESTRICT_ABOVE: return stringToNasal(c, "above");
461 case RESTRICT_BELOW: return stringToNasal(c, "below");
462 case SPEED_RESTRICT_MACH: return stringToNasal(c, "mach");
463 case RESTRICT_COMPUTED: return stringToNasal(c, "computed");
464 case SPEED_COMPUTED_MACH: return stringToNasal(c, "computed-mach");
465 case RESTRICT_DELETE: return stringToNasal(c, "delete");
471 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out)
473 const char* fieldName = naStr_data(field);
474 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
475 Waypt* wpt = leg->waypoint();
477 if (!strcmp(fieldName, "parents")) {
478 *out = naNewVector(c);
479 naVec_append(*out, fpLegPrototype);
480 } else if (!strcmp(fieldName, "index")) {
481 *out = naNum(leg->index());
482 } else if (!strcmp(fieldName, "alt_cstr")) {
483 *out = naNum(leg->altitudeFt());
484 } else if (!strcmp(fieldName, "alt_cstr_type")) {
485 *out = routeRestrictionToNasal(c, leg->altitudeRestriction());
486 } else if (!strcmp(fieldName, "speed_cstr")) {
487 double s = isMachRestrict(leg->speedRestriction()) ? leg->speedMach() : leg->speedKts();
489 } else if (!strcmp(fieldName, "speed_cstr_type")) {
490 *out = routeRestrictionToNasal(c, leg->speedRestriction());
491 } else if (!strcmp(fieldName, "leg_distance")) {
492 *out = naNum(leg->distanceNm());
493 } else if (!strcmp(fieldName, "leg_bearing")) {
494 *out = naNum(leg->courseDeg());
495 } else if (!strcmp(fieldName, "distance_along_route")) {
496 *out = naNum(leg->distanceAlongRoute());
497 } else { // check for fields defined on the underlying waypoint
498 return waypointCommonGetMember(c, wpt, fieldName, out);
501 return ""; // success
504 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value)
506 const char* fieldName = naStr_data(field);
507 Waypt* wpt = (Waypt*) g;
508 waypointCommonSetMember(c, wpt, fieldName, value);
511 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value)
513 const char* fieldName = naStr_data(field);
514 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
516 waypointCommonSetMember(c, leg->waypoint(), fieldName, value);
519 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out)
521 const char* fieldName = naStr_data(field);
522 FlightPlan* fp = (FlightPlan*) g;
524 if (!strcmp(fieldName, "parents")) {
525 *out = naNewVector(c);
526 naVec_append(*out, flightplanPrototype);
527 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fp->ident());
528 else if (!strcmp(fieldName, "departure")) *out = ghostForAirport(c, fp->departureAirport());
529 else if (!strcmp(fieldName, "destination")) *out = ghostForAirport(c, fp->destinationAirport());
530 else if (!strcmp(fieldName, "departure_runway")) *out = ghostForRunway(c, fp->departureRunway());
531 else if (!strcmp(fieldName, "destination_runway")) *out = ghostForRunway(c, fp->destinationRunway());
532 else if (!strcmp(fieldName, "sid")) *out = ghostForProcedure(c, fp->sid());
533 else if (!strcmp(fieldName, "sid_trans")) *out = ghostForProcedure(c, fp->sidTransition());
534 else if (!strcmp(fieldName, "star")) *out = ghostForProcedure(c, fp->star());
535 else if (!strcmp(fieldName, "star_trans")) *out = ghostForProcedure(c, fp->starTransition());
536 else if (!strcmp(fieldName, "approach")) *out = ghostForProcedure(c, fp->approach());
537 else if (!strcmp(fieldName, "current")) *out = naNum(fp->currentIndex());
545 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value)
547 const char* fieldName = naStr_data(field);
548 FlightPlan* fp = (FlightPlan*) g;
550 if (!strcmp(fieldName, "id")) {
551 if (!naIsString(value)) naRuntimeError(c, "flightplan.id must be a string");
552 fp->setIdent(naStr_data(value));
553 } else if (!strcmp(fieldName, "current")) {
554 int index = value.num;
555 if ((index < 0) || (index >= fp->numLegs())) {
558 fp->setCurrentIndex(index);
559 } else if (!strcmp(fieldName, "departure")) {
560 FGAirport* apt = airportGhost(value);
562 fp->setDeparture(apt);
566 FGRunway* rwy = runwayGhost(value);
568 fp->setDeparture(rwy);
572 naRuntimeError(c, "bad argument type setting departure");
573 } else if (!strcmp(fieldName, "destination")) {
574 FGAirport* apt = airportGhost(value);
576 fp->setDestination(apt);
580 FGRunway* rwy = runwayGhost(value);
582 fp->setDestination(rwy);
586 naRuntimeError(c, "bad argument type setting destination");
587 } else if (!strcmp(fieldName, "departure_runway")) {
588 FGRunway* rwy = runwayGhost(value);
590 fp->setDeparture(rwy);
594 naRuntimeError(c, "bad argument type setting departure");
595 } else if (!strcmp(fieldName, "destination_runway")) {
596 FGRunway* rwy = runwayGhost(value);
598 fp->setDestination(rwy);
602 naRuntimeError(c, "bad argument type setting departure");
603 } else if (!strcmp(fieldName, "sid")) {
604 Procedure* proc = procedureGhost(value);
605 if (proc && (proc->type() == PROCEDURE_SID)) {
606 fp->setSID((flightgear::SID*) proc);
609 // allow a SID transition to be set, implicitly include the SID itself
610 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
611 fp->setSID((Transition*) proc);
615 if (naIsString(value)) {
616 FGAirport* apt = fp->departureAirport();
617 fp->setSID(apt->findSIDWithIdent(naStr_data(value)));
621 naRuntimeError(c, "bad argument type setting SID");
622 } else if (!strcmp(fieldName, "star")) {
623 Procedure* proc = procedureGhost(value);
624 if (proc && (proc->type() == PROCEDURE_STAR)) {
625 fp->setSTAR((STAR*) proc);
629 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
630 fp->setSTAR((Transition*) proc);
634 if (naIsString(value)) {
635 FGAirport* apt = fp->destinationAirport();
636 fp->setSTAR(apt->findSTARWithIdent(naStr_data(value)));
640 naRuntimeError(c, "bad argument type setting STAR");
641 } else if (!strcmp(fieldName, "approach")) {
642 Procedure* proc = procedureGhost(value);
643 if (proc && Approach::isApproach(proc->type())) {
644 fp->setApproach((Approach*) proc);
648 if (naIsString(value)) {
649 FGAirport* apt = fp->destinationAirport();
650 fp->setApproach(apt->findApproachWithIdent(naStr_data(value)));
654 naRuntimeError(c, "bad argument type setting approach");
659 static naRef procedureTpType(naContext c, ProcedureType ty)
662 case PROCEDURE_SID: return stringToNasal(c, "sid");
663 case PROCEDURE_STAR: return stringToNasal(c, "star");
664 case PROCEDURE_APPROACH_VOR:
665 case PROCEDURE_APPROACH_ILS:
666 case PROCEDURE_APPROACH_RNAV:
667 case PROCEDURE_APPROACH_NDB:
668 return stringToNasal(c, "IAP");
674 static naRef procedureRadioType(naContext c, ProcedureType ty)
677 case PROCEDURE_APPROACH_VOR: return stringToNasal(c, "VOR");
678 case PROCEDURE_APPROACH_ILS: return stringToNasal(c, "ILS");
679 case PROCEDURE_APPROACH_RNAV: return stringToNasal(c, "RNAV");
680 case PROCEDURE_APPROACH_NDB: return stringToNasal(c, "NDB");
686 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out)
688 const char* fieldName = naStr_data(field);
689 Procedure* proc = (Procedure*) g;
691 if (!strcmp(fieldName, "parents")) {
692 *out = naNewVector(c);
693 naVec_append(*out, procedurePrototype);
694 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, proc->ident());
695 else if (!strcmp(fieldName, "airport")) *out = ghostForAirport(c, proc->airport());
696 else if (!strcmp(fieldName, "tp_type")) *out = procedureTpType(c, proc->type());
697 else if (!strcmp(fieldName, "radio")) *out = procedureRadioType(c, proc->type());
698 else if (!strcmp(fieldName, "runways")) {
699 *out = naNewVector(c);
700 BOOST_FOREACH(FGRunwayRef rwy, proc->runways()) {
701 naVec_append(*out, stringToNasal(c, rwy->ident()));
703 } else if (!strcmp(fieldName, "transitions")) {
704 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
709 ArrivalDeparture* ad = static_cast<ArrivalDeparture*>(proc);
710 *out = naNewVector(c);
711 BOOST_FOREACH(string id, ad->transitionIdents()) {
712 naVec_append(*out, stringToNasal(c, id));
721 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
723 const char* fieldName = naStr_data(field);
724 FGRunwayBase* base = (FGRunwayBase*) g;
726 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, base->ident());
727 else if (!strcmp(fieldName, "lat")) *out = naNum(base->latitude());
728 else if (!strcmp(fieldName, "lon")) *out = naNum(base->longitude());
729 else if (!strcmp(fieldName, "heading")) *out = naNum(base->headingDeg());
730 else if (!strcmp(fieldName, "length")) *out = naNum(base->lengthM());
731 else if (!strcmp(fieldName, "width")) *out = naNum(base->widthM());
732 else if (!strcmp(fieldName, "surface")) *out = naNum(base->surface());
733 else if (base->type() == FGRunwayBase::RUNWAY) {
734 FGRunway* rwy = (FGRunway*) g;
735 if (!strcmp(fieldName, "threshold")) *out = naNum(rwy->displacedThresholdM());
736 else if (!strcmp(fieldName, "stopway")) *out = naNum(rwy->stopwayM());
737 else if (!strcmp(fieldName, "ils_frequency_mhz")) {
738 *out = rwy->ILS() ? naNum(rwy->ILS()->get_freq() / 100.0) : naNil();
739 } else if (!strcmp(fieldName, "ils")) {
740 *out = ghostForNavaid(c, rwy->ILS());
751 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out)
753 const char* fieldName = naStr_data(field);
754 FGNavRecord* nav = (FGNavRecord*) g;
756 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, nav->ident());
757 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, nav->name());
758 else if (!strcmp(fieldName, "lat")) *out = naNum(nav->get_lat());
759 else if (!strcmp(fieldName, "lon")) *out = naNum(nav->get_lon());
760 else if (!strcmp(fieldName, "elevation")) {
761 *out = naNum(nav->get_elev_ft() * SG_FEET_TO_METER);
762 } else if (!strcmp(fieldName, "type")) {
763 *out = stringToNasal(c, nav->nameForType(nav->type()));
764 } else if (!strcmp(fieldName, "frequency")) {
765 *out = naNum(nav->get_freq());
766 } else if (!strcmp(fieldName, "range_nm")) {
767 *out = naNum(nav->get_range());
768 } else if (!strcmp(fieldName, "course")) {
769 if ((nav->type() == FGPositioned::ILS) || (nav->type() == FGPositioned::LOC)) {
770 double radial = nav->get_multiuse();
771 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
772 *out = naNum(radial);
783 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out)
785 const char* fieldName = naStr_data(field);
786 FGFix* fix = (FGFix*) g;
788 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fix->ident());
789 else if (!strcmp(fieldName, "lat")) *out = naNum(fix->get_lat());
790 else if (!strcmp(fieldName, "lon")) *out = naNum(fix->get_lon());
798 static bool hashIsCoord(naRef h)
800 naRef parents = naHash_cget(h, (char*) "parents");
801 if (!naIsVector(parents)) {
805 return naEqual(naVec_get(parents, 0), geoCoordClass) != 0;
808 bool geodFromHash(naRef ref, SGGeod& result)
810 if (!naIsHash(ref)) {
815 // check for manual latitude / longitude names
816 naRef lat = naHash_cget(ref, (char*) "lat");
817 naRef lon = naHash_cget(ref, (char*) "lon");
818 if (naIsNum(lat) && naIsNum(lon)) {
819 result = SGGeod::fromDeg(naNumValue(lon).num, naNumValue(lat).num);
823 if (hashIsCoord(ref)) {
824 naRef lat = naHash_cget(ref, (char*) "_lat");
825 naRef lon = naHash_cget(ref, (char*) "_lon");
826 if (naIsNum(lat) && naIsNum(lon)) {
827 result = SGGeod::fromRad(naNumValue(lon).num, naNumValue(lat).num);
832 // check for any synonyms?
833 // latitude + longitude?
838 static int geodFromArgs(naRef* args, int offset, int argc, SGGeod& result)
840 if (offset >= argc) {
844 if (naIsGhost(args[offset])) {
845 naGhostType* gt = naGhost_type(args[offset]);
846 if (gt == &AirportGhostType) {
847 result = airportGhost(args[offset])->geod();
851 if (gt == &NavaidGhostType) {
852 result = navaidGhost(args[offset])->geod();
856 if (gt == &RunwayGhostType) {
857 result = runwayGhost(args[offset])->geod();
861 if (gt == &TaxiwayGhostType) {
862 result = taxiwayGhost(args[offset])->geod();
866 if (gt == &FixGhostType) {
867 result = fixGhost(args[offset])->geod();
871 if (gt == &WayptGhostType) {
872 result = wayptGhost(args[offset])->position();
877 if (geodFromHash(args[offset], result)) {
881 if (((argc - offset) >= 2) && naIsNum(args[offset]) && naIsNum(args[offset + 1])) {
882 double lat = naNumValue(args[0]).num,
883 lon = naNumValue(args[1]).num;
884 result = SGGeod::fromDeg(lon, lat);
891 // Convert a cartesian point to a geodetic lat/lon/altitude.
892 static naRef f_carttogeod(naContext c, naRef me, int argc, naRef* args)
894 double lat, lon, alt, xyz[3];
895 if(argc != 3) naRuntimeError(c, "carttogeod() expects 3 arguments");
896 for(int i=0; i<3; i++)
897 xyz[i] = naNumValue(args[i]).num;
898 sgCartToGeod(xyz, &lat, &lon, &alt);
899 lat *= SG_RADIANS_TO_DEGREES;
900 lon *= SG_RADIANS_TO_DEGREES;
901 naRef vec = naNewVector(c);
902 naVec_append(vec, naNum(lat));
903 naVec_append(vec, naNum(lon));
904 naVec_append(vec, naNum(alt));
908 // Convert a geodetic lat/lon/altitude to a cartesian point.
909 static naRef f_geodtocart(naContext c, naRef me, int argc, naRef* args)
911 if(argc != 3) naRuntimeError(c, "geodtocart() expects 3 arguments");
912 double lat = naNumValue(args[0]).num * SG_DEGREES_TO_RADIANS;
913 double lon = naNumValue(args[1]).num * SG_DEGREES_TO_RADIANS;
914 double alt = naNumValue(args[2]).num;
916 sgGeodToCart(lat, lon, alt, xyz);
917 naRef vec = naNewVector(c);
918 naVec_append(vec, naNum(xyz[0]));
919 naVec_append(vec, naNum(xyz[1]));
920 naVec_append(vec, naNum(xyz[2]));
924 // For given geodetic point return array with elevation, and a material data
925 // hash, or nil if there's no information available (tile not loaded). If
926 // information about the material isn't available, then nil is returned instead
928 static naRef f_geodinfo(naContext c, naRef me, int argc, naRef* args)
930 #define HASHSET(s,l,n) naHash_set(matdata, naStr_fromdata(naNewString(c),s,l),n)
931 if(argc < 2 || argc > 3)
932 naRuntimeError(c, "geodinfo() expects 2 or 3 arguments: lat, lon [, maxalt]");
933 double lat = naNumValue(args[0]).num;
934 double lon = naNumValue(args[1]).num;
935 double elev = argc == 3 ? naNumValue(args[2]).num : 10000;
936 const simgear::BVHMaterial *material;
937 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
938 if(!globals->get_scenery()->get_elevation_m(geod, elev, &material))
940 const SGMaterial *mat = dynamic_cast<const SGMaterial *>(material);
941 naRef vec = naNewVector(c);
942 naVec_append(vec, naNum(elev));
943 naRef matdata = naNil();
945 matdata = naNewHash(c);
946 naRef names = naNewVector(c);
947 BOOST_FOREACH(const std::string& n, mat->get_names())
948 naVec_append(names, stringToNasal(c, n));
950 HASHSET("names", 5, names);
951 HASHSET("solid", 5, naNum(mat->get_solid()));
952 HASHSET("friction_factor", 15, naNum(mat->get_friction_factor()));
953 HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction()));
954 HASHSET("load_resistance", 15, naNum(mat->get_load_resistance()));
955 HASHSET("bumpiness", 9, naNum(mat->get_bumpiness()));
956 HASHSET("light_coverage", 14, naNum(mat->get_light_coverage()));
958 naVec_append(vec, matdata);
964 class AirportInfoFilter : public FGAirport::AirportFilter
967 AirportInfoFilter() : type(FGPositioned::AIRPORT) {
968 minRunwayLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
971 bool fromArg(naRef arg)
973 const char *s = naStr_data(arg);
974 if(!strcmp(s, "airport")) type = FGPositioned::AIRPORT;
975 else if(!strcmp(s, "seaport")) type = FGPositioned::SEAPORT;
976 else if(!strcmp(s, "heliport")) type = FGPositioned::HELIPORT;
983 virtual FGPositioned::Type minType() const {
987 virtual FGPositioned::Type maxType() const {
991 virtual bool pass(FGPositioned* aPos) const
993 FGAirport* apt = (FGAirport*) aPos;
994 if ((apt->type() == FGPositioned::AIRPORT) &&
995 !apt->hasHardRunwayOfLengthFt(minRunwayLengthFt))
1003 FGPositioned::Type type;
1004 double minRunwayLengthFt;
1007 // Returns data hash for particular or nearest airport of a <type>, or nil
1010 // airportinfo(<id>); e.g. "KSFO"
1011 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
1012 // airportinfo() same as airportinfo("airport")
1013 // airportinfo(<lat>, <lon> [, <type>]);
1014 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
1016 SGGeod pos = globals->get_aircraft_position();
1017 FGAirport* apt = NULL;
1019 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1020 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1025 double maxRange = 10000.0; // expose this? or pick a smaller value?
1027 AirportInfoFilter filter; // defaults to airports only
1030 // fall through and use AIRPORT
1031 } else if(argc == 1 && naIsString(args[0])) {
1032 if (filter.fromArg(args[0])) {
1035 // user provided an <id>, hopefully
1036 apt = FGAirport::findByIdent(naStr_data(args[0]));
1038 // return nil here, but don't raise a runtime error; this is a
1039 // legitamate way to validate an ICAO code, for example in a
1040 // dialog box or similar.
1045 naRuntimeError(c, "airportinfo() with invalid function arguments");
1050 apt = FGAirport::findClosest(pos, maxRange, &filter);
1051 if(!apt) return naNil();
1054 return ghostForAirport(c, apt);
1057 static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
1060 SGGeod pos = globals->get_aircraft_position();
1061 argOffset += geodFromArgs(args, 0, argc, pos);
1063 if (!naIsNum(args[argOffset])) {
1064 naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset);
1067 AirportInfoFilter filter; // defaults to airports only
1068 double rangeNm = args[argOffset++].num;
1069 if (argOffset < argc) {
1070 filter.fromArg(args[argOffset++]);
1073 naRef r = naNewVector(c);
1075 FGPositioned::List apts = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1076 FGPositioned::sortByRange(apts, pos);
1078 BOOST_FOREACH(FGPositionedRef a, apts) {
1079 FGAirport* apt = (FGAirport*) a.get();
1080 naVec_append(r, ghostForAirport(c, apt));
1086 static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
1088 if (!naIsString(args[0])) {
1089 naRuntimeError(c, "findAirportsByICAO expects string as arg 0");
1093 string prefix(naStr_data(args[argOffset++]));
1094 AirportInfoFilter filter; // defaults to airports only
1095 if (argOffset < argc) {
1096 filter.fromArg(args[argOffset++]);
1099 naRef r = naNewVector(c);
1101 FGPositioned::List apts = FGPositioned::findAllWithIdent(prefix, &filter, false);
1103 BOOST_FOREACH(FGPositionedRef a, apts) {
1104 FGAirport* apt = (FGAirport*) a.get();
1105 naVec_append(r, ghostForAirport(c, apt));
1111 static naRef f_airport_tower(naContext c, naRef me, int argc, naRef* args)
1113 FGAirport* apt = airportGhost(me);
1115 naRuntimeError(c, "airport.tower called on non-airport object");
1118 // build a hash for the tower position
1119 SGGeod towerLoc = apt->getTowerLocation();
1120 naRef tower = naNewHash(c);
1121 hashset(c, tower, "lat", naNum(towerLoc.getLatitudeDeg()));
1122 hashset(c, tower, "lon", naNum(towerLoc.getLongitudeDeg()));
1123 hashset(c, tower, "elevation", naNum(towerLoc.getElevationM()));
1127 static naRef f_airport_comms(naContext c, naRef me, int argc, naRef* args)
1129 FGAirport* apt = airportGhost(me);
1131 naRuntimeError(c, "airport.comms called on non-airport object");
1133 naRef comms = naNewVector(c);
1135 // if we have an explicit type, return a simple vector of frequencies
1136 if (argc > 0 && naIsScalar(args[0])) {
1137 std::string commName = naStr_data(args[0]);
1138 FGPositioned::Type commType = FGPositioned::typeFromName(commName);
1140 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStationsOfType(commType)) {
1141 naVec_append(comms, naNum(comm->freqMHz()));
1144 // otherwise return a vector of hashes, one for each comm station.
1145 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStations()) {
1146 naRef commHash = naNewHash(c);
1147 hashset(c, commHash, "frequency", naNum(comm->freqMHz()));
1148 hashset(c, commHash, "ident", stringToNasal(c, comm->ident()));
1149 naVec_append(comms, commHash);
1156 static naRef f_airport_runway(naContext c, naRef me, int argc, naRef* args)
1158 FGAirport* apt = airportGhost(me);
1160 naRuntimeError(c, "airport.runway called on non-airport object");
1163 if ((argc < 1) || !naIsString(args[0])) {
1164 naRuntimeError(c, "airport.runway expects a runway ident argument");
1167 std::string ident(naStr_data(args[0]));
1168 boost::to_upper(ident);
1169 if (!apt->hasRunwayWithIdent(ident)) {
1173 return ghostForRunway(c, apt->getRunwayByIdent(ident));
1176 static naRef f_airport_taxiway(naContext c, naRef me, int argc, naRef* args)
1178 FGAirport* apt = airportGhost(me);
1180 naRuntimeError(c, "airport.taxiway called on non-airport object");
1183 if ((argc < 1) || !naIsString(args[0])) {
1184 naRuntimeError(c, "airport.taxiway expects a taxiway ident argument");
1187 naRef taxiways = naNewVector(c);
1189 for (unsigned int i = 0; i < apt->numTaxiways(); i++) {
1190 naVec_append(taxiways, ghostForTaxiway(c, apt->getTaxiwayByIndex(i)));
1196 static naRef f_airport_sids(naContext c, naRef me, int argc, naRef* args)
1198 FGAirport* apt = airportGhost(me);
1200 naRuntimeError(c, "airport.sids called on non-airport object");
1203 naRef sids = naNewVector(c);
1205 FGRunway* rwy = NULL;
1206 if (argc > 0 && naIsString(args[0])) {
1207 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1211 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1212 } else if (argc > 0) {
1213 rwy = runwayGhost(args[0]);
1217 BOOST_FOREACH(flightgear::SID* sid, rwy->getSIDs()) {
1218 naRef procId = stringToNasal(c, sid->ident());
1219 naVec_append(sids, procId);
1222 for (unsigned int s=0; s<apt->numSIDs(); ++s) {
1223 flightgear::SID* sid = apt->getSIDByIndex(s);
1224 naRef procId = stringToNasal(c, sid->ident());
1225 naVec_append(sids, procId);
1232 static naRef f_airport_stars(naContext c, naRef me, int argc, naRef* args)
1234 FGAirport* apt = airportGhost(me);
1236 naRuntimeError(c, "airport.stars called on non-airport object");
1239 naRef stars = naNewVector(c);
1241 FGRunway* rwy = NULL;
1242 if (argc > 0 && naIsString(args[0])) {
1243 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1247 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1248 } else if (argc > 0) {
1249 rwy = runwayGhost(args[0]);
1253 BOOST_FOREACH(flightgear::STAR* s, rwy->getSTARs()) {
1254 naRef procId = stringToNasal(c, s->ident());
1255 naVec_append(stars, procId);
1258 for (unsigned int s=0; s<apt->numSTARs(); ++s) {
1259 flightgear::STAR* star = apt->getSTARByIndex(s);
1260 naRef procId = stringToNasal(c, star->ident());
1261 naVec_append(stars, procId);
1268 static naRef f_airport_approaches(naContext c, naRef me, int argc, naRef* args)
1270 FGAirport* apt = airportGhost(me);
1272 naRuntimeError(c, "airport.getApproachList called on non-airport object");
1275 naRef approaches = naNewVector(c);
1277 ProcedureType ty = PROCEDURE_INVALID;
1278 if ((argc > 1) && naIsString(args[1])) {
1279 std::string u(naStr_data(args[1]));
1281 if (u == "NDB") ty = PROCEDURE_APPROACH_NDB;
1282 if (u == "VOR") ty = PROCEDURE_APPROACH_VOR;
1283 if (u == "ILS") ty = PROCEDURE_APPROACH_ILS;
1284 if (u == "RNAV") ty = PROCEDURE_APPROACH_RNAV;
1287 FGRunway* rwy = NULL;
1288 if (argc > 0 && (rwy = runwayGhost(args[0]))) {
1290 } else if (argc > 0 && naIsString(args[0])) {
1291 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1295 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1299 BOOST_FOREACH(Approach* s, rwy->getApproaches()) {
1300 if ((ty != PROCEDURE_INVALID) && (s->type() != ty)) {
1304 naRef procId = stringToNasal(c, s->ident());
1305 naVec_append(approaches, procId);
1308 // no runway specified, report them all
1309 for (unsigned int s=0; s<apt->numApproaches(); ++s) {
1310 Approach* app = apt->getApproachByIndex(s);
1311 if ((ty != PROCEDURE_INVALID) && (app->type() != ty)) {
1315 naRef procId = stringToNasal(c, app->ident());
1316 naVec_append(approaches, procId);
1323 static naRef f_airport_parking(naContext c, naRef me, int argc, naRef* args)
1325 FGAirport* apt = airportGhost(me);
1327 naRuntimeError(c, "airport.parking called on non-airport object");
1330 naRef r = naNewVector(c);
1332 bool onlyAvailable = false;
1334 if (argc > 0 && naIsString(args[0])) {
1335 type = naStr_data(args[0]);
1338 if ((argc > 1) && naIsNum(args[1])) {
1339 onlyAvailable = (args[1].num != 0.0);
1342 FGAirportDynamics* dynamics = apt->getDynamics();
1343 PositionedIDVec parkings = flightgear::NavDataCache::instance()->airportItemsOfType(apt->guid(),
1344 FGPositioned::PARKING);
1346 BOOST_FOREACH(PositionedID parking, parkings) {
1347 // filter out based on availability and type
1348 if (onlyAvailable && !dynamics->isParkingAvailable(parking)) {
1352 FGParking* park = dynamics->getParking(parking);
1353 if (!type.empty() && (park->getType() != type)) {
1357 const SGGeod& parkLoc = park->geod();
1358 naRef ph = naNewHash(c);
1359 hashset(c, ph, "name", stringToNasal(c, park->getName()));
1360 hashset(c, ph, "lat", naNum(parkLoc.getLatitudeDeg()));
1361 hashset(c, ph, "lon", naNum(parkLoc.getLongitudeDeg()));
1362 hashset(c, ph, "elevation", naNum(parkLoc.getElevationM()));
1363 naVec_append(r, ph);
1369 static naRef f_airport_getSid(naContext c, naRef me, int argc, naRef* args)
1371 FGAirport* apt = airportGhost(me);
1373 naRuntimeError(c, "airport.getSid called on non-airport object");
1376 if ((argc != 1) || !naIsString(args[0])) {
1377 naRuntimeError(c, "airport.getSid passed invalid argument");
1380 string ident = naStr_data(args[0]);
1381 return ghostForProcedure(c, apt->findSIDWithIdent(ident));
1384 static naRef f_airport_getStar(naContext c, naRef me, int argc, naRef* args)
1386 FGAirport* apt = airportGhost(me);
1388 naRuntimeError(c, "airport.getStar called on non-airport object");
1391 if ((argc != 1) || !naIsString(args[0])) {
1392 naRuntimeError(c, "airport.getStar passed invalid argument");
1395 string ident = naStr_data(args[0]);
1396 return ghostForProcedure(c, apt->findSTARWithIdent(ident));
1399 static naRef f_airport_getApproach(naContext c, naRef me, int argc, naRef* args)
1401 FGAirport* apt = airportGhost(me);
1403 naRuntimeError(c, "airport.getIAP called on non-airport object");
1406 if ((argc != 1) || !naIsString(args[0])) {
1407 naRuntimeError(c, "airport.getIAP passed invalid argument");
1410 string ident = naStr_data(args[0]);
1411 return ghostForProcedure(c, apt->findApproachWithIdent(ident));
1414 static naRef f_airport_toString(naContext c, naRef me, int argc, naRef* args)
1416 FGAirport* apt = airportGhost(me);
1418 naRuntimeError(c, "airport.tostring called on non-airport object");
1421 return stringToNasal(c, "an airport " + apt->ident());
1424 // Returns vector of data hash for navaid of a <type>, nil on error
1425 // navaids sorted by ascending distance
1426 // navinfo([<lat>,<lon>],[<type>],[<id>])
1427 // lat/lon (numeric): use latitude/longitude instead of ac position
1428 // type: ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
1429 // id: (partial) id of the fix
1431 // navinfo("vor") returns all vors
1432 // navinfo("HAM") return all navaids who's name start with "HAM"
1433 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
1434 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
1435 // sorted by distance relative to lat=34, lon=48
1436 static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
1440 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1441 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1445 pos = globals->get_aircraft_position();
1448 FGPositioned::Type type = FGPositioned::INVALID;
1449 nav_list_type navlist;
1450 const char * id = "";
1452 if(argc > 0 && naIsString(args[0])) {
1453 const char *s = naStr_data(args[0]);
1454 if(!strcmp(s, "any")) type = FGPositioned::INVALID;
1455 else if(!strcmp(s, "fix")) type = FGPositioned::FIX;
1456 else if(!strcmp(s, "vor")) type = FGPositioned::VOR;
1457 else if(!strcmp(s, "ndb")) type = FGPositioned::NDB;
1458 else if(!strcmp(s, "ils")) type = FGPositioned::ILS;
1459 else if(!strcmp(s, "dme")) type = FGPositioned::DME;
1460 else if(!strcmp(s, "tacan")) type = FGPositioned::TACAN;
1461 else id = s; // this is an id
1466 if(argc > 0 && naIsString(args[0])) {
1468 naRuntimeError(c, "navinfo() called with navaid id");
1471 id = naStr_data(args[0]);
1477 naRuntimeError(c, "navinfo() called with too many arguments");
1481 FGNavList::TypeFilter filter(type);
1482 navlist = FGNavList::findByIdentAndFreq( pos, id, 0.0, &filter );
1484 naRef reply = naNewVector(c);
1485 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
1486 naVec_append( reply, ghostForNavaid(c, *it) );
1491 static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
1494 SGGeod pos = globals->get_aircraft_position();
1495 argOffset += geodFromArgs(args, 0, argc, pos);
1497 if (!naIsNum(args[argOffset])) {
1498 naRuntimeError(c, "findNavaidsWithinRange expected range (in nm) as arg %d", argOffset);
1501 FGPositioned::Type type = FGPositioned::INVALID;
1502 double rangeNm = args[argOffset++].num;
1503 if (argOffset < argc) {
1504 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1507 naRef r = naNewVector(c);
1508 FGNavList::TypeFilter filter(type);
1509 FGPositioned::List navs = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1510 FGPositioned::sortByRange(navs, pos);
1512 BOOST_FOREACH(FGPositionedRef a, navs) {
1513 FGNavRecord* nav = (FGNavRecord*) a.get();
1514 naVec_append(r, ghostForNavaid(c, nav));
1520 static naRef f_findNavaidByFrequency(naContext c, naRef me, int argc, naRef* args)
1523 SGGeod pos = globals->get_aircraft_position();
1524 argOffset += geodFromArgs(args, 0, argc, pos);
1526 if (!naIsNum(args[argOffset])) {
1527 naRuntimeError(c, "findNavaidByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1530 FGPositioned::Type type = FGPositioned::INVALID;
1531 double freqMhz = args[argOffset++].num;
1532 if (argOffset < argc) {
1533 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1536 FGNavList::TypeFilter filter(type);
1537 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1542 return ghostForNavaid(c, navs.front().ptr());
1545 static naRef f_findNavaidsByFrequency(naContext c, naRef me, int argc, naRef* args)
1548 SGGeod pos = globals->get_aircraft_position();
1549 argOffset += geodFromArgs(args, 0, argc, pos);
1551 if (!naIsNum(args[argOffset])) {
1552 naRuntimeError(c, "findNavaidsByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1555 FGPositioned::Type type = FGPositioned::INVALID;
1556 double freqMhz = args[argOffset++].num;
1557 if (argOffset < argc) {
1558 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1561 naRef r = naNewVector(c);
1563 FGNavList::TypeFilter filter(type);
1564 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1566 BOOST_FOREACH(nav_rec_ptr a, navs) {
1567 naVec_append(r, ghostForNavaid(c, a.ptr()));
1573 static naRef f_findNavaidsByIdent(naContext c, naRef me, int argc, naRef* args)
1576 SGGeod pos = globals->get_aircraft_position();
1577 argOffset += geodFromArgs(args, 0, argc, pos);
1579 if (!naIsString(args[argOffset])) {
1580 naRuntimeError(c, "findNavaidsByIdent expectes ident string as arg %d", argOffset);
1583 FGPositioned::Type type = FGPositioned::INVALID;
1584 string ident = naStr_data(args[argOffset++]);
1585 if (argOffset < argc) {
1586 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1589 FGNavList::TypeFilter filter(type);
1590 naRef r = naNewVector(c);
1591 nav_list_type navs = FGNavList::findByIdentAndFreq(pos, ident, 0.0, &filter);
1593 BOOST_FOREACH(nav_rec_ptr a, navs) {
1594 naVec_append(r, ghostForNavaid(c, a.ptr()));
1600 static naRef f_findFixesByIdent(naContext c, naRef me, int argc, naRef* args)
1603 SGGeod pos = globals->get_aircraft_position();
1604 argOffset += geodFromArgs(args, 0, argc, pos);
1606 if (!naIsString(args[argOffset])) {
1607 naRuntimeError(c, "findFixesByIdent expectes ident string as arg %d", argOffset);
1610 string ident(naStr_data(args[argOffset]));
1611 naRef r = naNewVector(c);
1613 FGPositioned::TypeFilter filter(FGPositioned::FIX);
1614 FGPositioned::List fixes = FGPositioned::findAllWithIdent(ident, &filter);
1615 FGPositioned::sortByRange(fixes, pos);
1617 BOOST_FOREACH(FGPositionedRef f, fixes) {
1618 naVec_append(r, ghostForFix(c, (FGFix*) f.ptr()));
1624 // Convert a cartesian point to a geodetic lat/lon/altitude.
1625 static naRef f_magvar(naContext c, naRef me, int argc, naRef* args)
1627 SGGeod pos = globals->get_aircraft_position();
1629 // fine, use aircraft position
1630 } else if (geodFromArgs(args, 0, argc, pos)) {
1633 naRuntimeError(c, "magvar() expects no arguments, a positioned hash or lat,lon pair");
1636 double jd = globals->get_time_params()->getJD();
1637 double magvarDeg = sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
1638 return naNum(magvarDeg);
1641 static naRef f_courseAndDistance(naContext c, naRef me, int argc, naRef* args)
1643 SGGeod from = globals->get_aircraft_position(), to, p;
1644 int argOffset = geodFromArgs(args, 0, argc, p);
1645 if (geodFromArgs(args, argOffset, argc, to)) {
1646 from = p; // we parsed both FROM and TO args, so first was from
1648 to = p; // only parsed one arg, so FROM is current
1651 if (argOffset == 0) {
1652 naRuntimeError(c, "invalid arguments to courseAndDistance");
1655 double course, course2, d;
1656 SGGeodesy::inverse(from, to, course, course2, d);
1658 naRef result = naNewVector(c);
1659 naVec_append(result, naNum(course));
1660 naVec_append(result, naNum(d * SG_METER_TO_NM));
1664 static naRef f_greatCircleMove(naContext c, naRef me, int argc, naRef* args)
1666 SGGeod from = globals->get_aircraft_position(), to;
1669 // complication - don't inerpret two doubles (as the only args)
1670 // as a lat,lon pair - only do so if we have at least three args.
1672 argOffset = geodFromArgs(args, 0, argc, from);
1675 if ((argOffset + 1) >= argc) {
1676 naRuntimeError(c, "isufficent arguments to greatCircleMove");
1679 if (!naIsNum(args[argOffset]) || !naIsNum(args[argOffset+1])) {
1680 naRuntimeError(c, "invalid arguments %d and %d to greatCircleMove",
1681 argOffset, argOffset + 1);
1684 double course = args[argOffset].num, course2;
1685 double distanceNm = args[argOffset + 1].num;
1686 SGGeodesy::direct(from, course, distanceNm * SG_NM_TO_METER, to, course2);
1689 naRef coord = naNewHash(c);
1690 hashset(c, coord, "lat", naNum(to.getLatitudeDeg()));
1691 hashset(c, coord, "lon", naNum(to.getLongitudeDeg()));
1695 static naRef f_tilePath(naContext c, naRef me, int argc, naRef* args)
1697 SGGeod pos = globals->get_aircraft_position();
1698 geodFromArgs(args, 0, argc, pos);
1700 return stringToNasal(c, b.gen_base_path());
1703 static naRef f_tileIndex(naContext c, naRef me, int argc, naRef* args)
1705 SGGeod pos = globals->get_aircraft_position();
1706 geodFromArgs(args, 0, argc, pos);
1708 return naNum(b.gen_index());
1711 static naRef f_route(naContext c, naRef me, int argc, naRef* args)
1714 FGRouteMgr* rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
1715 return ghostForFlightPlan(c, rm->flightPlan());
1718 if ((argc > 0) && naIsString(args[0])) {
1719 flightgear::FlightPlan* fp = new flightgear::FlightPlan;
1720 SGPath path(naStr_data(args[0]));
1721 if (!path.exists()) {
1722 naRuntimeError(c, "flightplan, no file at path %s", path.c_str());
1725 if (!fp->load(path)) {
1726 SG_LOG(SG_NASAL, SG_WARN, "failed to load flight-plan from " << path);
1731 return ghostForFlightPlan(c, fp);
1734 naRuntimeError(c, "bad arguments to flightplan()");
1738 class NasalFPDelegate : public FlightPlan::Delegate
1741 NasalFPDelegate(FlightPlan* fp, FGNasalSys* sys, naRef ins) :
1746 SG_LOG(SG_NASAL, SG_INFO, "created Nasal delegate for " << fp);
1747 _gcSaveKey = _nasal->gcSave(ins);
1750 virtual ~NasalFPDelegate()
1752 SG_LOG(SG_NASAL, SG_INFO, "destroying Nasal delegate for " << _plan);
1753 _nasal->gcRelease(_gcSaveKey);
1756 virtual void departureChanged()
1758 callDelegateMethod("departureChanged");
1761 virtual void arrivalChanged()
1763 callDelegateMethod("arrivalChanged");
1766 virtual void waypointsChanged()
1768 callDelegateMethod("waypointsChanged");
1771 virtual void currentWaypointChanged()
1773 callDelegateMethod("currentWaypointChanged");
1777 void callDelegateMethod(const char* method)
1780 naMember_cget(_nasal->context(), _instance, method, &f);
1782 return; // no method on the delegate
1786 arg[0] = ghostForFlightPlan(_nasal->context(), _plan);
1787 _nasal->callMethod(f, _instance, 1, arg, naNil());
1796 class NasalFPDelegateFactory : public FlightPlan::DelegateFactory
1799 NasalFPDelegateFactory(naRef code)
1801 _nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1803 _gcSaveKey = _nasal->gcSave(_func);
1806 ~NasalFPDelegateFactory()
1808 _nasal->gcRelease(_gcSaveKey);
1811 virtual FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp)
1814 args[0] = ghostForFlightPlan(_nasal->context(), fp);
1815 naRef instance = _nasal->call(_func, 1, args, naNil());
1816 if (naIsNil(instance)) {
1820 return new NasalFPDelegate(fp, _nasal, instance);
1828 static naRef f_registerFPDelegate(naContext c, naRef me, int argc, naRef* args)
1830 if ((argc < 1) || !naIsFunc(args[0])) {
1831 naRuntimeError(c, "non-function argument to registerFlightPlanDelegate");
1834 NasalFPDelegateFactory* factory = new NasalFPDelegateFactory(args[0]);
1835 FlightPlan::registerDelegateFactory(factory);
1840 static WayptRef wayptFromArg(naRef arg)
1842 WayptRef r = wayptGhost(arg);
1847 FGPositioned* pos = positionedGhost(arg);
1849 // let's check if the arg is hash, coudl extra a geod and hence build
1850 // a simple waypoint
1855 // special-case for runways
1856 if (pos->type() == FGPositioned::RUNWAY) {
1857 return new RunwayWaypt((FGRunway*) pos, NULL);
1860 return new NavaidWaypoint(pos, NULL);
1863 static naRef convertWayptVecToNasal(naContext c, const WayptVec& wps)
1865 naRef result = naNewVector(c);
1866 BOOST_FOREACH(WayptRef wpt, wps) {
1867 naVec_append(result, ghostForWaypt(c, wpt.get()));
1872 static naRef f_airwaySearch(naContext c, naRef me, int argc, naRef* args)
1875 naRuntimeError(c, "airwaysSearch needs at least two arguments");
1878 WayptRef start = wayptFromArg(args[0]),
1879 end = wayptFromArg(args[1]);
1881 if (!start || !end) {
1882 SG_LOG(SG_NASAL, SG_WARN, "airwaysSearch: start or end points are invalid");
1886 bool highLevel = true;
1887 if ((argc > 2) && naIsString(args[2])) {
1888 if (!strcmp(naStr_data(args[2]), "lowlevel")) {
1895 Airway::highLevel()->route(start, end, route);
1897 Airway::lowLevel()->route(start, end, route);
1900 return convertWayptVecToNasal(c, route);
1903 static naRef f_createWP(naContext c, naRef me, int argc, naRef* args)
1906 int argOffset = geodFromArgs(args, 0, argc, pos);
1908 if (((argc - argOffset) < 1) || !naIsString(args[argOffset])) {
1909 naRuntimeError(c, "createWP: no identifier supplied");
1912 string ident = naStr_data(args[argOffset++]);
1913 WayptRef wpt = new BasicWaypt(pos, ident, NULL);
1915 // set waypt flags - approach, departure, pseudo, etc
1916 if (argc > argOffset) {
1917 WayptFlag f = wayptFlagFromString(naStr_data(args[argOffset++]));
1921 return ghostForWaypt(c, wpt);
1924 static naRef f_createWPFrom(naContext c, naRef me, int argc, naRef* args)
1927 naRuntimeError(c, "createWPFrom: need at least one argument");
1930 FGPositioned* positioned = positionedGhost(args[0]);
1932 naRuntimeError(c, "createWPFrom: couldn;t convert arg[0] to FGPositioned");
1936 if (positioned->type() == FGPositioned::RUNWAY) {
1937 wpt = new RunwayWaypt((FGRunway*) positioned, NULL);
1939 wpt = new NavaidWaypoint(positioned, NULL);
1942 // set waypt flags - approach, departure, pseudo, etc
1944 WayptFlag f = wayptFlagFromString(naStr_data(args[1]));
1948 return ghostForWaypt(c, wpt);
1951 static naRef f_flightplan_getWP(naContext c, naRef me, int argc, naRef* args)
1953 FlightPlan* fp = flightplanGhost(me);
1955 naRuntimeError(c, "flightplan.getWP called on non-flightplan object");
1960 index = fp->currentIndex();
1962 index = (int) naNumValue(args[0]).num;
1965 if ((index < 0) || (index >= fp->numLegs())) {
1969 return ghostForLeg(c, fp->legAtIndex(index));
1972 static naRef f_flightplan_currentWP(naContext c, naRef me, int argc, naRef* args)
1974 FlightPlan* fp = flightplanGhost(me);
1976 naRuntimeError(c, "flightplan.currentWP called on non-flightplan object");
1978 return ghostForLeg(c, fp->currentLeg());
1981 static naRef f_flightplan_nextWP(naContext c, naRef me, int argc, naRef* args)
1983 FlightPlan* fp = flightplanGhost(me);
1985 naRuntimeError(c, "flightplan.nextWP called on non-flightplan object");
1987 return ghostForLeg(c, fp->nextLeg());
1990 static naRef f_flightplan_numWaypoints(naContext c, naRef me, int argc, naRef* args)
1992 FlightPlan* fp = flightplanGhost(me);
1994 naRuntimeError(c, "flightplan.numWaypoints called on non-flightplan object");
1996 return naNum(fp->numLegs());
1999 static naRef f_flightplan_appendWP(naContext c, naRef me, int argc, naRef* args)
2001 FlightPlan* fp = flightplanGhost(me);
2003 naRuntimeError(c, "flightplan.appendWP called on non-flightplan object");
2006 WayptRef wp = wayptGhost(args[0]);
2007 int index = fp->numLegs();
2008 fp->insertWayptAtIndex(wp.get(), index);
2009 return naNum(index);
2012 static naRef f_flightplan_insertWP(naContext c, naRef me, int argc, naRef* args)
2014 FlightPlan* fp = flightplanGhost(me);
2016 naRuntimeError(c, "flightplan.insertWP called on non-flightplan object");
2019 WayptRef wp = wayptGhost(args[0]);
2020 int index = -1; // append
2021 if ((argc > 1) && naIsNum(args[1])) {
2022 index = (int) args[1].num;
2025 fp->insertWayptAtIndex(wp.get(), index);
2029 static naRef f_flightplan_insertWPAfter(naContext c, naRef me, int argc, naRef* args)
2031 FlightPlan* fp = flightplanGhost(me);
2033 naRuntimeError(c, "flightplan.insertWPAfter called on non-flightplan object");
2036 WayptRef wp = wayptGhost(args[0]);
2037 int index = -1; // append
2038 if ((argc > 1) && naIsNum(args[1])) {
2039 index = (int) args[1].num;
2042 fp->insertWayptAtIndex(wp.get(), index + 1);
2046 static naRef f_flightplan_insertWaypoints(naContext c, naRef me, int argc, naRef* args)
2048 FlightPlan* fp = flightplanGhost(me);
2050 naRuntimeError(c, "flightplan.insertWaypoints called on non-flightplan object");
2054 if (!naIsVector(args[0])) {
2055 naRuntimeError(c, "flightplan.insertWaypoints expects vector as first arg");
2058 int count = naVec_size(args[0]);
2059 for (int i=0; i<count; ++i) {
2060 Waypt* wp = wayptGhost(naVec_get(args[0], i));
2066 int index = -1; // append
2067 if ((argc > 1) && naIsNum(args[1])) {
2068 index = (int) args[1].num;
2071 fp->insertWayptsAtIndex(wps, index);
2075 static naRef f_flightplan_deleteWP(naContext c, naRef me, int argc, naRef* args)
2077 FlightPlan* fp = flightplanGhost(me);
2079 naRuntimeError(c, "flightplan.deleteWP called on non-flightplan object");
2082 if ((argc < 1) || !naIsNum(args[0])) {
2083 naRuntimeError(c, "bad argument to flightplan.deleteWP");
2086 int index = (int) args[0].num;
2087 fp->deleteIndex(index);
2091 static naRef f_flightplan_clearPlan(naContext c, naRef me, int argc, naRef* args)
2093 FlightPlan* fp = flightplanGhost(me);
2095 naRuntimeError(c, "flightplan.clearPlan called on non-flightplan object");
2102 static naRef f_flightplan_clearWPType(naContext c, naRef me, int argc, naRef* args)
2104 FlightPlan* fp = flightplanGhost(me);
2106 naRuntimeError(c, "flightplan.clearWPType called on non-flightplan object");
2110 naRuntimeError(c, "insufficent args to flightplan.clearWPType");
2113 WayptFlag flag = wayptFlagFromString(naStr_data(args[0]));
2114 fp->clearWayptsWithFlag(flag);
2118 static naRef f_flightplan_clone(naContext c, naRef me, int argc, naRef* args)
2120 FlightPlan* fp = flightplanGhost(me);
2122 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2125 return ghostForFlightPlan(c, fp->clone());
2128 static naRef f_flightplan_pathGeod(naContext c, naRef me, int argc, naRef* args)
2130 FlightPlan* fp = flightplanGhost(me);
2132 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2135 if ((argc < 1) || !naIsNum(args[0])) {
2136 naRuntimeError(c, "bad argument to flightplan.pathGeod");
2139 if ((argc > 1) && !naIsNum(args[1])) {
2140 naRuntimeError(c, "bad argument to flightplan.pathGeod");
2143 int index = (int) args[0].num;
2144 double offset = (argc > 1) ? args[1].num : 0.0;
2145 naRef result = naNewHash(c);
2146 SGGeod g = fp->pointAlongRoute(index, offset);
2147 hashset(c, result, "lat", naNum(g.getLatitudeDeg()));
2148 hashset(c, result, "lon", naNum(g.getLongitudeDeg()));
2153 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
2155 FlightPlan::Leg* leg = fpLegGhost(me);
2157 naRuntimeError(c, "leg.setSpeed called on non-flightplan-leg object");
2161 naRuntimeError(c, "bad arguments to leg.setSpeed");
2164 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2165 leg->setSpeed(rr, args[0].num);
2169 static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
2171 FlightPlan::Leg* leg = fpLegGhost(me);
2173 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2177 naRuntimeError(c, "bad arguments to leg.setAltitude");
2180 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2181 leg->setAltitude(rr, args[0].num);
2185 static naRef f_leg_path(naContext c, naRef me, int argc, naRef* args)
2187 FlightPlan::Leg* leg = fpLegGhost(me);
2189 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2192 RoutePath path(leg->owner());
2193 SGGeodVec gv(path.pathForIndex(leg->index()));
2195 naRef result = naNewVector(c);
2196 BOOST_FOREACH(SGGeod p, gv) {
2197 // construct a geo.Coord!
2198 naRef coord = naNewHash(c);
2199 hashset(c, coord, "lat", naNum(p.getLatitudeDeg()));
2200 hashset(c, coord, "lon", naNum(p.getLongitudeDeg()));
2201 naVec_append(result, coord);
2207 static naRef f_waypoint_navaid(naContext c, naRef me, int argc, naRef* args)
2209 flightgear::Waypt* w = wayptGhost(me);
2211 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2214 FGPositioned* pos = w->source();
2219 switch (pos->type()) {
2220 case FGPositioned::VOR:
2221 case FGPositioned::NDB:
2222 case FGPositioned::ILS:
2223 case FGPositioned::LOC:
2224 case FGPositioned::GS:
2225 case FGPositioned::DME:
2226 case FGPositioned::TACAN: {
2227 FGNavRecord* nav = (FGNavRecord*) pos;
2228 return ghostForNavaid(c, nav);
2236 static naRef f_waypoint_airport(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 || FGAirport::isAirportType(pos)) {
2248 return ghostForAirport(c, (FGAirport*) pos);
2251 static naRef f_waypoint_runway(naContext c, naRef me, int argc, naRef* args)
2253 flightgear::Waypt* w = wayptGhost(me);
2255 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2258 FGPositioned* pos = w->source();
2259 if (!pos || (pos->type() != FGPositioned::RUNWAY)) {
2263 return ghostForRunway(c, (FGRunway*) pos);
2266 static naRef f_procedure_transition(naContext c, naRef me, int argc, naRef* args)
2268 Procedure* proc = procedureGhost(me);
2270 naRuntimeError(c, "procedure.transition called on non-procedure object");
2273 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2274 naRuntimeError(c, "procedure.transition called on non-SID or -STAR");
2277 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2278 Transition* trans = ad->findTransitionByName(naStr_data(args[0]));
2280 return ghostForProcedure(c, trans);
2283 static naRef f_procedure_route(naContext c, naRef me, int argc, naRef* args)
2285 Procedure* proc = procedureGhost(me);
2287 naRuntimeError(c, "procedure.route called on non-procedure object");
2290 // wrapping up tow different routines here - approach routing from the IAF
2291 // to the associated runway, and SID/STAR routing via an enroute transition
2292 // and possibly a runway transition or not.
2293 if (Approach::isApproach(proc->type())) {
2296 iaf = wayptFromArg(args[0]);
2300 Approach* app = (Approach*) proc;
2301 if (!app->route(iaf, r)) {
2302 SG_LOG(SG_NASAL, SG_WARN, "procedure.route failed for Approach somehow");
2306 return convertWayptVecToNasal(c, r);
2307 } else if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2308 naRuntimeError(c, "procedure.route called on unsuitable procedure type");
2312 FGRunway* rwy = runwayGhost(args[0]);
2313 if (rwy) ++argOffset;
2315 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2316 Transition* trans = NULL;
2317 if (argOffset < argc) {
2318 trans = (Transition*) procedureGhost(args[argOffset]);
2321 // note either runway or trans may be NULL - that's ok
2323 if (!ad->route(rwy, trans, r)) {
2324 SG_LOG(SG_NASAL, SG_WARN, "prcoedure.route failed for ArrvialDeparture somehow");
2328 return convertWayptVecToNasal(c, r);
2332 // Table of extension functions. Terminate with zeros.
2333 static struct { const char* name; naCFunction func; } funcs[] = {
2334 { "carttogeod", f_carttogeod },
2335 { "geodtocart", f_geodtocart },
2336 { "geodinfo", f_geodinfo },
2337 { "airportinfo", f_airportinfo },
2338 { "findAirportsWithinRange", f_findAirportsWithinRange },
2339 { "findAirportsByICAO", f_findAirportsByICAO },
2340 { "navinfo", f_navinfo },
2341 { "findNavaidsWithinRange", f_findNavaidsWithinRange },
2342 { "findNavaidByFrequency", f_findNavaidByFrequency },
2343 { "findNavaidsByFrequency", f_findNavaidsByFrequency },
2344 { "findNavaidsByID", f_findNavaidsByIdent },
2345 { "findFixesByID", f_findFixesByIdent },
2346 { "flightplan", f_route },
2347 { "registerFlightPlanDelegate", f_registerFPDelegate },
2348 { "createWP", f_createWP },
2349 { "createWPFrom", f_createWPFrom },
2350 { "airwaysRoute", f_airwaySearch },
2351 { "magvar", f_magvar },
2352 { "courseAndDistance", f_courseAndDistance },
2353 { "greatCircleMove", f_greatCircleMove },
2354 { "tileIndex", f_tileIndex },
2355 { "tilePath", f_tilePath },
2360 naRef initNasalPositioned(naRef globals, naContext c, naRef gcSave)
2362 airportPrototype = naNewHash(c);
2363 hashset(c, gcSave, "airportProto", airportPrototype);
2365 hashset(c, airportPrototype, "runway", naNewFunc(c, naNewCCode(c, f_airport_runway)));
2366 hashset(c, airportPrototype, "taxiway", naNewFunc(c, naNewCCode(c, f_airport_taxiway)));
2367 hashset(c, airportPrototype, "tower", naNewFunc(c, naNewCCode(c, f_airport_tower)));
2368 hashset(c, airportPrototype, "comms", naNewFunc(c, naNewCCode(c, f_airport_comms)));
2369 hashset(c, airportPrototype, "sids", naNewFunc(c, naNewCCode(c, f_airport_sids)));
2370 hashset(c, airportPrototype, "stars", naNewFunc(c, naNewCCode(c, f_airport_stars)));
2371 hashset(c, airportPrototype, "getApproachList", naNewFunc(c, naNewCCode(c, f_airport_approaches)));
2372 hashset(c, airportPrototype, "parking", naNewFunc(c, naNewCCode(c, f_airport_parking)));
2373 hashset(c, airportPrototype, "getSid", naNewFunc(c, naNewCCode(c, f_airport_getSid)));
2374 hashset(c, airportPrototype, "getStar", naNewFunc(c, naNewCCode(c, f_airport_getStar)));
2375 hashset(c, airportPrototype, "getIAP", naNewFunc(c, naNewCCode(c, f_airport_getApproach)));
2376 hashset(c, airportPrototype, "tostring", naNewFunc(c, naNewCCode(c, f_airport_toString)));
2378 flightplanPrototype = naNewHash(c);
2379 hashset(c, gcSave, "flightplanProto", flightplanPrototype);
2381 hashset(c, flightplanPrototype, "getWP", naNewFunc(c, naNewCCode(c, f_flightplan_getWP)));
2382 hashset(c, flightplanPrototype, "currentWP", naNewFunc(c, naNewCCode(c, f_flightplan_currentWP)));
2383 hashset(c, flightplanPrototype, "nextWP", naNewFunc(c, naNewCCode(c, f_flightplan_nextWP)));
2384 hashset(c, flightplanPrototype, "getPlanSize", naNewFunc(c, naNewCCode(c, f_flightplan_numWaypoints)));
2385 hashset(c, flightplanPrototype, "appendWP", naNewFunc(c, naNewCCode(c, f_flightplan_appendWP)));
2386 hashset(c, flightplanPrototype, "insertWP", naNewFunc(c, naNewCCode(c, f_flightplan_insertWP)));
2387 hashset(c, flightplanPrototype, "deleteWP", naNewFunc(c, naNewCCode(c, f_flightplan_deleteWP)));
2388 hashset(c, flightplanPrototype, "insertWPAfter", naNewFunc(c, naNewCCode(c, f_flightplan_insertWPAfter)));
2389 hashset(c, flightplanPrototype, "insertWaypoints", naNewFunc(c, naNewCCode(c, f_flightplan_insertWaypoints)));
2390 hashset(c, flightplanPrototype, "cleanPlan", naNewFunc(c, naNewCCode(c, f_flightplan_clearPlan)));
2391 hashset(c, flightplanPrototype, "clearWPType", naNewFunc(c, naNewCCode(c, f_flightplan_clearWPType)));
2392 hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone)));
2393 hashset(c, flightplanPrototype, "pathGeod", naNewFunc(c, naNewCCode(c, f_flightplan_pathGeod)));
2395 waypointPrototype = naNewHash(c);
2396 hashset(c, gcSave, "wayptProto", waypointPrototype);
2398 hashset(c, waypointPrototype, "navaid", naNewFunc(c, naNewCCode(c, f_waypoint_navaid)));
2399 hashset(c, waypointPrototype, "runway", naNewFunc(c, naNewCCode(c, f_waypoint_runway)));
2400 hashset(c, waypointPrototype, "airport", naNewFunc(c, naNewCCode(c, f_waypoint_airport)));
2402 procedurePrototype = naNewHash(c);
2403 hashset(c, gcSave, "procedureProto", procedurePrototype);
2404 hashset(c, procedurePrototype, "transition", naNewFunc(c, naNewCCode(c, f_procedure_transition)));
2405 hashset(c, procedurePrototype, "route", naNewFunc(c, naNewCCode(c, f_procedure_route)));
2407 fpLegPrototype = naNewHash(c);
2408 hashset(c, gcSave, "fpLegProto", fpLegPrototype);
2409 hashset(c, fpLegPrototype, "setSpeed", naNewFunc(c, naNewCCode(c, f_leg_setSpeed)));
2410 hashset(c, fpLegPrototype, "setAltitude", naNewFunc(c, naNewCCode(c, f_leg_setAltitude)));
2411 hashset(c, fpLegPrototype, "path", naNewFunc(c, naNewCCode(c, f_leg_path)));
2413 for(int i=0; funcs[i].name; i++) {
2414 hashset(c, globals, funcs[i].name,
2415 naNewFunc(c, naNewCCode(c, funcs[i].func)));
2421 void postinitNasalPositioned(naRef globals, naContext c)
2423 naRef geoModule = naHash_cget(globals, (char*) "geo");
2424 if (naIsNil(geoModule)) {
2425 SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalPositioned: geo.nas not loaded");
2429 geoCoordClass = naHash_cget(geoModule, (char*) "Coord");