Freeciv-3.2
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13#ifndef FC__MAP_H
14#define FC__MAP_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20#include <math.h> /* sqrt */
21
22/* utility */
23#include "bitvector.h"
24#include "iterator.h"
25#include "log.h" /* fc_assert() */
26
27/* common */
28#include "fc_types.h"
29#include "game.h"
30#include "map_types.h"
31#include "packets.h"
32#include "world_object.h"
33
34struct tile;
35
36/* Parameters for terrain counting functions. */
37static const bool C_ADJACENT = FALSE;
38static const bool C_CARDINAL = TRUE;
39static const bool C_NUMBER = FALSE;
40static const bool C_PERCENT = TRUE;
41
42#define MAP_IS_ISOMETRIC (CURRENT_TOPOLOGY & (TF_ISO + TF_HEX))
43
44#define CURRENT_TOPOLOGY (wld.map.topology_id)
45#define CURRENT_WRAP (wld.map.wrap_id)
46
47#define topo_has_flag(topo, flag) (((topo) & (flag)) != 0)
48#define current_topo_has_flag(flag) topo_has_flag((CURRENT_TOPOLOGY), (flag))
49
50#define wrap_has_flag(wrap, flag) (((wrap) & (flag)) != 0)
51#define current_wrap_has_flag(flag) wrap_has_flag((CURRENT_WRAP), (flag))
52
53#define ALL_DIRECTIONS_CARDINAL() topo_has_flag((CURRENT_TOPOLOGY), TF_HEX)
54
55bool map_is_empty(void);
56void map_init(struct civ_map *imap, bool server_side);
57void map_init_topology(struct civ_map *nmap);
58void map_allocate(struct civ_map *amap);
59void main_map_allocate(void);
60void map_free(struct civ_map *fmap);
61void main_map_free(void);
62
63int map_vector_to_real_distance(int dx, int dy);
64int map_vector_to_sq_distance(int dx, int dy);
65int map_distance(const struct tile *tile0, const struct tile *tile1);
66int real_map_distance(const struct tile *tile0, const struct tile *tile1);
67int sq_map_distance(const struct tile *tile0,const struct tile *tile1);
68
69bool same_pos(const struct tile *tile0, const struct tile *tile1);
71 const struct tile *src_tile,
72 const struct tile *dst_tile,
73 enum direction8 *dir);
74int get_direction_for_step(const struct civ_map *nmap,
75 const struct tile *src_tile,
76 const struct tile *dst_tile);
77
78
79/* Specific functions for start positions. */
80struct startpos *map_startpos_by_number(int id);
81int startpos_number(const struct startpos *psp);
82
83bool startpos_allow(struct startpos *psp, struct nation_type *pnation);
84bool startpos_disallow(struct startpos *psp, struct nation_type *pnation);
85
86struct tile *startpos_tile(const struct startpos *psp);
87bool startpos_nation_allowed(const struct startpos *psp,
88 const struct nation_type *pnation);
89bool startpos_allows_all(const struct startpos *psp);
90
91bool startpos_pack(const struct startpos *psp,
92 struct packet_edit_startpos_full *packet);
93bool startpos_unpack(struct startpos *psp,
94 const struct packet_edit_startpos_full *packet);
95
96/* See comment in "common/map.c". */
97bool startpos_is_excluding(const struct startpos *psp);
98const struct nation_hash *startpos_raw_nations(const struct startpos *psp);
99
100/****************************************************************************
101 Iterate over all nations at the start position for which the function
102 startpos_nation_allowed() would return TRUE. This automatically takes into
103 account the value of startpos_is_excluding() and startpos_allows_all() to
104 iterate over the correct set of nations.
105****************************************************************************/
106struct startpos_iter;
107size_t startpos_iter_sizeof(void);
108struct iterator *startpos_iter_init(struct startpos_iter *it,
109 const struct startpos *psp);
110#define startpos_nations_iterate(ARG_psp, NAME_pnation) \
111 generic_iterate(struct startpos_iter, const struct nation_type *, \
112 NAME_pnation, startpos_iter_sizeof, \
113 startpos_iter_init, (ARG_psp))
114#define startpos_nations_iterate_end generic_iterate_end
115
116
117/* General map start positions functions. */
118int map_startpos_count(void);
119struct startpos *map_startpos_new(struct tile *ptile);
120struct startpos *map_startpos_get(const struct tile *ptile);
121bool map_startpos_remove(struct tile *ptile);
122
123/****************************************************************************
124 Iterate over all start positions placed on the map.
125****************************************************************************/
126struct map_startpos_iter;
127size_t map_startpos_iter_sizeof(void);
129
130#define map_startpos_iterate(NAME_psp) \
131 generic_iterate(struct map_startpos_iter, struct startpos *, \
132 NAME_psp, map_startpos_iter_sizeof, map_startpos_iter_init)
133#define map_startpos_iterate_end generic_iterate_end
134
135
136/* Number of index coordinates (for sanity checks and allocations) */
137#define MAP_INDEX_SIZE (wld.map.xsize * wld.map.ysize)
138
139#ifdef FREECIV_DEBUG
140#define CHECK_MAP_POS(x,y) \
141 fc_assert(is_normal_map_pos((x),(y)))
142#define CHECK_NATIVE_POS(x, y) \
143 fc_assert((x) >= 0 && (x) < wld.map.xsize && (y) >= 0 && (y) < wld.map.ysize)
144#define CHECK_INDEX(mindex) \
145 fc_assert((mindex) >= 0 && (mindex) < MAP_INDEX_SIZE)
146#else /* FREECIV_DEBUG */
147#define CHECK_MAP_POS(x,y) ((void)0)
148#define CHECK_NATIVE_POS(x, y) ((void)0)
149#define CHECK_INDEX(mindex) ((void)0)
150#endif /* FREECIV_DEBUG */
151
152#define native_pos_to_index_nocheck(nat_x, nat_y) \
153 ((nat_x) + (nat_y) * wld.map.xsize)
154#define native_pos_to_index(nat_x, nat_y) \
155 (CHECK_NATIVE_POS((nat_x), (nat_y)), \
156 native_pos_to_index_nocheck(nat_x, nat_y))
157#define index_to_native_pos(pnat_x, pnat_y, mindex) \
158 (*(pnat_x) = index_to_native_pos_x(mindex), \
159 *(pnat_y) = index_to_native_pos_y(mindex))
160#define index_to_native_pos_x(mindex) \
161 ((mindex) % wld.map.xsize)
162#define index_to_native_pos_y(mindex) \
163 ((mindex) / wld.map.xsize)
164
165/* Obscure math. See explanation in doc/HACKING. */
166#define NATIVE_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \
167 (MAP_IS_ISOMETRIC \
168 ? (*(pmap_x) = ((nat_y) + ((nat_y) & 1)) / 2 + (nat_x), \
169 *(pmap_y) = (nat_y) - *(pmap_x) + wld.map.xsize) \
170 : (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y)))
171
172#define MAP_TO_NATIVE_POS(pnat_x, pnat_y, map_x, map_y) \
173 (MAP_IS_ISOMETRIC \
174 ? (*(pnat_y) = (map_x) + (map_y) - wld.map.xsize, \
175 *(pnat_x) = (2 * (map_x) - *(pnat_y) - (*(pnat_y) & 1)) / 2) \
176 : (*(pnat_x) = (map_x), *(pnat_y) = (map_y)))
177
178#define NATURAL_TO_MAP_POS(pmap_x, pmap_y, nat_x, nat_y) \
179 (MAP_IS_ISOMETRIC \
180 ? (*(pmap_x) = ((nat_y) + (nat_x)) / 2, \
181 *(pmap_y) = (nat_y) - *(pmap_x) + wld.map.xsize) \
182 : (*(pmap_x) = (nat_x), *(pmap_y) = (nat_y)))
183
184#define MAP_TO_NATURAL_POS(pnat_x, pnat_y, map_x, map_y) \
185 (MAP_IS_ISOMETRIC \
186 ? (*(pnat_y) = (map_x) + (map_y) - wld.map.xsize, \
187 *(pnat_x) = 2 * (map_x) - *(pnat_y)) \
188 : (*(pnat_x) = (map_x), *(pnat_y) = (map_y)))
189
190
191/* Provide a block to convert from map to native coordinates. This allows
192 * you to use a native version of the map position within the block. Note
193 * that the native position is declared as const and can't be changed
194 * inside the block. */
195#define do_in_native_pos(nat_x, nat_y, map_x, map_y) \
196{ \
197 int _nat_x, _nat_y; \
198 MAP_TO_NATIVE_POS(&_nat_x, &_nat_y, map_x, map_y); \
199 { \
200 const int nat_x = _nat_x, nat_y = _nat_y;
201
202#define do_in_native_pos_end \
203 } \
204}
205
206/* Provide a block to convert from map to natural coordinates. This allows
207 * you to use a natural version of the map position within the block. Note
208 * that the natural position is declared as const and can't be changed
209 * inside the block. */
210#define do_in_natural_pos(ntl_x, ntl_y, map_x, map_y) \
211{ \
212 int _ntl_x, _ntl_y; \
213 MAP_TO_NATURAL_POS(&_ntl_x, &_ntl_y, map_x, map_y); \
214 { \
215 const int ntl_x = _ntl_x, ntl_y = _ntl_y;
216
217#define do_in_natural_pos_end \
218 } \
219}
220
221/* Width and height of the map, in native coordinates. */
222#define NATIVE_WIDTH wld.map.xsize
223#define NATIVE_HEIGHT wld.map.ysize
224
225/* Width and height of the map, in natural coordinates. */
226#define NATURAL_WIDTH (MAP_IS_ISOMETRIC ? 2 * wld.map.xsize : wld.map.xsize)
227#define NATURAL_HEIGHT wld.map.ysize
228
229static inline int map_pos_to_index(struct civ_map *nmap,
230 int map_x, int map_y);
231
232/* index_to_map_pos(int *, int *, int) inverts map_pos_to_index */
233#define index_to_map_pos(pmap_x, pmap_y, mindex) \
234 (CHECK_INDEX(mindex), \
235 index_to_native_pos(pmap_x, pmap_y, mindex), \
236 NATIVE_TO_MAP_POS(pmap_x, pmap_y, *(pmap_x), *(pmap_y)))
237static inline int index_to_map_pos_x(int mindex);
238static inline int index_to_map_pos_y(int mindex);
239
240#define DIRSTEP(dest_x, dest_y, dir) \
241( (dest_x) = DIR_DX[(dir)], \
242 (dest_y) = DIR_DY[(dir)])
243
244/*
245 * Steps from the tile in the given direction, yielding a new tile (or NULL).
246 *
247 * Direct calls to DIR_DXY should be avoided and DIRSTEP should be
248 * used. But to allow dest and src to be the same, as in
249 * MAPSTEP(x, y, x, y, dir)
250 * we bend this rule here.
251 */
252struct tile *mapstep(const struct civ_map *nmap, const struct tile *ptile,
253 enum direction8 dir);
254
255struct tile *map_pos_to_tile(const struct civ_map *nmap, int x, int y);
256struct tile *native_pos_to_tile(const struct civ_map *nmap,
257 int nat_x, int nat_y);
258struct tile *index_to_tile(const struct civ_map *imap, int mindex);
259
260bool is_real_map_pos(const struct civ_map *nmap, int x, int y);
261bool is_normal_map_pos(int x, int y);
262
263bool is_singular_tile(const struct tile *ptile, int dist);
264bool normalize_map_pos(const struct civ_map *nmap, int *x, int *y);
265struct tile *nearest_real_tile(const struct civ_map *nmap, int x, int y);
266void base_map_distance_vector(int *dx, int *dy,
267 int x0, int y0, int x1, int y1);
268void map_distance_vector(int *dx, int *dy, const struct tile *ptile0,
269 const struct tile *ptile1);
270int map_num_tiles(void);
271#define map_size_checked() MAX(map_num_tiles() / 1000, 1)
272
273struct tile *rand_map_pos(const struct civ_map *nmap);
274struct tile *rand_map_pos_filtered(const struct civ_map *nmap, void *data,
275 bool (*filter)(const struct tile *ptile,
276 const void *data));
277
278bool is_tiles_adjacent(const struct tile *ptile0, const struct tile *ptile1);
279bool is_move_cardinal(const struct civ_map *nmap,
280 const struct tile *src_tile,
281 const struct tile *dst_tile);
282
283int tile_move_cost_ptrs(const struct civ_map *nmap,
284 const struct unit *punit,
285 const struct unit_type *punittype,
286 const struct player *pplayer,
287 const struct tile *t1, const struct tile *t2);
288
289/***************************************************************
290 The cost to move punit from where it is to tile x,y.
291 It is assumed the move is a valid one, e.g. the tiles are adjacent.
292***************************************************************/
293static inline int map_move_cost_unit(const struct civ_map *nmap,
294 struct unit *punit,
295 const struct tile *ptile)
296{
299 unit_tile(punit), ptile);
300}
301
302/***************************************************************
303 Move cost between two tiles
304***************************************************************/
305static inline int map_move_cost(const struct civ_map *nmap,
306 const struct player *pplayer,
307 const struct unit_type *punittype,
308 const struct tile *src_tile,
309 const struct tile *dst_tile)
310{
311 return tile_move_cost_ptrs(nmap, NULL, punittype, pplayer,
312 src_tile, dst_tile);
313}
314
315bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile);
316bv_extras get_tile_infrastructure_set(const struct tile *ptile,
317 int *count);
318
319bool can_channel_land(const struct civ_map *nmap,
320 const struct tile *ptile);
321bool can_reclaim_ocean(const struct civ_map *nmap,
322 const struct tile *ptile);
323bool can_thaw_terrain(const struct civ_map *nmap,
324 const struct tile *ptile);
325bool can_freeze_terrain(const struct civ_map *nmap,
326 const struct tile *ptile);
328 const struct tile *ptile,
329 const struct terrain *pterrain);
330
331extern struct terrain_misc terrain_control;
332
333/* This iterates outwards from the starting point. Every tile within max_dist
334 * (including the starting tile) will show up exactly once, in an outward
335 * (based on real map distance) order. The returned values are always real
336 * and are normalized. The starting position must be normal.
337 *
338 * See also iterate_outward() */
339#define iterate_outward_dxy(nmap, start_tile, max_dist, _tile, _x, _y) \
340{ \
341 int _x, _y, _tile##_x, _tile##_y, _start##_x, _start##_y; \
342 struct tile *_tile; \
343 const struct tile *_tile##_start = (start_tile); \
344 int _tile##_max = (max_dist); \
345 int _tile##_index = 0; \
346 index_to_map_pos(&_start##_x, &_start##_y, tile_index(_tile##_start)); \
347 for (; \
348 _tile##_index < wld.map.num_iterate_outwards_indices; \
349 _tile##_index++) { \
350 if (wld.map.iterate_outwards_indices[_tile##_index].dist > _tile##_max) { \
351 break; \
352 } \
353 _x = wld.map.iterate_outwards_indices[_tile##_index].dx; \
354 _y = wld.map.iterate_outwards_indices[_tile##_index].dy; \
355 _tile##_x = _x + _start##_x; \
356 _tile##_y = _y + _start##_y; \
357 _tile = map_pos_to_tile(nmap, _tile##_x, _tile##_y); \
358 if (NULL == _tile) { \
359 continue; \
360 }
361
362#define iterate_outward_dxy_end \
363 } \
364}
365
366/* See iterate_outward_dxy() */
367#define iterate_outward(nmap, start_tile, max_dist, itr_tile) \
368 iterate_outward_dxy(nmap, start_tile, max_dist, itr_tile, \
369 _dx_itr##itr_tile, _dy_itr##itr_tile)
370
371#define iterate_outward_end iterate_outward_dxy_end
372
373/*
374 * Iterate through all tiles in a square with given center and radius.
375 * The position (x_itr, y_itr) that is returned will be normalized;
376 * unreal positions will be automatically discarded. (dx_itr, dy_itr)
377 * is the standard distance vector between the position and the center
378 * position. Note that when the square is larger than the map the
379 * distance vector may not be the minimum distance vector.
380 */
381#define square_dxy_iterate(nmap, center_tile, radius, tile_itr, dx_itr, dy_itr) \
382 iterate_outward_dxy(nmap, center_tile, radius, tile_itr, dx_itr, dy_itr)
383
384#define square_dxy_iterate_end iterate_outward_dxy_end
385
386/*
387 * Iterate through all tiles in a square with given center and radius.
388 * Positions returned will have adjusted x, and positions with illegal
389 * y will be automatically discarded.
390 */
391#define square_iterate(nmap, center_tile, radius, tile_itr) \
392 square_dxy_iterate(nmap, center_tile, radius, tile_itr, _dummy_x, dummy_y)
393
394#define square_iterate_end square_dxy_iterate_end
395
396/*
397 * Iterate through all tiles in a circle with given center and squared
398 * radius. Positions returned will have adjusted (x, y); unreal
399 * positions will be automatically discarded.
400 */
401#define circle_iterate(nmap, center_tile, sq_radius, tile_itr) \
402 circle_dxyr_iterate(nmap, center_tile, sq_radius, tile_itr, _dx, _dy, _dr)
403
404#define circle_iterate_end \
405 circle_dxyr_iterate_end
406
407/* dx, dy, dr are distance from center to tile in x, y and square distance;
408 * do not rely on x, y distance, since they do not work for hex topologies */
409#define circle_dxyr_iterate(nmap, center_tile, sq_radius, \
410 _tile, dx, dy, dr) \
411{ \
412 const int _tile##_sq_radius = (sq_radius); \
413 const int _tile##_cr_radius = (int)sqrt((double)MAX(_tile##_sq_radius, 0)); \
414 \
415 square_dxy_iterate(nmap, center_tile, _tile##_cr_radius, _tile, dx, dy) { \
416 const int dr = map_vector_to_sq_distance(dx, dy); \
417 \
418 if (dr <= _tile##_sq_radius) {
419
420#define circle_dxyr_iterate_end \
421 } \
422 } square_dxy_iterate_end; \
423}
424
425/* Iterate itr_tile through all map tiles adjacent to the given center map
426 * position, with normalization. Does not include the center position.
427 * The order of positions is unspecified. */
428#define adjc_iterate(nmap, center_tile, itr_tile) \
429{ \
430 /* Written as a wrapper to adjc_dir_iterate since it's the cleanest and \
431 * most efficient. */ \
432 adjc_dir_iterate(nmap, center_tile, itr_tile, ADJC_ITERATE_dir_itr##itr_tile) {
434#define adjc_iterate_end \
435 } adjc_dir_iterate_end; \
436}
437
438/* As adjc_iterate() but also set direction8 iterator variable dir_itr */
439#define adjc_dir_iterate(nmap, center_tile, itr_tile, dir_itr) \
440 adjc_dirlist_iterate(nmap, center_tile, itr_tile, dir_itr, \
441 (nmap)->valid_dirs, (nmap)->num_valid_dirs)
443#define adjc_dir_iterate_end adjc_dirlist_iterate_end
444
445/* Only set direction8 dir_itr (not tile) */
446#define adjc_dir_base_iterate(nmap, center_tile, dir_itr) \
447 adjc_dirlist_base_iterate(nmap, center_tile, dir_itr, \
448 (nmap)->valid_dirs, (nmap)->num_valid_dirs)
450#define adjc_dir_base_iterate_end \
451 adjc_dirlist_base_iterate_end
452
453/* Iterate itr_tile through all map tiles cardinally adjacent to the given
454 * center map position, with normalization. Does not include the center
455 * position. The order of positions is unspecified. */
456#define cardinal_adjc_iterate(nmap, center_tile, itr_tile) \
457 adjc_dirlist_iterate(nmap, center_tile, itr_tile, _dir_itr##itr_tile, \
458 (nmap)->cardinal_dirs, (nmap)->num_cardinal_dirs)
460#define cardinal_adjc_iterate_end adjc_dirlist_iterate_end
461
462/* As cardinal_adjc_iterate but also set direction8 variable dir_itr */
463#define cardinal_adjc_dir_iterate(nmap, center_tile, itr_tile, dir_itr) \
464 adjc_dirlist_iterate(nmap, center_tile, itr_tile, dir_itr, \
465 (nmap)->cardinal_dirs, (nmap)->num_cardinal_dirs)
467#define cardinal_adjc_dir_iterate_end adjc_dirlist_iterate_end
468
469/* Only set direction8 dir_itr (not tile) */
470#define cardinal_adjc_dir_base_iterate(nmap, center_tile, dir_itr) \
471 adjc_dirlist_base_iterate(nmap, center_tile, dir_itr, \
472 (nmap)->cardinal_dirs, (nmap)->num_cardinal_dirs)
474#define cardinal_adjc_dir_base_iterate_end \
475 adjc_dirlist_base_iterate_end
476
477/* Iterate through all tiles cardinally adjacent to both tile1 and tile2 */
478#define cardinal_between_iterate(nmap, tile1, tile2, between) \
479 cardinal_adjc_iterate(nmap, tile1, between) { \
480 cardinal_adjc_iterate(nmap, between, second) { \
481 if (same_pos(second, tile2)) {
483#define cardinal_between_iterate_end \
484 } \
485 } cardinal_adjc_iterate_end; \
486 } cardinal_adjc_iterate_end;
487
488/* Iterate through all tiles adjacent to a tile using the given list of
489 * directions. _dir is the directional value, (center_x, center_y) is
490 * the center tile (which must be normalized). The center tile is not
491 * included in the iteration.
492 *
493 * This macro should not be used directly. Instead, use adjc_iterate,
494 * cardinal_adjc_iterate, or related iterators. */
495#define adjc_dirlist_iterate(nmap, center_tile, _tile, _dir, \
496 dirlist, dircount) \
497{ \
498 enum direction8 _dir; \
499 int _tile##_x, _tile##_y, _tile##_cx, _tile##_cy; \
500 struct tile *_tile; \
501 const struct tile *_tile##_center = (center_tile); \
502 int _tile##_index = 0; \
503 index_to_map_pos(&_tile##_cx, &_tile##_cy, tile_index(_tile##_center)); \
504 for (; \
505 _tile##_index < (dircount); \
506 _tile##_index++) { \
507 _dir = (dirlist)[_tile##_index]; \
508 DIRSTEP(_tile##_x, _tile##_y, _dir); \
509 _tile##_x += _tile##_cx; \
510 _tile##_y += _tile##_cy; \
511 _tile = map_pos_to_tile(nmap, _tile##_x, _tile##_y); \
512 if (NULL == _tile) { \
513 continue; \
514 }
516#define adjc_dirlist_iterate_end \
517 } \
518}
519
520/* Same as above but without setting the tile. */
521#define adjc_dirlist_base_iterate(nmap, center_tile, _dir, dirlist, dircount) \
522{ \
523 enum direction8 _dir; \
524 int _tile##_x, _tile##_y, _center##_x, _center##_y; \
525 const struct tile *_tile##_center = (center_tile); \
526 bool _tile##_is_border = is_border_tile(_tile##_center, 1); \
527 int _tile##_index = 0; \
528 index_to_map_pos(&_center##_x, &_center##_y, tile_index(_tile##_center)); \
529 for (; \
530 _tile##_index < (dircount); \
531 _tile##_index++) { \
532 _dir = (dirlist)[_tile##_index]; \
533 DIRSTEP(_tile##_x, _tile##_y, _dir); \
534 _tile##_x += _center##_x; \
535 _tile##_y += _center##_y; \
536 if (_tile##_is_border && !normalize_map_pos(nmap, &_tile##_x, &_tile##_y)) { \
537 continue; \
538 }
540#define adjc_dirlist_base_iterate_end \
541 } \
542}
543
544/* Iterate over all positions on the globe.
545 * Use index positions for cache efficiency. */
546#define whole_map_iterate(_map, _tile) \
547{ \
548 struct tile *_tile; \
549 int _tile##_index = 0; \
550 for (; \
551 _tile##_index < MAP_INDEX_SIZE; \
552 _tile##_index++) { \
553 _tile = (_map)->tiles + _tile##_index;
555#define whole_map_iterate_end \
556 } \
557}
560
561/* return the reverse of the direction */
562#define DIR_REVERSE(dir) (7 - (dir))
563
564enum direction8 dir_cw(enum direction8 dir);
565enum direction8 dir_ccw(enum direction8 dir);
566const char *dir_get_name(enum direction8 dir);
568bool is_valid_dir(enum direction8 dir);
569bool is_cardinal_dir(enum direction8 dir);
570
571extern const int DIR_DX[8];
572extern const int DIR_DY[8];
573
574/* Latitude granularity, irrespective of map/generator settings */
575#define MAP_MAX_LATITUDE 1000
577#define MAP_MAX_LATITUDE_BOUND (MAP_MAX_LATITUDE)
578#define MAP_MIN_LATITUDE_BOUND (-MAP_MAX_LATITUDE)
579#define MAP_DEFAULT_NORTH_LATITUDE MAP_MAX_LATITUDE_BOUND
580#define MAP_DEFAULT_SOUTH_LATITUDE MAP_MIN_LATITUDE_BOUND
581
582/* Northernmost and southernmost latitude for the given map */
583#define MAP_NORTH_LATITUDE(_nmap) ((_nmap).north_latitude)
584#define MAP_SOUTH_LATITUDE(_nmap) ((_nmap).south_latitude)
585
586/* Maximum and minimum latitude present in the given map */
587#define MAP_MAX_REAL_LATITUDE(_nmap) \
588 MAX(MAP_NORTH_LATITUDE(_nmap), MAP_SOUTH_LATITUDE(_nmap))
589#define MAP_MIN_REAL_LATITUDE(_nmap) \
590 MIN(MAP_NORTH_LATITUDE(_nmap), MAP_SOUTH_LATITUDE(_nmap))
591#define MAP_REAL_LATITUDE_RANGE(_nmap) \
592 (MAP_MAX_REAL_LATITUDE(_nmap) - MAP_MIN_REAL_LATITUDE(_nmap))
593
594/* Maximum and minimum absolute latitude */
595#define MAP_MAX_ABS_LATITUDE(_nmap) \
596 MAX(MAP_MAX_REAL_LATITUDE(_nmap), -MAP_MIN_REAL_LATITUDE(_nmap))
597#define MAP_MIN_ABS_LATITUDE(_nmap) \
598 MAX(0, MAX(MAP_MIN_REAL_LATITUDE(_nmap), -MAP_MAX_REAL_LATITUDE(_nmap)))
599
600int map_signed_latitude(const struct tile *ptile);
601
602/* Used for network transmission; do not change. */
603#define MAP_TILE_OWNER_NULL MAX_UINT8
605#define MAP_DEFAULT_HUTS 15
606#define MAP_MIN_HUTS 0
607#define MAP_MAX_HUTS 500
609#define MAP_DEFAULT_ANIMALS 20
610#define MAP_MIN_ANIMALS 0
611#define MAP_MAX_ANIMALS 500
613#define MAP_DEFAULT_MAPSIZE MAPSIZE_FULLSIZE
614
615/* Size of the map in thousands of tiles. If MAP_MAX_SIZE is increased,
616 * MAX_DBV_LENGTH in bitvector.c must be checked; see the static assertion
617 * below. */
618#ifdef FREECIV_WEB
619#define MAP_DEFAULT_SIZE 3
620#define MAP_MIN_SIZE 0
621#define MAP_MAX_SIZE 38
622#else /* FREECIV_WEB */
623#define MAP_DEFAULT_SIZE 4
624#define MAP_MIN_SIZE 0
625#define MAP_MAX_SIZE 2048
626#endif /* FREECIV_WEB */
630/* We communicate through the network with signed 32-bits integers. */
631FC_STATIC_ASSERT((long unsigned) MAP_MAX_SIZE * 1000
632 < (long unsigned) 1 << 31,
635#define MAP_DEFAULT_TILESPERPLAYER 100
636#define MAP_MIN_TILESPERPLAYER 1
637#define MAP_MAX_TILESPERPLAYER 1000
638
639/* This defines the maximum linear size in _native_ coordinates. */
640#define MAP_DEFAULT_LINEAR_SIZE 64
641#define MAP_MAX_LINEAR_SIZE (MAP_MAX_SIZE * 1000 / MAP_MIN_LINEAR_SIZE)
642#define MAP_MIN_LINEAR_SIZE 16
643
644/* The distance between two points at a map shouldn't be larger than this.
645Adds MAP_MIN_LINEAR_SIZE because hex topologies forbids certain diagonal
646moves. Includes MAP_MAX_LINEAR_SIZE because a map can be non wrapping. */
647#define MAP_DISTANCE_MAX (MAP_MAX_LINEAR_SIZE + MAP_MIN_LINEAR_SIZE)
649#define MAP_ORIGINAL_TOPO TF_WRAPX
650#ifdef FREECIV_WEB
651/* Freeciv-web doesn't support isometric maps yet. */
652#define MAP_DEFAULT_TOPO 0
653#define MAP_DEFAULT_WRAP WRAP_X
654#else /* FREECIV_WEB */
655#define MAP_DEFAULT_TOPO (TF_ISO|TF_HEX)
656#define MAP_DEFAULT_WRAP (WRAP_X)
657#endif /* FREECIV_WEB */
659#define MAP_DEFAULT_SEED 0
660#define MAP_MIN_SEED 0
661#define MAP_MAX_SEED (MAX_UINT32 >> 1)
663#define MAP_DEFAULT_LANDMASS 30
664#define MAP_MIN_LANDMASS 15
665#define MAP_MAX_LANDMASS 85
667#define MAP_DEFAULT_RICHES 250
668#define MAP_MIN_RICHES 0
669#define MAP_MAX_RICHES 1000
671#define MAP_DEFAULT_STEEPNESS 30
672#define MAP_MIN_STEEPNESS 0
673#define MAP_MAX_STEEPNESS 100
675#define MAP_DEFAULT_WETNESS 50
676#define MAP_MIN_WETNESS 0
677#define MAP_MAX_WETNESS 100
679#define MAP_DEFAULT_GENERATOR MAPGEN_RANDOM
681#define MAP_DEFAULT_STARTPOS MAPSTARTPOS_DEFAULT
683#define MAP_DEFAULT_TINYISLES FALSE
684#define MAP_MIN_TINYISLES FALSE
685#define MAP_MAX_TINYISLES TRUE
687#define MAP_DEFAULT_SEPARATE_POLES TRUE
688#define MAP_MIN_SEPARATE_POLES FALSE
689#define MAP_MAX_SEPARATE_POLES TRUE
691#define MAP_DEFAULT_FLATPOLES 100
692#define MAP_MIN_FLATPOLES 0
693#define MAP_MAX_FLATPOLES 100
695#define MAP_DEFAULT_TEMPERATURE 50
696#define MAP_MIN_TEMPERATURE 0
697#define MAP_MAX_TEMPERATURE 100
699#define MAP_DEFAULT_TEAM_PLACEMENT TEAM_PLACEMENT_CLOSEST
700
701/*
702 * Inline function definitions. These are at the bottom because they may use
703 * elements defined above.
704 */
706static inline int map_pos_to_index(struct civ_map *nmap, int map_x, int map_y)
707{
708 /* Note: writing this as a macro is hard; it needs temp variables. */
709 int nat_x, nat_y;
710
714}
716static inline int index_to_map_pos_x(int mindex)
717{
718 /* Note: writing this as a macro is hard; it needs temp variables. */
719 int map_x, map_y;
720
722 return map_x;
723}
725static inline int index_to_map_pos_y(int mindex)
726{
727 /* Note: writing this as a macro is hard; it needs temp variables. */
728 int map_x, map_y;
729
731 return map_y;
732}
733
734/****************************************************************************
735 A "border position" is any map position that _may have_ positions within
736 real map distance dist that are non-normal. To see its correctness,
737 consider the case where dist is 1 or 0.
738****************************************************************************/
739static inline bool is_border_tile(const struct tile *ptile, int dist)
740{
741 /* HACK: An iso-map compresses the value in the X direction but not in
742 * the Y direction. Hence (x+1,y) is 1 tile away while (x,y+2) is also
743 * one tile away. */
744 int xdist = dist;
745 int ydist = (MAP_IS_ISOMETRIC ? (2 * dist) : dist);
746 int nat_x, nat_y;
747
749
750 return (nat_x < xdist
751 || nat_y < ydist
752 || nat_x >= wld.map.xsize - xdist
753 || nat_y >= wld.map.ysize - ydist);
754}
755
756enum direction8 rand_direction(void);
758
759#ifdef __cplusplus
760}
761#endif /* __cplusplus */
762
763#endif /* FC__MAP_H */
#define MAX_DBV_LENGTH
Definition bitvector.h:63
#define BV_DEFINE(name, bits)
Definition bitvector.h:132
char * incite_cost
Definition comments.c:75
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct world wld
Definition game.c:63
#define nat_x
#define nat_y
#define MAP_MAX_SIZE
Definition map.h:624
bool can_channel_land(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:699
#define native_pos_to_index(nat_x, nat_y)
Definition map.h:154
struct startpos * map_startpos_get(const struct tile *ptile)
Definition map.c:1883
bool is_normal_map_pos(int x, int y)
Definition map.c:963
const int DIR_DY[8]
Definition map.c:87
int startpos_number(const struct startpos *psp)
Definition map.c:1633
bool can_reclaim_ocean(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:684
void map_free(struct civ_map *fmap)
Definition map.c:530
bool startpos_disallow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1660
void main_map_free(void)
Definition map.c:554
enum direction8 opposite_direction(enum direction8 dir)
Definition map.c:1937
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:641
bool can_freeze_terrain(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:728
static int index_to_map_pos_y(int mindex)
Definition map.h:724
struct startpos * map_startpos_new(struct tile *ptile)
Definition map.c:1866
void map_distance_vector(int *dx, int *dy, const struct tile *ptile0, const struct tile *ptile1)
Definition map.c:1073
bool can_thaw_terrain(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:713
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)
Definition map.c:780
bool base_get_direction_for_step(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile, enum direction8 *dir)
Definition map.c:1328
bool startpos_nation_allowed(const struct startpos *psp, const struct nation_type *pnation)
Definition map.c:1685
int map_num_tiles(void)
Definition map.c:1014
static const bool C_PERCENT
Definition map.h:40
static const bool C_NUMBER
Definition map.h:39
struct tile * startpos_tile(const struct startpos *psp)
Definition map.c:1676
bool startpos_allows_all(const struct startpos *psp)
Definition map.c:1696
const struct nation_hash * startpos_raw_nations(const struct startpos *psp)
Definition map.c:1765
FC_STATIC_ASSERT((long unsigned) MAP_MAX_SIZE *1000<(long unsigned) 1<< 31, map_too_big_for_network)
const int DIR_DX[8]
Definition map.c:86
#define MAP_IS_ISOMETRIC
Definition map.h:42
#define CHECK_MAP_POS(x, y)
Definition map.h:147
struct tile * rand_map_pos(const struct civ_map *nmap)
Definition map.c:1088
const char * dir_get_name(enum direction8 dir)
Definition map.c:1146
enum direction8 dir_ccw(enum direction8 dir)
Definition map.c:1203
struct iterator * map_startpos_iter_init(struct map_startpos_iter *iter)
Definition map.c:1920
void map_allocate(struct civ_map *amap)
Definition map.c:493
void map_init_topology(struct civ_map *nmap)
Definition map.c:303
#define MAP_TO_NATIVE_POS(pnat_x, pnat_y, map_x, map_y)
Definition map.h:172
bool same_pos(const struct tile *tile0, const struct tile *tile1)
Definition map.c:940
static const bool C_ADJACENT
Definition map.h:37
void main_map_allocate(void)
Definition map.c:519
bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:667
bool startpos_pack(const struct startpos *psp, struct packet_edit_startpos_full *packet)
Definition map.c:1706
static int map_pos_to_index(struct civ_map *nmap, int map_x, int map_y)
Definition map.h:705
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
int map_signed_latitude(const struct tile *ptile)
Definition map.c:1550
void base_map_distance_vector(int *dx, int *dy, int x0, int y0, int x1, int y1)
Definition map.c:1024
int map_vector_to_sq_distance(int dx, int dy)
Definition map.c:614
bool map_startpos_remove(struct tile *ptile)
Definition map.c:1899
static int map_move_cost_unit(const struct civ_map *nmap, struct unit *punit, const struct tile *ptile)
Definition map.h:293
bool is_move_cardinal(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile)
Definition map.c:1365
struct tile * rand_map_pos_filtered(const struct civ_map *nmap, void *data, bool(*filter)(const struct tile *ptile, const void *data))
Definition map.c:1101
int map_startpos_count(void)
Definition map.c:1853
bv_extras get_tile_infrastructure_set(const struct tile *ptile, int *count)
Definition map.c:101
size_t startpos_iter_sizeof(void)
Definition map.c:1777
int get_direction_for_step(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile)
Definition map.c:1347
static int index_to_map_pos_x(int mindex)
Definition map.h:715
bool is_cardinal_dir(enum direction8 dir)
Definition map.c:1316
static const bool C_CARDINAL
Definition map.h:38
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1273
bool terrain_surroundings_allow_change(const struct civ_map *nmap, const struct tile *ptile, const struct terrain *pterrain)
Definition map.c:742
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:630
bool startpos_unpack(struct startpos *psp, const struct packet_edit_startpos_full *packet)
Definition map.c:1726
struct iterator * startpos_iter_init(struct startpos_iter *it, const struct startpos *psp)
Definition map.c:1825
static bool is_border_tile(const struct tile *ptile, int dist)
Definition map.h:738
bool is_valid_dir(enum direction8 dir)
Definition map.c:1260
enum direction8 dir_cw(enum direction8 dir)
Definition map.c:1174
enum direction8 rand_direction(void)
Definition map.c:1929
struct tile * mapstep(const struct civ_map *nmap, const struct tile *ptile, enum direction8 dir)
Definition map.c:371
struct terrain_misc terrain_control
Definition map.c:69
bool is_tiles_adjacent(const struct tile *ptile0, const struct tile *ptile1)
Definition map.c:931
struct tile * nearest_real_tile(const struct civ_map *nmap, int x, int y)
Definition map.c:995
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Definition map.c:443
struct tile * map_pos_to_tile(const struct civ_map *nmap, int x, int y)
Definition map.c:419
#define index_to_native_pos(pnat_x, pnat_y, mindex)
Definition map.h:157
void map_init(struct civ_map *imap, bool server_side)
Definition map.c:157
bool startpos_is_excluding(const struct startpos *psp)
Definition map.c:1753
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:305
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
struct startpos * map_startpos_by_number(int id)
Definition map.c:1624
bool map_is_empty(void)
Definition map.c:149
size_t map_startpos_iter_sizeof(void)
Definition map.c:1911
bool startpos_allow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1643
bool normalize_map_pos(const struct civ_map *nmap, int *x, int *y)
Definition map.c:979
bool is_real_map_pos(const struct civ_map *nmap, int x, int y)
Definition map.c:952
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:233
bool is_singular_tile(const struct tile *ptile, int dist)
Definition map.c:1581
int map_vector_to_real_distance(int dx, int dy)
Definition map.c:578
#define terrain_misc
Definition map_types.h:29
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
int xsize
Definition map_types.h:78
int ysize
Definition map_types.h:78
Definition map.c:41
Definition tile.h:50
Definition unit.h:138
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define tile_index(_pt_)
Definition tile.h:88
#define unit_tile(_pu)
Definition unit.h:397
#define unit_owner(_pu)
Definition unit.h:396
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123