Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00028
00029
00030
00031
00032
00033 class gamemap
00034 {
00035 public:
00036
00037
00038
00039
00040
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
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
00097
00098
00099
00100
00101
00102
00103
00104 gamemap(const config &cfg, const std::string &data);
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 gamemap(const config &cfg, const config &level);
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
00125 void overlay(const gamemap& m, const config& rules, int x=0, int y=0, bool border=false);
00126
00127
00128 int w() const { return w_; }
00129
00130
00131 int h() const { return h_; }
00132
00133
00134 int border_size() const { return border_size_; }
00135
00136
00137 int total_width() const { return total_width_; }
00138
00139
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
00147
00148
00149
00150
00151 t_translation::t_terrain get_terrain(const map_location& loc) const;
00152
00153
00154 void write_terrain(const map_location &loc, config& cfg) const;
00155
00156
00157
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
00166
00167
00168
00169
00170 bool on_board(const map_location& loc) const;
00171 bool on_board_with_border(const map_location& loc) const;
00172
00173
00174 bool empty() const
00175 {
00176 return w_ == 0 || h_ == 0;
00177 }
00178
00179
00180 const std::vector<map_location>& villages() const { return villages_; }
00181
00182
00183
00184
00185
00186 const terrain_type& get_terrain_info(const t_translation::t_terrain terrain) const;
00187
00188
00189 const terrain_type& get_terrain_info(const map_location &loc) const
00190 { return get_terrain_info(get_terrain(loc)); }
00191
00192
00193 const t_translation::t_list& get_terrain_list() const
00194 { return terrainList_; }
00195
00196
00197
00198
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
00204
00205
00206 const std::map<t_translation::t_terrain, size_t>& get_weighted_terrain_frequencies() const;
00207
00208
00209
00210
00211
00212
00213
00214 void remove_from_border_cache(const map_location &loc)
00215 { borderCache_.erase(loc); }
00216
00217
00218
00219
00220
00221
00222
00223 enum { MAX_PLAYERS = 9 };
00224
00225
00226 tusage get_usage() const { return usage_; }
00227
00228
00229 static const tborder default_border;
00230
00231
00232
00233
00234
00235
00236
00237
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
00246
00247
00248 map_location startingPositions_[MAX_PLAYERS+1];
00249
00250
00251
00252
00253 void clear_border_cache() { borderCache_.clear(); }
00254
00255 private:
00256
00257 void set_usage(const std::string& usage);
00258
00259
00260
00261
00262
00263
00264 int read_header(const std::string& data);
00265
00266 int num_starting_positions() const
00267 { return sizeof(startingPositions_)/sizeof(*startingPositions_); }
00268
00269
00270 const t_translation::t_list operator[](int index) const
00271 { return tiles_[index + border_size_]; }
00272
00273
00274
00275
00276
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
00289 int w_;
00290 int h_;
00291
00292
00293 int total_width_;
00294 int total_height_;
00295
00296 private:
00297
00298 int border_size_;
00299
00300
00301 tusage usage_;
00302 };
00303
00304 #endif