Skip to content

Commit ffa83b0

Browse files
authored
Merge pull request #469 from OpenVicProject/add/pathfinding-unit-enter-check
Add ArmyAStarPathing for ArmyInstance pathing
2 parents 9fffe1e + 4a94dc9 commit ffa83b0

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

src/openvic-simulation/map/MapInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ MapInstance::MapInstance(
1313
ThreadPool& new_thread_pool
1414
) : map_definition { new_map_definition },
1515
thread_pool { new_thread_pool },
16-
land_pathing { map_definition.get_path_map_land() },
16+
land_pathing { *this },
1717
sea_pathing { map_definition.get_path_map_sea() } {}
1818

1919
ProvinceInstance& MapInstance::get_province_instance_from_definition(ProvinceDefinition const& province) {

src/openvic-simulation/map/MapInstance.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace OpenVic {
3535
// TODO - should this be a vector of bools which we resize to the largest enabled canal index?
3636
ordered_set<canal_index_t> PROPERTY(enabled_canals);
3737

38-
AStarPathing PROPERTY_REF(land_pathing);
38+
ArmyAStarPathing PROPERTY_REF(land_pathing);
3939
AStarPathing PROPERTY_REF(sea_pathing);
4040

4141
public:

src/openvic-simulation/military/UnitInstanceGroup.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ namespace OpenVic {
9999
NavyInstance* PROPERTY_PTR(transport_navy, nullptr);
100100
dig_in_level_t PROPERTY(dig_in_level, 0);
101101

102+
bool PROPERTY_RW_CUSTOM_NAME(exiled, is_exiled, set_is_exiled, false);
103+
102104
UnitInstanceGroupBranched(
103105
unique_id_t new_unique_id,
104106
std::string_view new_name

src/openvic-simulation/pathfinding/AStarPathing.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
#include <range/v3/algorithm/find.hpp>
99
#include <range/v3/algorithm/heap_algorithm.hpp>
1010

11+
#include "openvic-simulation/country/CountryInstance.hpp"
12+
#include "openvic-simulation/map/MapDefinition.hpp"
13+
#include "openvic-simulation/map/MapInstance.hpp"
14+
#include "openvic-simulation/map/ProvinceInstance.hpp"
15+
#include "openvic-simulation/military/UnitInstanceGroup.hpp"
1116
#include "openvic-simulation/pathfinding/PointMap.hpp"
1217
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
18+
#include "openvic-simulation/utility/Utility.hpp"
1319

1420
using namespace OpenVic;
1521

@@ -91,3 +97,34 @@ bool AStarPathing::_solve(search_iterator begin_point, search_iterator end_point
9197

9298
return found_route;
9399
}
100+
101+
ArmyAStarPathing::ArmyAStarPathing(MapInstance const& map)
102+
: map_instance(map), AStarPathing(&map.get_map_definition().get_path_map_land()) {}
103+
104+
bool ArmyAStarPathing::_is_point_enabled(search_const_iterator it) const {
105+
if (!AStarPathing::_is_point_enabled(it)) {
106+
return false;
107+
}
108+
109+
if (OV_unlikely(army_instance == nullptr)) {
110+
return true;
111+
}
112+
113+
if (army_instance->is_exiled()) {
114+
return true;
115+
}
116+
117+
ProvinceInstance const* province = map_instance.get_province_instance_by_index(it->first);
118+
119+
if (OV_unlikely(province == nullptr)) {
120+
return true;
121+
}
122+
123+
CountryInstance const* province_owner = province->get_owner();
124+
125+
if (OV_unlikely(province_owner == nullptr)) {
126+
return true;
127+
}
128+
129+
return army_instance->get_country()->can_units_enter(*province_owner);
130+
}

src/openvic-simulation/pathfinding/AStarPathing.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
#include <tsl/ordered_map.h>
66
#include <tsl/ordered_set.h>
77

8+
#include "openvic-simulation/military/UnitType.hpp"
89
#include "openvic-simulation/pathfinding/PathingBase.hpp"
910
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
1011

1112
namespace OpenVic {
13+
struct MapInstance;
14+
template<UnitType::branch_t>
15+
struct UnitInstanceGroupBranched;
16+
1217
struct AStarPathingNode final : public PathingNodeBase<AStarPathingNode> {
1318
using PathingNodeBase::PathingNodeBase;
1419

@@ -49,4 +54,14 @@ namespace OpenVic {
4954
search_iterator begin_point, search_iterator end_point, uint64_t pass, bool allow_partial_path
5055
) override;
5156
};
57+
58+
struct ArmyAStarPathing final : public AStarPathing {
59+
ArmyAStarPathing(MapInstance const& map);
60+
61+
protected:
62+
UnitInstanceGroupBranched<UnitType::branch_t::LAND> const* PROPERTY_RW_ACCESS(army_instance, protected, nullptr);
63+
MapInstance const& PROPERTY(map_instance);
64+
65+
virtual bool _is_point_enabled(search_const_iterator it) const override;
66+
};
5267
}

0 commit comments

Comments
 (0)