1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-05 15:55:45 +00:00
OpenMW/components/detournavigator/offmeshconnectionsmanager.hpp

105 lines
3.1 KiB
C++
Raw Normal View History

#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_OFFMESHCONNECTIONSMANAGER_H
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_OFFMESHCONNECTIONSMANAGER_H
#include "settings.hpp"
#include "settingsutils.hpp"
#include "tileposition.hpp"
2018-09-22 15:36:57 +00:00
#include "objectid.hpp"
#include "offmeshconnection.hpp"
2018-09-29 19:57:41 +00:00
#include <components/misc/guarded.hpp>
#include <osg/Vec3f>
2020-04-30 19:57:22 +00:00
#include <algorithm>
#include <map>
#include <unordered_set>
#include <vector>
#include <set>
namespace DetourNavigator
{
class OffMeshConnectionsManager
{
public:
OffMeshConnectionsManager(const Settings& settings)
: mSettings(settings)
{}
void add(const ObjectId id, const OffMeshConnection& value)
{
2018-09-29 19:57:41 +00:00
const auto values = mValues.lock();
values->mById.insert(std::make_pair(id, value));
const auto startTilePosition = getTilePosition(mSettings, value.mStart);
const auto endTilePosition = getTilePosition(mSettings, value.mEnd);
2018-09-29 19:57:41 +00:00
values->mByTilePosition[startTilePosition].insert(id);
if (startTilePosition != endTilePosition)
2018-09-29 19:57:41 +00:00
values->mByTilePosition[endTilePosition].insert(id);
}
std::set<TilePosition> remove(const ObjectId id)
{
2018-09-29 19:57:41 +00:00
const auto values = mValues.lock();
const auto byId = values->mById.equal_range(id);
2021-06-27 13:23:15 +00:00
if (byId.first == byId.second)
return {};
std::set<TilePosition> removed;
std::for_each(byId.first, byId.second, [&] (const auto& v) {
const auto startTilePosition = getTilePosition(mSettings, v.second.mStart);
const auto endTilePosition = getTilePosition(mSettings, v.second.mEnd);
removed.emplace(startTilePosition);
if (startTilePosition != endTilePosition)
removed.emplace(endTilePosition);
});
values->mById.erase(byId.first, byId.second);
return removed;
}
std::vector<OffMeshConnection> get(const TilePosition& tilePosition)
{
std::vector<OffMeshConnection> result;
2018-09-29 19:57:41 +00:00
const auto values = mValues.lock();
2018-09-29 19:57:41 +00:00
const auto itByTilePosition = values->mByTilePosition.find(tilePosition);
2018-09-29 19:57:41 +00:00
if (itByTilePosition == values->mByTilePosition.end())
return result;
std::for_each(itByTilePosition->second.begin(), itByTilePosition->second.end(),
2018-09-22 15:36:57 +00:00
[&] (const ObjectId v)
{
const auto byId = values->mById.equal_range(v);
std::for_each(byId.first, byId.second, [&] (const auto& v) { result.push_back(v.second); });
});
std::sort(result.begin(), result.end());
return result;
}
private:
2018-09-29 19:57:41 +00:00
struct Values
{
std::multimap<ObjectId, OffMeshConnection> mById;
2018-09-29 19:57:41 +00:00
std::map<TilePosition, std::unordered_set<ObjectId>> mByTilePosition;
};
const Settings& mSettings;
2018-09-29 19:57:41 +00:00
Misc::ScopeGuarded<Values> mValues;
};
}
#endif