From 30e0a16ee97a2e125000056e4bdd773ed60bce6b Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Mon, 6 May 2024 15:33:30 +0000 Subject: [PATCH] Fix some 'new' issues reported by Sonar Cloud --- AI/Nullkiller/Engine/Nullkiller.cpp | 2 +- AI/Nullkiller/Pathfinding/ObjectGraph.h | 2 +- client/battle/BattleInterface.cpp | 4 ++-- client/eventsSDL/InputHandler.cpp | 8 +++---- .../eventsSDL/InputSourceGameController.cpp | 22 +++++++++--------- client/eventsSDL/InputSourceGameController.h | 4 ++-- client/globalLobby/GlobalLobbyRoomWindow.cpp | 4 ++-- client/widgets/CreatureCostBox.cpp | 5 ++-- lib/mapObjects/CGDwelling.h | 2 +- lib/mapObjects/ObstacleSetHandler.cpp | 23 ++++++++----------- lib/mapObjects/ObstacleSetHandler.h | 10 ++++---- lib/modding/ModIncompatibility.h | 2 +- 12 files changed, 43 insertions(+), 45 deletions(-) diff --git a/AI/Nullkiller/Engine/Nullkiller.cpp b/AI/Nullkiller/Engine/Nullkiller.cpp index 2be3bf7f7..695ecdc0a 100644 --- a/AI/Nullkiller/Engine/Nullkiller.cpp +++ b/AI/Nullkiller/Engine/Nullkiller.cpp @@ -418,7 +418,7 @@ void Nullkiller::makeTurn() scanDepth = ScanDepth::ALL_FULL; useHeroChain = false; hasAnySuccess = true; - break;; + break; } logAi->trace("Goal %s has too low priority. It is not worth doing it.", taskDescription); diff --git a/AI/Nullkiller/Pathfinding/ObjectGraph.h b/AI/Nullkiller/Pathfinding/ObjectGraph.h index 368006331..b1ab1522c 100644 --- a/AI/Nullkiller/Pathfinding/ObjectGraph.h +++ b/AI/Nullkiller/Pathfinding/ObjectGraph.h @@ -140,7 +140,7 @@ struct GraphPathNodePointer } }; -typedef std::unordered_map GraphNodeStorage; +using GraphNodeStorage = std::unordered_map; class GraphNodeComparer { diff --git a/client/battle/BattleInterface.cpp b/client/battle/BattleInterface.cpp index 29519dc6f..5a5b45104 100644 --- a/client/battle/BattleInterface.cpp +++ b/client/battle/BattleInterface.cpp @@ -729,10 +729,10 @@ void BattleInterface::requestAutofightingAIToTakeAction() // FIXME: unsafe // Run task in separate thread to avoid UI lock while AI is making turn (which might take some time) // HOWEVER this thread won't atttempt to lock game state, potentially leading to races - boost::thread aiThread([battleID = this->battleID, curInt = this->curInt, activeStack]() + boost::thread aiThread([localBattleID = battleID, localCurInt = curInt, activeStack]() { setThreadName("autofightingAI"); - curInt->autofightingAI->activeStack(battleID, activeStack); + localCurInt->autofightingAI->activeStack(localBattleID, activeStack); }); aiThread.detach(); } diff --git a/client/eventsSDL/InputHandler.cpp b/client/eventsSDL/InputHandler.cpp index 10a080ef0..8bd5228d4 100644 --- a/client/eventsSDL/InputHandler.cpp +++ b/client/eventsSDL/InputHandler.cpp @@ -33,14 +33,14 @@ #include InputHandler::InputHandler() - : mouseHandler(std::make_unique()) + : enableMouse(settings["input"]["enableMouse"].Bool()) + , enableTouch(settings["input"]["enableTouch"].Bool()) + , enableController(settings["input"]["enableController"].Bool()) + , mouseHandler(std::make_unique()) , keyboardHandler(std::make_unique()) , fingerHandler(std::make_unique()) , textHandler(std::make_unique()) , gameControllerHandler(std::make_unique()) - , enableMouse(settings["input"]["enableMouse"].Bool()) - , enableTouch(settings["input"]["enableTouch"].Bool()) - , enableController(settings["input"]["enableController"].Bool()) { } diff --git a/client/eventsSDL/InputSourceGameController.cpp b/client/eventsSDL/InputSourceGameController.cpp index 09cbb0324..fb5a1a3b3 100644 --- a/client/eventsSDL/InputSourceGameController.cpp +++ b/client/eventsSDL/InputSourceGameController.cpp @@ -28,13 +28,6 @@ void InputSourceGameController::gameControllerDeleter(SDL_GameController * gameC } InputSourceGameController::InputSourceGameController(): - configTriggerTreshold(settings["input"]["controllerTriggerTreshold"].Float()), - configAxisDeadZone(settings["input"]["controllerAxisDeadZone"].Float()), - configAxisFullZone(settings["input"]["controllerAxisFullZone"].Float()), - configPointerSpeed(settings["input"]["controllerPointerSpeed"].Float()), - configPointerScale(settings["input"]["controllerPointerScale"].Float()), - configPanningSpeed(settings["input"]["controllerPanningSpeed"].Float()), - configPanningScale(settings["input"]["controllerPanningScale"].Float()), cursorAxisValueX(0), cursorAxisValueY(0), cursorPlanDisX(0.0), @@ -45,7 +38,14 @@ InputSourceGameController::InputSourceGameController(): scrollAxisValueX(0), scrollAxisValueY(0), scrollPlanDisX(0.0), - scrollPlanDisY(0.0) + scrollPlanDisY(0.0), + configTriggerTreshold(settings["input"]["controllerTriggerTreshold"].Float()), + configAxisDeadZone(settings["input"]["controllerAxisDeadZone"].Float()), + configAxisFullZone(settings["input"]["controllerAxisFullZone"].Float()), + configPointerSpeed(settings["input"]["controllerPointerSpeed"].Float()), + configPointerScale(settings["input"]["controllerPointerScale"].Float()), + configPanningSpeed(settings["input"]["controllerPanningSpeed"].Float()), + configPanningScale(settings["input"]["controllerPanningScale"].Float()) { tryOpenAllGameControllers(); } @@ -129,7 +129,7 @@ void InputSourceGameController::handleEventDeviceRemapped(const SDL_ControllerDe openGameController(device.which); } -double InputSourceGameController::getRealAxisValue(int value) +double InputSourceGameController::getRealAxisValue(int value) const { double ratio = static_cast(value) / SDL_JOYSTICK_AXIS_MAX; double greenZone = configAxisFullZone - configAxisDeadZone; @@ -323,7 +323,7 @@ void InputSourceGameController::handleScrollUpdate(int32_t deltaTimeMs) } } -bool InputSourceGameController::isScrollAxisReleased() +bool InputSourceGameController::isScrollAxisReleased() const { - return scrollAxisValueX == 0 && scrollAxisValueY == 0; + return vstd::isAlmostZero(scrollAxisValueX) && vstd::isAlmostZero(scrollAxisValueY); } diff --git a/client/eventsSDL/InputSourceGameController.h b/client/eventsSDL/InputSourceGameController.h index 348e5e77a..5eadc3058 100644 --- a/client/eventsSDL/InputSourceGameController.h +++ b/client/eventsSDL/InputSourceGameController.h @@ -49,14 +49,14 @@ class InputSourceGameController void openGameController(int index); int getJoystickIndex(SDL_GameController * controller); - double getRealAxisValue(int value); + double getRealAxisValue(int value) const; void dispatchAxisShortcuts(const std::vector & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue); void tryToConvertCursor(); void doCursorMove(int deltaX, int deltaY); int getMoveDis(float planDis); void handleCursorUpdate(int32_t deltaTimeMs); void handleScrollUpdate(int32_t deltaTimeMs); - bool isScrollAxisReleased(); + bool isScrollAxisReleased() const; public: InputSourceGameController(); diff --git a/client/globalLobby/GlobalLobbyRoomWindow.cpp b/client/globalLobby/GlobalLobbyRoomWindow.cpp index 45b9cc8d3..cf2f281dd 100644 --- a/client/globalLobby/GlobalLobbyRoomWindow.cpp +++ b/client/globalLobby/GlobalLobbyRoomWindow.cpp @@ -62,7 +62,7 @@ GlobalLobbyRoomModCard::GlobalLobbyRoomModCard(const GlobalLobbyRoomModInfo & mo labelStatus = std::make_shared(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.mod.state." + statusToString.at(modInfo.status))); } -static const std::string getJoinRoomErrorMessage(const GlobalLobbyRoom & roomDescription, const std::vector & modVerificationList) +static std::string getJoinRoomErrorMessage(const GlobalLobbyRoom & roomDescription, const std::vector & modVerificationList) { bool publicRoom = roomDescription.statusID == "public"; bool privateRoom = roomDescription.statusID == "private"; @@ -106,8 +106,8 @@ static const std::string getJoinRoomErrorMessage(const GlobalLobbyRoom & roomDes GlobalLobbyRoomWindow::GlobalLobbyRoomWindow(GlobalLobbyWindow * window, const std::string & roomUUID) : CWindowObject(BORDERED) - , roomUUID(roomUUID) , window(window) + , roomUUID(roomUUID) { OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE; diff --git a/client/widgets/CreatureCostBox.cpp b/client/widgets/CreatureCostBox.cpp index c7a651bfa..813b38b7e 100644 --- a/client/widgets/CreatureCostBox.cpp +++ b/client/widgets/CreatureCostBox.cpp @@ -49,14 +49,15 @@ void CreatureCostBox::createItems(TResources res) { int curx = pos.w / 2; int spacing = 48; + int resourcesCount = static_cast(resources.size()); if (resources.size() > 2) { spacing = 32; - curx -= (15 + 16 * ((int)resources.size() - 1)); + curx -= (15 + 16 * (resourcesCount - 1)); } else { - curx -= ((16 * (int)resources.size()) + (8 * ((int)resources.size() - 1))); + curx -= ((16 * resourcesCount) + (8 * (resourcesCount - 1))); } //reverse to display gold as first resource for(auto & currentRes : boost::adaptors::reverse(resources)) diff --git a/lib/mapObjects/CGDwelling.h b/lib/mapObjects/CGDwelling.h index 08d592227..da85542d2 100644 --- a/lib/mapObjects/CGDwelling.h +++ b/lib/mapObjects/CGDwelling.h @@ -33,7 +33,7 @@ public: class DLL_LINKAGE CGDwelling : public CArmedInstance { public: - typedef std::vector > > TCreaturesSet; + using TCreaturesSet = std::vector > >; std::optional randomizationInfo; //random dwelling options; not serialized TCreaturesSet creatures; //creatures[level] -> diff --git a/lib/mapObjects/ObstacleSetHandler.cpp b/lib/mapObjects/ObstacleSetHandler.cpp index 17637ad01..71995979c 100644 --- a/lib/mapObjects/ObstacleSetHandler.cpp +++ b/lib/mapObjects/ObstacleSetHandler.cpp @@ -49,17 +49,17 @@ void ObstacleSet::removeEmptyTemplates() ObstacleSetFilter::ObstacleSetFilter(std::vector allowedTypes, TerrainId terrain = TerrainId::ANY_TERRAIN, FactionID faction = FactionID::ANY, EAlignment alignment = EAlignment::ANY): allowedTypes(allowedTypes), - terrain(terrain), faction(faction), - alignment(alignment) + alignment(alignment), + terrain(terrain) { } ObstacleSetFilter::ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain = TerrainId::ANY_TERRAIN, FactionID faction = FactionID::ANY, EAlignment alignment = EAlignment::ANY): allowedTypes({allowedType}), - terrain(terrain), faction(faction), - alignment(alignment) + alignment(alignment), + terrain(terrain) { } @@ -142,9 +142,9 @@ ObstacleSet::EObstacleType ObstacleSet::getType() const return type; } -void ObstacleSet::setType(EObstacleType type) +void ObstacleSet::setType(EObstacleType newType) { - this->type = type; + type = newType; } std::vector> ObstacleSet::getObstacles() const @@ -258,7 +258,7 @@ void ObstacleSetFilter::setType(ObstacleSet::EObstacleType type) allowedTypes = {type}; } -void ObstacleSetFilter::setTypes(std::vector types) +void ObstacleSetFilter::setTypes(const std::vector & types) { this->allowedTypes = types; } @@ -424,10 +424,9 @@ void ObstacleSetHandler::addObstacleSet(std::shared_ptr os) void ObstacleSetHandler::afterLoadFinalization() { - for (auto &os :biomes) - { + for(const auto & os : biomes) os->removeEmptyTemplates(); - } + vstd::erase_if(biomes, [](const std::shared_ptr &os) { if (os->getObstacles().empty()) @@ -439,10 +438,8 @@ void ObstacleSetHandler::afterLoadFinalization() }); // Populate map - for (auto &os : biomes) - { + for(const auto & os : biomes) obstacleSets[os->getType()].push_back(os); - } } TObstacleTypes ObstacleSetHandler::getObstacles( const ObstacleSetFilter &filter) const diff --git a/lib/mapObjects/ObstacleSetHandler.h b/lib/mapObjects/ObstacleSetHandler.h index de34f447a..0c6fd5183 100644 --- a/lib/mapObjects/ObstacleSetHandler.h +++ b/lib/mapObjects/ObstacleSetHandler.h @@ -70,7 +70,7 @@ private: std::vector> obstacles; }; -typedef std::vector> TObstacleTypes; +using TObstacleTypes = std::vector>; class DLL_LINKAGE ObstacleSetFilter { @@ -81,7 +81,7 @@ public: bool filter(const ObstacleSet &set) const; void setType(ObstacleSet::EObstacleType type); - void setTypes(std::vector types); + void setTypes(const std::vector & types); std::vector getAllowedTypes() const; TerrainId getTerrain() const; @@ -104,8 +104,8 @@ public: ~ObstacleSetHandler() = default; std::vector loadLegacyData() override; - virtual void loadObject(std::string scope, std::string name, const JsonNode & data) override; - virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override; + void loadObject(std::string scope, std::string name, const JsonNode & data) override; + void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override; std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index); ObstacleSet::EObstacleType convertObstacleClass(MapObjectID id); @@ -130,4 +130,4 @@ private: std::map>> obstacleSets; }; -VCMI_LIB_NAMESPACE_END \ No newline at end of file +VCMI_LIB_NAMESPACE_END diff --git a/lib/modding/ModIncompatibility.h b/lib/modding/ModIncompatibility.h index 0d0ab6581..e21ad8bff 100644 --- a/lib/modding/ModIncompatibility.h +++ b/lib/modding/ModIncompatibility.h @@ -24,7 +24,7 @@ public: messageMissingMods = _ss.str(); } - ModIncompatibility(const ModList & _missingMods, ModList & _excessiveMods) + ModIncompatibility(const ModList & _missingMods, const ModList & _excessiveMods) : ModIncompatibility(_missingMods) { std::ostringstream _ss;