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
|
#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_definition_manager().get_country_definition_by_identifier("ENG")
);
ret &= map_instance.get_state_manager().generate_states(map_instance);
ret &= country_instance_manager.generate_country_instances(definition_manager.get_country_definition_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);
}
|