Freeciv-3.3: /fast/freeciv/nightly/main/server/diplhand.c Source File

Freeciv-3.3

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