diff options
author | BrickPi <49528459+BrickPi@users.noreply.github.com> | 2023-12-04 23:03:34 +0100 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2023-12-05 00:56:37 +0100 |
commit | 471d0c80ac0b62d7e5e538ccd921bf7c5c014307 (patch) | |
tree | c0411b2b6051bb274cc8d3fb39613fbd800df511 /src/openvic-simulation | |
parent | 444a27726695478e44e0166e75df1f354b6432d5 (diff) | |
parent | e78249bea71e2503be77f9e4cfab6ffdf164126b (diff) |
Merge pull request #79 from OpenVicProject/states
Diffstat (limited to 'src/openvic-simulation')
-rw-r--r-- | src/openvic-simulation/GameManager.cpp | 1 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.hpp | 3 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.hpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/map/State.cpp | 83 | ||||
-rw-r--r-- | src/openvic-simulation/map/State.hpp | 54 |
6 files changed, 145 insertions, 2 deletions
diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index 04e680d..5b74ec4 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -59,6 +59,7 @@ bool GameManager::load_bookmark(Bookmark const* new_bookmark) { } today = bookmark->get_date(); ret &= map.apply_history_to_provinces(history_manager.get_province_manager(), today); + map.get_state_manager().generate_states(map); // TODO - apply country history // TODO - apply pop history return ret; diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp index 638d7a5..f286276 100644 --- a/src/openvic-simulation/map/Map.hpp +++ b/src/openvic-simulation/map/Map.hpp @@ -7,6 +7,7 @@ #include "openvic-simulation/map/Region.hpp" #include "openvic-simulation/map/TerrainType.hpp" +#include "openvic-simulation/map/State.hpp" namespace OpenVic { namespace fs = std::filesystem; @@ -69,6 +70,8 @@ namespace OpenVic { Province::index_t get_index_from_colour(colour_t colour) const; bool _generate_province_adjacencies(); + StateManager PROPERTY_REF(state_manager); + public: Map(); diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 193b1f9..201d3a0 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -32,7 +32,7 @@ bool Province::load_positions(BuildingManager const& building_manager, ast::Node "factory", ZERO_OR_ONE, expect_fvec2(assign_variable_callback(positions.factory)), "building_construction", ZERO_OR_ONE, expect_fvec2(assign_variable_callback(positions.building_construction)), "military_construction", ZERO_OR_ONE, expect_fvec2(assign_variable_callback(positions.military_construction)), - "building_position", ZERO_OR_ONE, expect_dictionary_keys( + "building_position", ZERO_OR_ONE, expect_dictionary_keys( // TODO: for TGC etc are building positions available for modded-in buildings? needs testing "fort", ZERO_OR_ONE, expect_fvec2(assign_variable_callback(positions.fort)), "railroad", ZERO_OR_ONE, expect_fvec2(assign_variable_callback(positions.railroad)), "naval_base", ZERO_OR_ONE, expect_fvec2(assign_variable_callback(positions.navalbase)) diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index a57f914..dd440fa 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -10,6 +10,7 @@ namespace OpenVic { struct Map; struct Region; + struct State; struct Good; struct TerrainType; struct TerrainTypeMapping; @@ -27,7 +28,7 @@ namespace OpenVic { using distance_t = uint16_t; using flags_t = uint16_t; - enum struct colony_status_t : int8_t { STATE, PROTECTORATE, COLONY }; + enum struct colony_status_t : uint8_t { STATE, PROTECTORATE, COLONY }; struct adjacency_t { friend struct Province; @@ -62,6 +63,7 @@ namespace OpenVic { private: const index_t PROPERTY(index); Region* PROPERTY(region); + State const* PROPERTY_RW(state); bool PROPERTY(on_map); bool PROPERTY(has_region); bool PROPERTY(water); diff --git a/src/openvic-simulation/map/State.cpp b/src/openvic-simulation/map/State.cpp new file mode 100644 index 0000000..bc78469 --- /dev/null +++ b/src/openvic-simulation/map/State.cpp @@ -0,0 +1,83 @@ +#include "State.hpp" + +#include "Map.hpp" + +using namespace OpenVic; + +State::State( + Country const* owner, Province const* capital, Region::provinces_t&& provinces, Province::colony_status_t colony_status +) : owner { owner }, capital { capital }, provinces { std::move(provinces) }, colony_status { colony_status } {} + +StateSet::StateSet(Region const* new_region) { + if (region->get_meta()) { + Logger::error("Cannot use meta region as state template!"); + } + region = new_region; + + std::vector<Region::provinces_t> temp_provinces; + bool in_state = false; + + for (Province* province : region->get_provinces()) { + // add to existing state if shared owner & status... + for (Region::provinces_t& provinces : temp_provinces) { + if (provinces[0] == province) { + provinces.push_back(province); + in_state = true; + break; + } + } + if (in_state) { + in_state = false; + } else { + // ...otherwise start a new state + temp_provinces.push_back({ province }); + } + } + + for (Region::provinces_t& provinces : temp_provinces) { + states.push_back({ + /* TODO: capital province logic */ + provinces[0]->get_owner(), provinces[0], std::move(provinces), provinces[0]->get_colony_status() + }); + } + + // Go back and assign each new state to its provinces. + for (State const& state : states) { + for (Province* province : state.get_provinces()) { + province->set_state(&state); + } + } +} + +bool StateSet::add_state(State&& state) { + const auto existing = std::find(states.begin(), states.end(), state); + if (existing != states.end()) { + Logger::error("Attempted to add existing state!"); + return false; + } + states.push_back(std::move(state)); + return true; +} + +bool StateSet::remove_state(State const* state) { + const auto existing = std::find(states.begin(), states.end(), *state); + if (existing == states.end()) { + Logger::error("Attempted to remove non-existant state!"); + return false; + } + states.erase(existing); + return true; +} + +StateSet::states_t& StateSet::get_states() { + return states; +} + +void StateManager::generate_states(Map const& map) { + regions.clear(); + regions.reserve(map.get_region_count()); + for(Region const& region : map.get_regions()) { + regions.push_back(StateSet(®ion)); + } + Logger::info("Generated states."); +} diff --git a/src/openvic-simulation/map/State.hpp b/src/openvic-simulation/map/State.hpp new file mode 100644 index 0000000..e403580 --- /dev/null +++ b/src/openvic-simulation/map/State.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include "openvic-simulation/map/Province.hpp" +#include "openvic-simulation/map/Region.hpp" +#include "openvic-simulation/country/Country.hpp" + +#include <deque> + +namespace OpenVic { + struct State { + private: + Country const* PROPERTY_RW(owner); + Province const* PROPERTY_RW(capital); + Region::provinces_t PROPERTY(provinces); + Province::colony_status_t PROPERTY_RW(colony_status); + + public: + State( + Country const* owner, Province const* capital, Region::provinces_t&& provinces, + Province::colony_status_t colony_status + ); + }; + + inline bool operator==(const State& lhs, const State& rhs) { + return (lhs.get_owner() == rhs.get_owner() && lhs.get_colony_status() == rhs.get_colony_status()); + } + + struct StateSet { + using states_t = std::deque<State>; + + private: + Region const* PROPERTY(region); + states_t states; + + public: + StateSet(Region const* new_region); + + bool add_state(State&& state); + bool remove_state(State const* state); + states_t& get_states(); + }; + + /* Contains all current states.*/ + struct StateManager { + private: + std::vector<StateSet> PROPERTY(regions); + + public: + /* Creates states from current province gamestate & regions, sets province state value. + * After this function, the `regions` property is unmanaged and must be carefully updated and + * validated by functions that modify it. */ + void generate_states(Map const& map); + }; +} // namespace OpenVic |