diff --git a/src/fheroes2/editor/editor_interface.cpp b/src/fheroes2/editor/editor_interface.cpp index 75999444a8c..e95a9de02bf 100644 --- a/src/fheroes2/editor/editor_interface.cpp +++ b/src/fheroes2/editor/editor_interface.cpp @@ -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( townObjects[object.index].metadata[0] ) ); + + world.addCastle( static_cast( 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( metadata[0] ) ); + + Heroes * hero = world.GetHeroForHire( static_cast( metadata[1] ) ); + if ( hero ) { + hero->SetCenter( { static_cast( i ) % world.w(), static_cast( i ) / world.w() } ); + hero->SetColor( color ); + } + } + } + } + } } namespace Interface @@ -1104,6 +1131,16 @@ namespace Interface return; } + Heroes * hero = world.GetHeroForHire( Race::IndexToRace( static_cast( objectInfo.metadata[1] ) ) ); + if ( hero ) { + hero->SetCenter( tilePos ); + hero->SetColor( Color::IndexToColor( static_cast( 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." ) ); } @@ -1362,6 +1399,8 @@ namespace Interface return; } + world.addCastle( tile.GetIndex(), Race::IndexToRace( static_cast( townObjectInfo.metadata[0] ) ), Color::IndexToColor( color ) ); + action.commit(); if ( !Maps::updateMapPlayers( _mapFormat ) ) { @@ -1531,6 +1570,8 @@ namespace Interface return false; } + updateWorldCastlesHeroes( _mapFormat ); + return true; } } diff --git a/src/fheroes2/world/world.h b/src/fheroes2/world/world.h index 60e85f3f9a0..eb33f33515b 100644 --- a/src/fheroes2/world/world.h +++ b/src/fheroes2/world/world.h @@ -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 * @@ -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 {