map.hpp

Go to the documentation of this file.
00001 /* $Id: map.hpp 53711 2012-03-31 08:29:26Z boucman $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /** @file */
00017 
00018 #ifndef MAP_H_INCLUDED
00019 #define MAP_H_INCLUDED
00020 
00021 class config;
00022 
00023 #include "terrain.hpp"
00024 #include "map_location.hpp"
00025 
00026 /**
00027  * Encapsulates the map of the game.
00028  *
00029  * Although the game is hexagonal, the map is stored as a grid.
00030  * Each type of terrain is represented by a multiletter terrain code.
00031  * @todo Update for new map-format.
00032  */
00033 class gamemap
00034 {
00035 public:
00036 
00037     // The name of the terrain is the terrain itself,
00038     // The underlying terrain is the name of the terrain for game-logic purposes.
00039     // I.e. if the terrain is simply an alias, the underlying terrain name
00040     // is the name of the terrain that it's aliased to.
00041     const t_translation::t_list& underlying_mvt_terrain(t_translation::t_terrain terrain) const;
00042     const t_translation::t_list& underlying_def_terrain(t_translation::t_terrain terrain) const;
00043     const t_translation::t_list& underlying_union_terrain(t_translation::t_terrain terrain) const;
00044     /**
00045      * Get a formatted terrain name -- terrain (underlying, terrains)
00046      */
00047     std::string get_terrain_string(const t_translation::t_terrain& terrain) const;
00048     std::string get_terrain_editor_string(const t_translation::t_terrain& terrain) const;
00049     std::string get_underlying_terrain_string(const t_translation::t_terrain& terrain) const;
00050 
00051     const t_translation::t_list& underlying_mvt_terrain(const map_location& loc) const
00052         { return underlying_mvt_terrain(get_terrain(loc)); }
00053     const t_translation::t_list& underlying_def_terrain(const map_location& loc) const
00054         { return underlying_def_terrain(get_terrain(loc)); }
00055     const t_translation::t_list& underlying_union_terrain(const map_location& loc) const
00056         { return underlying_union_terrain(get_terrain(loc)); }
00057     std::string get_terrain_string(const map_location& loc) const
00058         { return get_terrain_string(get_terrain(loc)); }
00059     std::string get_terrain_editor_string(const map_location& loc) const
00060         { return get_terrain_editor_string(get_terrain(loc)); }
00061     bool is_village(t_translation::t_terrain terrain) const
00062         { return get_terrain_info(terrain).is_village(); }
00063     int gives_healing(t_translation::t_terrain terrain) const
00064         { return get_terrain_info(terrain).gives_healing(); }
00065     bool is_castle(t_translation::t_terrain terrain) const
00066         { return get_terrain_info(terrain).is_castle(); }
00067     bool is_keep(t_translation::t_terrain terrain) const
00068         { return get_terrain_info(terrain).is_keep(); }
00069 
00070     bool is_village(const map_location& loc) const
00071         { return on_board(loc) && is_village(get_terrain(loc)); }
00072     int gives_healing(const map_location& loc) const
00073         { return on_board(loc) ?  gives_healing(get_terrain(loc)) : 0; }
00074     bool is_castle(const map_location& loc) const
00075         { return on_board(loc) && is_castle(get_terrain(loc)); }
00076     bool is_keep(const map_location& loc) const
00077         { return on_board(loc) && is_keep(get_terrain(loc)); }
00078 
00079     enum tborder {
00080         NO_BORDER = 0,
00081         SINGLE_TILE_BORDER
00082         };
00083 
00084     enum tusage {
00085         IS_MAP,
00086         IS_MASK
00087         };
00088 
00089     enum tmerge_mode {
00090         BASE,
00091         OVERLAY,
00092         BOTH
00093         };
00094 
00095     /**
00096      * Loads a map, with the given terrain configuration.
00097      *
00098      * Data should be a series of lines, with each character representing one
00099      * hex on the map.  Starting locations are represented by numbers.
00100      *
00101      * @param cfg the game config.
00102      * @param data the map data to load.
00103      */
00104     gamemap(const config &cfg, const std::string &data); //throw(incorrect_map_format_error)
00105 
00106 
00107     /**
00108      * Loads a map, from the [map] wml config in @level.
00109      *
00110      * Data should be a series of lines, with each character representing one
00111      * hex on the map.  Starting locations are represented by numbers
00112      *
00113      * @param cfg the game confg.
00114      * @param level the scenario config to load from.
00115      */
00116     gamemap(const config &cfg, const config &level); //throw(incorrect_map_format_error)
00117 
00118     virtual ~gamemap();
00119 
00120     void read(const std::string& data, const bool allow_invalid = true, const int border_size = 1, const std::string usage = "map");
00121 
00122     virtual void write(config&) const;
00123 
00124     /** Overlays another map onto this one at the given position. */
00125     void overlay(const gamemap& m, const config& rules, int x=0, int y=0, bool border=false);
00126 
00127     /** Effective map width. */
00128     int w() const { return w_; }
00129 
00130     /** Effective map height. */
00131     int h() const { return h_; }
00132 
00133     /** Size of the map border. */
00134     int border_size() const { return border_size_; }
00135 
00136     /** Real width of the map, including borders. */
00137     int total_width()  const { return total_width_; }
00138 
00139     /** Real height of the map, including borders */
00140     int total_height() const { return total_height_; }
00141 
00142     const t_translation::t_terrain operator[](const map_location& loc) const
00143         { return tiles_[loc.x + border_size_][loc.y + border_size_]; }
00144 
00145     /**
00146      * Looks up terrain at a particular location.
00147      *
00148      * Hexes off the map may be looked up, and their 'emulated' terrain will
00149      * also be returned.  This allows proper drawing of the edges of the map.
00150      */
00151     t_translation::t_terrain get_terrain(const map_location& loc) const;
00152 
00153     /** Writes the terrain at loc to cfg. */
00154     void write_terrain(const map_location &loc, config& cfg) const;
00155 
00156 
00157     /** Manipulate starting positions of the different sides. */
00158     const map_location& starting_position(int side) const;
00159     int is_starting_position(const map_location& loc) const;
00160     int num_valid_starting_positions() const;
00161 
00162     void set_starting_position(int side, const map_location& loc);
00163 
00164     /**
00165      * Tell if a location is on the map.
00166      *
00167      * Should be called before indexing using [].
00168      * @todo inline for performance? -- Ilor
00169      */
00170     bool on_board(const map_location& loc) const;
00171     bool on_board_with_border(const map_location& loc) const;
00172 
00173     /** Tell if the map is of 0 size. */
00174     bool empty() const
00175     {
00176         return w_ == 0 || h_ == 0;
00177     }
00178 
00179     /** Return a list of the locations of villages on the map. */
00180     const std::vector<map_location>& villages() const { return villages_; }
00181 
00182     /**
00183      * Get the corresponding terrain_type information object
00184      * for a given type of terrain.
00185      */
00186     const terrain_type& get_terrain_info(const t_translation::t_terrain terrain) const;
00187 
00188     /** Shortcut to get_terrain_info(get_terrain(loc)). */
00189     const terrain_type& get_terrain_info(const map_location &loc) const
00190         { return get_terrain_info(get_terrain(loc)); }
00191 
00192     /** Gets the list of terrains. */
00193     const t_translation::t_list& get_terrain_list() const
00194         { return terrainList_; }
00195 
00196     /**
00197      * Clobbers over the terrain at location 'loc', with the given terrain.
00198      * Uses mode and replace_if_failed like merge_terrains().
00199      */
00200     void set_terrain(const map_location& loc, const t_translation::t_terrain terrain, const tmerge_mode mode=BOTH, bool replace_if_failed = false);
00201 
00202     /**
00203      * Returns a list of the frequencies of different terrain types on the map,
00204      * with terrain nearer the center getting weighted higher.
00205      */
00206     const std::map<t_translation::t_terrain, size_t>& get_weighted_terrain_frequencies() const;
00207 
00208     /**
00209      * Remove the cached border terrain at loc.
00210      *
00211      * Needed by the editor to make tiles at the border update correctly when
00212      * drawing other tiles.
00213      */
00214     void remove_from_border_cache(const map_location &loc)
00215         { borderCache_.erase(loc); }
00216 
00217     /**
00218      * Maximum number of players supported.
00219      *
00220      * Warning: when you increase this, you need to add
00221      * more definitions to the team_colors.cfg file.
00222      */
00223     enum { MAX_PLAYERS = 9 };
00224 
00225     /** Returns the usage of the map. */
00226     tusage get_usage() const { return usage_; }
00227 
00228     /** The default border style for a map. */
00229     static const tborder default_border;
00230 
00231     /**
00232      * Tries to merge old and new terrain using the merge_settings config
00233      * Relevant parameters are "layer" and "replace_conflicting"
00234      * "layer" specifies the layer that should be replaced (base or overlay, default is both).
00235      * If "replace_conflicting" is true the new terrain will replace the old one if merging failed
00236      * (using the default base if new terrain is an overlay terrain)
00237      * Will return the resulting terrain or NONE_TERRAIN if merging failed
00238      */
00239     t_translation::t_terrain merge_terrains(const t_translation::t_terrain old_t, const t_translation::t_terrain new_t, const tmerge_mode mode, bool replace_if_failed = false);
00240 
00241 protected:
00242     t_translation::t_map tiles_;
00243 
00244     /**
00245      * The size of the starting positions array is MAX_PLAYERS + 1,
00246      * because the positions themselves are numbered from 1.
00247      */
00248     map_location startingPositions_[MAX_PLAYERS+1];
00249 
00250     /**
00251      * Clears the border cache, needed for the editor
00252      */
00253     void clear_border_cache() { borderCache_.clear(); }
00254 
00255 private:
00256 
00257     void set_usage(const std::string& usage);
00258 
00259     /**
00260      * Reads the header of a map which is saved in the deprecated map_data format.
00261      *
00262      * @param data                The mapdata to load.
00263      */
00264     int read_header(const std::string& data);
00265 
00266     int num_starting_positions() const
00267         { return sizeof(startingPositions_)/sizeof(*startingPositions_); }
00268 
00269     /** Allows lookup of terrain at a particular location. */
00270     const t_translation::t_list operator[](int index) const
00271         { return tiles_[index + border_size_]; }
00272 
00273     /**
00274      * Tries to find out if "terrain" can be created by combining two existing
00275      * terrains Will add the resulting terrain to the terrain list if
00276      * successful
00277      */
00278     bool try_merge_terrains(const t_translation::t_terrain terrain);
00279 
00280     t_translation::t_list terrainList_;
00281     std::map<t_translation::t_terrain, terrain_type> tcodeToTerrain_;
00282     std::vector<map_location> villages_;
00283 
00284     mutable std::map<map_location, t_translation::t_terrain> borderCache_;
00285     mutable std::map<t_translation::t_terrain, size_t> terrainFrequencyCache_;
00286 
00287 protected:
00288     /** Sizes of the map area. */
00289     int w_;
00290     int h_;
00291 
00292     /** Sizes of the map including the borders. */
00293     int total_width_;
00294     int total_height_;
00295 
00296 private:
00297     /** The size of the border around the map. */
00298     int border_size_;
00299 
00300     /** The kind of map is being loaded. */
00301     tusage usage_;
00302 };
00303 
00304 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:05 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs