Skip to content

Commit a2eada4

Browse files
committed
multiple wvpm suggestions
1 parent 7a6e146 commit a2eada4

2 files changed

Lines changed: 200 additions & 188 deletions

File tree

src/openvic-simulation/scripts/Context.cpp

Lines changed: 150 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -8,198 +8,184 @@
88
#include "openvic-simulation/InstanceManager.hpp"
99

1010
using namespace OpenVic;
11-
scope_type_t Context::get_scope_type() const {
12-
return std::visit(
13-
[](auto* p) -> scope_type_t {
14-
using T = std::decay_t<decltype(*p)>;
15-
16-
if constexpr (std::is_same_v<T, CountryInstance>) {
17-
return scope_type_t::COUNTRY;
18-
} else if constexpr (std::is_same_v<T, ProvinceInstance>) {
19-
return scope_type_t::PROVINCE;
20-
} else if constexpr (std::is_same_v<T, State>) {
21-
return scope_type_t::STATE;
22-
} else if constexpr (std::is_same_v<T, Pop>) {
23-
return scope_type_t::POP;
24-
}
25-
26-
return scope_type_t::NO_SCOPE;
27-
},
28-
ptr
29-
);
30-
}
31-
3211
std::string_view Context::get_identifier() const {
33-
return std::visit([](auto const* p) -> std::string_view {
34-
if (!p) return "";
35-
12+
return std::visit([](auto const& p) -> std::string_view {
3613
using T = std::decay_t<decltype(p)>;
3714

38-
if constexpr (std::is_same_v<T, CountryInstance const*>) {
39-
return p->get_identifier();
40-
}
41-
else if constexpr (std::is_same_v<T, ProvinceInstance const*>) {
42-
return p->get_identifier();
43-
}
44-
/*else if constexpr (std::is_same_v<T, State const*>) {
45-
return p->get_identifier();
46-
}*/
47-
else if constexpr (std::is_same_v<T, Pop const*>) {
15+
if constexpr (std::is_same_v<T, std::monostate>) {
4816
return "";
49-
}
17+
} else {
18+
if (!p) return "";
5019

51-
return "";
20+
if constexpr (std::is_same_v<T, CountryInstance const*>) {
21+
return p->get_identifier();
22+
}
23+
else if constexpr (std::is_same_v<T, ProvinceInstance const*>) {
24+
return p->get_identifier();
25+
}
26+
/*else if constexpr (std::is_same_v<T, State const*>) {
27+
return p->get_identifier();
28+
}*/
29+
else if constexpr (std::is_same_v<T, Pop const*>) {
30+
return "";
31+
}
32+
else {
33+
return "";
34+
}
35+
}
5236
}, ptr);
5337
}
5438

5539
bool Context::evaluate_leaf(ConditionNode const& node) const {
5640
std::string_view const id = node.get_condition()->get_identifier();
5741

5842
return std::visit(
59-
[&](auto const* p) -> bool {
60-
if (!p) {
61-
return false;
62-
}
43+
[&](auto const& p) -> bool {
44+
using Ptr = std::decay_t<decltype(p)>;
6345

64-
using T = std::decay_t<decltype(*p)>;
46+
if constexpr (std::is_same_v<Ptr, std::monostate>) {
47+
return false;
48+
} else {
49+
using T = std::decay_t<decltype(*p)>;
6550

66-
if constexpr (std::is_same_v<T, CountryInstance>) {
67-
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Country_Scope
51+
if constexpr (std::is_same_v<T, CountryInstance>) {
52+
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Country_Scope
6853

69-
if (id == "ai") {
70-
bool expected = std::get<bool>(node.get_value());
71-
return p->is_ai() == expected;
72-
}
73-
if (id == "average_consciousness") {
74-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
75-
return p->get_average_consciousness() >= expected;
76-
}
77-
if (id == "average_militancy") {
78-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
79-
return p->get_average_militancy() >= expected;
80-
}
81-
if (id == "badboy") {
82-
fixed_point_t expected_ratio = std::get<fixed_point_t>(node.get_value());
83-
return p->get_infamy_untracked() >= expected_ratio * definition_manager.get_define_manager().get_country_defines().get_infamy_containment_limit();
84-
}
85-
if (id == "civilized") {
86-
bool expected = std::get<bool>(node.get_value());
87-
return p->is_civilised() == expected;
88-
}
89-
if (id == "colonial_nation") {
90-
bool expected = std::get<bool>(node.get_value());
91-
bool has_colonies = false;
92-
for (State const* state : p->get_states()) {
93-
if (state && state->is_colonial_state()) {
94-
has_colonies = true;
95-
break;
54+
if (id == "ai") {
55+
bool expected = std::get<bool>(node.get_value());
56+
return p->is_ai() == expected;
57+
}
58+
if (id == "average_consciousness") {
59+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
60+
return p->get_average_consciousness() >= expected;
61+
}
62+
if (id == "average_militancy") {
63+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
64+
return p->get_average_militancy() >= expected;
65+
}
66+
if (id == "badboy") {
67+
fixed_point_t expected_ratio = std::get<fixed_point_t>(node.get_value());
68+
return p->get_infamy_untracked() >= expected_ratio * definition_manager.get_define_manager().get_country_defines().get_infamy_containment_limit();
69+
}
70+
if (id == "civilized") {
71+
bool expected = std::get<bool>(node.get_value());
72+
return p->is_civilised() == expected;
73+
}
74+
if (id == "colonial_nation") {
75+
bool expected = std::get<bool>(node.get_value());
76+
bool has_colonies = false;
77+
for (State const* state : p->get_states()) {
78+
if (state && state->is_colonial_state()) {
79+
has_colonies = true;
80+
break;
81+
}
9682
}
83+
return has_colonies == expected;
9784
}
98-
return has_colonies == expected;
99-
}
100-
if (id == "exists") {
101-
auto const& value = node.get_value();
102-
if (auto const* expected = std::get_if<bool>(&value)) {
103-
return p->exists() == *expected;
85+
if (id == "exists") {
86+
auto const& value = node.get_value();
87+
if (auto const* expected = std::get_if<bool>(&value)) {
88+
return p->exists() == *expected;
89+
}
90+
if (auto const* tag = std::get_if<memory::string>(&value)) {
91+
CountryInstance const* country_instance = instance_manager.get_country_instance_manager().get_country_instance_by_identifier(*tag);
92+
return country_instance != nullptr && country_instance->exists();
93+
}
94+
return false;
95+
}
96+
if (id == "industrial_score") {
97+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
98+
return p->get_industrial_power_untracked() >= expected;
99+
}
100+
if (id == "is_disarmed") {
101+
bool expected = std::get<bool>(node.get_value());
102+
return p->is_disarmed() == expected;
103+
}
104+
if (id == "is_greater_power") {
105+
bool expected = std::get<bool>(node.get_value());
106+
return p->is_great_power() == expected;
104107
}
105-
if (auto const* tag = std::get_if<memory::string>(&value)) {
106-
CountryInstance const* country_instance = instance_manager.get_country_instance_manager().get_country_instance_by_identifier(*tag);
107-
return country_instance != nullptr && country_instance->exists();
108+
if (id == "is_mobilised") {
109+
bool expected = std::get<bool>(node.get_value());
110+
return p->is_mobilised() == expected;
108111
}
112+
if (id == "is_secondary_power") {
113+
bool expected = std::get<bool>(node.get_value());
114+
return p->is_secondary_power() == expected;
115+
}
116+
if (id == "num_of_cities") {
117+
uint64_t expected = std::get<uint64_t>(node.get_value());
118+
return p->get_owned_provinces().size() >= expected;
119+
}
120+
if (id == "num_of_ports") {
121+
uint64_t expected = std::get<uint64_t>(node.get_value());
122+
return p->get_port_count() >= expected;
123+
}
124+
if (id == "number_of_states") {
125+
uint64_t expected = std::get<uint64_t>(node.get_value());
126+
return p->get_states().size() >= expected;
127+
}
128+
if (id == "prestige") {
129+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
130+
return p->get_prestige_untracked() >= expected;
131+
}
132+
if (id == "plurality") {
133+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
134+
return p->get_plurality_untracked() >= expected;
135+
}
136+
if (id == "total_amount_of_ships") {
137+
uint64_t expected = std::get<uint64_t>(node.get_value());
138+
return p->get_ship_count() >= expected;
139+
}
140+
if (id == "rank") {
141+
uint64_t expected = std::get<uint64_t>(node.get_value());
142+
return p->get_total_rank() >= expected;
143+
}
144+
if (id == "tag") {
145+
memory::string const& expected = std::get<memory::string>(node.get_value());
146+
return p->get_identifier() == expected;
147+
}
148+
if (id == "war") {
149+
bool expected = std::get<bool>(node.get_value());
150+
return p->is_at_war() == expected;
151+
}
152+
if (id == "war_exhaustion") {
153+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
154+
return p->get_war_exhaustion() >= expected;
155+
}
156+
157+
spdlog::warn_s("Condition {} not implemented for Country scope", id);
109158
return false;
110159
}
111-
if (id == "industrial_score") {
112-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
113-
return p->get_industrial_power_untracked() >= expected;
114-
}
115-
if (id == "is_disarmed") {
116-
bool expected = std::get<bool>(node.get_value());
117-
return p->is_disarmed() == expected;
118-
}
119-
if (id == "is_greater_power") {
120-
bool expected = std::get<bool>(node.get_value());
121-
return p->is_great_power() == expected;
122-
}
123-
if (id == "is_mobilised") {
124-
bool expected = std::get<bool>(node.get_value());
125-
return p->is_mobilised() == expected;
126-
}
127-
if (id == "is_secondary_power") {
128-
bool expected = std::get<bool>(node.get_value());
129-
return p->is_secondary_power() == expected;
130-
}
131-
if (id == "num_of_cities") {
132-
uint64_t expected = std::get<uint64_t>(node.get_value());
133-
return p->get_owned_provinces().size() >= expected;
134-
}
135-
if (id == "num_of_ports") {
136-
uint64_t expected = std::get<uint64_t>(node.get_value());
137-
return p->get_port_count() >= expected;
138-
}
139-
if (id == "number_of_states") {
140-
uint64_t expected = std::get<uint64_t>(node.get_value());
141-
return p->get_states().size() >= expected;
142-
}
143-
if (id == "prestige") {
144-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
145-
return p->get_prestige_untracked() >= expected;
146-
}
147-
if (id == "plurality") {
148-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
149-
return p->get_plurality_untracked() >= expected;
150-
}
151-
if (id == "total_amount_of_ships") {
152-
uint64_t expected = std::get<uint64_t>(node.get_value());
153-
return p->get_ship_count() >= expected;
154-
}
155-
if (id == "rank") {
156-
uint64_t expected = std::get<uint64_t>(node.get_value());
157-
return p->get_total_rank() >= expected;
158-
}
159-
if (id == "tag") {
160-
memory::string const& expected = std::get<memory::string>(node.get_value());
161-
return p->get_identifier() == expected;
160+
else if constexpr (std::is_same_v<T, State>) {
161+
// No state conditions according to wiki?
162+
spdlog::warn_s("Condition {} not implemented for State scope", id);
163+
return false;
162164
}
163-
if (id == "war") {
164-
bool expected = std::get<bool>(node.get_value());
165-
return p->is_at_war() == expected;
165+
else if constexpr (std::is_same_v<T, ProvinceInstance>) {
166+
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Province_Scope
167+
spdlog::warn_s("Condition {} not implemented for Province scope", id);
168+
return false;
166169
}
167-
if (id == "war_exhaustion") {
168-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
169-
return p->get_war_exhaustion() >= expected;
170+
else if constexpr (std::is_same_v<T, Pop>) {
171+
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Pop_Scope
172+
if (id == "consciousness") {
173+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
174+
return p->get_consciousness() >= expected;
175+
}
176+
spdlog::warn_s("Condition {} not implemented for Pop scope", id);
177+
return false;
170178
}
171179

172-
spdlog::warn_s("Condition {} not implemented for Country scope", id);
173-
return false;
174-
}
175-
else if constexpr (std::is_same_v<T, State>) {
176-
// No state conditions according to wiki?
177-
spdlog::warn_s("Condition {} not implemented for State scope", id);
178-
return false;
179-
}
180-
else if constexpr (std::is_same_v<T, ProvinceInstance>) {
181-
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Province_Scope
182-
spdlog::warn_s("Condition {} not implemented for Province scope", id);
183180
return false;
184181
}
185-
else if constexpr (std::is_same_v<T, Pop>) {
186-
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Pop_Scope
187-
if (id == "consciousness") {
188-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
189-
return p->get_consciousness() >= expected;
190-
}
191-
spdlog::warn_s("Condition {} not implemented for Pop scope", id);
192-
return false;
193-
}
194-
195-
return false;
196182
},
197183
ptr
198184
);
199185
}
200186

201-
std::vector<Context> Context::get_sub_contexts(std::string_view condition_id, scope_type_t target) const {
202-
std::vector<Context> result;
187+
memory::vector<Context> Context::get_sub_contexts(std::string_view condition_id, scope_type_t target) const {
188+
memory::vector<Context> result;
203189

204190
if (std::holds_alternative<CountryInstance const*>(ptr)) {
205191
CountryInstance const* country = std::get<CountryInstance const*>(ptr);
@@ -240,15 +226,15 @@ std::vector<Context> Context::get_sub_contexts(std::string_view condition_id, sc
240226

241227
std::optional<Context> Context::get_redirect_context(std::string_view condition_id, scope_type_t target) const {
242228
if (target == scope_type_t::THIS) {
243-
if (this_scope != nullptr) {
244-
return *this_scope;
229+
if (!std::holds_alternative<std::monostate>(this_scope)) {
230+
return Context(this_scope, definition_manager, instance_manager, this_scope, from_scope);
245231
}
246232
return std::nullopt;
247233
}
248234

249235
if (target == scope_type_t::FROM) {
250-
if (from_scope != nullptr) {
251-
return *from_scope;
236+
if (!std::holds_alternative<std::monostate>(from_scope)) {
237+
return Context(from_scope, definition_manager, instance_manager, this_scope, from_scope);
252238
}
253239
return std::nullopt;
254240
}

0 commit comments

Comments
 (0)