1 // NasalPositioned_cppbind.cxx -- expose FGPositioned classes to Nasal
3 // Port of NasalPositioned.cpp to the new nasal/cppbind helpers. Will replace
4 // old NasalPositioned.cpp once finished.
6 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "NasalPositioned.hxx"
31 #include <boost/foreach.hpp>
32 #include <boost/algorithm/string/case_conv.hpp>
34 #include <simgear/nasal/cppbind/from_nasal.hxx>
35 #include <simgear/nasal/cppbind/to_nasal.hxx>
36 #include <simgear/nasal/cppbind/NasalHash.hxx>
37 #include <simgear/nasal/cppbind/Ghost.hxx>
39 #include <Airports/airport.hxx>
40 #include <Airports/dynamics.hxx>
41 #include <ATC/CommStation.hxx>
42 #include <Navaids/NavDataCache.hxx>
44 typedef nasal::Ghost<FGPositionedRef> NasalPositioned;
45 typedef nasal::Ghost<FGRunwayRef> NasalRunway;
46 typedef nasal::Ghost<FGParkingRef> NasalParking;
47 typedef nasal::Ghost<FGAirportRef> NasalAirport;
48 typedef nasal::Ghost<flightgear::CommStationRef> NasalCommStation;
50 //------------------------------------------------------------------------------
51 naRef to_nasal_helper(naContext c, FGPositioned* positioned)
53 return NasalPositioned::create(c, positioned);
56 //------------------------------------------------------------------------------
57 naRef to_nasal_helper(naContext c, FGPavement* rwy)
59 return NasalPositioned::create(c, (FGPositioned*)rwy);
62 //------------------------------------------------------------------------------
63 naRef to_nasal_helper(naContext c, FGRunwayBase* rwy)
65 return NasalPositioned::create(c, (FGPositioned*)rwy);
68 //------------------------------------------------------------------------------
69 naRef to_nasal_helper(naContext c, FGParking* parking)
71 return NasalParking::create(c, parking);
74 //------------------------------------------------------------------------------
75 naRef to_nasal_helper(naContext c, flightgear::SID* sid)
78 return nasal::to_nasal(c, sid->ident());
81 //------------------------------------------------------------------------------
82 naRef to_nasal_helper(naContext c, flightgear::STAR* star)
85 return nasal::to_nasal(c, star->ident());
88 //------------------------------------------------------------------------------
89 naRef to_nasal_helper(naContext c, flightgear::Approach* iap)
91 // TODO Approach ghost
92 return nasal::to_nasal(c, iap->ident());
95 //------------------------------------------------------------------------------
96 naRef to_nasal_helper(naContext c, FGAirport* apt)
98 return NasalAirport::create(c, apt);
101 //------------------------------------------------------------------------------
102 naRef to_nasal_helper(naContext c, const SGGeod& pos)
105 hash.set("lat", pos.getLatitudeDeg());
106 hash.set("lon", pos.getLongitudeDeg());
107 hash.set("elevation", pos.getElevationM());
108 return hash.get_naRef();
111 //------------------------------------------------------------------------------
112 static FGRunwayBase* f_airport_runway(FGAirport& apt, std::string ident)
114 boost::to_upper(ident);
116 if( apt.hasRunwayWithIdent(ident) )
117 return apt.getRunwayByIdent(ident);
118 else if( apt.hasHelipadWithIdent(ident) )
119 return apt.getHelipadByIdent(ident);
124 //------------------------------------------------------------------------------
125 template<class T, class C1, class C2>
126 std::vector<T> extract( const std::vector<C1*>& in,
127 T (C2::*getter)() const )
129 std::vector<T> ret(in.size());
130 std::transform(in.begin(), in.end(), ret.begin(), std::mem_fun(getter));
134 //------------------------------------------------------------------------------
135 static naRef f_airport_comms(FGAirport& apt, const nasal::CallContext& ctx)
137 FGPositioned::Type comm_type =
138 FGPositioned::typeFromName( ctx.getArg<std::string>(0) );
140 // if we have an explicit type, return a simple vector of frequencies
141 if( comm_type != FGPositioned::INVALID )
144 extract( apt.commStationsOfType(comm_type),
145 &flightgear::CommStation::freqMHz )
148 // otherwise return a vector of ghosts, one for each comm station.
149 return ctx.to_nasal(apt.commStations());
152 //------------------------------------------------------------------------------
153 FGRunway* runwayFromNasalArg( const FGAirport& apt,
154 const nasal::CallContext& ctx,
157 if( index >= ctx.argc )
162 std::string ident = ctx.getArg<std::string>(index);
165 if( !apt.hasRunwayWithIdent(ident) )
166 // TODO warning/exception?
169 return apt.getRunwayByIdent(ident);
175 // TODO warn/error if no runway?
176 return NasalRunway::fromNasal(ctx.c, ctx.args[index]);
179 //------------------------------------------------------------------------------
180 static naRef f_airport_sids(FGAirport& apt, const nasal::CallContext& ctx)
182 FGRunway* rwy = runwayFromNasalArg(apt, ctx);
185 extract(rwy ? rwy->getSIDs() : apt.getSIDs(), &flightgear::SID::ident)
189 //------------------------------------------------------------------------------
190 static naRef f_airport_stars(FGAirport& apt, const nasal::CallContext& ctx)
192 FGRunway* rwy = runwayFromNasalArg(apt, ctx);
195 extract(rwy ? rwy->getSTARs() : apt.getSTARs(), &flightgear::STAR::ident)
199 //------------------------------------------------------------------------------
200 static naRef f_airport_approaches(FGAirport& apt, const nasal::CallContext& ctx)
202 FGRunway* rwy = runwayFromNasalArg(apt, ctx);
204 flightgear::ProcedureType type = flightgear::PROCEDURE_INVALID;
205 std::string type_str = ctx.getArg<std::string>(1);
206 if( !type_str.empty() )
208 boost::to_upper(type_str);
209 if( type_str == "NDB" ) type = flightgear::PROCEDURE_APPROACH_NDB;
210 else if( type_str == "VOR" ) type = flightgear::PROCEDURE_APPROACH_VOR;
211 else if( type_str == "ILS" ) type = flightgear::PROCEDURE_APPROACH_ILS;
212 else if( type_str == "RNAV") type = flightgear::PROCEDURE_APPROACH_RNAV;
217 extract( rwy ? rwy->getApproaches(type)
218 // no runway specified, report them all
219 : apt.getApproaches(type),
220 &flightgear::Approach::ident )
224 //------------------------------------------------------------------------------
226 f_airport_parking(FGAirport& apt, const nasal::CallContext& ctx)
228 std::string type = ctx.getArg<std::string>(0);
229 bool only_available = ctx.getArg<bool>(1);
231 FGAirportDynamics* dynamics = apt.getDynamics();
232 PositionedIDVec parkings =
233 flightgear::NavDataCache::instance()
234 ->airportItemsOfType(apt.guid(), FGPositioned::PARKING);
237 BOOST_FOREACH(PositionedID parking, parkings)
239 // filter out based on availability and type
240 if( only_available && !dynamics->isParkingAvailable(parking) )
243 FGParking* park = dynamics->getParking(parking);
244 if( !type.empty() && (park->getType() != type) )
253 //------------------------------------------------------------------------------
254 // Returns Nasal ghost for particular or nearest airport of a <type>, or nil
255 // on error. (Currently only airportinfo(<id>) is implemented)
257 // airportinfo(<id>); e.g. "KSFO"
258 // airportinfo(<type>); type := ("airport"|"seaport"|"heliport")
259 // airportinfo() same as airportinfo("airport")
260 // airportinfo(<lat>, <lon> [, <type>]);
261 static naRef f_airportinfo(naContext c, naRef me, int argc, naRef* args)
263 nasal::CallContext ctx(c, argc, args);
264 // TODO think of something comfortable to overload functions or use variable
265 // number/types of arguments.
266 return ctx.to_nasal(FGAirport::findByIdent( ctx.requireArg<std::string>(0) ));
269 //------------------------------------------------------------------------------
270 naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c, naRef gcSave)
272 NasalPositioned::init("FGPositioned")
273 .member("id", &FGPositioned::ident)
274 .member("ident", &FGPositioned::ident) // TODO to we really need id and ident?
275 .member("name", &FGPositioned::name)
276 .member("lat", &FGPositioned::latitude)
277 .member("lon", &FGPositioned::longitude)
278 .member("elevation", &FGPositioned::elevationM);
279 NasalRunway::init("FGRunway")
280 .bases<NasalPositioned>();
281 NasalParking::init("FGParking")
282 .bases<NasalPositioned>();
283 NasalCommStation::init("CommStation")
284 .bases<NasalPositioned>()
285 .member("frequency", &flightgear::CommStation::freqMHz);
286 NasalAirport::init("FGAirport")
287 .bases<NasalPositioned>()
288 .member("has_metar", &FGAirport::getMetar)
289 .member("runways", &FGAirport::getRunwayMap)
290 .member("helipads", &FGAirport::getHelipadMap)
291 .member("taxiways", &FGAirport::getTaxiways)
292 .member("pavements", &FGAirport::getPavements)
293 .method("runway", &f_airport_runway)
294 .method("helipad", &f_airport_runway)
295 .method("tower", &FGAirport::getTowerLocation)
296 .method("comms", &f_airport_comms)
297 .method("sids", &f_airport_sids)
298 .method("stars", &f_airport_stars)
299 .method("getApproachList", f_airport_approaches)
300 .method("parking", &f_airport_parking)
301 .method("getSid", &FGAirport::findSIDWithIdent)
302 .method("getStar", &FGAirport::findSTARWithIdent)
303 .method("getIAP", &FGAirport::findApproachWithIdent)
304 .method("tostring", &FGAirport::toString);
306 nasal::Hash globals(globalsRef, c),
307 positioned( globals.createHash("positioned") );
309 positioned.set("airportinfo", &f_airportinfo);