diff --git a/Global.h b/Global.h index 0e5029d1f..f8ee40e4b 100644 --- a/Global.h +++ b/Global.h @@ -425,27 +425,11 @@ namespace vstd } } - // c++17: makes a to fit the range - template - 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 - template - t1 &abetween(t1 &a, const t2 &b, const t3 &c) + template + 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 @@ -727,7 +711,6 @@ namespace vstd return a + (b - a) * f; } - ///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr static constexpr int abs(int i) { if(i < 0) return -i; diff --git a/client/battle/BattleAnimationClasses.cpp b/client/battle/BattleAnimationClasses.cpp index 6eb0308ce..a7e2d5b36 100644 --- a/client/battle/BattleAnimationClasses.cpp +++ b/client/battle/BattleAnimationClasses.cpp @@ -759,7 +759,7 @@ uint32_t ShootingAnimation::getAttackClimaxFrame() const uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(getGroup()); uint32_t climaxFrame = shooterInfo->animation.attackClimaxFrame; - uint32_t selectedFrame = vstd::clamp(shooterInfo->animation.attackClimaxFrame, 1, maxFrames); + uint32_t selectedFrame = std::clamp(shooterInfo->animation.attackClimaxFrame, 1, maxFrames); if (climaxFrame != selectedFrame) logGlobal->warn("Shooter %s has ranged attack climax frame set to %d, but only %d available!", shooterInfo->getNamePluralTranslated(), climaxFrame, maxFrames); diff --git a/client/mapView/MapViewController.cpp b/client/mapView/MapViewController.cpp index 2f0fd06bd..3e547a396 100644 --- a/client/mapView/MapViewController.cpp +++ b/client/mapView/MapViewController.cpp @@ -58,10 +58,10 @@ void MapViewController::setViewCenter(const Point & position, int level) 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->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 adventureInt->onMapViewMoved(model->getTilesTotalRect(), model->getLevel()); diff --git a/client/widgets/Buttons.cpp b/client/widgets/Buttons.cpp index c9d39c1e0..bb1553fd6 100644 --- a/client/widgets/Buttons.cpp +++ b/client/widgets/Buttons.cpp @@ -488,7 +488,7 @@ void CVolumeSlider::setVolume(int value_) void CVolumeSlider::moveTo(int id) { - vstd::abetween(id, 0, animImage->size() - 1); + vstd::abetween(id, 0, animImage->size() - 1); animImage->setFrame(id); animImage->moveTo(Point(pos.x + (animImage->pos.w + 1) * id, pos.y)); if (active) diff --git a/client/windows/CSpellWindow.cpp b/client/windows/CSpellWindow.cpp index 6150ab154..3fc04fdd4 100644 --- a/client/windows/CSpellWindow.cpp +++ b/client/windows/CSpellWindow.cpp @@ -433,7 +433,7 @@ void CSpellWindow::keyPressed(const SDL_Keycode & key) int index = -1; while(schoolsOrder[++index] != selectedTab); index += (down ? 1 : -1); - vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1); + vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1); if(selectedTab != schoolsOrder[index]) selectSchool(schoolsOrder[index]); break; diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index 82c638d46..4f7cd3ec4 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -1907,7 +1907,7 @@ void CObjectListWindow::keyPressed (const SDL_Keycode & key) return; } - vstd::abetween(sel, 0, items.size()-1); + vstd::abetween(sel, 0, items.size()-1); list->scrollTo(sel); changeSelection(sel); } diff --git a/lib/HeroBonus.cpp b/lib/HeroBonus.cpp index 6e2039948..24f9e1a97 100644 --- a/lib/HeroBonus.cpp +++ b/lib/HeroBonus.cpp @@ -457,7 +457,7 @@ int BonusList::totalValue() const }; auto percent = [](int64_t base, int64_t percent) -> int { - return static_cast(vstd::clamp((base * (100 + percent)) / 100, std::numeric_limits::min(), std::numeric_limits::max())); + return static_cast(std::clamp((base * (100 + percent)) / 100, std::numeric_limits::min(), std::numeric_limits::max())); }; std::array sources = {}; BonusCollection any; @@ -520,7 +520,7 @@ int BonusList::totalValue() const })); 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; } @@ -709,9 +709,7 @@ int IBonusBearer::MoraleVal() const if(anaffectedByMorale.getHasBonus()) return 0; - int ret = moraleValue.getValue(); - - return vstd::abetween(ret, -3, +3); + return std::clamp(moraleValue.getValue(), -3, +3); } int IBonusBearer::LuckVal() const @@ -719,9 +717,7 @@ int IBonusBearer::LuckVal() const if(hasBonusOfType(Bonus::NO_LUCK)) 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 @@ -732,9 +728,8 @@ int IBonusBearer::MoraleValAndBonusList(TConstBonusListPtr & bonusList) const bonusList = std::make_shared(); 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 @@ -745,9 +740,8 @@ int IBonusBearer::LuckValAndBonusList(TConstBonusListPtr & bonusList) const bonusList = std::make_shared(); 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 diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index f124bd6c7..260abd6d8 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -1688,7 +1688,9 @@ void RebalanceStacks::applyGs(CGameState * gs) 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); auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT); @@ -1742,7 +1744,8 @@ void RebalanceStacks::applyGs(CGameState * gs) } 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); if (stackExp) diff --git a/lib/battle/CUnitState.cpp b/lib/battle/CUnitState.cpp index 137fe8a79..52ac0c379 100644 --- a/lib/battle/CUnitState.cpp +++ b/lib/battle/CUnitState.cpp @@ -249,7 +249,7 @@ void CHealth::heal(int64_t & amount, EHealLevel level, EHealPower power) } vstd::amax(maxHeal, 0); - vstd::abetween(amount, 0, maxHeal); + vstd::abetween(amount, int64_t(0), maxHeal); if(amount == 0) return; diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index fd08f831c..76e2cd739 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -66,15 +66,14 @@ void CCreGenAsCastleInfo::serializeJson(JsonSerializeFormat & handler) void CCreGenLeveledInfo::serializeJson(JsonSerializeFormat & handler) { - handler.serializeInt("minLevel", minLevel, static_cast(1)); - handler.serializeInt("maxLevel", maxLevel, static_cast(7)); + handler.serializeInt("minLevel", minLevel, static_cast(1)); + handler.serializeInt("maxLevel", maxLevel, static_cast(7)); if(!handler.saving) { //todo: safely allow any level > 7 - vstd::amax(minLevel, 1); - vstd::amin(minLevel, 7); - vstd::abetween(maxLevel, minLevel, 7); + vstd::abetween(minLevel, 1, 7); + vstd::abetween(maxLevel, minLevel, 7); } } diff --git a/server/NetPacksLobbyServer.cpp b/server/NetPacksLobbyServer.cpp index a4a85c900..cfeead4a0 100644 --- a/server/NetPacksLobbyServer.cpp +++ b/server/NetPacksLobbyServer.cpp @@ -388,7 +388,7 @@ void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack) void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack) { - srv.si->difficulty = vstd::abetween(pack.difficulty, 0, 4); + srv.si->difficulty = std::clamp(pack.difficulty, 0, 4); result = true; }