Skip to content

Commit d50d9cf

Browse files
authored
Merge pull request #662 from OpenVicProject/type_safe_game_actions
Refactor game actions to use type safe
2 parents 82757cf + 453bd0b commit d50d9cf

File tree

6 files changed

+266
-521
lines changed

6 files changed

+266
-521
lines changed

src/openvic-simulation/InstanceManager.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "openvic-simulation/DefinitionManager.hpp"
44
#include "openvic-simulation/console/ConsoleInstance.hpp"
5+
#include "openvic-simulation/misc/GameAction.hpp"
56
#include "openvic-simulation/utility/Logger.hpp"
67

78
using namespace OpenVic;
@@ -96,7 +97,7 @@ InstanceManager::InstanceManager(
9697
},
9798
simulation_clock {
9899
[this]() -> void {
99-
queue_game_action(game_action_type_t::GAME_ACTION_TICK, {});
100+
queue_game_action<tick_argument_t>();
100101
},
101102
[this]() -> void {
102103
execute_game_actions();
@@ -335,17 +336,12 @@ void InstanceManager::update_modifier_sums() {
335336
);
336337
}
337338

338-
bool InstanceManager::queue_game_action(game_action_type_t type, game_action_argument_t&& argument) {
339+
bool InstanceManager::queue_game_action(game_action_t&& game_action) {
339340
if (currently_executing_game_actions) {
340341
spdlog::error_s("Attempted to queue a game action while already executing game actions!");
341342
return false;
342343
}
343344

344-
if (type >= game_action_type_t::MAX_GAME_ACTION) {
345-
spdlog::critical_s("Invalid game action type {}", static_cast<uint64_t>(type));
346-
return false;
347-
}
348-
349-
game_action_queue.emplace_back(type, std::move(argument));
345+
game_action_queue.emplace_back(std::move(game_action));
350346
return true;
351347
}

src/openvic-simulation/InstanceManager.hpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#pragma once
22

3+
#include <utility>
4+
5+
#include <function2/function2.hpp>
6+
37
#include "openvic-simulation/console/ConsoleInstance.hpp"
48
#include "openvic-simulation/country/CountryInstanceManager.hpp"
59
#include "openvic-simulation/country/CountryInstanceDeps.hpp"
@@ -21,8 +25,6 @@
2125
#include "openvic-simulation/utility/ThreadPool.hpp"
2226
#include "openvic-simulation/utility/Containers.hpp"
2327

24-
#include <function2/function2.hpp>
25-
2628
namespace OpenVic {
2729

2830
struct DefinitionManager;
@@ -97,6 +99,15 @@ namespace OpenVic {
9799

98100
bool set_today_and_update(Date new_today);
99101

100-
bool queue_game_action(game_action_type_t type, game_action_argument_t&& argument);
102+
template<typename T, typename... Args>
103+
bool queue_game_action(Args&&... args) {
104+
return queue_game_action(
105+
game_action_t(
106+
std::in_place_type<T>,
107+
std::forward<Args>(args)...
108+
)
109+
);
110+
}
111+
bool queue_game_action(game_action_t&& game_action);
101112
};
102113
}

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ bool ProvinceInstance::remove_core(CountryInstance& core_to_remove, bool warn) {
172172
return true;
173173
}
174174

175-
bool ProvinceInstance::expand_building(size_t building_index) {
176-
BuildingInstance* building = buildings.get_item_by_index(building_index);
175+
bool ProvinceInstance::expand_building(building_type_index_t building_type_index) {
176+
BuildingInstance* building = buildings.get_item_by_index(type_safe::get(building_type_index));
177177
if (building == nullptr) {
178178
spdlog::error_s(
179179
"Trying to expand non-existent building index {} in province {}",
180-
building_index, *this
180+
building_type_index, *this
181181
);
182182
return false;
183183
}

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ namespace OpenVic {
158158
return owner == nullptr;
159159
}
160160

161-
bool expand_building(size_t building_index);
161+
bool expand_building(building_type_index_t building_type_index);
162162

163163
bool add_pop(Pop&& pop);
164164
bool add_pop_vec(

0 commit comments

Comments
 (0)