The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
gamemap Class Reference

Encapsulates the map of the game. More...

#include <map.hpp>

Inheritance diagram for gamemap:
Inheritance graph

Public Types

enum  { MAX_PLAYERS = 9 }
 Maximum number of players supported. More...
 
using tstarting_positions = t_translation::tstarting_positions
 

Public Member Functions

const t_translation::t_listunderlying_mvt_terrain (const map_location &loc) const
 
const t_translation::t_listunderlying_def_terrain (const map_location &loc) const
 
const t_translation::t_listunderlying_union_terrain (const map_location &loc) const
 
std::string get_terrain_string (const map_location &loc) const
 
std::string get_terrain_editor_string (const map_location &loc) const
 
bool is_village (const map_location &loc) const
 
int gives_healing (const map_location &loc) const
 
bool is_castle (const map_location &loc) const
 
bool is_keep (const map_location &loc) const
 
const t_translation::t_listunderlying_mvt_terrain (const t_translation::t_terrain &terrain) const
 
const t_translation::t_listunderlying_def_terrain (const t_translation::t_terrain &terrain) const
 
const t_translation::t_listunderlying_union_terrain (const t_translation::t_terrain &terrain) const
 
std::string get_terrain_string (const t_translation::t_terrain &terrain) const
 
std::string get_terrain_editor_string (const t_translation::t_terrain &terrain) const
 
std::string get_underlying_terrain_string (const t_translation::t_terrain &terrain) const
 
bool is_village (const t_translation::t_terrain &terrain) const
 
int gives_healing (const t_translation::t_terrain &terrain) const
 
bool is_castle (const t_translation::t_terrain &terrain) const
 
bool is_keep (const t_translation::t_terrain &terrain) const
 
const terrain_typeget_terrain_info (const t_translation::t_terrain &terrain) const
 
const tdata_cachetdata () const
 
 gamemap (const tdata_cache &tdata, const std::string &data)
 Loads a map, with the given terrain configuration. More...
 
 gamemap (const tdata_cache &tdata, const config &level)
 Loads a map, from the [map] wml config in level. More...
 
virtual ~gamemap ()
 
void read (const std::string &data, const bool allow_invalid=true, const int border_size=1)
 
std::string write () const
 
void overlay (const gamemap &m, const config &rules, int x=0, int y=0, bool border=false)
 Overlays another map onto this one at the given position. More...
 
int w () const
 Effective map width. More...
 
int h () const
 Effective map height. More...
 
int border_size () const
 Size of the map border. More...
 
int total_width () const
 Real width of the map, including borders. More...
 
int total_height () const
 Real height of the map, including borders. More...
 
const t_translation::t_terrain operator[] (const map_location &loc) const
 
t_translation::t_terrain get_terrain (const map_location &loc) const
 Looks up terrain at a particular location. More...
 
void write_terrain (const map_location &loc, config &cfg) const
 Writes the terrain at loc to cfg. More...
 
void set_starting_position (int side, const map_location &loc)
 Manipulate starting positions of the different sides. More...
 
map_location starting_position (int side) const
 
void set_special_location (const std::string &id, const map_location &loc)
 
map_location special_location (const std::string &id) const
 
const std::stringis_starting_position (const map_location &loc) const
 returns the side number of the side starting at position loc, 0 if no such side exists. More...
 
int num_valid_starting_positions () const
 
bool on_board (const map_location &loc) const
 Tell if a location is on the map. More...
 
bool on_board_with_border (const map_location &loc) const
 
bool empty () const
 Tell if the map is of 0 size. More...
 
const std::vector< map_location > & villages () const
 Return a list of the locations of villages on the map. More...
 
const terrain_typeget_terrain_info (const map_location &loc) const
 Shortcut to get_terrain_info(get_terrain(loc)). More...
 
const t_translation::t_listget_terrain_list () const
 Gets the list of terrains. More...
 
void set_terrain (const map_location &loc, const t_translation::t_terrain &terrain, const terrain_type_data::tmerge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false)
 Clobbers over the terrain at location 'loc', with the given terrain. More...
 
