Skip to content
Open
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
72 changes: 34 additions & 38 deletions src/headless/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,44 +258,40 @@ static bool run_headless(fs::path const& root, memory::vector<memory::string>& m

// TODO - REMOVE TEST CODE
SPDLOG_INFO("===== Ranking system test... =====");
if (game_manager.get_instance_manager()) {
const auto print_ranking_list = [ //
](std::string_view title, OpenVic::forwardable_span<CountryInstance* const> countries) -> void {
memory::string countries_str;
for (CountryInstance* country : countries) {
countries_str += fmt::format(
"\n\t{} - Total #{} ({}), Prestige #{} ({}), Industry #{} ({}), Military #{} ({})", //
*country, //
country->get_total_rank(), country->total_score.get_untracked().to_string(1), //
country->get_prestige_rank(), country->get_prestige_untracked().to_string(1),
country->get_industrial_rank(), country->get_industrial_power_untracked().to_string(1),
country->get_military_rank(), country->military_power.get_untracked().to_string(1)
);
}
SPDLOG_INFO("{}:{}", title, countries_str);
};

CountryInstanceManager const& country_instance_manager =
game_manager.get_instance_manager()->get_country_instance_manager();

OpenVic::forwardable_span<CountryInstance* const> great_powers = country_instance_manager.get_great_powers();
print_ranking_list("Great Powers", great_powers);
print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers());
print_ranking_list("All countries", country_instance_manager.get_total_ranking());

SPDLOG_INFO("===== RGO test... =====");
for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) {
CountryInstance const& great_power = *great_powers[i];
ProvinceInstance const* const capital_province = great_power.get_capital();
if (capital_province == nullptr) {
spdlog::warn_s("{} has no capital ProvinceInstance set.", great_power);
} else {
print_rgo(*capital_province);
}
InstanceManager& instance_manager = *game_manager.get_instance_manager();
const auto print_ranking_list = [](
std::string_view title,
OpenVic::forwardable_span<CountryInstance* const> countries
) -> void {
memory::string countries_str;
for (CountryInstance* country : countries) {
countries_str += fmt::format(
"\n\t{} - Total #{} ({}), Prestige #{} ({}), Industry #{} ({}), Military #{} ({})", //
*country, //
country->get_total_rank(), country->total_score.get_untracked().to_string(1), //
country->get_prestige_rank(), country->get_prestige_untracked().to_string(1),
country->get_industrial_rank(), country->get_industrial_power_untracked().to_string(1),
country->get_military_rank(), country->military_power.get_untracked().to_string(1)
);
}
SPDLOG_INFO("{}:{}", title, countries_str);
};

CountryInstanceManager const& country_instance_manager = instance_manager.get_country_instance_manager();
OpenVic::forwardable_span<CountryInstance* const> great_powers = country_instance_manager.get_great_powers();
print_ranking_list("Great Powers", great_powers);
print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers());
print_ranking_list("All countries", country_instance_manager.get_total_ranking());

SPDLOG_INFO("===== RGO test... =====");
for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) {
CountryInstance const& great_power = *great_powers[i];
ProvinceInstance const* const capital_province = great_power.get_capital();
if (capital_province == nullptr) {
spdlog::warn_s("{} has no capital ProvinceInstance set.", great_power);
} else {
print_rgo(*capital_province);
}
} else {
spdlog::error_s("Instance manager not available!");
ret = false;
}

