Skip to content
Merged
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
30 changes: 20 additions & 10 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ void CountryInstance::_update_budget() {
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);
administrator_percentage.set(fixed_point_t(administrators) / total_non_colonial_population);

const fixed_point_t desired_administrators = desired_administrator_percentage.get_untracked() * total_non_colonial_population;
const fixed_point_t administrative_efficiency_from_administrators_unclamped = std::min(
Expand Down Expand Up @@ -1563,8 +1563,11 @@ void CountryInstance::_update_military() {
}

military_power_from_land.set(
supply_consumption * fixed_point_t::parse(regular_army_size) * sum_of_regiment_type_stats
/ fixed_point_t::parse(7 * (1 + unit_type_manager.get_regiment_type_count()))
supply_consumption * fixed_point_t::mul_div(
sum_of_regiment_type_stats,
fixed_point_t::parse_raw(regular_army_size),
fixed_point_t::parse_raw(7 * (1 + unit_type_manager.get_regiment_type_count()))
)
);

if (disarmed) {
Expand All @@ -1588,7 +1591,12 @@ void CountryInstance::_update_military() {
military_power_from_sea.set(military_power_from_sea_running_total / 250);

military_power_from_leaders.set(
fixed_point_t::parse(std::min(get_leader_count(), deployed_non_mobilised_regiments))
fixed_point_t::parse_capped(
std::min(
get_leader_count(),
deployed_non_mobilised_regiments
)
)
);

// Mobilisation calculations
Expand All @@ -1598,8 +1606,8 @@ void CountryInstance::_update_military() {

// TODO - use country_defines.get_min_mobilize_limit(); (wiki: "lowest maximum of brigades you can mobilize. (by default 3)")

mobilisation_max_regiment_count =
((fixed_point_t::_1 + mobilisation_impact) * fixed_point_t::parse(regiment_count)).floor<size_t>();
mobilisation_max_regiment_count = regiment_count
+ fixed_point_t::multiply_truncate(regiment_count, mobilisation_impact);

mobilisation_potential_regiment_count = 0; // TODO - calculate max regiments from poor citizens
if (mobilisation_potential_regiment_count > mobilisation_max_regiment_count) {
Expand All @@ -1626,10 +1634,12 @@ void CountryInstance::_update_military() {
naval_unit_start_experience += get_modifier_effect_value(*modifier_effect_cache.get_naval_unit_start_experience());

recruit_time = fixed_point_t::_1 + get_modifier_effect_value(*modifier_effect_cache.get_unit_recruitment_time());
combat_width = ( //
fixed_point_t::parse(military_defines.get_base_combat_width()) +
get_modifier_effect_value(*modifier_effect_cache.get_combat_width_additive())
).floor<int32_t>();
combat_width = combat_width_t(
(
type_safe::get(military_defines.get_base_combat_width())
+ get_modifier_effect_value(*modifier_effect_cache.get_combat_width_additive())
).floor<type_safe::underlying_type<combat_width_t>>()
);
dig_in_cap = get_modifier_effect_value(*modifier_effect_cache.get_dig_in_cap()).floor<int32_t>();
military_tactics = military_defines.get_base_military_tactics() +
get_modifier_effect_value(*modifier_effect_cache.get_military_tactics());
Expand Down
5 changes: 3 additions & 2 deletions src/openvic-simulation/country/CountryInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <fmt/base.h>

#include "openvic-simulation/diplomacy/CountryRelation.hpp"
#include "openvic-simulation/military/CombatWidth.hpp"
#include "openvic-simulation/military/UnitBranchedGetterMacro.hpp"
#include "openvic-simulation/modifier/ModifierSum.hpp"
#include "openvic-simulation/politics/Rule.hpp"
Expand Down Expand Up @@ -100,7 +101,7 @@ namespace OpenVic {
};

// Thresholds for different uncivilised country statuses
static constexpr fixed_point_t PRIMITIVE_CIVILISATION_PROGRESS = fixed_point_t::parse(15) / 100;
static constexpr fixed_point_t PRIMITIVE_CIVILISATION_PROGRESS = fixed_point_t(15) / 100;
static constexpr fixed_point_t UNCIVILISED_CIVILISATION_PROGRESS = fixed_point_t::_0_50;

private:
Expand Down Expand Up @@ -382,7 +383,7 @@ namespace OpenVic {
fixed_point_t PROPERTY(land_unit_start_experience);
fixed_point_t PROPERTY(naval_unit_start_experience);
fixed_point_t PROPERTY(recruit_time);
int32_t PROPERTY(combat_width, 1);
combat_width_t PROPERTY(combat_width, combat_width_t(1));
int32_t PROPERTY(dig_in_cap, 0);
fixed_point_t PROPERTY(military_tactics);
OV_IFLATMAP_PROPERTY(RegimentType, technology_unlock_level_t, regiment_type_unlock_levels);
Expand Down
11 changes: 10 additions & 1 deletion src/openvic-simulation/defines/MilitaryDefines.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "MilitaryDefines.hpp"

#include <cstdint>

#include "openvic-simulation/military/CombatWidth.hpp"

#include <type_safe/strong_typedef.hpp>

using namespace OpenVic;
using namespace OpenVic::NodeTools;

Expand All @@ -14,7 +20,10 @@ node_callback_t MilitaryDefines::expect_defines() {
"DIG_IN_INCREASE_EACH_DAYS", ONE_EXACTLY, expect_days(assign_variable_callback(dig_in_increase_each_days)),
"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)),
"BASE_COMBAT_WIDTH", ONE_EXACTLY, expect_int<int8_t>([this](int8_t val)->bool{
base_combat_width = combat_width_t(static_cast<type_safe::underlying_type<combat_width_t>>(val));
return true;
}),
"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)),
"SOLDIER_TO_POP_DAMAGE", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(soldier_to_pop_damage)),
Expand Down
3 changes: 2 additions & 1 deletion src/openvic-simulation/defines/MilitaryDefines.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/military/CombatWidth.hpp"
#include "openvic-simulation/types/Date.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/PopSize.hpp"
Expand All @@ -16,7 +17,7 @@ namespace OpenVic {
Timespan PROPERTY(dig_in_increase_each_days);
fixed_point_t PROPERTY(reinforce_speed);
fixed_point_t PROPERTY(combat_difficulty_impact);
size_t PROPERTY(base_combat_width, 0);
combat_width_t PROPERTY(base_combat_width, combat_width_t(0));
pop_size_t PROPERTY(min_pop_size_for_regiment, 0);
pop_size_t PROPERTY(pop_size_per_regiment, 0);
fixed_point_t PROPERTY(soldier_to_pop_damage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ ProductionType const* ArtisanalProducer::pick_production_type(
}

if (current_score == score_estimate) {
relative_score = fixed_point_t::parse(ranked_artisanal_production_types.size() - i)
relative_score = fixed_point_t(static_cast<int32_t>(ranked_artisanal_production_types.size() - i))
/ static_cast<int32_t>(1 + ranked_artisanal_production_types.size());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,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(employees_of_type) / fixed_point_t(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
26 changes: 26 additions & 0 deletions src/openvic-simulation/military/CombatWidth.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <cstdint>

#include <fmt/base.h>
#include <fmt/format.h>

#include <type_safe/strong_typedef.hpp>

namespace OpenVic {
struct combat_width_t : type_safe::strong_typedef<combat_width_t, std::int8_t>,
type_safe::strong_typedef_op::equality_comparison<combat_width_t>,
type_safe::strong_typedef_op::relational_comparison<combat_width_t>,
type_safe::strong_typedef_op::integer_arithmetic<combat_width_t>,
type_safe::strong_typedef_op::mixed_addition<combat_width_t, std::uint8_t>,
type_safe::strong_typedef_op::mixed_subtraction<combat_width_t, std::uint8_t> {
using strong_typedef::strong_typedef;
};
}

template<>
struct fmt::formatter<OpenVic::combat_width_t> : fmt::formatter<std::int32_t> {
fmt::format_context::iterator format(OpenVic::combat_width_t const& value, fmt::format_context& ctx) const {
return fmt::formatter<std::int32_t>::format(type_safe::get(value), ctx);
}
};
6 changes: 3 additions & 3 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(get_unemployed()) / size;
}

void Pop::setup_pop_test_values(IssueManager const& issue_manager) {
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(size) / (
fixed_point_t(military_defines.get_pop_size_per_regiment()) * pop_size_per_regiment_multiplier
)).floor<size_t>() + 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/openvic-simulation/population/PopsAggregate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void PopsAggregate::add_pops_aggregate(PopsAggregate& part) {
_yesterdays_import_value_running_total += part.get_yesterdays_import_value_untracked();

// TODO - change casting if pop_size_t changes type
const fixed_point_t part_population = fixed_point_t::parse(part.get_total_population());
const fixed_point_t part_population = fixed_point_t(part.get_total_population());
average_literacy += part.get_average_literacy() * part_population;
average_consciousness += part.get_average_consciousness() * part_population;
average_militancy += part.get_average_militancy() * part_population;
Expand Down
2 changes: 1 addition & 1 deletion src/openvic-simulation/scripts/ConditionalWeight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ node_callback_t ConditionalWeight<TYPE>::expect_conditional_weight() {
const auto time_callback = [this](std::string_view key, Timespan (*to_timespan)(Timespan::day_t)) -> auto {
return [this, key, to_timespan](uint32_t value) -> bool {
if (base == 0) {
base = fixed_point_t::parse((*to_timespan)(value).to_int());
base = fixed_point_t::parse_capped((*to_timespan)(value).to_int());
return true;
} else {
spdlog::error_s(
Expand Down
38 changes: 34 additions & 4 deletions src/openvic-simulation/types/fixed_point/FixedPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ namespace OpenVic {
return static_cast<T>(*this);
}

template<std::integral T>
OV_SPEED_INLINE static constexpr T multiply_truncate(T const& integer, fixed_point_t const& fp) {
return static_cast<T>(
(static_cast<value_type>(integer) * fp.get_raw_value()) >> PRECISION
);
}

template<std::floating_point T>
OV_SPEED_INLINE explicit constexpr operator T() const {
return value / static_cast<T>(ONE);
Expand Down Expand Up @@ -402,9 +409,28 @@ namespace OpenVic {
return fixed_point_t { raw_value, value };
}

// Deterministic
OV_SPEED_INLINE static constexpr fixed_point_t parse(int64_t value) {
return parse_raw(value << PRECISION);
template<std::integral T>
static constexpr fixed_point_t parse_capped(const T value) {
fixed_point_t result;
if (value > std::numeric_limits<int32_t>::max()) {
if (value >= fixed_point_t::max.truncate<T>()) {
if (std::is_constant_evaluated()) {
assert(value >= fixed_point_t::max.truncate<T>());
} else {
spdlog::error_s("parse_capped value exceeded int32 max. Falling back to fixed_point_t::max");
}
result = fixed_point_t::max;
} else {
if (!std::is_constant_evaluated()) {
spdlog::warn_s("parse_capped value exceeded int32 max. It still fits but exceeds fixed_point_t::usable_max");
}
result = fixed_point_t::parse_raw(value << fixed_point_t::PRECISION);
}
} else {
result = fixed_point_t(static_cast<int32_t>(value));
}

return result;
}

// Deterministic
Expand Down Expand Up @@ -683,7 +709,11 @@ namespace OpenVic {
int64_t parsed_value = 0;
std::from_chars_result result = string_to_int64(str, end, parsed_value);
if (result.ec == std::errc{}) {
value = parse(parsed_value);
if (parsed_value > std::numeric_limits<int32_t>::max()) {
result.ec = std::errc::value_too_large;
} else {
value = fixed_point_t(static_cast<int32_t>(parsed_value));
}
}
return result;
}
Expand Down
49 changes: 34 additions & 15 deletions tests/src/types/FixedPoint.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"

#include <cmath>
#include <cstdlib>
#include <cstdint>
#include <limits>
#include <numbers>
#include <string_view>
#include <system_error>
Expand Down Expand Up @@ -120,18 +120,21 @@ TEST_CASE("fixed_point_t Rounding methods", "[fixed_point_t][fixed_point_t-round
}

TEST_CASE("fixed_point_t Parse methods", "[fixed_point_t][fixed_point_t-parse]") {
CONSTEXPR_CHECK(fixed_point_t::parse(1) == 1);
CONSTEXPR_CHECK(fixed_point_t::parse(2) == 2);
CONSTEXPR_CHECK(fixed_point_t::parse(3) == 3);
CONSTEXPR_CHECK(fixed_point_t::parse(4) == 4);
CONSTEXPR_CHECK(fixed_point_t::parse(5) == 5);
CONSTEXPR_CHECK(fixed_point_t::parse(6) == 6);
CONSTEXPR_CHECK(fixed_point_t::parse(7) == 7);
CONSTEXPR_CHECK(fixed_point_t::parse(8) == 8);
CONSTEXPR_CHECK(fixed_point_t::parse(9) == 9);
CONSTEXPR_CHECK(fixed_point_t::parse(10) == 10);
CONSTEXPR_CHECK(fixed_point_t(1) == 1);
CONSTEXPR_CHECK(fixed_point_t(2) == 2);
CONSTEXPR_CHECK(fixed_point_t(3) == 3);
CONSTEXPR_CHECK(fixed_point_t(4) == 4);
CONSTEXPR_CHECK(fixed_point_t(5) == 5);
CONSTEXPR_CHECK(fixed_point_t(6) == 6);
CONSTEXPR_CHECK(fixed_point_t(7) == 7);
CONSTEXPR_CHECK(fixed_point_t(8) == 8);
CONSTEXPR_CHECK(fixed_point_t(9) == 9);
CONSTEXPR_CHECK(fixed_point_t(10) == 10);
CONSTEXPR_CHECK(fixed_point_t::parse_raw(10) == fixed_point_t::epsilon * 10);
CONSTEXPR_CHECK(fixed_point_t::parse(10) == 10);

CONSTEXPR_CHECK(fixed_point_t::parse_capped(140737488355328LL) == fixed_point_t::max);
CONSTEXPR_CHECK(fixed_point_t::parse_capped(140737488355327LL) == fixed_point_t::max);
CONSTEXPR_CHECK(fixed_point_t::parse_capped(140737488355326LL).truncate<int64_t>() == 140737488355326LL);

static constexpr std::string_view fixed_point_str = "4.5432"sv;
CONSTEXPR_CHECK(fixed_point_t::parse(fixed_point_str) == 4.5432_a);
Expand Down Expand Up @@ -285,8 +288,24 @@ TEST_CASE("fixed_point_t Operators", "[fixed_point_t][fixed_point_t-operators]")
CONSTEXPR_CHECK(((int32_t)decimal4) == 3);

CONSTEXPR_CHECK(
fixed_point_t::mul_div(fixed_point_t::parse_raw(2), fixed_point_t::parse_raw(3), fixed_point_t::parse_raw(6)) ==
fixed_point_t::parse_raw(1)
fixed_point_t::mul_div(
fixed_point_t::parse_raw(2),
fixed_point_t::parse_raw(3),
fixed_point_t::parse_raw(6)
) == fixed_point_t::parse_raw(1)
);

CONSTEXPR_CHECK(
fixed_point_t::multiply_truncate<int64_t>(
4294967295LL, //2^32 - 1
fixed_point_t::usable_max //2^31 / 2^16
) == 140737488322560LL //2^47 - 2^15
);
CONSTEXPR_CHECK(
fixed_point_t::multiply_truncate<int64_t>(
281474976710655LL, //2^48 - 1
fixed_point_t::_0_50
) == 140737488355327LL //2^47 - 1
);
}

Expand Down