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)
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 ***********************************************************************/
18 #endif /* __cplusplus */
21 #include "bitvector.h"
25 #include "connection.h"
27 #include "multipliers.h"
30 #include "spaceship.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
46 #define ANON_USER_NAME N_("Unassigned")
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"
67 struct player_economic
{
74 #define SPECENUM_NAME player_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
84 #define SPECENUM_COUNT PSTATUS_COUNT
85 #include "specenum_gen.h"
87 BV_DEFINE(bv_pstatus
, PSTATUS_COUNT
);
94 int specialists
[SP_MAX
];
100 int population
; /* in thousand of citizen */
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. */
113 int game
; /* Total score you get in player dialog. */
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
);
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,
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
{
222 #define MAX_ATTRIBUTE_BLOCK (256*1024) /* largest attribute block */
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)
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 */
244 struct government
*government
; /* may be NULL in pregame */
245 struct government
*target_government
; /* may be NULL */
246 struct nation_type
*nation
;
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 */
252 int turns_alive
; /* Number of turns this player has spent alive;
253 * 0 when created, increment at the end of each turn */
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
;
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
;
278 bool was_created
; /* if the player was /created */
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
285 int wonders
[B_LAST
]; /* contains city id's, WONDER_NOT_BUILT,
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
304 /* Only used in the server (./ai/ and ./server/). */
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. */
316 bv_player really_gives_vision
; /* takes into account that p3 may see
317 * what p1 has via p2 */
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. */
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
];
352 bool color_changeable
;
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 player_can_trust_tile_has_no_units(const struct player
*pplayer
,
404 const struct tile
*ptile
);
405 bool can_player_see_hypotetic_units_at(const struct player
*pplayer
,
406 const struct tile
*ptile
);
407 bool can_player_see_unit(const struct player
*pplayer
,
408 const struct unit
*punit
);
409 bool can_player_see_unit_at(const struct player
*pplayer
,
410 const struct unit
*punit
,
411 const struct tile
*ptile
,
412 bool is_transported
);
414 bool can_player_see_units_in_city(const struct player
*pplayer
,
415 const struct city
*pcity
);
416 bool can_player_see_city_internals(const struct player
*pplayer
,
417 const struct city
*pcity
);
418 bool player_can_see_city_externals(const struct player
*pow_player
,
419 const struct city
*target_city
);
421 bool player_owns_city(const struct player
*pplayer
,
422 const struct city
*pcity
);
423 bool player_can_invade_tile(const struct player
*pplayer
,
424 const struct tile
*ptile
);
426 struct city
*player_city_by_number(const struct player
*pplayer
,
428 struct unit
*player_unit_by_number(const struct player
*pplayer
,
431 bool player_in_city_map(const struct player
*pplayer
,
432 const struct tile
*ptile
);
433 bool player_knows_techs_with_flag(const struct player
*pplayer
,
434 enum tech_flag_id flag
);
435 int num_known_tech_with_flag(const struct player
*pplayer
,
436 enum tech_flag_id flag
);
437 int player_get_expected_income(const struct player
*pplayer
);
439 struct city
*player_capital(const struct player
*pplayer
);
441 const char *love_text(const int love
);
443 enum diplstate_type
cancel_pact_result(enum diplstate_type oldstate
);
445 struct player_diplstate
*player_diplstate_get(const struct player
*plr1
,
446 const struct player
*plr2
);
447 bool are_diplstates_equal(const struct player_diplstate
*pds1
,
448 const struct player_diplstate
*pds2
);
449 enum dipl_reason
pplayer_can_make_treaty(const struct player
*p1
,
450 const struct player
*p2
,
451 enum diplstate_type treaty
);
452 enum dipl_reason
pplayer_can_cancel_treaty(const struct player
*p1
,
453 const struct player
*p2
);
454 bool pplayers_at_war(const struct player
*pplayer
,
455 const struct player
*pplayer2
);
456 bool pplayers_allied(const struct player
*pplayer
,
457 const struct player
*pplayer2
);
458 bool pplayers_in_peace(const struct player
*pplayer
,
459 const struct player
*pplayer2
);
460 bool players_non_invade(const struct player
*pplayer1
,
461 const struct player
*pplayer2
);
462 bool pplayers_non_attack(const struct player
*pplayer
,
463 const struct player
*pplayer2
);
464 bool players_on_same_team(const struct player
*pplayer1
,
465 const struct player
*pplayer2
);
466 int player_in_territory(const struct player
*pplayer
,
467 const struct player
*pplayer2
);
469 /**************************************************************************
470 Return TRUE iff player is any kind of barbarian
471 **************************************************************************/
472 static inline bool is_barbarian(const struct player
*pplayer
)
474 return pplayer
->ai_common
.barbarian_type
!= NOT_A_BARBARIAN
;
477 bool gives_shared_vision(const struct player
*me
, const struct player
*them
);
479 void diplrel_mess_close(void);
480 bool is_diplrel_between(const struct player
*player1
,
481 const struct player
*player2
,
483 bool is_diplrel_to_other(const struct player
*pplayer
, int diplrel
);
484 int diplrel_by_rule_name(const char *value
);
485 const char *diplrel_rule_name(int value
);
486 const char *diplrel_name_translation(int value
);
488 bv_diplrel_all_reqs
diplrel_req_contradicts(const struct requirement
*req
);
490 int player_multiplier_value(const struct player
*pplayer
,
491 const struct multiplier
*pmul
);
492 int player_multiplier_effect_value(const struct player
*pplayer
,
493 const struct multiplier
*pmul
);
494 int player_multiplier_target_value(const struct player
*pplayer
,
495 const struct multiplier
*pmul
);
497 /* iterate over all player slots */
498 #define player_slots_iterate(_pslot) \
499 if (player_slots_initialised()) { \
500 struct player_slot *_pslot = player_slot_first(); \
501 for (; NULL != _pslot; _pslot = player_slot_next(_pslot)) {
502 #define player_slots_iterate_end \
506 /* iterate over all players, which are used at the moment */
507 #define players_iterate(_pplayer) \
508 player_slots_iterate(_pslot##_pplayer) { \
509 struct player *_pplayer = player_slot_get_player(_pslot##_pplayer); \
510 if (_pplayer != NULL) {
512 #define players_iterate_end \
514 } player_slots_iterate_end;
516 /* iterate over all players, which are used at the moment and are alive */
517 #define players_iterate_alive(_pplayer) \
518 players_iterate(_pplayer) { \
519 if (!_pplayer->is_alive) { \
522 #define players_iterate_alive_end \
523 } players_iterate_end;
525 /* get 'struct player_list' and related functions: */
526 #define SPECLIST_TAG player
527 #define SPECLIST_TYPE struct player
528 #include "speclist.h"
530 #define player_list_iterate(playerlist, pplayer) \
531 TYPED_LIST_ITERATE(struct player, playerlist, pplayer)
532 #define player_list_iterate_end \
535 /* ai love values should be in range [-MAX_AI_LOVE..MAX_AI_LOVE] */
536 #define MAX_AI_LOVE 1000
539 /* User functions. */
540 bool is_valid_username(const char *name
);
542 #define ai_level_cmd(_level_) ai_level_name(_level_)
543 bool is_settable_ai_level(enum ai_level level
);
544 int number_of_ai_levels(void);
546 void *player_ai_data(const struct player
*pplayer
, const struct ai_type
*ai
);
547 void player_set_ai_data(struct player
*pplayer
, const struct ai_type
*ai
,
550 static inline bool player_is_cpuhog(const struct player
*pplayer
)
552 /* You have to make code change here to enable cpuhog AI. There is no even
553 * configure option to change this. That's intentional.
554 * Enabling them causes game to proceed differently, and for reproducing
555 * reported bugs we want to know if this has been changed. People are more
556 * likely to report that they have made code changes than remembering some
557 * specific configure option they happened to pass to build this time - or even
558 * knowing what configure options somebody else used when building freeciv for them. */
564 #endif /* __cplusplus */
566 #endif /* FC__PLAYER_H */