Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/openvic-simulation/core/template/Concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,9 @@ namespace OpenVic {
concept mul_add_assignable = requires(Lhs& lhs, const A a, const B b) {
{ lhs += a * b } -> std::same_as<Lhs&>;
};

template<typename Lhs, typename Rhs = Lhs>
concept equalable = requires(Lhs const& lhs, Rhs const& rhs) {
{ lhs == rhs } -> std::convertible_to<bool>;
};
}
12 changes: 6 additions & 6 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include "openvic-simulation/types/Date.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/IndexedFlatMap.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/types/UnitBranchType.hpp"
#include "openvic-simulation/utility/Containers.hpp"
#include "openvic-simulation/utility/Logger.hpp"
Expand Down Expand Up @@ -1365,13 +1365,13 @@ void CountryInstance::_update_budget() {
if (total_non_colonial_population == 0) {
administrative_efficiency_from_administrators.set(fixed_point_t::_1);
administrator_percentage.set(fixed_point_t::_0);
} else {
administrator_percentage.set(fixed_point_t::parse(administrators) / total_non_colonial_population);
} else {
administrator_percentage.set(fixed_point_t::parse(type_safe::get(administrators)) / type_safe::get(total_non_colonial_population));

const fixed_point_t desired_administrators = desired_administrator_percentage.get_untracked() * total_non_colonial_population;
const fixed_point_t desired_administrators = desired_administrator_percentage.get_untracked() * type_safe::get(total_non_colonial_population);
const fixed_point_t administrative_efficiency_from_administrators_unclamped = std::min(
fixed_point_t::mul_div(
administrators,
type_safe::get(administrators),
fixed_point_t::_1 + get_modifier_effect_value(*modifier_effect_cache.get_administrative_efficiency()),
desired_administrators
)
Expand All @@ -1397,7 +1397,7 @@ void CountryInstance::_update_budget() {
projected_education_spending_unscaled_by_slider_running_total += size * education_salary_base_by_pop_type.at(pop_type).get_untracked();
projected_military_spending_unscaled_by_slider_running_total += size * military_salary_base_by_pop_type.at(pop_type).get_untracked();
projected_pensions_spending_unscaled_by_slider_running_total += size * calculate_pensions_base(pop_type);
projected_unemployment_subsidies_spending_unscaled_by_slider_running_total += get_unemployed_pops_by_type(pop_type)
projected_unemployment_subsidies_spending_unscaled_by_slider_running_total += type_safe::get(get_unemployed_pops_by_type(pop_type))
* calculate_unemployment_subsidies_base(pop_type);
}

Expand Down
52 changes: 40 additions & 12 deletions src/openvic-simulation/dataloader/NodeTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,49 @@ using namespace std::string_view_literals;
return expect_uint(callback, base);
}

template<derived_from_specialization_of<type_safe::strong_typedef> T, typename AsT = type_safe::underlying_type<T>>
NodeCallback auto expect_strong_typedef(callback_t<T>& callback, int base = 10) {
if constexpr (std::unsigned_integral<AsT>) {
return expect_uint64(
[callback](uint64_t val) mutable -> bool {
if (val <= static_cast<uint64_t>(std::numeric_limits<AsT>::max())) {
return callback(T(val));
}
spdlog::error_s(
"Invalid uint: {} (valid range: [0, {}])", val,
static_cast<uint64_t>(std::numeric_limits<AsT>::max())
);
return false;
},
base
);
} else {
return expect_int64(
[callback](int64_t val) mutable -> bool {
if (val >= static_cast<int64_t>(std::numeric_limits<AsT>::min()) &&
val <= static_cast<int64_t>(std::numeric_limits<AsT>::max())) {
return callback(T(val));
}
spdlog::error_s(
"Invalid int: {} (valid range: [{}, {}])", val,
static_cast<int64_t>(std::numeric_limits<AsT>::min()),
static_cast<int64_t>(std::numeric_limits<AsT>::max())
);
return false;
},
base
);
}
}
template<derived_from_specialization_of<type_safe::strong_typedef> T, typename AsT = type_safe::underlying_type<T>>
NodeCallback auto expect_strong_typedef(callback_t<T>&& callback, int base = 10) {
return expect_strong_typedef<T, AsT>(callback, base);
}

template<derived_from_specialization_of<type_safe::strong_typedef> T>
requires std::unsigned_integral<type_safe::underlying_type<T>>
NodeCallback auto expect_index(callback_t<T>& callback, int base = 10) {
using underlying_type = type_safe::underlying_type<T>;

return expect_uint64([callback](uint64_t val) mutable -> bool {
if (val <= static_cast<uint64_t>(std::numeric_limits<underlying_type>::max())) {
return callback(T(val));
}
spdlog::error_s(
"Invalid uint: {} (valid range: [0, {}])",
val, static_cast<uint64_t>(std::numeric_limits<underlying_type>::max())
);
return false;
}, base);
return expect_strong_typedef<T>(callback, base);
}
template<derived_from_specialization_of<type_safe::strong_typedef> T>
requires std::unsigned_integral<type_safe::underlying_type<T>>
Expand Down
4 changes: 2 additions & 2 deletions src/openvic-simulation/defines/MilitaryDefines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ node_callback_t MilitaryDefines::expect_defines() {
"REINFORCE_SPEED", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(reinforce_speed)),
"COMBAT_DIFFICULTY_IMPACT", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(combat_difficulty_impact)),
"BASE_COMBAT_WIDTH", ONE_EXACTLY, expect_uint(assign_variable_callback(base_combat_width)),
"POP_MIN_SIZE_FOR_REGIMENT", ONE_EXACTLY, expect_uint(assign_variable_callback(min_pop_size_for_regiment)),
"POP_SIZE_PER_REGIMENT", ONE_EXACTLY, expect_uint(assign_variable_callback(pop_size_per_regiment)),
"POP_MIN_SIZE_FOR_REGIMENT", ONE_EXACTLY, expect_strong_typedef<pop_size_t>(assign_variable_callback(min_pop_size_for_regiment)),
"POP_SIZE_PER_REGIMENT", ONE_EXACTLY, expect_strong_typedef<pop_size_t>(assign_variable_callback(pop_size_per_regiment)),
"SOLDIER_TO_POP_DAMAGE", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(soldier_to_pop_damage)),
"LAND_SPEED_MODIFIER", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(land_speed_modifier)),
"NAVAL_SPEED_MODIFIER", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(naval_speed_modifier)),
Expand Down
2 changes: 1 addition & 1 deletion src/openvic-simulation/defines/MilitaryDefines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/types/Date.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/utility/Getters.hpp"

