From 7a8ac1cdb8e71a4166393ac06b1b60512db0ef8c Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 28 Sep 2023 13:16:17 +0200 Subject: feat: renamed Building to BuildingInstance, implemented Building and BuildingType defines --- src/openvic-simulation/GameManager.cpp | 16 +--- src/openvic-simulation/map/Building.cpp | 164 ++++++++++++++++++++++---------- src/openvic-simulation/map/Building.hpp | 137 ++++++++++++++++++++------ src/openvic-simulation/map/Province.cpp | 21 ++-- src/openvic-simulation/map/Province.hpp | 8 +- 5 files changed, 236 insertions(+), 110 deletions(-) diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index d9dc3d9..958049d 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -177,12 +177,12 @@ bool GameManager::load_hardcoded_defines() { } }, { "mapmode_infrastructure", [](Map const& map, Province const& province) -> colour_t { - Building const* railroad = province.get_building_by_identifier("building_railroad"); + BuildingInstance const* railroad = province.get_building_by_identifier("building_railroad"); if (railroad != nullptr) { - colour_t val = fraction_to_colour_byte(railroad->get_level(), railroad->get_type().get_max_level() + 1, 0.5f, 1.0f); + colour_t val = fraction_to_colour_byte(railroad->get_current_level(), railroad->get_building().get_max_level() + 1, 0.5f, 1.0f); switch (railroad->get_expansion_state()) { - case Building::ExpansionState::CannotExpand: val <<= 16; break; - case Building::ExpansionState::CanExpand: break; + case ExpansionState::CannotExpand: val <<= 16; break; + case ExpansionState::CanExpand: break; default: val <<= 8; break; } return HIGH_ALPHA_VALUE | val; @@ -208,13 +208,5 @@ bool GameManager::load_hardcoded_defines() { ret &= map.add_mapmode(mapmode.first, mapmode.second); map.lock_mapmodes(); - using building_type_t = std::tuple; - const std::vector building_types { - { "building_fort", 4, 8 }, { "building_naval_base", 6, 15 }, { "building_railroad", 5, 10 } // Move this to building.hpp - }; - for (building_type_t const& type : building_types) - ret &= building_manager.add_building_type(std::get<0>(type), std::get<1>(type), std::get<2>(type)); - building_manager.lock_building_types(); - return ret; } diff --git a/src/openvic-simulation/map/Building.cpp b/src/openvic-simulation/map/Building.cpp index eba2049..f4b5034 100644 --- a/src/openvic-simulation/map/Building.cpp +++ b/src/openvic-simulation/map/Building.cpp @@ -1,45 +1,133 @@ #include "Building.hpp" -#include - -#include "openvic-simulation/map/Province.hpp" -#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/map/Province.hpp" //imported here so the hpp doesn't get circular imports using namespace OpenVic; +using namespace OpenVic::NodeTools; -Building::Building(BuildingType const& new_type) - : HasIdentifier { new_type.get_identifier() }, - type { new_type } {} - -bool Building::_can_expand() const { - return level < type.get_max_level(); -} +Building::Building(std::string_view identifier, ARGS) : HasIdentifier { identifier }, type { type }, on_completion { on_completion }, + completion_size { completion_size }, max_level { max_level }, build_cost { build_cost }, build_time { build_time }, visibility { visibility }, on_map { on_map }, + default_enabled { default_enabled }, production_type { production_type }, pop_build_factory { pop_build_factory }, strategic_factory { strategic_factory }, + fort_level { fort_level }, naval_capacity { naval_capacity }, colonial_points { colonial_points }, in_province { in_province }, one_per_state { one_per_state }, + colonial_range { colonial_range }, infrastructure { infrastructure }, movement_cost { movement_cost }, spawn_railway_track { spawn_railway_track } {} BuildingType const& Building::get_type() const { return type; } -Building::level_t Building::get_level() const { +Building::sound_t Building::get_on_completion() const { + return on_completion; +} + +fixed_point_t Building::get_completion_size() const { + return completion_size; +} + +Building::level_t Building::get_max_level() const { + return max_level; +} + +std::map const& Building::get_build_cost() const { + return build_cost; +} + +Timespan Building::get_build_time() const { + return build_time; +} + +bool Building::has_visibility() const { + return visibility; +} + +bool Building::is_on_map() const { + return on_map; +} + +bool Building::is_default_enabled() const { + return default_enabled; +} + +ProductionType const* Building::get_production_type() const { + return production_type; +} + +bool Building::is_pop_built_factory() const { + return pop_build_factory; +} + +bool Building::is_strategic_factory() const { + return strategic_factory; +} + +Building::level_t Building::get_fort_level() const { + return fort_level; +} + +uint64_t Building::get_naval_capacity() const { + return naval_capacity; +} + +std::vector const& Building::get_colonial_points() const { + return colonial_points; +} + +bool Building::is_in_province() const { + return in_province; +} + +bool Building::is_one_per_state() const { + return one_per_state; +} + +uint64_t Building::get_colonial_range() const { + return colonial_range; +} + +fixed_point_t Building::get_infrastructure() const { + return infrastructure; +} + +fixed_point_t Building::get_movement_cost() const { + return movement_cost; +} + +bool Building::spawned_railway_track() const { + return spawn_railway_track; +} + +BuildingType::BuildingType(const std::string_view new_identifier) : HasIdentifier { new_identifier } {} + +BuildingInstance::BuildingInstance(Building const& building) : HasIdentifier { building.get_identifier() }, building { building } {} + +Building const& BuildingInstance::get_building() const { + return building; +} + +bool BuildingInstance::_can_expand() const { + return level < building.get_max_level(); +} + +BuildingInstance::level_t BuildingInstance::get_current_level() const { return level; } -Building::ExpansionState Building::get_expansion_state() const { +ExpansionState BuildingInstance::get_expansion_state() const { return expansion_state; } -Date const& Building::get_start_date() const { +Date const& BuildingInstance::get_start_date() const { return start; } -Date const& Building::get_end_date() const { +Date const& BuildingInstance::get_end_date() const { return end; } -float Building::get_expansion_progress() const { +float BuildingInstance::get_expansion_progress() const { return expansion_progress; } -bool Building::expand() { +bool BuildingInstance::expand() { if (expansion_state == ExpansionState::CanExpand) { expansion_state = ExpansionState::Preparing; expansion_progress = 0.0f; @@ -51,11 +139,11 @@ bool Building::expand() { /* REQUIREMENTS: * MAP-71, MAP-74, MAP-77 */ -void Building::update_state(Date const& today) { +void BuildingInstance::update_state(Date const& today) { switch (expansion_state) { case ExpansionState::Preparing: start = today; - end = start + type.get_build_time(); + end = start + building.get_build_time(); break; case ExpansionState::Expanding: expansion_progress = static_cast(today - start) / static_cast(end - start); @@ -64,7 +152,7 @@ void Building::update_state(Date const& today) { } } -void Building::tick(Date const& today) { +void BuildingInstance::tick(Date const& today) { if (expansion_state == ExpansionState::Preparing) { expansion_state = ExpansionState::Expanding; } @@ -76,38 +164,14 @@ void Building::tick(Date const& today) { } } -BuildingType::BuildingType(const std::string_view new_identifier, Building::level_t new_max_level, Timespan new_build_time) - : HasIdentifier { new_identifier }, - max_level { new_max_level }, - build_time { new_build_time } { - assert(max_level >= 0); - assert(build_time >= 0); -} - -Building::level_t BuildingType::get_max_level() const { - return max_level; -} - -Timespan BuildingType::get_build_time() const { - return build_time; -} +BuildingManager::BuildingManager() : building_types { "building types" }, buildings { "buildings" } {} -BuildingManager::BuildingManager() : building_types { "building types" } {} - -bool BuildingManager::add_building_type(const std::string_view identifier, Building::level_t max_level, Timespan build_time) { +bool BuildingManager::add_building_type(const std::string_view identifier) { if (identifier.empty()) { Logger::error("Invalid building type identifier - empty!"); return false; } - if (max_level < 0) { - Logger::error("Invalid building type max level for ", identifier, ": ", max_level); - return false; - } - if (build_time < 0) { - Logger::error("Invalid building type build time for ", identifier, ": ", build_time); - return false; - } - return building_types.add_item({ identifier, max_level, build_time }); + return building_types.add_item({ identifier }); } bool BuildingManager::generate_province_buildings(Province& province) const { @@ -118,10 +182,10 @@ bool BuildingManager::generate_province_buildings(Province& province) const { } bool ret = true; if (!province.get_water()) { - for (BuildingType const& type : building_types.get_items()) { - ret &= province.add_building({ type }); + for (Building const& building : buildings.get_items()) { + ret &= province.add_building({ building }); } } province.lock_buildings(); return ret; -} +} \ No newline at end of file diff --git a/src/openvic-simulation/map/Building.hpp b/src/openvic-simulation/map/Building.hpp index d36dfd4..cd649ff 100644 --- a/src/openvic-simulation/map/Building.hpp +++ b/src/openvic-simulation/map/Building.hpp @@ -1,9 +1,19 @@ #pragma once +#include +#include #include #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" + +#define ARGS BuildingType const& type, sound_t on_completion, fixed_point_t completion_size, level_t max_level, std::map build_cost, \ + Timespan build_time, bool visibility, bool on_map, const bool default_enabled, ProductionType const* production_type, bool pop_build_factory, \ + bool strategic_factory, level_t fort_level, uint64_t naval_capacity, std::vector colonial_points, bool in_province, bool one_per_state, \ + uint64_t colonial_range, fixed_point_t infrastructure, fixed_point_t movement_cost, bool spawn_railway_track namespace OpenVic { @@ -18,31 +28,109 @@ namespace OpenVic { struct Building : HasIdentifier { friend struct BuildingManager; - using level_t = int8_t; - - enum class ExpansionState { - CannotExpand, - CanExpand, - Preparing, - Expanding - }; + using level_t = uint8_t; + using sound_t = std::string_view; private: BuildingType const& type; + const sound_t on_completion; //probably sound played on completion + const fixed_point_t completion_size; + const level_t max_level; + const std::map build_cost; + const Timespan build_time; //time + const bool visibility; + const bool on_map; //onmap + + const bool default_enabled; //some(false) + ProductionType const* production_type; + const bool pop_build_factory; + const bool strategic_factory; + + const level_t fort_level; //some(0), probably the step-per-level? + + const uint64_t naval_capacity; //some(0) + const std::vector colonial_points; + const bool in_province; //province some(false) + const bool one_per_state; + const uint64_t colonial_range; + + const fixed_point_t infrastructure; + const fixed_point_t movement_cost; + const bool spawn_railway_track; //some(false) + + //any modifiers really + + Building(std::string_view identifier, ARGS); + + public: + Building(Building&&) = default; + + BuildingType const& get_type() const; + sound_t get_on_completion() const; + fixed_point_t get_completion_size() const; + level_t get_max_level() const; + std::map const& get_build_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; + + 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; + uint64_t get_colonial_range() const; + + fixed_point_t get_infrastructure() const; + fixed_point_t get_movement_cost() const; + bool spawned_railway_track() const; + }; + + struct BuildingType : HasIdentifier { + friend struct BuildingManager; + + private: + BuildingType(const 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; - Building(BuildingType const& new_type); - bool _can_expand() const; + BuildingInstance(Building const& building); + public: - Building(Building&&) = default; + BuildingInstance(BuildingInstance&&) = default; - BuildingType const& get_type() const; - level_t get_level() const; + 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; @@ -51,22 +139,7 @@ namespace OpenVic { bool expand(); void update_state(Date const& today); void tick(Date const& today); - }; - struct BuildingType : HasIdentifier { - friend struct BuildingManager; - - private: - const Building::level_t max_level; - const Timespan build_time; - - BuildingType(const std::string_view new_identifier, Building::level_t new_max_level, Timespan new_build_time); - - public: - BuildingType(BuildingType&&) = default; - - Building::level_t get_max_level() const; - Timespan get_build_time() const; }; struct Province; @@ -74,12 +147,18 @@ namespace OpenVic { struct BuildingManager { private: IdentifierRegistry building_types; + IdentifierRegistry buildings; public: BuildingManager(); - bool add_building_type(const std::string_view identifier, Building::level_t max_level, Timespan build_time); + bool add_building_type(const std::string_view identifier); IDENTIFIER_REGISTRY_ACCESSORS(BuildingType, building_type) bool generate_province_buildings(Province& province) const; + + bool add_building(std::string_view identifier); + IDENTIFIER_REGISTRY_ACCESSORS(Building, building) + + //bool load_buildings_file(ast::NodeCPtr root); }; } diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index ef574cf..21bdb85 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -1,20 +1,11 @@ #include "Province.hpp" -#include -#include -#include -#include -#include - -#include "openvic-simulation/map/TerrainType.hpp" - using namespace OpenVic; using namespace OpenVic::NodeTools; Province::Province(const std::string_view new_identifier, colour_t new_colour, index_t new_index) : HasIdentifierAndColour { new_identifier, new_colour, false, false }, - index { new_index }, - buildings { "buildings", false } { + index { new_index }, buildings { "buildings", false } { assert(index != NULL_INDEX); } @@ -48,8 +39,8 @@ bool Province::load_positions(BuildingManager const& building_manager, ast::Node return true; } -bool Province::add_building(Building&& building) { - return buildings.add_item(std::move(building)); +bool Province::add_building(BuildingInstance&& building_instance) { + return buildings.add_item(std::move(building_instance)); } void Province::reset_buildings() { @@ -57,7 +48,7 @@ void Province::reset_buildings() { } bool Province::expand_building(const std::string_view building_type_identifier) { - Building* building = buildings.get_item_by_identifier(building_type_identifier); + BuildingInstance* building = buildings.get_item_by_identifier(building_type_identifier); if (building == nullptr) return false; return building->expand(); } @@ -136,13 +127,13 @@ void Province::update_pops() { } void Province::update_state(Date const& today) { - for (Building& building : buildings.get_items()) + for (BuildingInstance& building : buildings.get_items()) building.update_state(today); update_pops(); } void Province::tick(Date const& today) { - for (Building& building : buildings.get_items()) + for (BuildingInstance& building : buildings.get_items()) building.tick(today); } diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 6e4ac7e..349c16d 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include "openvic-simulation/map/Building.hpp" #include "openvic-simulation/pop/Pop.hpp" @@ -46,7 +46,7 @@ namespace OpenVic { Region* region = nullptr; bool has_region = false, water = false; life_rating_t life_rating = 0; - IdentifierRegistry buildings; + IdentifierRegistry buildings; // TODO - change this into a factory-like structure Good const* rgo = nullptr; @@ -73,8 +73,8 @@ namespace OpenVic { life_rating_t get_life_rating() const; bool load_positions(BuildingManager const& building_manager, ast::NodeCPtr root); - bool add_building(Building&& building); - IDENTIFIER_REGISTRY_ACCESSORS(Building, building) + bool add_building(BuildingInstance&& building_instance); + IDENTIFIER_REGISTRY_ACCESSORS(BuildingInstance, building) void reset_buildings(); bool expand_building(const std::string_view building_type_identifier); Good const* get_rgo() const; -- cgit v1.2.3-56-ga3b1