const std::map
< t_translation::t_terrain,
size_t > & 
get_weighted_terrain_frequencies () const
 Returns a list of the frequencies of different terrain types on the map, with terrain nearer the center getting weighted higher. More...
 
void remove_from_border_cache (const map_location &loc)
 Remove the cached border terrain at loc. More...
 
std::vector< map_locationparse_location_range (const std::string &xvals, const std::string &yvals, bool with_border=false) const
 Parses ranges of locations into a vector of locations, using this map's dimensions as bounds. More...
 
const tstarting_positionsspecial_locations () const
 

Static Public Attributes

static const int default_border = 1
 The default border style for a map. More...
 

Protected Member Functions

void clear_border_cache ()
 Clears the border cache, needed for the editor. More...
 

Protected Attributes

t_translation::t_map tiles_
 
tstarting_positions starting_positions_
 The size of the starting positions array is MAX_PLAYERS + 1, because the positions themselves are numbered from 1. More...
 
int w_
 Sizes of the map area. More...
 
int h_
 
int total_width_
 Sizes of the map including the borders. More...
 
int total_height_
 

Private Member Functions

int read_header (const std::string &data)
 Reads the header of a map which is saved in the deprecated map_data format. More...
 

Private Attributes

tdata_cache tdata_
 Allows lookup of terrain at a particular location. More...
 
std::vector< map_locationvillages_
 
std::map< map_location,
t_translation::t_terrain
borderCache_
 
std::map
< t_translation::t_terrain,
size_t > 
terrainFrequencyCache_
 
int border_size_
 The size of the border around the map. More...
 

Detailed Description

Encapsulates the map of the game.

Although the game is hexagonal, the map is stored as a grid. Each type of terrain is represented by a multiletter terrain code.

Todo:
Update for new map-format.

Definition at line 37 of file map.hpp.

Member Typedef Documentation

Definition at line 207 of file map.hpp.

Member Enumeration Documentation

anonymous enum

Maximum number of players supported.

Warning: when you increase this, you need to add more definitions to the team_colors.cfg file.

Enumerator
MAX_PLAYERS 

Definition at line 198 of file map.hpp.

Constructor & Destructor Documentation

gamemap::gamemap ( const tdata_cache tdata,
const std::string data 
)

Loads a map, with the given terrain configuration.

Data should be a series of lines, with each character representing one hex on the map. Starting locations are represented by numbers.

Parameters
tdatathe terrain data
datathe map data to load.

Definition at line 108 of file map.cpp.

References DBG_G, and read().

gamemap::gamemap ( const tdata_cache tdata,
const config level 
)

Loads a map, from the [map] wml config in level.

Data should be a series of lines, with each character representing one hex on the map. Starting locations are represented by numbers

Parameters
tdatathe terrain data
levelthe scenario config to load from.

Definition at line 125 of file map.cpp.

References DBG_G, config::debug(), h_, read(), total_height_, total_width_, and w_.

gamemap::~gamemap ( )
virtual

Definition at line 150 of file map.cpp.

Member Function Documentation

int gamemap::border_size ( ) const
inline
void gamemap::clear_border_cache ( )
inlineprotected

Clears the border cache, needed for the editor.

Definition at line 221 of file map.hpp.

References borderCache_.

Referenced by editor::editor_map::expand_left(), and editor::editor_map::expand_top().

bool gamemap::empty ( ) const
inline

Tell if the map is of 0 size.

Definition at line 157 of file map.hpp.

References h_, and w_.

Referenced by display::draw(), and team_builder::init().

t_translation::t_terrain gamemap::get_terrain ( const map_location loc) const

Looks up terrain at a particular location.

Hexes off the map may be looked up, and their 'emulated' terrain will also be returned. This allows proper drawing of the edges of the map.

Definition at line 341 of file map.cpp.

References border_size_, borderCache_, c, get_adjacent_tiles(), i, mp_ui_alerts::items, itor, t_translation::NONE_TERRAIN, map_location::null_location(), on_board(), on_board_with_border(), tdata_, tiles_, map_location::x, and map_location::y.

