aboutsummaryrefslogtreecommitdiff
path: root/src/openvic
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic')
-rw-r--r--src/openvic/Types.cpp5
-rw-r--r--src/openvic/Types.hpp25
-rw-r--r--src/openvic/economy/Good.cpp3
-rw-r--r--src/openvic/economy/Good.hpp4
-rw-r--r--src/openvic/map/Map.cpp4
-rw-r--r--src/openvic/map/Province.cpp37
-rw-r--r--src/openvic/map/Province.hpp8
-rw-r--r--src/openvic/pop/Culture.cpp3
-rw-r--r--src/openvic/pop/Culture.hpp2
-rw-r--r--src/openvic/pop/Pop.cpp7
-rw-r--r--src/openvic/pop/Pop.hpp2
-rw-r--r--src/openvic/pop/Religion.cpp3
-rw-r--r--src/openvic/pop/Religion.hpp2
13 files changed, 76 insertions, 29 deletions
diff --git a/src/openvic/Types.cpp b/src/openvic/Types.cpp
index ab5d12a..c7ad3ae 100644
--- a/src/openvic/Types.cpp
+++ b/src/openvic/Types.cpp
@@ -29,3 +29,8 @@ std::string HasColour::colour_to_hex_string(colour_t const colour) {
std::string HasColour::colour_to_hex_string() const {
return colour_to_hex_string(colour);
}
+
+HasIdentifierAndColour::HasIdentifierAndColour(std::string const& new_identifier,
+ const colour_t new_colour, bool can_be_null)
+ : HasIdentifier { new_identifier },
+ HasColour { new_colour, can_be_null } {}
diff --git a/src/openvic/Types.hpp b/src/openvic/Types.hpp
index 7740e17..d22f686 100644
--- a/src/openvic/Types.hpp
+++ b/src/openvic/Types.hpp
@@ -3,6 +3,7 @@
#include <algorithm>
#include <cstdint>
#include <map>
+#include <unordered_map>
#include <vector>
#include "utility/Logger.hpp"
@@ -25,6 +26,9 @@ namespace OpenVic {
constexpr colour_t float_to_alpha_value(float a) {
return float_to_colour_byte(a) << 24;
}
+ constexpr float colour_byte_to_float(colour_t colour) {
+ return std::clamp(static_cast<float>(colour) / 255.0f, 0.0f, 1.0f);
+ }
using index_t = uint16_t;
static constexpr index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF;
@@ -58,13 +62,13 @@ namespace OpenVic {
};
/*
- * Base class for objects with associated colour information
+ * Base class for objects with associated colour information.
*/
class HasColour {
const colour_t colour;
protected:
- HasColour(colour_t const new_colour, bool can_be_null);
+ HasColour(const colour_t new_colour, bool can_be_null);
public:
HasColour(HasColour const&) = delete;
@@ -78,6 +82,23 @@ namespace OpenVic {
};
/*
+ * Base class for objects with a unique string identifier
+ * and associated colour information.
+ */
+ class HasIdentifierAndColour : public HasIdentifier, public HasColour {
+ protected:
+ HasIdentifierAndColour(std::string const& new_identifier, const colour_t new_colour, bool can_be_null);
+
+ public:
+ HasIdentifierAndColour(HasIdentifierAndColour const&) = delete;
+ HasIdentifierAndColour(HasIdentifierAndColour&&) = default;
+ HasIdentifierAndColour& operator=(HasIdentifierAndColour const&) = delete;
+ HasIdentifierAndColour& operator=(HasIdentifierAndColour&&) = delete;
+ };
+
+ using distribution_t = std::unordered_map<HasIdentifierAndColour const*, float>;
+
+ /*
* Template for a list of objects with unique string identifiers that can
* be locked to prevent any further additions. The template argument T is
* the type of object that the registry will store, and the second part ensures
diff --git a/src/openvic/economy/Good.cpp b/src/openvic/economy/Good.cpp
index 4524e5f..27d6133 100644
--- a/src/openvic/economy/Good.cpp
+++ b/src/openvic/economy/Good.cpp
@@ -6,8 +6,7 @@ using namespace OpenVic;
Good::Good(std::string const& new_identifier, std::string const& new_category, colour_t new_colour, price_t new_base_price,
bool new_default_available, bool new_tradeable, bool new_currency, bool new_overseas_maintenance)
- : HasIdentifier { new_identifier },
- HasColour { new_colour, true },
+ : HasIdentifierAndColour { new_identifier, new_colour, true },
category { new_category },
base_price { new_base_price },
default_available { new_default_available },
diff --git a/src/openvic/economy/Good.hpp b/src/openvic/economy/Good.hpp
index e159725..833b17a 100644
--- a/src/openvic/economy/Good.hpp
+++ b/src/openvic/economy/Good.hpp
@@ -6,7 +6,7 @@ namespace OpenVic {
struct GoodManager;
/* REQUIREMENTS:
- *
+ *
* ECON-3 , ECON-4 , ECON-5 , ECON-6 , ECON-7 , ECON-8 , ECON-9 , ECON-10, ECON-11, ECON-12, ECON-13, ECON-14,
* ECON-15, ECON-16, ECON-17, ECON-18, ECON-19, ECON-20, ECON-21, ECON-22, ECON-23, ECON-24, ECON-25, ECON-26,
* ECON-27, ECON-28, ECON-29, ECON-30, ECON-31, ECON-32, ECON-33, ECON-34, ECON-35, ECON-36, ECON-37, ECON-38,
@@ -17,7 +17,7 @@ namespace OpenVic {
* ECON-238, ECON-239, ECON-240, ECON-241, ECON-242, ECON-243, ECON-244, ECON-245, ECON-246, ECON-247, ECON-248, ECON-249,
* ECON-250, ECON-251, ECON-252, ECON-253, ECON-254, ECON-255, ECON-256, ECON-257, ECON-258, ECON-259, ECON-260, ECON-261
*/
- struct Good : HasIdentifier, HasColour {
+ struct Good : HasIdentifierAndColour {
friend struct GoodManager;
private:
diff --git a/src/openvic/map/Map.cpp b/src/openvic/map/Map.cpp
index 0f4ed46..21bb6a5 100644
--- a/src/openvic/map/Map.cpp
+++ b/src/openvic/map/Map.cpp
@@ -366,12 +366,14 @@ Pop::pop_size_t Map::get_total_map_population() const {
return_t Map::setup(GoodManager const& good_manager, BuildingManager const& building_manager, PopManager const& pop_manager) {
return_t ret = SUCCESS;
for (Province& province : provinces.get_items()) {
+ province.clear_pops();
// Set all land provinces to have an RGO based on their index to test them
if (!province.is_water() && good_manager.get_good_count() > 0)
province.rgo = good_manager.get_good_by_index(province.get_index() % good_manager.get_good_count());
if (building_manager.generate_province_buildings(province) != SUCCESS) ret = FAILURE;
// Add some pops to the province (for testing purposes)
- pop_manager.generate_test_pops(province);
+ if (!province.is_water())
+ pop_manager.generate_test_pops(province);
}
return ret;
}
diff --git a/src/openvic/map/Province.cpp b/src/openvic/map/Province.cpp
index 75612ad..0b6d1f4 100644
--- a/src/openvic/map/Province.cpp
+++ b/src/openvic/map/Province.cpp
@@ -7,8 +7,7 @@
using namespace OpenVic;
Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour)
- : HasIdentifier { new_identifier },
- HasColour { new_colour, false },
+ : HasIdentifierAndColour { new_identifier, new_colour, false },
index { new_index },
buildings { "buildings" } {
assert(index != NULL_INDEX);
@@ -67,27 +66,47 @@ std::string Province::to_string() const {
}
void Province::add_pop(Pop&& pop) {
- pops.push_back(std::move(pop));
+ if (is_water()) {
+ Logger::error("Trying to add pop to water province ", get_identifier());
+ } else {
+ pops.push_back(std::move(pop));
+ }
+}
+
+void Province::clear_pops() {
+ pops.clear();
+}
+
+Pop::pop_size_t Province::get_total_population() const {
+ return total_population;
+}
+
+distribution_t const& Province::get_pop_type_distribution() const {
+ return pop_types;
+}
+
+distribution_t const& Province::get_culture_distribution() const {
+ return cultures;
}
/* REQUIREMENTS:
* MAP-65
*/
-void Province::update_total_population() {
+void Province::update_pops() {
total_population = 0;
+ pop_types.clear();
+ cultures.clear();
for (Pop const& pop : pops) {
total_population += pop.get_size();
+ pop_types[&pop.get_type()] += pop.get_size();
+ cultures[&pop.get_culture()] += pop.get_size();
}
}
-Pop::pop_size_t Province::get_total_population() const {
- return total_population;
-}
-
void Province::update_state(Date const& today) {
for (Building& building : buildings.get_items())
building.update_state(today);
- update_total_population();
+ update_pops();
}
void Province::tick(Date const& today) {
diff --git a/src/openvic/map/Province.hpp b/src/openvic/map/Province.hpp
index cd90f7d..3ca5d93 100644
--- a/src/openvic/map/Province.hpp
+++ b/src/openvic/map/Province.hpp
@@ -11,7 +11,7 @@ namespace OpenVic {
/* REQUIREMENTS:
* MAP-5, MAP-7, MAP-8, MAP-43, MAP-47
*/
- struct Province : HasIdentifier, HasColour {
+ struct Province : HasIdentifierAndColour {
friend struct Map;
using life_rating_t = int8_t;
@@ -27,6 +27,7 @@ namespace OpenVic {
std::vector<Pop> pops;
Pop::pop_size_t total_population;
+ distribution_t pop_types, cultures;
Province(index_t new_index, std::string const& new_identifier, colour_t new_colour);
@@ -47,8 +48,11 @@ namespace OpenVic {
std::string to_string() const;
void add_pop(Pop&& pop);
- void update_total_population();
+ void clear_pops();
Pop::pop_size_t get_total_population() const;
+ distribution_t const& get_pop_type_distribution() const;
+ distribution_t const& get_culture_distribution() const;
+ void update_pops();
void update_state(Date const& today);
void tick(Date const& today);
diff --git a/src/openvic/pop/Culture.cpp b/src/openvic/pop/Culture.cpp
index 8f72f20..b683804 100644
--- a/src/openvic/pop/Culture.cpp
+++ b/src/openvic/pop/Culture.cpp
@@ -18,8 +18,7 @@ GraphicalCultureType const& CultureGroup::get_unit_graphical_culture_type() cons
Culture::Culture(CultureGroup const& new_group, std::string const& new_identifier,
colour_t new_colour, name_list_t const& new_first_names, name_list_t const& new_last_names)
: group { new_group },
- HasIdentifier { new_identifier },
- HasColour { new_colour, true },
+ HasIdentifierAndColour { new_identifier, new_colour, true },
first_names { new_first_names },
last_names { new_last_names } {
}
diff --git a/src/openvic/pop/Culture.hpp b/src/openvic/pop/Culture.hpp
index 971ef71..657a531 100644
--- a/src/openvic/pop/Culture.hpp
+++ b/src/openvic/pop/Culture.hpp
@@ -32,7 +32,7 @@ namespace OpenVic {
GraphicalCultureType const& get_unit_graphical_culture_type() const;
};
- struct Culture : HasIdentifier, HasColour {
+ struct Culture : HasIdentifierAndColour {
friend struct CultureManager;
using name_list_t = std::vector<std::string>;
diff --git a/src/openvic/pop/Pop.cpp b/src/openvic/pop/Pop.cpp
index babcd25..913d31f 100644
--- a/src/openvic/pop/Pop.cpp
+++ b/src/openvic/pop/Pop.cpp
@@ -35,8 +35,7 @@ PopType::PopType(std::string const& new_identifier, colour_t new_colour,
strata_t new_strata, sprite_t new_sprite,
Pop::pop_size_t new_max_size, Pop::pop_size_t new_merge_max_size,
bool new_state_capital_only, bool new_demote_migrant, bool new_is_artisan, bool new_is_slave)
- : HasIdentifier { new_identifier },
- HasColour { new_colour, true },
+ : HasIdentifierAndColour { new_identifier, new_colour, true },
strata { new_strata },
sprite { new_sprite },
max_size { new_max_size },
@@ -141,8 +140,8 @@ void PopManager::generate_test_pops(Province& province) const {
static PopType const& type_rich = *get_pop_type_by_identifier("test_pop_type_rich");
static Culture const& culture = *culture_manager.get_culture_by_identifier("test_culture");
static Religion const& religion = *religion_manager.get_religion_by_identifier("test_religion");
- province.add_pop({ type_poor, culture, religion, static_cast<Pop::pop_size_t>(province.get_index() * province.get_index() * province.get_index()) * 1000 });
- province.add_pop({ type_middle, culture, religion, static_cast<Pop::pop_size_t>(province.get_index() * province.get_index()) * 1000 });
+ province.add_pop({ type_poor, culture, religion, static_cast<Pop::pop_size_t>(province.get_index() * province.get_index()) * 100 });
+ province.add_pop({ type_middle, culture, religion, static_cast<Pop::pop_size_t>(province.get_index() * province.get_index()) * 50 });
province.add_pop({ type_rich, culture, religion, static_cast<Pop::pop_size_t>(province.get_index()) * 1000 });
} else {
Logger::error("Cannot generate pops before pop types registry is locked!");
diff --git a/src/openvic/pop/Pop.hpp b/src/openvic/pop/Pop.hpp
index 626cd8c..628194d 100644
--- a/src/openvic/pop/Pop.hpp
+++ b/src/openvic/pop/Pop.hpp
@@ -40,7 +40,7 @@ namespace OpenVic {
/* REQUIREMENTS:
* POP-26
*/
- struct PopType : HasIdentifier, HasColour {
+ struct PopType : HasIdentifierAndColour {
friend struct PopManager;
using sprite_t = uint8_t;
diff --git a/src/openvic/pop/Religion.cpp b/src/openvic/pop/Religion.cpp
index a5b2abb..f3d3341 100644
--- a/src/openvic/pop/Religion.cpp
+++ b/src/openvic/pop/Religion.cpp
@@ -9,8 +9,7 @@ ReligionGroup::ReligionGroup(std::string const& new_identifier) : HasIdentifier
Religion::Religion(ReligionGroup const& new_group, std::string const& new_identifier,
colour_t new_colour, icon_t new_icon, bool new_pagan)
: group { new_group },
- HasIdentifier { new_identifier },
- HasColour { new_colour, true },
+ HasIdentifierAndColour { new_identifier, new_colour, true },
icon { new_icon },
pagan { new_pagan } {
assert(icon > 0);
diff --git a/src/openvic/pop/Religion.hpp b/src/openvic/pop/Religion.hpp
index 2a67ce3..e781cf9 100644
--- a/src/openvic/pop/Religion.hpp
+++ b/src/openvic/pop/Religion.hpp
@@ -16,7 +16,7 @@ namespace OpenVic {
ReligionGroup(ReligionGroup&&) = default;
};
- struct Religion : HasIdentifier, HasColour {
+ struct Religion : HasIdentifierAndColour {
friend struct ReligionManager;
using icon_t = uint8_t;