From 4c70abbeb5b6e2008addbe2596097930be37ad04 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Mon, 12 Feb 2024 13:49:45 +0200 Subject: [PATCH] Reduced usage of global variables - removed or made const / constexpr --- AI/BattleAI/AttackPossibility.h | 1 - AI/BattleAI/BattleAI.cpp | 2 -- AI/BattleAI/CMakeLists.txt | 2 -- AI/BattleAI/common.cpp | 23 ------------ AI/BattleAI/common.h | 26 -------------- AI/BattleAI/main.cpp | 2 +- AI/EmptyAI/main.cpp | 1 - .../Analyzers/DangerHitMapAnalyzer.cpp | 4 +-- .../Analyzers/DangerHitMapAnalyzer.h | 2 +- AI/Nullkiller/Analyzers/HeroManager.cpp | 6 ++-- AI/Nullkiller/Analyzers/HeroManager.h | 6 ++-- AI/Nullkiller/Pathfinding/AINodeStorage.cpp | 2 +- AI/Nullkiller/Pathfinding/Actors.cpp | 2 +- AI/Nullkiller/main.cpp | 2 +- AI/StupidAI/main.cpp | 2 +- AI/VCAI/BuildingManager.cpp | 2 +- AI/VCAI/main.cpp | 2 +- client/CMT.cpp | 3 +- client/CMusicHandler.cpp | 2 +- client/windows/CCastleInterface.cpp | 36 ++++++++----------- client/windows/CSpellWindow.cpp | 4 ++- lib/rmg/Zone.cpp | 2 +- lib/rmg/Zone.h | 2 +- scripting/lua/LuaScriptModule.cpp | 2 +- 24 files changed, 39 insertions(+), 99 deletions(-) delete mode 100644 AI/BattleAI/common.cpp delete mode 100644 AI/BattleAI/common.h diff --git a/AI/BattleAI/AttackPossibility.h b/AI/BattleAI/AttackPossibility.h index 2181d883a..b8ff77218 100644 --- a/AI/BattleAI/AttackPossibility.h +++ b/AI/BattleAI/AttackPossibility.h @@ -10,7 +10,6 @@ #pragma once #include "../../lib/battle/CUnitState.h" #include "../../CCallback.h" -#include "common.h" #include "StackWithBonuses.h" #define BATTLE_TRACE_LEVEL 0 diff --git a/AI/BattleAI/BattleAI.cpp b/AI/BattleAI/BattleAI.cpp index 631c163a0..37ba4f270 100644 --- a/AI/BattleAI/BattleAI.cpp +++ b/AI/BattleAI/BattleAI.cpp @@ -49,7 +49,6 @@ CBattleAI::~CBattleAI() void CBattleAI::initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB) { - setCbc(CB); env = ENV; cb = CB; playerID = *CB->getPlayerID(); @@ -121,7 +120,6 @@ void CBattleAI::activeStack(const BattleID & battleID, const CStack * stack ) }; BattleAction result = BattleAction::makeDefend(stack); - setCbc(cb); //TODO: make solid sure that AIs always use their callbacks (need to take care of event handlers too) auto start = std::chrono::high_resolution_clock::now(); diff --git a/AI/BattleAI/CMakeLists.txt b/AI/BattleAI/CMakeLists.txt index 335c92f5c..e51fa3072 100644 --- a/AI/BattleAI/CMakeLists.txt +++ b/AI/BattleAI/CMakeLists.txt @@ -2,7 +2,6 @@ set(battleAI_SRCS AttackPossibility.cpp BattleAI.cpp BattleEvaluator.cpp - common.cpp EnemyInfo.cpp PossibleSpellcast.cpp PotentialTargets.cpp @@ -17,7 +16,6 @@ set(battleAI_HEADERS AttackPossibility.h BattleAI.h BattleEvaluator.h - common.h EnemyInfo.h PotentialTargets.h PossibleSpellcast.h diff --git a/AI/BattleAI/common.cpp b/AI/BattleAI/common.cpp deleted file mode 100644 index 4a467c8cc..000000000 --- a/AI/BattleAI/common.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * common.cpp, part of VCMI engine - * - * Authors: listed in file AUTHORS in main folder - * - * License: GNU General Public License v2.0 or later - * Full text of license available in license.txt file, in main folder - * - */ -#include "StdInc.h" -#include "common.h" - -std::shared_ptr cbc; - -void setCbc(std::shared_ptr cb) -{ - cbc = cb; -} - -std::shared_ptr getCbc() -{ - return cbc; -} diff --git a/AI/BattleAI/common.h b/AI/BattleAI/common.h deleted file mode 100644 index dfc9b4623..000000000 --- a/AI/BattleAI/common.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * common.h, part of VCMI engine - * - * Authors: listed in file AUTHORS in main folder - * - * License: GNU General Public License v2.0 or later - * Full text of license available in license.txt file, in main folder - * - */ -#pragma once - -class CBattleCallback; - -template -const Val getValOr(const std::map &Map, const Key &key, const Val2 defaultValue) -{ - //returning references here won't work: defaultValue must be converted into Val, creating temporary - auto i = Map.find(key); - if(i != Map.end()) - return i->second; - else - return defaultValue; -} - -void setCbc(std::shared_ptr cb); -std::shared_ptr getCbc(); diff --git a/AI/BattleAI/main.cpp b/AI/BattleAI/main.cpp index 101491d93..7e8b93c05 100644 --- a/AI/BattleAI/main.cpp +++ b/AI/BattleAI/main.cpp @@ -15,7 +15,7 @@ #define strcpy_s(a, b, c) strncpy(a, c, b) #endif -static const char *g_cszAiName = "Battle AI"; +static const char * const g_cszAiName = "Battle AI"; extern "C" DLL_EXPORT int GetGlobalAiVersion() { diff --git a/AI/EmptyAI/main.cpp b/AI/EmptyAI/main.cpp index e6ae9483c..e67974b9e 100644 --- a/AI/EmptyAI/main.cpp +++ b/AI/EmptyAI/main.cpp @@ -11,7 +11,6 @@ #include "CEmptyAI.h" -std::set ais; extern "C" DLL_EXPORT int GetGlobalAiVersion() { return AI_INTERFACE_VER; diff --git a/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp b/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp index ce74645a1..ccf6b7ecb 100644 --- a/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp +++ b/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp @@ -16,7 +16,7 @@ namespace NKAI { -HitMapInfo HitMapInfo::NoThreat; +const HitMapInfo HitMapInfo::NoThreat; double HitMapInfo::value() const { @@ -285,8 +285,6 @@ const HitMapNode & DangerHitMapAnalyzer::getTileThreat(const int3 & tile) const return hitMap[tile.x][tile.y][tile.z]; } -const std::set empty = {}; - std::set DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const { std::set result; diff --git a/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.h b/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.h index 45538c99b..fc2890846 100644 --- a/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.h +++ b/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.h @@ -18,7 +18,7 @@ struct AIPath; struct HitMapInfo { - static HitMapInfo NoThreat; + static const HitMapInfo NoThreat; uint64_t danger; uint8_t turn; diff --git a/AI/Nullkiller/Analyzers/HeroManager.cpp b/AI/Nullkiller/Analyzers/HeroManager.cpp index 4131182cb..42a3ae29f 100644 --- a/AI/Nullkiller/Analyzers/HeroManager.cpp +++ b/AI/Nullkiller/Analyzers/HeroManager.cpp @@ -17,7 +17,7 @@ namespace NKAI { -SecondarySkillEvaluator HeroManager::wariorSkillsScores = SecondarySkillEvaluator( +const SecondarySkillEvaluator HeroManager::wariorSkillsScores = SecondarySkillEvaluator( { std::make_shared( std::map @@ -46,7 +46,7 @@ SecondarySkillEvaluator HeroManager::wariorSkillsScores = SecondarySkillEvaluato std::make_shared() }); -SecondarySkillEvaluator HeroManager::scountSkillsScores = SecondarySkillEvaluator( +const SecondarySkillEvaluator HeroManager::scountSkillsScores = SecondarySkillEvaluator( { std::make_shared( std::map @@ -332,7 +332,7 @@ void WisdomRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill score += 1.5; } -std::vector AtLeastOneMagicRule::magicSchools = { +const std::vector AtLeastOneMagicRule::magicSchools = { SecondarySkill::AIR_MAGIC, SecondarySkill::EARTH_MAGIC, SecondarySkill::FIRE_MAGIC, diff --git a/AI/Nullkiller/Analyzers/HeroManager.h b/AI/Nullkiller/Analyzers/HeroManager.h index a7744ad1f..fa44198b7 100644 --- a/AI/Nullkiller/Analyzers/HeroManager.h +++ b/AI/Nullkiller/Analyzers/HeroManager.h @@ -58,8 +58,8 @@ public: class DLL_EXPORT HeroManager : public IHeroManager { private: - static SecondarySkillEvaluator wariorSkillsScores; - static SecondarySkillEvaluator scountSkillsScores; + static const SecondarySkillEvaluator wariorSkillsScores; + static const SecondarySkillEvaluator scountSkillsScores; CCallback * cb; //this is enough, but we downcast from CCallback const Nullkiller * ai; @@ -114,7 +114,7 @@ public: class AtLeastOneMagicRule : public ISecondarySkillRule { private: - static std::vector magicSchools; + static const std::vector magicSchools; public: void evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const override; diff --git a/AI/Nullkiller/Pathfinding/AINodeStorage.cpp b/AI/Nullkiller/Pathfinding/AINodeStorage.cpp index ffd529370..863549dd7 100644 --- a/AI/Nullkiller/Pathfinding/AINodeStorage.cpp +++ b/AI/Nullkiller/Pathfinding/AINodeStorage.cpp @@ -332,7 +332,7 @@ std::vector AINodeStorage::calculateNeighbours( return neighbours; } -EPathfindingLayer phisycalLayers[2] = {EPathfindingLayer::LAND, EPathfindingLayer::SAIL}; +constexpr std::array phisycalLayers = {EPathfindingLayer::LAND, EPathfindingLayer::SAIL}; bool AINodeStorage::increaseHeroChainTurnLimit() { diff --git a/AI/Nullkiller/Pathfinding/Actors.cpp b/AI/Nullkiller/Pathfinding/Actors.cpp index 900d65506..71259fa05 100644 --- a/AI/Nullkiller/Pathfinding/Actors.cpp +++ b/AI/Nullkiller/Pathfinding/Actors.cpp @@ -18,7 +18,7 @@ using namespace NKAI; -CCreatureSet emptyArmy; +const CCreatureSet emptyArmy; bool HeroExchangeArmy::needsLastStack() const { diff --git a/AI/Nullkiller/main.cpp b/AI/Nullkiller/main.cpp index fff944c8d..695d000b3 100644 --- a/AI/Nullkiller/main.cpp +++ b/AI/Nullkiller/main.cpp @@ -14,7 +14,7 @@ #define strcpy_s(a, b, c) strncpy(a, c, b) #endif -static const char * g_cszAiName = "Nullkiller"; +static const char * const g_cszAiName = "Nullkiller"; extern "C" DLL_EXPORT int GetGlobalAiVersion() { diff --git a/AI/StupidAI/main.cpp b/AI/StupidAI/main.cpp index 93f2ff517..5cf31e497 100644 --- a/AI/StupidAI/main.cpp +++ b/AI/StupidAI/main.cpp @@ -16,7 +16,7 @@ #define strcpy_s(a, b, c) strncpy(a, c, b) #endif -static const char *g_cszAiName = "Stupid AI 0.1"; +static const char * const g_cszAiName = "Stupid AI 0.1"; extern "C" DLL_EXPORT int GetGlobalAiVersion() { diff --git a/AI/VCAI/BuildingManager.cpp b/AI/VCAI/BuildingManager.cpp index 843c54d3f..f791a594f 100644 --- a/AI/VCAI/BuildingManager.cpp +++ b/AI/VCAI/BuildingManager.cpp @@ -148,7 +148,7 @@ BuildingID::DWELL_LVL_4_UP, BuildingID::DWELL_LVL_5_UP, BuildingID::DWELL_LVL_6_ static const std::vector unitGrowth = { BuildingID::HORDE_1, BuildingID::HORDE_1_UPGR, BuildingID::HORDE_2, BuildingID::HORDE_2_UPGR }; static const std::vector _spells = { BuildingID::MAGES_GUILD_1, BuildingID::MAGES_GUILD_2, BuildingID::MAGES_GUILD_3, BuildingID::MAGES_GUILD_4, BuildingID::MAGES_GUILD_5 }; -static const std::vector extra = { BuildingID::MARKETPLACE, BuildingID::BLACKSMITH, BuildingID::RESOURCE_SILO, BuildingID::SPECIAL_1, BuildingID::SPECIAL_2, +static const std::vector extra = { BuildingID::MARKETPLACE, BuildingID::BLACKSMITH, BuildingID::RESOURCE_SILO, BuildingID::SPECIAL_1, BuildingID::SPECIAL_2, BuildingID::SPECIAL_3, BuildingID::SPECIAL_4, BuildingID::SHIPYARD }; // all remaining buildings bool BuildingManager::getBuildingOptions(const CGTownInstance * t) diff --git a/AI/VCAI/main.cpp b/AI/VCAI/main.cpp index 3a88b2716..1e262024e 100644 --- a/AI/VCAI/main.cpp +++ b/AI/VCAI/main.cpp @@ -14,7 +14,7 @@ #define strcpy_s(a, b, c) strncpy(a, c, b) #endif -static const char * g_cszAiName = "VCAI"; +static const char * const g_cszAiName = "VCAI"; extern "C" DLL_EXPORT int GetGlobalAiVersion() { diff --git a/client/CMT.cpp b/client/CMT.cpp index 5305a273e..956851095 100644 --- a/client/CMT.cpp +++ b/client/CMT.cpp @@ -56,7 +56,6 @@ namespace po = boost::program_options; namespace po_style = boost::program_options::command_line_style; static std::atomic quitRequestedDuringOpeningPlayback = false; -static po::variables_map vm; #ifndef VCMI_IOS void processCommand(const std::string &message); @@ -118,6 +117,8 @@ int main(int argc, char * argv[]) #endif std::cout << "Starting... " << std::endl; po::options_description opts("Allowed options"); + po::variables_map vm; + opts.add_options() ("help,h", "display help and exit") ("version,v", "display version information and exit") diff --git a/client/CMusicHandler.cpp b/client/CMusicHandler.cpp index b3089375a..1339a4de9 100644 --- a/client/CMusicHandler.cpp +++ b/client/CMusicHandler.cpp @@ -30,7 +30,7 @@ #define VCMI_SOUND_FILE(y) #y, // sounds mapped to soundBase enum -static std::string sounds[] = { +static const std::string sounds[] = { "", // invalid "", // todo VCMI_SOUND_LIST diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 2f398a116..16fdd2365 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -562,27 +562,6 @@ void HeroSlots::swapArmies() LOCPLINT->cb->swapGarrisonHero(town); } -class SORTHELP -{ -public: - bool operator() (const CIntObject * a, const CIntObject * b) - { - auto b1 = dynamic_cast(a); - auto b2 = dynamic_cast(b); - - if(!b1 && !b2) - return intptr_t(a) < intptr_t(b); - if(b1 && !b2) - return false; - if(!b1 && b2) - return true; - - return (*b1)<(*b2); - } -}; - -SORTHELP buildSorter; - CCastleBuildings::CCastleBuildings(const CGTownInstance* Town): town(Town), selectedBuilding(nullptr) @@ -650,6 +629,21 @@ void CCastleBuildings::recreate() buildings.push_back(std::make_shared(this, town, toAdd)); } + auto const & buildSorter = [] (const CIntObject * a, const CIntObject * b) + { + auto b1 = dynamic_cast(a); + auto b2 = dynamic_cast(b); + + if(!b1 && !b2) + return intptr_t(a) < intptr_t(b); + if(b1 && !b2) + return false; + if(!b1 && b2) + return true; + + return (*b1)<(*b2); + }; + boost::sort(children, buildSorter); //TODO: create building in blit order } diff --git a/client/windows/CSpellWindow.cpp b/client/windows/CSpellWindow.cpp index 9c41f673e..2ecc9acf4 100644 --- a/client/windows/CSpellWindow.cpp +++ b/client/windows/CSpellWindow.cpp @@ -94,7 +94,7 @@ public: return A->getNameTranslated() < B->getNameTranslated(); } -} spellsorter; +}; CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells): CWindowObject(PLAYER_COLORED | (settings["gameTweaks"]["enableLargeSpellbook"].Bool() ? BORDERED : 0)), @@ -293,6 +293,8 @@ void CSpellWindow::processSpells() if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell) && searchTextFound) mySpells.push_back(spell); } + + SpellbookSpellSorter spellsorter; std::sort(mySpells.begin(), mySpells.end(), spellsorter); //initializing sizes of spellbook's parts diff --git a/lib/rmg/Zone.cpp b/lib/rmg/Zone.cpp index 09f563727..dad4f22d7 100644 --- a/lib/rmg/Zone.cpp +++ b/lib/rmg/Zone.cpp @@ -19,7 +19,7 @@ VCMI_LIB_NAMESPACE_BEGIN -std::function AREA_NO_FILTER = [](const int3 & t) +const std::function AREA_NO_FILTER = [](const int3 & t) { return true; }; diff --git a/lib/rmg/Zone.h b/lib/rmg/Zone.h index cce97f2e4..0d2376ad8 100644 --- a/lib/rmg/Zone.h +++ b/lib/rmg/Zone.h @@ -30,7 +30,7 @@ class CMapGenerator; class Modificator; class CRandomGenerator; -extern std::function AREA_NO_FILTER; +extern const std::function AREA_NO_FILTER; typedef std::list> TModificators; diff --git a/scripting/lua/LuaScriptModule.cpp b/scripting/lua/LuaScriptModule.cpp index fb3ef8a76..99ac829e4 100644 --- a/scripting/lua/LuaScriptModule.cpp +++ b/scripting/lua/LuaScriptModule.cpp @@ -17,7 +17,7 @@ #define strcpy_s(a, b, c) strncpy(a, c, b) #endif -static const char *g_cszAiName = "Lua interpreter"; +static const char * const g_cszAiName = "Lua interpreter"; VCMI_LIB_NAMESPACE_BEGIN