Skip to content

Commit 827e7ad

Browse files
committed
More execution callbacks, including simplifying some
1 parent 392ee9e commit 827e7ad

File tree

1 file changed

+107
-141
lines changed

1 file changed

+107
-141
lines changed

src/openvic-simulation/scripts/Condition.cpp

Lines changed: 107 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,99 +1222,35 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
12221222

12231223
/* Trigger Country Scopes */
12241224
ret &= add_condition(
1225-
"all_core",
1225+
"all_core", // All core provinces of a country?
12261226
_parse_condition_node_list_callback<PROVINCE, COUNTRY>,
1227-
_execute_condition_node_cast_argument_callback<std::vector<ConditionNode>, scope_t, scope_t, scope_t>(
1228-
[](
1229-
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope, scope_t this_scope,
1230-
scope_t from_scope, std::vector<ConditionNode> const& argument
1231-
) -> bool {
1232-
struct visitor_t {
1233-
1234-
Condition const& condition;
1235-
InstanceManager const& instance_manager;
1236-
scope_t const& this_scope;
1237-
scope_t const& from_scope;
1238-
std::vector<ConditionNode> const& argument;
1239-
bool only_cores = false;
1240-
1241-
bool operator()(no_scope_t no_scope) const {
1242-
Logger::error("Error executing condition \"", condition.get_identifier(), "\": no current scope!");
1243-
return false;
1244-
}
1245-
1246-
constexpr bool operator()(CountryInstance const* country) {
1247-
// Used to skip having to look up country in every province's core ordered_set<CountryInstance*>
1248-
only_cores = true;
1249-
return _execute_iterative<expect_true, require_all>(country->get_core_provinces(), *this);
1250-
}
1251-
constexpr bool operator()(State const* state) const {
1252-
return _execute_iterative<expect_true, require_all>(state->get_provinces(), *this);
1253-
}
1254-
constexpr bool operator()(ProvinceInstance const* province) const {
1255-
return (only_cores || province->is_owner_core())
1256-
&& _execute_condition_node_list<expect_true, require_all>(
1257-
instance_manager, province, this_scope, from_scope, argument
1258-
);
1259-
}
1260-
constexpr bool operator()(Pop const* pop) const {
1261-
ProvinceInstance const* province = pop->get_location();
1262-
return province != nullptr && (*this)(province);
1263-
}
1264-
};
1265-
1266-
return std::visit(visitor_t {
1267-
condition, instance_manager, this_scope, from_scope, argument
1268-
}, current_scope);
1269-
}
1227+
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, argument_t const&>(
1228+
_execute_condition_node_list_multi_scope_callback<
1229+
expect_true, require_all, ordered_set<ProvinceInstance*> const&, CountryInstance const*
1230+
>(
1231+
[](
1232+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1233+
scope_t this_scope, scope_t from_scope
1234+
) -> ordered_set<ProvinceInstance*> const& {
1235+
return current_scope->get_core_provinces();
1236+
}
1237+
)
12701238
)
12711239
);
12721240
ret &= add_condition(
1273-
"any_core",
1274-
_parse_condition_node_list_callback<PROVINCE, COUNTRY | PROVINCE>,
1275-
_execute_condition_node_cast_argument_callback<std::vector<ConditionNode>, scope_t, scope_t, scope_t>(
1276-
[](
1277-
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope, scope_t this_scope,
1278-
scope_t from_scope, std::vector<ConditionNode> const& argument
1279-
) -> bool {
1280-
struct visitor_t {
1281-
1282-
Condition const& condition;
1283-
InstanceManager const& instance_manager;
1284-
scope_t const& this_scope;
1285-
scope_t const& from_scope;
1286-
std::vector<ConditionNode> const& argument;
1287-
bool only_cores = false;
1288-
1289-
bool operator()(no_scope_t no_scope) const {
1290-
Logger::error("Error executing condition \"", condition.get_identifier(), "\": no current scope!");
1291-
return false;
1292-
}
1293-
1294-
constexpr bool operator()(CountryInstance const* country) {
1295-
// Used to skip having to look up country in every province's core ordered_set<CountryInstance*>
1296-
only_cores = true;
1297-
return _execute_iterative<expect_true, require_any>(country->get_core_provinces(), *this);
1298-
}
1299-
constexpr bool operator()(State const* state) const {
1300-
return _execute_iterative<expect_true, require_any>(state->get_provinces(), *this);
1301-
}
1302-
constexpr bool operator()(ProvinceInstance const* province) const {
1303-
return (only_cores || province->is_owner_core())
1304-
&& _execute_condition_node_list<expect_true, require_all>(
1305-
instance_manager, province, this_scope, from_scope, argument
1306-
);
1307-
}
1308-
constexpr bool operator()(Pop const* pop) const {
1309-
ProvinceInstance const* province = pop->get_location();
1310-
return province != nullptr && (*this)(province);
1311-
}
1312-
};
1313-
1314-
return std::visit(visitor_t {
1315-
condition, instance_manager, this_scope, from_scope, argument
1316-
}, current_scope);
1317-
}
1241+
"any_core", // Any country with a core in this province?
1242+
_parse_condition_node_list_callback<COUNTRY, PROVINCE>,
1243+
_execute_condition_node_convert_scope<ProvinceInstance, scope_t, scope_t, argument_t const&>(
1244+
_execute_condition_node_list_multi_scope_callback<
1245+
expect_true, require_any, ordered_set<CountryInstance*> const&, ProvinceInstance const*
1246+
>(
1247+
[](
1248+
Condition const& condition, InstanceManager const& instance_manager, ProvinceInstance const* current_scope,
1249+
scope_t this_scope, scope_t from_scope
1250+
) -> ordered_set<CountryInstance*> const& {
1251+
return current_scope->get_cores();
1252+
}
1253+
)
13181254
)
13191255
);
13201256
ret &= add_condition(
@@ -1366,50 +1302,19 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
13661302
)
13671303
);
13681304
ret &= add_condition(
1369-
"any_pop",
1370-
_parse_condition_node_list_callback<POP, COUNTRY | PROVINCE>,
1371-
_execute_condition_node_cast_argument_callback<std::vector<ConditionNode>, scope_t, scope_t, scope_t>(
1372-
[](
1373-
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope, scope_t this_scope,
1374-
scope_t from_scope, std::vector<ConditionNode> const& argument
1375-
) -> bool {
1376-
struct visitor_t {
1377-
1378-
Condition const& condition;
1379-
InstanceManager const& instance_manager;
1380-
scope_t const& this_scope;
1381-
scope_t const& from_scope;
1382-
std::vector<ConditionNode> const& argument;
1383-
1384-
bool operator()(no_scope_t no_scope) const {
1385-
Logger::error("Error executing condition \"", condition.get_identifier(), "\": no current scope!");
1386-
return false;
1387-
}
1388-
1389-
constexpr bool operator()(CountryInstance const* country) {
1390-
// TODO - should this be controlled provinces?
1391-
return _execute_iterative<expect_true, require_any>(country->get_owned_provinces(), *this);
1392-
}
1393-
constexpr bool operator()(State const* state) const {
1394-
return _execute_iterative<expect_true, require_any>(state->get_provinces(), *this);
1395-
}
1396-
constexpr bool operator()(ProvinceInstance const* province) const {
1397-
return _execute_iterative<expect_true, require_any>(province->get_pops(), *this);
1398-
}
1399-
constexpr bool operator()(Pop const* pop) const {
1400-
return _execute_condition_node_list<expect_true, require_all>(
1401-
instance_manager, pop, this_scope, from_scope, argument
1402-
);
1403-
}
1404-
constexpr bool operator()(Pop const& pop) const {
1405-
return (*this)(&pop);
1406-
}
1407-
};
1408-
1409-
return std::visit(visitor_t {
1410-
condition, instance_manager, this_scope, from_scope, argument
1411-
}, current_scope);
1412-
}
1305+
"any_pop", // In vanilla this is only used at province scope
1306+
_parse_condition_node_list_callback<POP, PROVINCE>,
1307+
_execute_condition_node_convert_scope<ProvinceInstance, scope_t, scope_t, argument_t const&>(
1308+
_execute_condition_node_list_multi_scope_callback<
1309+
expect_true, require_any, plf::colony<Pop> const&, ProvinceInstance const*
1310+
>(
1311+
[](
1312+
Condition const& condition, InstanceManager const& instance_manager, ProvinceInstance const* current_scope,
1313+
scope_t this_scope, scope_t from_scope
1314+
) -> plf::colony<Pop> const& {
1315+
return current_scope->get_pops();
1316+
}
1317+
)
14131318
)
14141319
);
14151320
ret &= add_condition(
@@ -1937,7 +1842,17 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
19371842
ret &= add_condition(
19381843
"civilization_progress",
19391844
_parse_condition_node_value_callback<fixed_point_t, COUNTRY>,
1940-
_execute_condition_node_unimplemented
1845+
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
1846+
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
1847+
[](
1848+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1849+
scope_t this_scope, scope_t from_scope, fixed_point_t argument
1850+
) -> bool {
1851+
// TODO - check if civilisation progress of *current_scope is >= argument
1852+
return false;
1853+
}
1854+
)
1855+
)
19411856
);
19421857
ret &= add_condition(
19431858
"civilized",
@@ -1956,7 +1871,17 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
19561871
ret &= add_condition(
19571872
"colonial_nation",
19581873
_parse_condition_node_value_callback<bool, COUNTRY>,
1959-
_execute_condition_node_unimplemented
1874+
_execute_condition_node_cast_argument_callback<bool, scope_t, scope_t, scope_t>(
1875+
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, bool>(
1876+
[](
1877+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1878+
scope_t this_scope, scope_t from_scope, bool argument
1879+
) -> bool {
1880+
// TODO - check if *current_scope has any colonies == argument
1881+
return false;
1882+
}
1883+
)
1884+
)
19601885
);
19611886
ret &= add_condition(
19621887
"consciousness",
@@ -1975,12 +1900,32 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
19751900
ret &= add_condition(
19761901
"constructing_cb_progress",
19771902
_parse_condition_node_value_callback<fixed_point_t, COUNTRY>,
1978-
_execute_condition_node_unimplemented
1903+
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
1904+
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
1905+
[](
1906+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1907+
scope_t this_scope, scope_t from_scope, fixed_point_t argument
1908+
) -> bool {
1909+
// TODO - check if *current_scope is constructing a CB with progress >= argument
1910+
return false;
1911+
}
1912+
)
1913+
)
19791914
);
19801915
ret &= add_condition(
19811916
"constructing_cb_type",
19821917
_parse_condition_node_value_callback<WargoalType const*, COUNTRY>,
1983-
_execute_condition_node_unimplemented
1918+
_execute_condition_node_cast_argument_callback<WargoalType const*, scope_t, scope_t, scope_t>(
1919+
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, WargoalType const*>(
1920+
[](
1921+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1922+
scope_t this_scope, scope_t from_scope, WargoalType const* argument
1923+
) -> bool {
1924+
// TODO - check if *current_scope is constructing a CB with type == argument
1925+
return false;
1926+
}
1927+
)
1928+
)
19841929
);
19851930
ret &= add_condition(
19861931
"continent",
@@ -2012,14 +1957,35 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
20121957
)
20131958
);
20141959
ret &= add_condition(
2015-
"crime_fighting",
2016-
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | PROVINCE>,
2017-
_execute_condition_node_unimplemented
1960+
"crime_fighting", // Could be province scope too?
1961+
_parse_condition_node_value_callback<fixed_point_t, COUNTRY>,
1962+
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
1963+
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
1964+
[](
1965+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1966+
scope_t this_scope, scope_t from_scope, fixed_point_t argument
1967+
) -> bool {
1968+
return current_scope->get_administration_spending().get_value() >= argument * CountryInstance::SLIDER_SIZE;
1969+
}
1970+
)
1971+
)
20181972
);
20191973
ret &= add_condition(
20201974
"crime_higher_than_education",
20211975
_parse_condition_node_value_callback<bool, COUNTRY>,
2022-
_execute_condition_node_unimplemented
1976+
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
1977+
_execute_condition_node_convert_scope<CountryInstance, scope_t, scope_t, fixed_point_t>(
1978+
[](
1979+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1980+
scope_t this_scope, scope_t from_scope, fixed_point_t argument
1981+
) -> bool {
1982+
return (
1983+
current_scope->get_administration_spending().get_value() >
1984+
current_scope->get_education_spending().get_value()
1985+
) == argument;
1986+
}
1987+
)
1988+
)
20231989
);
20241990
ret &= add_condition(
20251991
"crisis_exist",

0 commit comments

Comments
 (0)