From 7772f8871348b7b52cb0a478bb76df68d8799a07 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Fri, 8 Sep 2023 17:12:22 +0100 Subject: More refactoring and duplicate code removal --- src/openvic-simulation/pop/Pop.hpp | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/openvic-simulation/pop/Pop.hpp (limited to 'src/openvic-simulation/pop/Pop.hpp') diff --git a/src/openvic-simulation/pop/Pop.hpp b/src/openvic-simulation/pop/Pop.hpp new file mode 100644 index 0000000..3c635a4 --- /dev/null +++ b/src/openvic-simulation/pop/Pop.hpp @@ -0,0 +1,101 @@ +#pragma once + +#include + +#include "openvic-simulation/pop/Culture.hpp" +#include "openvic-simulation/pop/Religion.hpp" + +namespace OpenVic { + + struct PopManager; + struct PopType; + + /* REQUIREMENTS: + * POP-18, POP-19, POP-20, POP-21 + */ + struct Pop { + friend struct PopManager; + + using pop_size_t = int64_t; + + private: + PopType const& type; + Culture const& culture; + Religion const& religion; + pop_size_t size, num_promoted, num_demoted, num_migrated; + + Pop(PopType const& new_type, Culture const& new_culture, Religion const& new_religion, pop_size_t new_size); + + public: + Pop(Pop const&) = delete; + Pop(Pop&&) = default; + Pop& operator=(Pop const&) = delete; + Pop& operator=(Pop&&) = delete; + + PopType const& get_type() const; + Culture const& get_culture() const; + Religion const& get_religion() const; + pop_size_t get_size() const; + pop_size_t get_num_promoted() const; + pop_size_t get_num_demoted() const; + pop_size_t get_num_migrated() const; + pop_size_t get_pop_daily_change() const; + }; + + /* REQUIREMENTS: + * POP-26 + */ + struct PopType : HasIdentifierAndColour { + friend struct PopManager; + + using sprite_t = uint8_t; + + private: + const enum class strata_t { + POOR, + MIDDLE, + RICH + } strata; + const sprite_t sprite; + const Pop::pop_size_t max_size, merge_max_size; + const bool state_capital_only, demote_migrant, is_artisan, is_slave; + + // TODO - rebel composition, life/everyday/luxury needs, country and province migration targets, promote_to targets, ideologies and issues + + PopType(const std::string_view new_identifier, colour_t new_colour, strata_t new_strata, sprite_t new_sprite, Pop::pop_size_t new_max_size, Pop::pop_size_t new_merge_max_size, + bool new_state_capital_only, bool new_demote_migrant, bool new_is_artisan, bool new_is_slave); + + public: + PopType(PopType&&) = default; + + strata_t get_strata() const; + sprite_t get_sprite() const; + Pop::pop_size_t get_max_size() const; + Pop::pop_size_t get_merge_max_size() const; + bool get_state_capital_only() const; + bool get_demote_migrant() const; + bool get_is_artisan() const; + bool get_is_slave() const; + }; + + struct Province; + + struct PopManager { + private: + IdentifierRegistry pop_types; + + public: + CultureManager culture_manager; + ReligionManager religion_manager; + + PopManager(); + + bool add_pop_type(const std::string_view identifier, colour_t new_colour, PopType::strata_t strata, PopType::sprite_t sprite, + Pop::pop_size_t max_size, Pop::pop_size_t merge_max_size, bool state_capital_only, bool demote_migrant, + bool is_artisan, bool is_slave); + IDENTIFIER_REGISTRY_ACCESSORS(PopType, pop_type) + + bool load_pop_type_file(std::filesystem::path const& path, ast::NodeCPtr root); + bool load_pop_into_province(Province& province, ast::NodeCPtr root) const; + }; +} -- cgit v1.2.3-56-ga3b1 From b4220ad73e14e3b497b2fdeb83d76a6633664764 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sat, 9 Sep 2023 15:00:23 +0100 Subject: PR feedback + Logger::warning --- src/headless/main.cpp | 7 +- src/openvic-simulation/GameManager.cpp | 11 ++-- src/openvic-simulation/dataloader/Dataloader.cpp | 76 +++++++++++----------- src/openvic-simulation/dataloader/Dataloader.hpp | 29 +++++---- src/openvic-simulation/map/Map.cpp | 14 ++-- src/openvic-simulation/map/Province.cpp | 6 +- src/openvic-simulation/pop/Pop.cpp | 25 +++---- src/openvic-simulation/pop/Pop.hpp | 6 +- .../types/IdentifierRegistry.cpp | 8 +++ .../types/IdentifierRegistry.hpp | 3 + src/openvic-simulation/utility/Logger.cpp | 2 + src/openvic-simulation/utility/Logger.hpp | 1 + 12 files changed, 99 insertions(+), 89 deletions(-) (limited to 'src/openvic-simulation/pop/Pop.hpp') diff --git a/src/headless/main.cpp b/src/headless/main.cpp index 3185001..02eeb48 100644 --- a/src/headless/main.cpp +++ b/src/headless/main.cpp @@ -18,10 +18,11 @@ static char const* get_program_name(char const* name) { return last_separator; } -static bool headless_load(std::vector const& roots) { +static bool headless_load(Dataloader::path_vector_t const& roots) { bool ret = true; Logger::set_info_func([](std::string&& str) { std::cout << str; }); + Logger::set_warning_func([](std::string&& str) { std::cerr << str; }); Logger::set_error_func([](std::string&& str) { std::cerr << str; }); GameManager game_manager { []() { @@ -46,10 +47,10 @@ static bool headless_load(std::vector const& roots) { } int main(int argc, char const* argv[]) { - std::vector roots; + Dataloader::path_vector_t roots; if (argc < 2) { // TODO - non-Windows default paths - static const std::filesystem::path default_path = "C:/Program Files (x86)/Steam/steamapps/common/Victoria 2"; + static const fs::path default_path = "C:/Program Files (x86)/Steam/steamapps/common/Victoria 2"; std::cout << "Usage: " << get_program_name(argc > 0 ? argv[0] : nullptr) << " [[mod defines dir]+]\n" << "Requires defines path(s) as arguments, starting with the base defines and continuing with mods.\n" diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index ee5050d..0b42230 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -13,12 +13,11 @@ void GameManager::set_needs_update() { } void GameManager::update_state() { - if (needs_update) { - Logger::info("Update: ", today); - map.update_state(today); - if (state_updated) state_updated(); - needs_update = false; - } + if (!needs_update) return; + Logger::info("Update: ", today); + map.update_state(today); + if (state_updated) state_updated(); + needs_update = false; } /* REQUIREMENTS: diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 334d5b8..25641e2 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -11,15 +11,15 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; using namespace ovdl; -bool Dataloader::set_roots(std::vector new_roots) { +bool Dataloader::set_roots(path_vector_t new_roots) { if (!roots.empty()) { Logger::error("Overriding existing dataloader roots!"); roots.clear(); } bool ret = true; - for (std::reverse_iterator::const_iterator> it = new_roots.crbegin(); it != new_roots.crend(); ++it) { + for (std::reverse_iterator it = new_roots.crbegin(); it != new_roots.crend(); ++it) { if (std::find(roots.begin(), roots.end(), *it) == roots.end()) { - if (std::filesystem::is_directory(*it)) { + if (fs::is_directory(*it)) { Logger::info("Adding dataloader root: ", *it); roots.push_back(*it); } else { @@ -38,10 +38,10 @@ bool Dataloader::set_roots(std::vector new_roots) { return ret; } -std::filesystem::path Dataloader::lookup_file(std::filesystem::path const& path) const { - for (std::filesystem::path const& root : roots) { - const std::filesystem::path composed = root / path; - if (std::filesystem::is_regular_file(composed)) { +fs::path Dataloader::lookup_file(fs::path const& path) const { + for (fs::path const& root : roots) { + const fs::path composed = root / path; + if (fs::is_regular_file(composed)) { return composed; } } @@ -49,27 +49,27 @@ std::filesystem::path Dataloader::lookup_file(std::filesystem::path const& path) return {}; } -const std::filesystem::path Dataloader::TXT = ".txt"; +const fs::path Dataloader::TXT = ".txt"; -static bool contains_file_with_name(std::vector const& paths, - std::filesystem::path const& name) { +static bool contains_file_with_name(Dataloader::path_vector_t const& paths, + fs::path const& name) { - for (std::filesystem::path const& path : paths) { + for (fs::path const& path : paths) { if (path.filename() == name) return true; } return false; } -std::vector Dataloader::lookup_files_in_dir(std::filesystem::path const& path, - std::filesystem::path const* extension) const { +Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, + fs::path const* extension) const { - std::vector ret; - for (std::filesystem::path const& root : roots) { - const std::filesystem::path composed = root / path; + path_vector_t ret; + for (fs::path const& root : roots) { + const fs::path composed = root / path; std::error_code ec; - for (std::filesystem::directory_entry const& entry : std::filesystem::directory_iterator { composed, ec }) { + for (fs::directory_entry const& entry : fs::directory_iterator { composed, ec }) { if (entry.is_regular_file()) { - const std::filesystem::path file = entry; + const fs::path file = entry; if (extension == nullptr || file.extension() == *extension) { if (!contains_file_with_name(ret, file.filename())) { ret.push_back(file); @@ -81,19 +81,19 @@ std::vector Dataloader::lookup_files_in_dir(std::filesyst return ret; } -bool Dataloader::apply_to_files_in_dir(std::filesystem::path const& path, - std::function callback, - std::filesystem::path const* extension) const { +bool Dataloader::apply_to_files_in_dir(fs::path const& path, + std::function callback, + fs::path const* extension) const { bool ret = true; - for (std::filesystem::path const& file : lookup_files_in_dir(path, extension)) { + for (fs::path const& file : lookup_files_in_dir(path, extension)) { ret &= callback(file); } return ret; } template Parser, bool(Parser::*parse_func)()> -static Parser _run_ovdl_parser(std::filesystem::path const& path) { +static Parser _run_ovdl_parser(fs::path const& path) { Parser parser; std::string buffer; auto error_log_stream = ovdl::detail::CallbackStream { @@ -130,18 +130,18 @@ static Parser _run_ovdl_parser(std::filesystem::path const& path) { return parser; } -static v2script::Parser _parse_defines(std::filesystem::path const& path) { +static v2script::Parser _parse_defines(fs::path const& path) { return _run_ovdl_parser(path); } -static csv::Windows1252Parser _parse_csv(std::filesystem::path const& path) { +static csv::Windows1252Parser _parse_csv(fs::path const& path) { return _run_ovdl_parser(path); } -bool Dataloader::_load_pop_types(PopManager& pop_manager, std::filesystem::path const& pop_type_directory) const { +bool Dataloader::_load_pop_types(PopManager& pop_manager, fs::path const& pop_type_directory) const { const bool ret = apply_to_files_in_dir(pop_type_directory, - [&pop_manager](std::filesystem::path const& file) -> bool { - return pop_manager.load_pop_type_file(file, _parse_defines(file).get_file_node()); + [&pop_manager](fs::path const& file) -> bool { + return pop_manager.load_pop_type_file(file.stem().string(), _parse_defines(file).get_file_node()); } ); if (!ret) { @@ -151,8 +151,8 @@ bool Dataloader::_load_pop_types(PopManager& pop_manager, std::filesystem::path return ret; } -bool Dataloader::_load_map_dir(Map& map, std::filesystem::path const& map_directory) const { - static const std::filesystem::path defaults_filename = "default.map"; +bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { + static const fs::path defaults_filename = "default.map"; static const std::string default_definitions = "definition.csv"; static const std::string default_provinces = "provinces.bmp"; static const std::string default_positions = "positions.txt"; @@ -233,12 +233,12 @@ bool Dataloader::_load_map_dir(Map& map, std::filesystem::path const& map_direct } bool Dataloader::load_defines(GameManager& game_manager) const { - static const std::filesystem::path good_file = "common/goods.txt"; - static const std::filesystem::path pop_type_directory = "poptypes"; - static const std::filesystem::path graphical_culture_type_file = "common/graphicalculturetype.txt"; - static const std::filesystem::path culture_file = "common/cultures.txt"; - static const std::filesystem::path religion_file = "common/religion.txt"; - static const std::filesystem::path map_directory = "map"; + static const fs::path good_file = "common/goods.txt"; + static const fs::path pop_type_directory = "poptypes"; + static const fs::path graphical_culture_type_file = "common/graphicalculturetype.txt"; + static const fs::path culture_file = "common/cultures.txt"; + static const fs::path religion_file = "common/religion.txt"; + static const fs::path map_directory = "map"; bool ret = true; @@ -270,9 +270,9 @@ bool Dataloader::load_defines(GameManager& game_manager) const { return ret; } -bool Dataloader::load_pop_history(GameManager& game_manager, std::filesystem::path const& path) const { +bool Dataloader::load_pop_history(GameManager& game_manager, fs::path const& path) const { return apply_to_files_in_dir(path, - [&game_manager](std::filesystem::path const& file) -> bool { + [&game_manager](fs::path const& file) -> bool { return expect_dictionary( [&game_manager](std::string_view province_key, ast::NodeCPtr province_node) -> bool { Province* province = game_manager.map.get_province_by_identifier(province_key); diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index f723803..20538a6 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -5,31 +5,36 @@ #include namespace OpenVic { + namespace fs = std::filesystem; + struct GameManager; struct PopManager; struct Map; class Dataloader { - std::vector roots; + public: + using path_vector_t = std::vector; + private: + path_vector_t roots; - bool _load_pop_types(PopManager& pop_manager, std::filesystem::path const& pop_type_directory) const; - bool _load_map_dir(Map& map, std::filesystem::path const& map_directory) const; + bool _load_pop_types(PopManager& pop_manager, fs::path const& pop_type_directory) const; + bool _load_map_dir(Map& map, fs::path const& map_directory) const; public: Dataloader() = default; /* In reverse-load order, so base defines first and final loaded mod last */ - bool set_roots(std::vector new_roots); + bool set_roots(path_vector_t new_roots); - std::filesystem::path lookup_file(std::filesystem::path const& path) const; - static const std::filesystem::path TXT; - std::vector lookup_files_in_dir(std::filesystem::path const& path, - std::filesystem::path const* extension = &TXT) const; - bool apply_to_files_in_dir(std::filesystem::path const& path, - std::function callback, - std::filesystem::path const* extension = &TXT) const; + fs::path lookup_file(fs::path const& path) const; + static const fs::path TXT; + path_vector_t lookup_files_in_dir(fs::path const& path, + fs::path const* extension = &TXT) const; + bool apply_to_files_in_dir(fs::path const& path, + std::function callback, + fs::path const* extension = &TXT) const; bool load_defines(GameManager& game_manager) const; - bool load_pop_history(GameManager& game_manager, std::filesystem::path const& path) const; + bool load_pop_history(GameManager& game_manager, fs::path const& path) const; }; } diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 3f86ccc..8f10410 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -68,8 +68,8 @@ bool Map::set_water_province(const std::string_view identifier) { return false; } if (province->is_water()) { - Logger::error("Province ", identifier, " is already a water province!"); - return false; + Logger::warning("Province ", identifier, " is already a water province!"); + return true; } if (!water_provinces.add_province(province)) { Logger::error("Failed to add province ", identifier, " to water province set!"); @@ -278,7 +278,7 @@ bool Map::generate_province_shape_image(size_t new_width, size_t new_height, uin if (unrecognised_terrain_colours.find(terrain_colour) == unrecognised_terrain_colours.end()) { unrecognised_terrain_colours.insert(terrain_colour); if (detailed_errors) { - Logger::error("Unrecognised terrain colour ", colour_to_hex_string(terrain_colour), + Logger::warning("Unrecognised terrain colour ", colour_to_hex_string(terrain_colour), " at (", x, ", ", y, ")"); } } @@ -309,7 +309,7 @@ bool Map::generate_province_shape_image(size_t new_width, size_t new_height, uin if (unrecognised_province_colours.find(province_colour) == unrecognised_province_colours.end()) { unrecognised_province_colours.insert(province_colour); if (detailed_errors) { - Logger::error("Unrecognised province colour ", colour_to_hex_string(province_colour), + Logger::warning("Unrecognised province colour ", colour_to_hex_string(province_colour), " at (", x, ", ", y, ")"); } } @@ -317,12 +317,10 @@ bool Map::generate_province_shape_image(size_t new_width, size_t new_height, uin } } if (!unrecognised_province_colours.empty()) { - Logger::error("Province image contains ", unrecognised_province_colours.size(), " unrecognised province colours"); - ret = false; + Logger::warning("Province image contains ", unrecognised_province_colours.size(), " unrecognised province colours"); } if (!unrecognised_terrain_colours.empty()) { - Logger::error("Terrain image contains ", unrecognised_terrain_colours.size(), " unrecognised terrain colours"); - ret = false; + Logger::warning("Terrain image contains ", unrecognised_terrain_colours.size(), " unrecognised terrain colours"); } size_t missing = 0; diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 43eddd1..8d4abcb 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -55,10 +55,10 @@ std::string Province::to_string() const { } bool Province::load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root) { - return expect_list_reserve_length( + return expect_dictionary_reserve_length( pops, - [this, &pop_manager](ast::NodeCPtr pop_node) -> bool { - return pop_manager.load_pop_into_province(*this, pop_node); + [this, &pop_manager](std::string_view pop_type_identifier, ast::NodeCPtr pop_node) -> bool { + return pop_manager.load_pop_into_province(*this, pop_type_identifier, pop_node); } )(root); } diff --git a/src/openvic-simulation/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp index 7b4dd60..f8e7254 100644 --- a/src/openvic-simulation/pop/Pop.cpp +++ b/src/openvic-simulation/pop/Pop.cpp @@ -126,7 +126,7 @@ bool PopManager::add_pop_type(const std::string_view identifier, colour_t colour return pop_types.add_item({ identifier, colour, strata, sprite, max_size, merge_max_size, state_capital_only, demote_migrant, is_artisan, is_slave }); } -bool PopManager::load_pop_type_file(std::filesystem::path const& path, ast::NodeCPtr root) { +bool PopManager::load_pop_type_file(const std::string_view filestem, ast::NodeCPtr root) { // TODO - pop type loading @@ -135,29 +135,24 @@ bool PopManager::load_pop_type_file(std::filesystem::path const& path, ast::Node return true; } -bool PopManager::load_pop_into_province(Province& province, ast::NodeCPtr root) const { +bool PopManager::load_pop_into_province(Province& province, const std::string_view pop_type_identifier, ast::NodeCPtr pop_node) const { static PopType const* type = get_pop_type_by_identifier("test_pop_type"); Culture const* culture = nullptr; Religion const* religion = nullptr; Pop::pop_size_t size = 0; - bool ret = expect_assign( - [this, &culture, &religion, &size](std::string_view, ast::NodeCPtr pop_node) -> bool { - return expect_dictionary_keys( - "culture", ONE_EXACTLY, culture_manager.expect_culture(culture), - "religion", ONE_EXACTLY, religion_manager.expect_religion(religion), - "size", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("pop size", size)), - "militancy", ZERO_OR_ONE, success_callback, - "rebel_type", ZERO_OR_ONE, success_callback - )(pop_node); - } - )(root); + bool ret = expect_dictionary_keys( + "culture", ONE_EXACTLY, culture_manager.expect_culture(culture), + "religion", ONE_EXACTLY, religion_manager.expect_religion(religion), + "size", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("pop size", size)), + "militancy", ZERO_OR_ONE, success_callback, + "rebel_type", ZERO_OR_ONE, success_callback + )(pop_node); if (type != nullptr && culture != nullptr && religion != nullptr && size > 0) { ret &= province.add_pop({ *type, *culture, *religion, size }); } else { - Logger::error("Some pop arguments are invalid: type = ", type, ", culture = ", culture, ", religion = ", religion, ", size = ", size); - ret = false; + Logger::warning("Some pop arguments are invalid: province = ", province, ", type = ", type, ", culture = ", culture, ", religion = ", religion, ", size = ", size); } return ret; } diff --git a/src/openvic-simulation/pop/Pop.hpp b/src/openvic-simulation/pop/Pop.hpp index 3c635a4..d01eb97 100644 --- a/src/openvic-simulation/pop/Pop.hpp +++ b/src/openvic-simulation/pop/Pop.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include "openvic-simulation/pop/Culture.hpp" #include "openvic-simulation/pop/Religion.hpp" @@ -95,7 +93,7 @@ namespace OpenVic { bool is_artisan, bool is_slave); IDENTIFIER_REGISTRY_ACCESSORS(PopType, pop_type) - bool load_pop_type_file(std::filesystem::path const& path, ast::NodeCPtr root); - bool load_pop_into_province(Province& province, ast::NodeCPtr root) const; + bool load_pop_type_file(const std::string_view filestem, ast::NodeCPtr root); + bool load_pop_into_province(Province& province, const std::string_view pop_type_identifier, ast::NodeCPtr pop_node) const; }; } diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index 6d5221b..e33bc29 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp @@ -13,6 +13,14 @@ std::string const& HasIdentifier::get_identifier() const { return identifier; } +std::ostream& OpenVic::operator<<(std::ostream& stream, HasIdentifier const& obj) { + return stream << obj.get_identifier(); +} + +std::ostream& OpenVic::operator<<(std::ostream& stream, HasIdentifier const* obj) { + return obj != nullptr ? stream << *obj : stream << ""; +} + HasColour::HasColour(colour_t const new_colour, bool can_be_null) : colour(new_colour) { assert((can_be_null || colour != NULL_COLOUR) && colour <= MAX_COLOUR_RGB); } diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 494ff3e..20eebb9 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -27,6 +27,9 @@ namespace OpenVic { std::string const& get_identifier() const; }; + std::ostream& operator<<(std::ostream& stream, HasIdentifier const& obj); + std::ostream& operator<<(std::ostream& stream, HasIdentifier const* obj); + /* * Base class for objects with associated colour information. */ diff --git a/src/openvic-simulation/utility/Logger.cpp b/src/openvic-simulation/utility/Logger.cpp index bf9a67c..fca08a5 100644 --- a/src/openvic-simulation/utility/Logger.cpp +++ b/src/openvic-simulation/utility/Logger.cpp @@ -6,6 +6,8 @@ using namespace OpenVic; Logger::log_func_t Logger::info_func {}; Logger::log_queue_t Logger::info_queue {}; +Logger::log_func_t Logger::warning_func {}; +Logger::log_queue_t Logger::warning_queue {}; Logger::log_func_t Logger::error_func {}; Logger::log_queue_t Logger::error_queue {}; diff --git a/src/openvic-simulation/utility/Logger.hpp b/src/openvic-simulation/utility/Logger.hpp index 417aba7..f9ebd5d 100644 --- a/src/openvic-simulation/utility/Logger.hpp +++ b/src/openvic-simulation/utility/Logger.hpp @@ -80,6 +80,7 @@ namespace OpenVic { name(Ts&&...) -> name; LOG_FUNC(info) + LOG_FUNC(warning) LOG_FUNC(error) #undef LOG_FUNC -- cgit v1.2.3-56-ga3b1 From 3d7fbd9b376811ca0ed226fa78bcc8b6279ba8dc Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sat, 9 Sep 2023 21:49:52 +0100 Subject: Format cleanup and req comments --- .clang-format | 62 ++++++++++++++++++++++ src/headless/main.cpp | 3 +- src/openvic-simulation/dataloader/Dataloader.cpp | 56 ++++++++++--------- src/openvic-simulation/dataloader/Dataloader.hpp | 7 ++- src/openvic-simulation/dataloader/NodeTools.cpp | 2 - src/openvic-simulation/dataloader/NodeTools.hpp | 8 +-- src/openvic-simulation/economy/Good.hpp | 2 +- src/openvic-simulation/map/Map.cpp | 8 ++- src/openvic-simulation/map/Province.cpp | 2 +- src/openvic-simulation/map/Province.hpp | 1 + src/openvic-simulation/pop/Culture.cpp | 21 +++++++- src/openvic-simulation/pop/Pop.hpp | 2 +- src/openvic-simulation/pop/Religion.cpp | 6 ++- src/openvic-simulation/pop/Religion.hpp | 2 +- .../types/fixed_point/FixedPointLUT.hpp | 2 +- src/openvic-simulation/utility/StringUtils.hpp | 2 +- 16 files changed, 132 insertions(+), 54 deletions(-) create mode 100644 .clang-format (limited to 'src/openvic-simulation/pop/Pop.hpp') diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a85ae5e --- /dev/null +++ b/.clang-format @@ -0,0 +1,62 @@ +--- +Language: Cpp +UseCRLF: false +Standard: c++20 +UseTab: Always +TabWidth: 4 +IndentWidth: 4 +ColumnLimit: 0 +SpacesInSquareBrackets: false +SpacesInParentheses: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInConditionalStatement: false +SpacesInAngles: false +SpaceInEmptyParentheses: false +SpaceInEmptyBlock: false +SpaceBeforeSquareBrackets: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeInheritanceColon: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeCpp11BracedList: true +SpaceBeforeAssignmentOperators: true +SpaceAfterTemplateKeyword: false +SpaceAfterLogicalNot: false +PointerAlignment: Left +PackConstructorInitializers: BinPack +NamespaceIndentation: All +LambdaBodyIndentation: Signature +IndentExternBlock: Indent +IndentCaseLabels: true +IndentAccessModifiers: false +IncludeBlocks: Regroup +FixNamespaceComments: false +EmptyLineBeforeAccessModifier: LogicalBlock +Cpp11BracedListStyle: false +CompactNamespaces: false +BreakConstructorInitializers: BeforeColon +BreakBeforeBraces: Attach +AlwaysBreakTemplateDeclarations: Yes +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortEnumsOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: true +AlignTrailingComments: true +AlignEscapedNewlines: DontAlign +AlignAfterOpenBracket: BlockIndent +BinPackArguments: true +BinPackParameters: true +IndentRequiresClause: false +AccessModifierOffset: -4 +IncludeCategories: + - Regex: <[[:alnum:]_]+> + Priority: 1 + - Regex: <[[:alnum:]_]+[.]h> + Priority: 2 + - Regex: ^ 0 ? argv[0] : nullptr) << " [[mod defines dir]+]\n" + std::cout + << "Usage: " << get_program_name(argc > 0 ? argv[0] : nullptr) << " [[mod defines dir]+]\n" << "Requires defines path(s) as arguments, starting with the base defines and continuing with mods.\n" << "(Paths with spaces need to be enclosed in \"quotes\").\n" << "Defaulting to " << default_path << std::endl; diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 25641e2..82957fc 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -1,12 +1,12 @@ #include "Dataloader.hpp" -#include "openvic-simulation/GameManager.hpp" -#include "openvic-simulation/utility/Logger.hpp" - #include #include #include +#include "openvic-simulation/GameManager.hpp" +#include "openvic-simulation/utility/Logger.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; using namespace ovdl; @@ -51,8 +51,7 @@ fs::path Dataloader::lookup_file(fs::path const& path) const { const fs::path Dataloader::TXT = ".txt"; -static bool contains_file_with_name(Dataloader::path_vector_t const& paths, - fs::path const& name) { +static bool contains_file_with_name(Dataloader::path_vector_t const& paths, fs::path const& name) { for (fs::path const& path : paths) { if (path.filename() == name) return true; @@ -60,8 +59,7 @@ static bool contains_file_with_name(Dataloader::path_vector_t const& paths, return false; } -Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, - fs::path const* extension) const { +Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, fs::path const* extension) const { path_vector_t ret; for (fs::path const& root : roots) { @@ -81,9 +79,7 @@ Dataloader::path_vector_t Dataloader::lookup_files_in_dir(fs::path const& path, return ret; } -bool Dataloader::apply_to_files_in_dir(fs::path const& path, - std::function callback, - fs::path const* extension) const { +bool Dataloader::apply_to_files_in_dir(fs::path const& path, std::function callback, fs::path const* extension) const { bool ret = true; for (fs::path const& file : lookup_files_in_dir(path, extension)) { @@ -92,7 +88,7 @@ bool Dataloader::apply_to_files_in_dir(fs::path const& path, return ret; } -template Parser, bool(Parser::*parse_func)()> +template Parser, bool (Parser::*parse_func)()> static Parser _run_ovdl_parser(fs::path const& path) { Parser parser; std::string buffer; @@ -105,7 +101,8 @@ static Parser _run_ovdl_parser(fs::path const& path) { Logger::error("Invalid input to parser error log callback: ", s, " / ", n, " / ", user_data); return 0; } - }, &buffer + }, + &buffer }; parser.set_error_log_to(error_log_stream); parser.load_from_file(path); @@ -181,25 +178,26 @@ bool Dataloader::_load_map_dir(Map& map, fs::path const& map_directory) const { bool ret = expect_dictionary_keys( "max_provinces", ONE_EXACTLY, - expect_uint( - [&map](uint64_t val) -> bool { - if (Province::NULL_INDEX < val && val <= Province::MAX_INDEX) { - return map.set_max_provinces(val); - } - Logger::error("Invalid max province count ", val, " (out of valid range ", Province::NULL_INDEX, " < max_provinces <= ", Province::MAX_INDEX, ")"); - return false; + expect_uint( + [&map](uint64_t val) -> bool { + if (Province::NULL_INDEX < val && val <= Province::MAX_INDEX) { + return map.set_max_provinces(val); } - ), + Logger::error("Invalid max province count ", val, " (out of valid range ", + Province::NULL_INDEX, " < max_provinces <= ", Province::MAX_INDEX, ")"); + return false; + } + ), "sea_starts", ONE_EXACTLY, - expect_list_reserve_length( - water_province_identifiers, - expect_identifier( - [&water_province_identifiers](std::string_view identifier) -> bool { - water_province_identifiers.push_back(identifier); - return true; - } - ) - ), + expect_list_reserve_length( + water_province_identifiers, + expect_identifier( + [&water_province_identifiers](std::string_view identifier) -> bool { + water_province_identifiers.push_back(identifier); + return true; + } + ) + ), #define MAP_PATH_DICT_ENTRY(X) \ #X, ONE_EXACTLY, expect_string(assign_variable_callback(X)), diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index 20538a6..efada89 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -14,6 +14,7 @@ namespace OpenVic { class Dataloader { public: using path_vector_t = std::vector; + private: path_vector_t roots; @@ -28,10 +29,8 @@ namespace OpenVic { fs::path lookup_file(fs::path const& path) const; static const fs::path TXT; - path_vector_t lookup_files_in_dir(fs::path const& path, - fs::path const* extension = &TXT) const; - bool apply_to_files_in_dir(fs::path const& path, - std::function callback, + path_vector_t lookup_files_in_dir(fs::path const& path, fs::path const* extension = &TXT) const; + bool apply_to_files_in_dir(fs::path const& path, std::function callback, fs::path const* extension = &TXT) const; bool load_defines(GameManager& game_manager) const; diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 3b05d04..63a97ad 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -1,7 +1,5 @@ #include "NodeTools.hpp" -#include - using namespace OpenVic; using namespace OpenVic::NodeTools; diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 805de94..a68e922 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -2,12 +2,12 @@ #include +#include + #include "openvic-simulation/types/Colour.hpp" #include "openvic-simulation/types/Date.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#include - namespace OpenVic { namespace ast = ovdl::v2script::ast; @@ -56,7 +56,7 @@ namespace OpenVic { size_t count; dictionary_entry_t(expected_count_t new_expected_count, node_callback_t new_callback) - : expected_count { new_expected_count }, callback { new_callback }, count { 0 } {} + : expected_count { new_expected_count }, callback { new_callback }, count { 0 } {} constexpr bool must_appear() const { return static_cast(expected_count) & static_cast(expected_count_t::_MUST_APPEAR); @@ -106,7 +106,7 @@ namespace OpenVic { template concept Reservable = requires(T& t) { { t.size() } -> std::same_as; - t.reserve( size_t {} ); + t.reserve(size_t {}); }; template node_callback_t expect_list_reserve_length(T& t, node_callback_t callback) { diff --git a/src/openvic-simulation/economy/Good.hpp b/src/openvic-simulation/economy/Good.hpp index d5cd532..ce97cad 100644 --- a/src/openvic-simulation/economy/Good.hpp +++ b/src/openvic-simulation/economy/Good.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { struct GoodManager; diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 8f10410..728fc42 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -490,11 +490,9 @@ bool Map::load_province_definitions(std::vector const& lines) { const std::string_view identifier = line.get_value_for(0); if (!identifier.empty()) { colour_t colour; - if (!parse_province_colour(colour, { - line.get_value_for(1), - line.get_value_for(2), - line.get_value_for(3) - })) { + if (!parse_province_colour(colour, + { line.get_value_for(1), line.get_value_for(2), line.get_value_for(3) } + )) { Logger::error("Error reading colour in province definition: ", line); ret = false; } diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 8d4abcb..f53de3a 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -102,7 +102,7 @@ distribution_t const& Province::get_religion_distribution() const { } /* REQUIREMENTS: - * MAP-65 + * MAP-65, MAP-68, MAP-70, MAP-234 */ void Province::update_pops() { total_population = 0; diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 682fc16..67816ff 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -10,6 +10,7 @@ namespace OpenVic { /* REQUIREMENTS: * MAP-5, MAP-7, MAP-8, MAP-43, MAP-47 + * POP-22 */ struct Province : HasIdentifierAndColour { friend struct Map; diff --git a/src/openvic-simulation/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp index 7b595fb..709f305 100644 --- a/src/openvic-simulation/pop/Culture.cpp +++ b/src/openvic-simulation/pop/Culture.cpp @@ -13,7 +13,6 @@ CultureGroup::CultureGroup(const std::string_view new_identifier, const std::str unit_graphical_culture_type { new_unit_graphical_culture_type }, is_overseas { new_is_overseas } {} - std::string const& CultureGroup::get_leader() const { return leader; } @@ -161,6 +160,26 @@ bool CultureManager::_load_culture(CultureGroup const* culture_group, return ret; } +/* REQUIREMENTS: + * POP-59, POP-60, POP-61, POP-62, POP-63, POP-64, POP-65, POP-66, POP-67, POP-68, POP-69, POP-70, POP-71, + * POP-72, POP-73, POP-74, POP-75, POP-76, POP-77, POP-78, POP-79, POP-80, POP-81, POP-82, POP-83, POP-84, + * POP-85, POP-86, POP-87, POP-88, POP-89, POP-90, POP-91, POP-92, POP-93, POP-94, POP-95, POP-96, POP-97, + * POP-98, POP-99, POP-100, POP-101, POP-102, POP-103, POP-104, POP-105, POP-106, POP-107, POP-108, POP-109, POP-110, + * POP-111, POP-112, POP-113, POP-114, POP-115, POP-116, POP-117, POP-118, POP-119, POP-120, POP-121, POP-122, POP-123, + * POP-124, POP-125, POP-126, POP-127, POP-128, POP-129, POP-130, POP-131, POP-132, POP-133, POP-134, POP-135, POP-136, + * POP-137, POP-138, POP-139, POP-140, POP-141, POP-142, POP-143, POP-144, POP-145, POP-146, POP-147, POP-148, POP-149, + * POP-150, POP-151, POP-152, POP-153, POP-154, POP-155, POP-156, POP-157, POP-158, POP-159, POP-160, POP-161, POP-162, + * POP-163, POP-164, POP-165, POP-166, POP-167, POP-168, POP-169, POP-170, POP-171, POP-172, POP-173, POP-174, POP-175, + * POP-176, POP-177, POP-178, POP-179, POP-180, POP-181, POP-182, POP-183, POP-184, POP-185, POP-186, POP-187, POP-188, + * POP-189, POP-190, POP-191, POP-192, POP-193, POP-194, POP-195, POP-196, POP-197, POP-198, POP-199, POP-200, POP-201, + * POP-202, POP-203, POP-204, POP-205, POP-206, POP-207, POP-208, POP-209, POP-210, POP-211, POP-212, POP-213, POP-214, + * POP-215, POP-216, POP-217, POP-218, POP-219, POP-220, POP-221, POP-222, POP-223, POP-224, POP-225, POP-226, POP-227, + * POP-228, POP-229, POP-230, POP-231, POP-232, POP-233, POP-234, POP-235, POP-236, POP-237, POP-238, POP-239, POP-240, + * POP-241, POP-242, POP-243, POP-244, POP-245, POP-246, POP-247, POP-248, POP-249, POP-250, POP-251, POP-252, POP-253, + * POP-254, POP-255, POP-256, POP-257, POP-258, POP-259, POP-260, POP-261, POP-262, POP-263, POP-264, POP-265, POP-266, + * POP-267, POP-268, POP-269, POP-270, POP-271, POP-272, POP-273, POP-274, POP-275, POP-276, POP-277, POP-278, POP-279, + * POP-280, POP-281, POP-282, POP-283, POP-284 + */ bool CultureManager::load_culture_file(ast::NodeCPtr root) { if (!graphical_culture_types.is_locked()) { Logger::error("Cannot load culture groups until graphical culture types are locked!"); diff --git a/src/openvic-simulation/pop/Pop.hpp b/src/openvic-simulation/pop/Pop.hpp index d01eb97..e70bc0b 100644 --- a/src/openvic-simulation/pop/Pop.hpp +++ b/src/openvic-simulation/pop/Pop.hpp @@ -41,7 +41,7 @@ namespace OpenVic { }; /* REQUIREMENTS: - * POP-26 + * POP-15, POP-16, POP-17, POP-26 */ struct PopType : HasIdentifierAndColour { friend struct PopManager; diff --git a/src/openvic-simulation/pop/Religion.cpp b/src/openvic-simulation/pop/Religion.cpp index b336ae1..0652eb2 100644 --- a/src/openvic-simulation/pop/Religion.cpp +++ b/src/openvic-simulation/pop/Religion.cpp @@ -64,8 +64,11 @@ bool ReligionManager::add_religion(const std::string_view identifier, colour_t c return religions.add_item({ identifier, colour, *group, icon, pagan }); } +/* REQUIREMENTS: + * POP-286, POP-287, POP-288, POP-289, POP-290, POP-291, POP-292, + * POP-293, POP-294, POP-295, POP-296, POP-297, POP-298, POP-299 + */ bool ReligionManager::load_religion_file(ast::NodeCPtr root) { - size_t total_expected_religions = 0; bool ret = expect_dictionary_reserve_length( religion_groups, @@ -85,7 +88,6 @@ bool ReligionManager::load_religion_file(ast::NodeCPtr root) { religions.reserve(religions.size() + total_expected_religions); ret &= expect_dictionary( [this](std::string_view religion_group_key, ast::NodeCPtr religion_group_value) -> bool { - ReligionGroup const* religion_group = get_religion_group_by_identifier(religion_group_key); return expect_dictionary( diff --git a/src/openvic-simulation/pop/Religion.hpp b/src/openvic-simulation/pop/Religion.hpp index bd65321..8267659 100644 --- a/src/openvic-simulation/pop/Religion.hpp +++ b/src/openvic-simulation/pop/Religion.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { diff --git a/src/openvic-simulation/types/fixed_point/FixedPointLUT.hpp b/src/openvic-simulation/types/fixed_point/FixedPointLUT.hpp index 466517b..45cb639 100644 --- a/src/openvic-simulation/types/fixed_point/FixedPointLUT.hpp +++ b/src/openvic-simulation/types/fixed_point/FixedPointLUT.hpp @@ -9,7 +9,7 @@ namespace OpenVic::FPLUT { - #include "FixedPointLUT_sin.hpp" +#include "FixedPointLUT_sin.hpp" constexpr int32_t SHIFT = SIN_LUT_PRECISION - SIN_LUT_COUNT_LOG2; diff --git a/src/openvic-simulation/utility/StringUtils.hpp b/src/openvic-simulation/utility/StringUtils.hpp index 5d6001c..97efbed 100644 --- a/src/openvic-simulation/utility/StringUtils.hpp +++ b/src/openvic-simulation/utility/StringUtils.hpp @@ -26,7 +26,7 @@ namespace OpenVic::StringUtils { if (*str == '0') { if (str + 1 != end && (str[1] == 'x' || str[1] == 'X')) { base = 16; // Hexadecimal. - str += 2; // Skip '0x' or '0X' + str += 2; // Skip '0x' or '0X' if (str == end) return 0; } else { base = 8; // Octal. -- cgit v1.2.3-56-ga3b1