Skip to content

Commit

Permalink
Use std::array for adjacent map_location arrays
Browse files Browse the repository at this point in the history
I left the implementation of get_adjacent_tiles using messy C ptr arithmetic
since it most simply accounts for a few cases using arrays that are larger than
6 (such as in tod_manager) or underlying vector storage.

Also made use of std::array::size where applicable, and one case of std::array
iterators.

aspect_attacks_base::analyze_targets was changed to take a adjacent_loc_array_t
reference.
  • Loading branch information
Vultraz committed Feb 19, 2018
1 parent 77edec6 commit 515dbdd
Show file tree
Hide file tree
Showing 32 changed files with 142 additions and 139 deletions.
8 changes: 4 additions & 4 deletions src/actions/attack.cpp
Expand Up @@ -1633,12 +1633,12 @@ bool backstab_check(const map_location& attacker_loc,
return false; // No defender
}

map_location adj[6];
get_adjacent_tiles(defender_loc, adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(defender_loc, adj.data());

int i;
unsigned i;

for(i = 0; i != 6; ++i) {
for(i = 0; i < adj.size(); ++i) {
if(adj[i] == attacker_loc) {
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/actions/move.cpp
Expand Up @@ -420,9 +420,9 @@ namespace { // Private helpers for move_unit()
const unit_map &units = resources::gameboard->units();

// Need to check each adjacent hex for hidden enemies.
map_location adjacent[6];
get_adjacent_tiles(hex, adjacent);
for ( int i = 0; i != 6; ++i )
adjacent_loc_array_t adjacent;
get_adjacent_tiles(hex, adjacent.data());
for (unsigned i = 0; i < adjacent.size(); ++i )
{
const unit_map::const_iterator neighbor_it = units.find(adjacent[i]);

Expand Down
12 changes: 6 additions & 6 deletions src/actions/vision.cpp
Expand Up @@ -275,9 +275,9 @@ bool shroud_clearer::clear_loc(team &tm, const map_location &loc,
display::get_singleton()->invalidate(loc);
// Need to also invalidate adjacent hexes to get rid of the
// "fog edge" graphics.
map_location adjacent[6];
get_adjacent_tiles(loc, adjacent);
for ( int i = 0; i != 6; ++i )
adjacent_loc_array_t adjacent;
get_adjacent_tiles(loc, adjacent.data());
for (unsigned i = 0; i < adjacent.size(); ++i )
display::get_singleton()->invalidate(adjacent[i]);
}

Expand Down Expand Up @@ -511,9 +511,9 @@ bool shroud_clearer::clear_dest(const map_location &dest, const unit &viewer)

// Clear the adjacent hexes (will be seen even if vision is 0, and the
// graphics do not work so well for an isolated cleared hex).
map_location adjacent[6];
get_adjacent_tiles(dest, adjacent);
for ( int i = 0; i != 6; ++i )
adjacent_loc_array_t adjacent;
get_adjacent_tiles(dest, adjacent.data());
for (unsigned i = 0; i < adjacent.size(); ++i )
if ( clear_loc(viewing_team, adjacent[i], dest, real_loc, viewer_id,
true, enemies, friends) )
cleared_something = true;
Expand Down
10 changes: 5 additions & 5 deletions src/ai/contexts.cpp
Expand Up @@ -981,9 +981,9 @@ const std::set<map_location>& keeps_cache::get()
for(int y = 0; y != map_->h(); ++y) {
const map_location loc(x,y);
if(map_->is_keep(loc)) {
map_location adj[6];
get_adjacent_tiles(loc,adj);
for(size_t n = 0; n != 6; ++n) {
adjacent_loc_array_t adj;
get_adjacent_tiles(loc,adj.data());
for(size_t n = 0; n < adj.size(); ++n) {
if(map_->is_castle(adj[n])) {
keeps_.insert(loc);
break;
Expand Down Expand Up @@ -1058,8 +1058,8 @@ double readonly_context_impl::power_projection(const map_location& loc, const mo
std::fill_n(ratings, 0, 6);
int num_used_locs = 0;

map_location locs[6];
get_adjacent_tiles(loc,locs);
adjacent_loc_array_t locs;
get_adjacent_tiles(loc,locs.data());

const gamemap& map_ = resources::gameboard->map();
unit_map& units_ = resources::gameboard->units();
Expand Down
12 changes: 6 additions & 6 deletions src/ai/default/aspect_attacks.cpp
Expand Up @@ -109,8 +109,8 @@ std::shared_ptr<attacks_vector> aspect_attacks_base::analyze_targets() const
if (!is_allowed_enemy(*j)) {
continue;
}
map_location adjacent[6];
get_adjacent_tiles(j->get_location(), adjacent);
adjacent_loc_array_t adjacent;
get_adjacent_tiles(j->get_location(), adjacent.data());
attack_analysis analysis;
analysis.target = j->get_location();
analysis.vulnerability = 0.0;
Expand All @@ -130,7 +130,7 @@ void aspect_attacks_base::do_attack_analysis(
const move_map& srcdst, const move_map& dstsrc,
const move_map& fullmove_srcdst, const move_map& fullmove_dstsrc,
const move_map& enemy_srcdst, const move_map& enemy_dstsrc,
const map_location* tiles, bool* used_locations,
const adjacent_loc_array_t& tiles, bool* used_locations,
std::vector<map_location>& units,
std::vector<attack_analysis>& result,
attack_analysis& cur_analysis,
Expand Down Expand Up @@ -193,8 +193,8 @@ void aspect_attacks_base::do_attack_analysis(
bool is_flanked = false;
int enemy_units_around = 0;
int accessible_tiles = 0;
map_location adj[6];
get_adjacent_tiles(current_unit, adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(current_unit, adj.data());

size_t tile;
for(tile = 0; tile != 3; ++tile) {
Expand Down Expand Up @@ -237,7 +237,7 @@ void aspect_attacks_base::do_attack_analysis(
int cur_position = -1;

// Iterate over positions adjacent to the unit, finding the best rated one.
for(int j = 0; j != 6; ++j) {
for(unsigned j = 0; j < tiles.size(); ++j) {

// If in this planned attack, a unit is already in this location.
if(used_locations[j]) {
Expand Down
2 changes: 1 addition & 1 deletion src/ai/default/aspect_attacks.hpp
Expand Up @@ -55,7 +55,7 @@ class aspect_attacks_base : public typesafe_aspect<attacks_vector> {
const move_map& srcdst, const move_map& dstsrc,
const move_map& fullmove_srcdst, const move_map& fullmove_dstsrc,
const move_map& enemy_srcdst, const move_map& enemy_dstsrc,
const map_location* tiles, bool* used_locations,
const adjacent_loc_array_t& tiles, bool* used_locations,
std::vector<map_location>& units,
std::vector<attack_analysis>& result,
attack_analysis& cur_analysis,
Expand Down
6 changes: 3 additions & 3 deletions src/ai/default/attack.cpp
Expand Up @@ -51,10 +51,10 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,
assert(defend_it != units.end());

// See if the target is a threat to our leader or an ally's leader.
map_location adj[6];
get_adjacent_tiles(target,adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(target,adj.data());
size_t tile;
for(tile = 0; tile != 6; ++tile) {
for(tile = 0; tile < adj.size(); ++tile) {
const unit_map::const_iterator leader = units.find(adj[tile]);
if(leader != units.end() && leader->can_recruit() && !ai_obj.current_team().is_enemy(leader->side())) {
break;
Expand Down
8 changes: 4 additions & 4 deletions src/ai/default/ca.cpp
Expand Up @@ -1406,15 +1406,15 @@ double retreat_phase::evaluate()
calculate_possible_moves(dummy_possible_moves, fullmove_srcdst, fullmove_dstsrc,
false, true, &get_avoid());

/*map_location leader_adj[6];
/*adjacent_loc_array_t leader_adj;
if(leader != units_.end()) {
get_adjacent_tiles(leader->get_location(), leader_adj);
get_adjacent_tiles(leader->get_location(), leader_adj.data());
}*/
//int leader_adj_count = 0;
std::vector<map_location> leaders_adj_v;
for (unit_map::const_iterator leader : leaders) {
map_location tmp_leader_adj[6];
get_adjacent_tiles(leader->get_location(), tmp_leader_adj);
adjacent_loc_array_t tmp_leader_adj;
get_adjacent_tiles(leader->get_location(), tmp_leader_adj.data());
for (map_location &loc : tmp_leader_adj) {
bool found = false;
for (map_location &new_loc : leaders_adj_v) {
Expand Down
18 changes: 9 additions & 9 deletions src/ai/default/ca_move_to_targets.cpp
Expand Up @@ -664,9 +664,9 @@ double move_to_targets_phase::compare_groups(const std::set<map_location>& our_g
void move_to_targets_phase::enemies_along_path(const std::vector<map_location>& route, const move_map& dstsrc, std::set<map_location>& res)
{
for(std::vector<map_location>::const_iterator i = route.begin(); i != route.end(); ++i) {
map_location adj[6];
get_adjacent_tiles(*i,adj);
for(size_t n = 0; n != 6; ++n) {
adjacent_loc_array_t adj;
get_adjacent_tiles(*i,adj.data());
for(size_t n = 0; n < adj.size(); ++n) {
const std::pair<move_map::const_iterator,move_map::const_iterator> itors = dstsrc.equal_range(adj[n]);
for(move_map::const_iterator j = itors.first; j != itors.second; ++j) {
res.insert(j->second);
Expand Down Expand Up @@ -743,10 +743,10 @@ bool move_to_targets_phase::move_group(const map_location& dst, const std::vecto
}

if(next.valid()) {
map_location adj[6];
get_adjacent_tiles(dst,adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(dst,adj.data());

direction = std::find(adj,adj+6,next) - adj;
direction = std::distance(adj.begin(), std::find(adj.begin(), adj.end(), next));
}

std::deque<map_location> preferred_moves;
Expand Down Expand Up @@ -803,9 +803,9 @@ bool move_to_targets_phase::move_group(const map_location& dst, const std::vecto
preferred_moves.erase(std::find(preferred_moves.begin(),preferred_moves.end(),best_loc));

//find locations that are 'perpendicular' to the direction of movement for further units to move to.
map_location adj[6];
get_adjacent_tiles(best_loc,adj);
for(size_t n = 0; n != 6; ++n) {
adjacent_loc_array_t adj;
get_adjacent_tiles(best_loc,adj.data());
for(size_t n = 0; n < adj.size(); ++n) {
if(n != direction && ((n+3)%6) != direction && map_.on_board(adj[n]) &&
units_.count(adj[n]) == 0 && std::count(preferred_moves.begin(),preferred_moves.end(),adj[n]) == 0) {
preferred_moves.push_front(adj[n]);
Expand Down
12 changes: 6 additions & 6 deletions src/ai/default/contexts.cpp
Expand Up @@ -70,9 +70,9 @@ int default_ai_context_impl::count_free_hexes_in_castle(const map_location &loc,
{
int ret = 0;
unit_map &units_ = resources::gameboard->units();
map_location adj[6];
get_adjacent_tiles(loc,adj);
for(size_t n = 0; n != 6; ++n) {
adjacent_loc_array_t adj;
get_adjacent_tiles(loc,adj.data());
for(size_t n = 0; n < adj.size(); ++n) {
if (checked_hexes.find(adj[n]) != checked_hexes.end())
continue;
checked_hexes.insert(adj[n]);
Expand Down Expand Up @@ -149,9 +149,9 @@ std::vector<target> default_ai_context_impl::find_targets(const move_map& enemy_
//find the location of enemy threats
std::set<map_location> threats;

map_location adj[6];
get_adjacent_tiles(leader->get_location(), adj);
for(size_t n = 0; n != 6; ++n) {
adjacent_loc_array_t adj;
get_adjacent_tiles(leader->get_location(), adj.data());
for(size_t n = 0; n < adj.size(); ++n) {
std::pair<move_map::const_iterator,move_map::const_iterator> itors = enemy_dstsrc.equal_range(adj[n]);
while(itors.first != itors.second) {
if(units_.count(itors.first->second)) {
Expand Down
12 changes: 6 additions & 6 deletions src/ai/formula/ai.cpp
Expand Up @@ -221,10 +221,10 @@ pathfind::plain_route formula_ai::shortest_path_calculator(const map_location &s
const map_location::DIRECTION preferred = destination.get_relative_dir(src);

int best_rating = 100;//smaller is better
map_location adj[6];
get_adjacent_tiles(destination,adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(destination,adj.data());

for(size_t n = 0; n != 6; ++n) {
for(size_t n = 0; n < adj.size(); ++n) {
if(resources::gameboard->map().on_board(adj[n]) == false) {
continue;
}
Expand Down Expand Up @@ -606,9 +606,9 @@ variant formula_ai::get_keeps() const
for(size_t y = 0; y != size_t(resources::gameboard->map().h()); ++y) {
const map_location loc(x,y);
if(resources::gameboard->map().is_keep(loc)) {
map_location adj[6];
get_adjacent_tiles(loc,adj);
for(size_t n = 0; n != 6; ++n) {
adjacent_loc_array_t adj;
get_adjacent_tiles(loc,adj.data());
for(size_t n = 0; n < adj.size(); ++n) {
if(resources::gameboard->map().is_castle(adj[n])) {
vars.emplace_back(std::make_shared<location_callable>(loc));
break;
Expand Down
6 changes: 3 additions & 3 deletions src/ai/formula/callable_objects.cpp
Expand Up @@ -271,10 +271,10 @@ void attack_map_callable::get_inputs(formula_input_vector& inputs) const {

/* add to vars all attacks on enemy units around <attack_position> tile. attacker_location is tile where unit is currently standing. It's moved to attack_position first and then performs attack.*/
void attack_map_callable::collect_possible_attacks(std::vector<variant>& vars, map_location attacker_location, map_location attack_position) const {
map_location adj[6];
get_adjacent_tiles(attack_position, adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(attack_position, adj.data());

for(int n = 0; n != 6; ++n) {
for(unsigned n = 0; n < adj.size(); ++n) {
/* if adjacent tile is outside the board */
if (! resources::gameboard->map().on_board(adj[n]))
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/ai/formula/function_table.cpp
Expand Up @@ -207,7 +207,7 @@ namespace {
pq.pop_back();
n.in = search_counter;

get_adjacent_tiles(n.loc_, &locs[0]);
get_adjacent_tiles(n.loc_, locs.data());
for (int i = teleports.count(n.loc_) ? locs.size() : 6; i-- > 0;) {
if (!locs[i].valid(map.w(), map.h())) continue;

Expand Down Expand Up @@ -444,10 +444,10 @@ DEFINE_WFL_FUNCTION(castle_locs, 1, 1)

visited_locs.insert(loc);

map_location adj[6];
get_adjacent_tiles(loc, adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(loc, adj.data());

for(int n = 0; n != 6; ++n) {
for(unsigned n = 0; n < adj.size(); ++n) {
if (resources::gameboard->map().on_board(adj[n]) && visited_locs.find( adj[n] ) == visited_locs.end() ) {
if (resources::gameboard->map().get_terrain_info(adj[n]).is_keep() ||
resources::gameboard->map().get_terrain_info(adj[n]).is_castle() ) {
Expand Down
6 changes: 3 additions & 3 deletions src/display.cpp
Expand Up @@ -981,7 +981,7 @@ std::vector<surface> display::get_fog_shroud_images(const map_location& loc, ima
{
std::vector<std::string> names;

std::array<map_location, 6> adjacent;
adjacent_loc_array_t adjacent;
get_adjacent_tiles(loc, adjacent.data());

enum visibility {FOG=0, SHROUD=1, CLEAR=2};
Expand All @@ -990,7 +990,7 @@ std::vector<surface> display::get_fog_shroud_images(const map_location& loc, ima
const std::string* image_prefix[] =
{ &game_config::fog_prefix, &game_config::shroud_prefix};

for(int i = 0; i != 6; ++i) {
for(int i = 0; i < 6; ++i) {
if(shrouded(adjacent[i])) {
tiles[i] = SHROUD;
} else if(!fogged(loc) && fogged(adjacent[i])) {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ void display::get_terrain_images(const map_location &loc,
const time_of_day& tod = get_time_of_day(loc);

//get all the light transitions
std::array<map_location, 6> adjs;
adjacent_loc_array_t adjs;
std::array<const time_of_day*, 6> atods;
get_adjacent_tiles(loc, adjs.data());
for(size_t d = 0; d < adjs.size(); ++d){
Expand Down
10 changes: 5 additions & 5 deletions src/display_context.cpp
Expand Up @@ -31,8 +31,8 @@ const team& display_context::get_team(int side) const

bool display_context::would_be_discovered(const map_location & loc, int side_num, bool see_all)
{
map_location adjs[6];
get_adjacent_tiles(loc,adjs);
adjacent_loc_array_t adjs;
get_adjacent_tiles(loc,adjs.data());

for (const map_location &u_loc : adjs)
{
Expand Down Expand Up @@ -83,9 +83,9 @@ bool display_context::unit_can_move(const unit &u) const

const team &current_team = get_team(u.side());

map_location locs[6];
get_adjacent_tiles(u.get_location(), locs);
for(int n = 0; n != 6; ++n) {
adjacent_loc_array_t locs;
get_adjacent_tiles(u.get_location(), locs.data());
for(unsigned n = 0; n < locs.size(); ++n) {
if (map().on_board(locs[n])) {
const unit_map::const_iterator i = units().find(locs[n]);
if (i.valid() && !i->incapacitated() &&
Expand Down
6 changes: 3 additions & 3 deletions src/editor/action/mouse/mouse_action_item.cpp
Expand Up @@ -34,10 +34,10 @@ void mouse_action_item::move(editor_display& disp, const map_location& hex)
update_brush_highlights(disp, hex);

std::set<map_location> adjacent_set;
map_location adjacent[6];
get_adjacent_tiles(previous_move_hex_, adjacent);
adjacent_loc_array_t adjacent;
get_adjacent_tiles(previous_move_hex_, adjacent.data());

for (int i = 0; i < 6; i++)
for (unsigned i = 0; i < adjacent.size(); i++)
adjacent_set.insert(adjacent[i]);

disp.invalidate(adjacent_set);
Expand Down
6 changes: 3 additions & 3 deletions src/editor/action/mouse/mouse_action_unit.cpp
Expand Up @@ -38,10 +38,10 @@ void mouse_action_unit::move(editor_display& disp, const map_location& hex)
update_brush_highlights(disp, hex);

std::set<map_location> adjacent_set;
map_location adjacent[6];
get_adjacent_tiles(previous_move_hex_, adjacent);
adjacent_loc_array_t adjacent;
get_adjacent_tiles(previous_move_hex_, adjacent.data());

for (int i = 0; i < 6; i++)
for (int i = 0; i < adjacent.size(); i++)
adjacent_set.insert(adjacent[i]);

disp.invalidate(adjacent_set);
Expand Down
6 changes: 3 additions & 3 deletions src/editor/map/editor_map.cpp
Expand Up @@ -122,9 +122,9 @@ std::set<map_location> editor_map::get_contiguous_terrain_tiles(const map_locati
queue.push_back(start);
//this is basically a breadth-first search along adjacent hexes
do {
map_location adj[6];
get_adjacent_tiles(queue.front(), adj);
for (int i = 0; i < 6; ++i) {
adjacent_loc_array_t adj;
get_adjacent_tiles(queue.front(), adj.data());
for (unsigned i = 0; i < adj.size(); ++i) {
if (on_board_with_border(adj[i]) && get_terrain(adj[i]) == terrain
&& result.find(adj[i]) == result.end()) {
result.insert(adj[i]);
Expand Down
6 changes: 3 additions & 3 deletions src/formula/function.cpp
Expand Up @@ -1226,11 +1226,11 @@ DEFINE_WFL_FUNCTION(adjacent_locs, 1, 1)
.convert_to<location_callable>()
->loc();

map_location adj[6];
get_adjacent_tiles(loc, adj);
adjacent_loc_array_t adj;
get_adjacent_tiles(loc, adj.data());

std::vector<variant> v;
for(int n = 0; n != 6; ++n) {
for(unsigned n = 0; n < adj.size(); ++n) {
v.emplace_back(std::make_shared<location_callable>(adj[n]));
}

Expand Down

0 comments on commit 515dbdd

Please sign in to comment.