浏览代码

Replaced vstd clamp with std version

Ivan Savenko 2 年之前
父节点
当前提交
506c3d29bc

+ 3 - 20
Global.h

@@ -425,27 +425,11 @@ namespace vstd
 		}
 		}
 	}
 	}
 
 
-	// c++17: makes a to fit the range <b, c>
-	template <typename t1, typename t2, typename t3>
-	t1 clamp(const t1 &value, const t2 &low, const t3 &high)
-	{
-		if ( value > high)
-			return high;
-
-		if ( value < low)
-			return low;
-
-		return value;
-	}
-
-
 	//makes a to fit the range <b, c>
 	//makes a to fit the range <b, c>
-	template <typename t1, typename t2, typename t3>
-	t1 &abetween(t1 &a, const t2 &b, const t3 &c)
+	template <typename T>
+	void abetween(T &value, const T &min, const T &max)
 	{
 	{
-		amax(a,b);
-		amin(a,c);
-		return a;
+		value = std::clamp(value, min, max);
 	}
 	}
 
 
 	//checks if a is between b and c
 	//checks if a is between b and c
@@ -727,7 +711,6 @@ namespace vstd
 		return a + (b - a) * f;
 		return a + (b - a) * f;
 	}
 	}
 
 
-
 	///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
 	///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
 	static constexpr int abs(int i) {
 	static constexpr int abs(int i) {
 		if(i < 0) return -i;
 		if(i < 0) return -i;

+ 1 - 1
client/battle/BattleAnimationClasses.cpp

@@ -759,7 +759,7 @@ uint32_t ShootingAnimation::getAttackClimaxFrame() const
 
 
 	uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(getGroup());
 	uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(getGroup());
 	uint32_t climaxFrame = shooterInfo->animation.attackClimaxFrame;
 	uint32_t climaxFrame = shooterInfo->animation.attackClimaxFrame;
-	uint32_t selectedFrame = vstd::clamp(shooterInfo->animation.attackClimaxFrame, 1, maxFrames);
+	uint32_t selectedFrame = std::clamp<int>(shooterInfo->animation.attackClimaxFrame, 1, maxFrames);
 
 
 	if (climaxFrame != selectedFrame)
 	if (climaxFrame != selectedFrame)
 		logGlobal->warn("Shooter %s has ranged attack climax frame set to %d, but only %d available!", shooterInfo->getNamePluralTranslated(), climaxFrame, maxFrames);
 		logGlobal->warn("Shooter %s has ranged attack climax frame set to %d, but only %d available!", shooterInfo->getNamePluralTranslated(), climaxFrame, maxFrames);

+ 2 - 2
client/mapView/MapViewController.cpp

@@ -58,10 +58,10 @@ void MapViewController::setViewCenter(const Point & position, int level)
 		lowerLimit = actualLowerLimit;
 		lowerLimit = actualLowerLimit;
 	}
 	}
 
 
-	Point betterPosition = {vstd::clamp(position.x, lowerLimit.x, upperLimit.x), vstd::clamp(position.y, lowerLimit.y, upperLimit.y)};
+	Point betterPosition = {std::clamp(position.x, lowerLimit.x, upperLimit.x), std::clamp(position.y, lowerLimit.y, upperLimit.y)};
 
 
 	model->setViewCenter(betterPosition);
 	model->setViewCenter(betterPosition);
-	model->setLevel(vstd::clamp(level, 0, context->getMapSize().z));
+	model->setLevel(std::clamp(level, 0, context->getMapSize().z));
 
 
 	if(adventureInt) // may be called before adventureInt is initialized
 	if(adventureInt) // may be called before adventureInt is initialized
 		adventureInt->onMapViewMoved(model->getTilesTotalRect(), model->getLevel());
 		adventureInt->onMapViewMoved(model->getTilesTotalRect(), model->getLevel());

+ 1 - 1
client/widgets/Buttons.cpp

