]> git.mxchange.org Git - flightgear.git/commitdiff
Port more airport/navaid methods to cppbind
authorThomas Geymayer <tomgey@gmail.com>
Wed, 6 Mar 2013 00:04:18 +0000 (01:04 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Wed, 6 Mar 2013 00:04:52 +0000 (01:04 +0100)
src/Navaids/navaids_fwd.hxx [new file with mode: 0644]
src/Navaids/navlist.cxx
src/Navaids/navlist.hxx
src/Navaids/navrecord.hxx
src/Navaids/positioned.cxx
src/Navaids/positioned.hxx
src/Scripting/NasalPositioned_cppbind.cxx

diff --git a/src/Navaids/navaids_fwd.hxx b/src/Navaids/navaids_fwd.hxx
new file mode 100644 (file)
index 0000000..b40b62d
--- /dev/null
@@ -0,0 +1,27 @@
+// Navaids forward declarations
+//
+// Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+#ifndef NAVAIDS_FWD_HXX_
+#define NAVAIDS_FWD_HXX_
+
+#include <simgear/structure/SGSharedPtr.hxx>
+
+class FGNavRecord;
+typedef SGSharedPtr<FGNavRecord> FGNavRecordRef;
+
+#endif /* NAVAIDS_FWD_HXX_ */
index eb6bac7547b2d80060f488e65b42e8fa1e422f49..7d5e8c12390c2b6fba2017924a379f7d785e27da 100644 (file)
@@ -91,6 +91,7 @@ bool navidUsable(FGNavRecord* aNav, const SGGeod &aircraft)
 // FGNavList ------------------------------------------------------------------
 
 
+//------------------------------------------------------------------------------
 FGNavList::TypeFilter::TypeFilter(const FGPositioned::Type type)
 {
   if (type == FGPositioned::INVALID) {
@@ -101,14 +102,32 @@ FGNavList::TypeFilter::TypeFilter(const FGPositioned::Type type)
   }
 }
 
-
-FGNavList::TypeFilter::TypeFilter(const FGPositioned::Type minType,
-                                  const FGPositioned::Type maxType) :
+//------------------------------------------------------------------------------
+FGNavList::TypeFilter::TypeFilter( const FGPositioned::Type minType,
+                                   const FGPositioned::Type maxType ):
   _mintype(minType),
   _maxtype(maxType)
 {
 }
 
+//------------------------------------------------------------------------------
+bool FGNavList::TypeFilter::fromTypeString(const std::string& type)
+{
+  FGPositioned::Type t;
+  if(      type == "any"  ) t = FGPositioned::INVALID;
+  else if( type == "fix"  ) t = FGPositioned::FIX;
+  else if( type == "vor"  ) t = FGPositioned::VOR;
+  else if( type == "ndb"  ) t = FGPositioned::NDB;
+  else if( type == "ils"  ) t = FGPositioned::ILS;
+  else if( type == "dme"  ) t = FGPositioned::DME;
+  else if( type == "tacan") t = FGPositioned::TACAN;
+  else                      return false;
+
+  _mintype = _maxtype = t;
+
+  return true;
+}
+
 /**
  * Filter returning Tacan stations. Checks for both pure TACAN stations
  * but also co-located VORTACs. This is done by searching for DMEs whose
index e33697406b6d204cd24cc70e9fa189c25258c541..8f9204b7188aa491d408a7fb1476f35f047dd24b 100644 (file)
@@ -49,18 +49,19 @@ public:
   class TypeFilter : public FGPositioned::Filter
   {
   public:
-    TypeFilter(const FGPositioned::Type type);
+    TypeFilter( const FGPositioned::Type type = FGPositioned::INVALID );
+    TypeFilter( const FGPositioned::Type minType,
+                const FGPositioned::Type maxType );
     
-    TypeFilter(const FGPositioned::Type minType,
-               const FGPositioned::Type maxType);
-    
-    virtual FGPositioned::Type minType() const {
-      return _mintype;
-    }
+    /**
+     * Construct from string containing type
+     *
+     * @param type One of "fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any"
+     */
+    bool fromTypeString(const std::string& type);
     
-    virtual FGPositioned::Type maxType()  const {
-      return _maxtype;
-    }
+    virtual FGPositioned::Type minType() const { return _mintype; }
+    virtual FGPositioned::Type maxType() const { return _maxtype; }
     
   protected:
     FGPositioned::Type _mintype;
index aa4c1cb4e81e67c68b447eef90c8f90fc62d35d5..8710b9aa36e59c6d9d583b395010be02dd243ab0 100644 (file)
 
 #include <iosfwd>
 
+#include "navaids_fwd.hxx"
 #include "positioned.hxx"
+#include <Airports/airports_fwd.hxx>
 
 const double FG_NAV_DEFAULT_RANGE = 50; // nm
 const double FG_LOC_DEFAULT_RANGE = 18; // nm
 const double FG_DME_DEFAULT_RANGE = 50; // nm
 const double FG_NAV_MAX_RANGE = 300;    // nm
 
-// forward decls
-class FGRunway;
-class SGPropertyNode;
-
 class FGNavRecord : public FGPositioned 
 {
 
index 29e0ee0a466ddf7af831380f0c368b7f3693aa7a..e5227a2cc64fbe00c0be85a15597152786875c42 100644 (file)
@@ -134,6 +134,7 @@ FGPositioned::Type FGPositioned::typeFromName(const std::string& aName)
     {"village", VILLAGE},
       
   // aliases
+    {"localizer", LOC},
     {"gnd", FREQ_GROUND},
     {"twr", FREQ_TOWER},
     {"waypoint", WAYPOINT},
@@ -170,7 +171,7 @@ const char* FGPositioned::nameForType(Type aTy)
  case VOR: return "VOR";
  case NDB: return "NDB";
  case ILS: return "ILS";
- case LOC: return "localiser";
+ case LOC: return "localizer";
  case GS: return "glideslope";
  case OM: return "outer-marker";
  case MM: return "middle-marker";
index 8341c22f1c7968a97a9c5dc483fabb858c9d3b06..42476d8545e17c44d97f78de2c5fa5703371e8df 100644 (file)
@@ -98,6 +98,9 @@ public:
   Type type() const
   { return mType; }
 
+  const char* typeString() const
+  { return nameForType(mType); }
+
   const std::string& ident() const
   { return mIdent; }
 
index f0eb9331d26d462043cbaf631e991e9610d125d6..aab7489ea7a6a9182fea08a107f6f113f4bc8a01 100644 (file)
 #include <ATC/CommStation.hxx>
 #include <Main/globals.hxx>
 #include <Navaids/NavDataCache.hxx>
+#include <Navaids/navlist.hxx>
+#include <Navaids/navrecord.hxx>
 
 typedef nasal::Ghost<FGPositionedRef> NasalPositioned;
 typedef nasal::Ghost<FGRunwayRef> NasalRunway;
 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)
@@ -109,6 +112,18 @@ naRef to_nasal_helper(naContext c, const SGGeod& pos)
   return hash.get_naRef();
 }
 
