| 1 | /* |
| 2 | * SPDX-FileName: AIFlightPlan.cxx |
| 3 | * SPDX-FileComment: class for loading and storing AI flight plans |
| 4 | * SPDX-FileCopyrightText: Written by David Culp, started May 2004 - [email protected] |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include <config.h> |
| 10 | #endif |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <iterator> |
| 14 | |
| 15 | #include <simgear/constants.h> |
| 16 | #include <simgear/debug/logstream.hxx> |
| 17 | #include <simgear/io/iostreams/sgstream.hxx> |
| 18 | #include <simgear/math/sg_geodesy.hxx> |
| 19 | #include <simgear/misc/sg_path.hxx> |
| 20 | #include <simgear/props/props.hxx> |
| 21 | #include <simgear/props/props_io.hxx> |
| 22 | #include <simgear/structure/exception.hxx> |
| 23 | #include <simgear/timing/sg_time.hxx> |
| 24 | |
| 25 | #include <Airports/airport.hxx> |
| 26 | #include <Airports/dynamics.hxx> |
| 27 | #include <Airports/groundnetwork.hxx> |
| 28 | #include <Airports/runways.hxx> |
| 29 | #include <Main/fg_init.hxx> |
| 30 | #include <Main/fg_props.hxx> |
| 31 | #include <Main/globals.hxx> |
| 32 | |
| 33 | #include <Environment/environment.hxx> |
| 34 | #include <Environment/environment_mgr.hxx> |
| 35 | |
| 36 | #include <Traffic/Schedule.hxx> |
| 37 | |
| 38 | #include "AIAircraft.hxx" |
| 39 | #include "AIFlightPlan.hxx" |
| 40 | |
| 41 | using std::string; |
| 42 | |
| 43 | FGAIWaypoint::FGAIWaypoint() |
| 44 | { |
| 45 | speed = 0; |
| 46 | crossat = 0; |
| 47 | finished = 0; |
| 48 | gear_down = 0; |
| 49 | flaps = 0; |
| 50 | on_ground = 0; |
| 51 | routeIndex = 0; |
| 52 | time_sec = 0; |
| 53 | trackLength = 0; |
| 54 | } |
| 55 | |
| 56 | bool FGAIWaypoint::contains(const string& target) |
| 57 | { |
| 58 | size_t found = name.find(target); |
| 59 | if (found == string::npos) |
| 60 | return false; |
| 61 | else |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | double FGAIWaypoint::getLatitude() const |
| 66 | { |
| 67 | return pos.getLatitudeDeg(); |
| 68 | } |
| 69 | |
| 70 | double FGAIWaypoint::getLongitude() const |
| 71 | { |
| 72 | return pos.getLongitudeDeg(); |
| 73 | } |
| 74 | |
| 75 | double FGAIWaypoint::getAltitude() const |
| 76 | { |
| 77 | return pos.getElevationFt(); |
| 78 | } |
| 79 | |
| 80 | void FGAIWaypoint::setLatitude(double lat) |
| 81 | { |
| 82 | pos.setLatitudeDeg(lat); |
| 83 | } |
| 84 | |
| 85 | void FGAIWaypoint::setLongitude(double lon) |
| 86 | { |
| 87 | pos.setLongitudeDeg(lon); |
| 88 | } |
| 89 | |
| 90 | void FGAIWaypoint::setAltitude(double alt) |
| 91 | { |
| 92 | pos.setElevationFt(alt); |
| 93 | } |
| 94 | |
| 95 | FGAIFlightPlan::FGAIFlightPlan() : sid(NULL), |
| 96 | repeat(false), |
| 97 | distance_to_go(0), |
| 98 | lead_distance_ft(0), |
| 99 | leadInAngle(0), |
| 100 | start_time(0), |
| 101 | arrivalTime(0), |
| 102 | leg(0), |
| 103 | lastNodeVisited(0), |
| 104 | isValid(true) |
| 105 | { |
| 106 | wpt_iterator = waypoints.begin(); |
| 107 | } |
| 108 | |
| 109 | FGAIFlightPlan::FGAIFlightPlan(const string& filename) : sid(NULL), |
| 110 | repeat(false), |
| 111 | distance_to_go(0), |
| 112 | lead_distance_ft(0), |
| 113 | leadInAngle(0), |
| 114 | start_time(0), |
| 115 | arrivalTime(0), |
| 116 | leg(10), |
| 117 | lastNodeVisited(0), |
| 118 | isValid(parseProperties(filename)) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * This is a modified version of the constructor, |
| 125 | * Which not only reads the waypoints from a |
| 126 | * Flight plan file, but also adds the current |
| 127 | * Position computed by the traffic manager, as well |
| 128 | * as setting speeds and altitude computed by the |
| 129 | * traffic manager. |
| 130 | */ |
| 131 | FGAIFlightPlan::FGAIFlightPlan(FGAIAircraft* ac, |
| 132 | const std::string& p, |
| 133 | double course, |
| 134 | time_t start, |
| 135 | time_t remainingTime, |
| 136 | FGAirport* dep, |
| 137 | FGAirport* arr, |
| 138 | bool firstLeg, |
| 139 | double radius, |
| 140 | double alt, |
| 141 | double lat, |
| 142 | double lon, |
| 143 | double speed, |
| 144 | const string& fltType, |
| 145 | const string& acType, |
| 146 | const string& airline) : sid(NULL), |
| 147 | repeat(false), |
| 148 | distance_to_go(0), |
| 149 | lead_distance_ft(0), |
| 150 | leadInAngle(0), |
| 151 | start_time(start), |
| 152 | arrivalTime(0), |
| 153 | leg(10), |
| 154 | lastNodeVisited(0), |
| 155 | isValid(false), |
| 156 | departure(dep), |
| 157 | arrival(arr) |
| 158 | { |
| 159 | if (parseProperties(p)) { |
| 160 | isValid = true; |
| 161 | } else { |
| 162 | createWaypoints(ac, course, start, remainingTime, dep, arr, firstLeg, radius, |
| 163 | alt, lat, lon, speed, fltType, acType, airline); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | FGAIFlightPlan::~FGAIFlightPlan() |
| 168 | { |
| 169 | deleteWaypoints(); |
| 170 | //delete taxiRoute; |
| 171 | } |
| 172 | |
| 173 | void FGAIFlightPlan::createWaypoints(FGAIAircraft* ac, |
| 174 | double course, |
| 175 | time_t start, |
| 176 | time_t remainingTime, |
| 177 | FGAirport* dep, |
| 178 | FGAirport* arr, |
| 179 | bool firstLeg, |
| 180 | double radius, |
| 181 | double alt, |
| 182 | double lat, |
| 183 | double lon, |
| 184 | double speed, |
| 185 | const string& fltType, |
| 186 | const string& acType, |
| 187 | const string& airline) |
| 188 | { |
| 189 | time_t now = globals->get_time_params()->get_cur_time(); |
| 190 | time_t timeDiff = now - start; |
| 191 | leg = AILeg::STARTUP_PUSHBACK; |
| 192 | |
| 193 | if ((timeDiff > 60) && (timeDiff < 1500)) |
| 194 | leg = AILeg::TAXI; |
| 195 | else if ((timeDiff >= 1500) && (timeDiff < 2000)) |
| 196 | leg = AILeg::TAKEOFF; |
| 197 | else if (timeDiff >= 2000) { |
| 198 | if (remainingTime > 2000) { |
| 199 | leg = AILeg::CRUISE; |
| 200 | } else { |
| 201 | leg = AILeg::APPROACH; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | SG_LOG(SG_AI, SG_DEBUG, ac->getTrafficRef()->getCallSign() << "|Route from " << dep->getId() << " to " << arr->getId() << ". Set leg to : " << leg << " " << remainingTime); |
| 206 | |
| 207 | wpt_iterator = waypoints.begin(); |
| 208 | bool dist = 0; |
| 209 | isValid = create(ac, dep, arr, leg, alt, speed, lat, lon, |
| 210 | firstLeg, radius, fltType, acType, airline, dist); |
| 211 | wpt_iterator = waypoints.begin(); |
| 212 | } |
| 213 | |
| 214 | bool FGAIFlightPlan::parseProperties(const std::string& filename) |
| 215 | { |
| 216 | SGPath fp = globals->findDataPath("AI/FlightPlans/" + filename); |
| 217 | if (!fp.exists()) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | return readFlightplan(fp); |
| 222 | } |
| 223 | |
| 224 | bool FGAIFlightPlan::readFlightplan(const SGPath& file) |
| 225 | { |
| 226 | sg_ifstream f(file); |
| 227 | return readFlightplan(f, file); |
| 228 | } |
| 229 | |
| 230 | bool FGAIFlightPlan::readFlightplan(std::istream& stream, const sg_location& loc) |
| 231 | { |
| 232 | SGPropertyNode root; |
| 233 | try { |
| 234 | readProperties(stream, &root); |
| 235 | } catch (const sg_exception& e) { |
| 236 | SG_LOG(SG_AI, SG_ALERT, "Error reading AI flight plan: " << loc.asString() << " message:" << e.getFormattedMessage()); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | SGPropertyNode* node = root.getNode("flightplan"); |
| 241 | if (!node) { |
| 242 | SG_LOG(SG_AI, SG_ALERT, "Error reading AI flight plan: " << loc.asString() << ": no <flightplan> root element"); |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | for (int i = 0; i < node->nChildren(); i++) { |
| 247 | FGAIWaypoint* wpt = new FGAIWaypoint; |
| 248 | SGPropertyNode* wpt_node = node->getChild(i); |
| 249 | |
| 250 | bool gear, flaps; |
| 251 | |
| 252 | // Calculate some default values if they are not set explicitly in the flightplan |
| 253 | if (wpt_node->getDoubleValue("ktas", 0) < 1.0f) { |
| 254 | // Not moving so assume shut down. |
| 255 | wpt->setPowerDownLights(); |
| 256 | flaps = false; |
| 257 | gear = true; |
| 258 | } else if (wpt_node->getDoubleValue("alt", 0) > 10000.0f) { |
| 259 | // Cruise flight; |
| 260 | wpt->setCruiseLights(); |
| 261 | flaps = false; |
| 262 | gear = false; |
| 263 | } else if (wpt_node->getBoolValue("on-ground", false)) { |
| 264 | // On ground |
| 265 | wpt->setGroundLights(); |
| 266 | flaps = true; |
| 267 | gear = true; |
| 268 | } else if (wpt_node->getDoubleValue("alt", 0) < 3000.0f) { |
| 269 | // In the air below 3000 ft, so flaps and gear down. |
| 270 | wpt->setApproachLights(); |
| 271 | flaps = true; |
| 272 | gear = true; |
| 273 | } else { |
| 274 | // In the air 3000-10000 ft |
| 275 | wpt->setApproachLights(); |
| 276 | flaps = false; |
| 277 | gear = false; |
| 278 | } |
| 279 | |
| 280 | wpt->setName(wpt_node->getStringValue("name", "END")); |
| 281 | wpt->setLatitude(wpt_node->getDoubleValue("lat", 0)); |
| 282 | wpt->setLongitude(wpt_node->getDoubleValue("lon", 0)); |
| 283 | wpt->setAltitude(wpt_node->getDoubleValue("alt", 0)); |
| 284 | wpt->setSpeed(wpt_node->getDoubleValue("ktas", 0)); |
| 285 | wpt->setCrossat(wpt_node->getDoubleValue("crossat", -10000)); |
| 286 | wpt->setGear_down(wpt_node->getBoolValue("gear-down", gear)); |
| 287 | wpt->setFlaps(wpt_node->getBoolValue("flaps-down", flaps) ? 1.0 : 0.0); |
| 288 | wpt->setSpoilers(wpt_node->getBoolValue("spoilers", false) ? 1.0 : 0.0); |
| 289 | wpt->setSpeedBrakes(wpt_node->getBoolValue("speedbrakes", false) ? 1.0 : 0.0); |
| 290 | wpt->setOn_ground(wpt_node->getBoolValue("on-ground", false)); |
| 291 | wpt->setTime_sec(wpt_node->getDoubleValue("time-sec", 0)); |
| 292 | wpt->setTime(wpt_node->getStringValue("time", "")); |
| 293 | wpt->setFinished((wpt->getName() == "END")); |
| 294 | pushBackWaypoint(wpt); |
| 295 | } |
| 296 | |
| 297 | auto lastWp = getLastWaypoint(); |
| 298 | if (!lastWp || lastWp->getName().compare("END") != 0) { |
| 299 | SG_LOG(SG_AI, SG_ALERT, "FGAIFlightPlan::Flightplan (" + loc.asString() + ") missing END node"); |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | wpt_iterator = waypoints.begin(); |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | FGAIWaypoint* FGAIFlightPlan::getLastWaypoint() const |
| 309 | { |
| 310 | if (waypoints.empty()) |
| 311 | return nullptr; |
| 312 | |
| 313 | return waypoints.back(); |
| 314 | }; |
| 315 | |
| 316 | FGAIWaypoint* FGAIFlightPlan::getPreviousWaypoint() const |
| 317 | { |
| 318 | if (empty()) |
| 319 | return nullptr; |
| 320 | |
| 321 | if (wpt_iterator == waypoints.begin()) { |
| 322 | return nullptr; |
| 323 | } else { |
| 324 | wpt_vector_iterator prev = wpt_iterator; |
| 325 | return *(--prev); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | FGAIWaypoint* FGAIFlightPlan::getCurrentWaypoint() const |
| 330 | { |
| 331 | if (wpt_iterator == waypoints.end()) |
| 332 | return nullptr; |
| 333 | return *wpt_iterator; |
| 334 | } |
| 335 | |
| 336 | FGAIWaypoint* FGAIFlightPlan::getNextWaypoint() const |
| 337 | { |
| 338 | if (wpt_iterator == waypoints.end()) |
| 339 | return nullptr; |
| 340 | |
| 341 | wpt_vector_iterator last = waypoints.end() - 1; |
| 342 | if (wpt_iterator == last) { |
| 343 | return nullptr; |
| 344 | } else { |
| 345 | return *(wpt_iterator + 1); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | int FGAIFlightPlan::getNextTurnAngle() const |
| 350 | { |
| 351 | return nextTurnAngle; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | void FGAIFlightPlan::IncrementWaypoint(bool eraseWaypoints) |
| 356 | { |
| 357 | if (empty()) |
| 358 | return; |
| 359 | |
| 360 | if (eraseWaypoints) { |
| 361 | if (wpt_iterator == waypoints.begin()) |
| 362 | ++wpt_iterator; |
| 363 | else if (!waypoints.empty()) { |
| 364 | delete *(waypoints.begin()); |
| 365 | waypoints.erase(waypoints.begin()); |
| 366 | wpt_iterator = waypoints.begin(); |
| 367 | ++wpt_iterator; |
| 368 | } |
| 369 | } else { |
| 370 | ++wpt_iterator; |
| 371 | } |
| 372 | // Calculate the angle of the next turn. |
| 373 | if (wpt_iterator == waypoints.end()) |
| 374 | return; |
| 375 | if (wpt_iterator == waypoints.begin()) |
| 376 | return; |
| 377 | if (wpt_iterator + 1 == waypoints.end()) |
| 378 | return; |
| 379 | if (waypoints.size() < 3) |
| 380 | return; |
| 381 | FGAIWaypoint* previousWP = *(wpt_iterator - 1); |
| 382 | FGAIWaypoint* currentWP = *(wpt_iterator); |
| 383 | FGAIWaypoint* nextWP = *(wpt_iterator + 1); |
| 384 | int currentBearing = this->getBearing(previousWP, currentWP); |
| 385 | int nextBearing = this->getBearing(currentWP, nextWP); |
| 386 | |
| 387 | nextTurnAngle = SGMiscd::normalizePeriodic(-180, 180, nextBearing - currentBearing); |
| 388 | if ((previousWP->getSpeed() > 0 && nextWP->getSpeed() < 0) || |
| 389 | (previousWP->getSpeed() < 0 && nextWP->getSpeed() > 0)) { |
| 390 | nextTurnAngle += 180; |
| 391 | SG_LOG(SG_AI, SG_BULK, "Add 180 to turn angle pushback end"); |
| 392 | } |
| 393 | SG_LOG(SG_AI, SG_BULK, "Calculated next turn angle " << nextTurnAngle << " " << previousWP->getName() << " " << currentWP->getName() << " Previous Speed " << previousWP->getSpeed() << " Next Speed " << nextWP->getSpeed()); |
| 394 | } |
| 395 | |
| 396 | void FGAIFlightPlan::DecrementWaypoint() |
| 397 | { |
| 398 | if (empty()) |
| 399 | return; |
| 400 | |
| 401 | --wpt_iterator; |
| 402 | } |
| 403 | |
| 404 | void FGAIFlightPlan::eraseLastWaypoint() |
| 405 | { |
| 406 | if (empty()) |
| 407 | return; |
| 408 | |
| 409 | delete (waypoints.back()); |
| 410 | waypoints.pop_back(); |
| 411 | wpt_iterator = waypoints.begin(); |
| 412 | ++wpt_iterator; |
| 413 | } |
| 414 | |
| 415 | // gives distance in meters from a position to a waypoint |
| 416 | double FGAIFlightPlan::getDistanceToGo(double lat, double lon, FGAIWaypoint* wp) const |
| 417 | { |
| 418 | return SGGeodesy::distanceM(SGGeod::fromDeg(lon, lat), wp->getPos()); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | sets distance in feet from a lead point to the current waypoint |
| 423 | basically a catch radius, that triggers the advancement of WPs |
| 424 | */ |
| 425 | void FGAIFlightPlan::setLeadDistance(double speed, |
| 426 | double bearing, |
| 427 | FGAIWaypoint* current, |
| 428 | FGAIWaypoint* next) |
| 429 | { |
| 430 | double turn_radius_m; |
| 431 | // Handle Ground steering |
| 432 | // At a turn rate of 30 degrees per second, it takes 12 seconds to do a full 360 degree turn |
| 433 | // So, to get an estimate of the turn radius, calculate the circumference of the circle |
| 434 | // we travel on. Get the turn radius by dividing by PI (*2). |
| 435 | // FIXME Why when going backwards? No fabs |
| 436 | if (speed < 0.5) { |
| 437 | setLeadDistance(0.5); |
| 438 | return; |
| 439 | } |
| 440 | if (speed > 0 && speed < 0.5) { |
| 441 | setLeadDistance(5 * SG_METER_TO_FEET); |
| 442 | SG_LOG(SG_AI, SG_BULK, "Setting Leaddistance fixed " << (lead_distance_ft * SG_FEET_TO_METER)); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | double speed_mps = speed * SG_KT_TO_MPS; |
| 447 | if (speed < 25) { |
| 448 | turn_radius_m = ((360 / 30) * fabs(speed_mps)) / (2 * M_PI); |
| 449 | } else { |
| 450 | turn_radius_m = 0.1911 * speed * speed; // an estimate for 25 degrees bank |
| 451 | } |
| 452 | |
| 453 | double inbound = bearing; |
| 454 | double outbound = getBearing(current, next); |
| 455 | leadInAngle = fabs(inbound - outbound); |
| 456 | if (leadInAngle > 180.0) leadInAngle = 360.0 - leadInAngle; |
| 457 | //if (leadInAngle < 30.0) // To prevent lead_dist from getting so small it is skipped |
| 458 | // leadInAngle = 30.0; |
| 459 | |
| 460 | //lead_distance_ft = turn_radius * sin(leadInAngle * SG_DEGREES_TO_RADIANS); |
| 461 | |
| 462 | if ((int)leadInAngle == 0) { |
| 463 | double lead_distance_m = fabs(2 * speed) * SG_FEET_TO_METER; |
| 464 | setLeadDistance(lead_distance_m * SG_METER_TO_FEET); |
| 465 | if (lead_distance_ft > 1000) { |
| 466 | SG_LOG(SG_AI, SG_BULK, "Excessive leaddistance leadin 0 " << lead_distance_ft << " leadInAngle " << leadInAngle << " inbound " << inbound << " outbound " << outbound); |
| 467 | } |
| 468 | } else { |
| 469 | double lead_distance_m = turn_radius_m * tan((leadInAngle * SG_DEGREES_TO_RADIANS) / 2); |
| 470 | setLeadDistance(lead_distance_m * SG_METER_TO_FEET); |
| 471 | SG_LOG(SG_AI, SG_BULK, "Setting Leaddistance " << (lead_distance_ft * SG_FEET_TO_METER) << " Turnradius " << turn_radius_m << " Speed " << speed_mps << " Half turn Angle " << (leadInAngle) / 2); |
| 472 | if (lead_distance_ft > 1000) { |
| 473 | SG_LOG(SG_AI, SG_BULK, "Excessive leaddistance possible direction change " << lead_distance_ft << " leadInAngle " << leadInAngle << " inbound " << inbound << " outbound " << outbound << " at " << current->getName()); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | if ((lead_distance_ft > (3*turn_radius)) && (current->on_ground == false)) { |
| 479 | SG_LOG(SG_AI, SG_ALERT, "Warning: Lead-in distance is large. Inbound = " << inbound |
| 480 | << ". Outbound = " << outbound << ". Lead in angle = " << leadInAngle << ". Turn radius = " << turn_radius); |
| 481 | lead_distance_ft = 3 * turn_radius; |
| 482 | return; |
| 483 | } |
| 484 | if ((leadInAngle > 90) && (current->on_ground == true)) { |
| 485 | lead_distance_ft = turn_radius * tan((90 * SG_DEGREES_TO_RADIANS)/2); |
| 486 | return; |
| 487 | }*/ |
| 488 | } |
| 489 | |
| 490 | void FGAIFlightPlan::setLeadDistance(double distance_ft) |
| 491 | { |
| 492 | lead_distance_ft = distance_ft; |
| 493 | if (lead_distance_ft > 10000) { |
| 494 | SG_LOG(SG_AI, SG_BULK, "Excessive Leaddistance " << distance_ft); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | |
| 499 | double FGAIFlightPlan::getBearing(FGAIWaypoint* first, FGAIWaypoint* second) const |
| 500 | { |
| 501 | return SGGeodesy::courseDeg(first->getPos(), second->getPos()); |
| 502 | } |
| 503 | |
| 504 | double FGAIFlightPlan::getBearing(const SGGeod& aPos, FGAIWaypoint* wp) const |
| 505 | { |
| 506 | return SGGeodesy::courseDeg(aPos, wp->getPos()); |
| 507 | } |
| 508 | |
| 509 | void FGAIFlightPlan::deleteWaypoints() |
| 510 | { |
| 511 | for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end(); ++i) |
| 512 | delete (*i); |
| 513 | waypoints.clear(); |
| 514 | wpt_iterator = waypoints.begin(); |
| 515 | } |
| 516 | |
| 517 | // Delete all waypoints except the last, |
| 518 | // which we will recycle as the first waypoint in the next leg; |
| 519 | void FGAIFlightPlan::resetWaypoints() |
| 520 | { |
| 521 | if (waypoints.begin() == waypoints.end()) |
| 522 | return; |
| 523 | |
| 524 | |
| 525 | FGAIWaypoint* wpt = new FGAIWaypoint; |
| 526 | wpt_vector_iterator i = waypoints.end(); |
| 527 | --i; |
| 528 | wpt->setName((*i)->getName()); |
| 529 | wpt->setPos((*i)->getPos()); |
| 530 | wpt->setCrossat((*i)->getCrossat()); |
| 531 | wpt->setGear_down((*i)->getGear_down()); |
| 532 | wpt->setFlaps((*i)->getFlaps()); |
| 533 | wpt->setSpoilers((*i)->getSpoilers()); |
| 534 | wpt->setSpeedBrakes((*i)->getSpeedBrakes()); |
| 535 | wpt->setBeaconLight((*i)->getBeaconLight()); |
| 536 | wpt->setLandingLight((*i)->getLandingLight()); |
| 537 | wpt->setNavLight((*i)->getNavLight()); |
| 538 | wpt->setStrobeLight((*i)->getStrobeLight()); |
| 539 | wpt->setTaxiLight((*i)->getTaxiLight()); |
| 540 | wpt->setFinished(false); |
| 541 | wpt->setOn_ground((*i)->getOn_ground()); |
| 542 | SG_LOG(SG_AI, SG_DEBUG, "Recycling waypoint " << wpt->getName()); |
| 543 | deleteWaypoints(); |
| 544 | pushBackWaypoint(wpt); |
| 545 | } |
| 546 | |
| 547 | void FGAIFlightPlan::addWaypoint(FGAIWaypoint* wpt) |
| 548 | { |
| 549 | pushBackWaypoint(wpt); |
| 550 | } |
| 551 | |
| 552 | void FGAIFlightPlan::pushBackWaypoint(FGAIWaypoint* wpt) |
| 553 | { |
| 554 | if (!wpt) { |
| 555 | SG_LOG(SG_AI, SG_WARN, "Null WPT added"); |
| 556 | } |
| 557 | size_t pos = wpt_iterator - waypoints.begin(); |
| 558 | if (waypoints.size() > 0) { |
| 559 | double dist = SGGeodesy::distanceM(waypoints.back()->getPos(), wpt->getPos()); |
| 560 | if (dist == 0) { |
| 561 | SG_LOG(SG_AI, SG_DEBUG, "Double WP : \t" << wpt->getName() << " not added "); |
| 562 | } else { |
| 563 | waypoints.push_back(wpt); |
| 564 | SG_LOG(SG_AI, SG_BULK, "Added WP : \t" << std::setprecision(12) << wpt->getName() << "\t" << wpt->getPos() << "\t" << wpt->getSpeed()); |
| 565 | } |
| 566 | } else { |
| 567 | waypoints.push_back(wpt); |
| 568 | SG_LOG(SG_AI, SG_BULK, "Added WP : \t" << std::setprecision(12) << wpt->getName() << "\t" << wpt->getPos() << "\t" << wpt->getSpeed()); |
| 569 | } |
| 570 | // std::vector::push_back invalidates waypoints |
| 571 | // so we should restore wpt_iterator after push_back |
| 572 | // (or it could be an index in the vector) |
| 573 | wpt_iterator = waypoints.begin() + pos; |
| 574 | } |
| 575 | |
| 576 | // Start flightplan over from the beginning |
| 577 | void FGAIFlightPlan::restart() |
| 578 | { |
| 579 | wpt_iterator = waypoints.begin(); |
| 580 | } |
| 581 | |
| 582 | int FGAIFlightPlan::getRouteIndex(int i) const |
| 583 | { |
| 584 | if ((i > 0) && (i < (int)waypoints.size())) { |
| 585 | return waypoints[i]->getRouteIndex(); |
| 586 | } else |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | double FGAIFlightPlan::checkTrackLength(const string& wptName) const |
| 591 | { |
| 592 | // skip the first two waypoints: first one is behind, second one is partially done; |
| 593 | double trackDistance = 0; |
| 594 | wpt_vector_iterator wptvec = waypoints.begin(); |
| 595 | ++wptvec; |
| 596 | ++wptvec; |
| 597 | while ((wptvec != waypoints.end())) { |
| 598 | if (*wptvec != nullptr && (!((*wptvec)->contains(wptName)))) { |
| 599 | break; |
| 600 | } |
| 601 | trackDistance += (*wptvec)->getTrackLength(); |
| 602 | ++wptvec; |
| 603 | } |
| 604 | if (wptvec == waypoints.end()) { |
| 605 | trackDistance = 0; // name not found |
| 606 | } |
| 607 | return trackDistance; |
| 608 | } |
| 609 | |
| 610 | void FGAIFlightPlan::shortenToFirst(unsigned int number, const std::string& name) |
| 611 | { |
| 612 | while (waypoints.size() > number + 3) { |
| 613 | eraseLastWaypoint(); |
| 614 | } |
| 615 | (waypoints.back())->setName((waypoints.back())->getName() + name); |
| 616 | } |
| 617 | |
| 618 | void FGAIFlightPlan::setGate(const ParkingAssignment& pka) |
| 619 | { |
| 620 | gate = pka; |
| 621 | } |
| 622 | |
| 623 | FGParking* FGAIFlightPlan::getParkingGate() const |
| 624 | { |
| 625 | return gate.parking(); |
| 626 | } |
| 627 | |
| 628 | FGAirportRef FGAIFlightPlan::departureAirport() const |
| 629 | { |
| 630 | return departure; |
| 631 | } |
| 632 | |
| 633 | FGAirportRef FGAIFlightPlan::arrivalAirport() const |
| 634 | { |
| 635 | return arrival; |
| 636 | } |
| 637 | |
| 638 | FGAIFlightPlan* FGAIFlightPlan::createDummyUserPlan() |
| 639 | { |
| 640 | FGAIFlightPlan* fp = new FGAIFlightPlan; |
| 641 | fp->isValid = false; |
| 642 | return fp; |
| 643 | } |
| 644 | |
| 645 | bool FGAIFlightPlan::empty() const |
| 646 | { |
| 647 | return waypoints.empty(); |
| 648 | } |