Skip to content

Commit 24db45d

Browse files
committed
Add void const* condition_data
1 parent 53441f5 commit 24db45d

File tree

2 files changed

+89
-36
lines changed

2 files changed

+89
-36
lines changed

src/openvic-simulation/scripts/Condition.cpp

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,18 @@ std::ostream& OpenVic::operator<<(std::ostream& stream, ConditionNode const& nod
167167
Condition::Condition(
168168
std::string_view new_identifier,
169169
parse_callback_t&& new_parse_callback,
170-
execute_callback_t&& new_execute_callback
170+
execute_callback_t&& new_execute_callback,
171+
void const* new_condition_data
171172
) : HasIdentifier { new_identifier },
172173
parse_callback { std::move(new_parse_callback) },
173-
execute_callback { std::move(new_execute_callback) } {}
174+
execute_callback { std::move(new_execute_callback) },
175+
condition_data { new_condition_data } {}
174176

175177
bool ConditionManager::add_condition(
176178
std::string_view identifier,
177179
Condition::parse_callback_t&& parse_callback,
178-
Condition::execute_callback_t&& execute_callback
180+
Condition::execute_callback_t&& execute_callback,
181+
void const* condition_data
179182
) {
180183
if (identifier.empty()) {
181184
Logger::error("Invalid condition identifier - empty!");
@@ -195,7 +198,8 @@ bool ConditionManager::add_condition(
195198
return conditions.add_item({
196199
identifier,
197200
std::move(parse_callback),
198-
std::move(execute_callback)
201+
std::move(execute_callback),
202+
condition_data
199203
});
200204
}
201205

@@ -3833,23 +3837,28 @@ bool ConditionManager::setup_conditions() {
38333837
_parse_condition_node_value_callback<fixed_point_t, COUNTRY>,
38343838
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
38353839
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
3836-
[&strata](
3840+
[](
38373841
Condition const& condition, InstanceManager const& instance_manager,
38383842
CountryInstance const* current_scope, scope_t this_scope, scope_t from_scope, fixed_point_t argument
38393843
) -> bool {
3844+
Strata const& strata = *static_cast<Strata const*>(condition.get_condition_data());
3845+
38403846
return current_scope->get_tax_rate_slider_value_by_strata()[strata].get_value() >= argument;
38413847
}
38423848
)
3843-
)
3849+
),
3850+
&strata
38443851
);
38453852
ret &= add_condition(
38463853
StringUtils::append_string_views(identifier, "_strata_life_needs"),
38473854
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | PROVINCE>,
38483855
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
3849-
[&strata](
3856+
[](
38503857
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
38513858
scope_t this_scope, scope_t from_scope, fixed_point_t argument
38523859
) -> bool {
3860+
Strata const& strata = *static_cast<Strata const*>(condition.get_condition_data());
3861+
38533862
struct visitor_t {
38543863

38553864
Condition const& condition;
@@ -3880,16 +3889,19 @@ bool ConditionManager::setup_conditions() {
38803889

38813890
return std::visit(visitor_t { condition, argument, strata }, current_scope);
38823891
}
3883-
)
3892+
),
3893+
&strata
38843894
);
38853895
ret &= add_condition(
38863896
StringUtils::append_string_views(identifier, "_strata_everyday_needs"),
38873897
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | PROVINCE>,
38883898
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
3889-
[&strata](
3899+
[](
38903900
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
38913901
scope_t this_scope, scope_t from_scope, fixed_point_t argument
38923902
) -> bool {
3903+
Strata const& strata = *static_cast<Strata const*>(condition.get_condition_data());
3904+
38933905
struct visitor_t {
38943906

38953907
Condition const& condition;
@@ -3920,16 +3932,19 @@ bool ConditionManager::setup_conditions() {
39203932

39213933
return std::visit(visitor_t { condition, argument, strata }, current_scope);
39223934
}
3923-
)
3935+
),
3936+
&strata
39243937
);
39253938
ret &= add_condition(
39263939
StringUtils::append_string_views(identifier, "_strata_luxury_needs"),
39273940
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | PROVINCE>,
39283941
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
3929-
[&strata](
3942+
[](
39303943
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
39313944
scope_t this_scope, scope_t from_scope, fixed_point_t argument
39323945
) -> bool {
3946+
Strata const& strata = *static_cast<Strata const*>(condition.get_condition_data());
3947+
39333948
struct visitor_t {
39343949

39353950
Condition const& condition;
@@ -3960,16 +3975,19 @@ bool ConditionManager::setup_conditions() {
39603975

39613976
return std::visit(visitor_t { condition, argument, strata }, current_scope);
39623977
}
3963-
)
3978+
),
3979+
&strata
39643980
);
39653981
ret &= add_condition(
39663982
StringUtils::append_string_views(identifier, "_strata_militancy"),
39673983
_parse_condition_node_value_callback<fixed_point_t, COUNTRY>,
39683984
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
3969-
[&strata](
3985+
[](
39703986
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
39713987
scope_t this_scope, scope_t from_scope, fixed_point_t argument
39723988
) -> bool {
3989+
Strata const& strata = *static_cast<Strata const*>(condition.get_condition_data());
3990+
39733991
struct visitor_t {
39743992

39753993
Condition const& condition;
@@ -3999,7 +4017,8 @@ bool ConditionManager::setup_conditions() {
39994017

40004018
return std::visit(visitor_t { condition, argument, strata }, current_scope);
40014019
}
4002-
)
4020+
),
4021+
&strata
40034022
);
40044023
}
40054024

@@ -4009,13 +4028,16 @@ bool ConditionManager::setup_conditions() {
40094028
country.get_identifier(),
40104029
_parse_condition_node_list_callback<COUNTRY>,
40114030
_execute_condition_node_list_single_scope_callback<expect_true, require_all>(
4012-
[&country](
4031+
[](
40134032
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
40144033
scope_t this_scope, scope_t from_scope
40154034
) -> scope_t {
4035+
CountryDefinition const& country = *static_cast<CountryDefinition const*>(condition.get_condition_data());
4036+
40164037
return &instance_manager.get_country_instance_manager().get_country_instance_from_definition(country);
40174038
}
4018-
)
4039+
),
4040+
&country
40194041
);
40204042
}
40214043

@@ -4026,13 +4048,16 @@ bool ConditionManager::setup_conditions() {
40264048
_execute_condition_node_list_multi_scope_callback<
40274049
expect_true, require_all, std::vector<ProvinceDefinition const*> const&
40284050
>(
4029-
[&region](
4051+
[](
40304052
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
40314053
scope_t this_scope, scope_t from_scope
40324054
) -> std::vector<ProvinceDefinition const*> const& {
4055+
Region const& region = *static_cast<Region const*>(condition.get_condition_data());
4056+
40334057
return region.get_provinces();
40344058
}
4035-
)
4059+
),
4060+
&region
40364061
);
40374062
}
40384063

@@ -4041,13 +4066,17 @@ bool ConditionManager::setup_conditions() {
40414066
province.get_identifier(),
40424067
_parse_condition_node_list_callback<PROVINCE>,
40434068
_execute_condition_node_list_single_scope_callback<expect_true, require_all>(
4044-
[&province](
4069+
[](
40454070
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
40464071
scope_t this_scope, scope_t from_scope
40474072
) -> scope_t {
4073+
ProvinceDefinition const& province =
4074+
*static_cast<ProvinceDefinition const*>(condition.get_condition_data());
4075+
40484076
return &instance_manager.get_map_instance().get_province_instance_from_definition(province);
40494077
}
4050-
)
4078+
),
4079+
&province
40514080
);
40524081
}
40534082

@@ -4057,10 +4086,12 @@ bool ConditionManager::setup_conditions() {
40574086
ideology.get_identifier(),
40584087
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | PROVINCE>,
40594088
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
4060-
[&ideology](
4089+
[](
40614090
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope,
40624091
scope_t this_scope, scope_t from_scope, fixed_point_t argument
40634092
) -> bool {
4093+
Ideology const& ideology = *static_cast<Ideology const*>(condition.get_condition_data());
4094+
40644095
struct visitor_t {
40654096

40664097
Condition const& condition;
@@ -4088,7 +4119,8 @@ bool ConditionManager::setup_conditions() {
40884119

40894120
return std::visit(visitor_t { condition, argument, ideology }, current_scope);
40904121
}
4091-
)
4122+
),
4123+
&ideology
40924124
);
40934125
}
40944126

@@ -4098,14 +4130,17 @@ bool ConditionManager::setup_conditions() {
40984130
_parse_condition_node_value_callback<Reform const*, COUNTRY>,
40994131
_execute_condition_node_cast_argument_callback<Reform const*, scope_t, scope_t, scope_t>(
41004132
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, Reform const*>(
4101-
[&reform_group](
4133+
[](
41024134
Condition const& condition, InstanceManager const& instance_manager,
41034135
CountryInstance const* current_scope, scope_t this_scope, scope_t from_scope, Reform const* argument
41044136
) -> bool {
4137+
ReformGroup const& reform_group = *static_cast<ReformGroup const*>(condition.get_condition_data());
4138+
41054139
return current_scope->get_reforms()[reform_group] == argument;
41064140
}
41074141
)
4108-
)
4142+
),
4143+
&reform_group
41094144
);
41104145
}
41114146

