Skip to content

Commit

Permalink
Do not mark captured castle objects by Color::UNUSED (#9156)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-derevenetz authored Oct 15, 2024
1 parent ed50ad9 commit 5e86ba1
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 76 deletions.
1 change: 0 additions & 1 deletion src/fheroes2/heroes/heroes_spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ namespace

if ( spell == Spell::HAUNT ) {
world.CaptureObject( tile.GetIndex(), Color::NONE );
tile.removeOwnershipFlag( MP2::OBJ_MINE );

// Update the color of haunted mine on radar.
Interface::AdventureMap & I = Interface::AdventureMap::Get();
Expand Down
87 changes: 50 additions & 37 deletions src/fheroes2/kingdom/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "color.h"

#include <cassert>

#include "players.h"
#include "serialize.h"
#include "tools.h"
Expand All @@ -45,7 +47,7 @@ namespace
};
}

std::string Color::String( int color )
std::string Color::String( const int color )
{
switch ( color ) {
case Color::BLUE:
Expand All @@ -69,7 +71,7 @@ std::string Color::String( int color )
return "None";
}

int Color::GetIndex( int color )
int Color::GetIndex( const int color )
{
switch ( color ) {
case BLUE:
Expand All @@ -92,42 +94,31 @@ int Color::GetIndex( int color )
return 6;
}

int Color::Count( int colors )
int Color::Count( const int colors )
{
return CountBits( colors & ALL );
}

int Color::FromInt( int col )
{
switch ( col ) {
case BLUE:
case GREEN:
case RED:
case YELLOW:
case ORANGE:
case PURPLE:
return col;
default:
break;
}

return NONE;
}

int Color::GetFirst( int colors )
int Color::GetFirst( const int colors )
{
if ( colors & BLUE )
if ( colors & BLUE ) {
return BLUE;
else if ( colors & GREEN )
}
if ( colors & GREEN ) {
return GREEN;
else if ( colors & RED )
}
if ( colors & RED ) {
return RED;
else if ( colors & YELLOW )
}
if ( colors & YELLOW ) {
return YELLOW;
else if ( colors & ORANGE )
}
if ( colors & ORANGE ) {
return ORANGE;
else if ( colors & PURPLE )
}
if ( colors & PURPLE ) {
return PURPLE;
}

return NONE;
}
Expand Down Expand Up @@ -206,32 +197,54 @@ const char * fheroes2::getTentColorName( const int color )
return "None";
}

Colors::Colors( int colors )
Colors::Colors( const int colors /* = Color::ALL */ )
{
reserve( 6 );

if ( colors & Color::BLUE )
if ( colors & Color::BLUE ) {
push_back( Color::BLUE );
if ( colors & Color::GREEN )
}
if ( colors & Color::GREEN ) {
push_back( Color::GREEN );
if ( colors & Color::RED )
}
if ( colors & Color::RED ) {
push_back( Color::RED );
if ( colors & Color::YELLOW )
}
if ( colors & Color::YELLOW ) {
push_back( Color::YELLOW );
if ( colors & Color::ORANGE )
}
if ( colors & Color::ORANGE ) {
push_back( Color::ORANGE );
if ( colors & Color::PURPLE )
}
if ( colors & Color::PURPLE ) {
push_back( Color::PURPLE );
}
}

bool ColorBase::isFriends( int col ) const
bool ColorBase::isFriends( const int col ) const
{
return ( col & Color::ALL ) && ( color == col || Players::isFriends( color, col ) );
}

void ColorBase::SetColor( int col )
void ColorBase::SetColor( const int col )
{
color = Color::FromInt( col );
switch ( col ) {
case Color::NONE:
case Color::BLUE:
case Color::GREEN:
case Color::RED:
case Color::YELLOW:
case Color::ORANGE:
case Color::PURPLE:
color = col;
break;
default:
#ifdef WITH_DEBUG
assert( 0 );
#endif
color = Color::NONE;
break;
}
}

Kingdom & ColorBase::GetKingdom() const
Expand Down
24 changes: 13 additions & 11 deletions src/fheroes2/kingdom/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
class IStreamBase;
class OStreamBase;

class Kingdom;

namespace fheroes2
{
const char * getBarrierColorName( const int color );
Expand All @@ -53,23 +55,22 @@ namespace Color
ALL = BLUE | GREEN | RED | YELLOW | ORANGE | PURPLE
};

std::string String( int color );
int Count( int colors );
int GetIndex( int color );
int GetFirst( int colors );
int FromInt( int col );
std::string String( const int color );

int GetIndex( const int color );

int Count( const int colors );
int GetFirst( const int colors );

uint8_t IndexToColor( const int index );
}

class Colors : public std::vector<int>
{
public:
explicit Colors( int colors = Color::ALL );
explicit Colors( const int colors = Color::ALL );
};

class Kingdom;

