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"
/*
* 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
*
*/
CPathfinder : : PathfinderOptions : : PathfinderOptions ( )
{
useFlying = false ;
useWaterWalking = false ;
useEmbarkAndDisembark = true ;
useTeleportTwoWay = true ;
useTeleportOneWay = true ;
useTeleportOneWayRandom = false ;
useTeleportWhirlpool = false ;
2015-11-04 14:05:22 +02:00
lightweightFlyingMode = false ;
2015-10-27 02:34:47 +02:00
}
2015-11-05 14:04:56 +02:00
CPathfinder : : CPathfinder ( CPathsInfo & _out , CGameState * _gs , const CGHeroInstance * _hero )
: CGameInfoCallback ( _gs , boost : : optional < PlayerColor > ( ) ) , out ( _out ) , hero ( _hero )
2015-10-27 02:34:47 +02:00
{
assert ( hero ) ;
assert ( hero = = getHero ( hero - > id ) ) ;
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... " ;
throw std : : runtime_error ( " Wrong checksum " ) ;
}
if ( hero - > canFly ( ) )
options . useFlying = true ;
if ( hero - > canWalkOnSea ( ) )
options . useWaterWalking = true ;
if ( CGWhirlpool : : isProtected ( hero ) )
options . useTeleportWhirlpool = true ;
2015-11-03 00:29:43 +02:00
initializeGraph ( ) ;
2015-10-27 02:34:47 +02:00
neighbours . reserve ( 16 ) ;
}
void CPathfinder : : calculatePaths ( )
{
int maxMovePointsLand = hero - > maxMovePoints ( true ) ;
int maxMovePointsWater = hero - > maxMovePoints ( false ) ;
auto maxMovePoints = [ & ] ( CGPathNode * cp ) - > int
{
return cp - > land ? maxMovePointsLand : maxMovePointsWater ;
} ;
auto isBetterWay = [ & ] ( int remains , int turn ) - > bool
{
if ( dp - > turns = = 0xff ) //we haven't been here before
return true ;
else if ( dp - > turns > turn )
return true ;
else if ( dp - > turns > = turn & & dp - > moveRemains < remains ) //this route is faster
return true ;
return false ;
} ;
//logGlobal->infoStream() << boost::format("Calculating paths for hero %s (adress %d) of player %d") % hero->name % hero % hero->tempOwner;
//initial tile - set cost on 0 and add to the queue
2015-11-03 00:29:43 +02:00
CGPathNode * initialNode = out . getNode ( out . hpos , hero - > boat ? EPathfindingLayer : : SAIL : EPathfindingLayer : : LAND ) ;
initialNode - > turns = 0 ;
initialNode - > moveRemains = hero - > movement ;
2015-11-07 20:11:07 +02:00
pq . push ( initialNode ) ;
2015-10-27 02:34:47 +02:00
2015-11-07 20:11:07 +02:00
while ( ! pq . empty ( ) )
2015-10-27 02:34:47 +02:00
{
2015-11-07 20:11:07 +02:00
cp = pq . top ( ) ;
pq . pop ( ) ;
cp - > locked = true ;
2015-10-27 02:34:47 +02:00
int movement = cp - > moveRemains , turn = cp - > turns ;
if ( ! movement )
{
movement = maxMovePoints ( cp ) ;
turn + + ;
}
//add accessible neighbouring nodes to the queue
addNeighbours ( cp - > coord ) ;
for ( auto & neighbour : neighbours )
{
dt = & gs - > map - > getTile ( neighbour ) ;
2015-11-02 15:03:03 +02:00
for ( EPathfindingLayer i = EPathfindingLayer : : LAND ; i < = EPathfindingLayer : : AIR ; i . advance ( 1 ) )
2015-10-27 02:34:47 +02:00
{
2015-11-02 15:03:03 +02:00
useEmbarkCost = 0 ; //0 - usual movement; 1 - embark; 2 - disembark
dp = out . getNode ( neighbour , i ) ;
2015-11-03 02:25:12 +02:00
if ( dp - > accessible = = CGPathNode : : NOT_SET )
continue ;
2015-11-07 20:11:07 +02:00
if ( dp - > locked )
continue ;
2015-11-03 02:25:12 +02:00
if ( cp - > layer ! = i & & ! isLayerTransitionPossible ( ) )
2015-11-02 15:03:03 +02:00
continue ;
2015-11-04 10:47:43 +02:00
if ( ! isMovementToDestPossible ( ) )
2015-11-02 15:03:03 +02:00
continue ;
2015-11-03 02:25:12 +02:00
2015-11-02 15:03:03 +02:00
int cost = gs - > getMovementCost ( hero , cp - > coord , dp - > coord , movement ) ;
int remains = movement - cost ;
if ( useEmbarkCost )
{
remains = hero - > movementPointsAfterEmbark ( movement , cost , useEmbarkCost - 1 ) ;
cost = movement - remains ;
}
2015-10-27 02:34:47 +02:00
2015-11-02 15:03:03 +02:00
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 , moveAtNextTile ) ; //cost must be updated, movement points changed :(
remains = moveAtNextTile - cost ;
}
2015-10-27 02:34:47 +02:00
2015-11-02 15:03:03 +02:00
if ( isBetterWay ( remains , turnAtNextTile ) )
{
assert ( dp ! = cp - > theNodeBefore ) ; //two tiles can't point to each other
dp - > moveRemains = remains ;
dp - > turns = turnAtNextTile ;
dp - > theNodeBefore = cp ;
2015-10-27 02:34:47 +02:00
2015-11-04 10:47:43 +02:00
if ( isMovementAfterDestPossible ( ) )
2015-11-07 20:11:07 +02:00
pq . push ( dp ) ;
2015-11-02 15:03:03 +02:00
}
2015-10-27 02:34:47 +02:00
}
} //neighbours loop
//just add all passable teleport exits
2015-11-02 10:06:06 +02:00
if ( sTileObj & & canVisitObject ( ) )
2015-10-27 02:34:47 +02:00
{
addTeleportExits ( ) ;
for ( auto & neighbour : neighbours )
{
2015-11-02 15:03:03 +02:00
dp = out . getNode ( neighbour , cp - > layer ) ;
2015-11-07 20:11:07 +02:00
if ( dp - > locked )
continue ;
2015-10-27 02:34:47 +02:00
if ( isBetterWay ( movement , turn ) )
{
dp - > moveRemains = movement ;
dp - > turns = turn ;
dp - > theNodeBefore = cp ;
2015-11-07 20:11:07 +02:00
pq . push ( dp ) ;
2015-10-27 02:34:47 +02:00
}
}
}
} //queue loop
}
void CPathfinder : : addNeighbours ( const int3 & coord )
{
neighbours . clear ( ) ;
ct = & gs - > map - > getTile ( coord ) ;
std : : vector < int3 > tiles ;
gs - > getNeighbours ( * ct , coord , tiles , boost : : logic : : indeterminate , ! cp - > land ) ;
2015-11-04 14:38:15 +02:00
sTileObj = ct - > topVisitableObj ( coord = = out . hpos ) ;
2015-11-02 10:06:06 +02:00
if ( canVisitObject ( ) )
2015-10-27 02:34:47 +02:00
{
2015-11-02 10:06:06 +02:00
if ( sTileObj )
2015-10-27 02:34:47 +02:00
{
2015-11-02 10:06:06 +02:00
for ( int3 tile : tiles )
{
if ( canMoveBetween ( tile , sTileObj - > visitablePos ( ) ) )
neighbours . push_back ( tile ) ;
}
2015-10-27 02:34:47 +02:00
}
2015-11-02 10:06:06 +02:00
else
vstd : : concatenate ( neighbours , tiles ) ;
2015-10-27 02:34:47 +02:00
}
else
vstd : : concatenate ( neighbours , tiles ) ;
}
void CPathfinder : : addTeleportExits ( bool noTeleportExcludes )
{
assert ( sTileObj ) ;
neighbours . clear ( ) ;
auto isAllowedTeleportEntrance = [ & ] ( const CGTeleport * obj ) - > bool
{
if ( ! gs - > isTeleportEntrancePassable ( obj , hero - > tempOwner ) )
return false ;
if ( noTeleportExcludes )
return true ;
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 ;
} ;
const CGTeleport * sTileTeleport = dynamic_cast < const CGTeleport * > ( sTileObj ) ;
if ( isAllowedTeleportEntrance ( sTileTeleport ) )
{
for ( auto objId : gs - > getTeleportChannelExits ( sTileTeleport - > channel , hero - > tempOwner ) )
{
auto obj = getObj ( objId ) ;
if ( CGTeleport : : isExitPassable ( gs , hero , obj ) )
neighbours . push_back ( obj - > visitablePos ( ) ) ;
}
}
}
2015-11-04 11:29:51 +02:00
bool CPathfinder : : isLayerTransitionPossible ( )
{
if ( ( cp - > layer = = EPathfindingLayer : : AIR | | cp - > layer = = EPathfindingLayer : : WATER )
& & dp - > layer ! = EPathfindingLayer : : LAND )
{
return false ;
}
2015-11-04 14:05:22 +02:00
else if ( cp - > layer = = EPathfindingLayer : : LAND & & dp - > layer = = EPathfindingLayer : : AIR )
{
2015-11-04 14:38:15 +02:00
if ( options . lightweightFlyingMode & & ! isSourceInitialPosition ( ) )
2015-11-04 14:05:22 +02:00
return false ;
}
2015-11-04 11:29:51 +02:00
else if ( cp - > layer = = EPathfindingLayer : : SAIL & & dp - > layer ! = EPathfindingLayer : : LAND )
return false ;
else if ( cp - > layer = = EPathfindingLayer : : SAIL & & dp - > layer = = EPathfindingLayer : : LAND )
{
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 ;
}
else if ( cp - > layer = = EPathfindingLayer : : LAND & & dp - > layer = = EPathfindingLayer : : SAIL )
{
Obj destTopVisObjID = dt - > topVisitableId ( ) ;
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 ;
}
return true ;
}
2015-11-04 10:47:43 +02:00
bool CPathfinder : : isMovementToDestPossible ( )
2015-10-27 02:34:47 +02:00
{
2015-11-05 14:04:56 +02:00
switch ( dp - > layer )
2015-10-27 02:34:47 +02:00
{
2015-11-02 15:03:03 +02:00
case EPathfindingLayer : : LAND :
if ( ! canMoveBetween ( cp - > coord , dp - > coord ) | | dp - > accessible = = CGPathNode : : BLOCKED )
2015-10-27 02:34:47 +02:00
return false ;
2015-11-02 15:03:03 +02:00
if ( isSourceGuarded ( ) & & ! isDestinationGuardian ( ) ) // Can step into tile of guard
2015-10-27 02:34:47 +02:00
return false ;
2015-11-02 15:03:03 +02:00
break ;
case EPathfindingLayer : : SAIL :
if ( ! canMoveBetween ( cp - > coord , dp - > coord ) | | dp - > accessible = = CGPathNode : : BLOCKED )
return false ;
if ( isSourceGuarded ( ) & & ! isDestinationGuardian ( ) ) // Can step into tile of guard
2015-10-27 02:34:47 +02:00
return false ;
2015-11-02 15:03:03 +02:00
break ;
2015-10-27 02:34:47 +02:00
2015-11-02 15:03:03 +02:00
case EPathfindingLayer : : AIR :
2015-11-03 02:25:12 +02:00
//if(!canMoveBetween(cp->coord, dp->coord))
// return false;
2015-11-02 15:03:03 +02:00
break ;
case EPathfindingLayer : : WATER :
2015-11-05 10:25:41 +02:00
if ( ! canMoveBetween ( cp - > coord , dp - > coord ) | | dp - > accessible ! = CGPathNode : : ACCESSIBLE )
2015-11-02 15:03:03 +02:00
return false ;
if ( isDestinationGuarded ( ) )
return false ;
break ;
2015-10-27 02:34:47 +02:00
}
return true ;
}
2015-11-04 10:47:43 +02:00
bool CPathfinder : : isMovementAfterDestPossible ( )
2015-10-27 02:34:47 +02:00
{
2015-11-05 14:04:56 +02:00
switch ( dp - > layer )
2015-11-04 10:53:52 +02:00
{
case EPathfindingLayer : : LAND :
case EPathfindingLayer : : SAIL :
if ( dp - > accessible = = CGPathNode : : ACCESSIBLE )
return true ;
2015-11-04 14:38:15 +02:00
if ( dp - > coord = = out . hpos )
2015-11-04 10:53:52 +02:00
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 ( ) ) )
2015-11-05 10:34:01 +02:00
{
/// For now we'll always allow transit over teleporters
/// Transit over whirlpools only allowed when hero protected
auto whirlpool = dynamic_cast < const CGWhirlpool * > ( dt - > topVisitableObj ( ) ) ;
if ( ! whirlpool | | options . useTeleportWhirlpool )
return true ;
}
2015-11-04 10:53:52 +02:00
if ( useEmbarkCost & & options . useEmbarkAndDisembark )
return true ;
break ;
case EPathfindingLayer : : AIR :
case EPathfindingLayer : : WATER :
return true ;
break ;
}
2015-10-27 02:34:47 +02:00
return false ;
}
2015-11-04 14:38:15 +02:00
bool CPathfinder : : isSourceInitialPosition ( )
{
return cp - > coord = = out . hpos ;
}
2015-10-27 02:34:47 +02:00
int3 CPathfinder : : getSourceGuardPosition ( )
{
return gs - > map - > guardingCreaturePositions [ cp - > coord . x ] [ cp - > coord . y ] [ cp - > coord . z ] ;
}
bool CPathfinder : : isSourceGuarded ( )
{
//map can start with hero on guarded tile or teleport there using dimension door
//so threat tile hero standing on like it's not guarded because it's should be possible to move out of here
2015-11-04 14:38:15 +02:00
if ( getSourceGuardPosition ( ) ! = int3 ( - 1 , - 1 , - 1 ) & & ! isSourceInitialPosition ( ) )
2015-10-27 02:34:47 +02:00
{
//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
| | ! cp - > theNodeBefore - > land
| | ct - > topVisitableId ( ) ! = Obj : : BOAT )
{
return true ;
}
}
return false ;
}
bool CPathfinder : : isDestinationGuarded ( )
{
if ( gs - > map - > guardingCreaturePositions [ dp - > coord . x ] [ dp - > coord . y ] [ dp - > coord . z ] . valid ( )
& & dp - > accessible = = CGPathNode : : BLOCKVIS )
{
return true ;
}
return false ;
}
bool CPathfinder : : isDestinationGuardian ( )
{
return getSourceGuardPosition ( ) = = dp - > coord ;
}
void CPathfinder : : initializeGraph ( )
{
2015-11-05 14:04:56 +02:00
auto updateNode = [ & ] ( int3 pos , EPathfindingLayer layer , const TerrainTile * tinfo )
2015-11-02 10:06:06 +02:00
{
2015-11-03 00:29:43 +02:00
auto node = out . getNode ( pos , layer ) ;
2015-11-07 20:11:07 +02:00
node - > locked = false ;
2015-11-03 00:29:43 +02:00
node - > accessible = evaluateAccessibility ( pos , tinfo ) ;
node - > turns = 0xff ;
node - > moveRemains = 0 ;
node - > coord = pos ;
node - > land = tinfo - > terType ! = ETerrainType : : WATER ;
node - > theNodeBefore = nullptr ;
node - > layer = layer ;
2015-11-02 10:06:06 +02:00
} ;
2015-11-02 15:03:03 +02:00
int3 pos ;
2015-10-27 02:34:47 +02:00
for ( pos . x = 0 ; pos . x < out . sizes . x ; + + pos . x )
{
for ( pos . y = 0 ; pos . y < out . sizes . y ; + + pos . y )
{
for ( pos . z = 0 ; pos . z < out . sizes . z ; + + pos . z )
{
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
{
case ETerrainType : : ROCK :
break ;
case ETerrainType : : WATER :
2015-11-05 14:04:56 +02:00
updateNode ( pos , EPathfindingLayer : : SAIL , tinfo ) ;
2015-11-03 02:25:12 +02:00
if ( options . useFlying )
2015-11-05 14:04:56 +02:00
updateNode ( pos , EPathfindingLayer : : AIR , tinfo ) ;
2015-11-03 02:25:12 +02:00
if ( options . useWaterWalking )
2015-11-05 14:04:56 +02:00
updateNode ( pos , EPathfindingLayer : : WATER , tinfo ) ;
2015-11-02 10:06:06 +02:00
break ;
default :
2015-11-05 14:04:56 +02:00
updateNode ( pos , EPathfindingLayer : : LAND , tinfo ) ;
2015-11-03 02:25:12 +02:00
if ( options . useFlying )
2015-11-05 14:04:56 +02:00
updateNode ( pos , EPathfindingLayer : : AIR , tinfo ) ;
2015-11-02 10:06:06 +02:00
break ;
}
2015-10-27 02:34:47 +02:00
}
}
}
}
CGPathNode : : EAccessibility CPathfinder : : evaluateAccessibility ( const int3 & pos , const TerrainTile * tinfo ) const
{
CGPathNode : : EAccessibility ret = ( tinfo - > blocked ? CGPathNode : : BLOCKED : CGPathNode : : ACCESSIBLE ) ;
2015-11-05 14:04:56 +02:00
if ( tinfo - > terType = = ETerrainType : : ROCK | | ! isVisible ( pos , hero - > tempOwner ) )
2015-10-27 02:34:47 +02:00
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 [ pos . x ] [ pos . y ] [ pos . z ] . valid ( )
& & ! tinfo - > blocked )
{
// Monster close by; blocked visit for battle.
return CGPathNode : : BLOCKVIS ;
}
return ret ;
}
bool CPathfinder : : canMoveBetween ( const int3 & a , const int3 & b ) const
{
return gs - > checkForVisitableDir ( a , b ) ;
}
bool CPathfinder : : addTeleportTwoWay ( const CGTeleport * obj ) const
{
return options . useTeleportTwoWay & & gs - > isTeleportChannelBidirectional ( obj - > channel , hero - > tempOwner ) ;
}
bool CPathfinder : : addTeleportOneWay ( const CGTeleport * obj ) const
{
if ( options . useTeleportOneWay & & 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 ( options . useTeleportOneWayRandom & & 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 options . useTeleportWhirlpool & & obj ;
}
2015-11-02 10:06:06 +02:00
bool CPathfinder : : canVisitObject ( ) const
{
//hero can't visit objects while walking on water or flying
return cp - > layer = = EPathfindingLayer : : LAND | | cp - > layer = = EPathfindingLayer : : SAIL ;
}
2015-11-07 21:00:31 +02:00
CGPathNode : : CGPathNode ( int3 Coord , EPathfindingLayer Layer )
: coord ( Coord ) , layer ( Layer )
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 ;
land = 0 ;
moveRemains = 0 ;
turns = 255 ;
theNodeBefore = nullptr ;
}
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-05 14:04:56 +02:00
CPathsInfo : : CPathsInfo ( const int3 & Sizes )
: sizes ( Sizes )
2015-10-27 02:34:47 +02:00
{
hero = nullptr ;
2015-11-07 21:00:31 +02:00
nodes . resize ( boost : : extents [ sizes . x ] [ sizes . y ] [ sizes . z ] [ EPathfindingLayer : : NUM_LAYERS ] ) ;
2015-10-27 02:34:47 +02:00
for ( int i = 0 ; i < sizes . x ; i + + )
for ( int j = 0 ; j < sizes . y ; j + + )
2015-11-07 21:00:31 +02:00
for ( int z = 0 ; z < sizes . z ; z + + )
for ( int l = 0 ; l < EPathfindingLayer : : NUM_LAYERS ; l + + )
nodes [ i ] [ j ] [ z ] [ l ] = new CGPathNode ( int3 ( i , j , z ) , static_cast < EPathfindingLayer > ( l ) ) ;
2015-10-27 02:34:47 +02:00
}
CPathsInfo : : ~ CPathsInfo ( )
{
}
2015-11-02 10:06:06 +02:00
const CGPathNode * CPathsInfo : : getPathInfo ( const int3 & tile , const EPathfindingLayer & layer ) const
2015-10-27 02:34:47 +02:00
{
boost : : unique_lock < boost : : mutex > pathLock ( pathMx ) ;
2015-11-02 10:06:06 +02:00
if ( tile . x > = sizes . x | | tile . y > = sizes . y | | tile . z > = sizes . z | | layer > = EPathfindingLayer : : NUM_LAYERS )
2015-10-27 02:34:47 +02:00
return nullptr ;
2015-11-02 13:04:26 +02:00
return getNode ( tile , layer ) ;
2015-10-27 02:34:47 +02:00
}
2015-11-02 10:14:32 +02:00
bool CPathsInfo : : getPath ( CGPath & out , const int3 & dst , const EPathfindingLayer & layer ) const
2015-10-27 02:34:47 +02:00
{
boost : : unique_lock < boost : : mutex > pathLock ( pathMx ) ;
out . nodes . clear ( ) ;
2015-11-02 13:04:26 +02:00
const CGPathNode * curnode = getNode ( dst , layer ) ;
2015-10-27 02:34:47 +02:00
if ( ! curnode - > theNodeBefore )
return false ;
while ( curnode )
{
CGPathNode cpn = * curnode ;
curnode = curnode - > theNodeBefore ;
out . nodes . push_back ( cpn ) ;
}
return true ;
}
2015-11-02 10:06:06 +02:00
int CPathsInfo : : getDistance ( const int3 & tile , const EPathfindingLayer & layer ) const
2015-10-27 02:34:47 +02:00
{
boost : : unique_lock < boost : : mutex > pathLock ( pathMx ) ;
CGPath ret ;
2015-11-02 10:14:32 +02:00
if ( getPath ( ret , tile , layer ) )
2015-10-27 02:34:47 +02:00
return ret . nodes . size ( ) ;
else
return 255 ;
}
2015-11-02 13:04:26 +02:00
CGPathNode * CPathsInfo : : getNode ( const int3 & coord , const EPathfindingLayer & layer ) const
{
if ( layer ! = EPathfindingLayer : : AUTO )
2015-11-07 21:00:31 +02:00
return nodes [ coord . x ] [ coord . y ] [ coord . z ] [ layer ] ;
2015-11-02 13:04:26 +02:00
2015-11-07 21:00:31 +02:00
auto landNode = nodes [ coord . x ] [ coord . y ] [ coord . z ] [ EPathfindingLayer : : LAND ] ;
2015-11-02 13:04:26 +02:00
if ( landNode - > theNodeBefore )
return landNode ;
else
2015-11-07 21:00:31 +02:00
return nodes [ coord . x ] [ coord . y ] [ coord . z ] [ EPathfindingLayer : : SAIL ] ;
2015-11-02 13:04:26 +02:00
}