Freeciv21
Develop your civilization from humble roots to a global empire
tile.h
Go to the documentation of this file.
1 /**************************************************************************
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file
3  is part of Freeciv21. Freeciv21 is free software:
4 |\_/|,,_____,~~` you can redistribute it and/or modify it under the
5 (.".)~~ )`~}} terms of the GNU General Public License as published
6  \o/\ /---~\\ ~}} by the Free Software Foundation, either version 3 of
7  _// _// ~} the License, or (at your option) any later version.
8  You should have received a copy of the GNU General
9  Public License along with Freeciv21. If not, see
10  https://www.gnu.org/licenses/.
11 **************************************************************************/
12 
13 #pragma once
14 
15 // utility
16 #include "bitvector.h"
17 
18 // common
19 #include "base.h"
20 #include "extras.h"
21 #include "fc_types.h"
22 #include "player.h"
23 #include "road.h"
24 #include "terrain.h"
25 #include "unitlist.h"
26 
27 // network, order dependent
28 enum known_type {
32 };
33 
34 /* Convenience macro for accessing tile coordinates. This should only be
35  * used for debugging. */
36 #define TILE_XY(ptile) \
37  ((ptile) ? index_to_map_pos_x(tile_index(ptile)) : -1), \
38  ((ptile) ? index_to_map_pos_y(tile_index(ptile)) : -1)
39 
40 #define TILE_INDEX_NONE (-1)
41 
42 struct tile {
43  int index; /* Index coordinate of the tile. Used to calculate (x, y) pairs
44  * (index_to_map_pos()) and (nat_x, nat_y) pairs
45  * (index_to_native_pos()). */
47  bv_extras extras;
48  struct extra_type *resource; // nullptr for no resource
49  struct terrain *terrain; // nullptr for unknown tiles
50  struct unit_list *units;
51  struct city *worked; // nullptr for not worked
52  struct player *owner; // nullptr for not owned
56  struct tile *claimer;
57  char *label; // nullptr for no label
58  char *spec_sprite;
59 };
60 
61 // 'struct tile_list' and related functions.
62 #define SPECLIST_TAG tile
63 #define SPECLIST_TYPE struct tile
64 #include "speclist.h"
65 #define tile_list_iterate(tile_list, ptile) \
66  TYPED_LIST_ITERATE(struct tile, tile_list, ptile)
67 #define tile_list_iterate_end LIST_ITERATE_END
68 
69 // Tile accessor functions.
70 #define tile_index(_pt_) (_pt_)->index
71 
72 struct city *tile_city(const struct tile *ptile);
73 
74 #define tile_continent(_tile) ((_tile)->continent)
75 /*Continent_id tile_continent(const struct tile *ptile);*/
76 void tile_set_continent(struct tile *ptile, Continent_id val);
77 
78 #define tile_owner(_tile) ((_tile)->owner)
79 /*struct player *tile_owner(const struct tile *ptile);*/
80 void tile_set_owner(struct tile *ptile, struct player *pplayer,
81  struct tile *claimer);
82 #define tile_claimer(_tile) ((_tile)->claimer)
83 
84 #define tile_resource(_tile) ((_tile)->resource)
85 static inline bool tile_resource_is_valid(const struct tile *ptile)
86 {
87  return ptile->resource != nullptr
88  && BV_ISSET(ptile->extras, ptile->resource->id);
89 }
90 /*const struct resource *tile_resource(const struct tile *ptile);*/
91 void tile_set_resource(struct tile *ptile, struct extra_type *presource);
92 
93 #define tile_terrain(_tile) ((_tile)->terrain)
94 /*struct terrain *tile_terrain(const struct tile *ptile);*/
95 void tile_set_terrain(struct tile *ptile, struct terrain *pterrain);
96 
97 #define tile_worked(_tile) ((_tile)->worked)
98 // struct city *tile_worked(const struct tile *ptile);
99 void tile_set_worked(struct tile *ptile, struct city *pcity);
100 
101 const bv_extras *tile_extras_null();
102 static inline const bv_extras *tile_extras(const struct tile *ptile)
103 {
104  return &(ptile->extras);
105 }
106 
107 bool tile_has_base_flag(const struct tile *ptile, enum base_flag_id flag);
108 bool tile_has_base_flag_for_unit(const struct tile *ptile,
109  const struct unit_type *punittype,
110  enum base_flag_id flag);
111 bool tile_has_refuel_extra(const struct tile *ptile,
112  const struct unit_type *punittype);
113 bool tile_has_native_base(const struct tile *ptile,
114  const struct unit_type *punittype);
115 bool tile_has_claimable_base(const struct tile *ptile,
116  const struct unit_type *punittype);
117 int tile_extras_defense_bonus(const struct tile *ptile,
118  const struct unit_type *punittype);
119 int tile_extras_class_defense_bonus(const struct tile *ptile,
120  const struct unit_class *pclass);
121 
122 bool tile_has_road(const struct tile *ptile, const struct road_type *proad);
123 bool tile_has_road_flag(const struct tile *ptile, enum road_flag_id flag);
124 int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o);
125 int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o);
126 bool tile_has_river(const struct tile *tile);
127 
128 bool tile_extra_apply(struct tile *ptile, const extra_type *tgt);
129 bool tile_extra_rm_apply(struct tile *ptile, struct extra_type *tgt);
130 #define tile_has_extra(ptile, pextra) \
131  BV_ISSET(ptile->extras, extra_index(pextra))
132 bool tile_has_conflicting_extra(const struct tile *ptile,
133  const struct extra_type *pextra);
134 bool tile_has_visible_extra(const struct tile *ptile,
135  const struct extra_type *pextra);
136 void tile_add_extra(struct tile *ptile, const struct extra_type *pextra);
137 void tile_remove_extra(struct tile *ptile, const struct extra_type *pextra);
138 bool tile_has_extra_flag(const struct tile *ptile, enum extra_flag_id flag);
139 ;
140 
141 // Vision related
142 enum known_type tile_get_known(const struct tile *ptile,
143  const struct player *pplayer);
144 
145 bool tile_is_seen(const struct tile *target_tile,
146  const struct player *pow_player);
147 
148 /* A somewhat arbitrary integer value. Activity times are multiplied by
149  * this amount, and divided by them later before being used. This may
150  * help to avoid rounding errors; however it should probably be removed. */
151 #define ACTIVITY_FACTOR 10
152 int tile_activity_time(enum unit_activity activity, const struct tile *ptile,
153  const struct extra_type *tgt);
154 
155 // These are higher-level functions that handle side effects on the tile.
156 void tile_change_terrain(struct tile *ptile, struct terrain *pterrain);
157 bool tile_apply_activity(struct tile *ptile, Activity_type_id act,
158  struct extra_type *tgt);
159 
160 #define TILE_LB_TERRAIN_RIVER (1 << 0)
161 #define TILE_LB_RIVER_RESOURCE (1 << 1)
162 #define TILE_LB_RESOURCE_POLL (1 << 2)
163 const char *tile_get_info_text(const struct tile *ptile,
164  bool include_nuisances, int linebreaks);
165 
166 // Virtual tiles are tiles that do not exist on the game map.
167 struct tile *tile_virtual_new(const struct tile *ptile);
168 void tile_virtual_destroy(struct tile *vtile);
169 bool tile_virtual_check(const tile *vtile);
170 
171 bool tile_set_label(struct tile *ptile, const char *label);
172 bool tile_is_placing(const struct tile *ptile);
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
enum unit_activity Activity_type_id
Definition: fc_types.h:296
output_type_id
Definition: fc_types.h:84
signed short Continent_id
Definition: fc_types.h:289
Definition: city.h:291
int id
Definition: extras.h:75
Definition: player.h:231
Definition: road.h:54
Definition: tile.h:42
char * spec_sprite
Definition: tile.h:58
char * label
Definition: tile.h:57
int index
Definition: tile.h:43
bv_extras extras
Definition: tile.h:47
struct extra_type * resource
Definition: tile.h:48
struct unit_list * units
Definition: tile.h:50
struct player * extras_owner
Definition: tile.h:55
struct terrain * terrain
Definition: tile.h:49
int infra_turns
Definition: tile.h:54
struct extra_type * placing
Definition: tile.h:53
struct city * worked
Definition: tile.h:51
struct player * owner
Definition: tile.h:52
Continent_id continent
Definition: tile.h:46
struct tile * claimer
Definition: tile.h:56
int tile_extras_class_defense_bonus(const struct tile *ptile, const struct unit_class *pclass)
Calculate defense bonus given for unit class by extras.
Definition: tile.cpp:232
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Adds extra to tile.
Definition: tile.cpp:974
int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o)
Calculate output bonus given by roads.
Definition: tile.cpp:289
bool tile_has_extra_flag(const struct tile *ptile, enum extra_flag_id flag)
Check if tile contains extra providing effect.
Definition: tile.cpp:914
bool tile_has_claimable_base(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains base providing effect for unit.
Definition: tile.cpp:203
void tile_set_terrain(struct tile *ptile, struct terrain *pterrain)
Set the given terrain at the specified tile.
Definition: tile.cpp:114
void tile_virtual_destroy(struct tile *vtile)
Frees all memory used by the virtual tile, including freeing virtual units in the tile's unit list an...
Definition: tile.cpp:1051
bool tile_is_placing(const struct tile *ptile)
Is there a placing ongoing?
Definition: tile.cpp:1138
bool tile_has_river(const struct tile *tile)
Tile has any river type.
Definition: tile.cpp:876
bool tile_has_base_flag(const struct tile *ptile, enum base_flag_id flag)
Check if tile contains base providing effect.
Definition: tile.cpp:164
bool tile_apply_activity(struct tile *ptile, Activity_type_id act, struct extra_type *tgt)
Apply an activity (Activity_type_id, e.g., ACTIVITY_TRANSFORM) to a tile.
Definition: tile.cpp:701
const char * tile_get_info_text(const struct tile *ptile, bool include_nuisances, int linebreaks)
Return a (static) string with tile name describing terrain and extras of some categories.
Definition: tile.cpp:799
const bv_extras * tile_extras_null()
Returns a bit vector of the extras present at nullptr tile.
Definition: tile.cpp:149
void tile_remove_extra(struct tile *ptile, const struct extra_type *pextra)
Removes extra from tile if such exist.
Definition: tile.cpp:984
void tile_change_terrain(struct tile *ptile, struct terrain *pterrain)
Change the terrain to the given type.
Definition: tile.cpp:491
bool tile_has_visible_extra(const struct tile *ptile, const struct extra_type *pextra)
Returns TRUE if the given tile has a road of given type on it.
Definition: tile.cpp:948
static bool tile_resource_is_valid(const struct tile *ptile)
Definition: tile.h:85
int tile_extras_defense_bonus(const struct tile *ptile, const struct unit_type *punittype)
Calculate defense bonus given for unit type by bases and roads.
Definition: tile.cpp:223
bool tile_has_base_flag_for_unit(const struct tile *ptile, const struct unit_type *punittype, enum base_flag_id flag)
Check if tile contains base providing effect for unit.
Definition: tile.cpp:182
bool tile_has_road(const struct tile *ptile, const struct road_type *proad)
Returns TRUE if the given tile has a road of given type on it.
Definition: tile.cpp:868
bool tile_virtual_check(const tile *vtile)
Check if the given tile is a virtual one or not.
Definition: tile.cpp:1085
bool tile_has_conflicting_extra(const struct tile *ptile, const struct extra_type *pextra)
Returns TRUE if the given tile has a extra conflicting with the given one.
Definition: tile.cpp:930
void tile_set_owner(struct tile *ptile, struct player *pplayer, struct tile *claimer)
Set the owner of a tile (may be nullptr).
Definition: tile.cpp:58
bool tile_is_seen(const struct tile *target_tile, const struct player *pow_player)
Returns TRUE iff the target_tile is seen by pow_player.
Definition: tile.cpp:416
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Time to complete the given activity on the given tile.
Definition: tile.cpp:427
known_type
Definition: tile.h:28
@ TILE_KNOWN_UNSEEN
Definition: tile.h:30
@ TILE_UNKNOWN
Definition: tile.h:29
@ TILE_KNOWN_SEEN
Definition: tile.h:31
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains base native for unit.
Definition: tile.cpp:327
int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
Calculate output increment given by roads.
Definition: tile.cpp:266
bool tile_extra_rm_apply(struct tile *ptile, struct extra_type *tgt)
Remove extra and adjust other extras accordingly.
Definition: tile.cpp:605
bool tile_set_label(struct tile *ptile, const char *label)
Sets label for tile.
Definition: tile.cpp:1104
void tile_set_resource(struct tile *ptile, struct extra_type *presource)
Set the given resource at the specified tile.
Definition: tile.cpp:355
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Return a known_type enumeration value for the tile.
Definition: tile.cpp:398
bool tile_extra_apply(struct tile *ptile, const extra_type *tgt)
Add extra and adjust other extras accordingly.
Definition: tile.cpp:578
bool tile_has_refuel_extra(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains refuel extra native for unit.
Definition: tile.cpp:309
bool tile_has_road_flag(const struct tile *ptile, enum road_flag_id flag)
Check if tile contains road providing effect.
Definition: tile.cpp:894
static const bv_extras * tile_extras(const struct tile *ptile)
Definition: tile.h:102
void tile_set_worked(struct tile *ptile, struct city *pcity)
Set the city/worker on the tile (may be nullptr).
Definition: tile.cpp:96
void tile_set_continent(struct tile *ptile, Continent_id val)
Set the continent ID of the tile.
Definition: tile.cpp:388
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
struct tile * tile_virtual_new(const struct tile *ptile)
Returns a virtual tile.
Definition: tile.cpp:997