| 1 | // FlightPlan.cxx - flight plan object |
| 2 | |
| 3 | // Written by James Turner, started 2012. |
| 4 | // |
| 5 | // Copyright (C) 2012 Curtis L. Olson |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include "FlightPlan.hxx" |
| 24 | |
| 25 | // std |
| 26 | #include <map> |
| 27 | #include <fstream> |
| 28 | #include <cstring> |
| 29 | #include <cassert> |
| 30 | #include <algorithm> |
| 31 | |
| 32 | // SimGear |
| 33 | #include <simgear/structure/exception.hxx> |
| 34 | #include <simgear/misc/sg_path.hxx> |
| 35 | #include <simgear/magvar/magvar.hxx> |
| 36 | #include <simgear/timing/sg_time.hxx> |
| 37 | #include <simgear/io/iostreams/sgstream.hxx> |
| 38 | #include <simgear/misc/strutils.hxx> |
| 39 | #include <simgear/props/props_io.hxx> |
| 40 | #include <simgear/xml/easyxml.hxx> |
| 41 | |
| 42 | // FlightGear |
| 43 | #include <Main/globals.hxx> |
| 44 | #include "Main/fg_props.hxx" |
| 45 | #include <Navaids/procedure.hxx> |
| 46 | #include <Navaids/waypoint.hxx> |
| 47 | #include <Navaids/routePath.hxx> |
| 48 | #include <Navaids/airways.hxx> |
| 49 | #include <Autopilot/route_mgr.hxx> |
| 50 | #include <Aircraft/AircraftPerformance.hxx> |
| 51 | #include <Main/sentryIntegration.hxx> |
| 52 | |
| 53 | using std::string; |
| 54 | using std::vector; |
| 55 | using std::endl; |
| 56 | using std::fstream; |
| 57 | |
| 58 | namespace { |
| 59 | |
| 60 | const string_list static_icaoFlightRulesCode = { |
| 61 | "V", |
| 62 | "I", |
| 63 | "Y", |
| 64 | "Z" |
| 65 | }; |
| 66 | |
| 67 | const string_list static_icaoFlightTypeCode = { |
| 68 | "S", |
| 69 | "N", |
| 70 | "G", |
| 71 | "M", |
| 72 | "X" |
| 73 | }; |
| 74 | |
| 75 | } // of anonymous namespace |
| 76 | |
| 77 | namespace flightgear { |
| 78 | |
| 79 | // implemented in route.cxx |
| 80 | const char* restrictionToString(RouteRestriction aRestrict); |
| 81 | |
| 82 | typedef std::vector<FlightPlan::DelegateFactoryRef> FPDelegateFactoryVec; |
| 83 | static FPDelegateFactoryVec static_delegateFactories; |
| 84 | |
| 85 | FlightPlan::FlightPlan(bool isRoute) : |
| 86 | _isRoute(isRoute), |
| 87 | _currentIndex(-1), |
| 88 | _followLegTrackToFix(true), |
| 89 | _aircraftCategory(ICAO_AIRCRAFT_CATEGORY_C), |
| 90 | _departureRunway(nullptr), |
| 91 | _destinationRunway(nullptr), |
| 92 | _sid(nullptr), |
| 93 | _star(nullptr), |
| 94 | _approach(nullptr), |
| 95 | _totalDistance(0.0) |
| 96 | { |
| 97 | _departureChanged = _arrivalChanged = _waypointsChanged = _currentWaypointChanged = false; |
| 98 | _cruiseDataChanged = false; |
| 99 | |
| 100 | for (auto factory : static_delegateFactories) { |
| 101 | Delegate* d = factory->createFlightPlanDelegate(this); |
| 102 | if (d) { // factory might not always create a delegate |
| 103 | d->_factory = factory; // record for clean-up purposes |
| 104 | addDelegate(d); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | FlightPlanRef FlightPlan::create() |
| 110 | { |
| 111 | return new FlightPlan(false); |
| 112 | } |
| 113 | |
| 114 | FlightPlanRef FlightPlan::createRoute() |
| 115 | { |
| 116 | return new FlightPlan(true); |
| 117 | } |
| 118 | |
| 119 | FlightPlan::~FlightPlan() |
| 120 | { |
| 121 | // clean up delegates |
| 122 | for (auto d : _delegates) { |
| 123 | if (d->_factory) { |
| 124 | auto f = d->_factory; |
| 125 | f->destroyFlightPlanDelegate(this, d); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | FlightPlanRef FlightPlan::clone(const string& newIdent, bool convertIntoFlightPlan) const |
| 131 | { |
| 132 | // this is the only place we allow conversion of a route into an active FP, |
| 133 | // by design. Forces people to clone-to-a-flight-plan if they want to |
| 134 | // activate a route. |
| 135 | FlightPlanRef c = new FlightPlan(convertIntoFlightPlan ? false : _isRoute); |
| 136 | c->_ident = newIdent.empty() ? _ident : newIdent; |
| 137 | c->lockDelegates(); |
| 138 | |
| 139 | // copy destination / departure data. |
| 140 | c->setDeparture(_departure); |
| 141 | c->setDeparture(_departureRunway); |
| 142 | |
| 143 | if (_approach) { |
| 144 | c->setApproach(_approach, _approachTransition); |
| 145 | } else if (_destinationRunway) { |
| 146 | c->setDestination(_destinationRunway); |
| 147 | } else if (_destination) { |
| 148 | c->setDestination(_destination); |
| 149 | } |
| 150 | |
| 151 | c->setSTAR(_star, _starTransition); |
| 152 | c->setSID(_sid, _sidTransition); |
| 153 | |
| 154 | // mark data as unchanged since this is a clean plan |
| 155 | c->_arrivalChanged = false; |
| 156 | c->_departureChanged = false; |
| 157 | |
| 158 | // copy cruise data |
| 159 | if (_cruiseFlightLevel > 0) { |
| 160 | c->setCruiseFlightLevel(_cruiseFlightLevel); |
| 161 | } else if (_cruiseAltitudeFt > 0) { |
| 162 | c->setCruiseAltitudeFt(_cruiseAltitudeFt); |
| 163 | } else if (_cruiseAltitudeM > 0) { |
| 164 | c->setCruiseAltitudeM(_cruiseAltitudeM); |
| 165 | } |
| 166 | |
| 167 | if (_cruiseAirspeedMach > 0) { |
| 168 | c->setCruiseSpeedMach(_cruiseAirspeedMach); |
| 169 | } else if (_cruiseAirspeedKnots > 0) { |
| 170 | c->setCruiseSpeedKnots(_cruiseAirspeedKnots); |
| 171 | } else if (_cruiseAirspeedKph > 0) { |
| 172 | c->setCruiseSpeedKPH(_cruiseAirspeedKph); |
| 173 | } |
| 174 | |
| 175 | c->_didLoadFP = true; // set the loaded flag to give delegates a chance |
| 176 | |
| 177 | // copy legs |
| 178 | c->_waypointsChanged = true; |
| 179 | for (int l=0; l < numLegs(); ++l) { |
| 180 | c->_legs.push_back(_legs[l]->cloneFor(c)); |
| 181 | } |
| 182 | |
| 183 | c->expandVias(); |
| 184 | c->unlockDelegates(); |
| 185 | return c; |
| 186 | } |
| 187 | |
| 188 | void FlightPlan::setIdent(const string& s) |
| 189 | { |
| 190 | _ident = s; |
| 191 | } |
| 192 | |
| 193 | string FlightPlan::ident() const |
| 194 | { |
| 195 | return _ident; |
| 196 | } |
| 197 | |
| 198 | FlightPlan::LegRef FlightPlan::insertWayptAtIndex(Waypt* aWpt, int aIndex) |
| 199 | { |
| 200 | if (!aWpt) { |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
| 204 | WayptVec wps; |
| 205 | wps.push_back(aWpt); |
| 206 | |
| 207 | int index = aIndex; |
| 208 | if ((aIndex == -1) || (aIndex > (int) _legs.size())) { |
| 209 | index = _legs.size(); |
| 210 | } |
| 211 | |
| 212 | insertWayptsAtIndex(wps, index); |
| 213 | return legAtIndex(index); |
| 214 | } |
| 215 | |
| 216 | static WayptVec copyWaypointsExpandingVias(WayptRef preceeding, const WayptVec& wps) |
| 217 | { |
| 218 | WayptVec result; |
| 219 | result.reserve(wps.size()); |
| 220 | |
| 221 | for (auto wp : wps) { |
| 222 | if (wp->type() == "via") { |
| 223 | Via* via = static_cast<Via*>(wp.get()); |
| 224 | WayptVec viaPoints = via->expandToWaypoints(preceeding); |
| 225 | result.insert(result.end(), viaPoints.begin(), viaPoints.end()); |
| 226 | } else { |
| 227 | // everything else is copied directly |
| 228 | result.push_back(wp); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | void FlightPlan::insertWayptsAtIndex(const WayptVec& wps, int aIndex) |
| 236 | { |
| 237 | if (wps.empty()) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | int index = aIndex; |
| 242 | if ((aIndex == -1) || (aIndex > (int) _legs.size())) { |
| 243 | index = _legs.size(); |
| 244 | } |
| 245 | |
| 246 | WayptVec toInsertWps = wps; |
| 247 | // catch insert of VIAs here |
| 248 | if (!_isRoute && (index > 0)) { |
| 249 | const auto pre = _legs.at(index - 1)->waypoint(); |
| 250 | toInsertWps = copyWaypointsExpandingVias(pre, wps); |
| 251 | } else if (index == 0) { |
| 252 | if (wps.front()->type() == "via") { |
| 253 | SG_LOG(SG_AUTOPILOT, SG_DEV_ALERT, "Inserting a VIA at leg 0 of flight-plan, VIA cannot be expanded"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | auto it = _legs.begin() + index; |
| 258 | int endIndex = index + toInsertWps.size() - 1; |
| 259 | if (_currentIndex >= endIndex) { |
| 260 | _currentIndex += toInsertWps.size(); |
| 261 | } |
| 262 | |
| 263 | LegVec newLegs; |
| 264 | for (WayptRef wp : toInsertWps) { |
| 265 | newLegs.push_back(LegRef{new Leg(this, wp)}); |
| 266 | } |
| 267 | |
| 268 | lockDelegates(); |
| 269 | _waypointsChanged = true; |
| 270 | _legs.insert(it, newLegs.begin(), newLegs.end()); |
| 271 | unlockDelegates(); |
| 272 | } |
| 273 | |
| 274 | void FlightPlan::deleteIndex(int aIndex) |
| 275 | { |
| 276 | int index = aIndex; |
| 277 | if (aIndex < 0) { // negative indices count the the end |
| 278 | index = _legs.size() + index; |
| 279 | } |
| 280 | |
| 281 | if ((index < 0) || (index >= numLegs())) { |
| 282 | SG_LOG(SG_NAVAID, SG_WARN, "removeAtIndex with invalid index:" << aIndex); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | lockDelegates(); |
| 287 | _waypointsChanged = true; |
| 288 | |
| 289 | auto it = _legs.begin() + index; |
| 290 | LegRef l = *it; |
| 291 | _legs.erase(it); |
| 292 | l->_parent = nullptr; // orphan the leg so it's clear from Nasal |
| 293 | |
| 294 | if (_currentIndex == index) { |
| 295 | // current waypoint was removed |
| 296 | _currentWaypointChanged = true; |
| 297 | } else if (_currentIndex > index) { |
| 298 | --_currentIndex; // shift current index down if necessary |
| 299 | } |
| 300 | |
| 301 | unlockDelegates(); |
| 302 | } |
| 303 | |
| 304 | void FlightPlan::clearAll() |
| 305 | { |
| 306 | lockDelegates(); |
| 307 | _departure.clear(); |
| 308 | _departureRunway = nullptr; |
| 309 | _destinationRunway = nullptr; |
| 310 | _destination.clear(); |
| 311 | _sid.clear(); |
| 312 | _sidTransition.clear(); |
| 313 | _star.clear(); |
| 314 | _starTransition.clear(); |
| 315 | _approach.clear(); |
| 316 | _approachTransition.clear(); |
| 317 | _alternate.clear(); |
| 318 | |
| 319 | _cruiseAirspeedMach = 0.0; |
| 320 | _cruiseAirspeedKnots = 0; |
| 321 | _cruiseAirspeedKph = 0; |
| 322 | _cruiseFlightLevel = 0; |
| 323 | _cruiseAltitudeFt = 0; |
| 324 | _cruiseAltitudeM = 0; |
| 325 | |
| 326 | clearLegs(); |
| 327 | unlockDelegates(); |
| 328 | } |
| 329 | |
| 330 | void FlightPlan::clearLegs() |
| 331 | { |
| 332 | // some badly behaved CDU implementations call clear on a Nasal timer |
| 333 | // during startup. |
| 334 | if (_legs.empty() && (_currentIndex < 0)) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | lockDelegates(); |
| 339 | _waypointsChanged = true; |
| 340 | _currentWaypointChanged = true; |
| 341 | _arrivalChanged = true; |
| 342 | _departureChanged = true; |
| 343 | _cruiseDataChanged = true; |
| 344 | |
| 345 | _currentIndex = -1; |
| 346 | _legs.clear(); |
| 347 | |
| 348 | notifyCleared(); |
| 349 | unlockDelegates(); |
| 350 | } |
| 351 | |
| 352 | int FlightPlan::clearWayptsWithFlag(WayptFlag flag) |
| 353 | { |
| 354 | int count = 0; |
| 355 | // first pass, fix up currentIndex |
| 356 | for (int i=0; i<_currentIndex; ++i) { |
| 357 | const auto& l = _legs.at(i); |
| 358 | if (l->waypoint()->flag(flag)) { |
| 359 | ++count; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // test if the current leg will be removed |
| 364 | bool currentIsBeingCleared = false; |
| 365 | Leg* curLeg = currentLeg(); |
| 366 | if (curLeg) { |
| 367 | currentIsBeingCleared = curLeg->waypoint()->flag(flag); |
| 368 | } |
| 369 | |
| 370 | _currentIndex -= count; |
| 371 | |
| 372 | // if we're clearing the current waypoint, what shall we do with the |
| 373 | // index? there's various options, but safest is to select no waypoint |
| 374 | // and let the use re-activate. |
| 375 | // http://code.google.com/p/flightgear-bugs/issues/detail?id=1134 |
| 376 | if (currentIsBeingCleared) { |
| 377 | SG_LOG(SG_GENERAL, SG_INFO, "FlightPlan::clearWayptsWithFlag: currentIsBeingCleared:" << currentIsBeingCleared); |
| 378 | _currentIndex = -1; |
| 379 | } |
| 380 | |
| 381 | // now delete and remove |
| 382 | int numDeleted = 0; |
| 383 | auto it = std::remove_if(_legs.begin(), _legs.end(), |
| 384 | [flag, &numDeleted](const LegRef& leg) |
| 385 | { |
| 386 | if (leg->waypoint()->flag(flag)) { |
| 387 | ++numDeleted; |
| 388 | return true; |
| 389 | } |
| 390 | return false; |
| 391 | }); |
| 392 | if (it == _legs.end()) { |
| 393 | return 0; // nothing was cleared, don't fire the delegate |
| 394 | } |
| 395 | |
| 396 | lockDelegates(); |
| 397 | _waypointsChanged = true; |
| 398 | if ((count > 0) || currentIsBeingCleared) { |
| 399 | _currentWaypointChanged = true; |
| 400 | } |
| 401 | |
| 402 | _legs.erase(it, _legs.end()); |
| 403 | |
| 404 | if (_legs.empty()) { // maybe all legs were deleted |
| 405 | notifyCleared(); |
| 406 | } |
| 407 | |
| 408 | unlockDelegates(); |
| 409 | return numDeleted; |
| 410 | } |
| 411 | |
| 412 | bool FlightPlan::isRoute() const |
| 413 | { |
| 414 | return _isRoute; |
| 415 | } |
| 416 | |
| 417 | bool FlightPlan::isActive() const |
| 418 | { |
| 419 | if (_isRoute) |
| 420 | return false; |
| 421 | return (_currentIndex >= 0); |
| 422 | } |
| 423 | |
| 424 | void FlightPlan::setCurrentIndex(int index) |
| 425 | { |
| 426 | if ((index < -1) || (index >= numLegs())) { |
| 427 | throw sg_range_exception("invalid leg index", "FlightPlan::setCurrentIndex"); |
| 428 | } |
| 429 | |
| 430 | if (index == _currentIndex) { |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | lockDelegates(); |
| 435 | _currentIndex = index; |
| 436 | _currentWaypointChanged = true; |
| 437 | unlockDelegates(); |
| 438 | } |
| 439 | |
| 440 | void FlightPlan::sequence() |
| 441 | { |
| 442 | lockDelegates(); |
| 443 | for (auto d : _delegates) { |
| 444 | d->sequence(); |
| 445 | } |
| 446 | unlockDelegates(); |
| 447 | } |
| 448 | |
| 449 | void FlightPlan::finish() |
| 450 | { |
| 451 | if (_isRoute) { |
| 452 | throw sg_exception("Called finish on FlightPlan marked isRoute"); |
| 453 | } |
| 454 | |
| 455 | if (_currentIndex == -1) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | lockDelegates(); |
| 460 | _currentIndex = -1; |
| 461 | _currentWaypointChanged = true; |
| 462 | |
| 463 | for (auto d : _delegates) { |
| 464 | d->endOfFlightPlan(); |
| 465 | } |
| 466 | |
| 467 | unlockDelegates(); |
| 468 | } |
| 469 | |
| 470 | int FlightPlan::findWayptIndex(const SGGeod& aPos) const |
| 471 | { |
| 472 | for (int i=0; i<numLegs(); ++i) { |
| 473 | if (_legs[i]->waypoint()->matches(aPos)) { |
| 474 | return i; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | int FlightPlan::findWayptIndex(const FGPositionedRef aPos) const |
| 482 | { |
| 483 | for (int i=0; i<numLegs(); ++i) { |
| 484 | if (_legs[i]->waypoint()->matches(aPos)) { |
| 485 | return i; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | FlightPlan::LegRef FlightPlan::currentLeg() const |
| 493 | { |
| 494 | if ((_currentIndex < 0) || (_currentIndex >= numLegs())) |
| 495 | return nullptr; |
| 496 | return legAtIndex(_currentIndex); |
| 497 | } |
| 498 | |
| 499 | FlightPlan::LegRef FlightPlan::previousLeg() const |
| 500 | { |
| 501 | if (_currentIndex <= 0) { |
| 502 | return nullptr; |
| 503 | } |
| 504 | |
| 505 | return legAtIndex(_currentIndex - 1); |
| 506 | } |
| 507 | |
| 508 | FlightPlan::LegRef FlightPlan::nextLeg() const |
| 509 | { |
| 510 | if ((_currentIndex < 0) || ((_currentIndex + 1) >= numLegs())) { |
| 511 | return nullptr; |
| 512 | } |
| 513 | |
| 514 | return legAtIndex(_currentIndex + 1); |
| 515 | } |
| 516 | |
| 517 | FlightPlan::LegRef FlightPlan::legAtIndex(int index) const |
| 518 | { |
| 519 | if ((index < 0) || (index >= numLegs())) { |
| 520 | throw sg_range_exception("index out of range", "FlightPlan::legAtIndex"); |
| 521 | } |
| 522 | |
| 523 | return _legs.at(index); |
| 524 | } |
| 525 | |
| 526 | int FlightPlan::findLegIndex(const Leg* l) const |
| 527 | { |
| 528 | for (unsigned int i=0; i<_legs.size(); ++i) { |
| 529 | if (_legs.at(i).get() == l) { |
| 530 | return i; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | void FlightPlan::setDeparture(FGAirport* apt) |
| 538 | { |
| 539 | if (apt == _departure) { |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | lockDelegates(); |
| 544 | _departureChanged = true; |
| 545 | _departure = apt; |
| 546 | _departureRunway = nullptr; |
| 547 | clearSID(); |
| 548 | unlockDelegates(); |
| 549 | } |
| 550 | |
| 551 | void FlightPlan::setDeparture(FGRunway* rwy) |
| 552 | { |
| 553 | if (_departureRunway == rwy) { |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | lockDelegates(); |
| 558 | _departureChanged = true; |
| 559 | |
| 560 | _departureRunway = rwy; |
| 561 | if (rwy->airport() != _departure) { |
| 562 | _departure = rwy->airport(); |
| 563 | clearSID(); |
| 564 | } |
| 565 | |
| 566 | if (_sid && !_sid->isForRunway(_departureRunway)) { |
| 567 | clearSID(); |
| 568 | } |
| 569 | |
| 570 | unlockDelegates(); |
| 571 | } |
| 572 | |
| 573 | void FlightPlan::clearDeparture() |
| 574 | { |
| 575 | lockDelegates(); |
| 576 | _departureChanged = true; |
| 577 | _departure = nullptr; |
| 578 | _departureRunway = nullptr; |
| 579 | clearSID(); |
| 580 | unlockDelegates(); |
| 581 | } |
| 582 | |
| 583 | void FlightPlan::setSID(SID* sid, const std::string& transition) |
| 584 | { |
| 585 | if ((sid == _sid) && (_sidTransition == transition)) { |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | lockDelegates(); |
| 590 | _departureChanged = true; |
| 591 | _sid = sid; |
| 592 | _sidTransition = transition; |
| 593 | unlockDelegates(); |
| 594 | } |
| 595 | |
| 596 | void FlightPlan::setSID(Transition* trans) |
| 597 | { |
| 598 | if (!trans) { |
| 599 | setSID(static_cast<SID*>(nullptr)); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | if (trans->parent()->type() != PROCEDURE_SID) |
| 604 | throw sg_exception("FlightPlan::setSID: transition does not belong to a SID"); |
| 605 | |
| 606 | setSID(static_cast<SID*>(trans->parent()), trans->ident()); |
| 607 | } |
| 608 | |
| 609 | void FlightPlan::clearSID() |
| 610 | { |
| 611 | if (_sid == nullptr) { |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | lockDelegates(); |
| 616 | _departureChanged = true; |
| 617 | _sid = nullptr; |
| 618 | _sidTransition.clear(); |
| 619 | unlockDelegates(); |
| 620 | } |
| 621 | |
| 622 | Transition* FlightPlan::sidTransition() const |
| 623 | { |
| 624 | if (!_sid || _sidTransition.empty()) { |
| 625 | return nullptr; |
| 626 | } |
| 627 | |
| 628 | return _sid->findTransitionByName(_sidTransition); |
| 629 | } |
| 630 | |
| 631 | void FlightPlan::setDestination(FGAirport* apt) |
| 632 | { |
| 633 | if (apt == _destination) { |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | lockDelegates(); |
| 638 | _arrivalChanged = true; |
| 639 | _destination = apt; |
| 640 | _destinationRunway = nullptr; |
| 641 | clearSTAR(); |
| 642 | setApproach(static_cast<Approach*>(nullptr)); |
| 643 | unlockDelegates(); |
| 644 | } |
| 645 | |
| 646 | void FlightPlan::setDestination(FGRunway* rwy) |
| 647 | { |
| 648 | if (_destinationRunway == rwy) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | lockDelegates(); |
| 653 | _arrivalChanged = true; |
| 654 | _destinationRunway = rwy; |
| 655 | if (rwy && (_destination != rwy->airport())) { |
| 656 | _destination = rwy->airport(); |
| 657 | clearSTAR(); |
| 658 | } |
| 659 | |
| 660 | if (_star && !_star->isForRunway(_destinationRunway)) { |
| 661 | clearSTAR(); |
| 662 | } |
| 663 | |
| 664 | unlockDelegates(); |
| 665 | } |
| 666 | |
| 667 | void FlightPlan::clearDestination() |
| 668 | { |
| 669 | lockDelegates(); |
| 670 | _arrivalChanged = true; |
| 671 | _destination = nullptr; |
| 672 | _destinationRunway = nullptr; |
| 673 | clearSTAR(); |
| 674 | setApproach(static_cast<Approach*>(nullptr)); |
| 675 | unlockDelegates(); |
| 676 | } |
| 677 | |
| 678 | FGAirportRef FlightPlan::alternate() const |
| 679 | { |
| 680 | return _alternate; |
| 681 | } |
| 682 | |
| 683 | void FlightPlan::setAlternate(FGAirportRef alt) |
| 684 | { |
| 685 | lockDelegates(); |
| 686 | _alternate = alt; |
| 687 | _arrivalChanged = true; |
| 688 | unlockDelegates(); |
| 689 | } |
| 690 | |
| 691 | void FlightPlan::setSTAR(STAR* star, const std::string& transition) |
| 692 | { |
| 693 | if ((_star == star) && (_starTransition == transition)) { |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | lockDelegates(); |
| 698 | _arrivalChanged = true; |
| 699 | _star = star; |
| 700 | _starTransition = transition; |
| 701 | unlockDelegates(); |
| 702 | } |
| 703 | |
| 704 | void FlightPlan::setSTAR(Transition* trans) |
| 705 | { |
| 706 | if (!trans) { |
| 707 | setSTAR((STAR*) NULL); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | if (trans->parent()->type() != PROCEDURE_STAR) |
| 712 | throw sg_exception("FlightPlan::setSTAR: transition does not belong to a STAR"); |
| 713 | |
| 714 | setSTAR((STAR*) trans->parent(), trans->ident()); |
| 715 | } |
| 716 | |
| 717 | void FlightPlan::clearSTAR() |
| 718 | { |
| 719 | if (_star == nullptr) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | lockDelegates(); |
| 724 | _arrivalChanged = true; |
| 725 | _star = nullptr; |
| 726 | _starTransition.clear(); |
| 727 | unlockDelegates(); |
| 728 | } |
| 729 | |
| 730 | void FlightPlan::setEstimatedDurationMinutes(int mins) |
| 731 | { |
| 732 | _estimatedDuration = mins; |
| 733 | } |
| 734 | |
| 735 | void FlightPlan::computeDurationMinutes() |
| 736 | { |
| 737 | if ((_cruiseAirspeedMach < 0.01) && (_cruiseAirspeedKnots < 10) && (_cruiseAirspeedKph < 10)) { |
| 738 | SG_LOG(SG_AUTOPILOT, SG_WARN, "can't compute duration, no cruise speed set"); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | if ((_cruiseAltitudeFt < 100) && (_cruiseAltitudeM < 100) && (_cruiseFlightLevel < 10)) { |
| 743 | SG_LOG(SG_AUTOPILOT, SG_WARN, "can't compute duration, no cruise altitude set"); |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | } |
| 749 | |
| 750 | Transition* FlightPlan::starTransition() const |
| 751 | { |
| 752 | if (!_star || _starTransition.empty()) { |
| 753 | return nullptr; |
| 754 | } |
| 755 | |
| 756 | return _star->findTransitionByName(_starTransition); |
| 757 | } |
| 758 | |
| 759 | void FlightPlan::setApproach(flightgear::Approach* app, const std::string& trans) |
| 760 | { |
| 761 | if ((_approach == app) && (trans == _approachTransition)) { |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | lockDelegates(); |
| 766 | _arrivalChanged = true; |
| 767 | _approach = app; |
| 768 | _approachTransition = trans; |
| 769 | if (app) { |
| 770 | // keep runway + airport in sync |
| 771 | if (_destinationRunway != _approach->runway()) { |
| 772 | _destinationRunway = _approach->runway(); |
| 773 | } |
| 774 | |
| 775 | if (_destination != _destinationRunway->airport()) { |
| 776 | _destination = _destinationRunway->airport(); |
| 777 | } |
| 778 | } |
| 779 | unlockDelegates(); |
| 780 | } |
| 781 | |
| 782 | void FlightPlan::setApproach(Transition* approachWithTrans) |
| 783 | { |
| 784 | if (!approachWithTrans) { |
| 785 | setApproach((Approach*)nullptr); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | if (!Approach::isApproach(approachWithTrans->parent()->type())) |
| 790 | throw sg_exception("FlightPlan::setApproach: transition does not belong to an approach"); |
| 791 | |
| 792 | setApproach(static_cast<Approach*>(approachWithTrans->parent()), |
| 793 | approachWithTrans->ident()); |
| 794 | } |
| 795 | |
| 796 | Transition* FlightPlan::approachTransition() const |
| 797 | { |
| 798 | if (!_approach || _approachTransition.empty()) { |
| 799 | return nullptr; |
| 800 | } |
| 801 | |
| 802 | return _approach->findTransitionByName(_approachTransition); |
| 803 | } |
| 804 | |
| 805 | bool FlightPlan::save(std::ostream& stream) const |
| 806 | { |
| 807 | try { |
| 808 | SGPropertyNode_ptr d(new SGPropertyNode); |
| 809 | saveToProperties(d); |
| 810 | writeProperties(stream, d, true); |
| 811 | return true; |
| 812 | } catch (sg_exception& e) { |
| 813 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to save flight-plan " << e.getMessage()); |
| 814 | return false; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | bool FlightPlan::save(const SGPath& path) const |
| 819 | { |
| 820 | try { |
| 821 | SGPropertyNode_ptr d(new SGPropertyNode); |
| 822 | saveToProperties(d); |
| 823 | writeProperties(path, d, true /* write-all */); |
| 824 | return true; |
| 825 | } catch (sg_exception& e) { |
| 826 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to save flight-plan '" << path << "'. " << e.getMessage()); |
| 827 | return false; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | void FlightPlan::saveToProperties(SGPropertyNode* d) const |
| 832 | { |
| 833 | d->setIntValue("version", 2); |
| 834 | |
| 835 | // general data |
| 836 | if (_isRoute) { |
| 837 | d->setBoolValue("is-route", true); |
| 838 | } |
| 839 | |
| 840 | d->setStringValue("flight-rules", static_icaoFlightRulesCode[static_cast<int>(_flightRules)]); |
| 841 | d->setStringValue("flight-type", static_icaoFlightTypeCode[static_cast<int>(_flightType)]); |
| 842 | if (!_callsign.empty()) { |
| 843 | d->setStringValue("callsign", _callsign); |
| 844 | } |
| 845 | if (!_remarks.empty()) { |
| 846 | d->setStringValue("remarks", _remarks); |
| 847 | } |
| 848 | if (!_aircraftType.empty()) { |
| 849 | d->setStringValue("aircraft-type", _aircraftType); |
| 850 | } |
| 851 | d->setIntValue("estimated-duration-minutes", _estimatedDuration); |
| 852 | |
| 853 | if (_departure) { |
| 854 | d->setStringValue("departure/airport", _departure->ident()); |
| 855 | if (_sid) { |
| 856 | d->setStringValue("departure/sid", _sid->ident()); |
| 857 | if (!_sidTransition.empty()) |
| 858 | d->setStringValue("departure/sid_trans", _sidTransition); |
| 859 | } |
| 860 | |
| 861 | if (_departureRunway) { |
| 862 | d->setStringValue("departure/runway", _departureRunway->ident()); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (_destination) { |
| 867 | d->setStringValue("destination/airport", _destination->ident()); |
| 868 | if (_star) { |
| 869 | d->setStringValue("destination/star", _star->ident()); |
| 870 | if (!_starTransition.empty()) |
| 871 | d->setStringValue("destination/star_trans", _starTransition); |
| 872 | } |
| 873 | |
| 874 | if (_approach) { |
| 875 | d->setStringValue("destination/approach", _approach->ident()); |
| 876 | if (!_approachTransition.empty()) |
| 877 | d->setStringValue("destination/approach_trans", _approachTransition); |
| 878 | } |
| 879 | |
| 880 | if (_destinationRunway) { |
| 881 | d->setStringValue("destination/runway", _destinationRunway->ident()); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | if (_alternate) { |
| 886 | d->setStringValue("alternate", _alternate->ident()); |
| 887 | } |
| 888 | |
| 889 | // cruise data |
| 890 | if (_cruiseFlightLevel > 0) { |
| 891 | d->setIntValue("cruise/flight-level", _cruiseFlightLevel); |
| 892 | } else if (_cruiseAltitudeFt > 0) { |
| 893 | d->setIntValue("cruise/altitude-ft", _cruiseAltitudeFt); |
| 894 | } else if (_cruiseAltitudeM > 0) { |
| 895 | d->setIntValue("cruise/altitude-m", _cruiseAltitudeM); |
| 896 | } |
| 897 | |
| 898 | if (_cruiseAirspeedMach > 0.0) { |
| 899 | d->setDoubleValue("cruise/mach", _cruiseAirspeedMach); |
| 900 | } else if (_cruiseAirspeedKnots > 0) { |
| 901 | d->setIntValue("cruise/knots", _cruiseAirspeedKnots); |
| 902 | } else if (_cruiseAirspeedKph > 0) { |
| 903 | d->setIntValue("cruise/kph", _cruiseAirspeedKph); |
| 904 | } |
| 905 | |
| 906 | // route nodes |
| 907 | SGPropertyNode* routeNode = d->getChild("route", 0, true); |
| 908 | for (unsigned int i=0; i<_legs.size(); ++i) { |
| 909 | auto leg = _legs.at(i); |
| 910 | Waypt* wpt = leg->waypoint(); |
| 911 | auto legNode = routeNode->getChild("wp", i, true); |
| 912 | wpt->saveAsNode(legNode); |
| 913 | leg->writeToProperties(legNode); |
| 914 | } // of waypoint iteration |
| 915 | } |
| 916 | |
| 917 | static bool anyWaypointsWithFlag(FlightPlan* plan, WayptFlag flag) |
| 918 | { |
| 919 | bool r = false; |
| 920 | plan->forEachLeg([&r, flag](FlightPlan::Leg* l) { |
| 921 | if (l->waypoint()->flags() & flag) { |
| 922 | r = true; |
| 923 | } |
| 924 | }); |
| 925 | |
| 926 | return r; |
| 927 | } |
| 928 | |
| 929 | bool FlightPlan::load(const SGPath& path) |
| 930 | { |
| 931 | if (!path.exists()) { |
| 932 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan '" << path |
| 933 | << "'. The file does not exist."); |
| 934 | return false; |
| 935 | } |
| 936 | |
| 937 | SG_LOG(SG_NAVAID, SG_INFO, "going to read flight-plan from:" << path); |
| 938 | |
| 939 | bool Status = false; |
| 940 | lockDelegates(); |
| 941 | |
| 942 | // try different file formats |
| 943 | if (loadGpxFormat(path)) { // GPX format |
| 944 | _arrivalChanged = true; |
| 945 | _departureChanged = true; |
| 946 | Status = true; |
| 947 | } else if (loadXmlFormat(path)) { // XML property data |
| 948 | if (!_isRoute) { |
| 949 | expandVias(); |
| 950 | } |
| 951 | |
| 952 | // we don't want to re-compute the arrival / departure after |
| 953 | // a load, since we assume the flight-plan had it specified already |
| 954 | // especially, the XML might have a SID/STAR embedded, which we don't |
| 955 | // want to lose |
| 956 | |
| 957 | // however, we do want to run the normal delegate if no procedure was |
| 958 | // defined. We'll use the presence of waypoints tagged to decide |
| 959 | const bool hasArrival = anyWaypointsWithFlag(this, WPT_ARRIVAL) || anyWaypointsWithFlag(this, WPT_APPROACH); |
| 960 | const bool hasDeparture = anyWaypointsWithFlag(this, WPT_DEPARTURE); |
| 961 | _arrivalChanged = !hasArrival; |
| 962 | _departureChanged = !hasDeparture; |
| 963 | Status = true; |
| 964 | } else if (loadPlainTextFormat(path)) { // simple textual list of waypoints |
| 965 | _arrivalChanged = true; |
| 966 | _departureChanged = true; |
| 967 | Status = true; |
| 968 | |
| 969 | if (!_isRoute) { |
| 970 | expandVias(); // plain text could in principle contain VIAs |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | if (Status == true) { |
| 975 | setIdent(path.file_base()); |
| 976 | } |
| 977 | |
| 978 | _cruiseDataChanged = true; |
| 979 | _waypointsChanged = true; |
| 980 | _didLoadFP = true; |
| 981 | |
| 982 | unlockDelegates(); |
| 983 | |
| 984 | return Status; |
| 985 | } |
| 986 | |
| 987 | bool FlightPlan::load(std::istream &stream) |
| 988 | { |
| 989 | SGPropertyNode_ptr routeData(new SGPropertyNode); |
| 990 | try { |
| 991 | readProperties(stream, routeData); |
| 992 | } catch (sg_exception& e) { |
| 993 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan '" << e.getOrigin() |
| 994 | << "'. " << e.getMessage()); |
| 995 | return false; |
| 996 | } |
| 997 | |
| 998 | if (!routeData.valid()) |
| 999 | return false; |
| 1000 | |
| 1001 | bool Status = false; |
| 1002 | lockDelegates(); |
| 1003 | try { |
| 1004 | int version = routeData->getIntValue("version", 1); |
| 1005 | if (version == 2) { |
| 1006 | loadVersion2XMLRoute(routeData); |
| 1007 | Status = true; |
| 1008 | } else { |
| 1009 | throw sg_io_exception("unsupported XML route version"); |
| 1010 | } |
| 1011 | } catch (sg_exception& e) { |
| 1012 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan '" << e.getOrigin() |
| 1013 | << "'. " << e.getMessage()); |
| 1014 | Status = false; |
| 1015 | } |
| 1016 | |
| 1017 | if (!_isRoute) { |
| 1018 | expandVias(); |
| 1019 | } |
| 1020 | |
| 1021 | // we don't want to re-compute the arrival / departure after |
| 1022 | // a load, since we assume the flight-plan had it specified already |
| 1023 | // especially, the XML might have a SID/STAR embedded, which we don't |
| 1024 | // want to lose |
| 1025 | |
| 1026 | // however, we do want to run the normal delegate if no procedure was |
| 1027 | // defined. We'll use the presence of waypoints tagged to decide |
| 1028 | const bool hasArrival = anyWaypointsWithFlag(this, WPT_ARRIVAL); |
| 1029 | const bool hasDeparture = anyWaypointsWithFlag(this, WPT_DEPARTURE); |
| 1030 | _arrivalChanged = !hasArrival; |
| 1031 | _departureChanged = !hasDeparture; |
| 1032 | |
| 1033 | _cruiseDataChanged = true; |
| 1034 | _waypointsChanged = true; |
| 1035 | _didLoadFP = true; |
| 1036 | |
| 1037 | unlockDelegates(); |
| 1038 | |
| 1039 | return Status; |
| 1040 | } |
| 1041 | |
| 1042 | /** XML loader for GPX file format */ |
| 1043 | class GpxXmlVisitor : public XMLVisitor |
| 1044 | { |
| 1045 | public: |
| 1046 | GpxXmlVisitor(FlightPlan* fp) : _fp(fp), _lat(-9999), _lon(-9999) {} |
| 1047 | |
| 1048 | void startElement (const char * name, const XMLAttributes &atts) override; |
| 1049 | void endElement (const char * name) override; |
| 1050 | void data (const char * s, int length) override; |
| 1051 | |
| 1052 | const WayptVec& waypoints() const { return _waypoints; } |
| 1053 | private: |
| 1054 | FlightPlan* _fp; |
| 1055 | double _lat, _lon, _elevationM; |
| 1056 | string _element; |
| 1057 | string _waypoint; |
| 1058 | WayptVec _waypoints; |
| 1059 | }; |
| 1060 | |
| 1061 | void GpxXmlVisitor::startElement(const char * name, const XMLAttributes &atts) |
| 1062 | { |
| 1063 | _element = name; |
| 1064 | if (!strcmp(name, "rtept")) { |
| 1065 | _waypoint.clear(); |
| 1066 | _lat = _lon = _elevationM = -9999; |
| 1067 | const char* slat = atts.getValue("lat"); |
| 1068 | const char* slon = atts.getValue("lon"); |
| 1069 | if (slat && slon) { |
| 1070 | _lat = atof(slat); |
| 1071 | _lon = atof(slon); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | void GpxXmlVisitor::data(const char * s, int length) |
| 1077 | { |
| 1078 | // use "name" when given, otherwise use "cmt" (comment) as ID |
| 1079 | if ((_element == "name") || |
| 1080 | ((_waypoint.empty()) && (_element == "cmt"))) |
| 1081 | { |
| 1082 | _waypoint = std::string(s, static_cast<size_t>(length)); |
| 1083 | } |
| 1084 | |
| 1085 | if (_element == "ele") { |
| 1086 | _elevationM = atof(s); |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | void GpxXmlVisitor::endElement(const char * name) |
| 1091 | { |
| 1092 | _element.clear(); |
| 1093 | if (!strcmp(name, "rtept")) { |
| 1094 | if (_lon > -9990.0) { |
| 1095 | const auto geod = SGGeod::fromDeg(_lon, _lat); |
| 1096 | auto pos = FGPositioned::findClosestWithIdent(_waypoint, geod); |
| 1097 | WayptRef wp; |
| 1098 | |
| 1099 | if (pos) { |
| 1100 | // check distance |
| 1101 | const auto dist = SGGeodesy::distanceM(geod, pos->geod()); |
| 1102 | if (dist < 800.0) { |
| 1103 | wp = new NavaidWaypoint(pos, _fp); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | if (!wp) { |
| 1108 | wp = new BasicWaypt(geod, _waypoint, _fp); |
| 1109 | } |
| 1110 | |
| 1111 | if (_elevationM > -9990.0) { |
| 1112 | wp->setAltitude(_elevationM * SG_METER_TO_FEET, RESTRICT_AT); |
| 1113 | } |
| 1114 | _waypoints.push_back(wp); |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | /** Load a flightplan in GPX format */ |
| 1120 | bool FlightPlan::loadGpxFormat(const SGPath& path) |
| 1121 | { |
| 1122 | if (path.lower_extension() != "gpx") { |
| 1123 | // not a valid GPX file |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | GpxXmlVisitor gpxVisitor(this); |
| 1128 | try { |
| 1129 | readXML(path, gpxVisitor); |
| 1130 | } catch (sg_exception& e) { |
| 1131 | // XML parsing fails => not a GPX XML file |
| 1132 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan in GPX format: '" << e.getOrigin() |
| 1133 | << "'. " << e.getMessage()); |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | if (gpxVisitor.waypoints().empty()) { |
| 1138 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan in GPX format. No route found."); |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | clearAll(); |
| 1143 | |
| 1144 | // copy in case we need to modify |
| 1145 | WayptVec wps = gpxVisitor.waypoints(); |
| 1146 | // detect airports |
| 1147 | const auto depApt = FGAirport::findByIdent(wps.front()->ident()); |
| 1148 | const auto destApt = FGAirport::findByIdent(wps.back()->ident()); |
| 1149 | |
| 1150 | if (depApt) { |
| 1151 | wps.erase(wps.begin()); |
| 1152 | setDeparture(depApt); |
| 1153 | } |
| 1154 | |
| 1155 | // for a single-element waypoint list consisting of a single airport ID, |
| 1156 | // don't crash |
| 1157 | if (destApt && !wps.empty()) { |
| 1158 | wps.pop_back(); |
| 1159 | setDestination(destApt); |
| 1160 | } |
| 1161 | |
| 1162 | insertWayptsAtIndex(wps, -1); |
| 1163 | |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | /** Load a flightplan in FlightGear XML property format */ |
| 1168 | bool FlightPlan::loadXmlFormat(const SGPath& path) |
| 1169 | { |
| 1170 | SGPropertyNode_ptr routeData(new SGPropertyNode); |
| 1171 | |
| 1172 | try { |
| 1173 | readProperties(path, routeData); |
| 1174 | } catch (sg_exception& e) { |
| 1175 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan '" << e.getOrigin() |
| 1176 | << "'. " << e.getMessage()); |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | if (routeData.valid()) |
| 1181 | { |
| 1182 | try { |
| 1183 | int version = routeData->getIntValue("version", 1); |
| 1184 | bool ok = false; |
| 1185 | if (version == 1) { |
| 1186 | ok = loadVersion1XMLRoute(routeData); |
| 1187 | } else if (version == 2) { |
| 1188 | ok = loadVersion2XMLRoute(routeData); |
| 1189 | } else { |
| 1190 | SG_LOG(SG_NAVAID, SG_POPUP, "Unsupported flight plan version " << version << " loading " << path); |
| 1191 | } |
| 1192 | |
| 1193 | return ok; |
| 1194 | } catch (sg_exception& e) { |
| 1195 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan '" << e.getOrigin() |
| 1196 | << "'. " << e.getMessage()); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
| 1203 | void FlightPlan::loadXMLRouteHeader(SGPropertyNode_ptr routeData) |
| 1204 | { |
| 1205 | // general info |
| 1206 | const auto rules = routeData->getStringValue("flight-rules", "V"); |
| 1207 | auto it = std::find(static_icaoFlightRulesCode.begin(), |
| 1208 | static_icaoFlightRulesCode.end(), rules); |
| 1209 | _flightRules = static_cast<ICAOFlightRules>(std::distance(static_icaoFlightRulesCode.begin(), it)); |
| 1210 | |
| 1211 | const auto type = routeData->getStringValue("flight-type", "X"); |
| 1212 | auto it2 = std::find(static_icaoFlightTypeCode.begin(), |
| 1213 | static_icaoFlightTypeCode.end(), type); |
| 1214 | _flightType = static_cast<ICAOFlightType>(std::distance(static_icaoFlightTypeCode.begin(), it2)); |
| 1215 | |
| 1216 | _callsign = routeData->getStringValue("callsign"); |
| 1217 | _remarks = routeData->getStringValue("remarks"); |
| 1218 | _aircraftType = routeData->getStringValue("aircraft-type"); |
| 1219 | _estimatedDuration = routeData->getIntValue("estimated-duration-minutes"); |
| 1220 | |
| 1221 | if (routeData->hasValue("is-route")) { |
| 1222 | if (_isRoute != routeData->getBoolValue("is-route")) { |
| 1223 | // this is actually okay, we will expand any VIAs |
| 1224 | SG_LOG(SG_NAVAID, SG_INFO, "Loading XML marked with 'is-route' into FlightPlan with is-route not set"); |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | // departure nodes |
| 1229 | SGPropertyNode* dep = routeData->getChild("departure"); |
| 1230 | if (dep) { |
| 1231 | string depIdent = dep->getStringValue("airport"); |
| 1232 | setDeparture((FGAirport*) fgFindAirportID(depIdent)); |
| 1233 | if (_departure) { |
| 1234 | string rwy(dep->getStringValue("runway")); |
| 1235 | if (_departure->hasRunwayWithIdent(rwy)) { |
| 1236 | setDeparture(_departure->getRunwayByIdent(rwy)); |
| 1237 | } |
| 1238 | |
| 1239 | if (dep->hasChild("sid")) { |
| 1240 | // previously, we would write a transition id for 'SID' if set, |
| 1241 | // but this is ambigous. Starting with 2020.2, we only every try |
| 1242 | // to parse this value as a SID, and look for a seperate sid_trans |
| 1243 | // value |
| 1244 | const string trans = dep->getStringValue("sid_trans"); |
| 1245 | const auto sidIdent = dep->getStringValue("sid"); |
| 1246 | auto sid = _departure->findSIDWithIdent(sidIdent); |
| 1247 | if (!sid) { |
| 1248 | SG_LOG(SG_NAVAID, SG_WARN, "FlightPlan specifies unknown SID:" << sidIdent); |
| 1249 | } else { |
| 1250 | setSID(sid, trans); |
| 1251 | } |
| 1252 | } // of have a SID identifier |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | // destination |
| 1257 | SGPropertyNode* dst = routeData->getChild("destination"); |
| 1258 | if (dst) { |
| 1259 | setDestination((FGAirport*) fgFindAirportID(dst->getStringValue("airport"))); |
| 1260 | if (_destination) { |
| 1261 | string rwy(dst->getStringValue("runway")); |
| 1262 | if (_destination->hasRunwayWithIdent(rwy)) { |
| 1263 | setDestination(_destination->getRunwayByIdent(rwy)); |
| 1264 | } |
| 1265 | |
| 1266 | if (dst->hasChild("star")) { |
| 1267 | // prior to 2020.2 we would attempt to treat 'star' as a |
| 1268 | // transiiton ID, but this is ambiguous. Look for a seperate value now |
| 1269 | const auto starIdent = dst->getStringValue("star"); |
| 1270 | const string trans = dst->getStringValue("star_trans"); |
| 1271 | auto star = _destination->findSTARWithIdent(starIdent); |
| 1272 | |
| 1273 | if (star) { |
| 1274 | setSTAR(star, trans); |
| 1275 | } else { |
| 1276 | SG_LOG(SG_NAVAID, SG_WARN, "FlightPlan specifies unknown STAR:" << starIdent); |
| 1277 | } |
| 1278 | } // of STAR processing |
| 1279 | |
| 1280 | if (dst->hasChild("approach")) { |
| 1281 | auto app = _destination->findApproachWithIdent(dst->getStringValue("approach")); |
| 1282 | const auto trans = dst->getStringValue("approach_trans"); |
| 1283 | if (app) { |
| 1284 | setApproach(app, trans); |
| 1285 | } else { |
| 1286 | SG_LOG(SG_NAVAID, SG_WARN, "FlightPlan specifies unknown approach:" << dst->getStringValue("approach")); |
| 1287 | } |
| 1288 | } // of have approach in the XML |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | // alternate |
| 1293 | if (routeData->hasChild("alternate")) { |
| 1294 | setAlternate((FGAirport*) fgFindAirportID(routeData->getStringValue("alternate"))); |
| 1295 | } |
| 1296 | |
| 1297 | // cruise |
| 1298 | SGPropertyNode* crs = routeData->getChild("cruise"); |
| 1299 | if (crs) { |
| 1300 | if (crs->hasChild("flight-level")) { |
| 1301 | _cruiseFlightLevel = crs->getIntValue("flight-level"); |
| 1302 | } else if (crs->hasChild("altitude-ft")) { |
| 1303 | _cruiseAltitudeFt = crs->getIntValue("altitude-ft"); |
| 1304 | } else if (crs->hasChild("altitude-m")) { |
| 1305 | _cruiseAltitudeM = crs->getIntValue("altitude-m"); |
| 1306 | } |
| 1307 | |
| 1308 | if (crs->hasChild("mach")) { |
| 1309 | _cruiseAirspeedMach = crs->getDoubleValue("mach"); |
| 1310 | } else if (crs->hasChild("knots")) { |
| 1311 | _cruiseAirspeedKnots = crs->getIntValue("knots"); |
| 1312 | } else if (crs->hasChild("kph")) { |
| 1313 | _cruiseAirspeedKph = crs->getIntValue("kph"); |
| 1314 | } |
| 1315 | } // of cruise data loading |
| 1316 | } |
| 1317 | |
| 1318 | bool FlightPlan::loadVersion2XMLRoute(SGPropertyNode_ptr routeData) |
| 1319 | { |
| 1320 | if (!routeData->hasChild("route")) |
| 1321 | return false; |
| 1322 | |
| 1323 | loadXMLRouteHeader(routeData); |
| 1324 | |
| 1325 | // route nodes |
| 1326 | _legs.clear(); |
| 1327 | SGPropertyNode_ptr routeNode = routeData->getChild("route", 0); |
| 1328 | if (routeNode.valid()) { |
| 1329 | for (auto wpNode : routeNode->getChildren("wp")) { |
| 1330 | auto wp = Waypt::createFromProperties(this, wpNode); |
| 1331 | if (!wp) { |
| 1332 | continue; |
| 1333 | } |
| 1334 | |
| 1335 | LegRef l = new Leg{this, wp}; |
| 1336 | if (wpNode->hasChild("hold-count")) { |
| 1337 | l->setHoldCount(wpNode->getIntValue("hold-count")); |
| 1338 | } |
| 1339 | _legs.push_back(l); |
| 1340 | } // of route iteration |
| 1341 | } |
| 1342 | _waypointsChanged = true; |
| 1343 | return true; |
| 1344 | } |
| 1345 | |
| 1346 | bool FlightPlan::loadVersion1XMLRoute(SGPropertyNode_ptr routeData) |
| 1347 | { |
| 1348 | if (!routeData->hasChild("route")) |
| 1349 | return false; |
| 1350 | |
| 1351 | loadXMLRouteHeader(routeData); |
| 1352 | |
| 1353 | // _legs nodes |
| 1354 | _legs.clear(); |
| 1355 | SGPropertyNode_ptr routeNode = routeData->getChild("route", 0); |
| 1356 | for (int i=0; i<routeNode->nChildren(); ++i) { |
| 1357 | SGPropertyNode_ptr wpNode = routeNode->getChild("wp", i); |
| 1358 | LegRef l = new Leg(this, parseVersion1XMLWaypt(wpNode)); |
| 1359 | _legs.push_back(l); |
| 1360 | } // of route iteration |
| 1361 | _waypointsChanged = true; |
| 1362 | return true; |
| 1363 | } |
| 1364 | |
| 1365 | WayptRef FlightPlan::parseVersion1XMLWaypt(SGPropertyNode* aWP) |
| 1366 | { |
| 1367 | SGGeod lastPos; |
| 1368 | if (!_legs.empty()) { |
| 1369 | lastPos = _legs.back()->waypoint()->position(); |
| 1370 | } else if (_departure) { |
| 1371 | lastPos = _departure->geod(); |
| 1372 | } |
| 1373 | |
| 1374 | WayptRef w; |
| 1375 | string ident(aWP->getStringValue("ident")); |
| 1376 | if (aWP->hasChild("longitude-deg")) { |
| 1377 | // explicit longitude/latitude |
| 1378 | w = new BasicWaypt(SGGeod::fromDeg(aWP->getDoubleValue("longitude-deg"), |
| 1379 | aWP->getDoubleValue("latitude-deg")), ident, this); |
| 1380 | |
| 1381 | } else { |
| 1382 | string nid = aWP->getStringValue("navid", ident.c_str()); |
| 1383 | FGPositionedRef p = FGPositioned::findClosestWithIdent(nid, lastPos); |
| 1384 | SGGeod pos; |
| 1385 | |
| 1386 | if (p) { |
| 1387 | pos = p->geod(); |
| 1388 | } else { |
| 1389 | SG_LOG(SG_GENERAL, SG_WARN, "unknown navaid in flightplan:" << nid); |
| 1390 | pos = SGGeod::fromDeg(aWP->getDoubleValue("longitude-deg"), |
| 1391 | aWP->getDoubleValue("latitude-deg")); |
| 1392 | } |
| 1393 | |
| 1394 | if (aWP->hasChild("offset-nm") && aWP->hasChild("offset-radial")) { |
| 1395 | double radialDeg = aWP->getDoubleValue("offset-radial"); |
| 1396 | // convert magnetic radial to a true radial! |
| 1397 | radialDeg += magvarDegAt(pos); |
| 1398 | double offsetNm = aWP->getDoubleValue("offset-nm"); |
| 1399 | double az2; |
| 1400 | SGGeodesy::direct(pos, radialDeg, offsetNm * SG_NM_TO_METER, pos, az2); |
| 1401 | } |
| 1402 | |
| 1403 | w = new BasicWaypt(pos, ident, this); |
| 1404 | } |
| 1405 | |
| 1406 | double altFt = aWP->getDoubleValue("altitude-ft", -9999.9); |
| 1407 | if (altFt > -9990.0) { |
| 1408 | w->setAltitude(altFt, RESTRICT_AT, ALTITUDE_FEET); |
| 1409 | } |
| 1410 | |
| 1411 | return w; |
| 1412 | } |
| 1413 | |
| 1414 | /** Load a flightplan in FlightGear plain-text format */ |
| 1415 | bool FlightPlan::loadPlainTextFormat(const SGPath& path) |
| 1416 | { |
| 1417 | try { |
| 1418 | sg_gzifstream in(path); |
| 1419 | if (!in.is_open()) { |
| 1420 | throw sg_io_exception("Cannot open file for reading."); |
| 1421 | } |
| 1422 | |
| 1423 | _legs.clear(); |
| 1424 | while (!in.eof()) { |
| 1425 | string line; |
| 1426 | getline(in, line, '\n'); |
| 1427 | // trim CR from end of line, if found |
| 1428 | if (line[line.size() - 1] == '\r') { |
| 1429 | line.erase(line.size() - 1, 1); |
| 1430 | } |
| 1431 | |
| 1432 | line = simgear::strutils::strip(line); |
| 1433 | if (line.empty() || (line[0] == '#')) { |
| 1434 | continue; // ignore empty/comment lines |
| 1435 | } |
| 1436 | |
| 1437 | // prevent Sentry error 'FLIGHTGEAR-J6', when we try loading XML |
| 1438 | // data here |
| 1439 | if (simgear::strutils::starts_with(line, "<?xml")) { |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
| 1443 | SGGeod vicinity; |
| 1444 | if (!_legs.empty()) { |
| 1445 | vicinity = _legs.back()->waypoint()->position(); |
| 1446 | } |
| 1447 | WayptRef w = waypointFromString(line, vicinity); |
| 1448 | if (!w) { |
| 1449 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to create waypoint from '" << line << "' in " << path); |
| 1450 | _legs.clear(); |
| 1451 | return false; |
| 1452 | } |
| 1453 | |
| 1454 | _legs.push_back(LegRef{new Leg(this, w)}); |
| 1455 | } // of line iteration |
| 1456 | } catch (sg_exception& e) { |
| 1457 | SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load route from: '" << path << "'. " << e.getMessage()); |
| 1458 | _legs.clear(); |
| 1459 | return false; |
| 1460 | } |
| 1461 | |
| 1462 | return true; |
| 1463 | } |
| 1464 | |
| 1465 | double FlightPlan::magvarDegAt(const SGGeod& pos) const |
| 1466 | { |
| 1467 | double jd = globals->get_time_params()->getJD(); |
| 1468 | return sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES; |
| 1469 | } |
| 1470 | |
| 1471 | SGGeod FlightPlan::vicinityForInsertIndex(int aIndex) const |
| 1472 | { |
| 1473 | if (aIndex < 0 || aIndex >= numLegs()) { // appending, not inserting |
| 1474 | const int n = numLegs(); |
| 1475 | if (n > 0) { |
| 1476 | // if we have at least one existing leg, use its position |
| 1477 | // for the search vicinity |
| 1478 | return pointAlongRoute(n - 1, 0.0); |
| 1479 | } |
| 1480 | |
| 1481 | return SGGeod::invalid(); |
| 1482 | } |
| 1483 | |
| 1484 | // if we're somewhere in the middle of the route compute a search |
| 1485 | // vicinity halfway between the previous waypoint and the one we are |
| 1486 | // inserting at, i.e the middle of the leg. |
| 1487 | // if we're at the beginning, just use zero of course. |
| 1488 | const double normOffset = (aIndex > 0) ? -0.5 : 0.0; |
| 1489 | return pointAlongRouteNorm(aIndex, normOffset); |
| 1490 | } |
| 1491 | |
| 1492 | WayptRef FlightPlan::waypointFromString(const string& tgt, const SGGeod& vicinity) |
| 1493 | { |
| 1494 | SGGeod basePosition = vicinity; |
| 1495 | if (!vicinity.isValid()) { |
| 1496 | // compute basePosition using some heuristics |
| 1497 | if (_legs.empty()) { |
| 1498 | // route is empty, use departure position / aircraft position |
| 1499 | basePosition = _departure ? _departure->geod() : globals->get_aircraft_position(); |
| 1500 | } else { |
| 1501 | basePosition = _legs.back()->waypoint()->position(); |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | return Waypt::createFromString(this, tgt, basePosition); |
| 1506 | } |
| 1507 | |
| 1508 | bool FlightPlan::expandVias() |
| 1509 | { |
| 1510 | // must be called with the delegats locked, so that |
| 1511 | // waypointsChanged can be set on finish |
| 1512 | |
| 1513 | assert(_delegateLock > 0); |
| 1514 | bool didChangeAny = false; |
| 1515 | |
| 1516 | for (unsigned int i=1; i < _legs.size(); ) { |
| 1517 | if (_legs[i]->waypoint()->type() == "via") { |
| 1518 | WayptRef preceeding = _legs[i - 1]->waypoint(); |
| 1519 | Via* via = static_cast<Via*>(_legs[i]->waypoint()); |
| 1520 | WayptVec wps = via->expandToWaypoints(preceeding); |
| 1521 | |
| 1522 | // delete the VIA leg |
| 1523 | auto it = _legs.begin() + i; |
| 1524 | LegRef l = *it; |
| 1525 | _legs.erase(it); |
| 1526 | |
| 1527 | // create new legs and insert |
| 1528 | it = _legs.begin() + i; |
| 1529 | |
| 1530 | LegVec newLegs; |
| 1531 | for (WayptRef wp : wps) { |
| 1532 | newLegs.push_back(LegRef{new Leg(this, wp)}); |
| 1533 | } |
| 1534 | |
| 1535 | didChangeAny = true; |
| 1536 | _legs.insert(it, newLegs.begin(), newLegs.end()); |
| 1537 | } else { |
| 1538 | ++i; // normal case, no expansion |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | return didChangeAny; |
| 1543 | } |
| 1544 | |
| 1545 | void FlightPlan::activate() |
| 1546 | { |
| 1547 | if (_isRoute) { |
| 1548 | // no allowed, clone and make the non-route FP active |
| 1549 | SG_LOG(SG_NAVAID, SG_DEV_ALERT, "tried to activate an is-route FlightPlan"); |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | auto routeManager = globals->get_subsystem<FGRouteMgr>(); |
| 1554 | if (routeManager) { |
| 1555 | if (routeManager->flightPlan() != this) { |
| 1556 | SG_LOG(SG_NAVAID, SG_DEBUG, "setting new flight-plan on route-manager"); |
| 1557 | routeManager->setFlightPlan(this); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | lockDelegates(); |
| 1562 | |
| 1563 | _currentIndex = 0; |
| 1564 | _currentWaypointChanged = true; |
| 1565 | _waypointsChanged = expandVias(); |
| 1566 | |
| 1567 | for (auto d : _delegates) { |
| 1568 | d->activated(); |
| 1569 | } |
| 1570 | |
| 1571 | unlockDelegates(); |
| 1572 | } |
| 1573 | |
| 1574 | FlightPlan::Leg::Leg(FlightPlan* owner, WayptRef wpt) : |
| 1575 | _parent(owner), |
| 1576 | _waypt(wpt) |
| 1577 | { |
| 1578 | if (!wpt.valid()) { |
| 1579 | throw sg_exception("can't create FlightPlan::Leg without underlying waypoint"); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | FlightPlan::Leg* FlightPlan::Leg::cloneFor(FlightPlan* owner) const |
| 1584 | { |
| 1585 | Leg* c = new Leg(owner, _waypt); |
| 1586 | // clone local data |
| 1587 | c->_speed = _speed; |
| 1588 | c->_speedRestrict = _speedRestrict; |
| 1589 | c->_speedUnits = _speedUnits; |
| 1590 | c->_altitude = _altitude; |
| 1591 | c->_altRestrict = _altRestrict; |
| 1592 | c->_altitudeUnits = _altitudeUnits; |
| 1593 | c->_holdCount = c->_holdCount; |
| 1594 | |
| 1595 | return c; |
| 1596 | } |
| 1597 | |
| 1598 | FlightPlan::Leg* FlightPlan::Leg::nextLeg() const |
| 1599 | { |
| 1600 | if ((index() + 1) >= _parent->_legs.size()) |
| 1601 | return nullptr; |
| 1602 | |
| 1603 | return _parent->legAtIndex(index() + 1); |
| 1604 | } |
| 1605 | |
| 1606 | unsigned int FlightPlan::Leg::index() const |
| 1607 | { |
| 1608 | return _parent->findLegIndex(this); |
| 1609 | } |
| 1610 | |
| 1611 | double FlightPlan::Leg::altitude(RouteUnits aUnits) const |
| 1612 | { |
| 1613 | if (_altRestrict != RESTRICT_NONE) { |
| 1614 | return convertAltitudeUnits(_altitudeUnits, aUnits, _altitude); |
| 1615 | } |
| 1616 | |
| 1617 | return _waypt->altitude(aUnits); |
| 1618 | } |
| 1619 | |
| 1620 | int FlightPlan::Leg::altitudeFt() const |
| 1621 | { |
| 1622 | return static_cast<int>(altitude(ALTITUDE_FEET)); |
| 1623 | } |
| 1624 | |
| 1625 | double FlightPlan::Leg::speed(RouteUnits units) const |
| 1626 | { |
| 1627 | if (_speedRestrict != RESTRICT_NONE) { |
| 1628 | return convertSpeedUnits(_speedUnits, units, altitudeFt(), _speed); |
| 1629 | } |
| 1630 | |
| 1631 | return _waypt->speed(units); |
| 1632 | } |
| 1633 | |
| 1634 | int FlightPlan::Leg::speedKts() const |
| 1635 | { |
| 1636 | return static_cast<int>(speed(SPEED_KNOTS)); |
| 1637 | } |
| 1638 | |
| 1639 | double FlightPlan::Leg::speedMach() const |
| 1640 | { |
| 1641 | return speed(SPEED_MACH); |
| 1642 | } |
| 1643 | |
| 1644 | RouteRestriction FlightPlan::Leg::altitudeRestriction() const |
| 1645 | { |
| 1646 | if (_altRestrict != RESTRICT_NONE) { |
| 1647 | return _altRestrict; |
| 1648 | } |
| 1649 | |
| 1650 | return _waypt->altitudeRestriction(); |
| 1651 | } |
| 1652 | |
| 1653 | RouteRestriction FlightPlan::Leg::speedRestriction() const |
| 1654 | { |
| 1655 | if (_speedRestrict != RESTRICT_NONE) { |
| 1656 | return _speedRestrict; |
| 1657 | } |
| 1658 | |
| 1659 | return _waypt->speedRestriction(); |
| 1660 | } |
| 1661 | |
| 1662 | void FlightPlan::Leg::setSpeed(RouteRestriction ty, double speed, RouteUnits aUnit) |
| 1663 | { |
| 1664 | _speedRestrict = ty; |
| 1665 | if (aUnit == DEFAULT_UNITS) { |
| 1666 | if (isMachRestrict(ty)) { |
| 1667 | aUnit = SPEED_MACH; |
| 1668 | } else { |
| 1669 | // TODO: check for system in metric? |
| 1670 | aUnit = SPEED_KNOTS; |
| 1671 | } |
| 1672 | } |
| 1673 | _speedUnits = aUnit; |
| 1674 | _speed = speed; |
| 1675 | } |
| 1676 | |
| 1677 | void FlightPlan::Leg::setAltitude(RouteRestriction ty, double alt, RouteUnits aUnit) |
| 1678 | { |
| 1679 | _altRestrict = ty; |
| 1680 | if (aUnit == DEFAULT_UNITS) { |
| 1681 | aUnit = ALTITUDE_FEET; |
| 1682 | } |
| 1683 | _altitudeUnits = aUnit; |
| 1684 | _altitude = alt; |
| 1685 | } |
| 1686 | |
| 1687 | double FlightPlan::Leg::courseDeg() const |
| 1688 | { |
| 1689 | return _courseDeg; |
| 1690 | } |
| 1691 | |
| 1692 | double FlightPlan::Leg::distanceNm() const |
| 1693 | { |
| 1694 | return _pathDistance; |
| 1695 | } |
| 1696 | |
| 1697 | double FlightPlan::Leg::distanceAlongRoute() const |
| 1698 | { |
| 1699 | return _distanceAlongPath; |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | bool FlightPlan::Leg::convertWaypointToHold() |
| 1704 | { |
| 1705 | const auto wty = _waypt->type(); |
| 1706 | if (wty == "hold") { |
| 1707 | return true; |
| 1708 | } |
| 1709 | |
| 1710 | if ((wty != "basic") && (wty != "navaid")) { |
| 1711 | SG_LOG(SG_INSTR, SG_WARN, "convertWaypointToHold: cannot convert waypt " << index() << " " << _waypt->ident() << " to a hold"); |
| 1712 | return false; |
| 1713 | } |
| 1714 | |
| 1715 | auto hold = new Hold(_waypt->position(), _waypt->ident(), const_cast<FlightPlan*>(_parent)); |
| 1716 | |
| 1717 | // default to a 1 minute hold with the radial being our arrival radial |
| 1718 | hold->setHoldTime(60.0); |
| 1719 | hold->setHoldRadial(_courseDeg); |
| 1720 | _waypt = hold; // we drop our reference to the old waypoint |
| 1721 | |
| 1722 | markWaypointDirty(); |
| 1723 | |
| 1724 | return true; |
| 1725 | } |
| 1726 | |
| 1727 | bool FlightPlan::Leg::setHoldCount(int count) |
| 1728 | { |
| 1729 | if (count == 0) { |
| 1730 | _holdCount = count; |
| 1731 | return true; |
| 1732 | } |
| 1733 | |
| 1734 | if (!convertWaypointToHold()) { |
| 1735 | return false; |
| 1736 | } |
| 1737 | |
| 1738 | _holdCount = count; |
| 1739 | markWaypointDirty(); |
| 1740 | return true; |
| 1741 | } |
| 1742 | |
| 1743 | void FlightPlan::Leg::markWaypointDirty() |
| 1744 | { |
| 1745 | auto fp = owner(); |
| 1746 | fp->lockDelegates(); |
| 1747 | fp->_waypointsChanged = true; |
| 1748 | fp->unlockDelegates(); |
| 1749 | } |
| 1750 | |
| 1751 | int FlightPlan::Leg::holdCount() const |
| 1752 | { |
| 1753 | return _holdCount; |
| 1754 | } |
| 1755 | |
| 1756 | void FlightPlan::Leg::writeToProperties(SGPropertyNode* aProp) const |
| 1757 | { |
| 1758 | if (_speedRestrict != RESTRICT_NONE) { |
| 1759 | aProp->setStringValue("speed-restrict", restrictionToString(_speedRestrict)); |
| 1760 | if (_speedUnits == SPEED_MACH) { |
| 1761 | aProp->setDoubleValue("speed-mach", _speed); |
| 1762 | } else if (_speedUnits == SPEED_KPH) { |
| 1763 | aProp->setDoubleValue("speed-kph", _speed); |
| 1764 | } else { |
| 1765 | aProp->setDoubleValue("speed", _speed); |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | if (_altRestrict != RESTRICT_NONE) { |
| 1770 | aProp->setStringValue("alt-restrict", restrictionToString(_altRestrict)); |
| 1771 | if (_altitudeUnits == ALTITUDE_FLIGHTLEVEL) { |
| 1772 | aProp->setDoubleValue("flight-level", _altitude); |
| 1773 | } else if (_altitudeUnits == ALTITUDE_METER) { |
| 1774 | aProp->setDoubleValue("altitude-m", _altitude); |
| 1775 | } else { |
| 1776 | aProp->setDoubleValue("altitude-ft", _altitude); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | if (_holdCount > 0) { |
| 1781 | aProp->setDoubleValue("hold-count", _holdCount); |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | |
| 1786 | void FlightPlan::rebuildLegData() |
| 1787 | { |
| 1788 | _totalDistance = 0.0; |
| 1789 | double totalDistanceIncludingMissed = 0.0; |
| 1790 | RoutePath path(this); |
| 1791 | |
| 1792 | for (unsigned int l=0; l<_legs.size(); ++l) { |
| 1793 | _legs[l]->_courseDeg = path.trackForIndex(l); |
| 1794 | _legs[l]->_pathDistance = path.distanceForIndex(l) * SG_METER_TO_NM; |
| 1795 | |
| 1796 | totalDistanceIncludingMissed += _legs[l]->_pathDistance; |
| 1797 | // distance along path includes our own leg distance |
| 1798 | _legs[l]->_distanceAlongPath = totalDistanceIncludingMissed; |
| 1799 | |
| 1800 | // omit missed-approach waypoints from total distance calculation |
| 1801 | if (!_legs[l]->waypoint()->flag(WPT_MISS)) { |
| 1802 | _totalDistance += _legs[l]->_pathDistance; |
| 1803 | } |
| 1804 | } // of legs iteration |
| 1805 | |
| 1806 | } |
| 1807 | |
| 1808 | SGGeod FlightPlan::pointAlongRoute(int aIndex, double aOffsetNm) const |
| 1809 | { |
| 1810 | RoutePath rp(this); |
| 1811 | return rp.positionForDistanceFrom(aIndex, aOffsetNm * SG_NM_TO_METER); |
| 1812 | } |
| 1813 | |
| 1814 | SGGeod FlightPlan::pointAlongRouteNorm(int aIndex, double aOffsetNorm) const |
| 1815 | { |
| 1816 | RoutePath rp(this); |
| 1817 | if (fabs(aOffsetNorm) > 1.0) { |
| 1818 | SG_LOG(SG_AUTOPILOT, SG_ALERT, "FlightPlan::pointAlongRouteNorm: called with invalid arg:" << aOffsetNorm); |
| 1819 | return rp.positionForIndex(aIndex); |
| 1820 | } |
| 1821 | |
| 1822 | const bool forwards = (aOffsetNorm >= 0.0); |
| 1823 | double d = 0.0; |
| 1824 | if (forwards) { |
| 1825 | d = rp.distanceForIndex(aIndex + 1); |
| 1826 | } else { |
| 1827 | d = rp.distanceForIndex(aIndex); |
| 1828 | } |
| 1829 | |
| 1830 | // in degenerate cases, just use basic position of index |
| 1831 | if (d <= 0.0) { |
| 1832 | return rp.positionForIndex(aIndex); |
| 1833 | } |
| 1834 | |
| 1835 | return rp.positionForDistanceFrom(aIndex, d * aOffsetNorm); |
| 1836 | } |
| 1837 | |
| 1838 | void FlightPlan::lockDelegates() |
| 1839 | { |
| 1840 | if (_delegateLock == 0) { |
| 1841 | assert(!_departureChanged && !_arrivalChanged && |
| 1842 | !_waypointsChanged && !_currentWaypointChanged); |
| 1843 | } |
| 1844 | |
| 1845 | ++_delegateLock; |
| 1846 | if (_delegateLock > 10) { |
| 1847 | SG_LOG(SG_GENERAL, SG_ALERT, "hmmm"); |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | void FlightPlan::runDelegates() |
| 1852 | { |
| 1853 | if (_didLoadFP) { |
| 1854 | _didLoadFP = false; |
| 1855 | for (auto d : _delegates) { |
| 1856 | d->loaded(); |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | if (_departureChanged) { |
| 1861 | _departureChanged = false; |
| 1862 | for (auto d : _delegates) { |
| 1863 | d->departureChanged(); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | if (_arrivalChanged) { |
| 1868 | _arrivalChanged = false; |
| 1869 | for (auto d : _delegates) { |
| 1870 | d->arrivalChanged(); |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | if (_cruiseDataChanged) { |
| 1875 | _cruiseDataChanged = false; |
| 1876 | for (auto d : _delegates) { |
| 1877 | d->cruiseChanged(); |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | if (_waypointsChanged) { |
| 1882 | _waypointsChanged = false; |
| 1883 | rebuildLegData(); |
| 1884 | for (auto d : _delegates) { |
| 1885 | d->waypointsChanged(); |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | if (_currentWaypointChanged) { |
| 1890 | _currentWaypointChanged = false; |
| 1891 | for (auto d : _delegates) { |
| 1892 | d->currentWaypointChanged(); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | const auto cascadingUpdate = _departureChanged | _arrivalChanged | _waypointsChanged | _currentWaypointChanged; |
| 1897 | if (cascadingUpdate) { |
| 1898 | // one of our callbacks, caused a cascading change, so go around again. |
| 1899 | // we assume the delegates will converge after one or more cascades, so we don't check for infinite looping here |
| 1900 | SG_LOG(SG_AUTOPILOT, SG_INFO, "FlightPlan::runDelegates will re-run"); |
| 1901 | runDelegates(); |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | void FlightPlan::unlockDelegates() |
| 1906 | { |
| 1907 | assert(_delegateLock > 0); |
| 1908 | if (_delegateLock > 1) { |
| 1909 | --_delegateLock; |
| 1910 | return; |
| 1911 | } |
| 1912 | |
| 1913 | runDelegates(); |
| 1914 | --_delegateLock; |
| 1915 | } |
| 1916 | |
| 1917 | void FlightPlan::registerDelegateFactory(DelegateFactoryRef df) |
| 1918 | { |
| 1919 | auto it = std::find(static_delegateFactories.begin(), static_delegateFactories.end(), df); |
| 1920 | if (it != static_delegateFactories.end()) { |
| 1921 | throw sg_exception("duplicate delegate factory registration"); |
| 1922 | } |
| 1923 | |
| 1924 | static_delegateFactories.push_back(df); |
| 1925 | } |
| 1926 | |
| 1927 | void FlightPlan::unregisterDelegateFactory(DelegateFactoryRef df) |
| 1928 | { |
| 1929 | auto it = std::find(static_delegateFactories.begin(), static_delegateFactories.end(), df); |
| 1930 | if (it == static_delegateFactories.end()) { |
| 1931 | return; |
| 1932 | } |
| 1933 | |
| 1934 | static_delegateFactories.erase(it); |
| 1935 | } |
| 1936 | |
| 1937 | void FlightPlan::addDelegate(Delegate* d) |
| 1938 | { |
| 1939 | assert(d); |
| 1940 | #ifndef NDEBUG |
| 1941 | auto it = std::find(_delegates.begin(), _delegates.end(), d); |
| 1942 | #endif |
| 1943 | assert(it == _delegates.end()); |
| 1944 | _delegates.push_back(d); |
| 1945 | } |
| 1946 | |
| 1947 | void FlightPlan::removeDelegate(Delegate* d) |
| 1948 | { |
| 1949 | assert(d); |
| 1950 | auto it = std::find(_delegates.begin(), _delegates.end(), d); |
| 1951 | assert(it != _delegates.end()); |
| 1952 | _delegates.erase(it); |
| 1953 | } |
| 1954 | |
| 1955 | void FlightPlan::notifyCleared() |
| 1956 | { |
| 1957 | for (auto d : _delegates) { |
| 1958 | d->cleared(); |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | FlightPlan::Delegate::Delegate() |
| 1963 | { |
| 1964 | } |
| 1965 | |
| 1966 | FlightPlan::Delegate::~Delegate() |
| 1967 | { |
| 1968 | } |
| 1969 | |
| 1970 | void FlightPlan::setFollowLegTrackToFixes(bool tf) |
| 1971 | { |
| 1972 | _followLegTrackToFix = tf; |
| 1973 | } |
| 1974 | |
| 1975 | bool FlightPlan::followLegTrackToFixes() const |
| 1976 | { |
| 1977 | return _followLegTrackToFix; |
| 1978 | } |
| 1979 | |
| 1980 | void FlightPlan::setMaxFlyByTurnAngle(double deg) |
| 1981 | { |
| 1982 | _maxFlyByTurnAngle = deg; |
| 1983 | } |
| 1984 | |
| 1985 | double FlightPlan::maxFlyByTurnAngle() const |
| 1986 | { |
| 1987 | return _maxFlyByTurnAngle; |
| 1988 | } |
| 1989 | |
| 1990 | std::string FlightPlan::icaoAircraftCategory() const |
| 1991 | { |
| 1992 | std::string r; |
| 1993 | r.push_back(_aircraftCategory); |
| 1994 | return r; |
| 1995 | } |
| 1996 | |
| 1997 | void FlightPlan::setIcaoAircraftCategory(const std::string& cat) |
| 1998 | { |
| 1999 | if (cat.empty()) { |
| 2000 | throw sg_range_exception("Invalid ICAO aircraft category:", cat); |
| 2001 | } |
| 2002 | |
| 2003 | if ((cat[0] < ICAO_AIRCRAFT_CATEGORY_A) || (cat[0] > ICAO_AIRCRAFT_CATEGORY_E)) { |
| 2004 | throw sg_range_exception("Invalid ICAO aircraft category:", cat); |
| 2005 | } |
| 2006 | |
| 2007 | _aircraftCategory = cat[0]; |
| 2008 | } |
| 2009 | |
| 2010 | void FlightPlan::setIcaoAircraftType(const string &ty) |
| 2011 | { |
| 2012 | _aircraftType = ty; |
| 2013 | } |
| 2014 | |
| 2015 | bool FlightPlan::parseICAOLatLon(const std::string& s, SGGeod& p) |
| 2016 | { |
| 2017 | if (s.size() == 7) { |
| 2018 | double latDegrees = std::stoi(s); |
| 2019 | double lonDegrees = std::stoi(s.substr(3, 3)); |
| 2020 | if (s[2] == 'S') latDegrees = -latDegrees; |
| 2021 | if (s[6] == 'W') lonDegrees = -lonDegrees; |
| 2022 | p = SGGeod::fromDeg(lonDegrees, latDegrees); |
| 2023 | return true; |
| 2024 | } else if (s.size() == 11) { |
| 2025 | // problem here is we have minutes, not decimal degrees |
| 2026 | double latDegrees = std::stoi(s.substr(0, 2)); |
| 2027 | double latMinutes = std::stoi(s.substr(2, 2)); |
| 2028 | latDegrees += (latMinutes / 60.0); |
| 2029 | double lonDegrees = std::stoi(s.substr(5, 3)); |
| 2030 | double lonMinutes = std::stoi(s.substr(8, 2)); |
| 2031 | lonDegrees += (lonMinutes / 60.0); |
| 2032 | |
| 2033 | if (s[4] == 'S') latDegrees = -latDegrees; |
| 2034 | if (s[10] == 'W') lonDegrees = -lonDegrees; |
| 2035 | p = SGGeod::fromDeg(lonDegrees, latDegrees); |
| 2036 | return true; |
| 2037 | } |
| 2038 | |
| 2039 | return false; |
| 2040 | } |
| 2041 | |
| 2042 | bool FlightPlan::parseICAORouteString(const std::string& routeData) |
| 2043 | { |
| 2044 | auto tokens = simgear::strutils::split(routeData); |
| 2045 | if (tokens.empty()) |
| 2046 | return false; |
| 2047 | |
| 2048 | std::string tk; |
| 2049 | std::string nextToken; |
| 2050 | |
| 2051 | FGAirportRef firstICAO = FGAirport::findByIdent(tokens.front()); |
| 2052 | unsigned int i = 0; |
| 2053 | |
| 2054 | if (firstICAO) { |
| 2055 | // route string starts with an airport, let's see if it matches |
| 2056 | // our departure airport |
| 2057 | if (!_departure) { |
| 2058 | setDeparture(firstICAO); |
| 2059 | } else if (_departure != firstICAO) { |
| 2060 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route begins with an airport which is not the departure airport:" << tokens.front()); |
| 2061 | return false; |
| 2062 | } |
| 2063 | ++i; // either way, skip the ICAO token now |
| 2064 | } |
| 2065 | |
| 2066 | WayptVec enroute; |
| 2067 | SGGeod currentPos; |
| 2068 | if (_departure) |
| 2069 | currentPos = _departure->geod(); |
| 2070 | |
| 2071 | for (; i < tokens.size(); ++i) { |
| 2072 | tk = tokens.at(i); |
| 2073 | // update current position for next search |
| 2074 | if (!enroute.empty()) { |
| 2075 | currentPos = enroute.back()->position(); |
| 2076 | } |
| 2077 | |
| 2078 | if (isdigit(tk.front())) { |
| 2079 | // might be a lat lon (but some airway idents also start with a digit...) |
| 2080 | SGGeod geod; |
| 2081 | bool ok = parseICAOLatLon(tk, geod); |
| 2082 | if (ok) { |
| 2083 | enroute.push_back(new BasicWaypt(geod, tk, this)); |
| 2084 | continue; |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | nextToken = (i < (tokens.size() - 1)) ? tokens.at(i+1) : std::string(); |
| 2089 | |
| 2090 | if (tk == "DCT") { |
| 2091 | if (nextToken.empty()) { |
| 2092 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route DIRECT segment missing waypoint"); |
| 2093 | return false; |
| 2094 | } |
| 2095 | |
| 2096 | FGPositionedRef wpt = FGPositioned::findClosestWithIdent(nextToken, currentPos); |
| 2097 | if (!wpt) { |
| 2098 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route waypoint not found:" << nextToken); |
| 2099 | return false; |
| 2100 | } |
| 2101 | enroute.push_back(new NavaidWaypoint(wpt, this)); |
| 2102 | ++i; |
| 2103 | } else if (tk == "STAR") { |
| 2104 | // look for a STAR based on the preceeding transition point |
| 2105 | auto starTrans = _destination->selectSTARByEnrouteTransition(enroute.back()->source()); |
| 2106 | if (!starTrans) { |
| 2107 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route couldn't find STAR transitioning from " << |
| 2108 | enroute.back()->source()->ident()); |
| 2109 | } else { |
| 2110 | setSTAR(starTrans); |
| 2111 | } |
| 2112 | } else if (tk == "SID") { |
| 2113 | // select a SID based on the next point |
| 2114 | FGPositionedRef wpt = FGPositioned::findClosestWithIdent(nextToken, currentPos); |
| 2115 | auto sidTrans = _departure->selectSIDByEnrouteTransition(wpt); |
| 2116 | if (!sidTrans) { |
| 2117 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route couldn't find SID transitioning to " << nextToken); |
| 2118 | } else { |
| 2119 | setSID(sidTrans); |
| 2120 | } |
| 2121 | ++i; |
| 2122 | } else { |
| 2123 | if (nextToken.empty()) { |
| 2124 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route airway segment missing transition:" << tk); |
| 2125 | return false; |
| 2126 | } |
| 2127 | |
| 2128 | auto nav = Airway::highLevel()->findNodeByIdent(nextToken, currentPos); |
| 2129 | if (!nav) |
| 2130 | nav = Airway::lowLevel()->findNodeByIdent(nextToken, currentPos); |
| 2131 | if (!nav) { |
| 2132 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route waypoint not found:" << nextToken); |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
| 2136 | WayptRef toNav = new NavaidWaypoint(nav, nullptr); // temp waypoint for lookup |
| 2137 | WayptRef previous; |
| 2138 | if (enroute.empty()) { |
| 2139 | if (_sid) { |
| 2140 | if (!_sidTransition.empty()) { |
| 2141 | previous = _sid->findTransitionByName(_sidTransition)->enroute(); |
| 2142 | } else { |
| 2143 | // final wayypoint of common section |
| 2144 | previous = _sid->common().back(); |
| 2145 | } |
| 2146 | } else { |
| 2147 | SG_LOG(SG_AUTOPILOT, SG_WARN, "initial airway needs anchor point from SID:" << tk); |
| 2148 | return false; |
| 2149 | } |
| 2150 | } else { |
| 2151 | previous = enroute.back(); |
| 2152 | } |
| 2153 | |
| 2154 | AirwayRef way = Airway::findByIdentAndVia(tk, enroute.back(), toNav); |
| 2155 | if (way) { |
| 2156 | enroute.push_back(new Via(this, way, nav)); |
| 2157 | ++i; |
| 2158 | } else { |
| 2159 | SG_LOG(SG_AUTOPILOT, SG_WARN, "ICAO route unknown airway:" << tk); |
| 2160 | return false; |
| 2161 | } |
| 2162 | } |
| 2163 | } // of token iteration |
| 2164 | |
| 2165 | lockDelegates(); |
| 2166 | _waypointsChanged = true; |
| 2167 | |
| 2168 | SG_LOG(SG_AUTOPILOT, SG_INFO, "adding waypoints from string"); |
| 2169 | // rebuild legs from waypoints we created |
| 2170 | _legs.clear(); |
| 2171 | insertWayptsAtIndex(enroute, 0); |
| 2172 | |
| 2173 | unlockDelegates(); |
| 2174 | |
| 2175 | SG_LOG(SG_AUTOPILOT, SG_INFO, "legs now:" << numLegs()); |
| 2176 | |
| 2177 | return true; |
| 2178 | } |
| 2179 | |
| 2180 | std::string FlightPlan::asICAORouteString() const |
| 2181 | { |
| 2182 | std::string result; |
| 2183 | if (!_sidTransition.empty()) |
| 2184 | result += _sidTransition + " "; |
| 2185 | |
| 2186 | for (auto l : _legs) { |
| 2187 | const auto wpt = l->waypoint(); |
| 2188 | |
| 2189 | AirwayRef nextLegAirway; |
| 2190 | if (l->nextLeg() && l->nextLeg()->waypoint()->flag(WPT_VIA)) { |
| 2191 | nextLegAirway = static_cast<Airway*>(l->nextLeg()->waypoint()->owner()); |
| 2192 | } |
| 2193 | |
| 2194 | if (wpt->flag(WPT_GENERATED)) { |
| 2195 | if (wpt->flag(WPT_VIA)) { |
| 2196 | AirwayRef awy = static_cast<Airway*>(wpt->owner()); |
| 2197 | if (awy == nextLegAirway) { |
| 2198 | // skipepd entirely, next leg will output the airway |
| 2199 | continue; |
| 2200 | } |
| 2201 | |
| 2202 | result += awy->ident() + " "; |
| 2203 | } |
| 2204 | } else if (wpt->type() == "navaid") { |
| 2205 | // technically we need DCT unless both ends of the leg are |
| 2206 | // defined geographically |
| 2207 | result += "DCT "; |
| 2208 | } |
| 2209 | result += wpt->icaoDescription() + " "; |
| 2210 | } |
| 2211 | |
| 2212 | if (!_starTransition.empty()) |
| 2213 | result += _starTransition; |
| 2214 | |
| 2215 | return result; |
| 2216 | } |
| 2217 | |
| 2218 | void FlightPlan::setFlightRules(ICAOFlightRules rules) |
| 2219 | { |
| 2220 | _flightRules = rules; |
| 2221 | } |
| 2222 | |
| 2223 | ICAOFlightRules FlightPlan::flightRules() const |
| 2224 | { |
| 2225 | return _flightRules; |
| 2226 | } |
| 2227 | |
| 2228 | void FlightPlan::setCallsign(const std::string& callsign) |
| 2229 | { |
| 2230 | _callsign = callsign; |
| 2231 | } |
| 2232 | |
| 2233 | void FlightPlan::setRemarks(const std::string& remarks) |
| 2234 | { |
| 2235 | _remarks = remarks; |
| 2236 | } |
| 2237 | |
| 2238 | void FlightPlan::setFlightType(ICAOFlightType type) |
| 2239 | { |
| 2240 | _flightType = type; |
| 2241 | } |
| 2242 | |
| 2243 | ICAOFlightType FlightPlan::flightType() const |
| 2244 | { |
| 2245 | return _flightType; |
| 2246 | } |
| 2247 | |
| 2248 | void FlightPlan::setCruiseSpeedKnots(int kts) |
| 2249 | { |
| 2250 | lockDelegates(); |
| 2251 | _cruiseDataChanged = true; |
| 2252 | _cruiseAirspeedKnots = kts; |
| 2253 | _cruiseAirspeedMach = 0.0; |
| 2254 | _cruiseAirspeedKph = 0; |
| 2255 | unlockDelegates(); |
| 2256 | } |
| 2257 | |
| 2258 | int FlightPlan::cruiseSpeedKnots() const |
| 2259 | { |
| 2260 | return _cruiseAirspeedKnots; |
| 2261 | } |
| 2262 | |
| 2263 | void FlightPlan::setCruiseSpeedMach(double mach) |
| 2264 | { |
| 2265 | lockDelegates(); |
| 2266 | _cruiseDataChanged = true; |
| 2267 | _cruiseAirspeedKnots = 0; |
| 2268 | _cruiseAirspeedMach = mach; |
| 2269 | _cruiseAirspeedKph = 0; |
| 2270 | unlockDelegates(); |
| 2271 | } |
| 2272 | |
| 2273 | double FlightPlan::cruiseSpeedMach() const |
| 2274 | { |
| 2275 | return _cruiseAirspeedMach; |
| 2276 | } |
| 2277 | |
| 2278 | void FlightPlan::setCruiseSpeedKPH(int kph) |
| 2279 | { |
| 2280 | lockDelegates(); |
| 2281 | _cruiseDataChanged = true; |
| 2282 | _cruiseAirspeedKnots = 0; |
| 2283 | _cruiseAirspeedMach = 0.0; |
| 2284 | _cruiseAirspeedKph = kph; |
| 2285 | unlockDelegates(); |
| 2286 | } |
| 2287 | |
| 2288 | int FlightPlan::cruiseSpeedKPH() const |
| 2289 | { |
| 2290 | return _cruiseAirspeedKph; |
| 2291 | } |
| 2292 | |
| 2293 | void FlightPlan::setCruiseFlightLevel(int flightLevel) |
| 2294 | { |
| 2295 | lockDelegates(); |
| 2296 | _cruiseDataChanged = true; |
| 2297 | _cruiseAltitudeFt = 0; |
| 2298 | _cruiseAltitudeM = 0; |
| 2299 | _cruiseFlightLevel = flightLevel; |
| 2300 | unlockDelegates(); |
| 2301 | } |
| 2302 | |
| 2303 | int FlightPlan::cruiseFlightLevel() const |
| 2304 | { |
| 2305 | return _cruiseFlightLevel; |
| 2306 | } |
| 2307 | |
| 2308 | void FlightPlan::setCruiseAltitudeFt(int altFt) |
| 2309 | { |
| 2310 | lockDelegates(); |
| 2311 | _cruiseDataChanged = true; |
| 2312 | _cruiseAltitudeFt = altFt; |
| 2313 | _cruiseAltitudeM = 0; |
| 2314 | _cruiseFlightLevel = 0; |
| 2315 | unlockDelegates(); |
| 2316 | } |
| 2317 | |
| 2318 | int FlightPlan::cruiseAltitudeFt() const |
| 2319 | { |
| 2320 | return _cruiseAltitudeFt; |
| 2321 | } |
| 2322 | |
| 2323 | void FlightPlan::setCruiseAltitudeM(int altM) |
| 2324 | { |
| 2325 | lockDelegates(); |
| 2326 | _cruiseDataChanged = true; |
| 2327 | _cruiseAltitudeFt = 0; |
| 2328 | _cruiseAltitudeM = altM; |
| 2329 | _cruiseFlightLevel = 0; |
| 2330 | unlockDelegates(); |
| 2331 | } |
| 2332 | |
| 2333 | int FlightPlan::cruiseAltitudeM() const |
| 2334 | { |
| 2335 | return _cruiseAltitudeM; |
| 2336 | } |
| 2337 | |
| 2338 | void FlightPlan::forEachLeg(const LegVisitor& lv) |
| 2339 | { |
| 2340 | std::for_each(_legs.begin(), _legs.end(), lv); |
| 2341 | } |
| 2342 | |
| 2343 | |
| 2344 | int FlightPlan::indexOfFirstNonDepartureWaypoint() const |
| 2345 | { |
| 2346 | const auto numLegs = _legs.size(); |
| 2347 | for (size_t i = 0; i < numLegs; ++i) { |
| 2348 | if (!(_legs.at(i)->waypoint()->flags() & WPT_DEPARTURE)) |
| 2349 | return static_cast<int>(i); |
| 2350 | } |
| 2351 | |
| 2352 | // all waypoints are marked as departure |
| 2353 | return -1; |
| 2354 | } |
| 2355 | |
| 2356 | int FlightPlan::indexOfFirstArrivalWaypoint() const |
| 2357 | { |
| 2358 | const auto numLegs = _legs.size(); |
| 2359 | for (size_t i = 0; i < numLegs; ++i) { |
| 2360 | if (_legs.at(i)->waypoint()->flags() & WPT_ARRIVAL) |
| 2361 | return static_cast<int>(i); |
| 2362 | } |
| 2363 | |
| 2364 | // no waypoints are marked as arrival |
| 2365 | return -1; |
| 2366 | } |
| 2367 | |
| 2368 | int FlightPlan::indexOfFirstApproachWaypoint() const |
| 2369 | { |
| 2370 | const auto numLegs = _legs.size(); |
| 2371 | for (size_t i = 0; i < numLegs; ++i) { |
| 2372 | if (_legs.at(i)->waypoint()->flags() & WPT_APPROACH) |
| 2373 | return static_cast<int>(i); |
| 2374 | } |
| 2375 | |
| 2376 | // no waypoints are marked as arrival |
| 2377 | return -1; |
| 2378 | } |
| 2379 | |
| 2380 | int FlightPlan::indexOfDestinationRunwayWaypoint() const |
| 2381 | { |
| 2382 | if (!_destinationRunway) |
| 2383 | return -1; |
| 2384 | |
| 2385 | // work backwards in case the departure and destination match |
| 2386 | // this way we'll find the one we want |
| 2387 | for (int i = numLegs() - 1; i >= 0; i--) { |
| 2388 | if (_legs.at(i)->waypoint()->source() == _destinationRunway) { |
| 2389 | return i; |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | return -1; |
| 2394 | } |
| 2395 | |
| 2396 | void FlightPlan::DelegateFactory::destroyFlightPlanDelegate(FlightPlan* fp, Delegate* d) |
| 2397 | { |
| 2398 | // mimic old behaviour before destroyFlightPlanDelegate was added |
| 2399 | SG_UNUSED(fp); |
| 2400 | delete d; |
| 2401 | } |
| 2402 | |
| 2403 | |
| 2404 | } // of namespace flightgear |