mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-06 09:09:40 +02:00
Pathfinding: do path calculation in separate thread
This commit is contained in:
@@ -2164,8 +2164,11 @@ void CGameState::apply(CPack *pack)
|
|||||||
|
|
||||||
void CGameState::calculatePaths(const CGHeroInstance *hero, CPathsInfo &out)
|
void CGameState::calculatePaths(const CGHeroInstance *hero, CPathsInfo &out)
|
||||||
{
|
{
|
||||||
|
if(pathfinderWorking)
|
||||||
|
pathfinderWorking->interrupt();
|
||||||
|
|
||||||
CPathfinder pathfinder(out, this, hero);
|
CPathfinder pathfinder(out, this, hero);
|
||||||
pathfinder.calculatePaths();
|
pathfinderWorking = make_unique<boost::thread>(&CPathfinder::startPathfinder, pathfinder);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -314,6 +314,8 @@ public:
|
|||||||
|
|
||||||
boost::shared_mutex *mx;
|
boost::shared_mutex *mx;
|
||||||
|
|
||||||
|
unique_ptr<boost::thread> pathfinderWorking;
|
||||||
|
|
||||||
void giveHeroArtifact(CGHeroInstance *h, ArtifactID aid);
|
void giveHeroArtifact(CGHeroInstance *h, ArtifactID aid);
|
||||||
|
|
||||||
void apply(CPack *pack);
|
void apply(CPack *pack);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "mapObjects/CGHeroInstance.h"
|
#include "mapObjects/CGHeroInstance.h"
|
||||||
#include "GameConstants.h"
|
#include "GameConstants.h"
|
||||||
#include "CStopWatch.h"
|
#include "CStopWatch.h"
|
||||||
|
#include "CThreadHelper.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* CPathfinder.cpp, part of VCMI engine
|
* CPathfinder.cpp, part of VCMI engine
|
||||||
@@ -59,6 +60,24 @@ CPathfinder::CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance
|
|||||||
neighbours.reserve(16);
|
neighbours.reserve(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CPathfinder::startPathfinder()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
setThreadName("CPathfinder::startPathfinder");
|
||||||
|
|
||||||
|
calculatePaths();
|
||||||
|
}
|
||||||
|
catch(boost::thread_interrupted &e)
|
||||||
|
{
|
||||||
|
gs->pathfinderWorking.reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
gs->pathfinderWorking.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void CPathfinder::calculatePaths()
|
void CPathfinder::calculatePaths()
|
||||||
{
|
{
|
||||||
int maxMovePointsLand = hero->maxMovePoints(true);
|
int maxMovePointsLand = hero->maxMovePoints(true);
|
||||||
@@ -192,6 +211,8 @@ void CPathfinder::calculatePaths()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boost::this_thread::interruption_point();
|
||||||
} //queue loop
|
} //queue loop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ class CPathfinder : private CGameInfoCallback
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
|
CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
|
||||||
|
void startPathfinder();
|
||||||
void calculatePaths(); //calculates possible paths for hero, uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
|
void calculatePaths(); //calculates possible paths for hero, uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user