doxygen, comments

This commit is contained in:
Hans Joachim Gurt 2007-08-18 05:10:04 +00:00
parent 646ee18d2c
commit 061aa9840a
4 changed files with 455 additions and 411 deletions

View file

@ -12,6 +12,9 @@
See the COPYING file for more details.
*/
//! @file builder.cpp
//! Terrain builder.
#include "global.hpp"
#include "array.hpp"
@ -132,8 +135,8 @@ terrain_builder::terrain_builder(const config& cfg, const config& level,
const gamemap& map, const std::string& offmap_image) :
map_(map), tile_map_(map.w(), map.h())
{
// make sure there's nothing left in the cache since it might
// give problems
// Make sure there's nothing left in the cache,
// since it might give problems
image::flush_cache();
parse_config(cfg);
@ -195,7 +198,7 @@ bool terrain_builder::update_animation(const gamemap::location &loc)
return changed;
}
// TODO: rename this function
//! @todo TODO: rename this function
void terrain_builder::rebuild_terrain(const gamemap::location &loc)
{
if (tile_map_.on_map(loc)) {
@ -222,12 +225,12 @@ void terrain_builder::rebuild_all()
bool terrain_builder::rule_valid(const building_rule &rule) const
{
//if the rule has no constraints, it is invalid
// If the rule has no constraints, it is invalid
if(rule.constraints.empty())
return false;
//checks if all the images referenced by the current rule are valid.
//if not, this rule will not match.
// Checks if all the images referenced by the current rule are valid.
// If not, this rule will not match.
rule_imagelist::const_iterator image;
constraint_set::const_iterator constraint;
rule_image_variantlist::const_iterator variant;
@ -331,8 +334,8 @@ terrain_builder::terrain_constraint terrain_builder::rotate(const terrain_builde
// r = [[ 1/2 -3/4 ]
// [ 1 1/2 ]]
//
// And the following array contains I(2), r, r^2, r^3, r^4, r^5 (with
// r^3 == -I(2)), which are the successive rotations.
// And the following array contains I(2), r, r^2, r^3, r^4, r^5
// (with r^3 == -I(2)), which are the successive rotations.
static const struct {
double xx;
double xy;
@ -419,7 +422,7 @@ void terrain_builder::replace_token(terrain_builder::building_rule &rule, const
constraint_set::iterator cons;
for(cons = rule.constraints.begin(); cons != rule.constraints.end(); ++cons) {
//Transforms attributes
// Transforms attributes
std::vector<std::string>::iterator flag;
for(flag = cons->second.set_flag.begin(); flag != cons->second.set_flag.end(); flag++) {
@ -473,7 +476,7 @@ terrain_builder::building_rule terrain_builder::rotate_rule(const terrain_builde
miny -= 2;
for(cons2 = tmp_cons.begin(); cons2 != tmp_cons.end(); ++cons2) {
//Adjusts positions
// Adjusts positions
cons2->second.loc += gamemap::location(-minx, -((miny-1)/2));
ret.constraints[cons2->second.loc] = cons2->second;
}
@ -537,7 +540,7 @@ void terrain_builder::add_constraints(
const t_translation::t_match& type, const config& global_images)
{
if(constraints.find(loc) == constraints.end()) {
//the terrain at the current location did not exist, so create it
// The terrain at the current location did not exist, so create it
constraints[loc] = terrain_constraint(loc);
}
@ -580,9 +583,9 @@ void terrain_builder::parse_mapstring(const std::string &mapstring,
const t_translation::t_map map = t_translation::read_builder_map(mapstring);
// if there is an empty map leave directly
// determine after conversion, since a non empty
// string can return an empty map
// If there is an empty map leave directly.
// Determine after conversion, since a
// non-empty string can return an empty map.
if(map.empty()) {
return;
}
@ -596,8 +599,8 @@ void terrain_builder::parse_mapstring(const std::string &mapstring,
const t_translation::t_letter terrain = map[y_off][x_off];
if(terrain.base == t_translation::TB_DOT) {
// Dots are simple placeholders, which do not
// represent actual terrains.
// Dots are simple placeholders,
// which do not represent actual terrains.
} else if (terrain.overlay != 0 ) {
anchors.insert(std::pair<int, gamemap::location>(terrain.overlay, gamemap::location(x, y)));
} else if (terrain.base == t_translation::TB_STAR) {
@ -647,7 +650,7 @@ void terrain_builder::parse_config(const config &cfg)
{
log_scope("terrain_builder::parse_config");
//Parses the list of building rules (BRs)
// Parses the list of building rules (BRs)
const config::child_list& brs = cfg.get_children("terrain_graphics");
for(config::child_list::const_iterator br = brs.begin(); br != brs.end(); ++br) {
@ -661,7 +664,7 @@ void terrain_builder::parse_config(const config &cfg)
pbr.probability = (**br)["probability"].empty() ? -1 : atoi((**br)["probability"].c_str());
pbr.precedence = (**br)["precedence"].empty() ? 0 : atoi((**br)["precedence"].c_str());
//Mapping anchor indices to anchor locations.
// Mapping anchor indices to anchor locations.
anchormap anchors;
// Parse the map= , if there is one (and fill the anchors list)
@ -671,9 +674,8 @@ void terrain_builder::parse_config(const config &cfg)
config::child_list tcs((*br)->get_children("tile"));
for(config::child_list::const_iterator tc = tcs.begin(); tc != tcs.end(); tc++) {
//Adds the terrain constraint to the current built
//terrain's list of terrain constraints, if it does not
//exist.
// Adds the terrain constraint to the current built terrain's list
// of terrain constraints, if it does not exist.
gamemap::location loc;
if((**tc)["x"].size()) {
loc.x = atoi((**tc)["x"].c_str());
@ -733,7 +735,7 @@ void terrain_builder::parse_config(const config &cfg)
}
// debug output for the terrain rules
// Debug output for the terrain rules
#if 0
std::cerr << "Built terrain rules: \n";
@ -764,7 +766,7 @@ void terrain_builder::parse_config(const config &cfg)
void terrain_builder::add_off_map_rule(const std::string& image)
{
// build a config object
// Build a config object
config cfg;
cfg.add_child("terrain_graphics");
@ -785,7 +787,7 @@ void terrain_builder::add_off_map_rule(const std::string& image)
(*item)["no_flag"] = "base";
(*item)["set_flag"] = "base";
// parse the object
// Parse the object
parse_config(cfg);
}
@ -800,7 +802,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) {
// translated location
// Translated location
const gamemap::location tloc = loc + cons->second.loc;
if(!tile_map_.on_map(tloc)) {
@ -839,14 +841,14 @@ bool terrain_builder::rule_matches(const terrain_builder::building_rule &rule,
std::vector<std::string>::const_iterator itor;
for(itor = cons->second.no_flag.begin(); itor != cons->second.no_flag.end(); ++itor) {
//If a flag listed in "no_flag" is present, the rule does not match
// If a flag listed in "no_flag" is present, the rule does not match
if(btile.flags.find(*itor) != btile.flags.end()) {
return false;
}
}
for(itor = cons->second.has_flag.begin(); itor != cons->second.has_flag.end(); ++itor) {
//If a flag listed in "has_flag" is not present, this rule does not match
// If a flag listed in "has_flag" is not present, this rule does not match
if(btile.flags.find(*itor) == btile.flags.end()) {
return false;
}
@ -868,8 +870,8 @@ void terrain_builder::apply_rule(const terrain_builder::building_rule &rule, con
tile& btile = tile_map_[tloc];
// We want to order the images by layer first and base-y second, so we sort by
// layer*BASE_Y_INTERVAL + BASE_Y_INTERVAL/2 + basey
// We want to order the images by layer first and base-y second,
// so we sort by layer*BASE_Y_INTERVAL + BASE_Y_INTERVAL/2 + basey
// Thus, allowed values for basey are from -50000 to 49999
for(img = constraint->second.images.begin(); img != constraint->second.images.end(); ++img) {
btile.images.insert(std::pair<int, const rule_image*>(
@ -901,8 +903,9 @@ int terrain_builder::get_constraint_adjacents(const building_rule& rule, const g
return res;
}
//returns the "size" of a constraint: that is, the number of map tiles on which
//this constraint may possibly match. INT_MAX means "I don't know / all of them".
// Returns the "size" of a constraint: that is, the number of map tiles
// on which this constraint may possibly match.
// INT_MAX means "I don't know / all of them".
int terrain_builder::get_constraint_size(const building_rule& rule, const terrain_constraint& constraint, bool& border)
{
const t_translation::t_list& types = constraint.terrain_types_match.terrain;
@ -916,8 +919,8 @@ int terrain_builder::get_constraint_size(const building_rule& rule, const terrai
if(std::find(types.begin(), types.end(), t_translation::STAR) != types.end()) {
return INT_MAX;
}
// as soon as the list has 1 wildcard we bail out
// it might be better to try some more testing
// As soon as the list has 1 wildcard we bail out.
// It might be better to try some more testing
// before bailing out.
if(constraint.terrain_types_match.has_wildcard) {
return INT_MAX;
@ -928,8 +931,8 @@ int terrain_builder::get_constraint_size(const building_rule& rule, const terrai
border = false;
//if the current constraint only applies to a non-isolated tile,
//the "border" flag can be set.
// If the current constraint only applies to a non-isolated tile,
// the "border" flag can be set.
for(int i = 0; i < 6; ++i) {
if(rule.constraints.find(adj[i]) != rule.constraints.end()) {
const t_translation::t_list& atypes =
@ -967,7 +970,7 @@ void terrain_builder::build_terrains()
{
log_scope("terrain_builder::build_terrains");
//builds the terrain_by_type_ cache
// Builds the terrain_by_type_ cache
for(int x = -1; x <= map_.w(); ++x) {
for(int y = -1; y <= map_.h(); ++y) {
const gamemap::location loc(x,y);
@ -982,10 +985,10 @@ void terrain_builder::build_terrains()
tile_map_[loc].adjacents[0] = t;
for(i = 0; i < 6; ++i) {
//updates the list of adjacents for this tile
// Updates the list of adjacents for this tile
tile_map_[loc].adjacents[i+1] = map_.get_terrain(adj[i]);
//determines if this tile is a border tile
// Determines if this tile is a border tile
if(map_.get_terrain(adj[i]) != t) {
border = true;
}
@ -1008,7 +1011,7 @@ void terrain_builder::build_terrains()
constraint_set::const_iterator constraint;
//find the constraint that contains the less terrain of all terrain rules.
// Find the constraint that contains the less terrain of all terrain rules.
constraint_set::const_iterator smallest_constraint;
constraint_set::const_iterator constraint_most_adjacents;
int smallest_constraint_size = INT_MAX;
@ -1092,9 +1095,11 @@ void terrain_builder::build_terrains()
}
}
} else {
// some overlays fail but (probably their map size) this is fixed by
// changing the start position from -1 to -2. So it's no real fix but
// a hack, so still need to figure out the best number -- Mordante
//! @todo
// Some overlays fail but (probably their map size) this is
// fixed by changing the start position from -1 to -2.
// So it's no real fix but a hack, so still need
// to figure out the best number -- Mordante
for(int x = -2; x <= map_.w(); ++x) {
for(int y = -2; y <= map_.h(); ++y) {
const gamemap::location loc(x,y);

View file

@ -11,6 +11,10 @@
See the COPYING file for more details.
*/
//! @file builder.hpp
//! Definitions for the terrain builder.
#ifndef BUILDER_H_INCLUDED
#define BUILDER_H_INCLUDED
@ -24,13 +28,13 @@
class config;
/**
* The class terrain_builder is constructed from a config object, and a gamemap
* object. On construction, it parses the configuration and extracts the list
* of [terrain_graphics] rules. Each terrain_graphics rule attachs one or more
* images to a specific terrain pattern.
* The class terrain_builder is constructed from a config object, and a
* gamemap object. On construction, it parses the configuration and extracts
* the list of [terrain_graphics] rules. Each terrain_graphics rule attaches
* one or more images to a specific terrain pattern.
* It then applies the rules loaded from the configuration to the current map,
* and calculates the list of images that must be associated to each hex of the
* map.
* and calculates the list of images that must be associated to each hex of
* the map.
*
* The get_terrain_at method can then be used to obtain the list of images
* necessary to draw the terrain on a given tile.
@ -38,14 +42,14 @@ class config;
class terrain_builder
{
public:
/** Used as a parameter for the get_terrain_at function.
* ADJACENT_BACKGROUND represent terrains which are to be drawn behind
* unit sprites, ADJACENT_FOREGROUND terrains which are to be drawn in
* front of them.
*/
enum ADJACENT_TERRAIN_TYPE { ADJACENT_BACKGROUND, ADJACENT_FOREGROUND };
/** A shorthand typedef for a list of animated image locators, the base
* data type returned by the get_terrain_at method.
//! Used as a parameter for the get_terrain_at function.
enum ADJACENT_TERRAIN_TYPE {
ADJACENT_BACKGROUND, //!< Represents terrains which are to be drawn behind unit sprites
ADJACENT_FOREGROUND //!< Represents terrains which are to be drawn in front of them.
};
/** A shorthand typedef for a list of animated image locators,
* the base data type returned by the get_terrain_at method.
*/
typedef std::vector<animated<image::locator> > imagelist;
@ -55,12 +59,12 @@ public:
* [terrain_graphics] rule reside.
* @param level A level (scenario)-specific configuration file,
* containing scenario-specific [terrain_graphics] rules.
* @param map A properly-initialized gamemap object representing the
* current terrain map.
* @param map A properly-initialized gamemap object representing
* the current terrain map.
* @param offmap_image The filename of the image which will be used as
* off map image (see add_off_map_rule()). This image
* automatically gets the 'terrain/' prefix and '.png'
* suffix
* off map image (see add_off_map_rule()).
* This image automatically gets the 'terrain/' prefix
* and '.png' suffix
*/
terrain_builder(const config& cfg, const config &level,
const gamemap& map, const std::string& offmap_image);
@ -68,11 +72,11 @@ public:
/** Returns a vector of strings representing the images to load & blit
* together to get the built content for this tile.
*
* @param loc The location relative the the terrain map, where we ask
* for the image list
* @param tod The string representing the current time-of day. Will
* be used if some images specify several time-of-day-
* related variants.
* @param loc The location relative the the terrain map,
* where we ask for the image list
* @param tod The string representing the current time-of day.
* Will be used if some images specify several
* time-of-day- related variants.
* @param terrain_type ADJACENT_BACKGROUND or ADJACENT_FOREGROUND,
* depending on wheter we ask for the terrain which is
* before, or after the unit sprite.
@ -83,34 +87,34 @@ public:
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
* changed, and must be redrawn.
/** Updates the animation at a given tile.
* Returns true if something has changed, and must be redrawn.
*
* @param loc the location to update
*
* @return true: this tile must be redrawn.
* @retval true: this tile must be redrawn.
*/
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
* cache for a given location, and replaces it with a signle, default
* image for this terrain.
/** Performs a "quick-rebuild" of the terrain in a given location.
* The "quick-rebuild" is no proper rebuild: it only clears the
* terrain cache for a given location, and replaces it with a single,
* default image for this terrain.
*
* @param loc the location where to rebuild terrains
*/
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
* map.
* attached to a map.
* Should be called when a terrain is changed in the map.
*/
void rebuild_all();
/**
* An image variant. The in-memory representation of the [variant] WML
* tag of the [image] WML tag. When an image only has one variant, the
* [variant] tag may be omitted.
* An image variant. The in-memory representation of the [variant]
* WML tag of the [image] WML tag. When an image only has one variant,
* the [variant] tag may be omitted.
*/
struct rule_image_variant {
/** Shorthand constructor for this structure */
@ -118,37 +122,39 @@ public:
image_string(image_string), tod(tod) {};
/** A string representing either the filename for an image, or
* a list of images, with an optional timing for each image.
* Corresponds to the "name" parameter of the [variant] (or of
* the [image]) WML tag.
* a list of images, with an optional timing for each image.
* Corresponds to the "name" parameter of the [variant] (or of
* the [image]) WML tag.
*
* The timing string is in the following format (expressed in EBNF)
* The timing string is in the following format (expressed in EBNF)
*
* <timing_string> ::= <timed_image> ( "," <timed_image> ) +
*@verbatim
* <timing_string> ::= <timed_image> ( "," <timed_image> ) +
*
* <timed_image> ::= <image_name> [ ":" <timing> ]
* <timed_image> ::= <image_name> [ ":" <timing> ]
*
* Where <image_name> represents the actual filename of an
* image, and <timing> the number of milliseconds this image
* will last in the animation.
* Where <image_name> represents the actual filename of an image,
* and <timing> the number of milliseconds this image will last
* in the animation.
*@endverbatim
*/
std::string image_string;
/** An animated image locator built according to the image
* string. This will be the image locator which will actually
/** An animated image locator built according to the image string.
* This will be the image locator which will actually
* be returned to the user.
*/
animated<image::locator> image;
/**
* The time-of-day to which this variant applies. Set to the
* empty string, this variant applies to all TODs.
* The time-of-day to which this variant applies.
* Set to the empty string, this variant applies to all TODs.
*/
std::string tod;
};
/**
* A map associating a rule_image_variant to a string representing the
* time of day.
* A map associating a rule_image_variant to a string representing
* the time of day.
*/
typedef std::map<std::string, rule_image_variant> rule_image_variantlist;
@ -185,8 +191,8 @@ public:
typedef std::vector<rule_image> rule_imagelist;
/**
* The in-memory representation of a [tile] WML rule inside of a
* [terrain_graphics] WML rule.
* The in-memory representation of a [tile] WML rule
* inside of a [terrain_graphics] WML rule.
*/
struct terrain_constraint
{
@ -228,8 +234,8 @@ public:
void add_image_to_cache(const std::string &tod, ordered_ri_list::const_iterator itor);
/** Rebuilds the whole image cache, for a given time-of-day.
* Must be called when the time-of-day has changed, to select
* the correct images.
* Must be called when the time-of-day has changed,
* to select the correct images.
*
* @param tod The current time-of-day
*/
@ -241,19 +247,19 @@ public:
/** The list of flags present in this tile */
std::set<std::string> flags;
/** The list of images associated to this tile ordered by their
layer first and base-y position second.
/** The list of images associated to this tile, ordered by
* their layer first and base-y position second.
*/
ordered_ri_list images;
/** The list of images which are in front of the unit sprites,
* attached to this tile. This member is considered a cache: it
* is built once, and on-demand.
* attached to this tile. This member is considered a cache:
* it is built once, and on-demand.
*/
imagelist images_foreground;
/** The list of images which are behind the unit sprites,
* attached to this tile. This member is considered a cache: it
* is built once, and on-demand.
* attached to this tile. This member is considered a cache:
* it is built once, and on-demand.
*/
imagelist images_background;
/**
@ -262,8 +268,8 @@ public:
std::string last_tod;
/**
* The 6 adjacent terrains of this tile, and the terrain on
* this tile.
* The 6 adjacent terrains of this tile,
* and the terrain on this tile.
*/
t_translation::t_letter adjacents[7];
@ -286,9 +292,9 @@ private:
constraint_set constraints;
/**
* The location on which this map may match. Set to a valid
* gamemap::location if the "x" and "y" parameters of the
* [terrain_graphics] rule are set.
* The location on which this map may match.
* Set to a valid gamemap::location if the "x" and "y" parameters
* of the [terrain_graphics] rule are set.
*/
gamemap::location location_constraints;
@ -301,9 +307,9 @@ private:
/**
* The precedence of this rule. Used to order rules differently
* that the order in which they appear. Defined if the
* "precedence" parameter of the [terrain_graphics] element is
* set.
* that the order in which they appear.
* Defined if the "precedence" parameter of the
* [terrain_graphics] element is set.
*/
int precedence;
};
@ -325,7 +331,8 @@ private:
*
* @param loc The location of the tile
*
* @return A reference to the tile at this location.
* @return A reference to the tile at this location.
*
*/
tile &operator[](const gamemap::location &loc);
/**
@ -334,11 +341,11 @@ private:
const tile &operator[] (const gamemap::location &loc) const;
/**
* Tests if a location is on the map
* Tests if a location is on the map.
*
* @param loc The location to test
*
* @return true if loc is on the map, false otherwise.
* @return true if loc is on the map, false otherwise.
*/
bool on_map(const gamemap::location &loc) const;
@ -356,8 +363,8 @@ private:
};
/**
* A set of building rules. In-memory representation of the whole set
* of [terrain_graphics] rules.
* A set of building rules. In-memory representation
* of the whole set of [terrain_graphics] rules.
*/
typedef std::multimap<int, building_rule> building_ruleset;
@ -366,135 +373,145 @@ private:
* images are present. This method is used, when building the ruleset,
* to only add rules which are valid to the ruleset.
*
* @param rule The rule to test for validity
* @param rule The rule to test for validity
*
* @return true if the rule is valid, false if it is not.
* @return true if the rule is valid, false if it is not.
*/
bool rule_valid(const building_rule &rule) const;
/**
* Starts the animation on a rule.
*
* @param rule The rule on which ot start animations
* @param rule The rule on which ot start animations
*
* @return true
* @return true
*/
bool start_animation(building_rule &rule);
/**
* "Rotates" a constraint from a rule. Takes a template constraint from
* a template rule, and creates a constraint from this template,
* rotated to the given angle.
* "Rotates" a constraint from a rule.
* Takes a template constraint from a template rule, and creates
* a constraint from this template, rotated to the given angle.
*
* On a constraint, the relative position of each rule, and the "base"
* of each vertical images, are rotated according to the given angle.
* On a constraint, the relative position of each rule, and the "base"
* of each vertical images, are rotated according to the given angle.
*
* Template terrain constraints are defined like normal terrain
* constraints, except that, flags, and image filenames, may contain
* template strings of the form <code>@Rn</code>, n being a number from
* 0 to 5. See the rotate_rule method for more info.
* Template terrain constraints are defined like normal terrain
* constraints, except that, flags, and image filenames,
* may contain template strings of the form
*@verbatim
* <code>@Rn</code>,
*@endverbatim
* n being a number from 0 to 5.
* See the rotate_rule method for more info.
*
* @param constraint A template constraint to rotate
* @param angle An int, from 0 to 5, representing the rotation
* angle.
* @param constraint A template constraint to rotate
* @param angle An int, from 0 to 5, representing the rotation angle.
*/
terrain_constraint rotate(const terrain_constraint &constraint, int angle);
/**
* Replaces, in a given string, a token with its value.
*
* @param s The string in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
* @param s The string in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
*/
void replace_token(std::string &s, const std::string &token,
const std::string& replacement);
/**
* Replaces, in a given rule_image, a token with its value. The actual
* substitution is done in all variants of the given image.
* Replaces, in a given rule_image, a token with its value.
* The actual substitution is done in all variants of the given image.
*
* @param image The rule_image in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
* @param image The rule_image in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
*/
void replace_token(rule_image &image, const std::string &token,
const std::string& replacement);
/**
* Replaces, in a given rule_variant_image, a token with its value. The
* actual substitution is done in the "image_string" parameter of this
* rule_variant_image.
* Replaces, in a given rule_variant_image, a token with its value.
* The actual substitution is done in the "image_string" parameter
* of this rule_variant_image.
*
* @param variant The rule_variant_image in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
* @param variant The rule_variant_image in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
*/
void replace_token(rule_image_variant &variant, const std::string &token,
const std::string& replacement)
{ replace_token(variant.image_string, token, replacement); }
/**
* Replaces, in a given rule_imagelist, a token with its value. The
* actual substitution is done in all rule_images contained in the
* rule_imagelist.
* Replaces, in a given rule_imagelist, a token with its value.
* The actual substitution is done in all rule_images contained
* in the rule_imagelist.
*
* @param image The rule_imagelist in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
* @param & The rule_imagelist in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
*/
void replace_token(rule_imagelist &, const std::string &token,
const std::string& replacement);
/**
* Replaces, in a given building_rule, a token with its value. The
* actual substitution is done in the rule_imagelists contained in all
* constraints of the building_rule, and in the flags (has_flag,
* set_flag and no_flag) contained in all constraints of the
* building_rule.
* Replaces, in a given building_rule, a token with its value.
* The actual substitution is done in the rule_imagelists contained
* in all constraints of the building_rule, and in the flags
* (has_flag, set_flag and no_flag) contained in all constraints
* of the building_rule.
*
* @param image The building_rule in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
* @param s The building_rule in which to do the replacement
* @param token The token to substitute
* @param replacement The replacement string
*/
void replace_token(building_rule &s, const std::string &token,
const std::string& replacement);
/**
* Rotates a template rule to a given angle, and returns the rotated
* rule.
* Rotates a template rule to a given angle, and returns the rotated rule.
*
* Template rules are defined like normal rules, except that:
* * Flags and image filenames may contain template strings of the form
* <code>@Rn</code>, n being a number from 0 to 5.
* * The rule contains the rotations=r0,r1,r2,r3,r4,r5, with r0 to r5
* being strings describing the 6 different positions, typically, n,
* ne, se, s, sw, and nw (buy may be anything else.)
* Template rules are defined like normal rules, except that:
* * Flags and image filenames may contain template strings of the form
*@verbatim
* <code>@Rn</code>, n being a number from 0 to 5.
*@endverbatim
* * The rule contains the rotations=r0,r1,r2,r3,r4,r5, with r0 to r5
* being strings describing the 6 different positions, typically,
* n, ne, se, s, sw, and nw (but maybe anything else.)
*
* A template rule will generate 6 rules, which are similar to the
* template, except that:
* A template rule will generate 6 rules, which are similar
* to the template, except that:
*
* * The map of constraints ( [tile]s ) of this rule will be rotated by
* an angle, of 0 to 5 pi / 6
* * The map of constraints ( [tile]s ) of this rule will be
* rotated by an angle, of 0 to 5 pi / 6
*
* * On the rule which is rotated to 0rad, the template strings @R0,
* @R1, @R2, @R3, @R4, @R5, will be replaced by the corresponding r0,
* r1, r2, r3, r4, r5 variables given in the rotations= element.
* * On the rule which is rotated to 0rad, the template strings
*@verbatim
* @R0, @R1, @R2, @R3, @R4, @R5,
*@endverbatim
* will be replaced by the corresponding r0, r1, r2, r3, r4, r5
* variables given in the rotations= element.
*
* * On the rule which is rotated to pi/3 rad, the template strings
* @R0, @R1, @R2 etc. will be replaced by the corresponding <strong>r1,
* r2, r3, r4, r5, r0</strong> (note the shift in indices).
* * On the rule which is rotated to pi/3 rad, the template strings
*@verbatim
* @R0, @R1, @R2 etc.
*@endverbatim
* will be replaced by the corresponding
* <strong>r1, r2, r3, r4, r5, r0</strong> (note the shift in indices).
*
* * On the rule rotated 2pi/3, those will be replaced by r2, r3, r4,
* r5, r0, r1 and so on.
* * On the rule rotated 2pi/3, those will be replaced by
* r2, r3, r4, r5, r0, r1 and so on.
*
*/
building_rule rotate_rule(const building_rule &rule, int angle, const
std::vector<std::string>& angle_name);
building_rule rotate_rule(const building_rule &rule, int angle,
const std::vector<std::string>& angle_name);
/**
* Parses a "config" object, which should contains [image] children, and
* adds the corresponding parsed rule_images to a rule_imagelist.
* Parses a "config" object, which should contains [image] children,
* and adds the corresponding parsed rule_images to a rule_imagelist.
*
* @param images The rule_imagelist into which to add the parsed images.
* @param cfg The WML configuration object to parse
@ -516,27 +533,26 @@ private:
* Creates a rule constraint object which matches a given list of
* terrains, and adds it to the list of constraints of a rule.
*
* @param constraints The constraint set to which to add the constraint.
* @param loc The location of the constraint
* @param type The list of terrains this constraint will match
* @param constraints The constraint set to which to add the constraint.
* @param loc The location of the constraint
* @param type The list of terrains this constraint will match
* @param global_images A configuration object containing [image] tags
* describing rule-global images.
* describing rule-global images.
*/
void add_constraints(constraint_set& constraints,
const gamemap::location &loc, const t_translation::t_match& type,
const config& global_images);
/**
* Creates a rule constraint object from a config object and adds it to
* the list of constraints of a rule.
* Creates a rule constraint object from a config object and
* adds it to the list of constraints of a rule.
*
* @param constraints The constraint set to which to add the constraint.
* @param loc The location of the constraint
* @param cfg The config object describing this constraint.
* Usually, a [tile] child of a [terrain_graphics]
* rule.
* @param constraints The constraint set to which to add the constraint.
* @param loc The location of the constraint
* @param cfg The config object describing this constraint.
* Usually, a [tile] child of a [terrain_graphics] rule.
* @param global_images A configuration object containing [image] tags
* describing rule-global images.
* describing rule-global images.
*/
void add_constraints(constraint_set& constraints,
const gamemap::location &loc, const config &cfg,
@ -548,12 +564,12 @@ private:
* Parses a map string (the map= element of a [terrain_graphics] rule,
* and adds constraints from this map to a building_rule.
*
* @param mapstring The map vector to parse
* @param br The building rule into which to add the extracted
* constraints
* @param anchors A map where to put "anchors" extracted from the map.
* @param mapstring The map vector to parse
* @param br The building rule into which to add the extracted
* constraints
* @param anchors A map where to put "anchors" extracted from the map.
* @param global_images A config object representing the images defined
* as direct children of the [terrain_graphics] rule.
* as direct children of the [terrain_graphics] rule.
*/
void parse_mapstring(const std::string &mapstring, struct building_rule &br,
anchormap& anchors, const config& global_images);
@ -567,13 +583,14 @@ private:
void add_rule(building_ruleset& rules, building_rule &rule);
/**
* Adds a set of rules to a ruleset, from a template rule which spans 6
* rotations (or less if some of the rotated rules are invalid).
* Adds a set of rules to a ruleset, from a template rule which spans
* 6 rotations (or less if some of the rotated rules are invalid).
*
* @param rules The ruleset into which to add the rules.
* @param tpl The template rule
* @param rotations A coma-separated string containing the 6 values for
* replacing rotation template strings (@Rn)
* @param rules The ruleset into which to add the rules.
* @param tpl The template rule
* @param rotations A comma-separated string containing the
* 6 values for replacing rotation
* template strings @verbatim (@Rn) @endverbatim
*/
void add_rotated_rules(building_ruleset& rules, building_rule& tpl, const std::string &rotations);
@ -582,39 +599,41 @@ private:
* and fills the building_rules_ member of the current class according
* to those.
*
* @param config The configuration object to parse.
* @param cfg The configuration object to parse.
*/
void parse_config(const config &cfg);
/**
* adds a builder rule for the _off^_usr tile, this tile only has 1 image
* Adds a builder rule for the _off^_usr tile, this tile only has 1 image.
*
* @param image The filename of the image
*/
void add_off_map_rule(const std::string& image);
/**
* Checks whether a terrain letter matches a given list of terrain letters
* Checks whether a terrain letter matches a given list of terrain letters.
*
* @param letter The terrain to check
* @param terrains The terrain list agains which to check the terrain.
* May contain the metacharacter '*' STAR meaning
* "all terrains" or the character '!' NOT meaning "all
* terrains except those present in the list."
* @param letter The terrain to check
* @param terrains The terrain list agains which to check the terrain.
* May contain the metacharacters
* - '*' STAR, meaning "all terrains"
* - '!' NOT, meaning "all terrains except those present in the list."
*
* @return returns true if "letter" matches the list or the list is empty, else false.
* @return returns true if "letter" matches the list or the list is empty,
* else false.
*/
bool terrain_matches(t_translation::t_letter letter, const t_translation::t_list& terrains) const
{ return terrains.empty()? true : t_translation::terrain_matches(letter, terrains); }
/**
* Checks whether a terrain letter matches a given list of terrain letters
* Checks whether a terrain letter matches a given list of terrain letters.
*
* @param letter The terrain to check
* @param terrain The terrain match structure which to check the terrain.
* See previous definition for more details.
* @param letter The terrain to check
* @param terrain The terrain match structure which to check the terrain.
* See previous definition for more details.
*
* @return returns true if "letter" matches the list or the list is empty, else false.
* @return returns true if "letter" matches the list or the list is empty,
* else false.
*/
bool terrain_matches(t_translation::t_letter letter, const t_translation::t_match &terrain) const
{ return terrain.is_empty ? true : t_translation::terrain_matches(letter, terrain); }
@ -630,18 +649,18 @@ private:
* the pseudo-random-number generator used for
* probability calculations.
* @param check_loc If this parameter is set to false, the "location"
* of the rule (ie, wheter all constraints's terrain
* of the rule (ie, whether all constraints's terrain
* types do actually match) will be assumed to be
* already checked, only flags and probability will be
* checked.
* already checked, only flags and probability
* will be checked.
*/
bool rule_matches(const building_rule &rule, const gamemap::location &loc,
const int rule_index, const bool check_loc) const;
/**
* Applies a rule at a given location: applies the result of a matching
* rule at a given location: attachs the images corresponding to the
* rule, and sets the flags corresponding to the rule.
* Applies a rule at a given location: applies the result of a
* matching rule at a given location: attachs the images corresponding
* to the rule, and sets the flags corresponding to the rule.
*
* @param rule The rule to apply
* @param loc The location to which to apply the rule.
@ -649,8 +668,9 @@ private:
void apply_rule(const building_rule &rule, const gamemap::location &loc);
/**
* Returns the number of constraints adjacent to a given constraint in
* a rule. For example, of the rule map is something like that
* Returns the number of constraints adjacent to a given constraint
* in a rule.
* For example, if the rule map is something like that
* 1 .
* 2
* . 3
@ -658,30 +678,30 @@ private:
* to the "3", will each have 1 adjacent contraint, whereas the
* constraint corresponding to the "2" will have 2.
*
* @param rule The rule this applies to
* @param loc The location of the constraint to check for adjacent
* constraints
* @param rule The rule this applies to
* @param loc The location of the constraint to check for
* adjacent constraints
*
* @return the number of constraints adjacent to the location loc
* @return the number of constraints adjacent to the 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
* the current map, on which this constraint may possibly match. This
* is used to exclude positions where the rule has no change to match
* from being checked for.
* Returns the "size" of a constraint, that is, the number of tiles,
* in the current map, on which this constraint may possibly match.
* This is used to exclude positions where the rule has
* no change to match from being checked for.
*
* The terrain_by_type_border_ and terrain_by_type_ arrays must be
* initialized before this method is called.
* The terrain_by_type_border_ and terrain_by_type_ arrays
* must be initialized before this method is called.
*
* @param rule The rule to check for
* @param constraint The constraint to check for
* @param border (out) Set to true if this constraint applies to a non-isolated tile
*
* @return The number of tiles in the current map, on which the current
* constraint may match. INT_MAX means "all of them" or "unable
* to determine"
* @return The number of tiles in the current map, on which
* the current constraint may match.
* INT_MAX means "all of them" or "unable to determine".
*/
int get_constraint_size(const building_rule& rule, const terrain_constraint& constraint,
bool& border);
@ -713,8 +733,8 @@ private:
*/
terrain_by_type_map terrain_by_type_;
/**
* A map representing all locations whose terrain is adjacent to a
* terrain of a given type.
* A map representing all locations whose terrain
* is adjacent to a terrain of a given type.
*/
terrain_by_type_map terrain_by_type_border_;

View file

@ -12,6 +12,9 @@
See the COPYING file for more details.
*/
//! @file terrain_translation.cpp
//! Routines for terrain-conversion.
#include "global.hpp"
#include "gettext.hpp"
#include "log.hpp"
@ -29,12 +32,12 @@ namespace t_translation {
/***************************************************************************************/
// forward declaration of internal functions
// the low level convertors, these function are the ones which
// now about the internal format. All other functions are unaware
// of the internal format
// The low level convertors,
// These function are the ones which know about the internal format.
// All other functions are unaware of the internal format.
/**
* Get the mask for a single layer
* Get the mask for a single layer.
*
* @param terrain 1 layer of a terrain, might have a wildcard
*
@ -43,7 +46,7 @@ namespace t_translation {
static t_layer get_layer_mask_(t_layer terrain); //inlined
/**
* Gets a mask for a terrain, this mask is used for wildcard matching
* Gets a mask for a terrain, this mask is used for wildcard matching.
*
* @param terrain the terrain which might have a wildcard
*
@ -52,7 +55,7 @@ namespace t_translation {
static t_letter get_mask_(const t_letter& terrain);
/**
* converts a string to a layer
* Converts a string to a layer.
*
* @param str the terrain string to convert, but needs to be sanitized
* so no spaces and only the terrain to convert
@ -62,12 +65,12 @@ namespace t_translation {
static t_layer string_to_layer_(const std::string& str);
/**
* converts a terrain string to a number
* Converts a terrain string to a number.
* @param str the terrain string with an optional number
* @param start_position returns the start_position, the caller should set it on -1
* and it's only changed it there is a starting position found
* @param filler if the terrain has only 1 layer then the filler will be used
* as the second layer
* as the second layer.
*
* @return the letter found in the string
*/
@ -75,13 +78,13 @@ namespace t_translation {
static t_letter string_to_number_(std::string str, int& start_position, const t_layer filler);
/**
* converts a terrain number to a string
* Converts a terrain number to a string
*
* @param terrain the terrain number to convert
* @param starting_position the starting position, if smaller than 0 it's
* @param start_position the starting position, if smaller than 0 it's
* ignored else it's written
* @param min_size padds the results with spaces if required untill the
* result has a length of min_size
* @param min_size padds the results with spaces if required,
* until the result has a length of min_size.
* @return the converted string, if no starting position
* given it's padded to 4 chars else padded to 7 chars
*/
@ -89,8 +92,8 @@ namespace t_translation {
static std::string number_to_string_(t_letter terrain, const int start_position, const size_t min_size);
/**
* converts a terrain string to a letter for the builder the translation
* rules differ from the normal conversion rules
* Converts a terrain string to a letter for the builder.
* The translation rules differ from the normal conversion rules
*
* @param str the terrain string
*
@ -183,7 +186,7 @@ std::string write_letter(const t_letter& letter)
t_list read_list(const std::string& str, const t_layer filler)
{
// handle an empty string
// Handle an empty string
t_list result;
if(str.empty()) {
@ -193,18 +196,18 @@ t_list read_list(const std::string& str, const t_layer filler)
size_t offset = 0;
while(offset < str.length()) {
// get a terrain chunk
// Get a terrain chunk
const std::string separators = ",";
const size_t pos_separator = str.find_first_of(separators, offset);
const std::string terrain = str.substr(offset, pos_separator - offset);
// process the chunk
// Process the chunk
const t_letter tile = string_to_number_(terrain, filler);
// add the resulting terrain number
// Add the resulting terrain number
result.push_back(tile);
//evaluate the separator
// Evaluate the separator
if(pos_separator == std::string::npos) {
offset = str.length();
} else {
@ -238,47 +241,47 @@ t_map read_game_map(const std::string& str, std::map<int, coordinate>& starting_
size_t offset = 0;
size_t x = 0, y = 0, width = 0;
// skip the leading newlines
// Skip the leading newlines
while(offset < str.length() && utils::isnewline(str[offset])) {
++offset;
}
// did we get an empty map?
// Did we get an empty map?
if((offset + 1) >= str.length()) {
return result;
}
while(offset < str.length()) {
// get a terrain chunk
// Get a terrain chunk
const std::string separators = ",\n\r";
const size_t pos_separator = str.find_first_of(separators, offset);
const std::string terrain = str.substr(offset, pos_separator - offset);
// process the chunk
// Process the chunk
int starting_position = -1;
// the gamemap never has a wildcard
// The gamemap never has a wildcard
const t_letter tile = string_to_number_(terrain, starting_position, NO_LAYER);
// add to the resulting starting position
// Add to the resulting starting position
if(starting_position != -1) {
if(starting_positions.find(starting_position) != starting_positions.end()) {
// redefine existion position
// Redefine existion position
WRN_G << "Starting position " << starting_position << " is redefined.\n";
starting_positions[starting_position].x = x;
starting_positions[starting_position].y = y;
} else {
// add new position
// Add new position
const struct coordinate coord = {x, y};
starting_positions.insert(std::pair<int, coordinate>(starting_position, coord));
}
}
// make space for the new item
// NOTE we increase the vector every loop for every x and y profiling
// with an increase of y with 256 items didn't show an significant speed
// increase. So didn't rework the system to allocate larger vectors at
// once.
// Make space for the new item
// NOTE we increase the vector every loop for every x and y.
// Profiling with an increase of y with 256 items didn't show
// an significant speed increase.
// So didn't rework the system to allocate larger vectors at once.
if(result.size() <= x) {
result.resize(x + 1);
}
@ -286,10 +289,10 @@ t_map read_game_map(const std::string& str, std::map<int, coordinate>& starting_
result[x].resize(y + 1);
}
// add the resulting terrain number
// Add the resulting terrain number
result[x][y] = tile;
//evaluate the separator
// Evaluate the separator
if(utils::isnewline(str[pos_separator]) || pos_separator == std::string::npos) {
// the first line we set the with the other lines we check the width
if(y == 0) {
@ -302,18 +305,18 @@ t_map read_game_map(const std::string& str, std::map<int, coordinate>& starting_
}
}
// prepare next itertration
// Prepare next iteration
++y;
x = 0;
// avoid in infinite loop if the last line ends without an EOL
// Avoid in infinite loop if the last line ends without an EOL
if(pos_separator == std::string::npos) {
offset = str.length();
} else {
offset = pos_separator + 1;
//skip the following newlines
// Skip the following newlines
while(offset < str.length() && utils::isnewline(str[offset])) {
++offset;
}
@ -341,9 +344,10 @@ std::string write_game_map(const t_map& map, std::map<int, coordinate> starting_
for(size_t y = 0; y < map[0].size(); ++y) {
for(size_t x = 0; x < map.size(); ++x) {
// If the current location is a starting position it needs to
// be added to the terrain. After it's found it can't be found
// again so the location is removed from the map.
// If the current location is a starting position,
// it needs to be added to the terrain.
// After it's found it can't be found again,
// so the location is removed from the map.
std::map<int, coordinate>::iterator itor = starting_positions.begin();
int starting_position = -1;
for(; itor != starting_positions.end(); ++itor) {
@ -354,7 +358,7 @@ std::string write_game_map(const t_map& map, std::map<int, coordinate> starting_
}
}
// add the separator
// Add the separator
if(x != 0) {
str << ", ";
}
@ -374,10 +378,11 @@ bool terrain_matches(const t_letter& src, const t_letter& dest)
bool terrain_matches(const t_letter& src, const t_list& dest)
{
//NOTE we impose some code duplication we could have been rewritten
//to get a match structure and then call the version with the match
//structure. IMO that's some extra overhead to this function which is
//not required. Hence the two versions
// NOTE we impose some code duplication.
// It could have been rewritten to get a match structure
// and then call the version with the match structure.
// IMO that's some extra overhead to this function
// which is not required. Hence the two versions
if(dest.empty()) {
return false;
}
@ -392,26 +397,26 @@ bool terrain_matches(const t_letter& src, const t_list& dest)
bool result = true;
t_list::const_iterator itor = dest.begin();
// try to match the terrains if matched jump out of the loop.
// Try to match the terrains if matched jump out of the loop.
for(; itor != dest.end(); ++itor) {
// match wildcard
// Match wildcard
if(*itor == STAR) {
return result;
}
// match inverse symbol
// Match inverse symbol
if(*itor == NOT) {
result = !result;
continue;
}
// full match
// Full match
if(src == *itor) {
return result;
}
// does the destination wildcard match
// Does the destination wildcard match
const t_letter dest_mask = get_mask_(*itor);
const t_letter masked_dest = (*itor & dest_mask);
const bool dest_has_wildcard = has_wildcard(*itor);
@ -447,13 +452,13 @@ bool terrain_matches(const t_letter& src, const t_list& dest)
*/
}
// no match, return the inverse of the result
// No match, return the inverse of the result
return !result;
}
// this routine is use for the terrain building so it's one of the delays
// while loading a map. This routine is optimized a bit at the loss of
// readability.
// This routine is used for the terrain building,
// so it's one of the delays while loading a map.
// This routine is optimized a bit at the loss of readability.
bool terrain_matches(const t_letter& src, const t_match& dest)
{
if(dest.is_empty) {
@ -462,33 +467,34 @@ bool terrain_matches(const t_letter& src, const t_match& dest)
bool result = true;
// try to match the terrains if matched jump out of the loop.
// We loop on the dest.terrain since the iterator is faster than operator[]
// the i holds the value for operator[] since dest.mask and dest.masked_terrain
// need to be in sync, they are less often looked up so no iterator for them.
// Try to match the terrains if matched jump out of the loop.
// We loop on the dest.terrain since the iterator is faster than operator[].
// The i holds the value for operator[].
// Since dest.mask and dest.masked_terrain need to be in sync,
// they are less often looked up, so no iterator for them.
size_t i = 0;
t_list::const_iterator end = dest.terrain.end();
for(t_list::const_iterator terrain_itor = dest.terrain.begin();
terrain_itor != end;
++i, ++terrain_itor) {
// match wildcard
// Match wildcard
if(*terrain_itor == STAR) {
return result;
}
// match inverse symbol
// Match inverse symbol
if(*terrain_itor == NOT) {
result = !result;
continue;
}
// full match
// Full match
if(*terrain_itor == src) {
return result;
}
// does the destination wildcard match
// Does the destination wildcard match
if(dest.has_wildcard &&
(src.base & dest.mask[i].base) == dest.masked_terrain[i].base &&
(src.overlay & dest.mask[i].overlay) == dest.masked_terrain[i].overlay) {
@ -514,7 +520,7 @@ bool terrain_matches(const t_letter& src, const t_match& dest)
*/
}
// no match, return the inverse of the result
// No match, return the inverse of the result
return !result;
}
@ -533,7 +539,7 @@ bool has_wildcard(const t_list& list)
return false;
}
// test all items for a wildcard
// Test all items for a wildcard
t_list::const_iterator itor = list.begin();
for(; itor != list.end(); ++itor) {
if(has_wildcard(*itor)) {
@ -541,7 +547,7 @@ bool has_wildcard(const t_list& list)
}
}
// no wildcard found
// No wildcard found
return false;
}
@ -550,12 +556,12 @@ t_map read_builder_map(const std::string& str)
size_t offset = 0;
t_map result;
// skip the leading newlines
// Skip the leading newlines
while(offset < str.length() && utils::isnewline(str[offset])) {
++offset;
}
// did we get an empty map?
// Did we get an empty map?
if((offset + 1) >= str.length()) {
return result;
}
@ -563,21 +569,21 @@ t_map read_builder_map(const std::string& str)
size_t x = 0, y = 0;
while(offset < str.length()) {
// get a terrain chunk
// Get a terrain chunk
const std::string separators = ",\n\r";
const size_t pos_separator = str.find_first_of(separators, offset);
std::string terrain = "";
// make sure we didn't hit and empty chunk
// Make sure we didn't hit an empty chunk
// which is allowed
if(pos_separator != offset) {
terrain = str.substr(offset, pos_separator - offset);
}
// process the chunk
// Process the chunk
const t_letter tile = string_to_builder_number_(terrain);
// make space for the new item
// Make space for the new item
if(result.size() <= y) {
result.resize(y + 1);
}
@ -585,24 +591,24 @@ t_map read_builder_map(const std::string& str)
result[y].resize(x + 1);
}
// add the resulting terrain number,
// Add the resulting terrain number,
result[y][x] = tile;
//evaluate the separator
// evaluate the separator
if(pos_separator == std::string::npos) {
// probably not required to change the value but be sure
// the case should be handled at least. I'm not sure how
// it defined in the standard but here it's defined at
// max u32 which with +1 gives 0 and make a nice infinite
// loop.
// Probably not required to change the value,
// but be sure the case should be handled at least.
// I'm not sure how it is defined in the standard,
// but here it's defined at max u32 which with +1 gives 0
// and make a nice infinite loop.
offset = str.length();
} else if(utils::isnewline(str[pos_separator])) {
// prepare next itertration
// Prepare next iteration
++y;
x = 0;
offset = pos_separator + 1;
//skip the following newlines
// Skip the following newlines
while(offset < str.length() && utils::isnewline(str[offset])) {
++offset;
}
@ -618,12 +624,12 @@ t_map read_builder_map(const std::string& str)
}
/***************************************************************************************/
//internal
// Internal
inline t_layer get_layer_mask_(t_layer terrain)
{
// test for the star 0x2A in every postion and return the
// appropriate mask
// Test for the star 0x2A in every position
// and return the appropriate mask
/*
* This is what the code intents to do, but in order to gain some more
* speed it's changed to the code below, which does the same but faster.
@ -661,20 +667,21 @@ static t_layer string_to_layer_(const std::string& str)
t_layer result = 0;
//validate the string
// Validate the string
wassert(str.size() <= 4);
//the conversion to int puts the first char in the
//highest part of the number, this will make the
//wildcard matching later on a bit easier.
// The conversion to int puts the first char
// in the highest part of the number.
// This will make the wildcard matching
// later on a bit easier.
for(size_t i = 0; i < 4; ++i) {
const unsigned char c = (i < str.length()) ? str[i] : 0;
// clear the lower area is a nop on i == 0 so
// no need for if statement
// Clearing the lower area is a nop on i == 0
// so no need for if statement
result <<= 8;
// add the result
// Add the result
result += c;
}
@ -690,12 +697,12 @@ static t_letter string_to_number_(std::string str, int& start_position, const t_
{
t_letter result;
// need to store the orginal string for the error handling, this has been
// made to avoid the assertion failure which happens often and is not too
// user friendly
// Need to store the orginal string for the error handling.
// This has been made to avoid the assertion failure
// which happens often and is not too user friendly.
const std::string input(str);
//strip the spaces around us
// Strip the spaces around us
const std::string& whitespace = " \t";
str.erase(0, str.find_first_not_of(whitespace));
str.erase(str.find_last_not_of(whitespace) + 1);
@ -703,7 +710,7 @@ static t_letter string_to_number_(std::string str, int& start_position, const t_
return result;
}
// split if we have 1 space inside
// Split if we have 1 space inside
size_t offset = str.find(' ', 0);
if(offset != std::string::npos) {
try {
@ -716,7 +723,7 @@ static t_letter string_to_number_(std::string str, int& start_position, const t_
offset = str.find('^', 0);
if(offset != std::string::npos) {
// if either string is longer than 4 characters bail out
// If either string is longer than 4 characters bail out
if(offset > 4 || (str.size() - offset) > 5) {
goto terrain_error;
}
@ -724,13 +731,13 @@ static t_letter string_to_number_(std::string str, int& start_position, const t_
const std::string overlay_str(str, offset + 1, str.size());
result = t_letter(base_str, overlay_str);
} else {
// if the string is longer than 4 characters bail out
// If the string is longer than 4 characters bail out
if(str.size() > 4) {
goto terrain_error;
}
result = t_letter(str, filler);
//ugly hack
// Ugly hack
if(filler == WILDCARD && (result.base == NOT.base ||
result.base == STAR.base)) {
@ -741,8 +748,8 @@ static t_letter string_to_number_(std::string str, int& start_position, const t_
return result;
terrain_error:
// when this string is removed also test whether the gettext include
// is still required
// When this string is removed, also test
// whether the gettext include is still required
lg::wml_error << _("Invalid terrain found probably an 1.2 terrain format, "
"terrain = ") << input << '\n';
return VOID_TERRAIN;
@ -752,12 +759,12 @@ static std::string number_to_string_(t_letter terrain, const int start_position)
{
std::string result = "";
//insert the start position
// Insert the start position
if(start_position > 0) {
result = str_cast(start_position) + " ";
}
//insert the terrain letter
// Insert the terrain letter
unsigned char letter[9];
letter[0] = ((terrain.base & 0xFF000000) >> 24);
letter[1] = ((terrain.base & 0x00FF0000) >> 16);
@ -771,7 +778,7 @@ static std::string number_to_string_(t_letter terrain, const int start_position)
letter[7] = ((terrain.overlay & 0x0000FF00) >> 8);
letter[8] = (terrain.overlay & 0x000000FF);
} else {
// if no second layer, the second layer won't be written
// If no second layer, the second layer won't be written,
// so no need to initialize that part of the array
letter[4] = 0;
}
@ -801,30 +808,30 @@ static std::string number_to_string_(t_letter terrain, const int start_position,
static t_letter string_to_builder_number_(std::string str)
{
//strip the spaces around us
// Strip the spaces around us
const std::string& whitespace = " \t";
str.erase(0, str.find_first_not_of(whitespace));
if(! str.empty()) {
str.erase(str.find_last_not_of(whitespace) + 1);
}
// empty string is allowed here so handle it
// Empty string is allowed here, so handle it
if(str.empty()) {
return t_letter();
}
const int number = lexical_cast_default(str, -1);
if(number == -1) {
// at this point we have a single char
// which should be interpreted by the map
// builder, so return this number
// At this point we have a single char
// which should be interpreted by the
// map builder, so return this number
return t_letter(str[0] << 24, 0);
} else {
return t_letter(0, number);
}
}
} //namespace
} // end namespace t_translation
#if 0
// small helper rule to test the matching rules

View file

@ -12,6 +12,9 @@
See the COPYING file for more details.
*/
//! @file terrain_translation.hpp
//!
#ifndef TERRAIN_TRANSLATION_H_INCLUDED
#define TERRAIN_TRANSLATION_H_INCLUDED
@ -66,9 +69,9 @@ namespace t_translation {
typedef std::vector<std::vector<t_letter> > t_map;
/**
* This structure can be used for matching terrain strings
* it optimized for strings that need to be matched often
* and caches the wildcard info required for matching
* This structure can be used for matching terrain strings.
* It optimized for strings that need to be matched often,
* and caches the wildcard info required for matching.
*/
struct t_match{
t_match();
@ -82,31 +85,28 @@ namespace t_translation {
bool is_empty;
};
/**
* Contains an x and y coordinate used for starting positions
* in maps
*/
//! Contains an x and y coordinate used for starting positions in maps.
struct coordinate {
size_t x;
size_t y;
};
//exception thrown if there's an error with the terrain
//Note atm most thrown result in a crash but I like an
//uncatched exception better than an assert
// Exception thrown if there's an error with the terrain.
// Note: atm most thrown result in a crash, but I like
// an uncatched exception better than an assert.
struct error {
error(const std::string& msg) : message(msg) {}
std::string message;
};
//some types of terrain which must be known, and can't just be loaded
//in dynamically because they're special. It's asserted that there will
//be corresponding entries for these types of terrain in the terrain
//configuration file.
// Some types of terrain which must be known, and can't just
// be loaded in dynamically because they're special.
// It's asserted that there will be corresponding entries for
// these types of terrain in the terrain configuration file.
extern const t_letter VOID_TERRAIN;
extern const t_letter FOGGED;
// on the map the user can use this type to make odd shaped maps good
// On the map the user can use this type to make odd shaped maps look good.
extern const t_letter OFF_MAP_USER;
extern const t_letter HUMAN_CASTLE;
@ -130,15 +130,20 @@ namespace t_translation {
extern const t_letter STAR; // *
/**
* Reads a single terrain from a string
* Reads a single terrain from a string.
*
* @param str The string which should contain 1 letter the new format
* of a letter is 2 to 4 characters in the set
* [a-Z][A-Z]/|\_ The underscore is intended for internal
* use. Other letters and characters are not validated but
* users of these letters can get nasty surprices. The *
* is used as wildcard in some cases. This letter can
* be two groups separated by a caret, the first group
* is the base terrain, the second the overlay terrain.
*@verbatim
* [a-Z][A-Z]/|\_
*@endverbatim
* The underscore is intended for internal use.
* Other letters and characters are not validated but
* users of these letters can get nasty surprices.
* The * is used as wildcard in some cases.
* This letter can be two groups separated by a caret,
* the first group is the base terrain,
* the second the overlay terrain.
*
* @param filler if there's no layer this value will be used as the second layer
*
@ -148,7 +153,7 @@ namespace t_translation {
/**
* Writes a single letter to a string.
* The writers only support the new format
* The writers only support the new format.
*
* @param letter The letter to convert to a string
*
@ -160,8 +165,9 @@ namespace t_translation {
/**
* Reads a list of terrain from a string, when reading the
*
* @param str A string with one or more terrain letters (see read_letter)
* @param filler if there's no layer this value will be used as the second layer
* @param filler If there's no layer, this value will be used as the second layer
*
* @returns A vector which contains the letters found in the string
*/
@ -172,8 +178,8 @@ namespace t_translation {
*
* @param list A vector with one or more terrain letters
*
* @returns A string with the terrain numbers, comma separated and
* a space behind the comma's. Not padded.
* @returns A string with the terrain numbers, comma separated
* and a space behind the comma's. Not padded.
*/
std::string write_list(const t_list& list);
@ -212,22 +218,24 @@ namespace t_translation {
t_map read_game_map(const std::string& str, std::map<int, coordinate>& starting_positions);
/**
* Write a gamemap in to a vector string
* Write a gamemap in to a vector string.
*
* @param map A terrain vector, as returned from read_game_map
* @param map A terrain vector, as returned from read_game_map
* @param starting_positions A starting positions map, as returned from read_game_map
*
* @returns A terrain string which can be read with read_game_map.
* For readability the map is padded to groups of 12 chars
* followed by a comma and space
* For readability the map is padded to groups of 12 chars,
* followed by a comma and space.
*/
std::string write_game_map(const t_map& map, std::map<int, coordinate> starting_positions = std::map<int, coordinate>());
/**
* Tests whether a specific terrain matches a list of expressions. The list can
* use wildcard matching with *. It also has an inversion function. When a !
* is found the result of the match is inverted. The matching stops at the
* first match (regardless of the ! found) the data is match from start to end.
* Tests whether a specific terrain matches a list of expressions.
* The list can use wildcard matching with *.
* It also has an inversion function.
* When a ! is found the result of the match is inverted.
* The matching stops at the first match (regardless of the ! found)
* the data is match from start to end.
*
* Example:
* Ww, W* does match and returns true
@ -243,12 +251,12 @@ namespace t_translation {
* A*^ matches Abcd but *not* Abcd^Abcd
* A*^Abcd does not match Abcd but matches Abcd^Abcd
*
* Note: If an expression doesn't specify a second layer (i.e. it contains no
* caret) the second layer will be filled in with a default value (See read_letter
* and read_list).
* Note: If an expression doesn't specify a second layer (i.e. it contains
* no caret) the second layer will be filled in with a default value
* (See read_letter and read_list).
*
* In the terrain building code, the second layer will default to the wildcard, so
* both A* and A*^* will match Abcd^Abcd
* In the terrain building code, the second layer will default to the wildcard,
* so both A* and A*^* will match Abcd^Abcd
*
* @param src the value to match (may not contain wildcards)
* @param dest the list of expressions to match against
@ -258,8 +266,8 @@ namespace t_translation {
bool terrain_matches(const t_letter& src, const t_list& dest);
/**
* Tests whether a specific terrain matches an expression, for matching
* rules see above.
* Tests whether a specific terrain matches an expression,
* for matching rules see above.
*
* @param src the value to match (may not contain wildcards)
* @param dest the expression to match against
@ -274,51 +282,55 @@ namespace t_translation {
* certain overhead. This version uses a cache to cache the masks so if
* a list needs to be matched often this version is preferred.
*
* @param src the value to match (may not contain wildcards)
* @param src the value to match (may not contain wildcards)
* @param dest the cached list of expressions to match against
*
* @returns the result of the match (depending on the !'s)
* @returns the result of the match (depending on the !'s)
*/
bool terrain_matches(const t_letter& src, const t_match& dest);
/**
* Tests wither a terrain contains a wildcard
* Tests whether a terrain contains a wildcard
*
* @param the letter to test for a wildcard
* @param letter the letter to test for a wildcard
*
* @returns true if wildcard found else false
* @returns true if wildcard found,
* else false
*/
bool has_wildcard(const t_letter& letter);
/**
* Tests wither a terrain list contains at least
* Tests whether a terrain list contains at least
* one item with a wildcard
*
* @param the list to test for a wildcard
* @param list the list to test for a wildcard
*
* @returns true if a wildcard found else false
* @returns true if a wildcard found,
* else false
*/
bool has_wildcard(const t_list& list);
// these terrain letters are in the builder format, and
// not usable in other parts of the engine
const t_layer TB_STAR = '*' << 24; //it can be assumed this is the equivalent of STAR
const t_layer TB_DOT = '.' << 24;
// These terrain letters are in the builder format,
// and not usable in other parts of the engine
const t_layer TB_STAR = '*' << 24; // It can be assumed this is the equivalent of STAR
const t_layer TB_DOT = '.' << 24;
/**
* Reads a builder map, a builder map differs much from a normal map hence
* the different functions
* Reads a builder map.
* A builder map differs much from a normal map,
* hence the different functions.
*
* @param str The map data, a terrain letter is either a * or a . or a number as
* anchor. The star or dot are stored in the base part of the terrain
* and the anchor in the overlay part. If more letters are allowed as
* special case they will be stored in the base part. Anchor 0 is no
* anchor
* special case they will be stored in the base part.
* Anchor 0 is no anchor.
*
* @returns A 2D vector with the data found the vector data is stored
* like result[y][x] where x the column number is and y the row number.
*/
t_map read_builder_map(const std::string& str);
}
} // end namespace t_translation
#endif