Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Adventure Map object removal code to helper space #7453

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/fheroes2/ai/ai_hero_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ namespace
if ( destroy ) {
setMonsterCountOnTile( tile, 0 );

tile.RemoveObjectSprite();
removeObjectSprite( tile );
tile.setAsEmpty();
}
}
Expand All @@ -555,7 +555,7 @@ namespace
hero.GetKingdom().AddFundsResource( getFundsFromTile( tile ) );
}

tile.RemoveObjectSprite();
removeObjectSprite( tile );
resetObjectInfoOnTile( tile );
hero.GetPath().Reset();

Expand Down Expand Up @@ -634,7 +634,7 @@ namespace
kingdom.AddFundsResource( Funds( Resource::GOLD, gold ) );
}

tile.RemoveObjectSprite();
removeObjectSprite( tile );
resetObjectInfoOnTile( tile );

DEBUG_LOG( DBG_AI, DBG_INFO, hero.GetName() )
Expand Down Expand Up @@ -756,7 +756,7 @@ namespace
Maps::Tiles & tile = world.GetTiles( dst_index );

hero.GetKingdom().AddFundsResource( getFundsFromTile( tile ) );
tile.RemoveObjectSprite();
removeObjectSprite( tile );
resetObjectInfoOnTile( tile );

DEBUG_LOG( DBG_AI, DBG_INFO, hero.GetName() )
Expand Down Expand Up @@ -1308,7 +1308,7 @@ namespace

// Remove genie lamp sprite if no genies are available to hire.
if ( MP2::OBJ_GENIE_LAMP == objectType && ( availableTroopCount == recruitTroopCount ) ) {
tile.RemoveObjectSprite();
removeObjectSprite( tile );
tile.setAsEmpty();
}

Expand Down Expand Up @@ -1393,7 +1393,7 @@ namespace
const Kingdom & kingdom = hero.GetKingdom();

if ( kingdom.IsVisitTravelersTent( getColorFromTile( tile ) ) ) {
tile.RemoveObjectSprite();
removeObjectSprite( tile );
tile.setAsEmpty();
}

Expand All @@ -1419,7 +1419,7 @@ namespace
else
hero.PickupArtifact( getArtifactFromTile( tile ) );

tile.RemoveObjectSprite();
removeObjectSprite( tile );
resetObjectInfoOnTile( tile );

DEBUG_LOG( DBG_AI, DBG_INFO, hero.GetName() )
Expand Down Expand Up @@ -1466,7 +1466,7 @@ namespace
}

if ( result && hero.PickupArtifact( art ) ) {
tile.RemoveObjectSprite();
removeObjectSprite( tile );
resetObjectInfoOnTile( tile );
}
}
Expand Down Expand Up @@ -1540,7 +1540,7 @@ namespace
if ( kingdom.GetHeroes().size() < Kingdom::GetMaxHeroes() ) {
Maps::Tiles & tile = world.GetTiles( tileIndex );

tile.RemoveObjectSprite();
removeObjectSprite( tile );
tile.setAsEmpty();

Heroes * prisoner = world.FromJailHeroes( tileIndex );
Expand Down
3 changes: 2 additions & 1 deletion src/fheroes2/gui/interface_gamearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "logging.h"
#include "maps.h"
#include "maps_tiles.h"
#include "maps_tiles_helper.h"
#include "maps_tiles_render.h"
#include "pal.h"
#include "players.h"
Expand Down Expand Up @@ -1074,7 +1075,7 @@ Interface::ObjectFadingOutInfo::~ObjectFadingOutInfo()
Maps::Tiles & tile = world.GetTiles( tileId );

if ( tile.GetObject() == type ) {
tile.RemoveObjectSprite();
removeObjectSprite( tile );
tile.setAsEmpty();
}
}
21 changes: 0 additions & 21 deletions src/fheroes2/heroes/heroes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,21 +1101,6 @@ void Heroes::ResetAction()
ResetModes( ACTION );
}

uint32_t Heroes::GetCountArtifacts() const
{
return bag_artifacts.CountArtifacts();
}

bool Heroes::HasUltimateArtifact() const
{
return bag_artifacts.ContainUltimateArtifact();
}

bool Heroes::IsFullBagArtifacts() const
{
return bag_artifacts.isFull();
}

