1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 18:59:57 +00:00
openmw/components/detournavigator/navigatorimpl.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

216 lines
7.8 KiB
C++
Raw Normal View History

2019-02-16 12:27:02 +00:00
#include "navigatorimpl.hpp"
#include "makenavmesh.hpp"
2018-03-13 22:49:08 +00:00
#include "settingsutils.hpp"
#include "stats.hpp"
2018-03-13 22:49:08 +00:00
#include <components/esm3/loadpgrd.hpp>
2022-01-25 14:06:53 +00:00
#include <components/misc/convert.hpp>
#include <components/misc/coordinateconverter.hpp>
2018-03-13 22:49:08 +00:00
namespace DetourNavigator
{
NavigatorImpl::NavigatorImpl(const Settings& settings, std::unique_ptr<NavMeshDb>&& db)
2018-03-13 22:49:08 +00:00
: mSettings(settings)
, mNavMeshManager(mSettings, std::move(db))
2018-03-13 22:49:08 +00:00
{
}
bool NavigatorImpl::addAgent(const AgentBounds& agentBounds)
2018-03-13 22:49:08 +00:00
{
if (!isSupportedAgentBounds(mSettings.mRecast, agentBounds))
return false;
++mAgents[agentBounds];
mNavMeshManager.addAgent(agentBounds);
return true;
2018-03-13 22:49:08 +00:00
}
void NavigatorImpl::removeAgent(const AgentBounds& agentBounds)
2018-03-13 22:49:08 +00:00
{
const auto it = mAgents.find(agentBounds);
if (it == mAgents.end())
2018-03-13 22:49:08 +00:00
return;
if (it->second > 0)
--it->second;
2018-03-13 22:49:08 +00:00
}
void NavigatorImpl::updateBounds(ESM::RefId worldspace, const std::optional<CellGridBounds>& cellGridBounds,
const osg::Vec3f& playerPosition, const UpdateGuard* guard)
{
mNavMeshManager.updateBounds(worldspace, cellGridBounds, playerPosition, guard);
}
void NavigatorImpl::addObject(
const ObjectId id, const ObjectShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
{
addObjectImpl(id, shapes, transform, guard);
}
bool NavigatorImpl::addObjectImpl(
const ObjectId id, const ObjectShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
2018-07-12 08:44:11 +00:00
{
2021-11-04 00:57:27 +00:00
const CollisionShape collisionShape(
shapes.mShapeInstance, *shapes.mShapeInstance->mCollisionShape, shapes.mTransform);
bool result = mNavMeshManager.addObject(id, collisionShape, transform, AreaType_ground, guard);
2021-10-30 01:30:38 +00:00
if (const btCollisionShape* const avoidShape = shapes.mShapeInstance->mAvoidCollisionShape.get())
2018-07-12 08:44:11 +00:00
{
const ObjectId avoidId(avoidShape);
2021-11-04 00:57:27 +00:00
const CollisionShape avoidCollisionShape(shapes.mShapeInstance, *avoidShape, shapes.mTransform);
if (mNavMeshManager.addObject(avoidId, avoidCollisionShape, transform, AreaType_null, guard))
2018-07-12 08:44:11 +00:00
{
updateAvoidShapeId(id, avoidId, guard);
2018-07-12 08:44:11 +00:00
result = true;
}
}
return result;
2018-04-02 21:04:19 +00:00
}
void NavigatorImpl::addObject(
const ObjectId id, const DoorShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
{
if (addObjectImpl(id, static_cast<const ObjectShapes&>(shapes), transform, guard))
{
const osg::Vec3f start = toNavMeshCoordinates(mSettings.mRecast, shapes.mConnectionStart);
const osg::Vec3f end = toNavMeshCoordinates(mSettings.mRecast, shapes.mConnectionEnd);
mNavMeshManager.addOffMeshConnection(id, start, end, AreaType_door);
mNavMeshManager.addOffMeshConnection(id, end, start, AreaType_door);
}
}
void NavigatorImpl::updateObject(
const ObjectId id, const ObjectShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
2018-07-12 08:44:11 +00:00
{
mNavMeshManager.updateObject(id, transform, AreaType_ground, guard);
2021-10-30 01:30:38 +00:00
if (const btCollisionShape* const avoidShape = shapes.mShapeInstance->mAvoidCollisionShape.get())
2018-07-12 08:44:11 +00:00
{
const ObjectId avoidId(avoidShape);
if (mNavMeshManager.updateObject(avoidId, transform, AreaType_null, guard))
updateAvoidShapeId(id, avoidId, guard);
2018-07-12 08:44:11 +00:00
}
2018-05-26 14:44:25 +00:00
}
void NavigatorImpl::updateObject(
const ObjectId id, const DoorShapes& shapes, const btTransform& transform, const UpdateGuard* guard)
{
return updateObject(id, static_cast<const ObjectShapes&>(shapes), transform, guard);
}
void NavigatorImpl::removeObject(const ObjectId id, const UpdateGuard* guard)
2018-03-13 22:49:08 +00:00
{
mNavMeshManager.removeObject(id, guard);
2018-07-12 08:44:11 +00:00
const auto avoid = mAvoidIds.find(id);
2018-07-20 19:11:34 +00:00
if (avoid != mAvoidIds.end())
mNavMeshManager.removeObject(avoid->second, guard);
2018-07-20 19:11:34 +00:00
const auto water = mWaterIds.find(id);
if (water != mWaterIds.end())
mNavMeshManager.removeObject(water->second, guard);
mNavMeshManager.removeOffMeshConnections(id);
2018-07-20 19:11:34 +00:00
}
void NavigatorImpl::addWater(const osg::Vec2i& cellPosition, int cellSize, float level, const UpdateGuard* guard)
2018-07-20 19:11:34 +00:00
{
mNavMeshManager.addWater(cellPosition, cellSize, level, guard);
2018-07-20 19:11:34 +00:00
}
void NavigatorImpl::removeWater(const osg::Vec2i& cellPosition, const UpdateGuard* guard)
2018-07-20 19:11:34 +00:00
{
mNavMeshManager.removeWater(cellPosition, guard);
2018-03-13 22:49:08 +00:00
}
void NavigatorImpl::addHeightfield(
const osg::Vec2i& cellPosition, int cellSize, const HeightfieldShape& shape, const UpdateGuard* guard)
{
mNavMeshManager.addHeightfield(cellPosition, cellSize, shape, guard);
}
void NavigatorImpl::removeHeightfield(const osg::Vec2i& cellPosition, const UpdateGuard* guard)
{
mNavMeshManager.removeHeightfield(cellPosition, guard);
}
void NavigatorImpl::addPathgrid(const ESM::Cell& cell, const ESM::Pathgrid& pathgrid)
{
const Misc::CoordinateConverter converter = Misc::makeCoordinateConverter(cell);
2022-08-20 17:17:45 +00:00
for (const 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), toNavMeshCoordinates(mSettings.mRecast, src),
toNavMeshCoordinates(mSettings.mRecast, dst), AreaType_pathgrid);
}
}
void NavigatorImpl::removePathgrid(const ESM::Pathgrid& pathgrid)
{
mNavMeshManager.removeOffMeshConnections(ObjectId(&pathgrid));
}
void NavigatorImpl::update(const osg::Vec3f& playerPosition, const UpdateGuard* guard)
2018-03-13 22:49:08 +00:00
{
removeUnusedNavMeshes();
mNavMeshManager.update(playerPosition, guard);
2018-03-13 22:49:08 +00:00
}
void NavigatorImpl::wait(WaitConditionType waitConditionType, Loading::Listener* listener)
2018-03-13 22:49:08 +00:00
{
mNavMeshManager.wait(waitConditionType, listener);
2018-03-13 22:49:08 +00:00
}
SharedNavMeshCacheItem NavigatorImpl::getNavMesh(const AgentBounds& agentBounds) const
{
return mNavMeshManager.getNavMesh(agentBounds);
}
std::map<AgentBounds, SharedNavMeshCacheItem> NavigatorImpl::getNavMeshes() const
{
return mNavMeshManager.getNavMeshes();
}
const Settings& NavigatorImpl::getSettings() const
{
return mSettings;
}
2018-07-12 08:44:11 +00:00
Stats NavigatorImpl::getStats() const
2019-03-17 17:18:53 +00:00
{
return mNavMeshManager.getStats();
2019-03-17 17:18:53 +00:00
}
RecastMeshTiles NavigatorImpl::getRecastMeshTiles() const
2019-11-27 22:45:01 +00:00
{
return mNavMeshManager.getRecastMeshTiles();
}
void NavigatorImpl::updateAvoidShapeId(const ObjectId id, const ObjectId avoidId, const UpdateGuard* guard)
2018-07-20 19:11:34 +00:00
{
updateId(id, avoidId, mWaterIds, guard);
2018-07-20 19:11:34 +00:00
}
void NavigatorImpl::updateId(const ObjectId id, const ObjectId updateId,
std::unordered_map<ObjectId, ObjectId>& ids, const UpdateGuard* guard)
2018-07-12 08:44:11 +00:00
{
2018-07-20 19:11:34 +00:00
auto inserted = ids.insert(std::make_pair(id, updateId));
2018-07-12 08:44:11 +00:00
if (!inserted.second)
{
mNavMeshManager.removeObject(inserted.first->second, guard);
2018-09-22 15:36:57 +00:00
inserted.first->second = updateId;
2018-07-12 08:44:11 +00: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;
}
}
float NavigatorImpl::getMaxNavmeshAreaRealRadius() const
{
const auto& settings = getSettings();
return getRealTileSize(settings.mRecast) * getMaxNavmeshAreaRadius(settings);
}
2018-03-13 22:49:08 +00:00
}