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