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