-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from OpenVicProject/states
- Loading branch information
Showing
6 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |