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