diff options
author | zaaarf <me@zaaarf.foo> | 2023-12-10 13:35:25 +0100 |
---|---|---|
committer | zaaarf <me@zaaarf.foo> | 2023-12-10 23:19:43 +0100 |
commit | 85155e033e9b03d71752626137a75f6418a5786c (patch) | |
tree | 986b02c46996447780975f389b9b99d01a5fc600 /src | |
parent | 34fbe27857d77d6f193d6d1848b953a4596cedd2 (diff) |
feat: implemented pop history loading
Diffstat (limited to 'src')
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp | 33 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.hpp | 1 | ||||
-rw-r--r-- | src/openvic-simulation/history/HistoryMap.hpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/history/ProvinceHistory.cpp | 44 | ||||
-rw-r--r-- | src/openvic-simulation/history/ProvinceHistory.hpp | 6 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 8 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.hpp | 1 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Pop.cpp | 25 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Pop.hpp | 4 |
9 files changed, 73 insertions, 53 deletions
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index f49ae2d..382c7aa 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -1,5 +1,6 @@ #include "Dataloader.hpp" +#include <filesystem> #include <openvic-dataloader/csv/Parser.hpp> #include <openvic-dataloader/detail/CallbackOStream.hpp> #include <openvic-dataloader/v2script/Parser.hpp> @@ -498,6 +499,25 @@ bool Dataloader::_load_history(GameManager& game_manager, bool unused_history_fi ); } ); + + /* Pop History */ + static constexpr std::string_view pop_history_directory = "history/pops"; + for (fs::path root : roots) { + fs::path concat = root / pop_history_directory; + for (fs::path dir : fs::directory_iterator(concat)) { + const Date date = Date::from_string(dir.filename().string()); + ret &= apply_to_files( + lookup_basic_indentifier_prefixed_files_in_dir_recursive(dir.string(), ".txt"), + [this, &game_manager, &date](fs::path const& file) -> bool { + const std::string filename = file.stem().string(); + return game_manager.get_history_manager().get_province_manager().load_pop_history_file( + game_manager, date, parse_defines(file).get_file_node() + ); + } + ); + } + } + game_manager.get_history_manager().get_province_manager().lock_province_histories(game_manager.get_map(), false); static constexpr std::string_view diplomacy_history_directory = "history/diplomacy"; @@ -837,19 +857,6 @@ bool Dataloader::load_defines(GameManager& game_manager) const { return ret; } -bool Dataloader::load_pop_history(GameManager& game_manager, std::string_view path) const { - return apply_to_files( - lookup_files_in_dir(path, ".txt"), - [&game_manager](fs::path const& file) -> bool { - return game_manager.get_map().expect_province_dictionary( - [&game_manager](Province& province, ast::NodeCPtr value) -> bool { - return province.load_pop_list(game_manager.get_pop_manager(), value); - } - )(parse_defines(file).get_file_node()); - } - ); -} - static bool _load_localisation_file(Dataloader::localisation_callback_t callback, std::vector<csv::LineObject> const& lines) { bool ret = true; for (csv::LineObject const& line : lines) { diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index 78021b3..7f5d0aa 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -95,7 +95,6 @@ namespace OpenVic { bool apply_to_files(path_vector_t const& files, NodeTools::callback_t<fs::path const&> callback) const; bool load_defines(GameManager& game_manager) const; - bool load_pop_history(GameManager& game_manager, std::string_view path) const; enum locale_t : size_t { English, French, German, Polish, Spanish, Italian, Swedish, diff --git a/src/openvic-simulation/history/HistoryMap.hpp b/src/openvic-simulation/history/HistoryMap.hpp index 07f54f0..4a79474 100644 --- a/src/openvic-simulation/history/HistoryMap.hpp +++ b/src/openvic-simulation/history/HistoryMap.hpp @@ -27,8 +27,6 @@ namespace OpenVic { using entry_type = _Entry; private: - std::map<Date, std::unique_ptr<entry_type>> PROPERTY(entries); - bool _try_load_history_entry(GameManager const& game_manager, Args... args, Date date, ast::NodeCPtr root) { const Date end_date = _get_end_date(game_manager); if (date > end_date) { @@ -49,6 +47,8 @@ namespace OpenVic { } protected: + std::map<Date, std::unique_ptr<entry_type>> PROPERTY_ACCESS(entries, protected); + HistoryMap() = default; virtual std::unique_ptr<entry_type> _make_entry(Date date) const = 0; diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp index d9d3ef6..61638bc 100644 --- a/src/openvic-simulation/history/ProvinceHistory.cpp +++ b/src/openvic-simulation/history/ProvinceHistory.cpp @@ -2,6 +2,7 @@ #include "openvic-simulation/GameManager.hpp" #include "openvic-simulation/dataloader/NodeTools.hpp" +#include <memory> using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -15,6 +16,37 @@ std::unique_ptr<ProvinceHistoryEntry> ProvinceHistoryMap::_make_entry(Date date) return std::unique_ptr<ProvinceHistoryEntry> { new ProvinceHistoryEntry { province, date } }; } +ProvinceHistoryEntry* ProvinceHistoryMap::_get_entry(Date date) { + return entries.at(date).get(); +} + +bool ProvinceHistoryEntry::_load_province_pop_history(PopManager const& pop_manager, ast::NodeCPtr root) { + return pop_manager.expect_pop_type_dictionary([this, &pop_manager](PopType const& type, ast::NodeCPtr pop_node) -> bool { + CultureManager const& culture_manager = pop_manager.get_culture_manager(); + ReligionManager const& religion_manager = pop_manager.get_religion_manager(); + Culture const* culture = nullptr; + Religion const* religion = nullptr; + Pop::pop_size_t size = 0; + bool ret = expect_dictionary_keys( + "culture", ONE_EXACTLY, culture_manager.expect_culture_identifier(assign_variable_callback_pointer(culture)), + "religion", ONE_EXACTLY, religion_manager.expect_religion_identifier(assign_variable_callback_pointer(religion)), + "size", ONE_EXACTLY, expect_uint(assign_variable_callback(size)), + "militancy", ZERO_OR_ONE, success_callback, + "rebel_type", ZERO_OR_ONE, success_callback + )(pop_node); + if (culture != nullptr && religion != nullptr && size > 0) { + pops.emplace_back(Pop { type, *culture, *religion, size }); + } else { + Logger::warning( + "Some pop arguments are invalid: province = ", province, ", type = ", type, ", culture = ", culture, + ", religion = ", religion, ", size = ", size + ); + } + return ret; + } + )(root); +} + bool ProvinceHistoryMap::_load_history_entry( GameManager const& game_manager, ProvinceHistoryEntry& entry, ast::NodeCPtr root ) { @@ -161,3 +193,15 @@ bool ProvinceHistoryManager::load_province_history_file( return province_history._load_history_file(game_manager, root); } + +bool ProvinceHistoryManager::load_pop_history_file(GameManager const& game_manager, Date date, ast::NodeCPtr root) { + PopManager const& pop_manager = game_manager.get_pop_manager(); + return game_manager.get_map().expect_province_dictionary([this, &pop_manager, date](Province const& province, ast::NodeCPtr node) -> bool { + if (province_histories.contains(&province)) { + ProvinceHistoryEntry* entry = province_histories.at(&province)._get_entry(date); + return entry != nullptr + ? entry->_load_province_pop_history(pop_manager, node) + : false; + } else return false; + })(root); +}
\ No newline at end of file diff --git a/src/openvic-simulation/history/ProvinceHistory.hpp b/src/openvic-simulation/history/ProvinceHistory.hpp index e4adc08..b69408e 100644 --- a/src/openvic-simulation/history/ProvinceHistory.hpp +++ b/src/openvic-simulation/history/ProvinceHistory.hpp @@ -17,6 +17,7 @@ namespace OpenVic { struct ProvinceHistoryEntry : HistoryEntry { friend struct ProvinceHistoryMap; + friend struct ProvinceHistoryManager; private: Province const& PROPERTY(province); @@ -33,8 +34,10 @@ namespace OpenVic { std::map<BuildingType const*, BuildingType::level_t> PROPERTY(province_buildings); std::map<BuildingType const*, BuildingType::level_t> PROPERTY(state_buildings); fixed_point_map_t<Ideology const*> PROPERTY(party_loyalties); + std::vector<Pop> PROPERTY(pops); ProvinceHistoryEntry(Province const& new_province, Date new_date); + bool _load_province_pop_history(PopManager const& pop_manager, ast::NodeCPtr root); }; struct ProvinceHistoryManager; @@ -45,6 +48,8 @@ namespace OpenVic { private: Province const& PROPERTY(province); + ProvinceHistoryEntry* _get_entry(Date date); + protected: ProvinceHistoryMap(Province const& new_province); @@ -66,5 +71,6 @@ namespace OpenVic { ProvinceHistoryMap const* get_province_history(Province const* province) const; bool load_province_history_file(GameManager const& game_manager, Province const& province, ast::NodeCPtr root); + bool load_pop_history_file(GameManager const& game_manager, Date date, ast::NodeCPtr root); }; } // namespace OpenVic diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 363be6c..19d06df 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -58,14 +58,6 @@ bool Province::expand_building(std::string_view building_type_identifier) { return building->expand(); } -bool Province::load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root) { - return expect_dictionary_reserve_length(pops, - [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); -} - bool Province::add_pop(Pop&& pop) { if (!get_water()) { pops.push_back(std::move(pop)); diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 0b98588..dc82deb 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -100,7 +100,6 @@ namespace OpenVic { bool expand_building(std::string_view building_type_identifier); - bool load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root); bool add_pop(Pop&& pop); size_t get_pop_count() const; void update_pops(); diff --git a/src/openvic-simulation/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp index cf54c2b..eae2e52 100644 --- a/src/openvic-simulation/pop/Pop.cpp +++ b/src/openvic-simulation/pop/Pop.cpp @@ -156,28 +156,3 @@ bool PopManager::load_pop_type_file( ); return ret; } - -bool PopManager::load_pop_into_province(Province& province, std::string_view pop_type_identifier, ast::NodeCPtr pop_node) - const { - PopType const* type = get_pop_type_by_identifier(pop_type_identifier); - Culture const* culture = nullptr; - Religion const* religion = nullptr; - Pop::pop_size_t size = 0; - bool ret = expect_dictionary_keys( - "culture", ONE_EXACTLY, culture_manager.expect_culture_identifier(assign_variable_callback_pointer(culture)), - "religion", ONE_EXACTLY, religion_manager.expect_religion_identifier(assign_variable_callback_pointer(religion)), - "size", ONE_EXACTLY, expect_uint(assign_variable_callback(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::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 736b77d..12daaf6 100644 --- a/src/openvic-simulation/pop/Pop.hpp +++ b/src/openvic-simulation/pop/Pop.hpp @@ -27,9 +27,8 @@ namespace OpenVic { pop_size_t PROPERTY(num_demoted); pop_size_t PROPERTY(num_migrated); - Pop(PopType const& new_type, Culture const& new_culture, Religion const& new_religion, pop_size_t new_size); - public: + Pop(PopType const& new_type, Culture const& new_culture, Religion const& new_religion, pop_size_t new_size); Pop(Pop const&) = delete; Pop(Pop&&) = default; Pop& operator=(Pop const&) = delete; @@ -111,6 +110,5 @@ namespace OpenVic { bool load_pop_type_file( std::string_view filestem, UnitManager const& unit_manager, GoodManager const& good_manager, ast::NodeCPtr root ); - bool load_pop_into_province(Province& province, std::string_view pop_type_identifier, ast::NodeCPtr pop_node) const; }; } |