You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.1 KiB
C++
160 lines
5.1 KiB
C++
6 years ago
|
#include "navigatorimpl.hpp"
|
||
7 years ago
|
#include "debug.hpp"
|
||
|
#include "settingsutils.hpp"
|
||
|
|
||
7 years ago
|
#include <Recast.h>
|
||
|
|
||
7 years ago
|
namespace DetourNavigator
|
||
|
{
|
||
6 years ago
|
NavigatorImpl::NavigatorImpl(const Settings& settings)
|
||
7 years ago
|
: mSettings(settings)
|
||
|
, mNavMeshManager(mSettings)
|
||
|
{
|
||
|
}
|
||
|
|
||
6 years ago
|
void NavigatorImpl::addAgent(const osg::Vec3f& agentHalfExtents)
|
||
7 years ago
|
{
|
||
|
++mAgents[agentHalfExtents];
|
||
7 years ago
|
mNavMeshManager.addAgent(agentHalfExtents);
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
void NavigatorImpl::removeAgent(const osg::Vec3f& agentHalfExtents)
|
||
7 years ago
|
{
|
||
|
const auto it = mAgents.find(agentHalfExtents);
|
||
|
if (it == mAgents.end() || --it->second)
|
||
|
return;
|
||
|
mAgents.erase(it);
|
||
|
mNavMeshManager.reset(agentHalfExtents);
|
||
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::addObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform)
|
||
7 years ago
|
{
|
||
7 years ago
|
return mNavMeshManager.addObject(id, shape, transform, AreaType_ground);
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::addObject(const ObjectId id, const ObjectShapes& shapes, const btTransform& transform)
|
||
7 years ago
|
{
|
||
|
bool result = addObject(id, shapes.mShape, transform);
|
||
|
if (shapes.mAvoid)
|
||
|
{
|
||
6 years ago
|
const ObjectId avoidId(shapes.mAvoid);
|
||
7 years ago
|
if (mNavMeshManager.addObject(avoidId, *shapes.mAvoid, transform, AreaType_null))
|
||
7 years ago
|
{
|
||
|
updateAvoidShapeId(id, avoidId);
|
||
|
result = true;
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::addObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
|
||
6 years ago
|
{
|
||
|
if (addObject(id, static_cast<const ObjectShapes&>(shapes), transform))
|
||
|
{
|
||
|
mNavMeshManager.addOffMeshConnection(
|
||
|
id,
|
||
|
toNavMeshCoordinates(mSettings, shapes.mConnectionStart),
|
||
|
toNavMeshCoordinates(mSettings, shapes.mConnectionEnd)
|
||
|
);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::updateObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform)
|
||
7 years ago
|
{
|
||
7 years ago
|
return mNavMeshManager.updateObject(id, shape, transform, AreaType_ground);
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::updateObject(const ObjectId id, const ObjectShapes& shapes, const btTransform& transform)
|
||
7 years ago
|
{
|
||
|
bool result = updateObject(id, shapes.mShape, transform);
|
||
|
if (shapes.mAvoid)
|
||
|
{
|
||
6 years ago
|
const ObjectId avoidId(shapes.mAvoid);
|
||
7 years ago
|
if (mNavMeshManager.updateObject(avoidId, *shapes.mAvoid, transform, AreaType_null))
|
||
7 years ago
|
{
|
||
|
updateAvoidShapeId(id, avoidId);
|
||
|
result = true;
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::updateObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
|
||
6 years ago
|
{
|
||
|
return updateObject(id, static_cast<const ObjectShapes&>(shapes), transform);
|
||
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::removeObject(const ObjectId id)
|
||
7 years ago
|
{
|
||
7 years ago
|
bool result = mNavMeshManager.removeObject(id);
|
||
|
const auto avoid = mAvoidIds.find(id);
|
||
7 years ago
|
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;
|
||
6 years ago
|
mNavMeshManager.removeOffMeshConnection(id);
|
||
7 years ago
|
return result;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::addWater(const osg::Vec2i& cellPosition, const int cellSize, const btScalar level,
|
||
7 years ago
|
const btTransform& transform)
|
||
|
{
|
||
|
return mNavMeshManager.addWater(cellPosition, cellSize,
|
||
|
btTransform(transform.getBasis(), btVector3(transform.getOrigin().x(), transform.getOrigin().y(), level)));
|
||
|
}
|
||
|
|
||
6 years ago
|
bool NavigatorImpl::removeWater(const osg::Vec2i& cellPosition)
|
||
7 years ago
|
{
|
||
|
return mNavMeshManager.removeWater(cellPosition);
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
void NavigatorImpl::update(const osg::Vec3f& playerPosition)
|
||
7 years ago
|
{
|
||
|
for (const auto& v : mAgents)
|
||
7 years ago
|
mNavMeshManager.update(playerPosition, v.first);
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
void NavigatorImpl::wait()
|
||
7 years ago
|
{
|
||
|
mNavMeshManager.wait();
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
SharedNavMeshCacheItem NavigatorImpl::getNavMesh(const osg::Vec3f& agentHalfExtents) const
|
||
6 years ago
|
{
|
||
|
return mNavMeshManager.getNavMesh(agentHalfExtents);
|
||
|
}
|
||
|
|
||
6 years ago
|
std::map<osg::Vec3f, SharedNavMeshCacheItem> NavigatorImpl::getNavMeshes() const
|
||
7 years ago
|
{
|
||
|
return mNavMeshManager.getNavMeshes();
|
||
|
}
|
||
|
|
||
6 years ago
|
Settings NavigatorImpl::getSettings() const
|
||
7 years ago
|
{
|
||
|
return mSettings;
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
void NavigatorImpl::updateAvoidShapeId(const ObjectId id, const ObjectId avoidId)
|
||
7 years ago
|
{
|
||
|
updateId(id, avoidId, mWaterIds);
|
||
|
}
|
||
|
|
||
6 years ago
|
void NavigatorImpl::updateWaterShapeId(const ObjectId id, const ObjectId waterId)
|
||
7 years ago
|
{
|
||
|
updateId(id, waterId, mWaterIds);
|
||
|
}
|
||
|
|
||
6 years ago
|
void NavigatorImpl::updateId(const ObjectId id, const ObjectId updateId, std::unordered_map<ObjectId, ObjectId>& ids)
|
||
7 years ago
|
{
|
||
7 years ago
|
auto inserted = ids.insert(std::make_pair(id, updateId));
|
||
7 years ago
|
if (!inserted.second)
|
||
|
{
|
||
|
mNavMeshManager.removeObject(inserted.first->second);
|
||
6 years ago
|
inserted.first->second = updateId;
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
}
|