webperimental: killstack decides stack protects.
[freeciv.git] / server / diplhand.c
blobe0f755b66ecb0b5370f1b1ae34ad5353d08ff730
1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <stdio.h>
19 #include <stdlib.h>
21 /* utility */
22 #include "bitvector.h"
23 #include "fcintl.h"
24 #include "log.h"
25 #include "mem.h"
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"
38 /* common/scriptcore */
39 #include "luascript_types.h"
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"
50 /* server/advisors */
51 #include "autosettlers.h"
53 /* server/scripting */
54 #include "script_server.h"
56 #include "diplhand.h"
58 static struct treaty_list *treaties = NULL;
60 /* FIXME: Should this be put in a ruleset somewhere? */
61 #define TURNS_LEFT 16
63 /**************************************************************************
64 Calls treaty_evaluate function if such is set for AI player.
65 **************************************************************************/
66 static void call_treaty_evaluate(struct player *pplayer, struct player *aplayer,
67 struct Treaty *ptreaty)
69 if (is_ai(pplayer)) {
70 CALL_PLR_AI_FUNC(treaty_evaluate, pplayer, pplayer, aplayer, ptreaty);
74 /**************************************************************************
75 Calls treaty_accepted function if such is set for AI player.
76 **************************************************************************/
77 static void call_treaty_accepted(struct player *pplayer, struct player *aplayer,
78 struct Treaty *ptreaty)
80 if (is_ai(pplayer)) {
81 CALL_PLR_AI_FUNC(treaty_accepted, pplayer, pplayer, aplayer, ptreaty);
85 /**************************************************************************
86 Initialize diplhand module
87 **************************************************************************/
88 void diplhand_init(void)
90 treaties = treaty_list_new();
93 /**************************************************************************
94 Free all the resources allocated by diplhand.
95 **************************************************************************/
96 void diplhand_free(void)
98 free_treaties();
100 treaty_list_destroy(treaties);
101 treaties = NULL;
104 /**************************************************************************
105 Free all the treaties currently in treaty list.
106 **************************************************************************/
107 void free_treaties(void)
109 /* Free memory allocated for treaties */
110 treaty_list_iterate(treaties, pt) {
111 clear_treaty(pt);
112 free(pt);
113 } treaty_list_iterate_end;
115 treaty_list_clear(treaties);
118 /**************************************************************************
119 Find currently active treaty between two players.
120 **************************************************************************/
121 struct Treaty *find_treaty(struct player *plr0, struct player *plr1)
123 treaty_list_iterate(treaties, ptreaty) {
124 if ((ptreaty->plr0 == plr0 && ptreaty->plr1 == plr1) ||
125 (ptreaty->plr0 == plr1 && ptreaty->plr1 == plr0)) {
126 return ptreaty;
128 } treaty_list_iterate_end;
130 return NULL;
133 /**************************************************************************
134 Return the closest of the two diplstate types.
135 **************************************************************************/
136 static enum diplstate_type dst_closest(enum diplstate_type a,
137 enum diplstate_type b)
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,
149 if (how_close[a] < how_close[b]) {
150 return b;
151 } else {
152 return a;
156 /**************************************************************************
157 pplayer clicked the accept button. If he accepted the treaty we check the
158 clauses. If both players have now accepted the treaty we execute the agreed
159 clauses.
160 **************************************************************************/
161 void handle_diplomacy_accept_treaty_req(struct player *pplayer,
162 int counterpart)
164 struct Treaty *ptreaty;
165 bool *player_accept, *other_accept;
166 enum dipl_reason diplcheck;
167 bool worker_refresh_required = FALSE;
168 struct player *pother = player_by_number(counterpart);
170 if (NULL == pother || pplayer == pother) {
171 return;
174 ptreaty = find_treaty(pplayer, pother);
176 if (!ptreaty) {
177 return;
180 if (ptreaty->plr0 == pplayer) {
181 player_accept = &ptreaty->accept0;
182 other_accept = &ptreaty->accept1;
183 } else {
184 player_accept = &ptreaty->accept1;
185 other_accept = &ptreaty->accept0;
188 if (!*player_accept) { /* Tries to accept. */
190 /* Check that player who accepts can keep what (s)he promises. */
192 clause_list_iterate(ptreaty->clauses, pclause) {
193 struct city *pcity = NULL;
195 if (pclause->from == pplayer || is_pact_clause(pclause->type)) {
196 switch(pclause->type) {
197 case CLAUSE_EMBASSY:
198 if (player_has_real_embassy(pother, pplayer)) {
199 log_error("%s tried to give embassy to %s, who already "
200 "has an embassy",
201 player_name(pplayer), player_name(pother));
202 return;
204 break;
205 case CLAUSE_ADVANCE:
206 if (!research_invention_gettable(research_get(pother),
207 pclause->value, game.info.tech_trade_allow_holes)) {
208 /* It is impossible to give a technology to a civilization that
209 * can not possess it (the client should enforce this). */
210 log_error("Treaty: %s can't have tech %s",
211 nation_rule_name(nation_of_player(pother)),
212 advance_rule_name(advance_by_number(pclause->value)));
213 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
214 _("The %s can't accept %s."),
215 nation_plural_for_player(pother),
216 advance_name_translation(advance_by_number
217 (pclause->value)));
218 return;
220 if (research_invention_state(research_get(pplayer), pclause->value)
221 != TECH_KNOWN) {
222 log_error("Nation %s try to give unknown tech %s to nation %s.",
223 nation_rule_name(nation_of_player(pplayer)),
224 advance_rule_name(advance_by_number(pclause->value)),
225 nation_rule_name(nation_of_player(pother)));
226 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
227 _("You don't have tech %s, you can't accept treaty."),
228 advance_name_translation(advance_by_number
229 (pclause->value)));
230 return;
232 break;
233 case CLAUSE_CITY:
234 pcity = game_city_by_number(pclause->value);
235 if (!pcity) { /* Can't find out cityname any more. */
236 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
237 _("City you are trying to give no longer exists, "
238 "you can't accept treaty."));
239 return;
241 if (city_owner(pcity) != pplayer) {
242 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
243 _("You are not owner of %s, you can't accept treaty."),
244 city_link(pcity));
245 return;
247 if (is_capital(pcity)) {
248 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
249 _("Your capital (%s) is requested, "
250 "you can't accept treaty."),
251 city_link(pcity));
252 return;
254 break;
255 case CLAUSE_CEASEFIRE:
256 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_CEASEFIRE);
257 if (diplcheck != DIPL_OK) {
258 return;
260 break;
261 case CLAUSE_PEACE:
262 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_PEACE);
263 if (diplcheck != DIPL_OK) {
264 return;
266 break;
267 case CLAUSE_ALLIANCE:
268 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_ALLIANCE);
269 if (diplcheck == DIPL_ALLIANCE_PROBLEM_US) {
270 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
271 _("You cannot form an alliance because you are "
272 "at war with an ally of %s."),
273 player_name(pother));
274 } else if (diplcheck == DIPL_ALLIANCE_PROBLEM_THEM) {
275 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
276 _("You cannot form an alliance because %s is "
277 "at war with an ally of yours."),
278 player_name(pother));
280 if (diplcheck != DIPL_OK) {
281 return;
283 break;
284 case CLAUSE_GOLD:
285 if (pplayer->economic.gold < pclause->value) {
286 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
287 _("You don't have enough gold, "
288 "you can't accept treaty."));
289 return;
291 break;
292 default:
293 ; /* nothing */
296 } clause_list_iterate_end;
299 *player_accept = ! *player_accept;
301 dlsend_packet_diplomacy_accept_treaty(pplayer->connections,
302 player_number(pother), *player_accept,
303 *other_accept);
304 dlsend_packet_diplomacy_accept_treaty(pother->connections,
305 player_number(pplayer), *other_accept,
306 *player_accept);
308 if (ptreaty->accept0 && ptreaty->accept1) {
309 int nclauses = clause_list_size(ptreaty->clauses);
311 dlsend_packet_diplomacy_cancel_meeting(pplayer->connections,
312 player_number(pother),
313 player_number(pplayer));
314 dlsend_packet_diplomacy_cancel_meeting(pother->connections,
315 player_number(pplayer),
316 player_number(pplayer));
318 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
319 PL_("A treaty containing %d clause was agreed upon.",
320 "A treaty containing %d clauses was agreed upon.",
321 nclauses),
322 nclauses);
323 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
324 PL_("A treaty containing %d clause was agreed upon.",
325 "A treaty containing %d clauses was agreed upon.",
326 nclauses),
327 nclauses);
329 /* Check that one who accepted treaty earlier still have everything
330 (s)he promised to give. */
332 clause_list_iterate(ptreaty->clauses, pclause) {
333 struct city *pcity;
334 if (pclause->from == pother) {
335 switch (pclause->type) {
336 case CLAUSE_CITY:
337 pcity = game_city_by_number(pclause->value);
338 if (!pcity) { /* Can't find out cityname any more. */
339 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
340 _("One of the cities the %s are giving away"
341 " is destroyed! Treaty canceled!"),
342 nation_plural_for_player(pother));
343 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
344 _("One of the cities the %s are giving away"
345 " is destroyed! Treaty canceled!"),
346 nation_plural_for_player(pother));
347 goto cleanup;
349 if (city_owner(pcity) != pother) {
350 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
351 _("The %s no longer control %s! "
352 "Treaty canceled!"),
353 nation_plural_for_player(pother),
354 city_link(pcity));
355 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
356 _("The %s no longer control %s! "
357 "Treaty canceled!"),
358 nation_plural_for_player(pother),
359 city_link(pcity));
360 goto cleanup;
362 if (is_capital(pcity)) {
363 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
364 _("Your capital (%s) is requested, "
365 "you can't accept treaty."),
366 city_link(pcity));
367 goto cleanup;
370 break;
371 case CLAUSE_ALLIANCE:
372 /* We need to recheck this way since things might have
373 * changed. */
374 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_ALLIANCE);
375 if (diplcheck != DIPL_OK) {
376 goto cleanup;
378 break;
379 case CLAUSE_PEACE:
380 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_PEACE);
381 if (diplcheck != DIPL_OK) {
382 goto cleanup;
384 break;
385 case CLAUSE_CEASEFIRE:
386 diplcheck = pplayer_can_make_treaty(pplayer, pother, DS_CEASEFIRE);
387 if (diplcheck != DIPL_OK) {
388 goto cleanup;
390 break;
391 case CLAUSE_GOLD:
392 if (pother->economic.gold < pclause->value) {
393 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
394 _("The %s don't have the promised amount "
395 "of gold! Treaty canceled!"),
396 nation_plural_for_player(pother));
397 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
398 _("The %s don't have the promised amount "
399 "of gold! Treaty canceled!"),
400 nation_plural_for_player(pother));
401 goto cleanup;
403 break;
404 default:
405 ; /* nothing */
408 } clause_list_iterate_end;
410 call_treaty_accepted(pplayer, pother, ptreaty);
411 call_treaty_accepted(pother, pplayer, ptreaty);
413 clause_list_iterate(ptreaty->clauses, pclause) {
414 struct player *pgiver = pclause->from;
415 struct player *pdest = (pplayer == pgiver) ? pother : pplayer;
416 struct player_diplstate *ds_giverdest
417 = player_diplstate_get(pgiver, pdest);
418 struct player_diplstate *ds_destgiver
419 = player_diplstate_get(pdest, pgiver);
420 enum diplstate_type old_diplstate = ds_giverdest->type;
421 struct unit_list *pgiver_seen_units BAD_HEURISTIC_INIT(NULL);
422 struct unit_list *pdest_seen_units BAD_HEURISTIC_INIT(NULL);
424 switch (pclause->type) {
425 case CLAUSE_EMBASSY:
426 establish_embassy(pdest, pgiver); /* sic */
427 notify_player(pgiver, NULL, E_TREATY_EMBASSY, ftc_server,
428 _("You gave an embassy to %s."),
429 player_name(pdest));
430 notify_player(pdest, NULL, E_TREATY_EMBASSY, ftc_server,
431 _("%s allowed you to create an embassy!"),
432 player_name(pgiver));
433 break;
434 case CLAUSE_ADVANCE:
436 /* It is possible that two players open the diplomacy dialog
437 * and try to give us the same tech at the same time. This
438 * should be handled discreetly instead of giving a core dump. */
439 struct research *presearch = research_get(pdest);
440 const char *advance_name;
442 if (research_invention_state(presearch, pclause->value)
443 == TECH_KNOWN) {
444 log_verbose("Nation %s already know tech %s, "
445 "that %s want to give them.",
446 nation_rule_name(nation_of_player(pdest)),
447 advance_rule_name(advance_by_number(pclause->value)),
448 nation_rule_name(nation_of_player(pgiver)));
449 break;
451 advance_name = advance_name_translation(advance_by_number
452 (pclause->value));
453 notify_player(pdest, NULL, E_TECH_GAIN, ftc_server,
454 _("You are taught the knowledge of %s."),
455 advance_name);
457 if (tech_transfer(pdest, pgiver, pclause->value)) {
458 char research_name[MAX_LEN_NAME * 2];
460 research_pretty_name(presearch, research_name,
461 sizeof(research_name));
462 notify_research(presearch, pdest, E_TECH_GAIN, ftc_server,
463 _("You have acquired %s thanks to the %s "
464 "diplomacy with the %s."),
465 advance_name,
466 nation_plural_for_player(pdest),
467 nation_plural_for_player(pgiver));
468 notify_research_embassies
469 (presearch, pgiver, E_TECH_EMBASSY, ftc_server,
470 /* TRANS: Tech from another player */
471 Q_("?fromplr:The %s have acquired %s from the %s."),
472 research_name,
473 advance_name,
474 nation_plural_for_player(pgiver));
476 script_tech_learned(presearch, pdest,
477 advance_by_number(pclause->value), "traded");
478 research_apply_penalty(presearch, pclause->value,
479 game.server.diplbulbcost);
480 found_new_tech(presearch, pclause->value, FALSE, TRUE);
483 break;
484 case CLAUSE_GOLD:
486 int received = pclause->value
487 * (100 - game.server.diplgoldcost) / 100;
488 pgiver->economic.gold -= pclause->value;
489 pdest->economic.gold += received;
490 notify_player(pdest, NULL, E_DIPLOMACY, ftc_server,
491 PL_("You get %d gold.",
492 "You get %d gold.", received), received);
494 break;
495 case CLAUSE_MAP:
496 give_map_from_player_to_player(pgiver, pdest);
497 notify_player(pdest, NULL, E_DIPLOMACY, ftc_server,
498 /* TRANS: ... Polish worldmap. */
499 _("You receive the %s worldmap."),
500 nation_adjective_for_player(pgiver));
502 worker_refresh_required = TRUE; /* See CLAUSE_VISION */
503 break;
504 case CLAUSE_SEAMAP:
505 give_seamap_from_player_to_player(pgiver, pdest);
506 notify_player(pdest, NULL, E_DIPLOMACY, ftc_server,
507 /* TRANS: ... Polish seamap. */
508 _("You receive the %s seamap."),
509 nation_adjective_for_player(pgiver));
511 worker_refresh_required = TRUE; /* See CLAUSE_VISION */
512 break;
513 case CLAUSE_CITY:
515 struct city *pcity = game_city_by_number(pclause->value);
517 if (!pcity) {
518 log_error("Treaty city id %d not found - skipping clause.",
519 pclause->value);
520 break;
523 notify_player(pdest, city_tile(pcity), E_CITY_TRANSFER, ftc_server,
524 _("You receive the city of %s from %s."),
525 city_link(pcity), player_name(pgiver));
527 notify_player(pgiver, city_tile(pcity), E_CITY_LOST, ftc_server,
528 _("You give the city of %s to %s."),
529 city_link(pcity), player_name(pdest));
531 if (transfer_city(pdest, pcity, -1, TRUE, TRUE, FALSE,
532 !is_barbarian(pdest))) {
533 script_server_signal_emit("city_transferred", 4,
534 API_TYPE_CITY, pcity,
535 API_TYPE_PLAYER, pgiver,
536 API_TYPE_PLAYER, pdest,
537 API_TYPE_STRING, "trade");
539 break;
541 case CLAUSE_CEASEFIRE:
542 if (old_diplstate == DS_ALLIANCE) {
543 pgiver_seen_units = get_units_seen_via_ally(pgiver, pdest);
544 pdest_seen_units = get_units_seen_via_ally(pdest, pgiver);
546 ds_giverdest->type = DS_CEASEFIRE;
547 ds_giverdest->turns_left = TURNS_LEFT;
548 ds_destgiver->type = DS_CEASEFIRE;
549 ds_destgiver->turns_left = TURNS_LEFT;
550 notify_player(pgiver, NULL, E_TREATY_CEASEFIRE, ftc_server,
551 _("You agree on a cease-fire with %s."),
552 player_name(pdest));
553 notify_player(pdest, NULL, E_TREATY_CEASEFIRE, ftc_server,
554 _("You agree on a cease-fire with %s."),
555 player_name(pgiver));
556 if (old_diplstate == DS_ALLIANCE) {
557 update_players_after_alliance_breakup(pgiver, pdest,
558 pgiver_seen_units,
559 pdest_seen_units);
560 unit_list_destroy(pgiver_seen_units);
561 unit_list_destroy(pdest_seen_units);
564 worker_refresh_required = TRUE;
565 break;
566 case CLAUSE_PEACE:
567 if (old_diplstate == DS_ALLIANCE) {
568 pgiver_seen_units = get_units_seen_via_ally(pgiver, pdest);
569 pdest_seen_units = get_units_seen_via_ally(pdest, pgiver);
571 ds_giverdest->type = DS_ARMISTICE;
572 ds_destgiver->type = DS_ARMISTICE;
573 ds_giverdest->turns_left = TURNS_LEFT;
574 ds_destgiver->turns_left = TURNS_LEFT;
575 ds_giverdest->max_state = dst_closest(DS_PEACE,
576 ds_giverdest->max_state);
577 ds_destgiver->max_state = dst_closest(DS_PEACE,
578 ds_destgiver->max_state);
579 notify_player(pgiver, NULL, E_TREATY_PEACE, ftc_server,
580 /* TRANS: ... the Poles ... Polish territory. */
581 PL_("You agree on an armistice with the %s. In %d turn, "
582 "it will become a peace treaty. Move your "
583 "units out of %s territory.",
584 "You agree on an armistice with the %s. In %d turns, "
585 "it will become a peace treaty. Move your "
586 "units out of %s territory.",
587 TURNS_LEFT),
588 nation_plural_for_player(pdest),
589 TURNS_LEFT,
590 nation_adjective_for_player(pdest));
591 notify_player(pdest, NULL, E_TREATY_PEACE, ftc_server,
592 /* TRANS: ... the Poles ... Polish territory. */
593 PL_("You agree on an armistice with the %s. In %d turn, "
594 "it will become a peace treaty. Move your "
595 "units out of %s territory.",
596 "You agree on an armistice with the %s. In %d turns, "
597 "it will become a peace treaty. Move your "
598 "units out of %s territory.",
599 TURNS_LEFT),
600 nation_plural_for_player(pgiver),
601 TURNS_LEFT,
602 nation_adjective_for_player(pgiver));
603 if (old_diplstate == DS_ALLIANCE) {
604 update_players_after_alliance_breakup(pgiver, pdest,
605 pgiver_seen_units,
606 pdest_seen_units);
607 unit_list_destroy(pgiver_seen_units);
608 unit_list_destroy(pdest_seen_units);
611 worker_refresh_required = TRUE;
612 break;
613 case CLAUSE_ALLIANCE:
614 ds_giverdest->type = DS_ALLIANCE;
615 ds_destgiver->type = DS_ALLIANCE;
616 ds_giverdest->max_state = dst_closest(DS_ALLIANCE,
617 ds_giverdest->max_state);
618 ds_destgiver->max_state = dst_closest(DS_ALLIANCE,
619 ds_destgiver->max_state);
620 notify_player(pgiver, NULL, E_TREATY_ALLIANCE, ftc_server,
621 _("You agree on an alliance with %s."),
622 player_name(pdest));
623 notify_player(pdest, NULL, E_TREATY_ALLIANCE, ftc_server,
624 _("You agree on an alliance with %s."),
625 player_name(pgiver));
626 give_allied_visibility(pgiver, pdest);
627 give_allied_visibility(pdest, pgiver);
629 worker_refresh_required = TRUE;
630 break;
631 case CLAUSE_VISION:
632 give_shared_vision(pgiver, pdest);
633 notify_player(pgiver, NULL, E_TREATY_SHARED_VISION, ftc_server,
634 _("You give shared vision to %s."),
635 player_name(pdest));
636 notify_player(pdest, NULL, E_TREATY_SHARED_VISION, ftc_server,
637 _("%s gives you shared vision."),
638 player_name(pgiver));
640 /* Yes, shared vision may let us to _know_ tiles
641 * within radius of our own city. */
642 worker_refresh_required = TRUE;
643 break;
646 } clause_list_iterate_end;
648 /* In theory, we would need refresh only receiving party of
649 * CLAUSE_MAP, CLAUSE_SEAMAP and CLAUSE_VISION clauses.
650 * It's quite unlikely that there is such a clause going one
651 * way but no clauses affecting both parties or going other
652 * way. */
653 if (worker_refresh_required) {
654 city_map_update_all_cities_for_player(pplayer);
655 city_map_update_all_cities_for_player(pother);
656 sync_cities();
659 cleanup:
660 treaty_list_remove(treaties, ptreaty);
661 clear_treaty(ptreaty);
662 free(ptreaty);
663 send_player_all_c(pplayer, NULL);
664 send_player_all_c(pother, NULL);
668 /****************************************************************************
669 Create an embassy. pplayer gets an embassy with aplayer.
670 ****************************************************************************/
671 void establish_embassy(struct player *pplayer, struct player *aplayer)
673 /* Establish the embassy. */
674 BV_SET(pplayer->real_embassy, player_index(aplayer));
675 send_player_all_c(pplayer, pplayer->connections);
676 /* update player dialog with embassy */
677 send_player_all_c(pplayer, aplayer->connections);
678 /* INFO_EMBASSY level info */
679 send_player_all_c(aplayer, pplayer->connections);
680 /* Send research info */
681 send_research_info(research_get(aplayer), pplayer->connections);
684 /**************************************************************************
685 Handle request to remove clause from treaty.
686 **************************************************************************/
687 void handle_diplomacy_remove_clause_req(struct player *pplayer,
688 int counterpart, int giver,
689 enum clause_type type, int value)
691 struct Treaty *ptreaty;
692 struct player *pgiver = player_by_number(giver);
693 struct player *pother = player_by_number(counterpart);
695 if (NULL == pother || pplayer == pother || NULL == pgiver) {
696 return;
699 if (pgiver != pplayer && pgiver != pother) {
700 return;
703 ptreaty = find_treaty(pplayer, pother);
705 if (ptreaty && remove_clause(ptreaty, pgiver, type, value)) {
706 dlsend_packet_diplomacy_remove_clause(pplayer->connections,
707 player_number(pother), giver, type,
708 value);
709 dlsend_packet_diplomacy_remove_clause(pother->connections,
710 player_number(pplayer), giver, type,
711 value);
712 call_treaty_evaluate(pplayer, pother, ptreaty);
713 call_treaty_evaluate(pother, pplayer, ptreaty);
717 /**************************************************************************
718 Handle request to add clause to treaty between two players.
719 **************************************************************************/
720 void handle_diplomacy_create_clause_req(struct player *pplayer,
721 int counterpart, int giver,
722 enum clause_type type, int value)
724 struct Treaty *ptreaty;
725 struct player *pgiver = player_by_number(giver);
726 struct player *pother = player_by_number(counterpart);
728 if (NULL == pother || pplayer == pother || NULL == pgiver) {
729 return;
732 if (pgiver != pplayer && pgiver != pother) {
733 return;
736 ptreaty = find_treaty(pplayer, pother);
738 if (ptreaty && add_clause(ptreaty, pgiver, type, value)) {
740 * If we are trading cities, then it is possible that the
741 * dest is unaware of it's existence. We have 2 choices,
742 * forbid it, or lighten that area. If we assume that
743 * the giver knows what they are doing, then 2. is the
744 * most powerful option - I'll choose that for now.
745 * - Kris Bubendorfer
747 if (type == CLAUSE_CITY) {
748 struct city *pcity = game_city_by_number(value);
750 if (pcity && !map_is_known_and_seen(pcity->tile, pother, V_MAIN))
751 give_citymap_from_player_to_player(pcity, pplayer, pother);
754 dlsend_packet_diplomacy_create_clause(pplayer->connections,
755 player_number(pother), giver, type,
756 value);
757 dlsend_packet_diplomacy_create_clause(pother->connections,
758 player_number(pplayer), giver, type,
759 value);
760 call_treaty_evaluate(pplayer, pother, ptreaty);
761 call_treaty_evaluate(pother, pplayer, ptreaty);
765 /**************************************************************************
766 Cancel meeting. No sanity checking of input parameters, so don't call
767 this with input directly from untrusted source.
768 **************************************************************************/
769 static void really_diplomacy_cancel_meeting(struct player *pplayer,
770 struct player *pother)
772 struct Treaty *ptreaty = find_treaty(pplayer, pother);
774 if (ptreaty) {
775 dlsend_packet_diplomacy_cancel_meeting(pother->connections,
776 player_number(pplayer),
777 player_number(pplayer));
778 notify_player(pother, NULL, E_DIPLOMACY, ftc_server,
779 _("%s canceled the meeting!"),
780 player_name(pplayer));
781 /* Need to send to pplayer too, for multi-connects: */
782 dlsend_packet_diplomacy_cancel_meeting(pplayer->connections,
783 player_number(pother),
784 player_number(pplayer));
785 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
786 _("Meeting with %s canceled."),
787 player_name(pother));
788 treaty_list_remove(treaties, ptreaty);
789 clear_treaty(ptreaty);
790 free(ptreaty);
794 /**************************************************************************
795 Handle meeting cancelling request.
796 **************************************************************************/
797 void handle_diplomacy_cancel_meeting_req(struct player *pplayer,
798 int counterpart)
800 struct player *pother = player_by_number(counterpart);
802 if (NULL == pother || pplayer == pother) {
803 return;
806 really_diplomacy_cancel_meeting(pplayer, pother);
809 /**************************************************************************
810 Handle meeting opening request.
811 **************************************************************************/
812 void handle_diplomacy_init_meeting_req(struct player *pplayer,
813 int counterpart)
815 struct player *pother = player_by_number(counterpart);
817 if (NULL == pother || pplayer == pother) {
818 return;
821 if (find_treaty(pplayer, pother)) {
822 return;
825 if (get_player_bonus(pplayer, EFT_NO_DIPLOMACY) > 0
826 || get_player_bonus(pother, EFT_NO_DIPLOMACY) > 0) {
827 notify_player(pplayer, NULL, E_DIPLOMACY, ftc_server,
828 _("Your diplomatic envoy was decapitated!"));
829 return;
832 if (could_meet_with_player(pplayer, pother)) {
833 struct Treaty *ptreaty;
835 ptreaty = fc_malloc(sizeof(*ptreaty));
836 init_treaty(ptreaty, pplayer, pother);
837 treaty_list_prepend(treaties, ptreaty);
839 dlsend_packet_diplomacy_init_meeting(pplayer->connections,
840 player_number(pother),
841 player_number(pplayer));
842 dlsend_packet_diplomacy_init_meeting(pother->connections,
843 player_number(pplayer),
844 player_number(pplayer));
848 /**************************************************************************
849 Send information on any on-going diplomatic meetings for connection's
850 player. For re-connections.
851 **************************************************************************/
852 void send_diplomatic_meetings(struct connection *dest)
854 struct player *pplayer = dest->playing;
856 if (!pplayer) {
857 return;
859 players_iterate(other) {
860 struct Treaty *ptreaty = find_treaty(pplayer, other);
862 if (ptreaty) {
863 fc_assert_action(pplayer != other, continue);
864 dsend_packet_diplomacy_init_meeting(dest, player_number(other),
865 player_number(pplayer));
866 clause_list_iterate(ptreaty->clauses, pclause) {
867 dsend_packet_diplomacy_create_clause(dest,
868 player_number(other),
869 player_number(pclause->from),
870 pclause->type,
871 pclause->value);
872 } clause_list_iterate_end;
874 if (ptreaty->plr0 == pplayer) {
875 dsend_packet_diplomacy_accept_treaty(dest, player_number(other),
876 ptreaty->accept0,
877 ptreaty->accept1);
878 } else {
879 dsend_packet_diplomacy_accept_treaty(dest, player_number(other),
880 ptreaty->accept1,
881 ptreaty->accept0);
884 } players_iterate_end;
887 /**************************************************************************
888 Cancels all meetings of player.
889 **************************************************************************/
890 void cancel_all_meetings(struct player *pplayer)
892 players_iterate(pplayer2) {
893 if (find_treaty(pplayer, pplayer2)) {
894 really_diplomacy_cancel_meeting(pplayer, pplayer2);
896 } players_iterate_end;
899 /**************************************************************************
900 Reject all treaties currently being negotiated
901 **************************************************************************/
902 void reject_all_treaties(struct player *pplayer)
904 struct Treaty* treaty;
905 players_iterate(pplayer2) {
906 treaty = find_treaty(pplayer, pplayer2);
907 if (!treaty) {
908 continue;
910 treaty->accept0 = FALSE;
911 treaty->accept1 = FALSE;
912 dlsend_packet_diplomacy_accept_treaty(pplayer->connections,
913 player_number(pplayer2),
914 FALSE,
915 FALSE);
916 dlsend_packet_diplomacy_accept_treaty(pplayer2->connections,
917 player_number(pplayer),
918 FALSE,
919 FALSE);
920 } players_iterate_end;
923 /**************************************************************************
924 Get treaty list
925 **************************************************************************/
926 struct treaty_list *get_all_treaties(void)
928 return treaties;