Referenced by terrain_builder::build_terrains(), ai::ai_default_rca::move_to_targets_phase::choose_move(), editor::mouse_action_paint::click_left(), editor::mouse_action_fill::click_left(), editor::mouse_action_paint::click_right(), editor::mouse_action_fill::click_right(), display::draw_hex(), editor::map_context::draw_terrain_actual(), preferences::encounter_map_terrain(), ai::ai_default_rca::get_villages_phase::find_villages(), footsteps_images(), editor::editor_map::get_contiguous_terrain_tiles(), get_terrain_editor_string(), get_terrain_info(), get_terrain_string(), gives_healing(), basic_unit_filter_impl::internal_matches_filter(), unit_animation_component::invalidate(), is_castle(), is_keep(), is_village(), editor::editor_map::mask_to(), terrain_filter::match_internal(), unit_animation::matches(), ai::ai_default_rca::move_to_targets_phase::move_group(), editor::editor_action_fill::perform(), editor::editor_action_shuffle_area::perform_without_undo(), ai::ai_default_rca::move_to_targets_phase::rate_group(), ai::ai_default_rca::aspect_attacks_base::rate_terrain(), ai::default_ai_context_impl::rate_terrain(), unit_drawer::redraw_unit(), REPORT_GENERATOR(), set_terrain(), underlying_def_terrain(), underlying_mvt_terrain(), underlying_union_terrain(), unit_box_at(), and write_terrain().

std::string gamemap::get_terrain_editor_string ( const map_location loc) const
std::string gamemap::get_terrain_editor_string ( const t_translation::t_terrain terrain) const

Definition at line 87 of file map.cpp.

References tdata_.

const terrain_type & gamemap::get_terrain_info ( const t_translation::t_terrain terrain) const
const terrain_type & gamemap::get_terrain_info ( const map_location loc) const

Shortcut to get_terrain_info(get_terrain(loc)).

Definition at line 52 of file map.cpp.

References get_terrain(), and tdata_.

const t_translation::t_list & gamemap::get_terrain_list ( ) const

Gets the list of terrains.

Definition at line 46 of file map.cpp.

References tdata_.

Referenced by editor::terrain_palette::setup().

std::string gamemap::get_terrain_string ( const map_location loc) const

Definition at line 63 of file map.cpp.

References get_terrain().

Referenced by REPORT_GENERATOR().

std::string gamemap::get_terrain_string ( const t_translation::t_terrain terrain) const

Definition at line 85 of file map.cpp.

References tdata_.

std::string gamemap::get_underlying_terrain_string ( const t_translation::t_terrain terrain) const

Definition at line 89 of file map.cpp.

References tdata_.

Referenced by REPORT_GENERATOR().

const std::map< t_translation::t_terrain, size_t > & gamemap::get_weighted_terrain_frequencies ( ) const

Returns a list of the frequencies of different terrain types on the map, with terrain nearer the center getting weighted higher.

Definition at line 513 of file map.cpp.

References border_size_, distance_between(), h(), i, terrainFrequencyCache_, tiles_, w(), and map_location::ZERO().

int gamemap::gives_healing ( const map_location loc) const
int gamemap::gives_healing ( const t_translation::t_terrain terrain) const

Definition at line 93 of file map.cpp.

References tdata_.

int gamemap::h ( ) const
inline

Effective map height.

Definition at line 108 of file map.hpp.

References h_.

