The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
builder.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Philippe Plantier <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Definitions for the terrain builder.
18  */
19 
20 #ifndef TERRAIN_BUILDER_H_INCLUDED
21 #define TERRAIN_BUILDER_H_INCLUDED
22 
23 #include "animated.hpp"
24 #include "map/location.hpp"
25 #include "terrain/translation.hpp"
26 #include "game_config.hpp"
27 
28 class config;
29 class gamemap;
30 namespace image{ class locator; }
31 /**
32  * The class terrain_builder is constructed from a config object, and a
33  * gamemap object. On construction, it parses the configuration and extracts
34  * the list of [terrain_graphics] rules. Each terrain_graphics rule attaches
35  * one or more images to a specific terrain pattern.
36  * It then applies the rules loaded from the configuration to the current map,
37  * and calculates the list of images that must be associated to each hex of
38  * the map.
39  *
40  * The get_terrain_at method can then be used to obtain the list of images
41  * necessary to draw the terrain on a given tile.
42  */
44 {
45 public:
46  /** Used as a parameter for the get_terrain_at function. */
47  enum TERRAIN_TYPE {
48  BACKGROUND, /**<
49  * Represents terrains which are to be
50  * drawn behind unit sprites
51  */
53  * Represents terrains which are to be
54  * drawn in front of them.
55  */
56  };
57 
58  /** The position of unit graphics in a tile. Graphics whose y
59  * position is below this value are considered background for
60  * this tile; graphics whose y position is above this value are
61  * considered foreground.
62  */
63  static const int UNITPOS = 36 + 18;
64 
65  static const unsigned int DUMMY_HASH = 0;
66 
67  /** A shorthand typedef for a list of animated image locators,
68  * the base data type returned by the get_terrain_at method.
69  */
70  typedef std::vector<animated<image::locator> > imagelist;
71 
72  /** Constructor for the terrain_builder class.
73  *
74  * @param level A level (scenario)-specific configuration file,
75  * containing scenario-specific [terrain_graphics] rules.
76  * @param map A properly-initialized gamemap object representing
77  * the current terrain map.
78  * @param offmap_image The filename of the image which will be used as
79  * off map image (see add_off_map_rule()).
80  * This image automatically gets the 'terrain/' prefix
81  * and '.png' suffix
82  */
83  terrain_builder(const config &level, const gamemap* map,
84  const std::string& offmap_image);
85 
86  /** Set the config where we will parse the global terrain rules.
87  * This also flushes the terrain rules cache.
88  *
89  * @param cfg The main grame configuration object, where the
90  * [terrain_graphics] rule reside.
91  */
92  static void set_terrain_rules_cfg(const config& cfg);
93 
94  const gamemap& map() const { return *map_; }
95 
96  /**
97  * Updates internals that cache map size. This should be called when the map
98  * size has changed.
99  */
100  void reload_map();
101 
102  void change_map(const gamemap* m);
103 
104  /** Returns a vector of strings representing the images to load & blit
105  * together to get the built content for this tile.
106  *
107  * @param loc The location relative the the terrain map,
108  * where we ask for the image list
109  * @param tod The string representing the current time-of day.
110  * Will be used if some images specify several
111  * time-of-day- related variants.
112  * @param terrain_type BACKGROUND or FOREGROUND,
113  * depending on whether we ask for the terrain which is
114  * before, or after the unit sprite.
115  *
116  * @return Returns a pointer list of animated images corresponding
117  * to the parameters, or nullptr if there is none.
118  */
119  const imagelist *get_terrain_at(const map_location &loc,
120  const std::string &tod, TERRAIN_TYPE const terrain_type);
121 
122  /** Updates the animation at a given tile.
123  * Returns true if something has changed, and must be redrawn.
124  *
125  * @param loc the location to update
126  *
127  * @retval true: this tile must be redrawn.
128  */
129  bool update_animation(const map_location &loc);
130 
131  /** Performs a "quick-rebuild" of the terrain in a given location.
132  * The "quick-rebuild" is no proper rebuild: it only clears the
133  * terrain cache for a given location, and replaces it with a single,
134  * default image for this terrain.
135  *
136  * @param loc the location where to rebuild terrains
137  */
138  void rebuild_terrain(const map_location &loc);
139 
140  /** Performs a complete rebuild of the list of terrain graphics
141  * attached to a map.
142  * Should be called when a terrain is changed in the map.
143  */
144  void rebuild_all();
145 
146  void rebuild_cache_all();
147 
148  /**
149  * An image variant. The in-memory representation of the [variant]
150  * WML tag of the [image] WML tag. When an image only has one variant,
151  * the [variant] tag may be omitted.
152  */
154  /** Constructor for the normal defaut case */
156  image_string(image_string),
157  variations(variations),
158  images(),
159  tods(),
160  has_flag(),
162  {}
163 
164  /** Constructor for true [variant] cases */
166 
167  /** A string representing either the filename for an image, or
168  * a list of images, with an optional timing for each image.
169  * Corresponds to the "name" parameter of the [variant] (or of
170  * the [image]) WML tag.
171  *
172  * The timing string is in the following format (expressed in EBNF)
173  *
174  *@verbatim
175  * <timing_string> ::= <timed_image> ( "," <timed_image> ) +
176  *
177  * <timed_image> ::= <image_name> [ ":" <timing> ]
178  *
179  * Where <image_name> represents the actual filename of an image,
180  * and <timing> the number of milliseconds this image will last
181  * in the animation.
182  *@endverbatim
183  */
185 
186  /** A semi-solon separated list of string used to replace
187  * @verbatim <code>@V</code> @endverbatim in image_string (if present)
188  */
190 
191  /** An animated image locator built according to the image string.
192  * This will be the image locator which will actually
193  * be returned to the user.
194  */
195  std::vector< animated<image::locator> > images;
196 
197  /** The Time of Day associated to this variant (if any)*/
198  std::set<std::string> tods;
199 
200  std::vector<std::string> has_flag;
201 
202  /** Indicate if the animation uses a random shift */
204  };
205 
206  /**
207  * Each terrain_graphics rule is associated a set of images, which are
208  * applied on the terrain if the rule matches. An image is more than
209  * graphics: it is graphics (with several possible tod-alternatives,)
210  * and a position for these graphics.
211  * The rule_image structure represents one such image.
212  */
213  struct rule_image {
214  rule_image(int layer, int x, int y, bool global_image=false, int center_x=-1, int center_y=-1, bool is_water=false);
215 
216  bool is_background() const {
217  return layer < 0 || (layer == 0 && basey < UNITPOS);
218  }
219 
220  /** The layer of the image for horizontal layering */
221  int layer;
222  /** The position of the image base (that is, the point where
223  * the image reaches the floor) for vertical layering
224  */
225  int basex, basey;
226 
227  /** A list of variants for this image */
228  std::vector<rule_image_variant> variants;
229 
230  /** Set to true if the image was defined as a child of the
231  * [terrain_graphics] tag, set to false if it was defined as a
232  * child of a [tile] tag */
234 
235  /** The position where the center of the image base should be
236  */
238 
239  bool is_water;
240  };
241 
242  /**
243  * A shorthand notation for a vector of rule_images
244  */
245  typedef std::vector<rule_image> rule_imagelist;
246 
247  /**
248  * The in-memory representation of a [tile] WML rule
249  * inside of a [terrain_graphics] WML rule.
250  */
252  {
254  loc(),
256  set_flag(),
257  no_flag(),
258  has_flag(),
259  no_draw(),
260  images()
261  {}
262 
264  loc(loc),
266  set_flag(),
267  no_flag(),
268  has_flag(),
269  no_draw(),
270  images()
271  {}
272 
275  std::vector<std::string> set_flag;
276  std::vector<std::string> no_flag;
277  std::vector<std::string> has_flag;
278 
279  /** Whether to actually draw the images onto this hex or not */
280  bool no_draw;
281 
282  rule_imagelist images;
283  };
284 
285  /**
286  * Represents a tile of the game map, with all associated
287  * builder-specific parameters: flags, images attached to this tile,
288  * etc. An array of those tiles is built when terrains are built either
289  * during construction, or upon calling the rebuild_all() method.
290  */
291  struct tile
292  {
293  /** Constructor for the tile() structure */
294  tile();
295 
297  typedef std::pair<const rule_image_rand*, const rule_image_variant*> log_details;
298  typedef std::vector<log_details> logs;
299  /** Rebuilds the whole image cache, for a given time-of-day.
300  * Must be called when the time-of-day has changed,
301  * to select the correct images.
302  *
303  * @param tod The current time-of-day
304  */
305  void rebuild_cache(const std::string &tod, logs* log = nullptr);
306 
307  /** Clears all data in this tile, and resets the cache */
308  void clear();
309 
310  /** The list of flags present in this tile */
311  std::set<std::string> flags;
312 
313  /** Represent a rule_image applied with a random seed.*/
315  rule_image_rand(const rule_image* r_i, unsigned int rnd) : ri(r_i), rand(rnd) {}
316 
317  const rule_image* operator->() const {return ri;}
318  /** sort by layer first then by basey */
319  bool operator<(const rule_image_rand& o) const {
320  return ri->layer < o.ri->layer ||
321  (ri->layer == o.ri->layer && ri->basey < o.ri->basey);}
322 
323  const rule_image* ri;
324  unsigned int rand;
325  };
326 
327  /** The list of rule_images and random seeds associated to this tile.
328  */
329  std::vector<rule_image_rand> images;
330 
331  /** The list of images which are in front of the unit sprites,
332  * attached to this tile. This member is considered a cache:
333  * it is built once, and on-demand.
334  */
335  imagelist images_foreground;
336  /** The list of images which are behind the unit sprites,
337  * attached to this tile. This member is considered a cache:
338  * it is built once, and on-demand.
339  */
340  imagelist images_background;
341  /**
342  * The time-of-day to which the image caches correspond.
343  */
345 
346  /** Indicates if 'images' is sorted */
348  };
349 
350  tile* get_tile(const map_location &loc);
351 
352 private:
353 
354  /** The tile width used when using basex and basey. This is not,
355  * necessarily, the tile width in pixels, this is totally
356  * arbitrary. However, it will be set to 72 for convenience.
357  */
358  const int tilewidth_; // = game_config::tile_size;
359 
360  /**
361  * The list of constraints attached to a terrain_graphics WML rule.
362  */
363  typedef std::vector<terrain_constraint> constraint_set;
364 
365  /**
366  * The in-memory representation of a [terrain_graphics] WML rule.
367  */
369  {
371  constraints(),
374  probability(100),
375  precedence(0),
376  local(false),
377  hash_(DUMMY_HASH)
378  {}
379 
380  /**
381  * The set of [tile] constraints of this rule.
382  */
383  constraint_set constraints;
384 
385  /**
386  * The location on which this map may match.
387  * Set to a valid map_location if the "x" and "y" parameters
388  * of the [terrain_graphics] rule are set.
389  */
391 
392  /**
393  * Used to constrain locations to ones with coordinates that are
394  * multiples of the "mod_x" and "mod_y" parameters. Doesn't actually
395  * refer to a real map location.
396  */
398 
399  /**
400  * The probability of this rule to match, when all conditions
401  * are met. Defined if the "probability" parameter of the
402  * [terrain_graphics] element is set.
403  */
405 
406  /**
407  * Ordering relation between the rules.
408  */
410 
411  /**
412  * Indicate if the rule is only for this scenario
413  */
414  bool local;
415 
416  bool operator<(building_rule const &that) const
417  { return precedence < that.precedence; }
418 
419  unsigned int get_hash() const;
420  private:
421  mutable unsigned int hash_;
422  };
423 
424  /**
425  * The map of "tile" structures corresponding to the level map.
426  */
427  class tilemap
428  {
429  public:
430  /**
431  * Constructs a tilemap of dimensions x * y
432  */
433  tilemap(int x, int y) :
434  tiles_((x + 4) * (y + 4)),
435  x_(x),
436  y_(y)
437  {reset();}
438 
439  /**
440  * Returns a reference to the tile which is at the position
441  * pointed by loc. The location MUST be on the map!
442  *
443  * @param loc The location of the tile
444  *
445  * @return A reference to the tile at this location.
446  *
447  */
448  tile &operator[](const map_location &loc);
449  /**
450  * a const variant of operator[]
451  */
452  const tile &operator[] (const map_location &loc) const;
453 
454  /**
455  * Tests if a location is on the map.
456  *
457  * @param loc The location to test
458  *
459  * @return true if loc is on the map, false otherwise.
460  */
461  bool on_map(const map_location &loc) const;
462 
463  /**
464  * Resets the whole tile map
465  */
466  void reset();
467 
468  /**
469  * Rebuilds the map to a new set of dimensions
470  */
471  void reload(int x, int y);
472  private:
473  /** The map */
474  std::vector<tile> tiles_;
475  /** The x dimension of the map */
476  int x_;
477  /** The y dimension of the map */
478  int y_;
479  };
480 
481  /**
482  * A set of building rules. In-memory representation
483  * of the whole set of [terrain_graphics] rules.
484  */
485  typedef std::multiset<building_rule> building_ruleset;
486 
487  /**
488  * Load images and tests for validity of a rule. A rule is considered
489  * valid if all its images are present. This method is used, when building
490  * the ruleset, to only add rules which are valid to the ruleset.
491  *
492  * @param rule The rule to test for validity
493  *
494  * @return true if the rule is valid, false if it is not.
495  */
496  bool load_images(building_rule &rule);
497 
498  /**
499  * Starts the animation on a rule.
500  *
501  * @param rule The rule on which ot start animations
502  *
503  * @return true
504  */
505  bool start_animation(building_rule &rule);
506 
507  /**
508  * "Rotates" a constraint from a rule.
509  * Takes a template constraint from a template rule, and rotates
510  * to the given angle.
511  *
512  * On a constraint, the relative position of each rule, and the "base"
513  * of each vertical images, are rotated according to the given angle.
514  *
515  * Template terrain constraints are defined like normal terrain
516  * constraints, except that, flags, and image filenames,
517  * may contain template strings of the form
518  *@verbatim
519  * <code>@Rn</code>,
520  *@endverbatim
521  * n being a number from 0 to 5.
522  * See the rotate_rule method for more info.
523  *
524  * @param constraint A template constraint to rotate
525  * @param angle An int, from 0 to 5, representing the rotation angle.
526  */
527  void rotate(terrain_constraint &constraint, int angle);
528 
529  /**
530  * Replaces, in a given string, rotation tokens with their values.
531  *
532  * @param s the string in which to do the replacement
533  * @param angle the angle for substituting the correct replacement.
534  * @param replacement the replacement strings.
535  */
537  const std::vector<std::string> &replacement);
538 
539  /**
540  * Replaces, in a given rule_image, rotation tokens with their values.
541  * The actual substitution is done in all variants of the given image.
542  *
543  * @param image the rule_image in which to do the replacement.
544  * @param angle the angle for substituting the correct replacement.
545  * @param replacement the replacement strings.
546  */
548  const std::vector<std::string> &replacement);
549 
550  /**
551  * Replaces, in a given rule_variant_image, rotation tokens with their values.
552  * The actual substitution is done in the "image_string" parameter
553  * of this rule_variant_image.
554  *
555  * @param variant the rule_variant_image in which to do the replacement.
556  * @param angle the angle for substituting the correct replacement.
557  * @param replacement the replacement strings.
558  */
560  const std::vector<std::string> &replacement)
561  { replace_rotate_tokens(variant.image_string, angle, replacement); }
562 
563  /**
564  * Replaces, in a given rule_imagelist, rotation tokens with their values.
565  * The actual substitution is done in all rule_images contained
566  * in the rule_imagelist.
567  *
568  * @param list the rule_imagelist in which to do the replacement.
569  * @param angle the angle for substituting the correct replacement.
570  * @param replacement the replacement strings.
571  */
572  void replace_rotate_tokens(rule_imagelist &list, int angle,
573  const std::vector<std::string> &replacement);
574 
575  /**
576  * Replaces, in a given building_rule, rotation tokens with their values.
577  * The actual substitution is done in the rule_imagelists contained
578  * in all constraints of the building_rule, and in the flags
579  * (has_flag, set_flag and no_flag) contained in all constraints
580  * of the building_rule.
581  *
582  * @param rule the building_rule in which to do the replacement.
583  * @param angle the angle for substituting the correct replacement.
584  * @param replacement the replacement strings.
585  */
586  void replace_rotate_tokens(building_rule &rule, int angle,
587  const std::vector<std::string> &replacement);
588 
589  /**
590  * Rotates a template rule to a given angle.
591  *
592  * Template rules are defined like normal rules, except that:
593  * * Flags and image filenames may contain template strings of the form
594  *@verbatim
595  * <code>@Rn</code>, n being a number from 0 to 5.
596  *@endverbatim
597  * * The rule contains the rotations=r0,r1,r2,r3,r4,r5, with r0 to r5
598  * being strings describing the 6 different positions, typically,
599  * n, ne, se, s, sw, and nw (but maybe anything else.)
600  *
601  * A template rule will generate 6 rules, which are similar
602  * to the template, except that:
603  *
604  * * The map of constraints ( [tile]s ) of this rule will be
605  * rotated by an angle, of 0 to 5 pi / 6
606  *
607  * * On the rule which is rotated to 0rad, the template strings
608  *@verbatim
609  * @R0, @R1, @R2, @R3, @R4, @R5,
610  *@endverbatim
611  * will be replaced by the corresponding r0, r1, r2, r3, r4, r5
612  * variables given in the rotations= element.
613  *
614  * * On the rule which is rotated to pi/3 rad, the template strings
615  *@verbatim
616  * @R0, @R1, @R2 etc.
617  *@endverbatim
618  * will be replaced by the corresponding
619  * <strong>r1, r2, r3, r4, r5, r0</strong> (note the shift in indices).
620  *
621  * * On the rule rotated 2pi/3, those will be replaced by
622  * r2, r3, r4, r5, r0, r1 and so on.
623  *
624  */
625  void rotate_rule(building_rule &rule, int angle,
626  const std::vector<std::string> &angle_name);
627 
628  /**
629  * Parses a "config" object, which should contains [image] children,
630  * and adds the corresponding parsed rule_images to a rule_imagelist.
631  *
632  * @param images The rule_imagelist into which to add the parsed images.
633  * @param cfg The WML configuration object to parse
634  * @param global Whether those [image]s elements belong to a
635  * [terrain_graphics] element, or to a [tile] child.
636  * Set to true if those belong to a [terrain_graphics]
637  * element.
638  * @param dx The X coordinate of the constraint those images
639  * apply to, relative to the start of the rule. Only
640  * meaningful if global is set to false.
641  * @param dy The Y coordinate of the constraint those images
642  * apply to.
643  */
644  void add_images_from_config(rule_imagelist &images, const config &cfg, bool global,
645  int dx=0, int dy=0);
646 
647 
648  /**
649  * Creates a rule constraint object which matches a given list of
650  * terrains, and adds it to the list of constraints of a rule.
651  *
652  * @param constraints The constraint set to which to add the constraint.
653  * @param loc The location of the constraint
654  * @param type The list of terrains this constraint will match
655  * @param global_images A configuration object containing [image] tags
656  * describing rule-global images.
657  */
658  terrain_constraint &add_constraints(constraint_set& constraints,
659  const map_location &loc, const t_translation::t_match& type,
660  const config& global_images);
661 
662  /**
663  * Creates a rule constraint object from a config object and
664  * adds it to the list of constraints of a rule.
665  *
666  * @param constraints The constraint set to which to add the constraint.
667  * @param loc The location of the constraint
668  * @param cfg The config object describing this constraint.
669  * Usually, a [tile] child of a [terrain_graphics] rule.
670  * @param global_images A configuration object containing [image] tags
671  * describing rule-global images.
672  */
673  void add_constraints(constraint_set& constraints,
674  const map_location &loc, const config &cfg,
675  const config& global_images);
676 
677  typedef std::multimap<int, map_location> anchormap;
678 
679  /**
680  * Parses a map string (the map= element of a [terrain_graphics] rule,
681  * and adds constraints from this map to a building_rule.
682  *
683  * @param mapstring The map vector to parse
684  * @param br The building rule into which to add the extracted
685  * constraints
686  * @param anchors A map where to put "anchors" extracted from the map.
687  * @param global_images A config object representing the images defined
688  * as direct children of the [terrain_graphics] rule.
689  */
690  void parse_mapstring(const std::string &mapstring, struct building_rule &br,
691  anchormap& anchors, const config& global_images);
692 
693  /**
694  * Adds a rule to a ruleset. Checks for validity before adding the rule.
695  *
696  * @param rules The ruleset into which to add the rules.
697  * @param rule The rule to add.
698  */
699  void add_rule(building_ruleset& rules, building_rule &rule);
700 
701  /**
702  * Adds a set of rules to a ruleset, from a template rule which spans
703  * 6 rotations (or less if some of the rotated rules are invalid).
704  *
705  * @param rules The ruleset into which to add the rules.
706  * @param tpl The template rule
707  * @param rotations A comma-separated string containing the
708  * 6 values for replacing rotation template
709  * template strings @verbatim (@Rn) @endverbatim
710  */
711  void add_rotated_rules(building_ruleset& rules, building_rule& tpl,
712  const std::string &rotations);
713 
714  /**
715  * Parses a configuration object containing [terrain_graphics] rules,
716  * and fills the building_rules_ member of the current class according
717  * to those.
718  *
719  * @param cfg The configuration object to parse.
720  * @param local Mark the rules as local only.
721  */
722  void parse_config(const config &cfg, bool local = true);
723 
724  void parse_global_config(const config &cfg) { parse_config(cfg, false); }
725 
726  /**
727  * Adds a builder rule for the _off^_usr tile, this tile only has 1 image.
728  *
729  * @param image The filename of the image
730  */
731  void add_off_map_rule(const std::string& image);
732 
733  void flush_local_rules();
734 
735  /**
736  * Checks whether a terrain code matches a given list of terrain codes.
737  *
738  * @param tcode The terrain to check
739  * @param terrains The terrain list agains which to check the terrain.
740  * May contain the metacharacters
741  * - '*' STAR, meaning "all terrains"
742  * - '!' NOT, meaning "all terrains except those present in the list."
743  *
744  * @return returns true if "tcode" matches the list or the list is empty,
745  * else false.
746  */
747  bool terrain_matches(const t_translation::t_terrain & tcode, const t_translation::t_list& terrains) const
748  { return terrains.empty()? true : t_translation::terrain_matches(tcode, terrains); }
749 
750  /**
751  * Checks whether a terrain code matches a given list of terrain tcodes.
752  *
753  * @param tcode The terrain code to check
754  * @param terrain The terrain match structure which to check the terrain.
755  * See previous definition for more details.
756  *
757  * @return returns true if "tcode" matches the list or the list is empty,
758  * else false.
759  */
761  { return terrain.is_empty ? true : t_translation::terrain_matches(tcode, terrain); }
762 
763  /**
764  * Checks whether a rule matches a given location in the map.
765  *
766  * @param rule The rule to check.
767  * @param loc The location in the map where we want to check
768  * whether the rule matches.
769  * @param type_checked The constraint which we already know that its
770  * terrain types matches.
771  */
772  bool rule_matches(const building_rule &rule, const map_location &loc, const terrain_constraint *type_checked) const;
773 
774  /**
775  * Applies a rule at a given location: applies the result of a
776  * matching rule at a given location: attachs the images corresponding
777  * to the rule, and sets the flags corresponding to the rule.
778  *
779  * @param rule The rule to apply
780  * @param loc The location to which to apply the rule.
781  */
782  void apply_rule(const building_rule &rule, const map_location &loc);
783 
784  /**
785  * Calculates the list of terrains, and fills the tile_map_ member,
786  * from the gamemap and the building_rules_.
787  */
788  void build_terrains();
789 
790  /**
791  * A pointer to the gamemap class used in the current level.
792  */
793  const gamemap* map_;
794 
795  /**
796  * The tile_map_ for the current level, which is filled by the
797  * build_terrains_ method to contain "tiles" representing images
798  * attached to each tile.
799  */
801 
802  /**
803  * Shorthand typedef for a map associating a list of locations to a terrain type.
804  */
805  typedef std::map<t_translation::t_terrain, std::vector<map_location> > terrain_by_type_map;
806 
807  /**
808  * A map representing all locations whose terrain is of a given type.
809  */
810  terrain_by_type_map terrain_by_type_;
811 
812  /** Parsed terrain rules. Cached between instances */
813  static building_ruleset building_rules_;
814 
815  /** Config used to parse global terrain rules */
816  static const config* rules_cfg_;
817 };
818 
819 #endif
std::vector< tile > tiles_
The map.
Definition: builder.hpp:474
void rebuild_terrain(const map_location &loc)
Performs a "quick-rebuild" of the terrain in a given location.
Definition: builder.cpp:353
std::pair< const rule_image_rand *, const rule_image_variant * > log_details
Definition: builder.hpp:296
int layer
The layer of the image for horizontal layering.
Definition: builder.hpp:221
rule_image_rand(const rule_image *r_i, unsigned int rnd)
Definition: builder.hpp:315
void reload_map()
Updates internals that cache map size.
Definition: builder.cpp:293
The in-memory representation of a [terrain_graphics] WML rule.
Definition: builder.hpp:368
terrain_by_type_map terrain_by_type_
A map representing all locations whose terrain is of a given type.
Definition: builder.hpp:810
tile * get_tile(const map_location &loc)
Definition: builder.cpp:1204
int y_
The y dimension of the map.
Definition: builder.hpp:478
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...
Definition: builder.cpp:726
map_location modulo_constraints
Used to constrain locations to ones with coordinates that are multiples of the "mod_x" and "mod_y" pa...
Definition: builder.hpp:397
void reset()
Resets the whole tile map.
Definition: builder.cpp:199
GLint level
Definition: glew.h:1220
The in-memory representation of a [tile] WML rule inside of a [terrain_graphics] WML rule...
Definition: builder.hpp:251
void add_rule(building_ruleset &rules, building_rule &rule)
Adds a rule to a ruleset.
Definition: builder.cpp:835
rule_image_variant(const std::string &image_string, const std::string &variations, bool random_start=true)
Constructor for the normal defaut case.
Definition: builder.hpp:155
tile & operator[](const map_location &loc)
Returns a reference to the tile which is at the position pointed by loc.
Definition: builder.cpp:224
bool no_draw
Whether to actually draw the images onto this hex or not.
Definition: builder.hpp:280
void add_off_map_rule(const std::string &image)
Adds a builder rule for the _off^_usr tile, this tile only has 1 image.
Definition: builder.cpp:990
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
bool load_images(building_rule &rule)
Load images and tests for validity of a rule.
Definition: builder.cpp:431
std::vector< terrain_constraint > constraint_set
The list of constraints attached to a terrain_graphics WML rule.
Definition: builder.hpp:363
Represent a rule_image applied with a random seed.
Definition: builder.hpp:314
imagelist images_background
The list of images which are behind the unit sprites, attached to this tile.
Definition: builder.hpp:340
int precedence
Ordering relation between the rules.
Definition: builder.hpp:409
static building_ruleset building_rules_
Parsed terrain rules.
Definition: builder.hpp:813
std::multimap< int, map_location > anchormap
Definition: builder.hpp:677
void rebuild_cache_all()
Definition: builder.cpp:264
The class terrain_builder is constructed from a config object, and a gamemap object.
Definition: builder.hpp:43
t_translation::t_match terrain_types_match
Definition: builder.hpp:274
static const int UNITPOS
The position of unit graphics in a tile.
Definition: builder.hpp:63
int basex
The position of the image base (that is, the point where the image reaches the floor) for vertical la...
Definition: builder.hpp:225
const int tilewidth_
The tile width used when using basex and basey.
Definition: builder.hpp:358
bool operator<(building_rule const &that) const
Definition: builder.hpp:416
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
bool is_background() const
Definition: builder.hpp:216
terrain_constraint(map_location loc)
Definition: builder.hpp:263
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.
Definition: builder.cpp:1014
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...
Definition: builder.cpp:842
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...
Definition: builder.cpp:1071
void replace_rotate_tokens(rule_image_variant &variant, int angle, const std::vector< std::string > &replacement)
Replaces, in a given rule_variant_image, rotation tokens with their values.
Definition: builder.hpp:559
void rebuild_all()
Performs a complete rebuild of the list of terrain graphics attached to a map.
Definition: builder.cpp:385
void rebuild_cache(const std::string &tod, logs *log=nullptr)
Rebuilds the whole image cache, for a given time-of-day.
Definition: builder.cpp:110
static const unsigned int DUMMY_HASH
Definition: builder.hpp:65
tilemap(int x, int y)
Constructs a tilemap of dimensions x * y.
Definition: builder.hpp:433
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.
Definition: builder.cpp:575
int probability
The probability of this rule to match, when all conditions are met.
Definition: builder.hpp:404
Represents a tile of the game map, with all associated builder-specific parameters: flags...
Definition: builder.hpp:291
void change_map(const gamemap *m)
Definition: builder.cpp:300
bool operator<(const rule_image_rand &o) const
sort by layer first then by basey
Definition: builder.hpp:319
const gamemap * map_
A pointer to the gamemap class used in the current level.
Definition: builder.hpp:793
GLenum GLenum GLuint GLint GLint layer
Definition: glew.h:3455
This structure can be used for matching terrain strings.
Definition: translation.hpp:83
Animate units.
tilemap tile_map_
The tile_map_ for the current level, which is filled by the build_terrains_ method to contain "tiles"...
Definition: builder.hpp:800
Encapsulates the map of the game.
Definition: map.hpp:37
std::set< std::string > tods
The Time of Day associated to this variant (if any)
Definition: builder.hpp:198
TERRAIN_TYPE
Used as a parameter for the get_terrain_at function.
Definition: builder.hpp:47
void flush_local_rules()
Definition: builder.cpp:273
terrain_builder(const config &level, const gamemap *map, const std::string &offmap_image)
Constructor for the terrain_builder class.
Definition: builder.cpp:238
std::vector< rule_image_variant > variants
A list of variants for this image.
Definition: builder.hpp:228
std::map< t_translation::t_terrain, std::vector< map_location > > terrain_by_type_map
Shorthand typedef for a map associating a list of locations to a terrain type.
Definition: builder.hpp:805
std::vector< rule_image_rand > images
The list of rule_images and random seeds associated to this tile.
Definition: builder.hpp:329
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:135
std::vector< log_details > logs
Definition: builder.hpp:298
std::vector< animated< image::locator > > images
An animated image locator built according to the image string.
Definition: builder.hpp:195
The map of "tile" structures corresponding to the level map.
Definition: builder.hpp:427
bool local
Indicate if the rule is only for this scenario.
Definition: builder.hpp:414
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
Encapsulates the map of the game.
Definition: location.hpp:38
void rotate_rule(building_rule &rule, int angle, const std::vector< std::string > &angle_name)
Rotates a template rule to a given angle.
Definition: builder.cpp:627
bool update_animation(const map_location &loc)
Updates the animation at a given tile.
Definition: builder.cpp:329
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...
Definition: builder.hpp:233
std::string last_tod
The time-of-day to which the image caches correspond.
Definition: builder.hpp:344
void rotate(terrain_constraint &constraint, int angle)
"Rotates" a constraint from a rule.
Definition: builder.cpp:495
static const config * rules_cfg_
Config used to parse global terrain rules.
Definition: builder.hpp:816
std::vector< std::string > set_flag
Definition: builder.hpp:275
std::set< std::string > flags
The list of flags present in this tile.
Definition: builder.hpp:311
unsigned int get_hash() const
Definition: builder.cpp:1109
rule_image(int layer, int x, int y, bool global_image=false, int center_x=-1, int center_y=-1, bool is_water=false)
Definition: builder.cpp:90
std::vector< rule_image > rule_imagelist
A shorthand notation for a vector of rule_images.
Definition: builder.hpp:245
bool terrain_matches(const t_terrain &src, const t_terrain &dest)
Tests whether a specific terrain matches an expression, for matching rules see above.
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
bool terrain_matches(const t_translation::t_terrain &tcode, const t_translation::t_match &terrain) const
Checks whether a terrain code matches a given list of terrain tcodes.
Definition: builder.hpp:760
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...
Definition: builder.cpp:788
Represents terrains which are to be drawn in front of them.
Definition: builder.hpp:52
std::vector< std::string > has_flag
Definition: builder.hpp:200
void reload(int x, int y)
Rebuilds the map to a new set of dimensions.
Definition: builder.cpp:205
int center_x
The position where the center of the image base should be.
Definition: builder.hpp:237
Represents terrains which are to be drawn behind unit sprites.
Definition: builder.hpp:48
Each terrain_graphics rule is associated a set of images, which are applied on the terrain if the rul...
Definition: builder.hpp:213
std::vector< std::string > has_flag
Definition: builder.hpp:277
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...
Definition: builder.hpp:70
void build_terrains()
Calculates the list of terrains, and fills the tile_map_ member, from the gamemap and the building_ru...
Definition: builder.cpp:1130
tile()
Constructor for the tile() structure.
Definition: builder.cpp:101
std::multiset< building_rule > building_ruleset
A set of building rules.
Definition: builder.hpp:485
int x_
The x dimension of the map.
Definition: builder.hpp:476
const rule_image * operator->() const
Definition: builder.hpp:317
constraint_set constraints
The set of [tile] constraints of this rule.
Definition: builder.hpp:383
static std::map< std::string, std::string > images
Definition: about.cpp:58
bool sorted_images
Indicates if 'images' is sorted.
Definition: builder.hpp:347
static void set_terrain_rules_cfg(const config &cfg)
Set the config where we will parse the global terrain rules.
Definition: builder.cpp:284
bool random_start
Indicate if the animation uses a random shift.
Definition: builder.hpp:203
const GLdouble * m
Definition: glew.h:6968
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...
Definition: builder.cpp:677
map_location location_constraints
The location on which this map may match.
Definition: builder.hpp:390
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.
Definition: builder.hpp:747
GLdouble angle
Definition: glew.h:6979
this module manages the cache of images.
Definition: image.cpp:75
std::string image_string
A string representing either the filename for an image, or a list of images, with an optional timing ...
Definition: builder.hpp:184
std::string variations
A semi-solon separated list of string used to replace.
Definition: builder.hpp:189
void parse_config(const config &cfg, bool local=true)
Parses a configuration object containing [terrain_graphics] rules, and fills the building_rules_ memb...
Definition: builder.cpp:867
imagelist images_foreground
The list of images which are in front of the unit sprites, attached to this tile. ...
Definition: builder.hpp:335
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void parse_global_config(const config &cfg)
Definition: builder.hpp:724
void clear()
Clears all data in this tile, and resets the cache.
Definition: builder.cpp:181
GLdouble s
Definition: glew.h:1358
std::vector< std::string > no_flag
Definition: builder.hpp:276
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool start_animation(building_rule &rule)
Starts the animation on a rule.
bool on_map(const map_location &loc) const
Tests if a location is on the map.
Definition: builder.cpp:214
const gamemap & map() const
Definition: builder.hpp:94
std::vector< t_terrain > t_list
Definition: translation.hpp:75
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 ...
Definition: builder.cpp:306