1
0
Fork 0

Finish porting airportinfo to cppbind

This commit is contained in:
Thomas Geymayer 2013-03-04 23:14:26 +01:00
parent 3579a4404a
commit 22a1c9b2af
4 changed files with 111 additions and 70 deletions

View file

@ -414,6 +414,38 @@ bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const
return aApt->hasHardRunwayOfLengthFt(mMinLengthFt); 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<FGAirport*>(pos);
if( (apt->type() == FGPositioned::AIRPORT)
&& !apt->hasHardRunwayOfLengthFt(_min_runway_length_ft)
)
return false;
return true;
}
//------------------------------------------------------------------------------
FGAirport* FGAirport::findByIdent(const std::string& aIdent) FGAirport* FGAirport::findByIdent(const std::string& aIdent)
{ {
AirportCache::iterator it = airportCache.find(aIdent); AirportCache::iterator it = airportCache.find(aIdent);

View file

@ -174,6 +174,30 @@ public:
double mMinLengthFt; 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<flightgear::SID*>& aSids, void setProcedures(const std::vector<flightgear::SID*>& aSids,
const std::vector<flightgear::STAR*>& aStars, const std::vector<flightgear::STAR*>& aStars,

View file

@ -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 <type>, or nil // Returns data hash for particular or nearest airport of a <type>, or nil
// on error. // 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? 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) { if(argc == 0) {
// fall through and use AIRPORT // fall through and use AIRPORT
} else if(argc == 1 && naIsString(args[0])) { } else if(argc == 1 && naIsString(args[0])) {
if (filter.fromArg(args[0])) { if (filter.fromTypeString(naStr_data(args[0]))) {
// done! // done!
} else { } else {
// user provided an <id>, hopefully // user provided an <id>, 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); 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; double rangeNm = args[argOffset++].num;
if (argOffset < argc) { if (argOffset < argc) {
filter.fromArg(args[argOffset++]); filter.fromTypeString(naStr_data(args[argOffset++]));
} }
naRef r = naNewVector(c); naRef r = naNewVector(c);
@ -1113,9 +1070,9 @@ static naRef f_findAirportsByICAO(naContext c, naRef me, int argc, naRef* args)
int argOffset = 0; int argOffset = 0;
std::string prefix(naStr_data(args[argOffset++])); std::string prefix(naStr_data(args[argOffset++]));
AirportInfoFilter filter; // defaults to airports only FGAirport::TypeRunwayFilter filter; // defaults to airports only
if (argOffset < argc) { if (argOffset < argc) {
filter.fromArg(args[argOffset++]); filter.fromTypeString(naStr_data(args[argOffset++]));
} }
naRef r = naNewVector(c); naRef r = naNewVector(c);

View file

@ -39,6 +39,7 @@
#include <Airports/airport.hxx> #include <Airports/airport.hxx>
#include <Airports/dynamics.hxx> #include <Airports/dynamics.hxx>
#include <ATC/CommStation.hxx> #include <ATC/CommStation.hxx>
#include <Main/globals.hxx>
#include <Navaids/NavDataCache.hxx> #include <Navaids/NavDataCache.hxx>
typedef nasal::Ghost<FGPositionedRef> NasalPositioned; typedef nasal::Ghost<FGPositionedRef> NasalPositioned;
@ -252,7 +253,7 @@ f_airport_parking(FGAirport& apt, const nasal::CallContext& ctx)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Returns Nasal ghost for particular or nearest airport of a <type>, or nil // Returns Nasal ghost for particular or nearest airport of a <type>, or nil
// on error. (Currently only airportinfo(<id>) is implemented) // on error.
// //
// airportinfo(<id>); e.g. "KSFO" // airportinfo(<id>); e.g. "KSFO"
// airportinfo(<type>); type := ("airport"|"seaport"|"heliport") // airportinfo(<type>); 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); nasal::CallContext ctx(c, argc, args);
// TODO think of something comfortable to overload functions or use variable // TODO think of something comfortable to overload functions or use variable
// number/types of arguments. // number/types of arguments.
return ctx.to_nasal(FGAirport::findByIdent( ctx.requireArg<std::string>(0) ));
std::string ident = "airport";
SGGeod pos = globals->get_aircraft_position();
if( ctx.argc == 1 )
{
ident = ctx.requireArg<std::string>(0);
}
else if( ctx.argc >= 2 )
{
// Why are lat/lon swapped?
pos = SGGeod::fromDeg( ctx.requireArg<double>(1),
ctx.requireArg<double>(0) );
if( ctx.argc >= 3 )
ident = ctx.requireArg<std::string>(2);
if( ctx.argc > 3 )
naRuntimeError(ctx.c, "airportinfo() with invalid function arguments");
}
FGAirport::TypeRunwayFilter filter;
if( !filter.fromTypeString(ident) )
// user provided an <id>, 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) );
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------