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