mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-22 19:39:42 +00:00
Actors may have different collision shapes. Currently there are axis-aligned bounding boxes and rotating bounding boxes. With AABB it's required to use bounding cylinder for navmesh agent to avoid providing paths where actor can't pass. But for rotating bounding boxes cylinder with diameter equal to the front face width should be used to not reduce of available paths. For example rats have rotating bounding box as collision shape because of the difference between front and side faces width. * Add agent bounds to navmesh tile db cache key. This is required to distinguish tiles for agents with different bounds. * Increase navmesh version because navmesh tile db cache key and data has changed. * Move navmesh version to the code to avoid misconfiguration by users. * Fix all places where wrong half extents were used for pathfinding.
102 lines
3.3 KiB
C++
102 lines
3.3 KiB
C++
#include "generatenavmeshtile.hpp"
|
|
|
|
#include "dbrefgeometryobject.hpp"
|
|
#include "makenavmesh.hpp"
|
|
#include "offmeshconnectionsmanager.hpp"
|
|
#include "preparednavmeshdata.hpp"
|
|
#include "serialization.hpp"
|
|
#include "settings.hpp"
|
|
#include "tilecachedrecastmeshmanager.hpp"
|
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
#include <osg/Vec3f>
|
|
#include <osg/io_utils>
|
|
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
#include <optional>
|
|
#include <functional>
|
|
|
|
namespace DetourNavigator
|
|
{
|
|
namespace
|
|
{
|
|
struct Ignore
|
|
{
|
|
std::string_view mWorldspace;
|
|
const TilePosition& mTilePosition;
|
|
std::shared_ptr<NavMeshTileConsumer> mConsumer;
|
|
|
|
~Ignore() noexcept
|
|
{
|
|
if (mConsumer != nullptr)
|
|
mConsumer->ignore(mWorldspace, mTilePosition);
|
|
}
|
|
};
|
|
}
|
|
|
|
GenerateNavMeshTile::GenerateNavMeshTile(std::string worldspace, const TilePosition& tilePosition,
|
|
RecastMeshProvider recastMeshProvider, const AgentBounds& agentBounds,
|
|
const DetourNavigator::Settings& settings, std::weak_ptr<NavMeshTileConsumer> consumer)
|
|
: mWorldspace(std::move(worldspace))
|
|
, mTilePosition(tilePosition)
|
|
, mRecastMeshProvider(recastMeshProvider)
|
|
, mAgentBounds(agentBounds)
|
|
, mSettings(settings)
|
|
, mConsumer(std::move(consumer)) {}
|
|
|
|
void GenerateNavMeshTile::doWork()
|
|
{
|
|
impl();
|
|
}
|
|
|
|
void GenerateNavMeshTile::impl() noexcept
|
|
{
|
|
const auto consumer = mConsumer.lock();
|
|
|
|
if (consumer == nullptr)
|
|
return;
|
|
|
|
try
|
|
{
|
|
Ignore ignore {mWorldspace, mTilePosition, consumer};
|
|
|
|
const std::shared_ptr<RecastMesh> recastMesh = mRecastMeshProvider.getMesh(mWorldspace, mTilePosition);
|
|
|
|
if (recastMesh == nullptr || isEmpty(*recastMesh))
|
|
return;
|
|
|
|
const std::vector<DbRefGeometryObject> objects = makeDbRefGeometryObjects(recastMesh->getMeshSources(),
|
|
[&] (const MeshSource& v) { return consumer->resolveMeshSource(v); });
|
|
std::vector<std::byte> input = serialize(mSettings.mRecast, mAgentBounds, *recastMesh, objects);
|
|
const std::optional<NavMeshTileInfo> info = consumer->find(mWorldspace, mTilePosition, input);
|
|
|
|
if (info.has_value() && info->mVersion == navMeshVersion)
|
|
{
|
|
consumer->identity(mWorldspace, mTilePosition, info->mTileId);
|
|
ignore.mConsumer = nullptr;
|
|
return;
|
|
}
|
|
|
|
const auto data = prepareNavMeshTileData(*recastMesh, mTilePosition, mAgentBounds, mSettings.mRecast);
|
|
|
|
if (data == nullptr)
|
|
return;
|
|
|
|
if (info.has_value())
|
|
consumer->update(mWorldspace, mTilePosition, info->mTileId, navMeshVersion, *data);
|
|
else
|
|
consumer->insert(mWorldspace, mTilePosition, navMeshVersion, input, *data);
|
|
|
|
ignore.mConsumer = nullptr;
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
Log(Debug::Warning) << "Failed to generate navmesh for worldspace \"" << mWorldspace
|
|
<< "\" tile " << mTilePosition << ": " << e.what();
|
|
consumer->cancel(e.what());
|
|
}
|
|
}
|
|
}
|