class ColorBase
{
int color;
Expand All @@ -78,15 +79,16 @@ class ColorBase
friend IStreamBase & operator>>( IStreamBase & stream, ColorBase & col );

public:
explicit ColorBase( int col = Color::NONE )
explicit ColorBase( const int col = Color::NONE )
: color( col )
{}

bool isFriends( int ) const;
void SetColor( int );
bool isFriends( const int col ) const;

Kingdom & GetKingdom() const;

void SetColor( const int col );

int GetColor() const
{
return color;
Expand Down
15 changes: 8 additions & 7 deletions src/fheroes2/maps/maps_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ Maps::TilesAddon * Maps::Tiles::getAddonWithFlag( const uint32_t uid )
return nullptr;
}

void Maps::Tiles::setOwnershipFlag( const MP2::MapObjectType objectType, const int color )
void Maps::Tiles::setOwnershipFlag( const MP2::MapObjectType objectType, int color )
{
// All flags in FLAG32.ICN are actually the same except the fact of having different offset.
// Set the default value for the UNUSED color.
Expand Down Expand Up @@ -1153,7 +1153,8 @@ void Maps::Tiles::setOwnershipFlag( const MP2::MapObjectType objectType, const i
objectSpriteIndex = 5;
break;
case Color::UNUSED:
// Neutral (gray) flag. Index '6' is already set.
// Should never be called using this color as an argument.
assert( 0 );
break;
default:
// Did you add a new color type? Add logic above!
Expand Down Expand Up @@ -1216,6 +1217,11 @@ void Maps::Tiles::setOwnershipFlag( const MP2::MapObjectType objectType, const i
break;

case MP2::OBJ_CASTLE:
// Neutral castles always have flags on both sides of the gate, they should not be completely removed.
if ( color == Color::NONE ) {
color = Color::UNUSED;
}

objectSpriteIndex *= 2;
if ( isValidDirection( _index, Direction::LEFT ) ) {
Tiles & tile = world.GetTiles( GetDirectionIndex( _index, Direction::LEFT ) );
Expand All @@ -1234,11 +1240,6 @@ void Maps::Tiles::setOwnershipFlag( const MP2::MapObjectType objectType, const i
}
}

void Maps::Tiles::removeOwnershipFlag( const MP2::MapObjectType objectType )
{
setOwnershipFlag( objectType, Color::NONE );
}

void Maps::Tiles::updateFlag( const int color, const uint8_t objectSpriteIndex, const uint32_t uid, const bool setOnUpperLayer )
{
// Flag deletion or installation must be done in relation to object UID as flag is attached to the object.
Expand Down
4 changes: 1 addition & 3 deletions src/fheroes2/maps/maps_tiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ namespace Maps
// Update passability based on neighbours around.
void updatePassability();

void setOwnershipFlag( const MP2::MapObjectType objectType, const int color );

void removeOwnershipFlag( const MP2::MapObjectType objectType );
void setOwnershipFlag( const MP2::MapObjectType objectType, int color );

// Return fog direction of tile. A tile without fog returns "Direction::UNKNOWN".
uint16_t getFogDirection() const
Expand Down
26 changes: 15 additions & 11 deletions src/fheroes2/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void CapturedObjects::ResetColor( const int color )

auto & [objectType, col] = objCol;

col = ( objectType == MP2::OBJ_CASTLE ) ? Color::UNUSED : Color::NONE;
col = Color::NONE;
world.GetTiles( idx ).setOwnershipFlag( objectType, col );
}
}
Expand Down Expand Up @@ -855,12 +855,12 @@ int32_t World::NextWhirlpool( const int32_t index ) const
return Rand::Get( whilrpools );
}

uint32_t World::CountCapturedObject( const MP2::MapObjectType obj, const int col ) const
uint32_t World::CountCapturedObject( const MP2::MapObjectType obj, const int color ) const
{
return map_captureobj.GetCount( obj, col );
return map_captureobj.GetCount( obj, color );
}

uint32_t World::CountCapturedMines( int type, int color ) const
uint32_t World::CountCapturedMines( const int type, const int color ) const
{
switch ( type ) {
case Resource::WOOD:
Expand All @@ -874,32 +874,36 @@ uint32_t World::CountCapturedMines( int type, int color ) const
return map_captureobj.GetCountMines( type, color );
}

void World::CaptureObject( int32_t index, int color )
void World::CaptureObject( const int32_t index, const int color )
{
assert( CountBits( color ) <= 1 );

const MP2::MapObjectType objectType = GetTiles( index ).GetObject( false );
map_captureobj.Set( index, objectType, color );

if ( color != Color::NONE && !( color & Color::ALL ) ) {
return;
}

Castle * castle = getCastleEntrance( Maps::GetPoint( index ) );
if ( castle && castle->GetColor() != color ) {
castle->ChangeColor( color );
}

if ( color & ( Color::ALL | Color::UNUSED ) ) {
GetTiles( index ).setOwnershipFlag( objectType, color );
}
GetTiles( index ).setOwnershipFlag( objectType, color );
}

int World::ColorCapturedObject( int32_t index ) const
int World::ColorCapturedObject( const int32_t index ) const
{
return map_captureobj.GetColor( index );
}

CapturedObject & World::GetCapturedObject( int32_t index )
CapturedObject & World::GetCapturedObject( const int32_t index )
{
return map_captureobj.Get( index );
}

void World::ResetCapturedObjects( int color )
void World::ResetCapturedObjects( const int color )
{
map_captureobj.ResetColor( color );
}
Expand Down
15 changes: 9 additions & 6 deletions src/fheroes2/world/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,16 @@ class World : protected fheroes2::Size
int32_t NextWhirlpool( const int32_t index ) const;
MapsIndexes GetWhirlpoolEndPoints( const int32_t index ) const;

void CaptureObject( int32_t, int col );
uint32_t CountCapturedObject( const MP2::MapObjectType obj, const int col ) const;
uint32_t CountCapturedMines( int type, int col ) const;
void CaptureObject( const int32_t index, const int color );

uint32_t CountCapturedObject( const MP2::MapObjectType obj, const int color ) const;
uint32_t CountCapturedMines( const int type, const int color ) const;
uint32_t CountObeliskOnMaps();
int ColorCapturedObject( int32_t ) const;
void ResetCapturedObjects( int );
CapturedObject & GetCapturedObject( int32_t );

int ColorCapturedObject( const int32_t index ) const;
void ResetCapturedObjects( const int color );

CapturedObject & GetCapturedObject( const int32_t index );

void ActionForMagellanMaps( int color );
void ClearFog( int color ) const;
Expand Down

0 comments on commit 5e86ba1

Please sign in to comment.