The Battle for Wesnoth  1.15.0-dev
default_map_generator_job.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Map-generator, with standalone testprogram.
18  */
19 
22 #include "game_config_manager.hpp"
23 #include "gettext.hpp"
24 #include "log.hpp"
25 #include "map/map.hpp"
26 #include "generators/map_generator.hpp" // mapgen_exception
27 #include "pathfind/pathfind.hpp"
28 #include "pathutils.hpp"
30 #include "seed_rng.hpp"
31 #include "wml_exception.hpp"
32 
33 #include <SDL_timer.h>
34 
35 static lg::log_domain log_mapgen("mapgen");
36 #define ERR_NG LOG_STREAM(err, log_mapgen)
37 #define LOG_NG LOG_STREAM(info, log_mapgen)
38 
39 typedef std::vector<std::vector<int>> height_map;
41 
42 namespace {
43  /**
44  * Calculates the cost of building a road over terrain. For use in the
45  * a_star_search algorithm.
46  */
47  struct road_path_calculator : pathfind::cost_calculator
48  {
49  road_path_calculator(const terrain_map& terrain, const config& cfg, int seed)
50  : calls(0)
51  , map_(terrain)
52  , cfg_(cfg)
53  , windiness_(std::max<int>(1, cfg["road_windiness"].to_int())) // Find out how windey roads should be.
54  , seed_(seed)
55  , cache_()
56  {
57  }
58 
59  virtual double cost(const map_location& loc, const double so_far) const;
60 
61  mutable int calls;
62  private:
63  const terrain_map& map_;
64  const config& cfg_;
65  int windiness_;
66  int seed_;
67  mutable std::map<t_translation::terrain_code, double> cache_;
68  };
69 
70  double road_path_calculator::cost(const map_location& loc, const double /*so_far*/) const
71  {
72  ++calls;
73  if(loc.x < 0 || loc.y < 0 || loc.x >= map_.w || loc.y >= map_.h) {
74 
76  }
77 
78  // We multiply the cost by a random amount,
79  // depending upon how 'windy' the road should be.
80  // If windiness is 1, that will mean that the cost is always genuine,
81  // and so the road always takes the shortest path.
82  // If windiness is greater than 1, we sometimes over-report costs
83  // for some segments, to make the road wind a little.
84 
85  double windiness = 1.0;
86 
87  if(windiness_ > 1) {
88  // modified pseudo_random taken from builder.cpp
89  unsigned int a = (loc.x + 92872973) ^ 918273;
90  unsigned int b = (loc.y + 1672517) ^ 128123;
91  unsigned int c = a*b + a + b + seed_;
92  unsigned int random = c*c;
93  // this is just "big random number modulo windiness_"
94  // but avoid the "modulo by a low number (like 2)"
95  // because it can increase arithmetic patterns
96  int noise = random % (windiness_ * 137) / 137;
97  windiness += noise;
98  }
99 
100  const t_translation::terrain_code c = map_[loc.x][loc.y];
101  const std::map<t_translation::terrain_code, double>::const_iterator itor = cache_.find(c);
102  if(itor != cache_.end()) {
103  return itor->second*windiness;
104  }
105 
106  static std::string terrain;
108  double res = getNoPathValue();
109  if(const config &child = cfg_.find_child("road_cost", "terrain", terrain)) {
110  res = child["cost"].to_double();
111  }
112 
113  cache_.emplace(c, res);
114  return windiness*res;
115  }
116 
117 
118  struct is_valid_terrain
119  {
120  is_valid_terrain(const t_translation::ter_map& map, const t_translation::ter_list& terrain_list);
121  bool operator()(int x, int y) const;
122  private:
124  const t_translation::ter_list& terrain_;
125  };
126 
128  : map_(map), terrain_(terrain_list)
129  {
130  }
131 
132  bool is_valid_terrain::operator()(int x, int y) const
133  {
134  if(x < 0 || x >= map_.w || y < 0 || y >= map_.h) {
135 
136  return false;
137  }
138 
139  return std::find(terrain_.begin(),terrain_.end(),map_[x][y]) != terrain_.end();
140  }
141 
142 
143  /* the configuration file should contain a number of [height] tags:
144  * [height]
145  * height=n
146  * terrain=x
147  * [/height]
148  * These should be in descending order of n.
149  * They are checked sequentially, and if height is greater than n for that tile,
150  * then the tile is set to terrain type x.
151  */
152  class terrain_height_mapper
153  {
154  public:
155  explicit terrain_height_mapper(const config& cfg);
156 
157  bool convert_terrain(const int height) const;
158  t_translation::terrain_code convert_to() const;
159 
160  private:
161  int terrain_height;
163  };
164 
165  terrain_height_mapper::terrain_height_mapper(const config& cfg) :
166  terrain_height(cfg["height"]),
168  {
169  const std::string& terrain = cfg["terrain"];
170  if(!terrain.empty()) {
171  to = t_translation::read_terrain_code(terrain);
172  }
173  }
174 
175  bool terrain_height_mapper::convert_terrain(const int height) const
176  {
177  return height >= terrain_height;
178  }
179 
180  t_translation::terrain_code terrain_height_mapper::convert_to() const
181  {
182  return to;
183  }
184 
185 
186  class terrain_converter
187  {
188  public:
189  explicit terrain_converter(const config& cfg);
190 
191  bool convert_terrain(const t_translation::terrain_code & terrain, const int height, const int temperature) const;
192  t_translation::terrain_code convert_to() const;
193 
194  private:
195  int min_temp, max_temp, min_height, max_height;
198  };
199 
200  terrain_converter::terrain_converter(const config& cfg)
201  : min_temp(cfg["min_temperature"].to_int(-100000))
202  , max_temp(cfg["max_temperature"].to_int(100000))
203  , min_height(cfg["min_height"].to_int(-100000))
204  , max_height(cfg["max_height"].to_int(100000))
205  , from(t_translation::read_list(cfg["from"]))
207  {
208  const std::string& to_str = cfg["to"];
209  if(!to_str.empty()) {
211  }
212  }
213 
214  bool terrain_converter::convert_terrain(const t_translation::terrain_code & terrain,
215  const int height, const int temperature) const
216  {
217  return std::find(from.begin(),from.end(),terrain) != from.end() && height >= min_height && height <= max_height && temperature >= min_temp && temperature <= max_temp && to != t_translation::NONE_TERRAIN;
218  }
219 
220  t_translation::terrain_code terrain_converter::convert_to() const
221  {
222  return to;
223  }
224 
225 } // end anon namespace
226 
227 
229  : rng_(seed_rng::next_seed())
230  , game_config_(game_config_manager::get()->game_config())
231 {
232 }
233 
235  : rng_(seed)
237 {
238 }
239 
240 /**
241  * Generate a height-map.
242  *
243  * Basically we generate a lot of hills, each hill being centered at a certain
244  * point, with a certain radius - being a half sphere. Hills are combined
245  * additively to form a bumpy surface. The size of each hill varies randomly
246  * from 1-hill_size. We generate 'iterations' hills in total. The range of
247  * heights is normalized to 0-1000. 'island_size' controls whether or not the
248  * map should tend toward an island shape, and if so, how large the island
249  * should be. Hills with centers that are more than 'island_size' away from
250  * the center of the map will be inverted (i.e. be valleys). 'island_size' as
251  * 0 indicates no island.
252  */
253 height_map default_map_generator_job::generate_height_map(std::size_t width, std::size_t height, std::size_t iterations, std::size_t hill_size, std::size_t island_size, std::size_t island_off_center)
254 {
255  height_map res(width, std::vector<int>(height,0));
256 
257  std::size_t center_x = width/2;
258  std::size_t center_y = height/2;
259 
260  LOG_NG << "off-centering...\n";
261 
262  if(island_off_center != 0) {
263  switch(rng_()%4) {
264  case 0:
265  center_x += island_off_center;
266  break;
267  case 1:
268  center_y += island_off_center;
269  break;
270  case 2:
271  if(center_x < island_off_center) {
272  center_x = 0;
273  } else {
274  center_x -= island_off_center;
275  }
276  break;
277  case 3:
278  if(center_y < island_off_center) {
279  center_y = 0;
280  } else {
281  center_y -= island_off_center;
282  }
283  break;
284  }
285  }
286 
287  for(std::size_t i = 0; i != iterations; ++i) {
288 
289  // (x1,y1) is the location of the hill,
290  // and 'radius' is the radius of the hill.
291  // We iterate over all points, (x2,y2).
292  // The formula for the amount the height is increased by is:
293  // radius - sqrt((x2-x1)^2 + (y2-y1)^2) with negative values ignored.
294  //
295  // Rather than iterate over every single point, we can reduce the points
296  // to a rectangle that contains all the positive values for this formula --
297  // the rectangle is given by min_x,max_x,min_y,max_y.
298 
299  // Is this a negative hill? (i.e. a valley)
300  bool is_valley = false;
301 
302  int x1 = island_size > 0 ? center_x - island_size + (rng_()%(island_size*2)) : static_cast<int>(rng_()%width);
303  int y1 = island_size > 0 ? center_y - island_size + (rng_()%(island_size*2)) : static_cast<int>(rng_()%height);
304 
305  // We have to check whether this is actually a valley
306  if(island_size != 0) {
307  const std::size_t diffx = std::abs(x1 - static_cast<int>(center_x));
308  const std::size_t diffy = std::abs(y1 - static_cast<int>(center_y));
309  const std::size_t dist = std::size_t(std::sqrt(static_cast<double>(diffx*diffx + diffy*diffy)));
310  is_valley = dist > island_size;
311  }
312 
313  const int radius = rng_()%hill_size + 1;
314 
315  const int min_x = x1 - radius > 0 ? x1 - radius : 0;
316  const int max_x = x1 + radius < static_cast<long>(res.size()) ? x1 + radius : res.size();
317  const int min_y = y1 - radius > 0 ? y1 - radius : 0;
318  const int max_y = y1 + radius < static_cast<long>(res.front().size()) ? y1 + radius : res.front().size();
319 
320  for(int x2 = min_x; x2 < max_x; ++x2) {
321  for(int y2 = min_y; y2 < max_y; ++y2) {
322  const int xdiff = (x2-x1);
323  const int ydiff = (y2-y1);
324 
325  const int hill_height = radius - static_cast<int>(std::sqrt(static_cast<double>(xdiff*xdiff + ydiff*ydiff)));
326 
327  if(hill_height > 0) {
328  if(is_valley) {
329  if(hill_height > res[x2][y2]) {
330  res[x2][y2] = 0;
331  } else {
332  res[x2][y2] -= hill_height;
333  }
334  } else {
335  res[x2][y2] += hill_height;
336  }
337  }
338  }
339  }
340  }
341 
342  // Find the highest and lowest points on the map for normalization:
343  int highest = 0, lowest = 100000, x;
344  for(x = 0; std::size_t(x) != res.size(); ++x) {
345  for(int y = 0; std::size_t(y) != res[x].size(); ++y) {
346  if(res[x][y] > highest) {
347  highest = res[x][y];
348  }
349 
350  if(res[x][y] < lowest) {
351  lowest = res[x][y];
352  }
353  }
354  }
355 
356  // Normalize the heights to the range 0-1000:
357  highest -= lowest;
358  for(x = 0; std::size_t(x) != res.size(); ++x) {
359  for(int y = 0; std::size_t(y) != res[x].size(); ++y) {
360  res[x][y] -= lowest;
361  res[x][y] *= 1000;
362  if(highest != 0) {
363  res[x][y] /= highest;
364  }
365  }
366  }
367 
368  return res;
369 }
370 
371 /**
372  * Generate a lake.
373  *
374  * It will create water at (x,y), and then have 'lake_fall_off' % chance to
375  * make another water tile in each of the directions n,s,e,w. In each of the
376  * directions it does make another water tile, it will have 'lake_fall_off'/2 %
377  * chance to make another water tile in each of the directions. This will
378  * continue recursively.
379  */
380 bool default_map_generator_job::generate_lake(terrain_map& terrain, int x, int y, int lake_fall_off, std::set<map_location>& locs_touched)
381 {
382  if(x < 0 || y < 0 || x >= terrain.w || y >= terrain.h || lake_fall_off < 0) {
383  return false;
384  }
385  //we checked for this eariler.
386  unsigned int ulake_fall_off = lake_fall_off;
387  terrain[x][y] = t_translation::SHALLOW_WATER;
388  locs_touched.insert(map_location(x,y));
389 
390  if((rng_()%100) < ulake_fall_off) {
391  generate_lake(terrain,x+1,y,lake_fall_off/2,locs_touched);
392  }
393 
394  if((rng_()%100) < ulake_fall_off) {
395  generate_lake(terrain,x-1,y,lake_fall_off/2,locs_touched);
396  }
397 
398  if((rng_()%100) < ulake_fall_off) {
399  generate_lake(terrain,x,y+1,lake_fall_off/2,locs_touched);
400  }
401 
402  if((rng_()%100) < ulake_fall_off) {
403  generate_lake(terrain,x,y-1,lake_fall_off/2,locs_touched);
404  }
405 
406  return true;
407 }
408 
409 /**
410  * River generation.
411  *
412  * Rivers have a source, and then keep on flowing until they meet another body
413  * of water, which they flow into, or until they reach the edge of the map.
414  * Rivers will always flow downhill, except that they can flow a maximum of
415  * 'river_uphill' uphill. This is to represent the water eroding the higher
416  * ground lower.
417  *
418  * Every possible path for a river will be attempted, in random order, and the
419  * first river path that can be found that makes the river flow into another
420  * body of water or off the map will be used.
421  *
422  * If no path can be found, then the river's generation will be aborted, and
423  * false will be returned. true is returned if the river is generated
424  * successfully.
425  */
426 
428  terrain_map& terrain, int x, int y, std::vector<map_location>& river,
429  std::set<map_location>& seen_locations, int river_uphill)
430 {
431  const bool on_map = x >= 0 && y >= 0 &&
432  x < static_cast<long>(heights.size()) &&
433  y < static_cast<long>(heights.back().size());
434 
435  if(on_map && !river.empty() && heights[x][y] >
436  heights[river.back().x][river.back().y] + river_uphill) {
437 
438  return false;
439  }
440 
441  // If we're at the end of the river
442  if(!on_map || terrain[x][y] == t_translation::SHALLOW_WATER ||
443  terrain[x][y] == t_translation::DEEP_WATER) {
444 
445  LOG_NG << "generating river...\n";
446 
447  // Generate the river
448  for(auto i : river) {
449  terrain[i.x][i.y] = t_translation::SHALLOW_WATER;
450  }
451 
452  LOG_NG << "done generating river\n";
453 
454  return true;
455  }
456 
457  map_location current_loc(x,y);
459  get_adjacent_tiles(current_loc,adj.data());
460  std::shuffle(std::begin(adj), std::end(adj), rng_);
461 
462  // Mark that we have attempted from this map_location
463  seen_locations.insert(current_loc);
464  river.push_back(current_loc);
465  for(const map_location& loc : adj) {
466  if(seen_locations.count(loc) == 0) {
467  const bool res = generate_river_internal(heights,terrain,loc.x,loc.y,river,seen_locations,river_uphill);
468  if(res) {
469  return true;
470  }
471 
472  }
473  }
474 
475  river.pop_back();
476 
477  return false;
478 }
479 
480 std::vector<map_location> default_map_generator_job::generate_river(const height_map& heights, terrain_map& terrain, int x, int y, int river_uphill)
481 {
482  std::vector<map_location> river;
483  std::set<map_location> seen_locations;
484  const bool res = generate_river_internal(heights,terrain,x,y,river,seen_locations,river_uphill);
485  if(!res) {
486  river.clear();
487  }
488 
489  return river;
490 }
491 
492 /**
493  * Returns a random tile at one of the borders of a map that is of the given
494  * dimensions.
495  */
496 map_location default_map_generator_job::random_point_at_side(std::size_t width, std::size_t height)
497 {
498  const int side = rng_()%4;
499  if(side < 2) {
500  const int x = rng_()%width;
501  const int y = side == 0 ? 0 : height-1;
502  return map_location(x,y);
503  } else {
504  const int y = rng_()%height;
505  const int x = side == 2 ? 0 : width-1;
506  return map_location(x,y);
507  }
508 }
509 
510 /** Function which, given the map will output it in a valid format. */
511 static std::string output_map(const terrain_map& terrain,
513 {
514  // Remember that we only want the middle 1/9th of the map.
515  // All other segments of the map are there only to give
516  // the important middle part some context.
517  // We also have a border so also adjust for that.
518  const std::size_t begin_x = terrain.w / 3 - gamemap::default_border ;
519  const std::size_t end_x = terrain.w * 2 / 3 + gamemap::default_border;
520  const std::size_t begin_y = terrain.h / 3 - gamemap::default_border;
521  const std::size_t end_y = terrain.h * 2 / 3 + gamemap::default_border;
522 
523  terrain_map map(end_x - begin_x, end_y - begin_y);
524  for(std::size_t y = begin_y; y != end_y; ++y) {
525  for(std::size_t x = begin_x; x != end_x; ++x) {
526  map[x - begin_x][y - begin_y] = terrain[x][y];
527  }
528  }
529 
530  // Since the map has been resized,
531  // the starting locations also need to be fixed
532  for (auto it = starting_positions.left.begin(); it != starting_positions.left.end(); ++it) {
533  starting_positions.left.modify_data(it, [=](t_translation::coordinate& pos) { pos.x -= begin_x; pos.y -= begin_y; });
534  }
535  return t_translation::write_game_map(map, starting_positions);
536 }
537 
538 static int rank_castle_location(int x, int y, const is_valid_terrain& valid_terrain, int min_x, int max_x, int min_y, int max_y, std::size_t min_distance, const std::vector<map_location>& other_castles, int highest_ranking)
539 {
540  const map_location loc(x,y);
541 
542  std::size_t avg_distance = 0, lowest_distance = 1000;
543 
544  for(std::vector<map_location>::const_iterator c = other_castles.begin(); c != other_castles.end(); ++c) {
545  const std::size_t distance = distance_between(loc,*c);
546  if(distance < 6) {
547  return 0;
548  }
549 
550  if(distance < lowest_distance) {
551  lowest_distance = distance;
552  }
553 
554  if(distance < min_distance) {
555  return -1;
556  }
557 
558  avg_distance += distance;
559  }
560 
561  if(!other_castles.empty()) {
562  avg_distance /= other_castles.size();
563  }
564 
565  for(int i = x-1; i <= x+1; ++i) {
566  for(int j = y-1; j <= y+1; ++j) {
567  if(!valid_terrain(i,j)) {
568  return 0;
569  }
570  }
571  }
572 
573  const int x_from_border = std::min<int>(x - min_x,max_x - x);
574  const int y_from_border = std::min<int>(y - min_y,max_y - y);
575 
576  const int border_ranking = min_distance - std::min<int>(x_from_border,y_from_border) + min_distance - x_from_border - y_from_border;
577 
578  int current_ranking = border_ranking*2 + avg_distance*10 + lowest_distance*10;
579  static const int num_nearby_locations = 11*11;
580 
581  const int max_possible_ranking = current_ranking + num_nearby_locations;
582 
583  if(max_possible_ranking < highest_ranking) {
584  return current_ranking;
585  }
586 
587  int surrounding_ranking = 0;
588 
589  for(int xpos = x-5; xpos <= x+5; ++xpos) {
590  for(int ypos = y-5; ypos <= y+5; ++ypos) {
591  if(valid_terrain(xpos,ypos)) {
592  ++surrounding_ranking;
593  }
594  }
595  }
596 
597  return surrounding_ranking + current_ranking;
598 }
599 
600 typedef std::map<t_translation::terrain_code, t_translation::ter_list> tcode_list_cache;
601 
603  const std::size_t x, const std::size_t y, const std::size_t radius, const config& cfg,
604  tcode_list_cache &adj_liked_cache)
605 {
606  const map_location loc(x,y);
607  std::set<map_location> locs;
608  get_tiles_radius(loc,radius,locs);
609  map_location best_loc;
610  int best_rating = 0;
611  for(auto i : locs) {
612  if(i.x < 0 || i.y < 0 || i.x >= map.w ||
613  i.y >= map.h) {
614 
615  continue;
616  }
617 
618  const t_translation::terrain_code t = map[i.x][i.y];
619  const std::string str = t_translation::write_terrain_code(t);
620  if(const config &child = cfg.find_child("village", "terrain", str)) {
621  tcode_list_cache::iterator l = adj_liked_cache.find(t);
622  t_translation::ter_list *adjacent_liked;
623  if(l != adj_liked_cache.end()) {
624  adjacent_liked = &(l->second);
625  } else {
626  adj_liked_cache[t] = t_translation::read_list(child["adjacent_liked"]);
627  adjacent_liked = &(adj_liked_cache[t]);
628  }
629 
630  int rating = child["rating"];
632  get_adjacent_tiles(map_location(i.x,i.y),adj.data());
633  for(std::size_t n = 0; n < adj.size(); ++n) {
634  if(adj[n].x < 0 || adj[n].y < 0 ||
635  adj[n].x >= map.w ||
636  adj[n].y >= map.h) {
637 
638  continue;
639  }
640 
641  const t_translation::terrain_code t2 = map[adj[n].x][adj[n].y];
642  rating += std::count(adjacent_liked->begin(),adjacent_liked->end(),t2);
643  }
644 
645  if(rating > best_rating) {
646  best_loc = map_location(i.x,i.y);
647  best_rating = rating;
648  }
649  }
650  }
651 
652  return best_loc;
653 }
654 
655 // "flood fill" a tile name to adjacent tiles of certain terrain
656 static void flood_name(const map_location& start, const std::string& name, std::map<map_location,std::string>& tile_names,
657  const t_translation::ter_match& tile_types, const terrain_map& terrain,
658  unsigned width, unsigned height,
659  std::size_t label_count, std::map<map_location,std::string>* labels, const std::string& full_name) {
661  get_adjacent_tiles(start,adj.data());
662  std::size_t n;
663  //if adjacent tiles are tiles and unnamed, name them
664  for(n = 0; n < 6; n++) {
665  //we do not care for tiles outside the middle part
666  //cast to unsigned to skip x < 0 || y < 0 as well.
667  if(static_cast<unsigned>(adj[n].x) >= width / 3 || static_cast<unsigned>(adj[n].y) >= height / 3) {
668  continue;
669  }
670 
671  const t_translation::terrain_code terr = terrain[adj[n].x + (width / 3)][adj[n].y + (height / 3)];
672  const map_location loc(adj[n].x, adj[n].y);
673  if((t_translation::terrain_matches(terr, tile_types)) && (tile_names.find(loc) == tile_names.end())) {
674  tile_names.emplace(loc, name);
675  //labeling decision: this is result of trial and error on what looks best in game
676  if(label_count % 6 == 0) { //ensure that labels do not occur more often than every 6 recursions
677  labels->emplace(loc, full_name);
678  label_count++; //ensure that no adjacent tiles get labeled
679  }
680  flood_name(adj[n], name, tile_names, tile_types, terrain, width, height, label_count++, labels, full_name);
681  }
682  }
683 }
684 
685 std::string default_map_generator_job::default_generate_map(generator_data data, std::map<map_location,std::string>* labels, const config& cfg)
686 {
687  log_scope("map generation");
688 
689  // Odd widths are nasty
690  VALIDATE(is_even(data.width), _("Random maps with an odd width aren't supported."));
691 
692  // Try to find configuration for castles
693  const config& castle_config = cfg.child("castle");
694 
695  int ticks = SDL_GetTicks();
696 
697  // We want to generate a map that is 9 times bigger than the actual size desired.
698  // Only the middle part of the map will be used, but the rest is so that the map we
699  // end up using can have a context (e.g. rivers flowing from out of the map into the map,
700  // same for roads, etc.)
701  data.width *= 3;
702  data.height *= 3;
703 
704  config naming;
705 
706  if(cfg.has_child("naming")) {
707  naming = game_config_.child("naming");
708  naming.append_attributes(cfg.child("naming"));
709  }
710 
711  // If the [naming] child is empty, we cannot provide good names.
712  std::map<map_location,std::string>* misc_labels = naming.empty() ? nullptr : labels;
713 
714  std::shared_ptr<name_generator>
715  base_name_generator, river_name_generator, lake_name_generator,
716  road_name_generator, bridge_name_generator, mountain_name_generator,
717  forest_name_generator, swamp_name_generator;
718 
719  if(misc_labels != nullptr) {
720  name_generator_factory base_generator_factory{ naming, {"male", "base", "bridge", "road", "river", "forest", "lake", "mountain", "swamp"} };
721 
722  naming.get_old_attribute("base_names", "male_names", "naming");
723  //Due to the attribute detection feature of the factory we also support male_name_generator= but keep it undocumented.
724 
725  base_name_generator = base_generator_factory.get_name_generator( (naming.has_attribute("base_names") || naming.has_attribute("base_name_generator")) ? "base" : "male" );
726  river_name_generator = base_generator_factory.get_name_generator("river");
727  lake_name_generator = base_generator_factory.get_name_generator("lake");
728  road_name_generator = base_generator_factory.get_name_generator("road");
729  bridge_name_generator = base_generator_factory.get_name_generator("bridge");
730  mountain_name_generator = base_generator_factory.get_name_generator("mountain");
731  forest_name_generator = base_generator_factory.get_name_generator("forest");
732  swamp_name_generator = base_generator_factory.get_name_generator("swamp");
733  }
734 
735  // Generate the height of everything.
736  const height_map heights = generate_height_map(data.width, data.height, data.iterations, data.hill_size, data.island_size, data.island_off_center);
737 
738  LOG_NG << "Done generating height map. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
739  ticks = SDL_GetTicks();
740 
741  // Find out what the 'flatland' on this map is, i.e. grassland.
742  std::string flatland = cfg["default_flatland"];
743  if(flatland.empty()) {
745  }
746 
748 
749  std::vector<terrain_height_mapper> height_conversion;
750  for(const config& h : cfg.child_range("height")) {
751  height_conversion.emplace_back(h);
752  }
753 
754  terrain_map terrain(data.width, data.height, grassland);
755  for(std::size_t x = 0; x != heights.size(); ++x) {
756  for(std::size_t y = 0; y != heights[x].size(); ++y) {
757  for(auto i : height_conversion) {
758  if(i.convert_terrain(heights[x][y])) {
759  terrain[x][y] = i.convert_to();
760  break;
761  }
762  }
763  }
764  }
765 
767  LOG_NG << output_map(terrain, starting_positions);
768  LOG_NG << "Placed landforms. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
769  ticks = SDL_GetTicks();
770 
771  /* Now that we have our basic set of flatland/hills/mountains/water,
772  * we can place lakes and rivers on the map.
773  * All rivers are sourced at a lake.
774  * Lakes must be in high land - at least 'min_lake_height'.
775  * (Note that terrain below a certain altitude may be made into bodies of water
776  * in the code above - i.e. 'sea', but these are not considered 'lakes',
777  * because they are not sources of rivers).
778  *
779  * We attempt to place 'max_lakes' lakes.
780  * Each lake will be placed at a random location, if that random location meets theminimum
781  * terrain requirements for a lake. We will also attempt to source a river from each lake.
782  */
783  std::set<map_location> lake_locs;
784 
785  std::map<map_location, std::string> river_names, lake_names, road_names, bridge_names, mountain_names, forest_names, swamp_names;
786 
787  const std::size_t nlakes = data.max_lakes > 0 ? (rng_()%data.max_lakes) : 0;
788  for(std::size_t lake = 0; lake != nlakes; ++lake) {
789  for(int tries = 0; tries != 100; ++tries) {
790  const int x = rng_()%data.width;
791  const int y = rng_()%data.height;
792 
793  if(heights[x][y] <= cfg["min_lake_height"].to_int()) {
794  continue;
795  }
796 
797  std::vector<map_location> river = generate_river(heights, terrain, x, y, cfg["river_frequency"]);
798 
799  if(!river.empty() && misc_labels != nullptr) {
800  const std::string base_name = base_name_generator->generate();
801  const std::string& name = river_name_generator->generate({{"base", base_name}});
802  LOG_NG << "Named river '" << name << "'\n";
803 
804  std::size_t name_frequency = 20;
805  for(std::vector<map_location>::const_iterator r = river.begin(); r != river.end(); ++r) {
806  const map_location loc(r->x-data.width/3,r->y-data.height/3);
807 
808  if(((r - river.begin())%name_frequency) == name_frequency/2) {
809  misc_labels->emplace(loc, name);
810  }
811 
812  river_names.emplace(loc, base_name);
813  }
814  }
815 
816  LOG_NG << "Generating lake...\n";
817 
818  std::set<map_location> locs;
819  if(generate_lake(terrain, x, y, cfg["lake_size"], locs) && misc_labels != nullptr) {
820  bool touches_other_lake = false;
821 
822  std::string base_name = base_name_generator->generate();
823  const std::string& name = lake_name_generator->generate({{"base", base_name}});
824 
825  // Only generate a name if the lake hasn't touched any other lakes,
826  // so that we don't end up with one big lake with multiple names.
827  for(auto i : locs) {
828  if(lake_locs.count(i) != 0) {
829  touches_other_lake = true;
830 
831  // Reassign the name of this lake to be the same as the other lake
832  const map_location loc(i.x-data.width/3,i.y-data.height/3);
833  const std::map<map_location,std::string>::const_iterator other_name = lake_names.find(loc);
834  if(other_name != lake_names.end()) {
835  base_name = other_name->second;
836  }
837  }
838 
839  lake_locs.insert(i);
840  }
841 
842  if(!touches_other_lake) {
843  const map_location loc(x-data.width/3,y-data.height/3);
844  misc_labels->erase(loc);
845  misc_labels->emplace(loc, name);
846  }
847 
848  for(auto i : locs) {
849  const map_location loc(i.x-data.width/3,i.y-data.height/3);
850  lake_names.emplace(loc, base_name);
851  }
852  }
853 
854  break;
855  }
856  }
857 
858  LOG_NG << "Generated rivers. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
859  ticks = SDL_GetTicks();
860 
861  const std::size_t default_dimensions = 40*40*9;
862 
863  /*
864  * Convert grassland terrain to other types of flat terrain.
865  *
866  * We generate a 'temperature map' which uses the height generation
867  * algorithm to generate the temperature levels all over the map. Then we
868  * can use a combination of height and terrain to divide terrain up into
869  * more interesting types than the default.
870  */
871  const height_map temperature_map = generate_height_map(data.width,data.height,
872  cfg["temperature_iterations"].to_int() * data.width * data.height / default_dimensions,
873  cfg["temperature_size"], 0, 0);
874 
875  LOG_NG << "Generated temperature map. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
876  ticks = SDL_GetTicks();
877 
878  std::vector<terrain_converter> converters;
879  for(const config& cv : cfg.child_range("convert")) {
880  converters.emplace_back(cv);
881  }
882 
883  LOG_NG << "Created terrain converters. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
884  ticks = SDL_GetTicks();
885 
886  // Iterate over every flatland tile, and determine what type of flatland it is, based on our [convert] tags.
887  for(int x = 0; x != data.width; ++x) {
888  for(int y = 0; y != data.height; ++y) {
889  for(auto i : converters) {
890  if(i.convert_terrain(terrain[x][y],heights[x][y],temperature_map[x][y])) {
891  terrain[x][y] = i.convert_to();
892  break;
893  }
894  }
895  }
896  }
897 
898  LOG_NG << "Placing castles...\n";
899 
900  /*
901  * Attempt to place castles at random.
902  *
903  * After they are placed, we run a sanity check to make sure no two castles
904  * are closer than 'min_distance' hexes apart, and that they appear on a
905  * terrain listed in 'valid_terrain'.
906  *
907  * If not, we attempt to place them again.
908  */
909  std::vector<map_location> castles;
910  std::set<map_location> failed_locs;
911 
912  if(castle_config) {
913  /*
914  * Castle configuration tag contains a 'valid_terrain' attribute which is a
915  * list of terrains that the castle may appear on.
916  */
917  const t_translation::ter_list list = t_translation::read_list(castle_config["valid_terrain"]);
918 
919  const is_valid_terrain terrain_tester(terrain, list);
920 
921  for(int player = 0; player != data.nplayers; ++player) {
922  LOG_NG << "placing castle for " << player << "\n";
923  lg::scope_logger inner_scope_logging_object__(lg::general(), "placing castle");
924  const int min_x = data.width/3 + 3;
925  const int min_y = data.height/3 + 3;
926  const int max_x = (data.width/3)*2 - 4;
927  const int max_y = (data.height/3)*2 - 4;
928  int min_distance = castle_config["min_distance"];
929 
930  map_location best_loc;
931  int best_ranking = 0;
932  for(int x = min_x; x != max_x; ++x) {
933  for(int y = min_y; y != max_y; ++y) {
934  const map_location loc(x,y);
935  if(failed_locs.count(loc)) {
936  continue;
937  }
938 
939  const int ranking = rank_castle_location(x, y, terrain_tester, min_x, max_x, min_y, max_y, min_distance, castles, best_ranking);
940  if(ranking <= 0) {
941  failed_locs.insert(loc);
942  }
943 
944  if(ranking > best_ranking) {
945  best_ranking = ranking;
946  best_loc = loc;
947  }
948  }
949  }
950 
951  if(best_ranking == 0) {
952  ERR_NG << "No castle location found, aborting." << std::endl;
953  const std::string error = _("No valid castle location found. Too many or too few mountain hexes? (please check the 'max hill size' parameter)");
954  throw mapgen_exception(error);
955  }
956 
957  assert(std::find(castles.begin(), castles.end(), best_loc) == castles.end());
958  castles.push_back(best_loc);
959 
960  // Make sure the location can't get a second castle.
961  failed_locs.insert(best_loc);
962  }
963 
964  LOG_NG << "Placed castles. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
965  }
966  LOG_NG << "Placing roads...\n";
967  ticks = SDL_GetTicks();
968 
969  // Place roads.
970  // We select two tiles at random locations on the borders of the map
971  // and try to build roads between them.
972  int nroads = cfg["roads"];
973  if(data.link_castles) {
974  nroads += castles.size()*castles.size();
975  }
976 
977  std::set<map_location> bridges;
978 
979  road_path_calculator calc(terrain, cfg, rng_());
980  for(int road = 0; road != nroads; ++road) {
981  lg::scope_logger another_inner_scope_logging_object__(lg::general(), "creating road");
982 
983  /*
984  * We want the locations to be on the portion of the map we're actually
985  * going to use, since roads on other parts of the map won't have any
986  * influence, and doing it like this will be quicker.
987  */
988  map_location src = random_point_at_side(data.width/3 + 2,data.height/3 + 2);
989  map_location dst = random_point_at_side(data.width/3 + 2,data.height/3 + 2);
990 
991  src.x += data.width/3 - 1;
992  src.y += data.height/3 - 1;
993  dst.x += data.width/3 - 1;
994  dst.y += data.height/3 - 1;
995 
996  if(data.link_castles && road < static_cast<int>(castles.size() * castles.size())) {
997  const std::size_t src_castle = road/castles.size();
998  const std::size_t dst_castle = road%castles.size();
999  if(src_castle >= dst_castle) {
1000  continue;
1001  }
1002 
1003  src = castles[src_castle];
1004  dst = castles[dst_castle];
1005  } else if(src.x == dst.x || src.y == dst.y) {
1006  // If the road isn't very interesting (on the same border), don't draw it.
1007  continue;
1008  }
1009 
1010  if(calc.cost(src, 0.0) >= 1000.0 || calc.cost(dst, 0.0) >= 1000.0) {
1011  continue;
1012  }
1013 
1014  // Search a path out for the road
1015  pathfind::plain_route rt = pathfind::a_star_search(src, dst, 10000.0, calc, data.width, data.height);
1016 
1017  const std::string& road_base_name = misc_labels != nullptr
1018  ? base_name_generator->generate()
1019  : "";
1020  const std::string& road_name = misc_labels != nullptr
1021  ? road_name_generator->generate({{"base", road_base_name}})
1022  : "";
1023  const int name_frequency = 20;
1024  int name_count = 0;
1025 
1026  bool on_bridge = false;
1027 
1028  // Draw the road.
1029  // If the search failed, rt.steps will simply be empty.
1030  for(std::vector<map_location>::const_iterator step = rt.steps.begin();
1031  step != rt.steps.end(); ++step) {
1032 
1033  const int x = step->x;
1034  const int y = step->y;
1035 
1036  if(x < 0 || y < 0 || x >= static_cast<long>(data.width) || y >= static_cast<long>(data.height)) {
1037  continue;
1038  }
1039 
1040  // Find the configuration which tells us what to convert this tile to, to make it into a road.
1041  const config& child = cfg.find_child("road_cost", "terrain", t_translation::write_terrain_code(terrain[x][y]));
1042  if(child.empty()){
1043  continue;
1044  }
1045 
1046  /* Convert to bridge means that we want to convert depending on the direction of the road.
1047  * Typically it will be in a format like convert_to_bridge = \,|,/
1048  * '|' will be used if the road is going north-south
1049  * '/' will be used if the road is going south west-north east
1050  * '\' will be used if the road is going south east-north west
1051  * The terrain will be left unchanged otherwise (if there is no clear direction).
1052  */
1053  const std::string& convert_to_bridge = child["convert_to_bridge"];
1054  if(!convert_to_bridge.empty()) {
1055  if(step == rt.steps.begin() || step+1 == rt.steps.end()) {
1056  continue;
1057  }
1058 
1059  const map_location& last = *(step-1);
1060  const map_location& next = *(step+1);
1061 
1063  get_adjacent_tiles(*step,adj.data());
1064 
1065  int direction = -1;
1066 
1067  // If we are going north-south
1068  if((last == adj[0] && next == adj[3]) || (last == adj[3] && next == adj[0])) {
1069  direction = 0;
1070  }
1071 
1072  // If we are going south west-north east
1073  else if((last == adj[1] && next == adj[4]) || (last == adj[4] && next == adj[1])) {
1074  direction = 1;
1075  }
1076 
1077  // If we are going south east-north west
1078  else if((last == adj[2] && next == adj[5]) || (last == adj[5] && next == adj[2])) {
1079  direction = 2;
1080  }
1081 
1082  if(misc_labels != nullptr && !on_bridge) {
1083  on_bridge = true;
1084  std::string bridge_base_name = base_name_generator->generate();
1085  const std::string& name = bridge_name_generator->generate({{"base", bridge_base_name}});
1086  const map_location loc(x - data.width / 3, y-data.height/3);
1087  misc_labels->emplace(loc, name);
1088  bridge_names.emplace(loc, bridge_base_name); //add to use for village naming
1089  bridges.insert(loc);
1090  }
1091 
1092  if(direction != -1) {
1093  const std::vector<std::string> items = utils::split(convert_to_bridge);
1094  if(std::size_t(direction) < items.size() && !items[direction].empty()) {
1095  terrain[x][y] = t_translation::read_terrain_code(items[direction]);
1096  }
1097 
1098  continue;
1099  }
1100  } else {
1101  on_bridge = false;
1102  }
1103 
1104  // Just a plain terrain substitution for a road
1105  const std::string& convert_to = child["convert_to"];
1106  if(!convert_to.empty()) {
1108  if(misc_labels != nullptr && terrain[x][y] != letter && name_count++ == name_frequency && !on_bridge) {
1109  misc_labels->emplace(map_location(x - data.width / 3, y - data.height / 3), road_name);
1110  name_count = 0;
1111  }
1112 
1113  terrain[x][y] = letter;
1114  if(misc_labels != nullptr) {
1115  const map_location loc(x - data.width / 3, y - data.height / 3); //add to use for village naming
1116  if(!road_base_name.empty())
1117  road_names.emplace(loc, road_base_name);
1118  }
1119  }
1120  }
1121  }
1122 
1123  // Now that road drawing is done, we can plonk down the castles.
1124  for(std::vector<map_location>::const_iterator c = castles.begin(); c != castles.end(); ++c) {
1125  if(!c->valid()) {
1126  continue;
1127  }
1128 
1129  const int x = c->x;
1130  const int y = c->y;
1131  const int player = c - castles.begin() + 1;
1132  const t_translation::coordinate coord(x, y);
1133  starting_positions.insert(t_translation::starting_positions::value_type(std::to_string(player), coord));
1134  terrain[x][y] = t_translation::HUMAN_KEEP;
1135 
1136  const int castle_array[13][2] {
1137  {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {0, 1}, {-1, 1},
1138  {-2, 1}, {-2, 0}, {-2, -1}, {-1, -2}, {0, -2}, {1, -2}
1139  };
1140 
1141  for(int i = 0; i < data.castle_size - 1; i++) {
1142  terrain[x+ castle_array[i][0]][y+ castle_array[i][1]] = t_translation::HUMAN_CASTLE;
1143  }
1144 
1145  // Remove all labels under the castle tiles
1146  if(labels != nullptr) {
1147  labels->erase(map_location(x-data.width/3,y-data.height/3));
1148  for(int i = 0; i < data.castle_size - 1; i++) {
1149  labels->erase(map_location(x+ castle_array[i][0]-data.width/3, y+ castle_array[i][1]-data.height/3));
1150  }
1151  }
1152  }
1153 
1154  LOG_NG << "Placed roads. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
1155  ticks = SDL_GetTicks();
1156 
1157  /* Random naming for landforms: mountains, forests, swamps, hills
1158  * we name these now that everything else is placed (as e.g., placing
1159  * roads could split a forest)
1160  */
1161  if(misc_labels != nullptr) {
1162  for(int x = data.width / 3; x < (data.width / 3)*2; x++) {
1163  for(int y = data.height / 3; y < (data.height / 3) * 2;y++) {
1164  //check the terrain of the tile
1165  const map_location loc(x - data.width / 3, y - data.height / 3);
1166  const t_translation::terrain_code terr = terrain[x][y];
1167  std::string name, base_name;
1168  std::set<std::string> used_names;
1169 
1171  //name every 15th mountain
1172  if((rng_() % 15) == 0) {
1173  for(std::size_t ntry = 0; ntry != 30 && (ntry == 0 || used_names.count(name) > 0); ++ntry) {
1174  base_name = base_name_generator->generate();
1175  name = mountain_name_generator->generate({{"base", base_name}});
1176  }
1177  misc_labels->emplace(loc, name);
1178  mountain_names.emplace(loc, base_name);
1179  }
1181  // If the forest tile is not named yet, name it
1182  const std::map<map_location, std::string>::const_iterator forest_name = forest_names.find(loc);
1183  if(forest_name == forest_names.end()) {
1184  for(std::size_t ntry = 0; ntry != 30 && (ntry == 0 || used_names.count(name) > 0); ++ntry) {
1185  base_name = base_name_generator->generate();
1186  name = forest_name_generator->generate({{"base", base_name}});
1187  }
1188  forest_names.emplace(loc, base_name);
1189  // name all connected forest tiles accordingly
1190  flood_name(loc, base_name, forest_names, t_translation::ALL_FORESTS, terrain, data.width, data.height, 0, misc_labels, name);
1191  }
1193  // If the swamp tile is not named yet, name it
1194  const std::map<map_location, std::string>::const_iterator swamp_name = swamp_names.find(loc);
1195  if(swamp_name == swamp_names.end()) {
1196  for(std::size_t ntry = 0; ntry != 30 && (ntry == 0 || used_names.count(name) > 0); ++ntry) {
1197  base_name = base_name_generator->generate();
1198  name = swamp_name_generator->generate({{"base", base_name}});
1199  }
1200  swamp_names.emplace(loc, base_name);
1201  // name all connected swamp tiles accordingly
1202  flood_name(loc, base_name, swamp_names, t_translation::ALL_SWAMPS, terrain, data.width, data.height, 0, misc_labels, name);
1203  }
1204  }
1205  }
1206  }
1207  }
1208 
1209  LOG_NG << "Named landforms. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
1210  LOG_NG << "Placing villages...\n";
1211  ticks = SDL_GetTicks();
1212 
1213  /*
1214  * Place villages in a 'grid', to make placing fair, but with villages
1215  * displaced from their position according to terrain and randomness, to
1216  * add some variety.
1217  */
1218  std::set<map_location> villages;
1219 
1220  if(data.nvillages > 0) {
1221 
1222  // First we work out the size of the x and y distance between villages
1223  const std::size_t tiles_per_village = ((data.width*data.height)/9)/data.nvillages;
1224  std::size_t village_x = 1, village_y = 1;
1225 
1226  // Alternate between incrementing the x and y value.
1227  // When they are high enough to equal or exceed the tiles_per_village,
1228  // then we have them to the value we want them at.
1229  while(village_x*village_y < tiles_per_village) {
1230  if(village_x < village_y) {
1231  ++village_x;
1232  } else {
1233  ++village_y;
1234  }
1235  }
1236 
1237  std::set<std::string> used_names;
1238  tcode_list_cache adj_liked_cache;
1239 
1240  config village_naming = game_config_.child("village_naming");
1241 
1242  if(cfg.has_child("village_naming")) {
1243  village_naming.append_attributes(cfg.child("village_naming"));
1244  }
1245 
1246  // If the [village_naming] child is empty, we cannot provide good names.
1247  std::map<map_location,std::string>* village_labels = village_naming.empty() ? nullptr : labels;
1248 
1249  for(int vx = 0; vx < data.width; vx += village_x) {
1250  LOG_NG << "village at " << vx << "\n";
1251 
1252  for(int vy = rng_()%village_y; vy < data.height; vy += village_y) {
1253  const std::size_t add = rng_()%3;
1254  const std::size_t x = (vx + add) - 1;
1255  const std::size_t y = (vy + add) - 1;
1256 
1257  const map_location res = place_village(terrain, x, y, 2, cfg, adj_liked_cache);
1258 
1259  if(res.x < static_cast<long>(data.width ) / 3 ||
1260  res.x >= static_cast<long>(data.width * 2) / 3 ||
1261  res.y < static_cast<long>(data.height ) / 3 ||
1262  res.y >= static_cast<long>(data.height * 2) / 3) {
1263  continue;
1264  }
1265 
1266  const std::string str = t_translation::write_terrain_code(terrain[res.x][res.y]);
1267 
1268  const std::string& convert_to = cfg.find_child("village", "terrain", str)["convert_to"].str();
1269  if(convert_to.empty()) {
1270  continue;
1271  }
1272 
1273  terrain[res.x][res.y] = t_translation::read_terrain_code(convert_to);
1274 
1275  villages.insert(res);
1276 
1277  if(village_labels == nullptr) {
1278  continue;
1279  }
1280 
1281  name_generator_factory village_name_generator_factory{ village_naming,
1282  {"base", "male", "village", "lake", "river", "bridge", "grassland", "forest", "hill", "mountain", "mountain_anon", "road", "swamp"} };
1283 
1284  village_naming.get_old_attribute("base_names", "male_names", "village_naming");
1285  //Due to the attribute detection feature of the factory we also support male_name_generator= but keep it undocumented.
1286 
1287  base_name_generator = village_name_generator_factory.get_name_generator(
1288  (village_naming.has_attribute("base_names") || village_naming.has_attribute("base_name_generator")) ? "base" : "male" );
1289 
1290  const map_location loc(res.x-data.width/3,res.y-data.height/3);
1291 
1293  get_adjacent_tiles(loc,adj.data());
1294 
1295  std::string name_type = "village";
1301 
1302  std::size_t field_count = 0, forest_count = 0, mountain_count = 0, hill_count = 0;
1303 
1304  std::map<std::string,std::string> symbols;
1305 
1306  std::size_t n;
1307  for(n = 0; n != 6; ++n) {
1308  const std::map<map_location,std::string>::const_iterator road_name = road_names.find(adj[n]);
1309  if(road_name != road_names.end()) {
1310  symbols["road"] = road_name->second;
1311  name_type = "road";
1312  break;
1313  }
1314 
1315  const std::map<map_location,std::string>::const_iterator river_name = river_names.find(adj[n]);
1316  if(river_name != river_names.end()) {
1317  symbols["river"] = river_name->second;
1318  name_type = "river";
1319 
1320  const std::map<map_location,std::string>::const_iterator bridge_name = bridge_names.find(adj[n]);
1321  if(bridge_name != bridge_names.end()) {
1322  //we should always end up here, since if there is an adjacent bridge, there has to be an adjacent river too
1323  symbols["bridge"] = bridge_name->second;
1324  name_type = "river_bridge";
1325  }
1326 
1327  break;
1328  }
1329 
1330  const std::map<map_location,std::string>::const_iterator forest_name = forest_names.find(adj[n]);
1331  if(forest_name != forest_names.end()) {
1332  symbols["forest"] = forest_name->second;
1333  name_type = "forest";
1334  break;
1335  }
1336 
1337  const std::map<map_location,std::string>::const_iterator lake_name = lake_names.find(adj[n]);
1338  if(lake_name != lake_names.end()) {
1339  symbols["lake"] = lake_name->second;
1340  name_type = "lake";
1341  break;
1342  }
1343 
1344  const std::map<map_location,std::string>::const_iterator mountain_name = mountain_names.find(adj[n]);
1345  if(mountain_name != mountain_names.end()) {
1346  symbols["mountain"] = mountain_name->second;
1347  name_type = "mountain";
1348  break;
1349  }
1350 
1351  const std::map<map_location,std::string>::const_iterator swamp_name = swamp_names.find(adj[n]);
1352  if(swamp_name != swamp_names.end()) {
1353  symbols["swamp"] = swamp_name->second;
1354  name_type = "swamp";
1355  break;
1356  }
1357 
1358  const t_translation::terrain_code terr = terrain[adj[n].x+data.width/3][adj[n].y+data.height/3];
1359 
1360  if(std::count(field.begin(),field.end(),terr) > 0) {
1361  ++field_count;
1362  } else if(std::count(forest.begin(),forest.end(),terr) > 0) {
1363  ++forest_count;
1364  } else if(std::count(hill.begin(),hill.end(),terr) > 0) {
1365  ++hill_count;
1366  } else if(std::count(mountain.begin(),mountain.end(),terr) > 0) {
1367  ++mountain_count;
1368  }
1369  }
1370 
1371  if(n == 6) {
1372  if(field_count == 6) {
1373  name_type = "grassland";
1374  } else if(forest_count >= 2) {
1375  name_type = "forest";
1376  } else if(mountain_count >= 1) {
1377  name_type = "mountain_anon";
1378  } else if(hill_count >= 2) {
1379  name_type = "hill";
1380  }
1381  }
1382 
1383  std::string name;
1384 
1385  symbols["base"] = base_name_generator->generate();
1386  std::shared_ptr<name_generator> village_name_generator = village_name_generator_factory.get_name_generator(name_type);
1387 
1388  for(std::size_t ntry = 0; ntry != 30 && (ntry == 0 || used_names.count(name) > 0); ++ntry) {
1389  name = village_name_generator->generate(symbols);
1390  }
1391 
1392  used_names.insert(name);
1393  village_labels->emplace(loc, name);
1394  }
1395  }
1396  }
1397 
1398  LOG_NG << "Placed villages. " << (SDL_GetTicks() - ticks) << " ticks elapsed" << "\n";
1399 
1400  return output_map(terrain, starting_positions);
1401 }
static map_location place_village(const t_translation::ter_map &map, const std::size_t x, const std::size_t y, const std::size_t radius, const config &cfg, tcode_list_cache &adj_liked_cache)
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:423
void append_attributes(const config &cfg)
Adds attributes from cfg.
Definition: config.cpp:274
const terrain_code NONE_TERRAIN
Definition: translation.hpp:58
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.cpp:517
ter_list read_list(const std::string &str, const ter_layer filler)
Reads a list of terrains from a string, when reading the.
const terrain_code FOREST
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate > > starting_positions
config & find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:789
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
#define ERR_NG
map_location random_point_at_side(std::size_t width, std::size_t height)
Returns a random tile at one of the borders of a map that is of the given dimensions.
bool terrain_matches(const terrain_code &src, const terrain_code &dest)
Tests whether a specific terrain matches an expression, for matching rules see above.
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
bool has_attribute(config_key_type key) const
Definition: config.cpp:217
#define a
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:416
child_itors child_range(config_key_type key)
Definition: config.cpp:366
static const int default_border
The default border style for a map.
Definition: map.hpp:180
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
const attribute_value & get_old_attribute(config_key_type key, const std::string &old_key, const std::string &in_tag="") const
Function to handle backward compatibility Get the value of key and if missing try old_key and log msg...
Definition: config.cpp:715
#define h
const std::vector< std::string > items
const terrain_code HUMAN_KEEP
const terrain_code HILL
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
const ter_match ALL_MOUNTAINS("!,*^V*,!,M*")
bool generate_river_internal(const height_map &heights, terrain_map &terrain, int x, int y, std::vector< map_location > &river, std::set< map_location > &seen_locations, int river_uphill)
River generation.
static double getNoPathValue()
Definition: pathfind.hpp:63
#define b
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
static int rank_castle_location(int x, int y, const is_valid_terrain &valid_terrain, int min_x, int max_x, int min_y, int max_y, std::size_t min_distance, const std::vector< map_location > &other_castles, int highest_ranking)
const terrain_code MOUNTAIN
std::vector< map_location > steps
Definition: pathfind.hpp:134
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
bool is_even(T num)
Definition: math.hpp:31
static void field(LexState *ls, struct ConsControl *cc)
Definition: lparser.cpp:702
std::array< map_location, 6 > adjacent_loc_array_t
Definition: location.hpp:170
static std::string output_map(const terrain_map &terrain, t_translation::starting_positions &starting_positions)
Function which, given the map will output it in a valid format.
log_domain & general()
Definition: log.cpp:104
std::string write_terrain_code(const terrain_code &tcode)
Writes a single terrain code to a string.
const terrain_code HUMAN_CASTLE
Encapsulates the map of the game.
Definition: location.hpp:42
std::string default_generate_map(generator_data data, std::map< map_location, std::string > *labels, const config &cfg)
Generate the map.
#define LOG_NG
static lg::log_domain log_mapgen("mapgen")
std::size_t i
Definition: function.cpp:933
static void flood_name(const map_location &start, const std::string &name, std::map< map_location, std::string > &tile_names, const t_translation::ter_match &tile_types, const terrain_map &terrain, unsigned width, unsigned height, std::size_t label_count, std::map< map_location, std::string > *labels, const std::string &full_name)
const ter_match ALL_SWAMPS("!,*^V*,*^B*,!,S*")
Game configuration data as global variables.
Definition: build_info.cpp:46
EXIT_STATUS start(const config &game_conf, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition: editor_main.cpp:28
height_map generate_height_map(std::size_t width, std::size_t height, std::size_t iterations, std::size_t hill_size, std::size_t island_size, std::size_t island_off_center)
Generate a height-map.
void get_tiles_radius(const map_location &center, std::size_t radius, std::set< map_location > &result)
Function that will add to result all locations within radius tiles of center (including center itself...
Definition: pathutils.cpp:68
static bool is_valid_terrain(const t_translation::terrain_code &c)
#define log_scope(description)
Definition: log.hpp:186
CURSOR_TYPE get()
Definition: cursor.cpp:213
t_translation::ter_map terrain_map
std::string write_game_map(const ter_map &map, const starting_positions &starting_positions, coordinate border_offset)
Write a gamemap in to a vector string.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
const terrain_code DEEP_WATER
#define next(ls)
Definition: llex.cpp:32
std::size_t distance_between(const map_location &a, const map_location &b)
Function which gives the number of hexes between two tiles (i.e.
Definition: location.cpp:600
uint32_t next_seed()
Definition: seed_rng.cpp:45
std::vector< std::vector< int > > height_map
double t
Definition: astarsearch.cpp:63
virtual double cost(const map_location &loc, const double so_far) const =0
Standard logging facilities (interface).
std::vector< terrain_code > ter_list
Definition: translation.hpp:77
const terrain_code SHALLOW_WATER
std::map< t_translation::terrain_code, t_translation::ter_list > tcode_list_cache
std::vector< game_tip > shuffle(const std::vector< game_tip > &tips)
Shuffles the tips.
Definition: tips.cpp:46
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const std::size_t width, const std::size_t height, const teleport_map *teleports, bool border)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
static map_location::DIRECTION n
This module contains various pathfinding functions and utilities.
This structure can be used for matching terrain strings.
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
bool empty() const
Definition: config.cpp:837
const terrain_code GRASS_LAND
std::vector< map_location > generate_river(const height_map &heights, terrain_map &terrain, int x, int y, int river_uphill)
terrain_code read_terrain_code(const std::string &str, const ter_layer filler)
Reads a single terrain from a string.
std::vector< std::vector< int > > height_map
const ter_match ALL_FORESTS
bool generate_lake(t_translation::ter_map &terrain, int x, int y, int lake_fall_off, std::set< map_location > &locs_touched)
Generate a lake.