From 7b702bf8c2d42504f7be1b8f58f1c68a7fce0cad Mon Sep 17 00:00:00 2001 From: David Cernat Date: Thu, 19 Apr 2018 18:16:10 +0300 Subject: [PATCH] [General] Add and use utility function for int value checks in vectors (cherry picked from commit a796f8144492d53b1f99494725750d38d11c974a) --- apps/openmw-mp/Script/Functions/Stats.cpp | 18 +++++++++--------- components/openmw-mp/Utils.cpp | 5 +++++ components/openmw-mp/Utils.hpp | 3 +++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Stats.cpp b/apps/openmw-mp/Script/Functions/Stats.cpp index 4d88fdfbd..8e63067bd 100644 --- a/apps/openmw-mp/Script/Functions/Stats.cpp +++ b/apps/openmw-mp/Script/Functions/Stats.cpp @@ -409,8 +409,7 @@ void StatsFunctions::SetAttributeBase(unsigned short pid, unsigned short attribu player->creatureStats.mAttributes[attributeId].mBase = value; - if (std::find(player->attributeChanges.attributeIndexes.begin(), player->attributeChanges.attributeIndexes.end(), attributeId) == - player->attributeChanges.attributeIndexes.end()) + if (!Utils::vectorContains(&player->attributeChanges.attributeIndexes, attributeId)) player->attributeChanges.attributeIndexes.push_back(attributeId); } @@ -424,8 +423,7 @@ void StatsFunctions::ClearAttributeModifier(unsigned short pid, unsigned short a player->creatureStats.mAttributes[attributeId].mMod = 0; - if (std::find(player->attributeChanges.attributeIndexes.begin(), player->attributeChanges.attributeIndexes.end(), attributeId) == - player->attributeChanges.attributeIndexes.end()) + if (!Utils::vectorContains(&player->attributeChanges.attributeIndexes, attributeId)) player->attributeChanges.attributeIndexes.push_back(attributeId); } @@ -439,7 +437,8 @@ void StatsFunctions::SetSkillBase(unsigned short pid, unsigned short skillId, in player->npcStats.mSkills[skillId].mBase = value; - player->skillChanges.skillIndexes.push_back(skillId); + if (!Utils::vectorContains(&player->skillChanges.skillIndexes, skillId)) + player->skillChanges.skillIndexes.push_back(skillId); } void StatsFunctions::ClearSkillModifier(unsigned short pid, unsigned short skillId) noexcept @@ -452,7 +451,8 @@ void StatsFunctions::ClearSkillModifier(unsigned short pid, unsigned short skill player->npcStats.mSkills[skillId].mMod = 0; - player->skillChanges.skillIndexes.push_back(skillId); + if (!Utils::vectorContains(&player->skillChanges.skillIndexes, skillId)) + player->skillChanges.skillIndexes.push_back(skillId); } void StatsFunctions::SetSkillProgress(unsigned short pid, unsigned short skillId, double value) noexcept @@ -465,7 +465,8 @@ void StatsFunctions::SetSkillProgress(unsigned short pid, unsigned short skillId player->npcStats.mSkills[skillId].mProgress = value; - player->skillChanges.skillIndexes.push_back(skillId); + if (!Utils::vectorContains(&player->skillChanges.skillIndexes, skillId)) + player->skillChanges.skillIndexes.push_back(skillId); } void StatsFunctions::SetSkillIncrease(unsigned short pid, unsigned int attributeId, int value) noexcept @@ -478,8 +479,7 @@ void StatsFunctions::SetSkillIncrease(unsigned short pid, unsigned int attribute player->npcStats.mSkillIncrease[attributeId] = value; - if (std::find(player->attributeChanges.attributeIndexes.begin(), player->attributeChanges.attributeIndexes.end(), attributeId) == - player->attributeChanges.attributeIndexes.end()) + if (!Utils::vectorContains(&player->attributeChanges.attributeIndexes, attributeId)) player->attributeChanges.attributeIndexes.push_back(attributeId); } diff --git a/components/openmw-mp/Utils.cpp b/components/openmw-mp/Utils.cpp index a4a4b6e7b..f96173190 100644 --- a/components/openmw-mp/Utils.cpp +++ b/components/openmw-mp/Utils.cpp @@ -85,6 +85,11 @@ bool Utils::compareDoubles(double a, double b, double epsilon) return fabs(a - b) < epsilon; } +bool Utils::vectorContains(std::vector* vectorChecked, int value) +{ + return std::find(vectorChecked->begin(), vectorChecked->end(), value) != vectorChecked->end(); +} + std::string Utils::toString(int num) { std::ostringstream stream; diff --git a/components/openmw-mp/Utils.hpp b/components/openmw-mp/Utils.hpp index 448cb13d7..a809e3796 100644 --- a/components/openmw-mp/Utils.hpp +++ b/components/openmw-mp/Utils.hpp @@ -7,6 +7,7 @@ #include #include +#include #if (defined __WIN32__ || defined _WIN32 || defined WIN32) #define __WINDOWS @@ -26,6 +27,8 @@ namespace Utils bool compareDoubles(double a, double b, double epsilon); + bool vectorContains(std::vector* vectorChecked, int value); + std::string replaceString(const std::string &source, const char *find, const char *replace); std::string toString(int num);