mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-01 00:45:26 +02:00
Merge pull request #125 from ArseniyShestakov/newMovementSystem
Okay, time to merge this.
This commit is contained in:
@ -27,6 +27,7 @@
|
||||
#include "rmg/CMapGenerator.h"
|
||||
#include "CStopWatch.h"
|
||||
#include "mapping/CMapEditManager.h"
|
||||
#include "CPathfinder.h"
|
||||
|
||||
class CGObjectInstance;
|
||||
|
||||
@ -2099,7 +2100,7 @@ void CGameState::getNeighbours(const TerrainTile &srct, int3 tile, std::vector<i
|
||||
}
|
||||
}
|
||||
|
||||
int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, bool flying, int remainingMovePoints, bool checkLast)
|
||||
int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints, bool checkLast)
|
||||
{
|
||||
if(src == dest) //same tile
|
||||
return 0;
|
||||
@ -2110,21 +2111,18 @@ int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const
|
||||
//get basic cost
|
||||
int ret = h->getTileCost(d,s);
|
||||
|
||||
if(d.blocked && flying)
|
||||
if(d.blocked && h->canFly())
|
||||
{
|
||||
bool freeFlying = h->getBonusesCount(Selector::typeSubtype(Bonus::FLYING_MOVEMENT, 1)) > 0;
|
||||
|
||||
if(!freeFlying)
|
||||
{
|
||||
ret *= 1.4; //40% penalty for movement over blocked tile
|
||||
}
|
||||
ret *= (100.0 + h->valOfBonuses(Bonus::FLYING_MOVEMENT)) / 100.0;
|
||||
}
|
||||
else if (d.terType == ETerrainType::WATER)
|
||||
else if(d.terType == ETerrainType::WATER)
|
||||
{
|
||||
if(h->boat && s.hasFavourableWinds() && d.hasFavourableWinds()) //Favourable Winds
|
||||
ret *= 0.666;
|
||||
else if (!h->boat && h->getBonusesCount(Selector::typeSubtype(Bonus::WATER_WALKING, 1)) > 0)
|
||||
ret *= 1.4; //40% penalty for water walking
|
||||
else if(!h->boat && h->canWalkOnSea())
|
||||
{
|
||||
ret *= (100.0 + h->valOfBonuses(Bonus::WATER_WALKING)) / 100.0;
|
||||
}
|
||||
}
|
||||
|
||||
if(src.x != dest.x && src.y != dest.y) //it's diagonal move
|
||||
@ -2144,10 +2142,10 @@ int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const
|
||||
{
|
||||
std::vector<int3> vec;
|
||||
vec.reserve(8); //optimization
|
||||
getNeighbours(d, dest, vec, s.terType != ETerrainType::WATER, true);
|
||||
getNeighbours(d, dest, vec, s.terType != ETerrainType::WATER, true);
|
||||
for(auto & elem : vec)
|
||||
{
|
||||
int fcost = getMovementCost(h,dest, elem, flying, left, false);
|
||||
int fcost = getMovementCost(h, dest, elem, left, false);
|
||||
if(fcost <= left)
|
||||
{
|
||||
return ret;
|
||||
@ -2884,109 +2882,6 @@ CGHeroInstance * CGameState::getUsedHero(HeroTypeID hid) const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CGPathNode::CGPathNode()
|
||||
:coord(-1,-1,-1)
|
||||
{
|
||||
accessible = NOT_SET;
|
||||
land = 0;
|
||||
moveRemains = 0;
|
||||
turns = 255;
|
||||
theNodeBefore = nullptr;
|
||||
}
|
||||
|
||||
bool CGPathNode::reachable() const
|
||||
{
|
||||
return turns < 255;
|
||||
}
|
||||
|
||||
const CGPathNode * CPathsInfo::getPathInfo( int3 tile ) const
|
||||
{
|
||||
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];
|
||||
if(!curnode->theNodeBefore)
|
||||
return false;
|
||||
|
||||
|
||||
while(curnode)
|
||||
{
|
||||
CGPathNode cpn = *curnode;
|
||||
curnode = curnode->theNodeBefore;
|
||||
out.nodes.push_back(cpn);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
CPathsInfo::CPathsInfo( const int3 &Sizes )
|
||||
:sizes(Sizes)
|
||||
{
|
||||
hero = nullptr;
|
||||
nodes = new CGPathNode**[sizes.x];
|
||||
for(int i = 0; i < sizes.x; i++)
|
||||
{
|
||||
nodes[i] = new CGPathNode*[sizes.y];
|
||||
for (int j = 0; j < sizes.y; j++)
|
||||
{
|
||||
nodes[i][j] = new CGPathNode[sizes.z];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CPathsInfo::~CPathsInfo()
|
||||
{
|
||||
for(int i = 0; i < sizes.x; i++)
|
||||
{
|
||||
for (int j = 0; j < sizes.y; j++)
|
||||
{
|
||||
delete [] nodes[i][j];
|
||||
}
|
||||
delete [] nodes[i];
|
||||
}
|
||||
delete [] nodes;
|
||||
}
|
||||
|
||||
int3 CGPath::startPos() const
|
||||
{
|
||||
return nodes[nodes.size()-1].coord;
|
||||
}
|
||||
|
||||
int3 CGPath::endPos() const
|
||||
{
|
||||
return nodes[0].coord;
|
||||
}
|
||||
|
||||
void CGPath::convert( ui8 mode )
|
||||
{
|
||||
if(mode==0)
|
||||
{
|
||||
for(auto & elem : nodes)
|
||||
{
|
||||
elem.coord = CGHeroInstance::convertPosition(elem.coord,true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PlayerState::PlayerState()
|
||||
: color(-1), enteredWinningCheatCode(0),
|
||||
enteredLosingCheatCode(0), status(EPlayerStatus::INGAME)
|
||||
@ -3274,341 +3169,8 @@ TeamState::TeamState()
|
||||
setNodeType(TEAM);
|
||||
}
|
||||
|
||||
void CPathfinder::initializeGraph()
|
||||
{
|
||||
CGPathNode ***graph = out.nodes;
|
||||
for(size_t i=0; i < out.sizes.x; ++i)
|
||||
{
|
||||
for(size_t j=0; j < out.sizes.y; ++j)
|
||||
{
|
||||
for(size_t k=0; k < out.sizes.z; ++k)
|
||||
{
|
||||
curPos = int3(i,j,k);
|
||||
const TerrainTile *tinfo = &gs->map->getTile(int3(i, j, k));
|
||||
CGPathNode &node = graph[i][j][k];
|
||||
|
||||
node.accessible = evaluateAccessibility(tinfo);
|
||||
node.turns = 0xff;
|
||||
node.moveRemains = 0;
|
||||
node.coord.x = i;
|
||||
node.coord.y = j;
|
||||
node.coord.z = k;
|
||||
node.land = tinfo->terType != ETerrainType::WATER;
|
||||
node.theNodeBefore = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CPathfinder::calculatePaths()
|
||||
{
|
||||
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
|
||||
{
|
||||
return cp->land ? maxMovePointsLand : maxMovePointsWater;
|
||||
};
|
||||
|
||||
out.hero = hero;
|
||||
out.hpos = hero->getPosition(false);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//logGlobal->infoStream() << boost::format("Calculating paths for hero %s (adress %d) of player %d") % hero->name % hero % hero->tempOwner;
|
||||
initializeGraph();
|
||||
|
||||
//initial tile - set cost on 0 and add to the queue
|
||||
CGPathNode &initialNode = *getNode(out.hpos);
|
||||
initialNode.turns = 0;
|
||||
initialNode.moveRemains = hero->movement;
|
||||
mq.push_back(&initialNode);
|
||||
|
||||
std::vector<int3> neighbours;
|
||||
neighbours.reserve(16);
|
||||
while(!mq.empty())
|
||||
{
|
||||
cp = mq.front();
|
||||
mq.pop_front();
|
||||
|
||||
const int3 sourceGuardPosition = gs->map->guardingCreaturePositions[cp->coord.x][cp->coord.y][cp->coord.z];
|
||||
bool guardedSource = (sourceGuardPosition != int3(-1, -1, -1) && cp->coord != src);
|
||||
ct = &gs->map->getTile(cp->coord);
|
||||
|
||||
int movement = cp->moveRemains, turn = cp->turns;
|
||||
if(!movement)
|
||||
{
|
||||
movement = maxMovePoints(cp);
|
||||
turn++;
|
||||
}
|
||||
|
||||
//add accessible neighbouring nodes to the queue
|
||||
neighbours.clear();
|
||||
|
||||
auto isAllowedTeleportEntrance = [&](const CGTeleport * obj) -> bool
|
||||
{
|
||||
if(!gs->isTeleportEntrancePassable(obj, hero->tempOwner))
|
||||
return false;
|
||||
|
||||
auto whirlpool = dynamic_cast<const CGWhirlpool *>(obj);
|
||||
if(whirlpool)
|
||||
{
|
||||
if(addTeleportWhirlpool(whirlpool))
|
||||
return true;
|
||||
}
|
||||
else if(addTeleportTwoWay(obj) || addTeleportOneWay(obj) || addTeleportOneWayRandom(obj))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
auto sObj = ct->topVisitableObj(cp->coord == CGHeroInstance::convertPosition(hero->pos, false));
|
||||
auto cObj = dynamic_cast<const CGTeleport *>(sObj);
|
||||
if(isAllowedTeleportEntrance(cObj))
|
||||
{
|
||||
for(auto objId : gs->getTeleportChannelExits(cObj->channel, hero->tempOwner))
|
||||
{
|
||||
auto obj = getObj(objId);
|
||||
if(CGTeleport::isExitPassable(gs, hero, obj))
|
||||
neighbours.push_back(obj->visitablePos());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int3> neighbour_tiles;
|
||||
gs->getNeighbours(*ct, cp->coord, neighbour_tiles, boost::logic::indeterminate, !cp->land);
|
||||
if(sObj)
|
||||
{
|
||||
for(int3 neighbour_tile: neighbour_tiles)
|
||||
{
|
||||
if(canMoveBetween(neighbour_tile, sObj->visitablePos()))
|
||||
neighbours.push_back(neighbour_tile);
|
||||
}
|
||||
}
|
||||
else
|
||||
vstd::concatenate(neighbours, neighbour_tiles);
|
||||
|
||||
for(auto & neighbour : neighbours)
|
||||
{
|
||||
const int3 &n = neighbour; //current neighbor
|
||||
dp = getNode(n);
|
||||
dt = &gs->map->getTile(n);
|
||||
destTopVisObjID = dt->topVisitableId();
|
||||
|
||||
useEmbarkCost = 0; //0 - usual movement; 1 - embark; 2 - disembark
|
||||
const bool destIsGuardian = sourceGuardPosition == n;
|
||||
|
||||
auto dObj = dynamic_cast<const CGTeleport*>(dt->topVisitableObj());
|
||||
if(!goodForLandSeaTransition()
|
||||
|| (!canMoveBetween(cp->coord, dp->coord) && !CGTeleport::isConnected(cObj, dObj))
|
||||
|| dp->accessible == CGPathNode::BLOCKED)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//special case -> hero embarked a boat standing on a guarded tile -> we must allow to move away from that tile
|
||||
if(cp->accessible == CGPathNode::VISITABLE && guardedSource && cp->theNodeBefore->land && ct->topVisitableId() == Obj::BOAT)
|
||||
guardedSource = false;
|
||||
|
||||
int cost = gs->getMovementCost(hero, cp->coord, dp->coord, flying, movement);
|
||||
//special case -> moving from src Subterranean gate to dest gate -> it's free
|
||||
if(CGTeleport::isConnected(cObj, dObj))
|
||||
cost = 0;
|
||||
|
||||
int remains = movement - cost;
|
||||
if(useEmbarkCost)
|
||||
{
|
||||
remains = hero->movementPointsAfterEmbark(movement, cost, useEmbarkCost - 1);
|
||||
cost = movement - remains;
|
||||
}
|
||||
|
||||
int turnAtNextTile = turn;
|
||||
if(remains < 0)
|
||||
{
|
||||
//occurs rarely, when hero with low movepoints tries to leave the road
|
||||
turnAtNextTile++;
|
||||
int moveAtNextTile = maxMovePoints(cp);
|
||||
cost = gs->getMovementCost(hero, cp->coord, dp->coord, flying, moveAtNextTile); //cost must be updated, movement points changed :(
|
||||
remains = moveAtNextTile - cost;
|
||||
}
|
||||
|
||||
if((dp->turns==0xff //we haven't been here before
|
||||
|| dp->turns > turnAtNextTile
|
||||
|| (dp->turns >= turnAtNextTile && dp->moveRemains < remains)) //this route is faster
|
||||
&& (!guardedSource || destIsGuardian)) // Can step into tile of guard
|
||||
{
|
||||
assert(dp != cp->theNodeBefore); //two tiles can't point to each other
|
||||
dp->moveRemains = remains;
|
||||
dp->turns = turnAtNextTile;
|
||||
dp->theNodeBefore = cp;
|
||||
|
||||
const bool guardedDst = gs->map->guardingCreaturePositions[dp->coord.x][dp->coord.y][dp->coord.z].valid()
|
||||
&& dp->accessible == CGPathNode::BLOCKVIS;
|
||||
|
||||
auto checkDestinationTile = [&]() -> bool
|
||||
{
|
||||
if(dp->accessible == CGPathNode::ACCESSIBLE)
|
||||
return true;
|
||||
if(dp->coord == CGHeroInstance::convertPosition(hero->pos, false))
|
||||
return true; // This one is tricky, we can ignore fact that tile is not ACCESSIBLE in case if it's our hero block it. Though this need investigation
|
||||
if(dp->accessible == CGPathNode::VISITABLE && CGTeleport::isTeleport(dt->topVisitableObj()))
|
||||
return true; // For now we'll walways allos transit for teleports
|
||||
if(useEmbarkCost && allowEmbarkAndDisembark)
|
||||
return true;
|
||||
if(gs->isTeleportEntrancePassable(dObj, hero->tempOwner))
|
||||
return true; // Always add entry teleport with non-dummy channel
|
||||
if(CGTeleport::isConnected(cObj, dObj))
|
||||
return true; // Always add exit points of teleport
|
||||
if(guardedDst && !guardedSource)
|
||||
return true; // Can step into a hostile tile once
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
if(checkDestinationTile())
|
||||
mq.push_back(dp);
|
||||
}
|
||||
} //neighbours loop
|
||||
} //queue loop
|
||||
}
|
||||
|
||||
CGPathNode *CPathfinder::getNode(const int3 &coord)
|
||||
{
|
||||
return &out.nodes[coord.x][coord.y][coord.z];
|
||||
}
|
||||
|
||||
bool CPathfinder::canMoveBetween(const int3 &a, const int3 &b) const
|
||||
{
|
||||
return gs->checkForVisitableDir(a, b);
|
||||
}
|
||||
|
||||
CGPathNode::EAccessibility CPathfinder::evaluateAccessibility(const TerrainTile *tinfo) const
|
||||
{
|
||||
CGPathNode::EAccessibility ret = (tinfo->blocked ? CGPathNode::BLOCKED : CGPathNode::ACCESSIBLE);
|
||||
|
||||
|
||||
if(tinfo->terType == ETerrainType::ROCK || !FoW[curPos.x][curPos.y][curPos.z])
|
||||
return CGPathNode::BLOCKED;
|
||||
|
||||
if(tinfo->visitable)
|
||||
{
|
||||
if(tinfo->visitableObjects.front()->ID == Obj::SANCTUARY && tinfo->visitableObjects.back()->ID == Obj::HERO && tinfo->visitableObjects.back()->tempOwner != hero->tempOwner) //non-owned hero stands on Sanctuary
|
||||
{
|
||||
return CGPathNode::BLOCKED;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(const CGObjectInstance *obj : tinfo->visitableObjects)
|
||||
{
|
||||
if (obj->passableFor(hero->tempOwner))
|
||||
{
|
||||
ret = CGPathNode::ACCESSIBLE;
|
||||
}
|
||||
else if(obj->blockVisit)
|
||||
{
|
||||
return CGPathNode::BLOCKVIS;
|
||||
}
|
||||
else if(obj->ID != Obj::EVENT) //pathfinder should ignore placed events
|
||||
{
|
||||
ret = CGPathNode::VISITABLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (gs->map->guardingCreaturePositions[curPos.x][curPos.y][curPos.z].valid()
|
||||
&& !tinfo->blocked)
|
||||
{
|
||||
// Monster close by; blocked visit for battle.
|
||||
return CGPathNode::BLOCKVIS;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool CPathfinder::goodForLandSeaTransition()
|
||||
{
|
||||
if(cp->land != dp->land) //hero can traverse land<->sea only in special circumstances
|
||||
{
|
||||
if(cp->land) //from land to sea -> embark or assault hero on boat
|
||||
{
|
||||
if(dp->accessible == CGPathNode::ACCESSIBLE || destTopVisObjID < 0) //cannot enter empty water tile from land -> it has to be visitable
|
||||
return false;
|
||||
if(destTopVisObjID != Obj::HERO && destTopVisObjID != Obj::BOAT) //only boat or hero can be accessed from land
|
||||
return false;
|
||||
if(destTopVisObjID == Obj::BOAT)
|
||||
useEmbarkCost = 1;
|
||||
}
|
||||
else //disembark
|
||||
{
|
||||
//can disembark only on coastal tiles
|
||||
if(!dt->isCoastal())
|
||||
return false;
|
||||
|
||||
//tile must be accessible -> exception: unblocked blockvis tiles -> clear but guarded by nearby monster coast
|
||||
if( (dp->accessible != CGPathNode::ACCESSIBLE && (dp->accessible != CGPathNode::BLOCKVIS || dt->blocked))
|
||||
|| dt->visitable) //TODO: passableness problem -> town says it's passable (thus accessible) but we obviously can't disembark onto town gate
|
||||
return false;;
|
||||
|
||||
useEmbarkCost = 2;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
CPathfinder::CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero) : CGameInfoCallback(_gs, boost::optional<PlayerColor>()), out(_out), hero(_hero), FoW(getPlayerTeam(hero->tempOwner)->fogOfWarMap)
|
||||
{
|
||||
assert(hero);
|
||||
assert(hero == getHero(hero->id));
|
||||
|
||||
allowEmbarkAndDisembark = true;
|
||||
allowTeleportTwoWay = true;
|
||||
allowTeleportOneWay = true;
|
||||
allowTeleportOneWayRandom = false;
|
||||
allowTeleportWhirlpool = false;
|
||||
if (CGWhirlpool::isProtected(hero))
|
||||
allowTeleportWhirlpool = true;
|
||||
}
|
||||
|
||||
CRandomGenerator & CGameState::getRandomGenerator()
|
||||
{
|
||||
//logGlobal->traceStream() << "Fetching CGameState::rand with seed " << rand.nextInt();
|
||||
return rand;
|
||||
}
|
||||
|
||||
bool CPathfinder::addTeleportTwoWay(const CGTeleport * obj) const
|
||||
{
|
||||
return allowTeleportTwoWay && gs->isTeleportChannelBidirectional(obj->channel, hero->tempOwner);
|
||||
}
|
||||
|
||||
bool CPathfinder::addTeleportOneWay(const CGTeleport * obj) const
|
||||
{
|
||||
if(allowTeleportOneWay && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
|
||||
{
|
||||
auto passableExits = CGTeleport::getPassableExits(gs, hero, gs->getTeleportChannelExits(obj->channel, hero->tempOwner));
|
||||
if(passableExits.size() == 1)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CPathfinder::addTeleportOneWayRandom(const CGTeleport * obj) const
|
||||
{
|
||||
if(allowTeleportOneWayRandom && isTeleportChannelUnidirectional(obj->channel, hero->tempOwner))
|
||||
{
|
||||
auto passableExits = CGTeleport::getPassableExits(gs, hero, gs->getTeleportChannelExits(obj->channel, hero->tempOwner));
|
||||
if(passableExits.size() > 1)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CPathfinder::addTeleportWhirlpool(const CGWhirlpool * obj) const
|
||||
{
|
||||
return allowTeleportWhirlpool && obj;
|
||||
}
|
||||
|
Reference in New Issue
Block a user