2019-02-16 12:27:02 +00:00
|
|
|
#include "navigatorimpl.hpp"
|
2018-03-13 22:49:08 +00:00
|
|
|
#include "debug.hpp"
|
|
|
|
#include "settingsutils.hpp"
|
|
|
|
|
2020-06-11 21:23:30 +00:00
|
|
|
#include <components/esm/loadpgrd.hpp>
|
|
|
|
#include <components/misc/coordinateconverter.hpp>
|
|
|
|
|
2018-07-12 08:44:11 +00:00
|
|
|
#include <Recast.h>
|
|
|
|
|
2018-03-13 22:49:08 +00:00
|
|
|
namespace DetourNavigator
|
|
|
|
{
|
2019-02-16 12:27:02 +00:00
|
|
|
NavigatorImpl::NavigatorImpl(const Settings& settings)
|
2018-03-13 22:49:08 +00:00
|
|
|
: mSettings(settings)
|
|
|
|
, mNavMeshManager(mSettings)
|
2020-05-08 13:37:00 +00:00
|
|
|
, mUpdatesEnabled(true)
|
2018-03-13 22:49:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
void NavigatorImpl::addAgent(const osg::Vec3f& agentHalfExtents)
|
2018-03-13 22:49:08 +00:00
|
|
|
{
|
2021-03-07 19:58:09 +00:00
|
|
|
if(agentHalfExtents.length2() <= 0)
|
|
|
|
return;
|
2018-03-13 22:49:08 +00:00
|
|
|
++mAgents[agentHalfExtents];
|
2018-04-01 17:24:02 +00:00
|
|
|
mNavMeshManager.addAgent(agentHalfExtents);
|
2018-03-13 22:49:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
void NavigatorImpl::removeAgent(const osg::Vec3f& agentHalfExtents)
|
2018-03-13 22:49:08 +00:00
|
|
|
{
|
|
|
|
const auto it = mAgents.find(agentHalfExtents);
|
2019-03-10 11:36:16 +00:00
|
|
|
if (it == mAgents.end())
|
2018-03-13 22:49:08 +00:00
|
|
|
return;
|
2019-03-10 11:36:16 +00:00
|
|
|
if (it->second > 0)
|
|
|
|
--it->second;
|
2018-03-13 22:49:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::addObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform)
|
2018-04-02 21:04:19 +00:00
|
|
|
{
|
2018-07-18 19:09:50 +00:00
|
|
|
return mNavMeshManager.addObject(id, shape, transform, AreaType_ground);
|
2018-07-12 08:44:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::addObject(const ObjectId id, const ObjectShapes& shapes, const btTransform& transform)
|
2018-07-12 08:44:11 +00:00
|
|
|
{
|
|
|
|
bool result = addObject(id, shapes.mShape, transform);
|
|
|
|
if (shapes.mAvoid)
|
|
|
|
{
|
2018-09-22 15:36:57 +00:00
|
|
|
const ObjectId avoidId(shapes.mAvoid);
|
2018-07-18 19:09:50 +00:00
|
|
|
if (mNavMeshManager.addObject(avoidId, *shapes.mAvoid, transform, AreaType_null))
|
2018-07-12 08:44:11 +00:00
|
|
|
{
|
|
|
|
updateAvoidShapeId(id, avoidId);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2018-04-02 21:04:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::addObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
|
2018-08-26 20:27:38 +00:00
|
|
|
{
|
|
|
|
if (addObject(id, static_cast<const ObjectShapes&>(shapes), transform))
|
|
|
|
{
|
|
|
|
mNavMeshManager.addOffMeshConnection(
|
|
|
|
id,
|
|
|
|
toNavMeshCoordinates(mSettings, shapes.mConnectionStart),
|
2020-06-11 21:23:30 +00:00
|
|
|
toNavMeshCoordinates(mSettings, shapes.mConnectionEnd),
|
|
|
|
AreaType_door
|
2018-08-26 20:27:38 +00:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::updateObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform)
|
2018-05-26 14:44:25 +00:00
|
|
|
{
|
2018-07-18 19:09:50 +00:00
|
|
|
return mNavMeshManager.updateObject(id, shape, transform, AreaType_ground);
|
2018-07-12 08:44:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::updateObject(const ObjectId id, const ObjectShapes& shapes, const btTransform& transform)
|
2018-07-12 08:44:11 +00:00
|
|
|
{
|
|
|
|
bool result = updateObject(id, shapes.mShape, transform);
|
|
|
|
if (shapes.mAvoid)
|
|
|
|
{
|
2018-09-22 15:36:57 +00:00
|
|
|
const ObjectId avoidId(shapes.mAvoid);
|
2018-07-18 19:09:50 +00:00
|
|
|
if (mNavMeshManager.updateObject(avoidId, *shapes.mAvoid, transform, AreaType_null))
|
2018-07-12 08:44:11 +00:00
|
|
|
{
|
|
|
|
updateAvoidShapeId(id, avoidId);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2018-05-26 14:44:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::updateObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
|
2018-08-26 20:27:38 +00:00
|
|
|
{
|
|
|
|
return updateObject(id, static_cast<const ObjectShapes&>(shapes), transform);
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::removeObject(const ObjectId id)
|
2018-03-13 22:49:08 +00:00
|
|
|
{
|
2018-07-12 08:44:11 +00:00
|
|
|
bool result = mNavMeshManager.removeObject(id);
|
|
|
|
const auto avoid = mAvoidIds.find(id);
|
2018-07-20 19:11:34 +00: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 21:23:30 +00:00
|
|
|
mNavMeshManager.removeOffMeshConnections(id);
|
2018-07-20 19:11:34 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::addWater(const osg::Vec2i& cellPosition, const int cellSize, const btScalar level,
|
2018-07-20 19:11:34 +00:00
|
|
|
const btTransform& transform)
|
|
|
|
{
|
|
|
|
return mNavMeshManager.addWater(cellPosition, cellSize,
|
|
|
|
btTransform(transform.getBasis(), btVector3(transform.getOrigin().x(), transform.getOrigin().y(), level)));
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
bool NavigatorImpl::removeWater(const osg::Vec2i& cellPosition)
|
2018-07-20 19:11:34 +00:00
|
|
|
{
|
|
|
|
return mNavMeshManager.removeWater(cellPosition);
|
2018-03-13 22:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:23:30 +00: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),
|
|
|
|
toNavMeshCoordinates(mSettings, src),
|
|
|
|
toNavMeshCoordinates(mSettings, dst),
|
|
|
|
AreaType_pathgrid
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigatorImpl::removePathgrid(const ESM::Pathgrid& pathgrid)
|
|
|
|
{
|
|
|
|
mNavMeshManager.removeOffMeshConnections(ObjectId(&pathgrid));
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
void NavigatorImpl::update(const osg::Vec3f& playerPosition)
|
2018-03-13 22:49:08 +00:00
|
|
|
{
|
2020-05-08 13:37:00 +00:00
|
|
|
if (!mUpdatesEnabled)
|
|
|
|
return;
|
2019-03-10 11:36:16 +00:00
|
|
|
removeUnusedNavMeshes();
|
2018-03-13 22:49:08 +00:00
|
|
|
for (const auto& v : mAgents)
|
2018-04-01 17:24:02 +00:00
|
|
|
mNavMeshManager.update(playerPosition, v.first);
|
2018-03-13 22:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 13:37:00 +00:00
|
|
|
void NavigatorImpl::setUpdatesEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
mUpdatesEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
void NavigatorImpl::wait()
|
2018-03-13 22:49:08 +00:00
|
|
|
{
|
|
|
|
mNavMeshManager.wait();
|
|
|
|
}
|
2018-04-07 13:11:23 +00:00
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
SharedNavMeshCacheItem NavigatorImpl::getNavMesh(const osg::Vec3f& agentHalfExtents) const
|
2019-02-16 12:14:05 +00:00
|
|
|
{
|
|
|
|
return mNavMeshManager.getNavMesh(agentHalfExtents);
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
std::map<osg::Vec3f, SharedNavMeshCacheItem> NavigatorImpl::getNavMeshes() const
|
2018-04-07 13:11:23 +00:00
|
|
|
{
|
|
|
|
return mNavMeshManager.getNavMeshes();
|
|
|
|
}
|
|
|
|
|
2019-02-19 08:46:00 +00:00
|
|
|
const Settings& NavigatorImpl::getSettings() const
|
2018-04-07 13:11:23 +00:00
|
|
|
{
|
|
|
|
return mSettings;
|
|
|
|
}
|
2018-07-12 08:44:11 +00:00
|
|
|
|
2019-03-17 17:18:53 +00:00
|
|
|
void NavigatorImpl::reportStats(unsigned int frameNumber, osg::Stats& stats) const
|
|
|
|
{
|
|
|
|
mNavMeshManager.reportStats(frameNumber, stats);
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:45:01 +00:00
|
|
|
RecastMeshTiles NavigatorImpl::getRecastMeshTiles()
|
|
|
|
{
|
|
|
|
return mNavMeshManager.getRecastMeshTiles();
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
void NavigatorImpl::updateAvoidShapeId(const ObjectId id, const ObjectId avoidId)
|
2018-07-20 19:11:34 +00:00
|
|
|
{
|
|
|
|
updateId(id, avoidId, mWaterIds);
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
void NavigatorImpl::updateWaterShapeId(const ObjectId id, const ObjectId waterId)
|
2018-07-20 19:11:34 +00:00
|
|
|
{
|
|
|
|
updateId(id, waterId, mWaterIds);
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:27:02 +00:00
|
|
|
void NavigatorImpl::updateId(const ObjectId id, const ObjectId updateId, std::unordered_map<ObjectId, ObjectId>& ids)
|
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);
|
2018-09-22 15:36:57 +00:00
|
|
|
inserted.first->second = updateId;
|
2018-07-12 08:44:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-10 11:36:16 +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;
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 22:49:08 +00:00
|
|
|
}
|