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
21 changes: 20 additions & 1 deletion src/openvic-simulation/map/ProvinceInstance.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "ProvinceInstance.hpp"
#include "ProvinceInstanceDeps.hpp"

#include <type_traits>

#include "openvic-simulation/country/CountryDefinition.hpp"
#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/defines/MilitaryDefines.hpp"
Expand Down Expand Up @@ -518,4 +520,21 @@ void ProvinceInstance::setup_pop_test_values(IssueManager const& issue_manager)

memory::colony<Pop>& ProvinceInstance::get_mutable_pops() {
return pops;
}
}

template<typename T>
std::conditional_t<std::is_const_v<T>, Pop const*, Pop*> ProvinceInstance::_find_pop_by_id(T& self, const pop_id_in_province_t pop_id) {
if (pop_id.is_null()) {
return nullptr;
}

for (std::conditional_t<std::is_const_v<T>, Pop const&, Pop&>& pop : self.pops) {
if (pop.id_in_province == pop_id) {
return &pop;
}
}

return nullptr;
}
Pop* ProvinceInstance::find_pop_by_id(const pop_id_in_province_t pop_id) { return _find_pop_by_id(*this, pop_id); }
Pop const* ProvinceInstance::find_pop_by_id(const pop_id_in_province_t pop_id) const { return _find_pop_by_id(*this, pop_id); }
6 changes: 6 additions & 0 deletions src/openvic-simulation/map/ProvinceInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,11 @@ namespace OpenVic {

void setup_pop_test_values(IssueManager const& issue_manager);
memory::colony<Pop>& get_mutable_pops();
private:
template<typename T>
static std::conditional_t<std::is_const_v<T>, Pop const*, Pop*> _find_pop_by_id(T& self, const pop_id_in_province_t pop_id);
public:
Pop* find_pop_by_id(const pop_id_in_province_t pop_id);
Pop const* find_pop_by_id(const pop_id_in_province_t pop_id) const;
};
}
36 changes: 35 additions & 1 deletion src/openvic-simulation/misc/GameAction.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "GameAction.hpp"

#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/DefinitionManager.hpp"
#include "openvic-simulation/InstanceManager.hpp"
#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/map/ProvinceInstance.hpp"

using namespace OpenVic;

Expand Down Expand Up @@ -374,3 +375,36 @@ bool GameActionManager::VariantVisitor::operator() (set_mobilise_argument_t cons

return old_mobilise != country->is_mobilised();
}

bool GameActionManager::VariantVisitor::operator() (start_land_unit_recruitment_argument_t const& argument) const {
const auto [regiment_type_index, province_index, pop_id_in_province] = argument;

RegimentType const* const regiment_type = instance_manager.definition_manager
.get_military_manager()
.get_unit_type_manager()
.get_regiment_type_by_index(regiment_type_index);
if (OV_unlikely(regiment_type == nullptr)) {
spdlog::error_s("GAME_ACTION_START_LAND_UNIT_RECRUITMENT called with invalid regiment type index: {}", regiment_type_index);
return false;
}

ProvinceInstance* province = instance_manager
.get_map_instance()
.get_province_instance_by_index(province_index);
if (OV_unlikely(province == nullptr)) {
spdlog::error_s("GAME_ACTION_START_LAND_UNIT_RECRUITMENT called with invalid province index: {}", province_index);
return false;
}

Pop* pop = province->find_pop_by_id(pop_id_in_province);
if (OV_unlikely(pop == nullptr)) {
spdlog::error_s("GAME_ACTION_START_LAND_UNIT_RECRUITMENT called with invalid pop_id_in_province: {}", pop_id_in_province);
return false;
}

//these TODO's should be implemented in ProvinceInstance and/or some military type
//TODO verify pop's cultural status is acceptable for regiment type
//TODO verify pop is recruitable and has enough size (pop.try_recruit())
//TODO actually instantiate a regiment in recruitment state
return false;
}
4 changes: 3 additions & 1 deletion src/openvic-simulation/misc/GameAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <type_safe/strong_typedef.hpp>

#include "openvic-simulation/population/PopIdInProvince.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/types/TypedIndices.hpp"
#include "openvic-simulation/types/UniqueId.hpp"
Expand Down Expand Up @@ -85,7 +86,8 @@ X(create_leader, country_index_t, unit_branch_t) \
X(set_use_leader, unique_id_t, bool) \
X(set_auto_create_leaders, country_index_t, bool) \
X(set_auto_assign_leaders, country_index_t, bool) \
X(set_mobilise, country_index_t, bool)
X(set_mobilise, country_index_t, bool) \
X(start_land_unit_recruitment, regiment_type_index_t, province_index_t, pop_id_in_province_t)
// <--- ADD NEW GAME ACTIONS HERE (copy/edit an X(...) line)

//the argument type alias for each game action
Expand Down
3 changes: 3 additions & 0 deletions src/openvic-simulation/population/PopIdInProvince.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace OpenVic {
type_safe::strong_typedef_op::relational_comparison<pop_id_in_province_t>,
type_safe::strong_typedef_op::integer_arithmetic<pop_id_in_province_t> {
using strong_typedef::strong_typedef;

constexpr bool is_null() const { return type_safe::get(*this) == 0; }
constexpr bool operator!() const { return is_null(); }
};
}
namespace std {
Expand Down