2017-07-13 10:26:03 +02:00
/*
* CPathfinder . cpp , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
2015-10-27 02:34:47 +02:00
# include "StdInc.h"
# include "CPathfinder.h"
# include "CHeroHandler.h"
# include "mapping/CMap.h"
2015-10-27 16:42:31 +02:00
# include "CGameState.h"
# include "mapObjects/CGHeroInstance.h"
2015-10-27 02:34:47 +02:00
# include "GameConstants.h"
# include "CStopWatch.h"
2015-11-20 23:32:23 +02:00
# include "CConfigHandler.h"
2015-12-02 21:39:53 +02:00
# include "../lib/CPlayerState.h"
2015-10-27 02:34:47 +02:00
2018-07-29 13:50:22 +02:00
bool canSeeObj ( const CGObjectInstance * obj )
{
/// Pathfinder should ignore placed events
return obj ! = nullptr & & obj - > ID ! = Obj : : EVENT ;
}
2018-08-01 20:46:06 +02:00
CNeighbourFinder : : CNeighbourFinder ( )
2018-07-29 13:50:22 +02:00
{
}
2018-08-01 22:21:14 +02:00
std : : vector < CGPathNode * > CNeighbourFinder : : calculateNeighbours (
CPathNodeInfo & source ,
CPathfinderHelper * pathfinderHelper ,
CNodeHelper * nodeHelper ) const
2018-07-29 13:50:22 +02:00
{
2018-08-01 22:21:14 +02:00
std : : vector < CGPathNode * > neighbours ;
auto accessibleNeighbourTiles = getNeighbourTiles ( source , pathfinderHelper ) ;
2018-07-29 13:50:22 +02:00
for ( auto & neighbour : accessibleNeighbourTiles )
{
for ( EPathfindingLayer i = EPathfindingLayer : : LAND ; i < = EPathfindingLayer : : AIR ; i . advance ( 1 ) )
{
2018-08-01 20:46:06 +02:00
auto node = nodeHelper - > getNode ( neighbour , i ) ;
2018-07-29 13:50:22 +02:00
if ( node - > accessible = = CGPathNode : : NOT_SET )
continue ;
neighbours . push_back ( node ) ;
}
}
return neighbours ;
}
2018-08-01 22:21:14 +02:00
std : : vector < CGPathNode * > CNeighbourFinder : : calculateTeleportations (
CPathNodeInfo & source ,
CPathfinderHelper * pathfinderHelper ,
CNodeHelper * nodeHelper ) const
2018-07-29 13:50:22 +02:00
{
2018-08-01 22:21:14 +02:00
std : : vector < CGPathNode * > neighbours ;
auto accessibleExits = getTeleportExits ( source , pathfinderHelper ) ;
for ( auto & neighbour : accessibleExits )
{
auto node = nodeHelper - > getNode ( neighbour , source . node - > layer ) ;
neighbours . push_back ( node ) ;
}
return neighbours ;
}
std : : vector < int3 > CNeighbourFinder : : getNeighbourTiles ( CPathNodeInfo & source , CPathfinderHelper * pathfinderHelper ) const
{
std : : vector < int3 > neighbourTiles ;
2018-07-29 13:50:22 +02:00
2018-08-01 20:46:06 +02:00
pathfinderHelper - > getNeighbours (
* source . tile ,
source . node - > coord ,
neighbourTiles ,
boost : : logic : : indeterminate ,
source . node - > layer = = EPathfindingLayer : : SAIL ) ;
2018-07-29 13:50:22 +02:00
2018-08-01 20:46:06 +02:00
if ( source . isNodeObjectVisitable ( ) )
2018-07-29 13:50:22 +02:00
{
2018-08-01 22:21:14 +02:00
vstd : : erase_if ( neighbourTiles , [ & ] ( int3 tile ) - > bool
2018-07-29 13:50:22 +02:00
{
2018-08-01 22:21:14 +02:00
return ! pathfinderHelper - > canMoveBetween ( tile , source . nodeObject - > visitablePos ( ) ) ;
} ) ;
2018-07-29 13:50:22 +02:00
}
2018-08-01 22:21:14 +02:00
return neighbourTiles ;
2018-07-29 13:50:22 +02:00
}
class CPathfinderNodeHelper : public CNodeHelper
{
private :
CPathsInfo & out ;
public :
CPathfinderNodeHelper ( CPathsInfo & pathsInfo , const CGHeroInstance * hero )
: out ( pathsInfo )
{
out . hero = hero ;
out . hpos = hero - > getPosition ( false ) ;
}
virtual CGPathNode * getNode ( const int3 & coord , const EPathfindingLayer layer )
{
return out . getNode ( coord , layer ) ;
}
virtual CGPathNode * getInitialNode ( )
{
auto initialNode = getNode ( out . hpos , out . hero - > boat ? EPathfindingLayer : : SAIL : EPathfindingLayer : : LAND ) ;
initialNode - > turns = 0 ;
initialNode - > moveRemains = out . hero - > movement ;
return initialNode ;
}
} ;
2018-08-01 20:46:06 +02:00
PathfinderOptions : : PathfinderOptions ( )
2015-10-27 02:34:47 +02:00
{
2015-11-20 23:32:23 +02:00
useFlying = settings [ " pathfinder " ] [ " layers " ] [ " flying " ] . Bool ( ) ;
useWaterWalking = settings [ " pathfinder " ] [ " layers " ] [ " waterWalking " ] . Bool ( ) ;
useEmbarkAndDisembark = settings [ " pathfinder " ] [ " layers " ] [ " sailing " ] . Bool ( ) ;
useTeleportTwoWay = settings [ " pathfinder " ] [ " teleports " ] [ " twoWay " ] . Bool ( ) ;
useTeleportOneWay = settings [ " pathfinder " ] [ " teleports " ] [ " oneWay " ] . Bool ( ) ;
useTeleportOneWayRandom = settings [ " pathfinder " ] [ " teleports " ] [ " oneWayRandom " ] . Bool ( ) ;
useTeleportWhirlpool = settings [ " pathfinder " ] [ " teleports " ] [ " whirlpool " ] . Bool ( ) ;
2015-11-04 14:05:22 +02:00
2015-11-20 23:32:23 +02:00
useCastleGate = settings [ " pathfinder " ] [ " teleports " ] [ " castleGate " ] . Bool ( ) ;
2015-11-08 09:06:24 +02:00
2015-11-20 23:32:23 +02:00
lightweightFlyingMode = settings [ " pathfinder " ] [ " lightweightFlyingMode " ] . Bool ( ) ;
oneTurnSpecialLayersLimit = settings [ " pathfinder " ] [ " oneTurnSpecialLayersLimit " ] . Bool ( ) ;
originalMovementRules = settings [ " pathfinder " ] [ " originalMovementRules " ] . Bool ( ) ;
2015-10-27 02:34:47 +02:00
}
2018-07-29 13:50:22 +02:00
CPathfinder : : CPathfinder (
CPathsInfo & _out ,
CGameState * _gs ,
const CGHeroInstance * _hero )
: CPathfinder (
_gs ,
_hero ,
std : : make_shared < CPathfinderNodeHelper > ( _out , _hero ) ,
2018-08-01 20:46:06 +02:00
std : : make_shared < CNeighbourFinder > ( ) )
2018-07-29 13:50:22 +02:00
{
}
CPathfinder : : CPathfinder (
CGameState * _gs ,
const CGHeroInstance * _hero ,
std : : shared_ptr < CNodeHelper > nodeHelper ,
std : : shared_ptr < CNeighbourFinder > neighbourFinder )
: CGameInfoCallback ( _gs , boost : : optional < PlayerColor > ( ) )
, hero ( _hero )
, FoW ( getPlayerTeam ( hero - > tempOwner ) - > fogOfWarMap ) , patrolTiles ( { } )
, nodeHelper ( nodeHelper )
, neighbourFinder ( neighbourFinder )
, source ( )
, destination ( )
2015-10-27 02:34:47 +02:00
{
assert ( hero ) ;
assert ( hero = = getHero ( hero - > id ) ) ;
2018-08-01 20:46:06 +02:00
hlp = make_unique < CPathfinderHelper > ( _gs , hero , options ) ;
2015-10-27 02:34:47 +02:00
2015-11-28 19:34:50 +02:00
initializePatrol ( ) ;
2015-11-03 00:29:43 +02:00
initializeGraph ( ) ;
2015-10-27 02:34:47 +02:00
}
void CPathfinder : : calculatePaths ( )
{
2015-11-23 13:11:08 +02:00
auto passOneTurnLimitCheck = [ & ] ( ) - > bool
2015-11-08 06:44:00 +02:00
{
2015-11-23 13:11:08 +02:00
if ( ! options . oneTurnSpecialLayersLimit )
return true ;
2018-07-29 13:50:22 +02:00
if ( source . node - > layer = = ELayer : : WATER )
2015-11-23 13:11:08 +02:00
return false ;
2018-07-29 13:50:22 +02:00
if ( source . node - > layer = = ELayer : : AIR )
2015-11-08 06:44:00 +02:00
{
2018-07-29 13:50:22 +02:00
if ( options . originalMovementRules & & source . node - > accessible = = CGPathNode : : ACCESSIBLE )
2015-11-23 13:11:08 +02:00
return true ;
else
2015-11-08 06:44:00 +02:00
return false ;
}
2015-11-23 13:11:08 +02:00
2015-11-08 06:44:00 +02:00
return true ;
} ;
2015-10-27 02:34:47 +02:00
auto isBetterWay = [ & ] ( int remains , int turn ) - > bool
{
2018-07-29 13:50:22 +02:00
if ( destination . node - > turns = = 0xff ) //we haven't been here before
2015-10-27 02:34:47 +02:00
return true ;
2018-07-29 13:50:22 +02:00
else if ( destination . node - > turns > turn )
2015-10-27 02:34:47 +02:00
return true ;
2018-07-29 13:50:22 +02:00
else if ( destination . node - > turns > = turn & & destination . node - > moveRemains < remains ) //this route is faster
2015-10-27 02:34:47 +02:00
return true ;
return false ;
} ;
2017-08-10 20:59:55 +02:00
//logGlobal->info("Calculating paths for hero %s (adress %d) of player %d", hero->name, hero , hero->tempOwner);
2015-10-27 02:34:47 +02:00
//initial tile - set cost on 0 and add to the queue
2018-07-29 13:50:22 +02:00
CGPathNode * initialNode = nodeHelper - > getInitialNode ( ) ;
if ( ! isInTheMap ( initialNode - > coord ) /* || !gs->map->isInTheMap(dest)*/ ) //check input
{
logGlobal - > error ( " CGameState::calculatePaths: Hero outside the gs->map? How dare you... " ) ;
throw std : : runtime_error ( " Wrong checksum " ) ;
}
2015-11-28 19:34:50 +02:00
if ( isHeroPatrolLocked ( ) )
return ;
2015-10-27 02:34:47 +02:00
2015-11-28 19:34:50 +02:00
pq . push ( initialNode ) ;
2015-11-07 20:11:07 +02:00
while ( ! pq . empty ( ) )
2015-10-27 02:34:47 +02:00
{
2018-07-29 13:50:22 +02:00
auto node = pq . top ( ) ;
auto excludeOurHero = node - > coord = = initialNode - > coord ;
source . setNode ( gs , node , excludeOurHero ) ;
2015-11-07 20:11:07 +02:00
pq . pop ( ) ;
2018-07-29 13:50:22 +02:00
source . node - > locked = true ;
2015-10-27 02:34:47 +02:00
2018-07-29 13:50:22 +02:00
int movement = source . node - > moveRemains , turn = source . node - > turns ;
2015-11-10 13:26:45 +02:00
hlp - > updateTurnInfo ( turn ) ;
2015-10-27 02:34:47 +02:00
if ( ! movement )
{
2015-11-10 13:26:45 +02:00
hlp - > updateTurnInfo ( + + turn ) ;
2018-07-29 13:50:22 +02:00
movement = hlp - > getMaxMovePoints ( source . node - > layer ) ;
2015-11-23 13:11:08 +02:00
if ( ! passOneTurnLimitCheck ( ) )
2015-11-22 05:23:54 +02:00
continue ;
2015-10-27 02:34:47 +02:00
}
2018-08-01 22:21:14 +02:00
source . guarded = isSourceGuarded ( ) ;
if ( source . nodeObject )
source . objectRelations = gs - > getPlayerRelations ( hero - > tempOwner , source . nodeObject - > tempOwner ) ;
2015-10-27 02:34:47 +02:00
//add accessible neighbouring nodes to the queue
2018-08-01 20:46:06 +02:00
auto neighbourNodes = neighbourFinder - > calculateNeighbours ( source , hlp . get ( ) , nodeHelper . get ( ) ) ;
2018-08-01 22:21:14 +02:00
for ( CGPathNode * neighbour : neighbourNodes )
2015-10-27 02:34:47 +02:00
{
2018-07-29 13:50:22 +02:00
destination . setNode ( gs , neighbour ) ;
2015-11-22 05:16:16 +02:00
2018-07-29 13:50:22 +02:00
if ( destination . node - > locked )
continue ;
2015-11-22 05:16:16 +02:00
2018-07-29 13:50:22 +02:00
if ( ! isPatrolMovementAllowed ( destination . node - > coord ) )
continue ;
2015-11-07 20:11:07 +02:00
2018-07-29 13:50:22 +02:00
if ( ! hlp - > isLayerAvailable ( destination . node - > layer ) )
continue ;
2015-11-08 06:44:00 +02:00
2018-07-29 13:50:22 +02:00
/// Check transition without tile accessability rules
if ( source . node - > layer ! = destination . node - > layer & & ! isLayerTransitionPossible ( destination . node - > layer ) )
continue ;
/// Check transition using tile accessability rules
if ( source . node - > layer ! = destination . node - > layer & & ! isLayerTransitionPossible ( ) )
continue ;
2015-11-02 15:03:03 +02:00
2018-08-01 20:46:06 +02:00
destination . guarded = isDestinationGuarded ( ) ;
2018-08-01 22:21:14 +02:00
if ( destination . nodeObject )
destination . objectRelations = gs - > getPlayerRelations ( hero - > tempOwner , destination . nodeObject - > tempOwner ) ;
2018-08-01 20:46:06 +02:00
2018-07-29 13:50:22 +02:00
if ( ! isMovementToDestPossible ( ) )
continue ;
2015-11-03 02:25:12 +02:00
2018-08-01 20:46:06 +02:00
destination . action = getDestAction ( ) ;
2018-07-29 13:50:22 +02:00
int turnAtNextTile = turn , moveAtNextTile = movement ;
2018-08-01 20:46:06 +02:00
int cost = hlp - > getMovementCost ( source , destination , moveAtNextTile ) ;
2018-07-29 13:50:22 +02:00
int remains = moveAtNextTile - cost ;
if ( remains < 0 )
{
//occurs rarely, when hero with low movepoints tries to leave the road
hlp - > updateTurnInfo ( + + turnAtNextTile ) ;
moveAtNextTile = hlp - > getMaxMovePoints ( destination . node - > layer ) ;
2018-08-01 20:46:06 +02:00
cost = hlp - > getMovementCost ( source , destination , moveAtNextTile ) ; //cost must be updated, movement points changed :(
2018-07-29 13:50:22 +02:00
remains = moveAtNextTile - cost ;
}
2018-08-01 20:46:06 +02:00
if ( destination . action = = CGPathNode : : EMBARK | | destination . action = = CGPathNode : : DISEMBARK )
2018-07-29 13:50:22 +02:00
{
/// FREE_SHIP_BOARDING bonus only remove additional penalty
/// land <-> sail transition still cost movement points as normal movement
2018-08-01 20:46:06 +02:00
remains = hero - > movementPointsAfterEmbark ( moveAtNextTile , cost , destination . action - 1 , hlp - > getTurnInfo ( ) ) ;
2018-07-29 13:50:22 +02:00
cost = moveAtNextTile - remains ;
}
2015-10-27 02:34:47 +02:00
2018-07-29 13:50:22 +02:00
if ( isBetterWay ( remains , turnAtNextTile ) & &
( ( source . node - > turns = = turnAtNextTile & & remains ) | | passOneTurnLimitCheck ( ) ) )
{
2018-08-01 22:21:14 +02:00
assert ( destination . node ! = source . node - > theNodeBefore ) ; //two tiles can't point to each other
2018-07-29 13:50:22 +02:00
destination . node - > moveRemains = remains ;
destination . node - > turns = turnAtNextTile ;
destination . node - > theNodeBefore = source . node ;
2018-08-01 20:46:06 +02:00
destination . node - > action = destination . action ;
CMovementAfterDestinationRule rl = CMovementAfterDestinationRule ( ) ;
rl . process ( hlp . get ( ) , source , destination ) ;
2018-07-29 13:50:22 +02:00
if ( ! destination . furtherProcessingImpossible )
pq . push ( destination . node ) ;
2015-10-27 02:34:47 +02:00
}
} //neighbours loop
//just add all passable teleport exits
2018-08-01 22:21:14 +02:00
/// For now we disable teleports usage for patrol movement
/// VCAI not aware about patrol and may stuck while attempt to use teleport
if ( ! source . isNodeObjectVisitable ( ) | | patrolState = = PATROL_RADIUS )
continue ;
auto teleportationNodes = neighbourFinder - > calculateTeleportations ( source , hlp . get ( ) , nodeHelper . get ( ) ) ;
for ( CGPathNode * teleportNode : teleportationNodes )
2015-10-27 02:34:47 +02:00
{
2018-07-29 13:50:22 +02:00
if ( teleportNode - > locked )
2015-11-16 16:36:58 +02:00
continue ;
2015-12-11 03:00:10 +02:00
/// TODO: We may consider use invisible exits on FoW border in future
/// Useful for AI when at least one tile around exit is visible and passable
/// Objects are usually visible on FoW border anyway so it's not cheating.
///
/// For now it's disabled as it's will cause crashes in movement code.
2018-07-29 13:50:22 +02:00
if ( teleportNode - > accessible = = CGPathNode : : BLOCKED )
2015-12-11 03:00:10 +02:00
continue ;
2015-11-07 20:11:07 +02:00
2018-07-29 13:50:22 +02:00
destination . setNode ( gs , teleportNode ) ;
2015-11-16 16:36:58 +02:00
if ( isBetterWay ( movement , turn ) )
{
2018-07-29 13:50:22 +02:00
destination . node - > moveRemains = movement ;
destination . node - > turns = turn ;
destination . node - > theNodeBefore = source . node ;
destination . node - > action = getTeleportDestAction ( ) ;
if ( destination . node - > action = = CGPathNode : : TELEPORT_NORMAL )
pq . push ( destination . node ) ;
2015-10-27 02:34:47 +02:00
}
}
} //queue loop
}
2018-08-01 22:21:14 +02:00
std : : vector < int3 > CPathfinderHelper : : getAllowedTeleportChannelExits ( TeleportChannelID channelID ) const
2015-10-27 02:34:47 +02:00
{
2018-08-01 22:21:14 +02:00
std : : vector < int3 > allowedExits ;
2015-11-16 16:36:58 +02:00
2018-08-01 22:21:14 +02:00
for ( auto objId : getTeleportChannelExits ( channelID , hero - > tempOwner ) )
2015-10-27 02:34:47 +02:00
{
2018-08-01 22:21:14 +02:00
auto obj = getObj ( objId ) ;
if ( dynamic_cast < const CGWhirlpool * > ( obj ) )
2015-10-27 02:34:47 +02:00
{
2018-08-01 22:21:14 +02:00
auto pos = obj - > getBlockedPos ( ) ;
for ( auto p : pos )
2015-11-12 00:16:06 +02:00
{
2018-08-01 22:21:14 +02:00
if ( gs - > map - > getTile ( p ) . topVisitableId ( ) = = obj - > ID )
allowedExits . push_back ( p ) ;
2015-11-12 00:16:06 +02:00
}
2018-08-01 22:21:14 +02:00
}
else if ( CGTeleport : : isExitPassable ( gs , hero , obj ) )
allowedExits . push_back ( obj - > visitablePos ( ) ) ;
}
return allowedExits ;
}
std : : vector < int3 > CPathfinderHelper : : getCastleGates ( CPathNodeInfo & source ) const
{
std : : vector < int3 > allowedExits ;
auto towns = getPlayer ( hero - > tempOwner ) - > towns ;
for ( const auto & town : towns )
{
if ( town - > id ! = source . nodeObject - > id & & town - > visitingHero = = nullptr
& & town - > hasBuilt ( BuildingID : : CASTLE_GATE , ETownType : : INFERNO ) )
{
allowedExits . push_back ( town - > visitablePos ( ) ) ;
2015-10-27 02:34:47 +02:00
}
}
2015-11-08 09:06:24 +02:00
2018-08-01 22:21:14 +02:00
return allowedExits ;
}
std : : vector < int3 > CNeighbourFinder : : getTeleportExits (
CPathNodeInfo & source ,
CPathfinderHelper * pathfinderHelper ) const
{
std : : vector < int3 > teleportationExits ;
const CGTeleport * objTeleport = dynamic_cast < const CGTeleport * > ( source . nodeObject ) ;
if ( pathfinderHelper - > isAllowedTeleportEntrance ( objTeleport ) )
{
for ( auto exit : pathfinderHelper - > getAllowedTeleportChannelExits ( objTeleport - > channel ) )
{
teleportationExits . push_back ( exit ) ;
}
}
else if ( pathfinderHelper - > options . useCastleGate
2018-07-29 13:50:22 +02:00
& & ( source . nodeObject - > ID = = Obj : : TOWN & & source . nodeObject - > subID = = ETownType : : INFERNO
2018-08-01 22:21:14 +02:00
& & source . objectRelations ! = PlayerRelations : : ENEMIES ) )
2015-11-08 09:06:24 +02:00
{
/// TODO: Find way to reuse CPlayerSpecificInfoCallback::getTownsInfo
/// This may be handy if we allow to use teleportation to friendly towns
2018-08-01 22:21:14 +02:00
for ( auto exit : pathfinderHelper - > getCastleGates ( source ) )
2015-11-08 09:06:24 +02:00
{
2018-08-01 22:21:14 +02:00
teleportationExits . push_back ( exit ) ;
2015-11-08 09:06:24 +02:00
}
}
2018-08-01 22:21:14 +02:00
return teleportationExits ;
2015-10-27 02:34:47 +02:00
}
2015-11-28 19:34:50 +02:00
bool CPathfinder : : isHeroPatrolLocked ( ) const
{
return patrolState = = PATROL_LOCKED ;
}
bool CPathfinder : : isPatrolMovementAllowed ( const int3 & dst ) const
{
if ( patrolState = = PATROL_RADIUS )
{
if ( ! vstd : : contains ( patrolTiles , dst ) )
return false ;
}
return true ;
}
2015-11-22 05:16:16 +02:00
bool CPathfinder : : isLayerTransitionPossible ( const ELayer destLayer ) const
2015-11-04 11:29:51 +02:00
{
2015-11-10 18:15:59 +02:00
/// No layer transition allowed when previous node action is BATTLE
2018-07-29 13:50:22 +02:00
if ( source . node - > action = = CGPathNode : : BATTLE )
2015-11-10 18:15:59 +02:00
return false ;
2018-07-29 13:50:22 +02:00
switch ( source . node - > layer )
2015-11-10 18:15:59 +02:00
{
2015-11-11 21:08:15 +02:00
case ELayer : : LAND :
2016-02-26 20:43:43 +02:00
if ( destLayer = = ELayer : : AIR )
{
if ( ! options . lightweightFlyingMode | | isSourceInitialPosition ( ) )
return true ;
}
else if ( destLayer = = ELayer : : SAIL )
{
2018-07-29 13:50:22 +02:00
if ( destination . tile - > isWater ( ) )
2016-02-26 20:43:43 +02:00
return true ;
}
else
2015-11-22 05:16:16 +02:00
return true ;
break ;
case ELayer : : SAIL :
2018-07-29 13:50:22 +02:00
if ( destLayer = = ELayer : : LAND & & ! destination . tile - > isWater ( ) )
2015-11-22 05:16:16 +02:00
return true ;
break ;
case ELayer : : AIR :
if ( destLayer = = ELayer : : LAND )
return true ;
break ;
case ELayer : : WATER :
if ( destLayer = = ELayer : : LAND )
return true ;
break ;
}
return false ;
}
bool CPathfinder : : isLayerTransitionPossible ( ) const
{
2018-07-29 13:50:22 +02:00
switch ( source . node - > layer )
2015-11-22 05:16:16 +02:00
{
case ELayer : : LAND :
2018-07-29 13:50:22 +02:00
if ( destination . node - > layer = = ELayer : : SAIL )
2015-11-11 21:08:15 +02:00
{
/// Cannot enter empty water tile from land -> it has to be visitable
2018-07-29 13:50:22 +02:00
if ( destination . node - > accessible = = CGPathNode : : ACCESSIBLE )
2015-11-11 21:08:15 +02:00
return false ;
}
break ;
case ELayer : : SAIL :
//tile must be accessible -> exception: unblocked blockvis tiles -> clear but guarded by nearby monster coast
2018-07-29 13:50:22 +02:00
if ( ( destination . node - > accessible ! = CGPathNode : : ACCESSIBLE & & ( destination . node - > accessible ! = CGPathNode : : BLOCKVIS | | destination . tile - > blocked ) )
| | destination . tile - > visitable ) //TODO: passableness problem -> town says it's passable (thus accessible) but we obviously can't disembark onto town gate
2015-11-11 21:08:15 +02:00
{
return false ;
}
2015-11-11 21:29:20 +02:00
2015-11-11 21:08:15 +02:00
break ;
case ELayer : : AIR :
2015-11-10 20:07:27 +02:00
if ( options . originalMovementRules )
2015-11-10 18:15:59 +02:00
{
2018-07-29 13:50:22 +02:00
if ( ( source . node - > accessible ! = CGPathNode : : ACCESSIBLE & &
source . node - > accessible ! = CGPathNode : : VISITABLE ) & &
( destination . node - > accessible ! = CGPathNode : : VISITABLE & &
destination . node - > accessible ! = CGPathNode : : ACCESSIBLE ) )
2015-11-10 20:07:27 +02:00
{
return false ;
}
}
2018-07-29 13:50:22 +02:00
else if ( source . node - > accessible ! = CGPathNode : : ACCESSIBLE & & destination . node - > accessible ! = CGPathNode : : ACCESSIBLE )
2015-11-10 20:07:27 +02:00
{
/// Hero that fly can only land on accessible tiles
2015-11-10 18:15:59 +02:00
return false ;
}
2015-11-04 11:29:51 +02:00
2015-12-16 19:14:36 +02:00
break ;
case ELayer : : WATER :
2018-07-29 13:50:22 +02:00
if ( destination . node - > accessible ! = CGPathNode : : ACCESSIBLE & & destination . node - > accessible ! = CGPathNode : : VISITABLE )
2015-12-16 19:14:36 +02:00
{
/// Hero that walking on water can transit to accessible and visitable tiles
/// Though hero can't interact with blocking visit objects while standing on water
return false ;
}
2015-11-11 21:08:15 +02:00
break ;
2015-11-04 11:29:51 +02:00
}
2015-11-11 21:08:15 +02:00
2015-11-04 11:29:51 +02:00
return true ;
}
2015-11-11 18:51:08 +02:00
bool CPathfinder : : isMovementToDestPossible ( ) const
2015-10-27 02:34:47 +02:00
{
2018-07-29 13:50:22 +02:00
if ( destination . node - > accessible = = CGPathNode : : BLOCKED )
2015-11-17 02:59:02 +02:00
return false ;
2018-07-29 13:50:22 +02:00
switch ( destination . node - > layer )
2015-10-27 02:34:47 +02:00
{
2015-11-11 21:08:15 +02:00
case ELayer : : LAND :
2018-08-01 20:46:06 +02:00
if ( ! hlp - > canMoveBetween ( source . node - > coord , destination . node - > coord ) )
2015-11-11 21:08:15 +02:00
return false ;
if ( isSourceGuarded ( ) )
{
2018-07-29 13:50:22 +02:00
if ( ! ( options . originalMovementRules & & source . node - > layer = = ELayer : : AIR ) & &
2015-11-11 21:08:15 +02:00
! isDestinationGuardian ( ) ) // Can step into tile of guard
2015-11-10 20:07:27 +02:00
{
2015-10-27 02:34:47 +02:00
return false ;
2015-11-11 21:08:15 +02:00
}
}
2015-10-27 02:34:47 +02:00
2015-11-11 21:08:15 +02:00
break ;
2015-11-07 23:26:41 +02:00
2015-11-11 21:08:15 +02:00
case ELayer : : SAIL :
2018-08-01 20:46:06 +02:00
if ( ! hlp - > canMoveBetween ( source . node - > coord , destination . node - > coord ) )
2015-11-11 21:08:15 +02:00
return false ;
2015-11-11 21:29:20 +02:00
if ( isSourceGuarded ( ) )
{
// Hero embarked a boat standing on a guarded tile -> we must allow to move away from that tile
2018-07-29 13:50:22 +02:00
if ( source . node - > action ! = CGPathNode : : EMBARK & & ! isDestinationGuardian ( ) )
2015-11-11 21:29:20 +02:00
return false ;
}
2015-10-27 02:34:47 +02:00
2018-07-29 13:50:22 +02:00
if ( source . node - > layer = = ELayer : : LAND )
2015-11-11 21:08:15 +02:00
{
2018-07-29 13:50:22 +02:00
if ( ! destination . isNodeObjectVisitable ( ) )
2015-11-02 15:03:03 +02:00
return false ;
2015-11-11 21:08:15 +02:00
2018-07-29 13:50:22 +02:00
if ( destination . nodeObject - > ID ! = Obj : : BOAT & & destination . nodeObject - > ID ! = Obj : : HERO )
2015-11-02 15:03:03 +02:00
return false ;
2015-11-11 21:08:15 +02:00
}
2018-07-29 13:50:22 +02:00
else if ( destination . isNodeObjectVisitable ( ) & & destination . nodeObject - > ID = = Obj : : BOAT )
2015-11-13 09:28:44 +02:00
{
/// Hero in boat can't visit empty boats
return false ;
}
2015-11-11 21:29:20 +02:00
2015-11-11 21:08:15 +02:00
break ;
2015-11-02 15:03:03 +02:00
2015-11-11 21:08:15 +02:00
case ELayer : : WATER :
2018-08-01 20:46:06 +02:00
if ( ! hlp - > canMoveBetween ( source . node - > coord , destination . node - > coord ) | | destination . node - > accessible ! = CGPathNode : : ACCESSIBLE )
2015-11-11 21:08:15 +02:00
return false ;
if ( isDestinationGuarded ( ) )
return false ;
break ;
2015-10-27 02:34:47 +02:00
}
return true ;
}
2018-08-01 20:46:06 +02:00
void CMovementAfterDestinationRule : : process ( CPathfinderHelper * pathfinderHelper , CPathNodeInfo & source , CDestinationNodeInfo & destination )
2015-10-27 02:34:47 +02:00
{
2018-08-01 20:46:06 +02:00
switch ( destination . action )
2015-11-04 10:53:52 +02:00
{
2015-11-08 03:41:06 +02:00
/// TODO: Investigate what kind of limitation is possible to apply on movement from visitable tiles
/// Likely in many cases we don't need to add visitable tile to queue when hero don't fly
case CGPathNode : : VISIT :
2015-11-16 18:14:18 +02:00
{
2015-11-10 18:15:59 +02:00
/// For now we only add visitable tile into queue when it's teleporter that allow transit
/// Movement from visitable tile when hero is standing on it is possible into any layer
2018-07-29 13:50:22 +02:00
const CGTeleport * objTeleport = dynamic_cast < const CGTeleport * > ( destination . nodeObject ) ;
2018-08-01 20:46:06 +02:00
if ( pathfinderHelper - > isAllowedTeleportEntrance ( objTeleport ) )
2015-11-08 03:41:06 +02:00
{
/// For now we'll always allow transit over teleporters
/// Transit over whirlpools only allowed when hero protected
2018-07-29 13:50:22 +02:00
return ;
2015-11-08 03:41:06 +02:00
}
2018-08-01 20:46:06 +02:00
else if ( destination . nodeObject - > ID = = Obj : : GARRISON
| | destination . nodeObject - > ID = = Obj : : GARRISON2
2018-07-29 13:50:22 +02:00
| | destination . nodeObject - > ID = = Obj : : BORDER_GATE )
2015-11-17 23:07:25 +02:00
{
/// Transit via unguarded garrisons is always possible
2018-07-29 13:50:22 +02:00
return ;
2015-11-17 23:07:25 +02:00
}
2015-11-11 21:08:15 +02:00
break ;
2015-11-16 18:14:18 +02:00
}
2015-11-11 21:08:15 +02:00
2015-11-08 03:41:06 +02:00
case CGPathNode : : NORMAL :
2018-07-29 13:50:22 +02:00
return ;
2015-11-04 10:53:52 +02:00
2015-11-08 03:41:06 +02:00
case CGPathNode : : EMBARK :
2018-08-01 20:46:06 +02:00
if ( pathfinderHelper - > options . useEmbarkAndDisembark )
2018-07-29 13:50:22 +02:00
return ;
2015-11-04 10:53:52 +02:00
2015-11-11 21:08:15 +02:00
break ;
2015-11-08 03:41:06 +02:00
case CGPathNode : : DISEMBARK :
2018-08-01 20:46:06 +02:00
if ( pathfinderHelper - > options . useEmbarkAndDisembark & & ! destination . guarded )
2018-07-29 13:50:22 +02:00
return ;
2015-11-10 18:15:59 +02:00
2015-11-11 21:08:15 +02:00
break ;
2015-11-10 18:15:59 +02:00
case CGPathNode : : BATTLE :
2015-11-13 22:07:56 +02:00
/// Movement after BATTLE action only possible from guarded tile to guardian tile
2018-08-01 20:46:06 +02:00
if ( destination . guarded )
2018-07-29 13:50:22 +02:00
return ;
2015-11-11 21:08:15 +02:00
break ;
2015-11-04 10:53:52 +02:00
}
2015-10-27 02:34:47 +02:00
2018-07-29 13:50:22 +02:00
destination . furtherProcessingImpossible = true ;
2015-10-27 02:34:47 +02:00
}
2015-11-11 18:51:08 +02:00
CGPathNode : : ENodeAction CPathfinder : : getDestAction ( ) const
{
CGPathNode : : ENodeAction action = CGPathNode : : NORMAL ;
2018-07-29 13:50:22 +02:00
switch ( destination . node - > layer )
2015-11-11 18:51:08 +02:00
{
case ELayer : : LAND :
2018-07-29 13:50:22 +02:00
if ( source . node - > layer = = ELayer : : SAIL )
2015-11-11 18:51:08 +02:00
{
// TODO: Handle dismebark into guarded areaa
action = CGPathNode : : DISEMBARK ;
break ;
}
2015-11-16 22:07:36 +02:00
/// don't break - next case shared for both land and sail layers
2018-02-09 12:02:44 +02:00
FALLTHROUGH
2015-11-16 22:07:36 +02:00
2015-11-11 18:51:08 +02:00
case ELayer : : SAIL :
2018-07-29 13:50:22 +02:00
if ( destination . isNodeObjectVisitable ( ) )
2015-11-11 18:51:08 +02:00
{
2018-07-29 13:50:22 +02:00
auto objRel = getPlayerRelations ( destination . nodeObject - > tempOwner , hero - > tempOwner ) ;
2015-11-11 18:51:08 +02:00
2018-07-29 13:50:22 +02:00
if ( destination . nodeObject - > ID = = Obj : : BOAT )
2015-11-11 18:51:08 +02:00
action = CGPathNode : : EMBARK ;
2018-07-29 13:50:22 +02:00
else if ( destination . nodeObject - > ID = = Obj : : HERO )
2015-11-11 18:51:08 +02:00
{
if ( objRel = = PlayerRelations : : ENEMIES )
action = CGPathNode : : BATTLE ;
else
action = CGPathNode : : BLOCKING_VISIT ;
}
2018-07-29 13:50:22 +02:00
else if ( destination . nodeObject - > ID = = Obj : : TOWN )
2015-11-11 18:51:08 +02:00
{
2018-07-29 13:50:22 +02:00
if ( destination . nodeObject - > passableFor ( hero - > tempOwner ) )
2015-12-14 13:33:50 +02:00
action = CGPathNode : : VISIT ;
else if ( objRel = = PlayerRelations : : ENEMIES )
2015-11-11 18:51:08 +02:00
action = CGPathNode : : BATTLE ;
}
2018-07-29 13:50:22 +02:00
else if ( destination . nodeObject - > ID = = Obj : : GARRISON | | destination . nodeObject - > ID = = Obj : : GARRISON2 )
2015-11-11 18:51:08 +02:00
{
2018-07-29 13:50:22 +02:00
if ( destination . nodeObject - > passableFor ( hero - > tempOwner ) )
2015-12-14 13:33:50 +02:00
{
2018-08-01 20:46:06 +02:00
if ( isDestinationGuarded ( ) )
2015-12-14 13:33:50 +02:00
action = CGPathNode : : BATTLE ;
}
else if ( objRel = = PlayerRelations : : ENEMIES )
2015-11-11 18:51:08 +02:00
action = CGPathNode : : BATTLE ;
}
2018-07-29 13:50:22 +02:00
else if ( destination . nodeObject - > ID = = Obj : : BORDER_GATE )
2015-12-14 13:33:50 +02:00
{
2018-07-29 13:50:22 +02:00
if ( destination . nodeObject - > passableFor ( hero - > tempOwner ) )
2015-12-14 13:33:50 +02:00
{
2018-08-01 20:46:06 +02:00
if ( isDestinationGuarded ( ) )
2015-12-14 13:33:50 +02:00
action = CGPathNode : : BATTLE ;
}
else
action = CGPathNode : : BLOCKING_VISIT ;
}
2015-11-11 18:51:08 +02:00
else if ( isDestinationGuardian ( ) )
action = CGPathNode : : BATTLE ;
2018-07-29 13:50:22 +02:00
else if ( destination . nodeObject - > blockVisit & & ! ( options . useCastleGate & & destination . nodeObject - > ID = = Obj : : TOWN ) )
2015-11-11 18:51:08 +02:00
action = CGPathNode : : BLOCKING_VISIT ;
if ( action = = CGPathNode : : NORMAL )
{
if ( options . originalMovementRules & & isDestinationGuarded ( ) )
action = CGPathNode : : BATTLE ;
else
action = CGPathNode : : VISIT ;
}
}
else if ( isDestinationGuarded ( ) )
action = CGPathNode : : BATTLE ;
break ;
}
return action ;
}
2015-12-11 08:42:30 +02:00
CGPathNode : : ENodeAction CPathfinder : : getTeleportDestAction ( ) const
{
CGPathNode : : ENodeAction action = CGPathNode : : TELEPORT_NORMAL ;
2018-07-29 13:50:22 +02:00
if ( destination . isNodeObjectVisitable ( ) & & destination . nodeObject - > ID = = Obj : : HERO )
2015-12-11 08:42:30 +02:00
{
2018-07-29 13:50:22 +02:00
auto objRel = getPlayerRelations ( destination . nodeObject - > tempOwner , hero - > tempOwner ) ;
2015-12-11 08:42:30 +02:00
if ( objRel = = PlayerRelations : : ENEMIES )
action = CGPathNode : : TELEPORT_BATTLE ;
else
action = CGPathNode : : TELEPORT_BLOCKING_VISIT ;
}
return action ;
}
2015-11-08 07:39:00 +02:00
bool CPathfinder : : isSourceInitialPosition ( ) const
2015-11-04 14:38:15 +02:00
{
2018-07-29 13:50:22 +02:00
return source . node - > coord = = nodeHelper - > getInitialNode ( ) - > coord ;
2015-11-16 16:36:58 +02:00
}
2015-11-08 07:39:00 +02:00
bool CPathfinder : : isSourceGuarded ( ) const
2015-10-27 02:34:47 +02:00
{
2015-11-11 21:29:20 +02:00
/// Hero can move from guarded tile if movement started on that tile
/// It's possible at least in these cases:
/// - Map start with hero on guarded tile
/// - Dimention door used
2015-11-17 06:09:01 +02:00
/// TODO: check what happen when there is several guards
2018-07-29 13:50:22 +02:00
if ( gs - > guardingCreaturePosition ( source . node - > coord ) . valid ( ) & & ! isSourceInitialPosition ( ) )
2015-10-27 02:34:47 +02:00
{
2015-11-11 21:29:20 +02:00
return true ;
2015-10-27 02:34:47 +02:00
}
return false ;
}
2018-08-01 20:46:06 +02:00
bool CPathfinder : : isDestinationGuarded ( ) const
2015-10-27 02:34:47 +02:00
{
2015-11-16 22:07:36 +02:00
/// isDestinationGuarded is exception needed for garrisons.
/// When monster standing behind garrison it's visitable and guarded at the same time.
2018-08-01 20:46:06 +02:00
return gs - > guardingCreaturePosition ( destination . node - > coord ) . valid ( ) ;
2015-10-27 02:34:47 +02:00
}
2015-11-08 07:39:00 +02:00
bool CPathfinder : : isDestinationGuardian ( ) const
2015-10-27 02:34:47 +02:00
{
2018-07-29 13:50:22 +02:00
return gs - > guardingCreaturePosition ( source . node - > coord ) = = destination . node - > coord ;
2015-10-27 02:34:47 +02:00
}
2015-11-28 19:34:50 +02:00
void CPathfinder : : initializePatrol ( )
{
auto state = PATROL_NONE ;
if ( hero - > patrol . patrolling & & ! getPlayer ( hero - > tempOwner ) - > human )
{
2016-01-31 17:01:58 +02:00
if ( hero - > patrol . patrolRadius )
2015-11-28 19:34:50 +02:00
{
state = PATROL_RADIUS ;
2016-02-10 17:36:56 +02:00
gs - > getTilesInRange ( patrolTiles , hero - > patrol . initialPos , hero - > patrol . patrolRadius , boost : : optional < PlayerColor > ( ) , 0 , int3 : : DIST_MANHATTAN ) ;
2015-11-28 19:34:50 +02:00
}
else
state = PATROL_LOCKED ;
}
patrolState = state ;
}
2015-10-27 02:34:47 +02:00
void CPathfinder : : initializeGraph ( )
{
2015-11-17 02:59:02 +02:00
auto updateNode = [ & ] ( int3 pos , ELayer layer , const TerrainTile * tinfo )
2015-11-02 10:06:06 +02:00
{
2018-07-29 13:50:22 +02:00
auto node = nodeHelper - > getNode ( pos , layer ) ;
2015-11-17 02:59:02 +02:00
auto accessibility = evaluateAccessibility ( pos , tinfo , layer ) ;
2015-11-11 23:05:20 +02:00
node - > update ( pos , layer , accessibility ) ;
2015-11-02 10:06:06 +02:00
} ;
2015-11-02 15:03:03 +02:00
int3 pos ;
2018-07-29 13:50:22 +02:00
int3 sizes = gs - > getMapSize ( ) ;
for ( pos . x = 0 ; pos . x < sizes . x ; + + pos . x )
2015-10-27 02:34:47 +02:00
{
2018-07-29 13:50:22 +02:00
for ( pos . y = 0 ; pos . y < sizes . y ; + + pos . y )
2015-10-27 02:34:47 +02:00
{
2018-07-29 13:50:22 +02:00
for ( pos . z = 0 ; pos . z < sizes . z ; + + pos . z )
2015-10-27 02:34:47 +02:00
{
2015-11-21 13:31:30 +02:00
const TerrainTile * tinfo = & gs - > map - > getTile ( pos ) ;
2015-11-05 14:04:56 +02:00
switch ( tinfo - > terType )
2015-11-02 10:06:06 +02:00
{
2015-11-17 02:59:02 +02:00
case ETerrainType : : ROCK :
break ;
case ETerrainType : : WATER :
updateNode ( pos , ELayer : : SAIL , tinfo ) ;
if ( options . useFlying )
updateNode ( pos , ELayer : : AIR , tinfo ) ;
if ( options . useWaterWalking )
updateNode ( pos , ELayer : : WATER , tinfo ) ;
break ;
default :
updateNode ( pos , ELayer : : LAND , tinfo ) ;
if ( options . useFlying )
updateNode ( pos , ELayer : : AIR , tinfo ) ;
break ;
2015-11-02 10:06:06 +02:00
}
2015-10-27 02:34:47 +02:00
}
}
}
}
2015-11-17 02:59:02 +02:00
CGPathNode : : EAccessibility CPathfinder : : evaluateAccessibility ( const int3 & pos , const TerrainTile * tinfo , const ELayer layer ) const
2015-10-27 02:34:47 +02:00
{
2015-11-22 20:06:37 +02:00
if ( tinfo - > terType = = ETerrainType : : ROCK | | ! FoW [ pos . x ] [ pos . y ] [ pos . z ] )
2015-10-27 02:34:47 +02:00
return CGPathNode : : BLOCKED ;
2015-11-17 02:59:02 +02:00
switch ( layer )
2015-10-27 02:34:47 +02:00
{
2015-11-17 02:59:02 +02:00
case ELayer : : LAND :
case ELayer : : SAIL :
if ( tinfo - > visitable )
2015-10-27 02:34:47 +02:00
{
2015-11-17 02:59:02 +02:00
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
2015-10-27 02:34:47 +02:00
{
2015-11-17 02:59:02 +02:00
return CGPathNode : : BLOCKED ;
}
else
{
for ( const CGObjectInstance * obj : tinfo - > visitableObjects )
2015-10-27 02:34:47 +02:00
{
2015-11-22 20:31:47 +02:00
if ( obj - > blockVisit )
2015-11-17 02:59:02 +02:00
{
2015-11-22 20:31:47 +02:00
return CGPathNode : : BLOCKVIS ;
2015-11-17 02:59:02 +02:00
}
2015-11-22 20:31:47 +02:00
else if ( obj - > passableFor ( hero - > tempOwner ) )
2015-11-17 02:59:02 +02:00
{
2015-11-22 20:31:47 +02:00
return CGPathNode : : ACCESSIBLE ;
2015-11-17 02:59:02 +02:00
}
2015-11-17 06:09:01 +02:00
else if ( canSeeObj ( obj ) )
2015-11-17 02:59:02 +02:00
{
return CGPathNode : : VISITABLE ;
}
2015-10-27 02:34:47 +02:00
}
}
}
2015-11-22 20:31:47 +02:00
else if ( tinfo - > blocked )
{
return CGPathNode : : BLOCKED ;
}
else if ( gs - > guardingCreaturePosition ( pos ) . valid ( ) )
2015-11-17 02:59:02 +02:00
{
// Monster close by; blocked visit for battle
return CGPathNode : : BLOCKVIS ;
}
break ;
case ELayer : : WATER :
if ( tinfo - > blocked | | tinfo - > terType ! = ETerrainType : : WATER )
return CGPathNode : : BLOCKED ;
break ;
case ELayer : : AIR :
if ( tinfo - > blocked | | tinfo - > terType = = ETerrainType : : WATER )
return CGPathNode : : FLYABLE ;
break ;
2015-10-27 02:34:47 +02:00
}
2015-11-17 02:59:02 +02:00
return CGPathNode : : ACCESSIBLE ;
2015-10-27 02:34:47 +02:00
}
2018-08-01 20:46:06 +02:00
bool CPathfinderHelper : : canMoveBetween ( const int3 & a , const int3 & b ) const
2015-10-27 02:34:47 +02:00
{
return gs - > checkForVisitableDir ( a , b ) ;
}
2018-08-01 20:46:06 +02:00
bool CPathfinderHelper : : isAllowedTeleportEntrance ( const CGTeleport * obj ) const
2015-11-16 18:14:18 +02:00
{
2015-11-16 20:22:11 +02:00
if ( ! obj | | ! isTeleportEntrancePassable ( obj , hero - > tempOwner ) )
2015-11-16 18:14:18 +02:00
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 ;
}
2018-08-01 20:46:06 +02:00
bool CPathfinderHelper : : addTeleportTwoWay ( const CGTeleport * obj ) const
2015-10-27 02:34:47 +02:00
{
2015-11-16 20:22:11 +02:00
return options . useTeleportTwoWay & & isTeleportChannelBidirectional ( obj - > channel , hero - > tempOwner ) ;
2015-10-27 02:34:47 +02:00
}
2018-08-01 20:46:06 +02:00
bool CPathfinderHelper : : addTeleportOneWay ( const CGTeleport * obj ) const
2015-10-27 02:34:47 +02:00
{
if ( options . useTeleportOneWay & & isTeleportChannelUnidirectional ( obj - > channel , hero - > tempOwner ) )
{
2015-11-16 20:22:11 +02:00
auto passableExits = CGTeleport : : getPassableExits ( gs , hero , getTeleportChannelExits ( obj - > channel , hero - > tempOwner ) ) ;
2015-10-27 02:34:47 +02:00
if ( passableExits . size ( ) = = 1 )
return true ;
}
return false ;
}
2018-08-01 20:46:06 +02:00
bool CPathfinderHelper : : addTeleportOneWayRandom ( const CGTeleport * obj ) const
2015-10-27 02:34:47 +02:00
{
if ( options . useTeleportOneWayRandom & & isTeleportChannelUnidirectional ( obj - > channel , hero - > tempOwner ) )
{
2015-11-16 20:22:11 +02:00
auto passableExits = CGTeleport : : getPassableExits ( gs , hero , getTeleportChannelExits ( obj - > channel , hero - > tempOwner ) ) ;
2015-10-27 02:34:47 +02:00
if ( passableExits . size ( ) > 1 )
return true ;
}
return false ;
}
2018-08-01 20:46:06 +02:00
bool CPathfinderHelper : : addTeleportWhirlpool ( const CGWhirlpool * obj ) const
2015-10-27 02:34:47 +02:00
{
2018-08-01 20:46:06 +02:00
return options . useTeleportWhirlpool & & hasBonusOfType ( Bonus : : WHIRLPOOL_PROTECTION ) & & obj ;
2015-10-27 02:34:47 +02:00
}
2015-11-21 09:00:09 +02:00
TurnInfo : : BonusCache : : BonusCache ( TBonusListPtr bl )
{
noTerrainPenalty . reserve ( ETerrainType : : ROCK ) ;
for ( int i = 0 ; i < ETerrainType : : ROCK ; i + + )
{
2016-09-19 23:36:35 +02:00
noTerrainPenalty . push_back ( static_cast < bool > (
bl - > getFirst ( Selector : : type ( Bonus : : NO_TERRAIN_PENALTY ) . And ( Selector : : subtype ( i ) ) ) ) ) ;
2015-11-21 09:00:09 +02:00
}
2016-09-19 23:36:35 +02:00
freeShipBoarding = static_cast < bool > ( bl - > getFirst ( Selector : : type ( Bonus : : FREE_SHIP_BOARDING ) ) ) ;
flyingMovement = static_cast < bool > ( bl - > getFirst ( Selector : : type ( Bonus : : FLYING_MOVEMENT ) ) ) ;
2015-11-21 09:00:09 +02:00
flyingMovementVal = bl - > valOfBonuses ( Selector : : type ( Bonus : : FLYING_MOVEMENT ) ) ;
2016-09-19 23:36:35 +02:00
waterWalking = static_cast < bool > ( bl - > getFirst ( Selector : : type ( Bonus : : WATER_WALKING ) ) ) ;
2015-11-21 09:00:09 +02:00
waterWalkingVal = bl - > valOfBonuses ( Selector : : type ( Bonus : : WATER_WALKING ) ) ;
}
2015-11-12 13:04:33 +02:00
TurnInfo : : TurnInfo ( const CGHeroInstance * Hero , const int turn )
: hero ( Hero ) , maxMovePointsLand ( - 1 ) , maxMovePointsWater ( - 1 )
2015-11-12 04:20:32 +02:00
{
2015-11-20 11:28:35 +02:00
std : : stringstream cachingStr ;
cachingStr < < " days_ " < < turn ;
bonuses = hero - > getAllBonuses ( Selector : : days ( turn ) , nullptr , nullptr , cachingStr . str ( ) ) ;
2015-11-21 09:00:09 +02:00
bonusCache = make_unique < BonusCache > ( bonuses ) ;
2015-11-21 12:30:39 +02:00
nativeTerrain = hero - > getNativeTerrain ( ) ;
2015-11-12 13:04:33 +02:00
}
2015-11-12 13:39:22 +02:00
bool TurnInfo : : isLayerAvailable ( const EPathfindingLayer layer ) const
{
switch ( layer )
{
case EPathfindingLayer : : AIR :
if ( ! hasBonusOfType ( Bonus : : FLYING_MOVEMENT ) )
return false ;
break ;
case EPathfindingLayer : : WATER :
if ( ! hasBonusOfType ( Bonus : : WATER_WALKING ) )
return false ;
break ;
}
return true ;
}
2015-11-12 13:04:33 +02:00
bool TurnInfo : : hasBonusOfType ( Bonus : : BonusType type , int subtype ) const
{
2015-11-21 09:00:09 +02:00
switch ( type )
{
case Bonus : : FREE_SHIP_BOARDING :
return bonusCache - > freeShipBoarding ;
case Bonus : : FLYING_MOVEMENT :
return bonusCache - > flyingMovement ;
case Bonus : : WATER_WALKING :
return bonusCache - > waterWalking ;
case Bonus : : NO_TERRAIN_PENALTY :
return bonusCache - > noTerrainPenalty [ subtype ] ;
}
2016-09-19 23:36:35 +02:00
return static_cast < bool > (
bonuses - > getFirst ( Selector : : type ( type ) . And ( Selector : : subtype ( subtype ) ) ) ) ;
2015-11-12 13:04:33 +02:00
}
int TurnInfo : : valOfBonuses ( Bonus : : BonusType type , int subtype ) const
{
2015-11-21 09:00:09 +02:00
switch ( type )
{
case Bonus : : FLYING_MOVEMENT :
return bonusCache - > flyingMovementVal ;
case Bonus : : WATER_WALKING :
return bonusCache - > waterWalkingVal ;
}
2015-11-12 13:04:33 +02:00
return bonuses - > valOfBonuses ( Selector : : type ( type ) . And ( Selector : : subtype ( subtype ) ) ) ;
2015-11-12 04:20:32 +02:00
}
int TurnInfo : : getMaxMovePoints ( const EPathfindingLayer layer ) const
{
2015-11-12 13:04:33 +02:00
if ( maxMovePointsLand = = - 1 )
maxMovePointsLand = hero - > maxMovePoints ( true , this ) ;
if ( maxMovePointsWater = = - 1 )
maxMovePointsWater = hero - > maxMovePoints ( false , this ) ;
2015-11-12 04:20:32 +02:00
return layer = = EPathfindingLayer : : SAIL ? maxMovePointsWater : maxMovePointsLand ;
}
2018-08-01 20:46:06 +02:00
CPathfinderHelper : : CPathfinderHelper ( CGameState * gs , const CGHeroInstance * Hero , const PathfinderOptions & Options )
: CGameInfoCallback ( gs , boost : : optional < PlayerColor > ( ) ) , turn ( - 1 ) , hero ( Hero ) , options ( Options )
2015-11-10 13:26:45 +02:00
{
turnsInfo . reserve ( 16 ) ;
updateTurnInfo ( ) ;
}
2016-08-16 13:59:16 +02:00
CPathfinderHelper : : ~ CPathfinderHelper ( )
{
for ( auto ti : turnsInfo )
delete ti ;
}
2015-11-12 13:04:33 +02:00
void CPathfinderHelper : : updateTurnInfo ( const int Turn )
2015-11-10 13:26:45 +02:00
{
2015-11-12 13:04:33 +02:00
if ( turn ! = Turn )
2015-11-10 13:26:45 +02:00
{
2015-11-12 13:04:33 +02:00
turn = Turn ;
if ( turn > = turnsInfo . size ( ) )
2015-11-10 13:26:45 +02:00
{
2015-11-12 13:04:33 +02:00
auto ti = new TurnInfo ( hero , turn ) ;
2015-11-10 13:26:45 +02:00
turnsInfo . push_back ( ti ) ;
}
}
}
2015-11-12 13:39:22 +02:00
bool CPathfinderHelper : : isLayerAvailable ( const EPathfindingLayer layer ) const
{
2015-11-16 17:43:02 +02:00
switch ( layer )
{
case EPathfindingLayer : : AIR :
if ( ! options . useFlying )
return false ;
break ;
case EPathfindingLayer : : WATER :
if ( ! options . useWaterWalking )
return false ;
break ;
}
2015-11-12 13:39:22 +02:00
return turnsInfo [ turn ] - > isLayerAvailable ( layer ) ;
}
2015-11-12 13:04:33 +02:00
const TurnInfo * CPathfinderHelper : : getTurnInfo ( ) const
2015-11-10 13:26:45 +02:00
{
2015-11-12 04:20:32 +02:00
return turnsInfo [ turn ] ;
2015-11-10 13:26:45 +02:00
}
2015-11-12 13:04:33 +02:00
bool CPathfinderHelper : : hasBonusOfType ( const Bonus : : BonusType type , const int subtype ) const
{
return turnsInfo [ turn ] - > hasBonusOfType ( type , subtype ) ;
}
int CPathfinderHelper : : getMaxMovePoints ( const EPathfindingLayer layer ) const
2015-11-10 13:26:45 +02:00
{
2015-11-12 04:20:32 +02:00
return turnsInfo [ turn ] - > getMaxMovePoints ( layer ) ;
2015-11-10 13:26:45 +02:00
}
2018-08-01 20:46:06 +02:00
void CPathfinderHelper : : getNeighbours ( const TerrainTile & srct , const int3 & tile , std : : vector < int3 > & vec , const boost : : logic : : tribool & onLand , const bool limitCoastSailing )
2015-11-10 01:15:27 +02:00
{
2018-08-01 20:46:06 +02:00
CMap * map = gs - > map ;
2015-11-17 01:41:31 +02:00
static const int3 dirs [ ] = {
int3 ( - 1 , + 1 , + 0 ) , int3 ( 0 , + 1 , + 0 ) , int3 ( + 1 , + 1 , + 0 ) ,
int3 ( - 1 , + 0 , + 0 ) , /* source pos */ int3 ( + 1 , + 0 , + 0 ) ,
int3 ( - 1 , - 1 , + 0 ) , int3 ( 0 , - 1 , + 0 ) , int3 ( + 1 , - 1 , + 0 )
} ;
2015-11-10 01:15:27 +02:00
2015-11-11 21:08:15 +02:00
for ( auto & dir : dirs )
2015-11-10 01:15:27 +02:00
{
const int3 hlp = tile + dir ;
2015-11-16 20:22:11 +02:00
if ( ! map - > isInTheMap ( hlp ) )
2015-11-10 01:15:27 +02:00
continue ;
2015-11-16 20:22:11 +02:00
const TerrainTile & hlpt = map - > getTile ( hlp ) ;
2015-11-17 01:41:31 +02:00
if ( hlpt . terType = = ETerrainType : : ROCK )
continue ;
2015-11-10 01:15:27 +02:00
// //we cannot visit things from blocked tiles
// if(srct.blocked && !srct.visitable && hlpt.visitable && srct.blockingObjects.front()->ID != HEROI_TYPE)
// {
// continue;
// }
2015-11-17 01:41:31 +02:00
/// Following condition let us avoid diagonal movement over coast when sailing
2015-11-10 01:15:27 +02:00
if ( srct . terType = = ETerrainType : : WATER & & limitCoastSailing & & hlpt . terType = = ETerrainType : : WATER & & dir . x & & dir . y ) //diagonal move through water
{
int3 hlp1 = tile ,
hlp2 = tile ;
hlp1 . x + = dir . x ;
hlp2 . y + = dir . y ;
2015-11-16 20:22:11 +02:00
if ( map - > getTile ( hlp1 ) . terType ! = ETerrainType : : WATER | | map - > getTile ( hlp2 ) . terType ! = ETerrainType : : WATER )
2015-11-10 01:15:27 +02:00
continue ;
}
2015-11-17 01:41:31 +02:00
if ( indeterminate ( onLand ) | | onLand = = ( hlpt . terType ! = ETerrainType : : WATER ) )
2015-11-10 01:15:27 +02:00
{
vec . push_back ( hlp ) ;
}
}
}
2018-08-01 20:46:06 +02:00
int CPathfinderHelper : : getMovementCost ( const int3 & src , const int3 & dst , const TerrainTile * ct , const TerrainTile * dt , const int remainingMovePoints , const bool checkLast )
2015-11-10 01:15:27 +02:00
{
if ( src = = dst ) //same tile
return 0 ;
2018-08-01 20:46:06 +02:00
auto ti = getTurnInfo ( ) ;
2015-11-10 13:26:45 +02:00
2015-11-21 13:31:30 +02:00
if ( ct = = nullptr | | dt = = nullptr )
{
2018-08-01 20:46:06 +02:00
ct = hero - > cb - > getTile ( src ) ;
dt = hero - > cb - > getTile ( dst ) ;
2015-11-21 13:31:30 +02:00
}
2015-11-16 22:07:36 +02:00
/// TODO: by the original game rules hero shouldn't be affected by terrain penalty while flying.
/// Also flying movement only has penalty when player moving over blocked tiles.
/// So if you only have base flying with 40% penalty you can still ignore terrain penalty while having zero flying penalty.
2018-08-01 20:46:06 +02:00
int ret = hero - > getTileCost ( * dt , * ct , ti ) ;
2015-11-16 22:07:36 +02:00
/// Unfortunately this can't be implemented yet as server don't know when player flying and when he's not.
/// Difference in cost calculation on client and server is much worse than incorrect cost.
/// So this one is waiting till server going to use pathfinder rules for path validation.
2015-11-10 01:15:27 +02:00
2015-11-21 13:31:30 +02:00
if ( dt - > blocked & & ti - > hasBonusOfType ( Bonus : : FLYING_MOVEMENT ) )
2015-11-10 01:15:27 +02:00
{
2015-11-12 13:04:33 +02:00
ret * = ( 100.0 + ti - > valOfBonuses ( Bonus : : FLYING_MOVEMENT ) ) / 100.0 ;
2015-11-10 01:15:27 +02:00
}
2015-11-21 13:31:30 +02:00
else if ( dt - > terType = = ETerrainType : : WATER )
2015-11-10 01:15:27 +02:00
{
2018-08-01 20:46:06 +02:00
if ( hero - > boat & & ct - > hasFavorableWinds ( ) & & dt - > hasFavorableWinds ( ) )
2015-11-10 01:15:27 +02:00
ret * = 0.666 ;
2018-08-01 20:46:06 +02:00
else if ( ! hero - > boat & & ti - > hasBonusOfType ( Bonus : : WATER_WALKING ) )
2015-11-10 01:15:27 +02:00
{
2015-11-12 13:04:33 +02:00
ret * = ( 100.0 + ti - > valOfBonuses ( Bonus : : WATER_WALKING ) ) / 100.0 ;
2015-11-10 01:15:27 +02:00
}
}
if ( src . x ! = dst . x & & src . y ! = dst . y ) //it's diagonal move
{
int old = ret ;
ret * = 1.414213 ;
//diagonal move costs too much but normal move is possible - allow diagonal move for remaining move points
if ( ret > remainingMovePoints & & remainingMovePoints > = old )
2016-08-16 14:47:21 +02:00
{
2015-11-10 01:15:27 +02:00
return remainingMovePoints ;
2016-08-16 14:47:21 +02:00
}
2015-11-10 01:15:27 +02:00
}
2015-11-16 22:07:36 +02:00
/// TODO: This part need rework in order to work properly with flying and water walking
/// Currently it's only work properly for normal movement or sailing
2015-11-10 01:15:27 +02:00
int left = remainingMovePoints - ret ;
if ( checkLast & & left > 0 & & remainingMovePoints - ret < 250 ) //it might be the last tile - if no further move possible we take all move points
{
std : : vector < int3 > vec ;
vec . reserve ( 8 ) ; //optimization
2018-08-01 20:46:06 +02:00
getNeighbours ( * dt , dst , vec , ct - > terType ! = ETerrainType : : WATER , true ) ;
2015-11-10 01:15:27 +02:00
for ( auto & elem : vec )
{
2018-08-01 20:46:06 +02:00
int fcost = getMovementCost ( dst , elem , nullptr , nullptr , left , false ) ;
2015-11-10 01:15:27 +02:00
if ( fcost < = left )
2016-08-16 14:47:21 +02:00
{
2015-11-10 01:15:27 +02:00
return ret ;
2016-08-16 14:47:21 +02:00
}
2015-11-10 01:15:27 +02:00
}
ret = remainingMovePoints ;
}
2016-08-16 14:47:21 +02:00
2015-11-10 01:15:27 +02:00
return ret ;
}
2015-11-11 23:05:20 +02:00
CGPathNode : : CGPathNode ( )
: coord ( int3 ( - 1 , - 1 , - 1 ) ) , layer ( ELayer : : WRONG )
2015-11-07 21:16:45 +02:00
{
reset ( ) ;
}
void CGPathNode : : reset ( )
2015-10-27 02:34:47 +02:00
{
2015-11-07 20:11:07 +02:00
locked = false ;
2015-10-27 02:34:47 +02:00
accessible = NOT_SET ;
moveRemains = 0 ;
turns = 255 ;
theNodeBefore = nullptr ;
2015-11-07 23:26:41 +02:00
action = UNKNOWN ;
2015-10-27 02:34:47 +02:00
}
2015-11-11 23:05:20 +02:00
void CGPathNode : : update ( const int3 & Coord , const ELayer Layer , const EAccessibility Accessible )
{
if ( layer = = ELayer : : WRONG )
{
coord = Coord ;
layer = Layer ;
}
else
reset ( ) ;
accessible = Accessible ;
}
2015-10-27 02:34:47 +02:00
bool CGPathNode : : reachable ( ) const
{
return turns < 255 ;
}
int3 CGPath : : startPos ( ) const
{
return nodes [ nodes . size ( ) - 1 ] . coord ;
}
int3 CGPath : : endPos ( ) const
{
return nodes [ 0 ] . coord ;
}
2015-11-05 14:04:56 +02:00
void CGPath : : convert ( ui8 mode )
2015-10-27 02:34:47 +02:00
{
if ( mode = = 0 )
{
for ( auto & elem : nodes )
{
elem . coord = CGHeroInstance : : convertPosition ( elem . coord , true ) ;
}
}
}
2015-11-11 21:08:15 +02:00
CPathsInfo : : CPathsInfo ( const int3 & Sizes )
2015-11-05 14:04:56 +02:00
: sizes ( Sizes )
2015-10-27 02:34:47 +02:00
{
hero = nullptr ;
2015-11-08 07:27:51 +02:00
nodes . resize ( boost : : extents [ sizes . x ] [ sizes . y ] [ sizes . z ] [ ELayer : : NUM_LAYERS ] ) ;
2015-10-27 02:34:47 +02:00
}
CPathsInfo : : ~ CPathsInfo ( )
{
}
2015-11-11 23:05:20 +02:00
const CGPathNode * CPathsInfo : : getPathInfo ( const int3 & tile ) const
2015-10-27 02:34:47 +02:00
{
2015-11-11 23:05:20 +02:00
assert ( vstd : : iswithin ( tile . x , 0 , sizes . x ) ) ;
assert ( vstd : : iswithin ( tile . y , 0 , sizes . y ) ) ;
assert ( vstd : : iswithin ( tile . z , 0 , sizes . z ) ) ;
2015-10-27 02:34:47 +02:00
2015-11-11 23:05:20 +02:00
boost : : unique_lock < boost : : mutex > pathLock ( pathMx ) ;
return getNode ( tile ) ;
2015-10-27 02:34:47 +02:00
}
2015-11-11 23:05:20 +02:00
bool CPathsInfo : : getPath ( CGPath & out , const int3 & dst ) const
2015-10-27 02:34:47 +02:00
{
boost : : unique_lock < boost : : mutex > pathLock ( pathMx ) ;
out . nodes . clear ( ) ;
2015-11-11 23:05:20 +02:00
const CGPathNode * curnode = getNode ( dst ) ;
2015-10-27 02:34:47 +02:00
if ( ! curnode - > theNodeBefore )
return false ;
while ( curnode )
{
2015-11-11 23:05:20 +02:00
const CGPathNode cpn = * curnode ;
2015-10-27 02:34:47 +02:00
curnode = curnode - > theNodeBefore ;
out . nodes . push_back ( cpn ) ;
}
return true ;
}
2015-11-11 23:05:20 +02:00
int CPathsInfo : : getDistance ( const int3 & tile ) const
2015-10-27 02:34:47 +02:00
{
boost : : unique_lock < boost : : mutex > pathLock ( pathMx ) ;
CGPath ret ;
2015-11-11 23:05:20 +02:00
if ( getPath ( ret , tile ) )
2015-10-27 02:34:47 +02:00
return ret . nodes . size ( ) ;
else
return 255 ;
}
2015-11-02 13:04:26 +02:00
2015-11-11 23:05:20 +02:00
const CGPathNode * CPathsInfo : : getNode ( const int3 & coord ) const
2015-11-02 13:04:26 +02:00
{
2015-11-11 23:05:20 +02:00
auto landNode = & nodes [ coord . x ] [ coord . y ] [ coord . z ] [ ELayer : : LAND ] ;
if ( landNode - > reachable ( ) )
2015-11-02 13:04:26 +02:00
return landNode ;
else
2015-11-11 23:05:20 +02:00
return & nodes [ coord . x ] [ coord . y ] [ coord . z ] [ ELayer : : SAIL ] ;
}
CGPathNode * CPathsInfo : : getNode ( const int3 & coord , const ELayer layer )
{
return & nodes [ coord . x ] [ coord . y ] [ coord . z ] [ layer ] ;
2015-11-02 13:04:26 +02:00
}
2018-07-29 13:50:22 +02:00
CPathNodeInfo : : CPathNodeInfo ( )
2018-08-01 20:46:06 +02:00
: node ( nullptr ) , nodeObject ( nullptr ) , tile ( nullptr ) , coord ( - 1 , - 1 , - 1 ) , guarded ( false )
2018-07-29 13:50:22 +02:00
{
}
void CPathNodeInfo : : setNode ( CGameState * gs , CGPathNode * n , bool excludeTopObject )
{
node = n ;
if ( coord ! = node - > coord )
{
assert ( node - > coord . valid ( ) ) ;
coord = node - > coord ;
tile = gs - > getTile ( coord ) ;
nodeObject = tile - > topVisitableObj ( excludeTopObject ) ;
}
2018-08-01 20:46:06 +02:00
guarded = false ;
}
CDestinationNodeInfo : : CDestinationNodeInfo ( )
: CPathNodeInfo ( ) , blocked ( false ) , furtherProcessingImpossible ( false ) , action ( CGPathNode : : ENodeAction : : UNKNOWN )
{
}
void CDestinationNodeInfo : : setNode ( CGameState * gs , CGPathNode * n , bool excludeTopObject )
{
CPathNodeInfo : : setNode ( gs , n , excludeTopObject ) ;
2018-07-29 13:50:22 +02:00
blocked = false ;
furtherProcessingImpossible = false ;
2018-08-01 20:46:06 +02:00
action = CGPathNode : : ENodeAction : : UNKNOWN ;
2018-07-29 13:50:22 +02:00
}
bool CPathNodeInfo : : isNodeObjectVisitable ( ) const
{
/// Hero can't visit objects while walking on water or flying
return canSeeObj ( nodeObject ) & & ( node - > layer = = EPathfindingLayer : : LAND | | node - > layer = = EPathfindingLayer : : SAIL ) ;
}