Freeciv-3.3: /fast/freeciv/nightly/main/common/map.h Source File

Freeciv-3.3

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