From 515dbdd96b5733e0a9db436ec5c3726cdd8af34a Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 20 Feb 2018 06:25:43 +1100 Subject: [PATCH] Use std::array for adjacent map_location arrays 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. --- src/actions/attack.cpp | 8 +++---- src/actions/move.cpp | 6 ++--- src/actions/vision.cpp | 12 +++++----- src/ai/contexts.cpp | 10 ++++----- src/ai/default/aspect_attacks.cpp | 12 +++++----- src/ai/default/aspect_attacks.hpp | 2 +- src/ai/default/attack.cpp | 6 ++--- src/ai/default/ca.cpp | 8 +++---- src/ai/default/ca_move_to_targets.cpp | 18 +++++++-------- src/ai/default/contexts.cpp | 12 +++++----- src/ai/formula/ai.cpp | 12 +++++----- src/ai/formula/callable_objects.cpp | 6 ++--- src/ai/formula/function_table.cpp | 8 +++---- src/display.cpp | 6 ++--- src/display_context.cpp | 10 ++++----- src/editor/action/mouse/mouse_action_item.cpp | 6 ++--- src/editor/action/mouse/mouse_action_unit.cpp | 6 ++--- src/editor/map/editor_map.cpp | 6 ++--- src/formula/function.cpp | 6 ++--- src/formula/function_gamestate.cpp | 6 ++--- src/generators/cave_map_generator.cpp | 12 +++++----- src/generators/default_map_generator_job.cpp | 22 +++++++++---------- src/map/location.hpp | 3 +++ src/mouse_events.cpp | 14 ++++++------ src/pathfind/astarsearch.cpp | 2 +- src/pathfind/pathfind.cpp | 12 +++++----- src/pathutils.cpp | 6 ++--- src/terrain/filter.cpp | 4 ++-- src/tod_manager.cpp | 6 ++--- src/units/abilities.cpp | 20 ++++++++--------- src/units/filter.cpp | 4 ++-- src/units/udisplay.cpp | 10 ++++----- 32 files changed, 142 insertions(+), 139 deletions(-) diff --git a/src/actions/attack.cpp b/src/actions/attack.cpp index 6cec87d15a4ad..5deba2114eb51 100644 --- a/src/actions/attack.cpp +++ b/src/actions/attack.cpp @@ -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; } diff --git a/src/actions/move.cpp b/src/actions/move.cpp index f764619f22ac0..7f9eff34750ee 100644 --- a/src/actions/move.cpp +++ b/src/actions/move.cpp @@ -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]); diff --git a/src/actions/vision.cpp b/src/actions/vision.cpp index fb92c0ceca16a..113aea64d1762 100644 --- a/src/actions/vision.cpp +++ b/src/actions/vision.cpp @@ -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]); } @@ -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; diff --git a/src/ai/contexts.cpp b/src/ai/contexts.cpp index 2d187546174ad..6eb4009f5041b 100644 --- a/src/ai/contexts.cpp +++ b/src/ai/contexts.cpp @@ -981,9 +981,9 @@ const std::set& 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; @@ -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(); diff --git a/src/ai/default/aspect_attacks.cpp b/src/ai/default/aspect_attacks.cpp index bfd7e7fee0a2c..a0fe423b6b438 100644 --- a/src/ai/default/aspect_attacks.cpp +++ b/src/ai/default/aspect_attacks.cpp @@ -109,8 +109,8 @@ std::shared_ptr 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; @@ -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& units, std::vector& result, attack_analysis& cur_analysis, @@ -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) { @@ -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]) { diff --git a/src/ai/default/aspect_attacks.hpp b/src/ai/default/aspect_attacks.hpp index be30cb4e1b81c..9da132d5a87b9 100644 --- a/src/ai/default/aspect_attacks.hpp +++ b/src/ai/default/aspect_attacks.hpp @@ -55,7 +55,7 @@ class aspect_attacks_base : public typesafe_aspect { 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& units, std::vector& result, attack_analysis& cur_analysis, diff --git a/src/ai/default/attack.cpp b/src/ai/default/attack.cpp index f45f6bb88c68c..9f94db43d8cd9 100644 --- a/src/ai/default/attack.cpp +++ b/src/ai/default/attack.cpp @@ -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; diff --git a/src/ai/default/ca.cpp b/src/ai/default/ca.cpp index 67787c21867c8..f48994e338b27 100644 --- a/src/ai/default/ca.cpp +++ b/src/ai/default/ca.cpp @@ -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 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) { diff --git a/src/ai/default/ca_move_to_targets.cpp b/src/ai/default/ca_move_to_targets.cpp index 50cd468b85d5b..8d6f0dc16dacd 100644 --- a/src/ai/default/ca_move_to_targets.cpp +++ b/src/ai/default/ca_move_to_targets.cpp @@ -664,9 +664,9 @@ double move_to_targets_phase::compare_groups(const std::set& our_g void move_to_targets_phase::enemies_along_path(const std::vector& route, const move_map& dstsrc, std::set& res) { for(std::vector::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 itors = dstsrc.equal_range(adj[n]); for(move_map::const_iterator j = itors.first; j != itors.second; ++j) { res.insert(j->second); @@ -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 preferred_moves; @@ -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]); diff --git a/src/ai/default/contexts.cpp b/src/ai/default/contexts.cpp index bb19422019bf5..a3f7858e38714 100644 --- a/src/ai/default/contexts.cpp +++ b/src/ai/default/contexts.cpp @@ -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]); @@ -149,9 +149,9 @@ std::vector default_ai_context_impl::find_targets(const move_map& enemy_ //find the location of enemy threats std::set 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 itors = enemy_dstsrc.equal_range(adj[n]); while(itors.first != itors.second) { if(units_.count(itors.first->second)) { diff --git a/src/ai/formula/ai.cpp b/src/ai/formula/ai.cpp index 9f6539be54545..218b9abb6aad7 100644 --- a/src/ai/formula/ai.cpp +++ b/src/ai/formula/ai.cpp @@ -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; } @@ -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(loc)); break; diff --git a/src/ai/formula/callable_objects.cpp b/src/ai/formula/callable_objects.cpp index 88a1db66e8918..c44c783e2ca1f 100644 --- a/src/ai/formula/callable_objects.cpp +++ b/src/ai/formula/callable_objects.cpp @@ -271,10 +271,10 @@ void attack_map_callable::get_inputs(formula_input_vector& inputs) const { /* add to vars all attacks on enemy units around 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& 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; diff --git a/src/ai/formula/function_table.cpp b/src/ai/formula/function_table.cpp index 2991535e7cbd1..375919ff88e37 100644 --- a/src/ai/formula/function_table.cpp +++ b/src/ai/formula/function_table.cpp @@ -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; @@ -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() ) { diff --git a/src/display.cpp b/src/display.cpp index ad766e294e40c..186226857fd50 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -981,7 +981,7 @@ std::vector display::get_fog_shroud_images(const map_location& loc, ima { std::vector names; - std::array adjacent; + adjacent_loc_array_t adjacent; get_adjacent_tiles(loc, adjacent.data()); enum visibility {FOG=0, SHROUD=1, CLEAR=2}; @@ -990,7 +990,7 @@ std::vector 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])) { @@ -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 adjs; + adjacent_loc_array_t adjs; std::array atods; get_adjacent_tiles(loc, adjs.data()); for(size_t d = 0; d < adjs.size(); ++d){ diff --git a/src/display_context.cpp b/src/display_context.cpp index 3d85e065e6d0e..34041614fbaac 100644 --- a/src/display_context.cpp +++ b/src/display_context.cpp @@ -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) { @@ -83,9 +83,9 @@ bool display_context::unit_can_move(const unit &u) const const team ¤t_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() && diff --git a/src/editor/action/mouse/mouse_action_item.cpp b/src/editor/action/mouse/mouse_action_item.cpp index 06f4e3617b604..beae333912b08 100644 --- a/src/editor/action/mouse/mouse_action_item.cpp +++ b/src/editor/action/mouse/mouse_action_item.cpp @@ -34,10 +34,10 @@ void mouse_action_item::move(editor_display& disp, const map_location& hex) update_brush_highlights(disp, hex); std::set 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); diff --git a/src/editor/action/mouse/mouse_action_unit.cpp b/src/editor/action/mouse/mouse_action_unit.cpp index 471fa76660139..482ce4c3109d4 100644 --- a/src/editor/action/mouse/mouse_action_unit.cpp +++ b/src/editor/action/mouse/mouse_action_unit.cpp @@ -38,10 +38,10 @@ void mouse_action_unit::move(editor_display& disp, const map_location& hex) update_brush_highlights(disp, hex); std::set 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); diff --git a/src/editor/map/editor_map.cpp b/src/editor/map/editor_map.cpp index a22ef0794fb5c..ae5fe8a9df05e 100644 --- a/src/editor/map/editor_map.cpp +++ b/src/editor/map/editor_map.cpp @@ -122,9 +122,9 @@ std::set 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]); diff --git a/src/formula/function.cpp b/src/formula/function.cpp index 4338f4b0202aa..454807f0a6b4a 100644 --- a/src/formula/function.cpp +++ b/src/formula/function.cpp @@ -1226,11 +1226,11 @@ DEFINE_WFL_FUNCTION(adjacent_locs, 1, 1) .convert_to() ->loc(); - map_location adj[6]; - get_adjacent_tiles(loc, adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(loc, adj.data()); std::vector v; - for(int n = 0; n != 6; ++n) { + for(unsigned n = 0; n < adj.size(); ++n) { v.emplace_back(std::make_shared(adj[n])); } diff --git a/src/formula/function_gamestate.cpp b/src/formula/function_gamestate.cpp index 409fdaa3d1019..a15c59fc53838 100644 --- a/src/formula/function_gamestate.cpp +++ b/src/formula/function_gamestate.cpp @@ -29,11 +29,11 @@ namespace gamestate { DEFINE_WFL_FUNCTION(adjacent_locs, 1, 1) { const map_location loc = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "adjacent_locs:location")).convert_to()->loc(); - map_location adj[6]; - get_adjacent_tiles(loc, adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(loc, adj.data()); std::vector v; - for(int n = 0; n != 6; ++n) { + for(unsigned n = 0; n < adj.size(); ++n) { if(resources::gameboard->map().on_board(adj[n])) { v.emplace_back(std::make_shared(adj[n])); } diff --git a/src/generators/cave_map_generator.cpp b/src/generators/cave_map_generator.cpp index 40f58c2c49dc6..fd6a40ec5a40d 100644 --- a/src/generators/cave_map_generator.cpp +++ b/src/generators/cave_map_generator.cpp @@ -130,9 +130,9 @@ void cave_map_generator::cave_map_generator_job::build_chamber(map_location loc, locs.insert(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(int(rng_() % 100) < (100l - static_cast(jagged))) { build_chamber(adj[n],locs,size-1,jagged); } @@ -359,9 +359,9 @@ void cave_map_generator::cave_map_generator_job::place_castle(int starting_posit starting_positions_.insert(t_translation::starting_positions::value_type(std::to_string(starting_position), coord)); } - 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) { set_terrain(adj[n], params.castle_); } } diff --git a/src/generators/default_map_generator_job.cpp b/src/generators/default_map_generator_job.cpp index cf9467ffd028f..f8a85d6b44bf4 100644 --- a/src/generators/default_map_generator_job.cpp +++ b/src/generators/default_map_generator_job.cpp @@ -456,8 +456,8 @@ bool default_map_generator_job::generate_river_internal(const height_map& height } map_location current_loc(x,y); - map_location adj[6]; - get_adjacent_tiles(current_loc,adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(current_loc,adj.data()); std::shuffle(std::begin(adj), std::end(adj), rng_); // Mark that we have attempted from this map_location @@ -629,9 +629,9 @@ static map_location place_village(const t_translation::ter_map& map, } int rating = child["rating"]; - map_location adj[6]; - get_adjacent_tiles(map_location(i.x,i.y),adj); - for(size_t n = 0; n != 6; ++n) { + adjacent_loc_array_t adj; + get_adjacent_tiles(map_location(i.x,i.y),adj.data()); + for(size_t n = 0; n < adj.size(); ++n) { if(adj[n].x < 0 || adj[n].y < 0 || adj[n].x >= map.w || adj[n].y >= map.h) { @@ -658,8 +658,8 @@ static void flood_name(const map_location& start, const std::string& name, std:: const t_translation::ter_match& tile_types, const terrain_map& terrain, unsigned width, unsigned height, size_t label_count, std::map* labels, const std::string& full_name) { - map_location adj[6]; - get_adjacent_tiles(start,adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(start,adj.data()); size_t n; //if adjacent tiles are tiles and unnamed, name them for(n = 0; n < 6; n++) { @@ -1060,8 +1060,8 @@ std::string default_map_generator_job::default_generate_map(generator_data data, const map_location& last = *(step-1); const map_location& next = *(step+1); - map_location adj[6]; - get_adjacent_tiles(*step,adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(*step,adj.data()); int direction = -1; @@ -1290,8 +1290,8 @@ std::string default_map_generator_job::default_generate_map(generator_data data, const map_location loc(res.x-data.width/3,res.y-data.height/3); - map_location adj[6]; - get_adjacent_tiles(loc,adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(loc,adj.data()); std::string name_type = "village"; const t_translation::ter_list diff --git a/src/map/location.hpp b/src/map/location.hpp index baedbc66fa4b3..3b497b4d3b70f 100644 --- a/src/map/location.hpp +++ b/src/map/location.hpp @@ -19,6 +19,7 @@ class config; class variable_set; +#include #include #include #include @@ -125,6 +126,8 @@ struct map_location { int x, y; }; +using adjacent_loc_array_t = std::array; + /** Function which tells if two locations are adjacent. */ bool tiles_adjacent(const map_location& a, const map_location& b); diff --git a/src/mouse_events.cpp b/src/mouse_events.cpp index 601a8e7b2de34..5959e9f45507b 100644 --- a/src/mouse_events.cpp +++ b/src/mouse_events.cpp @@ -447,10 +447,10 @@ map_location mouse_handler::current_unit_attacks_from(const map_location& loc) c int best_rating = 100; // smaller is better map_location res; - map_location adj[6]; - get_adjacent_tiles(loc, adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(loc, adj.data()); - for(size_t n = 0; n != 6; ++n) { + for(size_t n = 0; n < adj.size(); ++n) { if(pc_.gamestate().board_.map().on_board(adj[n]) == false) { continue; } @@ -1085,8 +1085,8 @@ std::set mouse_handler::get_adj_enemies(const map_location& loc, i const team& uteam = pc_.gamestate().board_.teams_[side - 1]; - map_location adj[6]; - get_adjacent_tiles(loc, adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(loc, adj.data()); for(const map_location& aloc : adj) { unit_map::const_iterator i = find_unit(aloc); @@ -1117,8 +1117,8 @@ void mouse_handler::show_attack_options(const unit_map::const_iterator& u) const team& u_team = pc_.gamestate().board_.teams_[u->side() - 1]; // Check each adjacent hex. - map_location adj[6]; - get_adjacent_tiles(u->get_location(), adj); + adjacent_loc_array_t adj; + get_adjacent_tiles(u->get_location(), adj.data()); for(const map_location& loc : adj) { // No attack option shown if no visible unit present. diff --git a/src/pathfind/astarsearch.cpp b/src/pathfind/astarsearch.cpp index 95518ecd85421..829af49c56249 100644 --- a/src/pathfind/astarsearch.cpp +++ b/src/pathfind/astarsearch.cpp @@ -196,7 +196,7 @@ plain_route a_star_search(const map_location& src, const map_location& dst, int i = locs.size(); - get_adjacent_tiles(n.curr, &locs[0]); + get_adjacent_tiles(n.curr, locs.data()); for (; i-- > 0;) { if (!locs[i].valid(width, height, border)) continue; diff --git a/src/pathfind/pathfind.cpp b/src/pathfind/pathfind.cpp index 23a4d409641a9..c17f73b58efc6 100644 --- a/src/pathfind/pathfind.cpp +++ b/src/pathfind/pathfind.cpp @@ -92,8 +92,8 @@ map_location find_vacant_tile(const map_location& loc, VACANT_TILE_TYPE vacancy, if (pass_check_and_unreachable && distance > 10) continue; //If the hex is empty and we do either no pass check or the hex is reachable, return it. if (units.find(l) == units.end() && !pass_check_and_unreachable) return l; - map_location adjs[6]; - get_adjacent_tiles(l,adjs); + adjacent_loc_array_t adjs; + get_adjacent_tiles(l,adjs.data()); for (const map_location &l2 : adjs) { if (!map.on_board(l2)) continue; @@ -137,9 +137,9 @@ bool enemy_zoc(const team& current_team, const map_location& loc, const team& viewing_team, bool see_all) { // Check the adjacent tiles. - map_location locs[6]; - get_adjacent_tiles(loc,locs); - for (int i = 0; i != 6; ++i) + adjacent_loc_array_t locs; + get_adjacent_tiles(loc,locs.data()); + for (unsigned i = 0; i < locs.size(); ++i) { const unit *u = resources::gameboard->get_visible_unit(locs[i], viewing_team, see_all); if ( u && current_team.is_enemy(u->side()) && u->emits_zoc() ) @@ -345,7 +345,7 @@ static void find_routes( // Get the locations adjacent to current. std::vector adj_locs(6); - get_adjacent_tiles(cur_hex, &adj_locs[0]); + get_adjacent_tiles(cur_hex, adj_locs.data()); // Sort adjacents by on-boardness auto off_board_it = std::partition(adj_locs.begin(), adj_locs.end(), [&index](map_location loc){ diff --git a/src/pathutils.cpp b/src/pathutils.cpp index 130ee8b1e9566..3bb763c0f8607 100644 --- a/src/pathutils.cpp +++ b/src/pathutils.cpp @@ -249,9 +249,9 @@ void get_tiles_radius(const gamemap& map, const std::vector& locs, result.insert(it, it_end); for(; it != it_end; ++it) { - map_location adj[6]; - get_adjacent_tiles(*it, adj); - for(size_t i = 0; i != 6; ++i) { + adjacent_loc_array_t adj; + get_adjacent_tiles(*it, adj.data()); + for(size_t i = 0; i < adj.size(); ++i) { const map_location& loc = adj[i]; if ( with_border ? map.on_board_with_border(loc) : map.on_board(loc) ) { diff --git a/src/terrain/filter.cpp b/src/terrain/filter.cpp index 82c77cdccb20d..216575936f7b0 100644 --- a/src/terrain/filter.cpp +++ b/src/terrain/filter.cpp @@ -199,8 +199,8 @@ bool terrain_filter::match_internal(const map_location& loc, const unit* ref_uni //Allow filtering on adjacent locations if(cfg_.has_child("filter_adjacent_location")) { - map_location adjacent[6]; - get_adjacent_tiles(loc, adjacent); + adjacent_loc_array_t adjacent; + get_adjacent_tiles(loc, adjacent.data()); const vconfig::child_list& adj_cfgs = cfg_.get_children("filter_adjacent_location"); vconfig::child_list::const_iterator i, i_end, i_begin = adj_cfgs.begin(); for (i = i_begin, i_end = adj_cfgs.end(); i != i_end; ++i) { diff --git a/src/tod_manager.cpp b/src/tod_manager.cpp index 8c1cf3a9aa36c..c9fb7cdf3e177 100644 --- a/src/tod_manager.cpp +++ b/src/tod_manager.cpp @@ -234,10 +234,10 @@ const time_of_day tod_manager::get_illuminated_time_of_day(const unit_map & unit int most_sub = 0; // Find the "illuminates" effects from units that can affect loc. - map_location locs[7]; + std::array locs; locs[0] = loc; - get_adjacent_tiles(loc,locs+1); - for ( size_t i = 0; i != 7; ++i ) { + get_adjacent_tiles(loc, locs.data() + 1); // start at [1] + for ( size_t i = 0; i < locs.size(); ++i ) { const unit_map::const_iterator itor = units.find(locs[i]); if (itor != units.end() && itor->get_ability_bool("illuminates", *resources::gameboard) && diff --git a/src/units/abilities.cpp b/src/units/abilities.cpp index 40cf6b30cece6..3131ac07026d3 100644 --- a/src/units/abilities.cpp +++ b/src/units/abilities.cpp @@ -149,9 +149,9 @@ bool unit::get_ability_bool(const std::string& tag_name, const map_location& loc assert(display::get_singleton()); const unit_map& units = display::get_singleton()->get_units(); - 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) { const unit_map::const_iterator it = units.find(adjacent[i]); if (it == units.end() || it->incapacitated()) continue; @@ -190,9 +190,9 @@ unit_ability_list unit::get_abilities(const std::string& tag_name, const map_loc assert(display::get_singleton()); const unit_map& units = display::get_singleton()->get_units(); - 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) { const unit_map::const_iterator it = units.find(adjacent[i]); if (it == units.end() || it->incapacitated()) continue; @@ -312,8 +312,8 @@ bool unit::ability_active(const std::string& ability,const config& cfg,const map if ( !unit_filter(vconfig(afilter)).set_use_flat_tod(illuminates).matches(*this, loc) ) return false; - map_location adjacent[6]; - get_adjacent_tiles(loc,adjacent); + adjacent_loc_array_t adjacent; + get_adjacent_tiles(loc,adjacent.data()); assert(display::get_singleton()); const unit_map& units = display::get_singleton()->get_units(); @@ -946,8 +946,8 @@ bool attack_type::special_active(const config& special, AFFECTS whom, if (!special_unit_matches(def, att, def_loc, def_weapon, special, is_for_listing_, "filter_defender")) return false; - map_location adjacent[6]; - get_adjacent_tiles(self_loc_, adjacent); + adjacent_loc_array_t adjacent; + get_adjacent_tiles(self_loc_, adjacent.data()); // Filter the adjacent units. for (const config &i : special.child_range("filter_adjacent")) diff --git a/src/units/filter.cpp b/src/units/filter.cpp index f1057016e5309..e288ffc73ee25 100644 --- a/src/units/filter.cpp +++ b/src/units/filter.cpp @@ -125,8 +125,8 @@ struct unit_filter_adjacent : public unit_filter_base virtual bool matches(const unit_filter_args& args) const override { const unit_map& units = args.fc->get_disp_context().units(); - map_location adjacent[6]; - get_adjacent_tiles(args.loc, adjacent); + adjacent_loc_array_t adjacent; + get_adjacent_tiles(args.loc, adjacent.data()); int match_count=0; config::attribute_value i_adjacent = cfg_["adjacent"]; diff --git a/src/units/udisplay.cpp b/src/units/udisplay.cpp index 814df00d78b08..5a0c194817e06 100644 --- a/src/units/udisplay.cpp +++ b/src/units/udisplay.cpp @@ -381,12 +381,12 @@ void unit_mover::wait_for_anims() /// not left on screen after unit movement in particular. if ( disp_ ) { // Should always be true if we get here. // Invalidate the hexes around the move that prompted this wait. - map_location arr[6]; - get_adjacent_tiles(path_[current_-1], arr); - for ( unsigned i = 0; i < 6; ++i ) + adjacent_loc_array_t arr; + get_adjacent_tiles(path_[current_-1], arr.data()); + for ( unsigned i = 0; i < arr.size(); ++i ) disp_->invalidate(arr[i]); - get_adjacent_tiles(path_[current_], arr); - for ( unsigned i = 0; i < 6; ++i ) + get_adjacent_tiles(path_[current_], arr.data()); + for ( unsigned i = 0; i < arr.size(); ++i ) disp_->invalidate(arr[i]); } }