Referenced by ai::ai_default_rca::move_to_targets_phase::access_points(), display::bounds_check_position(), terrain_builder::build_terrains(), game_state::can_recruit_on(), ai::ai_default_rca::move_to_targets_phase::choose_move(), actions::shroud_clearer::clear_loc(), editor::editor_map::clone_column(), ai::default_recruitment::recruitment::compare_cost_maps_and_update_important_hexes(), events::menu_handler::do_search(), display::draw_minimap(), display::draw_minimap_units(), preferences::encounter_map_terrain(), ai::ai_default_rca::goto_phase::evaluate(), ai::ai_default_rca::move_leader_to_goals_phase::evaluate(), ai::ai_default_rca::move_leader_to_keep_phase::evaluate(), pathfind::find_routes(), ai::default_ai_context_impl::find_targets(), pathfind::full_cost_map::full_cost_map(), game_info::game_info(), ai::keeps_cache::get(), terrain_filter::get_locations(), pathfind::full_cost_map::get_pair_at(), events::mouse_handler::get_route(), get_tiles_radius(), get_weighted_terrain_frequencies(), image::getMinimap(), game_lua_kernel::intf_find_path(), game_lua_kernel::intf_get_map_size(), editor::editor_map::invert_selection(), editor::editor_map::mask_to(), display::max_map_area(), display::minimap_location_on(), editor::context_manager::new_map_dialog(), editor::context_manager::new_scenario_dialog(), overlay(), parse_location_range(), mp::gamebrowser::populate_game_item_map_info(), terrain_builder::rebuild_cache_all(), editor::editor_map::resize(), editor::context_manager::resize_map_dialog(), editor::editor_map::same_size_as(), editor::editor_map::sanity_check(), editor::map_context::set_map(), ai::move_result::test_route(), and ai::default_recruitment::recruitment::update_average_local_cost().

bool gamemap::is_castle ( const map_location loc) const
bool gamemap::is_castle ( const t_translation::t_terrain terrain) const

Definition at line 95 of file map.cpp.

References tdata_.

bool gamemap::is_keep ( const map_location loc) const
bool gamemap::is_keep ( const t_translation::t_terrain terrain) const

Definition at line 97 of file map.cpp.

References tdata_.

const std::string * gamemap::is_starting_position ( const map_location loc) const
bool gamemap::is_village ( const map_location loc) const
bool gamemap::is_village ( const t_translation::t_terrain terrain) const

Definition at line 91 of file map.cpp.

References tdata_.

int gamemap::num_valid_starting_positions ( ) const

Definition at line 426 of file map.cpp.

References starting_positions_.

Referenced by game_state::place_sides_in_preferred_locations().

bool gamemap::on_board ( const map_location loc) const

Tell if a location is on the map.

Should be called before indexing using [].

Todo:
inline for performance? – Ilor

Definition at line 467 of file map.cpp.

References h_, map_location::valid(), w_, map_location::x, and map_location::y.

Referenced by ai::ai_default_rca::move_to_targets_phase::choose_move(), actions::shroud_clearer::clear_loc(), editor::mouse_action_item::click_left(), editor::mouse_action_unit::click_left(), pathfind::shortest_path_calculator::cost(), pathfind::move_type_path_calculator::cost(), pathfind::emergency_path_calculator::cost(), events::mouse_handler::current_unit_attacks_from(), ai::ai_default_rca::aspect_attacks_base::do_attack_analysis(), events::console_handler::do_create(), editor::mouse_action_item::drag_end_left(), editor::mouse_action_unit::drag_end_left(), game_display::draw_hex(), display::draw_hex(), display::draw_invalidated(), ai::ai_default_rca::goto_phase::evaluate(), ai::ai_default_rca::move_to_targets_phase::execute(), unit_creator::find_location(), ai::default_ai_context_impl::find_targets(), pathfind::find_vacant_tile(), get_terrain(), get_tiles_radius(), image::getMinimap(), gives_healing(), basic_unit_filter_impl::internal_matches_filter(), game_lua_kernel::intf_teleport(), is_castle(), is_keep(), is_village(), unit::is_visible_to_team(), editor::mouse_action::key_event(), terrain_filter::match(), terrain_filter::match_internal(), events::mouse_handler::mouse_motion(), ai::ai_default_rca::move_to_targets_phase::move_group(), ai::ai_default_rca::remove_wrong_targets::operator()(), placing_score(), controller_base::play_slice(), ai::readonly_context_impl::power_projection(), game_board::replace_map(), REPORT_GENERATOR(), events::mouse_handler::select_or_action(), set_terrain(), events::mouse_handler::show_attack_options(), display_context::unit_can_move(), unit_defense(), unit_status(), editor::mouse_action_village::up_left(), editor::mouse_action_map_label::up_left(), editor::mouse_action_unit::up_left(), editor::mouse_action_item::up_left(), editor::mouse_action_starting_position::up_left(), editor::mouse_action_village::up_right(), and ai::default_recruitment::recruitment::update_important_hexes().

