Freeciv-3.2: /fast/freeciv/nightly/S3_2/server/diplhand.c Source File

Freeciv-3.2

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