aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/InstanceManager.cpp
blob: d0a4a18072215fb2ea259555173d016fdd3c0346 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#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);

   // Update gamestate...
   map_instance.update_gamestate(today, definition_manager.get_define_manager());
   country_instance_manager.update_gamestate(
      today, definition_manager.get_define_manager(), definition_manager.get_military_manager().get_unit_type_manager()
   );

   gamestate_updated();
   gamestate_needs_update = false;

   currently_updating_gamestate = false;
}

/* REQUIREMENTS:
 * SS-98, SS-101
 */
void InstanceManager::tick() {
   today++;

   Logger::info("Tick: ", today);

   // Tick...
   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(),
      definition_manager.get_pop_manager().get_pop_types(),
      definition_manager.get_politics_manager().get_ideology_manager().get_ideologies()
   );
   ret &= country_instance_manager.generate_country_instances(
      definition_manager.get_country_definition_manager(),
      definition_manager.get_economy_manager().get_building_type_manager().get_building_types(),
      definition_manager.get_research_manager().get_technology_manager().get_technologies(),
      definition_manager.get_research_manager().get_invention_manager().get_inventions(),
      definition_manager.get_politics_manager().get_ideology_manager().get_ideologies(),
      definition_manager.get_politics_manager().get_issue_manager().get_reform_groups(),
      definition_manager.get_politics_manager().get_government_type_manager().get_government_types(),
      definition_manager.get_crime_manager().get_crime_modifiers(),
      definition_manager.get_pop_manager().get_pop_types(),
      definition_manager.get_military_manager().get_unit_type_manager().get_regiment_types(),
      definition_manager.get_military_manager().get_unit_type_manager().get_ship_types()
   );

   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,
      country_instance_manager,
      // TODO - the following argument is for generating test pop attributes
      definition_manager.get_politics_manager().get_issue_manager(),
      definition_manager.get_economy_manager().get_production_type_manager()
   );

   ret &= country_instance_manager.apply_history_to_countries(
      definition_manager.get_history_manager().get_country_manager(), today, unit_instance_manager, map_instance
   );

   ret &= map_instance.get_state_manager().generate_states(
      map_instance, definition_manager.get_pop_manager().get_pop_types()
   );

   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::update_clock() {
   if (!is_game_session_started()) {
      Logger::error("Cannot update clock - game session not started!");
      return false;
   }

   simulation_clock.conditionally_advance_game();
   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);
}