@@ -488,7 +488,7 @@ void CVolumeSlider::setVolume(int value_)
 
 
 void CVolumeSlider::moveTo(int id)
 void CVolumeSlider::moveTo(int id)
 {
 {
-	vstd::abetween(id, 0, animImage->size() - 1);
+	vstd::abetween<int>(id, 0, animImage->size() - 1);
 	animImage->setFrame(id);
 	animImage->setFrame(id);
 	animImage->moveTo(Point(pos.x + (animImage->pos.w + 1) * id, pos.y));
 	animImage->moveTo(Point(pos.x + (animImage->pos.w + 1) * id, pos.y));
 	if (active)
 	if (active)

+ 1 - 1
client/windows/CSpellWindow.cpp

@@ -433,7 +433,7 @@ void CSpellWindow::keyPressed(const SDL_Keycode & key)
 			int index = -1;
 			int index = -1;
 			while(schoolsOrder[++index] != selectedTab);
 			while(schoolsOrder[++index] != selectedTab);
 			index += (down ? 1 : -1);
 			index += (down ? 1 : -1);
-			vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
+			vstd::abetween<int>(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
 			if(selectedTab != schoolsOrder[index])
 			if(selectedTab != schoolsOrder[index])
 				selectSchool(schoolsOrder[index]);
 				selectSchool(schoolsOrder[index]);
 			break;
 			break;

+ 1 - 1
client/windows/GUIClasses.cpp

@@ -1907,7 +1907,7 @@ void CObjectListWindow::keyPressed (const SDL_Keycode & key)
 		return;
 		return;
 	}
 	}
 
 
-	vstd::abetween(sel, 0, items.size()-1);
+	vstd::abetween<int>(sel, 0, items.size()-1);
 	list->scrollTo(sel);
 	list->scrollTo(sel);
 	changeSelection(sel);
 	changeSelection(sel);
 }
 }

+ 6 - 12
lib/HeroBonus.cpp

@@ -457,7 +457,7 @@ int BonusList::totalValue() const
 	};
 	};
 
 
 	auto percent = [](int64_t base, int64_t percent) -> int {
 	auto percent = [](int64_t base, int64_t percent) -> int {
-		return static_cast<int>(vstd::clamp((base * (100 + percent)) / 100, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
+		return static_cast<int>(std::clamp<int64_t>((base * (100 + percent)) / 100, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
 	};
 	};
 	std::array <BonusCollection, Bonus::BonusSource::NUM_BONUS_SOURCE> sources = {};
 	std::array <BonusCollection, Bonus::BonusSource::NUM_BONUS_SOURCE> sources = {};
 	BonusCollection any;
 	BonusCollection any;
@@ -520,7 +520,7 @@ int BonusList::totalValue() const
 	}));
 	}));
 
 
 	if(notIndepBonuses)
 	if(notIndepBonuses)
-		return vstd::clamp(valFirst, any.indepMax, any.indepMin);
+		return std::clamp(valFirst, any.indepMax, any.indepMin);
 	
 	
 	return hasIndepMin ? any.indepMin : hasIndepMax ? any.indepMax : 0;
 	return hasIndepMin ? any.indepMin : hasIndepMax ? any.indepMax : 0;
 }
 }
@@ -709,9 +709,7 @@ int IBonusBearer::MoraleVal() const
 	if(anaffectedByMorale.getHasBonus())
 	if(anaffectedByMorale.getHasBonus())
 		return 0;
 		return 0;
 
 
-	int ret = moraleValue.getValue();
-
-	return vstd::abetween(ret, -3, +3);
+	return std::clamp(moraleValue.getValue(), -3, +3);
 }
 }
 
 
 int IBonusBearer::LuckVal() const
 int IBonusBearer::LuckVal() const
@@ -719,9 +717,7 @@ int IBonusBearer::LuckVal() const
 	if(hasBonusOfType(Bonus::NO_LUCK))
 	if(hasBonusOfType(Bonus::NO_LUCK))
 		return 0;
 		return 0;
 
 
-	int ret = luckValue.getValue();
-
-	return vstd::abetween(ret, -3, +3);
+	return std::clamp(luckValue.getValue(), -3, +3);
 }
 }
 
 
 int IBonusBearer::MoraleValAndBonusList(TConstBonusListPtr & bonusList) const
 int IBonusBearer::MoraleValAndBonusList(TConstBonusListPtr & bonusList) const
@@ -732,9 +728,8 @@ int IBonusBearer::MoraleValAndBonusList(TConstBonusListPtr & bonusList) const
 			bonusList = std::make_shared<const BonusList>();
 			bonusList = std::make_shared<const BonusList>();
 		return 0;
 		return 0;
 	}
 	}
