aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2023-11-14 22:47:35 +0100
committer hop311 <hop3114@gmail.com>2023-11-16 20:38:34 +0100
commit886b8b8f396438fc2b7da7d2508f2064d14150a8 (patch)
treeeeed419a7d97ecb58adf63a17eb9184db3e5ed7a /src/openvic-simulation/economy
parent8271b1519e095ee3e7245cde2f0b54561c3ec619 (diff)
Misc changes
Diffstat (limited to 'src/openvic-simulation/economy')
-rw-r--r--src/openvic-simulation/economy/BuildingInstance.cpp52
-rw-r--r--src/openvic-simulation/economy/BuildingInstance.hpp33
-rw-r--r--src/openvic-simulation/economy/BuildingType.cpp (renamed from src/openvic-simulation/economy/Building.cpp)107
-rw-r--r--src/openvic-simulation/economy/BuildingType.hpp (renamed from src/openvic-simulation/economy/Building.hpp)60
-rw-r--r--src/openvic-simulation/economy/EconomyManager.hpp2
-rw-r--r--src/openvic-simulation/economy/Good.cpp14
-rw-r--r--src/openvic-simulation/economy/Good.hpp2
-rw-r--r--src/openvic-simulation/economy/ProductionType.cpp4
-rw-r--r--src/openvic-simulation/economy/ProductionType.hpp2
9 files changed, 120 insertions, 156 deletions
diff --git a/src/openvic-simulation/economy/BuildingInstance.cpp b/src/openvic-simulation/economy/BuildingInstance.cpp
new file mode 100644
index 0000000..417cdda
--- /dev/null
+++ b/src/openvic-simulation/economy/BuildingInstance.cpp
@@ -0,0 +1,52 @@
+#include "BuildingInstance.hpp"
+
+using namespace OpenVic;
+
+BuildingInstance::BuildingInstance(BuildingType const& new_building_type, level_t new_level)
+ : HasIdentifier { building_type.get_identifier() }, building_type { new_building_type }, level { new_level },
+ expansion_state { ExpansionState::CannotExpand } {}
+
+bool BuildingInstance::_can_expand() const {
+ return level < building_type.get_max_level();
+}
+
+void BuildingInstance::set_level(BuildingInstance::level_t new_level) {
+ level = new_level;
+}
+
+bool BuildingInstance::expand() {
+ if (expansion_state == ExpansionState::CanExpand) {
+ expansion_state = ExpansionState::Preparing;
+ expansion_progress = 0.0f;
+ return true;
+ }
+ return false;
+}
+
+/* REQUIREMENTS:
+ * MAP-71, MAP-74, MAP-77
+ */
+void BuildingInstance::update_state(Date today) {
+ switch (expansion_state) {
+ case ExpansionState::Preparing:
+ start_date = today;
+ end_date = start_date + building_type.get_build_time();
+ break;
+ case ExpansionState::Expanding:
+ expansion_progress = static_cast<double>(today - start_date) / static_cast<double>(end_date - start_date);
+ break;
+ default: expansion_state = _can_expand() ? ExpansionState::CanExpand : ExpansionState::CannotExpand;
+ }
+}
+
+void BuildingInstance::tick(Date today) {
+ if (expansion_state == ExpansionState::Preparing) {
+ expansion_state = ExpansionState::Expanding;
+ }
+ if (expansion_state == ExpansionState::Expanding) {
+ if (end_date <= today) {
+ level++;
+ expansion_state = ExpansionState::CannotExpand;
+ }
+ }
+}
diff --git a/src/openvic-simulation/economy/BuildingInstance.hpp b/src/openvic-simulation/economy/BuildingInstance.hpp
new file mode 100644
index 0000000..9fc9df1
--- /dev/null
+++ b/src/openvic-simulation/economy/BuildingInstance.hpp
@@ -0,0 +1,33 @@
+#pragma once
+
+#include "openvic-simulation/economy/BuildingType.hpp"
+
+namespace OpenVic {
+
+ struct BuildingInstance : HasIdentifier { // used in the actual game
+ using level_t = BuildingType::level_t;
+
+ enum class ExpansionState { CannotExpand, CanExpand, Preparing, Expanding };
+
+ private:
+ BuildingType const& PROPERTY(building_type);
+
+ level_t PROPERTY(level);
+ ExpansionState PROPERTY(expansion_state);
+ Date PROPERTY(start_date)
+ Date PROPERTY(end_date);
+ float PROPERTY(expansion_progress);
+
+ bool _can_expand() const;
+
+ public:
+ BuildingInstance(BuildingType const& new_building_type, level_t new_level = 0);
+ BuildingInstance(BuildingInstance&&) = default;
+
+ void set_level(level_t new_level);
+
+ bool expand();
+ void update_state(Date today);
+ void tick(Date today);
+ };
+}
diff --git a/src/openvic-simulation/economy/Building.cpp b/src/openvic-simulation/economy/BuildingType.cpp
index c5707ea..b80999a 100644
--- a/src/openvic-simulation/economy/Building.cpp
+++ b/src/openvic-simulation/economy/BuildingType.cpp
@@ -1,12 +1,10 @@
-#include "Building.hpp"
+#include "BuildingType.hpp"
using namespace OpenVic;
using namespace OpenVic::NodeTools;
-BuildingType::BuildingType(std::string_view new_identifier) : HasIdentifier { new_identifier } {}
-
-Building::Building(
- std::string_view identifier, BuildingType const& type, ARGS
+BuildingType::BuildingType(
+ std::string_view identifier, ARGS
) : HasIdentifier { identifier }, type { type }, modifier { std::move(modifier) }, on_completion { on_completion },
completion_size { completion_size }, max_level { max_level }, goods_cost { std::move(goods_cost) }, cost { cost },
build_time { build_time }, visibility { visibility }, on_map { on_map }, default_enabled { default_enabled },
@@ -16,81 +14,16 @@ Building::Building(
colonial_range { colonial_range }, infrastructure { infrastructure }, spawn_railway_track { spawn_railway_track },
sail { sail }, steam { steam }, capital { capital }, port { port } {}
-BuildingInstance::BuildingInstance(Building const& new_building, level_t new_level)
- : HasIdentifier { building.get_identifier() }, building { new_building }, level { new_level },
- expansion_state { ExpansionState::CannotExpand } {}
-
-bool BuildingInstance::_can_expand() const {
- return level < building.get_max_level();
-}
-
-void BuildingInstance::set_level(BuildingInstance::level_t new_level) {
- level = new_level;
-}
-
-bool BuildingInstance::expand() {
- if (expansion_state == ExpansionState::CanExpand) {
- expansion_state = ExpansionState::Preparing;
- expansion_progress = 0.0f;
- return true;
- }
- return false;
-}
+BuildingManager::BuildingManager() : building_types { "building types" } {}
-/* REQUIREMENTS:
- * MAP-71, MAP-74, MAP-77
- */
-void BuildingInstance::update_state(Date today) {
- switch (expansion_state) {
- case ExpansionState::Preparing:
- start_date = today;
- end_date = start_date + building.get_build_time();
- break;
- case ExpansionState::Expanding:
- expansion_progress = static_cast<double>(today - start_date) / static_cast<double>(end_date - start_date);
- break;
- default: expansion_state = _can_expand() ? ExpansionState::CanExpand : ExpansionState::CannotExpand;
- }
-}
-
-void BuildingInstance::tick(Date today) {
- if (expansion_state == ExpansionState::Preparing) {
- expansion_state = ExpansionState::Expanding;
- }
- if (expansion_state == ExpansionState::Expanding) {
- if (end_date <= today) {
- level++;
- expansion_state = ExpansionState::CannotExpand;
- }
- }
-}
-
-BuildingManager::BuildingManager() : building_types { "building types" }, buildings { "buildings" } {}
-
-bool BuildingManager::add_building_type(std::string_view identifier) {
- if (identifier.empty()) {
- Logger::error("Invalid building type identifier - empty!");
- return false;
- }
- return building_types.add_item({ identifier }, duplicate_ignore_callback);
-}
-
-bool BuildingManager::add_building(std::string_view identifier, BuildingType const* type, ARGS) {
- if (!building_types.is_locked()) {
- Logger::error("Cannot add buildings until building types are locked!");
- return false;
- }
+bool BuildingManager::add_building_type(std::string_view identifier, ARGS) {
if (identifier.empty()) {
Logger::error("Invalid building identifier - empty!");
return false;
}
- if (type == nullptr) {
- Logger::error("Invalid building type for ", identifier, ": null");
- return false;
- }
- return buildings.add_item({
- identifier, *type, std::move(modifier), on_completion, completion_size, max_level, std::move(goods_cost),
+ return building_types.add_item({
+ identifier, type, std::move(modifier), on_completion, completion_size, max_level, std::move(goods_cost),
cost, build_time, visibility, on_map, default_enabled, production_type, pop_build_factory, strategic_factory,
advanced_factory, fort_level, naval_capacity, std::move(colonial_points), in_province, one_per_state,
colonial_range, infrastructure, spawn_railway_track, sail, steam, capital, port
@@ -101,20 +34,14 @@ bool BuildingManager::load_buildings_file(
GoodManager const& good_manager, ProductionTypeManager const& production_type_manager, ModifierManager& modifier_manager,
ast::NodeCPtr root
) {
- bool ret = expect_dictionary_reserve_length(buildings, [this](std::string_view, ast::NodeCPtr value) -> bool {
- return expect_key("type", expect_identifier(
- std::bind(&BuildingManager::add_building_type, this, std::placeholders::_1)
- ))(value);
- })(root);
- lock_building_types();
-
- ret &= expect_dictionary(
+ const bool ret = expect_dictionary_reserve_length(
+ building_types,
[this, &good_manager, &production_type_manager, &modifier_manager](std::string_view key, ast::NodeCPtr value) -> bool {
- BuildingType const* type = nullptr;
+ std::string_view type;
ProductionType const* production_type = nullptr;
std::string_view on_completion;
fixed_point_t completion_size = 0, cost = 0, infrastructure = 0, colonial_range = 0;
- Building::level_t max_level = 0, fort_level = 0;
+ BuildingType::level_t max_level = 0, fort_level = 0;
Good::good_map_t goods_cost;
Timespan build_time;
bool visibility = false, on_map = false, default_enabled = false, pop_build_factory = false;
@@ -126,7 +53,7 @@ bool BuildingManager::load_buildings_file(
ModifierValue modifier;
bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(modifier),
- "type", ONE_EXACTLY, expect_building_type_identifier(assign_variable_callback_pointer(type)),
+ "type", ONE_EXACTLY, expect_identifier(assign_variable_callback(type)),
"on_completion", ZERO_OR_ONE, expect_identifier(assign_variable_callback(on_completion)),
"completion_size", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(completion_size)),
"max_level", ONE_EXACTLY, expect_uint(assign_variable_callback(max_level)),
@@ -160,7 +87,7 @@ bool BuildingManager::load_buildings_file(
"port", ZERO_OR_ONE, expect_bool(assign_variable_callback(port))
)(value);
- ret &= add_building(
+ ret &= add_building_type(
key, type, std::move(modifier), on_completion, completion_size, max_level, std::move(goods_cost), cost,
build_time, visibility, on_map, default_enabled, production_type, pop_build_factory, strategic_factory,
advanced_factory, fort_level, naval_capacity, std::move(colonial_points), in_province, one_per_state,
@@ -170,16 +97,16 @@ bool BuildingManager::load_buildings_file(
return ret;
}
)(root);
- lock_buildings();
+ lock_building_types();
- for (Building const& building : buildings.get_items()) {
+ for (BuildingType const& building_type : building_types.get_items()) {
std::string max_modifier_prefix = "max_";
std::string min_modifier_prefix = "min_build_";
modifier_manager.add_modifier_effect(
- max_modifier_prefix.append(building.get_identifier()), true, ModifierEffect::format_t::INT
+ max_modifier_prefix.append(building_type.get_identifier()), true, ModifierEffect::format_t::INT
);
modifier_manager.add_modifier_effect(
- min_modifier_prefix.append(building.get_identifier()), false, ModifierEffect::format_t::INT
+ min_modifier_prefix.append(building_type.get_identifier()), false, ModifierEffect::format_t::INT
);
}
diff --git a/src/openvic-simulation/economy/Building.hpp b/src/openvic-simulation/economy/BuildingType.hpp
index ed1190b..a49c461 100644
--- a/src/openvic-simulation/economy/Building.hpp
+++ b/src/openvic-simulation/economy/BuildingType.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include "openvic-simulation/Modifier.hpp"
+#include "openvic-simulation/misc/Modifier.hpp"
#include "openvic-simulation/economy/Good.hpp"
#include "openvic-simulation/economy/ProductionType.hpp"
#include "openvic-simulation/types/Date.hpp"
@@ -8,8 +8,8 @@
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#define ARGS \
- ModifierValue&& modifier, std::string_view on_completion, fixed_point_t completion_size, level_t max_level, \
- Good::good_map_t&& goods_cost, fixed_point_t cost, Timespan build_time, bool visibility, bool on_map, \
+ std::string_view type, ModifierValue&& modifier, std::string_view on_completion, fixed_point_t completion_size, \
+ level_t max_level, Good::good_map_t&& 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<fixed_point_t>&& colonial_points, \
bool in_province, bool one_per_state, fixed_point_t colonial_range, fixed_point_t infrastructure, \
@@ -19,28 +19,18 @@ namespace OpenVic {
struct BuildingManager;
- struct BuildingType : HasIdentifier {
- friend struct BuildingManager;
-
- private:
- BuildingType(std::string_view new_identifier);
-
- public:
- BuildingType(BuildingType&&) = default;
- };
-
/* REQUIREMENTS:
* MAP-11, MAP-72, MAP-73
* MAP-12, MAP-75, MAP-76
* MAP-13, MAP-78, MAP-79
*/
- struct Building : HasIdentifier {
+ struct BuildingType : HasIdentifier {
friend struct BuildingManager;
using level_t = int16_t;
private:
- BuildingType const& PROPERTY(type);
+ std::string PROPERTY(type);
ModifierValue PROPERTY(modifier);
std::string PROPERTY(on_completion); // probably sound played on completion
fixed_point_t PROPERTY(completion_size);
@@ -73,56 +63,24 @@ namespace OpenVic {
bool PROPERTY(capital); // only in naval base
bool PROPERTY(port); // only in naval base
- Building(std::string_view identifier, BuildingType const& type, ARGS);
-
- public:
- Building(Building&&) = 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& PROPERTY(building);
-
- level_t PROPERTY(level);
- ExpansionState PROPERTY(expansion_state);
- Date PROPERTY(start_date)
- Date PROPERTY(end_date);
- float PROPERTY(expansion_progress);
-
- bool _can_expand() const;
+ BuildingType(std::string_view identifier, ARGS);
public:
- BuildingInstance(Building const& new_building, level_t new_level = 0);
- BuildingInstance(BuildingInstance&&) = default;
-
- void set_level(level_t new_level);
-
- bool expand();
- void update_state(Date today);
- void tick(Date today);
+ BuildingType(BuildingType&&) = default;
};
struct BuildingManager {
- using level_t = Building::level_t; // this is getting ridiculous
+ using level_t = BuildingType::level_t; // this is getting ridiculous
private:
IdentifierRegistry<BuildingType> building_types;
- IdentifierRegistry<Building> buildings;
public:
BuildingManager();
- bool add_building_type(std::string_view identifier);
+ bool add_building_type(std::string_view identifier, ARGS);
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& modifier_manager, ast::NodeCPtr root
diff --git a/src/openvic-simulation/economy/EconomyManager.hpp b/src/openvic-simulation/economy/EconomyManager.hpp
index 7445614..d53aa7e 100644
--- a/src/openvic-simulation/economy/EconomyManager.hpp
+++ b/src/openvic-simulation/economy/EconomyManager.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include "openvic-simulation/economy/Building.hpp"
+#include "openvic-simulation/economy/BuildingType.hpp"
#include "openvic-simulation/economy/Good.hpp"
#include "openvic-simulation/economy/ProductionType.hpp"
diff --git a/src/openvic-simulation/economy/Good.cpp b/src/openvic-simulation/economy/Good.cpp
index 2b1d694..4c1f7ba 100644
--- a/src/openvic-simulation/economy/Good.cpp
+++ b/src/openvic-simulation/economy/Good.cpp
@@ -64,7 +64,7 @@ bool GoodManager::add_good_category(std::string_view identifier) {
}
bool GoodManager::add_good(
- std::string_view identifier, colour_t colour, GoodCategory const* category, Good::price_t base_price,
+ std::string_view identifier, colour_t colour, GoodCategory const& category, Good::price_t base_price,
bool available_from_start, bool tradeable, bool money, bool overseas_penalty
) {
if (identifier.empty()) {
@@ -75,16 +75,12 @@ bool GoodManager::add_good(
Logger::error("Invalid good colour for ", identifier, ": ", colour_to_hex_string(colour));
return false;
}
- if (category == nullptr) {
- Logger::error("Invalid good category for ", identifier, ": null");
- return false;
- }
if (base_price <= Good::NULL_PRICE) {
Logger::error("Invalid base price for ", identifier, ": ", base_price);
return false;
}
return goods.add_item({
- identifier, colour, *category, base_price, available_from_start,
+ identifier, colour, category, base_price, available_from_start,
tradeable, money, overseas_penalty
});
}
@@ -107,10 +103,8 @@ bool GoodManager::load_goods_file(ast::NodeCPtr root) {
)(root);
lock_good_categories();
goods.reserve(goods.size() + total_expected_goods);
- ret &= expect_dictionary([this](std::string_view good_category_key, ast::NodeCPtr good_category_value) -> bool {
- GoodCategory const* good_category = get_good_category_by_identifier(good_category_key);
-
- return expect_dictionary([this, good_category](std::string_view key, ast::NodeCPtr value) -> bool {
+ ret &= expect_good_category_dictionary([this](GoodCategory const& good_category, ast::NodeCPtr good_category_value) -> bool {
+ return expect_dictionary([this, &good_category](std::string_view key, ast::NodeCPtr value) -> bool {
colour_t colour = NULL_COLOUR;
Good::price_t base_price;
bool available_from_start = true, tradeable = true;
diff --git a/src/openvic-simulation/economy/Good.hpp b/src/openvic-simulation/economy/Good.hpp
index bec5cca..805e6a5 100644
--- a/src/openvic-simulation/economy/Good.hpp
+++ b/src/openvic-simulation/economy/Good.hpp
@@ -73,7 +73,7 @@ namespace OpenVic {
IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(good_category, good_categories)
bool add_good(
- std::string_view identifier, colour_t colour, GoodCategory const* category, Good::price_t base_price,
+ std::string_view identifier, colour_t colour, GoodCategory const& category, Good::price_t base_price,
bool available_from_start, bool tradeable, bool money, bool overseas_penalty
);
IDENTIFIER_REGISTRY_ACCESSORS(good)
diff --git a/src/openvic-simulation/economy/ProductionType.cpp b/src/openvic-simulation/economy/ProductionType.cpp
index 1c65c4d..6eb7ff9 100644
--- a/src/openvic-simulation/economy/ProductionType.cpp
+++ b/src/openvic-simulation/economy/ProductionType.cpp
@@ -145,7 +145,7 @@ node_callback_t ProductionTypeManager::_expect_employed_pop_list(
return false; \
}
-bool ProductionTypeManager::add_production_type(PRODUCTION_TYPE_ARGS, GoodManager const& good_manager) {
+bool ProductionTypeManager::add_production_type(PRODUCTION_TYPE_ARGS) {
if (identifier.empty()) {
Logger::error("Invalid production type identifier - empty!");
return false;
@@ -274,7 +274,7 @@ bool ProductionTypeManager::load_production_types_file(
ret &= add_production_type(
key, owner, employees, type, workforce, std::move(input_goods), output_goods, value, std::move(bonuses),
- std::move(efficiency), coastal, farm, mine, good_manager
+ std::move(efficiency), coastal, farm, mine
);
return ret;
}
diff --git a/src/openvic-simulation/economy/ProductionType.hpp b/src/openvic-simulation/economy/ProductionType.hpp
index 802580b..13862a7 100644
--- a/src/openvic-simulation/economy/ProductionType.hpp
+++ b/src/openvic-simulation/economy/ProductionType.hpp
@@ -101,7 +101,7 @@ namespace OpenVic {
public:
ProductionTypeManager();
- bool add_production_type(PRODUCTION_TYPE_ARGS, GoodManager const& good_manager);
+ bool add_production_type(PRODUCTION_TYPE_ARGS);
IDENTIFIER_REGISTRY_ACCESSORS(production_type)
bool load_production_types_file(GoodManager const& good_manager, PopManager const& pop_manager, ast::NodeCPtr root);