From: Thomas Geymayer Date: Mon, 4 Mar 2013 22:14:26 +0000 (+0100) Subject: Finish porting airportinfo to cppbind X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=22a1c9b2af9fd6c4c2758d095c3004da28e83b85;p=flightgear.git Finish porting airportinfo to cppbind --- diff --git a/src/Airports/airport.cxx b/src/Airports/airport.cxx index 508eb9934..1c96c2ef2 100644 --- a/src/Airports/airport.cxx +++ b/src/Airports/airport.cxx @@ -408,12 +408,44 @@ FGAirport::HardSurfaceFilter::HardSurfaceFilter(double minLengthFt) : mMinLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0); } } - + bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const { return aApt->hasHardRunwayOfLengthFt(mMinLengthFt); } +//------------------------------------------------------------------------------ +FGAirport::TypeRunwayFilter::TypeRunwayFilter(): + _type(FGPositioned::AIRPORT), + _min_runway_length_ft( fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0) ) +{ + +} + +//------------------------------------------------------------------------------ +bool FGAirport::TypeRunwayFilter::fromTypeString(const std::string& type) +{ + if( type == "heliport" ) _type = FGPositioned::HELIPORT; + else if( type == "seaport" ) _type = FGPositioned::SEAPORT; + else if( type == "airport" ) _type = FGPositioned::AIRPORT; + else return false; + + return true; +} + +//------------------------------------------------------------------------------ +bool FGAirport::TypeRunwayFilter::pass(FGPositioned* pos) const +{ + FGAirport* apt = static_cast(pos); + if( (apt->type() == FGPositioned::AIRPORT) + && !apt->hasHardRunwayOfLengthFt(_min_runway_length_ft) + ) + return false; + + return true; +} + +//------------------------------------------------------------------------------ FGAirport* FGAirport::findByIdent(const std::string& aIdent) { AirportCache::iterator it = airportCache.find(aIdent); diff --git a/src/Airports/airport.hxx b/src/Airports/airport.hxx index d32326c65..f0618018d 100644 --- a/src/Airports/airport.hxx +++ b/src/Airports/airport.hxx @@ -44,7 +44,7 @@ **************************************************************************************/ class FGAirport : public FGPositioned { -public: + public: FGAirport(PositionedID aGuid, const std::string& id, const SGGeod& location, const std::string& name, bool has_metar, Type aType); ~FGAirport(); @@ -133,23 +133,23 @@ public: FGPavementList getPavements() const; class AirportFilter : public Filter - { - public: - virtual bool pass(FGPositioned* aPos) const { - return passAirport(static_cast(aPos)); - } - - virtual Type minType() const { - return AIRPORT; - } - - virtual Type maxType() const { - return AIRPORT; - } - - virtual bool passAirport(FGAirport* aApt) const { - return true; - } + { + public: + virtual bool pass(FGPositioned* aPos) const { + return passAirport(static_cast(aPos)); + } + + virtual Type minType() const { + return AIRPORT; + } + + virtual Type maxType() const { + return AIRPORT; + } + + virtual bool passAirport(FGAirport* aApt) const { + return true; + } }; /** @@ -174,6 +174,30 @@ public: double mMinLengthFt; }; + /** + * Filter which passes specified port type and in case of airport checks + * if a runway larger the /sim/navdb/min-runway-lenght-ft exists. + */ + class TypeRunwayFilter: + public AirportFilter + { + public: + TypeRunwayFilter(); + + /** + * Construct from string containing type (airport, seaport or heliport) + */ + bool fromTypeString(const std::string& type); + + virtual FGPositioned::Type minType() const { return _type; } + virtual FGPositioned::Type maxType() const { return _type; } + virtual bool pass(FGPositioned* pos) const; + + protected: + FGPositioned::Type _type; + double _min_runway_length_ft; + }; + void setProcedures(const std::vector& aSids, const std::vector& aStars, diff --git a/src/Scripting/NasalPositioned.cxx b/src/Scripting/NasalPositioned.cxx index f4eeb8444..eb572973e 100644 --- a/src/Scripting/NasalPositioned.cxx +++ b/src/Scripting/NasalPositioned.cxx @@ -983,49 +983,6 @@ static naRef f_geodinfo(naContext c, naRef me, int argc, naRef* args) } -class AirportInfoFilter : public FGAirport::AirportFilter -{ -public: - AirportInfoFilter() : type(FGPositioned::AIRPORT) { - minRunwayLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 0.0); - } - - bool fromArg(naRef arg) - { - const char *s = naStr_data(arg); - if(!strcmp(s, "airport")) type = FGPositioned::AIRPORT; - else if(!strcmp(s, "seaport")) type = FGPositioned::SEAPORT; - else if(!strcmp(s, "heliport")) type = FGPositioned::HELIPORT; - else - return false; - - return true; - } - - virtual FGPositioned::Type minType() const { - return type; - } - - virtual FGPositioned::Type maxType() const { - return type; - } - - virtual bool pass(FGPositioned* aPos) const - { - FGAirport* apt = (FGAirport*) aPos; - if ((apt->type() == FGPositioned::AIRPORT) && - !apt->hasHardRunwayOfLengthFt(minRunwayLengthFt)) - { - return false; - } - - return true; - } - - FGPositioned::Type type; - double minRunwayLengthFt; -}; - // Returns data hash for particular or nearest airport of a , or nil // on error. // @@ -1046,12 +1003,12 @@ static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args) double maxRange = 10000.0; // expose this? or pick a smaller value? - AirportInfoFilter filter; // defaults to airports only + FGAirport::TypeRunwayFilter filter; // defaults to airports only if(argc == 0) { // fall through and use AIRPORT } else if(argc == 1 && naIsString(args[0])) { - if (filter.fromArg(args[0])) { + if (filter.fromTypeString(naStr_data(args[0]))) { // done! } else { // user provided an , hopefully @@ -1086,10 +1043,10 @@ static naRef f_findAirportsWithinRange(naContext c, naRef me, int argc, naRef* a naRuntimeError(c, "findAirportsWithinRange expected range (in nm) as arg %d", argOffset); } - AirportInfoFilter filter; // defaults to airports only + FGAirport::TypeRunwayFilter filter; // defaults to airports only double rangeNm = args[argOffset++].num; if (argOffset < argc) { - filter.fromArg(args[argOffset++]); + filter.fromTypeString(naStr_data(args[argOffset++])); } naRef r = naNewVector(c); @@ -1113,9 +1070,9 @@ static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args) int argOffset = 0; std::string prefix(naStr_data(args[argOffset++])); - AirportInfoFilter filter; // defaults to airports only + FGAirport::TypeRunwayFilter filter; // defaults to airports only if (argOffset < argc) { - filter.fromArg(args[argOffset++]); + filter.fromTypeString(naStr_data(args[argOffset++])); } naRef r = naNewVector(c); diff --git a/src/Scripting/NasalPositioned_cppbind.cxx b/src/Scripting/NasalPositioned_cppbind.cxx index b2ae98c4d..aeede726b 100644 --- a/src/Scripting/NasalPositioned_cppbind.cxx +++ b/src/Scripting/NasalPositioned_cppbind.cxx @@ -39,6 +39,7 @@ #include #include #include +#include
#include typedef nasal::Ghost NasalPositioned; @@ -252,7 +253,7 @@ f_airport_parking(FGAirport& apt, const nasal::CallContext& ctx) //------------------------------------------------------------------------------ // Returns Nasal ghost for particular or nearest airport of a , or nil -// on error. (Currently only airportinfo() is implemented) +// on error. // // airportinfo(); e.g. "KSFO" // airportinfo(); type := ("airport"|"seaport"|"heliport") @@ -263,7 +264,34 @@ static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args) nasal::CallContext ctx(c, argc, args); // TODO think of something comfortable to overload functions or use variable // number/types of arguments. - return ctx.to_nasal(FGAirport::findByIdent( ctx.requireArg(0) )); + + std::string ident = "airport"; + SGGeod pos = globals->get_aircraft_position(); + + if( ctx.argc == 1 ) + { + ident = ctx.requireArg(0); + } + else if( ctx.argc >= 2 ) + { + // Why are lat/lon swapped? + pos = SGGeod::fromDeg( ctx.requireArg(1), + ctx.requireArg(0) ); + + if( ctx.argc >= 3 ) + ident = ctx.requireArg(2); + + if( ctx.argc > 3 ) + naRuntimeError(ctx.c, "airportinfo() with invalid function arguments"); + } + + FGAirport::TypeRunwayFilter filter; + if( !filter.fromTypeString(ident) ) + // user provided an , hopefully + return ctx.to_nasal(FGAirport::findByIdent(ident)); + + double maxRange = 10000.0; // expose this? or pick a smaller value? + return ctx.to_nasal( FGAirport::findClosest(pos, maxRange, &filter) ); } //------------------------------------------------------------------------------