Skip to content

Commit

Permalink
Replace Direction::Get with faster Maps::GetDirection
Browse files Browse the repository at this point in the history
  • Loading branch information
idshibanov committed Sep 9, 2020
1 parent aaf3a73 commit 7e9a94f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/fheroes2/dialog/dialog_quickinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ std::string ShowGroundInfo( const Maps::Tiles & tile, bool show, const Heroes *
std::string str = Maps::Ground::String( tile.GetGround() );

if ( show && hero ) {
int dir = Direction::Get( hero->GetIndex(), tile.GetIndex() );
int dir = Maps::GetDirection( hero->GetIndex(), tile.GetIndex() );
if ( dir != Direction::UNKNOWN ) {
uint32_t cost = tile.isRoad() ? Maps::Ground::roadPenalty : Maps::Ground::GetPenalty( tile, hero->GetLevelSkill( Skill::Secondary::PATHFINDING ) );

Expand Down
11 changes: 0 additions & 11 deletions src/fheroes2/heroes/direction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@ std::string Direction::String( int direct )
return res.empty() ? str_direct[0] : res;
}

int Direction::Get( s32 from, s32 to )
{
const Directions directions = Direction::All();

for ( Directions::const_iterator it = directions.begin(); it != directions.end(); ++it )
if ( to == Maps::GetDirectionIndex( from, *it ) )
return *it;

return to == from ? CENTER : UNKNOWN;
}

bool Direction::ShortDistanceClockWise( int from, int to )
{
switch ( from ) {
Expand Down
1 change: 0 additions & 1 deletion src/fheroes2/heroes/direction.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace Direction

std::string String( int );

int Get( s32 from, s32 to );
int Reflect( int direct );

bool ShortDistanceClockWise( int direct1, int direct2 );
Expand Down
4 changes: 2 additions & 2 deletions src/fheroes2/heroes/route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Route::Path & Route::Path::operator=( const Path & p )

int Route::Path::GetFrontDirection( void ) const
{
return empty() ? ( dst != hero->GetIndex() ? Direction::Get( hero->GetIndex(), dst ) : Direction::CENTER ) : front().GetDirection();
return empty() ? ( dst != hero->GetIndex() ? Maps::GetDirection( hero->GetIndex(), dst ) : Direction::CENTER ) : front().GetDirection();
}

u32 Route::Path::GetFrontPenalty( void ) const
Expand Down Expand Up @@ -144,7 +144,7 @@ void Route::Path::Reset( void )

bool Route::Path::isComplete( void ) const
{
return dst == hero->GetIndex() || ( empty() && Direction::UNKNOWN != Direction::Get( hero->GetIndex(), dst ) );
return dst == hero->GetIndex() || ( empty() && Direction::UNKNOWN != Maps::GetDirection( hero->GetIndex(), dst ) );
}

bool Route::Path::isValid( void ) const
Expand Down
42 changes: 39 additions & 3 deletions src/fheroes2/maps/maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ const char * Maps::GetMinesName( int type )
return _( "Mine" );
}

int Maps::GetDirection( int from, int to )
{
if ( from == to )
return Direction::CENTER;

const int diff = to - from;
const int width = world.w();

if ( diff == ( -width - 1 ) ) {
return Direction::TOP_LEFT;
}
else if ( diff == -width ) {
return Direction::TOP;
}
else if ( diff == ( -width + 1 ) ) {
return Direction::TOP_RIGHT;
}
else if ( diff == -1 ) {
return Direction::LEFT;
}
else if ( diff == 1 ) {
return Direction::RIGHT;
}
else if ( diff == width - 1 ) {
return Direction::BOTTOM_LEFT;
}
else if ( diff == width ) {
return Direction::BOTTOM;
}
else if ( diff == width + 1 ) {
return Direction::BOTTOM_RIGHT;
}

return Direction::UNKNOWN;
}

s32 Maps::GetDirectionIndex( s32 from, int vector )
{
switch ( vector ) {
Expand Down Expand Up @@ -428,9 +464,9 @@ bool MapsTileIsUnderProtection( s32 from, s32 index ) /* from: center, index: mo
const Maps::Tiles & tile2 = world.GetTiles( index );

if ( tile2.GetObject() == MP2::OBJ_MONSTER && tile1.isWater() == tile2.isWater() ) {
const int monsterDirection = Direction::Get( index, from );
const int monsterDirection = Maps::GetDirection( index, from );
/* if monster can attack to */
result = ( tile2.GetPassable() & monsterDirection ) && ( tile1.GetPassable() & Direction::Get( from, index ) );
result = ( tile2.GetPassable() & monsterDirection ) && ( tile1.GetPassable() & Maps::GetDirection( from, index ) );

if ( !result ) {
/* h2 specific monster attack: BOTTOM_LEFT impassable! */
Expand All @@ -448,7 +484,7 @@ bool MapsTileIsUnderProtection( s32 from, s32 index ) /* from: center, index: mo

bool Maps::IsNearTiles( s32 index1, s32 index2 )
{
return DIRECTION_ALL & Direction::Get( index1, index2 );
return DIRECTION_ALL & Maps::GetDirection( index1, index2 );
}

bool Maps::TileIsUnderProtection( s32 center )
Expand Down
1 change: 1 addition & 0 deletions src/fheroes2/maps/maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace Maps
const char * SizeString( int size );
const char * GetMinesName( int res );

int GetDirection( int from, int to );
s32 GetDirectionIndex( s32, int direct );
bool isValidDirection( s32, int direct );

Expand Down
2 changes: 1 addition & 1 deletion src/fheroes2/maps/maps_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ std::pair<int, int> Maps::Tiles::GetMonsterSpriteIndices( const Tiles & tile, ui
if ( attackerIndex != -1 && !Settings::Get().ExtWorldOnlyFirstMonsterAttack() ) {
spriteIndices.first += 7;

switch ( Direction::Get( tileIndex, attackerIndex ) ) {
switch ( Maps::GetDirection( tileIndex, attackerIndex ) ) {
case Direction::TOP_LEFT:
case Direction::LEFT:
case Direction::BOTTOM_LEFT:
Expand Down
4 changes: 2 additions & 2 deletions src/fheroes2/world/world_pathfinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ std::list<Route::Step> Pathfinder::buildPath( int from, int target, uint8_t skil
while ( currentNode != from && currentNode != -1 ) {
PathfindingNode & node = _cache[currentNode];

path.emplace_front( node._from, Direction::Get( node._from, currentNode ), cost - node._cost );
path.emplace_front( node._from, Maps::GetDirection( node._from, currentNode ), cost - node._cost );
currentNode = node._from;
cost = node._cost;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ void Pathfinder::evaluateMap( int start, uint8_t skill )
// check if current tile is protected, can move only to adjacent monster
if ( !monsters.empty() ) {
for ( int monsterIndex : monsters ) {
const int direction = Direction::Get( currentNodeIdx, monsterIndex );
const int direction = Maps::GetDirection( currentNodeIdx, monsterIndex );

if ( direction != Direction::UNKNOWN && direction != Direction::CENTER && world.isValidPath( currentNodeIdx, direction ) ) {
// add straight to cache, can't move further from the monster
Expand Down

0 comments on commit 7e9a94f

Please sign in to comment.