+//------------------------------------------------------------------------------
+static naRef f_navaid_course(naContext, FGNavRecord& nav)
+{
+  if( !(  nav.type() == FGPositioned::ILS
+       || nav.type() == FGPositioned::LOC
+       ) )
+    return naNil();
+
+  double radial = nav.get_multiuse();
+  return naNum(SGMiscd::normalizePeriodic(0.5, 360.5, radial));
+}
+
 //------------------------------------------------------------------------------
 static FGRunwayBase* f_airport_runway(FGAirport& apt, std::string ident)
 {
@@ -314,6 +329,18 @@ static bool extractGeod(nasal::CallContext& ctx, SGGeod& result)
   return false;
 }
 
+/**
+ * Extract position from ctx or return current aircraft position if not given.
+ */
+static SGGeod getPosition(nasal::CallContext& ctx)
+{
+  SGGeod pos;
+  if( !extractGeod(ctx, pos) )
+    pos = globals->get_aircraft_position();
+
+  return pos;
+}
+
 //------------------------------------------------------------------------------
 // Returns Nasal ghost for particular or nearest airport of a <type>, or nil
 // on error.
@@ -326,15 +353,15 @@ static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
 {
   nasal::CallContext ctx(c, argc, args);
 
-  SGGeod pos;
-  if( !extractGeod(ctx, pos) )
-    pos = globals->get_aircraft_position();
+  SGGeod pos = getPosition(ctx);
 
   if( ctx.argc > 1 )
     naRuntimeError(ctx.c, "airportinfo() with invalid function arguments");
 
   // optional type/ident
-  std::string ident = ctx.getArg<std::string>(0, "airport");
+  std::string ident("airport");
+  if( ctx.isString(0) )
+    ident = ctx.requireArg<std::string>(0);
 
   FGAirport::TypeRunwayFilter filter;
   if( !filter.fromTypeString(ident) )
@@ -345,23 +372,110 @@ static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
   return ctx.to_nasal( FGAirport::findClosest(pos, maxRange, &filter) );
 }
 
