Stop sharing requirement_unit_state_ereq().
[freeciv.git] / common / player.h
blob398ba5c8ba38a98e2573fa633cfb268777b12c28
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.
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__PLAYER_H
14 #define FC__PLAYER_H
16 #ifdef __cplusplus
17 extern "C" {
18 #endif /* __cplusplus */
20 /* utility */
21 #include "bitvector.h"
23 /* common */
24 #include "city.h"
25 #include "connection.h"
26 #include "fc_types.h"
27 #include "multipliers.h"
28 #include "nation.h"
29 #include "shared.h"
30 #include "spaceship.h"
31 #include "style.h"
32 #include "tech.h"
33 #include "traits.h"
34 #include "unitlist.h"
35 #include "vision.h"
37 #define PLAYER_DEFAULT_TAX_RATE 0
38 #define PLAYER_DEFAULT_SCIENCE_RATE 100
39 #define PLAYER_DEFAULT_LUXURY_RATE 0
41 #define ANON_PLAYER_NAME "noname"
43 /* If changing this, be sure to adjust loading of pre-2.6 savegames
44 * which depend on saved username to tell if user (or ranked_user) is
45 * anonymous. */
46 #define ANON_USER_NAME N_("Unassigned")
48 enum plrcolor_mode {
49 PLRCOL_PLR_ORDER,
50 PLRCOL_PLR_RANDOM,
51 PLRCOL_PLR_SET,
52 PLRCOL_TEAM_ORDER,
53 PLRCOL_NATION_ORDER
56 #define SPECENUM_NAME plr_flag_id
57 #define SPECENUM_VALUE0 PLRF_AI
58 #define SPECENUM_VALUE0NAME "ai"
59 #define SPECENUM_VALUE1 PLRF_SCENARIO_RESERVED
60 #define SPECENUM_VALUE1NAME "ScenarioReserved"
61 #define SPECENUM_COUNT PLRF_COUNT
62 #define SPECENUM_BITVECTOR bv_plr_flags
63 #include "specenum_gen.h"
65 struct player_slot;
67 struct player_economic {
68 int gold;
69 int tax;
70 int science;
71 int luxury;
74 #define SPECENUM_NAME player_status
75 /* 'normal' status */
76 #define SPECENUM_VALUE0 PSTATUS_NORMAL
77 /* set once the player is in the process of dying */
78 #define SPECENUM_VALUE1 PSTATUS_DYING
79 /* this player is winner in scenario game */
80 #define SPECENUM_VALUE2 PSTATUS_WINNER
81 /* has indicated willingness to surrender */
82 #define SPECENUM_VALUE3 PSTATUS_SURRENDER
83 /* keep this last */
84 #define SPECENUM_COUNT PSTATUS_COUNT
85 #include "specenum_gen.h"
87 BV_DEFINE(bv_pstatus, PSTATUS_COUNT);
89 struct player_score {
90 int happy;
91 int content;
92 int unhappy;
93 int angry;
94 int specialists[SP_MAX];
95 int wonders;
96 int techs;
97 int techout;
98 int landarea;
99 int settledarea;
100 int population; /* in thousand of citizen */
101 int cities;
102 int units;
103 int pollution;
104 int literacy;
105 int bnp;
106 int mfg;
107 int spaceship;
108 int units_built; /* Number of units this player produced. */
109 int units_killed; /* Number of enemy units killed. */
110 int units_lost; /* Number of own units that died,
111 * by combat or otherwise. */
112 int culture;
113 int game; /* Total score you get in player dialog. */
116 struct player_ai {
117 int maxbuycost;
118 void *handicaps;
119 enum ai_level skill_level; /* 0-10 value for save/load/display */
120 int fuzzy; /* chance in 1000 to mis-decide */
121 int expand; /* percentage factor to value new cities */
122 int science_cost; /* Cost in bulbs to get new tech, relative
123 to non-AI players (100: Equal cost) */
124 int warmth, frost; /* threat of global warming / nuclear winter */
125 enum barbarian_type barbarian_type;
127 int love[MAX_NUM_PLAYER_SLOTS];
129 struct ai_trait *traits;
132 /* Diplomatic states (how one player views another).
133 * (Some diplomatic states are "pacts" (mutual agreements), others aren't.)
135 * Adding to or reordering this array will break many things.
137 * Used in the network protocol.
139 #define SPECENUM_NAME diplstate_type
140 #define SPECENUM_VALUE0 DS_ARMISTICE
141 #define SPECENUM_VALUE0NAME N_("?diplomatic_state:Armistice")
142 #define SPECENUM_VALUE1 DS_WAR
143 #define SPECENUM_VALUE1NAME N_("?diplomatic_state:War")
144 #define SPECENUM_VALUE2 DS_CEASEFIRE
145 #define SPECENUM_VALUE2NAME N_("?diplomatic_state:Cease-fire")
146 #define SPECENUM_VALUE3 DS_PEACE
147 #define SPECENUM_VALUE3NAME N_("?diplomatic_state:Peace")
148 #define SPECENUM_VALUE4 DS_ALLIANCE
149 #define SPECENUM_VALUE4NAME N_("?diplomatic_state:Alliance")
150 #define SPECENUM_VALUE5 DS_NO_CONTACT
151 #define SPECENUM_VALUE5NAME N_("?diplomatic_state:Never met")
152 #define SPECENUM_VALUE6 DS_TEAM
153 #define SPECENUM_VALUE6NAME N_("?diplomatic_state:Team")
154 /* When adding or removing entries, note that first value
155 * of diplrel_other should be next to last diplstate_type */
156 #define SPECENUM_COUNT DS_LAST /* leave this last */
157 #include "specenum_gen.h"
159 /* Other diplomatic relation properties.
161 * The first element here is numbered DS_LAST
163 * Used in the network protocol.
165 #define SPECENUM_NAME diplrel_other
166 #define SPECENUM_VALUE7 DRO_GIVES_SHARED_VISION
167 #define SPECENUM_VALUE7NAME N_("Gives shared vision")
168 #define SPECENUM_VALUE8 DRO_RECEIVES_SHARED_VISION
169 #define SPECENUM_VALUE8NAME N_("Receives shared vision")
170 #define SPECENUM_VALUE9 DRO_HOSTS_EMBASSY
171 #define SPECENUM_VALUE9NAME N_("Hosts embassy")
172 #define SPECENUM_VALUE10 DRO_HAS_EMBASSY
173 #define SPECENUM_VALUE10NAME N_("Has embassy")
174 #define SPECENUM_VALUE11 DRO_HOSTS_REAL_EMBASSY
175 #define SPECENUM_VALUE11NAME N_("Hosts real embassy")
176 #define SPECENUM_VALUE12 DRO_HAS_REAL_EMBASSY
177 #define SPECENUM_VALUE12NAME N_("Has real embassy")
178 #define SPECENUM_VALUE13 DRO_HAS_CASUS_BELLI
179 #define SPECENUM_VALUE13NAME N_("Has Casus Belli")
180 #define SPECENUM_VALUE14 DRO_PROVIDED_CASUS_BELLI
181 #define SPECENUM_VALUE14NAME N_("Provided Casus Belli")
182 #define SPECENUM_VALUE15 DRO_FOREIGN
183 #define SPECENUM_VALUE15NAME N_("Foreign")
184 #define SPECENUM_COUNT DRO_LAST
185 #include "specenum_gen.h"
187 BV_DEFINE(bv_diplrel_all_reqs,
188 /* Reserve a location for each possible DiplRel requirement. */
189 ((DRO_LAST - 1) * 2) * REQ_RANGE_COUNT);
191 enum dipl_reason {
192 DIPL_OK, DIPL_ERROR, DIPL_SENATE_BLOCKING,
193 DIPL_ALLIANCE_PROBLEM_US, DIPL_ALLIANCE_PROBLEM_THEM
196 /* the following are for "pacts" */
197 struct player_diplstate {
198 enum diplstate_type type; /* this player's disposition towards other */
199 enum diplstate_type max_state; /* maximum treaty level ever had */
200 int first_contact_turn; /* turn we had first contact with this player */
201 int turns_left; /* until pact (e.g., cease-fire) ends */
202 int has_reason_to_cancel; /* 0: no, 1: this turn, 2: this or next turn */
203 int contact_turns_left; /* until contact ends */
205 int auto_cancel_turn; /* used to avoid asymmetric turns_left */
208 /***************************************************************************
209 On the distinction between nations(formerly races), players, and users,
210 see doc/HACKING
211 ***************************************************************************/
213 enum player_debug_types {
214 PLAYER_DEBUG_DIPLOMACY, PLAYER_DEBUG_TECH, PLAYER_DEBUG_LAST
217 BV_DEFINE(bv_debug, PLAYER_DEBUG_LAST);
219 struct attribute_block_s {
220 void *data;
221 int length;
222 #define MAX_ATTRIBUTE_BLOCK (256*1024) /* largest attribute block */
225 struct ai_type;
226 struct ai_data;
228 bool player_has_flag(const struct player *pplayer, enum plr_flag_id flag);
230 #define is_human(plr) !player_has_flag((plr), PLRF_AI)
231 #define is_ai(plr) player_has_flag((plr), PLRF_AI)
232 #define set_as_human(plr) BV_CLR((plr)->flags, PLRF_AI)
233 #define set_as_ai(plr) BV_SET((plr)->flags, PLRF_AI)
235 struct player {
236 struct player_slot *slot;
237 char name[MAX_LEN_NAME];
238 char username[MAX_LEN_NAME];
239 bool unassigned_user;
240 char ranked_username[MAX_LEN_NAME]; /* the user who will be ranked */
241 bool unassigned_ranked;
242 int user_turns; /* number of turns this user has played */
243 bool is_male;
244 struct government *government; /* may be NULL in pregame */
245 struct government *target_government; /* may be NULL */
246 struct nation_type *nation;
247 struct team *team;
248 bool is_ready; /* Did the player click "start" yet? */
249 bool phase_done; /* Has human player finished */
250 bool ai_phase_done; /* Has AI type finished */
251 int nturns_idle;
252 int turns_alive; /* Number of turns this player has spent alive;
253 * 0 when created, increment at the end of each turn */
254 bool is_alive;
255 bool is_winner;
256 int last_war_action;
258 /* Turn in which the player's revolution is over; see update_revolution. */
259 int revolution_finishes;
261 bv_player real_embassy;
262 const struct player_diplstate **diplstates;
263 struct nation_style *style;
264 int music_style;
265 struct city_list *cities;
266 struct unit_list *units;
267 struct player_score score;
268 struct player_economic economic;
270 struct player_spaceship spaceship;
272 struct player_ai ai_common;
273 const struct ai_type *ai;
274 char *savegame_ai_type_name;
276 bv_plr_flags flags;
278 bool was_created; /* if the player was /created */
279 bool random_name;
280 bool is_connected;
281 struct connection *current_conn; /* non-null while handling packet */
282 struct conn_list *connections; /* will replace conn */
283 bv_player gives_shared_vision; /* bitvector those that give you
284 * shared vision */
285 int wonders[B_LAST]; /* contains city id's, WONDER_NOT_BUILT,
286 * or WONDER_LOST */
287 struct attribute_block_s attribute_block;
288 struct attribute_block_s attribute_block_buffer;
290 struct dbv tile_known;
292 struct rgbcolor *rgb;
294 /* Values currently in force. */
295 int multipliers[MAX_NUM_MULTIPLIERS];
296 /* Values to be used next turn. */
297 int multipliers_target[MAX_NUM_MULTIPLIERS];
299 int culture; /* National level culture - does not include culture of individual
300 * cities. */
302 union {
303 struct {
304 /* Only used in the server (./ai/ and ./server/). */
305 bv_pstatus status;
307 bool got_first_city; /* used to give player init_buildings in first
308 * city. Once set, never becomes unset.
309 * (Previously 'capital'.) */
311 struct player_tile *private_map;
313 /* Player can see inside his borders. */
314 bool border_vision;
316 bv_player really_gives_vision; /* takes into account that p3 may see
317 * what p1 has via p2 */
319 bv_debug debug;
321 struct adv_data *adv;
323 void *ais[FREECIV_AI_MOD_LAST];
325 /* This user is allowed to take over the player. */
326 char delegate_to[MAX_LEN_NAME];
327 /* This is set when a player is 'involved' in a delegation.
328 * There are two cases:
329 * - if delegate_to[] is set, it records the original owner, with
330 * 'username' temporarily holding the delegate's name;
331 * - otherwise, it's set when a delegate's original player is 'put
332 * aside' while the delegate user controls a delegated player.
333 * (In this case orig_username == username.) */
334 char orig_username[MAX_LEN_NAME];
336 int huts; /* How many huts this player has found */
338 int bulbs_last_turn; /* Number of bulbs researched last turn only. */
339 } server;
341 struct {
342 /* Only used at the client (the server is omniscient; ./client/). */
344 /* Corresponds to the result of
345 (player:server:private_map[tile_index]:seen_count[vlayer] != 0). */
346 struct dbv tile_vision[V_COUNT];
348 enum mood_type mood;
350 int tech_upkeep;
352 bool color_changeable;
353 } client;
357 /* Initialization and iteration */
358 void player_slots_init(void);
359 bool player_slots_initialised(void);
360 void player_slots_free(void);
362 struct player_slot *player_slot_first(void);
363 struct player_slot *player_slot_next(struct player_slot *pslot);
365 /* A player slot contains a possibly uninitialized player. */
366 int player_slot_count(void);
367 int player_slot_index(const struct player_slot *pslot);
368 struct player *player_slot_get_player(const struct player_slot *pslot);
369 bool player_slot_is_used(const struct player_slot *pslot);
370 struct player_slot *player_slot_by_number(int player_id);
371 int player_slot_max_used_number(void);
373 /* General player accessor functions. */
374 struct player *player_new(struct player_slot *pslot);
375 void player_set_color(struct player *pplayer,
376 const struct rgbcolor *prgbcolor);
377 void player_clear(struct player *pplayer, bool full);
378 void player_ruleset_close(struct player *pplayer);
379 void player_destroy(struct player *pplayer);
381 int player_count(void);
382 int player_index(const struct player *pplayer);
383 int player_number(const struct player *pplayer);
384 struct player *player_by_number(const int player_id);
386 const char *player_name(const struct player *pplayer);
387 struct player *player_by_name(const char *name);
388 struct player *player_by_name_prefix(const char *name,
389 enum m_pre_result *result);
390 struct player *player_by_user(const char *name);
392 bool player_set_nation(struct player *pplayer, struct nation_type *pnation);
394 bool player_has_embassy(const struct player *pplayer,
395 const struct player *pplayer2);
396 bool player_has_real_embassy(const struct player *pplayer,
397 const struct player *pplayer2);
398 bool player_has_embassy_from_effect(const struct player *pplayer,
399 const struct player *pplayer2);
401 int player_age(const struct player *pplayer);
403 bool can_player_see_hypotetic_units_at(const struct player *pplayer,
404 const struct tile *ptile);
405 bool can_player_see_unit(const struct player *pplayer,
406 const struct unit *punit);
407 bool can_player_see_unit_at(const struct player *pplayer,
408 const struct unit *punit,
409 const struct tile *ptile,
410 bool is_transported);
412 bool can_player_see_units_in_city(const struct player *pplayer,
413 const struct city *pcity);
414 bool can_player_see_city_internals(const struct player *pplayer,
415 const struct city *pcity);
416 bool player_can_see_city_externals(const struct player *pow_player,
417 const struct city *target_city);
419 bool player_owns_city(const struct player *pplayer,
420 const struct city *pcity);
421 bool player_can_invade_tile(const struct player *pplayer,
422 const struct tile *ptile);
424 struct city *player_city_by_number(const struct player *pplayer,
425 int city_id);
426 struct unit *player_unit_by_number(const struct player *pplayer,
427 int unit_id);
429 bool player_in_city_map(const struct player *pplayer,
430 const struct tile *ptile);
431 bool player_knows_techs_with_flag(const struct player *pplayer,
432 enum tech_flag_id flag);
433 int num_known_tech_with_flag(const struct player *pplayer,
434 enum tech_flag_id flag);
435 int player_get_expected_income(const struct player *pplayer);
437 struct city *player_capital(const struct player *pplayer);
439 const char *love_text(const int love);
441 enum diplstate_type cancel_pact_result(enum diplstate_type oldstate);
443 struct player_diplstate *player_diplstate_get(const struct player *plr1,
444 const struct player *plr2);
445 bool are_diplstates_equal(const struct player_diplstate *pds1,
446 const struct player_diplstate *pds2);
447 enum dipl_reason pplayer_can_make_treaty(const struct player *p1,
448 const struct player *p2,
449 enum diplstate_type treaty);
450 enum dipl_reason pplayer_can_cancel_treaty(const struct player *p1,
451 const struct player *p2);
452 bool pplayers_at_war(const struct player *pplayer,
453 const struct player *pplayer2);
454 bool pplayers_allied(const struct player *pplayer,
455 const struct player *pplayer2);
456 bool pplayers_in_peace(const struct player *pplayer,
457 const struct player *pplayer2);
458 bool players_non_invade(const struct player *pplayer1,
459 const struct player *pplayer2);
460 bool pplayers_non_attack(const struct player *pplayer,
461 const struct player *pplayer2);
462 bool players_on_same_team(const struct player *pplayer1,
463 const struct player *pplayer2);
464 int player_in_territory(const struct player *pplayer,
465 const struct player *pplayer2);
467 /**************************************************************************
468 Return TRUE iff player is any kind of barbarian
469 **************************************************************************/
470 static inline bool is_barbarian(const struct player *pplayer)
472 return pplayer->ai_common.barbarian_type != NOT_A_BARBARIAN;
475 bool gives_shared_vision(const struct player *me, const struct player *them);
477 void diplrel_mess_close(void);
478 bool is_diplrel_between(const struct player *player1,
479 const struct player *player2,
480 int diplrel);
481 bool is_diplrel_to_other(const struct player *pplayer, int diplrel);
482 int diplrel_by_rule_name(const char *value);
483 const char *diplrel_rule_name(int value);
484 const char *diplrel_name_translation(int value);
486 bv_diplrel_all_reqs diplrel_req_contradicts(const struct requirement *req);
488 int player_multiplier_value(const struct player *pplayer,
489 const struct multiplier *pmul);
490 int player_multiplier_effect_value(const struct player *pplayer,
491 const struct multiplier *pmul);
492 int player_multiplier_target_value(const struct player *pplayer,
493 const struct multiplier *pmul);
495 /* iterate over all player slots */
496 #define player_slots_iterate(_pslot) \
497 if (player_slots_initialised()) { \
498 struct player_slot *_pslot = player_slot_first(); \
499 for (; NULL != _pslot; _pslot = player_slot_next(_pslot)) {
500 #define player_slots_iterate_end \
504 /* iterate over all players, which are used at the moment */
505 #define players_iterate(_pplayer) \
506 player_slots_iterate(_pslot##_pplayer) { \
507 struct player *_pplayer = player_slot_get_player(_pslot##_pplayer); \
508 if (_pplayer != NULL) {
510 #define players_iterate_end \
512 } player_slots_iterate_end;
514 /* iterate over all players, which are used at the moment and are alive */
515 #define players_iterate_alive(_pplayer) \
516 players_iterate(_pplayer) { \
517 if (!_pplayer->is_alive) { \
518 continue; \
520 #define players_iterate_alive_end \
521 } players_iterate_end;
523 /* get 'struct player_list' and related functions: */
524 #define SPECLIST_TAG player
525 #define SPECLIST_TYPE struct player
526 #include "speclist.h"
528 #define player_list_iterate(playerlist, pplayer) \
529 TYPED_LIST_ITERATE(struct player, playerlist, pplayer)
530 #define player_list_iterate_end \
531 LIST_ITERATE_END
533 /* ai love values should be in range [-MAX_AI_LOVE..MAX_AI_LOVE] */
534 #define MAX_AI_LOVE 1000
537 /* User functions. */
538 bool is_valid_username(const char *name);
540 #define ai_level_cmd(_level_) ai_level_name(_level_)
541 bool is_settable_ai_level(enum ai_level level);
542 int number_of_ai_levels(void);
544 void *player_ai_data(const struct player *pplayer, const struct ai_type *ai);
545 void player_set_ai_data(struct player *pplayer, const struct ai_type *ai,
546 void *data);
548 static inline bool player_is_cpuhog(const struct player *pplayer)
550 /* You have to make code change here to enable cpuhog AI. There is no even
551 * configure option to change this. That's intentional.
552 * Enabling them causes game to proceed differently, and for reproducing
553 * reported bugs we want to know if this has been changed. People are more
554 * likely to report that they have made code changes than remembering some
555 * specific configure option they happened to pass to build this time - or even
556 * knowing what configure options somebody else used when building freeciv for them. */
557 return FALSE;
560 #ifdef __cplusplus
562 #endif /* __cplusplus */
564 #endif /* FC__PLAYER_H */