@@ -4115,15 +4150,18 @@ bool ConditionManager::setup_conditions() {
41154150
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | PROVINCE>,
41164151
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
41174152
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
4118-
[&reform](
4153+
[](
41194154
Condition const& condition, InstanceManager const& instance_manager,
41204155
CountryInstance const* current_scope, scope_t this_scope, scope_t from_scope, fixed_point_t argument
41214156
) -> bool {
4157+
Reform const& reform = *static_cast<Reform const*>(condition.get_condition_data());
4158+
41224159
return current_scope->get_issue_support(reform) * 100 >=
41234160
argument * current_scope->get_total_population();
41244161
}
41254162
)
4126-
)
4163+
),
4164+
&reform
41274165
);
41284166
}
41294167

@@ -4134,16 +4172,19 @@ bool ConditionManager::setup_conditions() {
41344172
_parse_condition_node_value_callback<Issue const*, COUNTRY>,
41354173
_execute_condition_node_cast_argument_callback<Issue const*, scope_t, scope_t, scope_t>(
41364174
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, Issue const*>(
4137-
[&issue_group](
4175+
[](
41384176
Condition const& condition, InstanceManager const& instance_manager,
41394177
CountryInstance const* current_scope, scope_t this_scope, scope_t from_scope, Issue const* argument
41404178
) -> bool {
4179+
IssueGroup const& issue_group = *static_cast<IssueGroup const*>(condition.get_condition_data());
4180+
41414181
CountryParty const* ruling_party = current_scope->get_ruling_party();
41424182

41434183
return ruling_party != nullptr ? ruling_party->get_policies()[issue_group] == argument : false;
41444184
}
41454185
)
4146-
)
4186+
),
4187+
&issue_group
41474188
);
41484189
}
41494190

