1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 21:19:57 +00:00
openmw-tes3mp/components/detournavigator/tilecachedrecastmeshmanager.cpp

186 lines
6.7 KiB
C++
Raw Normal View History

2018-04-15 22:07:18 +00:00
#include "tilecachedrecastmeshmanager.hpp"
#include "makenavmesh.hpp"
#include "gettilespositions.hpp"
#include "settingsutils.hpp"
namespace DetourNavigator
{
TileCachedRecastMeshManager::TileCachedRecastMeshManager(const Settings& settings)
: mSettings(settings)
2018-07-12 08:44:11 +00:00
{}
2018-04-15 22:07:18 +00:00
2018-09-22 15:36:57 +00:00
bool TileCachedRecastMeshManager::addObject(const ObjectId id, const btCollisionShape& shape,
2018-07-18 19:09:50 +00:00
const btTransform& transform, const AreaType areaType)
2018-04-15 22:07:18 +00:00
{
bool result = false;
auto& tilesPositions = mObjectsTilesPositions[id];
const auto border = getBorderSize(mSettings);
{
auto tiles = mTiles.lock();
getTilesPositions(shape, transform, mSettings, [&] (const TilePosition& tilePosition)
2018-04-15 22:07:18 +00:00
{
if (addTile(id, shape, transform, areaType, tilePosition, border, tiles.get()))
{
tilesPositions.insert(tilePosition);
result = true;
}
});
}
if (result)
++mRevision;
2018-04-15 22:07:18 +00:00
return result;
}
2018-09-22 15:36:57 +00:00
boost::optional<RemovedRecastMeshObject> TileCachedRecastMeshManager::removeObject(const ObjectId id)
2018-04-15 22:07:18 +00:00
{
const auto object = mObjectsTilesPositions.find(id);
if (object == mObjectsTilesPositions.end())
return boost::none;
2018-05-26 14:44:25 +00:00
boost::optional<RemovedRecastMeshObject> result;
2018-04-15 22:07:18 +00:00
{
auto tiles = mTiles.lock();
for (const auto& tilePosition : object->second)
{
const auto removed = removeTile(id, tilePosition, tiles.get());
if (removed && !result)
result = removed;
}
2018-04-15 22:07:18 +00:00
}
if (result)
++mRevision;
2018-04-15 22:07:18 +00:00
return result;
}
2018-07-20 19:11:34 +00:00
bool TileCachedRecastMeshManager::addWater(const osg::Vec2i& cellPosition, const int cellSize,
const btTransform& transform)
{
const auto border = getBorderSize(mSettings);
auto& tilesPositions = mWaterTilesPositions[cellPosition];
bool result = false;
if (cellSize == std::numeric_limits<int>::max())
{
2018-09-29 19:57:41 +00:00
const auto tiles = mTiles.lock();
for (auto& tile : *tiles)
2018-07-20 19:11:34 +00:00
{
if (tile.second.addWater(cellPosition, cellSize, transform))
{
tilesPositions.push_back(tile.first);
result = true;
}
}
}
else
{
getTilesPositions(cellSize, transform, mSettings, [&] (const TilePosition& tilePosition)
{
2018-09-29 19:57:41 +00:00
const auto tiles = mTiles.lock();
auto tile = tiles->find(tilePosition);
if (tile == tiles->end())
2018-07-20 19:11:34 +00:00
{
auto tileBounds = makeTileBounds(mSettings, tilePosition);
tileBounds.mMin -= osg::Vec2f(border, border);
tileBounds.mMax += osg::Vec2f(border, border);
2018-09-29 19:57:41 +00:00
tile = tiles->insert(std::make_pair(tilePosition,
2019-11-27 22:45:01 +00:00
CachedRecastMeshManager(mSettings, tileBounds, mTilesGeneration))).first;
2018-07-20 19:11:34 +00:00
}
if (tile->second.addWater(cellPosition, cellSize, transform))
{
tilesPositions.push_back(tilePosition);
result = true;
}
});
}
if (result)
++mRevision;
return result;
}
boost::optional<RecastMeshManager::Water> TileCachedRecastMeshManager::removeWater(const osg::Vec2i& cellPosition)
{
const auto object = mWaterTilesPositions.find(cellPosition);
if (object == mWaterTilesPositions.end())
return boost::none;
boost::optional<RecastMeshManager::Water> result;
for (const auto& tilePosition : object->second)
{
2018-09-29 19:57:41 +00:00
const auto tiles = mTiles.lock();
const auto tile = tiles->find(tilePosition);
if (tile == tiles->end())
2018-07-20 19:11:34 +00:00
continue;
const auto tileResult = tile->second.removeWater(cellPosition);
if (tile->second.isEmpty())
2019-11-27 22:45:01 +00:00
{
2018-09-29 19:57:41 +00:00
tiles->erase(tile);
2019-11-27 22:45:01 +00:00
++mTilesGeneration;
}
2018-07-20 19:11:34 +00:00
if (tileResult && !result)
result = tileResult;
}
if (result)
++mRevision;
return result;
}
2018-04-15 22:07:18 +00:00
std::shared_ptr<RecastMesh> TileCachedRecastMeshManager::getMesh(const TilePosition& tilePosition)
{
2018-09-29 19:57:41 +00:00
const auto tiles = mTiles.lock();
const auto it = tiles->find(tilePosition);
if (it == tiles->end())
2018-04-15 22:07:18 +00:00
return nullptr;
return it->second.getMesh();
}
bool TileCachedRecastMeshManager::hasTile(const TilePosition& tilePosition)
{
2018-09-29 19:57:41 +00:00
return mTiles.lockConst()->count(tilePosition);
}
std::size_t TileCachedRecastMeshManager::getRevision() const
{
return mRevision;
}
bool TileCachedRecastMeshManager::addTile(const ObjectId id, const btCollisionShape& shape,
const btTransform& transform, const AreaType areaType, const TilePosition& tilePosition, float border,
std::map<TilePosition, CachedRecastMeshManager>& tiles)
{
auto tile = tiles.find(tilePosition);
if (tile == tiles.end())
{
auto tileBounds = makeTileBounds(mSettings, tilePosition);
tileBounds.mMin -= osg::Vec2f(border, border);
tileBounds.mMax += osg::Vec2f(border, border);
2019-11-27 22:45:01 +00:00
tile = tiles.insert(std::make_pair(
tilePosition, CachedRecastMeshManager(mSettings, tileBounds, mTilesGeneration))).first;
}
return tile->second.addObject(id, shape, transform, areaType);
}
bool TileCachedRecastMeshManager::updateTile(const ObjectId id, const btTransform& transform,
const AreaType areaType, const TilePosition& tilePosition, std::map<TilePosition, CachedRecastMeshManager>& tiles)
{
const auto tile = tiles.find(tilePosition);
return tile != tiles.end() && tile->second.updateObject(id, transform, areaType);
}
boost::optional<RemovedRecastMeshObject> TileCachedRecastMeshManager::removeTile(const ObjectId id,
const TilePosition& tilePosition, std::map<TilePosition, CachedRecastMeshManager>& tiles)
{
const auto tile = tiles.find(tilePosition);
if (tile == tiles.end())
return boost::optional<RemovedRecastMeshObject>();
const auto tileResult = tile->second.removeObject(id);
if (tile->second.isEmpty())
2019-11-27 22:45:01 +00:00
{
tiles.erase(tile);
2019-11-27 22:45:01 +00:00
++mTilesGeneration;
}
return tileResult;
}
2018-04-15 22:07:18 +00:00
}