Skip to content

Commit

Permalink
small pathfind refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgtdf committed Jul 3, 2020
1 parent 04dce20 commit 8121a92
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
44 changes: 22 additions & 22 deletions src/ai/default/recruitment.cpp
Expand Up @@ -642,30 +642,30 @@ void recruitment::compare_cost_maps_and_update_important_hexes(
border_cost_map important_hexes_candidates;
double smallest_border_movecost = 999999;
double biggest_border_movecost = 0;
for(int x = 0; x < map.w(); ++x) {
for (int y = 0; y < map.h(); ++y) {
double my_cost_average = my_cost_map.get_average_cost_at(x, y);
double enemy_cost_average = enemy_cost_map.get_average_cost_at(x, y);
if (my_cost_average == -1 || enemy_cost_average == -1) {
continue;

map.for_each_walkable_loc([&](map_location loc) {
double my_cost_average = my_cost_map.get_average_cost_at(loc);
double enemy_cost_average = enemy_cost_map.get_average_cost_at(loc);
if (my_cost_average == -1 || enemy_cost_average == -1) {
return;
}
// We multiply the threshold MAP_BORDER_THICKNESS by the average_local_cost
// to favor high cost hexes (a bit).
if (std::abs(my_cost_average - MAP_OFFENSIVE_SHIFT - enemy_cost_average) <
MAP_BORDER_THICKNESS * average_local_cost_[loc]) {
double border_movecost = (my_cost_average + enemy_cost_average) / 2;
important_hexes_candidates[loc] = border_movecost;

if (border_movecost < smallest_border_movecost) {
smallest_border_movecost = border_movecost;
}
// We multiply the threshold MAP_BORDER_THICKNESS by the average_local_cost
// to favor high cost hexes (a bit).
if (std::abs(my_cost_average - MAP_OFFENSIVE_SHIFT - enemy_cost_average) <
MAP_BORDER_THICKNESS * average_local_cost_[map_location(x, y)]) {
double border_movecost = (my_cost_average + enemy_cost_average) / 2;
if (border_movecost > biggest_border_movecost) {
biggest_border_movecost = border_movecost;
}
}

important_hexes_candidates[map_location(x, y)] = border_movecost;
});

if (border_movecost < smallest_border_movecost) {
smallest_border_movecost = border_movecost;
}
if (border_movecost > biggest_border_movecost) {
biggest_border_movecost = border_movecost;
}
}
} // for
} // for
double threshold = (biggest_border_movecost - smallest_border_movecost) *
MAP_BORDER_WIDTH + smallest_border_movecost;
for (const border_cost_map::value_type& candidate : important_hexes_candidates) {
Expand Down Expand Up @@ -870,7 +870,7 @@ void recruitment::update_important_hexes() {
get_tiles_in_radius(village, MAP_VILLAGE_SURROUNDING, surrounding);
for (const map_location& hex : surrounding) {
// only add hex if one of our units can reach the hex
if (map.on_board(hex) && my_cost_map.get_cost_at(hex.x, hex.y) != -1) {
if (map.on_board(hex) && my_cost_map.get_cost_at(hex) != -1) {
important_hexes_.insert(hex);
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/pathfind/pathfind.cpp
Expand Up @@ -959,16 +959,16 @@ void full_cost_map::add_unit(const map_location& origin, const unit_type* const
* @return the entry of the cost_map at (x, y)
* or (-1, 0) if value is not set or (x, y) is invalid.
*/
std::pair<int, int> full_cost_map::get_pair_at(int x, int y) const
std::pair<int, int> full_cost_map::get_pair_at(map_location loc) const
{
const gamemap& map = resources::gameboard->map();
assert(cost_map.size() == static_cast<unsigned>(map.w() * map.h()));

if (x < 0 || x >= map.w() || y < 0 || y >= map.h()) {
if (!map.on_board(loc)) {
return std::make_pair(-1, 0); // invalid
}

return cost_map[x + (y * map.w())];
return cost_map[loc.x + (loc.y * map.w())];
}

/**
Expand All @@ -977,9 +977,9 @@ std::pair<int, int> full_cost_map::get_pair_at(int x, int y) const
* @return the value of the cost_map at (x, y)
* or -1 if value is not set or (x, y) is invalid.
*/
int full_cost_map::get_cost_at(int x, int y) const
int full_cost_map::get_cost_at(map_location loc) const
{
return get_pair_at(x, y).first;
return get_pair_at(loc).first;
}

/**
Expand All @@ -988,12 +988,13 @@ int full_cost_map::get_cost_at(int x, int y) const
* @return The average cost of all added units for this hex
* or -1 if no unit can reach the hex.
*/
double full_cost_map::get_average_cost_at(int x, int y) const
double full_cost_map::get_average_cost_at(map_location loc) const
{
if (get_pair_at(x, y).second == 0) {
auto p = get_pair_at(loc);
if (p.second == 0) {
return -1;
} else {
return static_cast<double>(get_pair_at(x, y).first) / get_pair_at(x, y).second;
return static_cast<double>(p.first) /p.second;
}
}
}//namespace pathfind
6 changes: 3 additions & 3 deletions src/pathfind/pathfind.hpp
Expand Up @@ -278,9 +278,9 @@ struct full_cost_map

void add_unit(const unit& u, bool use_max_moves=true);
void add_unit(const map_location& origin, const unit_type* const unit_type, int side);
int get_cost_at(int x, int y) const;
std::pair<int, int> get_pair_at(int x, int y) const;
double get_average_cost_at(int x, int y) const;
int get_cost_at(map_location loc) const;
std::pair<int, int> get_pair_at(map_location loc) const;
double get_average_cost_at(map_location loc) const;
virtual ~full_cost_map()
{
}
Expand Down
8 changes: 4 additions & 4 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -2064,9 +2064,9 @@ int game_lua_kernel::intf_find_cost_map(lua_State *L)
for (const map_location& loc : location_set)
{
std::stringstream s;
s << cost_map.get_pair_at(loc.x, loc.y).first;
s << cost_map.get_pair_at(loc).first;
s << " / ";
s << cost_map.get_pair_at(loc.x, loc.y).second;
s << cost_map.get_pair_at(loc).second;
game_display_->labels().set_label(loc, s.str());
}
}
Expand All @@ -2085,10 +2085,10 @@ int game_lua_kernel::intf_find_cost_map(lua_State *L)
lua_pushinteger(L, loc.wml_y());
lua_rawseti(L, -2, 2);

lua_pushinteger(L, cost_map.get_pair_at(loc.x, loc.y).first);
lua_pushinteger(L, cost_map.get_pair_at(loc).first);
lua_rawseti(L, -2, 3);

lua_pushinteger(L, cost_map.get_pair_at(loc.x, loc.y).second);
lua_pushinteger(L, cost_map.get_pair_at(loc).second);
lua_rawseti(L, -2, 4);

lua_rawseti(L, -2, counter);
Expand Down

0 comments on commit 8121a92

Please sign in to comment.