Freeciv21
Develop your civilization from humble roots to a global empire
map.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 #include <cmath> // sqrt
16 
17 // utility
18 #include "bitvector.h"
19 #include "iterator.h"
20 #include "log.h" // fc_assert
21 
22 // common
23 #include "game.h"
24 #include "map_types.h"
25 
26 // Parameters for terrain counting functions.
27 static const bool C_ADJACENT = false;
28 static const bool C_CARDINAL = true;
29 static const bool C_NUMBER = false;
30 static const bool C_PERCENT = true;
31 
32 #define MAP_IS_ISOMETRIC (CURRENT_TOPOLOGY & (TF_ISO + TF_HEX))
33 
34 #define CURRENT_TOPOLOGY (wld.map.topology_id)
35 
36 #define topo_has_flag(topo, flag) (((topo) & (flag)) != 0)
37 #define current_topo_has_flag(flag) topo_has_flag((CURRENT_TOPOLOGY), (flag))
38 
39 #define ALL_DIRECTIONS_CARDINAL() topo_has_flag((CURRENT_TOPOLOGY), TF_HEX)
40 
41 bool map_is_empty();
42 void map_init(struct civ_map *imap, bool server_side);
43 void map_init_topology();
44 void map_allocate(struct civ_map *amap);
45 void main_map_allocate();
46 void map_free(struct civ_map *fmap);
47 void main_map_free();
48 
49 int map_vector_to_real_distance(int dx, int dy);
50 int map_vector_to_sq_distance(int dx, int dy);
51 int map_distance(const struct tile *tile0, const struct tile *tile1);
52 int real_map_distance(const struct tile *tile0, const struct tile *tile1);
53 int sq_map_distance(const struct tile *tile0, const struct tile *tile1);
54 
55 bool same_pos(const struct tile *tile0, const struct tile *tile1);
56 bool base_get_direction_for_step(const struct civ_map *nmap,
57  const struct tile *src_tile,
58  const struct tile *dst_tile,
59  enum direction8 *dir);
60 int get_direction_for_step(const struct civ_map *nmap,
61  const struct tile *src_tile,
62  const struct tile *dst_tile);
63 
64 int startpos_number(const struct startpos *psp);
65 
66 bool startpos_allow(struct startpos *psp, struct nation_type *pnation);
67 bool startpos_disallow(struct startpos *psp, struct nation_type *pnation);
68 
69 struct tile *startpos_tile(const struct startpos *psp);
70 bool startpos_nation_allowed(const struct startpos *psp,
71  const struct nation_type *pnation);
72 bool startpos_allows_all(const struct startpos *psp);
73 
74 bool startpos_pack(const struct startpos *psp,
75  struct packet_edit_startpos_full *packet);
76 bool startpos_unpack(struct startpos *psp,
77  const struct packet_edit_startpos_full *packet);
78 
79 /* See comment in "common/map.c". */
80 bool startpos_is_excluding(const struct startpos *psp);
81 QSet<const struct nation_type *> *
82 startpos_raw_nations(const struct startpos *psp);
83 
84 // General map start positions functions.
85 int map_startpos_count();
86 struct startpos *map_startpos_new(struct tile *ptile);
87 struct startpos *map_startpos_get(const struct tile *ptile);
88 bool map_startpos_remove(struct tile *ptile);
89 
90 // Number of index coordinates (for sanity checks and allocations)
91 #define MAP_INDEX_SIZE (wld.map.xsize * wld.map.ysize)
92 
93 #ifdef FREECIV_DEBUG
94 #define CHECK_MAP_POS(x, y) fc_assert(is_normal_map_pos((x), (y)))
95 #define CHECK_NATIVE_POS(x, y) \
96  fc_assert((x) >= 0 && (x) < wld.map.xsize && (y) >= 0 \
97  && (y) < wld.map.ysize)
98 #define CHECK_INDEX(mindex) \
99  fc_assert((mindex) >= 0 && (mindex) < MAP_INDEX_SIZE)
100 #else // FREECIV_DEBUG
101 #define CHECK_MAP_POS(x, y) ((void) 0)
102 #define CHECK_NATIVE_POS(x, y) ((void) 0)
103 #define CHECK_INDEX(mindex) ((void) 0)
104 #endif // FREECIV_DEBUG
105 
106 #define native_pos_to_index_nocheck(nat_x, nat_y) \
107  ((nat_x) + (nat_y) *wld.map.xsize)
108 #define native_pos_to_index(nat_x, nat_y) \
109  (CHECK_NATIVE_POS((nat_x), (nat_y)), \
110  native_pos_to_index_nocheck(nat_x, nat_y))
111 #define index_to_native_pos(pnat_x, pnat_y, mindex) \
112  (*(pnat_x) = index_to_native_pos_x(mindex), \
113  *(pnat_y) = index_to_native_pos_y(mindex))
114 #define index_to_native_pos_x(mindex) ((mindex) % wld.map.xsize)
115 #define index_to_native_pos_y(mindex) ((mindex) / wld.map.xsize)
116 
117 /* Obscure math. See explanation in doc/HACKING. */
118 #define NATIVE_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \
119  (MAP_IS_ISOMETRIC ? (*(pmap_x) = ((nat_y) + ((nat_y) &1)) / 2 + (nat_x), \
120  *(pmap_y) = (nat_y) - *(pmap_x) + wld.map.xsize) \
121  : (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y)))
122 
123 #define MAP_TO_NATIVE_POS(pnat_x, pnat_y, map_x, map_y) \
124  (MAP_IS_ISOMETRIC \
125  ? (*(pnat_y) = (map_x) + (map_y) -wld.map.xsize, \
126  *(pnat_x) = (2 * (map_x) - *(pnat_y) - (*(pnat_y) &1)) / 2) \
127  : (*(pnat_x) = (map_x), *(pnat_y) = (map_y)))
128 
129 #define NATURAL_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \
130  (MAP_IS_ISOMETRIC ? (*(pmap_x) = ((nat_y) + (nat_x)) / 2, \
131  *(pmap_y) = (nat_y) - *(pmap_x) + wld.map.xsize) \
132  : (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y)))
133 
134 #define MAP_TO_NATURAL_POS(pnat_x, pnat_y, map_x, map_y) \
135  (MAP_IS_ISOMETRIC ? (*(pnat_y) = (map_x) + (map_y) -wld.map.xsize, \
136  *(pnat_x) = 2 * (map_x) - *(pnat_y)) \
137  : (*(pnat_x) = (map_x), *(pnat_y) = (map_y)))
138 
139 /* Provide a block to convert from map to natural coordinates. This allows
140  * you to use a natural version of the map position within the block. Note
141  * that the natural position is declared as const and can't be changed
142  * inside the block. */
143 #define do_in_natural_pos(ntl_x, ntl_y, map_x, map_y) \
144  { \
145  int _ntl_x, _ntl_y; \
146  MAP_TO_NATURAL_POS(&_ntl_x, &_ntl_y, map_x, map_y); \
147  { \
148  const int ntl_x = _ntl_x, ntl_y = _ntl_y;
149 
150 #define do_in_natural_pos_end \
151  } \
152  }
153 
154 // Width and height of the map, in native coordinates.
155 
156 // Width and height of the map, in natural coordinates.
157 #define NATURAL_WIDTH (MAP_IS_ISOMETRIC ? 2 * wld.map.xsize : wld.map.xsize)
158 #define NATURAL_HEIGHT wld.map.ysize
159 
160 static inline int map_pos_to_index(struct civ_map *nmap, int map_x,
161  int map_y);
162 
163 // index_to_map_pos(int *, int *, int) inverts map_pos_to_index
164 #define index_to_map_pos(pmap_x, pmap_y, mindex) \
165  (CHECK_INDEX(mindex), index_to_native_pos(pmap_x, pmap_y, mindex), \
166  NATIVE_TO_MAP_POS(pmap_x, pmap_y, *(pmap_x), *(pmap_y)))
167 static inline int index_to_map_pos_x(int mindex);
168 static inline int index_to_map_pos_y(int mindex);
169 
170 #define DIRSTEP(dest_x, dest_y, dir) \
171  ((dest_x) = DIR_DX[(dir)], (dest_y) = DIR_DY[(dir)])
172 
173 /*
174  * Steps from the tile in the given direction, yielding a new tile (or
175  * nullptr).
176  *
177  * Direct calls to DIR_DXY should be avoided and DIRSTEP should be
178  * used. But to allow dest and src to be the same, as in
179  * MAPSTEP(x, y, x, y, dir)
180  * we bend this rule here.
181  */
182 struct tile *mapstep(const struct civ_map *nmap, const struct tile *ptile,
183  enum direction8 dir);
184 
185 struct tile *map_pos_to_tile(const struct civ_map *nmap, int x, int y);
186 struct tile *native_pos_to_tile(const struct civ_map *nmap, int nat_x,
187  int nat_y);
188 struct tile *index_to_tile(const struct civ_map *imap, int mindex);
189 
190 bool is_normal_map_pos(int x, int y);
191 
192 bool is_singular_tile(const struct tile *ptile, int dist);
193 bool normalize_map_pos(const struct civ_map *nmap, int *x, int *y);
194 struct tile *nearest_real_tile(const struct civ_map *nmap, int x, int y);
195 void base_map_distance_vector(int *dx, int *dy, int x0, int y0, int x1,
196  int y1);
197 void map_distance_vector(int *dx, int *dy, const struct tile *ptile0,
198  const struct tile *ptile1);
199 int map_num_tiles();
200 #define map_size_checked() MAX(map_num_tiles() / 1000, 1)
201 
202 struct tile *rand_map_pos(const struct civ_map *nmap);
203 struct tile *rand_map_pos_filtered(const struct civ_map *nmap, void *data,
204  bool (*filter)(const struct tile *ptile,
205  const void *data));
206 
207 bool is_tiles_adjacent(const struct tile *ptile0, const struct tile *ptile1);
208 bool is_move_cardinal(const struct civ_map *nmap,
209  const struct tile *src_tile,
210  const struct tile *dst_tile);
211 
212 int tile_move_cost_ptrs(const struct civ_map *nmap, const struct unit *punit,
213  const struct unit_type *punittype,
214  const struct player *pplayer, const struct tile *t1,
215  const struct tile *t2);
216 
217 /***************************************************************
218  The cost to move punit from where it is to tile x,y.
219  It is assumed the move is a valid one, e.g. the tiles are adjacent.
220 ***************************************************************/
221 static inline int map_move_cost_unit(const struct civ_map *nmap,
222  struct unit *punit,
223  const struct tile *ptile)
224 {
225  return tile_move_cost_ptrs(nmap, punit, unit_type_get(punit),
226  unit_owner(punit), unit_tile(punit), ptile);
227 }
228 
229 /***************************************************************
230  Move cost between two tiles
231 ***************************************************************/
232 static inline int map_move_cost(const struct civ_map *nmap,
233  const struct player *pplayer,
234  const struct unit_type *punittype,
235  const struct tile *src_tile,
236  const struct tile *dst_tile)
237 {
238  return tile_move_cost_ptrs(nmap, nullptr, punittype, pplayer, src_tile,
239  dst_tile);
240 }
241 
242 bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile);
243 bv_extras get_tile_infrastructure_set(const struct tile *ptile, int *count);
244 
245 bool can_channel_land(const struct tile *ptile);
246 bool can_reclaim_ocean(const struct tile *ptile);
247 bool can_thaw_terrain(const struct tile *ptile);
248 bool can_freeze_terrain(const struct tile *ptile);
249 bool terrain_surroundings_allow_change(const struct tile *ptile,
250  const struct terrain *pterrain);
251 
252 extern struct terrain_misc terrain_control;
253 
254 /* This iterates outwards from the starting point. Every tile within
255  * max_dist (including the starting tile) will show up exactly once, in an
256  * outward (based on real map distance) order. The returned values are
257  * always real and are normalized. The starting position must be normal.
258  *
259  * See also iterate_outward() */
260 #define iterate_outward_dxy(nmap, start_tile, max_dist, _tile, _x, _y) \
261  { \
262  int _x, _y, _tile##_x, _tile##_y, _start##_x, _start##_y; \
263  struct tile *_tile; \
264  const struct tile *_tile##_start = (start_tile); \
265  int _tile##_max = (max_dist); \
266  int _tile##_index = 0; \
267  index_to_map_pos(&_start##_x, &_start##_y, tile_index(_tile##_start)); \
268  for (; _tile##_index < wld.map.num_iterate_outwards_indices; \
269  _tile##_index++) { \
270  if (wld.map.iterate_outwards_indices[_tile##_index].dist \
271  > _tile##_max) { \
272  break; \
273  } \
274  _x = wld.map.iterate_outwards_indices[_tile##_index].dx; \
275  _y = wld.map.iterate_outwards_indices[_tile##_index].dy; \
276  _tile##_x = _x + _start##_x; \
277  _tile##_y = _y + _start##_y; \
278  _tile = map_pos_to_tile(nmap, _tile##_x, _tile##_y); \
279  if (nullptr == _tile) { \
280  continue; \
281  }
282 
283 #define iterate_outward_dxy_end \
284  } \
285  }
286 
287 // See iterate_outward_dxy()
288 #define iterate_outward(nmap, start_tile, max_dist, itr_tile) \
289  iterate_outward_dxy(nmap, start_tile, max_dist, itr_tile, _dx_itr, _dy_itr)
290 
291 #define iterate_outward_end iterate_outward_dxy_end
292 
293 /*
294  * Iterate through all tiles in a square with given center and radius.
295  * The position (x_itr, y_itr) that is returned will be normalized;
296  * unreal positions will be automatically discarded. (dx_itr, dy_itr)
297  * is the standard distance vector between the position and the center
298  * position. Note that when the square is larger than the map the
299  * distance vector may not be the minimum distance vector.
300  */
301 #define square_dxy_iterate(nmap, center_tile, radius, tile_itr, dx_itr, \
302  dy_itr) \
303  iterate_outward_dxy(nmap, center_tile, radius, tile_itr, dx_itr, dy_itr)
304 
305 #define square_dxy_iterate_end iterate_outward_dxy_end
306 
307 /*
308  * Iterate through all tiles in a square with given center and radius.
309  * Positions returned will have adjusted x, and positions with illegal
310  * y will be automatically discarded.
311  */
312 #define square_iterate(nmap, center_tile, radius, tile_itr) \
313  square_dxy_iterate(nmap, center_tile, radius, tile_itr, _dummy_x, dummy_y)
314 
315 #define square_iterate_end square_dxy_iterate_end
316 
317 /*
318  * Iterate through all tiles in a circle with given center and squared
319  * radius. Positions returned will have adjusted (x, y); unreal
320  * positions will be automatically discarded.
321  */
322 #define circle_iterate(nmap, center_tile, sq_radius, tile_itr) \
323  circle_dxyr_iterate(nmap, center_tile, sq_radius, tile_itr, _dx, _dy, _dr)
324 
325 #define circle_iterate_end circle_dxyr_iterate_end
326 
327 /* dx, dy, dr are distance from center to tile in x, y and square distance;
328  * do not rely on x, y distance, since they do not work for hex topologies */
329 #define circle_dxyr_iterate(nmap, center_tile, sq_radius, _tile, dx, dy, \
330  dr) \
331  { \
332  const int _tile##_sq_radius = (sq_radius); \
333  const int _tile##_cr_radius = \
334  (int) sqrt((double) MAX(_tile##_sq_radius, 0)); \
335  \
336  square_dxy_iterate(nmap, center_tile, _tile##_cr_radius, _tile, dx, dy) \
337  { \
338  const int dr = map_vector_to_sq_distance(dx, dy); \
339  \
340  if (dr <= _tile##_sq_radius) {
341 
342 #define circle_dxyr_iterate_end \
343  } \
344  } \
345  square_dxy_iterate_end; \
346  }
347 
348 /* Iterate itr_tile through all map tiles adjacent to the given center map
349  * position, with normalization. Does not include the center position.
350  * The order of positions is unspecified. */
351 #define adjc_iterate(nmap, center_tile, itr_tile) \
352  { \
353  /* Written as a wrapper to adjc_dir_iterate since it's the cleanest and \
354  * most efficient. */ \
355  adjc_dir_iterate(nmap, center_tile, itr_tile, \
356  ADJC_ITERATE_dir_itr##itr_tile) \
357  {
358 
359 #define adjc_iterate_end \
360  } \
361  adjc_dir_iterate_end; \
362  }
363 
364 // As adjc_iterate() but also set direction8 iterator variable dir_itr
365 #define adjc_dir_iterate(nmap, center_tile, itr_tile, dir_itr) \
366  adjc_dirlist_iterate(nmap, center_tile, itr_tile, dir_itr, \
367  wld.map.valid_dirs, wld.map.num_valid_dirs)
368 
369 #define adjc_dir_iterate_end adjc_dirlist_iterate_end
370 
371 // Only set direction8 dir_itr (not tile)
372 #define adjc_dir_base_iterate(nmap, center_tile, dir_itr) \
373  adjc_dirlist_base_iterate(nmap, center_tile, dir_itr, wld.map.valid_dirs, \
374  wld.map.num_valid_dirs)
375 
376 #define adjc_dir_base_iterate_end adjc_dirlist_base_iterate_end
377 
378 /* Iterate itr_tile through all map tiles cardinally adjacent to the given
379  * center map position, with normalization. Does not include the center
380  * position. The order of positions is unspecified. */
381 #define cardinal_adjc_iterate(nmap, center_tile, itr_tile) \
382  adjc_dirlist_iterate(nmap, center_tile, itr_tile, _dir_itr##center_tile, \
383  wld.map.cardinal_dirs, wld.map.num_cardinal_dirs)
384 
385 #define cardinal_adjc_iterate_end adjc_dirlist_iterate_end
386 
387 // As cardinal_adjc_iterate but also set direction8 variable dir_itr
388 #define cardinal_adjc_dir_iterate(nmap, center_tile, itr_tile, dir_itr) \
389  adjc_dirlist_iterate(nmap, center_tile, itr_tile, dir_itr, \
390  wld.map.cardinal_dirs, wld.map.num_cardinal_dirs)
391 
392 #define cardinal_adjc_dir_iterate_end adjc_dirlist_iterate_end
393 
394 // Only set direction8 dir_itr (not tile)
395 #define cardinal_adjc_dir_base_iterate(nmap, center_tile, dir_itr) \
396  adjc_dirlist_base_iterate(nmap, center_tile, dir_itr, \
397  wld.map.cardinal_dirs, \
398  wld.map.num_cardinal_dirs)
399 
400 #define cardinal_adjc_dir_base_iterate_end adjc_dirlist_base_iterate_end
401 
402 // Iterate through all tiles cardinally adjacent to both tile1 and tile2
403 #define cardinal_between_iterate(nmap, tile1, tile2, between) \
404  cardinal_adjc_iterate(nmap, tile1, between) \
405  { \
406  cardinal_adjc_iterate(nmap, between, second) \
407  { \
408  if (same_pos(second, tile2)) {
409 
410 #define cardinal_between_iterate_end \
411  } \
412  } \
413  cardinal_adjc_iterate_end; \
414  } \
415  cardinal_adjc_iterate_end;
416 
417 /* Iterate through all tiles adjacent to a tile using the given list of
418  * directions. _dir is the directional value, (center_x, center_y) is
419  * the center tile (which must be normalized). The center tile is not
420  * included in the iteration.
421  *
422  * This macro should not be used directly. Instead, use adjc_iterate,
423  * cardinal_adjc_iterate, or related iterators. */
424 #define adjc_dirlist_iterate(nmap, center_tile, _tile, _dir, dirlist, \
425  dircount) \
426  { \
427  enum direction8 _dir; \
428  int _tile##_x, _tile##_y, _tile##_cx, _tile##_cy; \
429  struct tile *_tile; \
430  const struct tile *_tile##_center = (center_tile); \
431  int _tile##_index = 0; \
432  index_to_map_pos(&_tile##_cx, &_tile##_cy, tile_index(_tile##_center)); \
433  for (; _tile##_index < (dircount); _tile##_index++) { \
434  _dir = dirlist[_tile##_index]; \
435  DIRSTEP(_tile##_x, _tile##_y, _dir); \
436  _tile##_x += _tile##_cx; \
437  _tile##_y += _tile##_cy; \
438  _tile = map_pos_to_tile(nmap, _tile##_x, _tile##_y); \
439  if (nullptr == _tile) { \
440  continue; \
441  }
442 
443 #define adjc_dirlist_iterate_end \
444  } \
445  }
446 
447 // Same as above but without setting the tile.
448 #define adjc_dirlist_base_iterate(nmap, center_tile, _dir, dirlist, \
449  dircount) \
450  { \
451  enum direction8 _dir; \
452  int _tile##_x, _tile##_y, _center##_x, _center##_y; \
453  const struct tile *_tile##_center = (center_tile); \
454  bool _tile##_is_border = is_border_tile(_tile##_center, 1); \
455  int _tile##_index = 0; \
456  index_to_map_pos(&_center##_x, &_center##_y, \
457  tile_index(_tile##_center)); \
458  for (; _tile##_index < (dircount); _tile##_index++) { \
459  _dir = dirlist[_tile##_index]; \
460  DIRSTEP(_tile##_x, _tile##_y, _dir); \
461  _tile##_x += _center##_x; \
462  _tile##_y += _center##_y; \
463  if (_tile##_is_border \
464  && !normalize_map_pos(nmap, &_tile##_x, &_tile##_y)) { \
465  continue; \
466  }
467 
468 #define adjc_dirlist_base_iterate_end \
469  } \
470  }
471 
472 /* Iterate over all positions on the globe.
473  * Use index positions for cache efficiency. */
474 #define whole_map_iterate(_map, _tile) \
475  { \
476  struct tile *_tile; \
477  int _tile##_index = 0; \
478  for (; _tile##_index < MAP_INDEX_SIZE; _tile##_index++) { \
479  _tile = (_map)->tiles + _tile##_index;
480 
481 #define whole_map_iterate_end \
482  } \
483  }
484 
485 BV_DEFINE(dir_vector, 8);
486 
487 // return the reverse of the direction
488 #define DIR_REVERSE(dir) ((enum direction8)(7 - (dir)))
489 
490 enum direction8 dir_cw(enum direction8 dir);
491 enum direction8 dir_ccw(enum direction8 dir);
492 const char *dir_get_name(enum direction8 dir);
493 bool map_untrusted_dir_is_valid(enum direction8 dir);
494 bool is_valid_dir(enum direction8 dir);
495 bool is_cardinal_dir(enum direction8 dir);
496 
497 extern const int DIR_DX[8];
498 extern const int DIR_DY[8];
499 
500 // Used for network transmission; do not change.
501 #define MAP_TILE_OWNER_NULL MAX_UINT8
502 
503 #define MAP_DEFAULT_HUTS 15
504 #define MAP_MIN_HUTS 0
505 #define MAP_MAX_HUTS 500
506 
507 #define MAP_DEFAULT_ANIMALS 20
508 #define MAP_MIN_ANIMALS 0
509 #define MAP_MAX_ANIMALS 500
510 
511 #define MAP_DEFAULT_MAPSIZE MAPSIZE_FULLSIZE
512 
513 /* Size of the map in thousands of tiles. If MAP_MAX_SIZE is increased,
514  * MAX_DBV_LENGTH in bitvector.c must be checked; see the static assertion
515  * below. */
516 #define MAP_DEFAULT_SIZE 4
517 #define MAP_MIN_SIZE 0
518 #define MAP_MAX_SIZE 2048
519 
520 // We communicate through the network with signed 32-bits integers.
521 FC_STATIC_ASSERT((long unsigned) MAP_MAX_SIZE * 1000 < (long unsigned) 1
522  << 31,
523  map_too_big_for_network);
524 
525 #define MAP_DEFAULT_TILESPERPLAYER 100
526 #define MAP_MIN_TILESPERPLAYER 1
527 #define MAP_MAX_TILESPERPLAYER 1000
528 
529 // This defines the maximum linear size in _native_ coordinates.
530 #define MAP_DEFAULT_LINEAR_SIZE 64
531 #define MAP_MAX_LINEAR_SIZE (MAP_MAX_SIZE * 1000 / MAP_MIN_LINEAR_SIZE)
532 #define MAP_MIN_LINEAR_SIZE 16
533 
534 /* The distance between two points at a map shouldn't be larger than this.
535 Adds MAP_MIN_LINEAR_SIZE because hex topologies forbids certain diagonal
536 moves. Includes MAP_MAX_LINEAR_SIZE because a map can be non wrapping. */
537 #define MAP_DISTANCE_MAX (MAP_MAX_LINEAR_SIZE + MAP_MIN_LINEAR_SIZE)
538 
539 #define MAP_ORIGINAL_TOPO TF_WRAPX
540 #define MAP_DEFAULT_TOPO (TF_WRAPX | TF_ISO)
541 
542 #define MAP_DEFAULT_SEED 0
543 #define MAP_MIN_SEED 0
544 #define MAP_MAX_SEED (MAX_UINT32 >> 1)
545 
546 #define MAP_DEFAULT_LANDMASS 30
547 #define MAP_MIN_LANDMASS 15
548 #define MAP_MAX_LANDMASS 100
549 
550 #define MAP_DEFAULT_RICHES 250
551 #define MAP_MIN_RICHES 0
552 #define MAP_MAX_RICHES 1000
553 
554 #define MAP_DEFAULT_STEEPNESS 30
555 #define MAP_MIN_STEEPNESS 0
556 #define MAP_MAX_STEEPNESS 100
557 
558 #define MAP_DEFAULT_WETNESS 50
559 #define MAP_MIN_WETNESS 0
560 #define MAP_MAX_WETNESS 100
561 
562 #define MAP_DEFAULT_GENERATOR MAPGEN_RANDOM
563 
564 #define MAP_DEFAULT_STARTPOS MAPSTARTPOS_DEFAULT
565 
566 #define MAP_DEFAULT_TINYISLES false
567 #define MAP_MIN_TINYISLES false
568 #define MAP_MAX_TINYISLES true
569 
570 #define MAP_DEFAULT_SEPARATE_POLES true
571 #define MAP_MIN_SEPARATE_POLES false
572 #define MAP_MAX_SEPARATE_POLES true
573 
574 #define MAP_DEFAULT_FLATPOLES 100
575 #define MAP_MIN_FLATPOLES 0
576 #define MAP_MAX_FLATPOLES 100
577 
578 #define MAP_DEFAULT_SINGLE_POLE false
579 #define MAP_MIN_SINGLE_POLE false
580 #define MAP_MAX_SINGLE_POLE true
581 
582 #define MAP_DEFAULT_ALLTEMPERATE false
583 #define MAP_MIN_ALLTEMPERATE false
584 #define MAP_MAX_ALLTEMPERATE true
585 
586 #define MAP_DEFAULT_TEMPERATURE 50
587 #define MAP_MIN_TEMPERATURE 0
588 #define MAP_MAX_TEMPERATURE 100
589 
590 #define MAP_DEFAULT_TEAM_PLACEMENT TEAM_PLACEMENT_CLOSEST
591 
592 /*
593  * Inline function definitions. These are at the bottom because they may use
594  * elements defined above.
595  */
596 
597 static inline int map_pos_to_index(struct civ_map *nmap, int map_x,
598  int map_y)
599 {
600  // Note: writing this as a macro is hard; it needs temp variables.
601  int nat_x, nat_y;
602  Q_UNUSED(nmap)
603  CHECK_MAP_POS(map_x, map_y);
604  MAP_TO_NATIVE_POS(&nat_x, &nat_y, map_x, map_y);
606 }
607 
608 static inline int index_to_map_pos_x(int mindex)
609 {
610  // Note: writing this as a macro is hard; it needs temp variables.
611  int map_x, map_y;
612 
613  index_to_map_pos(&map_x, &map_y, mindex);
614  return map_x;
615 }
616 
617 static inline int index_to_map_pos_y(int mindex)
618 {
619  // Note: writing this as a macro is hard; it needs temp variables.
620  int map_x, map_y;
621 
622  index_to_map_pos(&map_x, &map_y, mindex);
623  return map_y;
624 }
625 
626 /****************************************************************************
627  A "border position" is any map position that _may have_ positions within
628  real map distance dist that are non-normal. To see its correctness,
629  consider the case where dist is 1 or 0.
630 ****************************************************************************/
631 static inline bool is_border_tile(const struct tile *ptile, int dist)
632 {
633  /* HACK: An iso-map compresses the value in the X direction but not in
634  * the Y direction. Hence (x+1,y) is 1 tile away while (x,y+2) is also
635  * one tile away. */
636  int xdist = dist;
637  int ydist = (MAP_IS_ISOMETRIC ? (2 * dist) : dist);
638  int nat_x, nat_y;
639 
641 
642  return (nat_x < xdist || nat_y < ydist || nat_x >= wld.map.xsize - xdist
643  || nat_y >= wld.map.ysize - ydist);
644 }
645 
646 enum direction8 rand_direction();
647 enum direction8 opposite_direction(enum direction8 dir);
struct world wld
Definition: game.cpp:48
#define nat_x
#define nat_y
#define MAP_MAX_SIZE
Definition: map.h:517
#define native_pos_to_index(nat_x, nat_y)
Definition: map.h:108
bool terrain_surroundings_allow_change(const struct tile *ptile, const struct terrain *pterrain)
Returns FALSE if a terrain change to 'pterrain' would be prevented by not having enough similar terra...
Definition: map.cpp:706
bool is_normal_map_pos(int x, int y)
Returns TRUE iff the map position is normal.
Definition: map.cpp:902
const int DIR_DY[8]
Definition: map.cpp:58
int startpos_number(const struct startpos *psp)
Returns the unique ID number for this start position.
Definition: map.cpp:1375
void map_free(struct civ_map *fmap)
Frees the allocated memory of the map.
Definition: map.cpp:498
bool startpos_disallow(struct startpos *psp, struct nation_type *pnation)
Disallow the nation to start at the start position.
Definition: map.cpp:1402
bool can_thaw_terrain(const struct tile *ptile)
Returns true if the tile at the given location can be thawed from terrain with a 'Frozen' flag to ter...
Definition: map.cpp:681
enum direction8 opposite_direction(enum direction8 dir)
Return direction that is opposite to given one.
Definition: map.cpp:1589
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Return squared distance between two tiles.
Definition: map.cpp:610
static int index_to_map_pos_y(int mindex)
Definition: map.h:616
void main_map_free()
Free main map and related global structures.
Definition: map.cpp:525
struct tile * rand_map_pos(const struct civ_map *nmap)
Random square anywhere on the map.
Definition: map.cpp:1026
void map_distance_vector(int *dx, int *dy, const struct tile *ptile0, const struct tile *ptile1)
Topology function to find the vector which has the minimum "real" distance between the map positions ...
Definition: map.cpp:1012
int tile_move_cost_ptrs(const struct civ_map *nmap, const struct unit *punit, const struct unit_type *punittype, const struct player *pplayer, const struct tile *t1, const struct tile *t2)
The basic cost to move punit from tile t1 to tile t2.
Definition: map.cpp:743
bool base_get_direction_for_step(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile, enum direction8 *dir)
Return TRUE and sets dir to the direction of the step if (end_x, end_y) can be reached from (start_x,...
Definition: map.cpp:1267
bool startpos_nation_allowed(const struct startpos *psp, const struct nation_type *pnation)
Returns TRUE if the given nation can start here.
Definition: map.cpp:1428
static const bool C_PERCENT
Definition: map.h:30
static const bool C_NUMBER
Definition: map.h:29
bool map_is_empty()
Returns TRUE if we are at a stage of the game where the map has not yet been generated/loaded.
Definition: map.cpp:124
int map_num_tiles()
Returns the total number of (real) positions (or tiles) on the map.
Definition: map.cpp:954
bool startpos_allows_all(const struct startpos *psp)
Returns TRUE if any nation can start here.
Definition: map.cpp:1439
FC_STATIC_ASSERT((long unsigned) MAP_MAX_SIZE *1000<(long unsigned) 1<< 31, map_too_big_for_network)
bool can_channel_land(const struct tile *ptile)
This function returns true if the tile at the given location can be "channeled" from land into ocean.
Definition: map.cpp:668
const int DIR_DX[8]
Definition: map.cpp:57
#define MAP_IS_ISOMETRIC
Definition: map.h:32
#define CHECK_MAP_POS(x, y)
Definition: map.h:101
enum direction8 dir_ccw(enum direction8 dir)
Returns the next direction counter-clock-wise.
Definition: map.cpp:1141
int map_startpos_count()
Is there start positions set for map.
Definition: map.cpp:1518
struct startpos * map_startpos_get(const struct tile *ptile)
Returns the start position at the given tile, or nullptr if none exists there.
Definition: map.cpp:1548
void map_allocate(struct civ_map *amap)
Allocate space for map, and initialise the tiles.
Definition: map.cpp:462
#define MAP_TO_NATIVE_POS(pnat_x, pnat_y, map_x, map_y)
Definition: map.h:123
struct tile * nearest_real_tile(const struct civ_map *nmap, int x, int y)
Twiddle *x and *y to point to the nearest real tile, and ensure that the position is normalized.
Definition: map.cpp:935
bool same_pos(const struct tile *tile0, const struct tile *tile1)
Are (x1,y1) and (x2,y2) really the same when adjusted? This function might be necessary ALOT of place...
Definition: map.cpp:887
static const bool C_ADJACENT
Definition: map.h:27
bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile)
Return TRUE if this ocean terrain is adjacent to a safe coastline.
Definition: map.cpp:636
enum direction8 rand_direction()
Return random direction that is valid in current map.
Definition: map.cpp:1581
bool startpos_pack(const struct startpos *psp, struct packet_edit_startpos_full *packet)
Fills the packet with all of the information at this start position.
Definition: map.cpp:1449
static int map_pos_to_index(struct civ_map *nmap, int map_x, int map_y)
Definition: map.h:596
bool can_reclaim_ocean(const struct tile *ptile)
This function returns true if the tile at the given location can be "reclaimed" from ocean into land.
Definition: map.cpp:655
void base_map_distance_vector(int *dx, int *dy, int x0, int y0, int x1, int y1)
Finds the difference between the two (unnormalized) positions, in cartesian (map) coordinates.
Definition: map.cpp:961
int map_vector_to_sq_distance(int dx, int dy)
Return the sq_distance for a given vector.
Definition: map.cpp:583
bool map_startpos_remove(struct tile *ptile)
Remove the start position at the given tile.
Definition: map.cpp:1565
static int map_move_cost_unit(const struct civ_map *nmap, struct unit *punit, const struct tile *ptile)
Definition: map.h:221
bool is_move_cardinal(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile)
Returns TRUE iff the move from the position (start_x,start_y) to (end_x,end_y) is a cardinal one.
Definition: map.cpp:1306
bool can_freeze_terrain(const struct tile *ptile)
Returns true if the tile at the given location can be turned from terrain without a 'Frozen' flag to ...
Definition: map.cpp:694
bv_extras get_tile_infrastructure_set(const struct tile *ptile, int *count)
Return a bitfield of the extras on the tile that are infrastructure.
Definition: map.cpp:72
struct startpos * map_startpos_new(struct tile *ptile)
Create a new start position at the given tile and return it.
Definition: map.cpp:1531
int get_direction_for_step(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile)
Return the direction which is needed for a step on the map from (start_x, start_y) to (end_x,...
Definition: map.cpp:1288
static int index_to_map_pos_x(int mindex)
Definition: map.h:607
const char * dir_get_name(enum direction8 dir)
Return the debugging name of the direction.
Definition: map.cpp:1084
struct tile * map_pos_to_tile(const struct civ_map *nmap, int x, int y)
Return the tile for the given cartesian (map) position.
Definition: map.cpp:391
bool is_cardinal_dir(enum direction8 dir)
Returns TRUE iff the given direction is a cardinal one.
Definition: map.cpp:1255
static const bool C_CARDINAL
Definition: map.h:28
bool map_untrusted_dir_is_valid(enum direction8 dir)
Returns TRUE iff the given direction is a valid one.
Definition: map.cpp:1212
struct tile * startpos_tile(const struct startpos *psp)
Returns the tile where this start position is located.
Definition: map.cpp:1419
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
Definition: map.cpp:599
bool startpos_unpack(struct startpos *psp, const struct packet_edit_startpos_full *packet)
Fills the start position with the nation information in the packet.
Definition: map.cpp:1469
static bool is_border_tile(const struct tile *ptile, int dist)
Definition: map.h:630
bool is_valid_dir(enum direction8 dir)
Returns TRUE iff the given direction is a valid one.
Definition: map.cpp:1199
enum direction8 dir_cw(enum direction8 dir)
Returns the next direction clock-wise.
Definition: map.cpp:1112
QSet< const struct nation_type * > * startpos_raw_nations(const struct startpos *psp)
Return a the nations hash, used for the property editor.
Definition: map.cpp:1509
void map_init_topology()
map_init_topology needs to be called after map.topology_id is changed.
Definition: map.cpp:276
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Return the tile for the given native position.
Definition: map.cpp:416
BV_DEFINE(dir_vector, 8)
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Return the tile for the given index position.
Definition: map.cpp:429
struct terrain_misc terrain_control
Definition: map.cpp:40
bool is_tiles_adjacent(const struct tile *ptile0, const struct tile *ptile1)
Are two tiles adjacent to each other.
Definition: map.cpp:878
void main_map_allocate()
Allocate main map and related global structures.
Definition: map.cpp:487
#define index_to_native_pos(pnat_x, pnat_y, mindex)
Definition: map.h:111
void map_init(struct civ_map *imap, bool server_side)
Put some sensible values into the map structure.
Definition: map.cpp:129
bool startpos_is_excluding(const struct startpos *psp)
Returns TRUE if the nations returned by startpos_raw_nations() are actually excluded from the nations...
Definition: map.cpp:1496
static int map_move_cost(const struct civ_map *nmap, const struct player *pplayer, const struct unit_type *punittype, const struct tile *src_tile, const struct tile *dst_tile)
Definition: map.h:232
int map_distance(const struct tile *tile0, const struct tile *tile1)
Return Manhattan distance between two tiles.
Definition: map.cpp:623
struct tile * rand_map_pos_filtered(const struct civ_map *nmap, void *data, bool(*filter)(const struct tile *ptile, const void *data))
Give a random tile anywhere on the map for which the 'filter' function returns TRUE.
Definition: map.cpp:1039
struct tile * mapstep(const struct civ_map *nmap, const struct tile *ptile, enum direction8 dir)
Step from the given tile in the given direction.
Definition: map.cpp:342
bool startpos_allow(struct startpos *psp, struct nation_type *pnation)
Allow the nation to start at the start position.
Definition: map.cpp:1385
bool normalize_map_pos(const struct civ_map *nmap, int *x, int *y)
If the position is real, it will be normalized and TRUE will be returned.
Definition: map.cpp:919
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition: map.h:164
bool is_singular_tile(const struct tile *ptile, int dist)
A "SINGULAR" position is any map position that has an abnormal number of tiles in the radius of dist.
Definition: map.cpp:1329
int map_vector_to_real_distance(int dx, int dy)
Return the "real" distance for a given vector.
Definition: map.cpp:549
#define terrain_misc
Definition: map_types.h:23
int xsize
Definition: map_types.h:73
int ysize
Definition: map_types.h:73
Definition: player.h:231
Definition: tile.h:42
Definition: unit.h:134
struct civ_map map
Definition: world_object.h:21
#define tile_index(_pt_)
Definition: tile.h:70
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_owner(_pu)
Definition: unit.h:370
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114