Freeciv21
Develop your civilization from humble roots to a global empire
terrain.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 // utility
16 #include "bitvector.h"
17 #include "shared.h"
18 
19 // common
20 #include "fc_types.h"
21 #include "name_translation.h"
22 #include "unittype.h"
23 
24 struct base_type;
25 struct rgbcolor;
26 
27 // Used in the network protocol.
33 };
34 
35 // ===
36 
37 struct resource_type {
38 
39  char id_old_save; // Single-character identifier used in old savegames.
40 #define RESOURCE_NULL_IDENTIFIER '\0'
41 #define RESOURCE_NONE_IDENTIFIER ' '
42 
43  int output[O_LAST]; // Amount added by this resource.
44 
45  struct extra_type *self;
46 };
47 
48 // ===
49 
50 #define T_NONE (nullptr) // A special flag meaning no terrain type.
51 #define T_UNKNOWN (nullptr) // An unknown terrain.
52 
53 // The first terrain value.
54 #define T_FIRST 0
55 
56 /* A hard limit on the number of terrains; useful for static arrays.
57  * Used in the network protocol. */
58 #define MAX_NUM_TERRAINS (96)
59 /* Reflect reality; but theoretically could be larger than terrains!
60  * Used in the network protocol. */
61 #define MAX_RESOURCE_TYPES (MAX_NUM_TERRAINS / 2)
62 
63 // Used in the network protocol.
64 #define SPECENUM_NAME terrain_class
65 #define SPECENUM_VALUE0 TC_LAND
66 // TRANS: terrain class: used adjectivally
67 #define SPECENUM_VALUE0NAME N_("Land")
68 #define SPECENUM_VALUE1 TC_OCEAN
69 // TRANS: terrain class: used adjectivally
70 #define SPECENUM_VALUE1NAME N_("Oceanic")
71 #define SPECENUM_COUNT TC_COUNT
72 #include "specenum_gen.h"
73 
74 /* Types of alterations available to terrain.
75  * This enum is only used in the effects system; the relevant information
76  * is encoded in other members of the terrain structure.
77  *
78  * Used in the network protocol. */
79 #define SPECENUM_NAME terrain_alteration
80 // Can build irrigation without changing terrain
81 #define SPECENUM_VALUE0 TA_CAN_IRRIGATE
82 /* TRANS: this and following strings may rarely be presented to the player
83  * in ruleset help text, to denote the set of terrains which can be altered
84  * in a particular way */
85 #define SPECENUM_VALUE0NAME N_("CanIrrigate")
86 // Can build mine without changing terrain
87 #define SPECENUM_VALUE1 TA_CAN_MINE
88 #define SPECENUM_VALUE1NAME N_("CanMine")
89 /* Can build roads and/or railroads */
90 #define SPECENUM_VALUE2 TA_CAN_ROAD
91 #define SPECENUM_VALUE2NAME N_("CanRoad")
92 #define SPECENUM_COUNT TA_COUNT
93 #include "specenum_gen.h"
94 
95 // Used in the network protocol.
96 #define SPECENUM_NAME terrain_flag_id
97 // No barbarians summoned on this terrain.
98 #define SPECENUM_VALUE0 TER_NO_BARBS
99 /* TRANS: this and following strings are 'terrain flags', which may rarely
100  * be presented to the player in ruleset help text */
101 #define SPECENUM_VALUE0NAME N_("NoBarbs")
102 // No cities on this terrain.
103 #define SPECENUM_VALUE1 TER_NO_CITIES
104 #define SPECENUM_VALUE1NAME N_("NoCities")
105 // Players will start on this terrain type.
106 #define SPECENUM_VALUE2 TER_STARTER
107 #define SPECENUM_VALUE2NAME N_("Starter")
108 // Terrains with this type can have road with "River" flag on them.
109 #define SPECENUM_VALUE3 TER_CAN_HAVE_RIVER
110 #define SPECENUM_VALUE3NAME N_("CanHaveRiver")
111 /*this tile is not safe as coast, (all ocean / ice) */
112 #define SPECENUM_VALUE4 TER_UNSAFE_COAST
113 #define SPECENUM_VALUE4NAME N_("UnsafeCoast")
114 // Fresh water terrain
115 #define SPECENUM_VALUE5 TER_FRESHWATER
116 #define SPECENUM_VALUE5NAME N_("FreshWater")
117 // Map generator does not place this terrain
118 #define SPECENUM_VALUE6 TER_NOT_GENERATED
119 #define SPECENUM_VALUE6NAME N_("NotGenerated")
120 // Units on this terrain are not generating or subject to zoc
121 #define SPECENUM_VALUE7 TER_NO_ZOC
122 #define SPECENUM_VALUE7NAME N_("NoZoc")
123 // Ice-covered terrain (affects minimap)
124 #define SPECENUM_VALUE8 TER_FROZEN
125 #define SPECENUM_VALUE8NAME N_("Frozen")
126 #define SPECENUM_VALUE9 TER_USER_1
127 #define SPECENUM_VALUE10 TER_USER_2
128 #define SPECENUM_VALUE11 TER_USER_3
129 #define SPECENUM_VALUE12 TER_USER_4
130 #define SPECENUM_VALUE13 TER_USER_5
131 #define SPECENUM_VALUE14 TER_USER_6
132 #define SPECENUM_VALUE15 TER_USER_7
133 #define SPECENUM_VALUE16 TER_USER_8
134 #define SPECENUM_VALUE17 TER_USER_9
135 #define SPECENUM_VALUE18 TER_USER_LAST
136 #define SPECENUM_NAMEOVERRIDE
137 #define SPECENUM_BITVECTOR bv_terrain_flags
138 #include "specenum_gen.h"
139 
140 #define MAX_NUM_USER_TER_FLAGS (TER_USER_LAST - TER_USER_1 + 1)
141 
142 #define SPECENUM_NAME mapgen_terrain_property
143 #define SPECENUM_VALUE0 MG_MOUNTAINOUS
144 #define SPECENUM_VALUE0NAME "mountainous"
145 #define SPECENUM_VALUE1 MG_GREEN
146 #define SPECENUM_VALUE1NAME "green"
147 #define SPECENUM_VALUE2 MG_FOLIAGE
148 #define SPECENUM_VALUE2NAME "foliage"
149 #define SPECENUM_VALUE3 MG_TROPICAL
150 #define SPECENUM_VALUE3NAME "tropical"
151 #define SPECENUM_VALUE4 MG_TEMPERATE
152 #define SPECENUM_VALUE4NAME "temperate"
153 #define SPECENUM_VALUE5 MG_COLD
154 #define SPECENUM_VALUE5NAME "cold"
155 #define SPECENUM_VALUE6 MG_FROZEN
156 #define SPECENUM_VALUE6NAME "frozen"
157 #define SPECENUM_VALUE7 MG_WET
158 #define SPECENUM_VALUE7NAME "wet"
159 #define SPECENUM_VALUE8 MG_DRY
160 #define SPECENUM_VALUE8NAME "dry"
161 #define SPECENUM_VALUE9 MG_OCEAN_DEPTH
162 #define SPECENUM_VALUE9NAME "ocean_depth"
163 #define SPECENUM_COUNT MG_COUNT
164 #include "specenum_gen.h"
165 
166 /*
167  * This struct gives data about each terrain type. There are many ways
168  * it could be extended.
169  */
170 struct terrain {
172  struct name_translation name;
173  bool ruledit_disabled; // Does not really exist - hole in terrain array
174  char graphic_str[MAX_LEN_NAME]; // add tile_ prefix
176 
177  char identifier; // Single-character identifier used in games saved.
178  char identifier_load; /* Single-character identifier that was used in the
179  * savegame loaded. */
180 
181 #define TERRAIN_UNKNOWN_IDENTIFIER 'u'
182 
183  enum terrain_class tclass;
184 
185  int movement_cost; // whole MP, not scaled by SINGLE_MOVE
186  int defense_bonus; // % defense bonus - defaults to zero
187 
189 
190  struct extra_type **resources; // nullptr-terminated
191 
195 
197 
199 
203 
207 
209 
215 
216  const struct unit_type *animal;
217 
218  // May be nullptr if the transformation is impossible.
221 
222  /* These are special properties of the terrain used by mapgen. If a tile
223  * has a property, then the value gives the weighted amount of tiles that
224  * will be assigned this terrain.
225  *
226  * For instance if mountains have 70 and hills have 30 of MG_MOUNTAINOUS
227  * then 70% of 'mountainous' tiles will be given mountains.
228  *
229  * Ocean_depth is different. Instead of a percentage, the depth of the
230  * tile in the range 0 (never chosen) to 100 (deepest) is used.
231  */
232  int property[MG_COUNT];
233 #define TERRAIN_OCEAN_DEPTH_MINIMUM (1)
234 #define TERRAIN_OCEAN_DEPTH_MAXIMUM (100)
235 
236  bv_unit_classes native_to;
237 
238  bv_terrain_flags flags;
239 
240  struct rgbcolor *rgb;
241 
243 };
244 
245 // General terrain accessor functions.
247 Terrain_type_id terrain_index(const struct terrain *pterrain);
248 Terrain_type_id terrain_number(const struct terrain *pterrain);
249 
250 struct terrain *terrain_by_number(const Terrain_type_id type);
251 
252 struct terrain *terrain_by_rule_name(const char *name);
253 struct terrain *terrain_by_translated_name(const char *name);
254 
255 char terrain_identifier(const struct terrain *pterrain);
256 const char *terrain_rule_name(const struct terrain *pterrain);
257 const char *terrain_name_translation(const struct terrain *pterrain);
258 
259 // Functions to operate on a terrain flag.
260 #define terrain_has_flag(terr, flag) BV_ISSET((terr)->flags, flag)
261 
262 bool is_terrain_flag_card_near(const struct tile *ptile,
263  enum terrain_flag_id flag);
264 bool is_terrain_flag_near_tile(const struct tile *ptile,
265  enum terrain_flag_id flag);
266 int count_terrain_flag_near_tile(const struct tile *ptile,
267  bool cardinal_only, bool percentage,
268  enum terrain_flag_id flag);
271 void set_user_terrain_flag_name(enum terrain_flag_id id, const char *name,
272  const char *helptxt);
273 const char *terrain_flag_helptxt(enum terrain_flag_id id);
274 
275 // Terrain-specific functions.
276 #define is_ocean(pterrain) \
277  ((pterrain) != T_UNKNOWN \
278  && terrain_type_terrain_class(pterrain) == TC_OCEAN)
279 #define is_ocean_tile(ptile) is_ocean(tile_terrain(ptile))
280 
281 bool terrain_has_resource(const struct terrain *pterrain,
282  const struct extra_type *presource);
283 
284 // Functions to operate on a general terrain type.
285 bool is_terrain_card_near(const struct tile *ptile,
286  const struct terrain *pterrain, bool check_self);
287 bool is_terrain_near_tile(const struct tile *ptile,
288  const struct terrain *pterrain, bool check_self);
289 int count_terrain_property_near_tile(const struct tile *ptile,
290  bool cardinal_only, bool percentage,
291  enum mapgen_terrain_property prop);
292 
293 struct resource_type *resource_type_init(struct extra_type *pextra);
294 void resource_types_free();
295 
296 // Special helper functions
297 const char *get_infrastructure_text(bv_extras extras);
298 struct extra_type *get_preferred_pillage(bv_extras extras);
299 
300 int terrain_extra_build_time(const struct terrain *pterrain,
301  enum unit_activity activity,
302  const struct extra_type *tgt);
303 int terrain_extra_removal_time(const struct terrain *pterrain,
304  enum unit_activity activity,
305  const struct extra_type *tgt);
306 
307 // Functions to operate on a terrain class.
308 const char *terrain_class_name_translation(enum terrain_class tclass);
309 
310 enum terrain_class
311 terrain_type_terrain_class(const struct terrain *pterrain);
312 bool is_terrain_class_card_near(const struct tile *ptile,
313  enum terrain_class tclass);
314 bool is_terrain_class_near_tile(const struct tile *ptile,
315  enum terrain_class tclass);
316 int count_terrain_class_near_tile(const struct tile *ptile,
317  bool cardinal_only, bool percentage,
318  enum terrain_class tclass);
319 
320 // Functions to deal with possible terrain alterations.
321 bool terrain_can_support_alteration(const struct terrain *pterrain,
322  enum terrain_alteration talter);
323 
324 // Initialization and iteration
325 void terrains_init();
326 void terrains_free();
327 
328 struct terrain *terrain_array_first();
329 const struct terrain *terrain_array_last();
330 
331 #define terrain_type_iterate(_p) \
332  { \
333  struct terrain *_p = terrain_array_first(); \
334  if (nullptr != _p) { \
335  for (; _p <= terrain_array_last(); _p++) {
336 
337 #define terrain_type_iterate_end \
338  } \
339  } \
340  }
341 
342 #define terrain_re_active_iterate(_p) \
343  terrain_type_iterate(_p) \
344  { \
345  if (!_p->ruledit_disabled) {
346 
347 #define terrain_re_active_iterate_end \
348  } \
349  } \
350  terrain_type_iterate_end;
char * extras
Definition: comments.cpp:34
int Terrain_type_id
Definition: fc_types.h:290
#define MAX_LEN_NAME
Definition: fc_types.h:61
@ O_LAST
Definition: fc_types.h:91
const char * name
Definition: inputfile.cpp:118
Definition: base.h:43
char id_old_save
Definition: terrain.h:39
int output[O_LAST]
Definition: terrain.h:43
int placing_time
Definition: terrain.h:208
struct terrain * cooler_wetter_result
Definition: terrain.h:220
struct extra_type ** resources
Definition: terrain.h:190
struct terrain * cooler_drier_result
Definition: terrain.h:220
int item_number
Definition: terrain.h:171
struct terrain * warmer_wetter_result
Definition: terrain.h:219
bv_terrain_flags flags
Definition: terrain.h:238
bv_unit_classes native_to
Definition: terrain.h:236
int road_time
Definition: terrain.h:194
struct name_translation name
Definition: terrain.h:172
int plant_time
Definition: terrain.h:198
int irrigation_food_incr
Definition: terrain.h:201
int clean_fallout_time
Definition: terrain.h:213
struct terrain * irrigation_result
Definition: terrain.h:200
struct terrain * warmer_drier_result
Definition: terrain.h:219
int defense_bonus
Definition: terrain.h:186
int cultivate_time
Definition: terrain.h:196
int movement_cost
Definition: terrain.h:185
QVector< QString > * helptext
Definition: terrain.h:242
const struct unit_type * animal
Definition: terrain.h:216
int pillage_time
Definition: terrain.h:214
int output[O_LAST]
Definition: terrain.h:188
int transform_time
Definition: terrain.h:211
struct terrain * mining_result
Definition: terrain.h:204
char graphic_alt[MAX_LEN_NAME]
Definition: terrain.h:175
int mining_time
Definition: terrain.h:206
char identifier_load
Definition: terrain.h:178
struct rgbcolor * rgb
Definition: terrain.h:240
int clean_pollution_time
Definition: terrain.h:212
char graphic_str[MAX_LEN_NAME]
Definition: terrain.h:174
enum terrain_class tclass
Definition: terrain.h:183
int road_output_incr_pct[O_LAST]
Definition: terrain.h:192
struct terrain * transform_result
Definition: terrain.h:210
int irrigation_time
Definition: terrain.h:202
int base_time
Definition: terrain.h:193
int mining_shield_incr
Definition: terrain.h:205
char identifier
Definition: terrain.h:177
bool ruledit_disabled
Definition: terrain.h:173
Definition: tile.h:42
int terrain_extra_removal_time(const struct terrain *pterrain, enum unit_activity activity, const struct extra_type *tgt)
Time to complete the extra removal activity on the given terrain.
Definition: terrain.cpp:615
Terrain_type_id terrain_count()
Return the number of terrains.
Definition: terrain.cpp:93
char terrain_identifier(const struct terrain *pterrain)
Return the terrain identifier.
Definition: terrain.cpp:98
struct extra_type * get_preferred_pillage(bv_extras extras)
Returns the highest-priority (best) extra to be pillaged from the terrain set.
Definition: terrain.cpp:439
void user_terrain_flags_init()
Initialize user terrain type flags.
Definition: terrain.cpp:649
int count_terrain_flag_near_tile(const struct tile *ptile, bool cardinal_only, bool percentage, enum terrain_flag_id flag)
Return the number of adjacent tiles that have terrain with the given flag (not including ptile itself...
Definition: terrain.cpp:365
bool is_terrain_class_card_near(const struct tile *ptile, enum terrain_class tclass)
Is there terrain of the given class cardinally near tile? (Does not check ptile itself....
Definition: terrain.cpp:473
bool is_terrain_flag_near_tile(const struct tile *ptile, enum terrain_flag_id flag)
Returns TRUE iff any adjacent tile contains terrain with the given flag (does not check ptile itself)...
Definition: terrain.cpp:345
bool is_terrain_flag_card_near(const struct tile *ptile, enum terrain_flag_id flag)
Returns TRUE iff any cardinally adjacent tile contains terrain with the given flag (does not check pt...
Definition: terrain.cpp:325
struct terrain * terrain_by_translated_name(const char *name)
Return the terrain type matching the name, or T_UNKNOWN if none matches.
Definition: terrain.cpp:158
bool terrain_can_support_alteration(const struct terrain *pterrain, enum terrain_alteration talter)
Can terrain support given infrastructure?
Definition: terrain.cpp:558
struct terrain * terrain_by_number(const Terrain_type_id type)
Return the terrain for the given terrain index.
Definition: terrain.cpp:128
const char * terrain_rule_name(const struct terrain *pterrain)
Return the (untranslated) rule name of the terrain.
Definition: terrain.cpp:184
int count_terrain_property_near_tile(const struct tile *ptile, bool cardinal_only, bool percentage, enum mapgen_terrain_property prop)
Return the number of adjacent tiles that have the given terrain property.
Definition: terrain.cpp:298
struct resource_type * resource_type_init(struct extra_type *pextra)
Initialize resource_type structure.
Definition: terrain.cpp:209
void terrains_free()
Free memory which is associated with terrain types.
Definition: terrain.cpp:50
struct terrain * terrain_by_rule_name(const char *name)
Return the terrain type matching the name, or T_UNKNOWN if none matches.
Definition: terrain.cpp:140
const char * terrain_class_name_translation(enum terrain_class tclass)
Return the (translated) name of the given terrain class.
Definition: terrain.cpp:546
Terrain_type_id terrain_index(const struct terrain *pterrain)
Return the terrain index.
Definition: terrain.cpp:110
bool is_terrain_near_tile(const struct tile *ptile, const struct terrain *pterrain, bool check_self)
Returns TRUE iff any adjacent tile contains the given terrain.
Definition: terrain.cpp:277
const char * get_infrastructure_text(bv_extras extras)
Return a (static) string with extra(s) name(s): eg: "Mine" eg: "Road/Farmland" This only includes "in...
Definition: terrain.cpp:394
const struct terrain * terrain_array_last()
Return the last item of terrains.
Definition: terrain.cpp:82
const char * terrain_flag_helptxt(enum terrain_flag_id id)
Return the (untranslated) helptxt of the user terrain flag.
Definition: terrain.cpp:708
bool is_terrain_card_near(const struct tile *ptile, const struct terrain *pterrain, bool check_self)
Returns TRUE iff any cardinally adjacent tile contains the given terrain.
Definition: terrain.cpp:256
void user_terrain_flags_free()
Frees the memory associated with all user terrain flags.
Definition: terrain.cpp:661
bool is_terrain_class_near_tile(const struct tile *ptile, enum terrain_class tclass)
Is there terrain of the given class near tile? (Does not check ptile itself.)
Definition: terrain.cpp:495
enum terrain_class terrain_type_terrain_class(const struct terrain *pterrain)
What terrain class terrain type belongs to.
Definition: terrain.cpp:464
bool terrain_has_resource(const struct terrain *pterrain, const struct extra_type *presource)
Check for resource in terrain resources list.
Definition: terrain.cpp:192
void set_user_terrain_flag_name(enum terrain_flag_id id, const char *name, const char *helptxt)
Sets user defined name for terrain flag.
Definition: terrain.cpp:673
const char * terrain_name_translation(const struct terrain *pterrain)
Return the (translated) name of the terrain.
Definition: terrain.cpp:175
int terrain_extra_build_time(const struct terrain *pterrain, enum unit_activity activity, const struct extra_type *tgt)
Time to complete the extra building activity on the given terrain.
Definition: terrain.cpp:579
Terrain_type_id terrain_number(const struct terrain *pterrain)
Return the terrain index.
Definition: terrain.cpp:119
int count_terrain_class_near_tile(const struct tile *ptile, bool cardinal_only, bool percentage, enum terrain_class tclass)
Return the number of adjacent tiles that have given terrain class (not including ptile itself).
Definition: terrain.cpp:517
struct terrain * terrain_array_first()
Return the first item of terrains.
Definition: terrain.cpp:71
special_river_move
Definition: terrain.h:28
@ RMV_NORMAL
Definition: terrain.h:29
@ RMV_FAST_RELAXED
Definition: terrain.h:31
@ RMV_FAST_STRICT
Definition: terrain.h:30
@ RMV_FAST_ALWAYS
Definition: terrain.h:32
void resource_types_free()
Free the memory associated with resource types.
Definition: terrain.cpp:223
void terrains_init()
Initialize terrain and resource structures.
Definition: terrain.cpp:34