namespace OpenVic {
Expand Down
4 changes: 2 additions & 2 deletions src/openvic-simulation/defines/PopsDefines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ node_callback_t PopsDefines::expect_defines() {
"MOVEMENT_SUPPORT_UH_FACTOR", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(movement_support_uh_factor)),
"REBEL_OCCUPATION_STRENGTH_BONUS", ONE_EXACTLY,
expect_fixed_point(assign_variable_callback(rebel_occupation_strength_bonus)),
"LARGE_POPULATION_LIMIT", ONE_EXACTLY, expect_uint(assign_variable_callback(large_population_limit)),
"LARGE_POPULATION_LIMIT", ONE_EXACTLY, expect_strong_typedef<pop_size_t>(assign_variable_callback(large_population_limit)),
"LARGE_POPULATION_INFLUENCE_PENALTY_CHUNK", ONE_EXACTLY,
expect_uint(assign_variable_callback(large_population_influence_penalty_chunk))
expect_strong_typedef<pop_size_t>(assign_variable_callback(large_population_influence_penalty_chunk))
);
}
2 changes: 1 addition & 1 deletion src/openvic-simulation/defines/PopsDefines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/types/ProvinceLifeRating.hpp"
#include "openvic-simulation/utility/Getters.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ void ArtisanalProducer::artisan_tick_handler::calculate_inputs(
//throughput scalar, the minimum of stockpile / base_desired_quantity
//inputs_bought_fraction uses base_desired_quantity as population size is cancelled in the production and input calculations.
const pop_size_t pop_size = pop.get_size();
fixed_point_t inputs_bought_numerator = pop_size,
inputs_bought_denominator = production_type.base_workforce_size,
fixed_point_t inputs_bought_numerator = type_safe::get(pop_size),
inputs_bought_denominator = type_safe::get(production_type.base_workforce_size),
inputs_bought_fraction_v = inputs_bought_numerator / inputs_bought_denominator;