-	int ret = moraleValue.getValueAndList(bonusList);
 
 
-	return vstd::abetween(ret, -3, +3);
+	return std::clamp(moraleValue.getValueAndList(bonusList), -3, +3);
 }
 }
 
 
 int IBonusBearer::LuckValAndBonusList(TConstBonusListPtr & bonusList) const
 int IBonusBearer::LuckValAndBonusList(TConstBonusListPtr & bonusList) const
@@ -745,9 +740,8 @@ int IBonusBearer::LuckValAndBonusList(TConstBonusListPtr & bonusList) const
 			bonusList = std::make_shared<const BonusList>();
 			bonusList = std::make_shared<const BonusList>();
 		return 0;
 		return 0;
 	}
 	}
-	int ret = luckValue.getValueAndList(bonusList);
 
 
-	return vstd::abetween(ret, -3, +3);
+	return std::clamp(luckValue.getValueAndList(bonusList), -3, +3);
 }
 }
 
 
 ui32 IBonusBearer::MaxHealth() const
 ui32 IBonusBearer::MaxHealth() const

+ 5 - 2
lib/NetPacksLib.cpp

@@ -1688,7 +1688,9 @@ void RebalanceStacks::applyGs(CGameState * gs)
 
 
 	if(srcCount == count) //moving whole stack
 	if(srcCount == count) //moving whole stack
 	{
 	{
-		if([[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> merge
+		[[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot);
+
+		if(c) //stack at dest -> merge
 		{
 		{
 			assert(c == srcType);
 			assert(c == srcType);
 			auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT);
 			auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT);
@@ -1742,7 +1744,8 @@ void RebalanceStacks::applyGs(CGameState * gs)
 	}
 	}
 	else
 	else
 	{
 	{
-		if([[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> rebalance
+		[[maybe_unused]] const CCreature *c = dst.army->getCreature(dst.slot);
+		if(c) //stack at dest -> rebalance
 		{
 		{
 			assert(c == srcType);
 			assert(c == srcType);
 			if (stackExp)
 			if (stackExp)

+ 1 - 1
lib/battle/CUnitState.cpp

@@ -249,7 +249,7 @@ void CHealth::heal(int64_t & amount, EHealLevel level, EHealPower power)
 	}
 	}
 
 
 	vstd::amax(maxHeal, 0);
 	vstd::amax(maxHeal, 0);
-	vstd::abetween(amount, 0, maxHeal);
+	vstd::abetween(amount, int64_t(0), maxHeal);
 
 
 	if(amount == 0)
 	if(amount == 0)
 		return;
 		return;

+ 4 - 5
lib/mapObjects/CGTownInstance.cpp

@@ -66,15 +66,14 @@ void CCreGenAsCastleInfo::serializeJson(JsonSerializeFormat & handler)
 
 
 void CCreGenLeveledInfo::serializeJson(JsonSerializeFormat & handler)
 void CCreGenLeveledInfo::serializeJson(JsonSerializeFormat & handler)
 {
 {
-	handler.serializeInt("minLevel", minLevel, static_cast<ui8>(1));
-	handler.serializeInt("maxLevel", maxLevel, static_cast<ui8>(7));
+	handler.serializeInt("minLevel", minLevel, static_cast<uint8_t>(1));
+	handler.serializeInt("maxLevel", maxLevel, static_cast<uint8_t>(7));
 
 
 	if(!handler.saving)
 	if(!handler.saving)
 	{
 	{
 		//todo: safely allow any level > 7
 		//todo: safely allow any level > 7
-		vstd::amax(minLevel, 1);
-		vstd::amin(minLevel, 7);
-		vstd::abetween(maxLevel, minLevel, 7);
+		vstd::abetween<uint8_t>(minLevel, 1, 7);
+		vstd::abetween<uint8_t>(maxLevel, minLevel, 7);
 	}
 	}
 }
 }
 
 

+ 1 - 1
server/NetPacksLobbyServer.cpp

@@ -388,7 +388,7 @@ void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
 
 
 void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
 void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
 {
 {
-	srv.si->difficulty = vstd::abetween(pack.difficulty, 0, 4);
+	srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
 	result = true;
 	result = true;
 }
 }