| 1 | /* |
| 2 | * SPDX-FileName: sidstar.cxx |
| 3 | * SPDX-FileComment: Code to manage departure / arrival procedures |
| 4 | * SPDX-FileCopyrightText: Written by Durk Talsma, started March 2009 |
| 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 <iostream> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #include <simgear/props/props.hxx> |
| 16 | #include <simgear/props/props_io.hxx> |
| 17 | |
| 18 | #include <AIModel/AIFlightPlan.hxx> |
| 19 | #include <Airports/airport.hxx> |
| 20 | |
| 21 | #include "sidstar.hxx" |
| 22 | |
| 23 | |
| 24 | FGSidStar::FGSidStar(FGAirport* ap) : id{ap->getId()} |
| 25 | { |
| 26 | initialized = false; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | FGSidStar::FGSidStar(const FGSidStar& other) |
| 31 | { |
| 32 | std::cerr << "TODO" << std::endl; |
| 33 | } |
| 34 | |
| 35 | void FGSidStar::load(SGPath filename) |
| 36 | { |
| 37 | SGPropertyNode root; |
| 38 | |
| 39 | try { |
| 40 | readProperties(filename, &root); |
| 41 | } catch (const sg_exception&) { |
| 42 | SG_LOG(SG_GENERAL, SG_ALERT, "Error reading AI flight plan: " << filename); |
| 43 | // cout << path.str() << std::endl; |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | SGPropertyNode* node = root.getNode("SIDS"); |
| 48 | FGAIFlightPlan* fp; |
| 49 | |
| 50 | for (int i = 0; i < node->nChildren(); i++) { |
| 51 | fp = new FGAIFlightPlan; |
| 52 | SGPropertyNode* fpl_node = node->getChild(i); |
| 53 | std::string name = fpl_node->getStringValue("name", "END"); |
| 54 | std::string runway = fpl_node->getStringValue("runway", "27"); |
| 55 | //std::cerr << "Runway = " << runway << std::endl; |
| 56 | |
| 57 | fp->setName(name); |
| 58 | SGPropertyNode* wpts_node = fpl_node->getNode("wpts"); |
| 59 | |
| 60 | for (int j = 0; j < wpts_node->nChildren(); j++) { |
| 61 | FGAIWaypoint* wpt = new FGAIWaypoint; |
| 62 | SGPropertyNode* wpt_node = wpts_node->getChild(j); |
| 63 | //std::cerr << "Reading waypoint " << j << wpt_node->getStringValue("name", "END") << std::endl; |
| 64 | |
| 65 | wpt->setName(wpt_node->getStringValue("name", "END")); |
| 66 | wpt->setLatitude(wpt_node->getDoubleValue("lat", 0)); |
| 67 | wpt->setLongitude(wpt_node->getDoubleValue("lon", 0)); |
| 68 | wpt->setAltitude(wpt_node->getDoubleValue("alt", 0)); |
| 69 | wpt->setSpeed(wpt_node->getDoubleValue("ktas", 0)); |
| 70 | wpt->setCrossat(wpt_node->getDoubleValue("crossat", -10000)); |
| 71 | wpt->setGear_down(wpt_node->getBoolValue("gear-down", false)); |
| 72 | wpt->setFlaps(wpt_node->getBoolValue("flaps-down", false) ? 0.5 : 0.0); // We'll assume all SIDS only require half-flaps |
| 73 | wpt->setOn_ground(wpt_node->getBoolValue("on-ground", false)); |
| 74 | wpt->setTime_sec(wpt_node->getDoubleValue("time-sec", 0)); |
| 75 | wpt->setTime(wpt_node->getStringValue("time", "")); |
| 76 | |
| 77 | if (wpt->contains("END")) |
| 78 | wpt->setFinished(true); |
| 79 | else |
| 80 | wpt->setFinished(false); |
| 81 | |
| 82 | // |
| 83 | fp->addWaypoint(wpt); |
| 84 | } |
| 85 | data[runway].push_back(fp); |
| 86 | //std::cerr << "Runway = " << runway << std::endl; |
| 87 | } |
| 88 | |
| 89 | //wpt_iterator = waypoints.begin(); |
| 90 | //cout << waypoints.size() << " waypoints read." << std::endl; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | FGAIFlightPlan* FGSidStar::getBest(const std::string& activeRunway, double heading) |
| 95 | { |
| 96 | //std::cerr << "Getting best procedure for " << activeRunway << std::endl; |
| 97 | for (FlightPlanVecIterator i = data[activeRunway].begin(); i != data[activeRunway].end(); i++) { |
| 98 | //std::cerr << (*i)->getName() << std::endl; |
| 99 | } |
| 100 | int size = data[activeRunway].size(); |
| 101 | //std::cerr << "size is " << size << std::endl; |
| 102 | if (size) { |
| 103 | return data[activeRunway][(rand() % size)]; |
| 104 | } else { |
| 105 | return 0; |
| 106 | } |
| 107 | } |