Stop sharing requirement_unit_state_ereq().
[freeciv.git] / common / player.c
blob1d4f8936519f5af0e624044981d74642f4b3e702
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "fcintl.h"
20 #include "log.h"
21 #include "mem.h"
22 #include "shared.h"
23 #include "support.h"
25 /* common */
26 #include "ai.h"
27 #include "city.h"
28 #include "fc_interface.h"
29 #include "featured_text.h"
30 #include "game.h"
31 #include "government.h"
32 #include "idex.h"
33 #include "improvement.h"
34 #include "map.h"
35 #include "research.h"
36 #include "rgbcolor.h"
37 #include "tech.h"
38 #include "unit.h"
39 #include "unitlist.h"
40 #include "vision.h"
42 #include "player.h"
45 struct player_slot {
46 struct player *player;
49 static struct {
50 struct player_slot *slots;
51 int used_slots; /* number of used/allocated players in the player slots */
52 } player_slots;
54 static void player_defaults(struct player *pplayer);
56 static void player_diplstate_new(const struct player *plr1,
57 const struct player *plr2);
58 static void player_diplstate_defaults(const struct player *plr1,
59 const struct player *plr2);
60 static void player_diplstate_destroy(const struct player *plr1,
61 const struct player *plr2);
63 /***************************************************************
64 Return the diplomatic state that cancelling a pact will
65 end up in.
66 ***************************************************************/
67 enum diplstate_type cancel_pact_result(enum diplstate_type oldstate)
69 switch(oldstate) {
70 case DS_NO_CONTACT: /* possible if someone declares war on our ally */
71 case DS_WAR: /* no change */
72 case DS_ARMISTICE:
73 case DS_CEASEFIRE:
74 case DS_PEACE:
75 return DS_WAR;
76 case DS_ALLIANCE:
77 return DS_ARMISTICE;
78 case DS_TEAM: /* no change */
79 return DS_TEAM;
80 default:
81 log_error("non-pact diplstate %d in cancel_pact_result", oldstate);
82 return DS_WAR; /* arbitrary */
86 /***************************************************************
87 The senate may not allow you to break the treaty. In this
88 case you must first dissolve the senate then you can break
89 it. This is waived if you have statue of liberty since you
90 could easily just dissolve and then recreate it.
91 ***************************************************************/
92 enum dipl_reason pplayer_can_cancel_treaty(const struct player *p1,
93 const struct player *p2)
95 enum diplstate_type ds = player_diplstate_get(p1, p2)->type;
97 if (p1 == p2 || ds == DS_WAR || ds == DS_NO_CONTACT) {
98 return DIPL_ERROR;
100 if (players_on_same_team(p1, p2)) {
101 return DIPL_ERROR;
103 if (!p1->is_alive || !p2->is_alive) {
104 return DIPL_ERROR;
106 if (player_diplstate_get(p1, p2)->has_reason_to_cancel == 0
107 && get_player_bonus(p1, EFT_HAS_SENATE) > 0
108 && get_player_bonus(p1, EFT_NO_ANARCHY) <= 0) {
109 return DIPL_SENATE_BLOCKING;
111 return DIPL_OK;
114 /***************************************************************
115 Returns true iff p1 can be in alliance with p2.
117 Check that we are not at war with any of p2's allies. Note
118 that for an alliance to be made, we need to check this both
119 ways.
121 The reason for this is to avoid the dread 'love-love-hate'
122 triad, in which p1 is allied to p2 is allied to p3 is at
123 war with p1. These lead to strange situations.
124 ***************************************************************/
125 static bool is_valid_alliance(const struct player *p1,
126 const struct player *p2)
128 players_iterate_alive(pplayer) {
129 enum diplstate_type ds = player_diplstate_get(p1, pplayer)->type;
131 if (pplayer != p1
132 && pplayer != p2
133 && ds == DS_WAR /* do not count 'never met' as war here */
134 && pplayers_allied(p2, pplayer)) {
135 return FALSE;
137 } players_iterate_alive_end;
139 return TRUE;
142 /***************************************************************
143 Returns true iff p1 can make given treaty with p2.
145 We cannot regress in a treaty chain. So we cannot suggest
146 'Peace' if we are in 'Alliance'. Then you have to cancel.
148 For alliance there is only one condition: We are not at war
149 with any of p2's allies.
150 ***************************************************************/
151 enum dipl_reason pplayer_can_make_treaty(const struct player *p1,
152 const struct player *p2,
153 enum diplstate_type treaty)
155 enum diplstate_type existing = player_diplstate_get(p1, p2)->type;
157 if (players_on_same_team(p1, p2)) {
158 /* This includes the case p1 == p2 */
159 return DIPL_ERROR;
161 if (get_player_bonus(p1, EFT_NO_DIPLOMACY) > 0
162 || get_player_bonus(p2, EFT_NO_DIPLOMACY) > 0) {
163 return DIPL_ERROR;
165 if (treaty == DS_WAR
166 || treaty == DS_NO_CONTACT
167 || treaty == DS_ARMISTICE
168 || treaty == DS_TEAM
169 || treaty == DS_LAST) {
170 return DIPL_ERROR; /* these are not negotiable treaties */
172 if (treaty == DS_CEASEFIRE && existing != DS_WAR) {
173 return DIPL_ERROR; /* only available from war */
175 if (treaty == DS_PEACE
176 && (existing != DS_WAR && existing != DS_CEASEFIRE)) {
177 return DIPL_ERROR;
179 if (treaty == DS_ALLIANCE) {
180 if (!is_valid_alliance(p1, p2)) {
181 /* Our war with a third party prevents entry to alliance. */
182 return DIPL_ALLIANCE_PROBLEM_US;
183 } else if (!is_valid_alliance(p2, p1)) {
184 /* Their war with a third party prevents entry to alliance. */
185 return DIPL_ALLIANCE_PROBLEM_THEM;
188 /* this check must be last: */
189 if (treaty == existing) {
190 return DIPL_ERROR;
192 return DIPL_OK;
195 /***************************************************************
196 Check if pplayer has an embassy with pplayer2. We always have
197 an embassy with ourselves.
198 ***************************************************************/
199 bool player_has_embassy(const struct player *pplayer,
200 const struct player *pplayer2)
202 return (pplayer == pplayer2
203 || player_has_real_embassy(pplayer, pplayer2)
204 || player_has_embassy_from_effect(pplayer, pplayer2));
207 /***************************************************************
208 Returns whether pplayer has a real embassy with pplayer2,
209 established from a diplomat, or through diplomatic meeting.
210 ***************************************************************/
211 bool player_has_real_embassy(const struct player *pplayer,
212 const struct player *pplayer2)
214 return BV_ISSET(pplayer->real_embassy, player_index(pplayer2));
217 /***************************************************************
218 Returns whether pplayer has got embassy with pplayer2 thanks
219 to an effect (e.g. Macro Polo Embassy).
220 ***************************************************************/
221 bool player_has_embassy_from_effect(const struct player *pplayer,
222 const struct player *pplayer2)
224 return (get_player_bonus(pplayer, EFT_HAVE_EMBASSIES) > 0
225 && player_diplstate_get(pplayer, pplayer2)->type != DS_NO_CONTACT
226 && !is_barbarian(pplayer2));
229 /****************************************************************************
230 Return TRUE iff the given player owns the city.
231 ****************************************************************************/
232 bool player_owns_city(const struct player *pplayer, const struct city *pcity)
234 return (pcity && pplayer && city_owner(pcity) == pplayer);
237 /****************************************************************************
238 Return TRUE iff the player can invade a particular tile (linked with
239 borders and diplomatic states).
240 ****************************************************************************/
241 bool player_can_invade_tile(const struct player *pplayer,
242 const struct tile *ptile)
244 const struct player *ptile_owner = tile_owner(ptile);
246 return (!ptile_owner
247 || ptile_owner == pplayer
248 || !players_non_invade(pplayer, ptile_owner));
251 /****************************************************************************
252 Allocate new diplstate structure for tracking state between given two
253 players.
254 ****************************************************************************/
255 static void player_diplstate_new(const struct player *plr1,
256 const struct player *plr2)
258 struct player_diplstate *diplstate;
260 fc_assert_ret(plr1 != NULL);
261 fc_assert_ret(plr2 != NULL);
263 const struct player_diplstate **diplstate_slot
264 = plr1->diplstates + player_index(plr2);
266 fc_assert_ret(*diplstate_slot == NULL);
268 diplstate = fc_calloc(1, sizeof(*diplstate));
269 *diplstate_slot = diplstate;
272 /****************************************************************************
273 Set diplstate between given two players to default values.
274 ****************************************************************************/
275 static void player_diplstate_defaults(const struct player *plr1,
276 const struct player *plr2)
278 struct player_diplstate *diplstate = player_diplstate_get(plr1, plr2);
280 fc_assert_ret(diplstate != NULL);
282 diplstate->type = DS_NO_CONTACT;
283 diplstate->max_state = DS_NO_CONTACT;
284 diplstate->first_contact_turn = 0;
285 diplstate->turns_left = 0;
286 diplstate->has_reason_to_cancel = 0;
287 diplstate->contact_turns_left = 0;
288 diplstate->auto_cancel_turn = -1;
292 /***************************************************************
293 Returns diplomatic state type between two players
294 ***************************************************************/
295 struct player_diplstate *player_diplstate_get(const struct player *plr1,
296 const struct player *plr2)
298 fc_assert_ret_val(plr1 != NULL, NULL);
299 fc_assert_ret_val(plr2 != NULL, NULL);
301 const struct player_diplstate **diplstate_slot
302 = plr1->diplstates + player_index(plr2);
304 fc_assert_ret_val(*diplstate_slot != NULL, NULL);
306 return (struct player_diplstate *) *diplstate_slot;
309 /****************************************************************************
310 Free resources used by diplstate between given two players.
311 ****************************************************************************/
312 static void player_diplstate_destroy(const struct player *plr1,
313 const struct player *plr2)
315 fc_assert_ret(plr1 != NULL);
316 fc_assert_ret(plr2 != NULL);
318 const struct player_diplstate **diplstate_slot
319 = plr1->diplstates + player_index(plr2);
321 if (*diplstate_slot != NULL) {
322 free(player_diplstate_get(plr1, plr2));
325 *diplstate_slot = NULL;
328 /***************************************************************
329 Initialise all player slots (= pointer to player pointers).
330 ***************************************************************/
331 void player_slots_init(void)
333 int i;
335 /* Init player slots. */
336 player_slots.slots = fc_calloc(player_slot_count(),
337 sizeof(*player_slots.slots));
338 /* Can't use the defined functions as the needed data will be
339 * defined here. */
340 for (i = 0; i < player_slot_count(); i++) {
341 player_slots.slots[i].player = NULL;
343 player_slots.used_slots = 0;
346 /***************************************************************
347 Return whether player slots are already initialized.
348 ***************************************************************/
349 bool player_slots_initialised(void)
351 return (player_slots.slots != NULL);
354 /***************************************************************
355 Remove all player slots.
356 ***************************************************************/
357 void player_slots_free(void)
359 players_iterate(pplayer) {
360 player_destroy(pplayer);
361 } players_iterate_end;
362 free(player_slots.slots);
363 player_slots.slots = NULL;
364 player_slots.used_slots = 0;
367 /****************************************************************************
368 Returns the first player slot.
369 ****************************************************************************/
370 struct player_slot *player_slot_first(void)
372 return player_slots.slots;
375 /****************************************************************************
376 Returns the next slot.
377 ****************************************************************************/
378 struct player_slot *player_slot_next(struct player_slot *pslot)
380 pslot++;
381 return (pslot < player_slots.slots + player_slot_count() ? pslot : NULL);
384 /***************************************************************
385 Returns the total number of player slots, i.e. the maximum
386 number of players (including barbarians, etc.) that could ever
387 exist at once.
388 ***************************************************************/
389 int player_slot_count(void)
391 return (MAX_NUM_PLAYER_SLOTS);
394 /****************************************************************************
395 Returns the index of the player slot.
396 ****************************************************************************/
397 int player_slot_index(const struct player_slot *pslot)
399 fc_assert_ret_val(NULL != pslot, -1);
401 return pslot - player_slots.slots;
404 /****************************************************************************
405 Returns the team corresponding to the slot. If the slot is not used, it
406 will return NULL. See also player_slot_is_used().
407 ****************************************************************************/
408 struct player *player_slot_get_player(const struct player_slot *pslot)
410 fc_assert_ret_val(NULL != pslot, NULL);
412 return pslot->player;
415 /****************************************************************************
416 Returns TRUE is this slot is "used" i.e. corresponds to a valid,
417 initialized player that exists in the game.
418 ****************************************************************************/
419 bool player_slot_is_used(const struct player_slot *pslot)
421 fc_assert_ret_val(NULL != pslot, FALSE);
423 /* No player slot available, if the game is not initialised. */
424 if (!player_slots_initialised()) {
425 return FALSE;
428 return NULL != pslot->player;
431 /****************************************************************************
432 Return the possibly unused and uninitialized player slot.
433 ****************************************************************************/
434 struct player_slot *player_slot_by_number(int player_id)
436 if (!player_slots_initialised()
437 || !(0 <= player_id && player_id < player_slot_count())) {
438 return NULL;
441 return player_slots.slots + player_id;
444 /****************************************************************************
445 Return the highest used player slot index.
446 ****************************************************************************/
447 int player_slot_max_used_number(void)
449 int max_pslot = 0;
451 player_slots_iterate(pslot) {
452 if (player_slot_is_used(pslot)) {
453 max_pslot = player_slot_index(pslot);
455 } player_slots_iterate_end;
457 return max_pslot;
460 /****************************************************************************
461 Creates a new player for the slot. If slot is NULL, it will lookup to a
462 free slot. If the slot already used, then just return the player.
463 ****************************************************************************/
464 struct player *player_new(struct player_slot *pslot)
466 struct player *pplayer;
468 fc_assert_ret_val(player_slots_initialised(), NULL);
470 if (NULL == pslot) {
471 player_slots_iterate(aslot) {
472 if (!player_slot_is_used(aslot)) {
473 pslot = aslot;
474 break;
476 } player_slots_iterate_end;
478 fc_assert_ret_val(NULL != pslot, NULL);
479 } else if (NULL != pslot->player) {
480 return pslot->player;
483 /* Now create the player. */
484 log_debug("Create player for slot %d.", player_slot_index(pslot));
485 pplayer = fc_calloc(1, sizeof(*pplayer));
486 pplayer->slot = pslot;
487 pslot->player = pplayer;
489 pplayer->diplstates = fc_calloc(player_slot_count(),
490 sizeof(*pplayer->diplstates));
491 player_slots_iterate(dslot) {
492 const struct player_diplstate **diplstate_slot
493 = pplayer->diplstates + player_slot_index(dslot);
495 *diplstate_slot = NULL;
496 } player_slots_iterate_end;
498 players_iterate(aplayer) {
499 /* Create diplomatic states for all other players. */
500 player_diplstate_new(pplayer, aplayer);
501 /* Create diplomatic state of this player. */
502 if (aplayer != pplayer) {
503 player_diplstate_new(aplayer, pplayer);
505 } players_iterate_end;
507 /* Set default values. */
508 player_defaults(pplayer);
510 /* Increase number of players. */
511 player_slots.used_slots++;
513 return pplayer;
516 /****************************************************************************
517 Set player structure to its default values.
518 No initialisation to ruleset-dependent values should be done here.
519 ****************************************************************************/
520 static void player_defaults(struct player *pplayer)
522 int i;
524 sz_strlcpy(pplayer->name, ANON_PLAYER_NAME);
525 sz_strlcpy(pplayer->username, _(ANON_USER_NAME));
526 pplayer->unassigned_user = TRUE;
527 sz_strlcpy(pplayer->ranked_username, _(ANON_USER_NAME));
528 pplayer->unassigned_ranked = TRUE;
529 pplayer->user_turns = 0;
530 pplayer->is_male = TRUE;
531 pplayer->government = NULL;
532 pplayer->target_government = NULL;
533 pplayer->nation = NO_NATION_SELECTED;
534 pplayer->team = NULL;
535 pplayer->is_ready = FALSE;
536 pplayer->nturns_idle = 0;
537 pplayer->is_alive = TRUE;
538 pplayer->turns_alive = 0;
539 pplayer->is_winner = FALSE;
540 pplayer->last_war_action = -1;
541 pplayer->phase_done = FALSE;
543 pplayer->revolution_finishes = -1;
545 BV_CLR_ALL(pplayer->real_embassy);
546 players_iterate(aplayer) {
547 /* create diplomatic states for all other players */
548 player_diplstate_defaults(pplayer, aplayer);
549 /* create diplomatic state of this player */
550 if (aplayer != pplayer) {
551 player_diplstate_defaults(aplayer, pplayer);
553 } players_iterate_end;
555 pplayer->style = 0;
556 pplayer->music_style = -1; /* even getting value 0 triggers change */
557 pplayer->cities = city_list_new();
558 pplayer->units = unit_list_new();
560 pplayer->economic.gold = 0;
561 pplayer->economic.tax = PLAYER_DEFAULT_TAX_RATE;
562 pplayer->economic.science = PLAYER_DEFAULT_SCIENCE_RATE;
563 pplayer->economic.luxury = PLAYER_DEFAULT_LUXURY_RATE;
565 spaceship_init(&pplayer->spaceship);
567 BV_CLR_ALL(pplayer->flags);
569 set_as_human(pplayer);
570 pplayer->ai_common.skill_level = ai_level_invalid();
571 pplayer->ai_common.fuzzy = 0;
572 pplayer->ai_common.expand = 100;
573 pplayer->ai_common.barbarian_type = NOT_A_BARBARIAN;
574 player_slots_iterate(pslot) {
575 pplayer->ai_common.love[player_slot_index(pslot)] = 1;
576 } player_slots_iterate_end;
577 pplayer->ai_common.traits = NULL;
579 pplayer->ai = NULL;
580 pplayer->was_created = FALSE;
581 pplayer->savegame_ai_type_name = NULL;
582 pplayer->random_name = TRUE;
583 pplayer->is_connected = FALSE;
584 pplayer->current_conn = NULL;
585 pplayer->connections = conn_list_new();
586 BV_CLR_ALL(pplayer->gives_shared_vision);
587 for (i = 0; i < B_LAST; i++) {
588 pplayer->wonders[i] = WONDER_NOT_BUILT;
591 pplayer->attribute_block.data = NULL;
592 pplayer->attribute_block.length = 0;
593 pplayer->attribute_block_buffer.data = NULL;
594 pplayer->attribute_block_buffer.length = 0;
596 pplayer->tile_known.vec = NULL;
597 pplayer->tile_known.bits = 0;
599 pplayer->rgb = NULL;
601 memset(pplayer->multipliers, 0, sizeof(pplayer->multipliers));
602 memset(pplayer->multipliers_target, 0, sizeof(pplayer->multipliers_target));
604 /* pplayer->server is initialised in
605 ./server/plrhand.c:server_player_init()
606 and pplayer->client in
607 ./client/climisc.c:client_player_init() */
610 /****************************************************************************
611 Set the player's color.
612 May be NULL in pregame.
613 ****************************************************************************/
614 void player_set_color(struct player *pplayer,
615 const struct rgbcolor *prgbcolor)
617 if (pplayer->rgb != NULL) {
618 rgbcolor_destroy(pplayer->rgb);
619 pplayer->rgb = NULL;
622 if (prgbcolor) {
623 pplayer->rgb = rgbcolor_copy(prgbcolor);
627 /****************************************************************************
628 Clear all player data. If full is set, then the nation and the team will
629 be cleared too.
630 ****************************************************************************/
631 void player_clear(struct player *pplayer, bool full)
633 bool client = !is_server();
635 if (pplayer == NULL) {
636 return;
639 if (pplayer->savegame_ai_type_name != NULL) {
640 free(pplayer->savegame_ai_type_name);
641 pplayer->savegame_ai_type_name = NULL;
644 /* Clears the attribute blocks. */
645 if (pplayer->attribute_block.data) {
646 free(pplayer->attribute_block.data);
647 pplayer->attribute_block.data = NULL;
649 pplayer->attribute_block.length = 0;
651 if (pplayer->attribute_block_buffer.data) {
652 free(pplayer->attribute_block_buffer.data);
653 pplayer->attribute_block_buffer.data = NULL;
655 pplayer->attribute_block_buffer.length = 0;
657 /* Clears units and cities. */
658 unit_list_iterate(pplayer->units, punit) {
659 /* Unload all cargos. */
660 unit_list_iterate(unit_transport_cargo(punit), pcargo) {
661 unit_transport_unload(pcargo);
662 if (client) {
663 pcargo->client.transported_by = -1;
665 } unit_list_iterate_end;
666 /* Unload the unit. */
667 unit_transport_unload(punit);
668 if (client) {
669 punit->client.transported_by = -1;
672 game_remove_unit(punit);
673 } unit_list_iterate_end;
675 city_list_iterate(pplayer->cities, pcity) {
676 game_remove_city(pcity);
677 } city_list_iterate_end;
679 if (full) {
680 team_remove_player(pplayer);
682 /* This comes last because log calls in the above functions
683 * may use it. */
684 if (pplayer->nation != NULL) {
685 player_set_nation(pplayer, NULL);
690 /****************************************************************************
691 Clear the ruleset dependent pointers of the player structure. Called by
692 game_ruleset_free().
693 ****************************************************************************/
694 void player_ruleset_close(struct player *pplayer)
696 pplayer->government = NULL;
697 pplayer->target_government = NULL;
698 player_set_nation(pplayer, NULL);
699 pplayer->style = NULL;
702 /****************************************************************************
703 Destroys and remove a player from the game.
704 ****************************************************************************/
705 void player_destroy(struct player *pplayer)
707 struct player_slot *pslot;
709 fc_assert_ret(NULL != pplayer);
711 pslot = pplayer->slot;
712 fc_assert(pslot->player == pplayer);
714 /* Remove all that is game-dependent in the player structure. */
715 player_clear(pplayer, TRUE);
717 fc_assert(0 == unit_list_size(pplayer->units));
718 unit_list_destroy(pplayer->units);
719 fc_assert(0 == city_list_size(pplayer->cities));
720 city_list_destroy(pplayer->cities);
722 fc_assert(conn_list_size(pplayer->connections) == 0);
723 conn_list_destroy(pplayer->connections);
725 players_iterate(aplayer) {
726 /* destroy the diplomatics states of this player with others ... */
727 player_diplstate_destroy(pplayer, aplayer);
728 /* and of others with this player. */
729 if (aplayer != pplayer) {
730 player_diplstate_destroy(aplayer, pplayer);
732 } players_iterate_end;
733 free(pplayer->diplstates);
735 /* Clear player color. */
736 if (pplayer->rgb) {
737 rgbcolor_destroy(pplayer->rgb);
740 dbv_free(&pplayer->tile_known);
742 if (!is_server()) {
743 vision_layer_iterate(v) {
744 dbv_free(&pplayer->client.tile_vision[v]);
745 } vision_layer_iterate_end;
748 free(pplayer);
749 pslot->player = NULL;
750 player_slots.used_slots--;
753 /**************************************************************************
754 Return the number of players.
755 **************************************************************************/
756 int player_count(void)
758 return player_slots.used_slots;
761 /**************************************************************************
762 Return the player index.
764 Currently same as player_number(), but indicates use as an array index.
765 The array must be sized by player_slot_count() or MAX_NUM_PLAYER_SLOTS
766 (player_count() *cannot* be used) and is likely to be sparse.
767 **************************************************************************/
768 int player_index(const struct player *pplayer)
770 return player_number(pplayer);
773 /**************************************************************************
774 Return the player index/number/id.
775 **************************************************************************/
776 int player_number(const struct player *pplayer)
778 fc_assert_ret_val(NULL != pplayer, -1);
779 return player_slot_index(pplayer->slot);
782 /**************************************************************************
783 Return struct player pointer for the given player index.
785 You can retrieve players that are not in the game (with IDs larger than
786 player_count). An out-of-range player request will return NULL.
787 **************************************************************************/
788 struct player *player_by_number(const int player_id)
790 struct player_slot *pslot = player_slot_by_number(player_id);
792 return (NULL != pslot ? player_slot_get_player(pslot) : NULL);
795 /****************************************************************************
796 Set the player's nation to the given nation (may be NULL). Returns TRUE
797 iff there was a change.
798 Doesn't check if the nation is legal wrt nationset.
799 ****************************************************************************/
800 bool player_set_nation(struct player *pplayer, struct nation_type *pnation)
802 if (pplayer->nation != pnation) {
803 if (pplayer->nation) {
804 fc_assert(pplayer->nation->player == pplayer);
805 pplayer->nation->player = NULL;
807 if (pnation) {
808 fc_assert(pnation->player == NULL);
809 pnation->player = pplayer;
811 pplayer->nation = pnation;
812 return TRUE;
814 return FALSE;
817 /***************************************************************
818 Find player by given name.
819 ***************************************************************/
820 struct player *player_by_name(const char *name)
822 players_iterate(pplayer) {
823 if (fc_strcasecmp(name, pplayer->name) == 0) {
824 return pplayer;
826 } players_iterate_end;
828 return NULL;
831 /**************************************************************************
832 Return the leader name of the player.
833 **************************************************************************/
834 const char *player_name(const struct player *pplayer)
836 if (!pplayer) {
837 return NULL;
839 return pplayer->name;
842 /***************************************************************
843 Find player by name, allowing unambigous prefix (ie abbreviation).
844 Returns NULL if could not match, or if ambiguous or other
845 problem, and fills *result with characterisation of match/non-match
846 (see shared.[ch])
847 ***************************************************************/
848 static const char *player_name_by_number(int i)
850 struct player *pplayer;
852 pplayer = player_by_number(i);
853 return player_name(pplayer);
856 /***************************************************************
857 Find player by its name prefix
858 ***************************************************************/
859 struct player *player_by_name_prefix(const char *name,
860 enum m_pre_result *result)
862 int ind;
864 *result = match_prefix(player_name_by_number,
865 player_slot_count(), MAX_LEN_NAME - 1,
866 fc_strncasequotecmp, effectivestrlenquote,
867 name, &ind);
869 if (*result < M_PRE_AMBIGUOUS) {
870 return player_by_number(ind);
871 } else {
872 return NULL;
876 /***************************************************************
877 Find player by its user name (not player/leader name)
878 ***************************************************************/
879 struct player *player_by_user(const char *name)
881 players_iterate(pplayer) {
882 if (fc_strcasecmp(name, pplayer->username) == 0) {
883 return pplayer;
885 } players_iterate_end;
887 return NULL;
890 /*************************************************************************
891 "Age" of the player: number of turns spent alive since created.
892 **************************************************************************/
893 int player_age(const struct player *pplayer)
895 fc_assert_ret_val(pplayer != NULL, 0);
896 return pplayer->turns_alive;
899 /*************************************************************************
900 Check if pplayer could see all units on ptile if it had units.
902 See can_player_see_unit_at() for rules about when an unit is visible.
903 **************************************************************************/
904 bool can_player_see_hypotetic_units_at(const struct player *pplayer,
905 const struct tile *ptile)
907 struct city *pcity;
909 /* Can't see invisible units. */
910 if (!fc_funcs->player_tile_vision_get(ptile, pplayer, V_INVIS)) {
911 return FALSE;
914 /* Can't see city units. */
915 pcity = tile_city(ptile);
916 if (pcity && !can_player_see_units_in_city(pplayer, pcity)
917 && unit_list_size(ptile->units) > 0) {
918 return FALSE;
921 /* Units within some extras may be hidden. */
922 if (!pplayers_allied(pplayer, ptile->extras_owner)) {
923 extra_type_list_iterate(extra_type_list_of_unit_hiders(), pextra) {
924 if (tile_has_extra(ptile, pextra)) {
925 return FALSE;
927 } extra_type_list_iterate_end;
930 /* Can't see non allied units in transports. */
931 unit_list_iterate(ptile->units, punit) {
932 if (unit_type_get(punit)->transport_capacity > 0
933 && unit_owner(punit) != pplayer) {
935 /* An ally could transport a non ally */
936 if (unit_list_size(punit->transporting) > 0) {
937 return FALSE;
940 } unit_list_iterate_end;
942 return TRUE;
945 /****************************************************************************
946 Checks if a unit can be seen by pplayer at (x,y).
947 A player can see a unit if he:
948 (a) can see the tile AND
949 (b) can see the unit at the tile (i.e. unit not invisible at this tile) AND
950 (c) the unit is outside a city OR in an allied city AND
951 (d) the unit isn't in a transporter, or we are allied AND
952 (e) the unit isn't in a transporter, or we can see the transporter
953 ****************************************************************************/
954 bool can_player_see_unit_at(const struct player *pplayer,
955 const struct unit *punit,
956 const struct tile *ptile,
957 bool is_transported)
959 struct city *pcity;
961 /* If the player can't even see the tile... */
962 if (TILE_KNOWN_SEEN != tile_get_known(ptile, pplayer)) {
963 return FALSE;
966 /* Don't show non-allied units that are in transports. This is logical
967 * because allied transports can also contain our units. Shared vision
968 * isn't taken into account. */
969 if (is_transported && unit_owner(punit) != pplayer
970 && !pplayers_allied(pplayer, unit_owner(punit))) {
971 return FALSE;
974 /* Units in cities may be hidden. */
975 pcity = tile_city(ptile);
976 if (pcity && !can_player_see_units_in_city(pplayer, pcity)) {
977 return FALSE;
980 /* Units within some extras may be hidden. */
981 if (!pplayers_allied(pplayer, ptile->extras_owner)) {
982 struct unit_type *ptype = unit_type_get(punit);
984 extra_type_list_iterate(extra_type_list_of_unit_hiders(), pextra) {
985 if (tile_has_extra(ptile, pextra) && is_native_extra_to_utype(pextra, ptype)) {
986 return FALSE;
988 } extra_type_list_iterate_end;
991 /* Allied or non-hiding units are always seen. */
992 if (pplayers_allied(unit_owner(punit), pplayer)
993 || !is_hiding_unit(punit)) {
994 return TRUE;
997 /* Hiding units are only seen by the V_INVIS fog layer. */
998 return fc_funcs->player_tile_vision_get(ptile, pplayer, V_INVIS);
1000 return FALSE;
1004 /****************************************************************************
1005 Checks if a unit can be seen by pplayer at its current location.
1007 See can_player_see_unit_at.
1008 ****************************************************************************/
1009 bool can_player_see_unit(const struct player *pplayer,
1010 const struct unit *punit)
1012 return can_player_see_unit_at(pplayer, punit, unit_tile(punit),
1013 unit_transported(punit));
1016 /****************************************************************************
1017 Return TRUE iff the player can see units in the city. Either they
1018 can see all units or none.
1020 If the player can see units in the city, then the server sends the
1021 unit info for units in the city to the client. The client uses the
1022 tile's unitlist to determine whether to show the city occupied flag. Of
1023 course the units will be visible to the player as well, if he clicks on
1024 them.
1026 If the player can't see units in the city, then the server doesn't send
1027 the unit info for these units. The client therefore uses the "occupied"
1028 flag sent in the short city packet to determine whether to show the city
1029 occupied flag.
1031 Note that can_player_see_city_internals => can_player_see_units_in_city.
1032 Otherwise the player would not know anything about the city's units at
1033 all, since the full city packet has no "occupied" flag.
1035 Returns TRUE if given a NULL player. This is used by the client when in
1036 observer mode.
1037 ****************************************************************************/
1038 bool can_player_see_units_in_city(const struct player *pplayer,
1039 const struct city *pcity)
1041 return (!pplayer
1042 || can_player_see_city_internals(pplayer, pcity)
1043 || pplayers_allied(pplayer, city_owner(pcity)));
1046 /****************************************************************************
1047 Return TRUE iff the player can see the city's internals. This means the
1048 full city packet is sent to the client, who should then be able to popup
1049 a dialog for it.
1051 Returns TRUE if given a NULL player. This is used by the client when in
1052 observer mode.
1053 ****************************************************************************/
1054 bool can_player_see_city_internals(const struct player *pplayer,
1055 const struct city *pcity)
1057 return (!pplayer || pplayer == city_owner(pcity));
1060 /**************************************************************************
1061 Returns TRUE iff pow_player can see externally visible features of
1062 target_city.
1064 A city's external features are visible to its owner, to players that
1065 currently sees the tile it is located at and to players that has it as
1066 a trade partner.
1067 **************************************************************************/
1068 bool player_can_see_city_externals(const struct player *pow_player,
1069 const struct city *target_city) {
1070 fc_assert_ret_val(target_city, FALSE);
1071 fc_assert_ret_val(pow_player, FALSE);
1073 if (can_player_see_city_internals(pow_player, target_city)) {
1074 /* City internals includes city externals. */
1075 return TRUE;
1078 if (tile_is_seen(city_tile(target_city), pow_player)) {
1079 /* The tile is being observed. */
1080 return TRUE;
1083 fc_assert_ret_val(target_city->routes, FALSE);
1085 trade_partners_iterate(target_city, trade_city) {
1086 if (city_owner(trade_city) == pow_player) {
1087 /* Revealed because of the trade route. */
1088 return TRUE;
1090 } trade_partners_iterate_end;
1092 return FALSE;
1095 /***************************************************************
1096 If the specified player owns the city with the specified id,
1097 return pointer to the city struct. Else return NULL.
1098 Now always uses fast idex_lookup_city.
1100 pplayer may be NULL in which case all cities registered to
1101 hash are considered - even those not currently owned by any
1102 player. Callers expect this behavior.
1103 ***************************************************************/
1104 struct city *player_city_by_number(const struct player *pplayer, int city_id)
1106 /* We call idex directly. Should use game_city_by_number() instead? */
1107 struct city *pcity = idex_lookup_city(city_id);
1109 if (!pcity) {
1110 return NULL;
1113 if (!pplayer || (city_owner(pcity) == pplayer)) {
1114 /* Correct owner */
1115 return pcity;
1118 return NULL;
1121 /***************************************************************
1122 If the specified player owns the unit with the specified id,
1123 return pointer to the unit struct. Else return NULL.
1124 Uses fast idex_lookup_city.
1126 pplayer may be NULL in which case all units registered to
1127 hash are considered - even those not currently owned by any
1128 player. Callers expect this behavior.
1129 ***************************************************************/
1130 struct unit *player_unit_by_number(const struct player *pplayer, int unit_id)
1132 /* We call idex directly. Should use game_unit_by_number() instead? */
1133 struct unit *punit = idex_lookup_unit(unit_id);
1135 if (!punit) {
1136 return NULL;
1139 if (!pplayer || (unit_owner(punit) == pplayer)) {
1140 /* Correct owner */
1141 return punit;
1144 return NULL;
1147 /*************************************************************************
1148 Return true iff x,y is inside any of the player's city map.
1149 **************************************************************************/
1150 bool player_in_city_map(const struct player *pplayer,
1151 const struct tile *ptile)
1153 city_tile_iterate(CITY_MAP_MAX_RADIUS_SQ, ptile, ptile1) {
1154 struct city *pcity = tile_city(ptile1);
1156 if (pcity
1157 && (pplayer == NULL || city_owner(pcity) == pplayer)
1158 && city_map_radius_sq_get(pcity) >= sq_map_distance(ptile,
1159 ptile1)) {
1160 return TRUE;
1162 } city_tile_iterate_end;
1164 return FALSE;
1167 /**************************************************************************
1168 Returns the number of techs the player has researched which has this
1169 flag. Needs to be optimized later (e.g. int tech_flags[TF_COUNT] in
1170 struct player)
1171 **************************************************************************/
1172 int num_known_tech_with_flag(const struct player *pplayer,
1173 enum tech_flag_id flag)
1175 return research_get(pplayer)->num_known_tech_with_flag[flag];
1178 /**************************************************************************
1179 Return the expected net income of the player this turn. This includes
1180 tax revenue and upkeep, but not one-time purchases or found gold.
1182 This function depends on pcity->prod[O_GOLD] being set for all cities, so
1183 make sure the player's cities have been refreshed.
1184 **************************************************************************/
1185 int player_get_expected_income(const struct player *pplayer)
1187 int income = 0;
1189 /* City income/expenses. */
1190 city_list_iterate(pplayer->cities, pcity) {
1191 /* Gold suplus accounts for imcome plus building and unit upkeep. */
1192 income += pcity->surplus[O_GOLD];
1194 /* Gold upkeep for buildings and units is defined by the setting
1195 * 'game.info.gold_upkeep_style':
1196 * GOLD_UPKEEP_CITY: Cities pay for buildings and units (this is
1197 * included in pcity->surplus[O_GOLD]).
1198 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
1199 * for units.
1200 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
1201 switch (game.info.gold_upkeep_style) {
1202 case GOLD_UPKEEP_CITY:
1203 break;
1204 case GOLD_UPKEEP_NATION:
1205 /* Nation pays for buildings (and units). */
1206 income -= city_total_impr_gold_upkeep(pcity);
1207 /* No break. */
1208 case GOLD_UPKEEP_MIXED:
1209 /* Nation pays for units. */
1210 income -= city_total_unit_gold_upkeep(pcity);
1211 break;
1214 /* Capitalization income. */
1215 if (city_production_has_flag(pcity, IF_GOLD)) {
1216 income += pcity->shield_stock + pcity->surplus[O_SHIELD];
1218 } city_list_iterate_end;
1220 return income;
1223 /**************************************************************************
1224 Returns TRUE iff the player knows at least one tech which has the
1225 given flag.
1226 **************************************************************************/
1227 bool player_knows_techs_with_flag(const struct player *pplayer,
1228 enum tech_flag_id flag)
1230 return num_known_tech_with_flag(pplayer, flag) > 0;
1233 /**************************************************************************
1234 Locate the player capital city, (NULL Otherwise)
1235 **************************************************************************/
1236 struct city *player_capital(const struct player *pplayer)
1238 if (!pplayer) {
1239 /* The client depends on this behavior in some places. */
1240 return NULL;
1242 city_list_iterate(pplayer->cities, pcity) {
1243 if (is_capital(pcity)) {
1244 return pcity;
1246 } city_list_iterate_end;
1247 return NULL;
1250 /**************************************************************************
1251 Return a text describing an AI's love for you. (Oooh, kinky!!)
1252 **************************************************************************/
1253 const char *love_text(const int love)
1255 if (love <= - MAX_AI_LOVE * 90 / 100) {
1256 /* TRANS: These words should be adjectives which can fit in the sentence
1257 "The x are y towards us"
1258 "The Babylonians are respectful towards us" */
1259 return Q_("?attitude:Genocidal");
1260 } else if (love <= - MAX_AI_LOVE * 70 / 100) {
1261 return Q_("?attitude:Belligerent");
1262 } else if (love <= - MAX_AI_LOVE * 50 / 100) {
1263 return Q_("?attitude:Hostile");
1264 } else if (love <= - MAX_AI_LOVE * 25 / 100) {
1265 return Q_("?attitude:Uncooperative");
1266 } else if (love <= - MAX_AI_LOVE * 10 / 100) {
1267 return Q_("?attitude:Uneasy");
1268 } else if (love <= MAX_AI_LOVE * 10 / 100) {
1269 return Q_("?attitude:Neutral");
1270 } else if (love <= MAX_AI_LOVE * 25 / 100) {
1271 return Q_("?attitude:Respectful");
1272 } else if (love <= MAX_AI_LOVE * 50 / 100) {
1273 return Q_("?attitude:Helpful");
1274 } else if (love <= MAX_AI_LOVE * 70 / 100) {
1275 return Q_("?attitude:Enthusiastic");
1276 } else if (love <= MAX_AI_LOVE * 90 / 100) {
1277 return Q_("?attitude:Admiring");
1278 } else {
1279 fc_assert(love > MAX_AI_LOVE * 90 / 100);
1280 return Q_("?attitude:Worshipful");
1284 /***************************************************************
1285 Returns true iff players can attack each other.
1286 ***************************************************************/
1287 bool pplayers_at_war(const struct player *pplayer,
1288 const struct player *pplayer2)
1290 enum diplstate_type ds;
1292 if (pplayer == pplayer2) {
1293 return FALSE;
1296 ds = player_diplstate_get(pplayer, pplayer2)->type;
1298 return ds == DS_WAR || ds == DS_NO_CONTACT;
1301 /***************************************************************
1302 Returns true iff players are allied.
1303 ***************************************************************/
1304 bool pplayers_allied(const struct player *pplayer,
1305 const struct player *pplayer2)
1307 enum diplstate_type ds;
1309 if (!pplayer || !pplayer2) {
1310 return FALSE;
1313 if (pplayer == pplayer2) {
1314 return TRUE;
1317 ds = player_diplstate_get(pplayer, pplayer2)->type;
1319 return (ds == DS_ALLIANCE || ds == DS_TEAM);
1322 /***************************************************************
1323 Returns true iff players are allied or at peace.
1324 ***************************************************************/
1325 bool pplayers_in_peace(const struct player *pplayer,
1326 const struct player *pplayer2)
1328 enum diplstate_type ds = player_diplstate_get(pplayer, pplayer2)->type;
1330 if (pplayer == pplayer2) {
1331 return TRUE;
1334 return (ds == DS_PEACE || ds == DS_ALLIANCE
1335 || ds == DS_ARMISTICE || ds == DS_TEAM);
1338 /****************************************************************************
1339 Returns TRUE if players can't enter each others' territory.
1340 ****************************************************************************/
1341 bool players_non_invade(const struct player *pplayer1,
1342 const struct player *pplayer2)
1344 if (pplayer1 == pplayer2 || !pplayer1 || !pplayer2) {
1345 return FALSE;
1348 /* Movement during armistice is allowed so that player can withdraw
1349 units deeper inside opponent territory. */
1351 return player_diplstate_get(pplayer1, pplayer2)->type == DS_PEACE;
1354 /***************************************************************
1355 Returns true iff players have peace, cease-fire, or
1356 armistice.
1357 ***************************************************************/
1358 bool pplayers_non_attack(const struct player *pplayer,
1359 const struct player *pplayer2)
1361 enum diplstate_type ds;
1363 if (pplayer == pplayer2) {
1364 return FALSE;
1367 ds = player_diplstate_get(pplayer, pplayer2)->type;
1369 return (ds == DS_PEACE || ds == DS_CEASEFIRE || ds == DS_ARMISTICE);
1372 /**************************************************************************
1373 Return TRUE if players are in the same team
1374 **************************************************************************/
1375 bool players_on_same_team(const struct player *pplayer1,
1376 const struct player *pplayer2)
1378 return pplayer1->team == pplayer2->team;
1381 /**************************************************************************
1382 Return TRUE iff the player me gives shared vision to player them.
1383 **************************************************************************/
1384 bool gives_shared_vision(const struct player *me, const struct player *them)
1386 return BV_ISSET(me->gives_shared_vision, player_index(them));
1389 /**************************************************************************
1390 Return TRUE iff the two diplstates are equal.
1391 **************************************************************************/
1392 bool are_diplstates_equal(const struct player_diplstate *pds1,
1393 const struct player_diplstate *pds2)
1395 return (pds1->type == pds2->type && pds1->turns_left == pds2->turns_left
1396 && pds1->has_reason_to_cancel == pds2->has_reason_to_cancel
1397 && pds1->contact_turns_left == pds2->contact_turns_left);
1400 /**************************************************************************
1401 Return TRUE iff player1 has the diplomatic relation to player2
1402 **************************************************************************/
1403 bool is_diplrel_between(const struct player *player1,
1404 const struct player *player2,
1405 int diplrel)
1407 fc_assert(player1 != NULL);
1408 fc_assert(player2 != NULL);
1410 /* No relationship to it self. */
1411 if (player1 == player2 && diplrel != DRO_FOREIGN) {
1412 return FALSE;
1415 if (diplrel < DS_LAST) {
1416 return player_diplstate_get(player1, player2)->type == diplrel;
1419 switch (diplrel) {
1420 case DRO_GIVES_SHARED_VISION:
1421 return gives_shared_vision(player1, player2);
1422 case DRO_RECEIVES_SHARED_VISION:
1423 return gives_shared_vision(player2, player1);
1424 case DRO_HOSTS_EMBASSY:
1425 return player_has_embassy(player2, player1);
1426 case DRO_HAS_EMBASSY:
1427 return player_has_embassy(player1, player2);
1428 case DRO_HOSTS_REAL_EMBASSY:
1429 return player_has_real_embassy(player2, player1);
1430 case DRO_HAS_REAL_EMBASSY:
1431 return player_has_real_embassy(player1, player2);
1432 case DRO_HAS_CASUS_BELLI:
1433 return 0 < player_diplstate_get(player1, player2)->has_reason_to_cancel;
1434 case DRO_PROVIDED_CASUS_BELLI:
1435 return 0 < player_diplstate_get(player2, player1)->has_reason_to_cancel;
1436 case DRO_FOREIGN:
1437 return player1 != player2;
1440 fc_assert_msg(FALSE, "diplrel_between(): invalid diplrel number %d.",
1441 diplrel);
1443 return FALSE;
1446 /**************************************************************************
1447 Return TRUE iff pplayer has the diplomatic relation to any living player
1448 **************************************************************************/
1449 bool is_diplrel_to_other(const struct player *pplayer, int diplrel)
1451 fc_assert(pplayer != NULL);
1453 players_iterate_alive(oplayer) {
1454 if (oplayer == pplayer) {
1455 continue;
1457 if (is_diplrel_between(pplayer, oplayer, diplrel)) {
1458 return TRUE;
1460 } players_iterate_alive_end;
1461 return FALSE;
1464 /**************************************************************************
1465 Return the diplomatic relation that has the given (untranslated) rule
1466 name.
1467 **************************************************************************/
1468 int diplrel_by_rule_name(const char *value)
1470 /* Look for asymmetric diplomatic relations */
1471 int diplrel = diplrel_other_by_name(value, fc_strcasecmp);
1473 if (diplrel != diplrel_other_invalid()) {
1474 return diplrel;
1477 /* Look for symmetric diplomatic relations */
1478 diplrel = diplstate_type_by_name(value, fc_strcasecmp);
1481 * Make sure DS_LAST isn't returned as DS_LAST is the first diplrel_other.
1483 * Can't happend now. This is in case that changes in the future. */
1484 fc_assert_ret_val(diplrel != DS_LAST, diplrel_other_invalid());
1487 * Make sure that diplrel_other_invalid() is returned.
1489 * Can't happen now. At the moment dpilrel_asym_invalid() is the same as
1490 * diplstate_type_invalid(). This is in case that changes in the future.
1492 if (diplrel != diplstate_type_invalid()) {
1493 return diplrel;
1496 return diplrel_other_invalid();
1499 /**************************************************************************
1500 Return the (untranslated) rule name of the given diplomatic relation.
1501 **************************************************************************/
1502 const char *diplrel_rule_name(int value)
1504 if (value < DS_LAST) {
1505 return diplstate_type_name(value);
1506 } else {
1507 return diplrel_other_name(value);
1511 /**************************************************************************
1512 Return the translated name of the given diplomatic relation.
1513 **************************************************************************/
1514 const char *diplrel_name_translation(int value)
1516 if (value < DS_LAST) {
1517 return diplstate_type_translated_name(value);
1518 } else {
1519 return _(diplrel_other_name(value));
1523 /* The number of mutually exclusive requirement sets that
1524 * diplrel_mess_gen() creates for the DiplRel requirement type. */
1525 #define DIPLREL_MESS_SIZE (3 + (DRO_LAST * (5 + 4 + 3 + 2 + 1)))
1527 /**************************************************************************
1528 Generate and return an array of mutually exclusive requirement sets for
1529 the DiplRel requirement type. The array has DIPLREL_MESS_SIZE sets.
1531 A mutually exclusive set is a set of requirements were the presence of
1532 one requirement proves the absence of every other requirement. In other
1533 words: at most one of the requirements in the set can be present.
1534 **************************************************************************/
1535 static bv_diplrel_all_reqs *diplrel_mess_gen(void)
1537 /* The ranges supported by the DiplRel requiremnt type. */
1538 const enum req_range legal_ranges[] = {
1539 REQ_RANGE_LOCAL,
1540 REQ_RANGE_PLAYER,
1541 REQ_RANGE_ALLIANCE,
1542 REQ_RANGE_TEAM,
1543 REQ_RANGE_WORLD
1546 /* Iterators. */
1547 int rel;
1548 int i;
1549 int j;
1551 /* Storage for the mutually exclusive requirement sets. */
1552 bv_diplrel_all_reqs *mess = fc_malloc(DIPLREL_MESS_SIZE
1553 * sizeof(bv_diplrel_all_reqs));
1555 /* Position in mess. */
1556 int mess_pos = 0;
1558 /* The first mutually exclusive set is about local diplstate. */
1559 BV_CLR_ALL(mess[mess_pos]);
1561 /* It is not possible to have more than one diplstate to a nation. */
1562 BV_SET(mess[mess_pos],
1563 requirement_diplrel_ereq(DS_ARMISTICE, REQ_RANGE_LOCAL, TRUE));
1564 BV_SET(mess[mess_pos],
1565 requirement_diplrel_ereq(DS_WAR, REQ_RANGE_LOCAL, TRUE));
1566 BV_SET(mess[mess_pos],
1567 requirement_diplrel_ereq(DS_CEASEFIRE, REQ_RANGE_LOCAL, TRUE));
1568 BV_SET(mess[mess_pos],
1569 requirement_diplrel_ereq(DS_PEACE, REQ_RANGE_LOCAL, TRUE));
1570 BV_SET(mess[mess_pos],
1571 requirement_diplrel_ereq(DS_ALLIANCE, REQ_RANGE_LOCAL, TRUE));
1572 BV_SET(mess[mess_pos],
1573 requirement_diplrel_ereq(DS_NO_CONTACT, REQ_RANGE_LOCAL, TRUE));
1574 BV_SET(mess[mess_pos],
1575 requirement_diplrel_ereq(DS_TEAM, REQ_RANGE_LOCAL, TRUE));
1577 /* It is not possible to have a diplstate to your self. */
1578 BV_SET(mess[mess_pos],
1579 requirement_diplrel_ereq(DRO_FOREIGN, REQ_RANGE_LOCAL, FALSE));
1581 mess_pos++;
1583 /* Having a real embassy excludes not having an embassy. */
1584 BV_CLR_ALL(mess[mess_pos]);
1586 BV_SET(mess[mess_pos],
1587 requirement_diplrel_ereq(DRO_HAS_REAL_EMBASSY, REQ_RANGE_LOCAL,
1588 TRUE));
1589 BV_SET(mess[mess_pos],
1590 requirement_diplrel_ereq(DRO_HAS_EMBASSY, REQ_RANGE_LOCAL,
1591 FALSE));
1593 mess_pos++;
1595 /* Hosting a real embassy excludes not hosting an embassy. */
1596 BV_CLR_ALL(mess[mess_pos]);
1598 BV_SET(mess[mess_pos],
1599 requirement_diplrel_ereq(DRO_HOSTS_REAL_EMBASSY, REQ_RANGE_LOCAL,
1600 TRUE));
1601 BV_SET(mess[mess_pos],
1602 requirement_diplrel_ereq(DRO_HOSTS_EMBASSY, REQ_RANGE_LOCAL,
1603 FALSE));
1605 mess_pos++;
1607 /* Loop over diplstate_type and diplrel_other. */
1608 for (rel = 0; rel < DRO_LAST; rel++) {
1609 /* The presence of a DiplRel at a more local range proves that it can't
1610 * be absent in a more global range. (The alliance range includes the
1611 * Team range) */
1612 for (i = 0; i < 5; i++) {
1613 for (j = i; j < 5; j++) {
1614 BV_CLR_ALL(mess[mess_pos]);
1616 BV_SET(mess[mess_pos],
1617 requirement_diplrel_ereq(rel, legal_ranges[i], TRUE));
1618 BV_SET(mess[mess_pos],
1619 requirement_diplrel_ereq(rel, legal_ranges[j], FALSE));
1621 mess_pos++;
1626 /* No uninitialized element exists. */
1627 fc_assert(mess_pos == DIPLREL_MESS_SIZE);
1629 return mess;
1632 /* An array of mutually exclusive requirement sets for the DiplRel
1633 * requirement type. Is initialized the first time diplrel_mess_get() is
1634 * called. */
1635 static bv_diplrel_all_reqs *diplrel_mess = NULL;
1637 /**************************************************************************
1638 Get the mutually exclusive requirement sets for DiplRel.
1639 **************************************************************************/
1640 static bv_diplrel_all_reqs *diplrel_mess_get(void)
1642 if (diplrel_mess == NULL) {
1643 /* This is the first call. Initialize diplrel_mess. */
1644 diplrel_mess = diplrel_mess_gen();
1647 return diplrel_mess;
1650 /**************************************************************************
1651 Free diplrel_mess
1652 **************************************************************************/
1653 void diplrel_mess_close(void)
1655 if (diplrel_mess != NULL) {
1656 free(diplrel_mess);
1657 diplrel_mess = NULL;
1661 /**************************************************************************
1662 Get the DiplRel requirements that are known to contradict the specified
1663 DiplRel requirement.
1665 The known contratictions have their position in the enumeration of all
1666 possible DiplRel requirements set in the returned bitvector.
1667 **************************************************************************/
1668 bv_diplrel_all_reqs diplrel_req_contradicts(const struct requirement *req)
1670 int diplrel_req_num;
1671 bv_diplrel_all_reqs *mess;
1672 bv_diplrel_all_reqs known;
1673 int set;
1675 /* Nothing is known to contradict the requirement yet. */
1676 BV_CLR_ALL(known);
1678 if (req->source.kind != VUT_DIPLREL) {
1679 /* No known contradiction of a requirement of any other kind. */
1680 fc_assert(req->source.kind == VUT_DIPLREL);
1682 return known;
1685 /* Convert the requirement to its position in the enumeration of all
1686 * DiplRel requirements. */
1687 diplrel_req_num = requirement_diplrel_ereq(req->source.value.diplrel,
1688 req->range, req->present);
1690 /* Get the mutually exclusive requirement sets for DiplRel. */
1691 mess = diplrel_mess_get();
1693 /* Add all known contradictions. */
1694 for (set = 0; set < DIPLREL_MESS_SIZE; set++) {
1695 if (BV_ISSET(mess[set], diplrel_req_num)) {
1696 /* The requirement req is mentioned in the set. It is therefore known
1697 * that all other requirements in the set contradicts it. They should
1698 * therefore be added to the known contradictions. */
1699 BV_SET_ALL_FROM(known, mess[set]);
1703 /* The requirement isn't self contradicting. It was set by the mutually
1704 * exclusive requirement sets that mentioned it. Remove it. */
1705 BV_CLR(known, diplrel_req_num);
1707 return known;
1710 /***************************************************************************
1711 Return the number of pplayer2's visible units in pplayer's territory,
1712 from the point of view of pplayer. Units that cannot be seen by pplayer
1713 will not be found (this function doesn't cheat).
1714 ***************************************************************************/
1715 int player_in_territory(const struct player *pplayer,
1716 const struct player *pplayer2)
1718 int in_territory = 0;
1720 /* This algorithm should work at server or client. It only returns the
1721 * number of visible units (a unit can potentially hide inside the
1722 * transport of a different player).
1724 * Note this may be quite slow. An even slower alternative is to iterate
1725 * over the entire map, checking all units inside the player's territory
1726 * to see if they're owned by the enemy. */
1727 unit_list_iterate(pplayer2->units, punit) {
1728 /* Get the owner of the tile/territory. */
1729 struct player *owner = tile_owner(unit_tile(punit));
1731 if (owner == pplayer && can_player_see_unit(pplayer, punit)) {
1732 /* Found one! */
1733 in_territory += 1;
1735 } unit_list_iterate_end;
1737 return in_territory;
1740 /****************************************************************************
1741 Returns whether this is a valid username. This is used by the server to
1742 validate usernames and should be used by the client to avoid invalid
1743 ones.
1744 ****************************************************************************/
1745 bool is_valid_username(const char *name)
1747 return (strlen(name) > 0
1748 && !fc_isdigit(name[0])
1749 && is_ascii_name(name)
1750 && fc_strcasecmp(name, ANON_USER_NAME) != 0);
1753 /***************************************************************
1754 Return is AI can be set to given level
1755 ***************************************************************/
1756 bool is_settable_ai_level(enum ai_level level)
1758 if (level == AI_LEVEL_AWAY) {
1759 /* Cannot set away level for AI */
1760 return FALSE;
1763 return TRUE;
1766 /***************************************************************
1767 Return number of AI levels in game
1768 ***************************************************************/
1769 int number_of_ai_levels(void)
1771 return AI_LEVEL_COUNT - 1; /* AI_LEVEL_AWAY is not real AI */
1774 /**************************************************************************
1775 Return pointer to ai data of given player and ai type.
1776 **************************************************************************/
1777 void *player_ai_data(const struct player *pplayer, const struct ai_type *ai)
1779 return pplayer->server.ais[ai_type_number(ai)];
1782 /**************************************************************************
1783 Attach ai data to player
1784 **************************************************************************/
1785 void player_set_ai_data(struct player *pplayer, const struct ai_type *ai,
1786 void *data)
1788 pplayer->server.ais[ai_type_number(ai)] = data;
1791 /**************************************************************************
1792 Return the multiplier value currently in effect for pplayer (in display
1793 units).
1794 **************************************************************************/
1795 int player_multiplier_value(const struct player *pplayer,
1796 const struct multiplier *pmul)
1798 return pplayer->multipliers[multiplier_index(pmul)];
1801 /**************************************************************************
1802 Return the multiplier value currently in effect for pplayer, scaled
1803 from display units to the units used in the effect system (if different).
1804 Result is multiplied by 100 (caller should divide down).
1805 **************************************************************************/
1806 int player_multiplier_effect_value(const struct player *pplayer,
1807 const struct multiplier *pmul)
1809 return (player_multiplier_value(pplayer, pmul) + pmul->offset)
1810 * pmul->factor;
1813 /**************************************************************************
1814 Return the player's target value for a multiplier (which may be
1815 different from the value currently in force; it will take effect
1816 next turn). Result is in display units.
1817 **************************************************************************/
1818 int player_multiplier_target_value(const struct player *pplayer,
1819 const struct multiplier *pmul)
1821 return pplayer->multipliers_target[multiplier_index(pmul)];
1824 /****************************************************************************
1825 Check if player has given flag
1826 ****************************************************************************/
1827 bool player_has_flag(const struct player *pplayer, enum plr_flag_id flag)
1829 return BV_ISSET(pplayer->flags, flag);