1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-29 00:41:38 +02:00

Merge branch 'optimization/swiftAI' into develop

Conflicts:
	lib/CGameState.cpp
This commit is contained in:
Ivan Savenko
2014-09-21 22:31:29 +03:00
29 changed files with 250 additions and 357 deletions

View File

@ -2165,10 +2165,10 @@ void CGameState::apply(CPack *pack)
applierGs->apps[typ]->applyOnGS(this,pack);
}
void CGameState::calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src, int movement)
void CGameState::calculatePaths(const CGHeroInstance *hero, CPathsInfo &out)
{
CPathfinder pathfinder(out, this, hero);
pathfinder.calculatePaths(src, movement);
pathfinder.calculatePaths();
}
/**
@ -2896,9 +2896,29 @@ bool CGPathNode::reachable() const
return turns < 255;
}
bool CPathsInfo::getPath( const int3 &dst, CGPath &out )
const CGPathNode * CPathsInfo::getPathInfo( int3 tile ) const
{
assert(isValid);
boost::unique_lock<boost::mutex> pathLock(pathMx);
if (tile.x >= sizes.x || tile.y >= sizes.y || tile.z >= sizes.z)
return nullptr;
return &nodes[tile.x][tile.y][tile.z];
}
int CPathsInfo::getDistance( int3 tile ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
CGPath ret;
if (getPath(tile, ret))
return ret.nodes.size();
else
return 255;
}
bool CPathsInfo::getPath( const int3 &dst, CGPath &out ) const
{
boost::unique_lock<boost::mutex> pathLock(pathMx);
out.nodes.clear();
const CGPathNode *curnode = &nodes[dst.x][dst.y][dst.z];
@ -2965,7 +2985,7 @@ void CGPath::convert( ui8 mode )
}
PlayerState::PlayerState()
: color(-1), currentSelection(0xffffffff), enteredWinningCheatCode(0),
: color(-1), enteredWinningCheatCode(0),
enteredLosingCheatCode(0), status(EPlayerStatus::INGAME)
{
setNodeType(PLAYER);
@ -3277,18 +3297,15 @@ void CPathfinder::initializeGraph()
}
}
void CPathfinder::calculatePaths(int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/)
void CPathfinder::calculatePaths()
{
assert(hero);
assert(hero == getHero(hero->id));
if(src.x < 0)
src = hero->getPosition(false);
if(movement < 0)
movement = hero->movement;
bool flying = hero->hasBonusOfType(Bonus::FLYING_MOVEMENT);
int maxMovePointsLand = hero->maxMovePoints(true);
int maxMovePointsWater = hero->maxMovePoints(false);
int3 src = hero->getPosition(false);
auto maxMovePoints = [&](CGPathNode *cp) -> int
{
@ -3296,9 +3313,9 @@ void CPathfinder::calculatePaths(int3 src /*= int3(-1,-1,-1)*/, int movement /*=
};
out.hero = hero;
out.hpos = src;
out.hpos = hero->getPosition(false);
if(!gs->map->isInTheMap(src)/* || !gs->map->isInTheMap(dest)*/) //check input
if(!gs->map->isInTheMap(out.hpos)/* || !gs->map->isInTheMap(dest)*/) //check input
{
logGlobal->errorStream() << "CGameState::calculatePaths: Hero outside the gs->map? How dare you...";
return;
@ -3308,9 +3325,9 @@ void CPathfinder::calculatePaths(int3 src /*= int3(-1,-1,-1)*/, int movement /*=
initializeGraph();
//initial tile - set cost on 0 and add to the queue
CGPathNode &initialNode = *getNode(src);
CGPathNode &initialNode = *getNode(out.hpos);
initialNode.turns = 0;
initialNode.moveRemains = movement;
initialNode.moveRemains = hero->movement;
mq.push_back(&initialNode);
std::vector<int3> neighbours;
@ -3426,8 +3443,6 @@ void CPathfinder::calculatePaths(int3 src /*= int3(-1,-1,-1)*/, int movement /*=
}
} //neighbours loop
} //queue loop
out.isValid = true;
}
CGPathNode *CPathfinder::getNode(const int3 &coord)