Skip to content

Commit d553f4f

Browse files
committed
condition evaluation wip
1 parent 0a08ff2 commit d553f4f

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
@@ -51,6 +51,7 @@
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"
5455

5556
using namespace OpenVic;
5657

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

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+
12261346
void CountryInstance::apply_foreign_investments(
12271347
fixed_point_map_t<CountryDefinition const*> const& investments, CountryInstanceManager const& country_instance_manager
12281348
) {

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ namespace OpenVic {
7777
struct TechnologySchool;
7878
struct UnitInstanceGroup;
7979
struct UnitTypeManager;
80+
struct ConditionNode;
8081

8182
static constexpr Timespan RECENT_WAR_LOSS_TIME_LIMIT = Timespan::from_years(5);
8283

@@ -606,6 +607,8 @@ namespace OpenVic {
606607
[[nodiscard]] bool can_research_tech(Technology const& technology, const Date today) const;
607608
void start_research(Technology const& technology, const Date today);
608609

610+
bool evaluate_leaf(ConditionNode const& node) const;
611+
609612
// Sets the investment of each country in the map (rather than adding to them), leaving the rest unchanged.
610613
void apply_foreign_investments(
611614
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

@@ -403,6 +404,11 @@ void ProvinceInstance::province_tick(
403404
rgo.rgo_tick(reusable_vectors[0]);
404405
}
405406

407+
bool ProvinceInstance::evaluate_leaf(ConditionNode const& node) const {
408+
// TODO: implement
409+
return false;
410+
}
411+
406412
bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup& group) {
407413
using enum unit_branch_t;
408414

src/openvic-simulation/map/ProvinceInstance.hpp

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

227+
bool evaluate_leaf(ConditionNode const& node) const;
228+
227229
bool add_unit_instance_group(UnitInstanceGroup& group);
228230
bool remove_unit_instance_group(UnitInstanceGroup const& group);
229231

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)