2019-02-16 15:27:02 +03:00
|
|
|
#include "navigatorimpl.hpp"
|
2023-01-17 23:31:17 +01:00
|
|
|
#include "makenavmesh.hpp"
|
2018-03-14 01:49:08 +03:00
|
|
|
#include "settingsutils.hpp"
|
2022-08-28 14:52:12 +02:00
|
|
|
#include "stats.hpp"
|
2018-03-14 01:49:08 +03:00
|
|
|
|
2022-01-22 15:58:41 +01:00
|
|
|
#include <components/esm3/loadpgrd.hpp>
|
2022-01-25 14:06:53 +00:00
|
|
|
#include <components/misc/convert.hpp>
|
2020-06-11 23:23:30 +02:00
|
|
|
#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))
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:31:17 +01:00
|
|
|
bool NavigatorImpl::addAgent(const AgentBounds& agentBounds)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2023-01-17 23:31:17 +01:00
|
|
|
if (!isSupportedAgentBounds(mSettings.mRecast, agentBounds))
|
|
|
|
return false;
|
2022-06-17 00:28:44 +02:00
|
|
|
++mAgents[agentBounds];
|
|
|
|
mNavMeshManager.addAgent(agentBounds);
|
2023-01-17 23:31:17 +01:00
|
|
|
return true;
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:28:44 +02:00
|
|
|
void NavigatorImpl::removeAgent(const AgentBounds& agentBounds)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2022-06-17 00:28:44 +02:00
|
|
|
const auto it = mAgents.find(agentBounds);
|
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
|
|
|
}
|
|
|
|
|
2024-05-19 15:59:12 +02:00
|
|
|
void NavigatorImpl::updateBounds(ESM::RefId worldspace, const std::optional<CellGridBounds>& cellGridBounds,
|
|
|
|
const osg::Vec3f& playerPosition, const UpdateGuard* guard)
|
2022-12-01 20:02:39 +01:00
|
|
|
{
|
2024-05-19 15:59:12 +02:00
|
|
|
mNavMeshManager.updateBounds(worldspace, cellGridBounds, playerPosition, guard);
|
2022-02-01 01:21:47 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::addObject(
|
|
|
|
const ObjectId id, const ObjectShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
|
2021-09-18 15:52:03 +02:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
addObjectImpl(id, shapes, transform, guard);
|
2021-09-18 15:52:03 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
bool NavigatorImpl::addObjectImpl(
|
|
|
|
const ObjectId id, const ObjectShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
|
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);
|
2023-04-21 01:45:21 +02:00
|
|
|
bool result = mNavMeshManager.addObject(id, collisionShape, transform, AreaType_ground, guard);
|
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);
|
2023-04-21 01:45:21 +02:00
|
|
|
if (mNavMeshManager.addObject(avoidId, avoidCollisionShape, transform, AreaType_null, guard))
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
updateAvoidShapeId(id, avoidId, guard);
|
2018-07-12 11:44:11 +03:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2018-04-03 00:04:19 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::addObject(
|
|
|
|
const ObjectId id, const DoorShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
if (addObjectImpl(id, static_cast<const ObjectShapes&>(shapes), transform, guard))
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::updateObject(
|
|
|
|
const ObjectId id, const ObjectShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
|
2018-07-12 11:44:11 +03:00
|
|
|
{
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.updateObject(id, transform, AreaType_ground, guard);
|
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);
|
2023-04-21 01:45:21 +02:00
|
|
|
if (mNavMeshManager.updateObject(avoidId, transform, AreaType_null, guard))
|
2022-09-05 09:23:14 +02:00
|
|
|
updateAvoidShapeId(id, avoidId, guard);
|
2018-07-12 11:44:11 +03:00
|
|
|
}
|
2018-05-26 17:44:25 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::updateObject(
|
|
|
|
const ObjectId id, const DoorShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
|
2018-08-26 23:27:38 +03:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
return updateObject(id, static_cast<const ObjectShapes&>(shapes), transform, guard);
|
2018-08-26 23:27:38 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::removeObject(const ObjectId id, const UpdateGuard* guard)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.removeObject(id, guard);
|
2018-07-12 11:44:11 +03:00
|
|
|
const auto avoid = mAvoidIds.find(id);
|
2018-07-20 22:11:34 +03:00
|
|
|
if (avoid != mAvoidIds.end())
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.removeObject(avoid->second, guard);
|
2018-07-20 22:11:34 +03:00
|
|
|
const auto water = mWaterIds.find(id);
|
|
|
|
if (water != mWaterIds.end())
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.removeObject(water->second, guard);
|
2020-06-11 23:23:30 +02:00
|
|
|
mNavMeshManager.removeOffMeshConnections(id);
|
2018-07-20 22:11:34 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::addWater(const osg::Vec2i& cellPosition, int cellSize, float level, const UpdateGuard* guard)
|
2018-07-20 22:11:34 +03:00
|
|
|
{
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.addWater(cellPosition, cellSize, level, guard);
|
2018-07-20 22:11:34 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::removeWater(const osg::Vec2i& cellPosition, const UpdateGuard* guard)
|
2018-07-20 22:11:34 +03:00
|
|
|
{
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.removeWater(cellPosition, guard);
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::addHeightfield(
|
|
|
|
const osg::Vec2i& cellPosition, int cellSize, const HeightfieldShape& shape, const UpdateGuard* guard)
|
2021-07-14 20:57:52 +02:00
|
|
|
{
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.addHeightfield(cellPosition, cellSize, shape, guard);
|
2021-07-14 20:57:52 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::removeHeightfield(const osg::Vec2i& cellPosition, const UpdateGuard* guard)
|
2021-07-14 20:57:52 +02:00
|
|
|
{
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.removeHeightfield(cellPosition, guard);
|
2021-07-14 20:57:52 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 23:23:30 +02:00
|
|
|
void NavigatorImpl::addPathgrid(const ESM::Cell& cell, const ESM::Pathgrid& pathgrid)
|
|
|
|
{
|
2023-08-12 16:09:17 +02:00
|
|
|
const Misc::CoordinateConverter converter = Misc::makeCoordinateConverter(cell);
|
2022-08-20 19:17:45 +02:00
|
|
|
for (const auto& edge : pathgrid.mEdges)
|
2020-06-11 23:23:30 +02:00
|
|
|
{
|
|
|
|
const auto src = Misc::Convert::makeOsgVec3f(converter.toWorldPoint(pathgrid.mPoints[edge.mV0]));
|
|
|
|
const auto dst = Misc::Convert::makeOsgVec3f(converter.toWorldPoint(pathgrid.mPoints[edge.mV1]));
|
2021-11-06 13:46:43 +01:00
|
|
|
mNavMeshManager.addOffMeshConnection(ObjectId(&pathgrid), toNavMeshCoordinates(mSettings.mRecast, src),
|
|
|
|
toNavMeshCoordinates(mSettings.mRecast, dst), AreaType_pathgrid);
|
2020-06-11 23:23:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigatorImpl::removePathgrid(const ESM::Pathgrid& pathgrid)
|
|
|
|
{
|
|
|
|
mNavMeshManager.removeOffMeshConnections(ObjectId(&pathgrid));
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::update(const osg::Vec3f& playerPosition, const UpdateGuard* guard)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2019-03-10 14:36:16 +03:00
|
|
|
removeUnusedNavMeshes();
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.update(playerPosition, guard);
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::wait(WaitConditionType waitConditionType, Loading::Listener* listener)
|
2018-03-14 01:49:08 +03:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
mNavMeshManager.wait(waitConditionType, listener);
|
2018-03-14 01:49:08 +03:00
|
|
|
}
|
2018-04-07 16:11:23 +03:00
|
|
|
|
2022-06-17 00:28:44 +02:00
|
|
|
SharedNavMeshCacheItem NavigatorImpl::getNavMesh(const AgentBounds& agentBounds) const
|
2019-02-16 15:14:05 +03:00
|
|
|
{
|
2022-06-17 00:28:44 +02:00
|
|
|
return mNavMeshManager.getNavMesh(agentBounds);
|
2019-02-16 15:14:05 +03:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:28:44 +02:00
|
|
|
std::map<AgentBounds, 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
|
|
|
|
2022-08-28 14:52:12 +02:00
|
|
|
Stats NavigatorImpl::getStats() const
|
2019-03-17 20:18:53 +03:00
|
|
|
{
|
2022-08-28 14:52:12 +02:00
|
|
|
return mNavMeshManager.getStats();
|
2019-03-17 20:18:53 +03:00
|
|
|
}
|
|
|
|
|
2021-10-09 18:36:37 +02:00
|
|
|
RecastMeshTiles NavigatorImpl::getRecastMeshTiles() const
|
2019-11-27 23:45:01 +01:00
|
|
|
{
|
|
|
|
return mNavMeshManager.getRecastMeshTiles();
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::updateAvoidShapeId(const ObjectId id, const ObjectId avoidId, const UpdateGuard* guard)
|
2018-07-20 22:11:34 +03:00
|
|
|
{
|
2022-09-05 09:23:14 +02:00
|
|
|
updateId(id, avoidId, mWaterIds, guard);
|
2018-07-20 22:11:34 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:23:14 +02:00
|
|
|
void NavigatorImpl::updateId(const ObjectId id, const ObjectId updateId,
|
|
|
|
std::unordered_map<ObjectId, ObjectId>& ids, const UpdateGuard* guard)
|
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)
|
|
|
|
{
|
2023-04-21 01:45:21 +02:00
|
|
|
mNavMeshManager.removeObject(inserted.first->second, guard);
|
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
|
|
|
}
|