Freeciv-3.1
Loading...
Searching...
No Matches
diplhand.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdio.h>
19#include <stdlib.h>
20
21/* utility */
22#include "bitvector.h"
23#include "fcintl.h"
24#include "log.h"
25#include "mem.h"
26
27/* common */
28#include "ai.h"
29#include "diptreaty.h"
30#include "events.h"
31#include "game.h"
32#include "map.h"
33#include "packets.h"
34#include "player.h"
35#include "research.h"
36#include "unit.h"
37
38/* common/scriptcore */
39#include "luascript_types.h"
40
41/* server */
42#include "citytools.h"
43#include "cityturn.h"
44#include "maphand.h"
45#include "plrhand.h"
46#include "notify.h"
47#include "techtools.h"
48#include "unittools.h"
49
50/* server/advisors */
51#include "autosettlers.h"
52
53/* server/scripting */
54#include "script_server.h"
55
56#include "diplhand.h"
57
58static struct treaty_list *treaties = NULL;
59
60/* FIXME: Should this be put in a ruleset somewhere? */
61#define TURNS_LEFT 16
62
63/**********************************************************************/
66static void call_treaty_evaluate(struct player *pplayer, struct player *aplayer,
67 struct Treaty *ptreaty)
68{
69 if (is_ai(pplayer)) {
70 CALL_PLR_AI_FUNC(treaty_evaluate, pplayer, pplayer, aplayer, ptreaty);
71 }
72}
73
74/**********************************************************************/
77static void call_treaty_accepted(struct player *pplayer, struct player *aplayer,
78 struct Treaty *ptreaty)
79{
80 if (is_ai(pplayer)) {
81 CALL_PLR_AI_FUNC(treaty_accepted, pplayer, pplayer, aplayer, ptreaty);
82 }
83}
84
85/**********************************************************************/
88void diplhand_init(void)
89{
90 treaties = treaty_list_new();
91}
92
93/**********************************************************************/
96void diplhand_free(void)
97{
99
100 treaty_list_destroy(treaties);
101 treaties = NULL;
102}
103
104/**********************************************************************/
108{
109 /* Free memory allocated for treaties */
111 clear_treaty(pt);
112 free(pt);
114
115 treaty_list_clear(treaties);
116}
117
118/**********************************************************************/
121struct Treaty *find_treaty(struct player *plr0, struct player *plr1)
122{
123 treaty_list_iterate(treaties, ptreaty) {
124 if ((ptreaty->plr0 == plr0 && ptreaty->plr1 == plr1)
125 || (ptreaty->plr0 == plr1 && ptreaty->plr1 == plr0)) {
126 return ptreaty;
127 }
129
130 return NULL;
131}
132
133/**********************************************************************/
136static enum diplstate_type dst_closest(enum diplstate_type a,
137 enum diplstate_type b)
138{
139 static const int how_close[DS_LAST] = {
140 [DS_NO_CONTACT] = 0,
141 [DS_WAR] = 1,
142 [DS_CEASEFIRE] = 2,
143 [DS_ARMISTICE] = 3,
144 [DS_PEACE] = 4,
145 [DS_ALLIANCE] = 5,
146 [DS_TEAM] = 6,
147 };
148
149 if (how_close[a] < how_close[b]) {
150 return b;
151 } else {
152 return a;
153 }
154}
155
156/**********************************************************************/
160 struct player_diplstate *state2,
161 enum diplstate_type type)
162{
163 enum diplstate_type max;
164
165 if (type == DS_ARMISTICE) {
166 max = DS_PEACE;
167 } else {
168 max = type;
169 }
170
171 max = dst_closest(max, state1->max_state);
172
173 state1->type = type;
174 state2->type = type;
175 state1->max_state = max;
176 state2->max_state = max;
177}
178
179/**********************************************************************/
185 int counterpart)
186{
187 struct Treaty *ptreaty;
188 bool *player_accept, *other_accept;
189 enum dipl_reason diplcheck;
190 bool worker_refresh_required = FALSE;
191 struct player *pother = player_by_number(counterpart);
192
193 const struct req_context player_ctxt = { .player = pplayer };
194 const struct req_context other_ctxt = { .player = pother };
195
196 if (NULL == pother || pplayer == pother) {
197 return;
198 }
199
200 ptreaty = find_treaty(pplayer, pother);
201
202 if (!ptreaty) {
203 return;
204 }
205
206 if (ptreaty->plr0 == pplayer) {
207 player_accept = &ptreaty->accept0;
208 other_accept = &ptreaty->accept1;
209 } else {
210 player_accept = &ptreaty->accept1;
211 other_accept = &ptreaty->accept0;
212 }
213
214 if (!*player_accept) { /* Tries to accept. */
215
216 /* Check that player who accepts can keep what they promise. */
217
218 clause_list_iterate(ptreaty->clauses, pclause) {
219 struct city *pcity = NULL;
220
221 if (pclause->from == pplayer) {
222 struct clause_info *info = clause_info_get(pclause->type);
223
224 if (!are_reqs_active(&player_ctxt, pother,
225 &(info->giver_reqs), RPT_POSSIBLE)
226 || !are_reqs_active(&other_ctxt, pplayer,
227 &(info->receiver_reqs), RPT_POSSIBLE)) {
228 log_error("Requirements of a clause between %s and %s not fulfilled",
229 player_name(pplayer), player_name(pother));
230 return;
231 }
232 }
233
234 if (pclause->from == pplayer || is_pact_clause(pclause->type)) {
235 switch (pclause->type) {
236 case CLAUSE_EMBASSY:
237 if (player_has_real_embassy(pother, pplayer)) {
238 log_error("%s tried to give embassy to %s, who already "
239 "has an embassy",
240 player_name(pplayer), player_name(pother));
241 return;
242 }
243 break;
244 case CLAUSE_ADVANCE:
246 pclause->value, game.info.tech_trade_allow_holes)) {
247 /* It is impossible to give a technology to a civilization that
248 * can not possess it (the client should enforce this). */
249 log_error("Treaty: %s can't have tech %s",
251 advance_rule_name(advance_by_number(pclause->value)));
252 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
253 _("The %s can't accept %s."),
256 (pclause->value)));
257 return;
258 }
259 if (research_invention_state(research_get(pplayer), pclause->value)
260 != TECH_KNOWN) {
261 log_error("Nation %s try to give unknown tech %s to nation %s.",
263 advance_rule_name(advance_by_number(pclause->value)),
265 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
266 _("You don't have tech %s, you can't accept treaty."),
268 (pclause->value)));
269 return;
270 }
271 break;
272 case CLAUSE_CITY:
273 pcity = game_city_by_number(pclause->value);
274 if (!pcity) { /* Can't find out cityname any more. */
275 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
276 _("City you are trying to give no longer exists, "
277 "you can't accept treaty."));
278 return;
279 }
280 if (city_owner(pcity) != pplayer) {
281 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
282 _("You are not owner of %s, you can't accept treaty."),
283 city_link(pcity));
284 return;
285 }
286 if (is_capital(pcity)) {
287 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
288 _("Your capital (%s) is requested, "
289 "you can't accept treaty."),
290 city_link(pcity));
291 return;
292 }
293 break;
294 case CLAUSE_CEASEFIRE:
295 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_CEASEFIRE);
296 if (diplcheck != DIPL_OK) {
297 return;
298 }
299 break;
300 case CLAUSE_PEACE:
301 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_PEACE);
302 if (diplcheck != DIPL_OK) {
303 return;
304 }
305 break;
306 case CLAUSE_ALLIANCE:
307 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_ALLIANCE);
308 if (diplcheck == DIPL_ALLIANCE_PROBLEM_US) {
309 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
310 _("You cannot form an alliance because you are "
311 "at war with an ally of %s."),
312 player_name(pother));
313 } else if (diplcheck == DIPL_ALLIANCE_PROBLEM_THEM) {
314 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
315 _("You cannot form an alliance because %s is "
316 "at war with an ally of yours."),
317 player_name(pother));
318 }
319 if (diplcheck != DIPL_OK) {
320 return;
321 }
322 break;
323 case CLAUSE_GOLD:
324 if (pplayer->economic.gold < pclause->value) {
325 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
326 _("You don't have enough gold, "
327 "you can't accept treaty."));
328 return;
329 }
330 break;
331 default:
332 ; /* nothing */
333 }
334 }
336 }
337
338 *player_accept = ! *player_accept;
339
341 player_number(pother), *player_accept,
342 *other_accept);
344 player_number(pplayer), *other_accept,
345 *player_accept);
346
347 if (ptreaty->accept0 && ptreaty->accept1) {
348 int nclauses = clause_list_size(ptreaty->clauses);
349
351 player_number(pother),
352 player_number(pplayer));
354 player_number(pplayer),
355 player_number(pplayer));
356
357 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
358 PL_("A treaty containing %d clause was agreed upon.",
359 "A treaty containing %d clauses was agreed upon.",
360 nclauses),
361 nclauses);
362 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
363 PL_("A treaty containing %d clause was agreed upon.",
364 "A treaty containing %d clauses was agreed upon.",
365 nclauses),
366 nclauses);
367
368 /* Check that one who accepted treaty earlier still have everything
369 they promised to give. */
370
371 clause_list_iterate(ptreaty->clauses, pclause) {
372 struct city *pcity;
373
374 if (pclause->from == pother) {
375 struct clause_info *info = clause_info_get(pclause->type);
376
377 if (!are_reqs_active(&other_ctxt, pplayer,
378 &(info->giver_reqs), RPT_POSSIBLE)
379 || !are_reqs_active(&player_ctxt, pother,
380 &(info->receiver_reqs), RPT_POSSIBLE)) {
381 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
382 _("Clause requirements are no longer fulfilled. "
383 "Treaty with %s canceled!"),
385 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
386 _("Clause requirements are no longer fulfilled. "
387 "Treaty with %s canceled!"),
388 nation_plural_for_player(pplayer));
389 return;
390 }
391 }
392
393 if (pclause->from == pother) {
394 switch (pclause->type) {
395 case CLAUSE_CITY:
396 pcity = game_city_by_number(pclause->value);
397 if (!pcity) { /* Can't find out cityname any more. */
398 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
399 _("One of the cities the %s are giving away"
400 " is destroyed! Treaty canceled!"),
402 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
403 _("One of the cities the %s are giving away"
404 " is destroyed! Treaty canceled!"),
406 goto cleanup;
407 }
408 if (city_owner(pcity) != pother) {
409 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
410 _("The %s no longer control %s! "
411 "Treaty canceled!"),
413 city_link(pcity));
414 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
415 _("The %s no longer control %s! "
416 "Treaty canceled!"),
418 city_link(pcity));
419 goto cleanup;
420 }
421 if (is_capital(pcity)) {
422 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
423 _("Your capital (%s) is requested, "
424 "you can't accept treaty."),
425 city_link(pcity));
426 goto cleanup;
427 }
428
429 break;
430 case CLAUSE_ALLIANCE:
431 /* We need to recheck this way since things might have
432 * changed. */
433 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_ALLIANCE);
434 if (diplcheck != DIPL_OK) {
435 goto cleanup;
436 }
437 break;
438 case CLAUSE_PEACE:
439 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_PEACE);
440 if (diplcheck != DIPL_OK) {
441 goto cleanup;
442 }
443 break;
444 case CLAUSE_CEASEFIRE:
445 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_CEASEFIRE);
446 if (diplcheck != DIPL_OK) {
447 goto cleanup;
448 }
449 break;
450 case CLAUSE_GOLD:
451 if (pother->economic.gold < pclause->value) {
452 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
453 _("The %s don't have the promised amount "
454 "of gold! Treaty canceled!"),
456 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
457 _("The %s don't have the promised amount "
458 "of gold! Treaty canceled!"),
460 goto cleanup;
461 }
462 break;
463 default:
464 ; /* nothing */
465 }
466 }
468
469 call_treaty_accepted(pplayer, pother, ptreaty);
470 call_treaty_accepted(pother, pplayer, ptreaty);
471
472 clause_list_iterate(ptreaty->clauses, pclause) {
473 struct player *pgiver = pclause->from;
474 struct player *pdest = (pplayer == pgiver) ? pother : pplayer;
475 struct player_diplstate *ds_giverdest
476 = player_diplstate_get(pgiver, pdest);
477 struct player_diplstate *ds_destgiver
478 = player_diplstate_get(pdest, pgiver);
479 enum diplstate_type old_diplstate = ds_giverdest->type;
480 struct unit_list *pgiver_seen_units BAD_HEURISTIC_INIT(NULL);
481 struct unit_list *pdest_seen_units BAD_HEURISTIC_INIT(NULL);
482
483 switch (pclause->type) {
484 case CLAUSE_EMBASSY:
485 establish_embassy(pdest, pgiver); /* sic */
486 notify_player(pgiver, NULL, E_TREATY_EMBASSY, ftc_server,
487 _("You gave an embassy to %s."),
488 player_name(pdest));
489 notify_player(pdest, NULL, E_TREATY_EMBASSY, ftc_server,
490 _("%s allowed you to create an embassy!"),
491 player_name(pgiver));
492 break;
493 case CLAUSE_ADVANCE:
494 {
495 /* It is possible that two players open the diplomacy dialog
496 * and try to give us the same tech at the same time. This
497 * should be handled discreetly instead of giving a core dump. */
498 struct research *presearch = research_get(pdest);
499 const char *advance_name;
500
501 if (research_invention_state(presearch, pclause->value)
502 == TECH_KNOWN) {
503 log_verbose("Nation %s already know tech %s, "
504 "that %s want to give them.",
506 advance_rule_name(advance_by_number(pclause->value)),
508 break;
509 }
511 (pclause->value));
512 notify_player(pdest, NULL, E_TECH_GAIN, ftc_server,
513 _("You are taught the knowledge of %s."),
514 advance_name);
515
516 if (tech_transfer(pdest, pgiver, pclause->value)) {
517 char research_name[MAX_LEN_NAME * 2];
518
519 research_pretty_name(presearch, research_name,
520 sizeof(research_name));
521 notify_research(presearch, pdest, E_TECH_GAIN, ftc_server,
522 _("You have acquired %s thanks to the %s "
523 "treaty with the %s."),
524 advance_name,
528 (presearch, pgiver, E_TECH_EMBASSY, ftc_server,
529 /* TRANS: Tech from another player */
530 Q_("?fromplr:The %s have acquired %s from the %s."),
531 research_name,
532 advance_name,
534
535 script_tech_learned(presearch, pdest,
536 advance_by_number(pclause->value), "traded");
537 research_apply_penalty(presearch, pclause->value,
539 found_new_tech(presearch, pclause->value, FALSE, TRUE);
540 }
541 }
542 break;
543 case CLAUSE_GOLD:
544 {
545 int received = pclause->value
546 * (100 - game.server.diplgoldcost) / 100;
547 pgiver->economic.gold -= pclause->value;
548 pdest->economic.gold += received;
549 notify_player(pdest, NULL, E_DIPLOMACY, ftc_server,
550 PL_("You get %d gold.",
551 "You get %d gold.", received), received);
552 }
553 break;
554 case CLAUSE_MAP:
555 give_map_from_player_to_player(pgiver, pdest);
556 notify_player(pdest, NULL, E_DIPLOMACY, ftc_server,
557 /* TRANS: ... Polish worldmap. */
558 _("You receive the %s worldmap."),
560
561 worker_refresh_required = TRUE; /* See CLAUSE_VISION */
562 break;
563 case CLAUSE_SEAMAP:
565 notify_player(pdest, NULL, E_DIPLOMACY, ftc_server,
566 /* TRANS: ... Polish seamap. */
567 _("You receive the %s seamap."),
569
570 worker_refresh_required = TRUE; /* See CLAUSE_VISION */
571 break;
572 case CLAUSE_CITY:
573 {
574 struct city *pcity = game_city_by_number(pclause->value);
575
576 if (!pcity) {
577 log_error("Treaty city id %d not found - skipping clause.",
578 pclause->value);
579 break;
580 }
581
582 notify_player(pdest, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
583 _("You receive the city of %s from %s."),
584 city_link(pcity), player_name(pgiver));
585
586 notify_player(pgiver, city_tile(pcity), E_CITY_LOST, ftc_server,
587 _("You give the city of %s to %s."),
588 city_link(pcity), player_name(pdest));
589
590 if (transfer_city(pdest, pcity, -1, TRUE, TRUE, FALSE,
591 !is_barbarian(pdest))) {
592 script_server_signal_emit("city_transferred", pcity, pgiver,
593 pdest, "trade");
594 }
595 break;
596 }
597 case CLAUSE_CEASEFIRE:
598 if (old_diplstate == DS_ALLIANCE) {
599 pgiver_seen_units = get_units_seen_via_ally(pgiver, pdest);
600 pdest_seen_units = get_units_seen_via_ally(pdest, pgiver);
601 }
602 ds_giverdest->type = DS_CEASEFIRE;
603 ds_giverdest->turns_left = TURNS_LEFT;
604 ds_destgiver->type = DS_CEASEFIRE;
605 ds_destgiver->turns_left = TURNS_LEFT;
606 notify_player(pgiver, NULL, E_TREATY_CEASEFIRE, ftc_server,
607 _("You agree on a cease-fire with %s."),
608 player_name(pdest));
609 notify_player(pdest, NULL, E_TREATY_CEASEFIRE, ftc_server,
610 _("You agree on a cease-fire with %s."),
611 player_name(pgiver));
612 if (old_diplstate == DS_ALLIANCE) {
614 pgiver_seen_units,
615 pdest_seen_units);
616 unit_list_destroy(pgiver_seen_units);
617 unit_list_destroy(pdest_seen_units);
618 }
619
620 worker_refresh_required = TRUE;
621 break;
622 case CLAUSE_PEACE:
623 if (old_diplstate == DS_ALLIANCE) {
624 pgiver_seen_units = get_units_seen_via_ally(pgiver, pdest);
625 pdest_seen_units = get_units_seen_via_ally(pdest, pgiver);
626 }
627 set_diplstate_type(ds_giverdest, ds_destgiver, DS_ARMISTICE);
628 ds_giverdest->turns_left = TURNS_LEFT;
629 ds_destgiver->turns_left = TURNS_LEFT;
630 notify_player(pgiver, NULL, E_TREATY_PEACE, ftc_server,
631 /* TRANS: ... the Poles ... Polish territory */
632 PL_("You agree on an armistice with the %s. In %d turn, "
633 "it will become a peace treaty. Move your "
634 "military units out of %s territory to avoid them "
635 "being disbanded.",
636 "You agree on an armistice with the %s. In %d turns, "
637 "it will become a peace treaty. Move any "
638 "military units out of %s territory to avoid them "
639 "being disbanded.",
640 TURNS_LEFT),
644 notify_player(pdest, NULL, E_TREATY_PEACE, ftc_server,
645 /* TRANS: ... the Poles ... Polish territory */
646 PL_("You agree on an armistice with the %s. In %d turn, "
647 "it will become a peace treaty. Move your "
648 "military units out of %s territory to avoid them "
649 "being disbanded.",
650 "You agree on an armistice with the %s. In %d turns, "
651 "it will become a peace treaty. Move any "
652 "military units out of %s territory to avoid them "
653 "being disbanded.",
654 TURNS_LEFT),
658 if (old_diplstate == DS_ALLIANCE) {
660 pgiver_seen_units,
661 pdest_seen_units);
662 unit_list_destroy(pgiver_seen_units);
663 unit_list_destroy(pdest_seen_units);
664 }
665
666 worker_refresh_required = TRUE;
667 break;
668 case CLAUSE_ALLIANCE:
669 set_diplstate_type(ds_giverdest, ds_destgiver, DS_ALLIANCE);
670 notify_player(pgiver, NULL, E_TREATY_ALLIANCE, ftc_server,
671 _("You agree on an alliance with %s."),
672 player_name(pdest));
673 notify_player(pdest, NULL, E_TREATY_ALLIANCE, ftc_server,
674 _("You agree on an alliance with %s."),
675 player_name(pgiver));
676 give_allied_visibility(pgiver, pdest);
677 give_allied_visibility(pdest, pgiver);
678
679 worker_refresh_required = TRUE;
680 break;
681 case CLAUSE_VISION:
682 give_shared_vision(pgiver, pdest);
683 notify_player(pgiver, NULL, E_TREATY_SHARED_VISION, ftc_server,
684 _("You give shared vision to %s."),
685 player_name(pdest));
686 notify_player(pdest, NULL, E_TREATY_SHARED_VISION, ftc_server,
687 _("%s gives you shared vision."),
688 player_name(pgiver));
689
690 /* Yes, shared vision may let us to _know_ tiles
691 * within radius of our own city. */
692 worker_refresh_required = TRUE;
693 break;
694 case CLAUSE_COUNT:
695 fc_assert(pclause->type != CLAUSE_COUNT);
696 break;
697 }
698
700
701 /* In theory, we would need refresh only receiving party of
702 * CLAUSE_MAP, CLAUSE_SEAMAP and CLAUSE_VISION clauses.
703 * It's quite unlikely that there is such a clause going one
704 * way but no clauses affecting both parties or going other
705 * way. */
706 if (worker_refresh_required) {
709 sync_cities();
710 }
711
712 cleanup:
713 treaty_list_remove(treaties, ptreaty);
714 clear_treaty(ptreaty);
715 free(ptreaty);
716 send_player_all_c(pplayer, NULL);
717 send_player_all_c(pother, NULL);
718 }
719}
720
721/**********************************************************************/
724void establish_embassy(struct player *pplayer, struct player *aplayer)
725{
726 /* Establish the embassy. */
727 BV_SET(pplayer->real_embassy, player_index(aplayer));
728
729 player_list_iterate(team_members(pplayer->team), teammate) {
730 /* Knowledge that pplayer has an embassy now */
731 send_player_all_c(pplayer, teammate->connections);
732 /* INFO_EMBASSY level info */
733 send_player_all_c(aplayer, teammate->connections);
735
736 /* update player dialog with embassy */
737 send_player_all_c(pplayer, aplayer->connections);
738
739 /* Send research info */
740 send_research_info(research_get(aplayer), pplayer->connections);
741}
742
743/**********************************************************************/
747 int counterpart, int giver,
748 enum clause_type type, int value)
749{
750 struct Treaty *ptreaty;
751 struct player *pgiver = player_by_number(giver);
752 struct player *pother = player_by_number(counterpart);
753
754 if (NULL == pother || pplayer == pother || NULL == pgiver) {
755 return;
756 }
757
758 if (pgiver != pplayer && pgiver != pother) {
759 return;
760 }
761
762 ptreaty = find_treaty(pplayer, pother);
763
764 if (ptreaty && remove_clause(ptreaty, pgiver, type, value)) {
766 player_number(pother), giver, type,
767 value);
769 player_number(pplayer), giver, type,
770 value);
771 call_treaty_evaluate(pplayer, pother, ptreaty);
772 call_treaty_evaluate(pother, pplayer, ptreaty);
773 }
774}
775
776/**********************************************************************/
780 int counterpart, int giver,
781 enum clause_type type, int value)
782{
783 struct Treaty *ptreaty;
784 struct player *pgiver = player_by_number(giver);
785 struct player *pother = player_by_number(counterpart);
786
787 if (NULL == pother || pplayer == pother || NULL == pgiver) {
788 return;
789 }
790
791 if (pgiver != pplayer && pgiver != pother) {
792 return;
793 }
794
795 ptreaty = find_treaty(pplayer, pother);
796
797 if (ptreaty != NULL
798 && add_clause(ptreaty, pgiver, type, value, NULL)) {
799 /*
800 * If we are trading cities, then it is possible that
801 * the dest is unaware of it's existence. We have 2 choices,
802 * forbid it, or lighten that area. If we assume that
803 * the giver knows what they are doing, then 2. is the
804 * most powerful option - I'll choose that for now.
805 * - Kris Bubendorfer
806 */
807 if (type == CLAUSE_CITY) {
808 struct city *pcity = game_city_by_number(value);
809
810 if (pcity && !map_is_known_and_seen(pcity->tile, pother, V_MAIN))
811 give_citymap_from_player_to_player(pcity, pplayer, pother);
812 }
813
815 player_number(pother), giver, type,
816 value);
818 player_number(pplayer), giver, type,
819 value);
820 call_treaty_evaluate(pplayer, pother, ptreaty);
821 call_treaty_evaluate(pother, pplayer, ptreaty);
822 }
823}
824
825/**********************************************************************/
829static void really_diplomacy_cancel_meeting(struct player *pplayer,
830 struct player *pother)
831{
832 struct Treaty *ptreaty = find_treaty(pplayer, pother);
833
834 if (ptreaty) {
836 player_number(pplayer),
837 player_number(pplayer));
838 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
839 _("%s canceled the meeting!"),
840 player_name(pplayer));
841 /* Need to send to pplayer too, for multi-connects: */
843 player_number(pother),
844 player_number(pplayer));
845 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
846 _("Meeting with %s canceled."),
847 player_name(pother));
848 treaty_list_remove(treaties, ptreaty);
849 clear_treaty(ptreaty);
850 free(ptreaty);
851 }
852}
853
854/**********************************************************************/
858 int counterpart)
859{
860 struct player *pother = player_by_number(counterpart);
861
862 if (NULL == pother || pplayer == pother) {
863 return;
864 }
865
866 really_diplomacy_cancel_meeting(pplayer, pother);
867}
868
869/**********************************************************************/
873 int counterpart)
874{
875 struct player *pother = player_by_number(counterpart);
876
877 if (NULL == pother || pplayer == pother) {
878 return;
879 }
880
881 if (find_treaty(pplayer, pother)) {
882 return;
883 }
884
885 if (get_player_bonus(pplayer, EFT_NO_DIPLOMACY) > 0
886 || get_player_bonus(pother, EFT_NO_DIPLOMACY) > 0) {
887 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
888 _("Your diplomatic envoy was decapitated!"));
889 return;
890 }
891
892 if (could_meet_with_player(pplayer, pother)) {
893 struct Treaty *ptreaty;
894
895 ptreaty = fc_malloc(sizeof(*ptreaty));
896 init_treaty(ptreaty, pplayer, pother);
897 treaty_list_prepend(treaties, ptreaty);
898
900 player_number(pother),
901 player_number(pplayer));
903 player_number(pplayer),
904 player_number(pplayer));
905 }
906}
907
908/**********************************************************************/
913{
914 struct player *pplayer = dest->playing;
915
916 if (!pplayer) {
917 return;
918 }
919 players_iterate(other) {
920 struct Treaty *ptreaty = find_treaty(pplayer, other);
921
922 if (ptreaty) {
923 fc_assert_action(pplayer != other, continue);
925 player_number(pplayer));
926 clause_list_iterate(ptreaty->clauses, pclause) {
928 player_number(other),
929 player_number(pclause->from),
930 pclause->type,
931 pclause->value);
933
934 if (ptreaty->plr0 == pplayer) {
936 ptreaty->accept0,
937 ptreaty->accept1);
938 } else {
940 ptreaty->accept1,
941 ptreaty->accept0);
942 }
943 }
945}
946
947/**********************************************************************/
950void cancel_all_meetings(struct player *pplayer)
951{
952 players_iterate(pplayer2) {
953 if (find_treaty(pplayer, pplayer2)) {
954 really_diplomacy_cancel_meeting(pplayer, pplayer2);
955 }
957}
958
959/**********************************************************************/
962void reject_all_treaties(struct player *pplayer)
963{
964 struct Treaty* treaty;
965 players_iterate(pplayer2) {
966 treaty = find_treaty(pplayer, pplayer2);
967 if (!treaty) {
968 continue;
969 }
970 treaty->accept0 = FALSE;
971 treaty->accept1 = FALSE;
973 player_number(pplayer2),
974 FALSE,
975 FALSE);
976 dlsend_packet_diplomacy_accept_treaty(pplayer2->connections,
977 player_number(pplayer),
978 FALSE,
979 FALSE);
981}
982
983/**********************************************************************/
986struct treaty_list *get_all_treaties(void)
987{
988 return treaties;
989}
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:374
#define BV_SET(bv, bit)
Definition bitvector.h:81
bool is_capital(const struct city *pcity)
Definition city.c:1552
#define city_tile(_pcity_)
Definition city.h:544
#define city_owner(_pcity_)
Definition city.h:543
void sync_cities(void)
Definition citytools.c:3238
void city_map_update_all_cities_for_player(struct player *pplayer)
Definition citytools.c:3272
bool transfer_city(struct player *ptaker, struct city *pcity, int kill_outside, bool transfer_unit_verbose, bool resolve_stack, bool raze, bool build_free)
Definition citytools.c:1070
void diplhand_free(void)
Definition diplhand.c:96
static struct treaty_list * treaties
Definition diplhand.c:58
void establish_embassy(struct player *pplayer, struct player *aplayer)
Definition diplhand.c:724
void send_diplomatic_meetings(struct connection *dest)
Definition diplhand.c:912
void diplhand_init(void)
Definition diplhand.c:88
void handle_diplomacy_remove_clause_req(struct player *pplayer, int counterpart, int giver, enum clause_type type, int value)
Definition diplhand.c:746
struct treaty_list * get_all_treaties(void)
Definition diplhand.c:986
void handle_diplomacy_create_clause_req(struct player *pplayer, int counterpart, int giver, enum clause_type type, int value)
Definition diplhand.c:779
void free_treaties(void)
Definition diplhand.c:107
static void call_treaty_accepted(struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
Definition diplhand.c:77
void reject_all_treaties(struct player *pplayer)
Definition diplhand.c:962
void set_diplstate_type(struct player_diplstate *state1, struct player_diplstate *state2, enum diplstate_type type)
Definition diplhand.c:159
void handle_diplomacy_init_meeting_req(struct player *pplayer, int counterpart)
Definition diplhand.c:872
void cancel_all_meetings(struct player *pplayer)
Definition diplhand.c:950
void handle_diplomacy_cancel_meeting_req(struct player *pplayer, int counterpart)
Definition diplhand.c:857
static void call_treaty_evaluate(struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
Definition diplhand.c:66
#define TURNS_LEFT
Definition diplhand.c:61
static enum diplstate_type dst_closest(enum diplstate_type a, enum diplstate_type b)
Definition diplhand.c:136
struct Treaty * find_treaty(struct player *plr0, struct player *plr1)
Definition diplhand.c:121
static void really_diplomacy_cancel_meeting(struct player *pplayer, struct player *pother)
Definition diplhand.c:829
void handle_diplomacy_accept_treaty_req(struct player *pplayer, int counterpart)
Definition diplhand.c:184
#define treaty_list_iterate(list, p)
Definition diplhand.h:30
#define treaty_list_iterate_end
Definition diplhand.h:32
int int initiated_from int int giver
Definition diplodlg_g.h:28
int counterpart
Definition diplodlg_g.h:25
void clear_treaty(struct Treaty *ptreaty)
Definition diptreaty.c:109
void init_treaty(struct Treaty *ptreaty, struct player *plr0, struct player *plr1)
Definition diptreaty.c:96
bool add_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val, struct player *client_player)
Definition diptreaty.c:142
bool remove_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val)
Definition diptreaty.c:120
struct clause_info * clause_info_get(enum clause_type type)
Definition diptreaty.c:273
bool could_meet_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:61
#define clause_list_iterate_end
Definition diptreaty.h:68
#define clause_list_iterate(clauselist, pclause)
Definition diptreaty.h:66
#define is_pact_clause(x)
Definition diptreaty.h:49
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
@ RPT_POSSIBLE
Definition fc_types.h:585
#define MAX_LEN_NAME
Definition fc_types.h:66
#define Q_(String)
Definition fcintl.h:70
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
const struct ft_color ftc_server
const char * city_link(const struct city *pcity)
struct civ_game game
Definition game.c:57
struct city * game_city_by_number(int id)
Definition game.c:102
GType type
Definition repodlgs.c:1312
#define log_verbose(message,...)
Definition log.h:109
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_action(condition, action)
Definition log.h:187
#define log_error(message,...)
Definition log.h:103
void give_map_from_player_to_player(struct player *pfrom, struct player *pdest)
Definition maphand.c:381
void give_seamap_from_player_to_player(struct player *pfrom, struct player *pdest)
Definition maphand.c:397
void give_citymap_from_player_to_player(struct city *pcity, struct player *pfrom, struct player *pdest)
Definition maphand.c:415
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:900
void give_shared_vision(struct player *pfrom, struct player *pto)
Definition maphand.c:1620
#define fc_malloc(sz)
Definition mem.h:34
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:137
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:168
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:177
void notify_research(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:393
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:291
void notify_research_embassies(const struct research *presearch, const struct player *exclude, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:433
void dlsend_packet_diplomacy_init_meeting(struct conn_list *dest, int counterpart, int initiated_from)
void dlsend_packet_diplomacy_remove_clause(struct conn_list *dest, int counterpart, int giver, enum clause_type type, int value)
int dsend_packet_diplomacy_create_clause(struct connection *pc, int counterpart, int giver, enum clause_type type, int value)
int dsend_packet_diplomacy_init_meeting(struct connection *pc, int counterpart, int initiated_from)
void dlsend_packet_diplomacy_create_clause(struct conn_list *dest, int counterpart, int giver, enum clause_type type, int value)
void dlsend_packet_diplomacy_accept_treaty(struct conn_list *dest, int counterpart, bool I_accepted, bool other_accepted)
void dlsend_packet_diplomacy_cancel_meeting(struct conn_list *dest, int counterpart, int initiated_from)
int dsend_packet_diplomacy_accept_treaty(struct connection *pc, int counterpart, bool I_accepted, bool other_accepted)
struct player * player_by_number(const int player_id)
Definition player.c:840
int player_number(const struct player *pplayer)
Definition player.c:828
enum dipl_reason pplayer_can_make_treaty(const struct player *p1, const struct player *p2, enum diplstate_type treaty)
Definition player.c:153
const char * player_name(const struct player *pplayer)
Definition player.c:886
bool player_has_real_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:234
int player_index(const struct player *pplayer)
Definition player.c:820
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:317
#define players_iterate_end
Definition player.h:535
dipl_reason
Definition player.h:194
@ DIPL_ALLIANCE_PROBLEM_THEM
Definition player.h:196
@ DIPL_ALLIANCE_PROBLEM_US
Definition player.h:196
@ DIPL_OK
Definition player.h:195
#define players_iterate(_pplayer)
Definition player.h:530
#define player_list_iterate(playerlist, pplayer)
Definition player.h:553
static bool is_barbarian(const struct player *pplayer)
Definition player.h:488
#define is_ai(plr)
Definition player.h:234
#define player_list_iterate_end
Definition player.h:555
void send_player_all_c(struct player *src, struct conn_list *dest)
Definition plrhand.c:983
void update_players_after_alliance_breakup(struct player *pplayer, struct player *pplayer2, const struct unit_list *pplayer_seen_units, const struct unit_list *pplayer2_seen_units)
Definition plrhand.c:685
bool are_reqs_active(const struct req_context *context, const struct player *other_player, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
struct research * research_get(const struct player *pplayer)
Definition research.c:126
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:616
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Definition research.c:690
int research_pretty_name(const struct research *presearch, char *buf, size_t buf_len)
Definition research.c:167
void script_server_signal_emit(const char *signal_name,...)
#define BAD_HEURISTIC_INIT(_ini_val_)
Definition shared.h:41
struct player * plr0
Definition diptreaty.h:77
bool accept0
Definition diptreaty.h:78
bool accept1
Definition diptreaty.h:78
struct clause_list * clauses
Definition diptreaty.h:79
struct player * plr1
Definition diptreaty.h:77
Definition city.h:309
struct tile * tile
Definition city.h:311
int diplgoldcost
Definition game.h:139
struct civ_game::@30::@34 server
struct packet_game_info info
Definition game.h:89
int diplbulbcost
Definition game.h:138
struct requirement_vector receiver_reqs
Definition diptreaty.h:57
struct requirement_vector giver_reqs
Definition diptreaty.h:56
struct player * playing
Definition connection.h:156
bool tech_trade_allow_holes
enum diplstate_type max_state
Definition player.h:202
enum diplstate_type type
Definition player.h:201
struct team * team
Definition player.h:261
struct conn_list * connections
Definition player.h:298
bv_player real_embassy
Definition player.h:277
struct player_economic economic
Definition player.h:284
const struct player * player
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
const struct player_list * team_members(const struct team *pteam)
Definition team.c:456
struct advance * advance_by_number(const Tech_type_id atype)
Definition tech.c:107
const char * advance_name_translation(const struct advance *padvance)
Definition tech.c:290
const char * advance_rule_name(const struct advance *padvance)
Definition tech.c:299
void found_new_tech(struct research *presearch, Tech_type_id tech_found, bool was_discovery, bool saving_bulbs)
Definition techtools.c:327
void research_apply_penalty(struct research *presearch, Tech_type_id tech, int penalty_percent)
Definition techtools.c:67
void send_research_info(const struct research *presearch, const struct conn_list *dest)
Definition techtools.c:273
void script_tech_learned(struct research *presearch, struct player *originating_plr, struct advance *tech, const char *reason)
Definition techtools.c:84
bool tech_transfer(struct player *plr_recv, struct player *plr_donor, Tech_type_id tech)
Definition techtools.c:1411
struct unit_list * get_units_seen_via_ally(const struct player *pplayer, const struct player *aplayer)
Definition unittools.c:1429
void give_allied_visibility(struct player *pplayer, struct player *aplayer)
Definition unittools.c:1488