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>
21 #include "bitvector.h"
31 #include "diptreaty.h"
32 #include "government.h"
34 #include "multipliers.h"
43 /* common/scriptcore */
44 #include "luascript_types.h"
48 #include "barbarian.h"
49 #include "citytools.h"
51 #include "connecthand.h"
60 #include "stdinhand.h"
61 #include "spaceship.h"
62 #include "spacerace.h"
63 #include "techtools.h"
64 #include "unittools.h"
69 #include "autosettlers.h"
71 /* server/scripting */
72 #include "script_server.h"
76 #include "difficulty.h"
77 #include "handicaps.h"
82 static void package_player_common(struct player
*plr
,
83 struct packet_player_info
*packet
);
85 static void package_player_diplstate(struct player
*plr1
,
87 struct packet_player_diplstate
*packet_ds
,
88 struct player
*receiver
,
89 enum plr_info_level min_info_level
);
90 static void package_player_info(struct player
*plr
,
91 struct packet_player_info
*packet
,
92 struct player
*receiver
,
93 enum plr_info_level min_info_level
);
94 static enum plr_info_level
player_info_level(struct player
*plr
,
95 struct player
*receiver
);
97 static void send_player_remove_info_c(const struct player_slot
*pslot
,
98 struct conn_list
*dest
);
99 static void send_player_info_c_real(struct player
*src
,
100 struct conn_list
*dest
);
101 static void send_player_diplstate_c_real(struct player
*src
,
102 struct conn_list
*dest
);
104 static void send_nation_availability_real(struct conn_list
*dest
,
105 bool nationset_change
);
107 /* Used by shuffle_players() and shuffled_player(). */
108 static int shuffled_order
[MAX_NUM_PLAYER_SLOTS
];
110 /* Used by player_info_freeze() and player_info_thaw(). */
111 static int player_info_frozen_level
= 0;
113 /**************************************************************************
114 Murder a player in cold blood.
116 Called from srv_main kill_dying_players() and edit packet handler
117 handle_edit_player_remove().
118 **************************************************************************/
119 void kill_player(struct player
*pplayer
)
122 struct player
*barbarians
= NULL
;
124 pplayer
->is_alive
= FALSE
;
126 /* reset player status */
127 player_status_reset(pplayer
);
129 /* Remove shared vision from dead player to friends. */
130 players_iterate(aplayer
) {
131 if (gives_shared_vision(pplayer
, aplayer
)) {
132 remove_shared_vision(pplayer
, aplayer
);
134 } players_iterate_end
;
136 cancel_all_meetings(pplayer
);
138 /* Show entire map for players who are *not* in a team if revealmap is set
139 * to REVEAL_MAP_DEAD. */
140 if (game
.server
.revealmap
& REVEAL_MAP_DEAD
141 && player_list_size(team_members(pplayer
->team
)) == 1) {
142 map_know_and_see_all(pplayer
);
145 if (!is_barbarian(pplayer
)) {
146 notify_player(NULL
, NULL
, E_DESTROYED
, ftc_server
,
147 _("The %s are no more!"),
148 nation_plural_for_player(pplayer
));
151 /* Transfer back all cities not originally owned by player to their
152 rightful owners, if they are still around */
153 save_palace
= game
.server
.savepalace
;
154 game
.server
.savepalace
= FALSE
; /* moving it around is dumb */
155 city_list_iterate_safe(pplayer
->cities
, pcity
) {
156 if (pcity
->original
!= pplayer
&& pcity
->original
->is_alive
) {
157 /* Transfer city to original owner, kill all its units outside of
158 a radius of 3, give verbose messages of every unit transferred,
159 and raze buildings according to raze chance (also removes palace) */
160 if (transfer_city(pcity
->original
, pcity
, 3, TRUE
, TRUE
, TRUE
,
162 script_server_signal_emit("city_transferred", 4,
163 API_TYPE_CITY
, pcity
,
164 API_TYPE_PLAYER
, pplayer
,
165 API_TYPE_PLAYER
, pcity
->original
,
166 API_TYPE_STRING
, "death-back_to_original");
169 } city_list_iterate_safe_end
;
170 game
.server
.savepalace
= save_palace
;
172 /* let there be civil war */
173 if (game
.info
.gameloss_style
& GAMELOSS_STYLE_CWAR
) {
174 if (city_list_size(pplayer
->cities
) >= 2 + MIN(GAME_MIN_CIVILWARSIZE
, 2)) {
175 log_verbose("Civil war strikes the remaining empire of %s",
177 /* out of sheer cruelty we reanimate the player
178 * so he can behold what happens to his empire */
179 pplayer
->is_alive
= TRUE
;
180 (void) civil_war(pplayer
);
182 log_verbose("The empire of %s is too small for civil war.",
186 pplayer
->is_alive
= FALSE
;
188 if (game
.info
.gameloss_style
& GAMELOSS_STYLE_BARB
) {
189 /* if parameter, create a barbarian, if possible */
190 barbarians
= create_barbarian_player(LAND_BARBARIAN
);
193 /* if there are barbarians around, they will take the remaining cities */
196 /* Moving victim's palace around is a waste of time, as they're dead */
197 bool palace
= game
.server
.savepalace
;
198 game
.server
.savepalace
= FALSE
;
200 log_verbose("Barbarians take the empire of %s", pplayer
->name
);
201 adv_data_phase_init(barbarians
, TRUE
);
203 /* Transfer any remaining cities */
204 city_list_iterate_safe(pplayer
->cities
, pcity
) {
205 if (transfer_city(barbarians
, pcity
, -1, FALSE
, FALSE
, FALSE
,
207 script_server_signal_emit("city_transferred", 4,
208 API_TYPE_CITY
, pcity
,
209 API_TYPE_PLAYER
, pplayer
,
210 API_TYPE_PLAYER
, barbarians
,
211 API_TYPE_STRING
, "death-barbarians_get");
213 } city_list_iterate_safe_end
;
215 game
.server
.savepalace
= palace
;
217 resolve_unit_stacks(pplayer
, barbarians
, FALSE
);
219 /* Barbarians don't get free buildings like Palaces, so we don't
220 * call city_build_free_buildings().
221 * FIXME: maybe this should be a ruleset option? */
223 /* Destroy any remaining cities */
224 city_list_iterate(pplayer
->cities
, pcity
) {
226 } city_list_iterate_end
;
229 /* Remove all units that are still ours */
230 unit_list_iterate_safe(pplayer
->units
, punit
) {
231 wipe_unit(punit
, ULR_PLAYER_DIED
, NULL
);
232 } unit_list_iterate_safe_end
;
234 /* Remove ownership of tiles */
235 whole_map_iterate(ptile
) {
236 if (tile_owner(ptile
) == pplayer
) {
237 map_claim_ownership(ptile
, NULL
, NULL
, FALSE
);
239 if (extra_owner(ptile
) == pplayer
) {
240 ptile
->extras_owner
= NULL
;
242 } whole_map_iterate_end
;
244 /* Ensure this dead player doesn't win with a spaceship.
245 * Now that would be truly unbelievably dumb - Per */
246 spaceship_init(&pplayer
->spaceship
);
247 send_spaceship_info(pplayer
, NULL
);
249 send_player_info_c(pplayer
, game
.est_connections
);
252 /**************************************************************************
253 Return player maxrate in legal range.
254 **************************************************************************/
255 static int get_player_maxrate(struct player
*pplayer
)
257 int maxrate
= get_player_bonus(pplayer
, EFT_MAX_RATES
);
260 return 100; /* effects not initialized yet */
263 /* 34 + 33 + 33 = 100 */
264 return CLIP(34, maxrate
, 100);
267 /**************************************************************************
268 Handle a client or AI request to change the tax/luxury/science rates.
269 This function does full sanity checking.
270 **************************************************************************/
271 void handle_player_rates(struct player
*pplayer
,
272 int tax
, int luxury
, int science
)
276 if (S_S_RUNNING
!= server_state()) {
277 log_error("received player_rates packet from %s before start",
278 player_name(pplayer
));
279 notify_player(pplayer
, NULL
, E_BAD_COMMAND
, ftc_server
,
280 _("Cannot change rates before game start."));
284 if (tax
+ luxury
+ science
!= 100) {
287 if (tax
< 0 || tax
> 100 || luxury
< 0 || luxury
> 100 || science
< 0
291 maxrate
= get_player_maxrate(pplayer
);
292 if (tax
> maxrate
|| luxury
> maxrate
|| science
> maxrate
) {
297 } else if (luxury
> maxrate
) {
300 rtype
= _("Science");
303 notify_player(pplayer
, NULL
, E_BAD_COMMAND
, ftc_server
,
304 _("%s rate exceeds the max rate for %s."),
306 government_name_for_player(pplayer
));
308 pplayer
->economic
.tax
= tax
;
309 pplayer
->economic
.luxury
= luxury
;
310 pplayer
->economic
.science
= science
;
312 city_refresh_for_player(pplayer
);
313 send_player_info_c(pplayer
, pplayer
->connections
);
317 /**************************************************************************
318 Finish the revolution and set the player's government. Call this as soon
319 as the player has set a target_government and the revolution_finishes
321 **************************************************************************/
322 void government_change(struct player
*pplayer
, struct government
*gov
,
323 bool revolution_finished
)
325 struct research
*presearch
;
327 if (revolution_finished
) {
328 fc_assert_ret(pplayer
->target_government
329 != game
.government_during_revolution
330 && NULL
!= pplayer
->target_government
);
331 fc_assert_ret(pplayer
->revolution_finishes
<= game
.info
.turn
);
333 gov
->changed_to_times
++;
336 pplayer
->government
= gov
;
337 pplayer
->target_government
= NULL
;
339 if (revolution_finished
) {
340 log_debug("Revolution finished for %s. Government is %s. "
341 "Revofin %d (%d).", player_name(pplayer
),
342 government_rule_name(gov
),
343 pplayer
->revolution_finishes
, game
.info
.turn
);
346 notify_player(pplayer
, NULL
, E_REVOLT_DONE
, ftc_server
,
347 _("%s now governs the %s as a %s."),
348 player_name(pplayer
),
349 nation_plural_for_player(pplayer
),
350 government_name_translation(gov
));
352 if (!pplayer
->ai_controlled
) {
353 /* Keep luxuries if we have any. Try to max out science. -GJW */
354 int max
= get_player_maxrate(pplayer
);
356 /* only change rates if one exceeds the maximal rate */
357 if (pplayer
->economic
.science
> max
|| pplayer
->economic
.tax
> max
358 || pplayer
->economic
.luxury
> max
) {
359 int save_science
= pplayer
->economic
.science
;
360 int save_tax
= pplayer
->economic
.tax
;
361 int save_luxury
= pplayer
->economic
.luxury
;
363 pplayer
->economic
.science
= MIN(100 - pplayer
->economic
.luxury
, max
);
364 pplayer
->economic
.tax
= MIN(100 - pplayer
->economic
.luxury
365 - pplayer
->economic
.science
, max
);
366 pplayer
->economic
.luxury
= 100 - pplayer
->economic
.science
367 - pplayer
->economic
.tax
;
369 notify_player(pplayer
, NULL
, E_REVOLT_DONE
, ftc_server
,
370 _("The tax rates for the %s are changed from "
371 "%3d%%/%3d%%/%3d%% (tax/luxury/science) to "
372 "%3d%%/%3d%%/%3d%%."),
373 nation_plural_for_player(pplayer
),
374 save_tax
, save_luxury
, save_science
,
375 pplayer
->economic
.tax
, pplayer
->economic
.luxury
,
376 pplayer
->economic
.science
);
380 check_player_max_rates(pplayer
);
381 city_refresh_for_player(pplayer
);
382 send_player_info_c(pplayer
, pplayer
->connections
);
384 presearch
= research_get(pplayer
);
385 research_update(presearch
);
386 send_research_info(presearch
, NULL
);
389 /**************************************************************************
390 Get length of a revolution.
391 **************************************************************************/
392 int revolution_length(struct government
*gov
, struct player
*plr
)
396 turns
= GAME_DEFAULT_REVOLUTION_LENGTH
; /* To avoid compiler warning */
397 switch (game
.info
.revolentype
) {
399 turns
= game
.server
.revolution_length
;
402 turns
= fc_rand(game
.server
.revolution_length
) + 1;
404 case REVOLEN_QUICKENING
:
405 case REVOLEN_RANDQUICK
:
406 if (gov
== game
.government_during_revolution
) {
407 /* Targetless revolution not acceptable */
408 notify_player(plr
, NULL
, E_REVOLT_DONE
, ftc_server
,
409 _("You can't revolt without selecting target government."));
412 turns
= game
.server
.revolution_length
- gov
->changed_to_times
;
413 turns
= MAX(1, turns
);
414 if (game
.info
.revolentype
== REVOLEN_RANDQUICK
) {
415 turns
= fc_rand(turns
) + 1;
423 /**************************************************************************
424 Called by the client or AI to change government.
425 **************************************************************************/
426 void handle_player_change_government(struct player
*pplayer
,
427 Government_type_id government
)
430 struct government
*gov
= government_by_number(government
);
433 if (!gov
|| !can_change_to_government(pplayer
, gov
)) {
437 log_debug("Government changed for %s. Target government is %s; "
438 "old %s. Revofin %d, Turn %d.", player_name(pplayer
),
439 government_rule_name(gov
),
440 government_rule_name(government_of_player(pplayer
)),
441 pplayer
->revolution_finishes
, game
.info
.turn
);
443 anarchy
= get_player_bonus(pplayer
, EFT_NO_ANARCHY
) <= 0;
445 /* Set revolution_finishes value. */
446 if (pplayer
->revolution_finishes
> 0) {
447 /* Player already has an active revolution. Note that the finish time
448 * may be in the future (we're waiting for it to finish), the current
449 * turn (it just finished - but isn't reset until the end of the turn)
450 * or even in the past (if the player is in anarchy and hasn't chosen
452 turns
= pplayer
->revolution_finishes
- game
.info
.turn
;
453 } else if ((pplayer
->ai_controlled
&& !has_handicap(pplayer
, H_REVOLUTION
))
455 /* AI players without the H_REVOLUTION handicap can skip anarchy */
458 turns
= revolution_length(gov
, pplayer
);
464 if (anarchy
&& turns
<= 0
465 && pplayer
->government
!= game
.government_during_revolution
) {
466 /* Multiple changes attempted after single anarchy period */
467 if (game
.info
.revolentype
== REVOLEN_QUICKENING
) {
468 notify_player(pplayer
, NULL
, E_REVOLT_DONE
, ftc_server
,
469 _("You can't revolt the same turn you finished previous revolution."));
474 pplayer
->government
= game
.government_during_revolution
;
475 pplayer
->target_government
= gov
;
476 pplayer
->revolution_finishes
= game
.info
.turn
+ turns
;
478 log_debug("Revolution started for %s. Target government is %s. "
479 "Revofin %d (%d).", player_name(pplayer
),
480 government_rule_name(pplayer
->target_government
),
481 pplayer
->revolution_finishes
, game
.info
.turn
);
483 /* Now see if the revolution is instantaneous. */
485 && pplayer
->target_government
!= game
.government_during_revolution
) {
486 government_change(pplayer
, pplayer
->target_government
, TRUE
);
488 } else if (turns
> 0) {
489 notify_player(pplayer
, NULL
, E_REVOLT_START
, ftc_server
,
490 /* TRANS: this is a message event so don't make it
492 PL_("The %s have incited a revolt! "
493 "%d turn of anarchy will ensue! "
494 "Target government is %s.",
495 "The %s have incited a revolt! "
496 "%d turns of anarchy will ensue! "
497 "Target government is %s.",
499 nation_plural_for_player(pplayer
),
501 government_name_translation(pplayer
->target_government
));
503 fc_assert(pplayer
->target_government
== game
.government_during_revolution
);
504 notify_player(pplayer
, NULL
, E_REVOLT_START
, ftc_server
,
505 _("Revolution: returning to anarchy."));
508 check_player_max_rates(pplayer
);
509 city_refresh_for_player(pplayer
);
510 send_player_info_c(pplayer
, pplayer
->connections
);
512 log_debug("Government change complete for %s. Target government is %s; "
513 "now %s. Turn %d; revofin %d.", player_name(pplayer
),
514 government_rule_name(pplayer
->target_government
),
515 government_rule_name(government_of_player(pplayer
)),
516 game
.info
.turn
, pplayer
->revolution_finishes
);
519 /**************************************************************************
520 See if the player has finished their revolution. This function should
521 be called at the beginning of a player's phase.
522 **************************************************************************/
523 void update_revolution(struct player
*pplayer
)
525 struct government
*current_gov
;
527 /* The player's revolution counter is stored in the revolution_finishes
528 * field. This value has the following meanings:
529 * - If negative (-1), then the player is not in a revolution. In this
530 * case the player should never be in anarchy.
531 * - If positive, the player is in the middle of a revolution. In this
532 * case the value indicates the turn in which the revolution finishes.
533 * * If this value is > than the current turn, then the revolution is
534 * in progress. In this case the player should always be in anarchy.
535 * * If the value is == to the current turn, then the revolution is
536 * finished. The player may now choose a government. However the
537 * value isn't reset until the end of the turn. If the player has
538 * chosen a government by the end of the turn, then the revolution is
539 * over and the value is reset to -1.
540 * * If the player doesn't pick a government then the revolution
541 * continues. At this point the value is <= to the current turn,
542 * and the player can leave the revolution at any time. The value
543 * is reset at the end of any turn when a non-anarchy government is
546 log_debug("Update revolution for %s. Current government %s, "
547 "target %s, revofin %d, turn %d.", player_name(pplayer
),
548 government_rule_name(government_of_player(pplayer
)),
549 pplayer
->target_government
550 ? government_rule_name(pplayer
->target_government
) : "(none)",
551 pplayer
->revolution_finishes
, game
.info
.turn
);
553 current_gov
= government_of_player(pplayer
);
555 if (current_gov
== game
.government_during_revolution
556 && pplayer
->revolution_finishes
<= game
.info
.turn
) {
557 if (pplayer
->target_government
!= game
.government_during_revolution
) {
558 /* If the revolution is over and a target government is set, go into
559 * the new government. */
560 log_debug("Update: finishing revolution for %s.", player_name(pplayer
));
561 government_change(pplayer
, pplayer
->target_government
, TRUE
);
563 /* If the revolution is over but there's no target government set,
564 * alert the player. */
565 notify_player(pplayer
, NULL
, E_REVOLT_DONE
, ftc_any
,
566 _("You should choose a new government from the "
567 "government menu."));
569 } else if (government_of_player(pplayer
) != game
.government_during_revolution
570 && pplayer
->revolution_finishes
< game
.info
.turn
) {
571 /* Reset the revolution counter. If the player has another revolution
572 * they'll have to re-enter anarchy. */
573 log_debug("Update: resetting revofin for %s.", player_name(pplayer
));
574 pplayer
->revolution_finishes
= -1;
575 send_player_info_c(pplayer
, pplayer
->connections
);
579 /**************************************************************************
580 The following checks that government rates are acceptable for the present
581 form of government. Has to be called when switching governments or when
582 toggling from AI to human.
583 **************************************************************************/
584 void check_player_max_rates(struct player
*pplayer
)
586 struct player_economic old_econ
= pplayer
->economic
;
588 pplayer
->economic
= player_limit_to_max_rates(pplayer
);
589 if (old_econ
.tax
> pplayer
->economic
.tax
) {
590 notify_player(pplayer
, NULL
, E_NEW_GOVERNMENT
, ftc_server
,
591 _("Tax rate exceeded the max rate; adjusted."));
593 if (old_econ
.science
> pplayer
->economic
.science
) {
594 notify_player(pplayer
, NULL
, E_NEW_GOVERNMENT
, ftc_server
,
595 _("Science rate exceeded the max rate; adjusted."));
597 if (old_econ
.luxury
> pplayer
->economic
.luxury
) {
598 notify_player(pplayer
, NULL
, E_NEW_GOVERNMENT
, ftc_server
,
599 _("Luxury rate exceeded the max rate; adjusted."));
603 /****************************************************************************
604 After the alliance is breaken, we need to do two things:
605 - Inform clients that they cannot see units inside the former's ally
607 - Remove units stacked together
608 Note that you shouldn't use the units listed in 'pplayer_seen_units'
609 and 'pplayer2_seen_units' after calling this function because these
610 units might have died during the process.
611 ****************************************************************************/
612 void update_players_after_alliance_breakup(struct player
*pplayer
,
613 struct player
*pplayer2
,
614 const struct unit_list
616 const struct unit_list
617 *pplayer2_seen_units
)
619 /* The client needs updated diplomatic state, because it is used
620 * during calculation of new states of occupied flags in cities */
621 send_player_all_c(pplayer
, NULL
);
622 send_player_all_c(pplayer2
, NULL
);
623 remove_allied_visibility(pplayer
, pplayer2
, pplayer_seen_units
);
624 remove_allied_visibility(pplayer2
, pplayer
, pplayer2_seen_units
);
625 resolve_unit_stacks(pplayer
, pplayer2
, TRUE
);
628 /**************************************************************************
629 If there's any units of new_owner on tile, they claim bases.
630 **************************************************************************/
631 static void maybe_claim_base(struct tile
*ptile
, struct player
*new_owner
,
632 struct player
*old_owner
)
636 unit_list_iterate(ptile
->units
, punit
) {
637 if (unit_owner(punit
) == new_owner
638 && tile_has_claimable_base(ptile
, unit_type_get(punit
))) {
642 } unit_list_iterate_end
;
645 extra_type_by_cause_iterate(EC_BASE
, pextra
) {
646 map_claim_base(ptile
, pextra
, new_owner
, old_owner
);
647 } extra_type_by_cause_iterate_end
;
649 ptile
->extras_owner
= new_owner
;
653 /**************************************************************************
654 Two players enter war.
655 **************************************************************************/
656 void enter_war(struct player
*pplayer
, struct player
*pplayer2
)
658 /* Claim bases where units are already standing */
659 whole_map_iterate(ptile
) {
660 struct player
*old_owner
= extra_owner(ptile
);
662 if (old_owner
== pplayer2
) {
663 maybe_claim_base(ptile
, pplayer
, old_owner
);
664 } else if (old_owner
== pplayer
) {
665 maybe_claim_base(ptile
, pplayer2
, old_owner
);
667 } whole_map_iterate_end
;
670 /**************************************************************************
671 Handles a player cancelling a "pact" with another player.
673 packet.id is id of player we want to cancel a pact with
674 packet.val1 is a special value indicating what kind of treaty we want
675 to break. If this is CLAUSE_VISION we break shared vision. If it is
676 a pact treaty type, we break one pact level. If it is CLAUSE_LAST
677 we break _all_ treaties and go straight to war.
678 **************************************************************************/
679 void handle_diplomacy_cancel_pact(struct player
*pplayer
,
681 enum clause_type clause
)
683 enum diplstate_type old_type
;
684 enum diplstate_type new_type
;
685 enum dipl_reason diplcheck
;
687 struct player
*pplayer2
= player_by_number(other_player_id
);
688 struct player_diplstate
*ds_plrplr2
, *ds_plr2plr
;
689 struct unit_list
*pplayer_seen_units
, *pplayer2_seen_units
;
691 if (NULL
== pplayer2
|| players_on_same_team(pplayer
, pplayer2
)) {
695 old_type
= player_diplstate_get(pplayer
, pplayer2
)->type
;
697 if (clause
== CLAUSE_VISION
) {
698 if (!gives_shared_vision(pplayer
, pplayer2
)) {
701 remove_shared_vision(pplayer
, pplayer2
);
702 notify_player(pplayer2
, NULL
, E_TREATY_BROKEN
, ftc_server
,
703 _("%s no longer gives us shared vision!"),
704 player_name(pplayer
));
708 diplcheck
= pplayer_can_cancel_treaty(pplayer
, pplayer2
);
710 /* The senate may not allow you to break the treaty. In this case you
711 * must first dissolve the senate then you can break it. */
712 if (diplcheck
== DIPL_SENATE_BLOCKING
) {
713 notify_player(pplayer
, NULL
, E_TREATY_BROKEN
, ftc_server
,
714 _("The senate will not allow you to break treaty "
715 "with the %s. You must either dissolve the senate "
716 "or wait until a more timely moment."),
717 nation_plural_for_player(pplayer2
));
721 if (diplcheck
!= DIPL_OK
) {
725 reject_all_treaties(pplayer
);
726 reject_all_treaties(pplayer2
);
727 /* else, breaking a treaty */
729 /* check what the new status will be */
730 new_type
= cancel_pact_result(old_type
);
732 ds_plrplr2
= player_diplstate_get(pplayer
, pplayer2
);
733 ds_plr2plr
= player_diplstate_get(pplayer2
, pplayer
);
735 if (old_type
== DS_ALLIANCE
) {
736 pplayer_seen_units
= get_units_seen_via_ally(pplayer
, pplayer2
);
737 pplayer2_seen_units
= get_units_seen_via_ally(pplayer2
, pplayer
);
739 pplayer_seen_units
= NULL
;
740 pplayer2_seen_units
= NULL
;
744 ds_plrplr2
->type
= ds_plr2plr
->type
= new_type
;
745 ds_plrplr2
->turns_left
= ds_plr2plr
->turns_left
= 16;
747 if (new_type
== DS_WAR
) {
748 pplayer
->last_war_action
= game
.info
.turn
;
749 pplayer2
->last_war_action
= game
.info
.turn
;
752 /* If the old state was alliance, the players' units can share tiles
753 illegally, and we need to call resolve_unit_stacks() */
754 if (old_type
== DS_ALLIANCE
) {
756 fc_assert(pplayer_seen_units
!= NULL
);
757 fc_assert(pplayer2_seen_units
!= NULL
);
759 update_players_after_alliance_breakup(pplayer
, pplayer2
,
761 pplayer2_seen_units
);
762 unit_list_destroy(pplayer_seen_units
);
763 unit_list_destroy(pplayer2_seen_units
);
766 /* if there's a reason to cancel the pact, do it without penalty */
767 /* FIXME: in the current implementation if you break more than one
768 * treaty simultaneously it may partially succed: the first treaty-breaking
769 * will happen but the second one will fail. */
770 if (get_player_bonus(pplayer
, EFT_HAS_SENATE
) > 0 && !repeat
) {
771 if (ds_plrplr2
->has_reason_to_cancel
> 0) {
772 notify_player(pplayer
, NULL
, E_TREATY_BROKEN
, ftc_server
,
773 _("The senate passes your bill because of the "
774 "constant provocations of the %s."),
775 nation_plural_for_player(pplayer2
));
776 } else if (new_type
== DS_WAR
) {
777 notify_player(pplayer
, NULL
, E_TREATY_BROKEN
, ftc_server
,
778 _("The senate refuses to break treaty with the %s, "
779 "but you have no trouble finding a new senate."),
780 nation_plural_for_player(pplayer2
));
783 if (new_type
== DS_WAR
) {
784 call_incident(INCIDENT_WAR
, pplayer
, pplayer2
);
786 enter_war(pplayer
, pplayer2
);
788 ds_plrplr2
->has_reason_to_cancel
= 0;
790 send_player_all_c(pplayer
, NULL
);
791 send_player_all_c(pplayer2
, NULL
);
794 * Refresh all cities which have a unit of the other side within
797 city_map_update_all_cities_for_player(pplayer
);
798 city_map_update_all_cities_for_player(pplayer2
);
801 notify_player(pplayer
, NULL
, E_TREATY_BROKEN
, ftc_server
,
802 _("The diplomatic state between the %s "
803 "and the %s is now %s."),
804 nation_plural_for_player(pplayer
),
805 nation_plural_for_player(pplayer2
),
806 diplstate_type_translated_name(new_type
));
807 notify_player(pplayer2
, NULL
, E_TREATY_BROKEN
, ftc_server
,
808 _(" %s canceled the diplomatic agreement! "
809 "The diplomatic state between the %s and the %s "
811 player_name(pplayer
),
812 nation_plural_for_player(pplayer2
),
813 nation_plural_for_player(pplayer
),
814 diplstate_type_translated_name(new_type
));
816 /* Check fall-out of a war declaration. */
817 players_iterate_alive(other
) {
818 if (other
!= pplayer
&& other
!= pplayer2
819 && new_type
== DS_WAR
&& pplayers_allied(pplayer2
, other
)
820 && pplayers_allied(pplayer
, other
)) {
821 if (!players_on_same_team(pplayer
, other
)) {
822 /* If an ally declares war on another ally, break off your alliance
823 * to the aggressor. This prevents in-alliance wars, which are not
825 notify_player(other
, NULL
, E_TREATY_BROKEN
, ftc_server
,
826 _("%s has attacked your ally %s! "
827 "You cancel your alliance to the aggressor."),
828 player_name(pplayer
),
829 player_name(pplayer2
));
830 player_diplstate_get(other
, pplayer
)->has_reason_to_cancel
= 1;
831 handle_diplomacy_cancel_pact(other
, player_number(pplayer
),
834 /* We are in the same team as the agressor; we cannot break
835 * alliance with him. We trust our team mate and break alliance
836 * with the attacked player */
837 notify_player(other
, NULL
, E_TREATY_BROKEN
, ftc_server
,
838 _("Your team mate %s declared war on %s. "
839 "You are obligated to cancel alliance with %s."),
840 player_name(pplayer
),
841 nation_plural_for_player(pplayer2
),
842 player_name(pplayer2
));
843 handle_diplomacy_cancel_pact(other
, player_number(pplayer2
), CLAUSE_ALLIANCE
);
846 } players_iterate_alive_end
;
849 /**************************************************************************
850 Send information about removed (unused) players.
851 **************************************************************************/
852 static void send_player_remove_info_c(const struct player_slot
*pslot
,
853 struct conn_list
*dest
)
856 dest
= game
.est_connections
;
859 fc_assert_ret(!player_slot_is_used(pslot
));
861 conn_list_iterate(dest
, pconn
) {
862 dsend_packet_player_remove(pconn
, player_slot_index(pslot
));
863 } conn_list_iterate_end
;
866 /****************************************************************************
867 Do not compute and send PACKET_PLAYER_INFO or PACKET_NATION_AVAILABILITY
868 until a call to player_info_thaw(). This is used during savegame load
869 or ruleset (re)load cycles, to avoid sending infos to the client that
870 depend on ruleset data it does not yet have.
871 ****************************************************************************/
872 void player_info_freeze(void)
874 player_info_frozen_level
++;
877 /****************************************************************************
878 If the frozen level is back to 0, send all players' infos, and nation
879 availability, to all connections.
880 ****************************************************************************/
881 void player_info_thaw(void)
883 if (0 == --player_info_frozen_level
) {
884 send_nation_availability_real(game
.est_connections
, FALSE
);
885 send_player_info_c(NULL
, NULL
);
887 fc_assert(0 <= player_info_frozen_level
);
890 /**************************************************************************
891 Send all information about a player (player_info and all
892 player_diplstates) to the given connections.
894 Send all players if src is NULL; send to all connections if dest is NULL.
896 This function also sends the diplstate of the player. So take care, that
897 all players are defined in the client and in the server. To create a
898 player without sending the diplstate, use send_player_info_c().
899 **************************************************************************/
900 void send_player_all_c(struct player
*src
, struct conn_list
*dest
)
902 send_player_info_c(src
, dest
);
903 send_player_diplstate_c(src
, dest
);
906 /**************************************************************************
907 Send information about player slot 'src', or all valid (i.e. used and
908 initialized) players if 'src' is NULL, to specified clients 'dest'.
909 If 'dest' is NULL, it is treated as game.est_connections.
911 Note: package_player_info contains incomplete info if it has NULL as a
912 dest arg and and info is < INFO_EMBASSY.
913 NB: If 'src' is NULL (meaning send information about all players) this
914 function will only send info for used players, i.e. player slots with
916 **************************************************************************/
917 void send_player_info_c(struct player
*src
, struct conn_list
*dest
)
919 if (0 < player_info_frozen_level
) {
920 return; /* Discard, see comment for player_info_freeze(). */
924 send_player_info_c_real(src
, dest
);
928 players_iterate(pplayer
) {
929 send_player_info_c_real(pplayer
, dest
);
930 } players_iterate_end
;
933 /**************************************************************************
934 Really send information. If 'dest' is NULL, then it is set to
935 game.est_connections.
936 **************************************************************************/
937 static void send_player_info_c_real(struct player
*src
,
938 struct conn_list
*dest
)
940 struct packet_player_info info
;
942 fc_assert_ret(src
!= NULL
);
945 dest
= game
.est_connections
;
948 package_player_common(src
, &info
);
950 conn_list_iterate(dest
, pconn
) {
951 if (NULL
== pconn
->playing
&& pconn
->observer
) {
952 /* Global observer. */
953 package_player_info(src
, &info
, pconn
->playing
, INFO_FULL
);
954 } else if (NULL
!= pconn
->playing
) {
955 /* Players (including regular observers) */
956 package_player_info(src
, &info
, pconn
->playing
, INFO_MINIMUM
);
958 package_player_info(src
, &info
, NULL
, INFO_MINIMUM
);
960 send_packet_player_info(pconn
, &info
);
961 } conn_list_iterate_end
;
964 /**************************************************************************
965 Identical to send_player_info_c(), but sends the diplstate of the
968 This function solves one problem of using an extra packet for the
969 diplstate. It can only be send if the player exists at the destination.
970 Thus, this function should be called after the player(s) exists on both
971 sides of the connection.
972 **************************************************************************/
973 void send_player_diplstate_c(struct player
*src
, struct conn_list
*dest
)
976 send_player_diplstate_c_real(src
, dest
);
980 players_iterate(pplayer
) {
981 send_player_diplstate_c_real(pplayer
, dest
);
982 } players_iterate_end
;
985 /**************************************************************************
986 Really send information. If 'dest' is NULL, then it is set to
987 game.est_connections.
988 **************************************************************************/
989 static void send_player_diplstate_c_real(struct player
*plr1
,
990 struct conn_list
*dest
)
992 fc_assert_ret(plr1
!= NULL
);
995 dest
= game
.est_connections
;
998 conn_list_iterate(dest
, pconn
) {
999 players_iterate(plr2
) {
1000 struct packet_player_diplstate packet_ds
;
1002 if (NULL
== pconn
->playing
&& pconn
->observer
) {
1003 /* Global observer. */
1004 package_player_diplstate(plr1
, plr2
, &packet_ds
, pconn
->playing
,
1006 } else if (NULL
!= pconn
->playing
) {
1007 /* Players (including regular observers) */
1008 package_player_diplstate(plr1
, plr2
, &packet_ds
, pconn
->playing
,
1011 package_player_diplstate(plr1
, plr2
, &packet_ds
, NULL
,
1014 send_packet_player_diplstate(pconn
, &packet_ds
);
1015 } players_iterate_end
;
1016 } conn_list_iterate_end
;
1019 /**************************************************************************
1020 Package player information that is always sent.
1021 **************************************************************************/
1022 static void package_player_common(struct player
*plr
,
1023 struct packet_player_info
*packet
)
1026 struct music_style
*music
;
1028 packet
->playerno
= player_number(plr
);
1029 sz_strlcpy(packet
->name
, player_name(plr
));
1030 sz_strlcpy(packet
->username
, plr
->username
);
1031 packet
->unassigned_user
= plr
->unassigned_user
;
1032 packet
->nation
= plr
->nation
? nation_number(plr
->nation
) : NATION_NONE
;
1033 packet
->is_male
= plr
->is_male
;
1034 packet
->team
= plr
->team
? team_number(plr
->team
) : team_count();
1035 packet
->is_ready
= plr
->is_ready
;
1036 packet
->was_created
= plr
->was_created
;
1037 packet
->style
= plr
->style
? style_number(plr
->style
) : 0;
1039 /* I think we could safely move the music style selection to
1040 * client side to not have it burden server side. Client could
1041 * actually avoid it completely when music disabled from the client options.
1042 * Client has no use for music styles of other players, and there should
1043 * be no such information about the player him/herself needed to determine
1044 * the music style that client does not know. */
1045 music
= player_music_style(plr
);
1046 if (music
!= NULL
) {
1047 packet
->music_style
= music_style_number(music
);
1049 packet
->music_style
= -1; /* No music style available */
1052 packet
->is_alive
=plr
->is_alive
;
1053 packet
->turns_alive
=plr
->turns_alive
;
1054 packet
->is_connected
=plr
->is_connected
;
1055 packet
->ai
= plr
->ai_controlled
;
1056 packet
->ai_skill_level
= plr
->ai_controlled
1057 ? plr
->ai_common
.skill_level
: 0;
1058 for (i
= 0; i
< player_slot_count(); i
++) {
1059 packet
->love
[i
] = plr
->ai_common
.love
[i
];
1061 packet
->barbarian_type
= plr
->ai_common
.barbarian_type
;
1063 packet
->phase_done
= plr
->phase_done
;
1064 packet
->nturns_idle
=plr
->nturns_idle
;
1066 for (i
= 0; i
< B_LAST
/*improvement_count()*/; i
++) {
1067 packet
->wonders
[i
] = plr
->wonders
[i
];
1069 packet
->science_cost
= plr
->ai_common
.science_cost
;
1072 /**************************************************************************
1073 Package player info depending on info_level. We send everything to
1074 plr's connections, we send almost everything to players with embassy
1075 to plr, we send a little to players we are in contact with and almost
1076 nothing to everyone else.
1078 Receiver may be NULL in which cases dummy values are sent for some
1080 **************************************************************************/
1081 static void package_player_info(struct player
*plr
,
1082 struct packet_player_info
*packet
,
1083 struct player
*receiver
,
1084 enum plr_info_level min_info_level
)
1086 enum plr_info_level info_level
;
1087 enum plr_info_level highest_team_level
;
1088 struct government
*pgov
= NULL
;
1091 info_level
= player_info_level(plr
, receiver
);
1092 info_level
= MAX(min_info_level
, info_level
);
1094 info_level
= min_info_level
;
1098 packet
->multip_count
= multiplier_count();
1099 if (info_level
>= INFO_FULL
) {
1100 multipliers_iterate(pmul
) {
1101 packet
->multiplier
[multiplier_index(pmul
)] =
1102 plr
->multipliers
[multiplier_index(pmul
)];
1103 packet
->multiplier_target
[multiplier_index(pmul
)] =
1104 plr
->multipliers_target
[multiplier_index(pmul
)];
1105 } multipliers_iterate_end
;
1107 multipliers_iterate(pmul
) {
1108 packet
->multiplier
[multiplier_index(pmul
)] = 0;
1109 packet
->multiplier_target
[multiplier_index(pmul
)] = 0;
1110 } multipliers_iterate_end
;
1113 /* We need to send all tech info for all players on the same
1114 * team, even if they are not in contact yet; otherwise we will
1115 * overwrite team research or confuse the client. */
1116 highest_team_level
= info_level
;
1117 players_iterate(aplayer
) {
1118 if (players_on_same_team(plr
, aplayer
) && receiver
) {
1119 highest_team_level
= MAX(highest_team_level
,
1120 player_info_level(aplayer
, receiver
));
1122 } players_iterate_end
;
1124 if (plr
->rgb
!= NULL
) {
1125 packet
->color_valid
= TRUE
;
1126 packet
->color_red
= plr
->rgb
->r
;
1127 packet
->color_green
= plr
->rgb
->g
;
1128 packet
->color_blue
= plr
->rgb
->b
;
1130 /* In pregame, send the color we expect to use, for consistency with
1131 * '/list colors' etc. */
1132 const struct rgbcolor
*preferred
= player_preferred_color(plr
);
1133 if (preferred
!= NULL
) {
1134 packet
->color_valid
= TRUE
;
1135 packet
->color_red
= preferred
->r
;
1136 packet
->color_green
= preferred
->g
;
1137 packet
->color_blue
= preferred
->b
;
1139 fc_assert(game
.info
.turn
< 1);
1140 packet
->color_valid
= FALSE
;
1141 /* Client shouldn't use these dummy values */
1142 packet
->color_red
= 0;
1143 packet
->color_green
= 0;
1144 packet
->color_blue
= 0;
1147 packet
->color_changeable
= player_color_changeable(plr
, NULL
);
1149 /* Only send score if we have contact */
1150 if (info_level
>= INFO_MEETING
) {
1151 packet
->score
= plr
->score
.game
;
1156 if (info_level
>= INFO_MEETING
) {
1157 packet
->gold
= plr
->economic
.gold
;
1158 pgov
= government_of_player(plr
);
1161 pgov
= game
.government_during_revolution
;
1163 packet
->government
= pgov
? government_number(pgov
) : government_count();
1165 /* Send diplomatic status of the player to everyone they are in
1167 if (info_level
>= INFO_EMBASSY
1169 && player_diplstate_get(receiver
, plr
)->contact_turns_left
> 0)) {
1170 packet
->target_government
= plr
->target_government
1171 ? government_number(plr
->target_government
)
1172 : government_count();
1173 memset(&packet
->real_embassy
, 0, sizeof(packet
->real_embassy
));
1174 players_iterate(pother
) {
1175 packet
->real_embassy
[player_index(pother
)] =
1176 player_has_real_embassy(plr
, pother
);
1177 } players_iterate_end
;
1178 packet
->gives_shared_vision
= plr
->gives_shared_vision
;
1180 packet
->target_government
= packet
->government
;
1181 memset(&packet
->real_embassy
, 0, sizeof(packet
->real_embassy
));
1182 if (receiver
&& player_has_real_embassy(plr
, receiver
)) {
1183 packet
->real_embassy
[player_index(receiver
)] = TRUE
;
1186 BV_CLR_ALL(packet
->gives_shared_vision
);
1187 if (receiver
&& gives_shared_vision(plr
, receiver
)) {
1188 BV_SET(packet
->gives_shared_vision
, player_index(receiver
));
1192 /* Make absolutely sure - in case you lose your embassy! */
1193 if (info_level
>= INFO_EMBASSY
1195 && player_diplstate_get(plr
, receiver
)->type
== DS_TEAM
)) {
1196 packet
->tech_upkeep
= player_tech_upkeep(plr
);
1198 packet
->tech_upkeep
= 0;
1201 /* Send most civ info about the player only to players who have an
1203 if (highest_team_level
>= INFO_EMBASSY
) {
1204 packet
->tax
= plr
->economic
.tax
;
1205 packet
->science
= plr
->economic
.science
;
1206 packet
->luxury
= plr
->economic
.luxury
;
1207 packet
->revolution_finishes
= plr
->revolution_finishes
;
1210 packet
->science
= 0;
1212 packet
->revolution_finishes
= -1;
1215 if (info_level
>= INFO_FULL
1217 && player_diplstate_get(plr
, receiver
)->type
== DS_TEAM
)) {
1218 packet
->mood
= player_mood(plr
);
1220 packet
->mood
= MOOD_COUNT
;
1223 if (info_level
>= INFO_FULL
) {
1224 packet
->culture
= plr
->culture
;
1226 packet
->culture
= 0;
1230 /**************************************************************************
1231 Package player diplstate depending on info_level. We send everything to
1232 plr's connections, we send almost everything to players with embassy
1233 to plr, we send a little to players we are in contact with and almost
1234 nothing to everyone else.
1236 Receiver may be NULL in which cases dummy values are sent for some
1238 **************************************************************************/
1239 static void package_player_diplstate(struct player
*plr1
,
1240 struct player
*plr2
,
1241 struct packet_player_diplstate
*packet_ds
,
1242 struct player
*receiver
,
1243 enum plr_info_level min_info_level
)
1245 enum plr_info_level info_level
;
1246 struct player_diplstate
*ds
= player_diplstate_get(plr1
, plr2
);
1249 info_level
= player_info_level(plr1
, receiver
);
1250 info_level
= MAX(min_info_level
, info_level
);
1252 info_level
= min_info_level
;
1255 packet_ds
->plr1
= player_index(plr1
);
1256 packet_ds
->plr2
= player_index(plr2
);
1257 /* A unique id for each combination is calculated here. */
1258 packet_ds
->diplstate_id
= packet_ds
->plr1
* MAX_NUM_PLAYER_SLOTS
1261 /* Send diplomatic status of the player to everyone they are in
1262 * contact with (embassy, remaining contact turns, the receiver). */
1263 if (info_level
>= INFO_EMBASSY
1265 && player_diplstate_get(receiver
, plr1
)->contact_turns_left
> 0)
1266 || (receiver
&& receiver
== plr2
)) {
1267 packet_ds
->type
= ds
->type
;
1268 packet_ds
->turns_left
= ds
->turns_left
;
1269 packet_ds
->has_reason_to_cancel
= ds
->has_reason_to_cancel
;
1270 packet_ds
->contact_turns_left
= ds
->contact_turns_left
;
1272 packet_ds
->type
= DS_WAR
;
1273 packet_ds
->turns_left
= 0;
1274 packet_ds
->has_reason_to_cancel
= 0;
1275 packet_ds
->contact_turns_left
= 0;
1279 /**************************************************************************
1280 Return level of information player should receive about another.
1281 **************************************************************************/
1282 static enum plr_info_level
player_info_level(struct player
*plr
,
1283 struct player
*receiver
)
1285 if (S_S_RUNNING
> server_state()) {
1286 return INFO_MINIMUM
;
1288 if (plr
== receiver
) {
1291 if (receiver
&& player_has_embassy(receiver
, plr
)) {
1292 return INFO_EMBASSY
;
1294 if (receiver
&& could_intel_with_player(receiver
, plr
)) {
1295 return INFO_MEETING
;
1297 return INFO_MINIMUM
;
1300 /**************************************************************************
1301 Convenience function to return "reply" destination connection list
1302 for player: pplayer->current_conn if set, else pplayer->connections.
1303 **************************************************************************/
1304 struct conn_list
*player_reply_dest(struct player
*pplayer
)
1306 return (pplayer
->current_conn
?
1307 pplayer
->current_conn
->self
:
1308 pplayer
->connections
);
1311 /**************************************************************************
1312 Call first_contact function if such is defined for player
1313 **************************************************************************/
1314 static void call_first_contact(struct player
*pplayer
, struct player
*aplayer
)
1316 CALL_PLR_AI_FUNC(first_contact
, pplayer
, pplayer
, aplayer
);
1319 /****************************************************************************
1320 Initialize ANY newly-created player on the server.
1322 The initmap option is used because we don't want to initialize the map
1323 before the x and y sizes have been determined. This should generally
1324 be FALSE in pregame.
1326 The needs_team options should be set for players who should be assigned
1327 a team. They will be put on their own newly-created team.
1328 ****************************************************************************/
1329 void server_player_init(struct player
*pplayer
, bool initmap
,
1332 player_status_reset(pplayer
);
1334 pplayer
->server
.got_first_city
= FALSE
;
1335 BV_CLR_ALL(pplayer
->server
.really_gives_vision
);
1336 BV_CLR_ALL(pplayer
->server
.debug
);
1338 player_map_free(pplayer
);
1339 pplayer
->server
.private_map
= NULL
;
1342 player_map_init(pplayer
);
1345 team_add_player(pplayer
, NULL
);
1348 /* This must be done after team information is initialised
1349 * as it might be needed to determine max rate effects.
1350 * Sometimes this server_player_init() gets called twice
1351 * with only latter one having needs_team set. We don't
1352 * want to call player_limit_to_max_rates() at first time
1353 * when team is not yet set. It's callers responsibility
1354 * to always have one server_player_init() call with
1355 * needs_team TRUE. */
1357 pplayer
->economic
= player_limit_to_max_rates(pplayer
);
1360 adv_data_default(pplayer
);
1362 /* We don't push this in calc_civ_score(), or it will be reset
1364 pplayer
->score
.units_built
= 0;
1365 pplayer
->score
.units_killed
= 0;
1366 pplayer
->score
.units_lost
= 0;
1368 /* No delegation. */
1369 pplayer
->server
.delegate_to
[0] = '\0';
1370 pplayer
->server
.orig_username
[0] = '\0';
1372 handicaps_init(pplayer
);
1375 /****************************************************************************
1376 If a player's color will be predictable when colors are assigned (or
1377 assignment has already happened), return that color. Otherwise (if the
1378 player's color is yet to be assigned randomly), return NULL.
1379 ****************************************************************************/
1380 const struct rgbcolor
*player_preferred_color(struct player
*pplayer
)
1383 return pplayer
->rgb
;
1384 } else if (playercolor_count() == 0) {
1385 /* If a ruleset isn't loaded, there are no colors to choose from. */
1387 } else if (game
.server
.plrcolormode
== PLRCOL_NATION_ORDER
) {
1388 if (pplayer
->nation
!= NO_NATION_SELECTED
) {
1389 return nation_color(nation_of_player(pplayer
)); /* may be NULL */
1391 return NULL
; /* don't know nation, hence don't know color */
1394 /* Modes indexing into game-defined player colors */
1396 switch (game
.server
.plrcolormode
) {
1397 case PLRCOL_PLR_SET
: /* player color (set) */
1398 case PLRCOL_PLR_RANDOM
: /* player color (random) */
1399 /* These depend on other players and will be assigned at game start. */
1402 log_error("Invalid value for 'game.server.plrcolormode' (%d)!",
1403 game
.server
.plrcolormode
);
1404 /* no break - using 'PLRCOL_PLR_ORDER' as fallback */
1405 case PLRCOL_PLR_ORDER
: /* player color (ordered) */
1406 colorid
= player_number(pplayer
) % playercolor_count();
1408 case PLRCOL_TEAM_ORDER
: /* team color (ordered) */
1409 colorid
= team_number(pplayer
->team
) % playercolor_count();
1412 return playercolor_get(colorid
);
1416 /****************************************************************************
1417 Return whether a player's color can currently be set with the
1418 '/playercolor' command. If not, give a reason why not, if 'reason' is
1419 not NULL (need not be freed).
1420 ****************************************************************************/
1421 bool player_color_changeable(const struct player
*pplayer
, const char **reason
)
1423 if (!game_was_started() && game
.server
.plrcolormode
!= PLRCOL_PLR_SET
) {
1425 *reason
= _("Can only set player color prior to game start if "
1426 "'plrcolormode' is PLR_SET.");
1433 /****************************************************************************
1434 Permanently assign colors to any players that don't already have them.
1435 First assign preferred colors, then assign the rest randomly, trying to
1437 ****************************************************************************/
1438 void assign_player_colors(void)
1440 struct rgbcolor_list
*spare_colors
=
1441 rgbcolor_list_copy(game
.server
.plr_colors
);
1442 int needed
= player_count();
1444 players_iterate(pplayer
) {
1445 const struct rgbcolor
*autocolor
;
1446 /* Assign the deterministic colors. */
1448 && (autocolor
= player_preferred_color(pplayer
))) {
1449 player_set_color(pplayer
, autocolor
);
1452 /* One fewer random color needed. */
1454 /* Try to avoid clashes between explicit and random colors. */
1455 rgbcolor_list_iterate(spare_colors
, prgbcolor
) {
1456 if (rgbcolors_are_equal(pplayer
->rgb
, prgbcolor
)) {
1457 rgbcolor_list_remove(spare_colors
, prgbcolor
);
1459 } rgbcolor_list_iterate_end
;
1461 } players_iterate_end
;
1464 /* No random colors needed */
1465 rgbcolor_list_destroy(spare_colors
);
1469 if (game
.server
.plrcolormode
== PLRCOL_NATION_ORDER
) {
1470 /* Additionally, try to avoid color clashes with certain nations not
1471 * yet in play (barbarians). */
1472 allowed_nations_iterate(pnation
) {
1473 const struct rgbcolor
*ncol
= nation_color(pnation
);
1474 if (ncol
&& nation_barbarian_type(pnation
) != NOT_A_BARBARIAN
) {
1475 /* Don't use this color. */
1476 rgbcolor_list_iterate(spare_colors
, prgbcolor
) {
1477 if (rgbcolors_are_equal(ncol
, prgbcolor
)) {
1478 rgbcolor_list_remove(spare_colors
, ncol
);
1480 } rgbcolor_list_iterate_end
;
1482 } allowed_nations_iterate_end
;
1485 fc_assert(game
.server
.plrcolormode
== PLRCOL_PLR_RANDOM
1486 || game
.server
.plrcolormode
== PLRCOL_PLR_SET
1487 || game
.server
.plrcolormode
== PLRCOL_NATION_ORDER
);
1489 if (needed
> rgbcolor_list_size(spare_colors
)) {
1490 log_verbose("Not enough unique colors for all players; there will be "
1492 /* Fallback: start again from full set of ruleset colors.
1493 * No longer attempt to avoid clashes with explicitly assigned colors. */
1494 rgbcolor_list_destroy(spare_colors
);
1495 spare_colors
= rgbcolor_list_copy(game
.server
.plr_colors
);
1497 /* We may still not have enough, if there are more players than
1498 * ruleset-defined colors. If so, top up with duplicates. */
1499 if (needed
> rgbcolor_list_size(spare_colors
)) {
1500 int i
, origsize
= rgbcolor_list_size(spare_colors
);
1501 /* Shuffle so that duplicates aren't biased to start of list */
1502 rgbcolor_list_shuffle(spare_colors
);
1503 /* Duplication process avoids one color being hit lots of times */
1504 for (i
= origsize
; i
< needed
; i
++) {
1505 rgbcolor_list_append(spare_colors
,
1506 rgbcolor_list_get(spare_colors
, i
- origsize
));
1509 /* Shuffle (including mixing any duplicates up) */
1510 rgbcolor_list_shuffle(spare_colors
);
1512 /* Finally, assign shuffled colors to players. */
1513 players_iterate(pplayer
) {
1514 if (!pplayer
->rgb
) {
1515 player_set_color(pplayer
, rgbcolor_list_front(spare_colors
));
1516 rgbcolor_list_pop_front(spare_colors
);
1518 } players_iterate_end
;
1520 rgbcolor_list_destroy(spare_colors
);
1523 /****************************************************************************
1524 Set the player's color. If 'prgbcolor' is not NULL the caller should free
1525 the pointer, as player_set_color() copies the data.
1526 ****************************************************************************/
1527 void server_player_set_color(struct player
*pplayer
,
1528 const struct rgbcolor
*prgbcolor
)
1530 if (prgbcolor
!= NULL
) {
1531 player_set_color(pplayer
, prgbcolor
);
1533 /* This can legitimately be NULL in pregame. */
1534 fc_assert_ret(!game_was_started());
1535 rgbcolor_destroy(pplayer
->rgb
);
1536 pplayer
->rgb
= NULL
;
1538 /* Update clients */
1539 send_player_info_c(pplayer
, NULL
);
1542 /****************************************************************************
1543 Return the player color as featured text string.
1544 (In pregame, this uses the color the player will take, if known, even if
1546 ****************************************************************************/
1547 const char *player_color_ftstr(struct player
*pplayer
)
1549 static char buf
[64];
1551 const struct rgbcolor
*prgbcolor
;
1553 fc_assert_ret_val(pplayer
!= NULL
, NULL
);
1556 prgbcolor
= player_preferred_color(pplayer
);
1557 if (prgbcolor
!= NULL
1558 && rgbcolor_to_hex(prgbcolor
, hex
, sizeof(hex
))) {
1559 struct ft_color plrcolor
= FT_COLOR("#000000", hex
);
1561 featured_text_apply_tag(hex
, buf
, sizeof(buf
), TTT_COLOR
, 0,
1562 FT_OFFSET_UNSET
, plrcolor
);
1564 cat_snprintf(buf
, sizeof(buf
), _("no color"));
1570 /**********************************************************************
1571 Gives units that every player should have. Usually called for
1572 players created midgame.
1573 ***********************************************************************/
1574 void give_midgame_initial_units(struct player
*pplayer
, struct tile
*ptile
)
1576 int sucount
= strlen(game
.server
.start_units
);
1579 for (i
= 0; i
< sucount
; i
++) {
1580 if (game
.server
.start_units
[i
] == 'k') {
1581 /* Every player should have king */
1582 struct unit_type
*utype
= crole_to_unit_type('k', pplayer
);
1584 if (utype
!= NULL
) {
1585 create_unit(pplayer
, ptile
, utype
, 0, 0, -1);
1591 /**********************************************************************
1592 Creates a new, uninitialized, used player slot. You should probably
1593 call server_player_init() to initialize it, and send_player_info_c()
1594 later to tell clients about it.
1596 May return NULL if creation was not possible.
1597 ***********************************************************************/
1598 struct player
*server_create_player(int player_id
, const char *ai_tname
,
1599 struct rgbcolor
*prgbcolor
,
1600 bool allow_ai_type_fallbacking
)
1602 struct player_slot
*pslot
;
1603 struct player
*pplayer
;
1605 pslot
= player_slot_by_number(player_id
);
1606 fc_assert(NULL
== pslot
|| !player_slot_is_used(pslot
));
1608 pplayer
= player_new(pslot
);
1609 if (NULL
== pplayer
) {
1613 if (allow_ai_type_fallbacking
) {
1614 pplayer
->savegame_ai_type_name
= fc_strdup(ai_tname
);
1615 ai_tname
= ai_type_name_or_fallback(ai_tname
);
1618 pplayer
->ai
= ai_type_by_name(ai_tname
);
1620 if (pplayer
->ai
== NULL
) {
1621 player_destroy(pplayer
);
1625 adv_data_init(pplayer
);
1627 CALL_FUNC_EACH_AI(player_alloc
, pplayer
);
1629 /* TODO: Do we really need this server_player_init() here? All our callers
1630 * will later make another server_player_init() call anyway, with boolean
1631 * parameters set to what they really need. */
1632 server_player_init(pplayer
, FALSE
, FALSE
);
1635 player_set_color(pplayer
, prgbcolor
);
1636 } /* else caller must ensure a color is assigned if game has started */
1641 /**********************************************************************
1642 This function does _not_ close any connections attached to this
1643 player. The function cut_connection() is used for that. Be sure
1644 to send_player_slot_info_c() afterwards to tell clients that the
1645 player slot has become unused.
1646 ***********************************************************************/
1647 void server_remove_player(struct player
*pplayer
)
1649 const struct player_slot
*pslot
;
1651 fc_assert_ret(NULL
!= pplayer
);
1653 /* save player slot */
1654 pslot
= pplayer
->slot
;
1656 log_normal(_("Removing player %s."), player_name(pplayer
));
1658 notify_conn(pplayer
->connections
, NULL
, E_CONNECTION
, ftc_server
,
1659 _("You've been removed from the game!"));
1661 notify_conn(game
.est_connections
, NULL
, E_CONNECTION
, ftc_server
,
1662 _("%s has been removed from the game."),
1663 player_name(pplayer
));
1665 if (is_barbarian(pplayer
)) {
1666 server
.nbarbarians
--;
1669 /* Don't use conn_list_iterate here because connection_detach() can be
1670 * recursive and free the next connection pointer. */
1671 while (conn_list_size(pplayer
->connections
) > 0) {
1672 connection_detach(conn_list_get(pplayer
->connections
, 0), FALSE
);
1675 script_server_remove_exported_object(pplayer
);
1676 /* Clear data saved in the other player structs. */
1677 players_iterate(aplayer
) {
1678 BV_CLR(aplayer
->real_embassy
, player_index(pplayer
));
1679 if (gives_shared_vision(aplayer
, pplayer
)) {
1680 remove_shared_vision(aplayer
, pplayer
);
1682 } players_iterate_end
;
1684 /* Remove citizens of this player from the cities of all other players. */
1685 /* FIXME: add a special case if the server quits - no need to run this for
1686 * each player in that case. */
1687 if (game
.info
.citizen_nationality
) {
1688 cities_iterate(pcity
) {
1689 if (city_owner(pcity
) != pplayer
) {
1690 citizens nationality
= citizens_nation_get(pcity
, pplayer
->slot
);
1691 if (nationality
!= 0) {
1692 /* Change nationality of the citizens to the nationality of the
1694 citizens_nation_move(pcity
, pplayer
->slot
, city_owner(pcity
)->slot
,
1696 city_refresh_queue_add(pcity
);
1699 } cities_iterate_end
1701 city_refresh_queue_processing();
1704 /* AI type lost control of this player */
1705 CALL_PLR_AI_FUNC(lost_control
, pplayer
, pplayer
);
1707 /* We have to clear all player data before the ai memory is freed because
1708 * some function may depend on it. */
1709 player_clear(pplayer
, TRUE
);
1711 if (!map_is_empty()) {
1712 remove_player_from_maps(pplayer
);
1714 player_map_free(pplayer
);
1716 /* Destroy advisor and ai data. */
1717 CALL_FUNC_EACH_AI(player_free
, pplayer
);
1719 handicaps_close(pplayer
);
1720 ai_traits_close(pplayer
);
1721 adv_data_close(pplayer
);
1722 player_destroy(pplayer
);
1724 send_updated_vote_totals(NULL
);
1725 /* must be called after the player was destroyed */
1726 send_player_remove_info_c(pslot
, NULL
);
1728 /* Recalculate borders. */
1729 map_calculate_borders();
1732 /**************************************************************************
1733 The following limits a player's rates to those that are acceptable for the
1734 present form of government. If a rate exceeds maxrate for this government,
1735 it adjusts rates automatically adding the extra to the 2nd highest rate,
1736 preferring science to taxes and taxes to luxuries.
1737 (It assumes that for any government maxrate>=50)
1739 Returns actual max rate used. This function should be called after team
1740 information are defined.
1741 **************************************************************************/
1742 struct player_economic
player_limit_to_max_rates(struct player
*pplayer
)
1744 int maxrate
, surplus
;
1745 struct player_economic economic
;
1747 /* ai players allowed to cheat */
1748 if (pplayer
->ai_controlled
) {
1749 return pplayer
->economic
;
1752 economic
= pplayer
->economic
;
1754 maxrate
= get_player_maxrate(pplayer
);
1757 if (economic
.luxury
> maxrate
) {
1758 surplus
+= economic
.luxury
- maxrate
;
1759 economic
.luxury
= maxrate
;
1761 if (economic
.tax
> maxrate
) {
1762 surplus
+= economic
.tax
- maxrate
;
1763 economic
.tax
= maxrate
;
1765 if (economic
.science
> maxrate
) {
1766 surplus
+= economic
.science
- maxrate
;
1767 economic
.science
= maxrate
;
1770 fc_assert(surplus
% 10 == 0);
1771 while (surplus
> 0) {
1772 if (economic
.science
< maxrate
) {
1773 economic
.science
+= 10;
1774 } else if (economic
.tax
< maxrate
) {
1776 } else if (economic
.luxury
< maxrate
) {
1777 economic
.luxury
+= 10;
1779 fc_assert_msg(FALSE
, "Failed to distribute the surplus. "
1780 "maxrate = %d.", maxrate
);
1788 /****************************************************************************
1789 Check if this name is allowed for the player. Fill out the error message
1790 (a translated string to be sent to the client) if not.
1791 ****************************************************************************/
1792 static bool server_player_name_is_allowed(const struct connection
*caller
,
1793 const struct player
*pplayer
,
1794 const struct nation_type
*pnation
,
1795 const char *name
, char *error_buf
,
1796 size_t error_buf_len
)
1798 /* An empty name is surely not allowed. */
1799 if (0 == strlen(name
)) {
1800 fc_strlcpy(error_buf
, _("Please choose a non-blank name."),
1805 /* Any name already taken is not allowed. */
1806 players_iterate(other_player
) {
1807 if (other_player
== pplayer
) {
1808 /* We don't care if we're the one using the name/nation. */
1810 } else if (NULL
!= pnation
&& other_player
->nation
== pnation
) {
1811 /* FIXME: currently cannot use nation_of_player(other_player) as the
1812 * nation debug code is buggy and doesn't test nation for NULL. */
1813 fc_strlcpy(error_buf
, _("That nation is already in use."),
1816 } else if (0 == fc_strcasecmp(player_name(other_player
), name
)) {
1817 fc_snprintf(error_buf
, error_buf_len
,
1818 _("Another player already has the name '%s'. Please "
1819 "choose another name."), name
);
1822 } players_iterate_end
;
1824 if (NULL
== pnation
) {
1825 /* FIXME: currently cannot use nation_of_player(other_player) as the
1826 * nation debug code is buggy and doesn't test nation for NULL. */
1827 pnation
= pplayer
->nation
;
1830 /* Any name from the default list is always allowed. */
1831 if (NULL
!= pnation
&& NULL
!= nation_leader_by_name(pnation
, name
)) {
1835 /* To prevent abuse, only players with HACK access (usually local
1836 * connections) can use non-ascii names. Otherwise players could use
1837 * confusing garbage names in multi-player games. */
1839 && caller
->access_level
< ALLOW_HACK
1840 && !is_ascii_name(name
)) {
1841 fc_strlcpy(error_buf
,
1842 _("Please choose a name containing only ASCII characters."),
1850 /****************************************************************************
1851 Try to set the player name to 'name'. Else, find a default name. Returns
1853 ****************************************************************************/
1854 bool server_player_set_name_full(const struct connection
*caller
,
1855 struct player
*pplayer
,
1856 const struct nation_type
*pnation
,
1858 char *error_buf
, size_t error_buf_len
)
1860 char real_name
[MAX_LEN_NAME
];
1864 /* Always provide an error buffer. */
1865 if (NULL
== error_buf
) {
1867 error_buf_len
= sizeof(buf
);
1869 error_buf
[0] = '\0';
1872 /* Ensure this is a correct name. */
1873 sz_strlcpy(real_name
, name
);
1874 remove_leading_trailing_spaces(real_name
);
1875 real_name
[0] = fc_toupper(real_name
[0]);
1877 if (server_player_name_is_allowed(caller
, pplayer
, pnation
, real_name
,
1878 error_buf
, error_buf_len
)) {
1879 log_debug("Name of player nb %d set to \"%s\".",
1880 player_number(pplayer
), real_name
);
1881 fc_strlcpy(pplayer
->name
, real_name
, sizeof(pplayer
->name
));
1882 return TRUE
; /* Success! */
1884 log_verbose("Failed to set the name of the player nb %d to \"%s\": %s",
1885 player_number(pplayer
), real_name
, error_buf
);
1890 if (NULL
!= caller
) {
1891 /* If we want to test, let's fail here. */
1892 fc_assert(NULL
!= name
);
1897 /* Try to append a number to 'real_name'. */
1898 char test
[MAX_LEN_NAME
];
1900 for (i
= 2; i
<= player_slot_count(); i
++) {
1901 fc_snprintf(test
, sizeof(test
), "%s%d", real_name
, i
);
1902 if (server_player_name_is_allowed(caller
, pplayer
, pnation
,
1903 test
, error_buf
, error_buf_len
)) {
1904 log_verbose("Name of player nb %d set to \"%s\" instead.",
1905 player_number(pplayer
), test
);
1906 fc_strlcpy(pplayer
->name
, test
, sizeof(pplayer
->name
));
1909 log_debug("Failed to set the name of the player nb %d to \"%s\": %s",
1910 player_number(pplayer
), test
, error_buf
);
1915 /* Try a default name. */
1916 fc_snprintf(real_name
, sizeof(real_name
),
1917 _("Player no. %d"), player_number(pplayer
));
1918 if (server_player_name_is_allowed(caller
, pplayer
, pnation
,
1919 real_name
, error_buf
, error_buf_len
)) {
1920 log_verbose("Name of player nb %d set to \"%s\".",
1921 player_number(pplayer
), real_name
);
1922 fc_strlcpy(pplayer
->name
, real_name
, sizeof(pplayer
->name
));
1925 log_debug("Failed to set the name of the player nb %d to \"%s\": %s",
1926 player_number(pplayer
), real_name
, error_buf
);
1929 /* Try a very default name... */
1930 for (i
= 0; i
< player_slot_count(); i
++) {
1931 fc_snprintf(real_name
, sizeof(real_name
), _("Player no. %d"), i
);
1932 if (server_player_name_is_allowed(caller
, pplayer
, pnation
,
1933 real_name
, error_buf
, error_buf_len
)) {
1934 log_verbose("Name of player nb %d to \"%s\".",
1935 player_number(pplayer
), real_name
);
1936 fc_strlcpy(pplayer
->name
, real_name
, sizeof(pplayer
->name
));
1939 log_debug("Failed to set the name of the player nb %d to \"%s\": %s",
1940 player_number(pplayer
), real_name
, error_buf
);
1944 /* This is really not normal... Maybe the size of 'real_name'
1945 * is not enough big, or a bug in server_player_name_is_allowed(). */
1946 fc_strlcpy(pplayer
->name
, _("A poorly-named player"),
1947 sizeof(pplayer
->name
));
1948 return FALSE
; /* Let's say it's a failure. */
1951 /****************************************************************************
1952 Try to set the player name to 'name'. Else, find a default name.
1953 ****************************************************************************/
1954 void server_player_set_name(struct player
*pplayer
, const char *name
)
1958 ret
= server_player_set_name_full(NULL
, pplayer
, NULL
, name
, NULL
, 0);
1959 fc_assert(TRUE
== ret
);
1962 /**************************************************************************
1963 Returns the default diplomatic state between 2 players.
1965 Mainly, this returns DS_WAR, but it can also return DS_PEACE if both
1966 players are allied with the same third player.
1967 **************************************************************************/
1968 static enum diplstate_type
1969 get_default_diplstate(const struct player
*pplayer1
,
1970 const struct player
*pplayer2
)
1972 players_iterate_alive(pplayer3
) {
1973 if (pplayer3
!= pplayer1
1974 && pplayer3
!= pplayer2
1975 && pplayers_allied(pplayer3
, pplayer1
)
1976 && pplayers_allied(pplayer3
, pplayer2
)) {
1979 } players_iterate_alive_end
;
1984 /**************************************************************************
1985 Update contact info.
1986 **************************************************************************/
1987 void make_contact(struct player
*pplayer1
, struct player
*pplayer2
,
1990 struct player_diplstate
*ds_plr1plr2
, *ds_plr2plr1
;
1992 if (pplayer1
== pplayer2
1993 || !pplayer1
->is_alive
1994 || !pplayer2
->is_alive
) {
1998 ds_plr1plr2
= player_diplstate_get(pplayer1
, pplayer2
);
1999 ds_plr2plr1
= player_diplstate_get(pplayer2
, pplayer1
);
2001 if (get_player_bonus(pplayer1
, EFT_NO_DIPLOMACY
) <= 0
2002 && get_player_bonus(pplayer2
, EFT_NO_DIPLOMACY
) <= 0) {
2003 ds_plr1plr2
->contact_turns_left
= game
.server
.contactturns
;
2004 ds_plr2plr1
->contact_turns_left
= game
.server
.contactturns
;
2006 if (ds_plr1plr2
->type
== DS_NO_CONTACT
) {
2007 enum diplstate_type new_state
= get_default_diplstate(pplayer1
,
2010 ds_plr1plr2
->type
= new_state
;
2011 ds_plr2plr1
->type
= new_state
;
2012 ds_plr1plr2
->first_contact_turn
= game
.info
.turn
;
2013 ds_plr2plr1
->first_contact_turn
= game
.info
.turn
;
2014 notify_player(pplayer1
, ptile
, E_FIRST_CONTACT
, ftc_server
,
2015 _("You have made contact with the %s, ruled by %s."),
2016 nation_plural_for_player(pplayer2
),
2017 player_name(pplayer2
));
2018 notify_player(pplayer2
, ptile
, E_FIRST_CONTACT
, ftc_server
,
2019 _("You have made contact with the %s, ruled by %s."),
2020 nation_plural_for_player(pplayer1
),
2021 player_name(pplayer1
));
2022 send_player_all_c(pplayer1
, pplayer2
->connections
);
2023 send_player_all_c(pplayer2
, pplayer1
->connections
);
2024 send_player_all_c(pplayer1
, pplayer1
->connections
);
2025 send_player_all_c(pplayer2
, pplayer2
->connections
);
2026 if (pplayer1
->ai_controlled
) {
2027 call_first_contact(pplayer1
, pplayer2
);
2029 if (pplayer2
->ai_controlled
) {
2030 call_first_contact(pplayer2
, pplayer1
);
2034 fc_assert(ds_plr2plr1
->type
!= DS_NO_CONTACT
);
2036 if (player_has_embassy(pplayer1
, pplayer2
)
2037 || player_has_embassy(pplayer2
, pplayer1
)) {
2038 return; /* Avoid sending too much info over the network */
2040 send_player_all_c(pplayer1
, pplayer1
->connections
);
2041 send_player_all_c(pplayer2
, pplayer2
->connections
);
2044 /**************************************************************************
2045 Check if we make contact with anyone.
2046 **************************************************************************/
2047 void maybe_make_contact(struct tile
*ptile
, struct player
*pplayer
)
2049 square_iterate(ptile
, 1, tile1
) {
2050 struct city
*pcity
= tile_city(tile1
);
2052 make_contact(pplayer
, city_owner(pcity
), ptile
);
2054 unit_list_iterate_safe(tile1
->units
, punit
) {
2055 make_contact(pplayer
, unit_owner(punit
), ptile
);
2056 } unit_list_iterate_safe_end
;
2057 } square_iterate_end
;
2060 /**************************************************************************
2061 Shuffle or reshuffle the player order, storing in static variables above.
2062 **************************************************************************/
2063 void shuffle_players(void)
2065 /* shuffled_order is defined global */
2066 int n
= player_slot_count();
2069 log_debug("shuffle_players: creating shuffled order");
2071 for (i
= 0; i
< n
; i
++) {
2072 shuffled_order
[i
] = i
;
2076 array_shuffle(shuffled_order
, n
);
2079 for (i
= 0; i
< n
; i
++) {
2080 log_debug("shuffled_order[%d] = %d", i
, shuffled_order
[i
]);
2085 /**************************************************************************
2086 Initialize the shuffled players list (as from a loaded savegame).
2087 **************************************************************************/
2088 void set_shuffled_players(int *shuffled_players
)
2092 log_debug("set_shuffled_players: loading shuffled array %p",
2095 for (i
= 0; i
< player_slot_count(); i
++) {
2096 shuffled_order
[i
] = shuffled_players
[i
];
2097 log_debug("shuffled_order[%d] = %d", i
, shuffled_order
[i
]);
2101 /**************************************************************************
2102 Returns the i'th shuffled player, or NULL.
2104 NB: You should never need to call this function directly.
2105 **************************************************************************/
2106 struct player
*shuffled_player(int i
)
2108 struct player
*pplayer
;
2110 pplayer
= player_by_number(shuffled_order
[i
]);
2111 log_debug("shuffled_player(%d) = %d (%s)",
2112 i
, shuffled_order
[i
], player_name(pplayer
));
2116 /****************************************************************************
2117 This function returns a random-ish nation that is suitable for 'barb_type'
2118 and is usable (not already in use by an existing player, and if
2119 needs_startpos is set, would not be prohibited from starting on the map
2120 by the current scenario -- NB this doesn't check that any start position
2123 Unless 'ignore_conflicts' is set, this function tries hard to avoid a
2124 nation marked as "conflicting with" one already in the game. A
2125 conflicting nation will be returned only if the alternative is to return
2126 NO_NATION_SELECTED. Such a return indicates that there are no remaining
2127 nations which match the above criteria.
2129 If 'choices' is non-NULL, nations from the supplied list are preferred;
2130 but if there are no (non-conflicting) nations on the list that match the
2131 criteria, one will be chosen from outside the list (as if the list had
2134 All other things being equal, prefers to pick a nation which returns a
2135 high score from nations_match() relative to any nations already in the
2137 ****************************************************************************/
2138 struct nation_type
*pick_a_nation(const struct nation_list
*choices
,
2139 bool ignore_conflicts
,
2140 bool needs_startpos
,
2141 enum barbarian_type barb_type
)
2144 UNAVAILABLE
, AVAILABLE
, PREFERRED
, UNWANTED
2145 } nations_used
[nation_count()], looking_for
;
2146 int match
[nation_count()], pick
, idx
;
2147 int num_avail_nations
= 0, num_pref_nations
= 0;
2149 /* Values of nations_used:
2150 * UNAVAILABLE - nation is already used or is a special nation.
2151 * AVAILABLE - we can use this nation.
2152 * PREFERRED - we can use this nation and it is on the choices list.
2153 * UNWANTED - we can use this nation, but we really don't want to. */
2154 nations_iterate(pnation
) {
2155 idx
= nation_index(pnation
);
2157 if (!nation_is_in_current_set(pnation
)
2159 || (needs_startpos
&& game
.scenario
.startpos_nations
2160 && pnation
->server
.no_startpos
)
2161 || (barb_type
!= nation_barbarian_type(pnation
))
2162 || (barb_type
== NOT_A_BARBARIAN
&& !is_nation_playable(pnation
))) {
2163 /* Nation is unplayable or already used: don't consider it.
2164 * (If nations aren't currently restricted to those with start
2165 * positions, we do nothing special here, but generate_players() will
2166 * tend to prefer them.) */
2167 nations_used
[idx
] = UNAVAILABLE
;
2172 nations_used
[idx
] = AVAILABLE
;
2174 /* Determine which nations look good with nations already in the game,
2175 * or conflict with them. */
2177 players_iterate(pplayer
) {
2178 if (pplayer
->nation
!= NO_NATION_SELECTED
) {
2179 int x
= nations_match(pnation
, nation_of_player(pplayer
),
2182 log_debug("Nations '%s' (nb %d) and '%s' (nb %d) are in conflict.",
2183 nation_rule_name(pnation
), nation_number(pnation
),
2184 nation_rule_name(nation_of_player(pplayer
)),
2185 nation_number(nation_of_player(pplayer
)));
2186 nations_used
[idx
] = UNWANTED
;
2187 match
[idx
] -= x
* 100;
2190 match
[idx
] += x
* 100;
2193 } players_iterate_end
;
2195 if (AVAILABLE
== nations_used
[idx
]) {
2196 num_avail_nations
+= match
[idx
];
2198 } nations_iterate_end
;
2200 /* Mark as preferred those nations which are on the choices list and
2201 * which are AVAILABLE, but no UNWANTED */
2202 if (NULL
!= choices
) {
2203 nation_list_iterate(choices
, pnation
) {
2204 idx
= nation_index(pnation
);
2205 if (nations_used
[idx
] == AVAILABLE
) {
2206 num_pref_nations
+= match
[idx
];
2207 nations_used
[idx
] = PREFERRED
;
2209 } nation_list_iterate_end
;
2212 if (0 < num_pref_nations
|| 0 < num_avail_nations
) {
2213 if (0 < num_pref_nations
) {
2214 /* Use a preferred nation only. */
2215 pick
= fc_rand(num_pref_nations
);
2216 looking_for
= PREFERRED
;
2217 log_debug("Picking a preferred nation.");
2219 /* Use any available nation. */
2220 fc_assert(0 < num_avail_nations
);
2221 pick
= fc_rand(num_avail_nations
);
2222 looking_for
= AVAILABLE
;
2223 log_debug("Picking an available nation.");
2226 nations_iterate(pnation
) {
2227 idx
= nation_index(pnation
);
2228 if (nations_used
[idx
] == looking_for
) {
2235 } nations_iterate_end
;
2237 /* No available nation: use unwanted nation... */
2238 struct nation_type
*less_worst_nation
= NO_NATION_SELECTED
;
2239 int less_worst_score
= -FC_INFINITY
;
2241 log_debug("Picking an unwanted nation.");
2242 nations_iterate(pnation
) {
2243 idx
= nation_index(pnation
);
2244 if (UNWANTED
== nations_used
[idx
]) {
2245 pick
= -fc_rand(match
[idx
]);
2246 if (pick
> less_worst_score
) {
2247 less_worst_nation
= pnation
;
2248 less_worst_score
= pick
;
2251 } nations_iterate_end
;
2253 if (NO_NATION_SELECTED
!= less_worst_nation
) {
2254 return less_worst_nation
;
2258 log_verbose("No nation found!");
2260 return NO_NATION_SELECTED
;
2263 /****************************************************************************
2264 Return the nationset currently in effect.
2265 ****************************************************************************/
2266 static struct nation_set
*current_nationset(void)
2268 return nation_set_by_setting_value(game
.server
.nationset
);
2271 /****************************************************************************
2272 Is the nation in the currently selected nationset?
2273 If not, it's not allowed to appear in the game.
2274 ****************************************************************************/
2275 bool nation_is_in_current_set(const struct nation_type
*pnation
)
2277 return nation_is_in_set(pnation
, current_nationset());
2280 /****************************************************************************
2281 Update the server's cached number of playable nations.
2282 Call when the nationset changes.
2283 ****************************************************************************/
2284 void count_playable_nations(void)
2286 server
.playable_nations
= 0;
2287 allowed_nations_iterate(pnation
) {
2288 if (is_nation_playable(pnation
)) {
2289 server
.playable_nations
++;
2291 } allowed_nations_iterate_end
;
2294 /****************************************************************************
2295 Return whether a nation is "pickable" -- whether players can select it
2297 (is_nation_pickable() is the equivalent function on the client.)
2298 ****************************************************************************/
2299 bool client_can_pick_nation(const struct nation_type
*pnation
)
2301 fc_assert_ret_val(pnation
!= NULL
, FALSE
);
2302 return nation_is_in_current_set(pnation
)
2303 && is_nation_playable(pnation
)
2304 && (!game
.scenario
.startpos_nations
2305 || !pnation
->server
.no_startpos
);
2308 /****************************************************************************
2309 Helper doing the actual work for send_nation_availability() (q.v.).
2310 ****************************************************************************/
2311 static void send_nation_availability_real(struct conn_list
*dest
,
2312 bool nationset_change
)
2314 struct packet_nation_availability packet
;
2316 packet
.ncount
= nation_count();
2317 packet
.nationset_change
= nationset_change
;
2318 nations_iterate(pnation
) {
2319 packet
.is_pickable
[nation_index(pnation
)] = client_can_pick_nation(pnation
);
2320 } nations_iterate_end
;
2321 lsend_packet_nation_availability(dest
, &packet
);
2324 /****************************************************************************
2325 Tell clients which nations can be picked given current server settings.
2326 ****************************************************************************/
2327 void send_nation_availability(struct conn_list
*dest
,
2328 bool nationset_change
)
2330 if (0 < player_info_frozen_level
) {
2331 return; /* Discard, see comment for player_info_freeze(). */
2333 send_nation_availability_real(dest
, nationset_change
);
2337 /****************************************************************************
2338 Try to select a nation set that fits the current players' nations, or
2339 failing that, unset the nations of some of the players.
2340 To be called when loading an old savegame that predates nationsets.
2341 ****************************************************************************/
2342 void fit_nationset_to_players(void)
2344 int misfits
[nation_set_count()];
2345 nation_sets_iterate(pset
) {
2346 misfits
[nation_set_index(pset
)] = 0;
2347 players_iterate(pplayer
) {
2348 if (pplayer
->nation
!= NO_NATION_SELECTED
2349 && !nation_is_in_set(pplayer
->nation
, pset
)) {
2350 misfits
[nation_set_index(pset
)]++;
2352 } players_iterate_end
;
2353 } nation_sets_iterate_end
;
2355 if (misfits
[nation_set_index(current_nationset())] == 0) {
2356 /* Current set is OK. */
2360 /* Otherwise, pick the least worst set (requires unsetting fewest
2361 * players, possibly none). */
2363 /* Quell compiler warning; but least_misfits initializer won't be used */
2364 int i
, least_misfits
= -1;
2365 const struct nation_set
*best
= NULL
;
2366 fc_assert(nation_set_count() > 0);
2367 for (i
= 0; i
< nation_set_count(); i
++) {
2368 if (best
== NULL
|| misfits
[i
] < least_misfits
) {
2369 best
= nation_set_by_number(i
);
2370 least_misfits
= misfits
[i
];
2371 if (least_misfits
== 0) {
2372 /* Not going to do any better. */
2377 fc_assert(least_misfits
>= 0);
2379 log_verbose("Current nationset \"%s\" doesn't fit all existing players.",
2380 nation_set_rule_name(current_nationset()));
2381 log_verbose("Selected nationset \"%s\".", nation_set_rule_name(best
));
2382 fc_strlcpy(game
.server
.nationset
, nation_set_rule_name(best
),
2383 sizeof(game
.server
.nationset
));
2384 count_playable_nations();
2385 /* No need to refresh clients, as we're assumed to be in the middle of
2386 * loading a savegame and will send new setting/availability later
2387 * along with everything else */
2390 /* The set we chose may not fit all the players; as a last resort,
2391 * unset nations (caller must then arrange new assignments). */
2392 players_iterate(pplayer
) {
2393 if (pplayer
->nation
!= NO_NATION_SELECTED
2394 && !nation_is_in_current_set(pplayer
->nation
)) {
2395 log_verbose("Nation %s of player %s not in nationset \"%s\", unsetting.",
2396 nation_plural_for_player(pplayer
), player_name(pplayer
),
2397 nation_set_rule_name(current_nationset()));
2398 player_set_nation(pplayer
, NO_NATION_SELECTED
);
2400 } players_iterate_end
;
2403 /****************************************************************************
2404 Called when something is changed; this resets everyone's readiness.
2405 ****************************************************************************/
2406 void reset_all_start_commands(bool plrchange
)
2408 if (S_S_INITIAL
!= server_state()) {
2411 players_iterate(pplayer
) {
2412 if (pplayer
->is_ready
) {
2413 bool persistent
= FALSE
;
2416 switch (game
.info
.persistent_ready
)
2418 case PERSISTENTR_DISABLED
:
2421 case PERSISTENTR_CONNECTED
:
2422 persistent
= pplayer
->is_connected
;
2428 pplayer
->is_ready
= FALSE
;
2429 send_player_info_c(pplayer
, game
.est_connections
);
2432 } players_iterate_end
;
2435 /**********************************************************************
2436 This function creates a new player and copies all of it's science
2437 research etc. Players are both thrown into anarchy and gold is
2438 split between both players.
2440 ***********************************************************************/
2441 static struct player
*split_player(struct player
*pplayer
)
2443 struct research
*new_research
, *old_research
;
2444 struct player
*cplayer
;
2445 struct nation_type
*rebel_nation
;
2447 /* make a new player, or not */
2448 cplayer
= server_create_player(-1, ai_name(pplayer
->ai
),
2453 server_player_init(cplayer
, TRUE
, TRUE
);
2455 /* Rebel will always be an AI player */
2456 rebel_nation
= pick_a_nation(nation_of_player(pplayer
)->server
.civilwar_nations
,
2457 TRUE
, FALSE
, NOT_A_BARBARIAN
);
2458 player_nation_defaults(cplayer
, rebel_nation
, TRUE
);
2460 fc_assert(game_was_started());
2461 /* Find a color for the new player. */
2462 assign_player_colors();
2464 /* Send information about the used player slot to all connections. */
2465 send_player_info_c(cplayer
, NULL
);
2467 sz_strlcpy(cplayer
->username
, _(ANON_USER_NAME
));
2468 cplayer
->unassigned_user
= TRUE
;
2469 cplayer
->is_connected
= FALSE
;
2470 cplayer
->government
= init_government_of_nation(nation_of_player(cplayer
));
2471 fc_assert(cplayer
->revolution_finishes
< 0);
2472 /* No capital for the splitted player. */
2473 cplayer
->server
.got_first_city
= FALSE
;
2475 players_iterate(other_player
) {
2476 struct player_diplstate
*ds_co
2477 = player_diplstate_get(cplayer
, other_player
);
2478 struct player_diplstate
*ds_oc
2479 = player_diplstate_get(other_player
, cplayer
);
2481 if (get_player_bonus(other_player
, EFT_NO_DIPLOMACY
) > 0) {
2482 ds_co
->type
= DS_WAR
;
2483 ds_oc
->type
= DS_WAR
;
2485 ds_co
->type
= DS_NO_CONTACT
;
2486 ds_oc
->type
= DS_NO_CONTACT
;
2489 ds_co
->has_reason_to_cancel
= 0;
2490 ds_co
->turns_left
= 0;
2491 ds_co
->contact_turns_left
= 0;
2492 ds_oc
->has_reason_to_cancel
= 0;
2493 ds_oc
->turns_left
= 0;
2494 ds_oc
->contact_turns_left
= 0;
2496 /* Send so that other_player sees updated diplomatic info;
2497 * pplayer will be sent later anyway
2499 if (other_player
!= pplayer
) {
2500 send_player_all_c(other_player
, other_player
->connections
);
2502 } players_iterate_end
;
2504 /* Split the resources */
2505 cplayer
->economic
.gold
= pplayer
->economic
.gold
;
2506 cplayer
->economic
.gold
/= 2;
2507 pplayer
->economic
.gold
-= cplayer
->economic
.gold
;
2509 /* Copy the research */
2510 new_research
= research_get(cplayer
);
2511 old_research
= research_get(pplayer
);
2513 new_research
->bulbs_researched
= 0;
2514 new_research
->techs_researched
= old_research
->techs_researched
;
2515 new_research
->researching
= old_research
->researching
;
2516 new_research
->tech_goal
= old_research
->tech_goal
;
2518 advance_index_iterate(A_NONE
, i
) {
2519 if (TECH_KNOWN
== research_invention_state(old_research
, i
)) {
2520 research_invention_set(new_research
, i
, TECH_KNOWN
);
2522 } advance_index_iterate_end
;
2523 cplayer
->phase_done
= TRUE
; /* Have other things to think
2524 about - paralysis */
2525 BV_CLR_ALL(cplayer
->real_embassy
); /* all embassies destroyed */
2526 research_update(new_research
);
2529 cplayer
->ai_controlled
= TRUE
;
2530 cplayer
->ai_common
.maxbuycost
= pplayer
->ai_common
.maxbuycost
;
2531 cplayer
->ai_common
.warmth
= pplayer
->ai_common
.warmth
;
2532 cplayer
->ai_common
.frost
= pplayer
->ai_common
.frost
;
2533 set_ai_level_direct(cplayer
, game
.info
.skill_level
);
2535 /* change the original player */
2536 if (government_of_player(pplayer
) != game
.government_during_revolution
) {
2537 pplayer
->target_government
= pplayer
->government
;
2538 pplayer
->government
= game
.government_during_revolution
;
2539 pplayer
->revolution_finishes
= game
.info
.turn
+ 1;
2541 old_research
->bulbs_researched
= 0;
2542 old_research
->researching_saved
= A_UNKNOWN
;
2543 BV_CLR_ALL(pplayer
->real_embassy
); /* all embassies destroyed */
2545 /* give splitted player the embassies to his team mates back, if any */
2546 if (pplayer
->team
) {
2547 players_iterate(pdest
) {
2548 if (pplayer
->team
== pdest
->team
2549 && pplayer
!= pdest
) {
2550 establish_embassy(pplayer
, pdest
);
2552 } players_iterate_end
;
2554 research_update(old_research
);
2556 pplayer
->economic
= player_limit_to_max_rates(pplayer
);
2560 give_map_from_player_to_player(pplayer
, cplayer
);
2562 /* Not sure if this is necessary, but might be a good idea
2563 * to avoid doing some ai calculations with bogus data. */
2564 adv_data_phase_init(cplayer
, TRUE
);
2565 CALL_PLR_AI_FUNC(phase_begin
, cplayer
, cplayer
, TRUE
);
2566 CALL_PLR_AI_FUNC(gained_control
, cplayer
, cplayer
);
2567 CALL_PLR_AI_FUNC(split_by_civil_war
, pplayer
, pplayer
, cplayer
);
2568 CALL_PLR_AI_FUNC(created_by_civil_war
, cplayer
, pplayer
, cplayer
);
2573 /**************************************************************************
2574 Check if civil war is possible for a player.
2575 If conquering_city is TRUE, one of the cities currently in the empire
2576 will shortly not be and shouldn't be considered.
2577 honour_server_option controls whether we honour the 'civilwarsize'
2578 server option. (If we don't, we still enforce a minimum empire size, to
2579 avoid the risk of creating a new player with no cities.)
2580 **************************************************************************/
2581 bool civil_war_possible(struct player
*pplayer
, bool conquering_city
,
2582 bool honour_server_option
)
2586 if (!game
.info
.civil_war_enabled
) {
2590 n
= city_list_size(pplayer
->cities
);
2592 if (n
- (conquering_city
?1:0) < GAME_MIN_CIVILWARSIZE
) {
2595 if (honour_server_option
) {
2596 return game
.server
.civilwarsize
< GAME_MAX_CIVILWARSIZE
2597 && n
>= game
.server
.civilwarsize
;
2603 /**********************************************************************
2604 civil_war_triggered:
2605 * The capture of a capital is not a sure fire way to throw
2606 and empire into civil war. Some governments are more susceptible
2607 than others, here are the base probabilities:
2611 Fundamentalism 60% (Only in civ2 ruleset)
2615 * In addition each city in disorder adds 5%, each celebrating city
2616 subtracts 5% from the probability of a civil war.
2617 * If you have at least 1 turns notice of the impending loss of
2618 your capital, you can hike luxuries up to the hightest value,
2619 and by this reduce the chance of a civil war. In fact by
2620 hiking the luxuries to 100% under Democracy, it is easy to
2621 get massively negative numbers - guaranteeing imunity from
2622 civil war. Likewise, 3 cities in disorder under despotism
2623 guarantees a civil war.
2624 * This routine calculates these probabilities and returns true
2625 if a civil war is triggered.
2627 ***********************************************************************/
2628 bool civil_war_triggered(struct player
*pplayer
)
2630 /* Get base probabilities */
2631 int dice
= fc_rand(100); /* Throw the dice */
2632 int prob
= get_player_bonus(pplayer
, EFT_CIVIL_WAR_CHANCE
);
2634 /* Now compute the contribution of the cities. */
2635 city_list_iterate(pplayer
->cities
, pcity
) {
2636 if (city_unhappy(pcity
)) {
2639 if (city_celebrating(pcity
)) {
2642 } city_list_iterate_end
;
2644 log_verbose("Civil war chance for %s: prob %d, dice %d",
2645 player_name(pplayer
), prob
, dice
);
2647 return (dice
< prob
);
2650 /**********************************************************************
2651 Capturing a nation's capital is a devastating blow. This function
2652 creates a new AI player, and randomly splits the original players
2653 city list into two. Of course this results in a real mix up of
2654 teritory - but since when have civil wars ever been tidy, or civil.
2656 Embassies: All embassies with other players are lost. Other players
2657 retain their embassies with pplayer.
2658 * Units: Units inside cities are assigned to the new owner
2659 of the city. Units outside are transferred along
2660 with the ownership of their supporting city.
2661 If the units are in a unit stack with non rebel units,
2662 then whichever units are nearest an allied city
2663 are teleported to that city. If the stack is a
2664 transport at sea, then all rebel units on the
2665 transport are teleported to their nearest allied city.
2667 Cities: Are split randomly into 2. This results in a real
2668 mix up of teritory - but since when have civil wars
2669 ever been tidy, or for any matter civil?
2671 One caveat, since the spliting of cities is random, you can
2672 conceive that this could result in either the original player
2673 or the rebel getting 0 cities. To prevent this, the hack below
2674 ensures that each side gets roughly half, which ones is still
2675 determined randomly.
2677 ***********************************************************************/
2678 struct player
*civil_war(struct player
*pplayer
)
2681 struct player
*cplayer
;
2682 struct city
*capital
;
2683 struct city_list
*defector_candidates
;
2685 /* It is possible that this function gets called after pplayer
2686 * died. Player pointers are safe even after death. */
2687 if (!pplayer
->is_alive
) {
2691 if (normal_player_count() >= MAX_NUM_PLAYERS
) {
2692 /* No space to make additional player */
2693 log_normal(_("Could not throw %s into civil war - too many players"),
2694 nation_plural_for_player(pplayer
));
2697 if (normal_player_count() >= server
.playable_nations
) {
2698 /* No nation for additional player */
2699 log_normal(_("Could not throw %s into civil war - no available nations"),
2700 nation_plural_for_player(pplayer
));
2704 /* It doesn't make sense to try to split an empire of 1 city.
2705 * This should have been enforced by civil_war_possible(). */
2706 fc_assert_ret_val(city_list_size(pplayer
->cities
) > 1, NULL
);
2708 defector_candidates
= city_list_new();
2709 city_list_iterate(pplayer
->cities
, pcity
) {
2710 bool gameloss_present
= FALSE
;
2712 /* Capital (probably new capital) won't defect */
2713 if (is_capital(pcity
)) {
2717 /* City hosting victim's GameLoss unit won't defect */
2718 unit_list_iterate(city_tile(pcity
)->units
, punit
) {
2719 if (unit_owner(punit
) == pplayer
2720 && unit_has_type_flag(punit
, UTYF_GAMELOSS
)) {
2721 gameloss_present
= TRUE
;
2724 } unit_list_iterate_end
;
2725 if (gameloss_present
) {
2729 city_list_append(defector_candidates
, pcity
);
2730 } city_list_iterate_end
;
2732 if (city_list_size(defector_candidates
) == 0) {
2733 log_verbose(_("Could not throw %s into civil war - no available cities"),
2734 nation_plural_for_player(pplayer
));
2735 city_list_destroy(defector_candidates
);
2739 /* We're definitely going to create a new rebel player. */
2741 if (normal_player_count() == game
.server
.max_players
) {
2742 /* 'maxplayers' must be increased to allow for a new player. */
2744 /* This assert should never be called due to the first check above. */
2745 fc_assert_ret_val(game
.server
.max_players
< MAX_NUM_PLAYERS
, NULL
);
2747 game
.server
.max_players
++;
2748 log_debug("Increased 'maxplayers' to allow the creation of a new player "
2749 "due to civil war.");
2752 cplayer
= split_player(pplayer
);
2754 /* Before units, cities, so clients know name of new nation
2755 * (for debugging etc).
2757 send_player_all_c(cplayer
, NULL
);
2758 send_player_all_c(pplayer
, NULL
);
2760 /* Now split the empire */
2762 log_verbose("%s civil war; created AI %s",
2763 nation_rule_name(nation_of_player(pplayer
)),
2764 nation_rule_name(nation_of_player(cplayer
)));
2765 notify_player(pplayer
, NULL
, E_CIVIL_WAR
, ftc_server
,
2766 _("Your nation is thrust into civil war."));
2768 notify_player(pplayer
, NULL
, E_FIRST_CONTACT
, ftc_server
,
2769 /* TRANS: <leader> ... the Poles. */
2770 _("%s is the rebellious leader of the %s."),
2771 player_name(cplayer
),
2772 nation_plural_for_player(cplayer
));
2774 j
= city_list_size(defector_candidates
); /* number left to process */
2775 /* Number to try to flip; ensure that at least one eligible city is
2778 city_list_iterate(defector_candidates
, pcity
) {
2779 fc_assert_action(!is_capital(pcity
), continue);
2780 if (i
>= j
|| (i
> 0 && fc_rand(2) == 1)) {
2781 /* Transfer city and units supported by this city to the new owner.
2782 * We do NOT resolve stack conflicts here, but rather later.
2783 * Reason: if we have a transporter from one city which is carrying
2784 * a unit from another city, and both cities join the rebellion. If we
2785 * resolved stack conflicts for each city we would teleport the first
2786 * of the units we met since the other would have another owner. */
2787 if (transfer_city(cplayer
, pcity
, -1, FALSE
, FALSE
, FALSE
, FALSE
)) {
2788 log_verbose("%s declares allegiance to the %s.", city_name_get(pcity
),
2789 nation_rule_name(nation_of_player(cplayer
)));
2790 notify_player(pplayer
, pcity
->tile
, E_CITY_LOST
, ftc_server
,
2791 /* TRANS: <city> ... the Poles. */
2792 _("%s declares allegiance to the %s."),
2794 nation_plural_for_player(cplayer
));
2795 script_server_signal_emit("city_transferred", 4,
2796 API_TYPE_CITY
, pcity
,
2797 API_TYPE_PLAYER
, pplayer
,
2798 API_TYPE_PLAYER
, cplayer
,
2799 API_TYPE_STRING
, "civil_war");
2804 } city_list_iterate_end
;
2806 city_list_destroy(defector_candidates
);
2808 resolve_unit_stacks(pplayer
, cplayer
, FALSE
);
2810 i
= city_list_size(cplayer
->cities
);
2811 fc_assert(i
> 0); /* rebels should have got at least one city */
2813 /* Choose a capital (random). */
2814 capital
= city_list_get(cplayer
->cities
, fc_rand(i
));
2815 city_build_free_buildings(capital
);
2816 give_midgame_initial_units(cplayer
, city_tile(capital
));
2818 notify_player(NULL
, NULL
, E_CIVIL_WAR
, ftc_server
,
2819 /* TRANS: ... Danes ... Poles ... <7> cities. */
2820 PL_("Civil war partitions the %s;"
2821 " the %s now hold %d city.",
2822 "Civil war partitions the %s;"
2823 " the %s now hold %d cities.",
2825 nation_plural_for_player(pplayer
),
2826 nation_plural_for_player(cplayer
),
2832 /**************************************************************************
2833 The client has send as a chunk of the attribute block.
2834 **************************************************************************/
2835 void handle_player_attribute_chunk(struct player
*pplayer
,
2836 const struct packet_player_attribute_chunk
2839 generic_handle_player_attribute_chunk(pplayer
, chunk
);
2842 /**************************************************************************
2843 The client request an attribute block.
2844 **************************************************************************/
2845 void handle_player_attribute_block(struct player
*pplayer
)
2847 send_attribute_block(pplayer
, pplayer
->current_conn
);
2850 /**************************************************************************
2852 (Hmm, how should "turn done" work for multi-connected non-observer players?)
2853 **************************************************************************/
2854 void handle_player_phase_done(struct player
*pplayer
,
2857 if (turn
!= game
.info
.turn
) {
2858 /* If this happens then the player actually pressed turn-done on a
2859 * previous turn but we didn't receive it until now. The player
2860 * probably didn't actually mean to end their turn! */
2863 pplayer
->phase_done
= TRUE
;
2865 check_for_full_turn_done();
2867 send_player_all_c(pplayer
, NULL
);
2870 /**************************************************************************
2871 Return the number of barbarian players.
2872 **************************************************************************/
2873 int barbarian_count(void)
2875 return server
.nbarbarians
;
2878 /**************************************************************************
2879 Return the number of non-barbarian players.
2880 **************************************************************************/
2881 int normal_player_count(void)
2883 return player_count() - server
.nbarbarians
;
2886 /****************************************************************************
2887 Add a status flag to a player.
2888 ****************************************************************************/
2889 void player_status_add(struct player
*plr
, enum player_status pstatus
)
2891 BV_SET(plr
->server
.status
, pstatus
);
2894 /****************************************************************************
2895 Check player status flag.
2896 ****************************************************************************/
2897 bool player_status_check(struct player
*plr
, enum player_status pstatus
)
2899 return BV_ISSET(plr
->server
.status
, pstatus
);
2902 /****************************************************************************
2903 Reset player status to 'normal'.
2904 ****************************************************************************/
2905 void player_status_reset(struct player
*plr
)
2907 BV_CLR_ALL(plr
->server
.status
);
2908 player_status_add(plr
, PSTATUS_NORMAL
);
2911 /**************************************************************************
2912 Returns the username that control of the player is delegated to, if any.
2913 **************************************************************************/
2914 const char *player_delegation_get(const struct player
*pplayer
)
2916 if (pplayer
== NULL
|| strlen(pplayer
->server
.delegate_to
) == 0) {
2917 /* No delegation if there is no player. */
2920 return pplayer
->server
.delegate_to
;
2924 /**************************************************************************
2925 Define a delegation. NULL for no delegate.
2926 **************************************************************************/
2927 void player_delegation_set(struct player
*pplayer
, const char *username
)
2929 fc_assert_ret(pplayer
!= NULL
);
2931 if (username
== NULL
|| strlen(username
) == 0) {
2932 pplayer
->server
.delegate_to
[0] = '\0';
2934 sz_strlcpy(pplayer
->server
.delegate_to
, username
);
2938 /*****************************************************************************
2939 Returns TRUE if a delegation is active.
2940 This means that either the player is controlled by a delegate, or the
2941 player has been temporarily 'put aside' by a delegate.
2942 *****************************************************************************/
2943 bool player_delegation_active(const struct player
*pplayer
)
2945 return (pplayer
&& strlen(pplayer
->server
.orig_username
) != 0);
2948 /*****************************************************************************
2949 Send information about delegations to reconnecting users.
2950 *****************************************************************************/
2951 void send_delegation_info(const struct connection
*pconn
)
2953 if (game
.info
.is_new_game
) {
2957 if (!pconn
->observer
2958 && pconn
->playing
&& player_delegation_get(pconn
->playing
) != NULL
) {
2959 notify_conn(pconn
->self
, NULL
, E_CONNECTION
, ftc_server
,
2960 /* TRANS: '/delegate cancel' is a server command and must not
2962 _("User '%s' is currently allowed to take control of your "
2963 "player while you are away. Use '/delegate cancel' to "
2964 "revoke this access."),
2965 player_delegation_get(pconn
->playing
));
2969 bool any_delegations
= FALSE
;
2970 players_iterate(aplayer
) {
2971 if (player_delegation_get(aplayer
) != NULL
2972 && strcmp(player_delegation_get(aplayer
), pconn
->username
) == 0) {
2973 notify_conn(pconn
->self
, NULL
, E_CONNECTION
, ftc_server
,
2974 _("Control of player '%s' is delegated to you."),
2975 player_name(aplayer
));
2976 any_delegations
= TRUE
;
2978 } players_iterate_end
;
2979 if (any_delegations
) {
2980 notify_conn(pconn
->self
, NULL
, E_CONNECTION
, ftc_server
,
2981 /* TRANS: '/delegate take' is a server command and must not
2982 * be translated; but <player> should be translated. */
2983 _("Use '/delegate take <player>' to take control of a "
2984 "delegated player."));
2989 /*****************************************************************************
2990 For a given user, if there is some player that the user originally
2991 controlled but is currently delegated to another user, return that player.
2992 See also player_by_user().
2993 *****************************************************************************/
2994 struct player
*player_by_user_delegated(const char *name
)
2996 players_iterate(pplayer
) {
2997 if (player_delegation_get(pplayer
)
2998 && fc_strcasecmp(name
, pplayer
->server
.orig_username
) == 0) {
3001 } players_iterate_end
;
3006 /****************************************************************************
3007 Initialise the player colors.
3008 ****************************************************************************/
3009 void playercolor_init(void)
3011 fc_assert_ret(game
.server
.plr_colors
== NULL
);
3012 game
.server
.plr_colors
= rgbcolor_list_new();
3015 /****************************************************************************
3016 Free the memory allocated for the player color.
3017 ****************************************************************************/
3018 void playercolor_free(void)
3020 if (game
.server
.plr_colors
== NULL
) {
3024 if (rgbcolor_list_size(game
.server
.plr_colors
) > 0) {
3025 rgbcolor_list_iterate(game
.server
.plr_colors
, prgbcolor
) {
3026 rgbcolor_list_remove(game
.server
.plr_colors
, prgbcolor
);
3027 rgbcolor_destroy(prgbcolor
);
3028 } rgbcolor_list_iterate_end
;
3030 rgbcolor_list_destroy(game
.server
.plr_colors
);
3031 game
.server
.plr_colors
= NULL
;
3034 /****************************************************************************
3035 Add a color to the list of all available player colors.
3036 ****************************************************************************/
3037 void playercolor_add(struct rgbcolor
*prgbcolor
)
3039 fc_assert_ret(game
.server
.plr_colors
!= NULL
);
3041 rgbcolor_list_append(game
.server
.plr_colors
, prgbcolor
);
3044 /****************************************************************************
3045 Get the player color with the index 'id'.
3046 ****************************************************************************/
3047 struct rgbcolor
*playercolor_get(int id
)
3049 fc_assert_ret_val(game
.server
.plr_colors
!= NULL
, NULL
);
3051 return rgbcolor_list_get(game
.server
.plr_colors
, id
);
3054 /****************************************************************************
3055 Number of player colors defined.
3056 ****************************************************************************/
3057 int playercolor_count(void)
3059 fc_assert_ret_val(game
.server
.plr_colors
!= NULL
, -1);
3061 return rgbcolor_list_size(game
.server
.plr_colors
);
3064 /****************************************************************************
3065 Sets player's multipliers.
3066 ****************************************************************************/
3067 void handle_player_multiplier(struct player
*pplayer
, int count
,
3068 const int *multipliers
)
3073 if (count
!= multiplier_count()) {
3074 log_error("Bad number of multipliers %d from client for %s",
3075 count
, player_name(pplayer
));
3079 for (i
= 0; i
< count
; i
++) {
3080 struct multiplier
*pmul
= multiplier_by_number(i
);
3082 if (multipliers
[i
] < pmul
->start
|| multipliers
[i
] > pmul
->stop
) {
3083 log_error("Multiplier value %d for %s out of range for %s",
3084 multipliers
[i
], multiplier_rule_name(pmul
),
3085 player_name(pplayer
));
3089 rval
= (multipliers
[i
] - pmul
->start
) / pmul
->step
* pmul
->step
+ pmul
->start
;
3090 if (rval
!= multipliers
[i
]) {
3091 log_error("Multiplier value %d between valid values for %s for %s",
3092 multipliers
[i
], multiplier_rule_name(pmul
),
3093 player_name(pplayer
));
3098 for (i
= 0; i
< count
; i
++) {
3099 pplayer
->multipliers_target
[i
] = multipliers
[i
];
3102 send_player_info_c(pplayer
, NULL
);
3105 /****************************************************************************
3106 Toggle player to AI mode.
3107 ****************************************************************************/
3108 void player_set_to_ai_mode(struct player
*pplayer
, enum ai_level skill_level
)
3110 pplayer
->ai_controlled
= TRUE
;
3112 set_ai_level_directer(pplayer
, skill_level
);
3113 cancel_all_meetings(pplayer
);
3114 CALL_PLR_AI_FUNC(gained_control
, pplayer
, pplayer
);
3115 if (is_player_phase(pplayer
, game
.info
.phase
)) {
3116 CALL_PLR_AI_FUNC(restart_phase
, pplayer
, pplayer
);
3119 if (S_S_RUNNING
== server_state()) {
3120 /* In case this was last player who has not pressed turn done. */
3121 check_for_full_turn_done();
3124 fc_assert(pplayer
->ai_common
.skill_level
== skill_level
);
3127 /****************************************************************************
3128 Toggle player under human control.
3129 ****************************************************************************/
3130 void player_set_under_human_control(struct player
*pplayer
)
3132 pplayer
->ai_controlled
= FALSE
;
3133 if (pplayer
->ai_common
.skill_level
== AI_LEVEL_AWAY
) {
3134 pplayer
->ai_common
.skill_level
= ai_level_invalid();
3137 CALL_PLR_AI_FUNC(lost_control
, pplayer
, pplayer
);
3139 /* Because the AI "cheats" with government rates but humans shouldn't. */
3140 if (!game
.info
.is_new_game
) {
3141 check_player_max_rates(pplayer
);
3143 cancel_all_meetings(pplayer
);