23#include <osgViewer/Viewer>
24#include <osg/PagedLOD>
27#include <simgear/misc/strutils.hxx>
28#include <simgear/props/props_io.hxx>
29#include <simgear/structure/exception.hxx>
30#include <simgear/structure/event_mgr.hxx>
31#include <simgear/scene/model/CheckSceneryVisitor.hxx>
32#include <simgear/scene/util/OsgMath.hxx>
80 fgSetDouble(
"/sim/presets/longitude-deg", aPos.getLongitudeDeg() );
81 fgSetDouble(
"/sim/presets/latitude-deg", aPos.getLatitudeDeg() );
82 fgSetDouble(
"/sim/presets/heading-deg", aHeadingDeg );
85 fgSetDouble(
"/position/longitude-deg", aPos.getLongitudeDeg() );
86 fgSetDouble(
"/position/latitude-deg", aPos.getLatitudeDeg() );
87 fgSetDouble(
"/orientation/heading-deg", aHeadingDeg );
90static void fgApplyStartOffset(
const SGGeod& aStartPos,
double aHeading,
double aTargetHeading = HUGE_VAL)
92 SGGeod startPos(aStartPos);
93 if (aTargetHeading == HUGE_VAL) {
94 aTargetHeading = aHeading;
97 if ( fabs(
fgGetDouble(
"/sim/presets/offset-distance-nm") ) > SG_EPSILON ) {
98 double offsetDistance =
fgGetDouble(
"/sim/presets/offset-distance-nm");
99 offsetDistance *= SG_NM_TO_METER;
100 double offsetAzimuth = aHeading;
101 if ( fabs(
fgGetDouble(
"/sim/presets/offset-azimuth-deg")) > SG_EPSILON ) {
102 offsetAzimuth =
fgGetDouble(
"/sim/presets/offset-azimuth-deg");
103 aHeading = aTargetHeading;
106 startPos = SGGeodesy::direct(startPos, offsetAzimuth + 180, offsetDistance);
114 fgSetString(
"/sim/atc/runway", runway->ident().c_str());
115 double offsetNm =
fgGetDouble(
"/sim/presets/offset-distance-nm");
116 double startOffset =
fgGetDouble(
"/sim/airport/runways/start-offset-m", 5.0);
117 SGGeod pos = runway->pointOnCenterline(startOffset);
119 const bool overrideHoldShort =
fgGetBool(
"/sim/presets/mp-hold-short-override",
false);
122 SG_LOG( SG_GENERAL, SG_MANDATORY_INFO,
"Requested to start on " << runway->airport()->ident() <<
"/" <<
123 runway->ident() <<
", MP is enabled so computing hold short position to avoid runway incursion");
130 double margin = startOffset + (runway->widthM() * 1.5);
134 fgSetBool(
"/sim/presets/avoided-mp-runway",
true);
135 return std::make_tuple(taxiNode->geod(), SGGeodesy::courseDeg(taxiNode->geod(), pos));
140 SG_LOG( SG_GENERAL, SG_ALERT,
"Unable to position off runway because groundnet has no taxi node.");
144 SG_LOG( SG_GENERAL, SG_ALERT,
"Unable to position off runway because no groundnet.");
148 return std::make_tuple(pos, runway->headingDeg());
157 SG_LOG( SG_GENERAL, SG_INFO,
158 "Attempting to set starting position from airport code "
159 <<
id <<
" heading " << tgt_hdg );
162 if (!apt)
return false;
165 double heading = tgt_hdg;
170 startPos = apt->
geod();
185 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to find airport " <<
id );
197 if (!aiManager || !aiManager->getUserAircraft()) {
198 SG_LOG( SG_GENERAL, SG_ALERT,
"finalizePositionForParkpos: >> failed to find AI manager / user aircraft");
204 SG_LOG( SG_GENERAL, SG_ALERT,
"finalizePositionForParkpos: >> failed to find user aircraft AI flight-plan");
210 SG_LOG( SG_GENERAL, SG_ALERT,
211 "Failed to find a parking at airport " <<
id <<
":" << parkpos);
215 fgSetString(
"/sim/presets/parkpos", pkr->getName());
227 SG_LOG( SG_GENERAL, SG_INFO,
228 "Attempting to set starting position for "
229 <<
id <<
":" << rwy );
233 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to find airport:" <<
id);
252 + rwy +
" not found at airport " + apt->
getId()
255 SG_LOG( SG_GENERAL, SG_INFO,
256 "Failed to find runway/helipad " << rwy <<
257 " at airport " <<
id <<
". Using default runway." );
265 string apt_id =
fgGetString(
"/sim/presets/airport-id");
266 double gs = SGMiscd::deg2rad(
fgGetDouble(
"/sim/presets/glideslope-deg"));
267 double od =
fgGetDouble(
"/sim/presets/offset-distance-nm");
268 double alt =
fgGetDouble(
"/sim/presets/altitude-ft");
270 double apt_elev = 0.0;
271 if ( ! apt_id.empty() ) {
273 if ( apt_elev < -9990.0 ) {
280 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
282 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
283 alt = fabs(od*tan(gs)) + apt_elev;
285 fgSetBool(
"/sim/presets/onground",
false);
286 SG_LOG( SG_GENERAL, SG_INFO,
"Calculated altitude as: "
288 }
else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
290 od = (alt - apt_elev) / tan(gs);
291 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
292 fgSetDouble(
"/sim/presets/offset-distance-nm", od);
293 fgSetBool(
"/sim/presets/onground",
false);
294 SG_LOG( SG_GENERAL, SG_INFO,
"Calculated offset distance as: "
296 }
else if( fabs(gs) > 0.01 ) {
297 SG_LOG( SG_GENERAL, SG_ALERT,
298 "Glideslope given but not altitude or offset-distance." );
299 SG_LOG( SG_GENERAL, SG_ALERT,
"Resetting glideslope to zero" );
301 fgSetBool(
"/sim/presets/onground",
true);
321 if (navlist.empty()) {
322 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to locate NAV = "
323 <<
id <<
":" << freq );
327 if( navlist.size() > 1 ) {
328 std::ostringstream buf;
329 buf <<
"Ambigous NAV-ID: '" <<
id <<
"'. Specify id and frequency. Available stations:" << endl;
330 for(
const auto& nav : navlist ) {
334 buf << nav->ident() <<
" "
335 << std::setprecision(5) <<
static_cast<double>(nav->get_freq() * factor) <<
" "
336 << nav->get_lat() <<
"/" << nav->get_lon()
340 SG_LOG( SG_GENERAL, SG_ALERT, buf.str() );
355 if (initialPos.first) {
358 fgSetDouble(
"/sim/presets/longitude-deg", initialPos.second.getLongitudeDeg());
359 fgSetDouble(
"/sim/presets/latitude-deg", initialPos.second.getLatitudeDeg());
361 const std::string carrier =
fgGetString(
"/sim/presets/carrier");
362 const std::string carrierpos =
fgGetString(
"/sim/presets/carrier-position");
363 const std::string parkpos =
fgGetString(
"/sim/presets/parkpos");
365 if (!carrier.empty())
367 std::string cpos = simgear::strutils::lowercase(carrierpos);
369 if (cpos ==
"flols" || cpos ==
"abeam")
370 fgSetInt(
"/sim/presets/carrier-course", 3);
371 else if (parkpos.find(
"cat") != std::string::npos)
372 fgSetInt(
"/sim/presets/carrier-course", 2);
374 fgSetInt(
"/sim/presets/carrier-course", 1);
377 fgSetInt(
"/sim/presets/carrier-course", 0);
379 SG_LOG( SG_GENERAL, SG_DEBUG,
"Initial carrier pos = " << initialPos.second <<
" course " <<
fgGetInt(
"/sim/presets/carrier-course"));
383 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to locate aircraft carrier = " << carrier );
389 SGVec3d cartPos = carrierRef->getCartPos();
390 auto framestamp =
globals->get_renderer()->getFrameStamp();
391 simgear::CheckSceneryVisitor csnv(
globals->get_scenery()->getPager(),
399 carrierRef->getSceneBranch()->accept(csnv);
400 if (!csnv.isLoaded()) {
405 if (!carrierRef->modelLoaded()) {
418 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to locate aircraft carrier = "
431 if (carrierRef->getParkPosition(posid, geodPos, heading, uvw)) {
434 double lon = geodPos.getLongitudeDeg();
435 double lat = geodPos.getLatitudeDeg();
436 double alt = geodPos.getElevationFt() + 2.0;
438 SG_LOG( SG_GENERAL, SG_INFO,
"Attempting to set starting position for "
439 << carrier <<
" at lat = " << lat <<
", lon = " << lon
440 <<
", alt = " << alt <<
", heading = " << heading);
459 fgSetBool(
"/sim/presets/onground",
true);
464 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to locate aircraft carrier = " << carrier );
472 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to locate aircraft carrier = "
479 SG_LOG(SG_GENERAL, SG_DEBUG,
"carrier scenery not yet loaded");
483 SGGeod flolsPosition;
484 double headingToFLOLS;
485 if (!carrierRef->getFLOLSPositionHeading(flolsPosition, headingToFLOLS)) {
486 SG_LOG( SG_GENERAL, SG_ALERT,
"Unable to compiute FLOLS position for carrier = "
491 const auto flolsElevationFt = flolsPosition.getElevationFt();
492 double gs = SGMiscd::deg2rad(carrierRef->getFLOLFSGlidepathAngleDeg());
493 const double od =
fgGetDouble(
"/sim/presets/offset-distance-nm");
499 startPos = SGGeodesy::direct(flolsPosition, headingToFLOLS - 90, od * SG_NM_TO_METER);
501 startPos = SGGeodesy::direct(flolsPosition, headingToFLOLS + 180, od * SG_NM_TO_METER);
504 double alt =
fgGetDouble(
"/sim/presets/altitude-ft");
508 const double offsetFt = od * SG_NM_TO_METER * SG_METER_TO_FEET;
509 startPos.setElevationFt(fabs(offsetFt*tan(gs)) + flolsElevationFt);
511 startPos.setElevationFt(alt);
514 fgSetDouble(
"/sim/presets/longitude-deg", startPos.getLongitudeDeg());
515 fgSetDouble(
"/sim/presets/latitude-deg", startPos.getLatitudeDeg());
516 fgSetDouble(
"/sim/presets/altitude-ft", startPos.getElevationFt());
517 fgSetDouble(
"/sim/presets/heading-deg", abeam ? (headingToFLOLS - 180) : headingToFLOLS);
518 fgSetDouble(
"/position/longitude-deg", startPos.getLongitudeDeg());
519 fgSetDouble(
"/position/latitude-deg", startPos.getLatitudeDeg());
520 fgSetDouble(
"/position/altitude-ft", startPos.getElevationFt());
521 fgSetDouble(
"/orientation/heading-deg", headingToFLOLS);
522 fgSetBool(
"/sim/presets/onground",
false);
539 SG_LOG( SG_GENERAL, SG_ALERT,
"Failed to locate fix = " <<
id );
556 double gs = SGMiscd::deg2rad(
fgGetDouble(
"/sim/presets/glideslope-deg"));
557 double od =
fgGetDouble(
"/sim/presets/offset-distance-nm");
558 double alt =
fgGetDouble(
"/sim/presets/altitude-ft");
560 bool set_pos =
false;
569 if ( fabs( gs ) > 0.01 ) {
580 double lon_deg =
fgGetDouble(
"/sim/presets/longitude-deg");
581 double lat_deg =
fgGetDouble(
"/sim/presets/latitude-deg");
582 if ( lon_deg >= -180.0 && lon_deg <= 180.0
583 && lat_deg >= -90.0 && lat_deg <= 90.0 )
588 string apt =
fgGetString(
"/sim/presets/airport-id");
589 const bool apt_req =
fgGetBool(
"/sim/presets/airport-requested");
590 string rwy_no =
fgGetString(
"/sim/presets/runway");
591 bool rwy_req =
fgGetBool(
"/sim/presets/runway-requested");
593 double vor_freq =
fgGetDouble(
"/sim/presets/vor-freq");
595 double ndb_freq =
fgGetDouble(
"/sim/presets/ndb-freq");
596 string carrier =
fgGetString(
"/sim/presets/carrier");
597 string parkpos =
fgGetString(
"/sim/presets/parkpos");
599 const auto tacan =
fgGetString(
"/sim/presets/tacan-id");
604 SGPropertyNode *hdg_preset =
fgGetNode(
"/sim/presets/heading-deg",
true);
605 double hdg = hdg_preset->getDoubleValue();
606 double original_hdg = hdg_preset->getDoubleValue();
610 static bool start_options_saved =
false;
611 if (!start_options_saved) {
612 start_options_saved =
true;
613 SGPropertyNode *opt =
fgGetNode(
"/sim/startup/options",
true);
615 opt->setDoubleValue(
"latitude-deg", lat_deg);
616 opt->setDoubleValue(
"longitude-deg", lon_deg);
617 opt->setDoubleValue(
"heading-deg", hdg);
618 opt->setStringValue(
"airport", apt.c_str());
619 opt->setStringValue(
"runway", rwy_no.c_str());
623 hdg =
fgGetDouble(
"/environment/config/boundary/entry/wind-from-heading-deg", 270);
626 if ( !set_pos && !carrier.empty() ) {
635 if (apt_req && !rwy_req) {
643 if ( !set_pos && !apt.empty() && !parkpos.empty() ) {
654 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
661 if ( !set_pos && !apt.empty() ) {
670 if (!apt.empty() && set_pos) {
672 fgSetString(
"/sim/airport/closest-airport-id", apt.c_str());
676 if (original_hdg < 9990.0) {
678 hdg_preset->setDoubleValue(original_hdg);
681 if ( !set_pos && !vor.empty() ) {
688 if ( !set_pos && !ndb.empty() ) {
695 if ( !set_pos && !fix.empty() ) {
702 if (!set_pos & !tacan.empty()) {
711 const std::string defaultAirportId =
fgGetString(
"/sim/presets/airport-id");
714 const SGGeod & airportGeod = airport->
geod();
715 fgSetDouble(
"/sim/presets/longitude-deg", airportGeod.getLongitudeDeg());
716 fgSetDouble(
"/sim/presets/latitude-deg", airportGeod.getLatitudeDeg());
720 fgSetDouble(
"/sim/presets/longitude-deg", -122.374843);
721 fgSetDouble(
"/sim/presets/latitude-deg", 37.619002);
722 SG_LOG(SG_GENERAL, SG_ALERT,
"Sorry, the default airport ('" << defaultAirportId
723 <<
"') seems to be unknown.");
731 fgSetDouble(
"/orientation/heading-deg", hdg_preset->getDoubleValue());
734 if ((fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1) && carrier.empty()) {
735 fgSetBool(
"/sim/presets/onground",
false);
737 fgSetBool(
"/sim/presets/onground",
true);
740 fgSetBool(
"/sim/position-finalized",
false);
752 double hdg =
fgGetDouble(
"/environment/metar/base-wind-dir-deg", 9999.0 );
753 string apt =
fgGetString(
"/sim/presets/airport-id");
754 double strthdg =
fgGetDouble(
"/sim/startup/options/heading-deg", 9999.0 );
755 string parkpos =
fgGetString(
"/sim/presets/parkpos" );
756 bool onground =
fgGetBool(
"/sim/presets/onground",
false );
757 const bool rwy_req =
fgGetBool(
"/sim/presets/runway-requested");
759 bool needMetar = (hdg < 360.0) &&
769 SG_LOG(SG_GENERAL, SG_WARN,
"finalizePosition: timed out waiting for METAR fetch");
773 if (
fgGetBool(
"/environment/metar/failure" )) {
774 SG_LOG(SG_ENVIRONMENT, SG_INFO,
"metar download failed, not waiting");
778 if (!
fgGetBool(
"/environment/metar/valid" )) {
782 SG_LOG(SG_ENVIRONMENT, SG_INFO,
783 "Using METAR for runway selection: '" <<
fgGetString(
"/environment/metar/data") <<
"'" );
808 const std::string carrier =
fgGetString(
"/sim/presets/carrier");
809 const std::string carrierpos =
fgGetString(
"/sim/presets/carrier-position");
810 const std::string parkpos =
fgGetString(
"/sim/presets/parkpos");
811 const std::string runway =
fgGetString(
"/sim/presets/runway");
812 const std::string apt =
fgGetString(
"/sim/presets/airport-id");
814 if (!carrier.empty())
824 std::string cpos = simgear::strutils::lowercase(carrierpos);
826 const bool inair = (cpos ==
"flols") || (cpos ==
"abeam");
827 const bool abeam = (cpos ==
"abeam");
838 }
else if (carrierResult ==
Failure) {
839 SG_LOG(SG_GENERAL, SG_ALERT,
"secondary carrier init failed");
845 SG_LOG(SG_GENERAL, SG_ALERT,
"Timeout waiting for carrier scenery to load, will start on the water.");
850 }
else if (!apt.empty() && !parkpos.empty()) {
855 SG_LOG(SG_GENERAL, SG_WARN,
"finalizePositionForParkPos failed, reverting to best runway");
865 fgSetBool(
"/sim/position-finalized", done);
867 globals->get_event_mgr()->removeTask(
"finalizePosition");
const FGAirport * fgFindAirportID(const std::string &id)
double fgGetAirportElev(const std::string &id)
SGSharedPtr< FGTaxiNode > FGTaxiNodeRef
SGSharedPtr< FGRunway > FGRunwayRef
FGAIFlightPlan * GetFlightPlan() const
static std::pair< bool, SGGeod > initialPositionForCarrier(const std::string &namePennant)
static SGSharedPtr< FGAICarrier > findCarrierByNameOrPennant(const std::string &namePennant)
type-safe wrapper around AIManager::getObjectFromProperty
FGParking * getParkingGate() const
FGAIAircraft * getUserAircraft() const
Retrieve the representation of the user's aircraft in the AI manager the position and velocity of thi...
FGRunwayRef findBestRunwayForHeading(double aHeading, struct FindBestRunwayForHeadingParams *parms=NULL) const
FGHelipadRef getHelipadByIndex(unsigned int aIndex) const
FGRunwayRef getRunwayByIdent(const std::string &aIdent) const
unsigned int numHelipads() const
bool hasHelipadWithIdent(const std::string &aIdent) const
bool hasRunwayWithIdent(const std::string &aIdent) const
const std::string & getName() const
const std::string & getId() const
FGHelipadRef getHelipadByIdent(const std::string &aIdent) const
FGGroundNetwork * groundNetwork() const
FGTaxiNodeRef findNearestNodeOffRunway(const SGGeod &aGeod, FGRunway *aRunway, double distanceM) const
FGAirport * airport() const
static bool isMultiplayerRequested()
helper to determine early in startup, if MP will be used.
static nav_list_type findByIdentAndFreq(const std::string &ident, const double freq, TypeFilter *filter=NULL)
virtual const SGGeod & geod() const
static FGPositionedRef findFirstWithIdent(const std::string &aIdent, Filter *aFilter)
static SGSharedPtr< T > loadById(PositionedID id)
@ DME
important that DME & TACAN are adjacent to keep the TacanFilter efficient - DMEs are proxies for TACA...
double headingDeg() const
Runway heading in degrees.
int fgGetInt(const char *name, int defaultValue)
Get an int value for a property.
std::string fgGetString(const char *name, const char *defaultValue)
Get a string value for a property.
bool fgSetInt(const char *name, int val)
Set an int value for a property.
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
MessageBoxResult modalMessageBox(const std::string &caption, const std::string &msg, const std::string &moreText)
static InitPosResult setInitialPosFromCarrier(const string &carrier)
static bool airportParkingSetVicinity(const string &id)
static bool finalizePositionForParkpos(const string &id, const string &parkpos)
static bool setPosFromAirportIDandHdg(const string &id, double tgt_hdg)
std::tuple< SGGeod, double > runwayStartPos(FGRunwayRef runway)
static bool fgSetPosFromNAV(const string &id, const double &freq, FGPositioned::Type type, PositionedID guid)
static SGTimeStamp global_finalizeTime
to avoid blocking when metar-fetch is enabled, but the network is unresponsive, we need a timeout val...
static void fgSetDistOrAltFromGlideSlope()
static void fgApplyStartOffset(const SGGeod &aStartPos, double aHeading, double aTargetHeading=HUGE_VAL)
SGSharedPtr< FGPositioned > FGPositionedRef
static bool global_callbackRegistered
static bool fgSetPosFromFix(const string &id, PositionedID guid)
static InitPosResult checkCarrierSceneryLoaded(const SGSharedPtr< FGAICarrier > carrierRef)
static InitPosResult setFinalPosFromCarrierFLOLS(const string &carrier, bool abeam)
static void setInitialPosition(const SGGeod &aPos, double aHeadingDeg)
static bool fgSetPosFromAirportIDandRwy(const string &id, const string &rwy, bool rwy_req)
static InitPosResult setFinalPosFromCarrier(const string &carrier, const string &posid)
SGSharedPtr< FGNavRecord > FGNavRecordRef
std::vector< nav_rec_ptr > nav_list_type
bool fgSetDouble(const char *name, double defaultValue)
Set a double value for a property.
bool fgGetBool(char const *name, bool def)
Get a bool value for a property.
bool fgSetBool(char const *name, bool val)
Set a bool value for a property.
double fgGetDouble(const char *name, double defaultValue)
Get a double value for a property.
bool fgSetString(char const *name, char const *str)
Set a string value for a property.
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.