]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalPositioned_cppbind.cxx
Reduce amount of log output at level=debug.
[flightgear.git] / src / Scripting / NasalPositioned_cppbind.cxx
index 2191ecd3061f5dc3bac2a732b63db4078b149075..d0fe0b942024b37101cb5a9eabd8301ea2e5b342 100644 (file)
@@ -44,6 +44,7 @@
 #include <Navaids/NavDataCache.hxx>
 #include <Navaids/navlist.hxx>
 #include <Navaids/navrecord.hxx>
+#include <Navaids/fix.hxx>
 
 typedef nasal::Ghost<FGPositionedRef> NasalPositioned;
 typedef nasal::Ghost<FGRunwayRef> NasalRunway;
@@ -51,6 +52,8 @@ typedef nasal::Ghost<FGParkingRef> NasalParking;
 typedef nasal::Ghost<FGAirportRef> NasalAirport;
 typedef nasal::Ghost<flightgear::CommStationRef> NasalCommStation;
 typedef nasal::Ghost<FGNavRecordRef> NasalNavRecord;
+typedef nasal::Ghost<FGRunwayRef> NasalRunway;
+typedef nasal::Ghost<FGFixRef> NasalFix;
 
 //------------------------------------------------------------------------------
 naRef to_nasal_helper(naContext c, flightgear::SID* sid)
@@ -73,16 +76,6 @@ naRef to_nasal_helper(naContext c, flightgear::Approach* iap)
   return nasal::to_nasal(c, iap->ident());
 }
 
-//------------------------------------------------------------------------------
-naRef to_nasal_helper(naContext c, const SGGeod& pos)
-{
-  nasal::Hash hash(c);
-  hash.set("lat", pos.getLatitudeDeg());
-  hash.set("lon", pos.getLongitudeDeg());
-  hash.set("elevation", pos.getElevationM());
-  return hash.get_naRef();
-}
-
 //------------------------------------------------------------------------------
 static naRef f_navaid_course(naContext, FGNavRecord& nav)
 {
@@ -398,18 +391,68 @@ static naRef f_navinfo(nasal::CallContext ctx)
 }
 
 //------------------------------------------------------------------------------
-static naRef f_findNavaidsWithinRange(nasal::CallContext ctx)
+static naRef f_findWithinRange(nasal::CallContext ctx)
 {
   SGGeod pos = getPosition(ctx);
   double range_nm = ctx.requireArg<double>(0);
 
-  FGNavList::TypeFilter filter;
-  filter.fromTypeString(ctx.getArg<std::string>(0));
+  FGPositioned::TypeFilter filter(FGPositioned::typeFromName(ctx.getArg<std::string>(1)));
+    
+  FGPositionedList items = FGPositioned::findWithinRange(pos, range_nm, &filter);
+  FGPositioned::sortByRange(items, pos);
+  return ctx.to_nasal(items);
+}
+
+static naRef f_findByIdent(nasal::CallContext ctx)
+{
+  std::string prefix = ctx.requireArg<std::string>(0);
+  std::string typeSpec = ctx.getArg<std::string>(1);
+  FGPositioned::TypeFilter filter(FGPositioned::TypeFilter::fromString(typeSpec));
+  bool exact = ctx.getArg<bool>(2, false);
+
+  return ctx.to_nasal( FGPositioned::findAllWithIdent(prefix, &filter, exact) );
+}
+
+static naRef f_findByName(nasal::CallContext ctx)
+{
+  std::string prefix = ctx.requireArg<std::string>(0);
+  std::string typeSpec = ctx.getArg<std::string>(1);
+  FGPositioned::TypeFilter filter(FGPositioned::TypeFilter::fromString(typeSpec));
+  
+  return ctx.to_nasal( FGPositioned::findAllWithName(prefix, &filter, false) );
+}
 
-  FGPositionedList navs = FGPositioned::findWithinRange(pos, range_nm, &filter);
-  FGPositioned::sortByRange(navs, pos);
+//------------------------------------------------------------------------------
 
-  return ctx.to_nasal(navs);
+static naRef f_courseAndDistance(nasal::CallContext ctx)
+{
+  SGGeod from = globals->get_aircraft_position(), to, pos;
+  bool ok = extractGeod(ctx, pos);
+  if (!ok) {
+    naRuntimeError(ctx.c, "invalid arguments to courseAndDistance");
+  }
+  
+  if (extractGeod(ctx, to)) {
+    from = pos; // we parsed both FROM and TO args, so first was FROM
+  } else {
+    to = pos; // only parsed one arg, so FROM is current
+  }
+  
+  double course, course2, d;
+  SGGeodesy::inverse(from, to, course, course2, d);
+  
+  naRef result = naNewVector(ctx.c);
+  naVec_append(result, naNum(course));
+  naVec_append(result, naNum(d * SG_METER_TO_NM));
+  return result;
+}
+
+static naRef f_sortByRange(nasal::CallContext ctx)
+{
+  FGPositionedList items = ctx.requireArg<FGPositionedList>(0);
+  ctx.popFront();
+  FGPositioned::sortByRange(items, getPosition(ctx));
+  return ctx.to_nasal(items);
 }
 
 //------------------------------------------------------------------------------
@@ -436,6 +479,9 @@ naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c, naRef gcSave)
     .member("range_nm", &FGNavRecord::get_range)
     .member("course", &f_navaid_course);
 
+  NasalFix::init("Fix")
+    .bases<NasalPositioned>();
+  
   NasalAirport::init("FGAirport")
     .bases<NasalPositioned>()
     .member("has_metar", &FGAirport::getMetar)
@@ -455,7 +501,7 @@ naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c, naRef gcSave)
     .method("getStar", &FGAirport::findSTARWithIdent)
     .method("getIAP", &FGAirport::findApproachWithIdent)
     .method("tostring", &FGAirport::toString);
-
+    
   nasal::Hash globals(globalsRef, c),
               positioned( globals.createHash("positioned") );
 
@@ -463,7 +509,12 @@ naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c, naRef gcSave)
   positioned.set("findAirportsWithinRange", f_findAirportsWithinRange);
   positioned.set("findAirportsByICAO", &f_findAirportsByICAO);
   positioned.set("navinfo", &f_navinfo);
-  positioned.set("findNavaidsWithinRange", &f_findNavaidsWithinRange);
-
+  
+  positioned.set("findWithinRange", &f_findWithinRange);
+  positioned.set("findByIdent", &f_findByIdent);
+  positioned.set("findByName", &f_findByName);
+  positioned.set("courseAndDistance", &f_courseAndDistance);
+  positioned.set("sortByRange", &f_sortByRange);
+  
   return naNil();
 }