@@ -4153,15 +4194,18 @@ bool ConditionManager::setup_conditions() {
41534194
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | PROVINCE>,
41544195
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
41554196
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
4156-
[&issue](
4197+
[](
41574198
Condition const& condition, InstanceManager const& instance_manager,
41584199
CountryInstance const* current_scope, scope_t this_scope, scope_t from_scope, fixed_point_t argument
41594200
) -> bool {
4201+
Issue const& issue = *static_cast<Issue const*>(condition.get_condition_data());
4202+
41604203
return current_scope->get_issue_support(issue) * 100 >=
41614204
argument * current_scope->get_total_population();
41624205
}
41634206
)
4164-
)
4207+
),
4208+
&issue
41654209
);
41664210
}
41674211

@@ -4171,15 +4215,18 @@ bool ConditionManager::setup_conditions() {
41714215
_parse_condition_node_value_callback<fixed_point_t, COUNTRY>,
41724216
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
41734217
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
4174-
[&pop_type](
4218+
[](
41754219
Condition const& condition, InstanceManager const& instance_manager,
41764220
CountryInstance const* current_scope, scope_t this_scope, scope_t from_scope, fixed_point_t argument
41774221
) -> bool {
4222+
PopType const& pop_type = *static_cast<PopType const*>(condition.get_condition_data());
4223+
41784224
return current_scope->get_pop_type_proportion(pop_type) >=
41794225
argument * current_scope->get_total_population();
41804226
}
41814227
)
4182-
)
4228+
),
4229+
&pop_type
41834230
);
41844231
}
41854232

@@ -4192,14 +4239,17 @@ bool ConditionManager::setup_conditions() {
41924239
_parse_condition_node_value_callback<integer_t, COUNTRY>,
41934240
_execute_condition_node_cast_argument_callback<integer_t, scope_t, scope_t, scope_t>(
41944241
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, integer_t>(
4195-
[&technology](
4242+
[](
41964243
Condition const& condition, InstanceManager const& instance_manager,
41974244
CountryInstance const* current_scope, scope_t this_scope, scope_t from_scope, integer_t argument
41984245
) -> bool {
4246+
Technology const& technology = *static_cast<Technology const*>(condition.get_condition_data());
4247+
41994248
return current_scope->is_technology_unlocked(technology) == (argument != 0);
42004249
}
42014250
)
4202-
)
4251+
),
4252+
&technology
42034253
);
42044254
}
42054255

src/openvic-simulation/scripts/Condition.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,13 @@ namespace OpenVic {
247247
private:
248248
parse_callback_t parse_callback;
249249
execute_callback_t execute_callback;
250+
void const* PROPERTY(condition_data);
250251

251252
Condition(
252253
std::string_view new_identifier,
253254
parse_callback_t&& new_parse_callback,
254-
execute_callback_t&& new_execute_callback
255+
execute_callback_t&& new_execute_callback,
256+
void const* new_condition_data
255257
);
256258

257259
public:
@@ -275,7 +277,8 @@ namespace OpenVic {
275277
bool add_condition(
276278
std::string_view identifier,
277279
Condition::parse_callback_t&& parse_callback,
278-
Condition::execute_callback_t&& execute_callback
280+
Condition::execute_callback_t&& execute_callback,
281+
void const* condition_data = nullptr
279282
);
280283

281284
template<

0 commit comments

Comments
 (0)