1
0
Fork 0

Property to toggle between preferred MP models

/sim/multiplay/use-detailed-models can be used to set whether
we will prefer models from Aircraft/ (true) or from AI/ (false).

Default (set in defaults.xml in fgdata) is true.
This commit is contained in:
Stuart Buchanan 2018-05-28 20:54:09 +01:00
parent ea9ae959e4
commit 9de3672b62
2 changed files with 52 additions and 47 deletions

View file

@ -2,7 +2,7 @@
// - a global management type for AI objects // - a global management type for AI objects
// //
// Written by David Culp, started October 2003. // Written by David Culp, started October 2003.
// - davidculp2@comcast.net // - davidculp2@comcast.net
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as // modify it under the terms of the GNU General Public License as
@ -59,12 +59,12 @@ public:
_objects.push_back(ai); _objects.push_back(ai);
} }
} // of scenario entry iteration } // of scenario entry iteration
SGPropertyNode* nasalScripts = scenarios->getChild("nasal"); SGPropertyNode* nasalScripts = scenarios->getChild("nasal");
if (!nasalScripts) { if (!nasalScripts) {
return; return;
} }
_unloadScript = nasalScripts->getStringValue("unload"); _unloadScript = nasalScripts->getStringValue("unload");
std::string loadScript = nasalScripts->getStringValue("load"); std::string loadScript = nasalScripts->getStringValue("load");
if (!loadScript.empty()) { if (!loadScript.empty()) {
@ -75,24 +75,24 @@ public:
0); 0);
} }
} }
~Scenario() ~Scenario()
{ {
std::for_each(_objects.begin(), _objects.end(), std::for_each(_objects.begin(), _objects.end(),
[](FGAIBasePtr ai) { ai->setDie(true); }); [](FGAIBasePtr ai) { ai->setDie(true); });
FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal"); FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
if (!nasalSys) if (!nasalSys)
return; return;
std::string moduleName = "scenario_" + _internalName; std::string moduleName = "scenario_" + _internalName;
if (!_unloadScript.empty()) { if (!_unloadScript.empty()) {
nasalSys->createModule(moduleName.c_str(), moduleName.c_str(), nasalSys->createModule(moduleName.c_str(), moduleName.c_str(),
_unloadScript.c_str(), _unloadScript.size(), _unloadScript.c_str(), _unloadScript.size(),
0); 0);
} }
nasalSys->deleteModule(moduleName.c_str()); nasalSys->deleteModule(moduleName.c_str());
} }
private: private:
@ -129,11 +129,13 @@ FGAIManager::init() {
user_altitude_agl_node = fgGetNode("/position/altitude-agl-ft", true); user_altitude_agl_node = fgGetNode("/position/altitude-agl-ft", true);
user_speed_node = fgGetNode("/velocities/uBody-fps", true); user_speed_node = fgGetNode("/velocities/uBody-fps", true);
globals->get_commands()->addCommand("load-scenario", this, &FGAIManager::loadScenarioCommand); globals->get_commands()->addCommand("load-scenario", this, &FGAIManager::loadScenarioCommand);
globals->get_commands()->addCommand("unload-scenario", this, &FGAIManager::unloadScenarioCommand); globals->get_commands()->addCommand("unload-scenario", this, &FGAIManager::unloadScenarioCommand);
_environmentVisiblity = fgGetNode("/environment/visibility-m"); _environmentVisiblity = fgGetNode("/environment/visibility-m");
_mp_use_detailed_models = fgGetNode("/sim/multiplay/use-detailed-models", true);
// Create an (invisible) AIAircraft representation of the current // Create an (invisible) AIAircraft representation of the current
// users's aircraft, that mimicks the user aircraft's behavior. // users's aircraft, that mimicks the user aircraft's behavior.
@ -175,10 +177,10 @@ FGAIManager::reinit()
{ {
// shutdown scenarios // shutdown scenarios
unloadAllScenarios(); unloadAllScenarios();
update(0.0); update(0.0);
std::for_each(ai_list.begin(), ai_list.end(), std::mem_fn(&FGAIBase::reinit)); std::for_each(ai_list.begin(), ai_list.end(), std::mem_fn(&FGAIBase::reinit));
// (re-)load scenarios // (re-)load scenarios
postinit(); postinit();
} }
@ -187,18 +189,18 @@ void
FGAIManager::shutdown() FGAIManager::shutdown()
{ {
unloadAllScenarios(); unloadAllScenarios();
for (FGAIBase* ai : ai_list) { for (FGAIBase* ai : ai_list) {
// other subsystems, especially ATC, may have references. This // other subsystems, especially ATC, may have references. This
// lets them detect if the AI object should be skipped // lets them detect if the AI object should be skipped
ai->setDie(true); ai->setDie(true);
ai->unbind(); ai->unbind();
} }
ai_list.clear(); ai_list.clear();
_environmentVisiblity.clear(); _environmentVisiblity.clear();
_userAircraft.clear(); _userAircraft.clear();
globals->get_commands()->removeCommand("load-scenario"); globals->get_commands()->removeCommand("load-scenario");
globals->get_commands()->removeCommand("unload-scenario"); globals->get_commands()->removeCommand("unload-scenario");
} }
@ -218,10 +220,10 @@ FGAIManager::unbind() {
void FGAIManager::removeDeadItem(FGAIBase* base) void FGAIManager::removeDeadItem(FGAIBase* base)
{ {
SGPropertyNode *props = base->_getProps(); SGPropertyNode *props = base->_getProps();
props->setBoolValue("valid", false); props->setBoolValue("valid", false);
base->unbind(); base->unbind();
// for backward compatibility reset properties, so that aircraft, // for backward compatibility reset properties, so that aircraft,
// which don't know the <valid> property, keep working // which don't know the <valid> property, keep working
// TODO: remove after a while // TODO: remove after a while
@ -245,14 +247,14 @@ FGAIManager::update(double dt)
// partition the list into dead followed by alive // partition the list into dead followed by alive
auto firstAlive = auto firstAlive =
std::stable_partition(ai_list.begin(), ai_list.end(), std::mem_fn(&FGAIBase::getDie)); std::stable_partition(ai_list.begin(), ai_list.end(), std::mem_fn(&FGAIBase::getDie));
// clean up each item and finally remove from the container // clean up each item and finally remove from the container
for (auto it=ai_list.begin(); it != firstAlive; ++it) { for (auto it=ai_list.begin(); it != firstAlive; ++it) {
removeDeadItem(*it); removeDeadItem(*it);
} }
ai_list.erase(ai_list.begin(), firstAlive); ai_list.erase(ai_list.begin(), firstAlive);
// every remaining item is alive. update them in turn, but guard for // every remaining item is alive. update them in turn, but guard for
// exceptions, so a single misbehaving AI object doesn't bring down the // exceptions, so a single misbehaving AI object doesn't bring down the
// entire subsystem. // entire subsystem.
@ -311,12 +313,14 @@ FGAIManager::attach(FGAIBase *model)
case FGAIBase::otAircraft: case FGAIBase::otAircraft:
case FGAIBase::otStatic: case FGAIBase::otStatic:
modelPolicy = FGAIBase::PREFER_AI; modelPolicy = FGAIBase::PREFER_AI;
break;
case FGAIBase::otMultiplayer: case FGAIBase::otMultiplayer:
modelPolicy = FGAIBase::PREFER_DATA; modelPolicy = this->_mp_use_detailed_models->getBoolValue() ? FGAIBase::PREFER_DATA : FGAIBase::PREFER_AI;
break;
default: default:
break; break;
} }
model->init(modelPolicy); model->init(modelPolicy);
model->bind(); model->bind();
p->setBoolValue("valid", true); p->setBoolValue("valid", true);
@ -343,7 +347,7 @@ FGAIManager::fetchUserState( double dt )
wind_from_east = wind_from_east_node->getDoubleValue(); wind_from_east = wind_from_east_node->getDoubleValue();
wind_from_north = wind_from_north_node->getDoubleValue(); wind_from_north = wind_from_north_node->getDoubleValue();
user_altitude_agl = user_altitude_agl_node->getDoubleValue(); user_altitude_agl = user_altitude_agl_node->getDoubleValue();
_userAircraft->setGeodPos(globals->get_aircraft_position()); _userAircraft->setGeodPos(globals->get_aircraft_position());
_userAircraft->setHeading(user_heading); _userAircraft->setHeading(user_heading);
_userAircraft->setSpeed(fgGetDouble("/velocities/groundspeed-kt")); _userAircraft->setSpeed(fgGetDouble("/velocities/groundspeed-kt"));
@ -374,22 +378,22 @@ bool FGAIManager::loadScenarioCommand(const SGPropertyNode* args, SGPropertyNode
return unloadScenario(name); return unloadScenario(name);
} }
} }
if (_scenarios.find(name) != _scenarios.end()) { if (_scenarios.find(name) != _scenarios.end()) {
SG_LOG(SG_AI, SG_WARN, "scenario '" << name << "' already loaded"); SG_LOG(SG_AI, SG_WARN, "scenario '" << name << "' already loaded");
return false; return false;
} }
bool ok = loadScenario(name); bool ok = loadScenario(name);
if (ok) { if (ok) {
// create /sim/ai node for consistency // create /sim/ai node for consistency
int index = 0; int index = 0;
for (; root->hasChild("scenario", index); ++index) {} for (; root->hasChild("scenario", index); ++index) {}
SGPropertyNode* scenarioNode = root->getChild("scenario", index, true); SGPropertyNode* scenarioNode = root->getChild("scenario", index, true);
scenarioNode->setStringValue(name); scenarioNode->setStringValue(name);
} }
return ok; return ok;
} }
@ -408,10 +412,10 @@ bool FGAIManager::addObjectCommand(const SGPropertyNode* definition)
FGAIBasePtr FGAIManager::addObject(const SGPropertyNode* definition) FGAIBasePtr FGAIManager::addObject(const SGPropertyNode* definition)
{ {
const std::string& type = definition->getStringValue("type", "aircraft"); const std::string& type = definition->getStringValue("type", "aircraft");
FGAIBase* ai = NULL; FGAIBase* ai = NULL;
if (type == "tanker") { // refueling scenarios if (type == "tanker") { // refueling scenarios
ai = new FGAITanker; ai = new FGAITanker;
} else if (type == "wingman") { } else if (type == "wingman") {
ai = new FGAIWingman; ai = new FGAIWingman;
} else if (type == "aircraft") { } else if (type == "aircraft") {
@ -455,7 +459,7 @@ bool FGAIManager::removeObject(const SGPropertyNode* args)
break; break;
} }
} }
return false; return false;
} }
@ -476,12 +480,12 @@ FGAIManager::loadScenario( const string &filename )
if (!file) { if (!file) {
return false; return false;
} }
SGPropertyNode_ptr scNode = file->getChild("scenario"); SGPropertyNode_ptr scNode = file->getChild("scenario");
if (!scNode) { if (!scNode) {
return false; return false;
} }
_scenarios[filename] = new Scenario(this, filename, scNode); _scenarios[filename] = new Scenario(this, filename, scNode);
return true; return true;
} }
@ -495,7 +499,7 @@ FGAIManager::unloadScenario( const string &filename)
SG_LOG(SG_AI, SG_WARN, "unload scenario: not found:" << filename); SG_LOG(SG_AI, SG_WARN, "unload scenario: not found:" << filename);
return false; return false;
} }
// remove /sim/ai node // remove /sim/ai node
unsigned int index = 0; unsigned int index = 0;
for (SGPropertyNode* n = NULL; (n = root->getChild("scenario", index)) != NULL; ++index) { for (SGPropertyNode* n = NULL; (n = root->getChild("scenario", index)) != NULL; ++index) {
@ -504,7 +508,7 @@ FGAIManager::unloadScenario( const string &filename)
break; break;
} }
} }
delete it->second; delete it->second;
_scenarios.erase(it); _scenarios.erase(it);
return true; return true;
@ -517,8 +521,8 @@ FGAIManager::unloadAllScenarios()
for (; it != _scenarios.end(); ++it) { for (; it != _scenarios.end(); ++it) {
delete it->second; delete it->second;
} // of scenarios iteration } // of scenarios iteration
// remove /sim/ai node // remove /sim/ai node
root->removeChildren("scenario"); root->removeChildren("scenario");
_scenarios.clear(); _scenarios.clear();
@ -552,7 +556,7 @@ FGAIManager::calcCollision(double alt, double lat, double lon, double fuse_range
SGGeod pos(SGGeod::fromDegFt(lon, lat, alt)); SGGeod pos(SGGeod::fromDegFt(lon, lat, alt));
SGVec3d cartPos(SGVec3d::fromGeod(pos)); SGVec3d cartPos(SGVec3d::fromGeod(pos));
while (ai_list_itr != end) { while (ai_list_itr != end) {
double tgt_alt = (*ai_list_itr)->_getAltitude(); double tgt_alt = (*ai_list_itr)->_getAltitude();
int type = (*ai_list_itr)->getType(); int type = (*ai_list_itr)->getType();

View file

@ -1,5 +1,5 @@
// AIManager.hxx - David Culp - based on: // AIManager.hxx - David Culp - based on:
// AIMgr.hxx - definition of FGAIMgr // AIMgr.hxx - definition of FGAIMgr
// - a global management class for FlightGear generated AI traffic // - a global management class for FlightGear generated AI traffic
// //
// Written by David Luff, started March 2002. // Written by David Luff, started March 2002.
@ -64,12 +64,12 @@ public:
inline double get_user_agl() const { return user_altitude_agl; } inline double get_user_agl() const { return user_altitude_agl; }
bool loadScenario( const std::string &filename ); bool loadScenario( const std::string &filename );
static SGPropertyNode_ptr loadScenarioFile(const std::string& filename); static SGPropertyNode_ptr loadScenarioFile(const std::string& filename);
FGAIBasePtr addObject(const SGPropertyNode* definition); FGAIBasePtr addObject(const SGPropertyNode* definition);
bool isVisible(const SGGeod& pos) const; bool isVisible(const SGGeod& pos) const;
/** /**
* @brief given a reference to an /ai/models/<foo>[n] node, return the * @brief given a reference to an /ai/models/<foo>[n] node, return the
* corresponding AIObject implementation, or NULL. * corresponding AIObject implementation, or NULL.
@ -84,7 +84,7 @@ public:
double calcRangeFt(const SGVec3d& aCartPos, const FGAIBase* aObject) const; double calcRangeFt(const SGVec3d& aCartPos, const FGAIBase* aObject) const;
static const char* subsystemName() { return "ai-model"; } static const char* subsystemName() { return "ai-model"; }
/** /**
* @brief Retrieve the representation of the user's aircraft in the AI manager * @brief Retrieve the representation of the user's aircraft in the AI manager
* the position and velocity of this object are slaved to the user's aircraft, * the position and velocity of this object are slaved to the user's aircraft,
@ -95,23 +95,23 @@ public:
private: private:
// FGSubmodelMgr is a friend for access to the AI_list // FGSubmodelMgr is a friend for access to the AI_list
friend class FGSubmodelMgr; friend class FGSubmodelMgr;
// A list of pointers to AI objects // A list of pointers to AI objects
typedef ai_list_type::iterator ai_list_iterator; typedef ai_list_type::iterator ai_list_iterator;
typedef ai_list_type::const_iterator ai_list_const_iterator; typedef ai_list_type::const_iterator ai_list_const_iterator;
int getNumAiObjects() const; int getNumAiObjects() const;
void removeDeadItem(FGAIBase* base); void removeDeadItem(FGAIBase* base);
bool loadScenarioCommand(const SGPropertyNode* args, SGPropertyNode* root); bool loadScenarioCommand(const SGPropertyNode* args, SGPropertyNode* root);
bool unloadScenarioCommand(const SGPropertyNode* args, SGPropertyNode* root); bool unloadScenarioCommand(const SGPropertyNode* args, SGPropertyNode* root);
bool addObjectCommand(const SGPropertyNode* definition); bool addObjectCommand(const SGPropertyNode* definition);
bool removeObject(const SGPropertyNode* args); bool removeObject(const SGPropertyNode* args);
bool unloadScenario( const std::string &filename ); bool unloadScenario( const std::string &filename );
void unloadAllScenarios(); void unloadAllScenarios();
SGPropertyNode_ptr root; SGPropertyNode_ptr root;
SGPropertyNode_ptr enabled; SGPropertyNode_ptr enabled;
SGPropertyNode_ptr thermal_lift_node; SGPropertyNode_ptr thermal_lift_node;
@ -120,10 +120,11 @@ private:
SGPropertyNode_ptr wind_from_east_node; SGPropertyNode_ptr wind_from_east_node;
SGPropertyNode_ptr wind_from_north_node; SGPropertyNode_ptr wind_from_north_node;
SGPropertyNode_ptr _environmentVisiblity; SGPropertyNode_ptr _environmentVisiblity;
SGPropertyNode_ptr _mp_use_detailed_models;
ai_list_type ai_list; ai_list_type ai_list;
double user_altitude_agl; double user_altitude_agl;
double user_heading; double user_heading;
double user_pitch; double user_pitch;
@ -141,11 +142,11 @@ private:
SGPropertyChangeCallback<FGAIManager> cb_ai_bare; SGPropertyChangeCallback<FGAIManager> cb_ai_bare;
SGPropertyChangeCallback<FGAIManager> cb_ai_detailed; SGPropertyChangeCallback<FGAIManager> cb_ai_detailed;
class Scenario; class Scenario;
typedef std::map<std::string, Scenario*> ScenarioDict; typedef std::map<std::string, Scenario*> ScenarioDict;
ScenarioDict _scenarios; ScenarioDict _scenarios;
SGSharedPtr<FGAIAircraft> _userAircraft; SGSharedPtr<FGAIAircraft> _userAircraft;
}; };