Przeglądaj źródła

- one more unitialized memory crash (#1163)
- minor tweaks for hero handler

Ivan Savenko 13 lat temu
rodzic
commit
c764ce6ebe
3 zmienionych plików z 13 dodań i 21 usunięć
  1. 5 1
      client/CAnimation.cpp
  2. 5 19
      lib/CHeroHandler.cpp
  3. 3 1
      lib/CHeroHandler.h

+ 5 - 1
client/CAnimation.cpp

@@ -84,9 +84,13 @@ class CFileCache
 			std::copy(data, data + size, ret);
 			return ret;
 		}
+		FileData():
+		    size(0),
+		    data(nullptr)
+		{}
 		~FileData()
 		{
-			delete data;
+			delete [] data;
 		}
 	};
 

+ 5 - 19
lib/CHeroHandler.cpp

@@ -253,10 +253,9 @@ void CHeroHandler::loadHeroes()
 
 	// Load heroes information
 	const JsonNode config(ResourceID("config/heroes.json"));
-	BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector()) {
-
+	BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector())
+	{
 		CHero * currentHero = heroes[hero["id"].Float()];
-		const JsonNode *value;
 
 		// sex: 0=male, 1=female
 		currentHero->sex = !!hero["female"].Bool();
@@ -268,9 +267,8 @@ void CHeroHandler::loadHeroes()
 			currentHero->secSkillsInit.push_back(std::make_pair(skillID, skillLevel));
 		}
 
-		value = &hero["spell"];
-		if (!value->isNull()) {
-			currentHero->startingSpell = value->Float();
+		if (!hero["spell"].isNull()) {
+			currentHero->startingSpell = hero["spell"].Float();
 		}
 
 		BOOST_FOREACH(const JsonNode &specialty, hero["specialties"].Vector())
@@ -322,19 +320,7 @@ void CHeroHandler::loadBallistics()
 
 ui32 CHeroHandler::level (ui64 experience) const
 {
-	int i;
-	if (experience <= expPerLevel.back())
-	{
-		for (i = expPerLevel.size()-1; experience < expPerLevel[i]; i--);
-		return i + 1;
-	}
-	else
-	{
-		i = expPerLevel.size() - 1;
-		while (experience > reqExp (i))
-			i++;
-		return i;
-	}
+	return boost::range::upper_bound(expPerLevel, experience) - boost::begin(expPerLevel);
 }
 
 ui64 CHeroHandler::reqExp (ui32 level) const

+ 3 - 1
lib/CHeroHandler.h

@@ -142,7 +142,9 @@ public:
 
 class DLL_LINKAGE CHeroHandler
 {
-	std::vector<ui64> expPerLevel; //expPerLEvel[i] is amount of exp needed to reach level i; if it is not in this vector, multiplicate last value by 1,2 to get next value
+	/// expPerLEvel[i] is amount of exp needed to reach level i;
+	/// consists of 201 values. Any higher levels require experience larger that ui64 can hold
+	std::vector<ui64> expPerLevel;
 
 public:
 	CHeroClassHandler classes;