Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/map.h Source File

Freeciv-3.1

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