Selaa lähdekoodia

initialize uidCounter when loading game

kdmcser 5 kuukautta sitten
vanhempi
sitoutus
82870e82c9
2 muutettua tiedostoa jossa 36 lisäystä ja 1 poistoa
  1. 31 0
      lib/mapping/CMap.cpp
  2. 5 1
      lib/mapping/CMap.h

+ 31 - 0
lib/mapping/CMap.cpp

@@ -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

+ 5 - 1
lib/mapping/CMap.h

@@ -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();
 	}
 };