The "Everything should be a method" assumption has led us astray.

There's a better solution, it will be in my next commit.
This commit is contained in:
Eric S. Raymond 2007-07-01 16:33:03 +00:00
parent 10232db2f7
commit f83e36e20f
10 changed files with 192 additions and 197 deletions

View File

@ -121,7 +121,7 @@ void terrain_builder::tilemap::reset()
it->clear();
}
bool terrain_builder::tilemap::on_map(const basemap::location &loc) const
bool terrain_builder::tilemap::on_map(const gamemap::location &loc) const
{
if(loc.x < -1 || loc.y < -1 || loc.x > x_ || loc.y > y_)
return false;
@ -129,30 +129,30 @@ bool terrain_builder::tilemap::on_map(const basemap::location &loc) const
return true;
}
terrain_builder::tile& terrain_builder::tilemap::operator[](const basemap::location &loc)
terrain_builder::tile& terrain_builder::tilemap::operator[](const gamemap::location &loc)
{
wassert(on_map(loc));
return map_[(loc.x+1) + (loc.y+1)*(x_+2)];
}
const terrain_builder::tile& terrain_builder::tilemap::operator[] (const basemap::location &loc) const
const terrain_builder::tile& terrain_builder::tilemap::operator[] (const gamemap::location &loc) const
{
wassert(on_map(loc));
return map_[(loc.x+1) + (loc.y+1)*(x_+2)];
}
terrain_builder::terrain_builder(const config& cfg, const config& level, const basemap& gmap) :
terrain_builder::terrain_builder(const config& cfg, const config& level, const gamemap& gmap) :
map_(gmap), tile_map_(gmap.x(), gmap.y())
{
parse_config(cfg);
parse_config(level);
build_terrains();
//rebuild_terrain(basemap::location(0,0));
//rebuild_terrain(gamemap::location(0,0));
}
const terrain_builder::imagelist *terrain_builder::get_terrain_at(const basemap::location &loc,
const terrain_builder::imagelist *terrain_builder::get_terrain_at(const gamemap::location &loc,
const std::string &tod, const ADJACENT_TERRAIN_TYPE terrain_type)
{
if(!tile_map_.on_map(loc))
@ -178,7 +178,7 @@ const terrain_builder::imagelist *terrain_builder::get_terrain_at(const basemap:
return NULL;
}
bool terrain_builder::update_animation(const basemap::location &loc)
bool terrain_builder::update_animation(const gamemap::location &loc)
{
if(!tile_map_.on_map(loc))
return false;
@ -205,7 +205,7 @@ bool terrain_builder::update_animation(const basemap::location &loc)
}
// TODO: rename this function
void terrain_builder::rebuild_terrain(const basemap::location &loc)
void terrain_builder::rebuild_terrain(const gamemap::location &loc)
{
if (tile_map_.on_map(loc)) {
tile& btile = tile_map_[loc];
@ -483,7 +483,7 @@ terrain_builder::building_rule terrain_builder::rotate_rule(const terrain_builde
for(cons2 = tmp_cons.begin(); cons2 != tmp_cons.end(); ++cons2) {
//Adjusts positions
cons2->second.loc += basemap::location(-minx, -((miny-1)/2));
cons2->second.loc += gamemap::location(-minx, -((miny-1)/2));
ret.constraints[cons2->second.loc] = cons2->second;
}
@ -542,7 +542,7 @@ void terrain_builder::add_images_from_config(rule_imagelist& images, const confi
void terrain_builder::add_constraints(
terrain_builder::constraint_set& constraints,
const basemap::location& loc,
const gamemap::location& loc,
const t_translation::t_match& type, const config& global_images)
{
if(constraints.find(loc) == constraints.end()) {
@ -560,7 +560,7 @@ void terrain_builder::add_constraints(
}
void terrain_builder::add_constraints(terrain_builder::constraint_set &constraints,
const basemap::location& loc, const config& cfg, const config& global_images)
const gamemap::location& loc, const config& cfg, const config& global_images)
{
add_constraints(constraints, loc, t_translation::t_match(cfg["type"], t_translation::WILDCARD), global_images);
@ -608,9 +608,9 @@ void terrain_builder::parse_mapstring(const std::string &mapstring,
// Dots are simple placeholders, which do not
// represent actual terrains.
} else if (terrain.overlay != 0 ) {
anchors.insert(std::pair<int, basemap::location>(terrain.overlay, basemap::location(x, y)));
anchors.insert(std::pair<int, gamemap::location>(terrain.overlay, gamemap::location(x, y)));
} else if (terrain.base == t_translation::TB_STAR) {
add_constraints(br.constraints, basemap::location(x, y), builder_letter(t_translation::STAR), global_images);
add_constraints(br.constraints, gamemap::location(x, y), builder_letter(t_translation::STAR), global_images);
} else {
ERR_NG << "Invalid terrain (" << t_translation::write_letter(terrain) << ") in builder map\n";
wassert(false);
@ -665,7 +665,7 @@ void terrain_builder::parse_config(const config &cfg)
// add_images_from_config(pbr.images, **br);
if(!((**br)["x"].empty() || (**br)["y"].empty()))
pbr.location_constraints = basemap::location(atoi((**br)["x"].c_str()), atoi((**br)["y"].c_str()));
pbr.location_constraints = gamemap::location(atoi((**br)["x"].c_str()), atoi((**br)["y"].c_str()));
pbr.probability = (**br)["probability"].empty() ? -1 : atoi((**br)["probability"].c_str());
pbr.precedence = (**br)["precedence"].empty() ? 0 : atoi((**br)["precedence"].c_str());
@ -683,7 +683,7 @@ void terrain_builder::parse_config(const config &cfg)
//Adds the terrain constraint to the current built
//terrain's list of terrain constraints, if it does not
//exist.
basemap::location loc;
gamemap::location loc;
if((**tc)["x"].size()) {
loc.x = atoi((**tc)["x"].c_str());
}
@ -772,7 +772,7 @@ void terrain_builder::parse_config(const config &cfg)
}
bool terrain_builder::rule_matches(const terrain_builder::building_rule &rule,
const basemap::location &loc, const int rule_index, const bool check_loc) const
const gamemap::location &loc, const int rule_index, const bool check_loc) const
{
if(rule.location_constraints.valid() && rule.location_constraints != loc) {
return false;
@ -783,7 +783,7 @@ bool terrain_builder::rule_matches(const terrain_builder::building_rule &rule,
cons != rule.constraints.end(); ++cons) {
// translated location
const basemap::location tloc = loc + cons->second.loc;
const gamemap::location tloc = loc + cons->second.loc;
if(!tile_map_.on_map(tloc)) {
return false;
@ -811,7 +811,7 @@ bool terrain_builder::rule_matches(const terrain_builder::building_rule &rule,
for(constraint_set::const_iterator cons = rule.constraints.begin();
cons != rule.constraints.end(); ++cons) {
const basemap::location tloc = loc + cons->second.loc;
const gamemap::location tloc = loc + cons->second.loc;
if(!tile_map_.on_map(tloc)) {
return false;
@ -838,13 +838,13 @@ bool terrain_builder::rule_matches(const terrain_builder::building_rule &rule,
return true;
}
void terrain_builder::apply_rule(const terrain_builder::building_rule &rule, const basemap::location &loc)
void terrain_builder::apply_rule(const terrain_builder::building_rule &rule, const gamemap::location &loc)
{
for(constraint_set::const_iterator constraint = rule.constraints.begin();
constraint != rule.constraints.end(); ++constraint) {
rule_imagelist::const_iterator img;
const basemap::location tloc = loc + constraint->second.loc;
const gamemap::location tloc = loc + constraint->second.loc;
if(!tile_map_.on_map(tloc))
return;
@ -867,11 +867,11 @@ void terrain_builder::apply_rule(const terrain_builder::building_rule &rule, con
}
}
int terrain_builder::get_constraint_adjacents(const building_rule& rule, const basemap::location& loc)
int terrain_builder::get_constraint_adjacents(const building_rule& rule, const gamemap::location& loc)
{
int res = 0;
basemap::location adj[6];
gamemap::location adj[6];
int i;
get_adjacent_tiles(loc, adj);
@ -905,7 +905,7 @@ int terrain_builder::get_constraint_size(const building_rule& rule, const terrai
return INT_MAX;
}
basemap::location adj[6];
gamemap::location adj[6];
get_adjacent_tiles(constraint.loc, adj);
border = false;
@ -952,11 +952,11 @@ void terrain_builder::build_terrains()
//builds the terrain_by_type_ cache
for(int x = -1; x <= map_.x(); ++x) {
for(int y = -1; y <= map_.y(); ++y) {
const basemap::location loc(x,y);
const gamemap::location loc(x,y);
const t_translation::t_letter t = builder_letter(map_.get_terrain(loc));
terrain_by_type_[t].push_back(loc);
basemap::location adj[6];
gamemap::location adj[6];
int i;
bool border = false;
@ -1018,7 +1018,7 @@ void terrain_builder::build_terrains()
util::array<t_translation::t_list, 7> adjacent_types;
if(biggest_constraint_adjacent > 0) {
basemap::location loc[7];
gamemap::location loc[7];
loc[0] = constraint_most_adjacents->second.loc;
get_adjacent_tiles(loc[0], loc+1);
for(int i = 0; i < 7; ++i) {
@ -1033,24 +1033,24 @@ void terrain_builder::build_terrains()
}
if(smallest_constraint_size != INT_MAX) {
const t_translation::t_list& types = smallest_constraint->second.terrain_types_match.terrain;
const basemap::location loc = smallest_constraint->second.loc;
const basemap::location aloc = constraint_most_adjacents->second.loc;
const gamemap::location loc = smallest_constraint->second.loc;
const gamemap::location aloc = constraint_most_adjacents->second.loc;
for(t_translation::t_list::const_iterator c = types.begin();
c != types.end(); ++c) {
const std::vector<basemap::location>* locations;
const std::vector<gamemap::location>* locations;
if(smallest_constraint_border) {
locations = &terrain_by_type_border_[*c];
} else {
locations = &terrain_by_type_[*c];
}
for(std::vector<basemap::location>::const_iterator itor = locations->begin();
for(std::vector<gamemap::location>::const_iterator itor = locations->begin();
itor != locations->end(); ++itor) {
if(biggest_constraint_adjacent > 0) {
const basemap::location pos = (*itor - loc) + aloc;
const gamemap::location pos = (*itor - loc) + aloc;
if(!tile_map_.on_map(pos))
continue;
@ -1079,7 +1079,7 @@ void terrain_builder::build_terrains()
// a hack, so still need to figure out the best number -- Mordante
for(int x = -2; x <= map_.x(); ++x) {
for(int y = -2; y <= map_.y(); ++y) {
const basemap::location loc(x,y);
const gamemap::location loc(x,y);
if(rule_matches(rule->second, loc, rule_index, true))
apply_rule(rule->second, loc);
}

View File

@ -54,10 +54,10 @@ public:
* [terrain_graphics] rule reside.
* @param level A level (scenario)-specific configuration file,
* containing scenario-specific [terrain_graphics] rules.
* @param gmap A properly-initialized basemap object representing the
* @param gmap A properly-initialized gamemap object representing the
* current terrain map.
*/
terrain_builder(const config& cfg, const config &level, const basemap& gmap);
terrain_builder(const config& cfg, const config &level, const gamemap& gmap);
/** Returns a vector of strings representing the images to load & blit
* together to get the built content for this tile.
@ -74,7 +74,7 @@ public:
* @return Returns a pointer list of animated images corresponding
* to the parameters, or NULL if there is none.
*/
const imagelist *get_terrain_at(const basemap::location &loc,
const imagelist *get_terrain_at(const gamemap::location &loc,
const std::string &tod, ADJACENT_TERRAIN_TYPE const terrain_type);
/** Updates the animation at a given tile. returns true if something has
@ -84,7 +84,7 @@ public:
*
* @return true: this tile must be redrawn.
*/
bool update_animation(const basemap::location &loc);
bool update_animation(const gamemap::location &loc);
/** Performs a "quick-rebuild" of the terrain in a given location. The
* "quick-rebuild" is no proper rebuild: it only clears the terrain
@ -93,7 +93,7 @@ public:
*
* @param loc the location where to rebuild terrains
*/
void rebuild_terrain(const basemap::location &loc);
void rebuild_terrain(const gamemap::location &loc);
/** Performs a complete rebuild of the list of terrain graphics
* attached to a map. Should be called when a terrain is changed in the
@ -186,9 +186,9 @@ public:
{
terrain_constraint() : loc() {};
terrain_constraint(basemap::location loc) : loc(loc) {};
terrain_constraint(gamemap::location loc) : loc(loc) {};
basemap::location loc;
gamemap::location loc;
t_translation::t_match terrain_types_match;
std::vector<std::string> set_flag;
std::vector<std::string> no_flag;
@ -267,7 +267,7 @@ private:
/**
* The list of constraints attached to a terrain_graphics WML rule.
*/
typedef std::map<basemap::location, terrain_constraint> constraint_set;
typedef std::map<gamemap::location, terrain_constraint> constraint_set;
/**
* The in-memory representation of a [terrain_graphics] WML rule.
@ -281,10 +281,10 @@ private:
/**
* The location on which this map may match. Set to a valid
* basemap::location if the "x" and "y" parameters of the
* gamemap::location if the "x" and "y" parameters of the
* [terrain_graphics] rule are set.
*/
basemap::location location_constraints;
gamemap::location location_constraints;
/**
* The probability of this rule to match, when all conditions
@ -321,11 +321,11 @@ private:
*
* @return A reference to the tile at this location.
*/
tile &operator[](const basemap::location &loc);
tile &operator[](const gamemap::location &loc);
/**
* a const variant of operator[]
*/
const tile &operator[] (const basemap::location &loc) const;
const tile &operator[] (const gamemap::location &loc) const;
/**
* Tests if a location is on the map
@ -334,7 +334,7 @@ private:
*
* @return true if loc is on the map, false otherwise.
*/
bool on_map(const basemap::location &loc) const;
bool on_map(const gamemap::location &loc) const;
/**
* Resets the whole tile map
@ -517,7 +517,7 @@ private:
* describing rule-global images.
*/
void add_constraints(constraint_set& constraints,
const basemap::location &loc, const t_translation::t_match& type,
const gamemap::location &loc, const t_translation::t_match& type,
const config& global_images);
/**
@ -533,10 +533,10 @@ private:
* describing rule-global images.
*/
void add_constraints(constraint_set& constraints,
const basemap::location &loc, const config &cfg,
const gamemap::location &loc, const config &cfg,
const config& global_images);
typedef std::multimap<int, basemap::location> anchormap;
typedef std::multimap<int, gamemap::location> anchormap;
/**
* Parses a map string (the map= element of a [terrain_graphics] rule,
@ -622,7 +622,7 @@ private:
* already checked, only flags and probability will be
* checked.
*/
bool rule_matches(const building_rule &rule, const basemap::location &loc,
bool rule_matches(const building_rule &rule, const gamemap::location &loc,
const int rule_index, const bool check_loc) const;
/**
@ -633,7 +633,7 @@ private:
* @param rule The rule to apply
* @param loc The location to which to apply the rule.
*/
void apply_rule(const building_rule &rule, const basemap::location &loc);
void apply_rule(const building_rule &rule, const gamemap::location &loc);
/**
* Returns the number of constraints adjacent to a given constraint in
@ -651,7 +651,7 @@ private:
*
* @return the number of constraints adjacent to the location loc
*/
int get_constraint_adjacents(const building_rule& rule, const basemap::location& loc);
int get_constraint_adjacents(const building_rule& rule, const gamemap::location& loc);
/**
* Returns the "size" of a constraint, that is, the number of tiles, in
@ -675,14 +675,14 @@ private:
/**
* Calculates the list of terrains, and fills the tile_map_ member,
* from the basemap and the building_rules_.
* from the gamemap and the building_rules_.
*/
void build_terrains();
/**
* A reference to the basemap class used in the current level.
* A reference to the gamemap class used in the current level.
*/
const basemap& map_;
const gamemap& map_;
/**
* The tile_map_ for the current level, which is filled by the
* build_terrains_ method to contain "tiles" representing images
@ -693,7 +693,7 @@ private:
/**
* Shorthand typedef for a map associating a list of locations to a terrain type.
*/
typedef std::map<t_translation::t_letter, std::vector<basemap::location> > terrain_by_type_map;
typedef std::map<t_translation::t_letter, std::vector<gamemap::location> > terrain_by_type_map;
/**
* A map representing all locations whose terrain is of a given type.

View File

@ -40,7 +40,7 @@ terrain_group::terrain_group(const config& cfg, display& gui):
}
terrain_palette::terrain_palette(display &gui, const size_specs &sizes,
const basemap &map, const config& cfg)
const gamemap &map, const config& cfg)
: gui::widget(gui.video()), size_specs_(sizes), gui_(gui), tstart_(0),
checked_group_btn_(0), map_(map),
top_button_(gui.video(), "", gui::button::TYPE_PRESS, "uparrow-button"),

View File

@ -46,7 +46,7 @@ struct terrain_group
class terrain_palette : public gui::widget {
public:
terrain_palette(display &gui, const size_specs &sizes,
const basemap &map, const config& cfg);
const gamemap &map, const config& cfg);
/// Scroll the terrain palette up one step if possible.
void scroll_up();
@ -136,7 +136,7 @@ private:
gui::button *checked_group_btn_;
t_translation::t_letter selected_fg_terrain_, selected_bg_terrain_;
const basemap &map_;
const gamemap &map_;
gui::button top_button_, bot_button_;
size_t button_x_, top_button_y_, bot_button_y_;
size_t nterrains_, terrain_start_;

View File

@ -29,21 +29,21 @@ map_undo_action::map_undo_action() {
starting_locations_set_ = false;
}
const std::map<basemap::location, t_translation::t_letter>& map_undo_action::undo_terrains() const
const std::map<gamemap::location, t_translation::t_letter>& map_undo_action::undo_terrains() const
{
return old_terrain_;
}
const std::map<basemap::location, t_translation::t_letter>& map_undo_action::redo_terrains() const
const std::map<gamemap::location, t_translation::t_letter>& map_undo_action::redo_terrains() const
{
return new_terrain_;
}
const std::set<basemap::location> map_undo_action::undo_selection() const {
const std::set<gamemap::location> map_undo_action::undo_selection() const {
return old_selection_;
}
const std::set<basemap::location> map_undo_action::redo_selection() const {
const std::set<gamemap::location> map_undo_action::redo_selection() const {
return new_selection_;
}
@ -55,17 +55,17 @@ std::string map_undo_action::new_map_data() const {
return new_map_data_;
}
const std::map<basemap::location, int>& map_undo_action::undo_starting_locations() const {
const std::map<gamemap::location, int>& map_undo_action::undo_starting_locations() const {
return old_starting_locations_;
}
const std::map<basemap::location, int>& map_undo_action::redo_starting_locations() const {
const std::map<gamemap::location, int>& map_undo_action::redo_starting_locations() const {
return new_starting_locations_;
}
void map_undo_action::add_terrain(const t_translation::t_letter& old_tr,
const t_translation::t_letter& new_tr,
const basemap::location& lc)
const gamemap::location& lc)
{
old_terrain_[lc] = old_tr;
new_terrain_[lc] = new_tr;
@ -76,8 +76,8 @@ bool map_undo_action::terrain_set() const {
return terrain_set_;
}
void map_undo_action::set_selection(const std::set<basemap::location> &old_selection,
const std::set<basemap::location> &new_selection) {
void map_undo_action::set_selection(const std::set<gamemap::location> &old_selection,
const std::set<gamemap::location> &new_selection) {
old_selection_ = old_selection;
new_selection_ = new_selection;
selection_set_ = true;
@ -99,8 +99,8 @@ bool map_undo_action::map_data_set() const {
}
void map_undo_action::add_starting_location(const int old_side, const int new_side,
const basemap::location &old_loc,
const basemap::location &new_loc) {
const gamemap::location &old_loc,
const gamemap::location &new_loc) {
old_starting_locations_[old_loc] = old_side;
new_starting_locations_[new_loc] = new_side;
starting_locations_set_ = true;

View File

@ -32,28 +32,28 @@ class map_undo_action {
public:
map_undo_action();
const std::map<basemap::location, t_translation::t_letter>& undo_terrains() const;
const std::map<basemap::location, t_translation::t_letter>& redo_terrains() const;
const std::map<gamemap::location, t_translation::t_letter>& undo_terrains() const;
const std::map<gamemap::location, t_translation::t_letter>& redo_terrains() const;
const std::set<basemap::location> undo_selection() const;
const std::set<basemap::location> redo_selection() const;
const std::set<gamemap::location> undo_selection() const;
const std::set<gamemap::location> redo_selection() const;
std::string new_map_data() const;
std::string old_map_data() const;
const std::map<basemap::location, int>& undo_starting_locations() const;
const std::map<basemap::location, int>& redo_starting_locations() const;
const std::map<gamemap::location, int>& undo_starting_locations() const;
const std::map<gamemap::location, int>& redo_starting_locations() const;
void add_terrain(const t_translation::t_letter& old_tr,
const t_translation::t_letter& new_tr,
const basemap::location& lc);
const gamemap::location& lc);
/// Return true if a terrain change has been saved in this undo
/// action.
bool terrain_set() const;
void set_selection(const std::set<basemap::location> &old_selection,
const std::set<basemap::location> &new_selection);
void set_selection(const std::set<gamemap::location> &old_selection,
const std::set<gamemap::location> &new_selection);
/// Return true if a selection change has been saved in this undo
/// action.
@ -67,25 +67,25 @@ public:
bool map_data_set() const;
void add_starting_location(const int old_side, const int new_side,
const basemap::location &old_loc,
const basemap::location &new_loc);
const gamemap::location &old_loc,
const gamemap::location &new_loc);
/// Return true if starting locations have been saved in this undo
/// action.
bool starting_location_set() const;
private:
std::map<basemap::location, t_translation::t_letter> old_terrain_;
std::map<basemap::location, t_translation::t_letter> new_terrain_;
std::map<gamemap::location, t_translation::t_letter> old_terrain_;
std::map<gamemap::location, t_translation::t_letter> new_terrain_;
bool terrain_set_;
std::set<basemap::location> old_selection_;
std::set<basemap::location> new_selection_;
std::set<gamemap::location> old_selection_;
std::set<gamemap::location> new_selection_;
bool selection_set_;
std::string old_map_data_;
std::string new_map_data_;
bool map_data_set_;
std::map<basemap::location,int> old_starting_locations_;
std::map<basemap::location,int> new_starting_locations_;
std::map<gamemap::location,int> old_starting_locations_;
std::map<gamemap::location,int> new_starting_locations_;
bool starting_locations_set_;
};

View File

@ -34,14 +34,14 @@
#define ERR_CF LOG_STREAM(err, config)
#define LOG_G LOG_STREAM(info, general)
std::ostream &operator<<(std::ostream &s, basemap::location const &l) {
std::ostream &operator<<(std::ostream &s, gamemap::location const &l) {
s << (l.x + 1) << ',' << (l.y + 1);
return s;
}
basemap::location basemap::location::null_location;
gamemap::location gamemap::location::null_location;
const t_translation::t_list& basemap::underlying_mvt_terrain(t_translation::t_letter terrain) const
const t_translation::t_list& gamemap::underlying_mvt_terrain(t_translation::t_letter terrain) const
{
const std::map<t_translation::t_letter,terrain_type>::const_iterator i =
letterToTerrain_.find(terrain);
@ -55,7 +55,7 @@ const t_translation::t_list& basemap::underlying_mvt_terrain(t_translation::t_le
}
}
const t_translation::t_list& basemap::underlying_def_terrain(t_translation::t_letter terrain) const
const t_translation::t_list& gamemap::underlying_def_terrain(t_translation::t_letter terrain) const
{
const std::map<t_translation::t_letter, terrain_type>::const_iterator i =
letterToTerrain_.find(terrain);
@ -69,7 +69,7 @@ const t_translation::t_list& basemap::underlying_def_terrain(t_translation::t_le
}
}
const t_translation::t_list& basemap::underlying_union_terrain(t_translation::t_letter terrain) const
const t_translation::t_list& gamemap::underlying_union_terrain(t_translation::t_letter terrain) const
{
const std::map<t_translation::t_letter,terrain_type>::const_iterator i =
letterToTerrain_.find(terrain);
@ -83,12 +83,12 @@ const t_translation::t_list& basemap::underlying_union_terrain(t_translation::t_
}
}
void basemap::write_terrain(const basemap::location &loc, config& cfg) const
void gamemap::write_terrain(const gamemap::location &loc, config& cfg) const
{
cfg["terrain"] = t_translation::write_letter(get_terrain(loc));
}
basemap::location::DIRECTION basemap::location::parse_direction(const std::string& str)
gamemap::location::DIRECTION gamemap::location::parse_direction(const std::string& str)
{
if(str == "n") {
return NORTH;
@ -107,7 +107,7 @@ basemap::location::DIRECTION basemap::location::parse_direction(const std::strin
}
}
std::string basemap::location::write_direction(basemap::location::DIRECTION dir)
std::string gamemap::location::write_direction(gamemap::location::DIRECTION dir)
{
switch(dir) {
case NORTH:
@ -128,7 +128,7 @@ std::string basemap::location::write_direction(basemap::location::DIRECTION dir)
}
}
void basemap::location::init(const std::string &xstr, const std::string &ystr)
void gamemap::location::init(const std::string &xstr, const std::string &ystr)
{
std::string xs = xstr, ys = ystr;
if (game_events::get_state_of_game())
@ -145,12 +145,17 @@ void basemap::location::init(const std::string &xstr, const std::string &ystr)
y = atoi(ys.c_str()) - 1;
}
basemap::location::location(const config& cfg) : x(-1), y(-1)
gamemap::location::location(const config& cfg) : x(-1), y(-1)
{
init(cfg["x"], cfg["y"]);
}
void basemap::location::write(config& cfg) const
gamemap::location::location(const vconfig& cfg) : x(-1), y(-1)
{
init(cfg["x"], cfg["y"]);
}
void gamemap::location::write(config& cfg) const
{
char buf[50];
snprintf(buf,sizeof(buf),"%d",x+1);
@ -159,7 +164,7 @@ void basemap::location::write(config& cfg) const
cfg["y"] = buf;
}
basemap::location basemap::location::operator-() const
gamemap::location gamemap::location::operator-() const
{
location ret;
ret.x = -x;
@ -168,14 +173,14 @@ basemap::location basemap::location::operator-() const
return ret;
}
basemap::location basemap::location::operator+(const basemap::location& a) const
gamemap::location gamemap::location::operator+(const gamemap::location& a) const
{
basemap::location ret = *this;
gamemap::location ret = *this;
ret += a;
return ret;
}
basemap::location& basemap::location::operator+=(const basemap::location &a)
gamemap::location& gamemap::location::operator+=(const gamemap::location &a)
{
bool parity = (x & 1) != 0;
@ -190,33 +195,33 @@ basemap::location& basemap::location::operator+=(const basemap::location &a)
return *this;
}
basemap::location basemap::location::operator-(const basemap::location &a) const
gamemap::location gamemap::location::operator-(const gamemap::location &a) const
{
return operator+(-a);
}
basemap::location& basemap::location::operator-=(const basemap::location &a)
gamemap::location& gamemap::location::operator-=(const gamemap::location &a)
{
return operator+=(-a);
}
basemap::location basemap::location::get_direction(
basemap::location::DIRECTION dir) const
gamemap::location gamemap::location::get_direction(
gamemap::location::DIRECTION dir) const
{
switch(dir) {
case NORTH: return basemap::location(x,y-1);
case NORTH_EAST: return basemap::location(x+1,y-is_even(x));
case SOUTH_EAST: return basemap::location(x+1,y+is_odd(x));
case SOUTH: return basemap::location(x,y+1);
case SOUTH_WEST: return basemap::location(x-1,y+is_odd(x));
case NORTH_WEST: return basemap::location(x-1,y-is_even(x));
case NORTH: return gamemap::location(x,y-1);
case NORTH_EAST: return gamemap::location(x+1,y-is_even(x));
case SOUTH_EAST: return gamemap::location(x+1,y+is_odd(x));
case SOUTH: return gamemap::location(x,y+1);
case SOUTH_WEST: return gamemap::location(x-1,y+is_odd(x));
case NORTH_WEST: return gamemap::location(x-1,y-is_even(x));
default:
wassert(false);
return basemap::location();
return gamemap::location();
}
}
basemap::location::DIRECTION basemap::location::get_relative_dir(basemap::location loc) const {
gamemap::location::DIRECTION gamemap::location::get_relative_dir(gamemap::location loc) const {
location diff = loc -*this;
if(diff == location(0,0)) return NDIRECTIONS;
if( diff.y < 0 && diff.x >= 0 && abs(diff.x) >= abs(diff.y)) return NORTH_EAST;
@ -233,7 +238,7 @@ basemap::location::DIRECTION basemap::location::get_relative_dir(basemap::locati
}
basemap::location::DIRECTION basemap::location::get_opposite_dir(basemap::location::DIRECTION d) const {
gamemap::location::DIRECTION gamemap::location::get_opposite_dir(gamemap::location::DIRECTION d) const {
switch (d) {
case NORTH:
return SOUTH;
@ -253,7 +258,7 @@ basemap::location::DIRECTION basemap::location::get_opposite_dir(basemap::locati
}
}
basemap::basemap(const config& cfg, const std::string& data) : tiles_(1), x_(-1), y_(-1)
gamemap::gamemap(const config& cfg, const std::string& data) : tiles_(1), x_(-1), y_(-1)
{
LOG_G << "loading map: '" << data << "'\n";
const config::child_list& terrains = cfg.get_children("terrain");
@ -262,7 +267,7 @@ basemap::basemap(const config& cfg, const std::string& data) : tiles_(1), x_(-1)
read(data);
}
void basemap::read(const std::string& data)
void gamemap::read(const std::string& data)
{
tiles_.clear();
villages_.clear();
@ -317,7 +322,7 @@ void basemap::read(const std::string& data)
}
}
std::string basemap::write() const
std::string gamemap::write() const
{
std::map<int, t_translation::coordinate> starting_positions = std::map<int, t_translation::coordinate>();
@ -335,7 +340,7 @@ std::string basemap::write() const
return t_translation::write_game_map(tiles_, starting_positions);
}
void basemap::overlay(const basemap& m, const config& rules_cfg, const int xpos, const int ypos)
void gamemap::overlay(const gamemap& m, const config& rules_cfg, const int xpos, const int ypos)
{
const config::child_list& rules = rules_cfg.get_children("rule");
@ -420,7 +425,7 @@ void basemap::overlay(const basemap& m, const config& rules_cfg, const int xpos,
// loc the location
// w the width of the map in hexes
// h the height of the map in hexes
static t_translation::t_letter get_border_terrain(const basemap::location& loc,
static t_translation::t_letter get_border_terrain(const gamemap::location& loc,
const int w, const int h)
{
if(loc.x == 0) {
@ -441,7 +446,7 @@ static t_translation::t_letter get_border_terrain(const basemap::location& loc,
return t_translation::OFF_MAP;
}
t_translation::t_letter basemap::get_terrain(const basemap::location& loc) const
t_translation::t_letter gamemap::get_terrain(const gamemap::location& loc) const
{
if(on_board(loc)) {
return tiles_[loc.x][loc.y];
@ -495,42 +500,42 @@ t_translation::t_letter basemap::get_terrain(const basemap::location& loc) const
#endif
}
const basemap::location& basemap::starting_position(int n) const
const gamemap::location& gamemap::starting_position(int n) const
{
if(size_t(n) < sizeof(startingPositions_)/sizeof(*startingPositions_)) {
return startingPositions_[n];
} else {
static const basemap::location null_loc;
static const gamemap::location null_loc;
return null_loc;
}
}
int basemap::num_valid_starting_positions() const
int gamemap::num_valid_starting_positions() const
{
const int res = is_starting_position(basemap::location());
const int res = is_starting_position(gamemap::location());
if(res == -1)
return num_starting_positions()-1;
else
return res;
}
int basemap::is_starting_position(const basemap::location& loc) const
int gamemap::is_starting_position(const gamemap::location& loc) const
{
const basemap::location* const beg = startingPositions_+1;
const basemap::location* const end = startingPositions_+num_starting_positions();
const basemap::location* const pos = std::find(beg,end,loc);
const gamemap::location* const beg = startingPositions_+1;
const gamemap::location* const end = startingPositions_+num_starting_positions();
const gamemap::location* const pos = std::find(beg,end,loc);
return pos == end ? -1 : pos - beg;
}
void basemap::set_starting_position(int side, const basemap::location& loc)
void gamemap::set_starting_position(int side, const gamemap::location& loc)
{
if(side >= 0 && side < num_starting_positions()) {
startingPositions_[side] = loc;
}
}
const terrain_type& basemap::get_terrain_info(const t_translation::t_letter terrain) const
const terrain_type& gamemap::get_terrain_info(const t_translation::t_letter terrain) const
{
static const terrain_type default_terrain;
const std::map<t_translation::t_letter,terrain_type>::const_iterator i =
@ -542,7 +547,7 @@ const terrain_type& basemap::get_terrain_info(const t_translation::t_letter terr
return default_terrain;
}
bool basemap::location::matches_range(const std::string& xloc, const std::string &yloc) const
bool gamemap::location::matches_range(const std::string& xloc, const std::string &yloc) const
{
if(std::find(xloc.begin(),xloc.end(),',') != xloc.end()) {
std::vector<std::string> xlocs = utils::split(xloc);
@ -611,20 +616,20 @@ namespace {
} //end anonymous namespace
bool gamemap::terrain_matches_filter(const basemap::location& loc, const vconfig& cfg,
bool gamemap::terrain_matches_filter(const gamemap::location& loc, const vconfig& cfg,
const gamestatus& game_status, const unit_map& units, const bool flat_tod,
const size_t max_loop) const
{
//handle radius
const size_t radius = minimum<size_t>(max_loop,
lexical_cast_default<size_t>(cfg["radius"], 0));
std::set<basemap::location> hexes;
std::vector<basemap::location> loc_vec(1, loc);
std::set<gamemap::location> hexes;
std::vector<gamemap::location> loc_vec(1, loc);
get_tiles_radius(*this, loc_vec, radius, hexes);
size_t loop_count = 0;
bool matches = false;
std::set<basemap::location>::const_iterator i;
std::set<gamemap::location>::const_iterator i;
terrain_cache_manager tcm;
for(i = hexes.begin(); i != hexes.end() && loop_count <= max_loop && !matches; ++i) {
matches = terrain_matches_internal(*i, cfg, game_status, units, flat_tod, false, tcm.ptr);
@ -671,7 +676,7 @@ bool gamemap::terrain_matches_filter(const basemap::location& loc, const vconfig
return matches;
}
bool gamemap::terrain_matches_internal(const basemap::location& loc, const vconfig& cfg,
bool gamemap::terrain_matches_internal(const gamemap::location& loc, const vconfig& cfg,
const gamestatus& game_status, const unit_map& units, const bool flat_tod,
const bool ignore_xy, t_translation::t_match*& parsed_terrain) const
{
@ -765,7 +770,7 @@ bool gamemap::terrain_matches_internal(const basemap::location& loc, const vconf
return true;
}
void basemap::set_terrain(const basemap::location& loc, const t_translation::t_letter terrain)
void gamemap::set_terrain(const gamemap::location& loc, const t_translation::t_letter terrain)
{
if(!on_board(loc))
return;
@ -788,10 +793,10 @@ void basemap::set_terrain(const basemap::location& loc, const t_translation::t_l
remove_from_border_cache(adj[n]);
}
std::vector<basemap::location> parse_location_range(const std::string& x, const std::string& y,
const basemap *const map)
std::vector<gamemap::location> parse_location_range(const std::string& x, const std::string& y,
const gamemap *const map)
{
std::vector<basemap::location> res;
std::vector<gamemap::location> res;
const std::vector<std::string> xvals = utils::split(x);
const std::vector<std::string> yvals = utils::split(y);
@ -820,14 +825,14 @@ std::vector<basemap::location> parse_location_range(const std::string& x, const
for(int x = xrange.first; x <= xrange.second; ++x) {
for(int y = yrange.first; y <= yrange.second; ++y) {
res.push_back(basemap::location(x-1,y-1));
res.push_back(gamemap::location(x-1,y-1));
}
}
}
return res;
}
const std::map<t_translation::t_letter, size_t>& basemap::get_weighted_terrain_frequencies() const
const std::map<t_translation::t_letter, size_t>& gamemap::get_weighted_terrain_frequencies() const
{
if(terrainFrequencyCache_.empty() == false) {
return terrainFrequencyCache_;
@ -850,11 +855,11 @@ const std::map<t_translation::t_letter, size_t>& basemap::get_weighted_terrain_f
return terrainFrequencyCache_;
}
void gamemap::get_locations(std::set<basemap::location>& locs, const vconfig& filter,
void gamemap::get_locations(std::set<gamemap::location>& locs, const vconfig& filter,
const gamestatus& game_status, const unit_map& units, const bool flat_tod,
const size_t max_loop) const
{
std::vector<basemap::location> xy_locs = parse_location_range(filter["x"],filter["y"],this);
std::vector<gamemap::location> xy_locs = parse_location_range(filter["x"],filter["y"],this);
if(xy_locs.empty()) {
//consider all locations on the map
for(int x=0; x < x_; x++) {
@ -866,7 +871,7 @@ void gamemap::get_locations(std::set<basemap::location>& locs, const vconfig& fi
//handle location filter
terrain_cache_manager tcm;
std::vector<basemap::location>::iterator loc_itor = xy_locs.begin();
std::vector<gamemap::location>::iterator loc_itor = xy_locs.begin();
while(loc_itor != xy_locs.end()) {
if(terrain_matches_internal(*loc_itor, filter, game_status, units, flat_tod, true, tcm.ptr)) {
++loc_itor;
@ -896,9 +901,9 @@ void gamemap::get_locations(std::set<basemap::location>& locs, const vconfig& fi
//handle [and]
if(cond_name == "and") {
std::set<basemap::location> intersect_hexes;
std::set<gamemap::location> intersect_hexes;
get_locations(intersect_hexes, cond_filter, game_status, units, flat_tod, max_loop);
std::set<basemap::location>::iterator intersect_itor = locs.begin();
std::set<gamemap::location>::iterator intersect_itor = locs.begin();
while(intersect_itor != locs.end()) {
if(intersect_hexes.find(*intersect_itor) == locs.end()) {
locs.erase(*intersect_itor++);
@ -909,10 +914,10 @@ void gamemap::get_locations(std::set<basemap::location>& locs, const vconfig& fi
}
//handle [or]
else if(cond_name == "or") {
std::set<basemap::location> union_hexes;
std::set<gamemap::location> union_hexes;
get_locations(union_hexes, cond_filter, game_status, units, flat_tod, max_loop);
//locs.insert(union_hexes.begin(), union_hexes.end()); //doesn't compile on MSVC
std::set<basemap::location>::iterator insert_itor = union_hexes.begin();
std::set<gamemap::location>::iterator insert_itor = union_hexes.begin();
while(insert_itor != union_hexes.end()) {
locs.insert(*insert_itor++);
}
@ -920,9 +925,9 @@ void gamemap::get_locations(std::set<basemap::location>& locs, const vconfig& fi
}
//handle [not]
else if(cond_name == "not") {
std::set<basemap::location> removal_hexes;
std::set<gamemap::location> removal_hexes;
get_locations(removal_hexes, cond_filter, game_status, units, flat_tod, max_loop);
std::set<basemap::location>::iterator erase_itor = removal_hexes.begin();
std::set<gamemap::location>::iterator erase_itor = removal_hexes.begin();
while(erase_itor != removal_hexes.end()) {
locs.erase(*erase_itor++);
}
@ -933,7 +938,7 @@ void gamemap::get_locations(std::set<basemap::location>& locs, const vconfig& fi
//restrict the potential number of locations to be returned
if(locs.size() > max_loop + 1) {
std::set<basemap::location>::iterator erase_itor = locs.begin();
std::set<gamemap::location>::iterator erase_itor = locs.begin();
for(unsigned i=0; i < max_loop + 1; ++i) {
++erase_itor;
}

View File

@ -15,6 +15,10 @@
#define MAP_H_INCLUDED
class config;
class gamestatus;
class unit;
class vconfig;
class unit_map;
#include "terrain.hpp"
@ -28,7 +32,7 @@ class config;
//class which 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 letter.
class basemap
class gamemap
{
public:
@ -58,7 +62,7 @@ public:
location() : x(-1), y(-1) {}
location(int x, int y) : x(x), y(y) {}
explicit location(const config& cfg);
//explicit location(const vconfig& cfg);
explicit location(const vconfig& cfg);
void write(config& cfg) const;
@ -121,13 +125,13 @@ public:
//data should be a series of lines, with each character representing
//one hex on the map. Starting locations are represented by numbers,
//and will be of type keep.
basemap(const config& terrain_cfg, const std::string& data); //throw(incorrect_format_exception)
gamemap(const config& terrain_cfg, const std::string& data); //throw(incorrect_format_exception)
void read(const std::string& data);
std::string write() const;
//overlays another map onto this one at the given position.
void overlay(const basemap& m, const config& rules, int x=0, int y=0);
void overlay(const gamemap& m, const config& rules, int x=0, int y=0);
//dimensions of the map.
int x() const { return x_; }
@ -143,7 +147,7 @@ public:
t_translation::t_letter get_terrain(const location& loc) const;
//writes the terrain at loc to cfg
void write_terrain(const basemap::location &loc, config& cfg) const;
void write_terrain(const gamemap::location &loc, config& cfg) const;
//functions to manipulate starting positions of the different sides.
@ -177,6 +181,16 @@ public:
const terrain_type& get_terrain_info(const location &loc) const
{ return get_terrain_info(get_terrain(loc)); }
//the terrain filter, also known as "standard location filter" or SLF
bool terrain_matches_filter(const location& loc, const vconfig& cfg,
const gamestatus& game_status, const unit_map& units,
const bool flat_tod=false, const size_t max_loop=MAX_MAP_AREA) const;
//gets all locations that match a given terrain filter
void get_locations(std::set<location>& locs, const vconfig& filter,
const gamestatus& game_status, const unit_map& units,
const bool flat_tod=false, const size_t max_loop=MAX_MAP_AREA) const;
//gets the list of terrains
const t_translation::t_list& get_terrain_list() const
{ return terrainList_; }
@ -202,47 +216,23 @@ protected:
t_translation::t_map tiles_;
location startingPositions_[STARTING_POSITIONS];
private:
bool terrain_matches_internal(const location& loc, const vconfig& cfg,
const gamestatus& game_status, const unit_map& units,
const bool flat_tod, const bool ignore_xy,
t_translation::t_match*& parsed_terrain) const;
int num_starting_positions() const
{ return sizeof(startingPositions_)/sizeof(*startingPositions_); }
t_translation::t_list terrainList_;
std::map<t_translation::t_letter, terrain_type> letterToTerrain_;
std::vector<location> villages_;
mutable std::map<location, t_translation::t_letter> borderCache_;
mutable std::map<t_translation::t_letter, size_t> terrainFrequencyCache_;
int x_;
int y_;
private:
int num_starting_positions() const
{ return sizeof(startingPositions_)/sizeof(*startingPositions_); }
};
class gamestatus;
class unit;
class vconfig;
class unit_map;
class gamemap : public basemap
{
public:
gamemap(const config& terrain_cfg, const std::string& data)
: basemap(terrain_cfg, data)
{};
//the terrain filter, also known as "standard location filter" or SLF
bool terrain_matches_filter(const location& loc, const vconfig& cfg,
const gamestatus& game_status, const unit_map& units,
const bool flat_tod=false, const size_t max_loop=MAX_MAP_AREA) const;
//gets all locations that match a given terrain filter
void get_locations(std::set<location>& locs, const vconfig& filter,
const gamestatus& game_status, const unit_map& units,
const bool flat_tod=false, const size_t max_loop=MAX_MAP_AREA) const;
private:
bool terrain_matches_internal(const location& loc, const vconfig& cfg,
const gamestatus& game_status, const unit_map& units,
const bool flat_tod, const bool ignore_xy,
t_translation::t_match*& parsed_terrain) const;
int x_;
int y_;
};
class viewpoint
@ -255,11 +245,11 @@ public:
//a utility function which parses ranges of locations
//into a vector of locations
std::vector<basemap::location> parse_location_range(const std::string& xvals,
std::vector<gamemap::location> parse_location_range(const std::string& xvals,
const std::string& yvals,
const basemap *const map=NULL);
const gamemap *const map=NULL);
//dump a position on a stream for debug purposes
std::ostream &operator<<(std::ostream &s, basemap::location const &l);
std::ostream &operator<<(std::ostream &s, gamemap::location const &l);
#endif

View File

@ -23,7 +23,7 @@
namespace image {
surface getMinimap(int w, int h, const basemap& map, const viewpoint* vw)
surface getMinimap(int w, int h, const gamemap& map, const viewpoint* vw)
{
const int scale = 8;
@ -53,7 +53,7 @@ surface getMinimap(int w, int h, const basemap& map, const viewpoint* vw)
surface surf(NULL);
const basemap::location loc(x,y);
const gamemap::location loc(x,y);
if(map.on_board(loc)) {
const bool shrouded = vw != NULL && vw->shrouded(x,y);
const bool fogged = vw != NULL && vw->fogged(x,y) && !shrouded;

View File

@ -22,7 +22,7 @@ class team;
namespace image {
///function to create the minimap for a given map
///the surface returned must be freed by the user
surface getMinimap(int w, int h, const basemap& map_, const viewpoint* vm=NULL);
surface getMinimap(int w, int h, const gamemap& map_, const viewpoint* vm=NULL);
}
#endif