bool gamemap::on_board_with_border ( const map_location loc) const
const t_translation::t_terrain gamemap::operator[] ( const map_location loc) const
inline

Definition at line 119 of file map.hpp.

References border_size_, tiles_, map_location::x, and map_location::y.

void gamemap::overlay ( const gamemap m,
const config rules,
int  x = 0,
int  y = 0,
bool  border = false 
)
std::vector< map_location > gamemap::parse_location_range ( const std::string xvals,
const std::string yvals,
bool  with_border = false 
) const

Parses ranges of locations into a vector of locations, using this map's dimensions as bounds.

Definition at line 537 of file map.cpp.

References border_size(), h(), i, utils::parse_range(), utils::split(), and w().

Referenced by tod_manager::add_time_area(), team::build(), terrain_filterimpl::filter_xy(), and terrain_filter::get_locations().

void gamemap::read ( const std::string data,
const bool  allow_invalid = true,
const int  border_size = 1 
)
int gamemap::read_header ( const std::string data)
private

Reads the header of a map which is saved in the deprecated map_data format.

Parameters
dataThe mapdata to load.

Definition at line 218 of file map.cpp.

References border_size_, and read().

Referenced by read().

void gamemap::remove_from_border_cache ( const map_location loc)
inline

Remove the cached border terrain at loc.

Needed by the editor to make tiles at the border update correctly when drawing other tiles.

Definition at line 189 of file map.hpp.

References borderCache_.

Referenced by set_terrain().

void gamemap::set_special_location ( const std::string id,
const map_location loc 
)
void gamemap::set_starting_position ( int  side,
const map_location loc 
)

Manipulate starting positions of the different sides.

Definition at line 462 of file map.cpp.

References set_special_location().

void gamemap::set_terrain ( const map_location loc,
const t_translation::t_terrain terrain,
const terrain_type_data::tmerge_mode  mode = terrain_type_data::BOTH,
bool  replace_if_failed = false 
)
map_location gamemap::special_location ( const std::string id) const
const tstarting_positions& gamemap::special_locations ( ) const
inline

Definition at line 208 of file map.hpp.

References starting_positions_.

map_location gamemap::starting_position ( int  side) const
const tdata_cache& gamemap::tdata ( ) const
inline
int gamemap::total_height ( ) const
inline

Real height of the map, including borders.

Definition at line 117 of file map.hpp.

References total_height_.

Referenced by editor::editor_map::everything_selected(), image::getMinimap(), editor::editor_map::sanity_check(), and game_events::WML_HANDLER_FUNCTION().

int gamemap::total_width ( ) const
inline

Real width of the map, including borders.

Definition at line 114 of file map.hpp.

References total_width_.

Referenced by editor::editor_map::everything_selected(), image::getMinimap(), editor::editor_map::sanity_check(), and game_events::WML_HANDLER_FUNCTION().

const t_translation::t_list & gamemap::underlying_def_terrain ( const map_location loc) const

Definition at line 59 of file map.cpp.

References get_terrain().

Referenced by unit_defense().

const t_translation::t_list & gamemap::underlying_def_terrain ( const t_translation::t_terrain terrain) const

Definition at line 81 of file map.cpp.

References tdata_.

const t_translation::t_list & gamemap::underlying_mvt_terrain ( const map_location loc) const

Definition at line 57 of file map.cpp.

References get_terrain().

const t_translation::t_list & gamemap::underlying_mvt_terrain ( const t_translation::t_terrain terrain) const

Definition at line 79 of file map.cpp.

References tdata_.

const t_translation::t_list & gamemap::underlying_union_terrain ( const map_location loc) const

Definition at line 61 of file map.cpp.

References get_terrain().

Referenced by preferences::encounter_map_terrain(), REPORT_GENERATOR(), and unit_box_at().

