Skip to content

Commit eb5ef0e

Browse files
committed
condition evaluation wip
1 parent 2775e5c commit eb5ef0e

File tree

12 files changed

+742
-165
lines changed

12 files changed

+742
-165
lines changed

src/openvic-simulation/country/CountryInstance.cpp

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

5455
using namespace OpenVic;
5556

@@ -1178,6 +1179,125 @@ void CountryInstance::start_research(Technology const& technology, const Date to
11781179
_update_current_tech(today);
11791180
}
11801181

1182+
bool CountryInstance::evaluate_leaf(ConditionNode const& node) const {
1183+
std::string_view const& id = node.get_condition()->get_identifier();
1184+
1185+
// TODO: https://vic2.paradoxwikis.com/List_of_conditions#Country_Scope Implement all of these
1186+
1187+
if (id == "ai") {
1188+
bool expected = std::get<bool>(node.get_value());
1189+
return is_ai() == expected;
1190+
}
1191+
1192+
if (id == "average_consciousness") {
1193+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1194+
return get_average_consciousness() >= expected;
1195+
}
1196+
1197+
if (id == "average_militancy") {
1198+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1199+
return get_average_militancy() >= expected;
1200+
}
1201+
1202+
if (id == "badboy") {
1203+
fixed_point_t expected_ratio = std::get<fixed_point_t>(node.get_value());
1204+
return get_infamy_untracked() >= (expected_ratio * fixed_point_t(25));
1205+
}
1206+
1207+
if (id == "civilized") {
1208+
bool expected = std::get<bool>(node.get_value());
1209+
return is_civilised() == expected;
1210+
}
1211+
1212+
if (id == "colonial_nation") {
1213+
bool expected = std::get<bool>(node.get_value());
1214+
return is_colonial(colony_status_t::COLONY) == expected;
1215+
}
1216+
1217+
if (id == "exists") {
1218+
bool expected = std::get<bool>(node.get_value());
1219+
return exists() == expected;
1220+
}
1221+
1222+
if (id == "industrial_score") {
1223+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1224+
return get_industrial_power_untracked() >= expected;
1225+
}
1226+
1227+
if (id == "is_disarmed") {
1228+
bool expected = std::get<bool>(node.get_value());
1229+
return is_disarmed() == expected;
1230+
}
1231+
1232+
if (id == "is_greater_power") {
1233+
bool expected = std::get<bool>(node.get_value());
1234+
return is_great_power() == expected;
1235+
}
1236+
1237+
if (id == "is_mobilised") {
1238+
bool expected = std::get<bool>(node.get_value());
1239+
return is_mobilised() == expected;
1240+
}
1241+
1242+
if (id == "is_secondary_power") {
1243+
bool expected = std::get<bool>(node.get_value());
1244+
return is_secondary_power() == expected;
1245+
}
1246+
1247+
if (id == "num_of_cities") {
1248+
uint64_t expected = std::get<uint64_t>(node.get_value());
1249+
return get_owned_provinces().size() >= expected;
1250+
}
1251+
1252+
if (id == "num_of_ports") {
1253+
uint64_t expected = std::get<uint64_t>(node.get_value());
1254+
return get_port_count() >= expected;
1255+
}
1256+
1257+
if (id == "number_of_states") {
1258+
uint64_t expected = std::get<uint64_t>(node.get_value());
1259+
return get_states().size() >= expected;
1260+
}
1261+
1262+
if (id == "prestige") {
1263+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1264+
return get_prestige_untracked() >= expected;
1265+
}
1266+
1267+
if (id == "plurality") {
1268+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1269+
return get_plurality_untracked() >= expected;
1270+
}
1271+
1272+
if (id == "total_amount_of_ships") {
1273+
uint64_t expected = std::get<uint64_t>(node.get_value());
1274+
return get_ship_count() >= expected;
1275+
}
1276+
1277+
if (id == "rank") {
1278+
uint64_t expected = std::get<uint64_t>(node.get_value());
1279+
return get_total_rank() >= expected;
1280+
}
1281+
1282+
if (id == "tag") {
1283+
memory::string const& expected = std::get<memory::string>(node.get_value());
1284+
return country_definition.get_identifier() == expected;
1285+
}
1286+
1287+
if (id == "war") {
1288+
bool expected = std::get<bool>(node.get_value());
1289+
return is_at_war() == expected;
1290+
}
1291+
1292+
if (id == "war_exhaustion") {
1293+
fixed_point_t expected = std::get<fixed_point_t>(node.get_value());
1294+
return get_war_exhaustion() >= expected;
1295+
}
1296+
1297+
spdlog::warn_s("Condition {} not implemented in CountryInstance::evaluate_leaf", node.get_condition() ? node.get_condition()->get_identifier() : "NULL");
1298+
return false;
1299+
}
1300+
11811301
void CountryInstance::apply_foreign_investments(
11821302
fixed_point_map_t<CountryDefinition const*> const& investments, CountryInstanceManager const& country_instance_manager
11831303
) {

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ namespace OpenVic {
7575
struct TechnologySchool;
7676
struct UnitInstanceGroup;
7777
struct UnitTypeManager;
78+
struct ConditionNode;
7879

7980
static constexpr Timespan RECENT_WAR_LOSS_TIME_LIMIT = Timespan::from_years(5);
8081

@@ -603,6 +604,8 @@ namespace OpenVic {
603604
bool can_research_tech(Technology const& technology, const Date today) const;
604605
void start_research(Technology const& technology, const Date today);
605606

607+
bool evaluate_leaf(ConditionNode const& node) const;
608+
606609
// Sets the investment of each country in the map (rather than adding to them), leaving the rest unchanged.
607610
void apply_foreign_investments(
608611
fixed_point_map_t<CountryDefinition const*> const& investments,

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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"
1920

2021
using namespace OpenVic;
2122

@@ -399,6 +400,11 @@ void ProvinceInstance::province_tick(
399400
rgo.rgo_tick(reusable_vectors[0]);
400401
}
401402

403+
bool ProvinceInstance::evaluate_leaf(ConditionNode const& node) const {
404+
// TODO: implement
405+
return false;
406+
}
407+
402408
bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup& group) {
403409
using enum unit_branch_t;
404410

src/openvic-simulation/map/ProvinceInstance.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ namespace OpenVic {
217217
> reusable_vectors
218218
);
219219

220+
bool evaluate_leaf(ConditionNode const& node) const;
221+
220222
bool add_unit_instance_group(UnitInstanceGroup& group);
221223
bool remove_unit_instance_group(UnitInstanceGroup const& group);
222224

src/openvic-simulation/map/State.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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"
1314

1415
using namespace OpenVic;
1516

@@ -95,6 +96,13 @@ void State::update_gamestate() {
9596
_update_country();
9697
}
9798

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+
98106
void State::_update_country() {
99107
CountryInstance* const owner_ptr = get_owner();
100108
if (owner_ptr == previous_country_ptr) {

src/openvic-simulation/map/State.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace OpenVic {
2525
struct StateManager;
2626
struct StateSet;
2727
struct Strata;
28+
struct ConditionNode;
2829

2930
struct State : PopsAggregate {
3031
friend struct StateManager;
@@ -67,6 +68,8 @@ namespace OpenVic {
6768
}
6869

6970
void update_gamestate();
71+
72+
bool evaluate_leaf(ConditionNode const& node) const;
7073
};
7174

7275
struct Region;

src/openvic-simulation/population/Pop.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "openvic-simulation/types/TypedIndices.hpp"
4343
#include "openvic-simulation/utility/Containers.hpp"
4444
#include "openvic-simulation/utility/Logger.hpp"
45+
#include "openvic-simulation/core/Typedefs.hpp"
46+
#include "openvic-simulation/scripts/Condition.hpp"
4547

4648

4749
using namespace OpenVic;
@@ -466,6 +468,18 @@ void Pop::allocate_for_needs(
466468
reusable_vector.clear();
467469
}
468470

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+
469483
void Pop::pop_tick(
470484
PopValuesFromProvince const& shared_values,
471485
RandomU32& random_number_generator,

src/openvic-simulation/population/Pop.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace OpenVic {
3535
struct RebelType;
3636
struct Religion;
3737
struct SellResult;
38+
struct ConditionNode;
3839

3940
struct PopBase {
4041
friend PopManager;
@@ -225,6 +226,8 @@ namespace OpenVic {
225226
DECLARE_POP_MONEY_STORE_FUNCTIONS(import_subsidies)
226227
#undef DECLARE_POP_MONEY_STORE_FUNCTIONS
227228

229+
bool evaluate_leaf(ConditionNode const& node) const;
230+
228231
void pop_tick(
229232
PopValuesFromProvince const& shared_values,
230233
RandomU32& random_number_generator,

0 commit comments

Comments
 (0)