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 };
74 naGhostType TaxiwayGhostType = { positionedGhostDestroy, "taxiway", runwayGhostGetMember, 0 };
76 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out);
77 naGhostType FixGhostType = { positionedGhostDestroy, "fix", fixGhostGetMember, 0 };
79 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out);
80 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value);
82 naGhostType WayptGhostType = { wayptGhostDestroy,
85 waypointGhostSetMember};
87 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out);
88 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value);
90 naGhostType FPLegGhostType = { legGhostDestroy,
95 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out);
96 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value);
98 naGhostType FlightPlanGhostType = { routeBaseGhostDestroy,
100 flightplanGhostGetMember,
101 flightplanGhostSetMember
104 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out);
105 naGhostType ProcedureGhostType = { routeBaseGhostDestroy,
107 procedureGhostGetMember,
110 static void hashset(naContext c, naRef hash, const char* key, naRef val)
112 naRef s = naNewString(c);
113 naStr_fromdata(s, (char*)key, strlen(key));
114 naHash_set(hash, s, val);
117 static naRef stringToNasal(naContext c, const std::string& s)
119 return naStr_fromdata(naNewString(c),
120 const_cast<char *>(s.c_str()),
124 static WayptFlag wayptFlagFromString(const char* s)
126 if (!strcmp(s, "sid")) return WPT_DEPARTURE;
127 if (!strcmp(s, "star")) return WPT_ARRIVAL;
128 if (!strcmp(s, "approach")) return WPT_APPROACH;
129 if (!strcmp(s, "missed")) return WPT_MISS;
130 if (!strcmp(s, "pseudo")) return WPT_PSEUDO;
132 return (WayptFlag) 0;
135 static naRef wayptFlagToNasal(naContext c, unsigned int flags)
137 if (flags & WPT_PSEUDO) return stringToNasal(c, "pseudo");
138 if (flags & WPT_DEPARTURE) return stringToNasal(c, "sid");
139 if (flags & WPT_ARRIVAL) return stringToNasal(c, "star");
140 if (flags & WPT_MISS) return stringToNasal(c, "missed");
141 if (flags & WPT_APPROACH) return stringToNasal(c, "approach");
145 static FGPositioned* positionedGhost(naRef r)
147 if ((naGhost_type(r) == &AirportGhostType) ||
148 (naGhost_type(r) == &NavaidGhostType) ||
149 (naGhost_type(r) == &RunwayGhostType) ||
150 (naGhost_type(r) == &FixGhostType))
152 return (FGPositioned*) naGhost_ptr(r);
158 static FGAirport* airportGhost(naRef r)
160 if (naGhost_type(r) == &AirportGhostType)
161 return (FGAirport*) naGhost_ptr(r);
165 static FGNavRecord* navaidGhost(naRef r)
167 if (naGhost_type(r) == &NavaidGhostType)
168 return (FGNavRecord*) naGhost_ptr(r);
172 static FGRunway* runwayGhost(naRef r)
174 if (naGhost_type(r) == &RunwayGhostType)
175 return (FGRunway*) naGhost_ptr(r);
179 static FGTaxiway* taxiwayGhost(naRef r)
181 if (naGhost_type(r) == &TaxiwayGhostType)
182 return (FGTaxiway*) naGhost_ptr(r);
186 static FGFix* fixGhost(naRef r)
188 if (naGhost_type(r) == &FixGhostType)
189 return (FGFix*) naGhost_ptr(r);
194 static void positionedGhostDestroy(void* g)
196 FGPositioned* pos = (FGPositioned*)g;
197 if (!FGPositioned::put(pos)) // unref
201 static Waypt* wayptGhost(naRef r)
203 if (naGhost_type(r) == &WayptGhostType)
204 return (Waypt*) naGhost_ptr(r);
206 if (naGhost_type(r) == &FPLegGhostType) {
207 FlightPlan::Leg* leg = (FlightPlan::Leg*) naGhost_ptr(r);
208 return leg->waypoint();
214 static void wayptGhostDestroy(void* g)
216 Waypt* wpt = (Waypt*)g;
217 if (!Waypt::put(wpt)) // unref
221 static void legGhostDestroy(void* g)
227 static FlightPlan::Leg* fpLegGhost(naRef r)
229 if (naGhost_type(r) == &FPLegGhostType)
230 return (FlightPlan::Leg*) naGhost_ptr(r);
234 static Procedure* procedureGhost(naRef r)
236 if (naGhost_type(r) == &ProcedureGhostType)
237 return (Procedure*) naGhost_ptr(r);
241 static FlightPlan* flightplanGhost(naRef r)
243 if (naGhost_type(r) == &FlightPlanGhostType)
244 return (FlightPlan*) naGhost_ptr(r);
248 static void routeBaseGhostDestroy(void* g)
253 static naRef airportPrototype;
254 static naRef flightplanPrototype;
255 static naRef waypointPrototype;
256 static naRef geoCoordClass;
257 static naRef fpLegPrototype;
258 static naRef procedurePrototype;
260 naRef ghostForAirport(naContext c, const FGAirport* apt)
266 FGPositioned::get(apt); // take a ref
267 return naNewGhost2(c, &AirportGhostType, (void*) apt);
270 naRef ghostForNavaid(naContext c, const FGNavRecord* n)
276 FGPositioned::get(n); // take a ref
277 return naNewGhost2(c, &NavaidGhostType, (void*) n);
280 naRef ghostForRunway(naContext c, const FGRunway* r)
286 FGPositioned::get(r); // take a ref
287 return naNewGhost2(c, &RunwayGhostType, (void*) r);
290 naRef ghostForTaxiway(naContext c, const FGTaxiway* r)
296 FGPositioned::get(r); // take a ref
297 return naNewGhost2(c, &TaxiwayGhostType, (void*) r);
300 naRef ghostForFix(naContext c, const FGFix* r)
306 FGPositioned::get(r); // take a ref
307 return naNewGhost2(c, &FixGhostType, (void*) r);
311 naRef ghostForWaypt(naContext c, const Waypt* wpt)
317 Waypt::get(wpt); // take a ref
318 return naNewGhost2(c, &WayptGhostType, (void*) wpt);
321 naRef ghostForLeg(naContext c, const FlightPlan::Leg* leg)
327 return naNewGhost2(c, &FPLegGhostType, (void*) leg);
330 naRef ghostForFlightPlan(naContext c, const FlightPlan* fp)
336 return naNewGhost2(c, &FlightPlanGhostType, (void*) fp);
339 naRef ghostForProcedure(naContext c, const Procedure* proc)
345 return naNewGhost2(c, &ProcedureGhostType, (void*) proc);
348 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out)
350 const char* fieldName = naStr_data(field);
351 FGAirport* apt = (FGAirport*) g;
353 if (!strcmp(fieldName, "parents")) {
354 *out = naNewVector(c);
355 naVec_append(*out, airportPrototype);
356 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, apt->ident());
357 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, apt->name());
358 else if (!strcmp(fieldName, "lat")) *out = naNum(apt->getLatitude());
359 else if (!strcmp(fieldName, "lon")) *out = naNum(apt->getLongitude());
360 else if (!strcmp(fieldName, "elevation")) {
361 *out = naNum(apt->getElevation() * SG_FEET_TO_METER);
362 } else if (!strcmp(fieldName, "has_metar")) {
363 *out = naNum(apt->getMetar());
364 } else if (!strcmp(fieldName, "runways")) {
366 double minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft");
367 for(unsigned int r=0; r<apt->numRunways(); ++r) {
368 FGRunway* rwy(apt->getRunwayByIndex(r));
370 // ignore unusably short runways
371 if (rwy->lengthFt() < minLengthFt) {
375 naRef rwyid = stringToNasal(c, rwy->ident());
376 naRef rwydata = ghostForRunway(c, rwy);
377 naHash_set(*out, rwyid, rwydata);
380 } else if (!strcmp(fieldName, "taxiways")) {
381 *out = naNewVector(c);
382 for(unsigned int r=0; r<apt->numTaxiways(); ++r) {
383 FGTaxiway* taxi(apt->getTaxiwayByIndex(r));
384 naRef taxidata = ghostForTaxiway(c, taxi);
385 naVec_append(*out, taxidata);
395 static const char* waypointCommonGetMember(naContext c, Waypt* wpt, const char* fieldName, naRef* out)
397 if (!strcmp(fieldName, "wp_name")) *out = stringToNasal(c, wpt->ident());
398 else if (!strcmp(fieldName, "wp_type")) *out = stringToNasal(c, wpt->type());
399 else if (!strcmp(fieldName, "wp_role")) *out = wayptFlagToNasal(c, wpt->flags());
400 else if (!strcmp(fieldName, "wp_lat")) *out = naNum(wpt->position().getLatitudeDeg());
401 else if (!strcmp(fieldName, "wp_lon")) *out = naNum(wpt->position().getLongitudeDeg());
402 else if (!strcmp(fieldName, "wp_parent_name")) {
403 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
404 *out = proc ? stringToNasal(c, proc->ident()) : naNil();
405 } else if (!strcmp(fieldName, "wp_parent")) {
406 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
407 *out = ghostForProcedure(c, proc);
408 } else if (!strcmp(fieldName, "fly_type")) {
409 if (wpt->type() == "hold") {
410 *out = stringToNasal(c, "Hold");
412 *out = stringToNasal(c, wpt->flag(WPT_OVERFLIGHT) ? "flyOver" : "flyBy");
415 return NULL; // member not found
421 static void waypointCommonSetMember(naContext c, Waypt* wpt, const char* fieldName, naRef value)
423 if (!strcmp(fieldName, "wp_role")) {
424 if (!naIsString(value)) naRuntimeError(c, "wp_role must be a string");
425 if (wpt->owner() != NULL) naRuntimeError(c, "cannot override wp_role on waypoint with parent");
426 WayptFlag f = wayptFlagFromString(naStr_data(value));
428 naRuntimeError(c, "unrecognized wp_role value %s", naStr_data(value));
431 wpt->setFlag(f, true);
435 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out)
437 const char* fieldName = naStr_data(field);
438 Waypt* wpt = (flightgear::Waypt*) g;
439 return waypointCommonGetMember(c, wpt, fieldName, out);
442 static RouteRestriction routeRestrictionFromString(const char* s)
446 if (u == "computed") return RESTRICT_COMPUTED;
447 if (u == "at") return RESTRICT_AT;
448 if (u == "mach") return SPEED_RESTRICT_MACH;
449 if (u == "computed-mach") return SPEED_COMPUTED_MACH;
450 if (u == "delete") return RESTRICT_DELETE;
451 return RESTRICT_NONE;
454 naRef routeRestrictionToNasal(naContext c, RouteRestriction rr)
457 case RESTRICT_NONE: return naNil();
458 case RESTRICT_AT: return stringToNasal(c, "at");
459 case RESTRICT_ABOVE: return stringToNasal(c, "above");
460 case RESTRICT_BELOW: return stringToNasal(c, "below");
461 case SPEED_RESTRICT_MACH: return stringToNasal(c, "mach");
462 case RESTRICT_COMPUTED: return stringToNasal(c, "computed");
463 case SPEED_COMPUTED_MACH: return stringToNasal(c, "computed-mach");
464 case RESTRICT_DELETE: return stringToNasal(c, "delete");
470 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out)
472 const char* fieldName = naStr_data(field);
473 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
474 Waypt* wpt = leg->waypoint();
476 if (!strcmp(fieldName, "parents")) {
477 *out = naNewVector(c);
478 naVec_append(*out, fpLegPrototype);
479 } else if (!strcmp(fieldName, "index")) {
480 *out = naNum(leg->index());
481 } else if (!strcmp(fieldName, "alt_cstr")) {
482 *out = naNum(leg->altitudeFt());
483 } else if (!strcmp(fieldName, "alt_cstr_type")) {
484 *out = routeRestrictionToNasal(c, leg->altitudeRestriction());
485 } else if (!strcmp(fieldName, "speed_cstr")) {
486 double s = isMachRestrict(leg->speedRestriction()) ? leg->speedMach() : leg->speedKts();
488 } else if (!strcmp(fieldName, "speed_cstr_type")) {
489 *out = routeRestrictionToNasal(c, leg->speedRestriction());
490 } else if (!strcmp(fieldName, "leg_distance")) {
491 *out = naNum(leg->distanceNm());
492 } else if (!strcmp(fieldName, "leg_bearing")) {
493 *out = naNum(leg->courseDeg());
494 } else if (!strcmp(fieldName, "distance_along_route")) {
495 *out = naNum(leg->distanceAlongRoute());
496 } else { // check for fields defined on the underlying waypoint
497 return waypointCommonGetMember(c, wpt, fieldName, out);
500 return ""; // success
503 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value)
505 const char* fieldName = naStr_data(field);
506 Waypt* wpt = (Waypt*) g;
507 waypointCommonSetMember(c, wpt, fieldName, value);
510 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value)
512 const char* fieldName = naStr_data(field);
513 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
515 waypointCommonSetMember(c, leg->waypoint(), fieldName, value);
518 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out)
520 const char* fieldName = naStr_data(field);
521 FlightPlan* fp = (FlightPlan*) g;
523 if (!strcmp(fieldName, "parents")) {
524 *out = naNewVector(c);
525 naVec_append(*out, flightplanPrototype);
526 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fp->ident());
527 else if (!strcmp(fieldName, "departure")) *out = ghostForAirport(c, fp->departureAirport());
528 else if (!strcmp(fieldName, "destination")) *out = ghostForAirport(c, fp->destinationAirport());
529 else if (!strcmp(fieldName, "departure_runway")) *out = ghostForRunway(c, fp->departureRunway());
530 else if (!strcmp(fieldName, "destination_runway")) *out = ghostForRunway(c, fp->destinationRunway());
531 else if (!strcmp(fieldName, "sid")) *out = ghostForProcedure(c, fp->sid());
532 else if (!strcmp(fieldName, "sid_trans")) *out = ghostForProcedure(c, fp->sidTransition());
533 else if (!strcmp(fieldName, "star")) *out = ghostForProcedure(c, fp->star());
534 else if (!strcmp(fieldName, "star_trans")) *out = ghostForProcedure(c, fp->starTransition());
535 else if (!strcmp(fieldName, "approach")) *out = ghostForProcedure(c, fp->approach());
536 else if (!strcmp(fieldName, "current")) *out = naNum(fp->currentIndex());
544 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value)
546 const char* fieldName = naStr_data(field);
547 FlightPlan* fp = (FlightPlan*) g;
549 if (!strcmp(fieldName, "id")) {
550 if (!naIsString(value)) naRuntimeError(c, "flightplan.id must be a string");
551 fp->setIdent(naStr_data(value));
552 } else if (!strcmp(fieldName, "current")) {
553 int index = value.num;
554 if ((index < 0) || (index >= fp->numLegs())) {
557 fp->setCurrentIndex(index);
558 } else if (!strcmp(fieldName, "departure")) {
559 FGAirport* apt = airportGhost(value);
561 fp->setDeparture(apt);
565 FGRunway* rwy = runwayGhost(value);
567 fp->setDeparture(rwy);
571 naRuntimeError(c, "bad argument type setting departure");
572 } else if (!strcmp(fieldName, "destination")) {
573 FGAirport* apt = airportGhost(value);
575 fp->setDestination(apt);
579 FGRunway* rwy = runwayGhost(value);
581 fp->setDestination(rwy);
585 naRuntimeError(c, "bad argument type setting destination");
586 } else if (!strcmp(fieldName, "departure_runway")) {
587 FGRunway* rwy = runwayGhost(value);
589 fp->setDeparture(rwy);
593 naRuntimeError(c, "bad argument type setting departure");
594 } else if (!strcmp(fieldName, "destination_runway")) {
595 FGRunway* rwy = runwayGhost(value);
597 fp->setDestination(rwy);
601 naRuntimeError(c, "bad argument type setting departure");
602 } else if (!strcmp(fieldName, "sid")) {
603 Procedure* proc = procedureGhost(value);
604 if (proc && (proc->type() == PROCEDURE_SID)) {
605 fp->setSID((flightgear::SID*) proc);
608 // allow a SID transition to be set, implicitly include the SID itself
609 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
610 fp->setSID((Transition*) proc);
614 if (naIsString(value)) {
615 FGAirport* apt = fp->departureAirport();
616 fp->setSID(apt->findSIDWithIdent(naStr_data(value)));
620 naRuntimeError(c, "bad argument type setting SID");
621 } else if (!strcmp(fieldName, "star")) {
622 Procedure* proc = procedureGhost(value);
623 if (proc && (proc->type() == PROCEDURE_STAR)) {
624 fp->setSTAR((STAR*) proc);
628 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
629 fp->setSTAR((Transition*) proc);
633 if (naIsString(value)) {
634 FGAirport* apt = fp->destinationAirport();
635 fp->setSTAR(apt->findSTARWithIdent(naStr_data(value)));
639 naRuntimeError(c, "bad argument type setting STAR");
640 } else if (!strcmp(fieldName, "approach")) {
641 Procedure* proc = procedureGhost(value);
642 if (proc && Approach::isApproach(proc->type())) {
643 fp->setApproach((Approach*) proc);
647 if (naIsString(value)) {
648 FGAirport* apt = fp->destinationAirport();
649 fp->setApproach(apt->findApproachWithIdent(naStr_data(value)));
653 naRuntimeError(c, "bad argument type setting approach");
658 static naRef procedureTpType(naContext c, ProcedureType ty)
661 case PROCEDURE_SID: return stringToNasal(c, "sid");
662 case PROCEDURE_STAR: return stringToNasal(c, "star");
663 case PROCEDURE_APPROACH_VOR:
664 case PROCEDURE_APPROACH_ILS:
665 case PROCEDURE_APPROACH_RNAV:
666 case PROCEDURE_APPROACH_NDB:
667 return stringToNasal(c, "IAP");
673 static naRef procedureRadioType(naContext c, ProcedureType ty)
676 case PROCEDURE_APPROACH_VOR: return stringToNasal(c, "VOR");
677 case PROCEDURE_APPROACH_ILS: return stringToNasal(c, "ILS");
678 case PROCEDURE_APPROACH_RNAV: return stringToNasal(c, "RNAV");
679 case PROCEDURE_APPROACH_NDB: return stringToNasal(c, "NDB");
685 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out)
687 const char* fieldName = naStr_data(field);
688 Procedure* proc = (Procedure*) g;
690 if (!strcmp(fieldName, "parents")) {
691 *out = naNewVector(c);
692 naVec_append(*out, procedurePrototype);
693 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, proc->ident());
694 else if (!strcmp(fieldName, "airport")) *out = ghostForAirport(c, proc->airport());
695 else if (!strcmp(fieldName, "tp_type")) *out = procedureTpType(c, proc->type());
696 else if (!strcmp(fieldName, "radio")) *out = procedureRadioType(c, proc->type());
697 else if (!strcmp(fieldName, "runways")) {
698 *out = naNewVector(c);
699 BOOST_FOREACH(FGRunwayRef rwy, proc->runways()) {
700 naVec_append(*out, stringToNasal(c, rwy->ident()));
702 } else if (!strcmp(fieldName, "transitions")) {
703 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
708 ArrivalDeparture* ad = static_cast<ArrivalDeparture*>(proc);
709 *out = naNewVector(c);
710 BOOST_FOREACH(string id, ad->transitionIdents()) {
711 naVec_append(*out, stringToNasal(c, id));
720 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
722 const char* fieldName = naStr_data(field);
723 FGRunwayBase* base = (FGRunwayBase*) g;
725 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, base->ident());
726 else if (!strcmp(fieldName, "lat")) *out = naNum(base->latitude());
727 else if (!strcmp(fieldName, "lon")) *out = naNum(base->longitude());
728 else if (!strcmp(fieldName, "heading")) *out = naNum(base->headingDeg());
729 else if (!strcmp(fieldName, "length")) *out = naNum(base->lengthM());
730 else if (!strcmp(fieldName, "width")) *out = naNum(base->widthM());
731 else if (!strcmp(fieldName, "surface")) *out = naNum(base->surface());
732 else if (base->type() == FGRunwayBase::RUNWAY) {
733 FGRunway* rwy = (FGRunway*) g;
734 if (!strcmp(fieldName, "threshold")) *out = naNum(rwy->displacedThresholdM());
735 else if (!strcmp(fieldName, "stopway")) *out = naNum(rwy->stopwayM());
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());
750 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out)
752 const char* fieldName = naStr_data(field);
753 FGNavRecord* nav = (FGNavRecord*) g;
755 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, nav->ident());
756 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, nav->name());
757 else if (!strcmp(fieldName, "lat")) *out = naNum(nav->get_lat());
758 else if (!strcmp(fieldName, "lon")) *out = naNum(nav->get_lon());
759 else if (!strcmp(fieldName, "elevation")) {
760 *out = naNum(nav->get_elev_ft() * SG_FEET_TO_METER);
761 } else if (!strcmp(fieldName, "type")) {
762 *out = stringToNasal(c, nav->nameForType(nav->type()));
763 } else if (!strcmp(fieldName, "frequency")) {
764 *out = naNum(nav->get_freq());
765 } else if (!strcmp(fieldName, "range_nm")) {
766 *out = naNum(nav->get_range());
767 } else if (!strcmp(fieldName, "course")) {
768 if ((nav->type() == FGPositioned::ILS) || (nav->type() == FGPositioned::LOC)) {
769 double radial = nav->get_multiuse();
770 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
771 *out = naNum(radial);
782 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out)
784 const char* fieldName = naStr_data(field);
785 FGFix* fix = (FGFix*) g;
787 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fix->ident());
788 else if (!strcmp(fieldName, "lat")) *out = naNum(fix->get_lat());
789 else if (!strcmp(fieldName, "lon")) *out = naNum(fix->get_lon());
797 static bool hashIsCoord(naRef h)
799 naRef parents = naHash_cget(h, (char*) "parents");
800 if (!naIsVector(parents)) {
804 return naEqual(naVec_get(parents, 0), geoCoordClass) != 0;
807 bool geodFromHash(naRef ref, SGGeod& result)
809 if (!naIsHash(ref)) {
814 // check for manual latitude / longitude names
815 naRef lat = naHash_cget(ref, (char*) "lat");
816 naRef lon = naHash_cget(ref, (char*) "lon");
817 if (naIsNum(lat) && naIsNum(lon)) {
818 result = SGGeod::fromDeg(naNumValue(lon).num, naNumValue(lat).num);
822 if (hashIsCoord(ref)) {
823 naRef lat = naHash_cget(ref, (char*) "_lat");
824 naRef lon = naHash_cget(ref, (char*) "_lon");
825 if (naIsNum(lat) && naIsNum(lon)) {
826 result = SGGeod::fromRad(naNumValue(lon).num, naNumValue(lat).num);
831 // check for any synonyms?
832 // latitude + longitude?
837 static int geodFromArgs(naRef* args, int offset, int argc, SGGeod& result)
839 if (offset >= argc) {
843 if (naIsGhost(args[offset])) {
844 naGhostType* gt = naGhost_type(args[offset]);
845 if (gt == &AirportGhostType) {
846 result = airportGhost(args[offset])->geod();
850 if (gt == &NavaidGhostType) {
851 result = navaidGhost(args[offset])->geod();
855 if (gt == &RunwayGhostType) {
856 result = runwayGhost(args[offset])->geod();
860 if (gt == &TaxiwayGhostType) {
861 result = taxiwayGhost(args[offset])->geod();
865 if (gt == &FixGhostType) {
866 result = fixGhost(args[offset])->geod();
870 if (gt == &WayptGhostType) {
871 result = wayptGhost(args[offset])->position();
876 if (geodFromHash(args[offset], result)) {
880 if (((argc - offset) >= 2) && naIsNum(args[offset]) && naIsNum(args[offset + 1])) {
881 double lat = naNumValue(args[0]).num,
882 lon = naNumValue(args[1]).num;
883 result = SGGeod::fromDeg(lon, lat);
890 // Convert a cartesian point to a geodetic lat/lon/altitude.
891 static naRef f_carttogeod(naContext c, naRef me, int argc, naRef* args)
893 double lat, lon, alt, xyz[3];
894 if(argc != 3) naRuntimeError(c, "carttogeod() expects 3 arguments");
895 for(int i=0; i<3; i++)
896 xyz[i] = naNumValue(args[i]).num;
897 sgCartToGeod(xyz, &lat, &lon, &alt);
898 lat *= SG_RADIANS_TO_DEGREES;
899 lon *= SG_RADIANS_TO_DEGREES;
900 naRef vec = naNewVector(c);
901 naVec_append(vec, naNum(lat));
902 naVec_append(vec, naNum(lon));
903 naVec_append(vec, naNum(alt));
907 // Convert a geodetic lat/lon/altitude to a cartesian point.
908 static naRef f_geodtocart(naContext c, naRef me, int argc, naRef* args)
910 if(argc != 3) naRuntimeError(c, "geodtocart() expects 3 arguments");
911 double lat = naNumValue(args[0]).num * SG_DEGREES_TO_RADIANS;
912 double lon = naNumValue(args[1]).num * SG_DEGREES_TO_RADIANS;
913 double alt = naNumValue(args[2]).num;
915 sgGeodToCart(lat, lon, alt, xyz);
916 naRef vec = naNewVector(c);
917 naVec_append(vec, naNum(xyz[0]));
918 naVec_append(vec, naNum(xyz[1]));
919 naVec_append(vec, naNum(xyz[2]));
923 // For given geodetic point return array with elevation, and a material data
924 // hash, or nil if there's no information available (tile not loaded). If
925 // information about the material isn't available, then nil is returned instead
927 static naRef f_geodinfo(naContext c, naRef me, int argc, naRef* args)
929 #define HASHSET(s,l,n) naHash_set(matdata, naStr_fromdata(naNewString(c),s,l),n)
930 if(argc < 2 || argc > 3)
931 naRuntimeError(c, "geodinfo() expects 2 or 3 arguments: lat, lon [, maxalt]");
932 double lat = naNumValue(args[0]).num;
933 double lon = naNumValue(args[1]).num;
934 double elev = argc == 3 ? naNumValue(args[2]).num : 10000;
935 const simgear::BVHMaterial *material;
936 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
937 if(!globals->get_scenery()->get_elevation_m(geod, elev, &material))
939 const SGMaterial *mat = dynamic_cast<const SGMaterial *>(material);
940 naRef vec = naNewVector(c);
941 naVec_append(vec, naNum(elev));
942 naRef matdata = naNil();
944 matdata = naNewHash(c);
945 naRef names = naNewVector(c);
946 BOOST_FOREACH(const std::string& n, mat->get_names())
947 naVec_append(names, stringToNasal(c, n));
949 HASHSET("names", 5, names);
950 HASHSET("solid", 5, naNum(mat->get_solid()));
951 HASHSET("friction_factor", 15, naNum(mat->get_friction_factor()));
952 HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction()));
953 HASHSET("load_resistance", 15, naNum(mat->get_load_resistance()));
954 HASHSET("bumpiness", 9, naNum(mat->get_bumpiness()));
955 HASHSET("light_coverage", 14, naNum(mat->get_light_coverage()));
957 naVec_append(vec, matdata);
963 class AirportInfoFilter : public FGAirport::AirportFilter
966 AirportInfoFilter() : type(FGPositioned::AIRPORT) {
967 minRunwayLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0);
970 bool fromArg(naRef arg)
972 const char *s = naStr_data(arg);
973 if(!strcmp(s, "airport")) type = FGPositioned::AIRPORT;
974 else if(!strcmp(s, "seaport")) type = FGPositioned::SEAPORT;
975 else if(!strcmp(s, "heliport")) type = FGPositioned::HELIPORT;
982 virtual FGPositioned::Type minType() const {
986 virtual FGPositioned::Type maxType() const {
990 virtual bool pass(FGPositioned* aPos) const
992 FGAirport* apt = (FGAirport*) aPos;
993 if ((apt->type() == FGPositioned::AIRPORT) &&
994 !apt->hasHardRunwayOfLengthFt(minRunwayLengthFt))
1002 FGPositioned::Type type;
1003 double minRunwayLengthFt;
1006 // Returns data hash for particular or nearest airport of a <type>, or nil
1009 // airportinfo(<id>); e.g. "KSFO"
1010 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
1011 // airportinfo() same as airportinfo("airport")
1012 // airportinfo(<lat>, <lon> [, <type>]);
1013 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
1015 SGGeod pos = globals->get_aircraft_position();
1016 FGAirport* apt = NULL;
1018 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1019 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1024 double maxRange = 10000.0; // expose this? or pick a smaller value?
1026 AirportInfoFilter filter; // defaults to airports only
1029 // fall through and use AIRPORT
1030 } else if(argc == 1 && naIsString(args[0])) {
1031 if (filter.fromArg(args[0])) {
1034 // user provided an <id>, hopefully
1035 apt = FGAirport::findByIdent(naStr_data(args[0]));
1037 // return nil here, but don't raise a runtime error; this is a
1038 // legitamate way to validate an ICAO code, for example in a
1039 // dialog box or similar.
1044 naRuntimeError(c, "airportinfo() with invalid function arguments");
1049 apt = FGAirport::findClosest(pos, maxRange, &filter);
1050 if(!apt) return naNil();
1053 return ghostForAirport(c, apt);
1056 static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
1059 SGGeod pos = globals->get_aircraft_position();
1060 argOffset += geodFromArgs(args, 0, argc, pos);
1062 if (!naIsNum(args[argOffset])) {
1063 naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset);
1066 AirportInfoFilter filter; // defaults to airports only
1067 double rangeNm = args[argOffset++].num;
1068 if (argOffset < argc) {
1069 filter.fromArg(args[argOffset++]);
1072 naRef r = naNewVector(c);
1074 FGPositioned::List apts = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1075 FGPositioned::sortByRange(apts, pos);
1077 BOOST_FOREACH(FGPositionedRef a, apts) {
1078 FGAirport* apt = (FGAirport*) a.get();
1079 naVec_append(r, ghostForAirport(c, apt));
1085 static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
1087 if (!naIsString(args[0])) {
1088 naRuntimeError(c, "findAirportsByICAO expects string as arg 0");
1092 string prefix(naStr_data(args[argOffset++]));
1093 AirportInfoFilter filter; // defaults to airports only
1094 if (argOffset < argc) {
1095 filter.fromArg(args[argOffset++]);
1098 naRef r = naNewVector(c);
1100 FGPositioned::List apts = FGPositioned::findAllWithIdent(prefix, &filter, false);
1102 BOOST_FOREACH(FGPositionedRef a, apts) {
1103 FGAirport* apt = (FGAirport*) a.get();
1104 naVec_append(r, ghostForAirport(c, apt));
1110 static naRef f_airport_tower(naContext c, naRef me, int argc, naRef* args)
1112 FGAirport* apt = airportGhost(me);
1114 naRuntimeError(c, "airport.tower called on non-airport object");
1117 // build a hash for the tower position
1118 SGGeod towerLoc = apt->getTowerLocation();
1119 naRef tower = naNewHash(c);
1120 hashset(c, tower, "lat", naNum(towerLoc.getLatitudeDeg()));
1121 hashset(c, tower, "lon", naNum(towerLoc.getLongitudeDeg()));
1122 hashset(c, tower, "elevation", naNum(towerLoc.getElevationM()));
1126 static naRef f_airport_comms(naContext c, naRef me, int argc, naRef* args)
1128 FGAirport* apt = airportGhost(me);
1130 naRuntimeError(c, "airport.comms called on non-airport object");
1132 naRef comms = naNewVector(c);
1134 // if we have an explicit type, return a simple vector of frequencies
1135 if (argc > 0 && naIsScalar(args[0])) {
1136 std::string commName = naStr_data(args[0]);
1137 FGPositioned::Type commType = FGPositioned::typeFromName(commName);
1139 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStationsOfType(commType)) {
1140 naVec_append(comms, naNum(comm->freqMHz()));
1143 // otherwise return a vector of hashes, one for each comm station.
1144 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStations()) {
1145 naRef commHash = naNewHash(c);
1146 hashset(c, commHash, "frequency", naNum(comm->freqMHz()));
1147 hashset(c, commHash, "ident", stringToNasal(c, comm->ident()));
1148 naVec_append(comms, commHash);
1155 static naRef f_airport_runway(naContext c, naRef me, int argc, naRef* args)
1157 FGAirport* apt = airportGhost(me);
1159 naRuntimeError(c, "airport.runway called on non-airport object");
1162 if ((argc < 1) || !naIsString(args[0])) {
1163 naRuntimeError(c, "airport.runway expects a runway ident argument");
1166 std::string ident(naStr_data(args[0]));
1167 boost::to_upper(ident);
1168 if (!apt->hasRunwayWithIdent(ident)) {
1172 return ghostForRunway(c, apt->getRunwayByIdent(ident));
1175 static naRef f_airport_taxiway(naContext c, naRef me, int argc, naRef* args)
1177 FGAirport* apt = airportGhost(me);
1179 naRuntimeError(c, "airport.taxiway called on non-airport object");
1182 if ((argc < 1) || !naIsString(args[0])) {
1183 naRuntimeError(c, "airport.taxiway expects a taxiway ident argument");
1186 naRef taxiways = naNewVector(c);
1188 for (unsigned int i = 0; i < apt->numTaxiways(); i++) {
1189 naVec_append(taxiways, ghostForTaxiway(c, apt->getTaxiwayByIndex(i)));
1195 static naRef f_airport_sids(naContext c, naRef me, int argc, naRef* args)
1197 FGAirport* apt = airportGhost(me);
1199 naRuntimeError(c, "airport.sids called on non-airport object");
1202 naRef sids = naNewVector(c);
1204 FGRunway* rwy = NULL;
1205 if (argc > 0 && naIsString(args[0])) {
1206 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1210 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1211 } else if (argc > 0) {
1212 rwy = runwayGhost(args[0]);
1216 BOOST_FOREACH(flightgear::SID* sid, rwy->getSIDs()) {
1217 naRef procId = stringToNasal(c, sid->ident());
1218 naVec_append(sids, procId);
1221 for (unsigned int s=0; s<apt->numSIDs(); ++s) {
1222 flightgear::SID* sid = apt->getSIDByIndex(s);
1223 naRef procId = stringToNasal(c, sid->ident());
1224 naVec_append(sids, procId);
1231 static naRef f_airport_stars(naContext c, naRef me, int argc, naRef* args)
1233 FGAirport* apt = airportGhost(me);
1235 naRuntimeError(c, "airport.stars called on non-airport object");
1238 naRef stars = naNewVector(c);
1240 FGRunway* rwy = NULL;
1241 if (argc > 0 && naIsString(args[0])) {
1242 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1246 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1247 } else if (argc > 0) {
1248 rwy = runwayGhost(args[0]);
1252 BOOST_FOREACH(flightgear::STAR* s, rwy->getSTARs()) {
1253 naRef procId = stringToNasal(c, s->ident());
1254 naVec_append(stars, procId);
1257 for (unsigned int s=0; s<apt->numSTARs(); ++s) {
1258 flightgear::STAR* star = apt->getSTARByIndex(s);
1259 naRef procId = stringToNasal(c, star->ident());
1260 naVec_append(stars, procId);
1267 static naRef f_airport_approaches(naContext c, naRef me, int argc, naRef* args)
1269 FGAirport* apt = airportGhost(me);
1271 naRuntimeError(c, "airport.getApproachList called on non-airport object");
1274 naRef approaches = naNewVector(c);
1276 ProcedureType ty = PROCEDURE_INVALID;
1277 if ((argc > 1) && naIsString(args[1])) {
1278 std::string u(naStr_data(args[1]));
1280 if (u == "NDB") ty = PROCEDURE_APPROACH_NDB;
1281 if (u == "VOR") ty = PROCEDURE_APPROACH_VOR;
1282 if (u == "ILS") ty = PROCEDURE_APPROACH_ILS;
1283 if (u == "RNAV") ty = PROCEDURE_APPROACH_RNAV;
1286 FGRunway* rwy = NULL;
1287 if (argc > 0 && (rwy = runwayGhost(args[0]))) {
1289 } else if (argc > 0 && naIsString(args[0])) {
1290 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1294 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1298 BOOST_FOREACH(Approach* s, rwy->getApproaches()) {
1299 if ((ty != PROCEDURE_INVALID) && (s->type() != ty)) {
1303 naRef procId = stringToNasal(c, s->ident());
1304 naVec_append(approaches, procId);
1307 // no runway specified, report them all
1308 for (unsigned int s=0; s<apt->numApproaches(); ++s) {
1309 Approach* app = apt->getApproachByIndex(s);
1310 if ((ty != PROCEDURE_INVALID) && (app->type() != ty)) {
1314 naRef procId = stringToNasal(c, app->ident());
1315 naVec_append(approaches, procId);
1322 static naRef f_airport_parking(naContext c, naRef me, int argc, naRef* args)
1324 FGAirport* apt = airportGhost(me);
1326 naRuntimeError(c, "airport.parking called on non-airport object");
1329 naRef r = naNewVector(c);
1331 bool onlyAvailable = false;
1333 if (argc > 0 && naIsString(args[0])) {
1334 type = naStr_data(args[0]);
1337 if ((argc > 1) && naIsNum(args[1])) {
1338 onlyAvailable = (args[1].num != 0.0);
1341 FGAirportDynamics* dynamics = apt->getDynamics();
1342 for (int i=0; i<dynamics->getNrOfParkings(); ++i) {
1343 FGParking* park = dynamics->getParking(i);
1344 // filter out based on availability and type
1345 if (onlyAvailable && !park->isAvailable()) {
1349 if (!type.empty() && (park->getType() != type)) {
1353 const SGGeod& parkLoc = park->geod();
1354 naRef ph = naNewHash(c);
1355 hashset(c, ph, "name", stringToNasal(c, park->getName()));
1356 hashset(c, ph, "lat", naNum(parkLoc.getLatitudeDeg()));
1357 hashset(c, ph, "lon", naNum(parkLoc.getLongitudeDeg()));
1358 hashset(c, ph, "elevation", naNum(parkLoc.getElevationM()));
1359 naVec_append(r, ph);
1365 static naRef f_airport_getSid(naContext c, naRef me, int argc, naRef* args)
1367 FGAirport* apt = airportGhost(me);
1369 naRuntimeError(c, "airport.getSid called on non-airport object");
1372 if ((argc != 1) || !naIsString(args[0])) {
1373 naRuntimeError(c, "airport.getSid passed invalid argument");
1376 string ident = naStr_data(args[0]);
1377 return ghostForProcedure(c, apt->findSIDWithIdent(ident));
1380 static naRef f_airport_getStar(naContext c, naRef me, int argc, naRef* args)
1382 FGAirport* apt = airportGhost(me);
1384 naRuntimeError(c, "airport.getStar called on non-airport object");
1387 if ((argc != 1) || !naIsString(args[0])) {
1388 naRuntimeError(c, "airport.getStar passed invalid argument");
1391 string ident = naStr_data(args[0]);
1392 return ghostForProcedure(c, apt->findSTARWithIdent(ident));
1395 static naRef f_airport_getApproach(naContext c, naRef me, int argc, naRef* args)
1397 FGAirport* apt = airportGhost(me);
1399 naRuntimeError(c, "airport.getIAP called on non-airport object");
1402 if ((argc != 1) || !naIsString(args[0])) {
1403 naRuntimeError(c, "airport.getIAP passed invalid argument");
1406 string ident = naStr_data(args[0]);
1407 return ghostForProcedure(c, apt->findApproachWithIdent(ident));
1410 static naRef f_airport_toString(naContext c, naRef me, int argc, naRef* args)
1412 FGAirport* apt = airportGhost(me);
1414 naRuntimeError(c, "airport.tostring called on non-airport object");
1417 return stringToNasal(c, "an airport " + apt->ident());
1420 // Returns vector of data hash for navaid of a <type>, nil on error
1421 // navaids sorted by ascending distance
1422 // navinfo([<lat>,<lon>],[<type>],[<id>])
1423 // lat/lon (numeric): use latitude/longitude instead of ac position
1424 // type: ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
1425 // id: (partial) id of the fix
1427 // navinfo("vor") returns all vors
1428 // navinfo("HAM") return all navaids who's name start with "HAM"
1429 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
1430 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
1431 // sorted by distance relative to lat=34, lon=48
1432 static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
1436 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1437 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1441 pos = globals->get_aircraft_position();
1444 FGPositioned::Type type = FGPositioned::INVALID;
1445 nav_list_type navlist;
1446 const char * id = "";
1448 if(argc > 0 && naIsString(args[0])) {
1449 const char *s = naStr_data(args[0]);
1450 if(!strcmp(s, "any")) type = FGPositioned::INVALID;
1451 else if(!strcmp(s, "fix")) type = FGPositioned::FIX;
1452 else if(!strcmp(s, "vor")) type = FGPositioned::VOR;
1453 else if(!strcmp(s, "ndb")) type = FGPositioned::NDB;
1454 else if(!strcmp(s, "ils")) type = FGPositioned::ILS;
1455 else if(!strcmp(s, "dme")) type = FGPositioned::DME;
1456 else if(!strcmp(s, "tacan")) type = FGPositioned::TACAN;
1457 else id = s; // this is an id
1462 if(argc > 0 && naIsString(args[0])) {
1464 naRuntimeError(c, "navinfo() called with navaid id");
1467 id = naStr_data(args[0]);
1473 naRuntimeError(c, "navinfo() called with too many arguments");
1477 FGNavList::TypeFilter filter(type);
1478 navlist = FGNavList::findByIdentAndFreq( pos, id, 0.0, &filter );
1480 naRef reply = naNewVector(c);
1481 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
1482 naVec_append( reply, ghostForNavaid(c, *it) );
1487 static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
1490 SGGeod pos = globals->get_aircraft_position();
1491 argOffset += geodFromArgs(args, 0, argc, pos);
1493 if (!naIsNum(args[argOffset])) {
1494 naRuntimeError(c, "findNavaidsWithinRange expected range (in nm) as arg %d", argOffset);
1497 FGPositioned::Type type = FGPositioned::INVALID;
1498 double rangeNm = args[argOffset++].num;
1499 if (argOffset < argc) {
1500 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1503 naRef r = naNewVector(c);
1504 FGNavList::TypeFilter filter(type);
1505 FGPositioned::List navs = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1506 FGPositioned::sortByRange(navs, pos);
1508 BOOST_FOREACH(FGPositionedRef a, navs) {
1509 FGNavRecord* nav = (FGNavRecord*) a.get();
1510 naVec_append(r, ghostForNavaid(c, nav));
1516 static naRef f_findNavaidByFrequency(naContext c, naRef me, int argc, naRef* args)
1519 SGGeod pos = globals->get_aircraft_position();
1520 argOffset += geodFromArgs(args, 0, argc, pos);
1522 if (!naIsNum(args[argOffset])) {
1523 naRuntimeError(c, "findNavaidByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1526 FGPositioned::Type type = FGPositioned::INVALID;
1527 double freqMhz = args[argOffset++].num;
1528 if (argOffset < argc) {
1529 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1532 FGNavList::TypeFilter filter(type);
1533 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1538 return ghostForNavaid(c, navs.front().ptr());
1541 static naRef f_findNavaidsByFrequency(naContext c, naRef me, int argc, naRef* args)
1544 SGGeod pos = globals->get_aircraft_position();
1545 argOffset += geodFromArgs(args, 0, argc, pos);
1547 if (!naIsNum(args[argOffset])) {
1548 naRuntimeError(c, "findNavaidsByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1551 FGPositioned::Type type = FGPositioned::INVALID;
1552 double freqMhz = args[argOffset++].num;
1553 if (argOffset < argc) {
1554 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1557 naRef r = naNewVector(c);
1559 FGNavList::TypeFilter filter(type);
1560 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1562 BOOST_FOREACH(nav_rec_ptr a, navs) {
1563 naVec_append(r, ghostForNavaid(c, a.ptr()));
1569 static naRef f_findNavaidsByIdent(naContext c, naRef me, int argc, naRef* args)
1572 SGGeod pos = globals->get_aircraft_position();
1573 argOffset += geodFromArgs(args, 0, argc, pos);
1575 if (!naIsString(args[argOffset])) {
1576 naRuntimeError(c, "findNavaidsByIdent expectes ident string as arg %d", argOffset);
1579 FGPositioned::Type type = FGPositioned::INVALID;
1580 string ident = naStr_data(args[argOffset++]);
1581 if (argOffset < argc) {
1582 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1585 FGNavList::TypeFilter filter(type);
1586 naRef r = naNewVector(c);
1587 nav_list_type navs = FGNavList::findByIdentAndFreq(pos, ident, 0.0, &filter);
1589 BOOST_FOREACH(nav_rec_ptr a, navs) {
1590 naVec_append(r, ghostForNavaid(c, a.ptr()));
1596 static naRef f_findFixesByIdent(naContext c, naRef me, int argc, naRef* args)
1599 SGGeod pos = globals->get_aircraft_position();
1600 argOffset += geodFromArgs(args, 0, argc, pos);
1602 if (!naIsString(args[argOffset])) {
1603 naRuntimeError(c, "findFixesByIdent expectes ident string as arg %d", argOffset);
1606 string ident(naStr_data(args[argOffset]));
1607 naRef r = naNewVector(c);
1609 FGPositioned::TypeFilter filter(FGPositioned::FIX);
1610 FGPositioned::List fixes = FGPositioned::findAllWithIdent(ident, &filter);
1611 FGPositioned::sortByRange(fixes, pos);
1613 BOOST_FOREACH(FGPositionedRef f, fixes) {
1614 naVec_append(r, ghostForFix(c, (FGFix*) f.ptr()));
1620 // Convert a cartesian point to a geodetic lat/lon/altitude.
1621 static naRef f_magvar(naContext c, naRef me, int argc, naRef* args)
1623 SGGeod pos = globals->get_aircraft_position();
1625 // fine, use aircraft position
1626 } else if (geodFromArgs(args, 0, argc, pos)) {
1629 naRuntimeError(c, "magvar() expects no arguments, a positioned hash or lat,lon pair");
1632 double jd = globals->get_time_params()->getJD();
1633 double magvarDeg = sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
1634 return naNum(magvarDeg);
1637 static naRef f_courseAndDistance(naContext c, naRef me, int argc, naRef* args)
1639 SGGeod from = globals->get_aircraft_position(), to, p;
1640 int argOffset = geodFromArgs(args, 0, argc, p);
1641 if (geodFromArgs(args, argOffset, argc, to)) {
1642 from = p; // we parsed both FROM and TO args, so first was from
1644 to = p; // only parsed one arg, so FROM is current
1647 if (argOffset == 0) {
1648 naRuntimeError(c, "invalid arguments to courseAndDistance");
1651 double course, course2, d;
1652 SGGeodesy::inverse(from, to, course, course2, d);
1654 naRef result = naNewVector(c);
1655 naVec_append(result, naNum(course));
1656 naVec_append(result, naNum(d * SG_METER_TO_NM));
1660 static naRef f_greatCircleMove(naContext c, naRef me, int argc, naRef* args)
1662 SGGeod from = globals->get_aircraft_position(), to;
1665 // complication - don't inerpret two doubles (as the only args)
1666 // as a lat,lon pair - only do so if we have at least three args.
1668 argOffset = geodFromArgs(args, 0, argc, from);
1671 if ((argOffset + 1) >= argc) {
1672 naRuntimeError(c, "isufficent arguments to greatCircleMove");
1675 if (!naIsNum(args[argOffset]) || !naIsNum(args[argOffset+1])) {
1676 naRuntimeError(c, "invalid arguments %d and %d to greatCircleMove",
1677 argOffset, argOffset + 1);
1680 double course = args[argOffset].num, course2;
1681 double distanceNm = args[argOffset + 1].num;
1682 SGGeodesy::direct(from, course, distanceNm * SG_NM_TO_METER, to, course2);
1685 naRef coord = naNewHash(c);
1686 hashset(c, coord, "lat", naNum(to.getLatitudeDeg()));
1687 hashset(c, coord, "lon", naNum(to.getLongitudeDeg()));
1691 static naRef f_tilePath(naContext c, naRef me, int argc, naRef* args)
1693 SGGeod pos = globals->get_aircraft_position();
1694 geodFromArgs(args, 0, argc, pos);
1696 return stringToNasal(c, b.gen_base_path());
1699 static naRef f_tileIndex(naContext c, naRef me, int argc, naRef* args)
1701 SGGeod pos = globals->get_aircraft_position();
1702 geodFromArgs(args, 0, argc, pos);
1704 return naNum(b.gen_index());
1707 static naRef f_route(naContext c, naRef me, int argc, naRef* args)
1710 FGRouteMgr* rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
1711 return ghostForFlightPlan(c, rm->flightPlan());
1714 if ((argc > 0) && naIsString(args[0])) {
1715 flightgear::FlightPlan* fp = new flightgear::FlightPlan;
1716 SGPath path(naStr_data(args[0]));
1717 if (!path.exists()) {
1718 naRuntimeError(c, "flightplan, no file at path %s", path.c_str());
1721 if (!fp->load(path)) {
1722 SG_LOG(SG_NASAL, SG_WARN, "failed to load flight-plan from " << path);
1727 return ghostForFlightPlan(c, fp);
1730 naRuntimeError(c, "bad arguments to flightplan()");
1734 class NasalFPDelegate : public FlightPlan::Delegate
1737 NasalFPDelegate(FlightPlan* fp, FGNasalSys* sys, naRef ins) :
1742 SG_LOG(SG_NASAL, SG_INFO, "created Nasal delegate for " << fp);
1743 _gcSaveKey = _nasal->gcSave(ins);
1746 virtual ~NasalFPDelegate()
1748 SG_LOG(SG_NASAL, SG_INFO, "destroying Nasal delegate for " << _plan);
1749 _nasal->gcRelease(_gcSaveKey);
1752 virtual void departureChanged()
1754 callDelegateMethod("departureChanged");
1757 virtual void arrivalChanged()
1759 callDelegateMethod("arrivalChanged");
1762 virtual void waypointsChanged()
1764 callDelegateMethod("waypointsChanged");
1767 virtual void currentWaypointChanged()
1769 callDelegateMethod("currentWaypointChanged");
1773 void callDelegateMethod(const char* method)
1776 naMember_cget(_nasal->context(), _instance, method, &f);
1778 return; // no method on the delegate
1782 arg[0] = ghostForFlightPlan(_nasal->context(), _plan);
1783 _nasal->callMethod(f, _instance, 1, arg, naNil());
1792 class NasalFPDelegateFactory : public FlightPlan::DelegateFactory
1795 NasalFPDelegateFactory(naRef code)
1797 _nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1799 _gcSaveKey = _nasal->gcSave(_func);
1802 ~NasalFPDelegateFactory()
1804 _nasal->gcRelease(_gcSaveKey);
1807 virtual FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp)
1810 args[0] = ghostForFlightPlan(_nasal->context(), fp);
1811 naRef instance = _nasal->call(_func, 1, args, naNil());
1812 if (naIsNil(instance)) {
1816 return new NasalFPDelegate(fp, _nasal, instance);
1824 static naRef f_registerFPDelegate(naContext c, naRef me, int argc, naRef* args)
1826 if ((argc < 1) || !naIsFunc(args[0])) {
1827 naRuntimeError(c, "non-function argument to registerFlightPlanDelegate");
1830 NasalFPDelegateFactory* factory = new NasalFPDelegateFactory(args[0]);
1831 FlightPlan::registerDelegateFactory(factory);
1836 static WayptRef wayptFromArg(naRef arg)
1838 WayptRef r = wayptGhost(arg);
1843 FGPositioned* pos = positionedGhost(arg);
1845 // let's check if the arg is hash, coudl extra a geod and hence build
1846 // a simple waypoint
1851 // special-case for runways
1852 if (pos->type() == FGPositioned::RUNWAY) {
1853 return new RunwayWaypt((FGRunway*) pos, NULL);
1856 return new NavaidWaypoint(pos, NULL);
1859 static naRef convertWayptVecToNasal(naContext c, const WayptVec& wps)
1861 naRef result = naNewVector(c);
1862 BOOST_FOREACH(WayptRef wpt, wps) {
1863 naVec_append(result, ghostForWaypt(c, wpt.get()));
1868 static naRef f_airwaySearch(naContext c, naRef me, int argc, naRef* args)
1871 naRuntimeError(c, "airwaysSearch needs at least two arguments");
1874 WayptRef start = wayptFromArg(args[0]),
1875 end = wayptFromArg(args[1]);
1877 if (!start || !end) {
1878 SG_LOG(SG_NASAL, SG_WARN, "airwaysSearch: start or end points are invalid");
1882 bool highLevel = true;
1883 if ((argc > 2) && naIsString(args[2])) {
1884 if (!strcmp(naStr_data(args[2]), "lowlevel")) {
1891 Airway::highLevel()->route(start, end, route);
1893 Airway::lowLevel()->route(start, end, route);
1896 return convertWayptVecToNasal(c, route);
1899 static naRef f_createWP(naContext c, naRef me, int argc, naRef* args)
1902 int argOffset = geodFromArgs(args, 0, argc, pos);
1904 if (((argc - argOffset) < 1) || !naIsString(args[argOffset])) {
1905 naRuntimeError(c, "createWP: no identifier supplied");
1908 string ident = naStr_data(args[argOffset++]);
1909 WayptRef wpt = new BasicWaypt(pos, ident, NULL);
1911 // set waypt flags - approach, departure, pseudo, etc
1912 if (argc > argOffset) {
1913 WayptFlag f = wayptFlagFromString(naStr_data(args[argOffset++]));
1917 return ghostForWaypt(c, wpt);
1920 static naRef f_createWPFrom(naContext c, naRef me, int argc, naRef* args)
1923 naRuntimeError(c, "createWPFrom: need at least one argument");
1926 FGPositioned* positioned = positionedGhost(args[0]);
1928 naRuntimeError(c, "createWPFrom: couldn;t convert arg[0] to FGPositioned");
1932 if (positioned->type() == FGPositioned::RUNWAY) {
1933 wpt = new RunwayWaypt((FGRunway*) positioned, NULL);
1935 wpt = new NavaidWaypoint(positioned, NULL);
1938 // set waypt flags - approach, departure, pseudo, etc
1940 WayptFlag f = wayptFlagFromString(naStr_data(args[1]));
1944 return ghostForWaypt(c, wpt);
1947 static naRef f_flightplan_getWP(naContext c, naRef me, int argc, naRef* args)
1949 FlightPlan* fp = flightplanGhost(me);
1951 naRuntimeError(c, "flightplan.getWP called on non-flightplan object");
1956 index = fp->currentIndex();
1958 index = (int) naNumValue(args[0]).num;
1961 if ((index < 0) || (index >= fp->numLegs())) {
1965 return ghostForLeg(c, fp->legAtIndex(index));
1968 static naRef f_flightplan_currentWP(naContext c, naRef me, int argc, naRef* args)
1970 FlightPlan* fp = flightplanGhost(me);
1972 naRuntimeError(c, "flightplan.currentWP called on non-flightplan object");
1974 return ghostForLeg(c, fp->currentLeg());
1977 static naRef f_flightplan_nextWP(naContext c, naRef me, int argc, naRef* args)
1979 FlightPlan* fp = flightplanGhost(me);
1981 naRuntimeError(c, "flightplan.nextWP called on non-flightplan object");
1983 return ghostForLeg(c, fp->nextLeg());
1986 static naRef f_flightplan_numWaypoints(naContext c, naRef me, int argc, naRef* args)
1988 FlightPlan* fp = flightplanGhost(me);
1990 naRuntimeError(c, "flightplan.numWaypoints called on non-flightplan object");
1992 return naNum(fp->numLegs());
1995 static naRef f_flightplan_appendWP(naContext c, naRef me, int argc, naRef* args)
1997 FlightPlan* fp = flightplanGhost(me);
1999 naRuntimeError(c, "flightplan.appendWP called on non-flightplan object");
2002 WayptRef wp = wayptGhost(args[0]);
2003 int index = fp->numLegs();
2004 fp->insertWayptAtIndex(wp.get(), index);
2005 return naNum(index);
2008 static naRef f_flightplan_insertWP(naContext c, naRef me, int argc, naRef* args)
2010 FlightPlan* fp = flightplanGhost(me);
2012 naRuntimeError(c, "flightplan.insertWP called on non-flightplan object");
2015 WayptRef wp = wayptGhost(args[0]);
2016 int index = -1; // append
2017 if ((argc > 1) && naIsNum(args[1])) {
2018 index = (int) args[1].num;
2021 fp->insertWayptAtIndex(wp.get(), index);
2025 static naRef f_flightplan_insertWPAfter(naContext c, naRef me, int argc, naRef* args)
2027 FlightPlan* fp = flightplanGhost(me);
2029 naRuntimeError(c, "flightplan.insertWPAfter called on non-flightplan object");
2032 WayptRef wp = wayptGhost(args[0]);
2033 int index = -1; // append
2034 if ((argc > 1) && naIsNum(args[1])) {
2035 index = (int) args[1].num;
2038 fp->insertWayptAtIndex(wp.get(), index + 1);
2042 static naRef f_flightplan_insertWaypoints(naContext c, naRef me, int argc, naRef* args)
2044 FlightPlan* fp = flightplanGhost(me);
2046 naRuntimeError(c, "flightplan.insertWaypoints called on non-flightplan object");
2050 if (!naIsVector(args[0])) {
2051 naRuntimeError(c, "flightplan.insertWaypoints expects vector as first arg");
2054 int count = naVec_size(args[0]);
2055 for (int i=0; i<count; ++i) {
2056 Waypt* wp = wayptGhost(naVec_get(args[0], i));
2062 int index = -1; // append
2063 if ((argc > 1) && naIsNum(args[1])) {
2064 index = (int) args[1].num;
2067 fp->insertWayptsAtIndex(wps, index);
2071 static naRef f_flightplan_deleteWP(naContext c, naRef me, int argc, naRef* args)
2073 FlightPlan* fp = flightplanGhost(me);
2075 naRuntimeError(c, "flightplan.deleteWP called on non-flightplan object");
2078 if ((argc < 1) || !naIsNum(args[0])) {
2079 naRuntimeError(c, "bad argument to flightplan.deleteWP");
2082 int index = (int) args[0].num;
2083 fp->deleteIndex(index);
2087 static naRef f_flightplan_clearPlan(naContext c, naRef me, int argc, naRef* args)
2089 FlightPlan* fp = flightplanGhost(me);
2091 naRuntimeError(c, "flightplan.clearPlan called on non-flightplan object");
2098 static naRef f_flightplan_clearWPType(naContext c, naRef me, int argc, naRef* args)
2100 FlightPlan* fp = flightplanGhost(me);
2102 naRuntimeError(c, "flightplan.clearWPType called on non-flightplan object");
2106 naRuntimeError(c, "insufficent args to flightplan.clearWPType");
2109 WayptFlag flag = wayptFlagFromString(naStr_data(args[0]));
2110 fp->clearWayptsWithFlag(flag);
2114 static naRef f_flightplan_clone(naContext c, naRef me, int argc, naRef* args)
2116 FlightPlan* fp = flightplanGhost(me);
2118 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2121 return ghostForFlightPlan(c, fp->clone());
2124 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
2126 FlightPlan::Leg* leg = fpLegGhost(me);
2128 naRuntimeError(c, "leg.setSpeed called on non-flightplan-leg object");
2132 naRuntimeError(c, "bad arguments to leg.setSpeed");
2135 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2136 leg->setSpeed(rr, args[0].num);
2140 static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
2142 FlightPlan::Leg* leg = fpLegGhost(me);
2144 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2148 naRuntimeError(c, "bad arguments to leg.setAltitude");
2151 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2152 leg->setAltitude(rr, args[0].num);
2156 static naRef f_leg_path(naContext c, naRef me, int argc, naRef* args)
2158 FlightPlan::Leg* leg = fpLegGhost(me);
2160 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2163 RoutePath path(leg->owner());
2164 SGGeodVec gv(path.pathForIndex(leg->index()));
2166 naRef result = naNewVector(c);
2167 BOOST_FOREACH(SGGeod p, gv) {
2168 // construct a geo.Coord!
2169 naRef coord = naNewHash(c);
2170 hashset(c, coord, "lat", naNum(p.getLatitudeDeg()));
2171 hashset(c, coord, "lon", naNum(p.getLongitudeDeg()));
2172 naVec_append(result, coord);
2178 static naRef f_waypoint_navaid(naContext c, naRef me, int argc, naRef* args)
2180 flightgear::Waypt* w = wayptGhost(me);
2182 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2185 FGPositioned* pos = w->source();
2190 switch (pos->type()) {
2191 case FGPositioned::VOR:
2192 case FGPositioned::NDB:
2193 case FGPositioned::ILS:
2194 case FGPositioned::LOC:
2195 case FGPositioned::GS:
2196 case FGPositioned::DME:
2197 case FGPositioned::TACAN: {
2198 FGNavRecord* nav = (FGNavRecord*) pos;
2199 return ghostForNavaid(c, nav);
2207 static naRef f_waypoint_airport(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();
2215 if (!pos || FGAirport::isAirportType(pos)) {
2219 return ghostForAirport(c, (FGAirport*) pos);
2222 static naRef f_waypoint_runway(naContext c, naRef me, int argc, naRef* args)
2224 flightgear::Waypt* w = wayptGhost(me);
2226 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2229 FGPositioned* pos = w->source();
2230 if (!pos || (pos->type() != FGPositioned::RUNWAY)) {
2234 return ghostForRunway(c, (FGRunway*) pos);
2237 static naRef f_procedure_transition(naContext c, naRef me, int argc, naRef* args)
2239 Procedure* proc = procedureGhost(me);
2241 naRuntimeError(c, "procedure.transition called on non-procedure object");
2244 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2245 naRuntimeError(c, "procedure.transition called on non-SID or -STAR");
2248 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2249 Transition* trans = ad->findTransitionByName(naStr_data(args[0]));
2251 return ghostForProcedure(c, trans);
2254 static naRef f_procedure_route(naContext c, naRef me, int argc, naRef* args)
2256 Procedure* proc = procedureGhost(me);
2258 naRuntimeError(c, "procedure.route called on non-procedure object");
2261 // wrapping up tow different routines here - approach routing from the IAF
2262 // to the associated runway, and SID/STAR routing via an enroute transition
2263 // and possibly a runway transition or not.
2264 if (Approach::isApproach(proc->type())) {
2267 iaf = wayptFromArg(args[0]);
2271 Approach* app = (Approach*) proc;
2272 if (!app->route(iaf, r)) {
2273 SG_LOG(SG_NASAL, SG_WARN, "procedure.route failed for Approach somehow");
2277 return convertWayptVecToNasal(c, r);
2278 } else if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2279 naRuntimeError(c, "procedure.route called on unsuitable procedure type");
2283 FGRunway* rwy = runwayGhost(args[0]);
2284 if (rwy) ++argOffset;
2286 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2287 Transition* trans = NULL;
2288 if (argOffset < argc) {
2289 trans = (Transition*) procedureGhost(args[argOffset]);
2292 // note either runway or trans may be NULL - that's ok
2294 if (!ad->route(rwy, trans, r)) {
2295 SG_LOG(SG_NASAL, SG_WARN, "prcoedure.route failed for ArrvialDeparture somehow");
2299 return convertWayptVecToNasal(c, r);
2303 // Table of extension functions. Terminate with zeros.
2304 static struct { const char* name; naCFunction func; } funcs[] = {
2305 { "carttogeod", f_carttogeod },
2306 { "geodtocart", f_geodtocart },
2307 { "geodinfo", f_geodinfo },
2308 { "airportinfo", f_airportinfo },
2309 { "findAirportsWithinRange", f_findAirportsWithinRange },
2310 { "findAirportsByICAO", f_findAirportsByICAO },
2311 { "navinfo", f_navinfo },
2312 { "findNavaidsWithinRange", f_findNavaidsWithinRange },
2313 { "findNavaidByFrequency", f_findNavaidByFrequency },
2314 { "findNavaidsByFrequency", f_findNavaidsByFrequency },
2315 { "findNavaidsByID", f_findNavaidsByIdent },
2316 { "findFixesByID", f_findFixesByIdent },
2317 { "flightplan", f_route },
2318 { "registerFlightPlanDelegate", f_registerFPDelegate },
2319 { "createWP", f_createWP },
2320 { "createWPFrom", f_createWPFrom },
2321 { "airwaysRoute", f_airwaySearch },
2322 { "magvar", f_magvar },
2323 { "courseAndDistance", f_courseAndDistance },
2324 { "greatCircleMove", f_greatCircleMove },
2325 { "tileIndex", f_tileIndex },
2326 { "tilePath", f_tilePath },
2331 naRef initNasalPositioned(naRef globals, naContext c, naRef gcSave)
2333 airportPrototype = naNewHash(c);
2334 hashset(c, gcSave, "airportProto", airportPrototype);
2336 hashset(c, airportPrototype, "runway", naNewFunc(c, naNewCCode(c, f_airport_runway)));
2337 hashset(c, airportPrototype, "taxiway", naNewFunc(c, naNewCCode(c, f_airport_taxiway)));
2338 hashset(c, airportPrototype, "tower", naNewFunc(c, naNewCCode(c, f_airport_tower)));
2339 hashset(c, airportPrototype, "comms", naNewFunc(c, naNewCCode(c, f_airport_comms)));
2340 hashset(c, airportPrototype, "sids", naNewFunc(c, naNewCCode(c, f_airport_sids)));
2341 hashset(c, airportPrototype, "stars", naNewFunc(c, naNewCCode(c, f_airport_stars)));
2342 hashset(c, airportPrototype, "getApproachList", naNewFunc(c, naNewCCode(c, f_airport_approaches)));
2343 hashset(c, airportPrototype, "parking", naNewFunc(c, naNewCCode(c, f_airport_parking)));
2344 hashset(c, airportPrototype, "getSid", naNewFunc(c, naNewCCode(c, f_airport_getSid)));
2345 hashset(c, airportPrototype, "getStar", naNewFunc(c, naNewCCode(c, f_airport_getStar)));
2346 hashset(c, airportPrototype, "getIAP", naNewFunc(c, naNewCCode(c, f_airport_getApproach)));
2347 hashset(c, airportPrototype, "tostring", naNewFunc(c, naNewCCode(c, f_airport_toString)));
2349 flightplanPrototype = naNewHash(c);
2350 hashset(c, gcSave, "flightplanProto", flightplanPrototype);
2352 hashset(c, flightplanPrototype, "getWP", naNewFunc(c, naNewCCode(c, f_flightplan_getWP)));
2353 hashset(c, flightplanPrototype, "currentWP", naNewFunc(c, naNewCCode(c, f_flightplan_currentWP)));
2354 hashset(c, flightplanPrototype, "nextWP", naNewFunc(c, naNewCCode(c, f_flightplan_nextWP)));
2355 hashset(c, flightplanPrototype, "getPlanSize", naNewFunc(c, naNewCCode(c, f_flightplan_numWaypoints)));
2356 hashset(c, flightplanPrototype, "appendWP", naNewFunc(c, naNewCCode(c, f_flightplan_appendWP)));
2357 hashset(c, flightplanPrototype, "insertWP", naNewFunc(c, naNewCCode(c, f_flightplan_insertWP)));
2358 hashset(c, flightplanPrototype, "deleteWP", naNewFunc(c, naNewCCode(c, f_flightplan_deleteWP)));
2359 hashset(c, flightplanPrototype, "insertWPAfter", naNewFunc(c, naNewCCode(c, f_flightplan_insertWPAfter)));
2360 hashset(c, flightplanPrototype, "insertWaypoints", naNewFunc(c, naNewCCode(c, f_flightplan_insertWaypoints)));
2361 hashset(c, flightplanPrototype, "cleanPlan", naNewFunc(c, naNewCCode(c, f_flightplan_clearPlan)));
2362 hashset(c, flightplanPrototype, "clearWPType", naNewFunc(c, naNewCCode(c, f_flightplan_clearWPType)));
2363 hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone)));
2365 waypointPrototype = naNewHash(c);
2366 hashset(c, gcSave, "wayptProto", waypointPrototype);
2368 hashset(c, waypointPrototype, "navaid", naNewFunc(c, naNewCCode(c, f_waypoint_navaid)));
2369 hashset(c, waypointPrototype, "runway", naNewFunc(c, naNewCCode(c, f_waypoint_runway)));
2370 hashset(c, waypointPrototype, "airport", naNewFunc(c, naNewCCode(c, f_waypoint_airport)));
2372 procedurePrototype = naNewHash(c);
2373 hashset(c, gcSave, "procedureProto", procedurePrototype);
2374 hashset(c, procedurePrototype, "transition", naNewFunc(c, naNewCCode(c, f_procedure_transition)));
2375 hashset(c, procedurePrototype, "route", naNewFunc(c, naNewCCode(c, f_procedure_route)));
2377 fpLegPrototype = naNewHash(c);
2378 hashset(c, gcSave, "fpLegProto", fpLegPrototype);
2379 hashset(c, fpLegPrototype, "setSpeed", naNewFunc(c, naNewCCode(c, f_leg_setSpeed)));
2380 hashset(c, fpLegPrototype, "setAltitude", naNewFunc(c, naNewCCode(c, f_leg_setAltitude)));
2381 hashset(c, fpLegPrototype, "path", naNewFunc(c, naNewCCode(c, f_leg_path)));
2383 for(int i=0; funcs[i].name; i++) {
2384 hashset(c, globals, funcs[i].name,
2385 naNewFunc(c, naNewCCode(c, funcs[i].func)));
2391 void postinitNasalPositioned(naRef globals, naContext c)
2393 naRef geoModule = naHash_cget(globals, (char*) "geo");
2394 if (naIsNil(geoModule)) {
2395 SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalPositioned: geo.nas not loaded");
2399 geoCoordClass = naHash_cget(geoModule, (char*) "Coord");