From 94783fe2ed71a2580d83f81530a19340809614eb Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 28 Feb 2025 18:57:40 +0000 Subject: [PATCH] Codechange: Use EnumBitSet for ScriptConfigFlags. (#13669) --- src/ai/ai_info.cpp | 18 +++++++++--------- src/game/game_gui.cpp | 14 +++++++------- src/game/game_info.cpp | 10 +++++----- src/script/script_config.cpp | 8 ++++---- src/script/script_config.hpp | 15 ++++++++------- src/script/script_gui.cpp | 10 +++++----- src/script/script_info.cpp | 2 +- 7 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/ai/ai_info.cpp b/src/ai/ai_info.cpp index a892ce3a18..79d5ca96dd 100644 --- a/src/ai/ai_info.cpp +++ b/src/ai/ai_info.cpp @@ -37,17 +37,17 @@ template <> SQInteger PushClassName(HSQUIRRELVM vm) { sq SQAIInfo.AddConstructor(engine, "x"); SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting"); SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels"); - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE"); - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_RANDOM"); // Deprecated, mapped to NONE. - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN"); - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME"); - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER"); + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_NONE"); + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_RANDOM"); // Deprecated, mapped to NONE. + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "CONFIG_BOOLEAN"); + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "CONFIG_INGAME"); + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Developer}.base(), "CONFIG_DEVELOPER"); /* Pre 1.2 had an AI prefix */ - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_NONE"); - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_RANDOM"); // Deprecated, mapped to NONE. - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "AICONFIG_BOOLEAN"); - SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "AICONFIG_INGAME"); + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "AICONFIG_NONE"); + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "AICONFIG_RANDOM"); // Deprecated, mapped to NONE. + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "AICONFIG_BOOLEAN"); + SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "AICONFIG_INGAME"); SQAIInfo.PostRegister(engine); engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx"); diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index bc9a298dc3..b021ee1665 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -124,7 +124,7 @@ struct GSConfigWindow : public Window { visible_settings.clear(); for (const auto &item : *this->gs_config->GetConfigList()) { - bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0; + bool no_hide = !item.flags.Test(ScriptConfigFlag::Developer); if (no_hide || _settings_client.gui.ai_developer_tools) { visible_settings.push_back(&item); } @@ -193,7 +193,7 @@ struct GSConfigWindow : public Window { int current_value = this->gs_config->GetSetting(config_item.name); bool editable = this->IsEditableItem(config_item); - if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) { + if (config_item.flags.Test(ScriptConfigFlag::Boolean)) { DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); } else { int i = static_cast(std::distance(std::begin(this->visible_settings), it)); @@ -264,7 +264,7 @@ struct GSConfigWindow : public Window { this->clicked_dropdown = false; } - bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0; + bool bool_item = config_item.flags.Test(ScriptConfigFlag::Boolean); Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero); int x = pt.x - r.left; @@ -404,15 +404,15 @@ private: bool IsEditableItem(const ScriptConfigItem &config_item) const { return _game_mode == GM_MENU - || _game_mode == GM_EDITOR - || (config_item.flags & SCRIPTCONFIG_INGAME) != 0 - || _settings_client.gui.ai_developer_tools; + || _game_mode == GM_EDITOR + || config_item.flags.Test(ScriptConfigFlag::InGame) + || _settings_client.gui.ai_developer_tools; } void SetValue(int value) { const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row]; - if (_game_mode == GM_NORMAL && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return; + if (_game_mode == GM_NORMAL && !config_item.flags.Test(ScriptConfigFlag::InGame)) return; this->gs_config->SetSetting(config_item.name, value); this->SetDirty(); } diff --git a/src/game/game_info.cpp b/src/game/game_info.cpp index 792e3a075e..1045f01d82 100644 --- a/src/game/game_info.cpp +++ b/src/game/game_info.cpp @@ -35,11 +35,11 @@ template <> SQInteger PushClassName(HSQUIRRELVM vm) { SQGSInfo.AddConstructor(engine, "x"); SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting"); SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels"); - SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE"); - SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_RANDOM"); // Deprecated, mapped to NONE. - SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN"); - SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME"); - SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER"); + SQGSInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_NONE"); + SQGSInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_RANDOM"); // Deprecated, mapped to NONE. + SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "CONFIG_BOOLEAN"); + SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "CONFIG_INGAME"); + SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Developer}.base(), "CONFIG_DEVELOPER"); SQGSInfo.PostRegister(engine); engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx"); diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index 2a4b7815cf..330c19da16 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -73,7 +73,7 @@ void ScriptConfig::ClearConfigList() void ScriptConfig::AnchorUnchangeableSettings() { for (const auto &item : *this->GetConfigList()) { - if ((item.flags & SCRIPTCONFIG_INGAME) == 0) { + if (!item.flags.Test(ScriptConfigFlag::InGame)) { this->SetSetting(item.name, this->GetSetting(item.name)); } } @@ -112,8 +112,8 @@ void ScriptConfig::ResetEditableSettings(bool yet_to_start) const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first); assert(config_item != nullptr); - bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0; - bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0; + bool editable = yet_to_start || config_item->flags.Test(ScriptConfigFlag::InGame); + bool visible = _settings_client.gui.ai_developer_tools || !config_item->flags.Test(ScriptConfigFlag::Developer); if (editable && visible) { it = this->settings.erase(it); @@ -193,7 +193,7 @@ ScriptInstance::ScriptData *ScriptConfig::GetToLoadData() static std::pair GetValueParams(const ScriptConfigItem &config_item, int value) { - if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) return {value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF, {}}; + if (config_item.flags.Test(ScriptConfigFlag::Boolean)) return {value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF, {}}; auto it = config_item.labels.find(value); if (it != std::end(config_item.labels)) return {STR_JUST_RAW_STRING, it->second}; diff --git a/src/script/script_config.hpp b/src/script/script_config.hpp index 8ac73a8d90..83daa7b7cb 100644 --- a/src/script/script_config.hpp +++ b/src/script/script_config.hpp @@ -17,15 +17,16 @@ /** Maximum of 10 digits for MIN / MAX_INT32, 1 for the sign and 1 for '\0'. */ static const int INT32_DIGITS_WITH_SIGN_AND_TERMINATION = 10 + 1 + 1; -/** Bitmask of flags for Script settings. */ -enum ScriptConfigFlags : uint8_t { - SCRIPTCONFIG_NONE = 0x0, ///< No flags set. +/** Flags for Script settings. */ +enum class ScriptConfigFlag : uint8_t { // Unused flag 0x1. - SCRIPTCONFIG_BOOLEAN = 0x2, ///< This value is a boolean (either 0 (false) or 1 (true) ). - SCRIPTCONFIG_INGAME = 0x4, ///< This setting can be changed while the Script is running. - SCRIPTCONFIG_DEVELOPER = 0x8, ///< This setting will only be visible when the Script development tools are active. + Boolean = 1, ///< This value is a boolean (either 0 (false) or 1 (true) ). + InGame = 2, ///< This setting can be changed while the Script is running. + Developer = 3, ///< This setting will only be visible when the Script development tools are active. }; +using ScriptConfigFlags = EnumBitSet; + typedef std::map LabelMapping; ///< Map-type used to map the setting numbers to labels. /** Info about a single Script setting. */ @@ -36,7 +37,7 @@ struct ScriptConfigItem { int max_value = 1; ///< The maximal value this configuration setting can have. int default_value = 0; ///< The default value of this configuration setting. int step_size = 1; ///< The step size in the gui. - ScriptConfigFlags flags = SCRIPTCONFIG_NONE; ///< Flags for the configuration setting. + ScriptConfigFlags flags{}; ///< Flags for the configuration setting. LabelMapping labels; ///< Text labels for the integer values. bool complete_labels = false; ///< True if all values have a label. diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 08e7909720..953af781d8 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -314,7 +314,7 @@ struct ScriptSettingsWindow : public Window { visible_settings.clear(); for (const auto &item : *this->script_config->GetConfigList()) { - bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0; + bool no_hide = !item.flags.Test(ScriptConfigFlag::Developer); if (no_hide || _settings_client.gui.ai_developer_tools) { visible_settings.push_back(&item); } @@ -360,7 +360,7 @@ struct ScriptSettingsWindow : public Window { int current_value = this->script_config->GetSetting(config_item.name); bool editable = this->IsEditableItem(config_item); - if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) { + if (config_item.flags.Test(ScriptConfigFlag::Boolean)) { DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); } else { int i = static_cast(std::distance(std::begin(this->visible_settings), it)); @@ -403,7 +403,7 @@ struct ScriptSettingsWindow : public Window { this->clicked_dropdown = false; } - bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0; + bool bool_item = config_item.flags.Test(ScriptConfigFlag::Boolean); Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero); int x = pt.x - r.left; @@ -537,14 +537,14 @@ private: return _game_mode == GM_MENU || _game_mode == GM_EDITOR || ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot)) - || (config_item.flags & SCRIPTCONFIG_INGAME) != 0 + || config_item.flags.Test(ScriptConfigFlag::InGame) || _settings_client.gui.ai_developer_tools; } void SetValue(int value) { const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row]; - if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return; + if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && !config_item.flags.Test(ScriptConfigFlag::InGame)) return; this->script_config->SetSetting(config_item.name, value); this->SetDirty(); } diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp index e086495781..ffb74bea5f 100644 --- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -177,7 +177,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) } /* Make sure all properties are defined */ - uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF; + uint mask = config.flags.Test(ScriptConfigFlag::Boolean) ? 0x1F3 : 0x1FF; if (items != mask) { this->engine->ThrowError("please define all properties of a setting (min/max not allowed for booleans)"); return SQ_ERROR;