1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-28 20:15:32 +00:00
openmw-tes3mp/components/detournavigator/asyncnavmeshupdater.hpp

90 lines
2.3 KiB
C++
Raw Normal View History

2018-03-13 22:49:08 +00:00
#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_ASYNCNAVMESHUPDATER_H
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_ASYNCNAVMESHUPDATER_H
#include "recastmesh.hpp"
#include "sharednavmesh.hpp"
#include "tileposition.hpp"
2018-03-13 22:49:08 +00:00
#include <osg/Vec3f>
#include <boost/optional.hpp>
#include <atomic>
#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <queue>
#include <set>
2018-03-13 22:49:08 +00:00
#include <thread>
class dtNavMesh;
namespace DetourNavigator
{
struct NavMeshCacheItem
{
SharedNavMesh mValue;
std::size_t mRecastMeshRevision;
std::atomic_size_t mNavMeshRevision;
NavMeshCacheItem(const NavMeshPtr& value, std::size_t revision)
: mValue(value), mRecastMeshRevision(revision), mNavMeshRevision(0) {}
2018-03-13 22:49:08 +00:00
};
class AsyncNavMeshUpdater
{
public:
AsyncNavMeshUpdater(const Settings& settings);
~AsyncNavMeshUpdater();
void post(const osg::Vec3f& agentHalfExtents, const std::shared_ptr<RecastMesh>& recastMesh,
const std::shared_ptr<NavMeshCacheItem>& navMeshCacheItem, const TilePosition& playerTile,
const std::set<TilePosition>& changedTiles);
2018-03-13 22:49:08 +00:00
void wait();
private:
struct Job
{
osg::Vec3f mAgentHalfExtents;
std::shared_ptr<NavMeshCacheItem> mNavMeshCacheItem;
TilePosition mChangedTile;
std::pair<int, int> mPriority;
friend inline bool operator <(const Job& lhs, const Job& rhs)
{
return lhs.mPriority > rhs.mPriority;
}
2018-03-13 22:49:08 +00:00
};
using Jobs = std::priority_queue<Job, std::deque<Job>>;
2018-03-13 22:49:08 +00:00
std::reference_wrapper<const Settings> mSettings;
std::atomic_bool mShouldStop;
std::mutex mMutex;
std::condition_variable mHasJob;
std::condition_variable mDone;
Jobs mJobs;
2018-04-04 00:20:48 +00:00
std::mutex mRecastMeshMutex;
std::shared_ptr<RecastMesh> mRecastMesh;
2018-03-13 22:49:08 +00:00
std::thread mThread;
void process() throw();
void processJob(const Job& job);
boost::optional<Job> getNextJob();
void notifyHasJob();
2018-04-04 00:20:48 +00:00
void writeDebugFiles(const Job& job, const RecastMesh& recastMesh) const;
std::shared_ptr<RecastMesh> getRecastMesh();
void setRecastMesh(const std::shared_ptr<RecastMesh>& value);
2018-03-13 22:49:08 +00:00
};
}
#endif