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