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 <Navaids/navlist.hxx>
43 #include <Navaids/procedure.hxx>
44 #include <Main/globals.hxx>
45 #include <Main/fg_props.hxx>
46 #include <Scenery/scenery.hxx>
47 #include <ATC/CommStation.hxx>
48 #include <Navaids/route.hxx>
49 #include <Autopilot/route_mgr.hxx>
50 #include <Navaids/procedure.hxx>
52 using namespace flightgear;
54 static void positionedGhostDestroy(void* g);
55 static void wayptGhostDestroy(void* g);
56 static void legGhostDestroy(void* g);
57 static void routeBaseGhostDestroy(void* g);
59 naGhostType PositionedGhostType = { positionedGhostDestroy, "positioned" };
61 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out);
62 naGhostType AirportGhostType = { positionedGhostDestroy, "airport", airportGhostGetMember, 0 };
64 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out);
65 naGhostType NavaidGhostType = { positionedGhostDestroy, "navaid", navaidGhostGetMember, 0 };
67 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out);
68 naGhostType RunwayGhostType = { positionedGhostDestroy, "runway", runwayGhostGetMember, 0 };
70 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out);
71 naGhostType WayptGhostType = { wayptGhostDestroy,
76 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out);
77 naGhostType FPLegGhostType = { legGhostDestroy,
82 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out);
83 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value);
85 naGhostType FlightPlanGhostType = { routeBaseGhostDestroy,
87 flightplanGhostGetMember,
88 flightplanGhostSetMember
91 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out);
92 naGhostType ProcedureGhostType = { routeBaseGhostDestroy,
94 procedureGhostGetMember,
97 static void hashset(naContext c, naRef hash, const char* key, naRef val)
99 naRef s = naNewString(c);
100 naStr_fromdata(s, (char*)key, strlen(key));
101 naHash_set(hash, s, val);
104 static naRef stringToNasal(naContext c, const std::string& s)
106 return naStr_fromdata(naNewString(c),
107 const_cast<char *>(s.c_str()),
111 static FGPositioned* positionedGhost(naRef r)
113 if (naGhost_type(r) == &PositionedGhostType)
114 return (FGPositioned*) naGhost_ptr(r);
118 static FGAirport* airportGhost(naRef r)
120 if (naGhost_type(r) == &AirportGhostType)
121 return (FGAirport*) naGhost_ptr(r);
125 static FGNavRecord* navaidGhost(naRef r)
127 if (naGhost_type(r) == &NavaidGhostType)
128 return (FGNavRecord*) naGhost_ptr(r);
132 static FGRunway* runwayGhost(naRef r)
134 if (naGhost_type(r) == &RunwayGhostType)
135 return (FGRunway*) naGhost_ptr(r);
139 static void positionedGhostDestroy(void* g)
141 FGPositioned* pos = (FGPositioned*)g;
142 if (!FGPositioned::put(pos)) // unref
146 static Waypt* wayptGhost(naRef r)
148 if (naGhost_type(r) == &WayptGhostType)
149 return (Waypt*) naGhost_ptr(r);
153 static void wayptGhostDestroy(void* g)
155 Waypt* wpt = (Waypt*)g;
156 if (!Waypt::put(wpt)) // unref
160 static void legGhostDestroy(void* g)
166 static FlightPlan::Leg* fpLegGhost(naRef r)
168 if (naGhost_type(r) == &FPLegGhostType)
169 return (FlightPlan::Leg*) naGhost_ptr(r);
173 static Procedure* procedureGhost(naRef r)
175 if (naGhost_type(r) == &ProcedureGhostType)
176 return (Procedure*) naGhost_ptr(r);
180 static FlightPlan* flightplanGhost(naRef r)
182 if (naGhost_type(r) == &FlightPlanGhostType)
183 return (FlightPlan*) naGhost_ptr(r);
187 static void routeBaseGhostDestroy(void* g)
192 static naRef airportPrototype;
193 static naRef flightplanPrototype;
194 static naRef waypointPrototype;
195 static naRef geoCoordClass;
196 static naRef fpLegPrototype;
197 static naRef procedurePrototype;
199 naRef ghostForPositioned(naContext c, const FGPositioned* pos)
205 FGPositioned::get(pos); // take a ref
206 return naNewGhost(c, &PositionedGhostType, (void*) pos);
209 naRef ghostForAirport(naContext c, const FGAirport* apt)
215 FGPositioned::get(apt); // take a ref
216 return naNewGhost2(c, &AirportGhostType, (void*) apt);
219 naRef ghostForNavaid(naContext c, const FGNavRecord* n)
225 FGPositioned::get(n); // take a ref
226 return naNewGhost2(c, &NavaidGhostType, (void*) n);
229 naRef ghostForRunway(naContext c, const FGRunway* r)
235 FGPositioned::get(r); // take a ref
236 return naNewGhost2(c, &RunwayGhostType, (void*) r);
239 naRef ghostForWaypt(naContext c, const Waypt* wpt)
245 Waypt::get(wpt); // take a ref
246 return naNewGhost2(c, &WayptGhostType, (void*) wpt);
249 naRef ghostForLeg(naContext c, const FlightPlan::Leg* leg)
255 return naNewGhost2(c, &FPLegGhostType, (void*) leg);
258 naRef ghostForFlightPlan(naContext c, const FlightPlan* fp)
264 return naNewGhost2(c, &FlightPlanGhostType, (void*) fp);
267 naRef ghostForProcedure(naContext c, const Procedure* proc)
273 return naNewGhost2(c, &ProcedureGhostType, (void*) proc);
276 static const char* airportGhostGetMember(naContext c, void* g, naRef field, naRef* out)
278 const char* fieldName = naStr_data(field);
279 FGAirport* apt = (FGAirport*) g;
281 if (!strcmp(fieldName, "parents")) {
282 *out = naNewVector(c);
283 naVec_append(*out, airportPrototype);
284 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, apt->ident());
285 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, apt->name());
286 else if (!strcmp(fieldName, "lat")) *out = naNum(apt->getLatitude());
287 else if (!strcmp(fieldName, "lon")) *out = naNum(apt->getLongitude());
288 else if (!strcmp(fieldName, "elevation")) {
289 *out = naNum(apt->getElevation() * SG_FEET_TO_METER);
290 } else if (!strcmp(fieldName, "has_metar")) {
291 *out = naNum(apt->getMetar());
292 } else if (!strcmp(fieldName, "runways")) {
294 for(unsigned int r=0; r<apt->numRunways(); ++r) {
295 FGRunway* rwy(apt->getRunwayByIndex(r));
296 naRef rwyid = stringToNasal(c, rwy->ident());
297 naRef rwydata = ghostForRunway(c, rwy);
298 naHash_set(*out, rwyid, rwydata);
308 static const char* waypointCommonGetMember(naContext c, Waypt* wpt, const char* fieldName, naRef* out)
310 if (!strcmp(fieldName, "wp_name")) *out = stringToNasal(c, wpt->ident());
311 else if (!strcmp(fieldName, "wp_type")) *out = stringToNasal(c, wpt->type());
312 else if (!strcmp(fieldName, "wp_lat")) *out = naNum(wpt->position().getLatitudeDeg());
313 else if (!strcmp(fieldName, "wp_lon")) *out = naNum(wpt->position().getLongitudeDeg());
314 else if (!strcmp(fieldName, "wp_parent_name")) {
315 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
316 *out = proc ? stringToNasal(c, proc->ident()) : naNil();
317 } else if (!strcmp(fieldName, "wp_parent")) {
318 Procedure* proc = dynamic_cast<Procedure*>(wpt->owner());
319 *out = ghostForProcedure(c, proc);
320 } else if (!strcmp(fieldName, "fly_type")) {
321 if (wpt->type() == "hold") {
322 *out = stringToNasal(c, "Hold");
324 *out = stringToNasal(c, wpt->flag(WPT_OVERFLIGHT) ? "flyOver" : "flyBy");
327 return NULL; // member not found
333 static const char* wayptGhostGetMember(naContext c, void* g, naRef field, naRef* out)
335 const char* fieldName = naStr_data(field);
336 Waypt* wpt = (flightgear::Waypt*) g;
337 return waypointCommonGetMember(c, wpt, fieldName, out);
340 static RouteRestriction routeRestrictionFromString(const char* s)
344 if (u == "computed") return RESTRICT_COMPUTED;
345 if (u == "at") return RESTRICT_AT;
346 if (u == "mach") return SPEED_RESTRICT_MACH;
347 if (u == "computed-mach") return SPEED_COMPUTED_MACH;
348 if (u == "delete") return RESTRICT_DELETE;
349 return RESTRICT_NONE;
352 naRef routeRestrictionToNasal(naContext c, RouteRestriction rr)
355 case RESTRICT_NONE: return naNil();
356 case RESTRICT_AT: return stringToNasal(c, "at");
357 case RESTRICT_ABOVE: return stringToNasal(c, "above");
358 case RESTRICT_BELOW: return stringToNasal(c, "below");
359 case SPEED_RESTRICT_MACH: return stringToNasal(c, "mach");
360 case RESTRICT_COMPUTED: return stringToNasal(c, "computed");
361 case SPEED_COMPUTED_MACH: return stringToNasal(c, "computed-mach");
362 case RESTRICT_DELETE: return stringToNasal(c, "delete");
368 static const char* legGhostGetMember(naContext c, void* g, naRef field, naRef* out)
370 const char* fieldName = naStr_data(field);
371 FlightPlan::Leg* leg = (FlightPlan::Leg*) g;
372 Waypt* wpt = leg->waypoint();
374 if (!strcmp(fieldName, "parents")) {
375 *out = naNewVector(c);
376 naVec_append(*out, fpLegPrototype);
377 } else if (!strcmp(fieldName, "alt_cstr")) {
378 *out = naNum(leg->altitudeFt());
379 } else if (!strcmp(fieldName, "alt_cstr_type")) {
380 *out = routeRestrictionToNasal(c, leg->altitudeRestriction());
381 } else if (!strcmp(fieldName, "speed_cstr")) {
382 double s = isMachRestrict(leg->speedRestriction()) ? leg->speedMach() : leg->speedKts();
384 } else if (!strcmp(fieldName, "speed_cstr_type")) {
385 *out = routeRestrictionToNasal(c, leg->speedRestriction());
386 } else if (!strcmp(fieldName, "leg_distance")) {
387 *out = naNum(leg->distanceNm());
388 } else if (!strcmp(fieldName, "leg_bearing")) {
389 *out = naNum(leg->courseDeg());
390 } else if (!strcmp(fieldName, "distance_along_route")) {
391 *out = naNum(leg->distanceAlongRoute());
392 } else { // check for fields defined on the underlying waypoint
393 return waypointCommonGetMember(c, wpt, fieldName, out);
396 return ""; // success
399 static const char* flightplanGhostGetMember(naContext c, void* g, naRef field, naRef* out)
401 const char* fieldName = naStr_data(field);
402 FlightPlan* fp = (FlightPlan*) g;
404 if (!strcmp(fieldName, "parents")) {
405 *out = naNewVector(c);
406 naVec_append(*out, flightplanPrototype);
407 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fp->ident());
408 else if (!strcmp(fieldName, "departure")) *out = ghostForAirport(c, fp->departureAirport());
409 else if (!strcmp(fieldName, "destination")) *out = ghostForAirport(c, fp->destinationAirport());
410 else if (!strcmp(fieldName, "departure_runway")) *out = ghostForRunway(c, fp->departureRunway());
411 else if (!strcmp(fieldName, "destination_runway")) *out = ghostForRunway(c, fp->destinationRunway());
412 else if (!strcmp(fieldName, "sid")) *out = ghostForProcedure(c, fp->sid());
413 else if (!strcmp(fieldName, "sid_trans")) *out = ghostForProcedure(c, fp->sidTransition());
414 else if (!strcmp(fieldName, "star")) *out = ghostForProcedure(c, fp->star());
415 else if (!strcmp(fieldName, "star_trans")) *out = ghostForProcedure(c, fp->starTransition());
416 else if (!strcmp(fieldName, "approach")) *out = ghostForProcedure(c, fp->approach());
417 else if (!strcmp(fieldName, "current")) *out = naNum(fp->currentIndex());
425 static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef value)
427 const char* fieldName = naStr_data(field);
428 FlightPlan* fp = (FlightPlan*) g;
430 if (!strcmp(fieldName, "id")) {
431 if (!naIsString(value)) naRuntimeError(c, "flightplan.id must be a string");
432 fp->setIdent(naStr_data(value));
433 } else if (!strcmp(fieldName, "current")) {
434 int index = value.num;
435 if ((index < 0) || (index >= fp->numLegs())) {
438 fp->setCurrentIndex(index);
439 } else if (!strcmp(fieldName, "departure")) {
440 FGAirport* apt = airportGhost(value);
442 fp->setDeparture(apt);
446 FGRunway* rwy = runwayGhost(value);
448 fp->setDeparture(rwy);
452 naRuntimeError(c, "bad argument type setting departure");
453 } else if (!strcmp(fieldName, "destination")) {
454 FGAirport* apt = airportGhost(value);
456 fp->setDestination(apt);
460 FGRunway* rwy = runwayGhost(value);
462 fp->setDestination(rwy);
466 naRuntimeError(c, "bad argument type setting destination");
467 } else if (!strcmp(fieldName, "departure_runway")) {
468 FGRunway* rwy = runwayGhost(value);
470 fp->setDeparture(rwy);
474 naRuntimeError(c, "bad argument type setting departure");
475 } else if (!strcmp(fieldName, "destination_runway")) {
476 FGRunway* rwy = runwayGhost(value);
478 fp->setDestination(rwy);
482 naRuntimeError(c, "bad argument type setting departure");
483 } else if (!strcmp(fieldName, "sid")) {
484 Procedure* proc = procedureGhost(value);
485 if (proc && (proc->type() == PROCEDURE_SID)) {
486 fp->setSID((flightgear::SID*) proc);
489 // allow a SID transition to be set, implicitly include the SID itself
490 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
491 fp->setSID((Transition*) proc);
495 if (naIsString(value)) {
496 FGAirport* apt = fp->departureAirport();
497 fp->setSID(apt->findSIDWithIdent(naStr_data(value)));
501 naRuntimeError(c, "bad argument type setting SID");
502 } else if (!strcmp(fieldName, "star")) {
503 Procedure* proc = procedureGhost(value);
504 if (proc && (proc->type() == PROCEDURE_STAR)) {
505 fp->setSTAR((STAR*) proc);
509 if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
510 fp->setSTAR((Transition*) proc);
514 if (naIsString(value)) {
515 FGAirport* apt = fp->destinationAirport();
516 fp->setSTAR(apt->findSTARWithIdent(naStr_data(value)));
520 naRuntimeError(c, "bad argument type setting STAR");
521 } else if (!strcmp(fieldName, "approach")) {
522 Procedure* proc = procedureGhost(value);
523 if (proc && Approach::isApproach(proc->type())) {
524 fp->setApproach((Approach*) proc);
528 if (naIsString(value)) {
529 FGAirport* apt = fp->destinationAirport();
530 fp->setApproach(apt->findApproachWithIdent(naStr_data(value)));
534 naRuntimeError(c, "bad argument type setting approach");
539 static naRef procedureTpType(naContext c, ProcedureType ty)
542 case PROCEDURE_SID: return stringToNasal(c, "sid");
543 case PROCEDURE_STAR: return stringToNasal(c, "star");
544 case PROCEDURE_APPROACH_VOR:
545 case PROCEDURE_APPROACH_ILS:
546 case PROCEDURE_APPROACH_RNAV:
547 case PROCEDURE_APPROACH_NDB:
548 return stringToNasal(c, "IAP");
554 static naRef procedureRadioType(naContext c, ProcedureType ty)
557 case PROCEDURE_APPROACH_VOR: return stringToNasal(c, "VOR");
558 case PROCEDURE_APPROACH_ILS: return stringToNasal(c, "ILS");
559 case PROCEDURE_APPROACH_RNAV: return stringToNasal(c, "RNAV");
560 case PROCEDURE_APPROACH_NDB: return stringToNasal(c, "NDB");
566 static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out)
568 const char* fieldName = naStr_data(field);
569 Procedure* proc = (Procedure*) g;
571 if (!strcmp(fieldName, "parents")) {
572 *out = naNewVector(c);
573 naVec_append(*out, procedurePrototype);
574 } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, proc->ident());
575 else if (!strcmp(fieldName, "airport")) *out = ghostForAirport(c, proc->airport());
576 else if (!strcmp(fieldName, "tp_type")) *out = procedureTpType(c, proc->type());
577 else if (!strcmp(fieldName, "radio")) *out = procedureRadioType(c, proc->type());
578 else if (!strcmp(fieldName, "runways")) {
579 *out = naNewVector(c);
580 BOOST_FOREACH(FGRunwayPtr rwy, proc->runways()) {
581 naVec_append(*out, stringToNasal(c, rwy->ident()));
583 } else if (!strcmp(fieldName, "transitions")) {
584 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
589 ArrivalDeparture* ad = static_cast<ArrivalDeparture*>(proc);
590 *out = naNewVector(c);
591 BOOST_FOREACH(string id, ad->transitionIdents()) {
592 naVec_append(*out, stringToNasal(c, id));
601 static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
603 const char* fieldName = naStr_data(field);
604 FGRunway* rwy = (FGRunway*) g;
606 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, rwy->ident());
607 else if (!strcmp(fieldName, "lat")) *out = naNum(rwy->latitude());
608 else if (!strcmp(fieldName, "lon")) *out = naNum(rwy->longitude());
609 else if (!strcmp(fieldName, "heading")) *out = naNum(rwy->headingDeg());
610 else if (!strcmp(fieldName, "length")) *out = naNum(rwy->lengthM());
611 else if (!strcmp(fieldName, "width")) *out = naNum(rwy->widthM());
612 else if (!strcmp(fieldName, "threshold")) *out = naNum(rwy->displacedThresholdM());
613 else if (!strcmp(fieldName, "stopway")) *out = naNum(rwy->stopwayM());
614 else if (!strcmp(fieldName, "ils_frequency_mhz")) {
615 *out = rwy->ILS() ? naNum(rwy->ILS()->get_freq() / 100.0) : naNil();
616 } else if (!strcmp(fieldName, "ils")) {
617 *out = ghostForNavaid(c, rwy->ILS());
625 static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out)
627 const char* fieldName = naStr_data(field);
628 FGNavRecord* nav = (FGNavRecord*) g;
630 if (!strcmp(fieldName, "id")) *out = stringToNasal(c, nav->ident());
631 else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, nav->name());
632 else if (!strcmp(fieldName, "lat")) *out = naNum(nav->get_lat());
633 else if (!strcmp(fieldName, "lon")) *out = naNum(nav->get_lon());
634 else if (!strcmp(fieldName, "elevation")) {
635 *out = naNum(nav->get_elev_ft() * SG_FEET_TO_METER);
636 } else if (!strcmp(fieldName, "type")) {
637 *out = stringToNasal(c, nav->nameForType(nav->type()));
638 } else if (!strcmp(fieldName, "frequency")) {
639 *out = naNum(nav->get_freq());
640 } else if (!strcmp(fieldName, "range_nm")) {
641 *out = naNum(nav->get_range());
642 } else if (!strcmp(fieldName, "course")) {
643 if ((nav->type() == FGPositioned::ILS) || (nav->type() == FGPositioned::LOC)) {
644 double radial = nav->get_multiuse();
645 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
646 *out = naNum(radial);
657 static bool hashIsCoord(naRef h)
659 naRef parents = naHash_cget(h, (char*) "parents");
660 if (!naIsVector(parents)) {
664 return naEqual(naVec_get(parents, 0), geoCoordClass);
667 bool geodFromHash(naRef ref, SGGeod& result)
669 if (!naIsHash(ref)) {
673 // first, see if the hash contains a FGPositioned ghost - in which case
674 // we can read off its position directly
675 naRef posGhost = naHash_cget(ref, (char*) "_positioned");
676 if (!naIsNil(posGhost)) {
677 FGPositioned* pos = positionedGhost(posGhost);
678 result = pos->geod();
682 // then check for manual latitude / longitude names
683 naRef lat = naHash_cget(ref, (char*) "lat");
684 naRef lon = naHash_cget(ref, (char*) "lon");
685 if (naIsNum(lat) && naIsNum(lon)) {
686 result = SGGeod::fromDeg(naNumValue(lon).num, naNumValue(lat).num);
690 if (hashIsCoord(ref)) {
691 naRef lat = naHash_cget(ref, (char*) "_lat");
692 naRef lon = naHash_cget(ref, (char*) "_lon");
693 if (naIsNum(lat) && naIsNum(lon)) {
694 result = SGGeod::fromRad(naNumValue(lon).num, naNumValue(lat).num);
699 // check for any synonyms?
700 // latitude + longitude?
705 static int geodFromArgs(naRef* args, int offset, int argc, SGGeod& result)
707 if (offset >= argc) {
711 if (naIsGhost(args[offset])) {
712 naGhostType* gt = naGhost_type(args[offset]);
713 if (gt == &AirportGhostType) {
714 result = airportGhost(args[offset])->geod();
718 if (gt == &NavaidGhostType) {
719 result = navaidGhost(args[offset])->geod();
723 if (gt == &RunwayGhostType) {
724 result = runwayGhost(args[offset])->geod();
728 if (gt == &WayptGhostType) {
729 result = wayptGhost(args[offset])->position();
734 if (geodFromHash(args[offset], result)) {
738 if (((argc - offset) >= 2) && naIsNum(args[offset]) && naIsNum(args[offset + 1])) {
739 double lat = naNumValue(args[0]).num,
740 lon = naNumValue(args[1]).num;
741 result = SGGeod::fromDeg(lon, lat);
748 // Convert a cartesian point to a geodetic lat/lon/altitude.
749 static naRef f_carttogeod(naContext c, naRef me, int argc, naRef* args)
751 double lat, lon, alt, xyz[3];
752 if(argc != 3) naRuntimeError(c, "carttogeod() expects 3 arguments");
753 for(int i=0; i<3; i++)
754 xyz[i] = naNumValue(args[i]).num;
755 sgCartToGeod(xyz, &lat, &lon, &alt);
756 lat *= SG_RADIANS_TO_DEGREES;
757 lon *= SG_RADIANS_TO_DEGREES;
758 naRef vec = naNewVector(c);
759 naVec_append(vec, naNum(lat));
760 naVec_append(vec, naNum(lon));
761 naVec_append(vec, naNum(alt));
765 // Convert a geodetic lat/lon/altitude to a cartesian point.
766 static naRef f_geodtocart(naContext c, naRef me, int argc, naRef* args)
768 if(argc != 3) naRuntimeError(c, "geodtocart() expects 3 arguments");
769 double lat = naNumValue(args[0]).num * SG_DEGREES_TO_RADIANS;
770 double lon = naNumValue(args[1]).num * SG_DEGREES_TO_RADIANS;
771 double alt = naNumValue(args[2]).num;
773 sgGeodToCart(lat, lon, alt, xyz);
774 naRef vec = naNewVector(c);
775 naVec_append(vec, naNum(xyz[0]));
776 naVec_append(vec, naNum(xyz[1]));
777 naVec_append(vec, naNum(xyz[2]));
781 // For given geodetic point return array with elevation, and a material data
782 // hash, or nil if there's no information available (tile not loaded). If
783 // information about the material isn't available, then nil is returned instead
785 static naRef f_geodinfo(naContext c, naRef me, int argc, naRef* args)
787 #define HASHSET(s,l,n) naHash_set(matdata, naStr_fromdata(naNewString(c),s,l),n)
788 if(argc < 2 || argc > 3)
789 naRuntimeError(c, "geodinfo() expects 2 or 3 arguments: lat, lon [, maxalt]");
790 double lat = naNumValue(args[0]).num;
791 double lon = naNumValue(args[1]).num;
792 double elev = argc == 3 ? naNumValue(args[2]).num : 10000;
793 const SGMaterial *mat;
794 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
795 if(!globals->get_scenery()->get_elevation_m(geod, elev, &mat))
797 naRef vec = naNewVector(c);
798 naVec_append(vec, naNum(elev));
799 naRef matdata = naNil();
801 matdata = naNewHash(c);
802 naRef names = naNewVector(c);
803 BOOST_FOREACH(const std::string& n, mat->get_names())
804 naVec_append(names, stringToNasal(c, n));
806 HASHSET("names", 5, names);
807 HASHSET("solid", 5, naNum(mat->get_solid()));
808 HASHSET("friction_factor", 15, naNum(mat->get_friction_factor()));
809 HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction()));
810 HASHSET("load_resistance", 15, naNum(mat->get_load_resistance()));
811 HASHSET("bumpiness", 9, naNum(mat->get_bumpiness()));
812 HASHSET("light_coverage", 14, naNum(mat->get_light_coverage()));
814 naVec_append(vec, matdata);
820 class AirportInfoFilter : public FGAirport::AirportFilter
823 AirportInfoFilter() : type(FGPositioned::AIRPORT) {
826 bool fromArg(naRef arg)
828 const char *s = naStr_data(arg);
829 if(!strcmp(s, "airport")) type = FGPositioned::AIRPORT;
830 else if(!strcmp(s, "seaport")) type = FGPositioned::SEAPORT;
831 else if(!strcmp(s, "heliport")) type = FGPositioned::HELIPORT;
838 virtual FGPositioned::Type minType() const {
842 virtual FGPositioned::Type maxType() const {
846 FGPositioned::Type type;
849 // Returns data hash for particular or nearest airport of a <type>, or nil
852 // airportinfo(<id>); e.g. "KSFO"
853 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
854 // airportinfo() same as airportinfo("airport")
855 // airportinfo(<lat>, <lon> [, <type>]);
856 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
858 SGGeod pos = globals->get_aircraft_position();
859 FGAirport* apt = NULL;
861 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
862 pos = SGGeod::fromDeg(args[1].num, args[0].num);
867 double maxRange = 10000.0; // expose this? or pick a smaller value?
869 AirportInfoFilter filter; // defaults to airports only
872 // fall through and use AIRPORT
873 } else if(argc == 1 && naIsString(args[0])) {
874 if (filter.fromArg(args[0])) {
877 // user provided an <id>, hopefully
878 apt = FGAirport::findByIdent(naStr_data(args[0]));
880 // return nil here, but don't raise a runtime error; this is a
881 // legitamate way to validate an ICAO code, for example in a
882 // dialog box or similar.
887 naRuntimeError(c, "airportinfo() with invalid function arguments");
892 apt = FGAirport::findClosest(pos, maxRange, &filter);
893 if(!apt) return naNil();
896 return ghostForAirport(c, apt);
899 static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
902 SGGeod pos = globals->get_aircraft_position();
903 argOffset += geodFromArgs(args, 0, argc, pos);
905 if (!naIsNum(args[argOffset])) {
906 naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset);
909 AirportInfoFilter filter; // defaults to airports only
910 double rangeNm = args[argOffset++].num;
911 if (argOffset < argc) {
912 filter.fromArg(args[argOffset++]);
915 naRef r = naNewVector(c);
917 FGPositioned::List apts = FGPositioned::findWithinRange(pos, rangeNm, &filter);
918 FGPositioned::sortByRange(apts, pos);
920 BOOST_FOREACH(FGPositionedRef a, apts) {
921 FGAirport* apt = (FGAirport*) a.get();
922 naVec_append(r, ghostForAirport(c, apt));
928 static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
930 if (!naIsString(args[0])) {
931 naRuntimeError(c, "findAirportsByICAO expects string as arg 0");
935 string prefix(naStr_data(args[argOffset++]));
936 AirportInfoFilter filter; // defaults to airports only
937 if (argOffset < argc) {
938 filter.fromArg(args[argOffset++]);
941 naRef r = naNewVector(c);
943 FGPositioned::List apts = FGPositioned::findAllWithIdent(prefix, &filter, false);
945 BOOST_FOREACH(FGPositionedRef a, apts) {
946 FGAirport* apt = (FGAirport*) a.get();
947 naVec_append(r, ghostForAirport(c, apt));
953 static naRef f_airport_tower(naContext c, naRef me, int argc, naRef* args)
955 FGAirport* apt = airportGhost(me);
957 naRuntimeError(c, "airport.tower called on non-airport object");
960 // build a hash for the tower position
961 SGGeod towerLoc = apt->getTowerLocation();
962 naRef tower = naNewHash(c);
963 hashset(c, tower, "lat", naNum(towerLoc.getLatitudeDeg()));
964 hashset(c, tower, "lon", naNum(towerLoc.getLongitudeDeg()));
965 hashset(c, tower, "elevation", naNum(towerLoc.getElevationM()));
969 static naRef f_airport_comms(naContext c, naRef me, int argc, naRef* args)
971 FGAirport* apt = airportGhost(me);
973 naRuntimeError(c, "airport.comms called on non-airport object");
975 naRef comms = naNewVector(c);
977 // if we have an explicit type, return a simple vector of frequencies
978 if (argc > 0 && naIsScalar(args[0])) {
979 std::string commName = naStr_data(args[0]);
980 FGPositioned::Type commType = FGPositioned::typeFromName(commName);
982 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStationsOfType(commType)) {
983 naVec_append(comms, naNum(comm->freqMHz()));
986 // otherwise return a vector of hashes, one for each comm station.
987 BOOST_FOREACH(flightgear::CommStation* comm, apt->commStations()) {
988 naRef commHash = naNewHash(c);
989 hashset(c, commHash, "frequency", naNum(comm->freqMHz()));
990 hashset(c, commHash, "ident", stringToNasal(c, comm->ident()));
991 naVec_append(comms, commHash);
998 static naRef f_airport_runway(naContext c, naRef me, int argc, naRef* args)
1000 FGAirport* apt = airportGhost(me);
1002 naRuntimeError(c, "airport.runway called on non-airport object");
1005 if ((argc < 1) || !naIsString(args[0])) {
1006 naRuntimeError(c, "airport.runway expects a runway ident argument");
1009 std::string ident(naStr_data(args[0]));
1010 boost::to_upper(ident);
1011 if (!apt->hasRunwayWithIdent(ident)) {
1015 return ghostForRunway(c, apt->getRunwayByIdent(ident));
1018 static naRef f_airport_sids(naContext c, naRef me, int argc, naRef* args)
1020 FGAirport* apt = airportGhost(me);
1022 naRuntimeError(c, "airport.sids called on non-airport object");
1025 naRef sids = naNewVector(c);
1027 FGRunway* rwy = NULL;
1028 if (argc > 0 && naIsString(args[0])) {
1029 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1033 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1034 } else if (argc > 0) {
1035 rwy = runwayGhost(args[0]);
1039 BOOST_FOREACH(flightgear::SID* sid, rwy->getSIDs()) {
1040 naRef procId = stringToNasal(c, sid->ident());
1041 naVec_append(sids, procId);
1044 for (unsigned int s=0; s<apt->numSIDs(); ++s) {
1045 flightgear::SID* sid = apt->getSIDByIndex(s);
1046 naRef procId = stringToNasal(c, sid->ident());
1047 naVec_append(sids, procId);
1054 static naRef f_airport_stars(naContext c, naRef me, int argc, naRef* args)
1056 FGAirport* apt = airportGhost(me);
1058 naRuntimeError(c, "airport.stars called on non-airport object");
1061 naRef stars = naNewVector(c);
1063 FGRunway* rwy = NULL;
1064 if (argc > 0 && naIsString(args[0])) {
1065 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1069 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1070 } else if (argc > 0) {
1071 rwy = runwayGhost(args[0]);
1075 BOOST_FOREACH(flightgear::STAR* s, rwy->getSTARs()) {
1076 naRef procId = stringToNasal(c, s->ident());
1077 naVec_append(stars, procId);
1080 for (unsigned int s=0; s<apt->numSTARs(); ++s) {
1081 flightgear::STAR* star = apt->getSTARByIndex(s);
1082 naRef procId = stringToNasal(c, star->ident());
1083 naVec_append(stars, procId);
1090 static naRef f_airport_approaches(naContext c, naRef me, int argc, naRef* args)
1092 FGAirport* apt = airportGhost(me);
1094 naRuntimeError(c, "airport.getApproachList called on non-airport object");
1097 naRef approaches = naNewVector(c);
1099 ProcedureType ty = PROCEDURE_INVALID;
1100 if ((argc > 1) && naIsString(args[1])) {
1101 std::string u(naStr_data(args[1]));
1103 if (u == "NDB") ty = PROCEDURE_APPROACH_NDB;
1104 if (u == "VOR") ty = PROCEDURE_APPROACH_VOR;
1105 if (u == "ILS") ty = PROCEDURE_APPROACH_ILS;
1106 if (u == "RNAV") ty = PROCEDURE_APPROACH_RNAV;
1109 FGRunway* rwy = NULL;
1110 if (argc > 0 && (rwy = runwayGhost(args[0]))) {
1112 } else if (argc > 0 && naIsString(args[0])) {
1113 if (!apt->hasRunwayWithIdent(naStr_data(args[0]))) {
1117 rwy = apt->getRunwayByIdent(naStr_data(args[0]));
1121 BOOST_FOREACH(Approach* s, rwy->getApproaches()) {
1122 if ((ty != PROCEDURE_INVALID) && (s->type() != ty)) {
1126 naRef procId = stringToNasal(c, s->ident());
1127 naVec_append(approaches, procId);
1130 // no runway specified, report them all
1131 for (unsigned int s=0; s<apt->numApproaches(); ++s) {
1132 Approach* app = apt->getApproachByIndex(s);
1133 if ((ty != PROCEDURE_INVALID) && (app->type() != ty)) {
1137 naRef procId = stringToNasal(c, app->ident());
1138 naVec_append(approaches, procId);
1145 static naRef f_airport_parking(naContext c, naRef me, int argc, naRef* args)
1147 FGAirport* apt = airportGhost(me);
1149 naRuntimeError(c, "airport.parking called on non-airport object");
1152 naRef r = naNewVector(c);
1154 bool onlyAvailable = false;
1156 if (argc > 0 && naIsString(args[0])) {
1157 type = naStr_data(args[0]);
1160 if ((argc > 1) && naIsNum(args[1])) {
1161 onlyAvailable = (args[1].num != 0.0);
1164 FGAirportDynamics* dynamics = apt->getDynamics();
1165 for (int i=0; i<dynamics->getNrOfParkings(); ++i) {
1166 FGParking* park = dynamics->getParking(i);
1167 // filter out based on availability and type
1168 if (onlyAvailable && !park->isAvailable()) {
1172 if (!type.empty() && (park->getType() != type)) {
1176 naRef nm = stringToNasal(c, park->getName());
1177 naVec_append(r, nm);
1183 static naRef f_airport_getSid(naContext c, naRef me, int argc, naRef* args)
1185 FGAirport* apt = airportGhost(me);
1187 naRuntimeError(c, "airport.getSid called on non-airport object");
1190 if ((argc != 1) || !naIsString(args[0])) {
1191 naRuntimeError(c, "airport.getSid passed invalid argument");
1194 string ident = naStr_data(args[0]);
1195 return ghostForProcedure(c, apt->findSIDWithIdent(ident));
1198 static naRef f_airport_getStar(naContext c, naRef me, int argc, naRef* args)
1200 FGAirport* apt = airportGhost(me);
1202 naRuntimeError(c, "airport.getStar called on non-airport object");
1205 if ((argc != 1) || !naIsString(args[0])) {
1206 naRuntimeError(c, "airport.getStar passed invalid argument");
1209 string ident = naStr_data(args[0]);
1210 return ghostForProcedure(c, apt->findSTARWithIdent(ident));
1213 static naRef f_airport_getApproach(naContext c, naRef me, int argc, naRef* args)
1215 FGAirport* apt = airportGhost(me);
1217 naRuntimeError(c, "airport.getIAP called on non-airport object");
1220 if ((argc != 1) || !naIsString(args[0])) {
1221 naRuntimeError(c, "airport.getIAP passed invalid argument");
1224 string ident = naStr_data(args[0]);
1225 return ghostForProcedure(c, apt->findApproachWithIdent(ident));
1228 // Returns vector of data hash for navaid of a <type>, nil on error
1229 // navaids sorted by ascending distance
1230 // navinfo([<lat>,<lon>],[<type>],[<id>])
1231 // lat/lon (numeric): use latitude/longitude instead of ac position
1232 // type: ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
1233 // id: (partial) id of the fix
1235 // navinfo("vor") returns all vors
1236 // navinfo("HAM") return all navaids who's name start with "HAM"
1237 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
1238 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
1239 // sorted by distance relative to lat=34, lon=48
1240 static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
1244 if(argc >= 2 && naIsNum(args[0]) && naIsNum(args[1])) {
1245 pos = SGGeod::fromDeg(args[1].num, args[0].num);
1249 pos = globals->get_aircraft_position();
1252 FGPositioned::Type type = FGPositioned::INVALID;
1253 nav_list_type navlist;
1254 const char * id = "";
1256 if(argc > 0 && naIsString(args[0])) {
1257 const char *s = naStr_data(args[0]);
1258 if(!strcmp(s, "any")) type = FGPositioned::INVALID;
1259 else if(!strcmp(s, "fix")) type = FGPositioned::FIX;
1260 else if(!strcmp(s, "vor")) type = FGPositioned::VOR;
1261 else if(!strcmp(s, "ndb")) type = FGPositioned::NDB;
1262 else if(!strcmp(s, "ils")) type = FGPositioned::ILS;
1263 else if(!strcmp(s, "dme")) type = FGPositioned::DME;
1264 else if(!strcmp(s, "tacan")) type = FGPositioned::TACAN;
1265 else id = s; // this is an id
1270 if(argc > 0 && naIsString(args[0])) {
1272 naRuntimeError(c, "navinfo() called with navaid id");
1275 id = naStr_data(args[0]);
1281 naRuntimeError(c, "navinfo() called with too many arguments");
1285 navlist = globals->get_navlist()->findByIdentAndFreq( pos, id, 0.0, type );
1287 naRef reply = naNewVector(c);
1288 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
1289 naVec_append( reply, ghostForNavaid(c, *it) );
1294 static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
1297 SGGeod pos = globals->get_aircraft_position();
1298 argOffset += geodFromArgs(args, 0, argc, pos);
1300 if (!naIsNum(args[argOffset])) {
1301 naRuntimeError(c, "findNavaidsWithinRange expected range (in nm) as arg %d", argOffset);
1304 FGPositioned::Type type = FGPositioned::INVALID;
1305 double rangeNm = args[argOffset++].num;
1306 if (argOffset < argc) {
1307 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1310 naRef r = naNewVector(c);
1311 FGNavList::TypeFilter filter(type);
1312 FGPositioned::List navs = FGPositioned::findWithinRange(pos, rangeNm, &filter);
1313 FGPositioned::sortByRange(navs, pos);
1315 BOOST_FOREACH(FGPositionedRef a, navs) {
1316 FGNavRecord* nav = (FGNavRecord*) a.get();
1317 naVec_append(r, ghostForNavaid(c, nav));
1323 static naRef f_findNavaidByFrequency(naContext c, naRef me, int argc, naRef* args)
1326 SGGeod pos = globals->get_aircraft_position();
1327 argOffset += geodFromArgs(args, 0, argc, pos);
1329 if (!naIsNum(args[argOffset])) {
1330 naRuntimeError(c, "findNavaidByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1333 FGPositioned::Type type = FGPositioned::INVALID;
1334 double freqMhz = args[argOffset++].num;
1335 if (argOffset < argc) {
1336 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1339 nav_list_type navs = globals->get_navlist()->findAllByFreq(freqMhz, pos, type);
1344 return ghostForNavaid(c, navs.front().ptr());
1347 static naRef f_findNavaidsByFrequency(naContext c, naRef me, int argc, naRef* args)
1350 SGGeod pos = globals->get_aircraft_position();
1351 argOffset += geodFromArgs(args, 0, argc, pos);
1353 if (!naIsNum(args[argOffset])) {
1354 naRuntimeError(c, "findNavaidsByFrequency expectes frequency (in Mhz) as arg %d", argOffset);
1357 FGPositioned::Type type = FGPositioned::INVALID;
1358 double freqMhz = args[argOffset++].num;
1359 if (argOffset < argc) {
1360 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1363 naRef r = naNewVector(c);
1364 nav_list_type navs = globals->get_navlist()->findAllByFreq(freqMhz, pos, type);
1366 BOOST_FOREACH(nav_rec_ptr a, navs) {
1367 naVec_append(r, ghostForNavaid(c, a.ptr()));
1373 static naRef f_findNavaidsByIdent(naContext c, naRef me, int argc, naRef* args)
1376 SGGeod pos = globals->get_aircraft_position();
1377 argOffset += geodFromArgs(args, 0, argc, pos);
1379 if (!naIsString(args[argOffset])) {
1380 naRuntimeError(c, "findNavaidsByIdent expectes ident string as arg %d", argOffset);
1383 FGPositioned::Type type = FGPositioned::INVALID;
1384 string ident = naStr_data(args[argOffset++]);
1385 if (argOffset < argc) {
1386 type = FGPositioned::typeFromName(naStr_data(args[argOffset]));
1389 naRef r = naNewVector(c);
1390 nav_list_type navs = globals->get_navlist()->findByIdentAndFreq(pos, ident, 0.0, type);
1392 BOOST_FOREACH(nav_rec_ptr a, navs) {
1393 naVec_append(r, ghostForNavaid(c, a.ptr()));
1400 // Convert a cartesian point to a geodetic lat/lon/altitude.
1401 static naRef f_magvar(naContext c, naRef me, int argc, naRef* args)
1403 SGGeod pos = globals->get_aircraft_position();
1405 // fine, use aircraft position
1406 } else if (geodFromArgs(args, 0, argc, pos)) {
1409 naRuntimeError(c, "magvar() expects no arguments, a positioned hash or lat,lon pair");
1412 double jd = globals->get_time_params()->getJD();
1413 double magvarDeg = sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
1414 return naNum(magvarDeg);
1417 static naRef f_courseAndDistance(naContext c, naRef me, int argc, naRef* args)
1419 SGGeod from = globals->get_aircraft_position(), to, p;
1420 int argOffset = geodFromArgs(args, 0, argc, p);
1421 if (geodFromArgs(args, argOffset, argc, to)) {
1422 from = p; // we parsed both FROM and TO args, so first was from
1424 to = p; // only parsed one arg, so FROM is current
1427 if (argOffset == 0) {
1428 naRuntimeError(c, "invalid arguments to courseAndDistance");
1431 double course, course2, d;
1432 SGGeodesy::inverse(from, to, course, course2, d);
1434 naRef result = naNewVector(c);
1435 naVec_append(result, naNum(course));
1436 naVec_append(result, naNum(d * SG_METER_TO_NM));
1440 static naRef f_greatCircleMove(naContext c, naRef me, int argc, naRef* args)
1442 SGGeod from = globals->get_aircraft_position(), to;
1445 // complication - don't inerpret two doubles (as the only args)
1446 // as a lat,lon pair - only do so if we have at least three args.
1448 argOffset = geodFromArgs(args, 0, argc, from);
1451 if ((argOffset + 1) >= argc) {
1452 naRuntimeError(c, "isufficent arguments to greatCircleMove");
1455 if (!naIsNum(args[argOffset]) || !naIsNum(args[argOffset+1])) {
1456 naRuntimeError(c, "invalid arguments %d and %d to greatCircleMove",
1457 argOffset, argOffset + 1);
1460 double course = args[argOffset].num, course2;
1461 double distanceNm = args[argOffset + 1].num;
1462 SGGeodesy::direct(from, course, distanceNm * SG_NM_TO_METER, to, course2);
1465 naRef coord = naNewHash(c);
1466 hashset(c, coord, "lat", naNum(to.getLatitudeDeg()));
1467 hashset(c, coord, "lon", naNum(to.getLongitudeDeg()));
1471 static naRef f_tilePath(naContext c, naRef me, int argc, naRef* args)
1473 SGGeod pos = globals->get_aircraft_position();
1474 geodFromArgs(args, 0, argc, pos);
1476 return stringToNasal(c, b.gen_base_path());
1479 static naRef f_tileIndex(naContext c, naRef me, int argc, naRef* args)
1481 SGGeod pos = globals->get_aircraft_position();
1482 geodFromArgs(args, 0, argc, pos);
1484 return naNum(b.gen_index());
1487 static naRef f_route(naContext c, naRef me, int argc, naRef* args)
1490 FGRouteMgr* rm = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
1491 return ghostForFlightPlan(c, rm->flightPlan());
1494 if ((argc > 0) && naIsString(args[0])) {
1495 flightgear::FlightPlan* fp = new flightgear::FlightPlan;
1496 SGPath path(naStr_data(args[0]));
1497 if (!path.exists()) {
1498 naRuntimeError(c, "flightplan, no file at path %s", path.c_str());
1501 if (!fp->load(path)) {
1502 SG_LOG(SG_NASAL, SG_WARN, "failed to load flight-plan from " << path);
1507 return ghostForFlightPlan(c, fp);
1510 naRuntimeError(c, "bad arguments to flightplan()");
1514 static naRef f_flightplan_getWP(naContext c, naRef me, int argc, naRef* args)
1516 FlightPlan* fp = flightplanGhost(me);
1518 naRuntimeError(c, "flightplan.getWP called on non-flightplan object");
1523 index = fp->currentIndex();
1525 index = (int) naNumValue(args[0]).num;
1528 if ((index < 0) || (index >= fp->numLegs())) {
1532 return ghostForLeg(c, fp->legAtIndex(index));
1535 static naRef f_flightplan_currentWP(naContext c, naRef me, int argc, naRef* args)
1537 FlightPlan* fp = flightplanGhost(me);
1539 naRuntimeError(c, "flightplan.currentWP called on non-flightplan object");
1541 return ghostForLeg(c, fp->currentLeg());
1544 static naRef f_flightplan_nextWP(naContext c, naRef me, int argc, naRef* args)
1546 FlightPlan* fp = flightplanGhost(me);
1548 naRuntimeError(c, "flightplan.nextWP called on non-flightplan object");
1550 return ghostForLeg(c, fp->nextLeg());
1553 static naRef f_flightplan_numWaypoints(naContext c, naRef me, int argc, naRef* args)
1555 FlightPlan* fp = flightplanGhost(me);
1557 naRuntimeError(c, "flightplan.numWaypoints called on non-flightplan object");
1559 return naNum(fp->numLegs());
1562 static naRef f_flightplan_appendWP(naContext c, naRef me, int argc, naRef* args)
1564 FlightPlan* fp = flightplanGhost(me);
1566 naRuntimeError(c, "flightplan.appendWP called on non-flightplan object");
1569 WayptRef wp = wayptGhost(args[0]);
1570 int index = fp->numLegs();
1571 fp->insertWayptAtIndex(wp.get(), index);
1572 return naNum(index);
1575 static naRef f_flightplan_insertWP(naContext c, naRef me, int argc, naRef* args)
1577 FlightPlan* fp = flightplanGhost(me);
1579 naRuntimeError(c, "flightplan.insertWP called on non-flightplan object");
1582 WayptRef wp = wayptGhost(args[0]);
1583 int index = -1; // append
1584 if ((argc > 1) && naIsNum(args[1])) {
1585 index = (int) args[1].num;
1588 fp->insertWayptAtIndex(wp.get(), index);
1592 static naRef f_flightplan_insertWPAfter(naContext c, naRef me, int argc, naRef* args)
1594 FlightPlan* fp = flightplanGhost(me);
1596 naRuntimeError(c, "flightplan.insertWPAfter called on non-flightplan object");
1599 WayptRef wp = wayptGhost(args[0]);
1600 int index = -1; // append
1601 if ((argc > 1) && naIsNum(args[1])) {
1602 index = (int) args[1].num;
1605 fp->insertWayptAtIndex(wp.get(), index + 1);
1609 static naRef f_flightplan_insertWaypoints(naContext c, naRef me, int argc, naRef* args)
1611 FlightPlan* fp = flightplanGhost(me);
1613 naRuntimeError(c, "flightplan.insertWaypoints called on non-flightplan object");
1617 if (!naIsVector(args[0])) {
1618 naRuntimeError(c, "flightplan.insertWaypoints expects vector as first arg");
1621 int count = naVec_size(args[0]);
1622 for (int i=0; i<count; ++i) {
1623 Waypt* wp = wayptGhost(naVec_get(args[0], i));
1629 int index = -1; // append
1630 if ((argc > 1) && naIsNum(args[1])) {
1631 index = (int) args[1].num;
1634 fp->insertWayptsAtIndex(wps, index);
1638 static naRef f_flightplan_clearPlan(naContext c, naRef me, int argc, naRef* args)
1640 FlightPlan* fp = flightplanGhost(me);
1642 naRuntimeError(c, "flightplan.clearPlan called on non-flightplan object");
1649 static WayptFlag wayptFlagFromString(const char* s)
1651 if (!strcmp(s, "sid")) return WPT_DEPARTURE;
1652 if (!strcmp(s, "star")) return WPT_ARRIVAL;
1653 if (!strcmp(s, "approach")) return WPT_APPROACH;
1654 if (!strcmp(s, "missed")) return WPT_MISS;
1655 if (!strcmp(s, "pseudo")) return WPT_PSEUDO;
1657 return (WayptFlag) 0;
1660 static naRef f_flightplan_clearWPType(naContext c, naRef me, int argc, naRef* args)
1662 FlightPlan* fp = flightplanGhost(me);
1664 naRuntimeError(c, "flightplan.clearWPType called on non-flightplan object");
1668 naRuntimeError(c, "insufficent args to flightplan.clearWPType");
1671 WayptFlag flag = wayptFlagFromString(naStr_data(args[0]));
1672 fp->clearWayptsWithFlag(flag);
1676 static naRef f_flightplan_clone(naContext c, naRef me, int argc, naRef* args)
1678 FlightPlan* fp = flightplanGhost(me);
1680 naRuntimeError(c, "flightplan.clone called on non-flightplan object");
1683 return ghostForFlightPlan(c, fp->clone());
1686 static naRef f_leg_setSpeed(naContext c, naRef me, int argc, naRef* args)
1688 FlightPlan::Leg* leg = fpLegGhost(me);
1690 naRuntimeError(c, "leg.setSpeed called on non-flightplan-leg object");
1694 naRuntimeError(c, "bad arguments to leg.setSpeed");
1697 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
1698 leg->setSpeed(rr, args[0].num);
1702 static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
1704 FlightPlan::Leg* leg = fpLegGhost(me);
1706 naRuntimeError(c, "leg.setAltitude called on non-flightplan-leg object");
1710 naRuntimeError(c, "bad arguments to leg.setAltitude");
1713 RouteRestriction rr = routeRestrictionFromString(naStr_data(args[1]));
1714 leg->setAltitude(rr, args[0].num);
1718 static naRef f_waypoint_navaid(naContext c, naRef me, int argc, naRef* args)
1720 flightgear::Waypt* w = wayptGhost(me);
1722 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
1725 FGPositioned* pos = w->source();
1730 switch (pos->type()) {
1731 case FGPositioned::VOR:
1732 case FGPositioned::NDB:
1733 case FGPositioned::ILS:
1734 case FGPositioned::LOC:
1735 case FGPositioned::GS:
1736 case FGPositioned::DME:
1737 case FGPositioned::TACAN: {
1738 FGNavRecord* nav = (FGNavRecord*) pos;
1739 return ghostForNavaid(c, nav);
1747 static naRef f_waypoint_airport(naContext c, naRef me, int argc, naRef* args)
1749 flightgear::Waypt* w = wayptGhost(me);
1751 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
1754 FGPositioned* pos = w->source();
1755 if (!pos || FGAirport::isAirportType(pos)) {
1759 return ghostForAirport(c, (FGAirport*) pos);
1762 static naRef f_waypoint_runway(naContext c, naRef me, int argc, naRef* args)
1764 flightgear::Waypt* w = wayptGhost(me);
1766 naRuntimeError(c, "waypoint.navaid called on non-waypoint object");
1769 FGPositioned* pos = w->source();
1770 if (!pos || (pos->type() != FGPositioned::RUNWAY)) {
1774 return ghostForRunway(c, (FGRunway*) pos);
1777 static naRef f_procedure_transition(naContext c, naRef me, int argc, naRef* args)
1779 Procedure* proc = procedureGhost(me);
1781 naRuntimeError(c, "procedure.transition called on non-procedure object");
1784 if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
1785 naRuntimeError(c, "procedure.transition called on non-SID or -STAR");
1788 ArrivalDeparture* ad = (ArrivalDeparture*) proc;
1789 Transition* trans = ad->findTransitionByName(naStr_data(args[0]));
1791 return ghostForProcedure(c, trans);
1794 // Table of extension functions. Terminate with zeros.
1795 static struct { const char* name; naCFunction func; } funcs[] = {
1796 { "carttogeod", f_carttogeod },
1797 { "geodtocart", f_geodtocart },
1798 { "geodinfo", f_geodinfo },
1799 { "airportinfo", f_airportinfo },
1800 { "findAirportsWithinRange", f_findAirportsWithinRange },
1801 { "findAirportsByICAO", f_findAirportsByICAO },
1802 { "navinfo", f_navinfo },
1803 { "findNavaidsWithinRange", f_findNavaidsWithinRange },
1804 { "findNavaidByFrequency", f_findNavaidByFrequency },
1805 { "findNavaidsByFrequency", f_findNavaidsByFrequency },
1806 { "findNavaidsByID", f_findNavaidsByIdent },
1807 { "flightplan", f_route },
1808 { "magvar", f_magvar },
1809 { "courseAndDistance", f_courseAndDistance },
1810 { "greatCircleMove", f_greatCircleMove },
1811 { "tileIndex", f_tileIndex },
1812 { "tilePath", f_tilePath },
1817 naRef initNasalPositioned(naRef globals, naContext c, naRef gcSave)
1819 airportPrototype = naNewHash(c);
1820 hashset(c, gcSave, "airportProto", airportPrototype);
1822 hashset(c, airportPrototype, "runway", naNewFunc(c, naNewCCode(c, f_airport_runway)));
1823 hashset(c, airportPrototype, "tower", naNewFunc(c, naNewCCode(c, f_airport_tower)));
1824 hashset(c, airportPrototype, "comms", naNewFunc(c, naNewCCode(c, f_airport_comms)));
1825 hashset(c, airportPrototype, "sids", naNewFunc(c, naNewCCode(c, f_airport_sids)));
1826 hashset(c, airportPrototype, "stars", naNewFunc(c, naNewCCode(c, f_airport_stars)));
1827 hashset(c, airportPrototype, "getApproachList", naNewFunc(c, naNewCCode(c, f_airport_approaches)));
1828 hashset(c, airportPrototype, "parking", naNewFunc(c, naNewCCode(c, f_airport_parking)));
1829 hashset(c, airportPrototype, "getSid", naNewFunc(c, naNewCCode(c, f_airport_getSid)));
1830 hashset(c, airportPrototype, "getStar", naNewFunc(c, naNewCCode(c, f_airport_getStar)));
1831 hashset(c, airportPrototype, "getIAP", naNewFunc(c, naNewCCode(c, f_airport_getApproach)));
1833 flightplanPrototype = naNewHash(c);
1834 hashset(c, gcSave, "flightplanProto", flightplanPrototype);
1836 hashset(c, flightplanPrototype, "getWP", naNewFunc(c, naNewCCode(c, f_flightplan_getWP)));
1837 hashset(c, flightplanPrototype, "currentWP", naNewFunc(c, naNewCCode(c, f_flightplan_currentWP)));
1838 hashset(c, flightplanPrototype, "nextWP", naNewFunc(c, naNewCCode(c, f_flightplan_nextWP)));
1839 hashset(c, flightplanPrototype, "getPlanSize", naNewFunc(c, naNewCCode(c, f_flightplan_numWaypoints)));
1840 hashset(c, flightplanPrototype, "appendWP", naNewFunc(c, naNewCCode(c, f_flightplan_appendWP)));
1841 hashset(c, flightplanPrototype, "insertWP", naNewFunc(c, naNewCCode(c, f_flightplan_insertWP)));
1842 hashset(c, flightplanPrototype, "insertWPAfter", naNewFunc(c, naNewCCode(c, f_flightplan_insertWPAfter)));
1843 hashset(c, flightplanPrototype, "insertWaypoints", naNewFunc(c, naNewCCode(c, f_flightplan_insertWaypoints)));
1844 hashset(c, flightplanPrototype, "cleanPlan", naNewFunc(c, naNewCCode(c, f_flightplan_clearPlan)));
1845 hashset(c, flightplanPrototype, "clearWPType", naNewFunc(c, naNewCCode(c, f_flightplan_clearWPType)));
1846 hashset(c, flightplanPrototype, "clone", naNewFunc(c, naNewCCode(c, f_flightplan_clone)));
1848 waypointPrototype = naNewHash(c);
1849 hashset(c, gcSave, "wayptProto", waypointPrototype);
1851 hashset(c, waypointPrototype, "navaid", naNewFunc(c, naNewCCode(c, f_waypoint_navaid)));
1852 hashset(c, waypointPrototype, "runway", naNewFunc(c, naNewCCode(c, f_waypoint_runway)));
1853 hashset(c, waypointPrototype, "airport", naNewFunc(c, naNewCCode(c, f_waypoint_airport)));
1855 procedurePrototype = naNewHash(c);
1856 hashset(c, gcSave, "procedureProto", procedurePrototype);
1857 // hashset(c, procedurePrototype, "runwayTransition", naNewFunc(c, naNewCCode(c, f_procedure_runwayTransition)));
1858 hashset(c, procedurePrototype, "transition", naNewFunc(c, naNewCCode(c, f_procedure_transition)));
1859 // hashset(c, procedurePrototype, "buildPath", naNewFunc(c, naNewCCode(c, f_procedure_build)));
1861 fpLegPrototype = naNewHash(c);
1862 hashset(c, gcSave, "fpLegProto", fpLegPrototype);
1863 hashset(c, fpLegPrototype, "setSpeed", naNewFunc(c, naNewCCode(c, f_leg_setSpeed)));
1864 hashset(c, fpLegPrototype, "setAltitude", naNewFunc(c, naNewCCode(c, f_leg_setAltitude)));
1866 for(int i=0; funcs[i].name; i++) {
1867 hashset(c, globals, funcs[i].name,
1868 naNewFunc(c, naNewCCode(c, funcs[i].func)));
1874 void postinitNasalPositioned(naRef globals, naContext c)
1876 naRef geoModule = naHash_cget(globals, (char*) "geo");
1877 if (naIsNil(geoModule)) {
1878 SG_LOG(SG_GENERAL, SG_WARN, "postinitNasalPositioned: geo.nas not loaded");
1882 geoCoordClass = naHash_cget(geoModule, (char*) "Coord");