| 1 | /* |
| 2 | * Manger class for aircraft generated by swift |
| 3 | * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/) |
| 4 | * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <[email protected]> |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "SwiftAircraftManager.h" |
| 9 | #include <Main/globals.hxx> |
| 10 | #include <utility> |
| 11 | |
| 12 | namespace flightgear::swift { |
| 13 | |
| 14 | FGSwiftAircraftManager::FGSwiftAircraftManager() |
| 15 | { |
| 16 | m_initialized = true; |
| 17 | } |
| 18 | |
| 19 | FGSwiftAircraftManager::~FGSwiftAircraftManager() |
| 20 | { |
| 21 | this->removeAllPlanes(); |
| 22 | m_initialized = false; |
| 23 | } |
| 24 | |
| 25 | bool FGSwiftAircraftManager::isInitialized() const |
| 26 | { |
| 27 | return m_initialized; |
| 28 | } |
| 29 | |
| 30 | bool FGSwiftAircraftManager::addPlane(const std::string& callsign, const std::string& modelString) |
| 31 | { |
| 32 | this->removePlane(callsign); // Remove plane if already exists e.g. when rematching is done. |
| 33 | auto curAircraft = FGAISwiftAircraftPtr(new FGAISwiftAircraft(callsign, modelString)); |
| 34 | globals->get_subsystem<FGAIManager>()->attach(curAircraft); |
| 35 | // Init props after prop-root is assigned |
| 36 | curAircraft->initProps(); |
| 37 | |
| 38 | aircraftByCallsign.insert(std::pair<std::string, FGAISwiftAircraftPtr>(callsign, curAircraft)); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | void FGSwiftAircraftManager::updatePlanes(const std::vector<SwiftPlaneUpdate>& updates) |
| 43 | { |
| 44 | for (auto& update : updates) { |
| 45 | auto it = aircraftByCallsign.find(update.callsign); |
| 46 | if (it != aircraftByCallsign.end()) { |
| 47 | it->second->updatePosition(update.position, update.orientation, update.groundspeed, true); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void FGSwiftAircraftManager::getRemoteAircraftData(std::vector<std::string>& callsigns, std::vector<double>& latitudesDeg, std::vector<double>& longitudesDeg, std::vector<double>& elevationsM, std::vector<double>& verticalOffsets) const |
| 53 | { |
| 54 | if (callsigns.empty() || aircraftByCallsign.empty()) { return; } |
| 55 | |
| 56 | const auto requestedCallsigns = callsigns; |
| 57 | callsigns.clear(); |
| 58 | latitudesDeg.clear(); |
| 59 | longitudesDeg.clear(); |
| 60 | elevationsM.clear(); |
| 61 | verticalOffsets.clear(); |
| 62 | |
| 63 | for (const auto& requestedCallsign : requestedCallsigns) { |
| 64 | const auto it = aircraftByCallsign.find(requestedCallsign); |
| 65 | if (it == aircraftByCallsign.end()) { continue; } |
| 66 | |
| 67 | const FGAISwiftAircraft* aircraft = it->second; |
| 68 | assert(aircraft); |
| 69 | |
| 70 | |
| 71 | SGGeod pos; |
| 72 | pos.setLatitudeDeg(aircraft->_getLatitude()); |
| 73 | pos.setLongitudeDeg(aircraft->_getLongitude()); |
| 74 | const double latDeg = pos.getLatitudeDeg(); |
| 75 | const double lonDeg = pos.getLongitudeDeg(); |
| 76 | double groundElevation = aircraft->getGroundElevation(pos); |
| 77 | |
| 78 | callsigns.push_back(requestedCallsign); |
| 79 | latitudesDeg.push_back(latDeg); |
| 80 | longitudesDeg.push_back(lonDeg); |
| 81 | elevationsM.push_back(groundElevation); |
| 82 | verticalOffsets.push_back(0); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void FGSwiftAircraftManager::removePlane(const std::string& callsign) |
| 87 | { |
| 88 | auto it = aircraftByCallsign.find(callsign); |
| 89 | if (it != aircraftByCallsign.end()) { |
| 90 | it->second->setDie(true); |
| 91 | aircraftByCallsign.erase(it); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void FGSwiftAircraftManager::removeAllPlanes() |
| 96 | { |
| 97 | for (const auto &[_, plane] : aircraftByCallsign) { |
| 98 | plane->setDie(true); |
| 99 | } |
| 100 | aircraftByCallsign.clear(); |
| 101 | } |
| 102 | |
| 103 | double FGSwiftAircraftManager::getElevationAtPosition(const std::string& callsign, const SGGeod& pos) const |
| 104 | { |
| 105 | auto it = aircraftByCallsign.find(callsign); |
| 106 | if (it != aircraftByCallsign.end()) { |
| 107 | return it->second->getGroundElevation(pos); |
| 108 | } |
| 109 | // Aircraft not found in list |
| 110 | return std::numeric_limits<double>::quiet_NaN(); |
| 111 | } |
| 112 | |
| 113 | void FGSwiftAircraftManager::setPlanesTransponders(const std::vector<AircraftTransponder>& transponders) |
| 114 | { |
| 115 | for (const auto& transponder : transponders) { |
| 116 | auto it = aircraftByCallsign.find(transponder.callsign); |
| 117 | if (it != aircraftByCallsign.end()) { |
| 118 | it->second->setPlaneTransponder(transponder); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void FGSwiftAircraftManager::setPlanesSurfaces(const std::vector<AircraftSurfaces>& surfaces) |
| 124 | { |
| 125 | for (const auto& surface : surfaces) { |
| 126 | auto it = aircraftByCallsign.find(surface.callsign); |
| 127 | if (it != aircraftByCallsign.end()) { |
| 128 | it->second->setPlaneSurface(surface); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } // namespace flightgear::swift |