30 #define ERR_NG LOG_STREAM(err, log_engine)
31 #define WRN_NG LOG_STREAM(warn, log_engine)
58 bool parity = (me.
x & 1) != 0;
61 if((a.
x > 0) && (a.
x % 2) && parity)
63 if((a.
x < 0) && (a.
x % 2) && !parity)
95 global_image(global_image),
106 last_tod(
"invalid_tod"),
112 images_background.clear();
113 images_foreground.clear();
119 sorted_images =
true;
123 bool is_background = ri->is_background();
126 imagelist& img_list = is_background ? images_background : images_foreground;
130 bool has_flag_match =
true;
134 has_flag_match =
false;
139 if(!has_flag_match) {
144 if(!variant.
tods.empty() && variant.
tods.find(tod) == variant.
tods.end())
149 unsigned int rnd = ri.rand / 7919;
152 bool is_empty =
true;
163 img_list.push_back(anim);
166 img_list.back().set_animation_time(ri.rand % img_list.back().get_animation_duration());
169 img_list.back().pause_animation();
173 log->push_back(std::make_pair(&ri, &variant));
185 sorted_images =
false;
186 images_foreground.clear();
187 images_background.clear();
188 last_tod =
"invalid_tod";
192 unsigned int a = (loc.
x + 92872973) ^ 918273;
193 unsigned int b = (loc.
y + 1672517) ^ 128123;
194 unsigned int c = (index + 127390) ^ 13923787;
195 unsigned int abc = a*b*c + a*b + b*c + a*c + a + b +
c;
209 std::vector<terrain_builder::tile> new_tiles((x + 4) * (y + 4));
210 tiles_.swap(new_tiles);
216 if(loc.
x < -2 || loc.
y < -2 || loc.
x > (
x_ + 1) || loc.
y > (
y_ + 1)) {
228 return tiles_[(loc.
x + 2) + (loc.
y + 2) * (
x_ + 4)];
235 return tiles_[(loc.
x + 2) + (loc.
y + 2) * (
x_ + 4)];
266 for(
int x = -2;
x <=
map().
w(); ++
x) {
267 for(
int y = -2;
y <=
map().
h(); ++
y) {
322 if(!img_list.empty()) {
334 bool changed =
false;
363 if(!filename.empty()) {
371 if(
map().get_terrain_info(loc).is_combined()) {
375 if(!filename_ovl.empty()) {
394 bool precached = name.find(
"..") == std::string::npos;
408 std::vector<std::string>
res;
409 if(variations.empty()){
413 std::string::size_type
pos = base.find(
"@V", 0);
414 if(pos == std::string::npos) {
418 std::vector<std::string> vars =
utils::split(variations,
';', 0);
422 std::string::size_type pos = 0;
423 while ((pos = res.back().find(
"@V", pos)) != std::string::npos) {
424 res.back().replace(pos, 2,
v);
458 const size_t tilde = str.find(
'~');
459 bool has_tilde = tilde != std::string::npos;
460 const std::string filename =
"terrain/" + (has_tilde ? str.substr(0,tilde) : str);
466 const std::string modif = (has_tilde ? str.substr(tilde+1) :
"");
469 if(items.size() > 1) {
470 time = std::stoi(items.back());
484 variant.
images.push_back(res);
486 if(variant.
images.empty())
497 static const struct {
int ii;
int ij;
int ji;
int jj; } rotations[6] =
498 { { 1, 0, 0, 1 }, { 1, 1, -1, 0 }, { 0, 1, -1, -1 },
499 { -1, 0, 0, -1 }, { -1, -1, 1, 0 }, { 0, -1, 1, 1 } };
528 static const struct {
535 { 1./2. , -3./4., 1., 1./2. },
536 { -1./2., -3./4., 1, -1./2.},
537 { -1. , 0., 0., -1. },
538 { -1./2., 3./4., -1., -1./2.},
539 { 1./2. , 3./4., -1., 1./2. },
550 int ri = rotations[
angle].ii * vi + rotations[
angle].ij * vj;
551 int rj = rotations[
angle].ji * vi + rotations[
angle].jj * vj;
554 ret.
loc.
y = ri + (rj >= 0 ? rj/2 : (rj-1)/2);
559 double vx, vy, rx, ry;
564 rx = xyrotations[
angle].xx * vx + xyrotations[
angle].xy * vy;
565 ry = xyrotations[
angle].yx * vx + xyrotations[
angle].yy * vy;
576 const std::vector<std::string> &replacement)
578 std::string::size_type
pos = 0;
579 while ((pos = s.find(
"@R", pos)) != std::string::npos) {
580 if (pos + 2 >= s.size())
return;
581 unsigned i = s[pos + 2] -
'0' +
angle;
583 if (i >= 6) { pos += 2;
continue; }
585 s.replace(pos, 3, r);
591 const std::vector<std::string> &replacement)
599 const std::vector<std::string> &replacement)
607 const std::vector<std::string> &replacement)
628 const std::vector<std::string> &rot)
630 if (rot.size() != 6) {
631 ERR_NG <<
"invalid rotations" << std::endl;
644 minx = std::min<int>(cons.
loc.
x, minx);
645 miny = std::min<int>(2 * cons.
loc.
y + (cons.
loc.
x & 1), miny);
648 if((miny & 1) && (minx & 1) && (minx < 0))
650 if(!(miny & 1) && (minx & 1) && (minx > 0))
661 image_string(image_string),
662 variations(variations),
666 random_start(random_start)
668 if(!has_flag.empty()) {
672 const std::vector<std::string> tod_list =
utils::split(tod);
673 tods.insert(tod_list.begin(), tod_list.end());
686 if(base.size() >= 2) {
687 basex = std::stoi(base[0]);
688 basey = std::stoi(base[1]);
692 int center_x = -1, center_y = -1;
694 std::vector<std::string> center =
utils::split(*center_);
695 if(center.size() >= 2) {
696 center_x = std::stoi(center[0]);
697 center_y = std::stoi(center[1]);
701 bool is_water =
img[
"is_water"].to_bool();
703 images.push_back(
rule_image(layer, basex - dx, basey - dy, global, center_x, center_y, is_water));
712 bool random_start =
variant[
"random_start"].to_bool(
true);
714 images.back().variants.push_back(
rule_image_variant(name, variations, tod, has_flag, random_start));
721 bool random_start =
img[
"random_start"].to_bool(
true);
742 cons = &constraints.back();
766 item_string.begin(), item_string.end());
770 item_string.begin(), item_string.end());
774 item_string.begin(), item_string.end());
778 item_string.begin(), item_string.end());
780 item_string.begin(), item_string.end());
783 constraint.
no_draw = cfg[
"no_draw"].to_bool(
false);
790 const config& global_images)
805 for(
size_t y_off = 0; y_off < map.size(); ++y_off) {
806 for(
size_t x_off = x; x_off < map[y_off].size(); ++x_off) {
813 }
else if (terrain.
overlay != 0 ) {
825 if(lineno % 2 == 1) {
845 if(rotations.empty()) {
850 const std::vector<std::string>& rot =
utils::split(rotations,
',');
869 log_scope(
"terrain_builder::parse_config");
881 map_location(br[
"x"].to_int() - 1, br[
"y"].to_int() - 1);
884 map_location(br[
"mod_x"].to_int(), br[
"mod_y"].to_int());
911 if(anchors.find(pos) == anchors.end()) {
912 WRN_NG <<
"Invalid anchor!" << std::endl;
916 std::pair<anchormap::const_iterator, anchormap::const_iterator>
range =
917 anchors.equal_range(pos);
919 for(; range.first != range.second; ++range.first) {
920 loc = range.first->second;
926 const std::vector<std::string> global_set_flag =
utils::split(br[
"set_flag"]);
927 const std::vector<std::string> global_no_flag =
utils::split(br[
"no_flag"]);
928 const std::vector<std::string> global_has_flag =
utils::split(br[
"has_flag"]);
929 const std::vector<std::string> global_set_no_flag =
utils::split(br[
"set_no_flag"]);
934 global_set_flag.begin(), global_set_flag.end());
936 global_no_flag.begin(), global_no_flag.end());
938 global_has_flag.begin(), global_has_flag.end());
940 global_set_no_flag.begin(), global_set_no_flag.end());
942 global_set_no_flag.begin(), global_set_no_flag.end());
960 std::cerr <<
"Built terrain rules: \n";
962 building_ruleset::const_iterator rule;
964 std::cerr <<
">> New rule: image_background = "
965 <<
"\n>> Location " << rule->second.location_constraints
966 <<
"\n>> Probability " << rule->second.probability
968 for(constraint_set::const_iterator constraint = rule->second.constraints.begin();
969 constraint != rule->second.constraints.end(); ++constraint) {
971 std::cerr <<
">>>> New constraint: location = (" << constraint->second.loc
974 std::vector<std::string>::const_iterator
flag;
976 for(flag = constraint->second.set_flag.begin(); flag != constraint->second.set_flag.end(); ++
flag) {
977 std::cerr <<
">>>>>> Set_flag: " << *flag <<
"\n";
980 for(flag = constraint->second.no_flag.begin(); flag != constraint->second.no_flag.end(); ++
flag) {
981 std::cerr <<
">>>>>> No_flag: " << *flag <<
"\n";
1003 tile_image[
"layer"] = -1000;
1004 tile_image[
"name"] =
image;
1006 item[
"probability"] = 100;
1007 item[
"no_flag"] =
"base";
1008 item[
"set_flag"] =
"base";
1031 if(random > static_cast<unsigned int>(rule.
probability)) {
1056 if (flags.find(s) != flags.end()) {
1062 if (flags.find(s) == flags.end()) {
1092 btile.
flags.insert(flag);
1104 for(std::string::const_iterator it = str.begin(), it_end = str.end(); it != it_end; ++it)
1105 h = ((h << 9) | (h >> (
sizeof(
int) * 8 - 9))) ^ (*it);
1115 for(
const rule_image& ri : constraint.images) {
1132 log_scope(
"terrain_builder::build_terrains");
1135 for(
int x = -2;
x <=
map().
w(); ++
x) {
1136 for(
int y = -2;
y <=
map().
h(); ++
y) {
1149 size_t min_size = INT_MAX;
1157 size_t constraint_size = 0;
1164 const size_t match_size = type_it->second.size();
1165 constraint_size += match_size;
1166 if (constraint_size >= min_size) {
1169 matching_types.push_back(t);
1173 if (constraint_size < min_size) {
1174 min_size = constraint_size;
1175 min_types = matching_types;
1176 min_constraint = &constraint;
1177 if (min_size == 0) {
1186 for(t_translation::t_list::const_iterator
t = min_types.begin();
1187 t != min_types.end(); ++
t) {
1191 for(std::vector<map_location>::const_iterator
itor = locations->begin();
1192 itor != locations->end(); ++
itor) {
void rebuild_terrain(const map_location &loc)
Performs a "quick-rebuild" of the terrain in a given location.
void reload_map()
Updates internals that cache map size.
child_itors child_range(const std::string &key)
tformula< unsigned > x_
The x coordinate of the rectangle.
The in-memory representation of a [terrain_graphics] WML rule.
void precache_file_existence(const std::string &subdir)
precache the existence of files in the subdir (ex: "terrain/")
terrain_by_type_map terrain_by_type_
A map representing all locations whose terrain is of a given type.
tile * get_tile(const map_location &loc)
terrain_constraint & add_constraints(constraint_set &constraints, const map_location &loc, const t_translation::t_match &type, const config &global_images)
Creates a rule constraint object which matches a given list of terrains, and adds it to the list of c...
map_location modulo_constraints
Used to constrain locations to ones with coordinates that are multiples of the "mod_x" and "mod_y" pa...
void reset()
Resets the whole tile map.
The in-memory representation of a [tile] WML rule inside of a [terrain_graphics] WML rule...
void add_rule(building_ruleset &rules, building_rule &rule)
Adds a rule to a ruleset.
rule_image_variant(const std::string &image_string, const std::string &variations, bool random_start=true)
Constructor for the normal defaut case.
tile & operator[](const map_location &loc)
Returns a reference to the tile which is at the position pointed by loc.
bool no_draw
Whether to actually draw the images onto this hex or not.
bool precached_file_exists(const std::string &file)
void add_off_map_rule(const std::string &image)
Adds a builder rule for the _off^_usr tile, this tile only has 1 image.
GLuint GLuint GLsizei GLenum type
const t_terrain NONE_TERRAIN
const std::string & minimap_image_overlay() const
bool load_images(building_rule &rule)
Load images and tests for validity of a rule.
std::vector< terrain_constraint > constraint_set
The list of constraints attached to a terrain_graphics WML rule.
Represent a rule_image applied with a random seed.
imagelist images_background
The list of images which are behind the unit sprites, attached to this tile.
int precedence
Ordering relation between the rules.
static building_ruleset building_rules_
Parsed terrain rules.
std::multimap< int, map_location > anchormap
GLenum GLsizei GLenum GLenum const GLvoid * image
t_translation::t_match terrain_types_match
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
const int tilewidth_
The tile width used when using basex and basey.
static map_location & legacy_sum_assign(map_location &me, const map_location &a)
GLint GLint GLint GLint GLint GLint y
const std::vector< std::string > items
static map_location legacy_difference(const map_location &me, const map_location &a)
static lg::log_domain log_engine("engine")
size_t get_frames_count() const
bool rule_matches(const building_rule &rule, const map_location &loc, const terrain_constraint *type_checked) const
Checks whether a rule matches a given location in the map.
const t_terrain OFF_MAP_USER
void add_rotated_rules(building_ruleset &rules, building_rule &tpl, const std::string &rotations)
Adds a set of rules to a ruleset, from a template rule which spans 6 rotations (or less if some of th...
Variant for storing WML attributes.
void apply_rule(const building_rule &rule, const map_location &loc)
Applies a rule at a given location: applies the result of a matching rule at a given location: attach...
static unsigned int get_noise(const map_location &loc, unsigned int index)
void rebuild_all()
Performs a complete rebuild of the list of terrain graphics attached to a map.
static const unsigned int DUMMY_HASH
void rebuild_cache(const std::string &tod, logs *log=nullptr)
Rebuilds the whole image cache, for a given time-of-day.
void replace_rotate_tokens(std::string &s, int angle, const std::vector< std::string > &replacement)
Replaces, in a given string, rotation tokens with their values.
int probability
The probability of this rule to match, when all conditions are met.
Represents a tile of the game map, with all associated builder-specific parameters: flags...
void change_map(const gamemap *m)
GLdouble GLdouble GLdouble b
std::vector< std::vector< t_terrain > > t_map
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
void update_last_draw_time(double acceleration=0)
const gamemap * map_
A pointer to the gamemap class used in the current level.
std::string write_list(const t_list &list)
Writes a list of terrains to a string, only writes the new format.
GLenum GLenum GLuint GLint GLint layer
This structure can be used for matching terrain strings.
GLubyte GLubyte GLubyte GLubyte w
int w() const
Effective map width.
tilemap tile_map_
The tile_map_ for the current level, which is filled by the build_terrains_ method to contain "tiles"...
static bool image_exists(const std::string &name)
GLboolean GLboolean GLboolean GLboolean a
Encapsulates the map of the game.
std::set< std::string > tods
The Time of Day associated to this variant (if any)
TERRAIN_TYPE
Used as a parameter for the get_terrain_at function.
config & add_child(const std::string &key)
terrain_builder(const config &level, const gamemap *map, const std::string &offmap_image)
Constructor for the terrain_builder class.
std::vector< rule_image_variant > variants
A list of variants for this image.
const T & get_frame(size_t n) const
std::vector< rule_image_rand > images
The list of rule_images and random seeds associated to this tile.
static const ::config * terrain
The terrain used to create the cache.
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
std::vector< log_details > logs
GLsizei const GLint * locations
std::vector< animated< image::locator > > images
An animated image locator built according to the image string.
cl_event GLbitfield flags
bool local
Indicate if the rule is only for this scenario.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Encapsulates the map of the game.
const terrain_type & get_terrain_info(const t_translation::t_terrain &terrain) const
void rotate_rule(building_rule &rule, int angle, const std::vector< std::string > &angle_name)
Rotates a template rule to a given angle.
bool update_animation(const map_location &loc)
Updates the animation at a given tile.
static void progress(const char *stage_name=nullptr)
bool global_image
Set to true if the image was defined as a child of the [terrain_graphics] tag, set to false if it was...
std::map< std::string, tfilter >::iterator itor
std::string last_tod
The time-of-day to which the image caches correspond.
void rotate(terrain_constraint &constraint, int angle)
"Rotates" a constraint from a rule.
int h() const
Effective map height.
static const config * rules_cfg_
Config used to parse global terrain rules.
std::vector< std::string > set_flag
Game configuration data as global variables.
std::set< std::string > flags
The list of flags present in this tile.
unsigned int get_hash() const
rule_image(int layer, int x, int y, bool global_image=false, int center_x=-1, int center_y=-1, bool is_water=false)
std::vector< rule_image > rule_imagelist
A shorthand notation for a vector of rule_images.
#define log_scope(description)
GLfloat GLfloat GLfloat GLfloat h
GLint GLint GLint GLint GLint x
void parse_mapstring(const std::string &mapstring, struct building_rule &br, anchormap &anchors, const config &global_images)
Parses a map string (the map= element of a [terrain_graphics] rule, and adds constraints from this ma...
tformula< unsigned > y_
The y coordinate of the rectangle.
std::vector< std::string > has_flag
GLdouble GLdouble GLdouble r
Definitions for the terrain builder.
int center_x
The position where the center of the image base should be.
void reload(int x, int y)
Rebuilds the map to a new set of dimensions.
Represents terrains which are to be drawn behind unit sprites.
Each terrain_graphics rule is associated a set of images, which are applied on the terrain if the rul...
std::vector< std::string > has_flag
std::string write_terrain_code(const t_terrain &tcode)
Writes a single terrain code to a string.
static map_location legacy_negation(const map_location &me)
These legacy map_location functions moved here from map_location.
std::vector< animated< image::locator > > imagelist
A shorthand typedef for a list of animated image locators, the base data type returned by the get_ter...
GLuint const GLchar * name
void build_terrains()
Calculates the list of terrains, and fills the tile_map_ member, from the gamemap and the building_ru...
t_translation::t_terrain get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
tile()
Constructor for the tile() structure.
std::multiset< building_rule > building_ruleset
A set of building rules.
constraint_set constraints
The set of [tile] constraints of this rule.
static std::map< std::string, std::string > images
static void set_terrain_rules_cfg(const config &cfg)
Set the config where we will parse the global terrain rules.
bool random_start
Indicate if the animation uses a random shift.
void add_images_from_config(rule_imagelist &images, const config &cfg, bool global, int dx=0, int dy=0)
Parses a "config" object, which should contains [image] children, and adds the corresponding parsed r...
bool is_empty_hex(const locator &i_locator)
function to check if an image is empty after hex cut should be only used on terrain image (cache the ...
map_location location_constraints
The location on which this map may match.
void swap(game_board &one, game_board &other)
bool terrain_matches(const t_translation::t_terrain &tcode, const t_translation::t_list &terrains) const
Checks whether a terrain code matches a given list of terrain codes.
this module manages the cache of images.
Standard logging facilities (interface).
static const char * match(MatchState *ms, const char *s, const char *p)
std::string image_string
A string representing either the filename for an image, or a list of images, with an optional timing ...
std::string variations
A semi-solon separated list of string used to replace.
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
void parse_config(const config &cfg, bool local=true)
Parses a configuration object containing [terrain_graphics] rules, and fills the building_rules_ memb...
static unsigned int hash_str(const std::string &str)
static map_location legacy_sum(const map_location &me, const map_location &a)
imagelist images_foreground
The list of images which are in front of the unit sprites, attached to this tile. ...
A config object defines a single node in a WML file, with access to child nodes.
void parse_global_config(const config &cfg)
void clear()
Clears all data in this tile, and resets the cache.
static std::vector< std::string > get_variations(const std::string &base, const std::string &variations)
std::vector< std::string > square_parenthetical_split(std::string const &val, const char separator, std::string const &left, std::string const &right, const int flags)
Similar to parenthetical_split, but also expands embedded square brackets.
const std::string & minimap_image() const
std::vector< std::string > no_flag
GLsizei const GLcharARB ** string
t_map read_builder_map(const std::string &str)
Reads a builder map.
bool on_map(const map_location &loc) const
Tests if a location is on the map.
const gamemap & map() const
std::vector< t_terrain > t_list
void start_animation(int start_time, bool cycles=false)
Starts an animation cycle.
const imagelist * get_terrain_at(const map_location &loc, const std::string &tod, TERRAIN_TYPE const terrain_type)
Returns a vector of strings representing the images to load & blit together to get the built content ...