| 1 |
// positioninit.cxx - helpers relating to setting initial aircraft position |
| 2 |
// |
| 3 |
// Copyright (C) 2012 James Turner [email protected] |
| 4 |
// |
| 5 |
// This program is free software; you can redistribute it and/or |
| 6 |
// modify it under the terms of the GNU General Public License as |
| 7 |
// published by the Free Software Foundation; either version 2 of the |
| 8 |
// License, or (at your option) any later version. |
| 9 |
// |
| 10 |
// This program is distributed in the hope that it will be useful, but |
| 11 |
// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
// General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with this program; if not, write to the Free Software |
| 17 |
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
|
| 19 |
#include "config.h" |
| 20 |
|
| 21 |
#include "positioninit.hxx" |
| 22 |
|
| 23 |
#include <osgViewer/Viewer> |
| 24 |
#include <osg/PagedLOD> |
| 25 |
|
| 26 |
// simgear |
| 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> |
| 33 |
|
| 34 |
#include "globals.hxx" |
| 35 |
#include "fg_props.hxx" |
| 36 |
#include "fg_io.hxx" |
| 37 |
|
| 38 |
#include <Navaids/navlist.hxx> |
| 39 |
#include <Airports/runways.hxx> |
| 40 |
#include <Airports/airport.hxx> |
| 41 |
#include <Airports/dynamics.hxx> |
| 42 |
#include <Airports/groundnetwork.hxx> |
| 43 |
|
| 44 |
#include <AIModel/AIManager.hxx> |
| 45 |
#include <AIModel/AICarrier.hxx> |
| 46 |
#include <AIModel/AIAircraft.hxx> |
| 47 |
#include <AIModel/AIFlightPlan.hxx> |
| 48 |
|
| 49 |
#include <Scenery/scenery.hxx> |
| 50 |
#include <GUI/MessageBox.hxx> |
| 51 |
#include <Viewer/renderer.hxx> |
| 52 |
|
| 53 |
using std::endl; |
| 54 |
using std::string; |
| 55 |
|
| 56 |
|
| 57 |
namespace flightgear |
| 58 |
{ |
| 59 |
|
| 60 |
|
| 61 |
enum InitPosResult { |
| 62 |
ExactPosition, |
| 63 |
VicinityPosition, |
| 64 |
ContinueWaiting, |
| 65 |
Failure |
| 66 |
}; |
| 67 |
|
| 68 |
/// to avoid blocking when metar-fetch is enabled, but the network is |
| 69 |
/// unresponsive, we need a timeout value. This value is reset on initPosition, |
| 70 |
/// and tracked through each call to finalizePosition. |
| 71 |
static SGTimeStamp global_finalizeTime; |
| 72 |
static bool global_callbackRegistered = false; |
| 73 |
|
| 74 |
void finalizePosition(); |
| 75 |
|
| 76 |
|
| 77 |
static void setInitialPosition(const SGGeod& aPos, double aHeadingDeg) |
| 78 |
{ |
| 79 |
// presets |
| 80 |
fgSetDouble("/sim/presets/longitude-deg", aPos.getLongitudeDeg() ); |
| 81 |
fgSetDouble("/sim/presets/latitude-deg", aPos.getLatitudeDeg() ); |
| 82 |
fgSetDouble("/sim/presets/heading-deg", aHeadingDeg ); |
| 83 |
|
| 84 |
// other code depends on the actual values being set ... |
| 85 |
fgSetDouble("/position/longitude-deg", aPos.getLongitudeDeg() ); |
| 86 |
fgSetDouble("/position/latitude-deg", aPos.getLatitudeDeg() ); |
| 87 |
fgSetDouble("/orientation/heading-deg", aHeadingDeg ); |
| 88 |
} |
| 89 |
|
| 90 |
static void fgApplyStartOffset(const SGGeod& aStartPos, double aHeading, double aTargetHeading = HUGE_VAL) |
| 91 |
{ |
| 92 |
SGGeod startPos(aStartPos); |
| 93 |
if (aTargetHeading == HUGE_VAL) { |
| 94 |
aTargetHeading = aHeading; |
| 95 |
} |
| 96 |
|
| 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; |
| 104 |
} |
| 105 |
|
| 106 |
startPos = SGGeodesy::direct(startPos, offsetAzimuth + 180, offsetDistance); |
| 107 |
} |
| 108 |
|
| 109 |
setInitialPosition(startPos, aHeading); |
| 110 |
} |
| 111 |
|
| 112 |
std::tuple<SGGeod, double> runwayStartPos(FGRunwayRef runway) |
| 113 |
{ |
| 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); |
| 118 |
|
| 119 |
const bool overrideHoldShort = fgGetBool("/sim/presets/mp-hold-short-override", false); |
| 120 |
|
| 121 |
if (!overrideHoldShort && FGIO::isMultiplayerRequested() && (fabs(offsetNm) <0.1)) { |
| 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"); |
| 124 |
|
| 125 |
FGGroundNetwork* groundNet = runway->airport()->groundNetwork(); |
| 126 |
|
| 127 |
if (groundNet) { |
| 128 |
// add a margin, try to keep the entire aeroplane comfortable off the |
| 129 |
// runway. |
| 130 |
double margin = startOffset + (runway->widthM() * 1.5); |
| 131 |
FGTaxiNodeRef taxiNode = groundNet->findNearestNodeOffRunway(pos, runway, margin); |
| 132 |
if (taxiNode) { |
| 133 |
// set this so multiplayer.nas can inform the user |
| 134 |
fgSetBool("/sim/presets/avoided-mp-runway", true); |
| 135 |
return std::make_tuple(taxiNode->geod(), SGGeodesy::courseDeg(taxiNode->geod(), pos)); |
| 136 |
} |
| 137 |
else { |
| 138 |
// if we couldn't find a suitable taxi-node, give up. Guessing a position |
| 139 |
// causes too much pain (starting in the water or similar bad things) |
| 140 |
SG_LOG( SG_GENERAL, SG_ALERT, "Unable to position off runway because groundnet has no taxi node."); |
| 141 |
} |
| 142 |
} |
| 143 |
else { |
| 144 |
SG_LOG( SG_GENERAL, SG_ALERT, "Unable to position off runway because no groundnet."); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return std::make_tuple(pos, runway->headingDeg()); |
| 149 |
} |
| 150 |
|
| 151 |
// Set current_options lon/lat given an airport id and heading (degrees) |
| 152 |
static bool setPosFromAirportIDandHdg( const string& id, double tgt_hdg ) { |
| 153 |
if ( id.empty() ) |
| 154 |
return false; |
| 155 |
|
| 156 |
// set initial position from runway and heading |
| 157 |
SG_LOG( SG_GENERAL, SG_INFO, |
| 158 |
"Attempting to set starting position from airport code " |
| 159 |
<< id << " heading " << tgt_hdg ); |
| 160 |
|
| 161 |
const FGAirport* apt = fgFindAirportID(id); |
| 162 |
if (!apt) return false; |
| 163 |
|
| 164 |
SGGeod startPos; |
| 165 |
double heading = tgt_hdg; |
| 166 |
if (apt->type() == FGPositioned::HELIPORT) { |
| 167 |
if (apt->numHelipads() > 0) { |
| 168 |
startPos = apt->getHelipadByIndex(0)->geod(); |
| 169 |
} else { |
| 170 |
startPos = apt->geod(); |
| 171 |
} |
| 172 |
} else { |
| 173 |
FGRunway* r = apt->findBestRunwayForHeading(tgt_hdg); |
| 174 |
std::tie(startPos, heading) = runwayStartPos(r); |
| 175 |
} |
| 176 |
|
| 177 |
fgApplyStartOffset(startPos, heading); |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
static bool airportParkingSetVicinity(const string& id) |
| 182 |
{ |
| 183 |
const FGAirport* apt = fgFindAirportID(id); |
| 184 |
if (!apt) { |
| 185 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to find airport " << id ); |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
setInitialPosition(apt->geod(), 0.0); |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
// Set current_options lon/lat given an airport id and parkig position name |
| 194 |
static bool finalizePositionForParkpos( const string& id, const string& parkpos ) |
| 195 |
{ |
| 196 |
auto aiManager = globals->get_subsystem<FGAIManager>(); |
| 197 |
if (!aiManager || !aiManager->getUserAircraft()) { |
| 198 |
SG_LOG( SG_GENERAL, SG_ALERT, "finalizePositionForParkpos: >> failed to find AI manager / user aircraft"); |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
auto userAIFP = aiManager->getUserAircraft()->GetFlightPlan(); |
| 203 |
if (!userAIFP) { |
| 204 |
SG_LOG( SG_GENERAL, SG_ALERT, "finalizePositionForParkpos: >> failed to find user aircraft AI flight-plan"); |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
auto pkr = userAIFP->getParkingGate(); |
| 209 |
if (!pkr) { |
| 210 |
SG_LOG( SG_GENERAL, SG_ALERT, |
| 211 |
"Failed to find a parking at airport " << id << ":" << parkpos); |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
fgSetString("/sim/presets/parkpos", pkr->getName()); |
| 216 |
fgApplyStartOffset(pkr->geod(), pkr->getHeading()); |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
// Set current_options lon/lat given an airport id and runway number |
| 222 |
static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy, bool rwy_req ) { |
| 223 |
if ( id.empty() ) |
| 224 |
return false; |
| 225 |
|
| 226 |
// set initial position from airport and runway number |
| 227 |
SG_LOG( SG_GENERAL, SG_INFO, |
| 228 |
"Attempting to set starting position for " |
| 229 |
<< id << ":" << rwy ); |
| 230 |
|
| 231 |
const FGAirport* apt = fgFindAirportID(id); |
| 232 |
if (!apt) { |
| 233 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to find airport:" << id); |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
if (apt->hasRunwayWithIdent(rwy)) { |
| 238 |
FGRunway* r(apt->getRunwayByIdent(rwy)); |
| 239 |
SGGeod startPos; |
| 240 |
double heading; |
| 241 |
std::tie(startPos, heading) = runwayStartPos(r); |
| 242 |
fgApplyStartOffset(startPos, heading); |
| 243 |
return true; |
| 244 |
} else if (apt->hasHelipadWithIdent(rwy)) { |
| 245 |
FGHelipad* h(apt->getHelipadByIdent(rwy)); |
| 246 |
fgApplyStartOffset(h->geod(), h->headingDeg()); |
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
if (rwy_req) { |
| 251 |
flightgear::modalMessageBox("Runway not available", "Runway/helipad " |
| 252 |
+ rwy + " not found at airport " + apt->getId() |
| 253 |
+ " - " + apt->getName() ); |
| 254 |
} else { |
| 255 |
SG_LOG( SG_GENERAL, SG_INFO, |
| 256 |
"Failed to find runway/helipad " << rwy << |
| 257 |
" at airport " << id << ". Using default runway." ); |
| 258 |
} |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
static void fgSetDistOrAltFromGlideSlope() |
| 264 |
{ |
| 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"); |
| 269 |
|
| 270 |
double apt_elev = 0.0; |
| 271 |
if ( ! apt_id.empty() ) { |
| 272 |
apt_elev = fgGetAirportElev( apt_id ); |
| 273 |
if ( apt_elev < -9990.0 ) { |
| 274 |
apt_elev = 0.0; |
| 275 |
} |
| 276 |
} else { |
| 277 |
apt_elev = 0.0; |
| 278 |
} |
| 279 |
|
| 280 |
if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) { |
| 281 |
// set altitude from glideslope and offset-distance |
| 282 |
od *= SG_NM_TO_METER * SG_METER_TO_FEET; |
| 283 |
alt = fabs(od*tan(gs)) + apt_elev; |
| 284 |
fgSetDouble("/sim/presets/altitude-ft", alt); |
| 285 |
fgSetBool("/sim/presets/onground", false); |
| 286 |
SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: " |
| 287 |
<< alt << " ft" ); |
| 288 |
} else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) { |
| 289 |
// set offset-distance from glideslope and altitude |
| 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: " |
| 295 |
<< od << " nm" ); |
| 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" ); |
| 300 |
fgSetDouble("/sim/presets/glideslope-deg", 0); |
| 301 |
fgSetBool("/sim/presets/onground", true); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
// Set current_options lon/lat given a Nav ID or GUID |
| 307 |
static bool fgSetPosFromNAV( const string& id, |
| 308 |
const double& freq, |
| 309 |
FGPositioned::Type type, |
| 310 |
PositionedID guid) |
| 311 |
{ |
| 312 |
FGNavRecordRef nav; |
| 313 |
if (guid != 0) { |
| 314 |
nav = FGPositioned::loadById<FGNavRecord>(guid); |
| 315 |
if (!nav) |
| 316 |
return false; |
| 317 |
} else { |
| 318 |
FGNavList::TypeFilter filter(type); |
| 319 |
const nav_list_type navlist = FGNavList::findByIdentAndFreq( id.c_str(), freq, &filter ); |
| 320 |
|
| 321 |
if (navlist.empty()) { |
| 322 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = " |
| 323 |
<< id << ":" << freq ); |
| 324 |
return false; |
| 325 |
} |
| 326 |
|
| 327 |
if( navlist.size() > 1 ) { |
| 328 |
std::ostringstream buf; |
| •329 |
buf << "Ambiguous NAV-ID: '" << id << "'. Specify id and frequency. Available stations:" << endl; |
| 330 |
for( const auto& nav : navlist ) { |
| 331 |
// NDB stored in kHz, VOR stored in MHz * 100 :-P |
| 332 |
double factor = nav->type() == FGPositioned::NDB ? 1.0 : 1/100.0; |
| 333 |
string unit = nav->type() == FGPositioned::NDB ? "kHz" : "MHz"; |
| 334 |
buf << nav->ident() << " " |
| 335 |
<< std::setprecision(5) << static_cast<double>(nav->get_freq() * factor) << " " |
| 336 |
<< nav->get_lat() << "/" << nav->get_lon() |
| 337 |
<< endl; |
| 338 |
} |
| 339 |
|
| 340 |
SG_LOG( SG_GENERAL, SG_ALERT, buf.str() ); |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
// nav list must be of length 1 |
| 345 |
nav = navlist[0]; |
| 346 |
} |
| 347 |
|
| 348 |
fgApplyStartOffset(nav->geod(), fgGetDouble("/sim/presets/heading-deg")); |
| 349 |
return true; |
| 350 |
} |
| 351 |
|
| 352 |
static InitPosResult setInitialPosFromCarrier( const string& carrier ) |
| 353 |
{ |
| 354 |
const auto initialPos = FGAICarrier::initialPositionForCarrier(carrier); |
| 355 |
if (initialPos.first) { |
| 356 |
// set these so scenery system has a vicinity to work with, and |
| 357 |
// so our PagedLOD is loaded |
| 358 |
fgSetDouble("/sim/presets/longitude-deg", initialPos.second.getLongitudeDeg()); |
| 359 |
fgSetDouble("/sim/presets/latitude-deg", initialPos.second.getLatitudeDeg()); |
| 360 |
|
| 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"); |
| 364 |
|
| 365 |
if (!carrier.empty()) |
| 366 |
{ |
| 367 |
std::string cpos = simgear::strutils::lowercase(carrierpos); |
| 368 |
|
| 369 |
if (cpos == "flols" || cpos == "abeam") |
| 370 |
fgSetInt("/sim/presets/carrier-course", 3);// base=1, launch=2, recovery=3 |
| 371 |
else if (parkpos.find("cat") != std::string::npos) |
| 372 |
fgSetInt("/sim/presets/carrier-course", 2);// base=1, launch=2, recovery=3 |
| 373 |
else |
| 374 |
fgSetInt("/sim/presets/carrier-course", 1); // base |
| 375 |
} |
| 376 |
else |
| 377 |
fgSetInt("/sim/presets/carrier-course", 0); // not defined. |
| 378 |
|
| 379 |
SG_LOG( SG_GENERAL, SG_DEBUG, "Initial carrier pos = " << initialPos.second << " course " << fgGetInt("/sim/presets/carrier-course")); |
| 380 |
return VicinityPosition; |
| 381 |
} |
| 382 |
|
| 383 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = " << carrier ); |
| 384 |
return Failure; |
| 385 |
} |
| 386 |
|
| 387 |
static InitPosResult checkCarrierSceneryLoaded(const SGSharedPtr<FGAICarrier> carrierRef) |
| 388 |
{ |
| 389 |
SGVec3d cartPos = carrierRef->getCartPos(); |
| 390 |
auto framestamp = globals->get_renderer()->getFrameStamp(); |
| 391 |
simgear::CheckSceneryVisitor csnv(globals->get_scenery()->getPager(), |
| 392 |
toOsg(cartPos), |
| 393 |
100.0 /* range in metres */, |
| 394 |
framestamp); |
| 395 |
|
| 396 |
// currently the PagedLODs will not be loaded by the DatabasePager |
| 397 |
// while the splashscreen is there, so CheckSceneryVisitor force-loads |
| 398 |
// missing objects in the main thread |
| 399 |
carrierRef->getSceneBranch()->accept(csnv); |
| 400 |
if (!csnv.isLoaded()) { |
| 401 |
return ContinueWaiting; |
| 402 |
} |
| 403 |
|
| 404 |
// and then wait for the load to actually be synced to the main thread |
| 405 |
if (!carrierRef->modelLoaded()) { |
| 406 |
return ContinueWaiting; |
| 407 |
} |
| 408 |
|
| 409 |
return VicinityPosition; |
| 410 |
} |
| 411 |
|
| 412 |
// Set current_options lon/lat given an aircraft carrier id |
| 413 |
static InitPosResult setFinalPosFromCarrier( const string& carrier, const string& posid ) |
| 414 |
{ |
| 415 |
|
| 416 |
SGSharedPtr<FGAICarrier> carrierRef = FGAICarrier::findCarrierByNameOrPennant(carrier); |
| 417 |
if (!carrierRef) { |
| 418 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = " |
| 419 |
<< carrier ); |
| 420 |
return Failure; |
| 421 |
} |
| 422 |
|
| 423 |
auto res = checkCarrierSceneryLoaded(carrierRef); |
| 424 |
if (res != VicinityPosition) { |
| 425 |
return res; // either failrue or keep waiting for scenery load |
| 426 |
} |
| 427 |
|
| 428 |
SGGeod geodPos; |
| 429 |
double heading; |
| 430 |
SGVec3d uvw; |
| 431 |
if (carrierRef->getParkPosition(posid, geodPos, heading, uvw)) { |
| 432 |
|
| 433 |
//////// |
| 434 |
double lon = geodPos.getLongitudeDeg(); |
| 435 |
double lat = geodPos.getLatitudeDeg(); |
| 436 |
double alt = geodPos.getElevationFt() + 2.0; |
| 437 |
|
| 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); |
| 441 |
|
| 442 |
fgSetDouble("/sim/presets/longitude-deg", lon); |
| 443 |
fgSetDouble("/sim/presets/latitude-deg", lat); |
| 444 |
fgSetDouble("/sim/presets/altitude-ft", alt); |
| 445 |
fgSetDouble("/sim/presets/heading-deg", heading); |
| 446 |
fgSetDouble("/position/longitude-deg", lon); |
| 447 |
fgSetDouble("/position/latitude-deg", lat); |
| 448 |
fgSetDouble("/position/altitude-ft", alt); |
| 449 |
fgSetDouble("/orientation/heading-deg", heading); |
| 450 |
|
| 451 |
fgSetString("/sim/presets/speed-set", "UVW"); |
| 452 |
fgSetDouble("/velocities/uBody-fps", uvw(0)); |
| 453 |
fgSetDouble("/velocities/vBody-fps", uvw(1)); |
| 454 |
fgSetDouble("/velocities/wBody-fps", uvw(2)); |
| 455 |
fgSetDouble("/sim/presets/uBody-fps", uvw(0)); |
| 456 |
fgSetDouble("/sim/presets/vBody-fps", uvw(1)); |
| 457 |
fgSetDouble("/sim/presets/wBody-fps", uvw(2)); |
| 458 |
|
| 459 |
fgSetBool("/sim/presets/onground", true); |
| 460 |
|
| 461 |
///////// |
| 462 |
return ExactPosition; |
| 463 |
} |
| 464 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = " << carrier ); |
| 465 |
return Failure; |
| 466 |
} |
| 467 |
|
| 468 |
static InitPosResult setFinalPosFromCarrierFLOLS(const string& carrier, bool abeam) |
| 469 |
{ |
| 470 |
SGSharedPtr<FGAICarrier> carrierRef = FGAICarrier::findCarrierByNameOrPennant(carrier); |
| 471 |
if (!carrierRef) { |
| 472 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = " |
| 473 |
<< carrier ); |
| 474 |
return Failure; |
| 475 |
} |
| 476 |
|
| 477 |
auto res = checkCarrierSceneryLoaded(carrierRef); |
| 478 |
if (res != VicinityPosition) { |
| 479 |
SG_LOG(SG_GENERAL, SG_DEBUG, "carrier scenery not yet loaded"); |
| 480 |
return res; // either failure or keep waiting for scenery load |
| 481 |
} |
| 482 |
|
| 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 = " |
| 487 |
<< carrier ); |
| 488 |
return Failure; |
| 489 |
} |
| 490 |
|
| 491 |
const auto flolsElevationFt = flolsPosition.getElevationFt(); |
| 492 |
double gs = SGMiscd::deg2rad(carrierRef->getFLOLFSGlidepathAngleDeg()); |
| 493 |
const double od = fgGetDouble("/sim/presets/offset-distance-nm"); |
| 494 |
|
| 495 |
// start position |
| 496 |
SGGeod startPos; |
| 497 |
if (abeam) { |
| 498 |
// If we're starting from the abeam position, we are opposite the FLOLS, downwind on a left hand circuit |
| 499 |
startPos = SGGeodesy::direct(flolsPosition, headingToFLOLS - 90, od * SG_NM_TO_METER); |
| 500 |
} else { |
| 501 |
startPos = SGGeodesy::direct(flolsPosition, headingToFLOLS + 180, od * SG_NM_TO_METER); |
| 502 |
} |
| 503 |
|
| 504 |
double alt = fgGetDouble("/sim/presets/altitude-ft"); |
| 505 |
|
| 506 |
if (alt < 0.0f) { |
| 507 |
// No altitude set, so base on glideslope |
| 508 |
const double offsetFt = od * SG_NM_TO_METER * SG_METER_TO_FEET; |
| 509 |
startPos.setElevationFt(fabs(offsetFt*tan(gs)) + flolsElevationFt); |
| 510 |
} else { |
| 511 |
startPos.setElevationFt(alt); |
| 512 |
} |
| 513 |
|
| 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); |
| 523 |
|
| 524 |
return ExactPosition; |
| 525 |
} |
| 526 |
|
| 527 |
// Set current_options lon/lat given a fix ident and GUID |
| 528 |
static bool fgSetPosFromFix( const string& id, PositionedID guid ) |
| 529 |
{ |
| 530 |
FGPositionedRef fix; |
| 531 |
if (guid != 0) { |
| 532 |
fix = FGPositioned::loadById<FGPositioned>(guid); |
| 533 |
} else { |
| 534 |
FGPositioned::TypeFilter fixFilter(FGPositioned::FIX); |
| 535 |
fix = FGPositioned::findFirstWithIdent(id, &fixFilter); |
| 536 |
} |
| 537 |
|
| 538 |
if (!fix) { |
| 539 |
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate fix = " << id ); |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
fgApplyStartOffset(fix->geod(), fgGetDouble("/sim/presets/heading-deg")); |
| 544 |
return true; |
| 545 |
} |
| 546 |
|
| 547 |
// Set the initial position based on presets (or defaults) |
| 548 |
bool initPosition() |
| 549 |
{ |
| 550 |
global_finalizeTime = SGTimeStamp(); // reset to invalid |
| 551 |
if (!global_callbackRegistered) { |
| 552 |
globals->get_event_mgr()->addTask("finalizePosition", &finalizePosition, 0.1); |
| 553 |
global_callbackRegistered = true; |
| 554 |
} |
| 555 |
|
| 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"); |
| 559 |
|
| 560 |
bool set_pos = false; |
| 561 |
|
| 562 |
// clear this value, so we don't preserve an old value and confuse |
| 563 |
// the ATC manager. We will set it again if it's valid |
| 564 |
fgSetString("/sim/atc/runway", ""); |
| 565 |
|
| 566 |
// If glideslope is specified, then calculate offset-distance or |
| 567 |
// altitude relative to glide slope if either of those was not |
| 568 |
// specified. |
| 569 |
if ( fabs( gs ) > 0.01 ) { |
| 570 |
fgSetDistOrAltFromGlideSlope(); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
// If we have an explicit, in-range lon/lat, don't change it, just use it. |
| 575 |
// If not, check for an airport-id and use that. |
| 576 |
// If not, default to the middle of the KSFO field. |
| 577 |
// The default values for lon/lat are deliberately out of range |
| 578 |
// so that the airport-id can take effect; valid lon/lat will |
| 579 |
// override airport-id, however. |
| 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 ) |
| 584 |
{ |
| 585 |
set_pos = true; |
| 586 |
} |
| 587 |
|
| 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"); |
| 592 |
string vor = fgGetString("/sim/presets/vor-id"); |
| 593 |
double vor_freq = fgGetDouble("/sim/presets/vor-freq"); |
| 594 |
string ndb = fgGetString("/sim/presets/ndb-id"); |
| 595 |
double ndb_freq = fgGetDouble("/sim/presets/ndb-freq"); |
| 596 |
string carrier = fgGetString("/sim/presets/carrier"); |
| 597 |
string parkpos = fgGetString("/sim/presets/parkpos"); |
| 598 |
string fix = fgGetString("/sim/presets/fix"); |
| 599 |
const auto tacan = fgGetString("/sim/presets/tacan-id"); |
| 600 |
|
| 601 |
// the launcher sets this to precisely identify a navaid |
| 602 |
PositionedID navaidId = fgGetInt("/sim/presets/navaid-id"); |
| 603 |
|
| 604 |
SGPropertyNode *hdg_preset = fgGetNode("/sim/presets/heading-deg", true); |
| 605 |
double hdg = hdg_preset->getDoubleValue(); |
| 606 |
double original_hdg = hdg_preset->getDoubleValue(); |
| 607 |
|
| 608 |
// save some start parameters, so that we can later say what the |
| 609 |
// user really requested. TODO generalize that and move it to options.cxx |
| 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); |
| 614 |
|
| 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()); |
| 620 |
} |
| 621 |
|
| 622 |
if (hdg > 9990.0) { |
| 623 |
hdg = fgGetDouble("/environment/config/boundary/entry/wind-from-heading-deg", 270); |
| 624 |
} |
| 625 |
|
| 626 |
if ( !set_pos && !carrier.empty() ) { |
| 627 |
// an aircraft carrier is requested |
| 628 |
const auto result = setInitialPosFromCarrier( carrier ); |
| 629 |
if (result != Failure) { |
| 630 |
// we at least found the carrier |
| 631 |
set_pos = true; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
if (apt_req && !rwy_req) { |
| 636 |
// ensure that if the users asks for a specific airport, but not a runway, |
| 637 |
// presumably because they want automatic selection, we do not look |
| 638 |
// for the default runway (from $FGDATA/location-preset.xml) which is |
| 639 |
// likely missing. |
| 640 |
rwy_no.clear(); |
| 641 |
} |
| 642 |
|
| 643 |
if ( !set_pos && !apt.empty() && !parkpos.empty() ) { |
| 644 |
// An airport + parking position is requested |
| 645 |
// since this depends on parking, which is part of dynamics, and hence |
| 646 |
// also depends on ATC (the ground controller), we need to defer this |
| 647 |
// until position finalisation |
| 648 |
// the rest of the work happens in finalizePosFromParkpos |
| 649 |
if ( airportParkingSetVicinity( apt ) ) { |
| 650 |
set_pos = true; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
if ( !set_pos && !apt.empty() && !rwy_no.empty() ) { |
| 655 |
// An airport + runway is requested |
| 656 |
if ( fgSetPosFromAirportIDandRwy( apt, rwy_no, rwy_req ) ) { |
| 657 |
set_pos = true; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
if ( !set_pos && !apt.empty() ) { |
| 662 |
// An airport is requested (find runway closest to hdg) |
| 663 |
if ( setPosFromAirportIDandHdg( apt, hdg ) ) { |
| 664 |
set_pos = true; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
// if an airport ID was requested, set closest-airport-id |
| 669 |
// and tower based upon it. |
| 670 |
if (!apt.empty() && set_pos) { |
| 671 |
// set tower position |
| 672 |
fgSetString("/sim/airport/closest-airport-id", apt.c_str()); |
| 673 |
fgSetString("/sim/tower/airport-id", apt.c_str()); |
| 674 |
} |
| 675 |
|
| 676 |
if (original_hdg < 9990.0) { |
| 677 |
// The user-set heading may be overridden by the setPosFromAirportID above. |
| 678 |
hdg_preset->setDoubleValue(original_hdg); |
| 679 |
} |
| 680 |
|
| 681 |
if ( !set_pos && !vor.empty() ) { |
| 682 |
// a VOR is requested |
| 683 |
if ( fgSetPosFromNAV( vor, vor_freq, FGPositioned::VOR, navaidId ) ) { |
| 684 |
set_pos = true; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
if ( !set_pos && !ndb.empty() ) { |
| 689 |
// an NDB is requested |
| 690 |
if ( fgSetPosFromNAV( ndb, ndb_freq, FGPositioned::NDB, navaidId ) ) { |
| 691 |
set_pos = true; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
if ( !set_pos && !fix.empty() ) { |
| 696 |
// a Fix is requested |
| 697 |
if ( fgSetPosFromFix( fix, navaidId ) ) { |
| 698 |
set_pos = true; |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
if (!set_pos & !tacan.empty()) { |
| 703 |
// we don't record TACANs in the NavCache right now, instead we |
| 704 |
// record DMEs, some of which have TACAN in the name. |
| 705 |
if (fgSetPosFromNAV(tacan, 0.0, FGPositioned::DME, navaidId)) { |
| 706 |
set_pos = true; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
if ( !set_pos ) { |
| 711 |
const std::string defaultAirportId = fgGetString("/sim/presets/airport-id"); |
| 712 |
const FGAirport* airport = fgFindAirportID(defaultAirportId); |
| 713 |
if( airport ) { |
| 714 |
const SGGeod & airportGeod = airport->geod(); |
| 715 |
fgSetDouble("/sim/presets/longitude-deg", airportGeod.getLongitudeDeg()); |
| 716 |
fgSetDouble("/sim/presets/latitude-deg", airportGeod.getLatitudeDeg()); |
| 717 |
} else { |
| 718 |
// So, the default airport is unknown? We are in serious trouble. |
| 719 |
// Let's hope KSFO still exists somehow |
| 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."); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
fgSetDouble( "/position/longitude-deg", |
| 728 |
fgGetDouble("/sim/presets/longitude-deg") ); |
| 729 |
fgSetDouble( "/position/latitude-deg", |
| 730 |
fgGetDouble("/sim/presets/latitude-deg") ); |
| 731 |
fgSetDouble( "/orientation/heading-deg", hdg_preset->getDoubleValue()); |
| 732 |
|
| 733 |
// determine if this should be an on-ground or in-air start |
| 734 |
if ((fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1) && carrier.empty()) { |
| 735 |
fgSetBool("/sim/presets/onground", false); |
| 736 |
} else { |
| 737 |
fgSetBool("/sim/presets/onground", true); |
| 738 |
} |
| 739 |
|
| 740 |
fgSetBool("/sim/position-finalized", false); |
| 741 |
|
| 742 |
// Initialize the longitude, latitude and altitude to the initial position |
| 743 |
fgSetDouble("/position/altitude-ft", fgGetDouble("/sim/presets/altitude-ft")); |
| 744 |
fgSetDouble("/position/longitude-deg", fgGetDouble("/sim/presets/longitude-deg")); |
| 745 |
fgSetDouble("/position/latitude-deg", fgGetDouble("/sim/presets/latitude-deg")); |
| 746 |
|
| 747 |
return true; |
| 748 |
} |
| 749 |
|
| 750 |
bool finalizeMetar() |
| 751 |
{ |
| 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"); |
| 758 |
// this logic is taken from former startup.nas |
| 759 |
bool needMetar = (hdg < 360.0) && |
| 760 |
!apt.empty() && |
| 761 |
(strthdg > 360.0) && |
| 762 |
!rwy_req && |
| 763 |
onground && |
| 764 |
parkpos.empty(); |
| 765 |
|
| 766 |
if (needMetar) { |
| 767 |
// timeout so we don't spin forever if the network is down |
| 768 |
if (global_finalizeTime.elapsedMSec() > fgGetInt("/sim/startup/metar-fetch-timeout-msec", 6000)) { |
| 769 |
SG_LOG(SG_GENERAL, SG_WARN, "finalizePosition: timed out waiting for METAR fetch"); |
| 770 |
return true; |
| 771 |
} |
| 772 |
|
| 773 |
if (fgGetBool( "/environment/metar/failure" )) { |
| 774 |
SG_LOG(SG_ENVIRONMENT, SG_INFO, "metar download failed, not waiting"); |
| 775 |
return true; |
| 776 |
} |
| 777 |
|
| 778 |
if (!fgGetBool( "/environment/metar/valid" )) { |
| 779 |
return false; |
| 780 |
} |
| 781 |
|
| 782 |
SG_LOG(SG_ENVIRONMENT, SG_INFO, |
| 783 |
"Using METAR for runway selection: '" << fgGetString("/environment/metar/data") << "'" ); |
| 784 |
setPosFromAirportIDandHdg( apt, hdg ); |
| 785 |
// fall through to return true |
| 786 |
} // of need-metar case |
| 787 |
|
| 788 |
return true; |
| 789 |
} |
| 790 |
|
| 791 |
void finalizePosition() |
| 792 |
{ |
| 793 |
// first call to finalize after an initPosition call |
| 794 |
if (global_finalizeTime.get_usec() == 0) { |
| 795 |
global_finalizeTime = SGTimeStamp::now(); |
| 796 |
} |
| 797 |
|
| 798 |
bool done = true; |
| 799 |
|
| 800 |
/* Scenarios require Nasal, so FGAIManager loads the scenarios, |
| 801 |
* including its models such as a/c carriers, in its 'postinit', |
| 802 |
* which is the very last thing we do. |
| 803 |
* flightgear::initPosition is called very early in main.cxx/fgIdleFunction, |
| 804 |
* one of the first things we do, long before scenarios/carriers are |
| 805 |
* loaded. => When requested "initial preset position" relates to a |
| 806 |
* carrier, recalculate the 'initial' position here |
| 807 |
*/ |
| 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"); |
| 813 |
|
| 814 |
if (!carrier.empty()) |
| 815 |
{ |
| 816 |
/* /sim/presets/carrier-position can take a number of different values |
| 817 |
- name of a parking/cataputt |
| 818 |
- FLOLS - to position on final approach |
| 819 |
- abeam - on an abeam position from the FLOLS, heading downwind |
| 820 |
- <empty> indicating a start position of a catapult |
| 821 |
*/ |
| 822 |
|
| 823 |
// Convert to lower case to simplify comparison |
| 824 |
std::string cpos = simgear::strutils::lowercase(carrierpos); |
| 825 |
|
| 826 |
const bool inair = (cpos == "flols") || (cpos == "abeam"); |
| 827 |
const bool abeam = (cpos == "abeam"); |
| 828 |
InitPosResult carrierResult; |
| 829 |
if (inair) { |
| 830 |
carrierResult = setFinalPosFromCarrierFLOLS(carrier, abeam); |
| 831 |
} else { |
| 832 |
// We don't simply use cpos as it is lower case, and the |
| 833 |
// search against parking/catapult positions is case-sensitive. |
| 834 |
carrierResult = setFinalPosFromCarrier(carrier, carrierpos); |
| 835 |
} |
| 836 |
if (carrierResult == ExactPosition) { |
| 837 |
done = true; |
| 838 |
} else if (carrierResult == Failure) { |
| 839 |
SG_LOG(SG_GENERAL, SG_ALERT, "secondary carrier init failed"); |
| 840 |
done = true; |
| 841 |
} else { |
| 842 |
done = false; |
| 843 |
// 60 second timeout on waiting for the carrier to load |
| 844 |
if (global_finalizeTime.elapsedMSec() > 60000) { |
| 845 |
SG_LOG(SG_GENERAL, SG_ALERT, "Timeout waiting for carrier scenery to load, will start on the water."); |
| 846 |
done = true; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
} else if (!apt.empty() && !parkpos.empty()) { |
| 851 |
// parking position depends on ATC / dynamics code to assign spaces, |
| 852 |
// so we wait until this point to initialise |
| 853 |
bool ok = finalizePositionForParkpos(apt, parkpos); |
| 854 |
if (!ok) { |
| 855 |
SG_LOG(SG_GENERAL, SG_WARN, "finalizePositionForParkPos failed, reverting to best runway"); |
| 856 |
|
| 857 |
// clear this so finalizeMetar works as expected |
| 858 |
fgSetString("/sim/presets/parkpos", ""); |
| 859 |
finalizeMetar(); |
| 860 |
} |
| 861 |
} else { |
| 862 |
done = finalizeMetar(); |
| 863 |
} |
| 864 |
|
| 865 |
fgSetBool("/sim/position-finalized", done); |
| 866 |
if (done) { |
| 867 |
globals->get_event_mgr()->removeTask("finalizePosition"); |
| 868 |
global_callbackRegistered = false; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
} // of namespace flightgear |