+ const char* fieldName = naStr_data(field);
+ FlightPlan* fp = (FlightPlan*) g;
+
+ if (!strcmp(fieldName, "id")) {
+ if (!naIsString(value)) naRuntimeError(c, "flightplan.id must be a string");
+ fp->setIdent(naStr_data(value));
+ } else if (!strcmp(fieldName, "current")) {
+ int index = value.num;
+ if ((index < 0) || (index >= fp->numLegs())) {
+ return;
+ }
+ fp->setCurrentIndex(index);
+ } else if (!strcmp(fieldName, "departure")) {
+ FGAirport* apt = airportGhost(value);
+ if (apt) {
+ fp->setDeparture(apt);
+ return;
+ }
+
+ FGRunway* rwy = runwayGhost(value);
+ if (rwy){
+ fp->setDeparture(rwy);
+ return;
+ }
+
+ if (naIsNil(value)) {
+ fp->setDeparture(static_cast<FGAirport*>(NULL));
+ return;
+ }
+
+ naRuntimeError(c, "bad argument type setting departure");
+ } else if (!strcmp(fieldName, "destination")) {
+ FGAirport* apt = airportGhost(value);
+ if (apt) {
+ fp->setDestination(apt);
+ return;
+ }
+
+ FGRunway* rwy = runwayGhost(value);
+ if (rwy){
+ fp->setDestination(rwy);
+ return;
+ }
+
+ naRuntimeError(c, "bad argument type setting destination");
+ } else if (!strcmp(fieldName, "departure_runway")) {
+ FGRunway* rwy = runwayGhost(value);
+ if (rwy){
+ fp->setDeparture(rwy);
+ return;
+ }
+
+ naRuntimeError(c, "bad argument type setting departure runway");
+ } else if (!strcmp(fieldName, "destination_runway")) {
+ FGRunway* rwy = runwayGhost(value);
+ if (rwy){
+ fp->setDestination(rwy);
+ return;
+ }
+
+ naRuntimeError(c, "bad argument type setting destination runway");
+ } else if (!strcmp(fieldName, "sid")) {
+ Procedure* proc = procedureGhost(value);
+ if (proc && (proc->type() == PROCEDURE_SID)) {
+ fp->setSID((flightgear::SID*) proc);
+ return;
+ }
+ // allow a SID transition to be set, implicitly include the SID itself
+ if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
+ fp->setSID((Transition*) proc);
+ return;
+ }
+
+ if (naIsString(value)) {
+ FGAirport* apt = fp->departureAirport();
+ fp->setSID(apt->findSIDWithIdent(naStr_data(value)));
+ return;
+ }
+
+ naRuntimeError(c, "bad argument type setting SID");
+ } else if (!strcmp(fieldName, "star")) {
+ Procedure* proc = procedureGhost(value);
+ if (proc && (proc->type() == PROCEDURE_STAR)) {
+ fp->setSTAR((STAR*) proc);
+ return;
+ }
+
+ if (proc && (proc->type() == PROCEDURE_TRANSITION)) {
+ fp->setSTAR((Transition*) proc);
+ return;
+ }
+
+ if (naIsString(value)) {
+ FGAirport* apt = fp->destinationAirport();
+ fp->setSTAR(apt->findSTARWithIdent(naStr_data(value)));
+ return;
+ }
+
+ naRuntimeError(c, "bad argument type setting STAR");
+ } else if (!strcmp(fieldName, "approach")) {
+ Procedure* proc = procedureGhost(value);
+ if (proc && Approach::isApproach(proc->type())) {
+ fp->setApproach((Approach*) proc);
+ return;
+ }
+
+ if (naIsString(value)) {
+ FGAirport* apt = fp->destinationAirport();
+ fp->setApproach(apt->findApproachWithIdent(naStr_data(value)));
+ return;
+ }
+
+ naRuntimeError(c, "bad argument type setting approach");
+ }
+}
+
+
+static naRef procedureTpType(naContext c, ProcedureType ty)
+{
+ switch (ty) {
+ case PROCEDURE_SID: return stringToNasal(c, "sid");
+ case PROCEDURE_STAR: return stringToNasal(c, "star");
+ case PROCEDURE_APPROACH_VOR:
+ case PROCEDURE_APPROACH_ILS:
+ case PROCEDURE_APPROACH_RNAV:
+ case PROCEDURE_APPROACH_NDB:
+ return stringToNasal(c, "IAP");
+ default:
+ return naNil();
+ }
+}
+
+static naRef procedureRadioType(naContext c, ProcedureType ty)
+{
+ switch (ty) {
+ case PROCEDURE_APPROACH_VOR: return stringToNasal(c, "VOR");
+ case PROCEDURE_APPROACH_ILS: return stringToNasal(c, "ILS");
+ case PROCEDURE_APPROACH_RNAV: return stringToNasal(c, "RNAV");
+ case PROCEDURE_APPROACH_NDB: return stringToNasal(c, "NDB");
+ default:
+ return naNil();
+ }
+}
+
+static const char* procedureGhostGetMember(naContext c, void* g, naRef field, naRef* out)
+{
+ const char* fieldName = naStr_data(field);
+ Procedure* proc = (Procedure*) g;
+
+ if (!strcmp(fieldName, "parents")) {
+ *out = naNewVector(c);
+ naVec_append(*out, procedurePrototype);
+ } else if (!strcmp(fieldName, "id")) *out = stringToNasal(c, proc->ident());
+ else if (!strcmp(fieldName, "airport")) *out = ghostForAirport(c, proc->airport());
+ else if (!strcmp(fieldName, "tp_type")) *out = procedureTpType(c, proc->type());
+ else if (!strcmp(fieldName, "radio")) *out = procedureRadioType(c, proc->type());
+ else if (!strcmp(fieldName, "runways")) {
+ *out = naNewVector(c);
+ BOOST_FOREACH(FGRunwayRef rwy, proc->runways()) {
+ naVec_append(*out, stringToNasal(c, rwy->ident()));
+ }
+ } else if (!strcmp(fieldName, "transitions")) {
+ if ((proc->type() != PROCEDURE_SID) && (proc->type() != PROCEDURE_STAR)) {
+ *out = naNil();
+ return "";
+ }
+
+ ArrivalDeparture* ad = static_cast<ArrivalDeparture*>(proc);
+ *out = naNewVector(c);
+ BOOST_FOREACH(std::string id, ad->transitionIdents()) {
+ naVec_append(*out, stringToNasal(c, id));
+ }
+ } else {
+ return 0;
+ }
+
+ return "";
+}
+
+static const char* runwayGhostGetMember(naContext c, void* g, naRef field, naRef* out)
+{
+ const char* fieldName = naStr_data(field);
+ FGRunwayBase* base = (FGRunwayBase*) g;
+
+ if (!strcmp(fieldName, "id")) *out = stringToNasal(c, base->ident());
+ else if (!strcmp(fieldName, "lat")) *out = naNum(base->latitude());
+ else if (!strcmp(fieldName, "lon")) *out = naNum(base->longitude());
+ else if (!strcmp(fieldName, "heading")) *out = naNum(base->headingDeg());
+ else if (!strcmp(fieldName, "length")) *out = naNum(base->lengthM());
+ else if (!strcmp(fieldName, "width")) *out = naNum(base->widthM());
+ else if (!strcmp(fieldName, "surface")) *out = naNum(base->surface());
+ else if (base->type() == FGRunwayBase::RUNWAY) {
+ FGRunway* rwy = (FGRunway*) g;
+ if (!strcmp(fieldName, "threshold")) *out = naNum(rwy->displacedThresholdM());
+ else if (!strcmp(fieldName, "stopway")) *out = naNum(rwy->stopwayM());
+ else if (!strcmp(fieldName, "reciprocal")) {
+ *out = ghostForRunway(c, rwy->reciprocalRunway());
+ } else if (!strcmp(fieldName, "ils_frequency_mhz")) {
+ *out = rwy->ILS() ? naNum(rwy->ILS()->get_freq() / 100.0) : naNil();
+ } else if (!strcmp(fieldName, "ils")) {
+ *out = ghostForNavaid(c, rwy->ILS());
+ } else {
+ return 0;
+ }
+ } else {
+ return 0;
+ }
+
+ return "";
+}
+
+static const char* navaidGhostGetMember(naContext c, void* g, naRef field, naRef* out)
+{
+ const char* fieldName = naStr_data(field);
+ FGNavRecord* nav = (FGNavRecord*) g;
+
+ if (!strcmp(fieldName, "id")) *out = stringToNasal(c, nav->ident());
+ else if (!strcmp(fieldName, "name")) *out = stringToNasal(c, nav->name());
+ else if (!strcmp(fieldName, "lat")) *out = naNum(nav->get_lat());
+ else if (!strcmp(fieldName, "lon")) *out = naNum(nav->get_lon());
+ else if (!strcmp(fieldName, "elevation")) {
+ *out = naNum(nav->get_elev_ft() * SG_FEET_TO_METER);
+ } else if (!strcmp(fieldName, "type")) {
+ *out = stringToNasal(c, nav->nameForType(nav->type()));
+ } else if (!strcmp(fieldName, "frequency")) {
+ *out = naNum(nav->get_freq());
+ } else if (!strcmp(fieldName, "range_nm")) {
+ *out = naNum(nav->get_range());
+ } else if (!strcmp(fieldName, "course")) {
+ if ((nav->type() == FGPositioned::ILS) || (nav->type() == FGPositioned::LOC)) {
+ double radial = nav->get_multiuse();
+ SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
+ *out = naNum(radial);
+ } else {
+ *out = naNil();
+ }
+ } else {
+ return 0;
+ }
+
+ return "";
+}
+
+static const char* fixGhostGetMember(naContext c, void* g, naRef field, naRef* out)
+{
+ const char* fieldName = naStr_data(field);
+ FGFix* fix = (FGFix*) g;
+
+ if (!strcmp(fieldName, "id")) *out = stringToNasal(c, fix->ident());
+ else if (!strcmp(fieldName, "lat")) *out = naNum(fix->get_lat());
+ else if (!strcmp(fieldName, "lon")) *out = naNum(fix->get_lon());
+ else {
+ return 0;
+ }
+
+ return "";
+}
+
+static bool hashIsCoord(naRef h)
+{
+ naRef parents = naHash_cget(h, (char*) "parents");
+ if (!naIsVector(parents)) {
+ return false;
+ }
+
+ return naEqual(naVec_get(parents, 0), geoCoordClass) != 0;
+}
+
+bool geodFromHash(naRef ref, SGGeod& result)
+{
+ if (!naIsHash(ref)) {
+ return false;
+ }
+
+
+// check for manual latitude / longitude names
+ naRef lat = naHash_cget(ref, (char*) "lat");
+ naRef lon = naHash_cget(ref, (char*) "lon");
+ if (naIsNum(lat) && naIsNum(lon)) {
+ result = SGGeod::fromDeg(naNumValue(lon).num, naNumValue(lat).num);
+ return true;
+ }
+
+ if (hashIsCoord(ref)) {
+ naRef lat = naHash_cget(ref, (char*) "_lat");
+ naRef lon = naHash_cget(ref, (char*) "_lon");
+ if (naIsNum(lat) && naIsNum(lon)) {
+ result = SGGeod::fromRad(naNumValue(lon).num, naNumValue(lat).num);
+ return true;
+ }
+ }
+
+// check for any synonyms?
+ // latitude + longitude?
+
+ return false;
+}
+
+static int geodFromArgs(naRef* args, int offset, int argc, SGGeod& result)
+{
+ if (offset >= argc) {
+ return 0;
+ }
+
+ if (naIsGhost(args[offset])) {
+ naGhostType* gt = naGhost_type(args[offset]);
+ if (gt == &AirportGhostType) {
+ result = airportGhost(args[offset])->geod();
+ return 1;
+ }
+
+ if (gt == &NavaidGhostType) {
+ result = navaidGhost(args[offset])->geod();
+ return 1;
+ }
+
+ if (gt == &RunwayGhostType) {
+ result = runwayGhost(args[offset])->geod();
+ return 1;
+ }
+
+ if (gt == &TaxiwayGhostType) {
+ result = taxiwayGhost(args[offset])->geod();
+ return 1;
+ }
+
+ if (gt == &FixGhostType) {
+ result = fixGhost(args[offset])->geod();
+ return 1;
+ }
+
+ if (gt == &WayptGhostType) {
+ result = wayptGhost(args[offset])->position();
+ return 1;
+ }
+ }
+
+ if (geodFromHash(args[offset], result)) {
+ return 1;
+ }
+
+ if (((argc - offset) >= 2) && naIsNum(args[offset]) && naIsNum(args[offset + 1])) {
+ double lat = naNumValue(args[0]).num,
+ lon = naNumValue(args[1]).num;
+ result = SGGeod::fromDeg(lon, lat);
+ return 2;
+ }
+
+ return 0;
+}
+
+// Convert a cartesian point to a geodetic lat/lon/altitude.
+static naRef f_carttogeod(naContext c, naRef me, int argc, naRef* args)
+{
+ double lat, lon, alt, xyz[3];
+ if(argc != 3) naRuntimeError(c, "carttogeod() expects 3 arguments");
+ for(int i=0; i<3; i++)
+ xyz[i] = naNumValue(args[i]).num;
+ sgCartToGeod(xyz, &lat, &lon, &alt);
+ lat *= SG_RADIANS_TO_DEGREES;
+ lon *= SG_RADIANS_TO_DEGREES;
+ naRef vec = naNewVector(c);
+ naVec_append(vec, naNum(lat));
+ naVec_append(vec, naNum(lon));
+ naVec_append(vec, naNum(alt));
+ return vec;
+}
+
+// Convert a geodetic lat/lon/altitude to a cartesian point.
+static naRef f_geodtocart(naContext c, naRef me, int argc, naRef* args)