From f823e033f1b5f20a983f28f387f6abbfa97f64a0 Mon Sep 17 00:00:00 2001 From: Lars Toenning <dev@ltoenning.de> Date: Mon, 11 May 2020 09:37:13 +0200 Subject: [PATCH] [swift] Use AIManager for swift aircrafts --- src/AIModel/AISwiftAircraft.cpp | 65 ++++++++++ src/AIModel/AISwiftAircraft.h | 46 +++++++ src/AIModel/CMakeLists.txt | 12 ++ src/Network/Swift/CMakeLists.txt | 2 - src/Network/Swift/SwiftAircraft.cpp | 132 --------------------- src/Network/Swift/SwiftAircraft.h | 61 ---------- src/Network/Swift/SwiftAircraftManager.cpp | 62 +++++----- src/Network/Swift/SwiftAircraftManager.h | 9 +- 8 files changed, 157 insertions(+), 232 deletions(-) create mode 100644 src/AIModel/AISwiftAircraft.cpp create mode 100644 src/AIModel/AISwiftAircraft.h delete mode 100644 src/Network/Swift/SwiftAircraft.cpp delete mode 100644 src/Network/Swift/SwiftAircraft.h diff --git a/src/AIModel/AISwiftAircraft.cpp b/src/AIModel/AISwiftAircraft.cpp new file mode 100644 index 000000000..082204251 --- /dev/null +++ b/src/AIModel/AISwiftAircraft.cpp @@ -0,0 +1,65 @@ +// AISwiftAircraft.cpp - Derived AIBase class for swift aircrafts +// +// Copyright (C) 2020 - swift Project Community / Contributors (http://swift-project.org/) +// Written by Lars Toenning <dev@ltoenning.de> started on April 2020. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#include "AISwiftAircraft.h" +#include <src/Main/globals.hxx> + + +FGAISwiftAircraft::FGAISwiftAircraft(const std::string& callsign, const std::string& modelString) : FGAIBase(otStatic, false) +{ + std::size_t pos = modelString.find("/Aircraft/"); // Only supporting AI models from FGDATA/AI/Aircraft for now + if(pos != std::string::npos) + model_path.append(modelString.substr(pos)); + else + model_path.append("INVALID_PATH"); + + setCallSign(callsign); + _searchOrder = PREFER_AI; +} +void FGAISwiftAircraft::updatePosition(SGGeod& position, SGVec3<double>& orientation, double groundspeed, bool initPos) +{ + m_initPos = initPos; + _setLatitude(position.getLatitudeDeg()); + _setLongitude(position.getLongitudeDeg()); + _setAltitude(position.getElevationFt()); + setPitch(orientation.x()); + setBank(orientation.y()); + setHeading(orientation.z()); + setSpeed(groundspeed); + +} + +void FGAISwiftAircraft::update(double dt) +{ + FGAIBase::update(dt); + Transform(); +} +double FGAISwiftAircraft::getGroundElevation(const SGGeod& pos) const +{ + if(!m_initPos) { return std::numeric_limits<double>::quiet_NaN(); } + double alt = 0; + SGGeod posReq; + posReq.setElevationFt(30000); + posReq.setLatitudeDeg(pos.getLatitudeDeg()); + posReq.setLongitudeDeg(pos.getLongitudeDeg()); + if(this->getGroundElevationM(posReq, alt, nullptr)) + return alt; + return std::numeric_limits<double>::quiet_NaN(); +} +FGAISwiftAircraft::~FGAISwiftAircraft() = default; diff --git a/src/AIModel/AISwiftAircraft.h b/src/AIModel/AISwiftAircraft.h new file mode 100644 index 000000000..eddeb60c5 --- /dev/null +++ b/src/AIModel/AISwiftAircraft.h @@ -0,0 +1,46 @@ +// AISwiftAircraft.h - Derived AIBase class for swift aircrafts +// +// Copyright (C) 2020 - swift Project Community / Contributors (http://swift-project.org/) +// Written by Lars Toenning <dev@ltoenning.de> started on April 2020. +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#ifndef FLIGHTGEAR_AISWIFTAIRCRAFT_H +#define FLIGHTGEAR_AISWIFTAIRCRAFT_H + + +#include "AIBase.hxx" +#include <string> + +using charPtr = const char*; + + +class FGAISwiftAircraft : public FGAIBase +{ +public: + FGAISwiftAircraft(const std::string& callsign, const std::string& modelString); + ~FGAISwiftAircraft() override; + void updatePosition(SGGeod& position, SGVec3<double>& orientation, double groundspeed, bool initPos); + void update(double dt) override; + double getGroundElevation(const SGGeod& pos) const; + + const char* getTypeString() const override { return "swift"; } + +private: + bool m_initPos = false; +}; + + +#endif //FLIGHTGEAR_AISWIFTAIRCRAFT_H diff --git a/src/AIModel/CMakeLists.txt b/src/AIModel/CMakeLists.txt index 08105fee0..e6c14499a 100644 --- a/src/AIModel/CMakeLists.txt +++ b/src/AIModel/CMakeLists.txt @@ -44,6 +44,18 @@ set(HEADERS performancedb.hxx submodel.hxx ) + +# Add sources if swift support is enabled +if(ENABLE_SWIFT) + set(HEADERS + ${HEADERS} + AISwiftAircraft.h + ) + set(SOURCES + ${SOURCES} + AISwiftAircraft.cpp + ) +endif() flightgear_component(AIModel "${SOURCES}" "${HEADERS}") \ No newline at end of file diff --git a/src/Network/Swift/CMakeLists.txt b/src/Network/Swift/CMakeLists.txt index d74afa8d4..f2a2a3b67 100644 --- a/src/Network/Swift/CMakeLists.txt +++ b/src/Network/Swift/CMakeLists.txt @@ -11,7 +11,6 @@ set(SOURCES plugin.cpp service.cpp traffic.cpp - SwiftAircraft.cpp SwiftAircraftManager.cpp ) @@ -27,7 +26,6 @@ set(HEADERS plugin.h service.h traffic.h - SwiftAircraft.h SwiftAircraftManager.h ) diff --git a/src/Network/Swift/SwiftAircraft.cpp b/src/Network/Swift/SwiftAircraft.cpp deleted file mode 100644 index 5c0e5f10d..000000000 --- a/src/Network/Swift/SwiftAircraft.cpp +++ /dev/null @@ -1,132 +0,0 @@ -// SwiftAircraft.cpp - Class representing an aircraft generated by swift -// -// Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/) -// Adapted to Flightgear by Lars Toenning <dev@ltoenning.de> -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -#include <string.h> - -#include <simgear/compiler.h> - -#include <string> - -#include <osg/Node> -#include <osg/ref_ptr> -#include <osgDB/FileUtils> - -#include <simgear/debug/logstream.hxx> -#include <simgear/misc/sg_path.hxx> -#include <simgear/props/props.hxx> -#include <simgear/scene/model/modellib.hxx> -#include <simgear/scene/util/SGNodeMasks.hxx> - -#include "SwiftAircraft.h" -#include <Main/fg_props.hxx> -#include <Main/globals.hxx> -#include <Scenery/scenery.hxx> -#include <Scripting/NasalModelData.hxx> -#include <Scripting/NasalSys.hxx> -#include <Sound/fg_fx.hxx> - -FGSwiftAircraft::FGSwiftAircraft(const std::string& callsign, const std::string& modelpath, SGPropertyNode_ptr p) -{ - using namespace simgear; - _model = SGModelLib::loadModel(modelpath); - _model->setName(callsign); - if (_model.valid()) { - aip.init(_model); - aip.setVisible(true); - aip.update(); - globals->get_scenery()->get_models_branch()->addChild(aip.getSceneGraph()); - - props = p; - props->setStringValue("callsign", callsign); - props->setBoolValue("valid",true); - } -} - -bool FGSwiftAircraft::updatePosition(SGGeod newPosition, SGVec3d orientation, double groundspeed, bool initPos) -{ - - position = newPosition; - aip.setPosition(position); - aip.setPitchDeg(orientation.x()); - aip.setRollDeg(orientation.y()); - aip.setHeadingDeg(orientation.z()); - aip.update(); - - this->initPos = initPos; - - //Update props - props->setDoubleValue("orientation/pitch-deg", orientation.x()); - props->setDoubleValue("orientation/roll-deg", orientation.y()); - props->setDoubleValue("orientation/true-heading-deg", orientation.z()); - SGVec3d cartPos = SGVec3d::fromGeod(position); - - props->setDoubleValue("position/global-x", cartPos.x()); - props->setDoubleValue("position/global-y", cartPos.y()); - props->setDoubleValue("position/global-z", cartPos.z()); - - - props->setDoubleValue("position/latitude-deg", position.getLatitudeDeg()); - props->setDoubleValue("position/longitude-deg", position.getLongitudeDeg()); - props->setDoubleValue("position/altitude-ft", position.getElevationFt()); - - props->setDoubleValue("velocities/true-airspeed-kt", groundspeed); - - return true; -} - - -FGSwiftAircraft::~FGSwiftAircraft() -{ - props->setBoolValue("valid",false); - props->setIntValue("id",-1); - aip.setVisible(false); -} - -double FGSwiftAircraft::getLatDeg() const -{ - return position.getLatitudeDeg(); -} - -double FGSwiftAircraft::getLongDeg() const -{ - return position.getLongitudeDeg(); -} - -double FGSwiftAircraft::getFudgeFactor() const -{ - return 0; -} - -inline bool FGSwiftAircraft::operator<(const std::string& extCallsign) -{ - return _model->getName().compare(extCallsign); -} - -double FGSwiftAircraft::getGroundElevation(const SGGeod& pos) const -{ - if(!initPos) { return std::numeric_limits<double>::quiet_NaN(); } - double alt = 0; - SGGeod posReq; - posReq.setElevationFt(30000); - posReq.setLatitudeDeg(pos.getLatitudeDeg()); - posReq.setLongitudeDeg(pos.getLongitudeDeg()); - globals->get_scenery()->get_elevation_m(posReq,alt,0,_model.get()); - return alt; - -} diff --git a/src/Network/Swift/SwiftAircraft.h b/src/Network/Swift/SwiftAircraft.h deleted file mode 100644 index 2bad224d5..000000000 --- a/src/Network/Swift/SwiftAircraft.h +++ /dev/null @@ -1,61 +0,0 @@ -// SwiftAircraft.h - Class representing an aircraft generated by swift -// -// Copyright (C) 2019 - swift Project Community / Contributors (http://swift-project.org/) -// Adapted to Flightgear by Lars Toenning <dev@ltoenning.de> -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -#ifndef FGSWIFTAIRCRAFT_H -#define FGSWIFTAIRCRAFT_H -#pragma once -#include <osg/ref_ptr> -#include <string> - -#include <simgear/constants.h> -#include <simgear/misc/sg_path.hxx> -#include <simgear/props/tiedpropertylist.hxx> -#include <simgear/scene/model/placement.hxx> -#include <simgear/sg_inlines.h> -#include <simgear/structure/SGReferenced.hxx> -#include <simgear/structure/SGSharedPtr.hxx> -#include <simgear/math/sg_geodesy.hxx> - - -namespace osg { -class PagedLOD; -} - - -class FGSwiftAircraft -{ -public: - FGSwiftAircraft(const std::string& callsign, const std::string& modelpath, SGPropertyNode_ptr p); - bool updatePosition(SGGeod newPosition, SGVec3d orientation, double groundspeed, bool initPos); - ~FGSwiftAircraft(); - std::string getName() { return _model->getName(); }; - double getLatDeg() const; - double getLongDeg() const; - double getGroundElevation(const SGGeod& pos) const; - double getFudgeFactor() const; - -private: - bool initPos = false; - SGGeod position; - SGPropertyNode_ptr props; - osg::ref_ptr<osg::Node> _model; - SGModelPlacement aip; - inline bool operator<(const std::string& extCallsign); -}; -#endif \ No newline at end of file diff --git a/src/Network/Swift/SwiftAircraftManager.cpp b/src/Network/Swift/SwiftAircraftManager.cpp index 8b2e6adfb..8c7bf8596 100644 --- a/src/Network/Swift/SwiftAircraftManager.cpp +++ b/src/Network/Swift/SwiftAircraftManager.cpp @@ -18,47 +18,37 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "SwiftAircraftManager.h" -#include "SwiftAircraft.h" #include <Main/globals.hxx> #include <utility> FGSwiftAircraftManager::FGSwiftAircraftManager() -= default; +{ +} FGSwiftAircraftManager::~FGSwiftAircraftManager() = default; bool FGSwiftAircraftManager::addPlane(const std::string& callsign, std::string modelString) { - if (aircraftByCallsign.find(callsign) != aircraftByCallsign.end()) - return false; + this->removePlane(callsign); // Remove plane if already exists e.g. when rematching is done. + auto curAircraft = new FGAISwiftAircraft(callsign, modelString); + globals->get_subsystem<FGAIManager>()->attach(curAircraft); - const char* typeString = "swift"; - SGPropertyNode_ptr root = globals->get_props()->getNode("ai/models",true); - SGPropertyNode_ptr p; - int i; - for(i = 0; i < 10000; i++){ - p = root->getNode(typeString,i,false); - - if(!p || !p->getBoolValue("valid",false)) - break; - } - p = root->getNode(typeString,i,true); - p->setIntValue("id",i); - auto curAircraft = new FGSwiftAircraft(callsign, std::move(modelString), p); - aircraftByCallsign.insert(std::pair<std::string, FGSwiftAircraft*>(callsign, curAircraft)); + aircraftByCallsign.insert(std::pair<std::string, FGAISwiftAircraft*>(callsign, curAircraft)); return true; } void FGSwiftAircraftManager::updatePlanes(std::vector<std::string> callsigns, std::vector<SGGeod> positions, std::vector<SGVec3d> orientations, std::vector<double> groundspeeds, std::vector<bool> onGrounds) { - for (long unsigned int i = 0; i < callsigns.size(); i++) { - auto it = aircraftByCallsign.find(callsigns.at(i)); - if (it != aircraftByCallsign.end()) { - it->second->updatePosition(positions.at(i), orientations.at(i), groundspeeds.at(i),true); - } - } + for (long unsigned int i = 0; i < callsigns.size(); i++) + { + auto it = aircraftByCallsign.find(callsigns.at(i)); + if(it != aircraftByCallsign.end()) + { + it->second->updatePosition(positions.at(i), orientations.at(i), groundspeeds.at(i),true); + } + } } 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 @@ -76,19 +66,17 @@ void FGSwiftAircraftManager::getRemoteAircraftData(std::vector<std::string>& cal const auto it = aircraftByCallsign.find(requestedCallsign); if(it == aircraftByCallsign.end()) { continue; } - const FGSwiftAircraft *aircraft = it->second; + const FGAISwiftAircraft* aircraft = it->second; assert(aircraft); SGGeod pos; - pos.setLatitudeDeg(aircraft->getLatDeg()); - pos.setLongitudeDeg(aircraft->getLongDeg()); + pos.setLatitudeDeg(aircraft->_getLatitude()); + pos.setLongitudeDeg(aircraft->_getLongitude()); const double latDeg = pos.getLatitudeDeg(); const double lonDeg = pos.getLongitudeDeg(); double groundElevation = aircraft->getGroundElevation(pos); - double fudgeFactor = aircraft->getFudgeFactor(); - (void)fudgeFactor; callsigns.push_back(requestedCallsign); latitudesDeg.push_back(latDeg); longitudesDeg.push_back(lonDeg); @@ -100,25 +88,29 @@ void FGSwiftAircraftManager::getRemoteAircraftData(std::vector<std::string>& cal void FGSwiftAircraftManager::removePlane(const std::string& callsign) { auto it = aircraftByCallsign.find(callsign); - if (it != aircraftByCallsign.end()) { - delete it->second; + if(it != aircraftByCallsign.end()) + { + it->second->setDie(true); aircraftByCallsign.erase(it); } } void FGSwiftAircraftManager::removeAllPlanes() { - for (auto it = aircraftByCallsign.begin(); it != aircraftByCallsign.end(); ++it) { - delete it->second; - aircraftByCallsign.erase(it); + for(auto it = aircraftByCallsign.begin(); it!= aircraftByCallsign.end();) + { + it->second->setDie(true); + it = aircraftByCallsign.erase(it); } } double FGSwiftAircraftManager::getElevationAtPosition(const std::string &callsign, const SGGeod& pos) const { auto it = aircraftByCallsign.find(callsign); - if(it != aircraftByCallsign.end()){ + if(it != aircraftByCallsign.end()) + { return it->second->getGroundElevation(pos); } + // Aircraft not found in list return std::numeric_limits<double>::quiet_NaN(); } diff --git a/src/Network/Swift/SwiftAircraftManager.h b/src/Network/Swift/SwiftAircraftManager.h index 0c1e731c2..06d88e266 100644 --- a/src/Network/Swift/SwiftAircraftManager.h +++ b/src/Network/Swift/SwiftAircraftManager.h @@ -17,9 +17,12 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -#include "SwiftAircraft.h" #include <Scenery/scenery.hxx> +#include <AIModel/AISwiftAircraft.h> +#include <AIModel/AIManager.hxx> + #include <vector> +#include <unordered_map> #ifndef FGSWIFTAIRCRAFTMANAGER_H #define FGSWIFTAIRCRAFTMANAGER_H @@ -29,7 +32,6 @@ class FGSwiftAircraftManager public: FGSwiftAircraftManager(); ~FGSwiftAircraftManager(); - std::map<std::string, FGSwiftAircraft*> aircraftByCallsign; bool addPlane(const std::string& callsign, std::string modelString); void updatePlanes(std::vector<std::string> callsigns, std::vector<SGGeod> positions, std::vector<SGVec3d> orientations, std::vector<double> groundspeeds, std::vector<bool> onGrounds); void getRemoteAircraftData(std::vector<std::string>& callsigns, std::vector<double>& latitudesDeg, std::vector<double>& longitudesDeg, @@ -37,5 +39,8 @@ public: void removePlane(const std::string& callsign); void removeAllPlanes(); double getElevationAtPosition(const std::string &callsign, const SGGeod& pos) const; + +private: + std::unordered_map<std::string, FGAISwiftAircraft*> aircraftByCallsign; }; #endif \ No newline at end of file