From fb476f349258bb4a1f191b4667d610e30caeefeb Mon Sep 17 00:00:00 2001 From: Ihar Hubchyk Date: Mon, 19 Dec 2022 08:05:58 +0800 Subject: [PATCH] Fix incorrect resource placements on certain maps (#6395) --- src/fheroes2/maps/maps_tiles_quantity.cpp | 43 ++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/fheroes2/maps/maps_tiles_quantity.cpp b/src/fheroes2/maps/maps_tiles_quantity.cpp index 5018f8e4acb..f35f3624dc7 100644 --- a/src/fheroes2/maps/maps_tiles_quantity.cpp +++ b/src/fheroes2/maps/maps_tiles_quantity.cpp @@ -22,10 +22,14 @@ ***************************************************************************/ #include +#include +#include +#include #include "army_troop.h" #include "artifact.h" #include "color.h" +#include "logging.h" #include "maps_tiles.h" #include "monster.h" #include "mp2.h" @@ -554,10 +558,32 @@ void Maps::Tiles::QuantityUpdate( bool isFirstLoad ) } case MP2::OBJ_RESOURCE: { - const int res = Resource::FromIndexSprite( objectIndex ); + int resourceType = Resource::UNKNOWN; + // TODO: add a function opposite to MP2::GetICNObject() to return tileset ID. + const int resourceTileSet = 46; + + if ( ( objectTileset >> 2 ) == resourceTileSet ) { + // The resource is located at the top. + resourceType = Resource::FromIndexSprite( objectIndex ); + } + else { + for ( TilesAddon & addon : addons_level1 ) { + if ( ( addon.object >> 2 ) == resourceTileSet ) { + resourceType = Resource::FromIndexSprite( addon.index ); + // If this happens we are in trouble. It looks like that map maker put the resource under an object which is impossible to do. + // Let's swap the addon and main tile objects + std::swap( addon.object, objectTileset ); + std::swap( addon.index, objectIndex ); + std::swap( addon.uniq, uniq ); + std::swap( addon.level, _level ); + + break; + } + } + } uint32_t count = 0; - switch ( res ) { + switch ( resourceType ) { case Resource::GOLD: count = 100 * Rand::Get( 5, 10 ); break; @@ -565,12 +591,21 @@ void Maps::Tiles::QuantityUpdate( bool isFirstLoad ) case Resource::ORE: count = Rand::Get( 5, 10 ); break; - default: + case Resource::MERCURY: + case Resource::SULFUR: + case Resource::CRYSTAL: + case Resource::GEMS: count = Rand::Get( 3, 6 ); break; + default: + // Some maps have broken resources being put which ideally we need to correct. Let's make them 0 Wood. + DEBUG_LOG( DBG_GAME, DBG_WARN, "Tile " << _index << " contains unknown resource type. Tileset " << objectTileset << ", object index " << objectIndex ) + resourceType = Resource::WOOD; + count = 0; + break; } - QuantitySetResource( res, count ); + QuantitySetResource( resourceType, count ); break; }