1
0
Fork 0

[swift] Use AIManager for swift aircrafts

This commit is contained in:
Lars Toenning 2020-05-11 09:37:13 +02:00 committed by James Turner
parent f010e78e55
commit f823e033f1
8 changed files with 157 additions and 232 deletions

View file

@ -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;

View file

@ -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

View file

@ -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}")

View file

@ -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
)

View file

@ -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;
}

View file

@ -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

View file

@ -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();
}

View file

@ -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