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>
31 #include <boost/tuple/tuple.hpp> // for boost::tie
33 #include <simgear/sg_inlines.h>
34 #include <simgear/scene/material/mat.hxx>
35 #include <simgear/magvar/magvar.hxx>
36 #include <simgear/timing/sg_time.hxx>
37 #include <simgear/bucket/newbucket.hxx>
39 #include <Airports/runways.hxx>
40 #include <Airports/airport.hxx>
41 #include <Airports/dynamics.hxx>
42 #include <Airports/parking.hxx>
43 #include <Scripting/NasalSys.hxx>
44 #include <Navaids/navlist.hxx>
45 #include <Navaids/procedure.hxx>
46 #include <Main/globals.hxx>
47 #include <Main/fg_props.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <ATC/CommStation.hxx>
50 #include <Navaids/FlightPlan.hxx>
51 #include <Navaids/waypoint.hxx>
52 #include <Navaids/fix.hxx>
53 #include <Autopilot/route_mgr.hxx>
54 #include <Navaids/routePath.hxx>
55 #include <Navaids/procedure.hxx>
56 #include <Navaids/airways.hxx>
57 #include <Navaids/NavDataCache.hxx>
59 using namespace flightgear;
61 static void positionedGhostDestroy(void* g);
62 static void wayptGhostDestroy(void* g);
63 static void legGhostDestroy(void* g);
64 static void routeBaseGhostDestroy(void* g);
66 naGhostType PositionedGhostType = { positionedGhostDestroy, "positioned" };
68 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out);
69 naGhostType AirportGhostType = { positionedGhostDestroy, "airport", airportGhostGetMember, 0 };
71 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out);
72 naGhostType NavaidGhostType = { positionedGhostDestroy, "navaid", navaidGhostGetMember, 0 };
74 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out);
75 naGhostType RunwayGhostType = { positionedGhostDestroy, "runway", runwayGhostGetMember, 0 };
76 naGhostType HelipadGhostType = { positionedGhostDestroy, "helipad", runwayGhostGetMember, 0 };
77 naGhostType TaxiwayGhostType = { positionedGhostDestroy, "taxiway", runwayGhostGetMember, 0 };
79 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out);
80 naGhostType FixGhostType = { positionedGhostDestroy, "fix", fixGhostGetMember, 0 };
82 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out);
83 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value);
85 naGhostType WayptGhostType = { wayptGhostDestroy,
88 waypointGhostSetMember};
90 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out);
91 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value);
93 naGhostType FPLegGhostType = { legGhostDestroy,
98 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out);
99 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value);
101 naGhostType FlightPlanGhostType = { routeBaseGhostDestroy,
103 flightplanGhostGetMember,
104 flightplanGhostSetMember
107 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out);
108 naGhostType ProcedureGhostType = { routeBaseGhostDestroy,
110 procedureGhostGetMember,
113 static void hashset(naContext c, naRef hash, const char* key, naRef val)
115 naRef s = naNewString(c);
116 naStr_fromdata(s, (char*)key, strlen(key));
117 naHash_set(hash, s, val);
120 static naRef stringToNasal(naContext c, const std::string& s)
122 return naStr_fromdata(naNewString(c),
123 const_cast<char *>(s.c_str()),
127 static WayptFlag wayptFlagFromString(const char* s)
129 if (!strcmp(s, "sid")) return WPT_DEPARTURE;
130 if (!strcmp(s, "star")) return WPT_ARRIVAL;
131 if (!strcmp(s, "approach")) return WPT_APPROACH;
132 if (!strcmp(s, "missed")) return WPT_MISS;
133 if (!strcmp(s, "pseudo")) return WPT_PSEUDO;
135 return (WayptFlag) 0;
138 static naRef wayptFlagToNasal(naContext c, unsigned int flags)
140 if (flags & WPT_PSEUDO) return stringToNasal(c, "pseudo");
141 if (flags & WPT_DEPARTURE) return stringToNasal(c, "sid");
142 if (flags & WPT_ARRIVAL) return stringToNasal(c, "star");
143 if (flags & WPT_MISS) return stringToNasal(c, "missed");
144 if (flags & WPT_APPROACH) return stringToNasal(c, "approach");
148 static FGPositioned* positionedGhost(naRef r)
150 if ((naGhost_type(r) == &AirportGhostType) ||
151 (naGhost_type(r) == &NavaidGhostType) ||
152 (naGhost_type(r) == &RunwayGhostType) ||
153 (naGhost_type(r) == &FixGhostType))
155 return (FGPositioned*) naGhost_ptr(r);
161 static FGAirport* airportGhost(naRef r)
163 if (naGhost_type(r) == &AirportGhostType)
164 return (FGAirport*) naGhost_ptr(r);
168 static FGNavRecord* navaidGhost(naRef r)
170 if (naGhost_type(r) == &NavaidGhostType)
171 return (FGNavRecord*) naGhost_ptr(r);
175 static FGRunway* runwayGhost(naRef r)
177 if (naGhost_type(r) == &RunwayGhostType)
178 return (FGRunway*) naGhost_ptr(r);
182 static FGTaxiway* taxiwayGhost(naRef r)
184 if (naGhost_type(r) == &TaxiwayGhostType)
185 return (FGTaxiway*) naGhost_ptr(r);
189 static FGFix* fixGhost(naRef r)
191 if (naGhost_type(r) == &FixGhostType)
192 return (FGFix*) naGhost_ptr(r);
197 static void positionedGhostDestroy(void* g)
199 FGPositioned* pos = (FGPositioned*)g;
200 if (!FGPositioned::put(pos)) // unref
204 static Waypt* wayptGhost(naRef r)
206 if (naGhost_type(r) == &WayptGhostType)
207 return (Waypt*) naGhost_ptr(r);
209 if (naGhost_type(r) == &FPLegGhostType) {
210 FlightPlan::Leg* leg = (FlightPlan::Leg*) naGhost_ptr(r);
211 return leg->waypoint();
217 static void wayptGhostDestroy(void* g)
219 Waypt* wpt = (Waypt*)g;
220 if (!Waypt::put(wpt)) // unref
224 static void legGhostDestroy(void* g)
230 static FlightPlan::Leg* fpLegGhost(naRef r)
232 if (naGhost_type(r) == &FPLegGhostType)
233 return (FlightPlan::Leg*) naGhost_ptr(r);
237 static Procedure* procedureGhost(naRef r)
239 if (naGhost_type(r) == &ProcedureGhostType)
240 return (Procedure*) naGhost_ptr(r);
244 static FlightPlan* flightplanGhost(naRef r)
246 if (naGhost_type(r) == &FlightPlanGhostType)
247 return (FlightPlan*) naGhost_ptr(r);
251 static void routeBaseGhostDestroy(void* g)
256 static naRef airportPrototype;
257 static naRef flightplanPrototype;
258 static naRef waypointPrototype;
259 static naRef geoCoordClass;
260 static naRef fpLegPrototype;
261 static naRef procedurePrototype;
263 naRef ghostForAirport(naContext c, const FGAirport* apt)
269 FGPositioned::get(apt); // take a ref
270 return naNewGhost2(c, &AirportGhostType, (void*) apt);
273 naRef ghostForNavaid(naContext c, const FGNavRecord* n)
279 FGPositioned::get(n); // take a ref
280 return naNewGhost2(c, &NavaidGhostType, (void*) n);
283 naRef ghostForRunway(naContext c, const FGRunway* r)
289 FGPositioned::get(r); // take a ref
290 return naNewGhost2(c, &RunwayGhostType, (void*) r);
293 naRef ghostForHelipad(naContext c, const FGHelipad* r)
299 FGPositioned::get(r); // take a ref
300 return naNewGhost2(c, &HelipadGhostType, (void*) r);
303 naRef ghostForTaxiway(naContext c, const FGTaxiway* r)
309 FGPositioned::get(r); // take a ref
310 return naNewGhost2(c, &TaxiwayGhostType, (void*) r);
313 naRef ghostForFix(naContext c, const FGFix* r)
319 FGPositioned::get(r); // take a ref
320 return naNewGhost2(c, &FixGhostType, (void*) r);
324 naRef ghostForWaypt(naContext c, const Waypt* wpt)
330 Waypt::get(wpt); // take a ref
331 return naNewGhost2(c, &WayptGhostType, (void*) wpt);
334 naRef ghostForLeg(naContext c, const FlightPlan::Leg* leg)
340 return naNewGhost2(c, &FPLegGhostType, (void*) leg);
343 naRef ghostForFlightPlan(naContext c, const FlightPlan* fp)
349 return naNewGhost2(c, &FlightPlanGhostType, (void*) fp);
352 naRef ghostForProcedure(naContext c, const Procedure* proc)
358 return naNewGhost2(c, &ProcedureGhostType, (void*) proc);
361 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out)
363 const char* fieldName = naStr_data(field);
364 FGAirport* apt = (FGAirport*) g;
366 if (!strcmp(fieldName, "parents")) {
367 *out = naNewVector(c);
368 naVec_append(*out, airportPrototype);
369 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, apt->ident());
370 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, apt->name());
371 else if (!strcmp(fieldName, "lat")) *out = naNum(apt->getLatitude());
372 else if (!strcmp(fieldName, "lon")) *out = naNum(apt->getLongitude());
373 else if (!strcmp(fieldName, "elevation")) {
374 *out = naNum(apt->getElevation() * SG_FEET_TO_METER);
375 } else if (!strcmp(fieldName, "has_metar")) {
376 *out = naNum(apt->getMetar());
377 } else if (!strcmp(fieldName, "runways")) {
379 double minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft");
380 for(unsigned int r=0; r<apt->numRunways(); ++r) {
381 FGRunway* rwy(apt->getRunwayByIndex(r));
382 // ignore unusably short runways
383 if (rwy->lengthFt() < minLengthFt) {
386 naRef rwyid = stringToNasal(c, rwy->ident());
387 naRef rwydata = ghostForRunway(c, rwy);
388 naHash_set(*out, rwyid, rwydata);
390 } else if (!strcmp(fieldName, "helipads")) {
393 for(unsigned int r=0; r<apt->numHelipads(); ++r) {
394 FGHelipad* hp(apt->getHelipadByIndex(r));
396 naRef rwyid = stringToNasal(c, hp->ident());
397 naRef rwydata = ghostForHelipad(c, hp);
398 naHash_set(*out, rwyid, rwydata);
401 } else if (!strcmp(fieldName, "taxiways")) {
402 *out = naNewVector(c);
403 for(unsigned int r=0; r<apt->numTaxiways(); ++r) {
404 FGTaxiway* taxi(apt->getTaxiwayByIndex(r));
405 naRef taxidata = ghostForTaxiway(c, taxi);
406 naVec_append(*out, taxidata);
416 static const char* waypointCommonGetMember(naContext c, Waypt* wpt, const char* fieldName, naRef* out)
418 if (!strcmp(fieldName, "wp_name") || !strcmp(fieldName, "id")) *out = stringToNasal(c, wpt->ident());
419 else if (!strcmp(fieldName, "wp_type")) *out = stringToNasal(c, wpt->type());
420 else if (!strcmp(fieldName, "wp_role")) *out = wayptFlagToNasal(c, wpt->flags());
421 else if (!strcmp(fieldName, "wp_lat") || !strcmp(fieldName, "lat")) *out = naNum(wpt->position().getLatitudeDeg());
422 else if (!strcmp(fieldName, "wp_lon") || !strcmp(fieldName, "lon")) *out = naNum(wpt->position().getLongitudeDeg());
423 else if (!strcmp(fieldName, "wp_parent_name")) {
424 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
425 *out = proc ? stringToNasal(c, proc->ident()) : naNil();
426 } else if (!strcmp(fieldName, "wp_parent")) {
427 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
428 *out = ghostForProcedure(c, proc);
429 } else if (!strcmp(fieldName, "fly_type")) {
430 if (wpt->type() == "hold") {
431 *out = stringToNasal(c, "Hold");
433 *out = stringToNasal(c, wpt->flag(WPT_OVERFLIGHT) ? "flyOver" : "flyBy");
436 return NULL; // member not found
442 static void waypointCommonSetMember(naContext c, Waypt* wpt, const char* fieldName, naRef value)
444 if (!strcmp(fieldName, "wp_role")) {
445 if (!naIsString(value)) naRuntimeError(c, "wp_role must be a string");
446 if (wpt->owner() != NULL) naRuntimeError(c, "cannot override wp_role on waypoint with parent");
447 WayptFlag f = wayptFlagFromString(naStr_data(value));
449 naRuntimeError(c, "unrecognized wp_role value %s", naStr_data(value));
452 wpt->setFlag(f, true);
456 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out)
458 const char* fieldName = naStr_data(field);
459 Waypt* wpt = (flightgear::Waypt*) g;
460 return waypointCommonGetMember(c, wpt, fieldName, out);
463 static RouteRestriction routeRestrictionFromString(const char* s)
467 if (u == "computed") return RESTRICT_COMPUTED;
468 if (u == "at") return RESTRICT_AT;
469 if (u == "mach") return SPEED_RESTRICT_MACH;
470 if (u == "computed-mach") return SPEED_COMPUTED_MACH;
471 if (u == "delete") return RESTRICT_DELETE;
472 return RESTRICT_NONE;
475 naRef routeRestrictionToNasal(naContext c, RouteRestriction rr)
478 case RESTRICT_NONE: return naNil();
479 case RESTRICT_AT: return stringToNasal(c, "at");
480 case RESTRICT_ABOVE: return stringToNasal(c, "above");
481 case RESTRICT_BELOW: return stringToNasal(c, "below");
482 case SPEED_RESTRICT_MACH: return stringToNasal(c, "mach");
483 case RESTRICT_COMPUTED: return stringToNasal(c, "computed");
484 case SPEED_COMPUTED_MACH: return stringToNasal(c, "computed-mach");
485 case RESTRICT_DELETE: return stringToNasal(c, "delete");
491 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out)
493 const char* fieldName = naStr_data(field);
494 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
495 Waypt* wpt = leg->waypoint();
497 if (!strcmp(fieldName, "parents")) {
498 *out = naNewVector(c);
499 naVec_append(*out, fpLegPrototype);
500 naVec_append(*out, waypointPrototype);
501 } else if (!strcmp(fieldName, "index")) {
502 *out = naNum(leg->index());
503 } else if (!strcmp(fieldName, "alt_cstr")) {
504 *out = naNum(leg->altitudeFt());
505 } else if (!strcmp(fieldName, "alt_cstr_type")) {
506 *out = routeRestrictionToNasal(c, leg->altitudeRestriction());
507 } else if (!strcmp(fieldName, "speed_cstr")) {
508 double s = isMachRestrict(leg->speedRestriction()) ? leg->speedMach() : leg->speedKts();
510 } else if (!strcmp(fieldName, "speed_cstr_type")) {
511 *out = routeRestrictionToNasal(c, leg->speedRestriction());
512 } else if (!strcmp(fieldName, "leg_distance")) {
513 *out = naNum(leg->distanceNm());
514 } else if (!strcmp(fieldName, "leg_bearing")) {
515 *out = naNum(leg->courseDeg());
516 } else if (!strcmp(fieldName, "distance_along_route")) {
517 *out = naNum(leg->distanceAlongRoute());
518 } else { // check for fields defined on the underlying waypoint
519 return waypointCommonGetMember(c, wpt, fieldName, out);
522 return ""; // success
525 static void waypointGhostSetMember(naContext c, void* g, naRef field, naRef value)
527 const char* fieldName = naStr_data(field);
528 Waypt* wpt = (Waypt*) g;
529 waypointCommonSetMember(c, wpt, fieldName, value);
532 static void legGhostSetMember(naContext c, void* g, naRef field, naRef value)
534 const char* fieldName = naStr_data(field);
535 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
537 waypointCommonSetMember(c, leg->waypoint(), fieldName, value);
540 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out)
542 const char* fieldName = naStr_data(field);
543 FlightPlan* fp = (FlightPlan*) g;
545 if (!strcmp(fieldName, "parents")) {
546 *out = naNewVector(c);
547 naVec_append(*out, flightplanPrototype);
548 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fp->ident());
549 else if (!strcmp(fieldName, "departure")) *out = ghostForAirport(c, fp->departureAirport());
550 else if (!strcmp(fieldName, "destination")) *out = ghostForAirport(c, fp->destinationAirport());
551 else if (!strcmp(fieldName, "departure_runway")) *out = ghostForRunway(c, fp->departureRunway());
552 else if (!strcmp(fieldName, "destination_runway")) *out = ghostForRunway(c, fp->destinationRunway());
553 else if (!strcmp(fieldName, "sid")) *out = ghostForProcedure(c, fp->sid());
554 else if (!strcmp(fieldName, "sid_trans")) *out = ghostForProcedure(c, fp->sidTransition());
555 else if (!strcmp(fieldName, "star")) *out = ghostForProcedure(c, fp->star());
556 else if (!strcmp(fieldName, "star_trans")) *out = ghostForProcedure(c, fp->starTransition());
557 else if (!strcmp(fieldName, "approach")) *out = ghostForProcedure(c, fp->approach());
558 else if (!strcmp(fieldName, "current")) *out = naNum(fp->currentIndex());
566 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value)
568 const char* fieldName = naStr_data(field);
569 FlightPlan* fp = (FlightPlan*) g;
571 if (!strcmp(fieldName, "id")) {
572 if (!naIsString(value)) naRuntimeError(c, "flightplan.id must be a string");
573 fp->setIdent(naStr_data(value));
574 } else if (!strcmp(fieldName, "current")) {
575 int index = value.num;
576 if ((index < 0) || (index >= fp->numLegs())) {
579 fp->setCurrentIndex(index);
580 } else if (!strcmp(fieldName, "departure")) {
581 FGAirport* apt = airportGhost(value);
583 fp->setDeparture(apt);
587 FGRunway* rwy = runwayGhost(value);
589 fp->setDeparture(rwy);
593 if (naIsNil(value)) {
594 fp->setDeparture(static_cast<FGAirport*>(NULL));
598 naRuntimeError(c, "bad argument type setting departure");
599 } else if (!strcmp(fieldName, "destination")) {
600 FGAirport* apt = airportGhost(value);
602 fp->setDestination(apt);
606 FGRunway* rwy = runwayGhost(value);
608 fp->setDestination(rwy);
612 naRuntimeError(c, "bad argument type setting destination");
613 } else if (!strcmp(fieldName, "departure_runway")) {
614 FGRunway* rwy = runwayGhost(value);
616 fp->setDeparture(rwy);
620 naRuntimeError(c, "bad argument type setting departure runway");
621 } else if (!strcmp(fieldName, "destination_runway")) {
622 FGRunway* rwy = runwayGhost(value);
624 fp->setDestination(rwy);
628 naRuntimeError(c, "bad argument type setting destination runway");
629 } else if (!strcmp(fieldName, "sid")) {
630 Procedure* proc = procedureGhost(value);
631 if (proc && (proc->type() == PROCEDURE_SID)) {
632 fp->setSID((flightgear::SID*) proc);
635 // allow a SID transition to be set, implicitly include the SID itself
636 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
637 fp->setSID((Transition*) proc);
641 if (naIsString(value)) {
642 FGAirport* apt = fp->departureAirport();
643 fp->setSID(apt->findSIDWithIdent(naStr_data(value)));
647 naRuntimeError(c, "bad argument type setting SID");
648 } else if (!strcmp(fieldName, "star")) {
649 Procedure* proc = procedureGhost(value);
650 if (proc && (proc->type() == PROCEDURE_STAR)) {
651 fp->setSTAR((STAR*) proc);
655 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
656 fp->setSTAR((Transition*) proc);
660 if (naIsString(value)) {
661 FGAirport* apt = fp->destinationAirport();
662 fp->setSTAR(apt->findSTARWithIdent(naStr_data(value)));
666 naRuntimeError(c, "bad argument type setting STAR");
667 } else if (!strcmp(fieldName, "approach")) {
668 Procedure* proc = procedureGhost(value);
669 if (proc && Approach::isApproach(proc->type())) {
670 fp->setApproach((Approach*) proc);
674 if (naIsString(value)) {
675 FGAirport* apt = fp->destinationAirport();
676 fp->setApproach(apt->findApproachWithIdent(naStr_data(value)));
680 naRuntimeError(c, "bad argument type setting approach");
685 static naRef procedureTpType(naContext c, ProcedureType ty)
688 case PROCEDURE_SID: return stringToNasal(c, "sid");
689 case PROCEDURE_STAR: return stringToNasal(c, "star");
690 case PROCEDURE_APPROACH_VOR:
691 case PROCEDURE_APPROACH_ILS:
692 case PROCEDURE_APPROACH_RNAV:
693 case PROCEDURE_APPROACH_NDB:
694 return stringToNasal(c, "IAP");
700 static naRef procedureRadioType(naContext c, ProcedureType ty)
703 case PROCEDURE_APPROACH_VOR: return stringToNasal(c, "VOR");
704 case PROCEDURE_APPROACH_ILS: return stringToNasal(c, "ILS");
705 case PROCEDURE_APPROACH_RNAV: return stringToNasal(c, "RNAV");
706 case PROCEDURE_APPROACH_NDB: return stringToNasal(c, "NDB");
712 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out)
714 const char* fieldName = naStr_data(field);
715 Procedure* proc = (Procedure*) g;
717 if (!strcmp(fieldName, "parents")) {
718 *out = naNewVector(c);
719 naVec_append(*out, procedurePrototype);
720 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, proc->ident());
721 else if (!strcmp(fieldName, "airport")) *out = ghostForAirport(c, proc->airport());
722 else if (!strcmp(fieldName, "tp_type")) *out = procedureTpType(c, proc->type());
723 else if (!strcmp(fieldName, "radio")) *out = procedureRadioType(c, proc->type());
724 else if (!strcmp(fieldName, "runways")) {
725 *out = naNewVector(c);
726 BOOST_FOREACH(FGRunwayRef rwy, proc->runways()) {
727 naVec_append(*out, stringToNasal(c, rwy->ident()));
729 } else if (!strcmp(fieldName, "transitions")) {
730 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
735 ArrivalDeparture* ad = static_cast<ArrivalDeparture*>(proc);
736 *out = naNewVector(c);
737 BOOST_FOREACH(std::string id, ad->transitionIdents()) {
738 naVec_append(*out, stringToNasal(c, id));
747 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
749 const char* fieldName = naStr_data(field);
750 FGRunwayBase* base = (FGRunwayBase*) g;
752 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, base->ident());
753 else if (!strcmp(fieldName, "lat")) *out = naNum(base->latitude());
754 else if (!strcmp(fieldName, "lon")) *out = naNum(base->longitude());
755 else if (!strcmp(fieldName, "heading")) *out = naNum(base->headingDeg());
756 else if (!strcmp(fieldName, "length")) *out = naNum(base->lengthM());
757 else if (!strcmp(fieldName, "width")) *out = naNum(base->widthM());
758 else if (!strcmp(fieldName, "surface")) *out = naNum(base->surface());
759 else if (base->type() == FGRunwayBase::RUNWAY) {
760 FGRunway* rwy = (FGRunway*) g;
761 if (!strcmp(fieldName, "threshold")) *out = naNum(rwy->displacedThresholdM());
762 else if (!strcmp(fieldName, "stopway")) *out = naNum(rwy->stopwayM());
763 else if (!strcmp(fieldName, "reciprocal")) {
764 *out = ghostForRunway(c, rwy->reciprocalRunway());
765 } else if (!strcmp(fieldName, "ils_frequency_mhz")) {
766 *out = rwy->ILS() ? naNum(rwy->ILS()->get_freq() / 100.0) : naNil();
767 } else if (!strcmp(fieldName, "ils")) {
768 *out = ghostForNavaid(c, rwy->ILS());
779 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out)
781 const char* fieldName = naStr_data(field);
782 FGNavRecord* nav = (FGNavRecord*) g;
784 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, nav->ident());
785 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, nav->name());
786 else if (!strcmp(fieldName, "lat")) *out = naNum(nav->get_lat());
787 else if (!strcmp(fieldName, "lon")) *out = naNum(nav->get_lon());
788 else if (!strcmp(fieldName, "elevation")) {
789 *out = naNum(nav->get_elev_ft() * SG_FEET_TO_METER);
790 } else if (!strcmp(fieldName, "type")) {
791 *out = stringToNasal(c, nav->nameForType(nav->type()));
792 } else if (!strcmp(fieldName, "frequency")) {
793 *out = naNum(nav->get_freq());
794 } else if (!strcmp(fieldName, "range_nm")) {
795 *out = naNum(nav->get_range());
796 } else if (!strcmp(fieldName, "course")) {
797 if ((nav->type() == FGPositioned::ILS) || (nav->type() == FGPositioned::LOC)) {
798 double radial = nav->get_multiuse();
799 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
800 *out = naNum(radial);
811 static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out)
813 const char* fieldName = naStr_data(field);
814 FGFix* fix = (FGFix*) g;
816 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fix->ident());
817 else if (!strcmp(fieldName, "lat")) *out = naNum(fix->get_lat());
818 else if (!strcmp(fieldName, "lon")) *out = naNum(fix->get_lon());
826 static bool hashIsCoord(naRef h)
828 naRef parents = naHash_cget(h, (char*) "parents");
829 if (!naIsVector(parents)) {
833 return naEqual(naVec_get(parents, 0), geoCoordClass) != 0;
836 bool geodFromHash(naRef ref, SGGeod& result)
838 if (!naIsHash(ref)) {
843 // check for manual latitude / longitude names
844 naRef lat = naHash_cget(ref, (char*) "lat");
845 naRef lon = naHash_cget(ref, (char*) "lon");
846 if (naIsNum(lat) && naIsNum(lon)) {
847 result = SGGeod::fromDeg(naNumValue(lon).num, naNumValue(lat).num);
851 if (hashIsCoord(ref)) {
852 naRef lat = naHash_cget(ref, (char*) "_lat");
853 naRef lon = naHash_cget(ref, (char*) "_lon");
854 if (naIsNum(lat) && naIsNum(lon)) {
855 result = SGGeod::fromRad(naNumValue(lon).num, naNumValue(lat).num);
860 // check for any synonyms?
861 // latitude + longitude?
866 static int geodFromArgs(naRef* args, int offset, int argc, SGGeod& result)
868 if (offset >= argc) {
872 if (naIsGhost(args[offset])) {
873 naGhostType* gt = naGhost_type(args[offset]);
874 if (gt == &AirportGhostType) {
875 result = airportGhost(args[offset])->geod();
879 if (gt == &NavaidGhostType) {
880 result = navaidGhost(args[offset])->geod();
884 if (gt == &RunwayGhostType) {
885 result = runwayGhost(args[offset])->geod();
889 if (gt == &TaxiwayGhostType) {
890 result = taxiwayGhost(args[offset])->geod();
894 if (gt == &FixGhostType) {
895 result = fixGhost(args[offset])->geod();
899 if (gt == &WayptGhostType) {
900 result = wayptGhost(args[offset])->position();
905 if (geodFromHash(args[offset], result)) {
909 if (((argc - offset) >= 2) && naIsNum(args[offset]) && naIsNum(args[offset + 1])) {
910 double lat = naNumValue(args[0]).num,
911 lon = naNumValue(args[1]).num;
912 result = SGGeod::fromDeg(lon, lat);
919 // Convert a cartesian point to a geodetic lat/lon/altitude.
920 static naRef f_carttogeod(naContext c, naRef me, int argc, naRef* args)
922 double lat, lon, alt, xyz[3];
923 if(argc != 3) naRuntimeError(c, "carttogeod() expects 3 arguments");
924 for(int i=0; i<3; i++)
925 xyz[i] = naNumValue(args[i]).num;
926 sgCartToGeod(xyz, &lat, &lon, &alt);
927 lat *= SG_RADIANS_TO_DEGREES;
928 lon *= SG_RADIANS_TO_DEGREES;
929 naRef vec = naNewVector(c);
930 naVec_append(vec, naNum(lat));
931 naVec_append(vec, naNum(lon));
932 naVec_append(vec, naNum(alt));
936 // Convert a geodetic lat/lon/altitude to a cartesian point.
937 static naRef f_geodtocart(naContext c, naRef me, int argc, naRef* args)
939 if(argc != 3) naRuntimeError(c, "geodtocart() expects 3 arguments");
940 double lat = naNumValue(args[0]).num * SG_DEGREES_TO_RADIANS;
941 double lon = naNumValue(args[1]).num * SG_DEGREES_TO_RADIANS;
942 double alt = naNumValue(args[2]).num;
944 sgGeodToCart(lat, lon, alt, xyz);
945 naRef vec = naNewVector(c);
946 naVec_append(vec, naNum(xyz[0]));
947 naVec_append(vec, naNum(xyz[1]));
948 naVec_append(vec, naNum(xyz[2]));
952 // For given geodetic point return array with elevation, and a material data
953 // hash, or nil if there's no information available (tile not loaded). If
954 // information about the material isn't available, then nil is returned instead
956 static naRef f_geodinfo(naContext c, naRef me, int argc, naRef* args)
958 #define HASHSET(s,l,n) naHash_set(matdata, naStr_fromdata(naNewString(c),s,l),n)
959 if(argc < 2 || argc > 3)
960 naRuntimeError(c, "geodinfo() expects 2 or 3 arguments: lat, lon [, maxalt]");
961 double lat = naNumValue(args[0]).num;
962 double lon = naNumValue(args[1]).num;
963 double elev = argc == 3 ? naNumValue(args[2]).num : 10000;
964 const simgear::BVHMaterial *material;
965 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
966 if(!globals->get_scenery()->get_elevation_m(geod, elev, &material))
968 const SGMaterial *mat = dynamic_cast<const SGMaterial *>(material);
969 naRef vec = naNewVector(c);
970 naVec_append(vec, naNum(elev));
971 naRef matdata = naNil();
973 matdata = naNewHash(c);
974 naRef names = naNewVector(c);
975 BOOST_FOREACH(const std::string& n, mat->get_names())
976 naVec_append(names, stringToNasal(c, n));
978 HASHSET("names", 5, names);
979 HASHSET("solid", 5, naNum(mat->get_solid()));
980 HASHSET("friction_factor", 15, naNum(mat->get_friction_factor()));
981 HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction()));
982 HASHSET("load_resistance", 15, naNum(mat->get_load_resistance()));
983 HASHSET("bumpiness", 9, naNum(mat->get_bumpiness()));
984 HASHSET("light_coverage", 14, naNum(mat->get_light_coverage()));
986 naVec_append(vec, matdata);
992 // Returns data hash for particular or nearest airport of a <type>, or nil
995 // airportinfo(<id>); e.g. "KSFO"
996 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
997 // airportinfo() same as airportinfo("airport")
998 // airportinfo(<lat>, <lon> [, <type>]);
999 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
1001 SGGeod pos = globals->get_aircraft_position();
1002 FGAirport* apt = NULL;
1004 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1005 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1010 double maxRange = 10000.0; // expose this? or pick a smaller value?
1012 FGAirport::TypeRunwayFilter filter; // defaults to airports only
1015 // fall through and use AIRPORT
1016 } else if(argc == 1 && naIsString(args[0])) {
1017 if (filter.fromTypeString(naStr_data(args[0]))) {
1020 // user provided an <id>, hopefully
1021 apt = FGAirport::findByIdent(naStr_data(args[0]));
1023 // return nil here, but don't raise a runtime error; this is a
1024 // legitamate way to validate an ICAO code, for example in a
1025 // dialog box or similar.
1030 naRuntimeError(c, "airportinfo() with invalid function arguments");
1035 apt = FGAirport::findClosest(pos, maxRange, &filter);
1036 if(!apt) return naNil();
1039 return ghostForAirport(c, apt);
1042 static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
1045 SGGeod pos = globals->get_aircraft_position();
1046 argOffset += geodFromArgs(args, 0, argc, pos);
1048 if (!naIsNum(args[argOffset])) {
1049 naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset);
1052 FGAirport::TypeRunwayFilter filter; // defaults to airports only
1053 double rangeNm = args[argOffset++].num;
1054 if (argOffset < argc) {
1055 filter.fromTypeString(naStr_data(args[argOffset++]));
1058 naRef r = naNewVector(c);
1060 FGPositionedList apts = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1061 FGPositioned::sortByRange(apts, pos);
1063 BOOST_FOREACH(FGPositionedRef a, apts) {
1064 FGAirport* apt = (FGAirport*) a.get();
1065 naVec_append(r, ghostForAirport(c, apt));
1071 static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
1073 if (!naIsString(args[0])) {
1074 naRuntimeError(c, "findAirportsByICAO expects string as arg 0");
1078 std::string prefix(naStr_data(args[argOffset++]));
1079 FGAirport::TypeRunwayFilter filter; // defaults to airports only
1080 if (argOffset < argc) {
1081 filter.fromTypeString(naStr_data(args[argOffset++]));
1084 naRef r = naNewVector(c);
1086 FGPositionedList apts = FGPositioned::findAllWithIdent(prefix, &filter, false);
1088 BOOST_FOREACH(FGPositionedRef a, apts) {
1089 FGAirport* apt = (FGAirport*) a.get();
1090 naVec_append(r, ghostForAirport(c, apt));
1096 static naRef f_airport_tower(naContext c, naRef me, int argc, naRef* args)
1098 FGAirport* apt = airportGhost(me);
1100 naRuntimeError(c, "airport.tower called on non-airport object");
1103 // build a hash for the tower position
1104 SGGeod towerLoc = apt->getTowerLocation();
1105 naRef tower = naNewHash(c);
1106 hashset(c, tower, "lat", naNum(towerLoc.getLatitudeDeg()));
1107 hashset(c, tower, "lon", naNum(towerLoc.getLongitudeDeg()));
1108 hashset(c, tower, "elevation", naNum(towerLoc.getElevationM()));
1112 static naRef f_airport_comms(naContext c, naRef me, int argc, naRef* args)
1114 FGAirport* apt = airportGhost(me);
1116 naRuntimeError(c, "airport.comms called on non-airport object");
1118 naRef comms = naNewVector(c);
1120 // if we have an explicit type, return a simple vector of frequencies
1121 if (argc > 0 && naIsScalar(args[0])) {
1122 std::string commName = naStr_data(args[0]);
1123 FGPositioned::Type commType = FGPositioned::typeFromName(commName);
1125 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStationsOfType(commType)) {
1126 naVec_append(comms, naNum(comm->freqMHz()));
1129 // otherwise return a vector of hashes, one for each comm station.
1130 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStations()) {
1131 naRef commHash = naNewHash(c);
1132 hashset(c, commHash, "frequency", naNum(comm->freqMHz()));
1133 hashset(c, commHash, "ident", stringToNasal(c, comm->ident()));
1134 naVec_append(comms, commHash);
1141 static naRef f_airport_runway(naContext c, naRef me, int argc, naRef* args)
1143 FGAirport* apt = airportGhost(me);
1145 naRuntimeError(c, "airport.runway called on non-airport object");
1148 if ((argc < 1) || !naIsString(args[0])) {
1149 naRuntimeError(c, "airport.runway expects a runway ident argument");
1152 std::string ident(naStr_data(args[0]));
1153 boost::to_upper(ident);
1155 if (apt->hasRunwayWithIdent(ident)) {
1156 return ghostForRunway(c, apt->getRunwayByIdent(ident));
1157 } else if (apt->hasHelipadWithIdent(ident)) {
1158 return ghostForHelipad(c, apt->getHelipadByIdent(ident));
1163 static naRef f_airport_runwaysWithoutReciprocals(naContext c, naRef me, int argc, naRef* args)
1165 FGAirport* apt = airportGhost(me);
1167 naRuntimeError(c, "airport.runwaysWithoutReciprocals called on non-airport object");
1170 FGRunwayList rwylist(apt->getRunwaysWithoutReciprocals());
1171 naRef runways = naNewVector(c);
1172 for (unsigned int r=0; r<rwylist.size(); ++r) {
1173 FGRunway* rwy(rwylist[r]);
1174 naVec_append(runways, ghostForRunway(c, apt->getRunwayByIdent(rwy->ident())));
1179 static naRef f_airport_sids(naContext c, naRef me, int argc, naRef* args)
1181 FGAirport* apt = airportGhost(me);
1183 naRuntimeError(c, "airport.sids called on non-airport object");
1186 naRef sids = naNewVector(c);
1188 FGRunway* rwy = NULL;
1189 if (argc > 0 && naIsString(args[0])) {
1190 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1194 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1195 } else if (argc > 0) {
1196 rwy = runwayGhost(args[0]);
1200 BOOST_FOREACH(flightgear::SID* sid, rwy->getSIDs()) {
1201 naRef procId = stringToNasal(c, sid->ident());
1202 naVec_append(sids, procId);
1205 for (unsigned int s=0; s<apt->numSIDs(); ++s) {
1206 flightgear::SID* sid = apt->getSIDByIndex(s);
1207 naRef procId = stringToNasal(c, sid->ident());
1208 naVec_append(sids, procId);
1215 static naRef f_airport_stars(naContext c, naRef me, int argc, naRef* args)
1217 FGAirport* apt = airportGhost(me);
1219 naRuntimeError(c, "airport.stars called on non-airport object");
1222 naRef stars = naNewVector(c);
1224 FGRunway* rwy = NULL;
1225 if (argc > 0 && naIsString(args[0])) {
1226 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1230 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1231 } else if (argc > 0) {
1232 rwy = runwayGhost(args[0]);
1236 BOOST_FOREACH(flightgear::STAR* s, rwy->getSTARs()) {
1237 naRef procId = stringToNasal(c, s->ident());
1238 naVec_append(stars, procId);
1241 for (unsigned int s=0; s<apt->numSTARs(); ++s) {
1242 flightgear::STAR* star = apt->getSTARByIndex(s);
1243 naRef procId = stringToNasal(c, star->ident());
1244 naVec_append(stars, procId);
1251 static naRef f_airport_approaches(naContext c, naRef me, int argc, naRef* args)
1253 FGAirport* apt = airportGhost(me);
1255 naRuntimeError(c, "airport.getApproachList called on non-airport object");
1258 naRef approaches = naNewVector(c);
1260 ProcedureType ty = PROCEDURE_INVALID;
1261 if ((argc > 1) && naIsString(args[1])) {
1262 std::string u(naStr_data(args[1]));
1264 if (u == "NDB") ty = PROCEDURE_APPROACH_NDB;
1265 if (u == "VOR") ty = PROCEDURE_APPROACH_VOR;
1266 if (u == "ILS") ty = PROCEDURE_APPROACH_ILS;
1267 if (u == "RNAV") ty = PROCEDURE_APPROACH_RNAV;
1270 FGRunway* rwy = NULL;
1271 if (argc > 0 && (rwy = runwayGhost(args[0]))) {
1273 } else if (argc > 0 && naIsString(args[0])) {
1274 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1278 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1282 BOOST_FOREACH(Approach* s, rwy->getApproaches()) {
1283 if ((ty != PROCEDURE_INVALID) && (s->type() != ty)) {
1287 naRef procId = stringToNasal(c, s->ident());
1288 naVec_append(approaches, procId);
1291 // no runway specified, report them all
1292 for (unsigned int s=0; s<apt->numApproaches(); ++s) {
1293 Approach* app = apt->getApproachByIndex(s);
1294 if ((ty != PROCEDURE_INVALID) && (app->type() != ty)) {
1298 naRef procId = stringToNasal(c, app->ident());
1299 naVec_append(approaches, procId);
1306 static naRef f_airport_parking(naContext c, naRef me, int argc, naRef* args)
1308 FGAirport* apt = airportGhost(me);
1310 naRuntimeError(c, "airport.parking called on non-airport object");
1313 naRef r = naNewVector(c);
1315 bool onlyAvailable = false;
1317 if (argc > 0 && naIsString(args[0])) {
1318 type = naStr_data(args[0]);
1321 if ((argc > 1) && naIsNum(args[1])) {
1322 onlyAvailable = (args[1].num != 0.0);
1325 FGAirportDynamics* dynamics = apt->getDynamics();
1326 PositionedIDVec parkings = flightgear::NavDataCache::instance()->airportItemsOfType(apt->guid(),
1327 FGPositioned::PARKING);
1329 BOOST_FOREACH(PositionedID parking, parkings) {
1330 // filter out based on availability and type
1331 if (onlyAvailable && !dynamics->isParkingAvailable(parking)) {
1335 FGParking* park = dynamics->getParking(parking);
1336 if (!type.empty() && (park->getType() != type)) {
1340 const SGGeod& parkLoc = park->geod();
1341 naRef ph = naNewHash(c);
1342 hashset(c, ph, "name", stringToNasal(c, park->getName()));
1343 hashset(c, ph, "lat", naNum(parkLoc.getLatitudeDeg()));
1344 hashset(c, ph, "lon", naNum(parkLoc.getLongitudeDeg()));
1345 hashset(c, ph, "elevation", naNum(parkLoc.getElevationM()));
1346 naVec_append(r, ph);
1352 static naRef f_airport_getSid(naContext c, naRef me, int argc, naRef* args)
1354 FGAirport* apt = airportGhost(me);
1356 naRuntimeError(c, "airport.getSid called on non-airport object");
1359 if ((argc != 1) || !naIsString(args[0])) {
1360 naRuntimeError(c, "airport.getSid passed invalid argument");
1363 std::string ident = naStr_data(args[0]);
1364 return ghostForProcedure(c, apt->findSIDWithIdent(ident));
1367 static naRef f_airport_getStar(naContext c, naRef me, int argc, naRef* args)
1369 FGAirport* apt = airportGhost(me);
1371 naRuntimeError(c, "airport.getStar called on non-airport object");
1374 if ((argc != 1) || !naIsString(args[0])) {
1375 naRuntimeError(c, "airport.getStar passed invalid argument");
1378 std::string ident = naStr_data(args[0]);
1379 return ghostForProcedure(c, apt->findSTARWithIdent(ident));
1382 static naRef f_airport_getApproach(naContext c, naRef me, int argc, naRef* args)
1384 FGAirport* apt = airportGhost(me);
1386 naRuntimeError(c, "airport.getIAP called on non-airport object");
1389 if ((argc != 1) || !naIsString(args[0])) {
1390 naRuntimeError(c, "airport.getIAP passed invalid argument");
1393 std::string ident = naStr_data(args[0]);
1394 return ghostForProcedure(c, apt->findApproachWithIdent(ident));
1397 static naRef f_airport_findBestRunway(naContext c, naRef me, int argc, naRef* args)
1399 FGAirport* apt = airportGhost(me);
1401 naRuntimeError(c, "findBestRunway called on non-airport object");
1405 if (!geodFromArgs(args, 0, argc, pos)) {
1406 naRuntimeError(c, "findBestRunway must be passed a position");
1409 return ghostForRunway(c, apt->findBestRunwayForPos(pos));
1412 static naRef f_airport_toString(naContext c, naRef me, int argc, naRef* args)
1414 FGAirport* apt = airportGhost(me);
1416 naRuntimeError(c, "airport.tostring called on non-airport object");
1419 return stringToNasal(c, "an airport " + apt->ident());
1422 // Returns vector of data hash for navaid of a <type>, nil on error
1423 // navaids sorted by ascending distance
1424 // navinfo([<lat>,<lon>],[<type>],[<id>])
1425 // lat/lon (numeric): use latitude/longitude instead of ac position
1426 // type: ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
1427 // id: (partial) id of the fix
1429 // navinfo("vor") returns all vors
1430 // navinfo("HAM") return all navaids who's name start with "HAM"
1431 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
1432 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
1433 // sorted by distance relative to lat=34, lon=48
1434 static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
1438 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1439 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1443 pos = globals->get_aircraft_position();
1446 FGPositioned::Type type = FGPositioned::INVALID;
1447 nav_list_type navlist;
1448 const char * id = "";
1450 if(argc > 0 && naIsString(args[0])) {
1451 const char *s = naStr_data(args[0]);
1452 if(!strcmp(s, "any")) type = FGPositioned::INVALID;
1453 else if(!strcmp(s, "fix")) type = FGPositioned::FIX;
1454 else if(!strcmp(s, "vor")) type = FGPositioned::VOR;
1455 else if(!strcmp(s, "ndb")) type = FGPositioned::NDB;
1456 else if(!strcmp(s, "ils")) type = FGPositioned::ILS;
1457 else if(!strcmp(s, "dme")) type = FGPositioned::DME;
1458 else if(!strcmp(s, "tacan")) type = FGPositioned::TACAN;
1459 else id = s; // this is an id
1464 if(argc > 0 && naIsString(args[0])) {
1466 naRuntimeError(c, "navinfo() called with navaid id");
1469 id = naStr_data(args[0]);
1475 naRuntimeError(c, "navinfo() called with too many arguments");
1479 FGNavList::TypeFilter filter(type);
1480 navlist = FGNavList::findByIdentAndFreq( pos, id, 0.0, &filter );
1482 naRef reply = naNewVector(c);
1483 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
1484 naVec_append( reply, ghostForNavaid(c, *it) );
1489 static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
1492 SGGeod pos = globals->get_aircraft_position();
1493 argOffset += geodFromArgs(args, 0, argc, pos);
1495 if (!naIsNum(args[argOffset])) {
1496 naRuntimeError(c, "findNavaidsWithinRange expected range (in nm) as arg %d", argOffset);
1499 FGPositioned::Type type = FGPositioned::INVALID;
1500 double rangeNm = args[argOffset++].num;
1501 if (argOffset < argc) {
1502 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1505 naRef r = naNewVector(c);
1506 FGNavList::TypeFilter filter(type);
1507 FGPositionedList navs = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1508 FGPositioned::sortByRange(navs, pos);
1510 BOOST_FOREACH(FGPositionedRef a, navs) {
1511 FGNavRecord* nav = (FGNavRecord*) a.get();
1512 naVec_append(r, ghostForNavaid(c, nav));
1518 static naRef f_findNavaidByFrequency(naContext c, naRef me, int argc, naRef* args)
1521 SGGeod pos = globals->get_aircraft_position();
1522 argOffset += geodFromArgs(args, 0, argc, pos);
1524 if (!naIsNum(args[argOffset])) {
1525 naRuntimeError(c, "findNavaidByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1528 FGPositioned::Type type = FGPositioned::INVALID;
1529 double freqMhz = args[argOffset++].num;
1530 if (argOffset < argc) {
1531 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1534 FGNavList::TypeFilter filter(type);
1535 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1540 return ghostForNavaid(c, navs.front().ptr());
1543 static naRef f_findNavaidsByFrequency(naContext c, naRef me, int argc, naRef* args)
1546 SGGeod pos = globals->get_aircraft_position();
1547 argOffset += geodFromArgs(args, 0, argc, pos);
1549 if (!naIsNum(args[argOffset])) {
1550 naRuntimeError(c, "findNavaidsByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1553 FGPositioned::Type type = FGPositioned::INVALID;
1554 double freqMhz = args[argOffset++].num;
1555 if (argOffset < argc) {
1556 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1559 naRef r = naNewVector(c);
1561 FGNavList::TypeFilter filter(type);
1562 nav_list_type navs = FGNavList::findAllByFreq(freqMhz, pos, &filter);
1564 BOOST_FOREACH(nav_rec_ptr a, navs) {
1565 naVec_append(r, ghostForNavaid(c, a.ptr()));
1571 static naRef f_findNavaidsByIdent(naContext c, naRef me, int argc, naRef* args)
1574 SGGeod pos = globals->get_aircraft_position();
1575 argOffset += geodFromArgs(args, 0, argc, pos);
1577 if (!naIsString(args[argOffset])) {
1578 naRuntimeError(c, "findNavaidsByIdent expectes ident string as arg %d", argOffset);
1581 FGPositioned::Type type = FGPositioned::INVALID;
1582 std::string ident = naStr_data(args[argOffset++]);
1583 if (argOffset < argc) {
1584 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1587 FGNavList::TypeFilter filter(type);
1588 naRef r = naNewVector(c);
1589 nav_list_type navs = FGNavList::findByIdentAndFreq(pos, ident, 0.0, &filter);
1591 BOOST_FOREACH(nav_rec_ptr a, navs) {
1592 naVec_append(r, ghostForNavaid(c, a.ptr()));
1598 static naRef f_findFixesByIdent(naContext c, naRef me, int argc, naRef* args)
1601 SGGeod pos = globals->get_aircraft_position();
1602 argOffset += geodFromArgs(args, 0, argc, pos);
1604 if (!naIsString(args[argOffset])) {
1605 naRuntimeError(c, "findFixesByIdent expectes ident string as arg %d", argOffset);
1608 std::string ident(naStr_data(args[argOffset]));
1609 naRef r = naNewVector(c);
1611 FGPositioned::TypeFilter filter(FGPositioned::FIX);
1612 FGPositionedList fixes = FGPositioned::findAllWithIdent(ident, &filter);
1613 FGPositioned::sortByRange(fixes, pos);
1615 BOOST_FOREACH(FGPositionedRef f, fixes) {
1616 naVec_append(r, ghostForFix(c, (FGFix*) f.ptr()));
1622 // Convert a cartesian point to a geodetic lat/lon/altitude.
1623 static naRef f_magvar(naContext c, naRef me, int argc, naRef* args)
1625 SGGeod pos = globals->get_aircraft_position();
1627 // fine, use aircraft position
1628 } else if (geodFromArgs(args, 0, argc, pos)) {
1631 naRuntimeError(c, "magvar() expects no arguments, a positioned hash or lat,lon pair");
1634 double jd = globals->get_time_params()->getJD();
1635 double magvarDeg = sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
1636 return naNum(magvarDeg);
1639 static naRef f_courseAndDistance(naContext c, naRef me, int argc, naRef* args)
1641 SGGeod from = globals->get_aircraft_position(), to, p;
1642 int argOffset = geodFromArgs(args, 0, argc, p);
1643 if (geodFromArgs(args, argOffset, argc, to)) {
1644 from = p; // we parsed both FROM and TO args, so first was from
1646 to = p; // only parsed one arg, so FROM is current
1649 if (argOffset == 0) {
1650 naRuntimeError(c, "invalid arguments to courseAndDistance");
1653 double course, course2, d;
1654 SGGeodesy::inverse(from, to, course, course2, d);
1656 naRef result = naNewVector(c);
1657 naVec_append(result, naNum(course));
1658 naVec_append(result, naNum(d * SG_METER_TO_NM));
1662 static naRef f_greatCircleMove(naContext c, naRef me, int argc, naRef* args)
1664 SGGeod from = globals->get_aircraft_position(), to;
1667 // complication - don't inerpret two doubles (as the only args)
1668 // as a lat,lon pair - only do so if we have at least three args.
1670 argOffset = geodFromArgs(args, 0, argc, from);
1673 if ((argOffset + 1) >= argc) {
1674 naRuntimeError(c, "isufficent arguments to greatCircleMove");
1677 if (!naIsNum(args[argOffset]) || !naIsNum(args[argOffset+1])) {
1678 naRuntimeError(c, "invalid arguments %d and %d to greatCircleMove",
1679 argOffset, argOffset + 1);
1682 double course = args[argOffset].num, course2;
1683 double distanceNm = args[argOffset + 1].num;
1684 SGGeodesy::direct(from, course, distanceNm * SG_NM_TO_METER, to, course2);
1687 naRef coord = naNewHash(c);
1688 hashset(c, coord, "lat", naNum(to.getLatitudeDeg()));
1689 hashset(c, coord, "lon", naNum(to.getLongitudeDeg()));
1693 static naRef f_tilePath(naContext c, naRef me, int argc, naRef* args)
1695 SGGeod pos = globals->get_aircraft_position();
1696 geodFromArgs(args, 0, argc, pos);
1698 return stringToNasal(c, b.gen_base_path());
1701 static naRef f_tileIndex(naContext c, naRef me, int argc, naRef* args)
1703 SGGeod pos = globals->get_aircraft_position();
1704 geodFromArgs(args, 0, argc, pos);
1706 return naNum(b.gen_index());
1709 static naRef f_route(naContext c, naRef me, int argc, naRef* args)
1712 FGRouteMgr* rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
1713 return ghostForFlightPlan(c, rm->flightPlan());
1716 if ((argc > 0) && naIsString(args[0])) {
1717 flightgear::FlightPlan* fp = new flightgear::FlightPlan;
1718 SGPath path(naStr_data(args[0]));
1719 if (!path.exists()) {
1720 naRuntimeError(c, "flightplan, no file at path %s", path.c_str());
1723 if (!fp->load(path)) {
1724 SG_LOG(SG_NASAL, SG_WARN, "failed to load flight-plan from " << path);
1729 return ghostForFlightPlan(c, fp);
1732 naRuntimeError(c, "bad arguments to flightplan()");
1736 class NasalFPDelegate : public FlightPlan::Delegate
1739 NasalFPDelegate(FlightPlan* fp, FGNasalSys* sys, naRef ins) :
1744 SG_LOG(SG_NASAL, SG_INFO, "created Nasal delegate for " << fp);
1745 _gcSaveKey = _nasal->gcSave(ins);
1748 virtual ~NasalFPDelegate()
1750 SG_LOG(SG_NASAL, SG_INFO, "destroying Nasal delegate for " << _plan);
1751 _nasal->gcRelease(_gcSaveKey);
1754 virtual void departureChanged()
1756 callDelegateMethod("departureChanged");
1759 virtual void arrivalChanged()
1761 callDelegateMethod("arrivalChanged");
1764 virtual void waypointsChanged()
1766 callDelegateMethod("waypointsChanged");
1769 virtual void currentWaypointChanged()
1771 callDelegateMethod("currentWaypointChanged");
1774 virtual void cleared()
1776 callDelegateMethod("cleared");
1779 virtual void endOfFlightPlan()
1781 callDelegateMethod("endOfFlightPlan");
1785 void callDelegateMethod(const char* method)
1788 if (naMember_cget(_nasal->context(), _instance, method, &f) == 0) {
1789 return; // no method on the delegate
1793 arg[0] = ghostForFlightPlan(_nasal->context(), _plan);
1794 _nasal->callMethod(f, _instance, 1, arg, naNil());
1803 class NasalFPDelegateFactory : public FlightPlan::DelegateFactory
1806 NasalFPDelegateFactory(naRef code)
1808 _nasal = (FGNasalSys*) globals->get_subsystem("nasal");
1810 _gcSaveKey = _nasal->gcSave(_func);
1813 ~NasalFPDelegateFactory()
1815 _nasal->gcRelease(_gcSaveKey);
1818 virtual FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp)
1821 args[0] = ghostForFlightPlan(_nasal->context(), fp);
1822 naRef instance = _nasal->call(_func, 1, args, naNil());
1823 if (naIsNil(instance)) {
1827 return new NasalFPDelegate(fp, _nasal, instance);
1835 static naRef f_registerFPDelegate(naContext c, naRef me, int argc, naRef* args)
1837 if ((argc < 1) || !naIsFunc(args[0])) {
1838 naRuntimeError(c, "non-function argument to registerFlightPlanDelegate");
1841 NasalFPDelegateFactory* factory = new NasalFPDelegateFactory(args[0]);
1842 FlightPlan::registerDelegateFactory(factory);
1847 static WayptRef wayptFromArg(naRef arg)
1849 WayptRef r = wayptGhost(arg);
1854 FGPositioned* pos = positionedGhost(arg);
1856 // let's check if the arg is hash, coudl extra a geod and hence build
1857 // a simple waypoint
1862 // special-case for runways
1863 if (pos->type() == FGPositioned::RUNWAY) {
1864 return new RunwayWaypt((FGRunway*) pos, NULL);
1867 return new NavaidWaypoint(pos, NULL);
1870 static naRef convertWayptVecToNasal(naContext c, const WayptVec& wps)
1872 naRef result = naNewVector(c);
1873 BOOST_FOREACH(WayptRef wpt, wps) {
1874 naVec_append(result, ghostForWaypt(c, wpt.get()));
1879 static naRef f_airwaySearch(naContext c, naRef me, int argc, naRef* args)
1882 naRuntimeError(c, "airwaysSearch needs at least two arguments");
1885 WayptRef start = wayptFromArg(args[0]),
1886 end = wayptFromArg(args[1]);
1888 if (!start || !end) {
1889 SG_LOG(SG_NASAL, SG_WARN, "airwaysSearch: start or end points are invalid");
1893 bool highLevel = true;
1894 if ((argc > 2) && naIsString(args[2])) {
1895 if (!strcmp(naStr_data(args[2]), "lowlevel")) {
1902 Airway::highLevel()->route(start, end, route);
1904 Airway::lowLevel()->route(start, end, route);
1907 return convertWayptVecToNasal(c, route);
1910 static naRef f_createWP(naContext c, naRef me, int argc, naRef* args)
1913 int argOffset = geodFromArgs(args, 0, argc, pos);
1915 if (((argc - argOffset) < 1) || !naIsString(args[argOffset])) {
1916 naRuntimeError(c, "createWP: no identifier supplied");
1919 std::string ident = naStr_data(args[argOffset++]);
1920 WayptRef wpt = new BasicWaypt(pos, ident, NULL);
1922 // set waypt flags - approach, departure, pseudo, etc
1923 if (argc > argOffset) {
1924 WayptFlag f = wayptFlagFromString(naStr_data(args[argOffset++]));
1928 return ghostForWaypt(c, wpt);
1931 static naRef f_createWPFrom(naContext c, naRef me, int argc, naRef* args)
1934 naRuntimeError(c, "createWPFrom: need at least one argument");
1937 FGPositioned* positioned = positionedGhost(args[0]);
1939 naRuntimeError(c, "createWPFrom: couldn;t convert arg[0] to FGPositioned");
1943 if (positioned->type() == FGPositioned::RUNWAY) {
1944 wpt = new RunwayWaypt((FGRunway*) positioned, NULL);
1946 wpt = new NavaidWaypoint(positioned, NULL);
1949 // set waypt flags - approach, departure, pseudo, etc
1951 WayptFlag f = wayptFlagFromString(naStr_data(args[1]));
1955 return ghostForWaypt(c, wpt);
1958 static naRef f_flightplan_getWP(naContext c, naRef me, int argc, naRef* args)
1960 FlightPlan* fp = flightplanGhost(me);
1962 naRuntimeError(c, "flightplan.getWP called on non-flightplan object");
1967 index = fp->currentIndex();
1969 index = (int) naNumValue(args[0]).num;
1972 if ((index < 0) || (index >= fp->numLegs())) {
1976 return ghostForLeg(c, fp->legAtIndex(index));
1979 static naRef f_flightplan_currentWP(naContext c, naRef me, int argc, naRef* args)
1981 FlightPlan* fp = flightplanGhost(me);
1983 naRuntimeError(c, "flightplan.currentWP called on non-flightplan object");
1985 return ghostForLeg(c, fp->currentLeg());
1988 static naRef f_flightplan_nextWP(naContext c, naRef me, int argc, naRef* args)
1990 FlightPlan* fp = flightplanGhost(me);
1992 naRuntimeError(c, "flightplan.nextWP called on non-flightplan object");
1994 return ghostForLeg(c, fp->nextLeg());
1997 static naRef f_flightplan_numWaypoints(naContext c, naRef me, int argc, naRef* args)
1999 FlightPlan* fp = flightplanGhost(me);
2001 naRuntimeError(c, "flightplan.numWaypoints called on non-flightplan object");
2003 return naNum(fp->numLegs());
2006 static naRef f_flightplan_appendWP(naContext c, naRef me, int argc, naRef* args)
2008 FlightPlan* fp = flightplanGhost(me);
2010 naRuntimeError(c, "flightplan.appendWP called on non-flightplan object");
2013 WayptRef wp = wayptGhost(args[0]);
2014 int index = fp->numLegs();
2015 fp->insertWayptAtIndex(wp.get(), index);
2016 return naNum(index);
2019 static naRef f_flightplan_insertWP(naContext c, naRef me, int argc, naRef* args)
2021 FlightPlan* fp = flightplanGhost(me);
2023 naRuntimeError(c, "flightplan.insertWP called on non-flightplan object");
2026 WayptRef wp = wayptGhost(args[0]);
2027 int index = -1; // append
2028 if ((argc > 1) && naIsNum(args[1])) {
2029 index = (int) args[1].num;
2032 fp->insertWayptAtIndex(wp.get(), index);
2036 static naRef f_flightplan_insertWPAfter(naContext c, naRef me, int argc, naRef* args)
2038 FlightPlan* fp = flightplanGhost(me);
2040 naRuntimeError(c, "flightplan.insertWPAfter called on non-flightplan object");
2043 WayptRef wp = wayptGhost(args[0]);
2044 int index = -1; // append
2045 if ((argc > 1) && naIsNum(args[1])) {
2046 index = (int) args[1].num;
2049 fp->insertWayptAtIndex(wp.get(), index + 1);
2053 static naRef f_flightplan_insertWaypoints(naContext c, naRef me, int argc, naRef* args)
2055 FlightPlan* fp = flightplanGhost(me);
2057 naRuntimeError(c, "flightplan.insertWaypoints called on non-flightplan object");
2061 if (!naIsVector(args[0])) {
2062 naRuntimeError(c, "flightplan.insertWaypoints expects vector as first arg");
2065 int count = naVec_size(args[0]);
2066 for (int i=0; i<count; ++i) {
2067 Waypt* wp = wayptGhost(naVec_get(args[0], i));
2073 int index = -1; // append
2074 if ((argc > 1) && naIsNum(args[1])) {
2075 index = (int) args[1].num;
2078 fp->insertWayptsAtIndex(wps, index);
2082 static naRef f_flightplan_deleteWP(naContext c, naRef me, int argc, naRef* args)
2084 FlightPlan* fp = flightplanGhost(me);
2086 naRuntimeError(c, "flightplan.deleteWP called on non-flightplan object");
2089 if ((argc < 1) || !naIsNum(args[0])) {
2090 naRuntimeError(c, "bad argument to flightplan.deleteWP");
2093 int index = (int) args[0].num;
2094 fp->deleteIndex(index);
2098 static naRef f_flightplan_clearPlan(naContext c, naRef me, int argc, naRef* args)
2100 FlightPlan* fp = flightplanGhost(me);
2102 naRuntimeError(c, "flightplan.clearPlan called on non-flightplan object");
2109 static naRef f_flightplan_clearWPType(naContext c, naRef me, int argc, naRef* args)
2111 FlightPlan* fp = flightplanGhost(me);
2113 naRuntimeError(c, "flightplan.clearWPType called on non-flightplan object");
2117 naRuntimeError(c, "insufficent args to flightplan.clearWPType");
2120 WayptFlag flag = wayptFlagFromString(naStr_data(args[0]));
2121 fp->clearWayptsWithFlag(flag);
2125 static naRef f_flightplan_clone(naContext c, naRef me, int argc, naRef* args)
2127 FlightPlan* fp = flightplanGhost(me);
2129 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2132 return ghostForFlightPlan(c, fp->clone());
2135 static naRef f_flightplan_pathGeod(naContext c, naRef me, int argc, naRef* args)
2137 FlightPlan* fp = flightplanGhost(me);
2139 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
2142 if ((argc < 1) || !naIsNum(args[0])) {
2143 naRuntimeError(c, "bad argument to flightplan.pathGeod");
2146 if ((argc > 1) && !naIsNum(args[1])) {
2147 naRuntimeError(c, "bad argument to flightplan.pathGeod");
2150 int index = (int) args[0].num;
2151 double offset = (argc > 1) ? args[1].num : 0.0;
2152 naRef result = naNewHash(c);
2153 SGGeod g = fp->pointAlongRoute(index, offset);
2154 hashset(c, result, "lat", naNum(g.getLatitudeDeg()));
2155 hashset(c, result, "lon", naNum(g.getLongitudeDeg()));
2159 static naRef f_flightplan_finish(naContext c, naRef me, int argc, naRef* args)
2161 FlightPlan* fp = flightplanGhost(me);
2163 naRuntimeError(c, "flightplan.finish called on non-flightplan object");
2170 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
2172 FlightPlan::Leg* leg = fpLegGhost(me);
2174 naRuntimeError(c, "leg.setSpeed called on non-flightplan-leg object");
2178 naRuntimeError(c, "bad arguments to leg.setSpeed");
2181 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2182 leg->setSpeed(rr, args[0].num);
2186 static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
2188 FlightPlan::Leg* leg = fpLegGhost(me);
2190 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2194 naRuntimeError(c, "bad arguments to leg.setAltitude");
2197 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
2198 leg->setAltitude(rr, args[0].num);
2202 static naRef f_leg_path(naContext c, naRef me, int argc, naRef* args)
2204 FlightPlan::Leg* leg = fpLegGhost(me);
2206 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
2209 RoutePath path(leg->owner());
2210 SGGeodVec gv(path.pathForIndex(leg->index()));
2212 naRef result = naNewVector(c);
2213 BOOST_FOREACH(SGGeod p, gv) {
2214 // construct a geo.Coord!
2215 naRef coord = naNewHash(c);
2216 hashset(c, coord, "lat", naNum(p.getLatitudeDeg()));
2217 hashset(c, coord, "lon", naNum(p.getLongitudeDeg()));
2218 naVec_append(result, coord);
2224 static naRef f_leg_courseAndDistanceFrom(naContext c, naRef me, int argc, naRef* args)
2226 FlightPlan::Leg* leg = fpLegGhost(me);
2228 naRuntimeError(c, "leg.courseAndDistanceFrom called on non-flightplan-leg object");
2232 geodFromArgs(args, 0, argc, pos);
2236 boost::tie(courseDeg, distanceM) = leg->waypoint()->courseAndDistanceFrom(pos);
2238 naRef result = naNewVector(c);
2239 naVec_append(result, naNum(courseDeg));
2240 naVec_append(result, naNum(distanceM * SG_METER_TO_NM));
2244 static naRef f_waypoint_navaid(naContext c, naRef me, int argc, naRef* args)
2246 flightgear::Waypt* w = wayptGhost(me);
2248 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2251 FGPositioned* pos = w->source();
2256 switch (pos->type()) {
2257 case FGPositioned::VOR:
2258 case FGPositioned::NDB:
2259 case FGPositioned::ILS:
2260 case FGPositioned::LOC:
2261 case FGPositioned::GS:
2262 case FGPositioned::DME:
2263 case FGPositioned::TACAN: {
2264 FGNavRecord* nav = (FGNavRecord*) pos;
2265 return ghostForNavaid(c, nav);
2273 static naRef f_waypoint_airport(naContext c, naRef me, int argc, naRef* args)
2275 flightgear::Waypt* w = wayptGhost(me);
2277 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2280 FGPositioned* pos = w->source();
2281 if (!pos || FGAirport::isAirportType(pos)) {
2285 return ghostForAirport(c, (FGAirport*) pos);
2288 static naRef f_waypoint_runway(naContext c, naRef me, int argc, naRef* args)
2290 flightgear::Waypt* w = wayptGhost(me);
2292 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
2295 FGPositioned* pos = w->source();
2296 if (!pos || (pos->type() != FGPositioned::RUNWAY)) {
2300 return ghostForRunway(c, (FGRunway*) pos);
2303 static naRef f_procedure_transition(naContext c, naRef me, int argc, naRef* args)
2305 Procedure* proc = procedureGhost(me);
2307 naRuntimeError(c, "procedure.transition called on non-procedure object");
2310 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2311 naRuntimeError(c, "procedure.transition called on non-SID or -STAR");
2314 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2315 Transition* trans = ad->findTransitionByName(naStr_data(args[0]));
2317 return ghostForProcedure(c, trans);
2320 static naRef f_procedure_route(naContext c, naRef me, int argc, naRef* args)
2322 Procedure* proc = procedureGhost(me);
2324 naRuntimeError(c, "procedure.route called on non-procedure object");
2327 // wrapping up tow different routines here - approach routing from the IAF
2328 // to the associated runway, and SID/STAR routing via an enroute transition
2329 // and possibly a runway transition or not.
2330 if (Approach::isApproach(proc->type())) {
2333 iaf = wayptFromArg(args[0]);
2337 Approach* app = (Approach*) proc;
2338 if (!app->route(iaf, r)) {
2339 SG_LOG(SG_NASAL, SG_WARN, "procedure.route failed for Approach somehow");
2343 return convertWayptVecToNasal(c, r);
2344 } else if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
2345 naRuntimeError(c, "procedure.route called on unsuitable procedure type");
2349 FGRunway* rwy = runwayGhost(args[0]);
2350 if (rwy) ++argOffset;
2352 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
2353 Transition* trans = NULL;
2354 if (argOffset < argc) {
2355 trans = (Transition*) procedureGhost(args[argOffset]);
2358 // note either runway or trans may be NULL - that's ok
2360 if (!ad->route(rwy, trans, r)) {
2361 SG_LOG(SG_NASAL, SG_WARN, "prcoedure.route failed for ArrvialDeparture somehow");
2365 return convertWayptVecToNasal(c, r);
2369 // Table of extension functions. Terminate with zeros.
2370 static struct { const char* name; naCFunction func; } funcs[] = {
2371 { "carttogeod", f_carttogeod },
2372 { "geodtocart", f_geodtocart },
2373 { "geodinfo", f_geodinfo },
2374 { "airportinfo", f_airportinfo },
2375 { "findAirportsWithinRange", f_findAirportsWithinRange },
2376 { "findAirportsByICAO", f_findAirportsByICAO },
2377 { "navinfo", f_navinfo },
2378 { "findNavaidsWithinRange", f_findNavaidsWithinRange },
2379 { "findNavaidByFrequency", f_findNavaidByFrequency },
2380 { "findNavaidsByFrequency", f_findNavaidsByFrequency },
2381 { "findNavaidsByID", f_findNavaidsByIdent },
2382 { "findFixesByID", f_findFixesByIdent },
2383 { "flightplan", f_route },
2384 { "registerFlightPlanDelegate", f_registerFPDelegate },
2385 { "createWP", f_createWP },
2386 { "createWPFrom", f_createWPFrom },
2387 { "airwaysRoute", f_airwaySearch },
2388 { "magvar", f_magvar },
2389 { "courseAndDistance", f_courseAndDistance },
2390 { "greatCircleMove", f_greatCircleMove },
2391 { "tileIndex", f_tileIndex },
2392 { "tilePath", f_tilePath },
2397 naRef initNasalPositioned(naRef globals, naContext c, naRef gcSave)
2399 airportPrototype = naNewHash(c);
2400 hashset(c, gcSave, "airportProto", airportPrototype);
2402 hashset(c, airportPrototype, "runway", naNewFunc(c, naNewCCode(c, f_airport_runway)));
2403 hashset(c, airportPrototype, "runwaysWithoutReciprocals", naNewFunc(c, naNewCCode(c, f_airport_runwaysWithoutReciprocals)));
2404 hashset(c, airportPrototype, "helipad", naNewFunc(c, naNewCCode(c, f_airport_runway)));
2405 hashset(c, airportPrototype, "tower", naNewFunc(c, naNewCCode(c, f_airport_tower)));
2406 hashset(c, airportPrototype, "comms", naNewFunc(c, naNewCCode(c, f_airport_comms)));
2407 hashset(c, airportPrototype, "sids", naNewFunc(c, naNewCCode(c, f_airport_sids)));
2408 hashset(c, airportPrototype, "stars", naNewFunc(c, naNewCCode(c, f_airport_stars)));
2409 hashset(c, airportPrototype, "getApproachList", naNewFunc(c, naNewCCode(c, f_airport_approaches)));
2410 hashset(c, airportPrototype, "parking", naNewFunc(c, naNewCCode(c, f_airport_parking)));
2411 hashset(c, airportPrototype, "getSid", naNewFunc(c, naNewCCode(c, f_airport_getSid)));
2412 hashset(c, airportPrototype, "getStar", naNewFunc(c, naNewCCode(c, f_airport_getStar)));
2413 hashset(c, airportPrototype, "getIAP", naNewFunc(c, naNewCCode(c, f_airport_getApproach)));
2414 hashset(c, airportPrototype, "findBestRunwayForPos", naNewFunc(c, naNewCCode(c, f_airport_findBestRunway)));
2415 hashset(c, airportPrototype, "tostring", naNewFunc(c, naNewCCode(c, f_airport_toString)));
2417 flightplanPrototype = naNewHash(c);
2418 hashset(c, gcSave, "flightplanProto", flightplanPrototype);
2420 hashset(c, flightplanPrototype, "getWP", naNewFunc(c, naNewCCode(c, f_flightplan_getWP)));
2421 hashset(c, flightplanPrototype, "currentWP", naNewFunc(c, naNewCCode(c, f_flightplan_currentWP)));
2422 hashset(c, flightplanPrototype, "nextWP", naNewFunc(c, naNewCCode(c, f_flightplan_nextWP)));
2423 hashset(c, flightplanPrototype, "getPlanSize", naNewFunc(c, naNewCCode(c, f_flightplan_numWaypoints)));
2424 hashset(c, flightplanPrototype, "appendWP", naNewFunc(c, naNewCCode(c, f_flightplan_appendWP)));
2425 hashset(c, flightplanPrototype, "insertWP", naNewFunc(c, naNewCCode(c, f_flightplan_insertWP)));
2426 hashset(c, flightplanPrototype, "deleteWP", naNewFunc(c, naNewCCode(c, f_flightplan_deleteWP)));
2427 hashset(c, flightplanPrototype, "insertWPAfter", naNewFunc(c, naNewCCode(c, f_flightplan_insertWPAfter)));
2428 hashset(c, flightplanPrototype, "insertWaypoints", naNewFunc(c, naNewCCode(c, f_flightplan_insertWaypoints)));
2429 hashset(c, flightplanPrototype, "cleanPlan", naNewFunc(c, naNewCCode(c, f_flightplan_clearPlan)));
2430 hashset(c, flightplanPrototype, "clearWPType", naNewFunc(c, naNewCCode(c, f_flightplan_clearWPType)));
2431 hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone)));
2432 hashset(c, flightplanPrototype, "pathGeod", naNewFunc(c, naNewCCode(c, f_flightplan_pathGeod)));
2433 hashset(c, flightplanPrototype, "finish", naNewFunc(c, naNewCCode(c, f_flightplan_finish)));
2435 waypointPrototype = naNewHash(c);
2436 hashset(c, gcSave, "wayptProto", waypointPrototype);
2438 hashset(c, waypointPrototype, "navaid", naNewFunc(c, naNewCCode(c, f_waypoint_navaid)));
2439 hashset(c, waypointPrototype, "runway", naNewFunc(c, naNewCCode(c, f_waypoint_runway)));
2440 hashset(c, waypointPrototype, "airport", naNewFunc(c, naNewCCode(c, f_waypoint_airport)));
2442 procedurePrototype = naNewHash(c);
2443 hashset(c, gcSave, "procedureProto", procedurePrototype);
2444 hashset(c, procedurePrototype, "transition", naNewFunc(c, naNewCCode(c, f_procedure_transition)));
2445 hashset(c, procedurePrototype, "route", naNewFunc(c, naNewCCode(c, f_procedure_route)));
2447 fpLegPrototype = naNewHash(c);
2448 hashset(c, gcSave, "fpLegProto", fpLegPrototype);
2449 hashset(c, fpLegPrototype, "setSpeed", naNewFunc(c, naNewCCode(c, f_leg_setSpeed)));
2450 hashset(c, fpLegPrototype, "setAltitude", naNewFunc(c, naNewCCode(c, f_leg_setAltitude)));
2451 hashset(c, fpLegPrototype, "path", naNewFunc(c, naNewCCode(c, f_leg_path)));
2452 hashset(c, fpLegPrototype, "courseAndDistanceFrom", naNewFunc(c, naNewCCode(c, f_leg_courseAndDistanceFrom)));
2454 for(int i=0; funcs[i].name; i++) {
2455 hashset(c, globals, funcs[i].name,
2456 naNewFunc(c, naNewCCode(c, funcs[i].func)));
2462 void postinitNasalPositioned(naRef globals, naContext c)
2464 naRef geoModule = naHash_cget(globals, (char*) "geo");
2465 if (naIsNil(geoModule)) {
2466 SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalPositioned: geo.nas not loaded");
2470 geoCoordClass = naHash_cget(geoModule, (char*) "Coord");