1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 20:49:56 +00:00
openmw-tes3mp/components/detournavigator/navigator.cpp

155 lines
4.8 KiB
C++
Raw Normal View History

2018-03-13 22:49:08 +00:00
#include "navigator.hpp"
#include "debug.hpp"
#include "settingsutils.hpp"
2018-07-12 08:44:11 +00:00
#include <Recast.h>
2018-03-13 22:49:08 +00:00
namespace DetourNavigator
{
Navigator::Navigator(const Settings& settings)
: mSettings(settings)
, mNavMeshManager(mSettings)
{
}
void Navigator::addAgent(const osg::Vec3f& agentHalfExtents)
{
++mAgents[agentHalfExtents];
mNavMeshManager.addAgent(agentHalfExtents);
2018-03-13 22:49:08 +00:00
}
void Navigator::removeAgent(const osg::Vec3f& agentHalfExtents)
{
const auto it = mAgents.find(agentHalfExtents);
if (it == mAgents.end() || --it->second)
return;
mAgents.erase(it);
mNavMeshManager.reset(agentHalfExtents);
}
2018-09-22 15:36:57 +00:00
bool Navigator::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
}
2018-09-22 15:36:57 +00:00
bool Navigator::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
}
2018-09-22 15:36:57 +00:00
bool Navigator::addObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
{
if (addObject(id, static_cast<const ObjectShapes&>(shapes), transform))
{
mNavMeshManager.addOffMeshConnection(
id,
toNavMeshCoordinates(mSettings, shapes.mConnectionStart),
toNavMeshCoordinates(mSettings, shapes.mConnectionEnd)
);
return true;
}
return false;
}
2018-09-22 15:36:57 +00:00
bool Navigator::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
}
2018-09-22 15:36:57 +00:00
bool Navigator::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
}
2018-09-22 15:36:57 +00:00
bool Navigator::updateObject(const ObjectId id, const DoorShapes& shapes, const btTransform& transform)
{
return updateObject(id, static_cast<const ObjectShapes&>(shapes), transform);
}
2018-09-22 15:36:57 +00:00
bool Navigator::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;
mNavMeshManager.removeOffMeshConnection(id);
2018-07-20 19:11:34 +00:00
return result;
}
bool Navigator::addWater(const osg::Vec2i& cellPosition, const int cellSize, const btScalar level,
const btTransform& transform)
{
return mNavMeshManager.addWater(cellPosition, cellSize,
btTransform(transform.getBasis(), btVector3(transform.getOrigin().x(), transform.getOrigin().y(), level)));
}
bool Navigator::removeWater(const osg::Vec2i& cellPosition)
{
return mNavMeshManager.removeWater(cellPosition);
2018-03-13 22:49:08 +00:00
}
void Navigator::update(const osg::Vec3f& playerPosition)
2018-03-13 22:49:08 +00:00
{
for (const auto& v : mAgents)
mNavMeshManager.update(playerPosition, v.first);
2018-03-13 22:49:08 +00:00
}
void Navigator::wait()
{
mNavMeshManager.wait();
}
std::map<osg::Vec3f, SharedNavMeshCacheItem> Navigator::getNavMeshes() const
{
return mNavMeshManager.getNavMeshes();
}
const Settings& Navigator::getSettings() const
{
return mSettings;
}
2018-07-12 08:44:11 +00:00
2018-09-22 15:36:57 +00:00
void Navigator::updateAvoidShapeId(const ObjectId id, const ObjectId avoidId)
2018-07-20 19:11:34 +00:00
{
updateId(id, avoidId, mWaterIds);
}
2018-09-22 15:36:57 +00:00
void Navigator::updateWaterShapeId(const ObjectId id, const ObjectId waterId)
2018-07-20 19:11:34 +00:00
{
updateId(id, waterId, mWaterIds);
}
2018-09-22 15:36:57 +00:00
void Navigator::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
}
}
2018-03-13 22:49:08 +00:00
}