Skip to content

Commit 6a9e542

Browse files
committed
Country script variables
1 parent 048631e commit 6a9e542

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

src/openvic-simulation/economy/production/ProductionType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ bool ProductionTypeManager::load_production_types_file(
273273
"bonus", ZERO_OR_MORE, [&bonuses](ast::NodeCPtr bonus_node) -> bool {
274274
using enum scope_type_t;
275275

276-
ConditionScript trigger { PROVINCE, NO_SCOPE, NO_SCOPE };
276+
ConditionScript trigger { STATE, NO_SCOPE, NO_SCOPE };
277277
fixed_point_t bonus_value {};
278278

279279
const bool ret = expect_dictionary_keys(

src/openvic-simulation/military/Wargoal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool WargoalTypeManager::load_wargoal_file(ovdl::v2script::Parser const& parser)
9898
ConditionScript can_use { COUNTRY, COUNTRY, COUNTRY };
9999
ConditionScript is_valid { COUNTRY, COUNTRY, COUNTRY };
100100
ConditionScript allowed_states { STATE, COUNTRY, COUNTRY };
101-
ConditionScript allowed_substate_regions { PROVINCE, COUNTRY, COUNTRY };
101+
ConditionScript allowed_substate_regions { STATE, COUNTRY, COUNTRY };
102102
ConditionScript allowed_states_in_crisis { STATE, COUNTRY, COUNTRY };
103103
ConditionScript allowed_countries { COUNTRY, COUNTRY, COUNTRY };
104104
EffectScript on_add, on_po_accepted; //country as default scope for both

src/openvic-simulation/scripts/Condition.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,20 +1967,19 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
19671967
);
19681968
ret &= add_condition(
19691969
"check_variable",
1970-
/* TODO - complex:
1971-
* - does this have any scope restrictions, and does it affect scope in any way? The wiki warns that this doesn't
1972-
* work from province scope.
1973-
* - value is a dictionary with two entries:
1974-
* - which = <name>
1975-
* - the name of the variable being checked, similar to the name of a global/country/province flag
1976-
* - do flags and variables interact in any way? what happens if you create a flag and variable with the same name?
1977-
* - are variables global or per country/province?
1978-
* - value = <number>
1979-
* - The number to compare the current variable value against. Returns true if the variable has been previously
1980-
* set and has a value greater than or equal to the number.
1981-
* - Can values be negative? Can they be non-integers? How big can they get? */
19821970
_parse_condition_node_value_callback<std::pair<std::string, fixed_point_t>, COUNTRY>,
1983-
_execute_condition_node_unimplemented
1971+
_execute_condition_node_cast_argument_callback<std::pair<std::string, fixed_point_t>, scope_t, scope_t, scope_t>(
1972+
_execute_condition_node_convert_scope<
1973+
CountryInstance, scope_t, scope_t, std::pair<std::string, fixed_point_t> const&
1974+
>(
1975+
[](
1976+
Condition const& condition, InstanceManager const& instance_manager, CountryInstance const* current_scope,
1977+
scope_t this_scope, scope_t from_scope, std::pair<std::string, fixed_point_t> const& argument
1978+
) -> bool {
1979+
return current_scope->get_script_variable(argument.first) >= argument.second;
1980+
}
1981+
)
1982+
)
19841983
);
19851984
ret &= add_condition(
19861985
"civilization_progress",
@@ -2330,7 +2329,8 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
23302329
return country->get_primary_culture();
23312330
}
23322331
constexpr Culture const* operator()(State const* state) const {
2333-
return (*this)(state->get_owner());
2332+
CountryInstance const* owner = state->get_owner();
2333+
return owner != nullptr ? (*this)(owner) : nullptr;
23342334
}
23352335
constexpr Culture const* operator()(ProvinceInstance const* province) const {
23362336
CountryInstance const* owner = province->get_owner();
@@ -2395,7 +2395,8 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
23952395
return country->get_religion();
23962396
}
23972397
constexpr Religion const* operator()(State const* state) const {
2398-
return (*this)(state->get_owner());
2398+
CountryInstance const* owner = state->get_owner();
2399+
return owner != nullptr ? (*this)(owner) : nullptr;
23992400
}
24002401
constexpr Religion const* operator()(ProvinceInstance const* province) const {
24012402
CountryInstance const* owner = province->get_owner();
@@ -2838,7 +2839,40 @@ bool ConditionManager::setup_conditions(DefinitionManager const& definition_mana
28382839
ret &= add_condition(
28392840
"money",
28402841
_parse_condition_node_value_callback<fixed_point_t, COUNTRY | POP>,
2841-
_execute_condition_node_unimplemented
2842+
_execute_condition_node_cast_argument_callback<fixed_point_t, scope_t, scope_t, scope_t>(
2843+
[](
2844+
Condition const& condition, InstanceManager const& instance_manager, scope_t current_scope, scope_t this_scope,
2845+
scope_t from_scope, fixed_point_t argument
2846+
) -> bool {
2847+
struct visitor_t {
2848+
2849+
Condition const& condition;
2850+
fixed_point_t const& argument;
2851+
2852+
bool operator()(no_scope_t no_scope) const {
2853+
Logger::error("Error executing condition \"", condition.get_identifier(), "\": no current scope!");
2854+
return false;
2855+
}
2856+
2857+
constexpr bool operator()(CountryInstance const* country) const {
2858+
return country->get_cash_stockpile() >= argument;
2859+
}
2860+
constexpr bool operator()(State const* state) const {
2861+
CountryInstance const* owner = state->get_owner();
2862+
return owner != nullptr && (*this)(owner);
2863+
}
2864+
constexpr bool operator()(ProvinceInstance const* province) const {
2865+
CountryInstance const* owner = province->get_owner();
2866+
return owner != nullptr && (*this)(owner);
2867+
}
2868+
constexpr bool operator()(Pop const* pop) const {
2869+
return pop->get_cash() >= argument;
2870+
}
2871+
};
2872+
2873+
return std::visit(visitor_t { condition, argument }, current_scope);
2874+
}
2875+
)
28422876
);
28432877
ret &= add_condition(
28442878
"nationalvalue",

0 commit comments

Comments
 (0)