distinct_goods_to_buy = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
#include "openvic-simulation/types/fixed_point/Fraction.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/utility/Getters.hpp"

namespace OpenVic {
Expand All @@ -22,6 +21,7 @@ namespace OpenVic {
struct ProductionType;
struct ProvinceInstance;
struct RandomU32;
struct pop_size_t;

struct ArtisanalProducer {
private:
Expand Down
2 changes: 1 addition & 1 deletion src/openvic-simulation/economy/production/Employee.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"

namespace OpenVic {
struct CountryInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
#include "openvic-simulation/types/OrderedContainers.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/utility/Getters.hpp"

namespace OpenVic {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ bool ProductionTypeManager::load_production_types_file(
"employees", ZERO_OR_ONE, _expect_job_list(good_definition_manager, pop_manager, move_variable_callback(jobs)),
"type", ZERO_OR_ONE,
expect_identifier(expect_mapped_string(template_type_map, assign_variable_callback(template_type))),
"workforce", ZERO_OR_ONE, expect_uint(assign_variable_callback(base_workforce_size)),
"workforce", ZERO_OR_ONE, expect_strong_typedef<pop_size_t>(assign_variable_callback(base_workforce_size)),
"input_goods", ZERO_OR_ONE,
good_definition_manager.expect_good_definition_decimal_map(move_variable_callback(input_goods)),
"output_goods", ZERO_OR_ONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "openvic-simulation/types/IdentifierRegistry.hpp"
#include "openvic-simulation/types/IndexedFlatMap.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/types/PopSprite.hpp"
#include "openvic-simulation/utility/Containers.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
#include "openvic-simulation/population/Pop.hpp"
#include "openvic-simulation/population/PopType.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/types/TypedIndices.hpp"
#include "openvic-simulation/utility/Logger.hpp"
#include "openvic-simulation/utility/Containers.hpp"

#include <type_safe/strong_typedef.hpp>

using namespace OpenVic;

ResourceGatheringOperation::ResourceGatheringOperation(
Expand Down Expand Up @@ -71,14 +73,14 @@ void ResourceGatheringOperation::initialise_rgo_size_multiplier() {
}

const fixed_point_t size_modifier = calculate_size_modifier();
const fixed_point_t base_workforce_size = production_type.base_workforce_size;
const fixed_point_t base_workforce_size = type_safe::get(production_type.base_workforce_size);
if (size_modifier == 0) {
size_multiplier = 0;
} else {
size_multiplier = ((total_worker_count_in_province / (size_modifier * base_workforce_size)).ceil() * fixed_point_t::_1_50).floor();
}

max_employee_count_cache = (size_modifier * size_multiplier * base_workforce_size).floor<pop_size_t>();
max_employee_count_cache = (size_modifier * size_multiplier * base_workforce_size).floor<type_safe::underlying_type<pop_size_t>>();
}

fixed_point_t ResourceGatheringOperation::calculate_size_modifier() const {
Expand Down Expand Up @@ -187,7 +189,7 @@ void ResourceGatheringOperation::hire() {
proportion_to_hire = 1;
} else {
//hire all pops proportionally
const fixed_point_t max_worker_count_real = max_employee_count_cache, available_worker_count_real = available_worker_count;
const fixed_point_t max_worker_count_real = type_safe::get(max_employee_count_cache), available_worker_count_real = type_safe::get(available_worker_count);
proportion_to_hire = max_worker_count_real / available_worker_count_real;
}

Expand All @@ -197,7 +199,7 @@ void ResourceGatheringOperation::hire() {
for (Job const& job : jobs) {
PopType const* const job_pop_type = job.get_pop_type();
if (job_pop_type && *job_pop_type == pop_type) {
const pop_size_t pop_size_to_hire = static_cast<pop_size_t>((proportion_to_hire * pop.get_size()).floor());
const pop_size_t pop_size_to_hire = (proportion_to_hire * pop.get_size()).floor<type_safe::underlying_type<pop_size_t>>();
if (pop_size_to_hire <= 0) {
continue;
}
Expand Down Expand Up @@ -306,7 +308,7 @@ fixed_point_t ResourceGatheringOperation::produce() {

const fixed_point_t effect_multiplier = job.get_effect_multiplier();
fixed_point_t relative_to_workforce =
fixed_point_t::parse(employees_of_type) / fixed_point_t::parse(max_employee_count_cache);
fixed_point_t::parse(type_safe::get(employees_of_type)) / fixed_point_t::parse(type_safe::get(max_employee_count_cache));
const fixed_point_t amount = job.get_amount();
if (effect_multiplier != fixed_point_t::_1 && relative_to_workforce > amount) {
relative_to_workforce = amount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "openvic-simulation/economy/production/Employee.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/IndexedFlatMap.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/utility/Getters.hpp"
#include "openvic-simulation/utility/Containers.hpp"

Expand Down
4 changes: 2 additions & 2 deletions src/openvic-simulation/map/Mapmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "openvic-simulation/population/Culture.hpp"
#include "openvic-simulation/population/Religion.hpp"
#include "openvic-simulation/types/OrderedContainersMath.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"

using namespace OpenVic;
using namespace OpenVic::colour_literals;
Expand Down Expand Up @@ -266,7 +266,7 @@ bool MapmodeManager::setup_mapmodes(MapDefinition const& map_definition) {
// by the same country, relative to the most populous province in that set of provinces
if (!province.province_definition.is_water()) {
const colour_argb_t::value_type val = colour_argb_t::colour_traits::component_from_fraction(
province.get_total_population(), map_instance.get_highest_province_population() + 1, 0.1f, 1.0f
type_safe::get(province.get_total_population()), type_safe::get(map_instance.get_highest_province_population()) + 1, 0.1f, 1.0f
);
return colour_argb_t { 0, val, 0, ALPHA_VALUE };
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/openvic-simulation/population/Pop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fixed_point_t Pop::get_unemployment_fraction() const {
if (!type->can_be_unemployed) {
return 0;
}
return fixed_point_t::parse(get_unemployed()) / size;
return fixed_point_t::parse(type_safe::get(get_unemployed())) / size;
}

void Pop::setup_pop_test_values(IssueManager const& issue_manager) {
Expand Down Expand Up @@ -120,7 +120,7 @@ void Pop::setup_pop_test_values(IssueManager const& issue_manager) {
for (Ideology const& ideology : supporter_equivalents_by_ideology.get_keys()) {
test_weight_indexed(supporter_equivalents_by_ideology, ideology, 1, 5);
}
supporter_equivalents_by_ideology.rescale(size);
supporter_equivalents_by_ideology.rescale(type_safe::get(size));

supporter_equivalents_by_issue.clear();
for (BaseIssue const& issue : issue_manager.get_party_policies()) {
Expand All @@ -131,14 +131,14 @@ void Pop::setup_pop_test_values(IssueManager const& issue_manager) {
test_weight_ordered(supporter_equivalents_by_issue, reform, 3, 6);
}
}
rescale_fixed_point_map(supporter_equivalents_by_issue, size);
rescale_fixed_point_map(supporter_equivalents_by_issue, type_safe::get(size));

if (!vote_equivalents_by_party.empty()) {
for (auto& [party, value] : vote_equivalents_by_party) {
vote_equivalents_by_party[party] = 0;
test_weight_ordered(vote_equivalents_by_party, party, 4, 10);
}
rescale_fixed_point_map(vote_equivalents_by_party, size);
rescale_fixed_point_map(vote_equivalents_by_party, type_safe::get(size));
}

/* Returns a fixed point between 0 and max. */
Expand Down Expand Up @@ -255,8 +255,8 @@ void Pop::update_gamestate(
) {
max_supported_regiments = 0;
} else {
max_supported_regiments = (fixed_point_t::parse(size) / (
fixed_point_t::parse(military_defines.get_pop_size_per_regiment()) * pop_size_per_regiment_multiplier
max_supported_regiments = (fixed_point_t::parse(type_safe::get(size)) / (
fixed_point_t::parse(type_safe::get(military_defines.get_pop_size_per_regiment())) * pop_size_per_regiment_multiplier
)).floor<size_t>() + 1;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/openvic-simulation/population/Pop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
#include "openvic-simulation/types/IndexedFlatMap.hpp"
#include "openvic-simulation/types/PopSize.hpp"
#include "openvic-simulation/population/PopSize.hpp"
#include "openvic-simulation/types/UnitBranchType.hpp"
#include "openvic-simulation/utility/Containers.hpp"
#include "openvic-simulation/core/portable/ForwardableSpan.hpp"

#include <type_safe/strong_typedef.hpp>

namespace OpenVic {
struct BaseIssue;
struct BuyResult;
Expand Down Expand Up @@ -83,7 +85,7 @@ namespace OpenVic {
return static_cast<uint8_t>(allowed) <= static_cast<uint8_t>(status);
}

static constexpr pop_size_t MAX_SIZE = std::numeric_limits<pop_size_t>::max();
static constexpr pop_size_t MAX_SIZE = std::numeric_limits<type_safe::underlying_type<pop_size_t>>::max();

private:
MarketInstance& market_instance;
Expand Down
9 changes: 6 additions & 3 deletions src/openvic-simulation/population/PopManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "openvic-simulation/modifier/ModifierManager.hpp"
#include "openvic-simulation/core/FormatValidate.hpp"
#include "openvic-simulation/utility/Logger.hpp"
#include "openvic-simulation/population/PopSize.hpp"

#include <type_safe/strong_typedef.hpp>

using namespace OpenVic;
using namespace OpenVic::NodeTools;
Expand Down Expand Up @@ -279,8 +282,8 @@ bool PopManager::load_pop_type_file(
"sprite", ONE_EXACTLY, expect_uint(assign_variable_callback(sprite)),
"color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)),
"is_artisan", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_artisan)),
"max_size", ZERO_OR_ONE, expect_uint(assign_variable_callback(max_size)),
"merge_max_size", ZERO_OR_ONE, expect_uint(assign_variable_callback(merge_max_size)),
"max_size", ZERO_OR_ONE, expect_strong_typedef<pop_size_t>(assign_variable_callback(max_size)),
"merge_max_size", ZERO_OR_ONE, expect_strong_typedef<pop_size_t>(assign_variable_callback(merge_max_size)),
"strata", ONE_EXACTLY, stratas.expect_item_identifier(assign_variable_callback_pointer(strata)),
"state_capital_only", ZERO_OR_ONE, expect_bool(assign_variable_callback(state_capital_only)),
"research_points", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(research_points)),
Expand Down Expand Up @@ -472,7 +475,7 @@ bool PopManager::load_pop_bases_into_vector(
return true;
}

if (culture != nullptr && religion != nullptr && size >= 1 && size <= std::numeric_limits<pop_size_t>::max()) {
if (culture != nullptr && religion != nullptr && size >= 1 && size <= std::numeric_limits<type_safe::underlying_type<pop_size_t>>::max()) {
vec.emplace_back(PopBase { type, *culture, *religion, size.floor<int32_t>(), militancy, consciousness, rebel_type });
} else {
spdlog::warn_s(
Expand Down
Loading