diff --git a/src/fheroes2/dialog/dialog_quickinfo.cpp b/src/fheroes2/dialog/dialog_quickinfo.cpp index 1f2bbfe30c5..39ff84611d3 100644 --- a/src/fheroes2/dialog/dialog_quickinfo.cpp +++ b/src/fheroes2/dialog/dialog_quickinfo.cpp @@ -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 ) ); diff --git a/src/fheroes2/heroes/direction.cpp b/src/fheroes2/heroes/direction.cpp index 6b618c828b7..69ceef43116 100644 --- a/src/fheroes2/heroes/direction.cpp +++ b/src/fheroes2/heroes/direction.cpp @@ -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 ) { diff --git a/src/fheroes2/heroes/direction.h b/src/fheroes2/heroes/direction.h index 41d9fcd3439..ec86bfa7391 100644 --- a/src/fheroes2/heroes/direction.h +++ b/src/fheroes2/heroes/direction.h @@ -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 ); diff --git a/src/fheroes2/heroes/route.cpp b/src/fheroes2/heroes/route.cpp index 0b04b049470..52593a47da9 100644 --- a/src/fheroes2/heroes/route.cpp +++ b/src/fheroes2/heroes/route.cpp @@ -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 @@ -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 diff --git a/src/fheroes2/maps/maps.cpp b/src/fheroes2/maps/maps.cpp index 63fec75cb17..3f94f87b7e5 100644 --- a/src/fheroes2/maps/maps.cpp +++ b/src/fheroes2/maps/maps.cpp @@ -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 ) { @@ -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! */ @@ -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 ) diff --git a/src/fheroes2/maps/maps.h b/src/fheroes2/maps/maps.h index b4276a99551..c8d43dc0a23 100644 --- a/src/fheroes2/maps/maps.h +++ b/src/fheroes2/maps/maps.h @@ -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 ); diff --git a/src/fheroes2/maps/maps_tiles.cpp b/src/fheroes2/maps/maps_tiles.cpp index b943edcd215..157334bd4c5 100644 --- a/src/fheroes2/maps/maps_tiles.cpp +++ b/src/fheroes2/maps/maps_tiles.cpp @@ -2464,7 +2464,7 @@ std::pair 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: diff --git a/src/fheroes2/world/world_pathfinding.cpp b/src/fheroes2/world/world_pathfinding.cpp index c783607d30f..497e6ed481d 100644 --- a/src/fheroes2/world/world_pathfinding.cpp +++ b/src/fheroes2/world/world_pathfinding.cpp @@ -42,7 +42,7 @@ std::list 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; } @@ -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