Freeciv21
Develop your civilization from humble roots to a global empire
improvement.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 // City Improvements, including Wonders. (Alternatively "Buildings".)
16 
17 // utility
18 #include "bitvector.h"
19 
20 // common
21 #include "name_translation.h"
22 #include "requirements.h"
23 
24 /* B_LAST is a value that is guaranteed to be larger than all
25  * actual Impr_type_id values. It is used as a flag value; it can
26  * also be used for fixed allocations to ensure ability to hold the
27  * full number of improvement types.
28  *
29  * B_NEVER is the pointer equivalent replacement for B_LAST flag value.
30  *
31  * Used in the network protocol.
32  */
33 #define B_LAST MAX_NUM_BUILDINGS
34 
35 #define B_NEVER (nullptr)
36 
37 // Changing these breaks network compatibility.
38 #define SPECENUM_NAME impr_flag_id
39 // improvement should be visible to others without spying
40 #define SPECENUM_VALUE0 IF_VISIBLE_BY_OTHERS
41 #define SPECENUM_VALUE0NAME "VisibleByOthers"
42 // this small wonder is moved to another city if game.savepalace is on.
43 #define SPECENUM_VALUE1 IF_SAVE_SMALL_WONDER
44 #define SPECENUM_VALUE1NAME "SaveSmallWonder"
45 // when built, gives gold
46 #define SPECENUM_VALUE2 IF_GOLD
47 #define SPECENUM_VALUE2NAME "Gold"
48 // Never destroyed by disasters
49 #define SPECENUM_VALUE3 IF_DISASTER_PROOF
50 #define SPECENUM_VALUE3NAME "DisasterProof"
51 #define SPECENUM_COUNT IF_COUNT
52 #define SPECENUM_BITVECTOR bv_impr_flags
53 #include "specenum_gen.h"
54 
55 // Used in the network protocol.
56 BV_DEFINE(bv_imprs, B_LAST);
57 
58 // Type of improvement. (Read from buildings.ruleset file.)
59 struct impr_type {
61  struct name_translation name;
62  bool ruledit_disabled; /* Does not really exist - hole in improvements
63  array */
64  char graphic_str[MAX_LEN_NAME]; // city icon of improv.
65  char graphic_alt[MAX_LEN_NAME]; // city icon of improv.
66  struct requirement_vector reqs;
67  struct requirement_vector obsolete_by;
68  int build_cost; // Use wrappers to access this.
69  int upkeep;
70  int sabotage; // Base chance of diplomat sabotage succeeding.
71  enum impr_genus_id genus; // genus; e.g. GreatWonder
72  bv_impr_flags flags;
76 
77  // Cache
82 };
83 
84 // General improvement accessor functions.
86 Impr_type_id improvement_index(const struct impr_type *pimprove);
87 Impr_type_id improvement_number(const struct impr_type *pimprove);
88 
90 
91 const struct impr_type *valid_improvement(const struct impr_type *pimprove);
92 
93 struct impr_type *improvement_by_rule_name(const char *name);
94 struct impr_type *improvement_by_translated_name(const char *name);
95 
96 const char *improvement_rule_name(const struct impr_type *pimprove);
97 const char *improvement_name_translation(const struct impr_type *pimprove);
98 
99 // General improvement flag accessor routines
100 bool improvement_has_flag(const struct impr_type *pimprove,
101  enum impr_flag_id flag);
102 
103 // Ancillary routines
104 int impr_build_shield_cost(const struct city *pcity,
105  const struct impr_type *pimprove);
106 int impr_base_build_shield_cost(const struct impr_type *pimprove);
107 int impr_estimate_build_shield_cost(const struct player *pplayer,
108  const struct tile *ptile,
109  const struct impr_type *pimprove);
110 int impr_buy_gold_cost(const struct city *pcity,
111  const struct impr_type *pimprove,
112  int shields_in_stock);
113 int impr_sell_gold(const struct impr_type *pimprove);
114 
115 bool is_improvement_visible(const struct impr_type *pimprove);
116 
117 bool is_great_wonder(const struct impr_type *pimprove);
118 bool is_small_wonder(const struct impr_type *pimprove);
119 bool is_wonder(const struct impr_type *pimprove);
120 bool is_improvement(const struct impr_type *pimprove);
121 bool is_special_improvement(const struct impr_type *pimprove);
122 
123 bool can_improvement_go_obsolete(const struct impr_type *pimprove);
124 
125 bool can_sell_building(const struct impr_type *pimprove);
126 bool can_city_sell_building(const struct city *pcity,
127  const struct impr_type *pimprove);
128 enum test_result
129 test_player_sell_building_now(struct player *pplayer, const city *pcity,
130  const struct impr_type *pimprove);
131 
132 const struct impr_type *
133 improvement_replacement(const struct impr_type *pimprove);
134 
135 // Macros for struct packet_game_info::great_wonder_owners[].
136 #define WONDER_DESTROYED \
137  (MAX_NUM_PLAYER_SLOTS + 1) /* Used as player id. \
138  */
139 #define WONDER_NOT_OWNED \
140  (MAX_NUM_PLAYER_SLOTS + 2) /* Used as player id. \
141  */
142 #define WONDER_OWNED(player_id) ((player_id) < MAX_NUM_PLAYER_SLOTS)
143 
144 // Macros for struct player::wonders[].
145 #define WONDER_LOST (-1) // Used as city id.
146 #define WONDER_NOT_BUILT 0 // Used as city id.
147 #define WONDER_BUILT(city_id) ((city_id) > 0)
148 
149 void wonder_built(const struct city *pcity,
150  const struct impr_type *pimprove);
151 void wonder_destroyed(const struct city *pcity,
152  const struct impr_type *pimprove);
153 
154 bool wonder_is_lost(const struct player *pplayer,
155  const struct impr_type *pimprove);
156 bool wonder_is_built(const struct player *pplayer,
157  const struct impr_type *pimprove);
158 struct city *city_from_wonder(const struct player *pplayer,
159  const struct impr_type *pimprove);
160 
161 bool great_wonder_is_built(const struct impr_type *pimprove);
162 bool great_wonder_is_destroyed(const struct impr_type *pimprove);
163 bool great_wonder_is_available(const struct impr_type *pimprove);
164 struct city *city_from_great_wonder(const struct impr_type *pimprove);
165 struct player *great_wonder_owner(const struct impr_type *pimprove);
166 
167 struct city *city_from_small_wonder(const struct player *pplayer,
168  const struct impr_type *pimprove);
169 
170 // player related improvement functions
171 bool improvement_obsolete(const struct player *pplayer,
172  const struct impr_type *pimprove,
173  const struct city *pcity);
174 bool is_improvement_productive(const struct city *pcity,
175  const struct impr_type *pimprove);
176 bool is_improvement_redundant(const struct city *pcity,
177  const struct impr_type *pimprove);
178 
179 bool can_player_build_improvement_direct(const struct player *p,
180  const struct impr_type *pimprove);
181 bool can_player_build_improvement_later(const struct player *p,
182  const struct impr_type *pimprove);
183 bool can_player_build_improvement_now(const struct player *p,
184  struct impr_type *pimprove);
185 
186 // Initialization and iteration
187 void improvements_init();
188 void improvements_free();
189 
191 
194 
195 #define improvement_iterate(_p) \
196  { \
197  struct impr_type *_p = improvement_array_first(); \
198  if (nullptr != _p) { \
199  for (; _p <= improvement_array_last(); _p++) {
200 
201 #define improvement_iterate_end \
202  } \
203  } \
204  }
205 
206 #define improvement_re_active_iterate(_p) \
207  improvement_iterate(_p) \
208  { \
209  if (!_p->ruledit_disabled) {
210 
211 #define improvement_re_active_iterate_end \
212  } \
213  } \
214  improvement_iterate_end;
int Impr_type_id
Definition: fc_types.h:293
test_result
Definition: fc_types.h:1063
#define MAX_LEN_NAME
Definition: fc_types.h:61
void improvements_free()
Frees the memory associated with all improvements.
Definition: improvement.cpp:68
bool can_sell_building(const struct impr_type *pimprove)
Return TRUE iff the improvement can be sold.
int impr_sell_gold(const struct impr_type *pimprove)
Returns the amount of gold received when this improvement is sold.
Impr_type_id improvement_count()
Return the number of improvements.
struct city * city_from_great_wonder(const struct impr_type *pimprove)
Get the world city with this great wonder.
bool can_city_sell_building(const struct city *pcity, const struct impr_type *pimprove)
Return TRUE iff the city can sell the given improvement.
bool can_player_build_improvement_direct(const struct player *p, const struct impr_type *pimprove)
Whether player can build given building somewhere, ignoring whether it is obsolete.
bool great_wonder_is_built(const struct impr_type *pimprove)
Returns whether this wonder is currently built.
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
struct impr_type * improvement_array_first()
Return the first item of improvements.
bool is_special_improvement(const struct impr_type *pimprove)
Returns TRUE if this is a "special" improvement.
struct impr_type * improvement_by_rule_name(const char *name)
Does a linear search of improvement_types[].name.vernacular Returns nullptr when none match.
bool is_improvement_redundant(const struct city *pcity, const struct impr_type *pimprove)
Returns TRUE if an improvement in a city is redundant, that is, the city wouldn't lose anything by lo...
bool can_player_build_improvement_later(const struct player *p, const struct impr_type *pimprove)
Whether player can eventually build given building somewhere – i.e., returns TRUE if building is avai...
void wonder_built(const struct city *pcity, const struct impr_type *pimprove)
Build a wonder in the city.
struct city * city_from_small_wonder(const struct player *pplayer, const struct impr_type *pimprove)
Get the player city with this small wonder.
void improvements_init()
Initialize building structures.
Definition: improvement.cpp:38
void improvement_feature_cache_init()
Cache features of the improvement.
Definition: improvement.cpp:77
bool great_wonder_is_destroyed(const struct impr_type *pimprove)
Returns whether this wonder has been destroyed.
const struct impr_type * valid_improvement(const struct impr_type *pimprove)
Returns pointer when the improvement_type "exists" in this game, returns nullptr otherwise.
bool wonder_is_lost(const struct player *pplayer, const struct impr_type *pimprove)
Returns whether the player has lost this wonder after having owned it (small or great).
bool can_player_build_improvement_now(const struct player *p, struct impr_type *pimprove)
Whether player can build given building somewhere immediately.
bool is_improvement(const struct impr_type *pimprove)
Is this building a regular improvement?
struct impr_type * improvement_by_number(const Impr_type_id id)
Returns the improvement type for the given index/ID.
bool wonder_is_built(const struct player *pplayer, const struct impr_type *pimprove)
Returns whether the player is currently in possession of this wonder (small or great) and it hasn't b...
int impr_base_build_shield_cost(const struct impr_type *pimprove)
Returns the base number of shields it takes to build this improvement.
Impr_type_id improvement_number(const struct impr_type *pimprove)
Return the improvement index.
const struct impr_type * improvement_array_last()
Return the last item of improvements.
bool can_improvement_go_obsolete(const struct impr_type *pimprove)
Return TRUE if the improvement can ever go obsolete.
bool is_improvement_visible(const struct impr_type *pimprove)
Return TRUE if the improvement should be visible to others without spying.
Impr_type_id improvement_index(const struct impr_type *pimprove)
Return the improvement index.
int impr_buy_gold_cost(const struct city *pcity, const struct impr_type *pimprove, int shields_in_stock)
Returns the amount of gold it takes to rush this improvement.
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
Returns the number of shields it takes to build this improvement.
struct impr_type * improvement_by_translated_name(const char *name)
Does a linear search of improvement_types[].name.translated Returns nullptr when none match.
bool is_wonder(const struct impr_type *pimprove)
Returns whether improvement is some kind of wonder.
bool is_great_wonder(const struct impr_type *pimprove)
Is this building a great wonder?
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
Returns TRUE if the improvement or wonder is obsolete.
struct player * great_wonder_owner(const struct impr_type *pimprove)
Get the player owning this small wonder.
bool improvement_has_flag(const struct impr_type *pimprove, enum impr_flag_id flag)
Return TRUE if the impr has this flag otherwise FALSE.
void wonder_destroyed(const struct city *pcity, const struct impr_type *pimprove)
Remove a wonder from a city and destroy it if it's a great wonder.
const struct impr_type * improvement_replacement(const struct impr_type *pimprove)
Try to find a sensible replacement building, based on other buildings that may have caused this one t...
struct city * city_from_wonder(const struct player *pplayer, const struct impr_type *pimprove)
Get the world city with this wonder (small or great).
const char * improvement_name_translation(const struct impr_type *pimprove)
Return the (translated) name of the given improvement.
bool is_small_wonder(const struct impr_type *pimprove)
Is this building a small wonder?
#define B_LAST
Definition: improvement.h:33
enum test_result test_player_sell_building_now(struct player *pplayer, const city *pcity, const struct impr_type *pimprove)
Return TRUE iff the player can sell the given improvement from city.
BV_DEFINE(bv_imprs, B_LAST)
int impr_estimate_build_shield_cost(const struct player *pplayer, const struct tile *ptile, const struct impr_type *pimprove)
Returns estimate of the number of shields it takes to build this improvement.
bool great_wonder_is_available(const struct impr_type *pimprove)
Returns whether this wonder can be currently built.
bool is_improvement_productive(const struct city *pcity, const struct impr_type *pimprove)
Returns TRUE if an improvement in a city is productive, in some way.
const char * name
Definition: inputfile.cpp:118
Definition: city.h:291
int build_cost
Definition: improvement.h:68
char graphic_str[MAX_LEN_NAME]
Definition: improvement.h:64
int upkeep
Definition: improvement.h:69
enum impr_genus_id genus
Definition: improvement.h:71
char graphic_alt[MAX_LEN_NAME]
Definition: improvement.h:65
bool ruledit_disabled
Definition: improvement.h:62
bool protects_vs_actions
Definition: improvement.h:81
struct requirement_vector obsolete_by
Definition: improvement.h:67
Impr_type_id item_number
Definition: improvement.h:60
bool prevents_disaster
Definition: improvement.h:80
int sabotage
Definition: improvement.h:70
char soundtag_alt[MAX_LEN_NAME]
Definition: improvement.h:75
struct requirement_vector reqs
Definition: improvement.h:66
bool allows_extras
Definition: improvement.h:79
bool allows_units
Definition: improvement.h:78
struct name_translation name
Definition: improvement.h:61
bv_impr_flags flags
Definition: improvement.h:72
QVector< QString > * helptext
Definition: improvement.h:73
char soundtag[MAX_LEN_NAME]
Definition: improvement.h:74
Definition: player.h:231
Definition: tile.h:42