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