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>
|
|
|
|
|
2021-05-27 13:25:21 +00:00
|
|
|
#include <algorithm>
|
2018-04-15 22:07:18 +00:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2021-05-27 13:25:21 +00:00
|
|
|
#include <vector>
|
2018-04-15 22:07:18 +00:00
|
|
|
|
|
|
|
namespace DetourNavigator
|
|
|
|
{
|
|
|
|
class TileCachedRecastMeshManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TileCachedRecastMeshManager(const Settings& settings);
|
|
|
|
|
2021-07-31 23:27:41 +00:00
|
|
|
bool addObject(const ObjectId id, const CollisionShape& 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>
|
2021-07-31 23:27:41 +00:00
|
|
|
bool updateObject(const ObjectId id, const CollisionShape& shape, const btTransform& transform,
|
2020-02-25 02:38:39 +00:00
|
|
|
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;
|
2021-05-27 13:25:21 +00:00
|
|
|
std::vector<TilePosition> newTiles;
|
2020-02-25 02:38:39 +00:00
|
|
|
{
|
|
|
|
auto tiles = mTiles.lock();
|
|
|
|
const auto onTilePosition = [&] (const TilePosition& tilePosition)
|
|
|
|
{
|
2021-05-27 13:25:21 +00:00
|
|
|
if (std::binary_search(currentTiles.begin(), currentTiles.end(), tilePosition))
|
2020-02-25 02:38:39 +00:00
|
|
|
{
|
2021-05-27 13:25:21 +00:00
|
|
|
newTiles.push_back(tilePosition);
|
2020-02-25 02:38:39 +00:00
|
|
|
if (updateTile(id, transform, areaType, tilePosition, tiles.get()))
|
|
|
|
{
|
|
|
|
onChangedTile(tilePosition);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (addTile(id, shape, transform, areaType, tilePosition, border, tiles.get()))
|
|
|
|
{
|
2021-05-27 13:25:21 +00:00
|
|
|
newTiles.push_back(tilePosition);
|
2020-02-25 02:38:39 +00:00
|
|
|
onChangedTile(tilePosition);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
};
|
2021-07-31 23:27:41 +00:00
|
|
|
getTilesPositions(shape.getShape(), transform, mSettings, onTilePosition);
|
2021-05-27 13:25:21 +00:00
|
|
|
std::sort(newTiles.begin(), newTiles.end());
|
2020-02-25 02:38:39 +00:00
|
|
|
for (const auto& tile : currentTiles)
|
|
|
|
{
|
2021-05-27 13:25:21 +00:00
|
|
|
if (!std::binary_search(newTiles.begin(), newTiles.end(), tile) && removeTile(id, tile, tiles.get()))
|
2020-02-25 02:38:39 +00:00
|
|
|
{
|
|
|
|
onChangedTile(tile);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed)
|
2021-05-27 13:25:21 +00:00
|
|
|
{
|
|
|
|
currentTiles = std::move(newTiles);
|
2020-02-25 02:38:39 +00:00
|
|
|
++mRevision;
|
2021-05-27 13:25:21 +00:00
|
|
|
}
|
2020-02-25 02:38:39 +00:00
|
|
|
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>
|
2021-05-26 23:09:58 +00:00
|
|
|
void forEachTile(Function&& function)
|
2018-04-20 23:57:01 +00:00
|
|
|
{
|
2021-05-26 23:09:58 +00:00
|
|
|
for (auto& [tilePosition, recastMeshManager] : *mTiles.lock())
|
2021-08-01 00:43:36 +00:00
|
|
|
function(tilePosition, *recastMeshManager);
|
2018-04-20 23:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2021-08-01 00:43:36 +00:00
|
|
|
using TilesMap = std::map<TilePosition, std::shared_ptr<CachedRecastMeshManager>>;
|
|
|
|
|
2018-04-15 22:07:18 +00:00
|
|
|
const Settings& mSettings;
|
2021-08-01 00:43:36 +00:00
|
|
|
Misc::ScopeGuarded<TilesMap> mTiles;
|
2021-05-27 13:25:21 +00:00
|
|
|
std::unordered_map<ObjectId, std::vector<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
|
|
|
|
2021-07-31 23:27:41 +00:00
|
|
|
bool addTile(const ObjectId id, const CollisionShape& shape, const btTransform& transform,
|
2021-08-01 00:43:36 +00:00
|
|
|
const AreaType areaType, const TilePosition& tilePosition, float border, TilesMap& tiles);
|
2019-02-16 21:48:07 +00:00
|
|
|
|
|
|
|
bool updateTile(const ObjectId id, const btTransform& transform, const AreaType areaType,
|
2021-08-01 00:43:36 +00:00
|
|
|
const TilePosition& tilePosition, TilesMap& tiles);
|
2019-02-16 21:48:07 +00:00
|
|
|
|
2020-10-24 22:33:41 +00:00
|
|
|
std::optional<RemovedRecastMeshObject> removeTile(const ObjectId id, const TilePosition& tilePosition,
|
2021-08-01 00:43:36 +00:00
|
|
|
TilesMap& tiles);
|
2018-04-15 22:07:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|