Skip to content

Commit

Permalink
Maps Tiles related code cleanup (#7418)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihhub authored Jul 17, 2023
1 parent a56d5bf commit 2563ba5
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 165 deletions.
4 changes: 2 additions & 2 deletions src/fheroes2/ai/ai_hero_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ namespace
setColorOnTile( tile, hero.GetColor() );
};

if ( tile.isCaptureObjectProtected() ) {
if ( isCaptureObjectProtected( tile ) ) {
Army army( tile );

Battle::Result result = Battle::Loader( hero.GetArmy(), army, dstIndex );
Expand Down Expand Up @@ -1342,7 +1342,7 @@ namespace
if ( result.AttackerWins() ) {
hero.IncreaseExperience( result.GetExperienceAttacker() );

Maps::Tiles::RestoreAbandonedMine( tile, Resource::GOLD );
Maps::restoreAbandonedMine( tile, Resource::GOLD );
hero.SetMapsObject( MP2::OBJ_MINES );
setColorOnTile( tile, hero.GetColor() );
}
Expand Down
17 changes: 8 additions & 9 deletions src/fheroes2/ai/normal/ai_normal_hero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ namespace
case MP2::OBJ_MINES:
case MP2::OBJ_SAWMILL:
if ( !hero.isFriends( getColorFromTile( tile ) ) ) {
if ( tile.isCaptureObjectProtected() ) {
if ( isCaptureObjectProtected( tile ) ) {
return isHeroStrongerThan( tile, objectType, ai, heroArmyStrength, AI::ARMY_ADVANTAGE_SMALL );
}

Expand Down Expand Up @@ -1305,7 +1305,6 @@ namespace AI

const Maps::Tiles & tile = world.GetTiles( index );
const MP2::MapObjectType objectType = tile.GetObject();
const bool anotherFriendlyHeroPresent = _regions[tile.GetRegion()].friendlyHeroes > 1;

switch ( objectType ) {
case MP2::OBJ_CASTLE: {
Expand Down Expand Up @@ -1399,6 +1398,7 @@ namespace AI
}

// TODO: we should add logic to compare monsters and hero army strengths.
const bool anotherFriendlyHeroPresent = _regions[tile.GetRegion()].friendlyHeroes > 1;
return ( anotherFriendlyHeroPresent ? 4000.0 : 1000.0 ) + monsters.getTotalHP() / 100.0;
}
case MP2::OBJ_ABANDONED_MINE: {
Expand All @@ -1423,6 +1423,7 @@ namespace AI
case MP2::OBJ_RESOURCE:
case MP2::OBJ_WATER_WHEEL:
case MP2::OBJ_WINDMILL: {
const bool anotherFriendlyHeroPresent = _regions[tile.GetRegion()].friendlyHeroes > 1;
if ( anotherFriendlyHeroPresent ) {
return 100.0;
}
Expand Down Expand Up @@ -1650,7 +1651,7 @@ namespace AI
if ( !otherHero || hero.GetID() == otherHero->GetID() )
continue;

Heroes::Role role = otherHero->getAIRole();
const Heroes::Role role = otherHero->getAIRole();
if ( role == Heroes::Role::COURIER || role == Heroes::Role::SCOUT )
continue;

Expand Down Expand Up @@ -1748,13 +1749,11 @@ namespace AI
// pre-cache the pathfinder
_pathfinder.reEvaluateIfNeeded( hero );

const uint32_t leftMovePoints = hero.GetMovePoints();

ObjectValidator objectValidator( hero, _pathfinder, *this );
ObjectValueStorage valueStorage( hero, *this, lowestPossibleValue );

auto getObjectValue = [&objectValidator, &valueStorage, this, heroStrength, &hero, leftMovePoints]( const int destination, uint32_t & distance, double & value,
const MP2::MapObjectType type, const bool isDimensionDoor ) {
auto getObjectValue = [&objectValidator, &valueStorage, this, heroStrength, &hero]( const int destination, uint32_t & distance, double & value,
const MP2::MapObjectType type, const bool isDimensionDoor ) {
if ( !isDimensionDoor ) {
// Dimension door path does not include any objects on the way.
std::vector<IndexObject> list = _pathfinder.getObjectsOnTheWay( destination );
Expand All @@ -1774,7 +1773,7 @@ namespace AI
// TODO: check nearby enemy heroes and distance to them instead of relying on region stats.
const RegionStats & regionStats = _regions[destinationTile.GetRegion()];

const bool isObjectReachableAtThisTurn = ( distance <= leftMovePoints );
const bool isObjectReachableAtThisTurn = ( distance <= hero.GetMovePoints() );

// Go into "coward" mode only if the threat is real. Equal by strength heroes rarely attack each other.
if ( heroStrength * AI::ARMY_ADVANTAGE_SMALL < regionStats.highestThreat ) {
Expand Down Expand Up @@ -1843,7 +1842,7 @@ namespace AI

if ( !isObjectReachableAtThisTurn ) {
// Distant object which is out of reach for the current turn must have lower priority.
distance = leftMovePoints + ( distance - leftMovePoints ) * 2;
distance = hero.GetMovePoints() + ( distance - hero.GetMovePoints() ) * 2;
}

value = ScaleWithDistance( value, distance, type );
Expand Down
2 changes: 1 addition & 1 deletion src/fheroes2/dialog/dialog_quickinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ namespace
const int32_t playerColor = Settings::Get().CurrentColor();
const MP2::MapObjectType objectType = tile.GetObject( false );

if ( tile.isCaptureObjectProtected() || objectType == MP2::OBJ_ABANDONED_MINE ) {
if ( objectType == MP2::OBJ_ABANDONED_MINE || isCaptureObjectProtected( tile ) ) {
return showGuardiansInfo( tile, playerColor == getColorFromTile( tile ) );
}

Expand Down
2 changes: 1 addition & 1 deletion src/fheroes2/game/game_startgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ int Interface::AdventureMap::GetCursorFocusHeroes( const Heroes & hero, const Ma
default:
if ( MP2::isActionObject( tile.GetObject() ) ) {
const bool isProtected
= ( Maps::isTileUnderProtection( tile.GetIndex() ) || ( !hero.isFriends( getColorFromTile( tile ) ) && tile.isCaptureObjectProtected() ) );
= ( Maps::isTileUnderProtection( tile.GetIndex() ) || ( !hero.isFriends( getColorFromTile( tile ) ) && isCaptureObjectProtected( tile ) ) );

return Cursor::DistanceThemes( isProtected ? Cursor::CURSOR_HERO_FIGHT : Cursor::CURSOR_HERO_ACTION, hero.getNumOfTravelDays( tile.GetIndex() ) );
}
Expand Down
4 changes: 2 additions & 2 deletions src/fheroes2/heroes/heroes_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,7 @@ namespace
}
};

if ( tile.isCaptureObjectProtected() ) {
if ( isCaptureObjectProtected( tile ) ) {
Army army( tile );

Battle::Result result = Battle::Loader( hero.GetArmy(), army, dstIndex );
Expand Down Expand Up @@ -2104,7 +2104,7 @@ namespace
if ( result.AttackerWins() ) {
hero.IncreaseExperience( result.GetExperienceAttacker() );

Maps::Tiles::RestoreAbandonedMine( tile, Resource::GOLD );
Maps::restoreAbandonedMine( tile, Resource::GOLD );
hero.SetMapsObject( MP2::OBJ_MINES );
setColorOnTile( tile, hero.GetColor() );

Expand Down
139 changes: 0 additions & 139 deletions src/fheroes2/maps/maps_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@
#include <cstdlib>
#include <iostream>
#include <limits>
#include <optional>
#include <set>
#include <type_traits>
#include <utility>

#include "army.h"
#include "army_troop.h"
#include "artifact.h"
#include "castle.h"
Expand Down Expand Up @@ -65,7 +63,6 @@
#include "resource.h"
#include "save_format_version.h"
#include "serialize.h"
#include "tools.h"
#include "trees.h"
#include "world.h"

Expand Down Expand Up @@ -1471,28 +1468,6 @@ void Maps::Tiles::fixTileObjectType( Tiles & tile )
}
}

bool Maps::Tiles::isCaptureObjectProtected() const
{
const MP2::MapObjectType objectType = GetObject( false );

if ( !MP2::isCaptureObject( objectType ) ) {
return false;
}

if ( MP2::OBJ_CASTLE == objectType ) {
Castle * castle = world.getCastleEntrance( GetCenter() );
assert( castle != nullptr );

if ( castle ) {
return castle->GetArmy().isValid();
}

return false;
}

return getTroopFromTile( *this ).isValid();
}

void Maps::Tiles::Remove( uint32_t uniqID )
{
addons_level1.remove_if( [uniqID]( const Maps::TilesAddon & v ) { return v.isUniq( uniqID ); } );
Expand Down Expand Up @@ -1633,120 +1608,6 @@ void Maps::Tiles::RemoveJailSprite()
Remove( _uid );
}

void Maps::Tiles::RestoreAbandonedMine( Tiles & tile, const int resource )
{
assert( tile.GetObject( false ) == MP2::OBJ_ABANDONED_MINE );
assert( tile._uid != 0 );

const payment_t info = ProfitConditions::FromMine( resource );
std::optional<uint32_t> resourceCount;

switch ( resource ) {
case Resource::ORE:
resourceCount = fheroes2::checkedCast<uint32_t>( info.ore );
break;
case Resource::SULFUR:
resourceCount = fheroes2::checkedCast<uint32_t>( info.sulfur );
break;
case Resource::CRYSTAL:
resourceCount = fheroes2::checkedCast<uint32_t>( info.crystal );
break;
case Resource::GEMS:
resourceCount = fheroes2::checkedCast<uint32_t>( info.gems );
break;
case Resource::GOLD:
resourceCount = fheroes2::checkedCast<uint32_t>( info.gold );
break;
default:
assert( 0 );
break;
}

assert( resourceCount.has_value() && resourceCount > 0U );

setResourceOnTile( tile, resource, resourceCount.value() );

auto restoreLeftSprite = [resource]( MP2::ObjectIcnType & objectIcnType, uint8_t & imageIndex ) {
if ( MP2::OBJ_ICN_TYPE_OBJNGRAS == objectIcnType && imageIndex == 6 ) {
objectIcnType = MP2::OBJ_ICN_TYPE_MTNGRAS;
imageIndex = 82;
}
else if ( MP2::OBJ_ICN_TYPE_OBJNDIRT == objectIcnType && imageIndex == 8 ) {
objectIcnType = MP2::OBJ_ICN_TYPE_MTNDIRT;
imageIndex = 112;
}
else if ( MP2::OBJ_ICN_TYPE_EXTRAOVR == objectIcnType && imageIndex == 5 ) {
switch ( resource ) {
case Resource::ORE:
imageIndex = 0;
break;
case Resource::SULFUR:
imageIndex = 1;
break;
case Resource::CRYSTAL:
imageIndex = 2;
break;
case Resource::GEMS:
imageIndex = 3;
break;
case Resource::GOLD:
imageIndex = 4;
break;
default:
break;
}
}
};

auto restoreRightSprite = []( MP2::ObjectIcnType & objectIcnType, uint8_t & imageIndex ) {
if ( MP2::OBJ_ICN_TYPE_OBJNDIRT == objectIcnType && imageIndex == 9 ) {
objectIcnType = MP2::OBJ_ICN_TYPE_MTNDIRT;
imageIndex = 113;
}
else if ( MP2::OBJ_ICN_TYPE_OBJNGRAS == objectIcnType && imageIndex == 7 ) {
objectIcnType = MP2::OBJ_ICN_TYPE_MTNGRAS;
imageIndex = 83;
}
};

auto restoreMineObjectType = [&tile]( int directionVector ) {
if ( Maps::isValidDirection( tile._index, directionVector ) ) {
Tiles & mineTile = world.GetTiles( Maps::GetDirectionIndex( tile._index, directionVector ) );
if ( ( mineTile.GetObject() == MP2::OBJ_NON_ACTION_ABANDONED_MINE )
&& ( mineTile._uid == tile._uid || mineTile.FindAddonLevel1( tile._uid ) || mineTile.FindAddonLevel2( tile._uid ) ) ) {
mineTile.SetObject( MP2::OBJ_NON_ACTION_MINES );
}
}
};

restoreLeftSprite( tile._objectIcnType, tile._imageIndex );
for ( TilesAddon & addon : tile.addons_level1 ) {
if ( addon._uid == tile._uid ) {
restoreLeftSprite( addon._objectIcnType, addon._imageIndex );
}
}

if ( Maps::isValidDirection( tile._index, Direction::RIGHT ) ) {
Tiles & rightTile = world.GetTiles( Maps::GetDirectionIndex( tile._index, Direction::RIGHT ) );

if ( rightTile._uid == tile._uid ) {
restoreRightSprite( rightTile._objectIcnType, rightTile._imageIndex );
}

TilesAddon * addon = rightTile.FindAddonLevel1( tile._uid );

if ( addon ) {
restoreRightSprite( addon->_objectIcnType, addon->_imageIndex );
}
}

restoreMineObjectType( Direction::LEFT );
restoreMineObjectType( Direction::RIGHT );
restoreMineObjectType( Direction::TOP );
restoreMineObjectType( Direction::TOP_LEFT );
restoreMineObjectType( Direction::TOP_RIGHT );
}

void Maps::Tiles::ClearFog( const int colors )
{
_fogColors &= ~colors;
Expand Down
10 changes: 0 additions & 10 deletions src/fheroes2/maps/maps_tiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,6 @@ namespace Maps

void ClearFog( const int colors );

// Checks whether the object to be captured is guarded by its own forces
// (castle has a hero or garrison, dwelling has creatures, etc)
bool isCaptureObjectProtected() const;

void SetObjectPassable( bool pass );

const std::array<uint32_t, 3> & metadata() const
Expand Down Expand Up @@ -349,12 +345,6 @@ namespace Maps

bool containsSprite( const MP2::ObjectIcnType objectIcnType, const uint32_t imageIdx ) const;

// Restores an abandoned mine whose main tile is 'tile', turning it into an ordinary mine that brings
// resources of type 'resource'. This method updates all sprites and sets object types for non-action
// tiles. The object type for the action tile (i.e. the main tile) remains unchanged and should be
// updated separately.
static void RestoreAbandonedMine( Tiles & tile, const int resource );

// Some tiles have incorrect object type. This is due to original Editor issues.
static void fixTileObjectType( Tiles & tile );

Expand Down
Loading

0 comments on commit 2563ba5

Please sign in to comment.