1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 20:49:56 +00:00
openmw-tes3mp/components/detournavigator/tilecachedrecastmeshmanager.hpp

115 lines
4.4 KiB
C++
Raw Normal View History

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"
#include "settingsutils.hpp"
#include "gettilespositions.hpp"
#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>
#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
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
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);
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);
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())
function(tile.first);
}
std::size_t getRevision() const;
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;
std::unordered_map<ObjectId, std::set<TilePosition>> mObjectsTilesPositions;
2018-07-20 19:11:34 +00:00
std::map<osg::Vec2i, std::vector<TilePosition>> mWaterTilesPositions;
std::size_t mRevision = 0;
2019-11-27 22:45:01 +00:00
std::size_t mTilesGeneration = 0;
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);
std::optional<RemovedRecastMeshObject> removeTile(const ObjectId id, const TilePosition& tilePosition,
std::map<TilePosition, CachedRecastMeshManager>& tiles);
2018-04-15 22:07:18 +00:00
};
}
#endif