diff --git a/src/fheroes2/heroes/heroes_spell.cpp b/src/fheroes2/heroes/heroes_spell.cpp index bffdb798a5..fcf25ace2d 100644 --- a/src/fheroes2/heroes/heroes_spell.cpp +++ b/src/fheroes2/heroes/heroes_spell.cpp @@ -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(); diff --git a/src/fheroes2/kingdom/color.cpp b/src/fheroes2/kingdom/color.cpp index f52a578b6a..55a7b1d230 100644 --- a/src/fheroes2/kingdom/color.cpp +++ b/src/fheroes2/kingdom/color.cpp @@ -23,6 +23,8 @@ #include "color.h" +#include + #include "players.h" #include "serialize.h" #include "tools.h" @@ -45,7 +47,7 @@ namespace }; } -std::string Color::String( int color ) +std::string Color::String( const int color ) { switch ( color ) { case Color::BLUE: @@ -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: @@ -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; } @@ -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 diff --git a/src/fheroes2/kingdom/color.h b/src/fheroes2/kingdom/color.h index 3ed36255b3..c37167bb5b 100644 --- a/src/fheroes2/kingdom/color.h +++ b/src/fheroes2/kingdom/color.h @@ -30,6 +30,8 @@ class IStreamBase; class OStreamBase; +class Kingdom; + namespace fheroes2 { const char * getBarrierColorName( const int color ); @@ -53,11 +55,12 @@ 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 ); } @@ -65,11 +68,9 @@ namespace Color class Colors : public std::vector { public: - explicit Colors( int colors = Color::ALL ); + explicit Colors( const int colors = Color::ALL ); }; -class Kingdom; - class ColorBase { int color; @@ -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; diff --git a/src/fheroes2/maps/maps_tiles.cpp b/src/fheroes2/maps/maps_tiles.cpp index f9facc091a..a514a2d5f9 100644 --- a/src/fheroes2/maps/maps_tiles.cpp +++ b/src/fheroes2/maps/maps_tiles.cpp @@ -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. @@ -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! @@ -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 ) ); @@ -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. diff --git a/src/fheroes2/maps/maps_tiles.h b/src/fheroes2/maps/maps_tiles.h index 403bff1ba7..11e99c99fe 100644 --- a/src/fheroes2/maps/maps_tiles.h +++ b/src/fheroes2/maps/maps_tiles.h @@ -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 diff --git a/src/fheroes2/world/world.cpp b/src/fheroes2/world/world.cpp index 9403332f43..45e6434932 100644 --- a/src/fheroes2/world/world.cpp +++ b/src/fheroes2/world/world.cpp @@ -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 ); } } @@ -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: @@ -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 ); } diff --git a/src/fheroes2/world/world.h b/src/fheroes2/world/world.h index 6c407cd4ad..7cbf6e70e9 100644 --- a/src/fheroes2/world/world.h +++ b/src/fheroes2/world/world.h @@ -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;