Skip to content

Commit

Permalink
Fix radar rendering for heroes and castles (#8577)
Browse files Browse the repository at this point in the history
  • Loading branch information
idshibanov authored Apr 24, 2024
1 parent bcbf85a commit 4fd43fb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/fheroes2/editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,33 @@ namespace

return needRedraw;
}

void updateWorldCastlesHeroes( const Maps::Map_Format::MapFormat & map )
{
const auto & townObjects = Maps::getObjectsByGroup( Maps::ObjectGroup::KINGDOM_TOWNS );
const auto & heroObjects = Maps::getObjectsByGroup( Maps::ObjectGroup::KINGDOM_HEROES );

for ( size_t i = 0; i < map.tiles.size(); ++i ) {
for ( const auto & object : map.tiles[i].objects ) {
if ( object.group == Maps::ObjectGroup::KINGDOM_TOWNS ) {
const uint8_t color = Color::IndexToColor( Maps::getTownColorIndex( map, i, object.id ) );
const uint8_t race = Race::IndexToRace( static_cast<int>( townObjects[object.index].metadata[0] ) );

world.addCastle( static_cast<int32_t>( i ), race, color );
}
else if ( object.group == Maps::ObjectGroup::KINGDOM_HEROES ) {
const auto & metadata = heroObjects[object.index].metadata;
const uint8_t color = Color::IndexToColor( static_cast<int>( metadata[0] ) );

Heroes * hero = world.GetHeroForHire( static_cast<int>( metadata[1] ) );
if ( hero ) {
hero->SetCenter( { static_cast<int32_t>( i ) % world.w(), static_cast<int32_t>( i ) / world.w() } );
hero->SetColor( color );
}
}
}
}
}
}

namespace Interface
Expand Down Expand Up @@ -1104,6 +1131,16 @@ namespace Interface
return;
}

Heroes * hero = world.GetHeroForHire( Race::IndexToRace( static_cast<int>( objectInfo.metadata[1] ) ) );
if ( hero ) {
hero->SetCenter( tilePos );
hero->SetColor( Color::IndexToColor( static_cast<int>( color ) ) );
}
else {
// How is it possible that the action was successful but no hero?
assert( 0 );
}

if ( !Maps::updateMapPlayers( _mapFormat ) ) {
_warningMessage.reset( _( "Failed to update player information." ) );
}
Expand Down Expand Up @@ -1362,6 +1399,8 @@ namespace Interface
return;
}

world.addCastle( tile.GetIndex(), Race::IndexToRace( static_cast<int>( townObjectInfo.metadata[0] ) ), Color::IndexToColor( color ) );

action.commit();

if ( !Maps::updateMapPlayers( _mapFormat ) ) {
Expand Down Expand Up @@ -1531,6 +1570,8 @@ namespace Interface
return false;
}

updateWorldCastlesHeroes( _mapFormat );

return true;
}
}
9 changes: 8 additions & 1 deletion src/fheroes2/world/world.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2019 - 2023 *
* Copyright (C) 2019 - 2024 *
* *
* Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
* Copyright (C) 2009 by Andrey Afletdinov <[email protected]> *
Expand Down Expand Up @@ -228,6 +228,13 @@ class World : protected fheroes2::Size
return vec_kingdoms.GetKingdom( color );
}

void addCastle( int32_t index, uint8_t race, uint8_t color )
{
Castle * castle = new Castle( index % width, index / width, race );
castle->SetColor( color );
vec_castles.AddCastle( castle );
}

// Get castle based on its tile. If the tile is not a part of a castle return nullptr.
const Castle * getCastle( const fheroes2::Point & tilePosition ) const
{
Expand Down

0 comments on commit 4fd43fb

Please sign in to comment.