if (ret) {
Expand All @@ -305,7 +301,7 @@ static bool run_headless(fs::path const& root, memory::vector<memory::string>& m

using test_time_units_t = std::chrono::milliseconds;

MapInstance& map_instance = game_manager.get_instance_manager()->get_map_instance();
MapInstance& map_instance = instance_manager.get_map_instance();

SPDLOG_INFO("===== Land Pathfinding test... =====");
test_duration_t duration = std::chrono::duration_cast<test_time_units_t>( //
Expand Down
8 changes: 5 additions & 3 deletions src/openvic-simulation/core/Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ namespace OpenVic {
s = h(v) << (sizeof(T) * CHAR_BIT);
(
[&] {
// If args is not last pointer of args
if (static_cast<void const*>(&(std::get<sizeof...(args) - 1>(arg_tuple))) !=
static_cast<void const*>(&args)) {
void const* const args_ptr = static_cast<void const*>(&args);
void const* const last_pointer_of_args = static_cast<void const*>(
&(std::get<sizeof...(args) - 1>(arg_tuple))
);
if (args_ptr != last_pointer_of_args) {
s <<= sizeof(Args) * CHAR_BIT;
}
s |= std::hash<Args> {}(args);
Expand Down
20 changes: 14 additions & 6 deletions src/openvic-simulation/core/portable/Deque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,10 @@ namespace OpenVic::utility::_detail::deque {
// (_Myoff() + _Mysize() - 1) is for the last element, i.e. the back() of the deque.
// Divide by _Block_size to get the unmasked index of the last used block.
// Add 1 to get the unmasked index of the first unused block.
const auto _Unmasked_first_unused_block_idx =
static_cast<size_type>(((_Myoff() + _Mysize() - 1) / _Block_size) + 1);
const auto _Unmasked_first_unused_block_idx = static_cast<size_type>(
((_Myoff() + _Mysize() - 1) / _Block_size)
+ 1
);

const auto _First_unused_block_idx = static_cast<size_type>(_Unmasked_first_unused_block_idx & _Mask);

Expand All @@ -1037,8 +1039,10 @@ namespace OpenVic::utility::_detail::deque {
}
}

const auto _Used_block_count =
static_cast<size_type>(_Unmasked_first_unused_block_idx - _Unmasked_first_used_block_idx);
const auto _Used_block_count = static_cast<size_type>(
_Unmasked_first_unused_block_idx
- _Unmasked_first_used_block_idx
);

size_type _New_block_count = _Minimum_map_size; // should be power of 2

Expand Down Expand Up @@ -1818,8 +1822,12 @@ namespace OpenVic::utility::_detail::deque {

_EXPORT_STD template<class _Ty, class _Alloc>
_NODISCARD bool operator==(const deque<_Ty, _Alloc>& _Left, const deque<_Ty, _Alloc>& _Right) {
return _Left.size() == _Right.size() &&
_STD equal(_Left._Unchecked_begin(), _Left._Unchecked_end(), _Right._Unchecked_begin());
return _Left.size() == _Right.size()
&& _STD equal(
_Left._Unchecked_begin(),
_Left._Unchecked_end(),
_Right._Unchecked_begin()
);
}

#if _HAS_CXX20
Expand Down
8 changes: 4 additions & 4 deletions src/openvic-simulation/core/portable/ForwardableSpan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ namespace OpenVic::_detail::forwardable_span {
concept span_array_convertible = std::is_convertible_v<From (*)[], To (*)[]>;

template<class It, class T>
concept span_compatible_iterator =
std::contiguous_iterator<It> && span_array_convertible<std::remove_reference_t<std::iter_reference_t<It>>, T>;
concept span_compatible_iterator = std::contiguous_iterator<It>
&& span_array_convertible<std::remove_reference_t<std::iter_reference_t<It>>, T>;

template<class Sentinel, class It>
concept span_compatible_sentinel_for =
std::sized_sentinel_for<Sentinel, It> && !std::is_convertible_v<Sentinel, std::size_t>;
concept span_compatible_sentinel_for = std::sized_sentinel_for<Sentinel, It>
&& !std::is_convertible_v<Sentinel, std::size_t>;

template<size_t Extent>
struct extent_storage {
Expand Down
14 changes: 10 additions & 4 deletions src/openvic-simulation/core/random/RandomGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ namespace OpenVic {
struct RandomGenerator {
using generator_type = T;

using state_type =
std::conditional_t<requires { typename generator_type::state_type; }, typename generator_type::state_type, void>;
using result_type =
std::conditional_t<requires { typename generator_type::result_type; }, typename generator_type::result_type, void>;
using state_type = std::conditional_t<
requires { typename generator_type::state_type; },
typename generator_type::state_type,
void
>;
using result_type = std::conditional_t<
requires { typename generator_type::result_type; },
typename generator_type::result_type,
void
>;

[[nodiscard]] OV_ALWAYS_INLINE explicit constexpr RandomGenerator()
requires requires {
Expand Down
18 changes: 10 additions & 8 deletions src/openvic-simulation/core/template/Concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ namespace OpenVic {
concept not_same_as = !std::same_as<T, T2>;

template<typename Allocator, typename T, typename Value = std::remove_cvref_t<typename Allocator::value_type>>
concept move_insertable_allocator_for =
requires(Allocator& alloc, Value* value, T move) { std::allocator_traits<Allocator>::construct(alloc, value, move); };
concept move_insertable_allocator_for = requires(Allocator& alloc, Value* value, T move) {
std::allocator_traits<Allocator>::construct(alloc, value, move);
};

template<typename Allocator>
struct enable_copy_insertable : std::false_type {};

template<typename Allocator>
concept copy_insertable_allocator = enable_copy_insertable<Allocator>::value ||
move_insertable_allocator_for<Allocator, typename Allocator::value_type const&>;
concept copy_insertable_allocator = enable_copy_insertable<Allocator>::value
|| move_insertable_allocator_for<Allocator, typename Allocator::value_type const&>;

template<typename T>
struct enable_copy_insertable<std::allocator<T>> : std::is_copy_constructible<T> {};
Expand All @@ -59,8 +60,8 @@ namespace OpenVic {
struct enable_move_insertable : std::false_type {};

template<typename Allocator>
concept move_insertable_allocator =
enable_move_insertable<Allocator>::value || move_insertable_allocator_for<Allocator, typename Allocator::value_type>;
concept move_insertable_allocator = enable_move_insertable<Allocator>::value
|| move_insertable_allocator_for<Allocator, typename Allocator::value_type>;

template<typename T>
struct enable_move_insertable<std::allocator<T>> : std::is_move_constructible<T> {};
Expand Down Expand Up @@ -109,8 +110,9 @@ namespace OpenVic {
};

template<typename T>
concept has_index = requires { typename T::index_t; } &&
derived_from_specialization_of<typename T::index_t, type_safe::strong_typedef> && requires {
concept has_index = requires { typename T::index_t; }
&& derived_from_specialization_of<typename T::index_t, type_safe::strong_typedef>
&& requires {
static_cast<std::size_t>(
static_cast<type_safe::underlying_type<decltype(std::declval<T>().index)>>(std::declval<T>().index)
);
Expand Down
60 changes: 34 additions & 26 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,12 @@ bool CountryInstance::remove_unit_instance_group(UnitInstanceGroup const& group)
const auto remove_from_vector = [this, &group]<unit_branch_t Branch>(
memory::vector<UnitInstanceGroupBranched<Branch>*>& unit_instance_groups
) -> bool {
const typename memory::vector<UnitInstanceGroupBranched<Branch>*>::const_iterator it =
std::find(unit_instance_groups.begin(), unit_instance_groups.end(), &group);
using const_it_t = typename memory::vector<UnitInstanceGroupBranched<Branch>*>::const_iterator;
const const_it_t it = std::find(
unit_instance_groups.begin(),
unit_instance_groups.end(),
&group
);

if (it != unit_instance_groups.end()) {
unit_instance_groups.erase(it);
Expand Down Expand Up @@ -1226,12 +1230,14 @@ bool CountryInstance::apply_history_to_country(
ret &= add_reform(*reform);
}
set_optional_state(tech_school, entry.get_tech_school());
constexpr auto set_bool_map_to_indexed_map =
[]<typename T>(IndexedFlatMap<T, bool>& target, ordered_map<T const*, bool> source) {
for (auto const& [key, value] : source) {
target[*key] = value;
}
};
constexpr auto set_bool_map_to_indexed_map = []<typename T>(
IndexedFlatMap<T, bool>& target,
ordered_map<T const*, bool> source
) {
for (auto const& [key, value] : source) {
target[*key] = value;
}
};

for (auto const& [technology, level] : entry.get_technologies()) {
ret &= set_technology_unlock_level(*technology, level);
Expand Down Expand Up @@ -1466,9 +1472,9 @@ void CountryInstance::_update_technology(const Date today) {
}

daily_research_points += get_modifier_effect_value(*modifier_effect_cache.get_research_points());
daily_research_points *= fixed_point_t::_1 +
get_modifier_effect_value(*modifier_effect_cache.get_research_points_modifier()) +
get_modifier_effect_value(*modifier_effect_cache.get_increase_research());
daily_research_points *= fixed_point_t::_1
+ get_modifier_effect_value(*modifier_effect_cache.get_research_points_modifier())
+ get_modifier_effect_value(*modifier_effect_cache.get_increase_research());

if (daily_research_points.get_untracked() < 0) {
daily_research_points.set(0);
Expand Down Expand Up @@ -1592,13 +1598,15 @@ void CountryInstance::_update_military() {

// Mobilisation calculations
mobilisation_impact = get_modifier_effect_value(*modifier_effect_cache.get_mobilization_impact());
mobilisation_economy_impact = get_modifier_effect_value(*modifier_effect_cache.get_mobilisation_economy_impact_tech()) +
get_modifier_effect_value(*modifier_effect_cache.get_mobilisation_economy_impact_country());
mobilisation_economy_impact = get_modifier_effect_value(*modifier_effect_cache.get_mobilisation_economy_impact_tech())
+ get_modifier_effect_value(*modifier_effect_cache.get_mobilisation_economy_impact_country());

// 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 = (
(fixed_point_t::_1 + mobilisation_impact)
* fixed_point_t::parse(regiment_count)
).floor<size_t>();

mobilisation_potential_regiment_count = 0; // TODO - calculate max regiments from poor citizens
if (mobilisation_potential_regiment_count > mobilisation_max_regiment_count) {
Expand All @@ -1612,9 +1620,9 @@ void CountryInstance::_update_military() {
get_modifier_effect_value(*modifier_effect_cache.get_max_war_exhaustion()), fixed_point_t::_0
);

organisation_regain = fixed_point_t::_1 +
get_modifier_effect_value(*modifier_effect_cache.get_org_regain()) +
get_modifier_effect_value(*modifier_effect_cache.get_morale_global());
organisation_regain = fixed_point_t::_1
+ get_modifier_effect_value(*modifier_effect_cache.get_org_regain())
+ get_modifier_effect_value(*modifier_effect_cache.get_morale_global());

land_organisation = fixed_point_t::_1 + get_modifier_effect_value(*modifier_effect_cache.get_land_organisation());
naval_organisation = fixed_point_t::_1 + get_modifier_effect_value(*modifier_effect_cache.get_naval_organisation());
Expand All @@ -1626,21 +1634,21 @@ void CountryInstance::_update_military() {

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())
fixed_point_t::parse(military_defines.get_base_combat_width())
+ get_modifier_effect_value(*modifier_effect_cache.get_combat_width_additive())
).floor<int32_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());
military_tactics = military_defines.get_base_military_tactics()
+ get_modifier_effect_value(*modifier_effect_cache.get_military_tactics());

if (leadership_point_stockpile < 0) {
leadership_point_stockpile = 0;
}
create_leader_count = (leadership_point_stockpile / military_defines.get_leader_recruit_cost()).floor<int32_t>();

monthly_leadership_points += get_modifier_effect_value(*modifier_effect_cache.get_leadership());
monthly_leadership_points *= fixed_point_t::_1 +
get_modifier_effect_value(*modifier_effect_cache.get_leadership_modifier());
monthly_leadership_points *= fixed_point_t::_1
+ get_modifier_effect_value(*modifier_effect_cache.get_leadership_modifier());

if (monthly_leadership_points < 0) {
monthly_leadership_points = 0;
Expand Down Expand Up @@ -1876,8 +1884,8 @@ void CountryInstance::update_gamestate(const Date today, MapInstance& map_instan
_update_politics();
_update_diplomacy();

const CountryDefinition::government_colour_map_t::const_iterator it =
country_definition.get_alternative_colours().find(government_type.get_untracked());
using const_it_t = typename CountryDefinition::government_colour_map_t::const_iterator;
const const_it_t it = country_definition.get_alternative_colours().find(government_type.get_untracked());

if (it != country_definition.get_alternative_colours().end()) {
colour = it.value();
Expand Down
5 changes: 3 additions & 2 deletions src/openvic-simulation/country/CountryInstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ CountryInstance const& CountryInstanceManager::get_country_instance_by_definitio
}

bool CountryInstanceManager::apply_history_to_countries(InstanceManager& instance_manager) {
CountryHistoryManager const& history_manager =
instance_manager.definition_manager.get_history_manager().get_country_manager();
CountryHistoryManager const& history_manager = instance_manager.definition_manager
.get_history_manager()
.get_country_manager();

bool ret = true;

Expand Down
Loading