2019-02-16 15:27:02 +03:00
|
|
|
#include "navigatorimpl.hpp"
|
2018-03-14 01:49:08 +03:00
|
|
|
#include "debug.hpp"
|
|
|
|
#include "settingsutils.hpp"
|
|
|
|
|
2021-07-14 20:57:52 +02:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2020-06-11 23:23:30 +02:00
|
|
|
#include <components/esm/loadpgrd.hpp>
|
|
|
|
#include <components/misc/coordinateconverter.hpp>
|
|
|
|
|
2018-03-14 01:49:08 +03:00
|
|
|
namespace DetourNavigator
|
|
|
|
{
|
2021-07-09 22:51:42 +02:00
|
|
|
NavigatorImpl::NavigatorImpl(const Settings& settings, std::unique_ptr<NavMeshDb>&& db)
|
2018-03-14 01:49:08 +03:00
|
|
|
: mSettings(settings)
|
2021-07-09 22:51:42 +02:00
|
|
|
, mNavMeshManager(mSettings, std::move(db))
|
2020-05-08 13:37:00 +00:00
|
|
|
, mUpdatesEnabled(true)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
void NavigatorImpl::addAgent(const osg::Vec3f& agentHalfExtents)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2021-03-07 20:58:09 +01:00
|
|
|
if(agentHalfExtents.length2() <= 0)
|
|
|
|
return;
|
2018-03-14 01:49:08 +03:00
|
|
|
++mAgents[agentHalfExtents];
|
2018-04-01 20:24:02 +03:00
|
|
|
mNavMeshManager.addAgent(agentHalfExtents);
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
void NavigatorImpl::removeAgent(const osg::Vec3f& agentHalfExtents)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
|
|
|
const auto it = mAgents.find(agentHalfExtents);
|
2019-03-10 14:36:16 +03:00
|
|
|
if (it == mAgents.end())
|
2018-03-14 01:49:08 +03:00
|
|
|
return;
|
2019-03-10 14:36:16 +03:00
|
|
|
if (it->second > 0)
|
|
|
|
--it->second;
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 22:51:42 +02:00
|
|
|
void NavigatorImpl::setWorldspace(std::string_view worldspace)
|
|
|
|
{
|
|
|
|
mNavMeshManager.setWorldspace(worldspace);
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
bool NavigatorImpl::addObject(const ObjectId id, const ObjectShapes& shapes, const btTransform& transform)
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
2021-11-04 01:57:27 +01:00
|
|
|
const CollisionShape collisionShape(shapes.mShapeInstance, *shapes.mShapeInstance->mCollisionShape, shapes.mTransform);
|
2021-08-01 02:13:55 +02:00
|
|
|
bool result = mNavMeshManager.addObject(id, collisionShape, transform, AreaType_ground);
|
2021-10-30 03:30:38 +02:00
|
|
|
if (const btCollisionShape* const avoidShape = shapes.mShapeInstance->mAvoidCollisionShape.get())
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
2021-08-01 02:13:55 +02:00
|
|
|
const ObjectId avoidId(avoidShape);
|
2021-11-04 01:57:27 +01:00
|
|
|
const CollisionShape avoidCollisionShape(shapes.mShapeInstance, *avoidShape, shapes.mTransform);
|
2021-09-29 11:36:05 +04:00
|
|
|
if (mNavMeshManager.addObject(avoidId, avoidCollisionShape, transform, AreaType_null))
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
|
|
|
updateAvoidShapeId(id, avoidId);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2018-04-03 00:04:19 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
bool NavigatorImpl::addObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
|
|
|
if (addObject(id, static_cast<const ObjectShapes&>(shapes), transform))
|
|
|
|
{
|
2021-11-06 13:46:43 +01:00
|
|
|
const osg::Vec3f start = toNavMeshCoordinates(mSettings.mRecast, shapes.mConnectionStart);
|
|
|
|
const osg::Vec3f end = toNavMeshCoordinates(mSettings.mRecast, shapes.mConnectionEnd);
|
2021-06-27 17:45:40 +02:00
|
|
|
mNavMeshManager.addOffMeshConnection(id, start, end, AreaType_door);
|
|
|
|
mNavMeshManager.addOffMeshConnection(id, end, start, AreaType_door);
|
2018-08-26 23:27:38 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
bool NavigatorImpl::updateObject(const ObjectId id, const ObjectShapes& shapes, const btTransform& transform)
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
2021-11-04 01:57:27 +01:00
|
|
|
const CollisionShape collisionShape(shapes.mShapeInstance, *shapes.mShapeInstance->mCollisionShape, shapes.mTransform);
|
2021-08-01 02:13:55 +02:00
|
|
|
bool result = mNavMeshManager.updateObject(id, collisionShape, transform, AreaType_ground);
|
2021-10-30 03:30:38 +02:00
|
|
|
if (const btCollisionShape* const avoidShape = shapes.mShapeInstance->mAvoidCollisionShape.get())
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
2021-08-01 02:13:55 +02:00
|
|
|
const ObjectId avoidId(avoidShape);
|
2021-11-04 01:57:27 +01:00
|
|
|
const CollisionShape avoidCollisionShape(shapes.mShapeInstance, *avoidShape, shapes.mTransform);
|
2021-10-24 18:45:04 +04:00
|
|
|
if (mNavMeshManager.updateObject(avoidId, avoidCollisionShape, transform, AreaType_null))
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
|
|
|
updateAvoidShapeId(id, avoidId);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2018-05-26 17:44:25 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
bool NavigatorImpl::updateObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
|
|
|
return updateObject(id, static_cast<const ObjectShapes&>(shapes), transform);
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
bool NavigatorImpl::removeObject(const ObjectId id)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2018-07-12 11:44:11 +03:00
|
|
|
bool result = mNavMeshManager.removeObject(id);
|
|
|
|
const auto avoid = mAvoidIds.find(id);
|
2018-07-20 22:11:34 +03:00
|
|
|
if (avoid != mAvoidIds.end())
|
|
|
|
result = mNavMeshManager.removeObject(avoid->second) || result;
|
|
|
|
const auto water = mWaterIds.find(id);
|
|
|
|
if (water != mWaterIds.end())
|
|
|
|
result = mNavMeshManager.removeObject(water->second) || result;
|
2020-06-11 23:23:30 +02:00
|
|
|
mNavMeshManager.removeOffMeshConnections(id);
|
2018-07-20 22:11:34 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:48:32 +01:00
|
|
|
bool NavigatorImpl::addWater(const osg::Vec2i& cellPosition, int cellSize, float level)
|
2018-07-20 22:11:34 +03:00
|
|
|
{
|
2021-11-04 02:48:32 +01:00
|
|
|
return mNavMeshManager.addWater(cellPosition, cellSize, level);
|
2018-07-20 22:11:34 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
bool NavigatorImpl::removeWater(const osg::Vec2i& cellPosition)
|
2018-07-20 22:11:34 +03:00
|
|
|
{
|
|
|
|
return mNavMeshManager.removeWater(cellPosition);
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
|
|
|
|
2021-11-04 03:19:41 +01:00
|
|
|
bool NavigatorImpl::addHeightfield(const osg::Vec2i& cellPosition, int cellSize, const HeightfieldShape& shape)
|
2021-07-14 20:57:52 +02:00
|
|
|
{
|
2021-11-04 03:19:41 +01:00
|
|
|
return mNavMeshManager.addHeightfield(cellPosition, cellSize, shape);
|
2021-07-14 20:57:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NavigatorImpl::removeHeightfield(const osg::Vec2i& cellPosition)
|
|
|
|
{
|
|
|
|
return mNavMeshManager.removeHeightfield(cellPosition);
|
|
|
|
}
|
|
|
|
|
2020-06-11 23:23:30 +02:00
|
|
|
void NavigatorImpl::addPathgrid(const ESM::Cell& cell, const ESM::Pathgrid& pathgrid)
|
|
|
|
{
|
|
|
|
Misc::CoordinateConverter converter(&cell);
|
|
|
|
for (auto edge : pathgrid.mEdges)
|
|
|
|
{
|
|
|
|
const auto src = Misc::Convert::makeOsgVec3f(converter.toWorldPoint(pathgrid.mPoints[edge.mV0]));
|
|
|
|
const auto dst = Misc::Convert::makeOsgVec3f(converter.toWorldPoint(pathgrid.mPoints[edge.mV1]));
|
|
|
|
mNavMeshManager.addOffMeshConnection(
|
|
|
|
ObjectId(&pathgrid),
|
2021-11-06 13:46:43 +01:00
|
|
|
toNavMeshCoordinates(mSettings.mRecast, src),
|
|
|
|
toNavMeshCoordinates(mSettings.mRecast, dst),
|
2020-06-11 23:23:30 +02:00
|
|
|
AreaType_pathgrid
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigatorImpl::removePathgrid(const ESM::Pathgrid& pathgrid)
|
|
|
|
{
|
|
|
|
mNavMeshManager.removeOffMeshConnections(ObjectId(&pathgrid));
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
void NavigatorImpl::update(const osg::Vec3f& playerPosition)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2020-05-08 13:37:00 +00:00
|
|
|
if (!mUpdatesEnabled)
|
|
|
|
return;
|
2019-03-10 14:36:16 +03:00
|
|
|
removeUnusedNavMeshes();
|
2018-03-14 01:49:08 +03:00
|
|
|
for (const auto& v : mAgents)
|
2018-04-01 20:24:02 +03:00
|
|
|
mNavMeshManager.update(playerPosition, v.first);
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
|
|
|
|
2021-05-27 16:52:01 +02:00
|
|
|
void NavigatorImpl::updatePlayerPosition(const osg::Vec3f& playerPosition)
|
|
|
|
{
|
2021-11-06 13:46:43 +01:00
|
|
|
const TilePosition tilePosition = getTilePosition(mSettings.mRecast, toNavMeshCoordinates(mSettings.mRecast, playerPosition));
|
2021-05-27 16:52:01 +02:00
|
|
|
if (mLastPlayerPosition.has_value() && *mLastPlayerPosition == tilePosition)
|
|
|
|
return;
|
|
|
|
update(playerPosition);
|
|
|
|
mLastPlayerPosition = tilePosition;
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:37:00 +00:00
|
|
|
void NavigatorImpl::setUpdatesEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
mUpdatesEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:06:29 +02:00
|
|
|
void NavigatorImpl::wait(Loading::Listener& listener, WaitConditionType waitConditionType)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2021-05-14 21:06:29 +02:00
|
|
|
mNavMeshManager.wait(listener, waitConditionType);
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
2018-04-07 16:11:23 +03:00
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
SharedNavMeshCacheItem NavigatorImpl::getNavMesh(const osg::Vec3f& agentHalfExtents) const
|
2019-02-16 15:14:05 +03:00
|
|
|
{
|
|
|
|
return mNavMeshManager.getNavMesh(agentHalfExtents);
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
std::map<osg::Vec3f, SharedNavMeshCacheItem> NavigatorImpl::getNavMeshes() const
|
2018-04-07 16:11:23 +03:00
|
|
|
{
|
|
|
|
return mNavMeshManager.getNavMeshes();
|
|
|
|
}
|
|
|
|
|
2019-02-19 11:46:00 +03:00
|
|
|
const Settings& NavigatorImpl::getSettings() const
|
2018-04-07 16:11:23 +03:00
|
|
|
{
|
|
|
|
return mSettings;
|
|
|
|
}
|
2018-07-12 11:44:11 +03:00
|
|
|
|
2019-03-17 20:18:53 +03:00
|
|
|
void NavigatorImpl::reportStats(unsigned int frameNumber, osg::Stats& stats) const
|
|
|
|
{
|
|
|
|
mNavMeshManager.reportStats(frameNumber, stats);
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:36:37 +02:00
|
|
|
RecastMeshTiles NavigatorImpl::getRecastMeshTiles() const
|
2019-11-27 23:45:01 +01:00
|
|
|
{
|
|
|
|
return mNavMeshManager.getRecastMeshTiles();
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
void NavigatorImpl::updateAvoidShapeId(const ObjectId id, const ObjectId avoidId)
|
2018-07-20 22:11:34 +03:00
|
|
|
{
|
|
|
|
updateId(id, avoidId, mWaterIds);
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
void NavigatorImpl::updateWaterShapeId(const ObjectId id, const ObjectId waterId)
|
2018-07-20 22:11:34 +03:00
|
|
|
{
|
|
|
|
updateId(id, waterId, mWaterIds);
|
|
|
|
}
|
|
|
|
|
2019-02-16 15:27:02 +03:00
|
|
|
void NavigatorImpl::updateId(const ObjectId id, const ObjectId updateId, std::unordered_map<ObjectId, ObjectId>& ids)
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
2018-07-20 22:11:34 +03:00
|
|
|
auto inserted = ids.insert(std::make_pair(id, updateId));
|
2018-07-12 11:44:11 +03:00
|
|
|
if (!inserted.second)
|
|
|
|
{
|
|
|
|
mNavMeshManager.removeObject(inserted.first->second);
|
2018-09-22 18:36:57 +03:00
|
|
|
inserted.first->second = updateId;
|
2018-07-12 11:44:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-10 14:36:16 +03:00
|
|
|
|
|
|
|
void NavigatorImpl::removeUnusedNavMeshes()
|
|
|
|
{
|
|
|
|
for (auto it = mAgents.begin(); it != mAgents.end();)
|
|
|
|
{
|
|
|
|
if (it->second == 0 && mNavMeshManager.reset(it->first))
|
|
|
|
it = mAgents.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 23:15:13 +01:00
|
|
|
|
|
|
|
float NavigatorImpl::getMaxNavmeshAreaRealRadius() const
|
|
|
|
{
|
|
|
|
const auto& settings = getSettings();
|
2021-11-06 13:46:43 +01:00
|
|
|
return getRealTileSize(settings.mRecast) * getMaxNavmeshAreaRadius(settings);
|
2021-03-23 23:15:13 +01:00
|
|
|
}
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|