+/**
+ * findAirportsWithinRange([<position>,] <range-nm> [, type])
+ */
+static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* args)
+{
+  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);
+  FGPositioned::sortByRange(apts, pos);
+
+  return ctx.to_nasal(apts);
+}
+
+/**
+ * findAirportsByICAO(<ident/prefix> [, type])
+ */
+static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
+{
+  nasal::CallContext ctx(c, argc, args);
+  std::string prefix = ctx.requireArg<std::string>(0);
+
+  FGAirport::TypeRunwayFilter filter; // defaults to airports only
+  filter.fromTypeString( ctx.getArg<std::string>(1) );
+
+  return ctx.to_nasal( FGPositioned::findAllWithIdent(prefix, &filter, false) );
+}
+
+// Returns vector of data hash for navaid of a <type>, nil on error
+// navaids sorted by ascending distance
+// navinfo([<lat>,<lon>],[<type>],[<id>])
+// lat/lon (numeric): use latitude/longitude instead of ac position
+// type:              ("fix"|"vor"|"ndb"|"ils"|"dme"|"tacan"|"any")
+// id:                (partial) id of the fix
+// examples:
+// navinfo("vor")     returns all vors
+// navinfo("HAM")     return all navaids who's name start with "HAM"
+// 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)
+{
+  nasal::CallContext ctx(c, argc, args);
+
+  SGGeod pos = getPosition(ctx);
+  std::string id = ctx.getArg<std::string>(0);
+
+  FGNavList::TypeFilter filter;
+  if( filter.fromTypeString(id) )
+    id = ctx.getArg<std::string>(1);
+  else if( ctx.argc > 1 )
+    naRuntimeError(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)
+{
+  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));
+
+  FGPositioned::List navs =
+    FGPositioned::findWithinRange(pos, range_nm, &filter);
+  FGPositioned::sortByRange(navs, pos);
+
+  return ctx.to_nasal(navs);
+}
+
 //------------------------------------------------------------------------------
 naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c, naRef gcSave)
 {
-  NasalPositioned::init("FGPositioned")
+  NasalPositioned::init("Positioned")
     .member("id", &FGPositioned::ident)
     .member("ident", &FGPositioned::ident) // TODO to we really need id and ident?
     .member("name", &FGPositioned::name)
+    .member("type", &FGPositioned::typeString)
     .member("lat", &FGPositioned::latitude)
     .member("lon", &FGPositioned::longitude)
     .member("elevation", &FGPositioned::elevationM);
-  NasalRunway::init("FGRunway")
+  NasalRunway::init("Runway")
     .bases<NasalPositioned>();
-  NasalParking::init("FGParking")
+  NasalParking::init("Parking")
     .bases<NasalPositioned>();
   NasalCommStation::init("CommStation")
     .bases<NasalPositioned>()
     .member("frequency", &flightgear::CommStation::freqMHz);
+  NasalNavRecord::init("Navaid")
+    .bases<NasalPositioned>()
+    .member("frequency", &FGNavRecord::get_freq)
+    .member("range_nm", &FGNavRecord::get_range)
+    .member("course", &f_navaid_course);
+
   NasalAirport::init("FGAirport")
     .bases<NasalPositioned>()
     .member("has_metar", &FGAirport::getMetar)
@@ -386,6 +500,10 @@ naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c, naRef gcSave)
               positioned( globals.createHash("positioned") );
 
   positioned.set("airportinfo", &f_airportinfo);
+  positioned.set("findAirportsWithinRange", f_findAirportsWithinRange);
+  positioned.set("findAirportsByICAO", &f_findAirportsByICAO);
+  positioned.set("navinfo", &f_navinfo);
+  positioned.set("findNavaidsWithinRange", &f_findNavaidsWithinRange);
 
   return naNil();
 }