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 ***********************************************************************/
15 #include <fc_config.h>
28 #include "fc_interface.h"
29 #include "featured_text.h"
31 #include "government.h"
33 #include "improvement.h"
46 struct player
*player
;
50 struct player_slot
*slots
;
51 int used_slots
; /* number of used/allocated players in the 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
66 ***************************************************************/
67 enum diplstate_type
cancel_pact_result(enum diplstate_type oldstate
)
70 case DS_NO_CONTACT
: /* possible if someone declares war on our ally */
71 case DS_WAR
: /* no change */
78 case DS_TEAM
: /* no change */
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
) {
100 if (players_on_same_team(p1
, p2
)) {
103 if (!p1
->is_alive
|| !p2
->is_alive
) {
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
;
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
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
;
133 && ds
== DS_WAR
/* do not count 'never met' as war here */
134 && pplayers_allied(p2
, pplayer
)) {
137 } players_iterate_alive_end
;
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 */
161 if (get_player_bonus(p1
, EFT_NO_DIPLOMACY
) > 0
162 || get_player_bonus(p2
, EFT_NO_DIPLOMACY
) > 0) {
166 || treaty
== DS_NO_CONTACT
167 || treaty
== DS_ARMISTICE
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
)) {
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
) {
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
);
247 || ptile_owner
== pplayer
248 || !players_non_invade(pplayer
, ptile_owner
));
251 /****************************************************************************
252 Allocate new diplstate structure for tracking state between given two
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)
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
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
)
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
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()) {
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())) {
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)
451 player_slots_iterate(pslot
) {
452 if (player_slot_is_used(pslot
)) {
453 max_pslot
= player_slot_index(pslot
);
455 } player_slots_iterate_end
;
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
);
471 player_slots_iterate(aslot
) {
472 if (!player_slot_is_used(aslot
)) {
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
++;
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
)
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
;
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
;
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;
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
);
623 pplayer
->rgb
= rgbcolor_copy(prgbcolor
);
627 /****************************************************************************
628 Clear all player data. If full is set, then the nation and the team will
630 ****************************************************************************/
631 void player_clear(struct player
*pplayer
, bool full
)
633 bool client
= !is_server();
635 if (pplayer
== NULL
) {
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
);
663 pcargo
->client
.transported_by
= -1;
665 } unit_list_iterate_end
;
666 /* Unload the unit. */
667 unit_transport_unload(punit
);
669 punit
->client
.transported_by
= -1;
672 game_remove_unit(&wld
, punit
);
673 } unit_list_iterate_end
;
675 city_list_iterate(pplayer
->cities
, pcity
) {
676 game_remove_city(&wld
, pcity
);
677 } city_list_iterate_end
;
680 team_remove_player(pplayer
);
682 /* This comes last because log calls in the above functions
684 if (pplayer
->nation
!= NULL
) {
685 player_set_nation(pplayer
, NULL
);
690 /****************************************************************************
691 Clear the ruleset dependent pointers of the player structure. Called by
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. */
737 rgbcolor_destroy(pplayer
->rgb
);
740 dbv_free(&pplayer
->tile_known
);
743 vision_layer_iterate(v
) {
744 dbv_free(&pplayer
->client
.tile_vision
[v
]);
745 } vision_layer_iterate_end
;
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
;
808 fc_assert(pnation
->player
== NULL
);
809 pnation
->player
= pplayer
;
811 pplayer
->nation
= pnation
;
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) {
826 } players_iterate_end
;
831 /**************************************************************************
832 Return the leader name of the player.
833 **************************************************************************/
834 const char *player_name(const struct player
*pplayer
)
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
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
)
864 *result
= match_prefix(player_name_by_number
,
865 player_slot_count(), MAX_LEN_NAME
- 1,
866 fc_strncasequotecmp
, effectivestrlenquote
,
869 if (*result
< M_PRE_AMBIGUOUS
) {
870 return player_by_number(ind
);
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) {
885 } players_iterate_end
;
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 Returns TRUE iff pplayer can trust that ptile really has no units when
901 it looks empty. A tile looks empty if the player can't see any units on
902 it and it doesn't contain anything marked as occupied by a unit.
904 See can_player_see_unit_at() for rules about when an unit is visible.
905 **************************************************************************/
906 bool player_can_trust_tile_has_no_units(const struct player
*pplayer
,
907 const struct tile
*ptile
)
909 /* Can't see invisible units. */
910 if (!fc_funcs
->player_tile_vision_get(ptile
, pplayer
, V_INVIS
)) {
914 /* Units within some extras may be hidden. */
915 if (!pplayers_allied(pplayer
, ptile
->extras_owner
)) {
916 extra_type_list_iterate(extra_type_list_of_unit_hiders(), pextra
) {
917 if (tile_has_extra(ptile
, pextra
)) {
920 } extra_type_list_iterate_end
;
926 /*************************************************************************
927 Check if pplayer could see all units on ptile if it had units.
929 See can_player_see_unit_at() for rules about when an unit is visible.
930 **************************************************************************/
931 bool can_player_see_hypotetic_units_at(const struct player
*pplayer
,
932 const struct tile
*ptile
)
936 if (!player_can_trust_tile_has_no_units(pplayer
, ptile
)) {
937 /* The existance of any units at all is hidden from the player. */
941 /* Can't see city units. */
942 pcity
= tile_city(ptile
);
943 if (pcity
&& !can_player_see_units_in_city(pplayer
, pcity
)
944 && unit_list_size(ptile
->units
) > 0) {
948 /* Can't see non allied units in transports. */
949 unit_list_iterate(ptile
->units
, punit
) {
950 if (unit_type_get(punit
)->transport_capacity
> 0
951 && unit_owner(punit
) != pplayer
) {
953 /* An ally could transport a non ally */
954 if (unit_list_size(punit
->transporting
) > 0) {
958 } unit_list_iterate_end
;
963 /****************************************************************************
964 Checks if a unit can be seen by pplayer at (x,y).
965 A player can see a unit if he:
966 (a) can see the tile AND
967 (b) can see the unit at the tile (i.e. unit not invisible at this tile) AND
968 (c) the unit is outside a city OR in an allied city AND
969 (d) the unit isn't in a transporter, or we are allied AND
970 (e) the unit isn't in a transporter, or we can see the transporter
971 ****************************************************************************/
972 bool can_player_see_unit_at(const struct player
*pplayer
,
973 const struct unit
*punit
,
974 const struct tile
*ptile
,
979 /* If the player can't even see the tile... */
980 if (TILE_KNOWN_SEEN
!= tile_get_known(ptile
, pplayer
)) {
984 /* Don't show non-allied units that are in transports. This is logical
985 * because allied transports can also contain our units. Shared vision
986 * isn't taken into account. */
987 if (is_transported
&& unit_owner(punit
) != pplayer
988 && !pplayers_allied(pplayer
, unit_owner(punit
))) {
992 /* Units in cities may be hidden. */
993 pcity
= tile_city(ptile
);
994 if (pcity
&& !can_player_see_units_in_city(pplayer
, pcity
)) {
998 /* Units within some extras may be hidden. */
999 if (!pplayers_allied(pplayer
, ptile
->extras_owner
)) {
1000 struct unit_type
*ptype
= unit_type_get(punit
);
1002 extra_type_list_iterate(extra_type_list_of_unit_hiders(), pextra
) {
1003 if (tile_has_extra(ptile
, pextra
) && is_native_extra_to_utype(pextra
, ptype
)) {
1006 } extra_type_list_iterate_end
;
1009 /* Allied or non-hiding units are always seen. */
1010 if (pplayers_allied(unit_owner(punit
), pplayer
)
1011 || !is_hiding_unit(punit
)) {
1015 /* Hiding units are only seen by the V_INVIS fog layer. */
1016 return fc_funcs
->player_tile_vision_get(ptile
, pplayer
, V_INVIS
);
1022 /****************************************************************************
1023 Checks if a unit can be seen by pplayer at its current location.
1025 See can_player_see_unit_at.
1026 ****************************************************************************/
1027 bool can_player_see_unit(const struct player
*pplayer
,
1028 const struct unit
*punit
)
1030 return can_player_see_unit_at(pplayer
, punit
, unit_tile(punit
),
1031 unit_transported(punit
));
1034 /****************************************************************************
1035 Return TRUE iff the player can see units in the city. Either they
1036 can see all units or none.
1038 If the player can see units in the city, then the server sends the
1039 unit info for units in the city to the client. The client uses the
1040 tile's unitlist to determine whether to show the city occupied flag. Of
1041 course the units will be visible to the player as well, if he clicks on
1044 If the player can't see units in the city, then the server doesn't send
1045 the unit info for these units. The client therefore uses the "occupied"
1046 flag sent in the short city packet to determine whether to show the city
1049 Note that can_player_see_city_internals => can_player_see_units_in_city.
1050 Otherwise the player would not know anything about the city's units at
1051 all, since the full city packet has no "occupied" flag.
1053 Returns TRUE if given a NULL player. This is used by the client when in
1055 ****************************************************************************/
1056 bool can_player_see_units_in_city(const struct player
*pplayer
,
1057 const struct city
*pcity
)
1060 || can_player_see_city_internals(pplayer
, pcity
)
1061 || pplayers_allied(pplayer
, city_owner(pcity
)));
1064 /****************************************************************************
1065 Return TRUE iff the player can see the city's internals. This means the
1066 full city packet is sent to the client, who should then be able to popup
1069 Returns TRUE if given a NULL player. This is used by the client when in
1071 ****************************************************************************/
1072 bool can_player_see_city_internals(const struct player
*pplayer
,
1073 const struct city
*pcity
)
1075 return (!pplayer
|| pplayer
== city_owner(pcity
));
1078 /**************************************************************************
1079 Returns TRUE iff pow_player can see externally visible features of
1082 A city's external features are visible to its owner, to players that
1083 currently sees the tile it is located at and to players that has it as
1085 **************************************************************************/
1086 bool player_can_see_city_externals(const struct player
*pow_player
,
1087 const struct city
*target_city
) {
1088 fc_assert_ret_val(target_city
, FALSE
);
1089 fc_assert_ret_val(pow_player
, FALSE
);
1091 if (can_player_see_city_internals(pow_player
, target_city
)) {
1092 /* City internals includes city externals. */
1096 if (tile_is_seen(city_tile(target_city
), pow_player
)) {
1097 /* The tile is being observed. */
1101 fc_assert_ret_val(target_city
->routes
, FALSE
);
1103 trade_partners_iterate(target_city
, trade_city
) {
1104 if (city_owner(trade_city
) == pow_player
) {
1105 /* Revealed because of the trade route. */
1108 } trade_partners_iterate_end
;
1113 /***************************************************************
1114 If the specified player owns the city with the specified id,
1115 return pointer to the city struct. Else return NULL.
1116 Now always uses fast idex_lookup_city.
1118 pplayer may be NULL in which case all cities registered to
1119 hash are considered - even those not currently owned by any
1120 player. Callers expect this behavior.
1121 ***************************************************************/
1122 struct city
*player_city_by_number(const struct player
*pplayer
, int city_id
)
1124 /* We call idex directly. Should use game_city_by_number() instead? */
1125 struct city
*pcity
= idex_lookup_city(&wld
, city_id
);
1131 if (!pplayer
|| (city_owner(pcity
) == pplayer
)) {
1139 /***************************************************************
1140 If the specified player owns the unit with the specified id,
1141 return pointer to the unit struct. Else return NULL.
1142 Uses fast idex_lookup_city.
1144 pplayer may be NULL in which case all units registered to
1145 hash are considered - even those not currently owned by any
1146 player. Callers expect this behavior.
1147 ***************************************************************/
1148 struct unit
*player_unit_by_number(const struct player
*pplayer
, int unit_id
)
1150 /* We call idex directly. Should use game_unit_by_number() instead? */
1151 struct unit
*punit
= idex_lookup_unit(&wld
, unit_id
);
1157 if (!pplayer
|| (unit_owner(punit
) == pplayer
)) {
1165 /*************************************************************************
1166 Return true iff x,y is inside any of the player's city map.
1167 **************************************************************************/
1168 bool player_in_city_map(const struct player
*pplayer
,
1169 const struct tile
*ptile
)
1171 city_tile_iterate(CITY_MAP_MAX_RADIUS_SQ
, ptile
, ptile1
) {
1172 struct city
*pcity
= tile_city(ptile1
);
1175 && (pplayer
== NULL
|| city_owner(pcity
) == pplayer
)
1176 && city_map_radius_sq_get(pcity
) >= sq_map_distance(ptile
,
1180 } city_tile_iterate_end
;
1185 /**************************************************************************
1186 Returns the number of techs the player has researched which has this
1187 flag. Needs to be optimized later (e.g. int tech_flags[TF_COUNT] in
1189 **************************************************************************/
1190 int num_known_tech_with_flag(const struct player
*pplayer
,
1191 enum tech_flag_id flag
)
1193 return research_get(pplayer
)->num_known_tech_with_flag
[flag
];
1196 /**************************************************************************
1197 Return the expected net income of the player this turn. This includes
1198 tax revenue and upkeep, but not one-time purchases or found gold.
1200 This function depends on pcity->prod[O_GOLD] being set for all cities, so
1201 make sure the player's cities have been refreshed.
1202 **************************************************************************/
1203 int player_get_expected_income(const struct player
*pplayer
)
1207 /* City income/expenses. */
1208 city_list_iterate(pplayer
->cities
, pcity
) {
1209 /* Gold suplus accounts for imcome plus building and unit upkeep. */
1210 income
+= pcity
->surplus
[O_GOLD
];
1212 /* Gold upkeep for buildings and units is defined by the setting
1213 * 'game.info.gold_upkeep_style':
1214 * GOLD_UPKEEP_CITY: Cities pay for buildings and units (this is
1215 * included in pcity->surplus[O_GOLD]).
1216 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
1218 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
1219 switch (game
.info
.gold_upkeep_style
) {
1220 case GOLD_UPKEEP_CITY
:
1222 case GOLD_UPKEEP_NATION
:
1223 /* Nation pays for buildings (and units). */
1224 income
-= city_total_impr_gold_upkeep(pcity
);
1226 case GOLD_UPKEEP_MIXED
:
1227 /* Nation pays for units. */
1228 income
-= city_total_unit_gold_upkeep(pcity
);
1232 /* Capitalization income. */
1233 if (city_production_has_flag(pcity
, IF_GOLD
)) {
1234 income
+= pcity
->shield_stock
+ pcity
->surplus
[O_SHIELD
];
1236 } city_list_iterate_end
;
1241 /**************************************************************************
1242 Returns TRUE iff the player knows at least one tech which has the
1244 **************************************************************************/
1245 bool player_knows_techs_with_flag(const struct player
*pplayer
,
1246 enum tech_flag_id flag
)
1248 return num_known_tech_with_flag(pplayer
, flag
) > 0;
1251 /**************************************************************************
1252 Locate the player capital city, (NULL Otherwise)
1253 **************************************************************************/
1254 struct city
*player_capital(const struct player
*pplayer
)
1257 /* The client depends on this behavior in some places. */
1260 city_list_iterate(pplayer
->cities
, pcity
) {
1261 if (is_capital(pcity
)) {
1264 } city_list_iterate_end
;
1268 /**************************************************************************
1269 Return a text describing an AI's love for you. (Oooh, kinky!!)
1270 **************************************************************************/
1271 const char *love_text(const int love
)
1273 if (love
<= - MAX_AI_LOVE
* 90 / 100) {
1274 /* TRANS: These words should be adjectives which can fit in the sentence
1275 "The x are y towards us"
1276 "The Babylonians are respectful towards us" */
1277 return Q_("?attitude:Genocidal");
1278 } else if (love
<= - MAX_AI_LOVE
* 70 / 100) {
1279 return Q_("?attitude:Belligerent");
1280 } else if (love
<= - MAX_AI_LOVE
* 50 / 100) {
1281 return Q_("?attitude:Hostile");
1282 } else if (love
<= - MAX_AI_LOVE
* 25 / 100) {
1283 return Q_("?attitude:Uncooperative");
1284 } else if (love
<= - MAX_AI_LOVE
* 10 / 100) {
1285 return Q_("?attitude:Uneasy");
1286 } else if (love
<= MAX_AI_LOVE
* 10 / 100) {
1287 return Q_("?attitude:Neutral");
1288 } else if (love
<= MAX_AI_LOVE
* 25 / 100) {
1289 return Q_("?attitude:Respectful");
1290 } else if (love
<= MAX_AI_LOVE
* 50 / 100) {
1291 return Q_("?attitude:Helpful");
1292 } else if (love
<= MAX_AI_LOVE
* 70 / 100) {
1293 return Q_("?attitude:Enthusiastic");
1294 } else if (love
<= MAX_AI_LOVE
* 90 / 100) {
1295 return Q_("?attitude:Admiring");
1297 fc_assert(love
> MAX_AI_LOVE
* 90 / 100);
1298 return Q_("?attitude:Worshipful");
1302 /***************************************************************
1303 Returns true iff players can attack each other.
1304 ***************************************************************/
1305 bool pplayers_at_war(const struct player
*pplayer
,
1306 const struct player
*pplayer2
)
1308 enum diplstate_type ds
;
1310 if (pplayer
== pplayer2
) {
1314 ds
= player_diplstate_get(pplayer
, pplayer2
)->type
;
1316 return ds
== DS_WAR
|| ds
== DS_NO_CONTACT
;
1319 /***************************************************************
1320 Returns true iff players are allied.
1321 ***************************************************************/
1322 bool pplayers_allied(const struct player
*pplayer
,
1323 const struct player
*pplayer2
)
1325 enum diplstate_type ds
;
1327 if (!pplayer
|| !pplayer2
) {
1331 if (pplayer
== pplayer2
) {
1335 ds
= player_diplstate_get(pplayer
, pplayer2
)->type
;
1337 return (ds
== DS_ALLIANCE
|| ds
== DS_TEAM
);
1340 /***************************************************************
1341 Returns true iff players are allied or at peace.
1342 ***************************************************************/
1343 bool pplayers_in_peace(const struct player
*pplayer
,
1344 const struct player
*pplayer2
)
1346 enum diplstate_type ds
= player_diplstate_get(pplayer
, pplayer2
)->type
;
1348 if (pplayer
== pplayer2
) {
1352 return (ds
== DS_PEACE
|| ds
== DS_ALLIANCE
1353 || ds
== DS_ARMISTICE
|| ds
== DS_TEAM
);
1356 /****************************************************************************
1357 Returns TRUE if players can't enter each others' territory.
1358 ****************************************************************************/
1359 bool players_non_invade(const struct player
*pplayer1
,
1360 const struct player
*pplayer2
)
1362 if (pplayer1
== pplayer2
|| !pplayer1
|| !pplayer2
) {
1366 /* Movement during armistice is allowed so that player can withdraw
1367 units deeper inside opponent territory. */
1369 return player_diplstate_get(pplayer1
, pplayer2
)->type
== DS_PEACE
;
1372 /***************************************************************
1373 Returns true iff players have peace, cease-fire, or
1375 ***************************************************************/
1376 bool pplayers_non_attack(const struct player
*pplayer
,
1377 const struct player
*pplayer2
)
1379 enum diplstate_type ds
;
1381 if (pplayer
== pplayer2
) {
1385 ds
= player_diplstate_get(pplayer
, pplayer2
)->type
;
1387 return (ds
== DS_PEACE
|| ds
== DS_CEASEFIRE
|| ds
== DS_ARMISTICE
);
1390 /**************************************************************************
1391 Return TRUE if players are in the same team
1392 **************************************************************************/
1393 bool players_on_same_team(const struct player
*pplayer1
,
1394 const struct player
*pplayer2
)
1396 return pplayer1
->team
== pplayer2
->team
;
1399 /**************************************************************************
1400 Return TRUE iff the player me gives shared vision to player them.
1401 **************************************************************************/
1402 bool gives_shared_vision(const struct player
*me
, const struct player
*them
)
1404 return BV_ISSET(me
->gives_shared_vision
, player_index(them
));
1407 /**************************************************************************
1408 Return TRUE iff the two diplstates are equal.
1409 **************************************************************************/
1410 bool are_diplstates_equal(const struct player_diplstate
*pds1
,
1411 const struct player_diplstate
*pds2
)
1413 return (pds1
->type
== pds2
->type
&& pds1
->turns_left
== pds2
->turns_left
1414 && pds1
->has_reason_to_cancel
== pds2
->has_reason_to_cancel
1415 && pds1
->contact_turns_left
== pds2
->contact_turns_left
);
1418 /**************************************************************************
1419 Return TRUE iff player1 has the diplomatic relation to player2
1420 **************************************************************************/
1421 bool is_diplrel_between(const struct player
*player1
,
1422 const struct player
*player2
,
1425 fc_assert(player1
!= NULL
);
1426 fc_assert(player2
!= NULL
);
1428 /* No relationship to it self. */
1429 if (player1
== player2
&& diplrel
!= DRO_FOREIGN
) {
1433 if (diplrel
< DS_LAST
) {
1434 return player_diplstate_get(player1
, player2
)->type
== diplrel
;
1438 case DRO_GIVES_SHARED_VISION
:
1439 return gives_shared_vision(player1
, player2
);
1440 case DRO_RECEIVES_SHARED_VISION
:
1441 return gives_shared_vision(player2
, player1
);
1442 case DRO_HOSTS_EMBASSY
:
1443 return player_has_embassy(player2
, player1
);
1444 case DRO_HAS_EMBASSY
:
1445 return player_has_embassy(player1
, player2
);
1446 case DRO_HOSTS_REAL_EMBASSY
:
1447 return player_has_real_embassy(player2
, player1
);
1448 case DRO_HAS_REAL_EMBASSY
:
1449 return player_has_real_embassy(player1
, player2
);
1450 case DRO_HAS_CASUS_BELLI
:
1451 return 0 < player_diplstate_get(player1
, player2
)->has_reason_to_cancel
;
1452 case DRO_PROVIDED_CASUS_BELLI
:
1453 return 0 < player_diplstate_get(player2
, player1
)->has_reason_to_cancel
;
1455 return player1
!= player2
;
1458 fc_assert_msg(FALSE
, "diplrel_between(): invalid diplrel number %d.",
1464 /**************************************************************************
1465 Return TRUE iff pplayer has the diplomatic relation to any living player
1466 **************************************************************************/
1467 bool is_diplrel_to_other(const struct player
*pplayer
, int diplrel
)
1469 fc_assert(pplayer
!= NULL
);
1471 players_iterate_alive(oplayer
) {
1472 if (oplayer
== pplayer
) {
1475 if (is_diplrel_between(pplayer
, oplayer
, diplrel
)) {
1478 } players_iterate_alive_end
;
1482 /**************************************************************************
1483 Return the diplomatic relation that has the given (untranslated) rule
1485 **************************************************************************/
1486 int diplrel_by_rule_name(const char *value
)
1488 /* Look for asymmetric diplomatic relations */
1489 int diplrel
= diplrel_other_by_name(value
, fc_strcasecmp
);
1491 if (diplrel
!= diplrel_other_invalid()) {
1495 /* Look for symmetric diplomatic relations */
1496 diplrel
= diplstate_type_by_name(value
, fc_strcasecmp
);
1499 * Make sure DS_LAST isn't returned as DS_LAST is the first diplrel_other.
1501 * Can't happend now. This is in case that changes in the future. */
1502 fc_assert_ret_val(diplrel
!= DS_LAST
, diplrel_other_invalid());
1505 * Make sure that diplrel_other_invalid() is returned.
1507 * Can't happen now. At the moment dpilrel_asym_invalid() is the same as
1508 * diplstate_type_invalid(). This is in case that changes in the future.
1510 if (diplrel
!= diplstate_type_invalid()) {
1514 return diplrel_other_invalid();
1517 /**************************************************************************
1518 Return the (untranslated) rule name of the given diplomatic relation.
1519 **************************************************************************/
1520 const char *diplrel_rule_name(int value
)
1522 if (value
< DS_LAST
) {
1523 return diplstate_type_name(value
);
1525 return diplrel_other_name(value
);
1529 /**************************************************************************
1530 Return the translated name of the given diplomatic relation.
1531 **************************************************************************/
1532 const char *diplrel_name_translation(int value
)
1534 if (value
< DS_LAST
) {
1535 return diplstate_type_translated_name(value
);
1537 return _(diplrel_other_name(value
));
1541 /* The number of mutually exclusive requirement sets that
1542 * diplrel_mess_gen() creates for the DiplRel requirement type. */
1543 #define DIPLREL_MESS_SIZE (3 + (DRO_LAST * (5 + 4 + 3 + 2 + 1)))
1545 /**************************************************************************
1546 Generate and return an array of mutually exclusive requirement sets for
1547 the DiplRel requirement type. The array has DIPLREL_MESS_SIZE sets.
1549 A mutually exclusive set is a set of requirements were the presence of
1550 one requirement proves the absence of every other requirement. In other
1551 words: at most one of the requirements in the set can be present.
1552 **************************************************************************/
1553 static bv_diplrel_all_reqs
*diplrel_mess_gen(void)
1555 /* The ranges supported by the DiplRel requiremnt type. */
1556 const enum req_range legal_ranges
[] = {
1569 /* Storage for the mutually exclusive requirement sets. */
1570 bv_diplrel_all_reqs
*mess
= fc_malloc(DIPLREL_MESS_SIZE
1571 * sizeof(bv_diplrel_all_reqs
));
1573 /* Position in mess. */
1576 /* The first mutually exclusive set is about local diplstate. */
1577 BV_CLR_ALL(mess
[mess_pos
]);
1579 /* It is not possible to have more than one diplstate to a nation. */
1580 BV_SET(mess
[mess_pos
],
1581 requirement_diplrel_ereq(DS_ARMISTICE
, REQ_RANGE_LOCAL
, TRUE
));
1582 BV_SET(mess
[mess_pos
],
1583 requirement_diplrel_ereq(DS_WAR
, REQ_RANGE_LOCAL
, TRUE
));
1584 BV_SET(mess
[mess_pos
],
1585 requirement_diplrel_ereq(DS_CEASEFIRE
, REQ_RANGE_LOCAL
, TRUE
));
1586 BV_SET(mess
[mess_pos
],
1587 requirement_diplrel_ereq(DS_PEACE
, REQ_RANGE_LOCAL
, TRUE
));
1588 BV_SET(mess
[mess_pos
],
1589 requirement_diplrel_ereq(DS_ALLIANCE
, REQ_RANGE_LOCAL
, TRUE
));
1590 BV_SET(mess
[mess_pos
],
1591 requirement_diplrel_ereq(DS_NO_CONTACT
, REQ_RANGE_LOCAL
, TRUE
));
1592 BV_SET(mess
[mess_pos
],
1593 requirement_diplrel_ereq(DS_TEAM
, REQ_RANGE_LOCAL
, TRUE
));
1595 /* It is not possible to have a diplstate to your self. */
1596 BV_SET(mess
[mess_pos
],
1597 requirement_diplrel_ereq(DRO_FOREIGN
, REQ_RANGE_LOCAL
, FALSE
));
1601 /* Having a real embassy excludes not having an embassy. */
1602 BV_CLR_ALL(mess
[mess_pos
]);
1604 BV_SET(mess
[mess_pos
],
1605 requirement_diplrel_ereq(DRO_HAS_REAL_EMBASSY
, REQ_RANGE_LOCAL
,
1607 BV_SET(mess
[mess_pos
],
1608 requirement_diplrel_ereq(DRO_HAS_EMBASSY
, REQ_RANGE_LOCAL
,
1613 /* Hosting a real embassy excludes not hosting an embassy. */
1614 BV_CLR_ALL(mess
[mess_pos
]);
1616 BV_SET(mess
[mess_pos
],
1617 requirement_diplrel_ereq(DRO_HOSTS_REAL_EMBASSY
, REQ_RANGE_LOCAL
,
1619 BV_SET(mess
[mess_pos
],
1620 requirement_diplrel_ereq(DRO_HOSTS_EMBASSY
, REQ_RANGE_LOCAL
,
1625 /* Loop over diplstate_type and diplrel_other. */
1626 for (rel
= 0; rel
< DRO_LAST
; rel
++) {
1627 /* The presence of a DiplRel at a more local range proves that it can't
1628 * be absent in a more global range. (The alliance range includes the
1630 for (i
= 0; i
< 5; i
++) {
1631 for (j
= i
; j
< 5; j
++) {
1632 BV_CLR_ALL(mess
[mess_pos
]);
1634 BV_SET(mess
[mess_pos
],
1635 requirement_diplrel_ereq(rel
, legal_ranges
[i
], TRUE
));
1636 BV_SET(mess
[mess_pos
],
1637 requirement_diplrel_ereq(rel
, legal_ranges
[j
], FALSE
));
1644 /* No uninitialized element exists. */
1645 fc_assert(mess_pos
== DIPLREL_MESS_SIZE
);
1650 /* An array of mutually exclusive requirement sets for the DiplRel
1651 * requirement type. Is initialized the first time diplrel_mess_get() is
1653 static bv_diplrel_all_reqs
*diplrel_mess
= NULL
;
1655 /**************************************************************************
1656 Get the mutually exclusive requirement sets for DiplRel.
1657 **************************************************************************/
1658 static bv_diplrel_all_reqs
*diplrel_mess_get(void)
1660 if (diplrel_mess
== NULL
) {
1661 /* This is the first call. Initialize diplrel_mess. */
1662 diplrel_mess
= diplrel_mess_gen();
1665 return diplrel_mess
;
1668 /**************************************************************************
1670 **************************************************************************/
1671 void diplrel_mess_close(void)
1673 if (diplrel_mess
!= NULL
) {
1675 diplrel_mess
= NULL
;
1679 /**************************************************************************
1680 Get the DiplRel requirements that are known to contradict the specified
1681 DiplRel requirement.
1683 The known contratictions have their position in the enumeration of all
1684 possible DiplRel requirements set in the returned bitvector.
1685 **************************************************************************/
1686 bv_diplrel_all_reqs
diplrel_req_contradicts(const struct requirement
*req
)
1688 int diplrel_req_num
;
1689 bv_diplrel_all_reqs
*mess
;
1690 bv_diplrel_all_reqs known
;
1693 /* Nothing is known to contradict the requirement yet. */
1696 if (req
->source
.kind
!= VUT_DIPLREL
) {
1697 /* No known contradiction of a requirement of any other kind. */
1698 fc_assert(req
->source
.kind
== VUT_DIPLREL
);
1703 /* Convert the requirement to its position in the enumeration of all
1704 * DiplRel requirements. */
1705 diplrel_req_num
= requirement_diplrel_ereq(req
->source
.value
.diplrel
,
1706 req
->range
, req
->present
);
1708 /* Get the mutually exclusive requirement sets for DiplRel. */
1709 mess
= diplrel_mess_get();
1711 /* Add all known contradictions. */
1712 for (set
= 0; set
< DIPLREL_MESS_SIZE
; set
++) {
1713 if (BV_ISSET(mess
[set
], diplrel_req_num
)) {
1714 /* The requirement req is mentioned in the set. It is therefore known
1715 * that all other requirements in the set contradicts it. They should
1716 * therefore be added to the known contradictions. */
1717 BV_SET_ALL_FROM(known
, mess
[set
]);
1721 /* The requirement isn't self contradicting. It was set by the mutually
1722 * exclusive requirement sets that mentioned it. Remove it. */
1723 BV_CLR(known
, diplrel_req_num
);
1728 /***************************************************************************
1729 Return the number of pplayer2's visible units in pplayer's territory,
1730 from the point of view of pplayer. Units that cannot be seen by pplayer
1731 will not be found (this function doesn't cheat).
1732 ***************************************************************************/
1733 int player_in_territory(const struct player
*pplayer
,
1734 const struct player
*pplayer2
)
1736 int in_territory
= 0;
1738 /* This algorithm should work at server or client. It only returns the
1739 * number of visible units (a unit can potentially hide inside the
1740 * transport of a different player).
1742 * Note this may be quite slow. An even slower alternative is to iterate
1743 * over the entire map, checking all units inside the player's territory
1744 * to see if they're owned by the enemy. */
1745 unit_list_iterate(pplayer2
->units
, punit
) {
1746 /* Get the owner of the tile/territory. */
1747 struct player
*owner
= tile_owner(unit_tile(punit
));
1749 if (owner
== pplayer
&& can_player_see_unit(pplayer
, punit
)) {
1753 } unit_list_iterate_end
;
1755 return in_territory
;
1758 /****************************************************************************
1759 Returns whether this is a valid username. This is used by the server to
1760 validate usernames and should be used by the client to avoid invalid
1762 ****************************************************************************/
1763 bool is_valid_username(const char *name
)
1765 return (strlen(name
) > 0
1766 && !fc_isdigit(name
[0])
1767 && is_ascii_name(name
)
1768 && fc_strcasecmp(name
, ANON_USER_NAME
) != 0);
1771 /***************************************************************
1772 Return is AI can be set to given level
1773 ***************************************************************/
1774 bool is_settable_ai_level(enum ai_level level
)
1776 if (level
== AI_LEVEL_AWAY
) {
1777 /* Cannot set away level for AI */
1784 /***************************************************************
1785 Return number of AI levels in game
1786 ***************************************************************/
1787 int number_of_ai_levels(void)
1789 return AI_LEVEL_COUNT
- 1; /* AI_LEVEL_AWAY is not real AI */
1792 /**************************************************************************
1793 Return pointer to ai data of given player and ai type.
1794 **************************************************************************/
1795 void *player_ai_data(const struct player
*pplayer
, const struct ai_type
*ai
)
1797 return pplayer
->server
.ais
[ai_type_number(ai
)];
1800 /**************************************************************************
1801 Attach ai data to player
1802 **************************************************************************/
1803 void player_set_ai_data(struct player
*pplayer
, const struct ai_type
*ai
,
1806 pplayer
->server
.ais
[ai_type_number(ai
)] = data
;
1809 /**************************************************************************
1810 Return the multiplier value currently in effect for pplayer (in display
1812 **************************************************************************/
1813 int player_multiplier_value(const struct player
*pplayer
,
1814 const struct multiplier
*pmul
)
1816 return pplayer
->multipliers
[multiplier_index(pmul
)];
1819 /**************************************************************************
1820 Return the multiplier value currently in effect for pplayer, scaled
1821 from display units to the units used in the effect system (if different).
1822 Result is multiplied by 100 (caller should divide down).
1823 **************************************************************************/
1824 int player_multiplier_effect_value(const struct player
*pplayer
,
1825 const struct multiplier
*pmul
)
1827 return (player_multiplier_value(pplayer
, pmul
) + pmul
->offset
)
1831 /**************************************************************************
1832 Return the player's target value for a multiplier (which may be
1833 different from the value currently in force; it will take effect
1834 next turn). Result is in display units.
1835 **************************************************************************/
1836 int player_multiplier_target_value(const struct player
*pplayer
,
1837 const struct multiplier
*pmul
)
1839 return pplayer
->multipliers_target
[multiplier_index(pmul
)];
1842 /****************************************************************************
1843 Check if player has given flag
1844 ****************************************************************************/
1845 bool player_has_flag(const struct player
*pplayer
, enum plr_flag_id flag
)
1847 return BV_ISSET(pplayer
->flags
, flag
);