const t_translation::t_list & gamemap::underlying_union_terrain ( const t_translation::t_terrain terrain) const

Definition at line 83 of file map.cpp.

References tdata_.

const std::vector<map_location>& gamemap::villages ( ) const
inline
int gamemap::w ( ) const
inline

Effective map width.

Definition at line 105 of file map.hpp.

References w_.

Referenced by ai::ai_default_rca::move_to_targets_phase::access_points(), display::bounds_check_position(), terrain_builder::build_terrains(), game_state::can_recruit_on(), ai::ai_default_rca::move_to_targets_phase::choose_move(), actions::shroud_clearer::clear_loc(), ai::default_recruitment::recruitment::compare_cost_maps_and_update_important_hexes(), events::menu_handler::do_search(), display::draw_minimap(), display::draw_minimap_units(), preferences::encounter_map_terrain(), ai::ai_default_rca::goto_phase::evaluate(), ai::ai_default_rca::move_leader_to_goals_phase::evaluate(), ai::ai_default_rca::move_leader_to_keep_phase::evaluate(), pathfind::find_routes(), ai::default_ai_context_impl::find_targets(), pathfind::full_cost_map::full_cost_map(), game_info::game_info(), ai::keeps_cache::get(), terrain_filter::get_locations(), pathfind::full_cost_map::get_pair_at(), events::mouse_handler::get_route(), get_tiles_radius(), get_weighted_terrain_frequencies(), image::getMinimap(), game_lua_kernel::intf_find_path(), game_lua_kernel::intf_get_map_size(), editor::editor_map::invert_selection(), editor::editor_map::mask_to(), display::max_map_area(), display::minimap_location_on(), editor::context_manager::new_map_dialog(), editor::context_manager::new_scenario_dialog(), overlay(), parse_location_range(), mp::gamebrowser::populate_game_item_map_info(), terrain_builder::rebuild_cache_all(), editor::editor_map::resize(), editor::context_manager::resize_map_dialog(), editor::editor_map::same_size_as(), editor::editor_map::sanity_check(), editor::map_context::set_map(), ai::move_result::test_route(), and ai::default_recruitment::recruitment::update_average_local_cost().

std::string gamemap::write ( ) const
void gamemap::write_terrain ( const map_location loc,
config cfg 
) const

Writes the terrain at loc to cfg.

Definition at line 103 of file map.cpp.

References get_terrain(), and t_translation::write_terrain_code().

Member Data Documentation

int gamemap::border_size_
private

The size of the border around the map.

Definition at line 254 of file map.hpp.

Referenced by border_size(), get_terrain(), get_weighted_terrain_frequencies(), on_board_with_border(), operator[](), overlay(), read(), read_header(), set_terrain(), and write().

std::map<map_location, t_translation::t_terrain> gamemap::borderCache_
mutableprivate

Definition at line 240 of file map.hpp.

Referenced by clear_border_cache(), get_terrain(), and remove_from_border_cache().

const int gamemap::default_border = 1
static
int gamemap::h_
protected
tstarting_positions gamemap::starting_positions_
protected

The size of the starting positions array is MAX_PLAYERS + 1, because the positions themselves are numbered from 1.

Definition at line 216 of file map.hpp.

Referenced by is_starting_position(), num_valid_starting_positions(), overlay(), read(), editor::editor_map::resize(), set_special_location(), editor::editor_map::set_starting_position_labels(), special_location(), special_locations(), and write().

tdata_cache gamemap::tdata_
private
std::map<t_translation::t_terrain, size_t> gamemap::terrainFrequencyCache_
mutableprivate

Definition at line 241 of file map.hpp.

Referenced by get_weighted_terrain_frequencies().

t_translation::t_map gamemap::tiles_
protected
int gamemap::total_height_
protected
int gamemap::total_width_
protected
std::vector<map_location> gamemap::villages_
private

Definition at line 238 of file map.hpp.

Referenced by read(), set_terrain(), and villages().

int gamemap::w_
protected

The documentation for this class was generated from the following files: