Freeciv-3.4
Loading...
Searching...
No Matches
daidiplomacy.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Team
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 <stdarg.h>
19#include <string.h>
20
21/* utility */
22#include "fcintl.h"
23#include "log.h"
24#include "mem.h"
25#include "rand.h"
26#include "shared.h"
27#include "support.h"
28
29/* common */
30#include "aisupport.h"
31#include "city.h"
32#include "diptreaty.h"
33#include "events.h"
34#include "game.h"
35#include "packets.h"
36#include "player.h"
37#include "nation.h"
38#include "research.h"
39#include "spaceship.h"
40#include "tech.h"
41#include "unitlist.h"
42
43/* server */
44#include "citytools.h"
45#include "diplhand.h"
46#include "maphand.h"
47#include "notify.h"
48#include "srv_log.h"
49
50/* server/advisors */
51#include "advdata.h"
52#include "advtools.h"
53
54/* ai */
55#include "aitraits.h"
56#include "handicaps.h"
57
58/* ai/default */
59#include "daicity.h"
60#include "daidata.h"
61#include "dailog.h"
62#include "daimilitary.h"
63#include "daiplayer.h"
64#include "daiunit.h"
65
66#include "daidiplomacy.h"
67
68#define LOG_DIPL LOG_DEBUG
69#define LOG_DIPL2 LOG_DEBUG
70
71/* One hundred thousand. Basically a number of gold that no player is
72 * ever likely to have, but not so big that we get integer overflows. */
73#define BIG_NUMBER 100000
74
75/* turn this off when we don't want functions to message players */
76static bool diplomacy_verbose = TRUE;
77
78/* turns to wait after contact before taking aim for war */
79#define TURNS_BEFORE_TARGET 15
80
81static void dai_incident_war(struct player *violator, struct player *victim);
82static void dai_incident_simple(struct player *receiver,
83 const struct player *violator,
84 const struct player *victim,
86 int how_bad);
87static void dai_incident_nuclear(struct player *receiver,
88 const struct player *violator,
89 const struct player *victim);
90static void dai_incident_nuclear_not_target(struct player *receiver,
91 const struct player *violator,
92 const struct player *victim);
93static void dai_incident_nuclear_self(struct player *receiver,
94 const struct player *violator,
95 const struct player *victim);
96static void clear_old_treaty(struct player *pplayer, struct player *aplayer);
97
98/******************************************************************/
103static void dai_diplo_notify(struct player *pplayer, const char *text, ...)
104{
105 if (diplomacy_verbose) {
106 va_list ap;
107 struct conn_list *dest = pplayer->connections;
108 struct packet_chat_msg packet;
109
110 va_start(ap, text);
112 text, ap);
113 va_end(ap);
114
115 lsend_packet_chat_msg(dest, &packet);
116 /* Add to the event cache. */
117 event_cache_add_for_player(&packet, pplayer);
118 }
119}
120
121/******************************************************************/
125static int greed(int missing_love)
126{
127 if (missing_love > 0) {
128 return 0;
129 } else {
130 /* Don't change the operation order here.
131 * We do not want integer overflows */
132 return -((missing_love * MAX_AI_LOVE) / 1000) *
133 ((missing_love * MAX_AI_LOVE) / 1000) / 10;
134 }
135}
136
137/******************************************************************/
141{
142 switch (type) {
143 case CLAUSE_ALLIANCE:
144 return DS_ALLIANCE;
145 case CLAUSE_PEACE:
146 return DS_PEACE;
147 case CLAUSE_CEASEFIRE:
148 return DS_CEASEFIRE;
149 default:
150 log_error("Invalid diplomatic clause %d.", type)
151 return DS_WAR;
152 }
153}
154
155/******************************************************************/
158static int dai_goldequiv_tech(struct ai_type *ait,
159 struct player *pplayer, Tech_type_id tech)
160{
161 int bulbs, tech_want, worth;
162 struct research *presearch = research_get(pplayer);
164 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
165
166 if (TECH_KNOWN == state
169 return 0;
170 }
172 tech_want = MAX(plr_data->tech_want[tech], 0) / MAX(game.info.turn, 1);
173 worth = bulbs + tech_want;
174 if (TECH_PREREQS_KNOWN == state) {
175 worth /= 2;
176 }
177
178 return worth;
179}
180
181/******************************************************************/
185static bool shared_vision_is_safe(struct player* pplayer,
186 struct player* aplayer)
187{
188 if (pplayer->team && pplayer->team == aplayer->team) {
189 return TRUE;
190 }
192 if (eplayer == pplayer || eplayer == aplayer) {
193 continue;
194 }
197
198 if (ds != DS_NO_CONTACT && ds != DS_ALLIANCE) {
199 return FALSE;
200 }
201 }
203
204 return TRUE;
205}
206
207/******************************************************************/
212 struct player* player1,
213 struct player* player2)
214{
215 return (player1->ai_common.love[player_index(player2)] > - (MAX_AI_LOVE * 4 / 10)
216 && dai_diplomacy_get(ait, player1, player2)->countdown == -1);
217}
218
219/******************************************************************/
226static int compute_tech_sell_price(struct ai_type *ait,
227 struct player *giver, struct player *taker,
228 int tech_id, bool *is_dangerous)
229{
230 int worth;
231
232 worth = dai_goldequiv_tech(ait, taker, tech_id);
233
234 *is_dangerous = FALSE;
235
236 /* Share and expect being shared brotherly between allies */
237 if (pplayers_allied(giver, taker)) {
238 worth /= 2;
239 }
240 if (players_on_same_team(giver, taker)) {
241 return 0;
242 }
243
244 /* Do not bother wanting a tech that we already have. */
246 == TECH_KNOWN) {
247 return 0;
248 }
249
250 /* Calculate in tech leak to our opponents, guess 50% chance */
252 if (eplayer == giver
253 || eplayer == taker
255 tech_id) == TECH_KNOWN) {
256 continue;
257 }
258
259 /* Don't risk it falling into enemy hands */
261 && adv_is_player_dangerous(giver, eplayer)) {
262 *is_dangerous = TRUE;
263 }
264
266 && !pplayers_allied(giver, eplayer)) {
267 /* Taker can enrich their side with this tech */
268 worth += dai_goldequiv_tech(ait, eplayer, tech_id) / 4;
269 }
271
272 return worth;
273}
274
275/******************************************************************/
278static const struct player *
280 const struct player *them)
281{
283 if (aplayer != us
284 && aplayer != them
287 return aplayer;
288 }
290
291 return nullptr;
292}
293
294/******************************************************************/
301static int dai_goldequiv_clause(struct ai_type *ait,
302 struct player *pplayer,
303 struct player *aplayer,
304 struct Clause *pclause,
305 bool verbose,
307{
308 bool close_here;
309 struct ai_plr *ai;
310 int worth = 0; /* Worth for pplayer of what aplayer gives */
311 bool give = (pplayer == pclause->from);
312 struct player *giver;
313 const struct player *penemy;
314 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
315 bool is_dangerous;
316 const struct civ_map *nmap = &(wld.map);
317
318 ai = dai_plr_data_get(ait, pplayer, &close_here);
319
320 fc_assert_ret_val(pplayer != aplayer, 0);
321
322 diplomacy_verbose = verbose;
324 giver = pclause->from;
325
326 switch (pclause->type) {
327 case CLAUSE_ADVANCE:
328 if (give) {
329 worth -= compute_tech_sell_price(ait, pplayer, aplayer, pclause->value,
330 &is_dangerous);
331 if (is_dangerous) {
332 worth = -BIG_NUMBER;
333 }
334 } else if (research_invention_state(research_get(pplayer),
335 pclause->value) != TECH_KNOWN) {
336 worth += compute_tech_sell_price(ait, aplayer, pplayer, pclause->value,
337 &is_dangerous);
338
340 /* Consider the upkeep costs! Thus, one can not get an AI player by
341 * - given AI lots of techs for gold/cities etc.
342 * - AI losses tech due to high upkeep.
343 * FIXME: Is there a better way for this? */
344 struct research *research = research_get(pplayer);
345 int limit;
346
347 /* Make sure there's no division by zero */
348 if (research->techs_researched > 0) {
349 int upk_per_tech = player_tech_upkeep(pplayer)
351
352 limit = MAX(1, upk_per_tech);
353 } else {
354 limit = 1;
355 fc_assert(player_tech_upkeep(pplayer) == 0);
356 }
357
358 if (pplayer->server.bulbs_last_turn < limit) {
359 if (research->bulbs_researched < 0) {
360 worth = -BIG_NUMBER;
361 } else {
362 worth /= 2;
363 }
364 }
365 }
366 }
367 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "%s clause worth %d",
369 break;
370
371 case CLAUSE_ALLIANCE:
372 case CLAUSE_PEACE:
373 case CLAUSE_CEASEFIRE:
374 /* Don't do anything in away mode */
375 if (has_handicap(pplayer, H_AWAY)) {
377 _("*%s (AI)* In away mode AI can't sign such a treaty."),
378 player_name(pplayer));
379 worth = -BIG_NUMBER;
380 break;
381 }
382
383 /* This guy is allied to one of our enemies. Only accept
384 * ceasefire. */
386 && pclause->type != CLAUSE_CEASEFIRE) {
387 dai_diplo_notify(aplayer, _("*%s (AI)* First break alliance with %s, %s."),
388 player_name(pplayer), player_name(penemy),
390 worth = -BIG_NUMBER;
391 break;
392 }
393
394 /* They were allied with an enemy at the beginning of the turn. */
395 if (adip->is_allied_with_enemy
396 && pclause->type != CLAUSE_CEASEFIRE) {
397 dai_diplo_notify(aplayer, _("*%s (AI)* I would like to see you keep your "
398 "distance from %s for some time, %s."),
399 player_name(pplayer),
400 player_name(adip->is_allied_with_enemy),
402 worth = -BIG_NUMBER;
403 break;
404 }
405
406 /* Steps of the treaty ladder */
407 if (pclause->type == CLAUSE_PEACE) {
408 const struct player_diplstate *ds
409 = player_diplstate_get(pplayer, aplayer);
410
411 if (!pplayers_non_attack(pplayer, aplayer)) {
413 _("*%s (AI)* Let us first cease hostilities, %s."),
414 player_name(pplayer),
416 worth = -BIG_NUMBER;
417 } else if (ds->type == DS_CEASEFIRE && ds->turns_left > 4) {
419 _("*%s (AI)* I wish to see you keep the current "
420 "ceasefire for a bit longer first, %s."),
421 player_name(pplayer),
423 worth = -BIG_NUMBER;
424 } else if (adip->countdown >= 0 || adip->countdown < -1) {
425 worth = -BIG_NUMBER; /* But say nothing */
426 } else {
427 worth = greed(pplayer->ai_common.love[player_index(aplayer)]
429 }
430 } else if (pclause->type == CLAUSE_ALLIANCE) {
431 if (!pplayers_in_peace(pplayer, aplayer)) {
432 worth = greed(pplayer->ai_common.love[player_index(aplayer)]
434 }
435 if (adip->countdown >= 0 || adip->countdown < -1) {
436 worth = -BIG_NUMBER; /* But say nothing */
437 } else {
438 worth += greed(pplayer->ai_common.love[player_index(aplayer)]
440 }
441 if (pplayer->ai_common.love[player_index(aplayer)] < MAX_AI_LOVE / 10) {
442 dai_diplo_notify(aplayer, _("*%s (AI)* I simply do not trust you with an "
443 "alliance yet, %s."),
444 player_name(pplayer),
446 worth = -BIG_NUMBER;
447 }
448 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "ally clause worth %d", worth);
449 } else {
450 if (is_ai(pplayer) && is_ai(aplayer)
452 worth = 0;
453 } else {
454 int turns = game.info.turn;
455
457 if (turns < TURNS_BEFORE_TARGET) {
458 worth = 0; /* Show some good faith */
459 break;
460 } else {
461 worth = greed(pplayer->ai_common.love[player_index(aplayer)]);
462 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "ceasefire worth=%d love=%d "
463 "turns=%d", worth,
465 turns);
466 }
467 }
468 }
469
470 /* Let's all hold hands in one happy family! */
471 if (adip->is_allied_with_ally) {
472 worth /= 2;
473 break;
474 }
475
476 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "treaty clause worth %d", worth);
477 break;
478
479 case CLAUSE_GOLD:
480 if (give) {
481 worth -= pclause->value;
482 } else {
483 worth += pclause->value * (100 - game.server.diplgoldcost) / 100;
484 }
485 break;
486
487 case CLAUSE_SEAMAP:
488 if (!give || ds_after == DS_ALLIANCE) {
489 /* Useless to us - we're omniscient! And allies get it for free! */
490 worth = 0;
491 } else {
492 /* Very silly algorithm 1: Sea map more worth if enemy has more
493 cities. Reasoning is they have more use of seamap for settling
494 new areas the more cities they have already. */
495 worth -= 15 * city_list_size(aplayer->cities);
496 /* Don't like them? Don't give it to them! */
497 worth = MIN(pplayer->ai_common.love[player_index(aplayer)] * 7, worth);
498 /* Make maps from novice player cheap */
499 if (has_handicap(pplayer, H_DIPLOMACY)) {
500 worth /= 2;
501 }
502 }
503 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "seamap clause worth %d",
504 worth);
505 break;
506
507 case CLAUSE_MAP:
508 if (!give || ds_after == DS_ALLIANCE) {
509 /* Useless to us - we're omniscient! And allies get it for free! */
510 worth = 0;
511 } else {
512 /* Very silly algorithm 2: Land map more worth the more cities
513 we have, since we expose all of these to the enemy. */
514 worth -= 40 * MAX(city_list_size(pplayer->cities), 1);
515 /* Inflate numbers if not peace */
516 if (!pplayers_in_peace(pplayer, aplayer)) {
517 worth *= 2;
518 }
519 /* Don't like them? Don't give it to them! */
520 worth = MIN(pplayer->ai_common.love[player_index(aplayer)] * 10, worth);
521 /* Make maps from novice player cheap */
522 if (has_handicap(pplayer, H_DIPLOMACY)) {
523 worth /= 6;
524 }
525 }
526 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "landmap clause worth %d",
527 worth);
528 break;
529
530 case CLAUSE_CITY: {
531 struct city *offer = city_list_find_number(pclause->from->cities,
532 pclause->value);
533
534 if (!offer || city_owner(offer) != giver) {
535 /* City destroyed or taken during negotiations */
537 _("*%s (AI)* I do not know the city you mention."),
538 player_name(pplayer));
539 worth = 0;
540 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer,
541 "city destroyed during negotiations");
542 } else if (give) {
543 /* AI must be crazy to trade away its cities */
544 worth -= city_gold_worth(nmap, offer);
545 if (is_capital(offer)) {
546 worth = -BIG_NUMBER; /* Never! Ever! */
547 } else {
548 worth *= 15;
549 }
550 if (aplayer == offer->original) {
551 /* Let them buy back their own city cheaper. */
552 worth /= 2;
553 }
554 } else {
555 worth = city_gold_worth(nmap, offer);
556 }
557 if (offer != nullptr) {
558 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "worth of %s is %d",
559 city_name_get(offer), worth);
560 }
561 break;
562 }
563
564 case CLAUSE_VISION:
565 if (give) {
566 if (ds_after == DS_ALLIANCE) {
567 if (!shared_vision_is_safe(pplayer, aplayer)) {
569 _("*%s (AI)* Sorry, sharing vision with you "
570 "is not safe."),
571 player_name(pplayer));
572 worth = -BIG_NUMBER;
573 } else {
574 worth = 0;
575 }
576 } else {
577 /* So out of the question */
578 worth = -BIG_NUMBER;
579 }
580 } else {
581 worth = 0; /* We are omniscient, so... */
582 }
583 break;
584
585 case CLAUSE_EMBASSY:
586 if (give) {
587 if (ds_after == DS_ALLIANCE) {
588 worth = 0;
589 } else if (ds_after == DS_PEACE) {
590 worth = -5 * game.info.turn;
591 } else {
592 worth = MIN(-50 * game.info.turn
593 + pplayer->ai_common.love[player_index(aplayer)],
594 -5 * game.info.turn);
595 }
597 worth = game.info.tech_leak_pct / 10;
598 } else {
599 worth = 0; /* We don't need no stinkin' embassy, do we? */
600 }
601 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "embassy clause worth %d",
602 worth);
603 break;
605 if (give) {
606 if (ds_after == DS_ALLIANCE) {
607 worth = 0;
608 } else {
609 /* Here we assume that number of cities correlate with number of tiles. */
610 worth = -3 * (city_list_size(pplayer->cities) + 1);
611 }
612 } else {
613 int cities = city_list_size(aplayer->cities);
614
615 worth = MIN(cities, 3);
616 }
617 break;
618 case CLAUSE_COUNT:
620 break;
621 } /* End of switch */
622
623 if (close_here) {
624 dai_data_phase_finished(ait, pplayer);
625 }
626
628
629 return worth;
630}
631
632/******************************************************************/
636void dai_treaty_evaluate(struct ai_type *ait, struct player *pplayer,
637 struct player *aplayer, struct treaty *ptreaty)
638{
639 int total_balance = 0;
640 bool only_gifts = TRUE;
642 = player_diplstate_get(pplayer, aplayer)->type;
643 int given_cities = 0;
644
646 if (is_pact_clause(pclause->type)) {
648 }
649 if (pclause->type == CLAUSE_CITY && pclause->from == pplayer) {
650 given_cities++;
651 }
653
654 /* Evaluate clauses */
656 const struct research *presearch = research_get(pplayer);
657
660
661 if (pclause->type != CLAUSE_GOLD && pclause->type != CLAUSE_MAP
662 && pclause->type != CLAUSE_SEAMAP && pclause->type != CLAUSE_VISION
663 && (pclause->type != CLAUSE_ADVANCE
665 || pclause->value == research_get(pplayer)->tech_goal
666 || pclause->value == research_get(pplayer)->researching
668 pclause->value))) {
669 /* We accept the above list of clauses as gifts, even if we are
670 * at war. We do not accept tech or cities since these can be used
671 * against us, unless we know that we want this tech anyway, or
672 * it doesn't matter due to tech cost style. */
674 }
676
677 /* If we are at war, and no peace is offered, then no deal, unless
678 * it is just gifts, in which case we gratefully accept. */
679 if (ds_after == DS_WAR && !only_gifts) {
680 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "no peace offered, must refuse");
681 return;
682 }
683
684 if (given_cities > 0
685 && city_list_size(pplayer->cities) - given_cities <= 2) {
686 /* always keep at least two cities */
687 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "cannot give last cities");
688 return;
689 }
690
691 /* Accept if balance is good */
692 if (total_balance >= 0) {
694 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "balance was good: %d",
696 } else {
697 /* AI complains about the treaty which was proposed, unless the AI
698 * made the proposal. */
699 if (pplayer != ptreaty->plr0) {
701 _("*%s (AI)* This deal was not very good for us, %s!"),
702 player_name(pplayer),
704 }
705 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "balance was bad: %d",
707 }
708}
709
710/******************************************************************/
714static void dai_treaty_react(struct ai_type *ait,
715 struct player *pplayer,
716 struct player *aplayer,
717 struct Clause *pclause)
718{
719 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
720
721 switch (pclause->type) {
722 case CLAUSE_ALLIANCE:
723 if (adip->is_allied_with_ally) {
724 dai_diplo_notify(aplayer, _("*%s (AI)* Welcome into our alliance %s!"),
725 player_name(pplayer),
727 } else {
728 dai_diplo_notify(aplayer, _("*%s (AI)* Yes, may we forever stand united, %s."),
729 player_name(pplayer),
731 }
732 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "become allies");
733 break;
734 case CLAUSE_PEACE:
735 dai_diplo_notify(aplayer, _("*%s (AI)* Yes, peace in our time!"),
736 player_name(pplayer));
737 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "sign peace treaty");
738 break;
739 case CLAUSE_CEASEFIRE:
740 dai_diplo_notify(aplayer, _("*%s (AI)* Agreed. No more hostilities, %s."),
741 player_name(pplayer),
743 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "sign ceasefire");
744 break;
745 default:
746 break;
747 }
748}
749
750/******************************************************************/
757void dai_treaty_accepted(struct ai_type *ait, struct player *pplayer,
758 struct player *aplayer, struct treaty *ptreaty)
759{
760 bool close_here;
761 struct ai_plr *ai;
762 int total_balance = 0;
763 bool gift = TRUE;
765 = player_diplstate_get(pplayer, aplayer)->type;
766
767 ai = dai_plr_data_get(ait, pplayer, &close_here);
768
769 fc_assert_ret(pplayer != aplayer);
770
772 if (is_pact_clause(pclause->type)) {
774 }
776
777 /* Evaluate clauses */
779 int balance
781
783 gift = (gift && (balance >= 0));
784 dai_treaty_react(ait, pplayer, aplayer, pclause);
785 if (is_pact_clause(pclause->type)
786 && dai_diplomacy_get(ait, pplayer, aplayer)->countdown != -1) {
787 /* Cancel a countdown towards war if we just agreed to peace... */
788 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "countdown nullified");
789 dai_diplomacy_get(ait, pplayer, aplayer)->countdown = -1;
790 }
792
793 /* Rather arbitrary algorithm to increase our love for a player if
794 * they offer us gifts. It is only a gift if _all_ the clauses
795 * are beneficial to us. */
796 if (total_balance > 0 && gift) {
797 int i = total_balance / ((city_list_size(pplayer->cities) * 10) + 1);
798
799 i = MIN(i, ai->diplomacy.love_incr * 150) * 10;
800 pplayer->ai_common.love[player_index(aplayer)] += i;
801 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "gift increased love by %d", i);
802 }
803
804 if (close_here) {
805 dai_data_phase_finished(ait, pplayer);
806 }
807}
808
809/******************************************************************/
816static int dai_war_desire(struct ai_type *ait, struct player *pplayer,
817 struct player *target)
818{
819 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
820 struct adv_data *adv = adv_data_get(pplayer, nullptr);
821 int want = 0, fear = 0, distance = 0, settlers = 0, cities = 0;
822 struct player_spaceship *ship = &target->spaceship;
823
824 city_list_iterate(target->cities, pcity) {
825 want += 100; /* Base city want */
826 want += city_size_get(pcity) * 20;
827 want += pcity->surplus[O_SHIELD] * 8;
828 want += pcity->surplus[O_TRADE] * 6;
829
830 /* FIXME: This might be buggy if it ignores unmet UnitClass reqs. */
832
833 city_built_iterate(pcity, pimprove) {
834 int cost = impr_build_shield_cost(pcity, pimprove);
835
836 want += cost;
837 if (improvement_obsolete(pplayer, pimprove, pcity)) {
838 continue;
839 }
840 if (is_great_wonder(pimprove)) {
841 want += cost * 2;
842 } else if (is_small_wonder(pimprove)) {
843 want += cost;
844 }
847 unit_list_iterate(target->units, punit) {
848 const struct unit_type *ptype = unit_type_get(punit);
849
851
852 /* Fear enemy expansionism */
854 want += 100;
855 }
857 unit_list_iterate(pplayer->units, punit) {
858 const struct unit_type *ptype = unit_type_get(punit);
859
860 fear -= ATTACK_POWER(ptype) / 2;
861
862 /* Our own expansionism reduces want for war */
864 want -= 200;
865 settlers++;
866 }
868 city_list_iterate(pplayer->cities, pcity) {
869 if (VUT_UTYPE == pcity->production.kind
870 && utype_is_cityfounder(pcity->production.value.utype)) {
871 want -= 150;
872 settlers++;
873 }
874 cities++;
876
877 /* Modify by settler/cities ratio to prevent early wars when
878 * we should be expanding. This will eliminate want if we
879 * produce settlers in all cities (ie full expansion). */
880 want -= abs(want) / MAX(cities - settlers, 1);
881
882 /* Calculate average distances to other player's empire. */
883 distance = player_distance_to_player(pplayer, target);
884 dai_diplomacy_get(ait, pplayer, target)->distance = distance;
885
886 /* Worry a bit if the other player has extreme amounts of wealth
887 * that can be used in cities to quickly buy an army. */
888 fear += (target->economic.gold / 5000) * city_list_size(target->cities);
889
890 /* Tech lead is worrisome. FIXME: Only consider 'military' techs. */
891 fear += MAX(research_get(target)->techs_researched
892 - research_get(pplayer)->techs_researched, 0) * 100;
893
894 /* Spacerace loss we will not allow! */
895 if (ship->state >= SSHIP_STARTED) {
896 want *= 2;
897 }
898 if (adv->dipl.spacerace_leader == target) {
900
901 return BIG_NUMBER; /* Do NOT amortize this number! */
902 }
903
904 /* Modify by which treaties we would break to other players, and what
905 * excuses we have to do so. FIXME: We only consider immediate
906 * allies, but we might trigger a wider chain reaction. */
908 bool cancel_excuse =
911
912 if (eplayer == pplayer) {
913 continue;
914 }
915
916 /* Remember: pplayers_allied() returns true when target == eplayer */
917 if (!cancel_excuse && pplayers_allied(target, eplayer)) {
918 if (ds == DS_ARMISTICE) {
919 want -= abs(want) / 10; /* 10% off */
920 } else if (ds == DS_CEASEFIRE) {
921 want -= abs(want) / 7; /* 15% off */
922 } else if (ds == DS_PEACE) {
923 want -= abs(want) / 5; /* 20% off */
924 } else if (ds == DS_ALLIANCE) {
925 want -= abs(want) / 3; /* 33% off */
926 }
927 }
929
930 /* Modify by love. Increase the divisor to make ai go to war earlier */
931 want -= MAX(0, want * pplayer->ai_common.love[player_index(target)]
932 / (2 * MAX_AI_LOVE));
933
934 /* Make novice AI more peaceful with human players */
935 if (has_handicap(pplayer, H_DIPLOMACY) && is_human(target)) {
936 want /= 2;
937 }
938
939 /* Amortize by distance */
940 want = amortize(want, distance);
941
942 if (pplayers_allied(pplayer, target)) {
943 want /= 4;
944 }
945
946 DIPLO_LOG(ait, LOG_DEBUG, pplayer, target, "War want %d, war fear %d",
947 want, fear);
948 return (want - fear);
949}
950
951/******************************************************************/
956static void dai_diplomacy_suggest(struct player *pplayer,
957 struct player *aplayer,
958 enum clause_type what,
959 bool to_pplayer,
960 int value)
961{
962 if (!could_meet_with_player(pplayer, aplayer)) {
963 log_base(LOG_DIPL2, "%s tries to do diplomacy to %s without contact",
964 player_name(pplayer), player_name(aplayer));
965 return;
966 }
967
971 : pplayer),
972 what, value);
973}
974
975/******************************************************************/
978void dai_diplomacy_first_contact(struct ai_type *ait, struct player *pplayer,
979 struct player *aplayer)
980{
981 bool wants_ceasefire = FALSE;
982
983 /* Randomize initial love */
984 pplayer->ai_common.love[player_index(aplayer)] += 2 - fc_rand(5);
985
986 if (is_ai(pplayer)
987 && player_diplstate_get(pplayer, aplayer)->type == DS_WAR
988 && could_meet_with_player(pplayer, aplayer)) {
989 if (has_handicap(pplayer, H_CEASEFIRE)) {
990 fc_assert(!has_handicap(pplayer, H_AWAY));
992 } else if (!has_handicap(pplayer, H_AWAY)) {
993 struct Clause clause;
994
995 clause.from = pplayer;
996 clause.value = 0;
997 clause.type = CLAUSE_CEASEFIRE;
998
999 if (dai_goldequiv_clause(ait, pplayer, aplayer, &clause,
1000 FALSE, DS_CEASEFIRE) > 0) {
1002 }
1003 }
1004 }
1005
1006 if (wants_ceasefire) {
1008 _("*%s (AI)* Greetings %s! May we suggest a ceasefire "
1009 "while we get to know each other better?"),
1010 player_name(pplayer),
1012 clear_old_treaty(pplayer, aplayer);
1014 } else {
1016 _("*%s (AI)* I found you %s! Now make it worth my "
1017 "letting you live, or be crushed."),
1018 player_name(pplayer),
1020 }
1021}
1022
1023/******************************************************************/
1032void dai_diplomacy_begin_new_phase(struct ai_type *ait, struct player *pplayer)
1033{
1034 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
1035 struct adv_data *adv = adv_data_get(pplayer, nullptr);
1037 int best_desire = 0;
1038 struct player *best_target = nullptr;
1039
1040 fc_assert_ret(is_ai(pplayer));
1041
1042 if (!pplayer->is_alive) {
1043 return; /* Duh */
1044 }
1045
1046 memset(war_desire, 0, sizeof(war_desire));
1047
1048 /* Calculate our desires, and find desired war target */
1050 /* We don't hate ourselves, those we don't know or team members. */
1051 if (aplayer == pplayer
1052 || NEVER_MET(pplayer, aplayer)
1053 || players_on_same_team(pplayer, aplayer)) {
1054 continue;
1055 }
1060 }
1062
1063 /* Time to make love. If we've been wronged, hold off that love
1064 * for a while. Also, cool our head each turn with love_coeff. */
1066 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1067 int amount = 0;
1068 int pit;
1069
1070 if (pplayer == aplayer) {
1071 continue;
1072 }
1073
1074 if ((pplayers_non_attack(pplayer, aplayer)
1075 || pplayers_allied(pplayer, aplayer))
1076 && player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel == 0
1077 && adip->countdown == -1
1078 && !adip->is_allied_with_enemy
1079 && !adip->at_war_with_ally
1080 && aplayer != best_target
1081 && adip->ally_patience >= 0) {
1082 amount += ai->diplomacy.love_incr / 2;
1083 if (pplayers_allied(pplayer, aplayer)) {
1084 amount += ai->diplomacy.love_incr / 3;
1085 }
1086 /* Increase love by each enemy they are at war with */
1088 if (WAR(eplayer, aplayer) && WAR(pplayer, eplayer)) {
1089 amount += ai->diplomacy.love_incr / 4;
1090 }
1093 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "Increased love by %d", amount);
1094 } else if (WAR(pplayer, aplayer)) {
1095 amount -= ai->diplomacy.love_incr / 2;
1097 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "%d love lost to war", amount);
1098 } else if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel
1099 != 0) {
1100 /* Provoked in time of peace */
1101 if (pplayer->ai_common.love[player_index(aplayer)] > 0) {
1102 amount -= pplayer->ai_common.love[player_index(aplayer)] / 2;
1103 }
1104 amount -= ai->diplomacy.love_incr * 6;
1106 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "Provoked! %d love lost!",
1107 amount);
1108 }
1109 if (pplayer->ai_common.love[player_index(aplayer)] > MAX_AI_LOVE * 8 / 10
1110 && !pplayers_allied(pplayer, aplayer)) {
1111 int love_change = ai->diplomacy.love_incr / 3;
1112
1113 /* Upper levels of AI trust and love is reserved for allies. */
1115 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "%d love lost from excess",
1116 love_change);
1117 }
1118 amount = 0;
1119
1120 /* Reduce love due to units in our territory.
1121 * AI is so naive, that we have to count it even if players are allied */
1122 pit = player_in_territory(pplayer, aplayer) * (MAX_AI_LOVE / 200);
1123 amount -= MIN(pit,
1125 * ((adip->is_allied_with_enemy != nullptr) + 1));
1127 if (amount != 0) {
1128 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "%d love lost due to units inside "
1129 "our borders", amount);
1130 }
1131
1132 /* Increase the love if aplayer has got a building that makes
1133 * us love them more. Typically it's Eiffel Tower */
1134 if (!NEVER_MET(pplayer, aplayer)) {
1135 pplayer->ai_common.love[player_index(aplayer)] +=
1137 }
1139
1140 /* Can we win by space race? */
1141 if (adv->dipl.spacerace_leader == pplayer) {
1142 log_base(LOG_DIPL2, "%s going for space race victory!",
1143 player_name(pplayer));
1144 ai->diplomacy.strategy = WIN_SPACE; /* Yes! */
1145 } else {
1146 if (ai->diplomacy.strategy == WIN_SPACE) {
1148 }
1149 }
1150
1152 int *love = &pplayer->ai_common.love[player_index(aplayer)];
1153
1154 if (aplayer == best_target && best_desire > 0) {
1155 int reduction = MIN(best_desire, MAX_AI_LOVE / 20);
1156
1157 *love -= reduction;
1158 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "Wants war, reducing "
1159 "love by %d ", reduction);
1160 }
1161
1162 /* Edge love towards zero */
1163 *love -= *love * ((double)ai->diplomacy.love_coeff / 100.0);
1164
1165 /* AI love should always be in range [-MAX_AI_LOVE..MAX_AI_LOVE] */
1166 *love = MAX(-MAX_AI_LOVE, MIN(MAX_AI_LOVE, *love));
1168}
1169
1170/******************************************************************/
1173static void suggest_tech_exchange(struct ai_type *ait,
1174 struct player *player1,
1175 struct player *player2)
1176{
1180 int worth[ac];
1181 bool is_dangerous;
1182
1183 worth[A_NONE] = 0;
1184
1187 == TECH_KNOWN) {
1190 worth[tech] = -compute_tech_sell_price(ait, player1, player2, tech,
1191 &is_dangerous);
1192 if (is_dangerous) {
1193 /* Don't try to exchange */
1194 worth[tech] = 0;
1195 }
1196 } else {
1197 worth[tech] = 0;
1198 }
1199 } else {
1203 worth[tech] = compute_tech_sell_price(ait, player2, player1, tech,
1204 &is_dangerous);
1205 if (is_dangerous) {
1206 /* Don't try to exchange */
1207 worth[tech] = 0;
1208 }
1209 } else {
1210 worth[tech] = 0;
1211 }
1212 }
1214
1216 if (worth[tech] <= 0) {
1217 continue;
1218 }
1220 int diff;
1221
1222 if (worth[tech2] >= 0) {
1223 continue;
1224 }
1225 /* Tech2 is given by player1, tech is given by player2 */
1226 diff = worth[tech] + worth[tech2];
1227 if ((diff > 0 && player1->economic.gold >= diff)
1228 || (diff < 0 && player2->economic.gold >= -diff)
1229 || diff == 0) {
1232 if (diff > 0) {
1234 } else if (diff < 0) {
1236 }
1237 return;
1238 }
1241}
1242
1243/******************************************************************/
1246static void clear_old_treaty(struct player *pplayer, struct player *aplayer)
1247{
1248 struct treaty *old_treaty = find_treaty(pplayer, aplayer);
1249
1250 if (old_treaty != nullptr) {
1251 /* Remove existing clauses */
1254 player_number(pplayer),
1255 player_number(pclause->from),
1256 pclause->type, pclause->value);
1257 free(pclause);
1260 old_treaty->clauses = clause_list_new();
1261 }
1262}
1263
1264/******************************************************************/
1267static void dai_share(struct ai_type *ait, struct player *pplayer,
1268 struct player *aplayer)
1269{
1270 struct research *presearch = research_get(pplayer);
1272 bool gives_vision;
1273
1274 clear_old_treaty(pplayer, aplayer);
1275
1276 /* Only share techs with team mates */
1277 if (presearch != aresearch
1278 && players_on_same_team(pplayer, aplayer)) {
1290 }
1292 }
1293
1294 /* Only give shared vision if safe. Only ask for shared vision if fair. */
1296 if (!gives_vision
1297 && shared_vision_is_safe(pplayer, aplayer)) {
1300 }
1301 if (gives_vision
1302 && !gives_shared_vision(aplayer, pplayer)
1303 && (is_human(aplayer)
1304 || shared_vision_is_safe(aplayer, pplayer))) {
1306 }
1307
1308 if (!player_has_embassy(pplayer, aplayer)) {
1310 }
1311 if (!player_has_embassy(aplayer, pplayer)) {
1313 }
1314
1315 if (!has_handicap(pplayer, H_DIPLOMACY) || is_human(aplayer)) {
1316 suggest_tech_exchange(ait, pplayer, aplayer);
1317 }
1318}
1319
1320/******************************************************************/
1327static void dai_declare_war(struct ai_type *ait, struct player *pplayer,
1328 struct player *target)
1329{
1330 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, target);
1331
1332 /* This will take us straight to war. */
1333 while (player_diplstate_get(pplayer, target)->type != DS_WAR) {
1334 if (pplayer_can_cancel_treaty(pplayer, target) != DIPL_OK) {
1335 DIPLO_LOG(ait, LOG_ERROR, pplayer, target,
1336 "Wanted to cancel treaty but was unable to.");
1337 adip->countdown = -1; /* War declaration aborted */
1338
1339 return;
1340 }
1342 }
1343
1344 /* Throw a tantrum */
1345 if (pplayer->ai_common.love[player_index(target)] > 0) {
1346 pplayer->ai_common.love[player_index(target)] = -1;
1347 }
1348 pplayer->ai_common.love[player_index(target)] -= MAX_AI_LOVE / 8;
1349
1350 fc_assert(!gives_shared_vision(pplayer, target));
1351
1352 DIPLO_LOG(ait, LOG_DIPL, pplayer, target, "war declared");
1353}
1354
1355/******************************************************************/
1360static void dai_go_to_war(struct ai_type *ait, struct player *pplayer,
1361 struct player *target, enum war_reason reason)
1362{
1363 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, target);
1364
1365 fc_assert_ret(pplayer != target);
1366 fc_assert_ret(target->is_alive);
1367
1368 switch (reason) {
1369 case DAI_WR_SPACE:
1370 dai_diplo_notify(target, _("*%s (AI)* Space will never be yours."),
1371 player_name(pplayer));
1372 adip->countdown = -10;
1373 break;
1374 case DAI_WR_BEHAVIOR:
1375 dai_diplo_notify(target,
1376 _("*%s (AI)* I have tolerated your vicious antics "
1377 "long enough! To war!"),
1378 player_name(pplayer));
1379 adip->countdown = -20;
1380 break;
1381 case DAI_WR_NONE:
1382 dai_diplo_notify(target, _("*%s (AI)* Peace in ... some other time."),
1383 player_name(pplayer));
1384 adip->countdown = -10;
1385 break;
1386 case DAI_WR_HATRED:
1387 dai_diplo_notify(target,
1388 _("*%s (AI)* Finally I get around to you! Did "
1389 "you really think you could get away with your crimes?"),
1390 player_name(pplayer));
1391 adip->countdown = -20;
1392 break;
1393 case DAI_WR_EXCUSE:
1394 dai_diplo_notify(target,
1395 _("*%s (AI)* Your covert hostilities brought "
1396 "this war upon you!"),
1397 player_name(pplayer));
1398 adip->countdown = -20;
1399 break;
1400 case DAI_WR_ALLIANCE:
1401 if (adip->at_war_with_ally) {
1402 dai_diplo_notify(target,
1403 _("*%s (AI)* Your aggression against %s was "
1404 "your last mistake!"),
1405 player_name(pplayer),
1406 player_name(adip->at_war_with_ally));
1407 adip->countdown = -3;
1408 } else {
1409 /* Ooops! */
1410 DIPLO_LOG(ait, LOG_DIPL, pplayer, target, "Wanted to declare war "
1411 "for their war against an ally, but can no longer find "
1412 "this ally! War declaration aborted.");
1413 adip->countdown = -1;
1414 return;
1415 }
1416 break;
1417 }
1418
1419 fc_assert_ret(adip->countdown < 0);
1420
1421 if (gives_shared_vision(pplayer, target)) {
1422 remove_shared_vision(pplayer, target);
1423 }
1424
1425 /* Check for Senate obstruction. If so, dissolve it. */
1426 if (pplayer_can_cancel_treaty(pplayer, target) == DIPL_SENATE_BLOCKING) {
1427 struct government *real_gov = pplayer->government;
1428
1430
1431 if (pplayer_can_cancel_treaty(pplayer, target) == DIPL_OK) {
1432 /* It seems that revolution would help. */
1433 pplayer->government = real_gov;
1436 def_ai_player_data(pplayer, ait)->diplomacy.war_target = target;
1437 return;
1438 } else {
1439 /* There would be Senate even during revolution. Better not to revolt for nothing */
1440 pplayer->government = real_gov;
1441 adip->countdown = -1; /* War declaration aborted */
1442
1443 DIPLO_LOG(ait, LOG_DEBUG, pplayer, target,
1444 "Not revolting, as there would be Senate regardless.");
1445
1446 return;
1447 }
1448 }
1449
1450 dai_declare_war(ait, pplayer, target);
1451}
1452
1453/******************************************************************/
1459void dai_revolution_start(struct ai_type *ait, struct player *pplayer)
1460{
1461 struct ai_plr *data = def_ai_player_data(pplayer, ait);
1462
1463 if (data->diplomacy.war_target != nullptr) {
1464 /* Target might have died since it was set. */
1465 if (data->diplomacy.war_target->is_alive) {
1466 if (gives_shared_vision(pplayer, data->diplomacy.war_target)) {
1468 }
1469
1470 dai_declare_war(ait, pplayer, data->diplomacy.war_target);
1471 }
1472
1473 data->diplomacy.war_target = nullptr;
1474 }
1475}
1476
1477/******************************************************************/
1489static void war_countdown(struct ai_type *ait, struct player *pplayer,
1490 struct player *target,
1491 int countdown, enum war_reason reason)
1492{
1493 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, target);
1494
1495 DIPLO_LOG(ait, LOG_DIPL, pplayer, target, "countdown to war in %d", countdown);
1496
1497 /* Otherwise we're resetting an existing countdown, which is very bad */
1498 fc_assert_ret(adip->countdown == -1);
1499
1500 adip->countdown = countdown;
1501 adip->war_reason = reason;
1502
1504 if (!pplayers_allied(pplayer, ally)
1505 || ally == target
1506 || NEVER_MET(pplayer, ally)) {
1507 continue;
1508 }
1509
1510 switch (reason) {
1511 case DAI_WR_SPACE:
1513 PL_("*%s (AI)* We will be launching an all-out war "
1514 "against %s in %d turn to stop the spaceship "
1515 "launch.",
1516 "*%s (AI)* We will be launching an all-out war "
1517 "against %s in %d turns to stop the spaceship "
1518 "launch.",
1519 countdown),
1520 player_name(pplayer),
1521 player_name(target),
1522 countdown);
1524 _("*%s (AI)* Your aid in this matter will be expected. "
1525 "Long live our glorious alliance!"),
1526 player_name(pplayer));
1527 break;
1528 case DAI_WR_BEHAVIOR:
1529 case DAI_WR_EXCUSE:
1531 PL_("*%s (AI)* %s has grossly violated their treaties "
1532 "with us for own gain. We will answer in force in "
1533 "%d turn and expect you to honor your alliance "
1534 "with us and do likewise!",
1535 "*%s (AI)* %s has grossly violated their treaties "
1536 "with us for own gain. We will answer in force in "
1537 "%d turns and expect you to honor your alliance "
1538 "with us and do likewise!", countdown),
1539 player_name(pplayer),
1540 player_name(target),
1541 countdown);
1542 break;
1543 case DAI_WR_NONE:
1545 PL_("*%s (AI)* We intend to pillage and plunder the rich "
1546 "civilization of %s. We declare war in %d turn.",
1547 "*%s (AI)* We intend to pillage and plunder the rich "
1548 "civilization of %s. We declare war in %d turns.",
1549 countdown),
1550 player_name(pplayer),
1551 player_name(target),
1552 countdown);
1554 _("*%s (AI)* If you want a piece of the loot, feel "
1555 "free to join in the action!"),
1556 player_name(pplayer));
1557 break;
1558 case DAI_WR_HATRED:
1560 PL_("*%s (AI)* We have had it with %s. Let us tear this "
1561 "pathetic civilization apart. We declare war in "
1562 "%d turn.",
1563 "*%s (AI)* We have had it with %s. Let us tear this "
1564 "pathetic civilization apart. We declare war in "
1565 "%d turns.",
1566 countdown),
1567 player_name(pplayer),
1568 player_name(target),
1569 countdown);
1571 _("*%s (AI)* As our glorious allies, we expect your "
1572 "help in this war."),
1573 player_name(pplayer));
1574 break;
1575 case DAI_WR_ALLIANCE:
1576 if (WAR(ally, target)) {
1578 PL_("*%s (AI)* We will honor our alliance and declare "
1579 "war on %s in %d turn. Hold on - we are coming!",
1580 "*%s (AI)* We will honor our alliance and declare "
1581 "war on %s in %d turns. Hold on - we are coming!",
1582 countdown),
1583 player_name(pplayer),
1584 player_name(target),
1585 countdown);
1586 } else if (adip->at_war_with_ally) {
1588 PL_("*%s (AI)* We will honor our alliance with %s and "
1589 "declare war on %s in %d turns. We expect you to "
1590 "do likewise.",
1591 "*%s (AI)* We will honor our alliance with %s and "
1592 "declare war on %s in %d turns. We expect you to "
1593 "do likewise.",
1594 countdown),
1595 player_name(pplayer),
1596 player_name(adip->at_war_with_ally),
1597 player_name(target),
1598 countdown);
1599 } else {
1600 fc_assert(FALSE); /* Huh? */
1601 }
1602 break;
1603 }
1605}
1606
1607/******************************************************************/
1613void dai_diplomacy_actions(struct ai_type *ait, struct player *pplayer)
1614{
1615 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
1616 bool need_targets = TRUE;
1617 struct player *target = nullptr;
1619 int war_threshold;
1620 int aggr;
1621 float aggr_sr;
1622 float max_sr;
1623
1624 fc_assert_ret(is_ai(pplayer));
1625
1626 if (!pplayer->is_alive) {
1627 return;
1628 }
1629
1630 if (get_player_bonus(pplayer, EFT_NO_DIPLOMACY) > 0) {
1631 /* Diplomacy disabled for this player */
1632 return;
1633 }
1634
1635 /*** If we are greviously insulted, go to war immediately. ***/
1636
1638 if (pplayer->ai_common.love[player_index(aplayer)] < 0
1639 && player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel >= 2
1640 && dai_diplomacy_get(ait, pplayer, aplayer)->countdown == -1) {
1641 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "Plans war in revenge");
1642 war_countdown(ait, pplayer, aplayer, map_size_checked(),
1644 }
1646
1647 /*** Stop other players from winning by space race ***/
1648
1649 if (ai->diplomacy.strategy != WIN_SPACE) {
1650 struct adv_data *adv = adv_data_get(pplayer, nullptr);
1651
1653 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1654 struct player_spaceship *ship = &aplayer->spaceship;
1655
1656 if (aplayer == pplayer
1657 || adip->countdown != -1 /* Already counting down to war */
1658 || ship->state == SSHIP_NONE
1659 || players_on_same_team(pplayer, aplayer)
1660 || pplayers_at_war(pplayer, aplayer)) {
1661 continue;
1662 }
1663
1664 /* A spaceship victory is always one single player's or team's victory */
1665 if (aplayer->spaceship.state == SSHIP_LAUNCHED
1666 && adv->dipl.spacerace_leader == aplayer
1667 && pplayers_allied(pplayer, aplayer)) {
1669 _("*%s (AI)* Your attempt to conquer space for "
1670 "yourself alone betrays your true intentions, and I "
1671 "will have no more of our alliance!"),
1672 player_name(pplayer));
1675 if (gives_shared_vision(pplayer, aplayer)) {
1676 remove_shared_vision(pplayer, aplayer);
1677 }
1678 /* Never forgive this */
1680 } else if (ship->state == SSHIP_STARTED
1681 && adip->warned_about_space == 0) {
1682 pplayer->ai_common.love[player_index(aplayer)] -= MAX_AI_LOVE / 10;
1683 adip->warned_about_space = 10 + fc_rand(6);
1685 _("*%s (AI)* Your attempt to unilaterally "
1686 "dominate outer space is highly offensive."),
1687 player_name(pplayer));
1689 _("*%s (AI)* If you do not stop constructing your "
1690 "spaceship, I may be forced to take action!"),
1691 player_name(pplayer));
1692 }
1693 if (aplayer->spaceship.state == SSHIP_LAUNCHED
1694 && aplayer == adv->dipl.spacerace_leader) {
1695 /* This means war!!! */
1696 pplayer->ai_common.love[player_index(aplayer)] -= MAX_AI_LOVE / 2;
1697 DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "plans war due to spaceship");
1698 war_countdown(ait, pplayer, aplayer, 4 + map_size_checked(),
1699 DAI_WR_SPACE);
1700 }
1702 }
1703
1704 /*** Declare war against somebody if we are out of targets ***/
1705
1707 int turns; /* Turns since contact */
1708
1709 if (NEVER_MET(pplayer, aplayer)) {
1710 continue;
1711 }
1712 turns = game.info.turn;
1714 if (WAR(pplayer, aplayer)) {
1716 } else if (pplayer->ai_common.love[player_index(aplayer)] < most_hatred
1717 && turns > TURNS_BEFORE_TARGET) {
1719 target = aplayer;
1720 }
1722
1725 aggr_sr = sqrt(aggr);
1726
1727 war_threshold = (MAX_AI_LOVE * (0.70 + aggr_sr / max_sr / 2.0)) - MAX_AI_LOVE;
1728
1729 if (need_targets && target && most_hatred < war_threshold
1730 && dai_diplomacy_get(ait, pplayer, target)->countdown == -1) {
1731 enum war_reason war_reason;
1732
1733 if (pplayers_allied(pplayer, target)) {
1734 DIPLO_LOG(ait, LOG_DEBUG, pplayer, target, "Plans war against an ally!");
1735 }
1736 if (player_diplstate_get(pplayer, target)->has_reason_to_cancel > 0) {
1737 /* We have good reason */
1738 war_reason = DAI_WR_EXCUSE;
1739 } else if (pplayer->ai_common.love[player_index(target)] < 0) {
1740 /* We have a reason of sorts from way back, maybe? */
1741 war_reason = DAI_WR_HATRED;
1742 } else {
1743 /* We have no legimitate reason... So what? */
1744 war_reason = DAI_WR_NONE;
1745 }
1746 DIPLO_LOG(ait, LOG_DEBUG, pplayer, target, "plans war for spoils");
1747 war_countdown(ait, pplayer, target, 4 + map_size_checked(), war_reason);
1748 }
1749
1750 /*** Declare war - against enemies of allies ***/
1751
1753 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1754
1755 if (adip->at_war_with_ally
1756 && adip->countdown == -1
1757 && !adip->is_allied_with_ally
1758 && !pplayers_at_war(pplayer, aplayer)
1760 || fc_rand(5) < 1)) {
1761 DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "plans war to help ally %s",
1762 player_name(adip->at_war_with_ally));
1763 war_countdown(ait, pplayer, aplayer, 2 + map_size_checked(),
1765 }
1767
1768 /*** Actually declare war (when we have moved units into position) ***/
1769
1771 if (!players_on_same_team(pplayer, aplayer)) {
1772 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1773
1774 if (!aplayer->is_alive) {
1775 adip->countdown = -1;
1776 continue;
1777 }
1778 if (adip->countdown > 0) {
1779 adip->countdown--;
1780 } else if (adip->countdown == 0) {
1781 if (!WAR(pplayer, aplayer)) {
1782 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "Declaring war!");
1783 dai_go_to_war(ait, pplayer, aplayer, adip->war_reason);
1784 }
1785 } else if (adip->countdown < -1) {
1786 /* Negative countdown less than -1 is war stubbornness */
1787 adip->countdown++;
1788 }
1789 }
1791
1792 /*** Try to make peace with everyone we love ***/
1793
1796 && diplomacy_possible(pplayer, aplayer)) {
1798 struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1799 struct Clause clause;
1800
1801 /* Meaningless values, but rather not have them unset. */
1802 clause.from = pplayer;
1803 clause.value = 0;
1804
1805 /* Remove shared vision if we are not allied or it is no longer safe. */
1806 if (gives_shared_vision(pplayer, aplayer)) {
1807 if (!pplayers_allied(pplayer, aplayer)) {
1808 remove_shared_vision(pplayer, aplayer);
1809 } else if (!players_on_same_team(pplayer, aplayer)
1810 && !shared_vision_is_safe(pplayer, aplayer)) {
1812 _("*%s (AI)* Sorry, sharing vision with you "
1813 "is no longer safe."),
1814 player_name(pplayer));
1815 remove_shared_vision(pplayer, aplayer);
1816 }
1817 }
1818
1819 /* No peace to enemies of our allies... or pointless peace. */
1821 || aplayer == pplayer
1822 || aplayer == target /* no mercy */
1823 || adip->countdown >= 0
1824 || !could_meet_with_player(pplayer, aplayer)) {
1825 continue;
1826 }
1827
1828 /* Spam control */
1829 adip->asked_about_peace = MAX(adip->asked_about_peace - 1, 0);
1830 adip->asked_about_alliance = MAX(adip->asked_about_alliance - 1, 0);
1831 adip->asked_about_ceasefire = MAX(adip->asked_about_ceasefire - 1, 0);
1832 adip->warned_about_space = MAX(adip->warned_about_space - 1, 0);
1833 adip->spam = MAX(adip->spam - 1, 0);
1834 if (adip->spam > 0 && is_ai(aplayer)) {
1835 /* Don't spam */
1836 continue;
1837 }
1838
1839 /* Canvass support from existing friends for our war, and try to
1840 * make friends with enemies. Then we wait some turns until next time
1841 * we spam them with our gibbering chatter. */
1842 if (adip->spam <= 0) {
1843 if (!pplayers_allied(pplayer, aplayer)) {
1844 adip->spam = fc_rand(4) + 3; /* Bugger allies often. */
1845 } else {
1846 adip->spam = fc_rand(8) + 6; /* Others are less important. */
1847 }
1848 }
1849
1850 switch (ds) {
1851 case DS_TEAM:
1852 dai_share(ait, pplayer, aplayer);
1853 break;
1854 case DS_ALLIANCE:
1855 /* See if our allies are diligently declaring war on our enemies... */
1856 if (adip->at_war_with_ally) {
1857 break;
1858 }
1859 target = nullptr;
1861 if (WAR(pplayer, eplayer)
1863 target = eplayer;
1864 break;
1865 }
1867
1868 if ((players_on_same_team(pplayer, aplayer)
1869 || pplayer->ai_common.love[player_index(aplayer)] > MAX_AI_LOVE / 2)) {
1870 /* Share techs only with team mates and allies we really like. */
1871 dai_share(ait, pplayer, aplayer);
1872 }
1873 if (!target || !target->is_alive) {
1874 adip->ally_patience = 0;
1875 break; /* No need to nag our ally */
1876 }
1877
1878 if (adip->spam <= 0) {
1879 /* Count down patience toward AI player (one that can have spam > 0)
1880 * at the same speed as toward human players. */
1881 if (adip->ally_patience == 0) {
1883 _("*%s (AI)* Greetings our most trustworthy "
1884 "ally. We call upon you to destroy our enemy, %s."),
1885 player_name(pplayer),
1886 player_name(target));
1887 adip->ally_patience--;
1888 } else if (adip->ally_patience == -1) {
1889 if (fc_rand(5) == 1) {
1891 _("*%s (AI)* Greetings ally, I see you have not yet "
1892 "made war with our enemy, %s. Why do I need to remind "
1893 "you of your promises?"),
1894 player_name(pplayer),
1895 player_name(target));
1896 adip->ally_patience--;
1897 }
1898 } else if (fc_rand(5) == 1
1899 && !players_on_same_team(pplayer, aplayer)) {
1901 _("*%s (AI)* Dishonored one, we made a pact of "
1902 "alliance, and yet you remain at peace with our mortal "
1903 "enemy, %s! This is unacceptable; our alliance is no "
1904 "more!"),
1905 player_name(pplayer),
1906 player_name(target));
1907 DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer,
1908 "breaking useless alliance");
1909 /* To peace */
1913 = MIN(pplayer->ai_common.love[player_index(aplayer)], 0);
1914 if (gives_shared_vision(pplayer, aplayer)) {
1915 remove_shared_vision(pplayer, aplayer);
1916 }
1917
1919 }
1920 }
1921 break;
1922
1923 case DS_PEACE:
1924 clause.type = CLAUSE_ALLIANCE;
1925 if (adip->at_war_with_ally
1926 || (is_human(aplayer) && adip->asked_about_alliance > 0)
1927 || dai_goldequiv_clause(ait, pplayer, aplayer, &clause,
1928 FALSE, DS_ALLIANCE) < 0) {
1929 break;
1930 }
1931 clear_old_treaty(pplayer, aplayer);
1933 adip->asked_about_alliance = is_human(aplayer) ? 13 : 0;
1935 _("*%s (AI)* Greetings friend, may we suggest "
1936 "making a common cause and join in an alliance?"),
1937 player_name(pplayer));
1938 break;
1939
1940 case DS_CEASEFIRE:
1941 clause.type = CLAUSE_PEACE;
1942 if (adip->at_war_with_ally
1943 || (is_human(aplayer) && adip->asked_about_peace > 0)
1944 || dai_goldequiv_clause(ait, pplayer, aplayer, &clause,
1945 FALSE, DS_PEACE) < 0) {
1946 break;
1947 }
1948 clear_old_treaty(pplayer, aplayer);
1950 adip->asked_about_peace = is_human(aplayer) ? 12 : 0;
1952 _("*%s (AI)* Greetings neighbor, may we suggest "
1953 "more peaceful relations?"),
1954 player_name(pplayer));
1955 break;
1956
1957 case DS_NO_CONTACT: /* But we do have embassy! weird. */
1958 case DS_WAR:
1959 clause.type = CLAUSE_CEASEFIRE;
1960 if ((is_human(aplayer) && adip->asked_about_ceasefire > 0)
1961 || dai_goldequiv_clause(ait, pplayer, aplayer, &clause,
1962 FALSE, DS_CEASEFIRE) < 0) {
1963 break; /* Fight until the end! */
1964 }
1965 clear_old_treaty(pplayer, aplayer);
1967 adip->asked_about_ceasefire = is_human(aplayer) ? 9 : 0;
1969 _("*%s (AI)* We grow weary of this constant "
1970 "bloodshed. May we suggest a cessation of hostilities?"),
1971 player_name(pplayer));
1972 break;
1973
1974 case DS_ARMISTICE:
1975 break;
1976 default:
1977 fc_assert_msg(FALSE, "Unknown pact type %d.", ds);
1978 break;
1979 }
1980 }
1982}
1983
1984/******************************************************************/
1988bool dai_on_war_footing(struct ai_type *ait, struct player *pplayer)
1989{
1991 if (dai_diplomacy_get(ait, pplayer, plr)->countdown >= 0) {
1992 return TRUE;
1993 }
1995
1996 return FALSE;
1997}
1998
1999/******************************************************************/
2002/* AI attitude call-backs */
2005 const struct action *paction,
2006 struct player *receiver,
2007 struct player *violator, struct player *victim)
2008{
2009 if (!is_ai(receiver) || violator == receiver) {
2010 return;
2011 }
2012
2013 /* Can only handle victim only and international incidents. */
2016
2017 switch (type) {
2018 case INCIDENT_ACTION:
2019 /* Feel free the change how the action results are grouped and how bad
2020 * the ai considers each group. */
2021 switch (paction->result) {
2022 case ACTRES_SPY_NUKE:
2023 case ACTRES_NUKE:
2024 case ACTRES_NUKE_UNITS:
2025 if (receiver == victim) {
2026 /* Tell the victim */
2028 } else if (violator == victim) {
2030 } else {
2032 }
2033 break;
2036 /* Snoping */
2037 dai_incident_simple(receiver, violator, victim, scope, 2);
2038 break;
2042 case ACTRES_STEAL_MAPS:
2043 /* Theft */
2044 dai_incident_simple(receiver, violator, victim, scope, 5);
2045 break;
2046 case ACTRES_EXPEL_UNIT:
2047 /* Unit position loss */
2048 dai_incident_simple(receiver, violator, victim, scope, 1);
2049 break;
2051 /* Unit weakening */
2052 dai_incident_simple(receiver, violator, victim, scope, 3);
2053 break;
2057 case ACTRES_BOMBARD:
2058 case ACTRES_ATTACK:
2059 case ACTRES_WIPE_UNITS:
2060 case ACTRES_SPY_ATTACK:
2062 /* Unit loss */
2063 dai_incident_simple(receiver, violator, victim, scope, 5);
2064 break;
2068 /* City loss */
2069 dai_incident_simple(receiver, violator, victim, scope, 10);
2070 break;
2076 /* Building loss */
2077 dai_incident_simple(receiver, violator, victim, scope, 5);
2078 break;
2079 case ACTRES_SPY_POISON:
2081 /* Population loss */
2082 dai_incident_simple(receiver, violator, victim, scope, 5);
2083 break;
2084 case ACTRES_FOUND_CITY:
2085 /* Terrain loss */
2086 dai_incident_simple(receiver, violator, victim, scope, 4);
2087 break;
2089 case ACTRES_PILLAGE:
2090 /* Extra loss */
2091 dai_incident_simple(receiver, violator, victim, scope, 2);
2092 break;
2093 case ACTRES_UNIT_MOVE:
2094 case ACTRES_TELEPORT:
2096 case ACTRES_SPY_ESCAPE:
2097 case ACTRES_TRADE_ROUTE:
2098 case ACTRES_MARKETPLACE:
2099 case ACTRES_HELP_WONDER:
2100 case ACTRES_JOIN_CITY:
2103 case ACTRES_HOME_CITY:
2104 case ACTRES_HOMELESS:
2106 case ACTRES_PARADROP:
2107 case ACTRES_PARADROP_CONQUER: /* TODO: Bigger incident */
2108 case ACTRES_AIRLIFT:
2109 case ACTRES_HEAL_UNIT:
2111 case ACTRES_CULTIVATE:
2112 case ACTRES_PLANT:
2113 case ACTRES_CLEAN:
2114 case ACTRES_FORTIFY:
2115 case ACTRES_ROAD:
2116 case ACTRES_CONVERT:
2117 case ACTRES_BASE:
2118 case ACTRES_MINE:
2119 case ACTRES_IRRIGATE:
2126 case ACTRES_HUT_ENTER:
2129 case ACTRES_NONE:
2130 /* Various */
2131 dai_incident_simple(receiver, violator, victim, scope, 1);
2132 break;
2133
2135 }
2136 break;
2137 case INCIDENT_WAR:
2138 if (receiver == victim) {
2140 }
2141 break;
2142 case INCIDENT_LAST:
2143 /* Assert that always fails, but with meaningful message */
2145 break;
2146 }
2147}
2148
2149/******************************************************************/
2152static void dai_incident_nuclear(struct player *receiver,
2153 const struct player *violator,
2154 const struct player *victim)
2155{
2156 fc_assert_ret(receiver == victim);
2157
2158 if (violator == victim) {
2159 return;
2160 }
2161
2162 receiver->ai_common.love[player_index(violator)] -= 3 * MAX_AI_LOVE / 10;
2163}
2164
2165/******************************************************************/
2168static void dai_incident_nuclear_not_target(struct player *receiver,
2169 const struct player *violator,
2170 const struct player *victim)
2171{
2172 fc_assert_ret(receiver != victim);
2173
2174 receiver->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 10;
2175}
2176
2177/******************************************************************/
2180static void dai_incident_nuclear_self(struct player *receiver,
2181 const struct player *violator,
2182 const struct player *victim)
2183{
2184 fc_assert_ret(receiver != victim);
2186
2187 receiver->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 20;
2188}
2189
2190/**********************************************************************/
2202static void dai_incident_simple(struct player *receiver,
2203 const struct player *violator,
2204 const struct player *victim,
2206 int how_bad)
2207{
2209 if (victim == receiver) {
2211 /* The ruleset finds this bad enough to cause International Outrage.
2212 * Trust the ruleset author. Double the displeasure. */
2214 }
2215 receiver->ai_common.love[player_index(violator)] -= displeasure / 35;
2216 } else if (violator == victim) {
2217 receiver->ai_common.love[player_index(violator)] -= displeasure / 1000;
2218 } else {
2219 receiver->ai_common.love[player_index(violator)] -= displeasure / 500;
2220 }
2221}
2222
2223/******************************************************************/
2231static void dai_incident_war(struct player *violator, struct player *victim)
2232{
2233 players_iterate(pplayer) {
2234 if (!is_ai(pplayer)) {
2235 continue;
2236 }
2237
2238 if (pplayer != violator) {
2239 /* Dislike backstabbing bastards */
2240 pplayer->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 30;
2241 if (player_diplstate_get(violator, victim)->max_state == DS_PEACE) {
2242 /* Extra penalty if they once had a peace treaty */
2243 pplayer->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 30;
2244 } else if (player_diplstate_get(violator, victim)->max_state
2245 == DS_ALLIANCE) {
2246 /* Extra penalty if they once had an alliance */
2247 pplayer->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 10;
2248 }
2249 if (victim == pplayer) {
2250 pplayer->ai_common.love[player_index(violator)] =
2251 MIN(pplayer->ai_common.love[player_index(violator)] - MAX_AI_LOVE / 3, -1);
2252 /* Scream for help!! */
2254 if (!pplayers_allied(pplayer, ally)) {
2255 continue;
2256 }
2258 _("*%s (AI)* We have been savagely attacked by "
2259 "%s, and we need your help! Honor our glorious "
2260 "alliance and your name will never be forgotten!"),
2264 }
2265 }
2267}
#define ASSERT_UNUSED_ACTRES_CASES
Definition actres.h:37
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:613
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
Definition advdata.c:1120
adv_want amortize(adv_want benefit, int delay)
Definition advtools.c:29
incident_type
Definition ai.h:45
@ INCIDENT_LAST
Definition ai.h:46
@ INCIDENT_WAR
Definition ai.h:46
@ INCIDENT_ACTION
Definition ai.h:46
int player_distance_to_player(struct player *pplayer, struct player *target)
Definition aisupport.c:75
int city_gold_worth(const struct civ_map *nmap, struct city *pcity)
Definition aisupport.c:108
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Definition aitraits.c:68
bool is_capital(const struct city *pcity)
Definition city.c:1620
const char * city_name_get(const struct city *pcity)
Definition city.c:1141
struct city * city_list_find_number(struct city_list *This, int id)
Definition city.c:1720
#define city_list_iterate(citylist, pcity)
Definition city.h:508
static citizens city_size_get(const struct city *pcity)
Definition city.h:570
#define city_owner(_pcity_)
Definition city.h:564
#define city_list_iterate_end
Definition city.h:510
#define city_built_iterate(_pcity, _p)
Definition city.h:836
#define city_built_iterate_end
Definition city.h:842
char * incite_cost
Definition comments.c:77
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Definition daidata.c:308
struct ai_dip_intel * dai_diplomacy_get(struct ai_type *ait, const struct player *plr1, const struct player *plr2)
Definition daidata.c:402
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Definition daidata.c:285
@ WIN_SPACE
Definition daidata.h:34
@ WIN_CAPITAL
Definition daidata.h:35
@ WIN_OPEN
Definition daidata.h:32
static void dai_incident_nuclear_not_target(struct player *receiver, const struct player *violator, const struct player *victim)
void dai_incident(struct ai_type *ait, enum incident_type type, enum casus_belli_range scope, const struct action *paction, struct player *receiver, struct player *violator, struct player *victim)
bool dai_on_war_footing(struct ai_type *ait, struct player *pplayer)
void dai_revolution_start(struct ai_type *ait, struct player *pplayer)
void dai_diplomacy_actions(struct ai_type *ait, struct player *pplayer)
static int dai_goldequiv_clause(struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Clause *pclause, bool verbose, enum diplstate_type ds_after)
static const struct player * get_allied_with_enemy_player(const struct player *us, const struct player *them)
static void dai_incident_simple(struct player *receiver, const struct player *violator, const struct player *victim, enum casus_belli_range scope, int how_bad)
static void dai_incident_nuclear_self(struct player *receiver, const struct player *violator, const struct player *victim)
void dai_treaty_accepted(struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct treaty *ptreaty)
void dai_diplomacy_begin_new_phase(struct ai_type *ait, struct player *pplayer)
static void suggest_tech_exchange(struct ai_type *ait, struct player *player1, struct player *player2)
#define LOG_DIPL2
static int greed(int missing_love)
static bool dai_players_can_agree_on_ceasefire(struct ai_type *ait, struct player *player1, struct player *player2)
static void dai_go_to_war(struct ai_type *ait, struct player *pplayer, struct player *target, enum war_reason reason)
void dai_treaty_evaluate(struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct treaty *ptreaty)
static int dai_goldequiv_tech(struct ai_type *ait, struct player *pplayer, Tech_type_id tech)
#define BIG_NUMBER
#define LOG_DIPL
static enum diplstate_type pact_clause_to_diplstate_type(enum clause_type type)
static void dai_diplomacy_suggest(struct player *pplayer, struct player *aplayer, enum clause_type what, bool to_pplayer, int value)
static void dai_treaty_react(struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Clause *pclause)
static int compute_tech_sell_price(struct ai_type *ait, struct player *giver, struct player *taker, int tech_id, bool *is_dangerous)
static void dai_declare_war(struct ai_type *ait, struct player *pplayer, struct player *target)
static void clear_old_treaty(struct player *pplayer, struct player *aplayer)
static void dai_diplo_notify(struct player *pplayer, const char *text,...)
static void dai_incident_nuclear(struct player *receiver, const struct player *violator, const struct player *victim)
static void dai_incident_war(struct player *violator, struct player *victim)
static void war_countdown(struct ai_type *ait, struct player *pplayer, struct player *target, int countdown, enum war_reason reason)
static void dai_share(struct ai_type *ait, struct player *pplayer, struct player *aplayer)
static bool shared_vision_is_safe(struct player *pplayer, struct player *aplayer)
static int dai_war_desire(struct ai_type *ait, struct player *pplayer, struct player *target)
#define TURNS_BEFORE_TARGET
static bool diplomacy_verbose
void dai_diplomacy_first_contact(struct ai_type *ait, struct player *pplayer, struct player *aplayer)
#define DIPLO_LOG(ait, loglevel, pplayer, aplayer, msg,...)
Definition dailog.h:53
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition daiplayer.h:56
#define WAR(plr1, plr2)
Definition daiunit.h:62
#define NEVER_MET(plr1, plr2)
Definition daiunit.h:64
#define ATTACK_POWER(ptype)
Definition daiunit.h:68
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
void handle_diplomacy_create_clause_req(struct player *pplayer, int counterpart, int giver, enum clause_type type, int value)
Definition diplhand.c:753
void handle_diplomacy_init_meeting_req(struct player *pplayer, int counterpart)
Definition diplhand.c:845
void handle_diplomacy_accept_treaty_req(struct player *pplayer, int counterpart)
Definition diplhand.c:147
struct treaty * ptreaty
Definition diplodlg_g.h:28
bool diplomacy_possible(const struct player *pplayer1, const struct player *pplayer2)
Definition diptreaty.c:36
struct treaty * find_treaty(struct player *plr0, struct player *plr1)
Definition diptreaty.c:362
bool could_meet_with_player(const struct player *pplayer, const struct player *aplayer)
Definition diptreaty.c:64
#define clause_list_iterate_end
Definition diptreaty.h:73
#define clause_list_iterate(clauselist, pclause)
Definition diptreaty.h:71
#define is_pact_clause(x)
Definition diptreaty.h:53
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:842
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:824
int Tech_type_id
Definition fc_types.h:238
#define ACTRES_NONE
Definition fc_types.h:188
@ O_SHIELD
Definition fc_types.h:103
@ O_TRADE
Definition fc_types.h:103
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
const struct ft_color ftc_chat_private
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
GType type
Definition repodlgs.c:1313
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_DIPLOMACY
Definition handicaps.h:29
@ H_AWAY
Definition handicaps.h:19
@ H_CEASEFIRE
Definition handicaps.h:33
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
bool is_great_wonder(const struct impr_type *pimprove)
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
bool is_small_wonder(const struct impr_type *pimprove)
#define fc_assert_msg(condition, message,...)
Definition log.h:182
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_base(level, message,...)
Definition log.h:95
@ LOG_ERROR
Definition log.h:31
@ LOG_DEBUG
Definition log.h:35
#define log_error(message,...)
Definition log.h:104
void lsend_packet_chat_msg(struct conn_list *dest, const struct packet_chat_msg *packet)
void dlsend_packet_diplomacy_remove_clause(struct conn_list *dest, int counterpart, int giver, enum clause_type type, int value)
void handle_player_change_government(struct player *pplayer, Government_type_id government)
Definition plrhand.c:565
void handle_diplomacy_cancel_pact(struct player *pplayer, int other_player_id, enum clause_type clause)
Definition plrhand.c:882
static int map_size_checked(void)
Definition map.h:272
void remove_shared_vision(struct player *pfrom, struct player *pto)
Definition maphand.c:1698
void vpackage_event(struct packet_chat_msg *packet, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format, va_list vargs)
Definition notify.c:147
void event_cache_add_for_player(const struct packet_chat_msg *packet, const struct player *pplayer)
Definition notify.c:645
struct city_list * cities
Definition packhand.c:120
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1468
int player_slot_count(void)
Definition player.c:415
int player_number(const struct player *pplayer)
Definition player.c:826
const char * player_name(const struct player *pplayer)
Definition player.c:885
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1376
int player_index(const struct player *pplayer)
Definition player.c:818
int player_in_territory(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1867
enum dipl_reason pplayer_can_cancel_treaty(const struct player *p1, const struct player *p2)
Definition player.c:103
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:325
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1397
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1451
bool player_has_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:213
bool gives_shared_vision(const struct player *me, const struct player *them)
Definition player.c:1477
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1418
#define players_iterate_end
Definition player.h:552
@ DIPL_SENATE_BLOCKING
Definition player.h:193
@ DIPL_OK
Definition player.h:193
#define players_iterate(_pplayer)
Definition player.h:547
#define MAX_AI_LOVE
Definition player.h:576
static bool is_barbarian(const struct player *pplayer)
Definition player.h:499
#define is_ai(plr)
Definition player.h:232
#define players_iterate_alive_end
Definition player.h:562
#define is_human(plr)
Definition player.h:231
#define players_iterate_alive(_pplayer)
Definition player.h:557
#define fc_rand(_size)
Definition rand.h:56
bool research_goal_tech_req(const struct research *presearch, Tech_type_id goal, Tech_type_id tech)
Definition research.c:810
int player_tech_upkeep(const struct player *pplayer)
Definition research.c:1062
int research_goal_bulbs_required(const struct research *presearch, Tech_type_id goal)
Definition research.c:775
struct research * research_get(const struct player *pplayer)
Definition research.c:130
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:622
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Definition research.c:696
#define MIN(x, y)
Definition shared.h:55
#define MAX(x, y)
Definition shared.h:54
@ SSHIP_STARTED
Definition spaceship.h:84
@ SSHIP_LAUNCHED
Definition spaceship.h:85
@ SSHIP_NONE
Definition spaceship.h:84
enum clause_type type
Definition diptreaty.h:76
struct player * from
Definition diptreaty.h:77
int value
Definition diptreaty.h:78
struct adv_data::@95 dipl
struct player * spacerace_leader
Definition advdata.h:104
enum war_reason war_reason
Definition daidata.h:62
int distance
Definition daidata.h:60
int countdown
Definition daidata.h:61
struct ai_plr::@280 diplomacy
adv_want tech_want[A_LAST+1]
Definition daidata.h:104
int req_love_for_alliance
Definition daidata.h:96
char love_incr
Definition daidata.h:94
char love_coeff
Definition daidata.h:93
struct player * war_target
Definition daidata.h:97
int req_love_for_peace
Definition daidata.h:95
enum winning_strategy strategy
Definition daidata.h:91
Definition ai.h:50
Definition city.h:318
int diplgoldcost
Definition game.h:147
struct packet_game_info info
Definition game.h:89
struct civ_game::@32::@36 server
struct government * government_during_revolution
Definition game.h:94
Government_type_id government_during_revolution_id
enum tech_upkeep_style tech_upkeep_style
enum tech_leakage_style tech_leakage
bool tech_trade_allow_holes
enum tech_cost_style tech_cost_style
int love[MAX_NUM_PLAYER_SLOTS]
Definition player.h:124
int first_contact_turn
Definition player.h:201
enum diplstate_type type
Definition player.h:199
char has_reason_to_cancel
Definition player.h:203
struct city_list * cities
Definition player.h:281
int bulbs_last_turn
Definition player.h:351
struct player_ai ai_common
Definition player.h:288
struct government * government
Definition player.h:258
struct team * team
Definition player.h:261
const struct ai_type * ai
Definition player.h:289
struct unit_list * units
Definition player.h:282
struct conn_list * connections
Definition player.h:298
bool is_alive
Definition player.h:268
struct player::@73::@75 server
struct player_economic economic
Definition player.h:284
struct player_spaceship spaceship
Definition player.h:286
Tech_type_id researching
Definition research.h:52
Tech_type_id tech_goal
Definition research.h:83
int techs_researched
Definition research.h:42
int bulbs_researched
Definition research.h:53
struct clause_list * clauses
Definition diptreaty.h:84
struct player * plr0
Definition diptreaty.h:82
struct unit_type::@90 adv
const struct unit_type * utype
Definition unit.h:141
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct advance * advance_by_number(const Tech_type_id atype)
Definition tech.c:110
const char * advance_rule_name(const struct advance *padvance)
Definition tech.c:314
#define advance_index_iterate_max(_start, _index, _max)
Definition tech.h:250
#define advance_index_iterate_end
Definition tech.h:246
#define advance_index_iterate_max_end
Definition tech.h:256
static Tech_type_id advance_count(void)
Definition tech.h:167
#define A_FIRST
Definition tech.h:44
#define A_NONE
Definition tech.h:43
#define advance_index_iterate(_start, _index)
Definition tech.h:242
#define TRAIT_MAX_VALUE_SR
Definition traits.h:34
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2745
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2997