aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/research/Invention.cpp
diff options
context:
space:
mode:
author zaaarf <80046572+zaaarf@users.noreply.github.com>2023-12-05 22:53:07 +0100
committer GitHub <noreply@github.com>2023-12-05 22:53:07 +0100
commit48a3f1729d709847d7cad33f594c77cac414e802 (patch)
treed890ed3ca182a960a35e5d1edae16e32e7f2b836 /src/openvic-simulation/research/Invention.cpp
parentdb00dcfb8c72448fea73e752e1f5c18047738876 (diff)
parentfa2ca50905f327713207069cf9a2e66cf6c00076 (diff)
Merge pull request #83 from OpenVicProject/dataloading-inventions
Dataloading inventions
Diffstat (limited to 'src/openvic-simulation/research/Invention.cpp')
-rw-r--r--src/openvic-simulation/research/Invention.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/openvic-simulation/research/Invention.cpp b/src/openvic-simulation/research/Invention.cpp
new file mode 100644
index 0000000..eb4e342
--- /dev/null
+++ b/src/openvic-simulation/research/Invention.cpp
@@ -0,0 +1,80 @@
+#include "Invention.hpp"
+#include <algorithm>
+#include <cstring>
+#include <utility>
+
+using namespace OpenVic;
+using namespace OpenVic::NodeTools;
+
+Invention::Invention(
+ std::string_view identifier, ModifierValue&& values, bool news, unit_set_t activated_units, building_set_t activated_buildings,
+ crime_set_t enabled_crimes, bool unlock_gas_attack, bool unlock_gas_defence)
+ : Modifier { identifier, std::move(values), 0 },
+ news { news }, activated_units { std::move(activated_units) }, activated_buildings { std::move(activated_buildings) },
+ enabled_crimes { std::move(enabled_crimes) }, unlock_gas_attack { unlock_gas_attack }, unlock_gas_defence { unlock_gas_defence } {} //TODO icon
+
+InventionManager::InventionManager() : inventions { "inventions" } {}
+
+bool InventionManager::add_invention(
+ std::string_view identifier, ModifierValue&& values, bool news, Invention::unit_set_t activated_units,
+ Invention::building_set_t activated_buildings, Invention::crime_set_t enabled_crimes,
+ bool unlock_gas_attack, bool unlock_gas_defence
+) {
+ if (identifier.empty()) {
+ Logger::error("Invalid invention identifier - empty!");
+ return false;
+ }
+
+ return inventions.add_item({
+ identifier, std::move(values), news, std::move(activated_units), std::move(activated_buildings),
+ std::move(enabled_crimes), unlock_gas_attack, unlock_gas_defence
+ });
+}
+
+bool InventionManager::load_inventions_file(
+ ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, ast::NodeCPtr root
+) {
+ return expect_dictionary_reserve_length(inventions, [this, &modifier_manager, &unit_manager, &building_manager](std::string_view identifier, ast::NodeCPtr value) -> bool {
+ ModifierValue loose_modifiers;
+ ModifierValue modifiers;
+
+ Invention::unit_set_t activated_units;
+ Invention::building_set_t activated_buildings;
+ Invention::crime_set_t enabled_crimes;
+
+ bool unlock_gas_attack = false;
+ bool unlock_gas_defence = false;
+ bool news = true; //defaults to true!
+
+ bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(loose_modifiers),
+ "news", ZERO_OR_ONE, expect_bool(assign_variable_callback(news)),
+ "limit", ONE_EXACTLY, success_callback,
+ "chance", ONE_EXACTLY, success_callback,
+ "effect", ZERO_OR_ONE, modifier_manager.expect_modifier_value_and_keys(
+ move_variable_callback(modifiers),
+ "gas_attack", ZERO_OR_ONE, expect_bool(assign_variable_callback(unlock_gas_attack)),
+ "gas_defence", ZERO_OR_ONE, expect_bool(assign_variable_callback(unlock_gas_defence)),
+ "activate_unit", ZERO_OR_MORE, unit_manager.expect_unit_identifier([this, &activated_units](Unit const& unit) -> bool {
+ activated_units.insert(&unit);
+ return true;
+ }),
+ "activate_building", ZERO_OR_MORE, building_manager.expect_building_type_identifier([this, &activated_buildings](BuildingType const& type) -> bool {
+ activated_buildings.insert(&type);
+ return true;
+ }),
+ "enable_crime", ZERO_OR_ONE, modifier_manager.expect_crime_modifier_identifier([this, &enabled_crimes](Crime const& crime) -> bool {
+ enabled_crimes.insert(&crime);
+ return true;
+ }))
+ )(value);
+
+ modifiers += loose_modifiers;
+
+ ret &= add_invention(
+ identifier, std::move(modifiers), news, activated_units, activated_buildings, enabled_crimes,
+ unlock_gas_attack, unlock_gas_defence
+ );
+
+ return ret;
+ })(root);
+} \ No newline at end of file