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