aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r--src/openvic-simulation/map/Crime.cpp26
-rw-r--r--src/openvic-simulation/map/Crime.hpp10
-rw-r--r--src/openvic-simulation/map/Map.cpp171
-rw-r--r--src/openvic-simulation/map/Map.hpp10
-rw-r--r--src/openvic-simulation/map/Province.cpp156
-rw-r--r--src/openvic-simulation/map/Province.hpp6
-rw-r--r--src/openvic-simulation/map/State.cpp2
7 files changed, 207 insertions, 174 deletions
diff --git a/src/openvic-simulation/map/Crime.cpp b/src/openvic-simulation/map/Crime.cpp
index c0bc04e..65760a3 100644
--- a/src/openvic-simulation/map/Crime.cpp
+++ b/src/openvic-simulation/map/Crime.cpp
@@ -3,17 +3,22 @@
using namespace OpenVic;
using namespace OpenVic::NodeTools;
-Crime::Crime(std::string_view new_identifier, ModifierValue&& new_values, icon_t new_icon, bool new_default_active)
- : TriggeredModifier { new_identifier, std::move(new_values), new_icon }, default_active { new_default_active } {}
+Crime::Crime(
+ std::string_view new_identifier, ModifierValue&& new_values, icon_t new_icon, ConditionScript&& new_trigger,
+ bool new_default_active
+) : TriggeredModifier { new_identifier, std::move(new_values), new_icon, std::move(new_trigger) },
+ default_active { new_default_active } {}
bool CrimeManager::add_crime_modifier(
- std::string_view identifier, ModifierValue&& values, Modifier::icon_t icon, bool default_active
+ std::string_view identifier, ModifierValue&& values, Modifier::icon_t icon, ConditionScript&& trigger, bool default_active
) {
if (identifier.empty()) {
Logger::error("Invalid crime modifier effect identifier - empty!");
return false;
}
- return crime_modifiers.add_item({ identifier, std::move(values), icon, default_active }, duplicate_warning_callback);
+ return crime_modifiers.add_item(
+ { identifier, std::move(values), icon, std::move(trigger), default_active }, duplicate_warning_callback
+ );
}
bool CrimeManager::load_crime_modifiers(ModifierManager const& modifier_manager, ast::NodeCPtr root) {
@@ -22,17 +27,26 @@ bool CrimeManager::load_crime_modifiers(ModifierManager const& modifier_manager,
[this, &modifier_manager](std::string_view key, ast::NodeCPtr value) -> bool {
ModifierValue modifier_value;
Modifier::icon_t icon = 0;
+ ConditionScript trigger;
bool default_active = false;
bool ret = modifier_manager.expect_modifier_value_and_keys(
move_variable_callback(modifier_value),
"icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon)),
- "trigger", ONE_EXACTLY, success_callback, // TODO - load condition
+ "trigger", ONE_EXACTLY, trigger.expect_script(),
"active", ZERO_OR_ONE, expect_bool(assign_variable_callback(default_active))
)(value);
- ret &= add_crime_modifier(key, std::move(modifier_value), icon, default_active);
+ ret &= add_crime_modifier(key, std::move(modifier_value), icon, std::move(trigger), default_active);
return ret;
}
)(root);
lock_crime_modifiers();
return ret;
}
+
+bool CrimeManager::parse_scripts(GameManager const& game_manager) {
+ bool ret = true;
+ for (Crime& crime : crime_modifiers.get_items()) {
+ ret &= crime.parse_scripts(game_manager);
+ }
+ return ret;
+}
diff --git a/src/openvic-simulation/map/Crime.hpp b/src/openvic-simulation/map/Crime.hpp
index 26f52ab..01044f9 100644
--- a/src/openvic-simulation/map/Crime.hpp
+++ b/src/openvic-simulation/map/Crime.hpp
@@ -9,7 +9,10 @@ namespace OpenVic {
private:
const bool PROPERTY(default_active);
- Crime(std::string_view new_identifier, ModifierValue&& new_values, icon_t new_icon, bool new_default_active);
+ Crime(
+ std::string_view new_identifier, ModifierValue&& new_values, icon_t new_icon, ConditionScript&& new_trigger,
+ bool new_default_active
+ );
public:
Crime(Crime&&) = default;
@@ -21,9 +24,12 @@ namespace OpenVic {
public:
bool add_crime_modifier(
- std::string_view identifier, ModifierValue&& values, Modifier::icon_t icon, bool default_active
+ std::string_view identifier, ModifierValue&& values, Modifier::icon_t icon, ConditionScript&& trigger,
+ bool default_active
);
bool load_crime_modifiers(ModifierManager const& modifier_manager, ast::NodeCPtr root);
+
+ bool parse_scripts(GameManager const& game_manager);
};
}
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp
index cce120e..95f4991 100644
--- a/src/openvic-simulation/map/Map.cpp
+++ b/src/openvic-simulation/map/Map.cpp
@@ -69,6 +69,173 @@ bool Map::add_province(std::string_view identifier, colour_t colour) {
return provinces.add_item(std::move(new_province));
}
+Province::distance_t Map::calculate_distance_between(Province const& from, Province const& to) const {
+ const fvec2_t to_pos = to.get_unit_position();
+ const fvec2_t from_pos = from.get_unit_position();
+
+ const fixed_point_t min_x = std::min(
+ (to_pos.x - from_pos.x).abs(),
+ std::min(
+ (to_pos.x - from_pos.x + width).abs(),
+ (to_pos.x - from_pos.x - width).abs()
+ )
+ );
+
+ return fvec2_t { min_x, to_pos.y - from_pos.y}.length_squared().sqrt();
+}
+
+/* This is called for all adjacent pixel pairs and returns whether or not a new adjacency was add,
+ * hence the lack of error messages in the false return cases. */
+bool Map::add_standard_adjacency(Province& from, Province& to) const {
+ if (from == to) {
+ return false;
+ }
+
+ const bool from_needs_adjacency = !from.is_adjacent_to(&to);
+ const bool to_needs_adjacency = !to.is_adjacent_to(&from);
+
+ if (!from_needs_adjacency && !to_needs_adjacency) {
+ return false;
+ }
+
+ const Province::distance_t distance = calculate_distance_between(from, to);
+
+ using enum Province::adjacency_t::type_t;
+
+ /* Default land-to-land adjacency */
+ Province::adjacency_t::type_t type = LAND;
+ if (from.is_water() != to.is_water()) {
+ /* Land-to-water adjacency */
+ type = COASTAL;
+
+ /* Mark the land province as coastal */
+ from.coastal = !from.is_water();
+ to.coastal = !to.is_water();
+ } else if (from.is_water()) {
+ /* Water-to-water adjacency */
+ type = WATER;
+ }
+
+ if (from_needs_adjacency) {
+ from.adjacencies.emplace_back(&to, distance, type, nullptr, 0);
+ }
+ if (to_needs_adjacency) {
+ to.adjacencies.emplace_back(&from, distance, type, nullptr, 0);
+ }
+ return true;
+}
+
+bool Map::add_special_adjacency(
+ Province& from, Province& to, Province::adjacency_t::type_t type, Province const* through,
+ Province::adjacency_t::data_t data
+) const {
+ if (from == to) {
+ Logger::error("Trying to add ", Province::adjacency_t::get_type_name(type), " adjacency from province ", from, " to itself!");
+ return false;
+ }
+
+ using enum Province::adjacency_t::type_t;
+
+ /* Check end points */
+ switch (type) {
+ case LAND:
+ case IMPASSABLE:
+ case STRAIT:
+ if (from.is_water() || to.is_water()) {
+ Logger::error(Province::adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has water endpoint(s)!");
+ return false;
+ }
+ break;
+ case WATER:
+ case CANAL:
+ if (!from.is_water() || !to.is_water()) {
+ Logger::error(Province::adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has land endpoint(s)!");
+ return false;
+ }
+ break;
+ case COASTAL:
+ if (from.is_water() == to.is_water()) {
+ Logger::error("Coastal adjacency from ", from, " to ", to, " has both land or water endpoints!");
+ return false;
+ }
+ break;
+ default:
+ Logger::error("Invalid adjacency type ", static_cast<uint32_t>(type));
+ return false;
+ }
+
+ /* Check through province */
+ if (type == STRAIT || type == CANAL) {
+ const bool water_expected = type == STRAIT;
+ if (through == nullptr || through->is_water() != water_expected) {
+ Logger::error(
+ Province::adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has a ",
+ (through == nullptr ? "null" : water_expected ? "land" : "water"), " through province ", through
+ );
+ return false;
+ }
+ } else if (through != nullptr) {
+ Logger::warning(
+ Province::adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has a non-null through province ",
+ through
+ );
+ through = nullptr;
+ }
+
+ /* Check canal data */
+ if (data != Province::adjacency_t::NO_CANAL && type != CANAL) {
+ Logger::warning(
+ Province::adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has invalid data ",
+ static_cast<uint32_t>(data)
+ );
+ data = Province::adjacency_t::NO_CANAL;
+ }
+
+ const Province::distance_t distance = calculate_distance_between(from, to);
+
+ const auto add_adjacency = [distance, type, through, data](Province& from, Province const& to) -> bool {
+ Province::adjacency_t* existing_adjacency = from.get_adjacency_to(&to);
+ if (existing_adjacency != nullptr) {
+ if (type == existing_adjacency->get_type()) {
+ Logger::warning(
+ "Adjacency from ", from, " to ", to, " already has type ", Province::adjacency_t::get_type_name(type), "!"
+ );
+ if (type != STRAIT && type != CANAL) {
+ /* Straits and canals might change through or data, otherwise we can exit early */
+ return true;
+ }
+ }
+ if (type != IMPASSABLE && type != STRAIT && type != CANAL) {
+ Logger::error(
+ "Provinces ", from, " and ", to, " already have an existing ",
+ Province::adjacency_t::get_type_name(existing_adjacency->get_type()), " adjacency, cannot create a ",
+ Province::adjacency_t::get_type_name(type), " adjacency!"
+ );
+ return false;
+ }
+ if (type != existing_adjacency->get_type() && existing_adjacency->get_type() != (type == CANAL ? WATER : LAND)) {
+ Logger::error(
+ "Cannot convert ", Province::adjacency_t::get_type_name(existing_adjacency->get_type()), " adjacency from ", from,
+ " to ", to, " to type ", Province::adjacency_t::get_type_name(type), "!"
+ );
+ return false;
+ }
+ *existing_adjacency = { &to, distance, type, through, data };
+ return true;
+ } else if (type == IMPASSABLE) {
+ Logger::warning(
+ "Provinces ", from, " and ", to, " do not have an existing adjacency to make impassable!"
+ );
+ return true;
+ } else {
+ from.adjacencies.emplace_back(&to, distance, type, through, data);
+ return true;
+ }
+ };
+
+ return add_adjacency(from, to) & add_adjacency(to, from);
+}
+
bool Map::set_water_province(std::string_view identifier) {
if (water_provinces.is_locked()) {
Logger::error("The map's water provinces have already been locked!");
@@ -564,7 +731,7 @@ bool Map::_generate_standard_province_adjacencies() {
const auto generate_adjacency = [this, &changed](Province* current, size_t x, size_t y) -> bool {
Province* neighbour = get_province_by_index(province_shape_image[x + y * width].index);
if (neighbour != nullptr) {
- return Province::add_standard_adjacency(*current, *neighbour);
+ return add_standard_adjacency(*current, *neighbour);
}
return false;
};
@@ -641,7 +808,7 @@ bool Map::generate_and_load_province_adjacencies(std::vector<LineObject> const&
}
const Province::adjacency_t::data_t data = data_uint;
- ret &= Province::add_special_adjacency(*from, *to, type, through, data);
+ ret &= add_special_adjacency(*from, *to, type, through, data);
}
);
return ret;
diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp
index 3e7ff35..a472a6c 100644
--- a/src/openvic-simulation/map/Map.hpp
+++ b/src/openvic-simulation/map/Map.hpp
@@ -5,6 +5,7 @@
#include <openvic-dataloader/csv/LineObject.hpp>
+#include "openvic-simulation/map/Province.hpp"
#include "openvic-simulation/map/Region.hpp"
#include "openvic-simulation/map/State.hpp"
#include "openvic-simulation/map/TerrainType.hpp"
@@ -75,7 +76,7 @@ namespace OpenVic {
Province::index_t PROPERTY(max_provinces);
Province* PROPERTY(selected_province);
- Pop::pop_size_t PROPERTY(highest_province_population)
+ Pop::pop_size_t PROPERTY(highest_province_population);
Pop::pop_size_t PROPERTY(total_map_population);
Province::index_t get_index_from_colour(colour_t colour) const;
@@ -89,6 +90,13 @@ namespace OpenVic {
bool add_province(std::string_view identifier, colour_t colour);
IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_INDEX_OFFSET(province, 1);
+ Province::distance_t calculate_distance_between(Province const& from, Province const& to) const;
+ bool add_standard_adjacency(Province& from, Province& to) const;
+ bool add_special_adjacency(
+ Province& from, Province& to, Province::adjacency_t::type_t type, Province const* through,
+ Province::adjacency_t::data_t data
+ ) const;
+
/* This provides a safe way to remove the const qualifier of a Province const*, via a non-const Map.
* It uses a const_cast (the fastest/simplest solution), but this could also be done without it by looking up the
* Province* using the Province const*'s index. Requiring a non-const Map ensures that this function can only be
diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp
index 1285b0d..a9bf329 100644
--- a/src/openvic-simulation/map/Province.cpp
+++ b/src/openvic-simulation/map/Province.cpp
@@ -185,166 +185,10 @@ bool Province::has_adjacency_going_through(Province const* province) const {
);
}
-/* This is called for all adjacent pixel pairs and returns whether or not a new adjacency was add,
- * hence the lack of error messages in the false return cases. */
-bool Province::add_standard_adjacency(Province& from, Province& to) {
- if (from == to) {
- return false;
- }
-
- const bool from_needs_adjacency = !from.is_adjacent_to(&to);
- const bool to_needs_adjacency = !to.is_adjacent_to(&from);
-
- if (!from_needs_adjacency && !to_needs_adjacency) {
- return false;
- }
-
- const distance_t distance = calculate_distance_between(from, to);
-
- using enum adjacency_t::type_t;
-
- /* Default land-to-land adjacency */
- adjacency_t::type_t type = LAND;
- if (from.is_water() != to.is_water()) {
- /* Land-to-water adjacency */
- type = COASTAL;
-
- /* Mark the land province as coastal */
- from.coastal = !from.is_water();
- to.coastal = !to.is_water();
- } else if (from.is_water()) {
- /* Water-to-water adjacency */
- type = WATER;
- }
-
- if (from_needs_adjacency) {
- from.adjacencies.emplace_back(&to, distance, type, nullptr, 0);
- }
- if (to_needs_adjacency) {
- to.adjacencies.emplace_back(&from, distance, type, nullptr, 0);
- }
- return true;
-}
-
-bool Province::add_special_adjacency(
- Province& from, Province& to, adjacency_t::type_t type, Province const* through, adjacency_t::data_t data
-) {
- if (from == to) {
- Logger::error("Trying to add ", adjacency_t::get_type_name(type), " adjacency from province ", from, " to itself!");
- return false;
- }
-
- using enum adjacency_t::type_t;
-
- /* Check end points */
- switch (type) {
- case LAND:
- case IMPASSABLE:
- case STRAIT:
- if (from.is_water() || to.is_water()) {
- Logger::error(adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has water endpoint(s)!");
- return false;
- }
- break;
- case WATER:
- case CANAL:
- if (!from.is_water() || !to.is_water()) {
- Logger::error(adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has land endpoint(s)!");
- return false;
- }
- break;
- case COASTAL:
- if (from.is_water() == to.is_water()) {
- Logger::error("Coastal adjacency from ", from, " to ", to, " has both land or water endpoints!");
- return false;
- }
- break;
- default:
- Logger::error("Invalid adjacency type ", static_cast<uint32_t>(type));
- return false;
- }
-
- /* Check through province */
- if (type == STRAIT || type == CANAL) {
- const bool water_expected = type == STRAIT;
- if (through == nullptr || through->is_water() != water_expected) {
- Logger::error(
- adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has a ",
- (through == nullptr ? "null" : water_expected ? "land" : "water"), " through province ", through
- );
- return false;
- }
- } else if (through != nullptr) {
- Logger::warning(
- adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has a non-null through province ",
- through
- );
- through = nullptr;
- }
-
- /* Check canal data */
- if (data != adjacency_t::NO_CANAL && type != CANAL) {
- Logger::warning(
- adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has invalid data ",
- static_cast<uint32_t>(data)
- );
- data = adjacency_t::NO_CANAL;
- }
-
- const distance_t distance = calculate_distance_between(from, to);
-
- const auto add_adjacency = [distance, type, through, data](Province& from, Province const& to) -> bool {
- adjacency_t* existing_adjacency = from.get_adjacency_to(&to);
- if (existing_adjacency != nullptr) {
- if (type == existing_adjacency->get_type()) {
- Logger::warning(
- "Adjacency from ", from, " to ", to, " already has type ", adjacency_t::get_type_name(type), "!"
- );
- if (type != STRAIT && type != CANAL) {
- /* Straits and canals might change through or data, otherwise we can exit early */
- return true;
- }
- }
- if (type != IMPASSABLE && type != STRAIT && type != CANAL) {
- Logger::error(
- "Provinces ", from, " and ", to, " already have an existing ",
- adjacency_t::get_type_name(existing_adjacency->get_type()), " adjacency, cannot create a ",
- adjacency_t::get_type_name(type), " adjacency!"
- );
- return false;
- }
- if (type != existing_adjacency->get_type() && existing_adjacency->get_type() != (type == CANAL ? WATER : LAND)) {
- Logger::error(
- "Cannot convert ", adjacency_t::get_type_name(existing_adjacency->get_type()), " adjacency from ", from,
- " to ", to, " to type ", adjacency_t::get_type_name(type), "!"
- );
- return false;
- }
- *existing_adjacency = { &to, distance, type, through, data };
- return true;
- } else if (type == IMPASSABLE) {
- Logger::warning(
- "Provinces ", from, " and ", to, " do not have an existing adjacency to make impassable!"
- );
- return true;
- } else {
- from.adjacencies.emplace_back(&to, distance, type, through, data);
- return true;
- }
- };
-
- return add_adjacency(from, to) & add_adjacency(to, from);
-}
-
fvec2_t Province::get_unit_position() const {
return positions.unit.value_or(positions.centre);
}
-Province::distance_t Province::calculate_distance_between(Province const& from, Province const& to) {
- const fvec2_t distance_vector = to.get_unit_position() - from.get_unit_position();
- return distance_vector.length_squared(); // TODO - replace with length using deterministic fixed point square root
-}
-
bool Province::reset(BuildingTypeManager const& building_type_manager) {
terrain_type = default_terrain_type;
life_rating = 0;
diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp
index bb10b29..b77ec06 100644
--- a/src/openvic-simulation/map/Province.hpp
+++ b/src/openvic-simulation/map/Province.hpp
@@ -155,13 +155,7 @@ namespace OpenVic {
std::vector<adjacency_t const*> get_adjacencies_going_through(Province const* province) const;
bool has_adjacency_going_through(Province const* province) const;
- static bool add_standard_adjacency(Province& from, Province& to);
- static bool add_special_adjacency(
- Province& from, Province& to, adjacency_t::type_t type, Province const* through, adjacency_t::data_t data
- );
-
fvec2_t get_unit_position() const;
- static distance_t calculate_distance_between(Province const& from, Province const& to);
bool reset(BuildingTypeManager const& building_type_manager);
bool apply_history_to_province(ProvinceHistoryEntry const* entry);
diff --git a/src/openvic-simulation/map/State.cpp b/src/openvic-simulation/map/State.cpp
index faf8d1b..c1f802d 100644
--- a/src/openvic-simulation/map/State.cpp
+++ b/src/openvic-simulation/map/State.cpp
@@ -59,7 +59,7 @@ StateSet::states_t& StateSet::get_states() {
void StateManager::generate_states(Map& map) {
regions.clear();
regions.reserve(map.get_region_count());
- for(Region const& region : map.get_regions()) {
+ for (Region const& region : map.get_regions()) {
if (!region.get_meta()) {
regions.emplace_back(map, region);
}