aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/research
diff options
context:
space:
mode:
author zaaarf <me@zaaarf.foo>2023-12-05 10:16:27 +0100
committer zaaarf <me@zaaarf.foo>2023-12-05 10:16:27 +0100
commitfa2ca50905f327713207069cf9a2e66cf6c00076 (patch)
treea6c82c543d5fc52183ca1363e171dbd4cc161943 /src/openvic-simulation/research
parent444a27726695478e44e0166e75df1f354b6432d5 (diff)
feat: implemented invention dataloading
Diffstat (limited to 'src/openvic-simulation/research')
-rw-r--r--src/openvic-simulation/research/Invention.cpp80
-rw-r--r--src/openvic-simulation/research/Invention.hpp55
-rw-r--r--src/openvic-simulation/research/ResearchManager.hpp12
-rw-r--r--src/openvic-simulation/research/Technology.cpp167
-rw-r--r--src/openvic-simulation/research/Technology.hpp98
5 files changed, 412 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
diff --git a/src/openvic-simulation/research/Invention.hpp b/src/openvic-simulation/research/Invention.hpp
new file mode 100644
index 0000000..321d982
--- /dev/null
+++ b/src/openvic-simulation/research/Invention.hpp
@@ -0,0 +1,55 @@
+#pragma once
+
+#include "openvic-simulation/economy/BuildingType.hpp"
+#include "openvic-simulation/military/Unit.hpp"
+#include "openvic-simulation/misc/Modifier.hpp"
+#include "openvic-simulation/types/IdentifierRegistry.hpp"
+#include <string_view>
+#include <unordered_set>
+
+namespace OpenVic {
+ struct Invention : Modifier {
+ friend struct InventionManager;
+ //TODO implement limit and chance
+ using unit_set_t = std::unordered_set<Unit const*>;
+ using building_set_t = std::unordered_set<BuildingType const*>;
+ using crime_set_t = std::unordered_set<Crime const*>;
+
+ private:
+ const bool PROPERTY_CUSTOM_PREFIX(news, is);
+ unit_set_t PROPERTY(activated_units);
+ building_set_t PROPERTY(activated_buildings);
+ crime_set_t PROPERTY(enabled_crimes);
+ const bool PROPERTY_CUSTOM_PREFIX(unlock_gas_attack, will);
+ const bool PROPERTY_CUSTOM_PREFIX(unlock_gas_defence, will);
+
+ 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
+ );
+
+ public:
+ Invention(Invention&&) = default;
+ };
+
+ struct InventionManager {
+ IdentifierRegistry<Invention> inventions;
+
+ public:
+ InventionManager();
+
+ bool 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
+ );
+
+ IDENTIFIER_REGISTRY_ACCESSORS(invention);
+
+ bool load_inventions_file(
+ ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager,
+ ast::NodeCPtr root
+ ); // inventions/*.txt
+ };
+} \ No newline at end of file
diff --git a/src/openvic-simulation/research/ResearchManager.hpp b/src/openvic-simulation/research/ResearchManager.hpp
new file mode 100644
index 0000000..d402a34
--- /dev/null
+++ b/src/openvic-simulation/research/ResearchManager.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+#include "openvic-simulation/research/Invention.hpp"
+#include "openvic-simulation/research/Technology.hpp"
+
+namespace OpenVic {
+ struct ResearchManager {
+ private:
+ TechnologyManager PROPERTY_REF(technology_manager);
+ InventionManager PROPERTY_REF(invention_manager);
+ };
+}
diff --git a/src/openvic-simulation/research/Technology.cpp b/src/openvic-simulation/research/Technology.cpp
new file mode 100644
index 0000000..07faaa3
--- /dev/null
+++ b/src/openvic-simulation/research/Technology.cpp
@@ -0,0 +1,167 @@
+#include "Technology.hpp"
+
+using namespace OpenVic;
+using namespace OpenVic::NodeTools;
+
+TechnologyFolder::TechnologyFolder(std::string_view new_identifier) : HasIdentifier { new_identifier } {}
+
+TechnologyArea::TechnologyArea(std::string_view new_identifier, TechnologyFolder const& new_folder)
+ : HasIdentifier { new_identifier }, folder { new_folder } {}
+
+Technology::Technology(
+ std::string_view new_identifier, TechnologyArea const& new_area, Date::year_t new_year, fixed_point_t new_cost,
+ bool new_unciv_military, uint8_t new_unit, unit_set_t&& new_activated_units, building_set_t&& new_activated_buildings,
+ ModifierValue&& new_values
+) : Modifier { new_identifier, std::move(new_values), 0 }, area { new_area }, year { new_year }, cost { new_cost },
+ unciv_military { new_unciv_military }, unit { new_unit }, activated_buildings { std::move(new_activated_units) },
+ activated_units { std::move(new_activated_buildings) } {}
+
+TechnologySchool::TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values)
+ : Modifier { new_identifier, std::move(new_values), 0 } {}
+
+TechnologyManager::TechnologyManager()
+ : technology_folders { "technology folders" }, technology_areas { "technology areas" }, technologies { "technologies" },
+ technology_schools { "technology schools" } {}
+
+bool TechnologyManager::add_technology_folder(std::string_view identifier) {
+ if (identifier.empty()) {
+ Logger::error("Invalid technology folder identifier - empty!");
+ return false;
+ }
+
+ return technology_folders.add_item({ identifier });
+}
+
+bool TechnologyManager::add_technology_area(std::string_view identifier, TechnologyFolder const* folder) {
+ if (identifier.empty()) {
+ Logger::error("Invalid technology area identifier - empty!");
+ return false;
+ }
+
+ if (folder == nullptr) {
+ Logger::error("Null folder for technology area \"", identifier, "\"!");
+ return false;
+ }
+
+ return technology_areas.add_item({ identifier, *folder });
+}
+
+bool TechnologyManager::add_technology(
+ std::string_view identifier, TechnologyArea const* area, Date::year_t year, fixed_point_t cost, bool unciv_military,
+ uint8_t unit, Technology::unit_set_t&& activated_units, Technology::building_set_t&& activated_buildings,
+ ModifierValue&& values
+) {
+ if (identifier.empty()) {
+ Logger::error("Invalid technology identifier - empty!");
+ return false;
+ }
+
+ if (area == nullptr) {
+ Logger::error("Null area for technology \"", identifier, "\"!");
+ return false;
+ }
+
+ return technologies.add_item({
+ identifier, *area, year, cost, unciv_military, unit, std::move(activated_units), std::move(activated_buildings),
+ std::move(values)
+ });
+}
+
+bool TechnologyManager::add_technology_school(std::string_view identifier, ModifierValue&& values) {
+ if (identifier.empty()) {
+ Logger::error("Invalid modifier effect identifier - empty!");
+ return false;
+ }
+
+ return technology_schools.add_item({ identifier, std::move(values) });
+}
+
+bool TechnologyManager::load_technology_file_areas(ast::NodeCPtr root) {
+ return expect_dictionary_reserve_length(technology_folders, [this](std::string_view root_key, ast::NodeCPtr root_value) -> bool {
+ if (root_key == "folders") {
+ return expect_dictionary([this](std::string_view folder_key, ast::NodeCPtr folder_value) -> bool {
+ bool ret = add_technology_folder(folder_key);
+ TechnologyFolder const* current_folder = get_technology_folder_by_identifier(folder_key);
+ if (current_folder == nullptr)
+ return false;
+ ret &= expect_list(expect_identifier([this, current_folder](std::string_view area) -> bool {
+ return add_technology_area(area, current_folder);
+ }))(folder_value);
+ return ret;
+ })(root_value);
+ lock_technology_folders();
+ lock_technology_areas();
+ } else if (root_key == "schools") return true; //ignore
+ else return false;
+ })(root);
+}
+
+bool TechnologyManager::load_technology_file_schools(ModifierManager const& modifier_manager, ast::NodeCPtr root) {
+ return expect_dictionary([this, &modifier_manager](std::string_view root_key, ast::NodeCPtr root_value) -> bool {
+ if (root_key == "schools") {
+ return expect_dictionary_reserve_length(technology_schools, [this, &modifier_manager](std::string_view school_key, ast::NodeCPtr school_value) -> bool {
+ ModifierValue modifiers;
+ bool ret = modifier_manager.expect_modifier_value(move_variable_callback(modifiers))(school_value);
+ ret &= add_technology_school(school_key, std::move(modifiers));
+ return ret;
+ })(root_value);
+ lock_technology_schools();
+ } else if (root_key == "folders") return true; //ignore
+ else return false;
+ })(root);
+}
+
+bool TechnologyManager::load_technologies_file(ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager, ast::NodeCPtr root) {
+ return expect_dictionary_reserve_length(technologies, [this, &modifier_manager, &unit_manager, &building_manager](std::string_view tech_key, ast::NodeCPtr tech_value) -> bool {
+ ModifierValue modifiers;
+ TechnologyArea const* area = nullptr;
+ Date::year_t year = 0;
+ fixed_point_t cost = 0;
+ bool unciv_military = false;
+ uint8_t unit = 0;
+ Technology::unit_set_t activated_units;
+ Technology::building_set_t activated_buildings;
+
+ bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(modifiers),
+ "area", ONE_EXACTLY, expect_technology_area_identifier(assign_variable_callback_pointer(area)),
+ "year", ONE_EXACTLY, expect_uint(assign_variable_callback(year)),
+ "cost", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(cost)),
+ "unciv_military", ZERO_OR_ONE, expect_bool(assign_variable_callback(unciv_military)),
+ "unit", ZERO_OR_ONE, expect_uint(assign_variable_callback(unit)),
+ "activate_unit", ZERO_OR_MORE, unit_manager.expect_unit_identifier([&activated_units](Unit const& unit) -> bool {
+ activated_units.insert(&unit);
+ return true;
+ }),
+ "activate_building", ZERO_OR_MORE, building_manager.expect_building_type_identifier(
+ [&activated_buildings](BuildingType const& building_type) -> bool {
+ activated_buildings.insert(&building_type);
+ return true;
+ }
+ ),
+ "ai_chance", ONE_EXACTLY, success_callback //TODO
+ )(tech_value);
+
+ ret &= add_technology(
+ tech_key, area, year, cost, unciv_military, unit, std::move(activated_units), std::move(activated_buildings),
+ std::move(modifiers)
+ );
+ return ret;
+ })(root);
+}
+
+#define TECH_MODIFIER(NAME, POS) ret &= modifier_manager.add_modifier_effect(NAME, POS, ModifierEffect::format_t::PROPORTION_DECIMAL)
+#define UNCIV_TECH_MODIFIER(NAME) TECH_MODIFIER(NAME, false); TECH_MODIFIER(StringUtils::append_string_views("self_", NAME), false);
+bool TechnologyManager::generate_modifiers(ModifierManager& modifier_manager) {
+ bool ret = true;
+
+ UNCIV_TECH_MODIFIER("unciv_military_modifier");
+ UNCIV_TECH_MODIFIER("unciv_economic_modifier");
+
+ for (TechnologyFolder const& folder : get_technology_folders()) {
+ TECH_MODIFIER(StringUtils::append_string_views(folder.get_identifier(), "_research_bonus"), true);
+ }
+
+ return ret;
+}
+#undef UNCIV_TECH_MODIFIER
+#undef TECH_MODIFIER \ No newline at end of file
diff --git a/src/openvic-simulation/research/Technology.hpp b/src/openvic-simulation/research/Technology.hpp
new file mode 100644
index 0000000..0939369
--- /dev/null
+++ b/src/openvic-simulation/research/Technology.hpp
@@ -0,0 +1,98 @@
+#pragma once
+
+#include "openvic-simulation/misc/Modifier.hpp"
+#include "openvic-simulation/types/Date.hpp"
+#include "openvic-simulation/military/Unit.hpp"
+#include "openvic-simulation/economy/BuildingType.hpp"
+#include <cstdint>
+
+namespace OpenVic {
+ struct TechnologyFolder : HasIdentifier {
+ friend struct TechnologyManager;
+
+ private:
+ TechnologyFolder(std::string_view new_identifier);
+
+ public:
+ TechnologyFolder(TechnologyFolder&&) = default;
+ };
+
+ struct TechnologyArea : HasIdentifier {
+ friend struct TechnologyManager;
+
+ private:
+ TechnologyFolder const& PROPERTY(folder);
+
+ TechnologyArea(std::string_view new_identifier, TechnologyFolder const& new_folder);
+
+ public:
+ TechnologyArea(TechnologyArea&&) = default;
+ };
+
+ struct Technology : Modifier {
+ friend struct TechnologyManager;
+
+ using unit_set_t = std::unordered_set<Unit const*>;
+ using building_set_t = std::unordered_set<BuildingType const*>;
+
+ private:
+ TechnologyArea const& PROPERTY(area);
+ const Date::year_t PROPERTY(year);
+ const fixed_point_t PROPERTY(cost);
+ const bool PROPERTY(unciv_military);
+ const uint8_t PROPERTY(unit);
+ const unit_set_t PROPERTY(activated_buildings);
+ const building_set_t PROPERTY(activated_units);
+
+ //TODO: implement rules/modifiers and ai_chance
+
+ Technology(
+ std::string_view new_identifier, TechnologyArea const& new_area, Date::year_t new_year, fixed_point_t new_cost,
+ bool new_unciv_military, uint8_t new_unit, unit_set_t&& new_activated_units,
+ building_set_t&& new_activated_buildings, ModifierValue&& new_values
+ );
+
+ public:
+ Technology(Technology&&) = default;
+ };
+
+ struct TechnologySchool : Modifier {
+ friend struct TechnologyManager;
+
+ private:
+ TechnologySchool(std::string_view new_identifier, ModifierValue&& new_values);
+ };
+
+ struct TechnologyManager {
+ IdentifierRegistry<TechnologyFolder> technology_folders;
+ IdentifierRegistry<TechnologyArea> technology_areas;
+ IdentifierRegistry<Technology> technologies;
+ IdentifierRegistry<TechnologySchool> technology_schools;
+
+ public:
+ TechnologyManager();
+
+ bool add_technology_folder(std::string_view identifier);
+ IDENTIFIER_REGISTRY_ACCESSORS(technology_folder)
+
+ bool add_technology_area(std::string_view identifier, TechnologyFolder const* folder);
+ IDENTIFIER_REGISTRY_ACCESSORS(technology_area)
+
+ bool add_technology(
+ std::string_view identifier, TechnologyArea const* area, Date::year_t year, fixed_point_t cost,
+ bool unciv_military, uint8_t unit, Technology::unit_set_t&& activated_units,
+ Technology::building_set_t&& activated_buildings, ModifierValue&& values);
+ IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(technology, technologies)
+
+ bool add_technology_school(std::string_view identifier, ModifierValue&& values);
+ IDENTIFIER_REGISTRY_ACCESSORS(technology_school);
+
+ bool load_technology_file_areas(ast::NodeCPtr root); // common/technology.txt
+ bool load_technology_file_schools(ModifierManager const& modifier_manager, ast::NodeCPtr root); // also common/technology.txt
+ bool load_technologies_file(
+ ModifierManager const& modifier_manager, UnitManager const& unit_manager, BuildingManager const& building_manager,
+ ast::NodeCPtr root
+ ); // technologies/*.txt
+ bool generate_modifiers(ModifierManager& modifier_manager);
+ };
+} \ No newline at end of file