aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/GameManager.cpp
blob: 84666cb33dc2ee23c8fd08696d4ab401da3313ed (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
#include "GameManager.hpp"

using namespace OpenVic;

GameManager::GameManager(
   InstanceManager::gamestate_updated_func_t new_gamestate_updated_callback,
   SimulationClock::state_changed_function_t new_clock_state_changed_callback
) : gamestate_updated_callback {
      new_gamestate_updated_callback ? std::move(new_gamestate_updated_callback) : []() {}
   }, clock_state_changed_callback {
      new_clock_state_changed_callback ? std::move(new_clock_state_changed_callback) : []() {}
   }, definitions_loaded { false } {}

bool GameManager::set_roots(Dataloader::path_vector_t const& roots) {
   if (!dataloader.set_roots(roots)) {
      Logger::error("Failed to set dataloader roots!");
      return false;
   }
   return true;
}

bool GameManager::load_definitions(Dataloader::localisation_callback_t localisation_callback) {
   if (definitions_loaded) {
      Logger::error("Cannot load definitions - already loaded!");
      return false;
   }

   bool ret = true;

   if (!dataloader.load_defines(definition_manager)) {
      Logger::error("Failed to load defines!");
      ret = false;
   }

   if (!dataloader.load_localisation_files(localisation_callback)) {
      Logger::error("Failed to load localisation!");
      ret = false;
   }

   definitions_loaded = true;

   return ret;
}

bool GameManager::setup_instance(Bookmark const* bookmark) {
   if (instance_manager) {
      Logger::info("Resetting existing game instance.");
   } else {
      Logger::info("Setting up first game instance.");
   }

   bool ret = true;

#if OV_MODIFIER_CALCULATION_TEST
   instance_manager.emplace(true, definition_manager, gamestate_updated_callback, clock_state_changed_callback);
   ret &= instance_manager->setup();
   ret &= instance_manager->load_bookmark(bookmark);

   instance_manager_no_add.emplace(false, definition_manager, gamestate_updated_callback, clock_state_changed_callback);
   ret &= instance_manager_no_add->setup();
   ret &= instance_manager_no_add->load_bookmark(bookmark);
#else
   instance_manager.emplace(definition_manager, gamestate_updated_callback, clock_state_changed_callback);
   ret &= instance_manager->setup();
   ret &= instance_manager->load_bookmark(bookmark);
#endif

   return ret;
}

bool GameManager::start_game_session() {
   if (!instance_manager || !instance_manager->is_game_instance_setup()) {
      Logger::error("Cannot start game session - instance manager not set up!");
      return false;
   }

   if (instance_manager->is_game_session_started()) {
      Logger::error("Cannot start game session - session already started!");
      return false;
   }

   if (!instance_manager->is_bookmark_loaded()) {
      Logger::warning("Starting game session with no bookmark loaded!");
   }

#if OV_MODIFIER_CALCULATION_TEST
   return instance_manager->start_game_session() & instance_manager_no_add->start_game_session();
#else
   return instance_manager->start_game_session();
#endif
}

bool GameManager::update_clock() {
   if (!instance_manager) {
      Logger::error("Cannot update clock - instance manager uninitialised!");
      return false;
   }

#if OV_MODIFIER_CALCULATION_TEST
   return instance_manager->update_clock() & instance_manager_no_add->update_clock();
#else
   return instance_manager->update_clock();
#endif
}