1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#include "MapInstance.hpp"
#include "openvic-simulation/history/ProvinceHistory.hpp"
#include "openvic-simulation/map/MapDefinition.hpp"
#include "openvic-simulation/utility/Logger.hpp"
using namespace OpenVic;
MapInstance::MapInstance(MapDefinition const& new_map_definition)
: map_definition { new_map_definition }, selected_province { nullptr }, highest_province_population { 0 },
total_map_population { 0 } {}
ProvinceInstance& MapInstance::get_province_instance_from_definition(ProvinceDefinition const& province) {
return province_instances.get_items()[province.get_index() - 1];
}
ProvinceInstance const& MapInstance::get_province_instance_from_definition(ProvinceDefinition const& province) const {
return province_instances.get_items()[province.get_index() - 1];
}
void MapInstance::set_selected_province(ProvinceDefinition::index_t index) {
if (index == ProvinceDefinition::NULL_INDEX) {
selected_province = nullptr;
} else {
selected_province = get_province_instance_by_index(index);
if (selected_province == nullptr) {
Logger::error(
"Trying to set selected province to an invalid index ", index, " (max index is ",
get_province_instance_count(), ")"
);
}
}
}
ProvinceInstance* MapInstance::get_selected_province() {
return selected_province;
}
ProvinceDefinition::index_t MapInstance::get_selected_province_index() const {
return selected_province != nullptr ? selected_province->get_province_definition().get_index()
: ProvinceDefinition::NULL_INDEX;
}
bool MapInstance::setup(
BuildingTypeManager const& building_type_manager,
decltype(ProvinceInstance::pop_type_distribution)::keys_t const& pop_type_keys,
decltype(ProvinceInstance::ideology_distribution)::keys_t const& ideology_keys
) {
if (province_instances_are_locked()) {
Logger::error("Cannot setup map - province instances are locked!");
return false;
}
if (!map_definition.province_definitions_are_locked()) {
Logger::error("Cannot setup map - province consts are not locked!");
return false;
}
bool ret = true;
province_instances.reserve(map_definition.get_province_definition_count());
for (ProvinceDefinition const& province : map_definition.get_province_definitions()) {
ret &= province_instances.add_item({ province, pop_type_keys, ideology_keys });
}
province_instances.lock();
for (ProvinceInstance& province : province_instances.get_items()) {
ret &= province.setup(building_type_manager);
}
if (get_province_instance_count() != map_definition.get_province_definition_count()) {
Logger::error(
"ProvinceInstance count (", get_province_instance_count(), ") does not match ProvinceDefinition count (",
map_definition.get_province_definition_count(), ")!"
);
return false;
}
return ret;
}
bool MapInstance::apply_history_to_provinces(
ProvinceHistoryManager const& history_manager, Date date, CountryInstanceManager& country_manager,
IssueManager const& issue_manager
) {
bool ret = true;
for (ProvinceInstance& province : province_instances.get_items()) {
ProvinceDefinition const& province_definition = province.get_province_definition();
if (!province_definition.is_water()) {
ProvinceHistoryMap const* history_map = history_manager.get_province_history(&province_definition);
if (history_map != nullptr) {
ProvinceHistoryEntry const* pop_history_entry = nullptr;
for (auto const& [entry_date, entry] : history_map->get_entries()) {
if (entry_date > date) {
break;
}
province.apply_history_to_province(*entry, country_manager);
if (!entry->get_pops().empty()) {
pop_history_entry = entry.get();
}
}
if (pop_history_entry != nullptr) {
province.add_pop_vec(pop_history_entry->get_pops());
province.setup_pop_test_values(issue_manager);
}
}
}
}
return ret;
}
void MapInstance::update_gamestate(Date today) {
for (ProvinceInstance& province : province_instances.get_items()) {
province.update_gamestate(today);
}
state_manager.update_gamestate();
// Update population stats
highest_province_population = 0;
total_map_population = 0;
for (ProvinceInstance const& province : province_instances.get_items()) {
const Pop::pop_size_t province_population = province.get_total_population();
if (highest_province_population < province_population) {
highest_province_population = province_population;
}
total_map_population += province_population;
}
}
void MapInstance::tick(Date today) {
for (ProvinceInstance& province : province_instances.get_items()) {
province.tick(today);
}
}
|