Freeciv-3.1
Loading...
Searching...
No Matches
player.c
Go to the documentation of this file.
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.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "fcintl.h"
20#include "log.h"
21#include "mem.h"
22#include "shared.h"
23#include "support.h"
24
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"
41
42#include "player.h"
43
44
46 struct player *player;
47};
48
49static struct {
51 int used_slots; /* number of used/allocated players in the player slots */
53
54static void player_defaults(struct player *pplayer);
55
56static void player_diplstate_new(const struct player *plr1,
57 const struct player *plr2);
58static void player_diplstate_defaults(const struct player *plr1,
59 const struct player *plr2);
60static void player_diplstate_destroy(const struct player *plr1,
61 const struct player *plr2);
62
63/*******************************************************************/
67enum diplstate_type cancel_pact_result(enum diplstate_type oldstate)
68{
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 */
83 }
84}
85
86/*******************************************************************/
93 const struct player *p2)
94{
95 enum diplstate_type ds = player_diplstate_get(p1, p2)->type;
96
97 if (p1 == p2 || ds == DS_WAR || ds == DS_NO_CONTACT) {
98 return DIPL_ERROR;
99 }
100 if (players_on_same_team(p1, p2)) {
101 return DIPL_ERROR;
102 }
103 if (!p1->is_alive || !p2->is_alive) {
104 return DIPL_ERROR;
105 }
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) {
110 }
111
112 return DIPL_OK;
113}
114
115/*******************************************************************/
126static bool is_valid_alliance(const struct player *p1,
127 const struct player *p2)
128{
129 players_iterate_alive(pplayer) {
130 enum diplstate_type ds = player_diplstate_get(p1, pplayer)->type;
131
132 if (pplayer != p1
133 && pplayer != p2
134 && ds == DS_WAR /* Do not count 'never met' as war here */
135 && pplayers_allied(p2, pplayer)) {
136 return FALSE;
137 }
139
140 return TRUE;
141}
142
143/*******************************************************************/
154 const struct player *p2,
155 enum diplstate_type treaty)
156{
157 enum diplstate_type existing = player_diplstate_get(p1, p2)->type;
158
159 if (players_on_same_team(p1, p2)) {
160 /* This includes the case p1 == p2 */
161 return DIPL_ERROR;
162 }
163 if (get_player_bonus(p1, EFT_NO_DIPLOMACY) > 0
164 || get_player_bonus(p2, EFT_NO_DIPLOMACY) > 0) {
165 return DIPL_ERROR;
166 }
167 if (treaty == DS_WAR
168 || treaty == DS_NO_CONTACT
169 || treaty == DS_ARMISTICE
170 || treaty == DS_TEAM
171 || treaty == DS_LAST) {
172 return DIPL_ERROR; /* These are not negotiable treaties */
173 }
174 if (treaty == DS_CEASEFIRE && existing != DS_WAR) {
175 return DIPL_ERROR; /* Only available from war */
176 }
177 if (treaty == DS_PEACE
178 && (existing != DS_WAR && existing != DS_CEASEFIRE)) {
179 return DIPL_ERROR;
180 }
181 if (treaty == DS_ALLIANCE) {
182 if (!is_valid_alliance(p1, p2)) {
183 /* Our war with a third party prevents entry to alliance. */
185 } else if (!is_valid_alliance(p2, p1)) {
186 /* Their war with a third party prevents entry to alliance. */
188 }
189 }
190 /* This check must be last: */
191 if (treaty == existing) {
192 return DIPL_ERROR;
193 }
194
195 return DIPL_OK;
196}
197
198/*******************************************************************/
202bool player_has_embassy(const struct player *pplayer,
203 const struct player *pplayer2)
204{
205 return (pplayer == pplayer2
206 || player_has_real_embassy(pplayer, pplayer2)
207 || player_has_embassy_from_effect(pplayer, pplayer2));
208}
209
210/*******************************************************************/
214bool team_has_embassy(const struct team *pteam, const struct player *tgt_player)
215{
216 if (tgt_player->team == pteam) {
217 return TRUE;
218 }
219
220 player_list_iterate(team_members(pteam), teammate) {
221 if (player_has_real_embassy(teammate, tgt_player)
222 || player_has_embassy_from_effect(teammate, tgt_player)) {
223 return TRUE;
224 }
226
227 return FALSE;
228}
229
230/*******************************************************************/
234bool player_has_real_embassy(const struct player *pplayer,
235 const struct player *pplayer2)
236{
237 return BV_ISSET(pplayer->real_embassy, player_index(pplayer2));
238}
239
240/*******************************************************************/
244bool player_has_embassy_from_effect(const struct player *pplayer,
245 const struct player *pplayer2)
246{
247 return (get_player_bonus(pplayer, EFT_HAVE_EMBASSIES) > 0
248 && player_diplstate_get(pplayer, pplayer2)->type != DS_NO_CONTACT
249 && !is_barbarian(pplayer2));
250}
251
252/*******************************************************************/
255bool player_owns_city(const struct player *pplayer, const struct city *pcity)
256{
257 return (pcity && pplayer && city_owner(pcity) == pplayer);
258}
259
260/*******************************************************************/
264bool player_can_invade_tile(const struct player *pplayer,
265 const struct tile *ptile)
266{
267 const struct player *ptile_owner = tile_owner(ptile);
268
269 return (!ptile_owner
270 || ptile_owner == pplayer
271 || !players_non_invade(pplayer, ptile_owner));
272}
273
274/*******************************************************************/
278static void player_diplstate_new(const struct player *plr1,
279 const struct player *plr2)
280{
281 struct player_diplstate *diplstate;
282
283 fc_assert_ret(plr1 != NULL);
284 fc_assert_ret(plr2 != NULL);
285
286 const struct player_diplstate **diplstate_slot
287 = plr1->diplstates + player_index(plr2);
288
289 fc_assert_ret(*diplstate_slot == NULL);
290
291 diplstate = fc_calloc(1, sizeof(*diplstate));
292 *diplstate_slot = diplstate;
293}
294
295/*******************************************************************/
298static void player_diplstate_defaults(const struct player *plr1,
299 const struct player *plr2)
300{
301 struct player_diplstate *diplstate = player_diplstate_get(plr1, plr2);
302
303 fc_assert_ret(diplstate != NULL);
304
305 diplstate->type = DS_NO_CONTACT;
306 diplstate->max_state = DS_NO_CONTACT;
307 diplstate->first_contact_turn = 0;
308 diplstate->turns_left = 0;
309 diplstate->has_reason_to_cancel = 0;
310 diplstate->contact_turns_left = 0;
311 diplstate->auto_cancel_turn = -1;
312}
313
314/*******************************************************************/
318 const struct player *plr2)
319{
320 fc_assert_ret_val(plr1 != NULL, NULL);
321 fc_assert_ret_val(plr2 != NULL, NULL);
322
323 const struct player_diplstate **diplstate_slot
324 = plr1->diplstates + player_index(plr2);
325
326 fc_assert_ret_val(*diplstate_slot != NULL, NULL);
327
328 return (struct player_diplstate *) *diplstate_slot;
329}
330
331/*******************************************************************/
334static void player_diplstate_destroy(const struct player *plr1,
335 const struct player *plr2)
336{
337 fc_assert_ret(plr1 != NULL);
338 fc_assert_ret(plr2 != NULL);
339
340 const struct player_diplstate **diplstate_slot
341 = plr1->diplstates + player_index(plr2);
342
343 if (*diplstate_slot != NULL) {
344 free(player_diplstate_get(plr1, plr2));
345 }
346
347 *diplstate_slot = NULL;
348}
349
350/*******************************************************************/
354{
355 int i;
356
357 /* Init player slots. */
359 sizeof(*player_slots.slots));
360 /* Can't use the defined functions as the needed data will be
361 * defined here. */
362 for (i = 0; i < player_slot_count(); i++) {
363 player_slots.slots[i].player = NULL;
364 }
365 player_slots.used_slots = 0;
366}
367
368/*******************************************************************/
372{
373 return (player_slots.slots != NULL);
374}
375
376/*******************************************************************/
380{
381 players_iterate(pplayer) {
382 player_destroy(pplayer);
384 free(player_slots.slots);
385 player_slots.slots = NULL;
386 player_slots.used_slots = 0;
387}
388
389/*******************************************************************/
393{
394 return player_slots.slots;
395}
396
397/*******************************************************************/
401{
402 pslot++;
403 return (pslot < player_slots.slots + player_slot_count() ? pslot : NULL);
404}
405
406/*******************************************************************/
412{
413 return (MAX_NUM_PLAYER_SLOTS);
414}
415
416/*******************************************************************/
419int player_slot_index(const struct player_slot *pslot)
420{
421 fc_assert_ret_val(NULL != pslot, -1);
422
423 return pslot - player_slots.slots;
424}
425
426/*******************************************************************/
430struct player *player_slot_get_player(const struct player_slot *pslot)
431{
432 fc_assert_ret_val(NULL != pslot, NULL);
433
434 return pslot->player;
435}
436
437/*******************************************************************/
441bool player_slot_is_used(const struct player_slot *pslot)
442{
443 fc_assert_ret_val(NULL != pslot, FALSE);
444
445 /* No player slot available, if the game is not initialised. */
447 return FALSE;
448 }
449
450 return NULL != pslot->player;
451}
452
453/*******************************************************************/
456struct player_slot *player_slot_by_number(int player_id)
457{
459 || !(0 <= player_id && player_id < player_slot_count())) {
460 return NULL;
461 }
462
463 return player_slots.slots + player_id;
464}
465
466/*******************************************************************/
470{
471 int max_pslot = 0;
472
473 player_slots_iterate(pslot) {
474 if (player_slot_is_used(pslot)) {
475 max_pslot = player_slot_index(pslot);
476 }
478
479 return max_pslot;
480}
481
482/*******************************************************************/
486struct player *player_new(struct player_slot *pslot)
487{
488 struct player *pplayer;
489
491
492 if (pslot == NULL) {
493 player_slots_iterate(aslot) {
494 if (!player_slot_is_used(aslot)) {
495 pslot = aslot;
496 break;
497 }
499
500 if (pslot == NULL) {
501 return NULL;
502 }
503 } else if (NULL != pslot->player) {
504 return pslot->player;
505 }
506
507 /* Now create the player. */
508 log_debug("Create player for slot %d.", player_slot_index(pslot));
509 pplayer = fc_calloc(1, sizeof(*pplayer));
510 pplayer->slot = pslot;
511 pslot->player = pplayer;
512
514 sizeof(*pplayer->diplstates));
515 player_slots_iterate(dslot) {
516 const struct player_diplstate **diplstate_slot
517 = pplayer->diplstates + player_slot_index(dslot);
518
519 *diplstate_slot = NULL;
521
522 players_iterate(aplayer) {
523 /* Create diplomatic states for all other players. */
524 player_diplstate_new(pplayer, aplayer);
525 /* Create diplomatic state of this player. */
526 if (aplayer != pplayer) {
527 player_diplstate_new(aplayer, pplayer);
528 }
530
531 /* Set default values. */
532 player_defaults(pplayer);
533
534 /* Increase number of players. */
535 player_slots.used_slots++;
536
537 return pplayer;
538}
539
540/*******************************************************************/
544static void player_defaults(struct player *pplayer)
545{
546 int i;
547
550 pplayer->unassigned_user = TRUE;
552 pplayer->unassigned_ranked = TRUE;
553 pplayer->user_turns = 0;
554 pplayer->is_male = TRUE;
555 pplayer->government = NULL;
556 pplayer->target_government = NULL;
557 pplayer->nation = NO_NATION_SELECTED;
558 pplayer->team = NULL;
559 pplayer->is_ready = FALSE;
560 pplayer->nturns_idle = 0;
561 pplayer->is_alive = TRUE;
562 pplayer->turns_alive = 0;
563 pplayer->is_winner = FALSE;
564 pplayer->last_war_action = -1;
565 pplayer->phase_done = FALSE;
566
567 pplayer->revolution_finishes = -1;
568 pplayer->primary_capital_id = 0;
569
570 BV_CLR_ALL(pplayer->real_embassy);
571 players_iterate(aplayer) {
572 /* create diplomatic states for all other players */
573 player_diplstate_defaults(pplayer, aplayer);
574 /* create diplomatic state of this player */
575 if (aplayer != pplayer) {
576 player_diplstate_defaults(aplayer, pplayer);
577 }
579
580 pplayer->style = 0;
581 pplayer->music_style = -1; /* even getting value 0 triggers change */
582 pplayer->cities = city_list_new();
583 pplayer->units = unit_list_new();
584
585 pplayer->economic.gold = 0;
589 pplayer->economic.infra_points = 0;
590
591 spaceship_init(&pplayer->spaceship);
592
593 BV_CLR_ALL(pplayer->flags);
594
595 set_as_human(pplayer);
596 pplayer->ai_common.skill_level = ai_level_invalid();
597 pplayer->ai_common.fuzzy = 0;
598 pplayer->ai_common.expand = 100;
599 pplayer->ai_common.barbarian_type = NOT_A_BARBARIAN;
600 player_slots_iterate(pslot) {
601 pplayer->ai_common.love[player_slot_index(pslot)] = 1;
603 pplayer->ai_common.traits = NULL;
604
605 pplayer->ai = NULL;
606 pplayer->was_created = FALSE;
607 pplayer->savegame_ai_type_name = NULL;
608 pplayer->random_name = TRUE;
609 pplayer->is_connected = FALSE;
610 pplayer->current_conn = NULL;
611 pplayer->connections = conn_list_new();
613 for (i = 0; i < B_LAST; i++) {
614 pplayer->wonders[i] = WONDER_NOT_BUILT;
615 }
616
617 pplayer->attribute_block.data = NULL;
618 pplayer->attribute_block.length = 0;
619 pplayer->attribute_block_buffer.data = NULL;
620 pplayer->attribute_block_buffer.length = 0;
621
622 pplayer->tile_known.vec = NULL;
623 pplayer->tile_known.bits = 0;
624
625 pplayer->rgb = NULL;
626
627 memset(pplayer->multipliers, 0, sizeof(pplayer->multipliers));
628
629 /* pplayer->server is initialised in
630 ./server/plrhand.c:server_player_init()
631 and pplayer->client in
632 ./client/climisc.c:client_player_init() */
633}
634
635/*******************************************************************/
639void player_set_color(struct player *pplayer,
640 const struct rgbcolor *prgbcolor)
641{
642 if (pplayer->rgb != NULL) {
643 rgbcolor_destroy(pplayer->rgb);
644 pplayer->rgb = NULL;
645 }
646
647 if (prgbcolor) {
648 pplayer->rgb = rgbcolor_copy(prgbcolor);
649 }
650}
651
652/*******************************************************************/
660void player_clear(struct player *pplayer, bool full)
661{
662 bool client = !is_server();
663
664 if (pplayer == NULL) {
665 return;
666 }
667
668 if (pplayer->savegame_ai_type_name != NULL) {
669 free(pplayer->savegame_ai_type_name);
670 pplayer->savegame_ai_type_name = NULL;
671 }
672
673 /* Clears the attribute blocks. */
674 if (pplayer->attribute_block.data) {
675 free(pplayer->attribute_block.data);
676 pplayer->attribute_block.data = NULL;
677 }
678 pplayer->attribute_block.length = 0;
679
680 if (pplayer->attribute_block_buffer.data) {
681 free(pplayer->attribute_block_buffer.data);
682 pplayer->attribute_block_buffer.data = NULL;
683 }
684 pplayer->attribute_block_buffer.length = 0;
685
686 /* Clears units and cities. */
687 unit_list_iterate(pplayer->units, punit) {
688 /* Unload all cargos. */
690 unit_transport_unload(pcargo);
691 if (client) {
692 pcargo->client.transported_by = -1;
693 }
695 /* Unload the unit. */
697 if (client) {
699 }
700
703
704 city_list_iterate(pplayer->cities, pcity) {
705 if (fc_funcs->destroy_city != NULL) {
706 fc_funcs->destroy_city(pcity);
707 } else {
708 game_remove_city(&wld, pcity);
709 }
711
712 if (full) {
713 team_remove_player(pplayer);
714
716 unit_list_iterate(owner->units, owned) {
717 if (unit_nationality(owned) == pplayer) {
718 /* Switch nationality to that of current owner. */
719 owned->nationality = owner;
720 }
723
724 /* This comes last because log calls in the above functions
725 * may use it. */
726 if (pplayer->nation != NULL) {
727 player_set_nation(pplayer, NULL);
728 }
729 }
730}
731
732/*******************************************************************/
736void player_ruleset_close(struct player *pplayer)
737{
738 pplayer->government = NULL;
739 pplayer->target_government = NULL;
740 player_set_nation(pplayer, NULL);
741 pplayer->style = NULL;
742}
743
744/*******************************************************************/
747void player_destroy(struct player *pplayer)
748{
749 struct player_slot *pslot;
750
751 fc_assert_ret(NULL != pplayer);
752
753 pslot = pplayer->slot;
754 fc_assert(pslot->player == pplayer);
755
756 /* Remove all that is game-dependent in the player structure. */
757 player_clear(pplayer, TRUE);
758
759 fc_assert(0 == unit_list_size(pplayer->units));
760 unit_list_destroy(pplayer->units);
761 fc_assert(0 == city_list_size(pplayer->cities));
762 city_list_destroy(pplayer->cities);
763
764 fc_assert(conn_list_size(pplayer->connections) == 0);
765 conn_list_destroy(pplayer->connections);
766
767 players_iterate(aplayer) {
768 /* Destroy the diplomatic states of this player with others ... */
769 player_diplstate_destroy(pplayer, aplayer);
770 /* ...and of others with this player. */
771 if (aplayer != pplayer) {
772 player_diplstate_destroy(aplayer, pplayer);
773
774 /* Check if any cities of the other player have been founded
775 * by the removed player. */
776 city_list_iterate(aplayer->cities, pcity) {
777 if (pcity->original == pplayer) {
778 /* Unknown origin is better than giving baseless
779 * benefits to current owner */
780 pcity->original = NULL;
781 }
783 }
785 free(pplayer->diplstates);
786
787 /* Clear player color. */
788 if (pplayer->rgb) {
789 rgbcolor_destroy(pplayer->rgb);
790 }
791
792 dbv_free(&pplayer->tile_known);
793
794 if (!is_server()) {
796 dbv_free(&pplayer->client.tile_vision[v]);
798 }
799
800 free(pplayer);
801 pslot->player = NULL;
802 player_slots.used_slots--;
803}
804
805/*******************************************************************/
809{
810 return player_slots.used_slots;
811}
812
813/*******************************************************************/
820int player_index(const struct player *pplayer)
821{
822 return player_number(pplayer);
823}
824
825/*******************************************************************/
828int player_number(const struct player *pplayer)
829{
830 fc_assert_ret_val(NULL != pplayer, -1);
831 return player_slot_index(pplayer->slot);
832}
833
834/*******************************************************************/
840struct player *player_by_number(const int player_id)
841{
842 struct player_slot *pslot = player_slot_by_number(player_id);
843
844 return (NULL != pslot ? player_slot_get_player(pslot) : NULL);
845}
846
847/*******************************************************************/
852bool player_set_nation(struct player *pplayer, struct nation_type *pnation)
853{
854 if (pplayer->nation != pnation) {
855 if (pplayer->nation) {
856 fc_assert(pplayer->nation->player == pplayer);
857 pplayer->nation->player = NULL;
858 }
859 if (pnation) {
860 fc_assert(pnation->player == NULL);
861 pnation->player = pplayer;
862 }
863 pplayer->nation = pnation;
864 return TRUE;
865 }
866 return FALSE;
867}
868
869/*******************************************************************/
872struct player *player_by_name(const char *name)
873{
874 players_iterate(pplayer) {
875 if (fc_strcasecmp(name, pplayer->name) == 0) {
876 return pplayer;
877 }
879
880 return NULL;
881}
882
883/*******************************************************************/
886const char *player_name(const struct player *pplayer)
887{
888 if (!pplayer) {
889 return NULL;
890 }
891 return pplayer->name;
892}
893
894/*******************************************************************/
900static const char *player_name_by_number(int i)
901{
902 struct player *pplayer;
903
904 pplayer = player_by_number(i);
905 return player_name(pplayer);
906}
907
908/*******************************************************************/
912 enum m_pre_result *result)
913{
914 int ind;
915
919 name, &ind);
920
921 if (*result < M_PRE_AMBIGUOUS) {
922 return player_by_number(ind);
923 } else {
924 return NULL;
925 }
926}
927
928/*******************************************************************/
931struct player *player_by_user(const char *name)
932{
933 players_iterate(pplayer) {
934 if (fc_strcasecmp(name, pplayer->username) == 0) {
935 return pplayer;
936 }
938
939 return NULL;
940}
941
942/*******************************************************************/
945int player_age(const struct player *pplayer)
946{
947 fc_assert_ret_val(pplayer != NULL, 0);
948 return pplayer->turns_alive;
949}
950
951/*******************************************************************/
959 const struct tile *ptile)
960{
961 /* Can't see invisible units. */
962 if (!fc_funcs->player_tile_vision_get(ptile, pplayer, V_INVIS)
963 || !fc_funcs->player_tile_vision_get(ptile, pplayer, V_SUBSURFACE)) {
964 return FALSE;
965 }
966
967 /* Units within some extras may be hidden. */
968 if (!pplayers_allied(pplayer, ptile->extras_owner)) {
970 if (tile_has_extra(ptile, pextra)) {
971 return FALSE;
972 }
974 }
975
976 return TRUE;
977}
978
979/*******************************************************************/
984bool can_player_see_hypotetic_units_at(const struct player *pplayer,
985 const struct tile *ptile)
986{
987 struct city *pcity;
988
989 if (!player_can_trust_tile_has_no_units(pplayer, ptile)) {
990 /* The existence of any units at all is hidden from the player. */
991 return FALSE;
992 }
993
994 /* Can't see city units. */
995 pcity = tile_city(ptile);
996 if (pcity && !can_player_see_units_in_city(pplayer, pcity)
997 && unit_list_size(ptile->units) > 0) {
998 return FALSE;
999 }
1000
1001 /* Can't see non-allied units in transports. */
1002 unit_list_iterate(ptile->units, punit) {
1003 if (unit_type_get(punit)->transport_capacity > 0
1004 && unit_owner(punit) != pplayer) {
1005
1006 /* An ally could transport a non ally */
1007 if (unit_list_size(punit->transporting) > 0) {
1008 return FALSE;
1009 }
1010 }
1012
1013 return TRUE;
1014}
1015
1016/*******************************************************************/
1025bool can_player_see_unit_at(const struct player *pplayer,
1026 const struct unit *punit,
1027 const struct tile *ptile,
1028 bool is_transported)
1029{
1030 struct city *pcity;
1031 bool allied;
1032 struct unit_class *pclass;
1033
1034 /* If the player can't even see the tile... */
1035 if (TILE_KNOWN_SEEN != tile_get_known(ptile, pplayer)) {
1036 return FALSE;
1037 }
1038
1039 /* Don't show non-allied units that are in transports. This is logical
1040 * because allied transports can also contain our units. Shared vision
1041 * isn't taken into account. */
1042 allied = pplayers_allied(pplayer, unit_owner(punit));
1043 if (is_transported && !allied) {
1044 return FALSE;
1045 }
1046
1047 /* Units in cities may be hidden. */
1048 pcity = tile_city(ptile);
1049 if (pcity && !can_player_see_units_in_city(pplayer, pcity)) {
1050 return FALSE;
1051 }
1052
1053 /* Allied units are always seen.
1054 * See also stealth unit hiding part in map_change_seen() */
1055 if (allied) {
1056 return TRUE;
1057 }
1058
1059 /* Units within some extras may be hidden. */
1060 pclass = unit_class_get(punit);
1061
1063 if (tile_has_extra(ptile, pextra)) {
1064 return FALSE;
1065 }
1067
1068 /* Non-hiding units are always seen.
1069 * See also stealth unit hiding part in map_change_seen() */
1070 if (!is_hiding_unit(punit)) {
1071 return TRUE;
1072 }
1073
1074 /* Hiding units are only seen by the V_INVIS fog layer. */
1075 return fc_funcs->player_tile_vision_get(ptile, pplayer,
1076 unit_type_get(punit)->vlayer);
1077}
1078
1079/*******************************************************************/
1084bool can_player_see_unit(const struct player *pplayer,
1085 const struct unit *punit)
1086{
1087 return can_player_see_unit_at(pplayer, punit, unit_tile(punit),
1089}
1090
1091/*******************************************************************/
1113bool can_player_see_units_in_city(const struct player *pplayer,
1114 const struct city *pcity)
1115{
1116 return (!pplayer
1117 || can_player_see_city_internals(pplayer, pcity)
1118 || pplayers_allied(pplayer, city_owner(pcity)));
1119}
1120
1121/*******************************************************************/
1129bool can_player_see_city_internals(const struct player *pplayer,
1130 const struct city *pcity)
1131{
1132 return (!pplayer || pplayer == city_owner(pcity));
1133}
1134
1135/*******************************************************************/
1143bool player_can_see_city_externals(const struct player *pow_player,
1144 const struct city *target_city) {
1146 fc_assert_ret_val(pow_player, FALSE);
1147
1148 if (can_player_see_city_internals(pow_player, target_city)) {
1149 /* City internals includes city externals. */
1150 return TRUE;
1151 }
1152
1153 if (tile_is_seen(city_tile(target_city), pow_player)) {
1154 /* The tile is being observed. */
1155 return TRUE;
1156 }
1157
1159
1161 if (city_owner(trade_city) == pow_player) {
1162 /* Revealed because of the trade route. */
1163 return TRUE;
1164 }
1166
1167 return FALSE;
1168}
1169
1170/*******************************************************************/
1179struct city *player_city_by_number(const struct player *pplayer, int city_id)
1180{
1181 /* We call idex directly. Should use game_city_by_number() instead? */
1182 struct city *pcity = idex_lookup_city(&wld, city_id);
1183
1184 if (!pcity) {
1185 return NULL;
1186 }
1187
1188 if (!pplayer || (city_owner(pcity) == pplayer)) {
1189 /* Correct owner */
1190 return pcity;
1191 }
1192
1193 return NULL;
1194}
1195
1196/*******************************************************************/
1205struct unit *player_unit_by_number(const struct player *pplayer, int unit_id)
1206{
1207 /* We call idex directly. Should use game_unit_by_number() instead? */
1208 struct unit *punit = idex_lookup_unit(&wld, unit_id);
1209
1210 if (!punit) {
1211 return NULL;
1212 }
1213
1214 if (!pplayer || (unit_owner(punit) == pplayer)) {
1215 /* Correct owner */
1216 return punit;
1217 }
1218
1219 return NULL;
1220}
1221
1222/*******************************************************************/
1225bool player_in_city_map(const struct player *pplayer,
1226 const struct tile *ptile)
1227{
1228 const struct civ_map *nmap = &(wld.map);
1229
1230 city_tile_iterate(nmap, CITY_MAP_MAX_RADIUS_SQ, ptile, ptile1) {
1231 struct city *pcity = tile_city(ptile1);
1232
1233 if (pcity
1234 && (pplayer == NULL || city_owner(pcity) == pplayer)
1235 && city_map_radius_sq_get(pcity) >= sq_map_distance(ptile,
1236 ptile1)) {
1237 return TRUE;
1238 }
1240
1241 return FALSE;
1242}
1243
1244/*******************************************************************/
1249int num_known_tech_with_flag(const struct player *pplayer,
1250 enum tech_flag_id flag)
1251{
1252 return research_get(pplayer)->num_known_tech_with_flag[flag];
1253}
1254
1255/*******************************************************************/
1262int player_get_expected_income(const struct player *pplayer)
1263{
1264 int income = 0;
1265
1266 /* City income/expenses. */
1267 city_list_iterate(pplayer->cities, pcity) {
1268 /* Gold suplus accounts for imcome plus building and unit upkeep. */
1269 income += pcity->surplus[O_GOLD];
1270
1271 /* Gold upkeep for buildings and units is defined by the setting
1272 * 'game.info.gold_upkeep_style':
1273 * GOLD_UPKEEP_CITY: Cities pay for buildings and units (this is
1274 * included in pcity->surplus[O_GOLD]).
1275 * GOLD_UPKEEP_MIXED: Cities pay only for buildings; the nation pays
1276 * for units.
1277 * GOLD_UPKEEP_NATION: The nation pays for buildings and units. */
1278 switch (game.info.gold_upkeep_style) {
1279 case GOLD_UPKEEP_CITY:
1280 break;
1281 case GOLD_UPKEEP_NATION:
1282 /* Nation pays for buildings (and units). */
1283 income -= city_total_impr_gold_upkeep(pcity);
1284 fc__fallthrough; /* No break. */
1285 case GOLD_UPKEEP_MIXED:
1286 /* Nation pays for units. */
1287 income -= city_total_unit_gold_upkeep(pcity);
1288 break;
1289 }
1290
1291 /* Capitalization income. */
1292 if (city_production_has_flag(pcity, IF_GOLD)) {
1293 income += pcity->shield_stock + pcity->surplus[O_SHIELD];
1294 }
1296
1297 return income;
1298}
1299
1300/*******************************************************************/
1304bool player_knows_techs_with_flag(const struct player *pplayer,
1305 enum tech_flag_id flag)
1306{
1307 return num_known_tech_with_flag(pplayer, flag) > 0;
1308}
1309
1310/*******************************************************************/
1313struct city *player_primary_capital(const struct player *pplayer)
1314{
1315 struct city *capital;
1316
1317 if (!pplayer) {
1318 /* The client depends on this behavior in some places. */
1319 return NULL;
1320 }
1321
1323
1324 return capital;
1325}
1326
1327/*******************************************************************/
1330const char *love_text(const int love)
1331{
1332 if (love <= - MAX_AI_LOVE * 90 / 100) {
1333 /* TRANS: These words should be adjectives which can fit in the sentence
1334 "The x are y towards us"
1335 "The Babylonians are respectful towards us" */
1336 return Q_("?attitude:Genocidal");
1337 } else if (love <= - MAX_AI_LOVE * 70 / 100) {
1338 return Q_("?attitude:Belligerent");
1339 } else if (love <= - MAX_AI_LOVE * 50 / 100) {
1340 return Q_("?attitude:Hostile");
1341 } else if (love <= - MAX_AI_LOVE * 25 / 100) {
1342 return Q_("?attitude:Uncooperative");
1343 } else if (love <= - MAX_AI_LOVE * 10 / 100) {
1344 return Q_("?attitude:Uneasy");
1345 } else if (love <= MAX_AI_LOVE * 10 / 100) {
1346 return Q_("?attitude:Neutral");
1347 } else if (love <= MAX_AI_LOVE * 25 / 100) {
1348 return Q_("?attitude:Respectful");
1349 } else if (love <= MAX_AI_LOVE * 50 / 100) {
1350 return Q_("?attitude:Helpful");
1351 } else if (love <= MAX_AI_LOVE * 70 / 100) {
1352 return Q_("?attitude:Enthusiastic");
1353 } else if (love <= MAX_AI_LOVE * 90 / 100) {
1354 return Q_("?attitude:Admiring");
1355 } else {
1356 fc_assert(love > MAX_AI_LOVE * 90 / 100);
1357 return Q_("?attitude:Worshipful");
1358 }
1359}
1360
1361/*******************************************************************/
1364bool pplayers_at_war(const struct player *pplayer,
1365 const struct player *pplayer2)
1366{
1367 enum diplstate_type ds;
1368
1369 if (pplayer == pplayer2) {
1370 return FALSE;
1371 }
1372
1373 ds = player_diplstate_get(pplayer, pplayer2)->type;
1374
1375 return ds == DS_WAR || ds == DS_NO_CONTACT;
1376}
1377
1378/*******************************************************************/
1381bool pplayers_allied(const struct player *pplayer,
1382 const struct player *pplayer2)
1383{
1384 enum diplstate_type ds;
1385
1386 if (!pplayer || !pplayer2) {
1387 return FALSE;
1388 }
1389
1390 if (pplayer == pplayer2) {
1391 return TRUE;
1392 }
1393
1394 ds = player_diplstate_get(pplayer, pplayer2)->type;
1395
1396 return (ds == DS_ALLIANCE || ds == DS_TEAM);
1397}
1398
1399/*******************************************************************/
1402bool pplayers_in_peace(const struct player *pplayer,
1403 const struct player *pplayer2)
1404{
1405 enum diplstate_type ds = player_diplstate_get(pplayer, pplayer2)->type;
1406
1407 if (pplayer == pplayer2) {
1408 return TRUE;
1409 }
1410
1411 return (ds == DS_PEACE || ds == DS_ALLIANCE
1412 || ds == DS_ARMISTICE || ds == DS_TEAM);
1413}
1414
1415/*******************************************************************/
1418bool players_non_invade(const struct player *pplayer1,
1419 const struct player *pplayer2)
1420{
1421 if (pplayer1 == pplayer2 || !pplayer1 || !pplayer2) {
1422 return FALSE;
1423 }
1424
1425 /* Movement during armistice is allowed so that player can withdraw
1426 units deeper inside opponent territory. */
1427
1428 return player_diplstate_get(pplayer1, pplayer2)->type == DS_PEACE;
1429}
1430
1431/*******************************************************************/
1435bool pplayers_non_attack(const struct player *pplayer,
1436 const struct player *pplayer2)
1437{
1438 enum diplstate_type ds;
1439
1440 if (pplayer == pplayer2) {
1441 return FALSE;
1442 }
1443
1444 ds = player_diplstate_get(pplayer, pplayer2)->type;
1445
1446 return (ds == DS_PEACE || ds == DS_CEASEFIRE || ds == DS_ARMISTICE);
1447}
1448
1449/*******************************************************************/
1452bool players_on_same_team(const struct player *pplayer1,
1453 const struct player *pplayer2)
1454{
1455 return pplayer1->team == pplayer2->team;
1456}
1457
1458/*******************************************************************/
1461bool gives_shared_vision(const struct player *me, const struct player *them)
1462{
1463 return BV_ISSET(me->gives_shared_vision, player_index(them));
1464}
1465
1466/*******************************************************************/
1470 const struct player_diplstate *pds2)
1471{
1472 return (pds1->type == pds2->type && pds1->turns_left == pds2->turns_left
1474 && pds1->contact_turns_left == pds2->contact_turns_left);
1475}
1476
1477/*******************************************************************/
1480bool is_diplrel_between(const struct player *player1,
1481 const struct player *player2,
1482 int diplrel)
1483{
1484 fc_assert(player1 != NULL);
1485 fc_assert(player2 != NULL);
1486
1487 /* No relationship to it self. */
1488 if (player1 == player2 && diplrel != DRO_FOREIGN) {
1489 return FALSE;
1490 }
1491
1492 if (diplrel < DS_LAST) {
1493 return player_diplstate_get(player1, player2)->type == diplrel;
1494 }
1495
1496 switch (diplrel) {
1497 case DRO_GIVES_SHARED_VISION:
1498 return gives_shared_vision(player1, player2);
1499 case DRO_RECEIVES_SHARED_VISION:
1500 return gives_shared_vision(player2, player1);
1501 case DRO_HOSTS_EMBASSY:
1502 return player_has_embassy(player2, player1);
1503 case DRO_HAS_EMBASSY:
1504 return player_has_embassy(player1, player2);
1505 case DRO_HOSTS_REAL_EMBASSY:
1506 return player_has_real_embassy(player2, player1);
1507 case DRO_HAS_REAL_EMBASSY:
1508 return player_has_real_embassy(player1, player2);
1509 case DRO_HAS_CASUS_BELLI:
1510 return 0 < player_diplstate_get(player1, player2)->has_reason_to_cancel;
1511 case DRO_PROVIDED_CASUS_BELLI:
1512 return 0 < player_diplstate_get(player2, player1)->has_reason_to_cancel;
1513 case DRO_FOREIGN:
1514 return player1 != player2;
1515 }
1516
1517 fc_assert_msg(FALSE, "diplrel_between(): invalid diplrel number %d.",
1518 diplrel);
1519
1520 return FALSE;
1521}
1522
1523/*******************************************************************/
1526bool is_diplrel_to_other(const struct player *pplayer, int diplrel)
1527{
1528 fc_assert(pplayer != NULL);
1529
1530 players_iterate_alive(oplayer) {
1531 if (oplayer == pplayer) {
1532 continue;
1533 }
1534 if (is_diplrel_between(pplayer, oplayer, diplrel)) {
1535 return TRUE;
1536 }
1538 return FALSE;
1539}
1540
1541/*******************************************************************/
1545int diplrel_by_rule_name(const char *value)
1546{
1547 /* Look for asymmetric diplomatic relations */
1548 int diplrel = diplrel_other_by_name(value, fc_strcasecmp);
1549
1550 if (diplrel != diplrel_other_invalid()) {
1551 return diplrel;
1552 }
1553
1554 /* Look for symmetric diplomatic relations */
1555 diplrel = diplstate_type_by_name(value, fc_strcasecmp);
1556
1557 /*
1558 * Make sure DS_LAST isn't returned as DS_LAST is the first diplrel_other.
1559 *
1560 * Can't happend now. This is in case that changes in the future. */
1561 fc_assert_ret_val(diplrel != DS_LAST, diplrel_other_invalid());
1562
1563 /*
1564 * Make sure that diplrel_other_invalid() is returned.
1565 *
1566 * Can't happen now. At the moment dpilrel_asym_invalid() is the same as
1567 * diplstate_type_invalid(). This is in case that changes in the future.
1568 */
1569 if (diplrel != diplstate_type_invalid()) {
1570 return diplrel;
1571 }
1572
1573 return diplrel_other_invalid();
1574}
1575
1576/*******************************************************************/
1579const char *diplrel_rule_name(int value)
1580{
1581 if (value < DS_LAST) {
1582 return diplstate_type_name(value);
1583 } else {
1584 return diplrel_other_name(value);
1585 }
1586}
1587
1588/*******************************************************************/
1591const char *diplrel_name_translation(int value)
1592{
1593 if (value < DS_LAST) {
1594 return diplstate_type_translated_name(value);
1595 } else {
1596 return _(diplrel_other_name(value));
1597 }
1598}
1599
1600/**************************************************************************
1601 Return the Casus Belli range when offender performs paction to tgt_plr
1602 at tgt_tile and the outcome is outcome.
1603**************************************************************************/
1604enum casus_belli_range casus_belli_range_for(const struct player *offender,
1605 const struct unit_type *off_ut,
1606 const struct player *tgt_plr,
1607 const enum effect_type outcome,
1608 const struct action *paction,
1609 const struct tile *tgt_tile)
1610{
1611 int casus_belli_amount;
1612
1613 /* The victim gets a casus belli if CASUS_BELLI_VICTIM or above. Everyone
1614 * gets a casus belli if CASUS_BELLI_OUTRAGE or above. */
1615 casus_belli_amount =
1617 &(const struct req_context) {
1618 .player = offender,
1619 .city = tile_city(tgt_tile),
1620 .tile = tgt_tile,
1621 .unittype = off_ut,
1622 .action = paction,
1623 },
1624 tgt_plr,
1625 outcome);
1626
1627 if (casus_belli_amount >= CASUS_BELLI_OUTRAGE) {
1628 /* International outrage: This isn't just between the offender and the
1629 * victim. */
1630 return CBR_INTERNATIONAL_OUTRAGE;
1631 }
1632
1633 if (casus_belli_amount >= CASUS_BELLI_VICTIM) {
1634 /* In this situation the specified action provides a casus belli
1635 * against the actor. */
1636 return CBR_VICTIM_ONLY;
1637 }
1638
1639 return CBR_NONE;
1640}
1641
1642/* The number of mutually exclusive requirement sets that
1643 * diplrel_mess_gen() creates for the DiplRel requirement type. */
1644#define DIPLREL_MESS_SIZE (3 + (DRO_LAST * (5 + 4 + 3 + 2 + 1)))
1645
1646/*******************************************************************/
1654static bv_diplrel_all_reqs *diplrel_mess_gen(void)
1655{
1656 /* The ranges supported by the DiplRel requiremnt type. */
1657 const enum req_range legal_ranges[] = {
1658 REQ_RANGE_LOCAL,
1659 REQ_RANGE_PLAYER,
1660 REQ_RANGE_ALLIANCE,
1661 REQ_RANGE_TEAM,
1662 REQ_RANGE_WORLD
1663 };
1664
1665 /* Iterators. */
1666 int rel;
1667 int i;
1668 int j;
1669
1670 /* Storage for the mutually exclusive requirement sets. */
1671 bv_diplrel_all_reqs *mess = fc_malloc(DIPLREL_MESS_SIZE
1672 * sizeof(bv_diplrel_all_reqs));
1673
1674 /* Position in mess. */
1675 int mess_pos = 0;
1676
1677 /* The first mutually exclusive set is about local diplstate. */
1678 BV_CLR_ALL(mess[mess_pos]);
1679
1680 /* It is not possible to have more than one diplstate to a nation. */
1681 BV_SET(mess[mess_pos],
1682 requirement_diplrel_ereq(DS_ARMISTICE, REQ_RANGE_LOCAL, TRUE));
1683 BV_SET(mess[mess_pos],
1684 requirement_diplrel_ereq(DS_WAR, REQ_RANGE_LOCAL, TRUE));
1685 BV_SET(mess[mess_pos],
1686 requirement_diplrel_ereq(DS_CEASEFIRE, REQ_RANGE_LOCAL, TRUE));
1687 BV_SET(mess[mess_pos],
1688 requirement_diplrel_ereq(DS_PEACE, REQ_RANGE_LOCAL, TRUE));
1689 BV_SET(mess[mess_pos],
1690 requirement_diplrel_ereq(DS_ALLIANCE, REQ_RANGE_LOCAL, TRUE));
1691 BV_SET(mess[mess_pos],
1692 requirement_diplrel_ereq(DS_NO_CONTACT, REQ_RANGE_LOCAL, TRUE));
1693 BV_SET(mess[mess_pos],
1694 requirement_diplrel_ereq(DS_TEAM, REQ_RANGE_LOCAL, TRUE));
1695
1696 /* It is not possible to have a diplstate to your self. */
1697 BV_SET(mess[mess_pos],
1698 requirement_diplrel_ereq(DRO_FOREIGN, REQ_RANGE_LOCAL, FALSE));
1699
1700 mess_pos++;
1701
1702 /* Having a real embassy excludes not having an embassy. */
1703 BV_CLR_ALL(mess[mess_pos]);
1704
1705 BV_SET(mess[mess_pos],
1706 requirement_diplrel_ereq(DRO_HAS_REAL_EMBASSY, REQ_RANGE_LOCAL,
1707 TRUE));
1708 BV_SET(mess[mess_pos],
1709 requirement_diplrel_ereq(DRO_HAS_EMBASSY, REQ_RANGE_LOCAL,
1710 FALSE));
1711
1712 mess_pos++;
1713
1714 /* Hosting a real embassy excludes not hosting an embassy. */
1715 BV_CLR_ALL(mess[mess_pos]);
1716
1717 BV_SET(mess[mess_pos],
1718 requirement_diplrel_ereq(DRO_HOSTS_REAL_EMBASSY, REQ_RANGE_LOCAL,
1719 TRUE));
1720 BV_SET(mess[mess_pos],
1721 requirement_diplrel_ereq(DRO_HOSTS_EMBASSY, REQ_RANGE_LOCAL,
1722 FALSE));
1723
1724 mess_pos++;
1725
1726 /* Loop over diplstate_type and diplrel_other. */
1727 for (rel = 0; rel < DRO_LAST; rel++) {
1728 /* The presence of a DiplRel at a more local range proves that it can't
1729 * be absent in a more global range. (The alliance range includes the
1730 * Team range) */
1731 for (i = 0; i < 5; i++) {
1732 for (j = i; j < 5; j++) {
1733 BV_CLR_ALL(mess[mess_pos]);
1734
1735 BV_SET(mess[mess_pos],
1736 requirement_diplrel_ereq(rel, legal_ranges[i], TRUE));
1737 BV_SET(mess[mess_pos],
1738 requirement_diplrel_ereq(rel, legal_ranges[j], FALSE));
1739
1740 mess_pos++;
1741 }
1742 }
1743 }
1744
1745 /* No uninitialized element exists. */
1746 fc_assert(mess_pos == DIPLREL_MESS_SIZE);
1747
1748 return mess;
1749}
1750
1751/* An array of mutually exclusive requirement sets for the DiplRel
1752 * requirement type. Is initialized the first time diplrel_mess_get() is
1753 * called. */
1754static bv_diplrel_all_reqs *diplrel_mess = NULL;
1755
1756/*******************************************************************/
1759static bv_diplrel_all_reqs *diplrel_mess_get(void)
1760{
1761 if (diplrel_mess == NULL) {
1762 /* This is the first call. Initialize diplrel_mess. */
1764 }
1765
1766 return diplrel_mess;
1767}
1768
1769/*******************************************************************/
1773{
1774 if (diplrel_mess != NULL) {
1775 free(diplrel_mess);
1776 diplrel_mess = NULL;
1777 }
1778}
1779
1780/*******************************************************************/
1787bv_diplrel_all_reqs diplrel_req_contradicts(const struct requirement *req)
1788{
1789 int diplrel_req_num;
1790 bv_diplrel_all_reqs *mess;
1791 bv_diplrel_all_reqs known;
1792 int set;
1793
1794 /* Nothing is known to contradict the requirement yet. */
1795 BV_CLR_ALL(known);
1796
1797 if (!(req->source.kind == VUT_DIPLREL
1798 || req->source.kind == VUT_DIPLREL_TILE
1799 || req->source.kind == VUT_DIPLREL_TILE_O
1800 || req->source.kind == VUT_DIPLREL_UNITANY
1801 || req->source.kind == VUT_DIPLREL_UNITANY_O)) {
1802 /* No known contradiction of a requirement of any other kind. */
1803 fc_assert(req->source.kind == VUT_DIPLREL
1804 || req->source.kind == VUT_DIPLREL_TILE
1805 || req->source.kind == VUT_DIPLREL_TILE_O
1806 || req->source.kind == VUT_DIPLREL_UNITANY
1807 || req->source.kind == VUT_DIPLREL_UNITANY_O);
1808
1809 return known;
1810 }
1811
1812 /* Convert the requirement to its position in the enumeration of all
1813 * DiplRel requirements. */
1814 diplrel_req_num = requirement_diplrel_ereq(req->source.value.diplrel,
1815 req->range, req->present);
1816
1817 /* Get the mutually exclusive requirement sets for DiplRel. */
1818 mess = diplrel_mess_get();
1819
1820 /* Add all known contradictions. */
1821 for (set = 0; set < DIPLREL_MESS_SIZE; set++) {
1822 if (BV_ISSET(mess[set], diplrel_req_num)) {
1823 /* The requirement req is mentioned in the set. It is therefore known
1824 * that all other requirements in the set contradicts it. They should
1825 * therefore be added to the known contradictions. */
1826 BV_SET_ALL_FROM(known, mess[set]);
1827 }
1828 }
1829
1830 /* The requirement isn't self contradicting. It was set by the mutually
1831 * exclusive requirement sets that mentioned it. Remove it. */
1832 BV_CLR(known, diplrel_req_num);
1833
1834 return known;
1835}
1836
1837/*******************************************************************/
1842int player_in_territory(const struct player *pplayer,
1843 const struct player *pplayer2)
1844{
1845 int in_territory = 0;
1846
1847 /* This algorithm should work at server or client. It only returns the
1848 * number of visible units (a unit can potentially hide inside the
1849 * transport of a different player).
1850 *
1851 * Note this may be quite slow. An even slower alternative is to iterate
1852 * over the entire map, checking all units inside the player's territory
1853 * to see if they're owned by the enemy. */
1854 unit_list_iterate(pplayer2->units, punit) {
1855 /* Get the owner of the tile/territory. */
1857
1858 if (owner == pplayer && can_player_see_unit(pplayer, punit)) {
1859 /* Found one! */
1860 in_territory += 1;
1861 }
1863
1864 return in_territory;
1865}
1866
1867/*******************************************************************/
1872bool is_valid_username(const char *name)
1873{
1874 return (strlen(name) > 0
1875 && !fc_isdigit(name[0])
1878}
1879
1880/*******************************************************************/
1883bool is_settable_ai_level(enum ai_level level)
1884{
1885 if (level == AI_LEVEL_AWAY) {
1886 /* Cannot set away level for AI */
1887 return FALSE;
1888 }
1889
1890 return TRUE;
1891}
1892
1893/*******************************************************************/
1897{
1898 return AI_LEVEL_COUNT - 1; /* AI_LEVEL_AWAY is not real AI */
1899}
1900
1901/*******************************************************************/
1904void *player_ai_data(const struct player *pplayer, const struct ai_type *ai)
1905{
1906 return pplayer->server.ais[ai_type_number(ai)];
1907}
1908
1909/*******************************************************************/
1912void player_set_ai_data(struct player *pplayer, const struct ai_type *ai,
1913 void *data)
1914{
1915 pplayer->server.ais[ai_type_number(ai)] = data;
1916}
1917
1918/*******************************************************************/
1922int player_multiplier_value(const struct player *pplayer,
1923 const struct multiplier *pmul)
1924{
1925 return pplayer->multipliers[multiplier_index(pmul)].value;
1926}
1927
1928/*******************************************************************/
1933int player_multiplier_effect_value(const struct player *pplayer,
1934 const struct multiplier *pmul)
1935{
1936 return (player_multiplier_value(pplayer, pmul) + pmul->offset)
1937 * pmul->factor;
1938}
1939
1940/*******************************************************************/
1945int player_multiplier_target_value(const struct player *pplayer,
1946 const struct multiplier *pmul)
1947{
1948 return pplayer->multipliers[multiplier_index(pmul)].target;
1949}
1950
1951/*******************************************************************/
1954bool player_has_flag(const struct player *pplayer, enum plr_flag_id flag)
1955{
1956 return BV_ISSET(pplayer->flags, flag);
1957}
int ai_type_number(const struct ai_type *ai)
Definition ai.c:272
void dbv_free(struct dbv *pdbv)
Definition bitvector.c:95
#define BV_SET_ALL_FROM(vec_to, vec_from)
Definition bitvector.h:120
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
#define BV_SET(bv, bit)
Definition bitvector.h:81
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
#define BV_CLR(bv, bit)
Definition bitvector.h:86
bool city_production_has_flag(const struct city *pcity, enum impr_flag_id flag)
Definition city.c:712
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:132
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1198
int city_total_impr_gold_upkeep(const struct city *pcity)
Definition city.c:1179
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:78
#define city_owner(_pcity_)
Definition city.h:543
#define city_list_iterate_end
Definition city.h:490
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:222
#define city_tile_iterate_end
Definition city.h:230
struct civclient client
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
struct unit struct city * target_city
Definition dialogs_g.h:55
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:691
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
struct extra_type_list * extra_type_list_of_unit_hiders(void)
Definition extras.c:251
#define extra_type_list_iterate(extralist, pextra)
Definition extras.h:159
#define extra_type_list_iterate_end
Definition extras.h:161
const struct functions * fc_funcs
static bool is_server(void)
#define CASUS_BELLI_OUTRAGE
Definition fc_types.h:454
#define MAX_NUM_PLAYER_SLOTS
Definition fc_types.h:32
#define CASUS_BELLI_VICTIM
Definition fc_types.h:448
#define MAX_LEN_NAME
Definition fc_types.h:66
@ O_SHIELD
Definition fc_types.h:91
@ O_GOLD
Definition fc_types.h:91
#define Q_(String)
Definition fcintl.h:70
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:57
struct world wld
Definition game.c:58
void game_remove_unit(struct world *gworld, struct unit *punit)
Definition game.c:119
void game_remove_city(struct world *gworld, struct city *pcity)
Definition game.c:173
struct city * owner
Definition citydlg.c:219
GType type
Definition repodlgs.c:1312
struct city * idex_lookup_city(struct world *iworld, int id)
Definition idex.c:132
struct unit * idex_lookup_unit(struct world *iworld, int id)
Definition idex.c:145
#define WONDER_NOT_BUILT
#define B_LAST
Definition improvement.h:42
const char * name
Definition inputfile.c:127
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_debug(message,...)
Definition log.h:115
#define log_error(message,...)
Definition log.h:103
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:639
#define fc_calloc(n, esz)
Definition mem.h:38
#define fc_malloc(sz)
Definition mem.h:34
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Definition multipliers.c:80
#define NO_NATION_SELECTED
Definition nation.h:29
bool is_settable_ai_level(enum ai_level level)
Definition player.c:1883
bool are_diplstates_equal(const struct player_diplstate *pds1, const struct player_diplstate *pds2)
Definition player.c:1469
bool player_can_invade_tile(const struct player *pplayer, const struct tile *ptile)
Definition player.c:264
bool player_knows_techs_with_flag(const struct player *pplayer, enum tech_flag_id flag)
Definition player.c:1304
int player_multiplier_effect_value(const struct player *pplayer, const struct multiplier *pmul)
Definition player.c:1933
void * player_ai_data(const struct player *pplayer, const struct ai_type *ai)
Definition player.c:1904
bool player_slot_is_used(const struct player_slot *pslot)
Definition player.c:441
bv_diplrel_all_reqs diplrel_req_contradicts(const struct requirement *req)
Definition player.c:1787
static bv_diplrel_all_reqs * diplrel_mess_get(void)
Definition player.c:1759
int num_known_tech_with_flag(const struct player *pplayer, enum tech_flag_id flag)
Definition player.c:1249
bool is_valid_username(const char *name)
Definition player.c:1872
static void player_diplstate_new(const struct player *plr1, const struct player *plr2)
Definition player.c:278
int player_multiplier_target_value(const struct player *pplayer, const struct multiplier *pmul)
Definition player.c:1945
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1205
static bv_diplrel_all_reqs * diplrel_mess
Definition player.c:1754
struct player * player_by_name_prefix(const char *name, enum m_pre_result *result)
Definition player.c:911
int diplrel_by_rule_name(const char *value)
Definition player.c:1545
struct player * player_new(struct player_slot *pslot)
Definition player.c:486
struct player * player_by_number(const int player_id)
Definition player.c:840
bool player_in_city_map(const struct player *pplayer, const struct tile *ptile)
Definition player.c:1225
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1452
static bv_diplrel_all_reqs * diplrel_mess_gen(void)
Definition player.c:1654
int used_slots
Definition player.c:51
bool can_player_see_unit_at(const struct player *pplayer, const struct unit *punit, const struct tile *ptile, bool is_transported)
Definition player.c:1025
int player_count(void)
Definition player.c:808
enum diplstate_type cancel_pact_result(enum diplstate_type oldstate)
Definition player.c:67
int player_slot_count(void)
Definition player.c:411
struct player_slot * player_slot_by_number(int player_id)
Definition player.c:456
int player_multiplier_value(const struct player *pplayer, const struct multiplier *pmul)
Definition player.c:1922
int player_get_expected_income(const struct player *pplayer)
Definition player.c:1262
int player_number(const struct player *pplayer)
Definition player.c:828
enum dipl_reason pplayer_can_make_treaty(const struct player *p1, const struct player *p2, enum diplstate_type treaty)
Definition player.c:153
const char * player_name(const struct player *pplayer)
Definition player.c:886
static void player_diplstate_defaults(const struct player *plr1, const struct player *plr2)
Definition player.c:298
void player_slots_free(void)
Definition player.c:379
bool is_diplrel_to_other(const struct player *pplayer, int diplrel)
Definition player.c:1526
int player_slot_max_used_number(void)
Definition player.c:469
static bool is_valid_alliance(const struct player *p1, const struct player *p2)
Definition player.c:126
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1084
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1364
void player_set_ai_data(struct player *pplayer, const struct ai_type *ai, void *data)
Definition player.c:1912
static void player_diplstate_destroy(const struct player *plr1, const struct player *plr2)
Definition player.c:334
void player_ruleset_close(struct player *pplayer)
Definition player.c:736
void player_clear(struct player *pplayer, bool full)
Definition player.c:660
int player_slot_index(const struct player_slot *pslot)
Definition player.c:419
struct player * player_by_name(const char *name)
Definition player.c:872
void player_slots_init(void)
Definition player.c:353
bool player_has_flag(const struct player *pplayer, enum plr_flag_id flag)
Definition player.c:1954
bool player_has_real_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:234
bool team_has_embassy(const struct team *pteam, const struct player *tgt_player)
Definition player.c:214
void player_set_color(struct player *pplayer, const struct rgbcolor *prgbcolor)
Definition player.c:639
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1179
struct city * player_primary_capital(const struct player *pplayer)
Definition player.c:1313
int player_index(const struct player *pplayer)
Definition player.c:820
int player_in_territory(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1842
struct player * player_by_user(const char *name)
Definition player.c:931
const char * diplrel_name_translation(int value)
Definition player.c:1591
bool is_diplrel_between(const struct player *player1, const struct player *player2, int diplrel)
Definition player.c:1480
static struct @68 player_slots
bool player_set_nation(struct player *pplayer, struct nation_type *pnation)
Definition player.c:852
#define DIPLREL_MESS_SIZE
Definition player.c:1644
bool player_can_see_city_externals(const struct player *pow_player, const struct city *target_city)
Definition player.c:1143
int player_age(const struct player *pplayer)
Definition player.c:945
enum dipl_reason pplayer_can_cancel_treaty(const struct player *p1, const struct player *p2)
Definition player.c:92
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:317
int number_of_ai_levels(void)
Definition player.c:1896
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
static void player_defaults(struct player *pplayer)
Definition player.c:544
bool can_player_see_hypotetic_units_at(const struct player *pplayer, const struct tile *ptile)
Definition player.c:984
struct player_slot * slots
Definition player.c:50
bool player_has_embassy_from_effect(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:244
void diplrel_mess_close(void)
Definition player.c:1772
bool players_non_invade(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1418
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1435
bool can_player_see_units_in_city(const struct player *pplayer, const struct city *pcity)
Definition player.c:1113
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:430
static const char * player_name_by_number(int i)
Definition player.c:900
bool can_player_see_city_internals(const struct player *pplayer, const struct city *pcity)
Definition player.c:1129
struct player_slot * player_slot_next(struct player_slot *pslot)
Definition player.c:400
bool player_can_trust_tile_has_no_units(const struct player *pplayer, const struct tile *ptile)
Definition player.c:958
bool player_slots_initialised(void)
Definition player.c:371
const char * love_text(const int love)
Definition player.c:1330
bool player_has_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:202
enum casus_belli_range casus_belli_range_for(const struct player *offender, const struct unit_type *off_ut, const struct player *tgt_plr, const enum effect_type outcome, const struct action *paction, const struct tile *tgt_tile)
Definition player.c:1604
const char * diplrel_rule_name(int value)
Definition player.c:1579
bool gives_shared_vision(const struct player *me, const struct player *them)
Definition player.c:1461
bool player_owns_city(const struct player *pplayer, const struct city *pcity)
Definition player.c:255
struct player_slot * player_slot_first(void)
Definition player.c:392
void player_destroy(struct player *pplayer)
Definition player.c:747
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1402
#define players_iterate_end
Definition player.h:535
dipl_reason
Definition player.h:194
@ DIPL_SENATE_BLOCKING
Definition player.h:195
@ DIPL_ALLIANCE_PROBLEM_THEM
Definition player.h:196
@ DIPL_ALLIANCE_PROBLEM_US
Definition player.h:196
@ DIPL_OK
Definition player.h:195
@ DIPL_ERROR
Definition player.h:195
#define players_iterate(_pplayer)
Definition player.h:530
#define player_list_iterate(playerlist, pplayer)
Definition player.h:553
#define MAX_AI_LOVE
Definition player.h:559
#define ANON_USER_NAME
Definition player.h:48
static bool is_barbarian(const struct player *pplayer)
Definition player.h:488
#define player_slots_iterate(_pslot)
Definition player.h:521
#define player_list_iterate_end
Definition player.h:555
#define PLAYER_DEFAULT_SCIENCE_RATE
Definition player.h:40
#define set_as_human(plr)
Definition player.h:235
#define players_iterate_alive_end
Definition player.h:545
#define player_slots_iterate_end
Definition player.h:525
#define ANON_PLAYER_NAME
Definition player.h:43
#define PLAYER_DEFAULT_TAX_RATE
Definition player.h:39
#define PLAYER_DEFAULT_LUXURY_RATE
Definition player.h:41
#define players_iterate_alive(_pplayer)
Definition player.h:540
#define requirement_diplrel_ereq(_id_, _range_, _present_)
struct research * research_get(const struct player *pplayer)
Definition research.c:126
void rgbcolor_destroy(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:74
struct rgbcolor * rgbcolor_copy(const struct rgbcolor *prgbcolor)
Definition rgbcolor.c:51
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
enum m_pre_result match_prefix(m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result)
Definition shared.c:1581
bool is_ascii_name(const char *name)
Definition shared.c:282
m_pre_result
Definition shared.h:207
@ M_PRE_AMBIGUOUS
Definition shared.h:210
void spaceship_init(struct player_spaceship *ship)
Definition spaceship.c:96
Definition ai.h:50
Definition city.h:309
enum capital_type capital
Definition city.h:317
struct tile * tile
Definition city.h:311
struct packet_game_info info
Definition game.h:89
int bits
Definition bitvector.h:33
unsigned char * vec
Definition bitvector.h:34
bool(* player_tile_vision_get)(const struct tile *ptile, const struct player *pplayer, enum vision_layer vision)
void(* destroy_city)(struct city *pcity)
struct player * player
Definition nation.h:117
enum gold_upkeep_style gold_upkeep_style
enum ai_level skill_level
Definition player.h:122
struct ai_trait * traits
Definition player.h:132
enum barbarian_type barbarian_type
Definition player.h:128
int love[MAX_NUM_PLAYER_SLOTS]
Definition player.h:130
int expand
Definition player.h:124
int fuzzy
Definition player.h:123
char contact_turns_left
Definition player.h:206
int auto_cancel_turn
Definition player.h:208
int first_contact_turn
Definition player.h:203
enum diplstate_type max_state
Definition player.h:202
enum diplstate_type type
Definition player.h:201
char has_reason_to_cancel
Definition player.h:205
int infra_points
Definition player.h:74
struct player * player
Definition player.c:46
struct city_list * cities
Definition player.h:281
bool random_name
Definition player.h:295
struct player_ai ai_common
Definition player.h:288
bv_plr_flags flags
Definition player.h:292
struct dbv tile_vision[V_COUNT]
Definition player.h:359
int primary_capital_id
Definition player.h:275
bool is_male
Definition player.h:257
int wonders[B_LAST]
Definition player.h:301
const struct player_diplstate ** diplstates
Definition player.h:278
bool unassigned_ranked
Definition player.h:255
struct government * target_government
Definition player.h:259
char username[MAX_LEN_NAME]
Definition player.h:252
bool is_winner
Definition player.h:269
bool is_connected
Definition player.h:296
int revolution_finishes
Definition player.h:273
struct player::@69::@72 client
void * ais[FREECIV_AI_MOD_LAST]
Definition player.h:336
int nturns_idle
Definition player.h:265
struct government * government
Definition player.h:258
struct dbv tile_known
Definition player.h:306
struct connection * current_conn
Definition player.h:297
struct team * team
Definition player.h:261
char * savegame_ai_type_name
Definition player.h:290
int turns_alive
Definition player.h:266
bool was_created
Definition player.h:294
const struct ai_type * ai
Definition player.h:289
struct unit_list * units
Definition player.h:282
char ranked_username[MAX_LEN_NAME]
Definition player.h:254
struct conn_list * connections
Definition player.h:298
bool is_alive
Definition player.h:268
bv_player real_embassy
Definition player.h:277
struct player_economic economic
Definition player.h:284
struct player_spaceship spaceship
Definition player.h:286
struct attribute_block_s attribute_block
Definition player.h:303
char name[MAX_LEN_NAME]
Definition player.h:251
bv_player gives_shared_vision
Definition player.h:299
struct attribute_block_s attribute_block_buffer
Definition player.h:304
struct multiplier_value multipliers[MAX_NUM_MULTIPLIERS]
Definition player.h:310
struct nation_type * nation
Definition player.h:260
int music_style
Definition player.h:280
struct nation_style * style
Definition player.h:279
bool phase_done
Definition player.h:263
struct player_slot * slot
Definition player.h:250
struct player::@69::@71 server
bool is_ready
Definition player.h:262
int user_turns
Definition player.h:256
int last_war_action
Definition player.h:270
struct rgbcolor * rgb
Definition player.h:308
bool unassigned_user
Definition player.h:253
enum req_range range
struct universal source
int num_known_tech_with_flag[TF_COUNT]
Definition research.h:90
Definition team.c:40
Definition tile.h:49
struct unit_list * units
Definition tile.h:57
struct player * extras_owner
Definition tile.h:62
struct unit_class::@85 cache
struct extra_type_list * hiding_extras
Definition unittype.h:159
Definition unit.h:138
struct unit_list * transporting
Definition unit.h:184
struct unit::@80::@82 client
int transported_by
Definition unit.h:213
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
struct civ_map map
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
bool fc_isdigit(char c)
Definition support.c:1227
int fc_strncasequotecmp(const char *str0, const char *str1, size_t n)
Definition support.c:379
size_t effectivestrlenquote(const char *str)
Definition support.c:359
#define sz_strlcpy(dest, src)
Definition support.h:167
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define fc__fallthrough
Definition support.h:109
const struct player_list * team_members(const struct team *pteam)
Definition team.c:456
void team_remove_player(struct player *pplayer)
Definition team.c:502
bool tile_is_seen(const struct tile *target_tile, const struct player *pow_player)
Definition tile.c:401
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:386
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
@ TILE_KNOWN_SEEN
Definition tile.h:37
#define tile_has_extra(ptile, pextra)
Definition tile.h:146
#define tile_owner(_tile)
Definition tile.h:95
#define trade_partners_iterate_end
#define trade_partners_iterate(c, p)
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:395
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1271
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2374
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2409
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2435
#define unit_tile(_pu)
Definition unit.h:395
#define unit_owner(_pu)
Definition unit.h:394
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2547
#define vision_layer_iterate(v)
Definition vision.h:77
#define vision_layer_iterate_end
Definition vision.h:80