]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalPositioned_cppbind.cxx
Reset: Nasal can be shutdown.
[flightgear.git] / src / Scripting / NasalPositioned_cppbind.cxx
index aab7489ea7a6a9182fea08a107f6f113f4bc8a01..cc3568c4b67f8ee16984e675774f12ea3b15da28 100644 (file)
 
 #include <Airports/airport.hxx>
 #include <Airports/dynamics.hxx>
+#include <Airports/pavement.hxx>
 #include <ATC/CommStation.hxx>
 #include <Main/globals.hxx>
 #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;
@@ -50,30 +52,8 @@ typedef nasal::Ghost<FGParkingRef> NasalParking;
 typedef nasal::Ghost<FGAirportRef> NasalAirport;
 typedef nasal::Ghost<flightgear::CommStationRef> NasalCommStation;
 typedef nasal::Ghost<FGNavRecordRef> NasalNavRecord;
-
-//------------------------------------------------------------------------------
-naRef to_nasal_helper(naContext c, FGPositioned* positioned)
-{
-  return NasalPositioned::create(c, positioned);
-}
-
-//------------------------------------------------------------------------------
-naRef to_nasal_helper(naContext c, FGPavement* rwy)
-{
-  return NasalPositioned::create(c, (FGPositioned*)rwy);
-}
-
-//------------------------------------------------------------------------------
-naRef to_nasal_helper(naContext c, FGRunwayBase* rwy)
-{
-  return NasalPositioned::create(c, (FGPositioned*)rwy);
-}
-
-//------------------------------------------------------------------------------
-naRef to_nasal_helper(naContext c, FGParking* parking)
-{
-  return NasalParking::create(c, parking);
-}
+typedef nasal::Ghost<FGRunwayRef> NasalRunway;
+typedef nasal::Ghost<FGFixRef> NasalFix;
 
 //------------------------------------------------------------------------------
 naRef to_nasal_helper(naContext c, flightgear::SID* sid)
@@ -96,22 +76,6 @@ naRef to_nasal_helper(naContext c, flightgear::Approach* iap)
   return nasal::to_nasal(c, iap->ident());
 }
 
-//------------------------------------------------------------------------------
-naRef to_nasal_helper(naContext c, FGAirport* apt)
-{
-  return NasalAirport::create(c, apt);
-}
-
-//------------------------------------------------------------------------------
-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)
 {
@@ -125,7 +89,7 @@ static naRef f_navaid_course(naContext, FGNavRecord& nav)
 }
 
 //------------------------------------------------------------------------------
-static FGRunwayBase* f_airport_runway(FGAirport& apt, std::string ident)
+static FGRunwayBaseRef f_airport_runway(FGAirport& apt, std::string ident)
 {
   boost::to_upper(ident);
 
@@ -139,7 +103,7 @@ static FGRunwayBase* f_airport_runway(FGAirport& apt, std::string ident)
 
 //------------------------------------------------------------------------------
 template<class T, class C1, class C2>
-std::vector<T> extract( const std::vector<C1*>& in,
+std::vector<T> extract( const std::vector<C1>& in,
                         T (C2::*getter)() const )
 {
   std::vector<T> ret(in.size());
@@ -349,10 +313,8 @@ static SGGeod getPosition(nasal::CallContext& ctx)
 // airportinfo(<type>);                 type := ("airport"|"seaport"|"heliport")
 // airportinfo()                        same as  airportinfo("airport")
 // airportinfo(<lat>, <lon> [, <type>]);
-static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
+static naRef f_airportinfo(nasal::CallContext ctx)
 {
-  nasal::CallContext ctx(c, argc, args);
-
   SGGeod pos = getPosition(ctx);
 
   if( ctx.argc > 1 )
@@ -375,18 +337,15 @@ static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
 /**
  * findAirportsWithinRange([<position>,] <range-nm> [, type])
  */
-static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
+static naRef f_findAirportsWithinRange(nasal::CallContext ctx)
 {
-  nasal::CallContext ctx(c, argc, args);
-
   SGGeod pos = getPosition(ctx);
   double range_nm = ctx.requireArg<double>(0);
 
   FGAirport::TypeRunwayFilter filter; // defaults to airports only
   filter.fromTypeString( ctx.getArg<std::string>(1) );
 
-  FGPositioned::List apts =
-    FGPositioned::findWithinRange(pos, range_nm, &filter);
+  FGPositionedList apts = FGPositioned::findWithinRange(pos, range_nm, &filter);
   FGPositioned::sortByRange(apts, pos);
 
   return ctx.to_nasal(apts);
@@ -395,9 +354,8 @@ static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* a
 /**
  * findAirportsByICAO(<ident/prefix> [, type])
  */
-static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
+static naRef f_findAirportsByICAO(nasal::CallContext ctx)
 {
-  nasal::CallContext ctx(c, argc, args);
   std::string prefix = ctx.requireArg<std::string>(0);
 
   FGAirport::TypeRunwayFilter filter; // defaults to airports only
@@ -418,10 +376,8 @@ static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
 // navinfo("vor", "HAM") return all vor who's name start with "HAM"
 //navinfo(34,48,"vor","HAM") return all vor who's name start with "HAM"
 //                           sorted by distance relative to lat=34, lon=48
-static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
+static naRef f_navinfo(nasal::CallContext ctx)
 {
-  nasal::CallContext ctx(c, argc, args);
-
   SGGeod pos = getPosition(ctx);
   std::string id = ctx.getArg<std::string>(0);
 
@@ -429,32 +385,82 @@ static naRef f_navinfo(naContext c, naRef me, int argc, naRef* args)
   if( filter.fromTypeString(id) )
     id = ctx.getArg<std::string>(1);
   else if( ctx.argc > 1 )
-    naRuntimeError(c, "navinfo() already got an ident");
+    naRuntimeError(ctx.c, "navinfo() already got an ident");
 
   return ctx.to_nasal( FGNavList::findByIdentAndFreq(pos, id, 0.0, &filter) );
 }
 
 //------------------------------------------------------------------------------
-static naRef f_findNavaidsWithinRange(naContext c, naRef me, int argc, naRef* args)
+static naRef f_findWithinRange(nasal::CallContext ctx)
 {
-  nasal::CallContext ctx(c, argc, args);
-
   SGGeod pos = getPosition(ctx);
   double range_nm = ctx.requireArg<double>(0);
 
-  FGNavList::TypeFilter filter;
-  filter.fromTypeString(ctx.getArg<std::string>(0));
+    std::string typeSpec = ctx.getArg<std::string>(1);
+    FGPositioned::TypeFilter filter(FGPositioned::TypeFilter::fromString(typeSpec));
+    
+  FGPositionedList items = FGPositioned::findWithinRange(pos, range_nm, &filter);
+  FGPositioned::sortByRange(items, pos);
+  return ctx.to_nasal(items);
+}
 
-  FGPositioned::List navs =
-    FGPositioned::findWithinRange(pos, range_nm, &filter);
-  FGPositioned::sortByRange(navs, pos);
+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(navs);
+  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) );
 }
 
 //------------------------------------------------------------------------------
-naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c, naRef gcSave)
+
+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);
+}
+
+//------------------------------------------------------------------------------
+naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c)
+{
+    if (!NasalPositioned::isInit()) {
+    
   NasalPositioned::init("Positioned")
     .member("id", &FGPositioned::ident)
     .member("ident", &FGPositioned::ident) // TODO to we really need id and ident?
@@ -476,6 +482,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)
@@ -495,7 +504,8 @@ 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") );
 
@@ -503,7 +513,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();
 }