diff options
author | Hop311 <Hop3114@gmail.com> | 2024-06-13 18:59:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-13 18:59:47 +0200 |
commit | bfd539513af435564daf70f94f6cce146dd5c948 (patch) | |
tree | 7ba5a06714c267185dd3a59aabc6556a105c122f /src/openvic-simulation/InstanceManager.cpp | |
parent | 01a8f60aaca9784d814f4cb35a3fc4be86e1dd20 (diff) | |
parent | 4b39e77304094708d5c503b6a221386408cc4409 (diff) |
Merge pull request #165 from OpenVicProject/instance-definition-managers
Separated GameManager into Definition and Instance Managers
Diffstat (limited to 'src/openvic-simulation/InstanceManager.cpp')
-rw-r--r-- | src/openvic-simulation/InstanceManager.cpp | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/openvic-simulation/InstanceManager.cpp b/src/openvic-simulation/InstanceManager.cpp new file mode 100644 index 0000000..659931c --- /dev/null +++ b/src/openvic-simulation/InstanceManager.cpp @@ -0,0 +1,140 @@ +#include "InstanceManager.hpp" + +#include "openvic-simulation/DefinitionManager.hpp" +#include "openvic-simulation/utility/Logger.hpp" + +using namespace OpenVic; + +InstanceManager::InstanceManager( + DefinitionManager const& new_definition_manager, gamestate_updated_func_t gamestate_updated_callback, + SimulationClock::state_changed_function_t clock_state_changed_callback +) : definition_manager { new_definition_manager }, + map_instance { new_definition_manager.get_map_definition() }, + simulation_clock { + std::bind(&InstanceManager::tick, this), std::bind(&InstanceManager::update_gamestate, this), + clock_state_changed_callback ? std::move(clock_state_changed_callback) : []() {} + }, + game_instance_setup { false }, + game_session_started { false }, + session_start { 0 }, + bookmark { nullptr }, + today {}, + gamestate_updated { gamestate_updated_callback ? std::move(gamestate_updated_callback) : []() {} }, + gamestate_needs_update { false }, + currently_updating_gamestate { false } {} + +void InstanceManager::set_gamestate_needs_update() { + if (!currently_updating_gamestate) { + gamestate_needs_update = true; + } else { + Logger::error("Attempted to queue a gamestate update already updating the gamestate!"); + } +} + +void InstanceManager::update_gamestate() { + if (!gamestate_needs_update) { + return; + } + currently_updating_gamestate = true; + Logger::info("Update: ", today); + map_instance.update_gamestate(today); + gamestate_updated(); + gamestate_needs_update = false; + currently_updating_gamestate = false; +} + +/* REQUIREMENTS: + * SS-98, SS-101 + */ +void InstanceManager::tick() { + today++; + Logger::info("Tick: ", today); + map_instance.tick(today); + set_gamestate_needs_update(); +} + +bool InstanceManager::setup() { + if (is_game_instance_setup()) { + Logger::error("Cannot setup game instance - already set up!"); + return false; + } + + bool ret = good_instance_manager.setup(definition_manager.get_economy_manager().get_good_definition_manager()); + ret &= map_instance.setup(definition_manager.get_economy_manager().get_building_type_manager()); + + game_instance_setup = true; + + return ret; +} + +bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) { + if (is_bookmark_loaded()) { + Logger::error("Cannot load bookmark - already loaded!"); + return false; + } + + if (!is_game_instance_setup()) { + Logger::error("Cannot load bookmark - game instance not set up!"); + return false; + } + + if (new_bookmark == nullptr) { + Logger::error("Cannot load bookmark - null!"); + return false; + } + + bookmark = new_bookmark; + + Logger::info("Loading bookmark ", bookmark->get_name(), " with start date ", bookmark->get_date()); + + if (!definition_manager.get_define_manager().in_game_period(bookmark->get_date())) { + Logger::warning("Bookmark date ", bookmark->get_date(), " is not in the game's time period!"); + } + + today = bookmark->get_date(); + + bool ret = map_instance.apply_history_to_provinces( + definition_manager.get_history_manager().get_province_manager(), today, + // TODO - the following arguments are for generating test pop attributes + definition_manager.get_politics_manager().get_ideology_manager(), + definition_manager.get_politics_manager().get_issue_manager(), + *definition_manager.get_country_manager().get_country_by_identifier("ENG") + ); + ret &= map_instance.get_state_manager().generate_states(map_instance); + + ret &= country_instance_manager.generate_country_instances(definition_manager.get_country_manager()); + ret &= country_instance_manager.apply_history_to_countries( + definition_manager.get_history_manager().get_country_manager(), today, unit_instance_manager, map_instance + ); + + return ret; +} + +bool InstanceManager::start_game_session() { + if (is_game_session_started()) { + Logger::error("Cannot start game session - already started!"); + return false; + } + + session_start = time(nullptr); + simulation_clock.reset(); + set_gamestate_needs_update(); + + game_session_started = true; + + return true; +} + +bool InstanceManager::expand_selected_province_building(size_t building_index) { + set_gamestate_needs_update(); + ProvinceInstance* province = map_instance.get_selected_province(); + if (province == nullptr) { + Logger::error("Cannot expand building index ", building_index, " - no province selected!"); + return false; + } + if (building_index < 0) { + Logger::error("Invalid building index ", building_index, " while trying to expand in province ", province); + return false; + } + return province->expand_building(building_index); +} |