From 1b8d1d66cf8d25e49e96574224c0bff068a86228 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 22 Mar 2022 09:32:55 -0400 Subject: [PATCH 1/5] Cleaned up a bunch more std::array uses --- src/display.cpp | 14 +-------- src/display.hpp | 12 +++++++- src/font/font_config.cpp | 2 +- src/game_initialization/connect_engine.cpp | 4 +-- src/generators/lua_map_generator.cpp | 2 +- src/replay.cpp | 2 +- src/scripting/game_lua_kernel.cpp | 4 +-- src/tests/test_serialization.cpp | 36 +++++++++++----------- 8 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 8aa58320f9d..88f87ad6d92 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -962,7 +962,7 @@ gui::button::TYPE display::string_to_button_type(const std::string& type) static const std::string& get_direction(std::size_t n) { - static const std::array dirs {{ "-n", "-ne", "-se", "-s", "-sw", "-nw" }}; + static const std::array dirs { "-n", "-ne", "-se", "-s", "-sw", "-nw" }; return dirs[n >= dirs.size() ? 0 : n]; } @@ -1222,18 +1222,6 @@ void display::drawing_buffer_add(const drawing_layer layer, drawing_buffer_.emplace_back(layer, loc, x, y, surf, clip); } -// FIXME: temporary method. Group splitting should be made -// public into the definition of drawing_layer -// -// The drawing is done per layer_group, the range per group is [low, high). -const std::array display::drawing_buffer_key::layer_groups {{ - LAYER_TERRAIN_BG, - LAYER_UNIT_FIRST, - LAYER_UNIT_MOVE_DEFAULT, - // Make sure the movement doesn't show above fog and reachmap. - LAYER_REACHMAP -}}; - enum { // you may adjust the following when needed: diff --git a/src/display.hpp b/src/display.hpp index fd3ecce80fe..64c45d6e50d 100644 --- a/src/display.hpp +++ b/src/display.hpp @@ -926,7 +926,17 @@ protected: private: unsigned int key_; - static const std::array layer_groups; + // FIXME: temporary method. Group splitting should be made + // public into the definition of drawing_layer + // + // The drawing is done per layer_group, the range per group is [low, high). + static inline const std::array layer_groups { + LAYER_TERRAIN_BG, + LAYER_UNIT_FIRST, + LAYER_UNIT_MOVE_DEFAULT, + // Make sure the movement doesn't show above fog and reachmap. + LAYER_REACHMAP + }; public: drawing_buffer_key(const map_location &loc, drawing_layer layer); diff --git a/src/font/font_config.cpp b/src/font/font_config.cpp index 9ae63070f2c..1441ab54279 100644 --- a/src/font/font_config.cpp +++ b/src/font/font_config.cpp @@ -87,7 +87,7 @@ namespace #ifdef CAIRO_HAS_WIN32_FONT bool is_valid_font_file(const std::string& file) { - static const std::array font_exts { ".ttf", ".ttc", ".otf" }; + static const std::array font_exts { ".ttf", ".ttc", ".otf" }; for(const std::string& ext : font_exts) { if(filesystem::ends_with(file, ext)) { diff --git a/src/game_initialization/connect_engine.cpp b/src/game_initialization/connect_engine.cpp index bdaa46968ad..c2e1c1ddbe4 100644 --- a/src/game_initialization/connect_engine.cpp +++ b/src/game_initialization/connect_engine.cpp @@ -50,13 +50,13 @@ static lg::log_domain log_network("network"); namespace { -const std::array controller_names {{ +const std::array controller_names { side_controller::human, side_controller::human, side_controller::ai, side_controller::none, side_controller::reserved -}}; +}; const std::set children_to_swap { "village", diff --git a/src/generators/lua_map_generator.cpp b/src/generators/lua_map_generator.cpp index bef68d038df..07299a6c4ce 100644 --- a/src/generators/lua_map_generator.cpp +++ b/src/generators/lua_map_generator.cpp @@ -37,7 +37,7 @@ lua_map_generator::lua_map_generator(const config & cfg, const config* vars) , generator_data_(cfg) { lk_.load_core(); - const std::array required {{"id", "config_name", "create_map"}}; + const std::array required {"id", "config_name", "create_map"}; for(const std::string& req : required) { if (!cfg.has_attribute(req)) { if(req == "create_map" && cfg.has_attribute("create_scenario")) { diff --git a/src/replay.cpp b/src/replay.cpp index 63178adc381..f3aa44e2bc7 100644 --- a/src/replay.cpp +++ b/src/replay.cpp @@ -106,7 +106,7 @@ static void verify(const unit_map& units, const config& cfg) { u->write(u_cfg); bool is_ok = true; - static const std::array fields {{"type","hitpoints","experience","side"}}; + static const std::array fields {"type","hitpoints","experience","side"}; for(const std::string& field : fields) { if (u_cfg[field] != un[field]) { errbuf << "ERROR IN FIELD '" << field << "' for unit at " diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 7565f69d740..a3ea7145d1b 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -4726,7 +4726,7 @@ void game_lua_kernel::set_game_display(game_display * gd) { * elsewhere (in the C++ code). * Any child tags not in this list will be passed to Lua's on_load event. */ -static const std::array handled_file_tags {{ +static const std::array handled_file_tags { "color_palette", "color_range", "display", @@ -4751,7 +4751,7 @@ static const std::array handled_file_tags {{ "tunnel", "undo_stack", "variables" -}}; +}; static bool is_handled_file_tag(const std::string& s) { diff --git a/src/tests/test_serialization.cpp b/src/tests/test_serialization.cpp index fbb5ae780d8..4fd24687704 100644 --- a/src/tests/test_serialization.cpp +++ b/src/tests/test_serialization.cpp @@ -57,52 +57,52 @@ BOOST_AUTO_TEST_CASE( utils_split_test ) { auto split = utils::split(test_string); - std::array expect = {"a", "bb", "ccc || d", "ee", "fff | | g", "hh", "iii"}; + std::array expect = {"a", "bb", "ccc || d", "ee", "fff | | g", "hh", "iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, ',', utils::REMOVE_EMPTY | utils::STRIP_SPACES); - std::array expect = {"a", "bb", "ccc || d", "ee", "fff | | g", "hh", "iii"}; + std::array expect = {"a", "bb", "ccc || d", "ee", "fff | | g", "hh", "iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, ',', utils::REMOVE_EMPTY); - std::array expect = {"a", " ", " bb", " ccc || d", " ee", " fff | | g", " ", " hh", " iii"}; + std::array expect = {"a", " ", " bb", " ccc || d", " ee", " fff | | g", " ", " hh", " iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, ',', utils::STRIP_SPACES); - std::array expect = {"a", "", "bb", "ccc || d", "ee", "", "fff | | g", "", "hh", "iii"}; + std::array expect = {"a", "", "bb", "ccc || d", "ee", "", "fff | | g", "", "hh", "iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, ',', 0); - std::array expect = {"a", " ", " bb", " ccc || d", " ee", "", " fff | | g", " ", " hh", " iii"}; + std::array expect = {"a", " ", " bb", " ccc || d", " ee", "", " fff | | g", " ", " hh", " iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, '|'); - std::array expect = {"a, , bb, ccc", "d, ee,, fff", "g, , hh, iii"}; + std::array expect = {"a, , bb, ccc", "d, ee,, fff", "g, , hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, '|', utils::REMOVE_EMPTY | utils::STRIP_SPACES); - std::array expect = {"a, , bb, ccc", "d, ee,, fff", "g, , hh, iii"}; + std::array expect = {"a, , bb, ccc", "d, ee,, fff", "g, , hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, '|', utils::REMOVE_EMPTY); - std::array expect = {"a, , bb, ccc ", " d, ee,, fff ", " ", " g, , hh, iii"}; + std::array expect = {"a, , bb, ccc ", " d, ee,, fff ", " ", " g, , hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, '|', utils::STRIP_SPACES); - std::array expect = {"a, , bb, ccc", "", "d, ee,, fff", "", "g, , hh, iii"}; + std::array expect = {"a, , bb, ccc", "", "d, ee,, fff", "", "g, , hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::split(test_string, '|', 0); - std::array expect = {"a, , bb, ccc ", "", " d, ee,, fff ", " ", " g, , hh, iii"}; + std::array expect = {"a, , bb, ccc ", "", " d, ee,, fff ", " ", " g, , hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } } @@ -113,42 +113,42 @@ BOOST_AUTO_TEST_CASE( utils_quoted_split_test ) { auto split = utils::quoted_split(test_string, ',', utils::REMOVE_EMPTY | utils::STRIP_SPACES, '`'); - std::array expect = {"a", "`, bb", "ccc || d", "ee", "fff | `| g", "`, hh", "iii"}; + std::array expect = {"a", "`, bb", "ccc || d", "ee", "fff | `| g", "`, hh", "iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::quoted_split(test_string, ',', utils::REMOVE_EMPTY, '`'); - std::array expect = {"a", " `, bb", " ccc || d", " ee", " fff | `| g", " `, hh", " iii"}; + std::array expect = {"a", " `, bb", " ccc || d", " ee", " fff | `| g", " `, hh", " iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::quoted_split(test_string, ',', utils::STRIP_SPACES, '`'); - std::array expect = {"a", "`, bb", "ccc || d", "ee", "", "fff | `| g", "`, hh", "iii"}; + std::array expect = {"a", "`, bb", "ccc || d", "ee", "", "fff | `| g", "`, hh", "iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::quoted_split(test_string, ',', 0, '`'); - std::array expect = {"a", " `, bb", " ccc || d", " ee", "", " fff | `| g", " `, hh", " iii"}; + std::array expect = {"a", " `, bb", " ccc || d", " ee", "", " fff | `| g", " `, hh", " iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::quoted_split(test_string, '|', utils::REMOVE_EMPTY | utils::STRIP_SPACES, '`'); - std::array expect = {"a, `, bb, ccc", "d, ee,, fff", "`| g, `, hh, iii"}; + std::array expect = {"a, `, bb, ccc", "d, ee,, fff", "`| g, `, hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::quoted_split(test_string, '|', utils::REMOVE_EMPTY, '`'); - std::array expect = {"a, `, bb, ccc ", " d, ee,, fff ", " `| g, `, hh, iii"}; + std::array expect = {"a, `, bb, ccc ", " d, ee,, fff ", " `| g, `, hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::quoted_split(test_string, '|', utils::STRIP_SPACES, '`'); - std::array expect = {"a, `, bb, ccc", "", "d, ee,, fff", "`| g, `, hh, iii"}; + std::array expect = {"a, `, bb, ccc", "", "d, ee,, fff", "`| g, `, hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } { auto split = utils::quoted_split(test_string, '|', 0, '`'); - std::array expect = {"a, `, bb, ccc ", "", " d, ee,, fff ", " `| g, `, hh, iii"}; + std::array expect = {"a, `, bb, ccc ", "", " d, ee,, fff ", " `| g, `, hh, iii"}; BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end()); } } From b1ea4a53fd2007ed1a9c9ffea1e8b51ef5926379 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 22 Mar 2022 09:35:17 -0400 Subject: [PATCH 2/5] Connect Engine: removed unused function argument from update_and_send_diff --- src/game_initialization/connect_engine.cpp | 6 +++--- src/game_initialization/connect_engine.hpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/game_initialization/connect_engine.cpp b/src/game_initialization/connect_engine.cpp index c2e1c1ddbe4..53a1858b0ce 100644 --- a/src/game_initialization/connect_engine.cpp +++ b/src/game_initialization/connect_engine.cpp @@ -317,7 +317,7 @@ void connect_engine::update_level() } } -void connect_engine::update_and_send_diff(bool /*update_time_of_day*/) +void connect_engine::update_and_send_diff() { config old_level = level_; update_level(); @@ -457,7 +457,7 @@ void connect_engine::start_game() config lock("stop_updates"); mp::send_to_server(lock); - update_and_send_diff(true); + update_and_send_diff(); save_reserved_sides_information(); @@ -523,7 +523,7 @@ void connect_engine::start_game_commandline(const commandline_options& cmdline_o side->resolve_random(rng); } // end top-level loop - update_and_send_diff(true); + update_and_send_diff(); // Update sides with commandline parameters. if(cmdline_opts.multiplayer_turns) { diff --git a/src/game_initialization/connect_engine.hpp b/src/game_initialization/connect_engine.hpp index 6dc2f086096..f1d192d62cd 100644 --- a/src/game_initialization/connect_engine.hpp +++ b/src/game_initialization/connect_engine.hpp @@ -60,7 +60,7 @@ public: // Import all sides into the level. void update_level(); // Updates the level and sends a diff to the clients. - void update_and_send_diff(bool update_time_of_day = false); + void update_and_send_diff(); bool can_start_game() const; void start_game(); From 2e869ce2042b0b2a617fb0f72ae7e0b80d2c97e3 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 22 Mar 2022 09:38:21 -0400 Subject: [PATCH 3/5] Few misc code cleanups I had sitting around --- src/ai/actions.cpp | 101 ++++++++++++++++++++-------------------- src/ai/actions.hpp | 2 +- src/terrain/builder.cpp | 9 ++-- src/terrain/builder.hpp | 4 +- 4 files changed, 56 insertions(+), 60 deletions(-) diff --git a/src/ai/actions.cpp b/src/ai/actions.cpp index 295678ee163..f946acd541f 100644 --- a/src/ai/actions.cpp +++ b/src/ai/actions.cpp @@ -1000,6 +1000,49 @@ static void execute_or_check(action_result& action, bool execute) } } +const std::map actions::error_names_ { + {action_result::AI_ACTION_SUCCESS, "action_result::AI_ACTION_SUCCESS"}, + {action_result::AI_ACTION_STARTED, "action_result::AI_ACTION_STARTED"}, + {action_result::AI_ACTION_FAILURE, "action_result::AI_ACTION_FAILURE"}, + + {attack_result::E_EMPTY_ATTACKER, "attack_result::E_EMPTY_ATTACKER"}, + {attack_result::E_EMPTY_DEFENDER, "attack_result::E_EMPTY_DEFENDER"}, + {attack_result::E_INCAPACITATED_ATTACKER, "attack_result::E_INCAPACITATED_ATTACKER"}, + {attack_result::E_INCAPACITATED_DEFENDER, "attack_result::E_INCAPACITATED_DEFENDER"}, + {attack_result::E_NOT_OWN_ATTACKER, "attack_result::E_NOT_OWN_ATTACKER"}, + {attack_result::E_NOT_ENEMY_DEFENDER, "attack_result::E_NOT_ENEMY_DEFENDER"}, + {attack_result::E_NO_ATTACKS_LEFT, "attack_result::E_NO_ATTACKS_LEFT"}, + {attack_result::E_WRONG_ATTACKER_WEAPON, "attack_result::E_WRONG_ATTACKER_WEAPON"}, + {attack_result::E_UNABLE_TO_CHOOSE_ATTACKER_WEAPON, "attack_result::E_UNABLE_TO_CHOOSE_ATTACKER_WEAPON"}, + {attack_result::E_ATTACKER_AND_DEFENDER_NOT_ADJACENT," attack_result::E_ATTACKER_AND_DEFENDER_NOT_ADJACENT"}, + + {move_result::E_EMPTY_MOVE, "move_result::E_EMPTY_MOVE"}, + {move_result::E_NO_UNIT, "move_result::E_NO_UNIT"}, + {move_result::E_NOT_OWN_UNIT, "move_result::E_NOT_OWN_UNIT"}, + {move_result::E_INCAPACITATED_UNIT, "move_result::E_INCAPACITATED_UNIT"}, + {move_result::E_AMBUSHED, "move_result::E_AMBUSHED"}, + {move_result::E_FAILED_TELEPORT, "move_result::E_FAILED_TELEPORT"}, + {move_result::E_NO_ROUTE, "move_result::E_NO_ROUTE"}, + {move_result::E_OFF_MAP, "move_result::E_OFF_MAP"}, + + {recall_result::E_NOT_AVAILABLE_FOR_RECALLING, "recall_result::E_NOT_AVAILABLE_FOR_RECALLING"}, + {recall_result::E_NO_GOLD, "recall_result::E_NO_GOLD"}, + {recall_result::E_NO_LEADER," recall_result::E_NO_LEADER"}, + {recall_result::E_LEADER_NOT_ON_KEEP, "recall_result::E_LEADER_NOT_ON_KEEP"}, + {recall_result::E_BAD_RECALL_LOCATION, "recall_result::E_BAD_RECALL_LOCATION"}, + + {recruit_result::E_NOT_AVAILABLE_FOR_RECRUITING, "recruit_result::E_NOT_AVAILABLE_FOR_RECRUITING"}, + {recruit_result::E_UNKNOWN_OR_DUMMY_UNIT_TYPE, "recruit_result::E_UNKNOWN_OR_DUMMY_UNIT_TYPE"}, + {recruit_result::E_NO_GOLD, "recruit_result::E_NO_GOLD"}, + {recruit_result::E_NO_LEADER, "recruit_result::E_NO_LEADER"}, + {recruit_result::E_LEADER_NOT_ON_KEEP, "recruit_result::E_LEADER_NOT_ON_KEEP"}, + {recruit_result::E_BAD_RECRUIT_LOCATION, "recruit_result::E_BAD_RECRUIT_LOCATION"}, + + {stopunit_result::E_NO_UNIT, "stopunit_result::E_NO_UNIT"}, + {stopunit_result::E_NOT_OWN_UNIT, "stopunit_result::E_NOT_OWN_UNIT"}, + {stopunit_result::E_INCAPACITATED_UNIT, "stopunit_result::E_INCAPACITATED_UNIT"}, +}; + attack_result_ptr actions::execute_attack_action(side_number side, bool execute, const map_location& attacker_loc, @@ -1007,7 +1050,7 @@ attack_result_ptr actions::execute_attack_action(side_number side, int attacker_weapon, double aggression) { - attack_result_ptr action(new attack_result(side, attacker_loc, defender_loc, attacker_weapon, aggression)); + auto action = std::make_shared(side, attacker_loc, defender_loc, attacker_weapon, aggression); execute_or_check(*action, execute); return action; } @@ -1019,7 +1062,7 @@ move_result_ptr actions::execute_move_action( side_number side, bool remove_movement, bool unreach_is_ok) { - move_result_ptr action(new move_result(side, from, to, remove_movement, unreach_is_ok)); + auto action = std::make_shared(side, from, to, remove_movement, unreach_is_ok); execute_or_check(*action, execute); return action; } @@ -1030,7 +1073,7 @@ recall_result_ptr actions::execute_recall_action( side_number side, const map_location& where, const map_location& from) { - recall_result_ptr action(new recall_result(side, unit_id, where, from)); + auto action = std::make_shared(side, unit_id, where, from); execute_or_check(*action, execute); return action; } @@ -1041,7 +1084,7 @@ recruit_result_ptr actions::execute_recruit_action( side_number side, const map_location& where, const map_location& from) { - recruit_result_ptr action(new recruit_result(side, unit_name, where, from)); + auto action = std::make_shared(side, unit_name, where, from); execute_or_check(*action, execute); return action; } @@ -1052,7 +1095,7 @@ stopunit_result_ptr actions::execute_stopunit_action( side_number side, bool remove_movement, bool remove_attacks) { - stopunit_result_ptr action(new stopunit_result(side, unit_location, remove_movement, remove_attacks)); + auto action = std::make_shared(side, unit_location, remove_movement, remove_attacks); execute_or_check(*action, execute); return action; } @@ -1062,56 +1105,14 @@ synced_command_result_ptr actions::execute_synced_command_action( side_number si const std::string& lua_code, const map_location& location) { - synced_command_result_ptr action(new synced_command_result(side, lua_code, location)); + auto action = std::make_shared(side, lua_code, location); execute_or_check(*action, execute); return action; } const std::string& actions::get_error_name(int error_code) { - if (error_names_.empty()){ - error_names_.emplace(action_result::AI_ACTION_SUCCESS, "action_result::AI_ACTION_SUCCESS"); - error_names_.emplace(action_result::AI_ACTION_STARTED, "action_result::AI_ACTION_STARTED"); - error_names_.emplace(action_result::AI_ACTION_FAILURE, "action_result::AI_ACTION_FAILURE"); - - error_names_.emplace(attack_result::E_EMPTY_ATTACKER, "attack_result::E_EMPTY_ATTACKER"); - error_names_.emplace(attack_result::E_EMPTY_DEFENDER, "attack_result::E_EMPTY_DEFENDER"); - error_names_.emplace(attack_result::E_INCAPACITATED_ATTACKER, "attack_result::E_INCAPACITATED_ATTACKER"); - error_names_.emplace(attack_result::E_INCAPACITATED_DEFENDER, "attack_result::E_INCAPACITATED_DEFENDER"); - error_names_.emplace(attack_result::E_NOT_OWN_ATTACKER, "attack_result::E_NOT_OWN_ATTACKER"); - error_names_.emplace(attack_result::E_NOT_ENEMY_DEFENDER, "attack_result::E_NOT_ENEMY_DEFENDER"); - error_names_.emplace(attack_result::E_NO_ATTACKS_LEFT, "attack_result::E_NO_ATTACKS_LEFT"); - error_names_.emplace(attack_result::E_WRONG_ATTACKER_WEAPON, "attack_result::E_WRONG_ATTACKER_WEAPON"); - error_names_.emplace(attack_result::E_UNABLE_TO_CHOOSE_ATTACKER_WEAPON, "attack_result::E_UNABLE_TO_CHOOSE_ATTACKER_WEAPON"); - error_names_.emplace(attack_result::E_ATTACKER_AND_DEFENDER_NOT_ADJACENT," attack_result::E_ATTACKER_AND_DEFENDER_NOT_ADJACENT"); - - error_names_.emplace(move_result::E_EMPTY_MOVE, "move_result::E_EMPTY_MOVE"); - error_names_.emplace(move_result::E_NO_UNIT, "move_result::E_NO_UNIT"); - error_names_.emplace(move_result::E_NOT_OWN_UNIT, "move_result::E_NOT_OWN_UNIT"); - error_names_.emplace(move_result::E_INCAPACITATED_UNIT, "move_result::E_INCAPACITATED_UNIT"); - error_names_.emplace(move_result::E_AMBUSHED, "move_result::E_AMBUSHED"); - error_names_.emplace(move_result::E_FAILED_TELEPORT, "move_result::E_FAILED_TELEPORT"); - error_names_.emplace(move_result::E_NO_ROUTE, "move_result::E_NO_ROUTE"); - error_names_.emplace(move_result::E_OFF_MAP, "move_result::E_OFF_MAP"); - - error_names_.emplace(recall_result::E_NOT_AVAILABLE_FOR_RECALLING, "recall_result::E_NOT_AVAILABLE_FOR_RECALLING"); - error_names_.emplace(recall_result::E_NO_GOLD, "recall_result::E_NO_GOLD"); - error_names_.emplace(recall_result::E_NO_LEADER," recall_result::E_NO_LEADER"); - error_names_.emplace(recall_result::E_LEADER_NOT_ON_KEEP, "recall_result::E_LEADER_NOT_ON_KEEP"); - error_names_.emplace(recall_result::E_BAD_RECALL_LOCATION, "recall_result::E_BAD_RECALL_LOCATION"); - - error_names_.emplace(recruit_result::E_NOT_AVAILABLE_FOR_RECRUITING, "recruit_result::E_NOT_AVAILABLE_FOR_RECRUITING"); - error_names_.emplace(recruit_result::E_UNKNOWN_OR_DUMMY_UNIT_TYPE, "recruit_result::E_UNKNOWN_OR_DUMMY_UNIT_TYPE"); - error_names_.emplace(recruit_result::E_NO_GOLD, "recruit_result::E_NO_GOLD"); - error_names_.emplace(recruit_result::E_NO_LEADER, "recruit_result::E_NO_LEADER"); - error_names_.emplace(recruit_result::E_LEADER_NOT_ON_KEEP, "recruit_result::E_LEADER_NOT_ON_KEEP"); - error_names_.emplace(recruit_result::E_BAD_RECRUIT_LOCATION, "recruit_result::E_BAD_RECRUIT_LOCATION"); - - error_names_.emplace(stopunit_result::E_NO_UNIT, "stopunit_result::E_NO_UNIT"); - error_names_.emplace(stopunit_result::E_NOT_OWN_UNIT, "stopunit_result::E_NOT_OWN_UNIT"); - error_names_.emplace(stopunit_result::E_INCAPACITATED_UNIT, "stopunit_result::E_INCAPACITATED_UNIT"); - } - std::map::iterator i = error_names_.find(error_code); + auto i = error_names_.find(error_code); if (i==error_names_.end()){ ERR_AI_ACTIONS << "error name not available for error #"<second; } -std::map actions::error_names_; - void sim_gamestate_changed(action_result *result, bool gamestate_changed){ if(gamestate_changed){ result->set_gamestate_changed(); diff --git a/src/ai/actions.hpp b/src/ai/actions.hpp index 6e169bcab90..af5ac898c58 100644 --- a/src/ai/actions.hpp +++ b/src/ai/actions.hpp @@ -425,7 +425,7 @@ const static std::string& get_error_name(int error_code); private: -static std::map error_names_; +static const std::map error_names_; }; diff --git a/src/terrain/builder.cpp b/src/terrain/builder.cpp index 25776fbbcf6..f4144e744c1 100644 --- a/src/terrain/builder.cpp +++ b/src/terrain/builder.cpp @@ -86,9 +86,6 @@ static map_location legacy_difference(const map_location& me, const map_location * */ -terrain_builder::building_ruleset terrain_builder::building_rules_; -const game_config_view* terrain_builder::rules_cfg_ = nullptr; - terrain_builder::rule_image::rule_image(int layer, int x, int y, bool global_image, int cx, int cy, bool is_water) : layer(layer) , basex(x) @@ -730,7 +727,7 @@ void terrain_builder::add_images_from_config(rule_imagelist& images, const confi // If an integer is given then assign that, but if a bool is given, then assign -1 if true and 0 if false int random_start = variant["random_start"].to_bool(true) ? variant["random_start"].to_int(-1) : 0; - images.back().variants.push_back(rule_image_variant(name, variations, tod, has_flag, random_start)); + images.back().variants.emplace_back(name, variations, tod, has_flag, random_start); } // Adds the main (default) variant of the image at the end, @@ -740,7 +737,7 @@ void terrain_builder::add_images_from_config(rule_imagelist& images, const confi int random_start = img["random_start"].to_bool(true) ? img["random_start"].to_int(-1) : 0; - images.back().variants.push_back(rule_image_variant(name, variations, random_start)); + images.back().variants.emplace_back(name, variations, random_start); } } @@ -1093,7 +1090,7 @@ void terrain_builder::apply_rule(const terrain_builder::building_rule& rule, con if(!constraint.no_draw) { for(const rule_image& img : constraint.images) { - btile.images.push_back(tile::rule_image_rand(&img, rand_seed)); + btile.images.emplace_back(&img, rand_seed); } } diff --git a/src/terrain/builder.hpp b/src/terrain/builder.hpp index 04f0a4e5642..32b81363424 100644 --- a/src/terrain/builder.hpp +++ b/src/terrain/builder.hpp @@ -860,8 +860,8 @@ private: bool draw_border_; /** Parsed terrain rules. Cached between instances */ - static building_ruleset building_rules_; + static inline building_ruleset building_rules_{}; /** Config used to parse global terrain rules */ - static const game_config_view* rules_cfg_; + static const inline game_config_view* rules_cfg_ = nullptr; }; From 3435cf7ab99450c7871699478aea9111bee7bd4c Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 22 Mar 2022 10:07:47 -0400 Subject: [PATCH 4/5] Added a sized_array alias to string enums --- src/enum_base.hpp | 12 ++++++++---- src/gui/dialogs/game_stats.cpp | 2 +- src/gui/dialogs/multiplayer/mp_create_game.cpp | 6 +++--- src/units/types.cpp | 10 +++++----- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/enum_base.hpp b/src/enum_base.hpp index 94e625cd54f..f00efb48f53 100644 --- a/src/enum_base.hpp +++ b/src/enum_base.hpp @@ -82,9 +82,13 @@ struct enum_base : public T } }; -#define ENUM_AND_ARRAY(...) \ - static constexpr std::size_t count = std::tuple_size::value; \ - enum class type { __VA_ARGS__ }; \ - static constexpr std::array values{ __VA_ARGS__ }; +#define ENUM_AND_ARRAY(...) \ + enum class type { __VA_ARGS__ }; \ + \ + /** Provide a variable template for an array of matching size. */ \ + template \ + using sized_array = std::array::value>; \ + \ + static constexpr sized_array values{__VA_ARGS__}; } // namespace string_enums diff --git a/src/gui/dialogs/game_stats.cpp b/src/gui/dialogs/game_stats.cpp index 04cedbb7fd4..4961574b229 100644 --- a/src/gui/dialogs/game_stats.cpp +++ b/src/gui/dialogs/game_stats.cpp @@ -67,7 +67,7 @@ unit_const_ptr game_stats::get_leader(const int side) static std::string controller_name(const team& t) { - static const std::array names {{_("controller^Idle"), _("controller^Human"), _("controller^AI"), _("controller^Reserved")}}; + static const side_controller::sized_array names {_("controller^Idle"), _("controller^Human"), _("controller^AI"), _("controller^Reserved")}; return "" + names[static_cast(t.controller())] + ""; } diff --git a/src/gui/dialogs/multiplayer/mp_create_game.cpp b/src/gui/dialogs/multiplayer/mp_create_game.cpp index 5d4b05c3d5a..b4bf0868788 100644 --- a/src/gui/dialogs/multiplayer/mp_create_game.cpp +++ b/src/gui/dialogs/multiplayer/mp_create_game.cpp @@ -253,12 +253,12 @@ void mp_create_game::pre_show(window& win) // // Set up random faction mode menu_button // - static const std::array names {{_("Independent"), _("No Mirror"), _("No Ally Mirror")}}; - static const std::array tooltips {{ + static const random_faction_mode::sized_array names {_("Independent"), _("No Mirror"), _("No Ally Mirror")}; + static const random_faction_mode::sized_array tooltips { _("Independent: Random factions assigned independently"), _("No Mirror: No two players will get the same faction"), _("No Ally Mirror: No two allied players will get the same faction") - }}; + }; std::vector rfm_options; for(std::size_t i = 0; i < random_faction_mode::size(); i++) { rfm_options.emplace_back("label", names[i]); diff --git a/src/units/types.cpp b/src/units/types.cpp index 488db24c0e2..4e7e639939d 100644 --- a/src/units/types.cpp +++ b/src/units/types.cpp @@ -839,8 +839,8 @@ bool unit_type::resistance_filter_matches( std::string unit_type::alignment_description(unit_alignments::type align, unit_race::GENDER gender) { - static const std::array male_names {{_("lawful"), _("neutral"), _("chaotic"), _("liminal")}}; - static const std::array female_names {{_("female^lawful"), _("female^neutral"), _("female^chaotic"), _("female^liminal")}}; + static const unit_alignments::sized_array male_names {_("lawful"), _("neutral"), _("chaotic"), _("liminal")}; + static const unit_alignments::sized_array female_names {_("female^lawful"), _("female^neutral"), _("female^chaotic"), _("female^liminal")}; if(gender == unit_race::FEMALE) { return female_names[static_cast(align)]; @@ -926,7 +926,7 @@ void patch_movetype(movetype& mt, // These three need to follow movetype's fallback system, where values for // movement costs are used for vision too. - const auto fallback_children = std::array{{"movement_costs", "vision_costs", "jamming_costs"}}; + const std::array fallback_children {"movement_costs", "vision_costs", "jamming_costs"}; config cumulative_values; for(const auto& x : fallback_children) { if(mt_cfg.has_child(x)) { @@ -943,7 +943,7 @@ void patch_movetype(movetype& mt, } // These don't need the fallback system - const auto child_names = std::array{{"defense", "resistance"}}; + const std::array child_names {"defense", "resistance"}; for(const auto& x : child_names) { if(mt_cfg.has_child(x)) { const auto& subtag = mt_cfg.child(x); @@ -1143,7 +1143,7 @@ void unit_type_data::set_config(const game_config_view& cfg) std::string alias; int default_val; }; - const std::array terrain_info_tags{ + const std::array terrain_info_tags{ ter_defs_to_movetype{{"movement_costs"}, {"movement"}, movetype::UNREACHABLE}, ter_defs_to_movetype{{"vision_costs"}, {"vision"}, movetype::UNREACHABLE}, ter_defs_to_movetype{{"jamming_costs"}, {"jamming"}, movetype::UNREACHABLE}, From 9b72df307783474e2c0fd79de2058a7e80697339 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 22 Mar 2022 10:19:05 -0400 Subject: [PATCH 5/5] Removed string_to_button_type from the display class --- src/display.cpp | 41 ++++++++++++++++++++++++----------------- src/display.hpp | 1 - 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 88f87ad6d92..94c56373203 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -877,6 +877,30 @@ void display::layout_buttons() } } +namespace +{ +gui::button::TYPE string_to_button_type(const std::string& type) +{ + if(type == "checkbox") { + return gui::button::TYPE_CHECK; + } else if(type == "image") { + return gui::button::TYPE_IMAGE; + } else if(type == "radiobox") { + return gui::button::TYPE_RADIO; + } else if(type == "turbo") { + return gui::button::TYPE_TURBO; + } else { + return gui::button::TYPE_PRESS; + } +} + +const std::string& get_direction(std::size_t n) +{ + static const std::array dirs{"-n", "-ne", "-se", "-s", "-sw", "-nw"}; + return dirs[n >= dirs.size() ? 0 : n]; +} +} // namespace + void display::create_buttons() { std::vector> menu_work; @@ -949,23 +973,6 @@ void display::render_buttons() } } - -gui::button::TYPE display::string_to_button_type(const std::string& type) -{ - gui::button::TYPE res = gui::button::TYPE_PRESS; - if (type == "checkbox") { res = gui::button::TYPE_CHECK; } - else if (type == "image") { res = gui::button::TYPE_IMAGE; } - else if (type == "radiobox") { res = gui::button::TYPE_RADIO; } - else if (type == "turbo") { res = gui::button::TYPE_TURBO; } - return res; -} - -static const std::string& get_direction(std::size_t n) -{ - static const std::array dirs { "-n", "-ne", "-se", "-s", "-sw", "-nw" }; - return dirs[n >= dirs.size() ? 0 : n]; -} - std::vector display::get_fog_shroud_images(const map_location& loc, image::TYPE image_type) { std::vector names; diff --git a/src/display.hpp b/src/display.hpp index 64c45d6e50d..54ae46a3241 100644 --- a/src/display.hpp +++ b/src/display.hpp @@ -397,7 +397,6 @@ public: std::shared_ptr find_action_button(const std::string& id); std::shared_ptr find_menu_button(const std::string& id); - static gui::button::TYPE string_to_button_type(const std::string& type); void create_buttons(); void layout_buttons();