Skip to content

Commit 2423a90

Browse files
committed
move evaluate_leaf out of the types
1 parent 08bc046 commit 2423a90

File tree

9 files changed

+129
-157
lines changed

9 files changed

+129
-157
lines changed

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include "openvic-simulation/utility/Containers.hpp"
5252
#include "openvic-simulation/utility/Logger.hpp"
5353
#include "openvic-simulation/core/Typedefs.hpp"
54-
#include "openvic-simulation/scripts/Condition.hpp"
5554

5655
using namespace OpenVic;
5756

@@ -1224,125 +1223,6 @@ void CountryInstance::start_research(Technology const& technology, const Date to
12241223
_update_current_tech(today);
12251224
}
12261225

1227-
bool CountryInstance::evaluate_leaf(ConditionNode const& node) const {
1228-
std::string_view const& id = node.get_condition()->get_identifier();
1229-
1230-
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Country_Scope Implement all of these
1231-
1232-
if (id == "ai") {
1233-
bool expected = std::get<bool>(node.get_value());
1234-
return is_ai() == expected;
1235-
}
1236-
1237-
if (id == "average_consciousness") {
1238-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1239-
return get_average_consciousness() >= expected;
1240-
}
1241-
1242-
if (id == "average_militancy") {
1243-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1244-
return get_average_militancy() >= expected;
1245-
}
1246-
1247-
if (id == "badboy") {
1248-
fixed_point_t expected_ratio = std::get<fixed_point_t>(node.get_value());
1249-
return get_infamy_untracked() >= (expected_ratio * fixed_point_t(25));
1250-
}
1251-
1252-
if (id == "civilized") {
1253-
bool expected = std::get<bool>(node.get_value());
1254-
return is_civilised() == expected;
1255-
}
1256-
1257-
if (id == "colonial_nation") {
1258-
bool expected = std::get<bool>(node.get_value());
1259-
return is_colonial(colony_status_t::COLONY) == expected;
1260-
}
1261-
1262-
if (id == "exists") {
1263-
bool expected = std::get<bool>(node.get_value());
1264-
return exists() == expected;
1265-
}
1266-
1267-
if (id == "industrial_score") {
1268-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1269-
return get_industrial_power_untracked() >= expected;
1270-
}
1271-
1272-
if (id == "is_disarmed") {
1273-
bool expected = std::get<bool>(node.get_value());
1274-
return is_disarmed() == expected;
1275-
}
1276-
1277-
if (id == "is_greater_power") {
1278-
bool expected = std::get<bool>(node.get_value());
1279-
return is_great_power() == expected;
1280-
}
1281-
1282-
if (id == "is_mobilised") {
1283-
bool expected = std::get<bool>(node.get_value());
1284-
return is_mobilised() == expected;
1285-
}
1286-
1287-
if (id == "is_secondary_power") {
1288-
bool expected = std::get<bool>(node.get_value());
1289-
return is_secondary_power() == expected;
1290-
}
1291-
1292-
if (id == "num_of_cities") {
1293-
uint64_t expected = std::get<uint64_t>(node.get_value());
1294-
return get_owned_provinces().size() >= expected;
1295-
}
1296-
1297-
if (id == "num_of_ports") {
1298-
uint64_t expected = std::get<uint64_t>(node.get_value());
1299-
return get_port_count() >= expected;
1300-
}
1301-
1302-
if (id == "number_of_states") {
1303-
uint64_t expected = std::get<uint64_t>(node.get_value());
1304-
return get_states().size() >= expected;
1305-
}
1306-
1307-
if (id == "prestige") {
1308-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1309-
return get_prestige_untracked() >= expected;
1310-
}
1311-
1312-
if (id == "plurality") {
1313-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1314-
return get_plurality_untracked() >= expected;
1315-
}
1316-
1317-
if (id == "total_amount_of_ships") {
1318-
uint64_t expected = std::get<uint64_t>(node.get_value());
1319-
return get_ship_count() >= expected;
1320-
}
1321-
1322-
if (id == "rank") {
1323-
uint64_t expected = std::get<uint64_t>(node.get_value());
1324-
return get_total_rank() >= expected;
1325-
}
1326-
1327-
if (id == "tag") {
1328-
memory::string const& expected = std::get<memory::string>(node.get_value());
1329-
return country_definition.get_identifier() == expected;
1330-
}
1331-
1332-
if (id == "war") {
1333-
bool expected = std::get<bool>(node.get_value());
1334-
return is_at_war() == expected;
1335-
}
1336-
1337-
if (id == "war_exhaustion") {
1338-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1339-
return get_war_exhaustion() >= expected;
1340-
}
1341-
1342-
spdlog::warn_s("Condition {} not implemented in CountryInstance::evaluate_leaf", node.get_condition() ? node.get_condition()->get_identifier() : "NULL");
1343-
return false;
1344-
}
1345-
13461226
void CountryInstance::apply_foreign_investments(
13471227
fixed_point_map_t<CountryDefinition const*> const& investments, CountryInstanceManager const& country_instance_manager
13481228
) {

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,6 @@ namespace OpenVic {
607607
[[nodiscard]] bool can_research_tech(Technology const& technology, const Date today) const;
608608
void start_research(Technology const& technology, const Date today);
609609

610-
bool evaluate_leaf(ConditionNode const& node) const;
611-
612610
// Sets the investment of each country in the map (rather than adding to them), leaving the rest unchanged.
613611
void apply_foreign_investments(
614612
fixed_point_map_t<CountryDefinition const*> const& investments,

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "openvic-simulation/misc/GameRulesManager.hpp"
1717
#include "openvic-simulation/modifier/StaticModifierCache.hpp"
1818
#include "openvic-simulation/types/TypedIndices.hpp"
19-
#include "openvic-simulation/scripts/Condition.hpp"
2019

2120
using namespace OpenVic;
2221

@@ -404,11 +403,6 @@ void ProvinceInstance::province_tick(
404403
rgo.rgo_tick(reusable_vectors[0]);
405404
}
406405

407-
bool ProvinceInstance::evaluate_leaf(ConditionNode const& node) const {
408-
// TODO: implement
409-
return false;
410-
}
411-
412406
bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup& group) {
413407
using enum unit_branch_t;
414408

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ namespace OpenVic {
224224
> reusable_vectors
225225
);
226226

227-
bool evaluate_leaf(ConditionNode const& node) const;
228-
229227
bool add_unit_instance_group(UnitInstanceGroup& group);
230228
bool remove_unit_instance_group(UnitInstanceGroup const& group);
231229

src/openvic-simulation/map/State.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "openvic-simulation/population/PopType.hpp"
1111
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
1212
#include "openvic-simulation/utility/Containers.hpp"
13-
#include "openvic-simulation/scripts/Condition.hpp"
1413

1514
using namespace OpenVic;
1615

@@ -96,13 +95,6 @@ void State::update_gamestate() {
9695
_update_country();
9796
}
9897

99-
bool State::evaluate_leaf(ConditionNode const& node) const {
100-
std::string_view const& id = node.get_condition()->get_identifier();
101-
102-
spdlog::warn_s("Condition {} not implemented in State::evaluate_leaf", id);
103-
return false;
104-
}
105-
10698
void State::_update_country() {
10799
CountryInstance* const owner_ptr = get_owner();
108100
if (owner_ptr == previous_country_ptr) {

src/openvic-simulation/map/State.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ namespace OpenVic {
6868
}
6969

7070
void update_gamestate();
71-
72-
bool evaluate_leaf(ConditionNode const& node) const;
7371
};
7472

7573
struct Region;

src/openvic-simulation/population/Pop.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include "openvic-simulation/utility/Containers.hpp"
4444
#include "openvic-simulation/utility/Logger.hpp"
4545
#include "openvic-simulation/core/Typedefs.hpp"
46-
#include "openvic-simulation/scripts/Condition.hpp"
4746

4847

4948
using namespace OpenVic;
@@ -468,18 +467,6 @@ void Pop::allocate_for_needs(
468467
reusable_vector.clear();
469468
}
470469

471-
bool Pop::evaluate_leaf(ConditionNode const& node) const {
472-
std::string_view const& id = node.get_condition()->get_identifier();
473-
474-
if (id == "consciousness") {
475-
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
476-
return get_consciousness() >= expected;
477-
}
478-
// TODO: Implement the rest
479-
spdlog::warn_s("Condition {} not implemented in Pop::evaluate_leaf", id);
480-
return false;
481-
}
482-
483470
void Pop::pop_tick(
484471
PopValuesFromProvince const& shared_values,
485472
RandomU32& random_number_generator,

src/openvic-simulation/population/Pop.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ namespace OpenVic {
226226
DECLARE_POP_MONEY_STORE_FUNCTIONS(import_subsidies)
227227
#undef DECLARE_POP_MONEY_STORE_FUNCTIONS
228228

229-
bool evaluate_leaf(ConditionNode const& node) const;
230-
231229
void pop_tick(
232230
PopValuesFromProvince const& shared_values,
233231
RandomU32& random_number_generator,

src/openvic-simulation/scripts/Context.cpp

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,139 @@ std::string_view Context::get_identifier() const {
5151
}
5252

5353
bool Context::evaluate_leaf(ConditionNode const& node) const {
54+
std::string_view const id = node.get_condition()->get_identifier();
55+
5456
return std::visit(
5557
[&](auto const* p) -> bool {
5658
if (!p) {
5759
return false;
5860
}
59-
return p->evaluate_leaf(node);
61+
62+
using T = std::decay_t<decltype(*p)>;
63+
64+
if constexpr (std::is_same_v<T, CountryInstance>) {
65+
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Country_Scope
66+
67+
if (id == "ai") {
68+
bool expected = std::get<bool>(node.get_value());
69+
return p->is_ai() == expected;
70+
}
71+
if (id == "average_consciousness") {
72+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
73+
return p->get_average_consciousness() >= expected;
74+
}
75+
if (id == "average_militancy") {
76+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
77+
return p->get_average_militancy() >= expected;
78+
}
79+
if (id == "badboy") {
80+
fixed_point_t expected_ratio = std::get<fixed_point_t>(node.get_value());
81+
return p->get_infamy_untracked() >= (expected_ratio * fixed_point_t(25));
82+
}
83+
if (id == "civilized") {
84+
bool expected = std::get<bool>(node.get_value());
85+
return p->is_civilised() == expected;
86+
}
87+
if (id == "colonial_nation") {
88+
bool expected = std::get<bool>(node.get_value());
89+
bool has_colonies = false;
90+
for (State const* state : p->get_states()) {
91+
if (state && state->is_colonial_state()) {
92+
has_colonies = true;
93+
break;
94+
}
95+
}
96+
return has_colonies == expected;
97+
}
98+
if (id == "exists") {
99+
bool expected = std::get<bool>(node.get_value());
100+
return p->exists() == expected;
101+
}
102+
if (id == "industrial_score") {
103+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
104+
return p->get_industrial_power_untracked() >= expected;
105+
}
106+
if (id == "is_disarmed") {
107+
bool expected = std::get<bool>(node.get_value());
108+
return p->is_disarmed() == expected;
109+
}
110+
if (id == "is_greater_power") {
111+
bool expected = std::get<bool>(node.get_value());
112+
return p->is_great_power() == expected;
113+
}
114+
if (id == "is_mobilised") {
115+
bool expected = std::get<bool>(node.get_value());
116+
return p->is_mobilised() == expected;
117+
}
118+
if (id == "is_secondary_power") {
119+
bool expected = std::get<bool>(node.get_value());
120+
return p->is_secondary_power() == expected;
121+
}
122+
if (id == "num_of_cities") {
123+
uint64_t expected = std::get<uint64_t>(node.get_value());
124+
return p->get_owned_provinces().size() >= expected;
125+
}
126+
if (id == "num_of_ports") {
127+
uint64_t expected = std::get<uint64_t>(node.get_value());
128+
return p->get_port_count() >= expected;
129+
}
130+
if (id == "number_of_states") {
131+
uint64_t expected = std::get<uint64_t>(node.get_value());
132+
return p->get_states().size() >= expected;
133+
}
134+
if (id == "prestige") {
135+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
136+
return p->get_prestige_untracked() >= expected;
137+
}
138+
if (id == "plurality") {
139+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
140+
return p->get_plurality_untracked() >= expected;
141+
}
142+
if (id == "total_amount_of_ships") {
143+
uint64_t expected = std::get<uint64_t>(node.get_value());
144+
return p->get_ship_count() >= expected;
145+
}
146+
if (id == "rank") {
147+
uint64_t expected = std::get<uint64_t>(node.get_value());
148+
return p->get_total_rank() >= expected;
149+
}
150+
if (id == "tag") {
151+
memory::string const& expected = std::get<memory::string>(node.get_value());
152+
return p->get_identifier() == expected;
153+
}
154+
if (id == "war") {
155+
bool expected = std::get<bool>(node.get_value());
156+
return p->is_at_war() == expected;
157+
}
158+
if (id == "war_exhaustion") {
159+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
160+
return p->get_war_exhaustion() >= expected;
161+
}
162+
163+
spdlog::warn_s("Condition {} not implemented for Country scope", id);
164+
return false;
165+
}
166+
else if constexpr (std::is_same_v<T, State>) {
167+
// No state conditions according to wiki?
168+
spdlog::warn_s("Condition {} not implemented for State scope", id);
169+
return false;
170+
}
171+
else if constexpr (std::is_same_v<T, ProvinceInstance>) {
172+
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Province_Scope
173+
spdlog::warn_s("Condition {} not implemented for Province scope", id);
174+
return false;
175+
}
176+
else if constexpr (std::is_same_v<T, Pop>) {
177+
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Pop_Scope
178+
if (id == "consciousness") {
179+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
180+
return p->get_consciousness() >= expected;
181+
}
182+
spdlog::warn_s("Condition {} not implemented for Pop scope", id);
183+
return false;
184+
}
185+
186+
return false;
60187
},
61188
ptr
62189
);
@@ -79,7 +206,7 @@ std::vector<Context> Context::get_sub_contexts(std::string_view condition_id, sc
79206
result.emplace_back(make_child(prov));
80207
}
81208
}
82-
else if (condition_id == "any_core" || condition_id == "all_core") {
209+
else if (condition_id == "any_core_province" || condition_id == "all_core") {
83210
for (ProvinceInstance const* prov : country->get_core_provinces()) {
84211
result.emplace_back(make_child(prov));
85212
}

0 commit comments

Comments
 (0)