bool Heroes::PickupArtifact( const Artifact & art )
{
if ( !art.isValid() ) {
Expand Down Expand Up @@ -1392,11 +1377,6 @@ void Heroes::SetShipMaster( bool f )
f ? SetModes( SHIPMASTER ) : ResetModes( SHIPMASTER );
}

Skill::SecSkills & Heroes::GetSecondarySkills()
{
return secondary_skills;
}

bool Heroes::HasSecondarySkill( int skill ) const
{
return Skill::Level::NONE != secondary_skills.GetLevel( skill );
Expand Down Expand Up @@ -1592,7 +1572,6 @@ void Heroes::LevelUpSecondarySkill( const HeroSeedsForLevelUp & seeds, int prima
}
}

/* apply penalty */
void Heroes::ApplyPenaltyMovement( uint32_t penalty )
{
if ( move_point >= penalty )
Expand Down
24 changes: 20 additions & 4 deletions src/fheroes2/heroes/heroes.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,28 @@ class Heroes final : public HeroBase, public ColorBase
int GetLevelSkill( int ) const override;
uint32_t GetSecondaryValues( int skill ) const override;
void LearnSkill( const Skill::Secondary & );
Skill::SecSkills & GetSecondarySkills();

Skill::SecSkills & GetSecondarySkills()
{
return secondary_skills;
}

bool PickupArtifact( const Artifact & );
bool HasUltimateArtifact() const;
uint32_t GetCountArtifacts() const;
bool IsFullBagArtifacts() const;

bool HasUltimateArtifact() const
{
return bag_artifacts.ContainUltimateArtifact();
}

uint32_t GetCountArtifacts() const
{
return bag_artifacts.CountArtifacts();
}

bool IsFullBagArtifacts() const
{
return bag_artifacts.isFull();
}

int GetMobilityIndexSprite() const;

Expand Down
78 changes: 0 additions & 78 deletions src/fheroes2/maps/maps_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,84 +1530,6 @@ void Maps::Tiles::updateObjectImageIndex( const uint32_t objectUid, const MP2::O
}
}

void Maps::Tiles::RemoveObjectSprite()
{
switch ( GetObject() ) {
case MP2::OBJ_MONSTER:
Remove( _uid );
break;
case MP2::OBJ_JAIL:
RemoveJailSprite();
tilePassable = DIRECTION_ALL;
break;
case MP2::OBJ_ARTIFACT: {
const uint32_t uidArtifact = getObjectIdByObjectIcnType( MP2::OBJ_ICN_TYPE_OBJNARTI );
Remove( uidArtifact );

if ( Maps::isValidDirection( _index, Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( _index, Direction::LEFT ) ).Remove( uidArtifact );
break;
}
case MP2::OBJ_TREASURE_CHEST:
case MP2::OBJ_RESOURCE: {
const uint32_t uidResource = getObjectIdByObjectIcnType( MP2::OBJ_ICN_TYPE_OBJNRSRC );
Remove( uidResource );

if ( Maps::isValidDirection( _index, Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( _index, Direction::LEFT ) ).Remove( uidResource );
break;
}
case MP2::OBJ_BARRIER:
tilePassable = DIRECTION_ALL;
[[fallthrough]];
default:
// remove shadow sprite from left cell
if ( Maps::isValidDirection( _index, Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( _index, Direction::LEFT ) ).Remove( _uid );

Remove( _uid );
break;
}
}

void Maps::Tiles::RemoveJailSprite()
{
// remove left sprite
if ( Maps::isValidDirection( _index, Direction::LEFT ) ) {
const int32_t left = Maps::GetDirectionIndex( _index, Direction::LEFT );
world.GetTiles( left ).Remove( _uid );

// remove left left sprite
if ( Maps::isValidDirection( left, Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( left, Direction::LEFT ) ).Remove( _uid );
}

// remove top sprite
if ( Maps::isValidDirection( _index, Direction::TOP ) ) {
const int32_t top = Maps::GetDirectionIndex( _index, Direction::TOP );
Maps::Tiles & topTile = world.GetTiles( top );
topTile.Remove( _uid );

if ( topTile.GetObject() == MP2::OBJ_JAIL ) {
topTile.setAsEmpty();
topTile.FixObject();
}

// remove top left sprite
if ( Maps::isValidDirection( top, Direction::LEFT ) ) {
Maps::Tiles & leftTile = world.GetTiles( Maps::GetDirectionIndex( top, Direction::LEFT ) );
leftTile.Remove( _uid );

if ( leftTile.GetObject() == MP2::OBJ_JAIL ) {
leftTile.setAsEmpty();
leftTile.FixObject();
}
}
}

Remove( _uid );
}

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

void resetPassability()
{
tilePassable = DIRECTION_ALL;
}

int GetGround() const;

bool isWater() const
Expand Down Expand Up @@ -291,7 +296,7 @@ namespace Maps

void AddonsSort();
void Remove( uint32_t uniqID );
void RemoveObjectSprite();

void updateObjectImageIndex( const uint32_t objectUid, const MP2::ObjectIcnType objectIcnType, const int imageIndexOffset );
void replaceObject( const uint32_t objectUid, const MP2::ObjectIcnType originalObjectIcnType, const MP2::ObjectIcnType newObjectIcnType,
const uint8_t originalImageIndex, const uint8_t newImageIndex );
Expand Down Expand Up @@ -367,7 +372,6 @@ namespace Maps

// Set or remove a flag which belongs to UID of the object.
void updateFlag( const int color, const uint8_t objectSpriteIndex, const uint32_t uid, const bool setOnUpperLayer );
void RemoveJailSprite();

bool isTallObject() const;

Expand Down
80 changes: 80 additions & 0 deletions src/fheroes2/maps/maps_tiles_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,46 @@ namespace

tile.setObjectSpriteIndex( static_cast<TileImageIndexType>( mons.GetID() - 1 ) ); // ICN::MONS32 starts from PEASANT
}

void removeJailSprite( Maps::Tiles & tile )
{
assert( tile.GetObject() == MP2::OBJ_JAIL );

// remove left sprite
if ( Maps::isValidDirection( tile.GetIndex(), Direction::LEFT ) ) {
const int32_t left = Maps::GetDirectionIndex( tile.GetIndex(), Direction::LEFT );
world.GetTiles( left ).Remove( tile.GetObjectUID() );

// remove left left sprite
if ( Maps::isValidDirection( left, Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( left, Direction::LEFT ) ).Remove( tile.GetObjectUID() );
}

// remove top sprite
if ( Maps::isValidDirection( tile.GetIndex(), Direction::TOP ) ) {
const int32_t top = Maps::GetDirectionIndex( tile.GetIndex(), Direction::TOP );
Maps::Tiles & topTile = world.GetTiles( top );
topTile.Remove( tile.GetObjectUID() );

if ( topTile.GetObject() == MP2::OBJ_JAIL ) {
topTile.setAsEmpty();
topTile.FixObject();
}

// remove top left sprite
if ( Maps::isValidDirection( top, Direction::LEFT ) ) {
Maps::Tiles & leftTile = world.GetTiles( Maps::GetDirectionIndex( top, Direction::LEFT ) );
leftTile.Remove( tile.GetObjectUID() );

if ( leftTile.GetObject() == MP2::OBJ_JAIL ) {
leftTile.setAsEmpty();
leftTile.FixObject();
}
}
}

tile.Remove( tile.GetObjectUID() );
}
}

namespace Maps
Expand Down Expand Up @@ -1723,4 +1763,44 @@ namespace Maps
restoreMineObjectType( Direction::TOP_LEFT );
restoreMineObjectType( Direction::TOP_RIGHT );
}

void removeObjectSprite( Tiles & tile )
{
switch ( tile.GetObject() ) {
case MP2::OBJ_MONSTER:
tile.Remove( tile.GetObjectUID() );
break;
case MP2::OBJ_JAIL:
removeJailSprite( tile );
tile.resetPassability();
break;
case MP2::OBJ_ARTIFACT: {
const uint32_t uidArtifact = tile.getObjectIdByObjectIcnType( MP2::OBJ_ICN_TYPE_OBJNARTI );
tile.Remove( uidArtifact );

if ( Maps::isValidDirection( tile.GetIndex(), Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( tile.GetIndex(), Direction::LEFT ) ).Remove( uidArtifact );
break;
}
case MP2::OBJ_TREASURE_CHEST:
case MP2::OBJ_RESOURCE: {
const uint32_t uidResource = tile.getObjectIdByObjectIcnType( MP2::OBJ_ICN_TYPE_OBJNRSRC );
tile.Remove( uidResource );

if ( Maps::isValidDirection( tile.GetIndex(), Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( tile.GetIndex(), Direction::LEFT ) ).Remove( uidResource );
break;
}
case MP2::OBJ_BARRIER:
tile.resetPassability();
[[fallthrough]];
default:
// remove shadow sprite from left cell
if ( Maps::isValidDirection( tile.GetIndex(), Direction::LEFT ) )
world.GetTiles( Maps::GetDirectionIndex( tile.GetIndex(), Direction::LEFT ) ).Remove( tile.GetObjectUID() );

tile.Remove( tile.GetObjectUID() );
break;
}
}
}
2 changes: 2 additions & 0 deletions src/fheroes2/maps/maps_tiles_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,6 @@ namespace Maps
// tiles. The object type for the action tile (i.e. the main tile) remains unchanged and should be
// updated separately.
void restoreAbandonedMine( Tiles & tile, const int resource );

void removeObjectSprite( Tiles & tile );
}