Freeciv-3.2: /fast/freeciv/nightly/S3_2/common/map.h Source File

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. */
37 static const bool C_ADJACENT = FALSE;
38 static const bool C_CARDINAL = TRUE;
39 static const bool C_NUMBER = FALSE;
40 static 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(void);
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);
70bool base_get_direction_for_step(const struct civ_map *nmap,
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);
128struct iterator *map_startpos_iter_init(struct map_startpos_iter *iter);
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***************************************************************/
293 static inline int map_move_cost_unit(const struct civ_map *nmap,
294 struct unit *punit,
295 const struct tile *ptile)
296{
297 return tile_move_cost_ptrs(nmap, punit,
298 unit_type_get(punit), unit_owner(punit),
299 unit_tile(punit), ptile);
300}
301
302/***************************************************************
303 Move cost between two tiles
304***************************************************************/
305 static 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);
316 bv_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);
327bool terrain_surroundings_allow_change(const struct civ_map *nmap,
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) {
433
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 wld.map.valid_dirs, wld.map.num_valid_dirs)
442
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 wld.map.valid_dirs, wld.map.num_valid_dirs)
449
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 wld.map.cardinal_dirs, wld.map.num_cardinal_dirs)
459
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 wld.map.cardinal_dirs, wld.map.num_cardinal_dirs)
466
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 wld.map.cardinal_dirs, wld.map.num_cardinal_dirs)
473
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)) {
482
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 }
515
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 }
539
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;
554
555#define whole_map_iterate_end \
556 } \
557}
558
559 BV_DEFINE(dir_vector, 8);
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);
567bool map_untrusted_dir_is_valid(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
576
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
604
605#define MAP_DEFAULT_HUTS 15
606#define MAP_MIN_HUTS 0
607#define MAP_MAX_HUTS 500
608
609#define MAP_DEFAULT_ANIMALS 20
610#define MAP_MIN_ANIMALS 0
611#define MAP_MAX_ANIMALS 500
612
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 */
627
628 FC_STATIC_ASSERT(MAP_MAX_SIZE * 1000 <= MAX_DBV_LENGTH,
629 map_too_big_for_bitvector);
630/* We communicate through the network with signed 32-bits integers. */
631 FC_STATIC_ASSERT((long unsigned) MAP_MAX_SIZE * 1000
632 < (long unsigned) 1 << 31,
633 map_too_big_for_network);
634
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
646 moves. 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)
648
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 */
658
659#define MAP_DEFAULT_SEED 0
660#define MAP_MIN_SEED 0
661#define MAP_MAX_SEED (MAX_UINT32 >> 1)
662
663#define MAP_DEFAULT_LANDMASS 30
664#define MAP_MIN_LANDMASS 15
665#define MAP_MAX_LANDMASS 85
666
667#define MAP_DEFAULT_RICHES 250
668#define MAP_MIN_RICHES 0
669#define MAP_MAX_RICHES 1000
670
671#define MAP_DEFAULT_STEEPNESS 30
672#define MAP_MIN_STEEPNESS 0
673#define MAP_MAX_STEEPNESS 100
674
675#define MAP_DEFAULT_WETNESS 50
676#define MAP_MIN_WETNESS 0
677#define MAP_MAX_WETNESS 100
678
679#define MAP_DEFAULT_GENERATOR MAPGEN_RANDOM
680
681#define MAP_DEFAULT_STARTPOS MAPSTARTPOS_DEFAULT
682
683#define MAP_DEFAULT_TINYISLES FALSE
684#define MAP_MIN_TINYISLES FALSE
685#define MAP_MAX_TINYISLES TRUE
686
687#define MAP_DEFAULT_SEPARATE_POLES TRUE
688#define MAP_MIN_SEPARATE_POLES FALSE
689#define MAP_MAX_SEPARATE_POLES TRUE
690
691#define MAP_DEFAULT_FLATPOLES 100
692#define MAP_MIN_FLATPOLES 0
693#define MAP_MAX_FLATPOLES 100
694
695#define MAP_DEFAULT_TEMPERATURE 50
696#define MAP_MIN_TEMPERATURE 0
697#define MAP_MAX_TEMPERATURE 100
698
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 */
705
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
711 CHECK_MAP_POS(map_x, map_y);
712 MAP_TO_NATIVE_POS(&nat_x, &nat_y, map_x, map_y);
713 return native_pos_to_index(nat_x, nat_y);
714}
715
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
721 index_to_map_pos(&map_x, &map_y, mindex);
722 return map_x;
723}
724
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
730 index_to_map_pos(&map_x, &map_y, mindex);
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
748 index_to_native_pos(&nat_x, &nat_y, tile_index(ptile));
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);
757enum direction8 opposite_direction(enum direction8 dir);
758
759#ifdef __cplusplus
760}
761#endif /* __cplusplus */
762
763#endif /* FC__MAP_H */
bitvector.h
MAX_DBV_LENGTH
#define MAX_DBV_LENGTH
Definition bitvector.h:63
BV_DEFINE
#define BV_DEFINE(name, bits)
Definition bitvector.h:132
incite_cost
char * incite_cost
Definition comments.c:74
punit
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
fc_types.h
wld
struct world wld
Definition game.c:63
game.h
iterator.h
log.h
nat_x
#define nat_x
nat_y
#define nat_y
MAP_MAX_SIZE
#define MAP_MAX_SIZE
Definition map.h:624
can_channel_land
bool can_channel_land(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:699
native_pos_to_index
#define native_pos_to_index(nat_x, nat_y)
Definition map.h:154
map_startpos_get
struct startpos * map_startpos_get(const struct tile *ptile)
Definition map.c:1883
is_normal_map_pos
bool is_normal_map_pos(int x, int y)
Definition map.c:963
DIR_DY
const int DIR_DY[8]
Definition map.c:87
startpos_number
int startpos_number(const struct startpos *psp)
Definition map.c:1633
can_reclaim_ocean
bool can_reclaim_ocean(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:684
map_free
void map_free(struct civ_map *fmap)
Definition map.c:530
startpos_disallow
bool startpos_disallow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1660
main_map_free
void main_map_free(void)
Definition map.c:554
opposite_direction
enum direction8 opposite_direction(enum direction8 dir)
Definition map.c:1937
sq_map_distance
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:641
can_freeze_terrain
bool can_freeze_terrain(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:728
index_to_map_pos_y
static int index_to_map_pos_y(int mindex)
Definition map.h:724
map_startpos_new
struct startpos * map_startpos_new(struct tile *ptile)
Definition map.c:1866
map_distance_vector
void map_distance_vector(int *dx, int *dy, const struct tile *ptile0, const struct tile *ptile1)
Definition map.c:1073
can_thaw_terrain
bool can_thaw_terrain(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:713
tile_move_cost_ptrs
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
base_get_direction_for_step
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
startpos_nation_allowed
bool startpos_nation_allowed(const struct startpos *psp, const struct nation_type *pnation)
Definition map.c:1685
map_num_tiles
int map_num_tiles(void)
Definition map.c:1014
C_PERCENT
static const bool C_PERCENT
Definition map.h:40
C_NUMBER
static const bool C_NUMBER
Definition map.h:39
startpos_tile
struct tile * startpos_tile(const struct startpos *psp)
Definition map.c:1676
startpos_allows_all
bool startpos_allows_all(const struct startpos *psp)
Definition map.c:1696
startpos_raw_nations
const struct nation_hash * startpos_raw_nations(const struct startpos *psp)
Definition map.c:1765
FC_STATIC_ASSERT
FC_STATIC_ASSERT((long unsigned) MAP_MAX_SIZE *1000<(long unsigned) 1<< 31, map_too_big_for_network)
DIR_DX
const int DIR_DX[8]
Definition map.c:86
MAP_IS_ISOMETRIC
#define MAP_IS_ISOMETRIC
Definition map.h:42
CHECK_MAP_POS
#define CHECK_MAP_POS(x, y)
Definition map.h:147
rand_map_pos
struct tile * rand_map_pos(const struct civ_map *nmap)
Definition map.c:1088
dir_get_name
const char * dir_get_name(enum direction8 dir)
Definition map.c:1146
dir_ccw
enum direction8 dir_ccw(enum direction8 dir)
Definition map.c:1203
map_startpos_iter_init
struct iterator * map_startpos_iter_init(struct map_startpos_iter *iter)
Definition map.c:1920
map_allocate
void map_allocate(struct civ_map *amap)
Definition map.c:493
MAP_TO_NATIVE_POS
#define MAP_TO_NATIVE_POS(pnat_x, pnat_y, map_x, map_y)
Definition map.h:172
same_pos
bool same_pos(const struct tile *tile0, const struct tile *tile1)
Definition map.c:940
C_ADJACENT
static const bool C_ADJACENT
Definition map.h:37
main_map_allocate
void main_map_allocate(void)
Definition map.c:519
is_safe_ocean
bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:667
startpos_pack
bool startpos_pack(const struct startpos *psp, struct packet_edit_startpos_full *packet)
Definition map.c:1706
map_pos_to_index
static int map_pos_to_index(struct civ_map *nmap, int map_x, int map_y)
Definition map.h:705
index_to_tile
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
map_signed_latitude
int map_signed_latitude(const struct tile *ptile)
Definition map.c:1550
base_map_distance_vector
void base_map_distance_vector(int *dx, int *dy, int x0, int y0, int x1, int y1)
Definition map.c:1024
map_vector_to_sq_distance
int map_vector_to_sq_distance(int dx, int dy)
Definition map.c:614
map_startpos_remove
bool map_startpos_remove(struct tile *ptile)
Definition map.c:1899
map_move_cost_unit
static int map_move_cost_unit(const struct civ_map *nmap, struct unit *punit, const struct tile *ptile)
Definition map.h:293
is_move_cardinal
bool is_move_cardinal(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile)
Definition map.c:1365
rand_map_pos_filtered
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
map_startpos_count
int map_startpos_count(void)
Definition map.c:1853
get_tile_infrastructure_set
bv_extras get_tile_infrastructure_set(const struct tile *ptile, int *count)
Definition map.c:101
startpos_iter_sizeof
size_t startpos_iter_sizeof(void)
Definition map.c:1777
get_direction_for_step
int get_direction_for_step(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile)
Definition map.c:1347
index_to_map_pos_x
static int index_to_map_pos_x(int mindex)
Definition map.h:715
is_cardinal_dir
bool is_cardinal_dir(enum direction8 dir)
Definition map.c:1316
C_CARDINAL
static const bool C_CARDINAL
Definition map.h:38
map_untrusted_dir_is_valid
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1273
terrain_surroundings_allow_change
bool terrain_surroundings_allow_change(const struct civ_map *nmap, const struct tile *ptile, const struct terrain *pterrain)
Definition map.c:742
real_map_distance
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:630
startpos_unpack
bool startpos_unpack(struct startpos *psp, const struct packet_edit_startpos_full *packet)
Definition map.c:1726
startpos_iter_init
struct iterator * startpos_iter_init(struct startpos_iter *it, const struct startpos *psp)
Definition map.c:1825
is_border_tile
static bool is_border_tile(const struct tile *ptile, int dist)
Definition map.h:738
is_valid_dir
bool is_valid_dir(enum direction8 dir)
Definition map.c:1260
dir_cw
enum direction8 dir_cw(enum direction8 dir)
Definition map.c:1174
rand_direction
enum direction8 rand_direction(void)
Definition map.c:1929
mapstep
struct tile * mapstep(const struct civ_map *nmap, const struct tile *ptile, enum direction8 dir)
Definition map.c:371
terrain_control
struct terrain_misc terrain_control
Definition map.c:69
is_tiles_adjacent
bool is_tiles_adjacent(const struct tile *ptile0, const struct tile *ptile1)
Definition map.c:931
nearest_real_tile
struct tile * nearest_real_tile(const struct civ_map *nmap, int x, int y)
Definition map.c:995
native_pos_to_tile
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Definition map.c:443
map_pos_to_tile
struct tile * map_pos_to_tile(const struct civ_map *nmap, int x, int y)
Definition map.c:419
index_to_native_pos
#define index_to_native_pos(pnat_x, pnat_y, mindex)
Definition map.h:157
map_init
void map_init(struct civ_map *imap, bool server_side)
Definition map.c:157
startpos_is_excluding
bool startpos_is_excluding(const struct startpos *psp)
Definition map.c:1753
map_move_cost
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
map_distance
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
map_startpos_by_number
struct startpos * map_startpos_by_number(int id)
Definition map.c:1624
map_is_empty
bool map_is_empty(void)
Definition map.c:149
map_startpos_iter_sizeof
size_t map_startpos_iter_sizeof(void)
Definition map.c:1911
startpos_allow
bool startpos_allow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1643
normalize_map_pos
bool normalize_map_pos(const struct civ_map *nmap, int *x, int *y)
Definition map.c:979
is_real_map_pos
bool is_real_map_pos(const struct civ_map *nmap, int x, int y)
Definition map.c:952
map_init_topology
void map_init_topology(void)
Definition map.c:303
index_to_map_pos
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:233
is_singular_tile
bool is_singular_tile(const struct tile *ptile, int dist)
Definition map.c:1581
map_vector_to_real_distance
int map_vector_to_real_distance(int dx, int dy)
Definition map.c:578
map_types.h
terrain_misc
#define terrain_misc
Definition map_types.h:29
packets.h
y
struct sprite int int y
Definition sprite_g.h:31
x
struct sprite int x
Definition sprite_g.h:31
civ_map
Definition map_types.h:71
civ_map::xsize
int xsize
Definition map_types.h:78
civ_map::ysize
int ysize
Definition map_types.h:78
iterator
Definition iterator.h:31
nation_type
Definition nation.h:98
packet_edit_startpos_full
Definition packets_gen.h:1694
player
Definition player.h:247
startpos_iter
Definition map.c:58
startpos
Definition map.c:41
terrain
Definition terrain.h:182
tile
Definition tile.h:50
unit_type
Definition unittype.h:500
unit
Definition unit.h:138
world::map
struct civ_map map
Definition world_object.h:38
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
tile_index
#define tile_index(_pt_)
Definition tile.h:88
unit_tile
#define unit_tile(_pu)
Definition unit.h:390
unit_owner
#define unit_owner(_pu)
Definition unit.h:389
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
world_object.h