From 6b3d2315225468e03bef30e942263fe9b8fc06fa Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Oct 2023 23:05:49 +0100 Subject: Further cleanup and rearrangement --- src/openvic-simulation/economy/Building.hpp | 174 ++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/openvic-simulation/economy/Building.hpp (limited to 'src/openvic-simulation/economy/Building.hpp') diff --git a/src/openvic-simulation/economy/Building.hpp b/src/openvic-simulation/economy/Building.hpp new file mode 100644 index 0000000..3f74c3d --- /dev/null +++ b/src/openvic-simulation/economy/Building.hpp @@ -0,0 +1,174 @@ +#pragma once + +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/economy/Good.hpp" +#include "openvic-simulation/economy/ProductionType.hpp" +#include "openvic-simulation/Modifier.hpp" + +#define ARGS std::string_view on_completion, fixed_point_t completion_size, level_t max_level, \ + std::map goods_cost, fixed_point_t cost, Timespan build_time, bool visibility, bool on_map, bool default_enabled, \ + ProductionType const* production_type, bool pop_build_factory, bool strategic_factory, bool advanced_factory, level_t fort_level, \ + uint64_t naval_capacity, std::vector colonial_points, bool in_province, bool one_per_state, fixed_point_t colonial_range, \ + fixed_point_t infrastructure, fixed_point_t movement_cost, fixed_point_t local_ship_build, bool spawn_railway_track, bool sail, bool steam, \ + bool capital, bool port, ModifierValue&& modifiers + +namespace OpenVic { + + struct BuildingManager; + struct BuildingType; + + /* REQUIREMENTS: + * MAP-11, MAP-72, MAP-73 + * MAP-12, MAP-75, MAP-76 + * MAP-13, MAP-78, MAP-79 + */ + struct Building : HasIdentifier, ModifierValue { + friend struct BuildingManager; + + using level_t = int16_t; + + private: + BuildingType const& type; + const std::string on_completion; //probably sound played on completion + const fixed_point_t completion_size; + const level_t max_level; + const std::map goods_cost; + const fixed_point_t cost; + const Timespan build_time; //time + const bool visibility; + const bool on_map; //onmap + + const bool default_enabled; + ProductionType const* production_type; + const bool pop_build_factory; + const bool strategic_factory; + const bool advanced_factory; + + const level_t fort_level; //probably the step-per-level + + const uint64_t naval_capacity; + const std::vector colonial_points; + const bool in_province; //province + const bool one_per_state; + const fixed_point_t colonial_range; + + const fixed_point_t infrastructure; + const fixed_point_t movement_cost; + const fixed_point_t local_ship_build; + const bool spawn_railway_track; + + const bool sail; //only in clipper shipyard + const bool steam; //only in steamer shipyard + const bool capital; //only in naval base + const bool port; //only in naval base + + Building(std::string_view identifier, BuildingType const& type, ARGS); + + public: + Building(Building&&) = default; + + BuildingType const& get_type() const; + std::string const& get_on_completion() const; + fixed_point_t get_completion_size() const; + level_t get_max_level() const; + std::map const& get_goods_cost() const; + fixed_point_t get_cost() const; + Timespan get_build_time() const; + bool has_visibility() const; + bool is_on_map() const; + + bool is_default_enabled() const; + ProductionType const* get_production_type() const; + bool is_pop_built_factory() const; + bool is_strategic_factory() const; + bool is_advanced_factory() const; + + level_t get_fort_level() const; + + uint64_t get_naval_capacity() const; + std::vector const& get_colonial_points() const; + bool is_in_province() const; + bool is_one_per_state() const; + fixed_point_t get_colonial_range() const; + + fixed_point_t get_infrastructure() const; + fixed_point_t get_movement_cost() const; + fixed_point_t get_local_ship_build() const; + bool spawned_railway_track() const; + }; + + struct BuildingType : HasIdentifier { + friend struct BuildingManager; + + private: + BuildingType(std::string_view new_identifier); + + public: + BuildingType(BuildingType&&) = default; + }; + + enum class ExpansionState { + CannotExpand, + CanExpand, + Preparing, + Expanding + }; + + struct BuildingInstance : HasIdentifier { //used in the actual game + friend struct BuildingManager; + using level_t = Building::level_t; + + private: + Building const& building; + + level_t level = 0; + ExpansionState expansion_state = ExpansionState::CannotExpand; + Date start, end; + float expansion_progress; + + bool _can_expand() const; + + BuildingInstance(Building const& building); + + public: + BuildingInstance(BuildingInstance&&) = default; + + Building const& get_building() const; + + level_t get_current_level() const; + ExpansionState get_expansion_state() const; + Date const& get_start_date() const; + Date const& get_end_date() const; + float get_expansion_progress() const; + + bool expand(); + void update_state(Date const& today); + void tick(Date const& today); + + }; + + struct Province; + + struct BuildingManager { + using level_t = Building::level_t; //this is getting ridiculous + + private: + IdentifierRegistry building_types; + IdentifierRegistry buildings; + + public: + BuildingManager(); + + bool add_building_type(std::string_view identifier); + IDENTIFIER_REGISTRY_ACCESSORS(building_type) + + bool add_building(std::string_view identifier, BuildingType const* type, ARGS); + IDENTIFIER_REGISTRY_ACCESSORS(building) + + bool load_buildings_file(GoodManager const& good_manager, ProductionTypeManager const& production_type_manager, ModifierManager const& modifier_manager, ast::NodeCPtr root); + + bool generate_province_buildings(Province& province) const; + }; +} -- cgit v1.2.3-56-ga3b1 From d95c3c9da75018e3e959e5493ebd9c520e00bf7a Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Oct 2023 23:43:10 +0100 Subject: Changed return types to std::string_view --- src/openvic-simulation/economy/Building.cpp | 2 +- src/openvic-simulation/economy/Building.hpp | 2 +- src/openvic-simulation/military/Unit.cpp | 14 +++++++------- src/openvic-simulation/military/Unit.hpp | 14 +++++++------- src/openvic-simulation/politics/Government.cpp | 2 +- src/openvic-simulation/politics/Government.hpp | 2 +- src/openvic-simulation/pop/Culture.cpp | 2 +- src/openvic-simulation/pop/Culture.hpp | 2 +- src/openvic-simulation/types/IdentifierRegistry.cpp | 2 +- src/openvic-simulation/types/IdentifierRegistry.hpp | 20 ++++++++++++++------ 10 files changed, 35 insertions(+), 27 deletions(-) (limited to 'src/openvic-simulation/economy/Building.hpp') diff --git a/src/openvic-simulation/economy/Building.cpp b/src/openvic-simulation/economy/Building.cpp index d27c91a..23bd04c 100644 --- a/src/openvic-simulation/economy/Building.cpp +++ b/src/openvic-simulation/economy/Building.cpp @@ -17,7 +17,7 @@ BuildingType const& Building::get_type() const { return type; } -std::string const& Building::get_on_completion() const { +std::string_view Building::get_on_completion() const { return on_completion; } diff --git a/src/openvic-simulation/economy/Building.hpp b/src/openvic-simulation/economy/Building.hpp index 3f74c3d..3d1e24b 100644 --- a/src/openvic-simulation/economy/Building.hpp +++ b/src/openvic-simulation/economy/Building.hpp @@ -70,7 +70,7 @@ namespace OpenVic { Building(Building&&) = default; BuildingType const& get_type() const; - std::string const& get_on_completion() const; + std::string_view get_on_completion() const; fixed_point_t get_completion_size() const; level_t get_max_level() const; std::map const& get_goods_cost() const; diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp index 3da24da..63ece91 100644 --- a/src/openvic-simulation/military/Unit.cpp +++ b/src/openvic-simulation/military/Unit.cpp @@ -27,7 +27,7 @@ Unit::type_t Unit::get_type() const { return type; } -std::string const& Unit::get_sprite() const { +std::string_view Unit::get_sprite() const { return sprite; } @@ -35,7 +35,7 @@ bool Unit::is_active() const { return active; } -std::string const& Unit::get_unit_type() const { +std::string_view Unit::get_unit_type() const { return unit_type; } @@ -88,15 +88,15 @@ bool LandUnit::get_primary_culture() const { return primary_culture; } -std::string const& LandUnit::get_sprite_override() const { +std::string_view LandUnit::get_sprite_override() const { return sprite_override; } -std::string const& LandUnit::get_sprite_mount() const { +std::string_view LandUnit::get_sprite_mount() const { return sprite_mount; } -std::string const& LandUnit::get_sprite_mount_attach_node() const { +std::string_view LandUnit::get_sprite_mount_attach_node() const { return sprite_mount_attach_node; } @@ -146,11 +146,11 @@ bool NavalUnit::is_transport() const { return transport; } -std::string const& NavalUnit::get_move_sound() const { +std::string_view NavalUnit::get_move_sound() const { return move_sound; } -std::string const& NavalUnit::get_select_sound() const { +std::string_view NavalUnit::get_select_sound() const { return select_sound; } diff --git a/src/openvic-simulation/military/Unit.hpp b/src/openvic-simulation/military/Unit.hpp index 65e6259..de30763 100644 --- a/src/openvic-simulation/military/Unit.hpp +++ b/src/openvic-simulation/military/Unit.hpp @@ -57,9 +57,9 @@ namespace OpenVic { icon_t get_icon() const; type_t get_type() const; - std::string const& get_sprite() const; + std::string_view get_sprite() const; bool is_active() const; - std::string const& get_unit_type() const; + std::string_view get_unit_type() const; bool has_floating_flag() const; uint32_t get_priority() const; @@ -94,9 +94,9 @@ namespace OpenVic { LandUnit(LandUnit&&) = default; bool get_primary_culture() const; - std::string const& get_sprite_override() const; - std::string const& get_sprite_mount() const; - std::string const& get_sprite_mount_attach_node() const; + std::string_view get_sprite_override() const; + std::string_view get_sprite_mount() const; + std::string_view get_sprite_mount_attach_node() const; fixed_point_t get_reconnaissance() const; fixed_point_t get_attack() const; @@ -138,8 +138,8 @@ namespace OpenVic { bool can_sail() const; bool is_transport() const; bool is_capital() const; - std::string const& get_move_sound() const; - std::string const& get_select_sound() const; + std::string_view get_move_sound() const; + std::string_view get_select_sound() const; fixed_point_t get_colonial_points() const; bool can_build_overseas() const; uint32_t get_min_port_level() const; diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 1a48806..16b3bc4 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -30,7 +30,7 @@ Timespan GovernmentType::get_term_duration() const { return term_duration; } -std::string const& GovernmentType::get_flag_type() const { +std::string_view GovernmentType::get_flag_type() const { return flag_type_identifier; } diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp index c72dae6..ba8496f 100644 --- a/src/openvic-simulation/politics/Government.hpp +++ b/src/openvic-simulation/politics/Government.hpp @@ -24,7 +24,7 @@ namespace OpenVic { bool holds_elections() const; bool can_appoint_ruling_party() const; Timespan get_term_duration() const; - std::string const& get_flag_type() const; + std::string_view get_flag_type() const; }; struct GovernmentTypeManager { diff --git a/src/openvic-simulation/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp index 261ad4c..e3ba5dd 100644 --- a/src/openvic-simulation/pop/Culture.cpp +++ b/src/openvic-simulation/pop/Culture.cpp @@ -15,7 +15,7 @@ CultureGroup::CultureGroup(std::string_view new_identifier, std::string_view new unit_graphical_culture_type { new_unit_graphical_culture_type }, is_overseas { new_is_overseas } {} -std::string const& CultureGroup::get_leader() const { +std::string_view CultureGroup::get_leader() const { return leader; } diff --git a/src/openvic-simulation/pop/Culture.hpp b/src/openvic-simulation/pop/Culture.hpp index b6b4952..d36a90b 100644 --- a/src/openvic-simulation/pop/Culture.hpp +++ b/src/openvic-simulation/pop/Culture.hpp @@ -31,7 +31,7 @@ namespace OpenVic { public: CultureGroup(CultureGroup&&) = default; - std::string const& get_leader() const; + std::string_view get_leader() const; GraphicalCultureType const& get_unit_graphical_culture_type() const; bool get_is_overseas() const; }; diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index d6afd3a..8e93cb1 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp @@ -9,7 +9,7 @@ HasIdentifier::HasIdentifier(std::string_view new_identifier) assert(!identifier.empty()); } -std::string const& HasIdentifier::get_identifier() const { +std::string_view HasIdentifier::get_identifier() const { return identifier; } diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index aea5728..3ba7fc4 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -28,7 +28,7 @@ namespace OpenVic { HasIdentifier& operator=(HasIdentifier const&) = delete; HasIdentifier& operator=(HasIdentifier&&) = delete; - std::string const& get_identifier() const; + std::string_view get_identifier() const; }; std::ostream& operator<<(std::ostream& stream, HasIdentifier const& obj); @@ -73,7 +73,7 @@ namespace OpenVic { distribution_t::value_type get_largest_item(distribution_t const& dist); template - using get_identifier_func_t = std::string const&(T::*)(void) const; + using get_identifier_func_t = std::string_view(T::*)(void) const; template _Type, get_identifier_func_t<_Base> get_identifier, typename _Storage, _Type* (*get_ptr)(_Storage&), _Type const* (*get_cptr)(_Storage const&)> @@ -92,7 +92,7 @@ namespace OpenVic { UniqueKeyRegistry(std::string_view new_name, bool new_log_lock = true) : name { new_name }, log_lock { new_log_lock } {} - std::string const& get_name() const { + std::string_view get_name() const { return name; } @@ -102,7 +102,7 @@ namespace OpenVic { return false; } value_type const* new_item = (*get_cptr)(item); - std::string const& new_identifier = (new_item->*get_identifier)(); + const std::string_view new_identifier = (new_item->*get_identifier)(); value_type const* old_item = get_item_by_identifier(new_identifier); if (old_item != nullptr) { #define DUPLICATE_MESSAGE "Cannot add item to the ", name, " registry - an item with the identifier \"", new_identifier, "\" already exists!" @@ -115,7 +115,7 @@ namespace OpenVic { } #undef DUPLICATE_MESSAGE } - identifier_index_map[new_identifier] = items.size(); + identifier_index_map.emplace(new_identifier, items.size()); items.push_back(std::move(item)); return true; } @@ -249,8 +249,16 @@ namespace OpenVic { } }; + template + [[nodiscard]] inline constexpr T* _addressof(T& v) noexcept { + return std::addressof(v); + } + + template + const T* _addressof(const T&&) = delete; + template _Type, get_identifier_func_t<_Base> get_identifier> - using ValueRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, _Type, std::addressof<_Type>, std::addressof>; + using ValueRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, _Type, _addressof<_Type>, _addressof>; template constexpr _Type* get_ptr(std::unique_ptr<_Type>& storage) { -- cgit v1.2.3-56-ga3b1