2018-04-15 22:07:18 +00:00
|
|
|
#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_TILECACHEDRECASTMESHMANAGER_H
|
|
|
|
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_TILECACHEDRECASTMESHMANAGER_H
|
|
|
|
|
|
|
|
#include "cachedrecastmeshmanager.hpp"
|
|
|
|
#include "tileposition.hpp"
|
2020-02-25 02:38:39 +00:00
|
|
|
#include "settingsutils.hpp"
|
|
|
|
#include "gettilespositions.hpp"
|
2021-04-18 14:51:52 +00:00
|
|
|
#include "version.hpp"
|
2018-04-15 22:07:18 +00:00
|
|
|
|
2018-09-29 19:57:41 +00:00
|
|
|
#include <components/misc/guarded.hpp>
|
|
|
|
|
2018-04-15 22:07:18 +00:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2019-02-16 21:48:07 +00:00
|
|
|
#include <set>
|
2018-04-15 22:07:18 +00:00
|
|
|
|
|
|
|
namespace DetourNavigator
|
|
|
|
{
|
|
|
|
class TileCachedRecastMeshManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TileCachedRecastMeshManager(const Settings& settings);
|
|
|
|
|
2018-09-22 15:36:57 +00:00
|
|
|
bool addObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform,
|
2018-07-18 19:09:50 +00:00
|
|
|
const AreaType areaType);
|
2018-04-15 22:07:18 +00:00
|
|
|
|
2020-02-25 02:38:39 +00:00
|
|
|
template <class OnChangedTile>
|
|
|
|
bool updateObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform,
|
|
|
|
const AreaType areaType, OnChangedTile&& onChangedTile)
|
|
|
|
{
|
|
|
|
const auto object = mObjectsTilesPositions.find(id);
|
|
|
|
if (object == mObjectsTilesPositions.end())
|
|
|
|
return false;
|
|
|
|
auto& currentTiles = object->second;
|
|
|
|
const auto border = getBorderSize(mSettings);
|
|
|
|
bool changed = false;
|
|
|
|
std::set<TilePosition> newTiles;
|
|
|
|
{
|
|
|
|
auto tiles = mTiles.lock();
|
|
|
|
const auto onTilePosition = [&] (const TilePosition& tilePosition)
|
|
|
|
{
|
|
|
|
if (currentTiles.count(tilePosition))
|
|
|
|
{
|
|
|
|
newTiles.insert(tilePosition);
|
|
|
|
if (updateTile(id, transform, areaType, tilePosition, tiles.get()))
|
|
|
|
{
|
|
|
|
onChangedTile(tilePosition);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (addTile(id, shape, transform, areaType, tilePosition, border, tiles.get()))
|
|
|
|
{
|
|
|
|
newTiles.insert(tilePosition);
|
|
|
|
onChangedTile(tilePosition);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getTilesPositions(shape, transform, mSettings, onTilePosition);
|
|
|
|
for (const auto& tile : currentTiles)
|
|
|
|
{
|
|
|
|
if (!newTiles.count(tile) && removeTile(id, tile, tiles.get()))
|
|
|
|
{
|
|
|
|
onChangedTile(tile);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::swap(currentTiles, newTiles);
|
|
|
|
if (changed)
|
|
|
|
++mRevision;
|
|
|
|
return changed;
|
|
|
|
}
|
2018-05-26 14:44:25 +00:00
|
|
|
|
2020-10-24 22:33:41 +00:00
|
|
|
std::optional<RemovedRecastMeshObject> removeObject(const ObjectId id);
|
2018-04-15 22:07:18 +00:00
|
|
|
|
2018-07-20 19:11:34 +00:00
|
|
|
bool addWater(const osg::Vec2i& cellPosition, const int cellSize, const btTransform& transform);
|
|
|
|
|
2020-10-24 22:33:41 +00:00
|
|
|
std::optional<RecastMeshManager::Water> removeWater(const osg::Vec2i& cellPosition);
|
2018-07-20 19:11:34 +00:00
|
|
|
|
2018-04-15 22:07:18 +00:00
|
|
|
std::shared_ptr<RecastMesh> getMesh(const TilePosition& tilePosition);
|
|
|
|
|
2018-04-20 23:57:01 +00:00
|
|
|
bool hasTile(const TilePosition& tilePosition);
|
|
|
|
|
|
|
|
template <class Function>
|
|
|
|
void forEachTilePosition(Function&& function)
|
|
|
|
{
|
2018-09-29 19:57:41 +00:00
|
|
|
for (const auto& tile : *mTiles.lock())
|
2018-04-20 23:57:01 +00:00
|
|
|
function(tile.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t getRevision() const;
|
|
|
|
|
2021-04-18 14:51:52 +00:00
|
|
|
void reportNavMeshChange(const TilePosition& tilePosition, Version recastMeshVersion, Version navMeshVersion);
|
|
|
|
|
2018-04-15 22:07:18 +00:00
|
|
|
private:
|
|
|
|
const Settings& mSettings;
|
2018-09-29 19:57:41 +00:00
|
|
|
Misc::ScopeGuarded<std::map<TilePosition, CachedRecastMeshManager>> mTiles;
|
2019-02-16 21:48:07 +00:00
|
|
|
std::unordered_map<ObjectId, std::set<TilePosition>> mObjectsTilesPositions;
|
2018-07-20 19:11:34 +00:00
|
|
|
std::map<osg::Vec2i, std::vector<TilePosition>> mWaterTilesPositions;
|
2018-04-20 23:57:01 +00:00
|
|
|
std::size_t mRevision = 0;
|
2019-11-27 22:45:01 +00:00
|
|
|
std::size_t mTilesGeneration = 0;
|
2019-02-16 21:48:07 +00:00
|
|
|
|
|
|
|
bool addTile(const ObjectId id, const btCollisionShape& shape, const btTransform& transform,
|
|
|
|
const AreaType areaType, const TilePosition& tilePosition, float border,
|
|
|
|
std::map<TilePosition, CachedRecastMeshManager>& tiles);
|
|
|
|
|
|
|
|
bool updateTile(const ObjectId id, const btTransform& transform, const AreaType areaType,
|
|
|
|
const TilePosition& tilePosition, std::map<TilePosition, CachedRecastMeshManager>& tiles);
|
|
|
|
|
2020-10-24 22:33:41 +00:00
|
|
|
std::optional<RemovedRecastMeshObject> removeTile(const ObjectId id, const TilePosition& tilePosition,
|
2019-02-16 21:48:07 +00:00
|
|
|
std::map<TilePosition, CachedRecastMeshManager>& tiles);
|
2018-04-15 22:07:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|