1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-05-23 22:40:07 +02:00

initialize uidCounter when loading game

This commit is contained in:
kdmcser 2025-05-05 01:19:29 +08:00
parent ac26b3ed9b
commit 82870e82c9
2 changed files with 36 additions and 1 deletions

View File

@ -936,4 +936,35 @@ ObjectInstanceID CMap::allocateUniqueInstanceID()
return ObjectInstanceID(objects.size() - 1);
}
void CMap::parseUidCounter()
{
int max_index = -1;
for (const auto& entry : instanceNames) {
const std::string& key = entry.first;
const size_t pos = key.find_last_of('_');
// Validate underscore position
if (pos == std::string::npos || pos + 1 >= key.size()) {
logGlobal->error("Instance name '%s' is not valid.", key);
continue;
}
const std::string index_part = key.substr(pos + 1);
try {
const int current_index = std::stoi(index_part);
max_index = std::max(max_index, current_index);
}
catch (const std::invalid_argument& e) {
logGlobal->error("Instance name %s contains non-numeric index part: %s", key, index_part);
}
catch (const std::out_of_range&) {
logGlobal->error("Instance name %s index part is overflow.", key);
}
}
// Directly set uidCounter using simplified logic
uidCounter = max_index + 1; // Automatically 0 when max_index = -1
}
VCMI_LIB_NAMESPACE_END

View File

@ -280,10 +280,12 @@ public:
void saveCompatibilityStoreAllocatedArtifactID();
private:
void parseUidCounter();
/// a 3-dimensional array of terrain tiles, access is as follows: x, y, level. where level=1 is underground
boost::multi_array<TerrainTile, 3> terrain;
si32 uidCounter; //TODO: initialize when loading an old map
si32 uidCounter;
public:
template <typename Handler>
@ -346,6 +348,8 @@ public:
h & instanceNames;
h & *gameSettings;
if (!h.saving)
parseUidCounter();
}
};