Freeciv21
Develop your civilization from humble roots to a global empire
daidiplomacy.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  /\/\ part of Freeciv21. Freeciv21 is free software: you can
4  \_\ _..._ redistribute it and/or modify it under the terms of the
5  (" )(_..._) GNU General Public License as published by the Free
6  ^^ // \\ Software Foundation, either version 3 of the License,
7  or (at your option) any later version. You should have
8 received a copy of the GNU General Public License along with Freeciv21.
9  If not, see https://www.gnu.org/licenses/.
10  */
11 
12 #include <cstdarg>
13 #include <cstring>
14 
15 // utility
16 #include "fcintl.h"
17 #include "log.h"
18 #include "rand.h"
19 #include "shared.h"
20 #include "support.h"
21 
22 // common
23 #include "aisupport.h"
24 #include "city.h"
25 #include "diptreaty.h"
26 #include "events.h"
27 #include "game.h"
28 #include "nation.h"
29 #include "packets.h"
30 #include "player.h"
31 #include "research.h"
32 #include "spaceship.h"
33 #include "tech.h"
34 #include "unitlist.h"
35 
36 // server
37 #include "diplhand.h"
38 #include "maphand.h"
39 #include "notify.h"
40 
41 /* server/advisors */
42 #include "advdata.h"
43 #include "advtools.h"
44 
45 // ai
46 #include "aitraits.h"
47 #include "handicaps.h"
48 
49 /* ai/default */
50 #include "aidata.h"
51 #include "ailog.h"
52 #include "aiplayer.h"
53 #include "aitools.h"
54 #include "aiunit.h"
55 #include "daicity.h"
56 
57 #include "daidiplomacy.h"
58 
59 #define LOG_DIPL2 LOG_DEBUG
60 
61 /* One hundred thousand. Basically a number of gold that no player is
62  * ever likely to have, but not so big that we get integer overflows. */
63 #define BIG_NUMBER 100000
64 
65 // turn this off when we don't want functions to message players
66 static bool diplomacy_verbose = true;
67 
68 // turns to wait after contact before taking aim for war
69 #define TURNS_BEFORE_TARGET 15
70 
71 static void dai_incident_war(struct player *violator, struct player *victim);
72 static void dai_incident_simple(struct player *receiver,
73  const struct player *violator,
74  const struct player *victim,
75  enum casus_belli_range scope, int how_bad);
76 static void dai_incident_nuclear(struct player *receiver,
77  const struct player *violator,
78  const struct player *victim);
79 static void dai_incident_nuclear_not_target(struct player *receiver,
80  const struct player *violator,
81  const struct player *victim);
82 static void dai_incident_nuclear_self(struct player *receiver,
83  const struct player *violator,
84  const struct player *victim);
85 static void clear_old_treaty(struct player *pplayer, struct player *aplayer);
86 
92 static void dai_diplo_notify(struct player *pplayer, const char *text, ...)
93 {
94  if (diplomacy_verbose) {
95  va_list ap;
96  struct conn_list *dest = pplayer->connections;
97  struct packet_chat_msg packet;
98 
99  va_start(ap, text);
100  vpackage_event(&packet, nullptr, E_DIPLOMACY, ftc_chat_private, text,
101  ap);
102  va_end(ap);
103 
104  lsend_packet_chat_msg(dest, &packet);
105  // Add to the event cache.
106  event_cache_add_for_player(&packet, pplayer);
107  }
108 }
109 
114 static int greed(int missing_love)
115 {
116  if (missing_love > 0) {
117  return 0;
118  } else {
119  /* Don't change the operation order here.
120  * We do not want integer overflows */
121  return -((missing_love * MAX_AI_LOVE) / 1000)
122  * ((missing_love * MAX_AI_LOVE) / 1000) / 10;
123  }
124 }
125 
129 static enum diplstate_type
130 pact_clause_to_diplstate_type(enum clause_type type)
131 {
132  switch (type) {
133  case CLAUSE_ALLIANCE:
134  return DS_ALLIANCE;
135  case CLAUSE_PEACE:
136  return DS_PEACE;
137  case CLAUSE_CEASEFIRE:
138  return DS_CEASEFIRE;
139  default:
140  qCritical("Invalid diplomatic clause %d.", type);
141  return DS_WAR;
142  }
143 }
144 
148 static int dai_goldequiv_tech(struct ai_type *ait, struct player *pplayer,
149  Tech_type_id tech)
150 {
151  int bulbs, tech_want, worth;
152  struct research *presearch = research_get(pplayer);
153  enum tech_state state = research_invention_state(presearch, tech);
154  struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
155 
156  if (TECH_KNOWN == state
157  || !research_invention_gettable(presearch, tech,
158  game.info.tech_trade_allow_holes)) {
159  return 0;
160  }
161  bulbs = research_goal_bulbs_required(presearch, tech) * 3;
162  tech_want = MAX(plr_data->tech_want[tech], 0) / MAX(game.info.turn, 1);
163  worth = bulbs + tech_want;
164  if (TECH_PREREQS_KNOWN == state) {
165  worth /= 2;
166  }
167  return worth;
168 }
169 
174 static bool shared_vision_is_safe(struct player *pplayer,
175  struct player *aplayer)
176 {
177  if (pplayer->team && pplayer->team == aplayer->team) {
178  return true;
179  }
180  players_iterate_alive(eplayer)
181  {
182  if (eplayer == pplayer || eplayer == aplayer) {
183  continue;
184  }
185  if (gives_shared_vision(aplayer, eplayer)) {
186  enum diplstate_type ds = player_diplstate_get(pplayer, eplayer)->type;
187 
188  if (ds != DS_NO_CONTACT && ds != DS_ALLIANCE) {
189  return false;
190  }
191  }
192  }
194  return true;
195 }
196 
202  struct player *player1,
203  struct player *player2)
204 {
205  return (player1->ai_common.love[player_index(player2)]
206  > -(MAX_AI_LOVE * 4 / 10)
207  && dai_diplomacy_get(ait, player1, player2)->countdown == -1);
208 }
209 
217 static int compute_tech_sell_price(struct ai_type *ait, struct player *giver,
218  struct player *taker, int tech_id,
219  bool *is_dangerous)
220 {
221  int worth;
222 
223  worth = dai_goldequiv_tech(ait, taker, tech_id);
224 
225  *is_dangerous = false;
226 
227  // Share and expect being shared brotherly between allies
228  if (pplayers_allied(giver, taker)) {
229  worth /= 2;
230  }
231  if (players_on_same_team(giver, taker)) {
232  return 0;
233  }
234 
235  // Do not bother wanting a tech that we already have.
236  if (research_invention_state(research_get(taker), tech_id) == TECH_KNOWN) {
237  return 0;
238  }
239 
240  // Calculate in tech leak to our opponents, guess 50% chance
241  players_iterate_alive(eplayer)
242  {
243  if (eplayer == giver || eplayer == taker
244  || research_invention_state(research_get(eplayer), tech_id)
245  == TECH_KNOWN) {
246  continue;
247  }
248 
249  // Don't risk it falling into enemy hands
250  if (pplayers_allied(taker, eplayer)
251  && adv_is_player_dangerous(giver, eplayer)) {
252  *is_dangerous = true;
253  }
254 
255  if (pplayers_allied(taker, eplayer)
256  && !pplayers_allied(giver, eplayer)) {
257  // Taker can enrichen his side with this tech
258  worth += dai_goldequiv_tech(ait, eplayer, tech_id) / 4;
259  }
260  }
262 
263  return worth;
264 }
265 
269 static const struct player *
271  const struct player *them)
272 {
273  players_iterate_alive(aplayer)
274  {
275  if (aplayer != us && aplayer != them && pplayers_allied(them, aplayer)
276  && player_diplstate_get(us, aplayer)->type == DS_WAR) {
277  return aplayer;
278  }
279  }
281  return nullptr;
282 }
283 
291 static int dai_goldequiv_clause(struct ai_type *ait, struct player *pplayer,
292  struct player *aplayer,
293  struct Clause *pclause, bool verbose,
294  enum diplstate_type ds_after)
295 {
296  bool close_here;
297  struct ai_plr *ai;
298  int worth = 0; // worth for pplayer of what aplayer gives
299  bool give = (pplayer == pclause->from);
300  struct player *giver;
301  const struct player *penemy;
302  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
303  bool is_dangerous;
304 
305  ai = dai_plr_data_get(ait, pplayer, &close_here);
306 
307  fc_assert_ret_val(pplayer != aplayer, 0);
308 
309  diplomacy_verbose = verbose;
310  ds_after = MAX(ds_after, player_diplstate_get(pplayer, aplayer)->type);
311  giver = pclause->from;
312 
313  switch (pclause->type) {
314  case CLAUSE_ADVANCE:
315  if (give) {
316  worth -= compute_tech_sell_price(ait, pplayer, aplayer, pclause->value,
317  &is_dangerous);
318  if (is_dangerous) {
319  worth = -BIG_NUMBER;
320  }
321  } else if (research_invention_state(research_get(pplayer),
322  pclause->value)
323  != TECH_KNOWN) {
324  worth += compute_tech_sell_price(ait, aplayer, pplayer, pclause->value,
325  &is_dangerous);
326 
327  if (game.info.tech_upkeep_style != TECH_UPKEEP_NONE) {
328  /* Consider the upkeep costs! Thus, one can not get an AI player by
329  * - given AI lots of techs for gold/cities etc.
330  * - AI losses tech due to high upkeep.
331  * FIXME: Is there a better way for this? */
332  struct research *research = research_get(pplayer);
333  int limit =
335 
336  if (pplayer->server.bulbs_last_turn < limit) {
337  worth /= 2;
338  }
339  }
340  }
341  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "%s clause worth %d",
342  advance_rule_name(advance_by_number(pclause->value)), worth);
343  break;
344 
345  case CLAUSE_ALLIANCE:
346  case CLAUSE_PEACE:
347  case CLAUSE_CEASEFIRE:
348  // Don't do anything in away mode
349  if (has_handicap(pplayer, H_AWAY)) {
351  aplayer, _("*%s (AI)* In away mode AI can't sign such a treaty."),
352  player_name(pplayer));
353  worth = -BIG_NUMBER;
354  break;
355  }
356 
357  /* This guy is allied to one of our enemies. Only accept
358  * ceasefire. */
359  penemy = get_allied_with_enemy_player(pplayer, aplayer);
360  if (penemy && pclause->type != CLAUSE_CEASEFIRE) {
362  aplayer, _("*%s (AI)* First break alliance with %s, %s."),
363  player_name(pplayer), player_name(penemy), player_name(aplayer));
364  worth = -BIG_NUMBER;
365  break;
366  }
367 
368  // He was allied with an enemy at the begining of the turn.
369  if (adip->is_allied_with_enemy && pclause->type != CLAUSE_CEASEFIRE) {
370  dai_diplo_notify(aplayer,
371  _("*%s (AI)* I would like to see you keep your "
372  "distance from %s for some time, %s."),
373  player_name(pplayer),
375  player_name(aplayer));
376  worth = -BIG_NUMBER;
377  break;
378  }
379 
380  // Steps of the treaty ladder
381  if (pclause->type == CLAUSE_PEACE) {
382  const struct player_diplstate *ds =
383  player_diplstate_get(pplayer, aplayer);
384 
385  if (!pplayers_non_attack(pplayer, aplayer)) {
386  dai_diplo_notify(aplayer,
387  _("*%s (AI)* Let us first cease hostilities, %s."),
388  player_name(pplayer), player_name(aplayer));
389  worth = -BIG_NUMBER;
390  } else if (ds->type == DS_CEASEFIRE && ds->turns_left > 4) {
391  dai_diplo_notify(aplayer,
392  _("*%s (AI)* I wish to see you keep the current "
393  "ceasefire for a bit longer first, %s."),
394  player_name(pplayer), player_name(aplayer));
395  worth = -BIG_NUMBER;
396  } else if (adip->countdown >= 0 || adip->countdown < -1) {
397  worth = -BIG_NUMBER; // but say nothing
398  } else {
399  worth = greed(pplayer->ai_common.love[player_index(aplayer)]
401  }
402  } else if (pclause->type == CLAUSE_ALLIANCE) {
403  if (!pplayers_in_peace(pplayer, aplayer)) {
404  worth = greed(pplayer->ai_common.love[player_index(aplayer)]
406  }
407  if (adip->countdown >= 0 || adip->countdown < -1) {
408  worth = -BIG_NUMBER; // but say nothing
409  } else {
410  worth += greed(pplayer->ai_common.love[player_index(aplayer)]
412  }
413  if (pplayer->ai_common.love[player_index(aplayer)]
414  < MAX_AI_LOVE / 10) {
415  dai_diplo_notify(aplayer,
416  _("*%s (AI)* I simply do not trust you with an "
417  "alliance yet, %s."),
418  player_name(pplayer), player_name(aplayer));
419  worth = -BIG_NUMBER;
420  }
421  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "ally clause worth %d",
422  worth);
423  } else {
424  if (is_ai(pplayer) && is_ai(aplayer)
425  && dai_players_can_agree_on_ceasefire(ait, pplayer, aplayer)) {
426  worth = 0;
427  } else {
428  int turns = game.info.turn;
429 
430  turns -= player_diplstate_get(pplayer, aplayer)->first_contact_turn;
431  if (turns < TURNS_BEFORE_TARGET) {
432  worth = 0; // show some good faith
433  break;
434  } else {
435  worth = greed(pplayer->ai_common.love[player_index(aplayer)]);
436  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer,
437  "ceasefire worth=%d love=%d "
438  "turns=%d",
439  worth, pplayer->ai_common.love[player_index(aplayer)],
440  turns);
441  }
442  }
443  }
444 
445  // Let's all hold hands in one happy family!
446  if (adip->is_allied_with_ally) {
447  worth /= 2;
448  break;
449  }
450 
451  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "treaty clause worth %d",
452  worth);
453  break;
454 
455  case CLAUSE_GOLD:
456  if (give) {
457  worth -= pclause->value;
458  } else {
459  worth += pclause->value * (100 - game.server.diplgoldcost) / 100;
460  }
461  break;
462 
463  case CLAUSE_SEAMAP:
464  if (!give || ds_after == DS_ALLIANCE) {
465  // Useless to us - we're omniscient! And allies get it for free!
466  worth = 0;
467  } else {
468  /* Very silly algorithm 1: Sea map more worth if enemy has more
469  cities. Reasoning is he has more use of seamap for settling
470  new areas the more cities he has already. */
471  worth -= 15 * city_list_size(aplayer->cities);
472  // Don't like him? Don't give him!
473  worth = MIN(pplayer->ai_common.love[player_index(aplayer)] * 7, worth);
474  // Make maps from novice player cheap
475  if (has_handicap(pplayer, H_DIPLOMACY)) {
476  worth /= 2;
477  }
478  }
479  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "seamap clause worth %d",
480  worth);
481  break;
482 
483  case CLAUSE_MAP:
484  if (!give || ds_after == DS_ALLIANCE) {
485  // Useless to us - we're omniscient! And allies get it for free!
486  worth = 0;
487  } else {
488  /* Very silly algorithm 2: Land map more worth the more cities
489  we have, since we expose all of these to the enemy. */
490  worth -= 40 * MAX(city_list_size(pplayer->cities), 1);
491  // Inflate numbers if not peace
492  if (!pplayers_in_peace(pplayer, aplayer)) {
493  worth *= 2;
494  }
495  // Don't like him? Don't give him!
496  worth =
497  MIN(pplayer->ai_common.love[player_index(aplayer)] * 10, worth);
498  // Make maps from novice player cheap
499  if (has_handicap(pplayer, H_DIPLOMACY)) {
500  worth /= 6;
501  }
502  }
503  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "landmap clause worth %d",
504  worth);
505  break;
506 
507  case CLAUSE_CITY: {
508  struct city *offer =
509  city_list_find_number(pclause->from->cities, pclause->value);
510 
511  if (!offer || city_owner(offer) != giver) {
512  // City destroyed or taken during negotiations
513  dai_diplo_notify(aplayer,
514  _("*%s (AI)* I do not know the city you mention."),
515  player_name(pplayer));
516  worth = 0;
517  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer,
518  "city destroyed during negotiations");
519  } else if (give) {
520  // AI must be crazy to trade away its cities
521  worth -= city_gold_worth(offer);
522  if (is_capital(offer)) {
523  worth = -BIG_NUMBER; // Never! Ever!
524  } else {
525  worth *= 15;
526  }
527  if (aplayer == offer->original) {
528  // Let them buy back their own city cheaper.
529  worth /= 2;
530  }
531  } else {
532  worth = city_gold_worth(offer);
533  }
534  if (offer != nullptr) {
535  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "worth of %s is %d",
536  city_name_get(offer), worth);
537  }
538  break;
539  }
540 
541  case CLAUSE_VISION:
542  if (give) {
543  if (ds_after == DS_ALLIANCE) {
544  if (!shared_vision_is_safe(pplayer, aplayer)) {
545  dai_diplo_notify(aplayer,
546  _("*%s (AI)* Sorry, sharing vision with you "
547  "is not safe."),
548  player_name(pplayer));
549  worth = -BIG_NUMBER;
550  } else {
551  worth = 0;
552  }
553  } else {
554  // so out of the question
555  worth = -BIG_NUMBER;
556  }
557  } else {
558  worth = 0; // We are omniscient, so...
559  }
560  break;
561 
562  case CLAUSE_EMBASSY:
563  if (give) {
564  if (ds_after == DS_ALLIANCE) {
565  worth = 0;
566  } else if (ds_after == DS_PEACE) {
567  worth = -5 * game.info.turn;
568  } else {
569  worth = MIN(-50 * game.info.turn
570  + pplayer->ai_common.love[player_index(aplayer)],
571  -5 * game.info.turn);
572  }
573  } else if (game.info.tech_leakage == TECH_LEAKAGE_EMBASSIES) {
574  worth = game.info.tech_leak_pct / 10;
575  } else {
576  worth = 0; // We don't need no stinkin' embassy, do we?
577  }
578  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "embassy clause worth %d",
579  worth);
580  break;
581  case CLAUSE_COUNT:
582  fc_assert(pclause->type != CLAUSE_COUNT);
583  break;
584  } // end of switch
585 
586  if (close_here) {
587  dai_data_phase_finished(ait, pplayer);
588  }
589 
590  diplomacy_verbose = true;
591  return worth;
592 }
593 
598 void dai_treaty_evaluate(struct ai_type *ait, struct player *pplayer,
599  struct player *aplayer, struct Treaty *ptreaty)
600 {
601  int total_balance = 0;
602  bool only_gifts = true;
603  enum diplstate_type ds_after =
604  player_diplstate_get(pplayer, aplayer)->type;
605  int given_cities = 0;
606 
607  clause_list_iterate(ptreaty->clauses, pclause)
608  {
609  if (is_pact_clause(pclause->type)) {
610  ds_after = pact_clause_to_diplstate_type(pclause->type);
611  }
612  if (pclause->type == CLAUSE_CITY && pclause->from == pplayer) {
613  given_cities++;
614  }
615  }
617 
618  // Evaluate clauses
619  clause_list_iterate(ptreaty->clauses, pclause)
620  {
621  const struct research *presearch = research_get(pplayer);
622 
623  total_balance +=
624  dai_goldequiv_clause(ait, pplayer, aplayer, pclause, true, ds_after);
625 
626  if (pclause->type != CLAUSE_GOLD && pclause->type != CLAUSE_MAP
627  && pclause->type != CLAUSE_SEAMAP && pclause->type != CLAUSE_VISION
628  && (pclause->type != CLAUSE_ADVANCE
629  || game.info.tech_cost_style != TECH_COST_CIV1CIV2
630  || pclause->value == research_get(pplayer)->tech_goal
631  || pclause->value == research_get(pplayer)->researching
632  || research_goal_tech_req(presearch, presearch->tech_goal,
633  pclause->value))) {
634  /* We accept the above list of clauses as gifts, even if we are
635  * at war. We do not accept tech or cities since these can be used
636  * against us, unless we know that we want this tech anyway, or
637  * it doesn't matter due to tech cost style. */
638  only_gifts = false;
639  }
640  }
642 
643  /* If we are at war, and no peace is offered, then no deal, unless
644  * it is just gifts, in which case we gratefully accept. */
645  if (ds_after == DS_WAR && !only_gifts) {
646  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer,
647  "no peace offered, must refuse");
648  return;
649  }
650 
651  if (given_cities > 0
652  && city_list_size(pplayer->cities) - given_cities <= 2) {
653  // always keep at least two cities
654  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "cannot give last cities");
655  return;
656  }
657 
658  // Accept if balance is good
659  if (total_balance >= 0) {
661  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "balance was good: %d",
662  total_balance);
663  } else {
664  /* AI complains about the treaty which was proposed, unless the AI
665  * made the proposal. */
666  if (pplayer != ptreaty->plr0) {
668  aplayer, _("*%s (AI)* This deal was not very good for us, %s!"),
669  player_name(pplayer), player_name(aplayer));
670  }
671  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "balance was bad: %d",
672  total_balance);
673  }
674 }
675 
680 static void dai_treaty_react(struct ai_type *ait, struct player *pplayer,
681  struct player *aplayer, struct Clause *pclause)
682 {
683  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
684 
685  switch (pclause->type) {
686  case CLAUSE_ALLIANCE:
687  if (adip->is_allied_with_ally) {
688  dai_diplo_notify(aplayer, _("*%s (AI)* Welcome into our alliance %s!"),
689  player_name(pplayer), player_name(aplayer));
690  } else {
691  dai_diplo_notify(aplayer,
692  _("*%s (AI)* Yes, may we forever stand united, %s."),
693  player_name(pplayer), player_name(aplayer));
694  }
695  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "become allies");
696  break;
697  case CLAUSE_PEACE:
698  dai_diplo_notify(aplayer, _("*%s (AI)* Yes, peace in our time!"),
699  player_name(pplayer));
700  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "sign peace treaty");
701  break;
702  case CLAUSE_CEASEFIRE:
703  dai_diplo_notify(aplayer,
704  _("*%s (AI)* Agreed. No more hostilities, %s."),
705  player_name(pplayer), player_name(aplayer));
706  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "sign ceasefire");
707  break;
708  default:
709  break;
710  }
711 }
712 
720 void dai_treaty_accepted(struct ai_type *ait, struct player *pplayer,
721  struct player *aplayer, struct Treaty *ptreaty)
722 {
723  bool close_here;
724  struct ai_plr *ai;
725  int total_balance = 0;
726  bool gift = true;
727  enum diplstate_type ds_after =
728  player_diplstate_get(pplayer, aplayer)->type;
729 
730  ai = dai_plr_data_get(ait, pplayer, &close_here);
731 
732  fc_assert_ret(pplayer != aplayer);
733 
734  clause_list_iterate(ptreaty->clauses, pclause)
735  {
736  if (is_pact_clause(pclause->type)) {
737  ds_after = pact_clause_to_diplstate_type(pclause->type);
738  }
739  }
741 
742  // Evaluate clauses
743  clause_list_iterate(ptreaty->clauses, pclause)
744  {
745  int balance =
746  dai_goldequiv_clause(ait, pplayer, aplayer, pclause, true, ds_after);
747 
748  total_balance += balance;
749  gift = (gift && (balance >= 0));
750  dai_treaty_react(ait, pplayer, aplayer, pclause);
751  if (is_pact_clause(pclause->type)
752  && dai_diplomacy_get(ait, pplayer, aplayer)->countdown != -1) {
753  // Cancel a countdown towards war if we just agreed to peace...
754  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer, "countdown nullified");
755  dai_diplomacy_get(ait, pplayer, aplayer)->countdown = -1;
756  }
757  }
759 
760  /* Rather arbitrary algorithm to increase our love for a player if
761  * he or she offers us gifts. It is only a gift if _all_ the clauses
762  * are beneficial to us. */
763  if (total_balance > 0 && gift) {
764  int i = total_balance / ((city_list_size(pplayer->cities) * 10) + 1);
765 
766  i = MIN(i, ai->diplomacy.love_incr * 150) * 10;
767  pplayer->ai_common.love[player_index(aplayer)] += i;
768  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "gift increased love by %d",
769  i);
770  }
771 
772  if (close_here) {
773  dai_data_phase_finished(ait, pplayer);
774  }
775 }
776 
784 static int dai_war_desire(struct ai_type *ait, struct player *pplayer,
785  struct player *target)
786 {
787  struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
788  struct adv_data *adv = adv_data_get(pplayer, nullptr);
789  int want = 0, fear = 0, distance = 0, settlers = 0, cities = 0;
790  struct player_spaceship *ship = &target->spaceship;
791 
792  city_list_iterate(target->cities, pcity)
793  {
794  want += 100; // base city want
795  want += city_size_get(pcity) * 20;
796  want += pcity->surplus[O_SHIELD] * 8;
797  want += pcity->surplus[O_TRADE] * 6;
798 
799  // FIXME: This might be buggy if it ignores unmet UnitClass reqs.
800  fear += get_city_bonus(pcity, EFT_DEFEND_BONUS);
801 
802  city_built_iterate(pcity, pimprove)
803  {
804  int cost = impr_build_shield_cost(pcity, pimprove);
805 
806  want += cost;
807  if (improvement_obsolete(pplayer, pimprove, pcity)) {
808  continue;
809  }
810  if (is_great_wonder(pimprove)) {
811  want += cost * 2;
812  } else if (is_small_wonder(pimprove)) {
813  want += cost;
814  }
815  }
817  }
819  unit_list_iterate(target->units, punit)
820  {
821  const struct unit_type *ptype = unit_type_get(punit);
822 
823  fear += ATTACK_POWER(ptype);
824 
825  // Fear enemy expansionism
826  if (unit_is_cityfounder(punit)) {
827  want += 100;
828  }
829  }
831  unit_list_iterate(pplayer->units, punit)
832  {
833  const struct unit_type *ptype = unit_type_get(punit);
834 
835  fear -= ATTACK_POWER(ptype) / 2;
836 
837  // Our own expansionism reduces want for war
838  if (unit_is_cityfounder(punit)) {
839  want -= 200;
840  settlers++;
841  }
842  }
844  city_list_iterate(pplayer->cities, pcity)
845  {
846  if (VUT_UTYPE == pcity->production.kind
847  && utype_is_cityfounder(pcity->production.value.utype)) {
848  want -= 150;
849  settlers++;
850  }
851  cities++;
852  }
854 
855  /* Modify by settler/cities ratio to prevent early wars when
856  * we should be expanding. This will eliminate want if we
857  * produce settlers in all cities (ie full expansion). */
858  want -= abs(want) / MAX(cities - settlers, 1);
859 
860  // Calculate average distances to other player's empire.
861  distance = player_distance_to_player(pplayer, target);
862  dai_diplomacy_get(ait, pplayer, target)->distance = distance;
863 
864  /* Worry a bit if the other player has extreme amounts of wealth
865  * that can be used in cities to quickly buy an army. */
866  fear += (target->economic.gold / 5000) * city_list_size(target->cities);
867 
868  // Tech lead is worrisome. FIXME: Only consider 'military' techs.
869  fear += MAX(research_get(target)->techs_researched
870  - research_get(pplayer)->techs_researched,
871  0)
872  * 100;
873 
874  // Spacerace loss we will not allow!
875  if (ship->state >= SSHIP_STARTED) {
876  want *= 2;
877  }
878  if (adv->dipl.spacerace_leader == target) {
880  return BIG_NUMBER; // do NOT amortize this number!
881  }
882 
883  /* Modify by which treaties we would break to other players, and what
884  * excuses we have to do so. FIXME: We only consider immediate
885  * allies, but we might trigger a wider chain reaction. */
886  players_iterate_alive(eplayer)
887  {
888  bool cancel_excuse =
889  player_diplstate_get(pplayer, eplayer)->has_reason_to_cancel != 0;
890  enum diplstate_type ds = player_diplstate_get(pplayer, eplayer)->type;
891 
892  if (eplayer == pplayer) {
893  continue;
894  }
895 
896  // Remember: pplayers_allied() returns true when target == eplayer
897  if (!cancel_excuse && pplayers_allied(target, eplayer)) {
898  if (ds == DS_ARMISTICE) {
899  want -= abs(want) / 10; // 10% off
900  } else if (ds == DS_CEASEFIRE) {
901  want -= abs(want) / 7; // 15% off
902  } else if (ds == DS_PEACE) {
903  want -= abs(want) / 5; // 20% off
904  } else if (ds == DS_ALLIANCE) {
905  want -= abs(want) / 3; // 33% off
906  }
907  }
908  }
910 
911  // Modify by love. Increase the divisor to make ai go to war earlier
912  want -= MAX(0, want * pplayer->ai_common.love[player_index(target)]
913  / (2 * MAX_AI_LOVE));
914 
915  // Make novice AI more peaceful with human players
916  if (has_handicap(pplayer, H_DIPLOMACY) && is_human(target)) {
917  want /= 2;
918  }
919 
920  // Amortize by distance
921  want = amortize(want, distance);
922 
923  if (pplayers_allied(pplayer, target)) {
924  want /= 4;
925  }
926 
927  DIPLO_LOG(ait, LOG_DEBUG, pplayer, target, "War want %d, war fear %d",
928  want, fear);
929  return (want - fear);
930 }
931 
937 static void dai_diplomacy_suggest(struct player *pplayer,
938  struct player *aplayer,
939  enum clause_type what, bool to_pplayer,
940  int value)
941 {
942  if (!could_meet_with_player(pplayer, aplayer)) {
943  log_base(LOG_DIPL2, "%s tries to do diplomacy to %s without contact",
944  player_name(pplayer), player_name(aplayer));
945  return;
946  }
947 
950  pplayer, player_number(aplayer),
951  player_number(to_pplayer ? aplayer : pplayer), what, value);
952 }
953 
957 void dai_diplomacy_first_contact(struct ai_type *ait, struct player *pplayer,
958  struct player *aplayer)
959 {
960  bool wants_ceasefire = false;
961 
962  // Randomize initial love
963  pplayer->ai_common.love[player_index(aplayer)] += 2 - fc_rand(5);
964 
965  if (is_ai(pplayer)
966  && player_diplstate_get(pplayer, aplayer)->type == DS_WAR
967  && could_meet_with_player(pplayer, aplayer)) {
968  if (has_handicap(pplayer, H_CEASEFIRE)) {
969  fc_assert(!has_handicap(pplayer, H_AWAY));
970  wants_ceasefire = true;
971  } else if (!has_handicap(pplayer, H_AWAY)) {
972  struct Clause clause;
973 
974  clause.from = pplayer;
975  clause.value = 0;
976  clause.type = CLAUSE_CEASEFIRE;
977 
978  if (dai_goldequiv_clause(ait, pplayer, aplayer, &clause, false,
979  DS_CEASEFIRE)
980  > 0) {
981  wants_ceasefire = true;
982  }
983  }
984  }
985 
986  if (wants_ceasefire) {
987  dai_diplo_notify(aplayer,
988  _("*%s (AI)* Greetings %s! May we suggest a ceasefire "
989  "while we get to know each other better?"),
990  player_name(pplayer), player_name(aplayer));
991  clear_old_treaty(pplayer, aplayer);
992  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_CEASEFIRE, false, 0);
993  } else {
994  dai_diplo_notify(aplayer,
995  _("*%s (AI)* I found you %s! Now make it worth my "
996  "letting you live, or be crushed."),
997  player_name(pplayer), player_name(aplayer));
998  }
999 }
1000 
1011  struct player *pplayer)
1012 {
1013  struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
1014  struct adv_data *adv = adv_data_get(pplayer, nullptr);
1015  int war_desire[MAX_NUM_PLAYER_SLOTS];
1016  int best_desire = 0;
1017  struct player *best_target = nullptr;
1018 
1019  fc_assert_ret(is_ai(pplayer));
1020 
1021  if (!pplayer->is_alive) {
1022  return; // duh
1023  }
1024 
1025  memset(war_desire, 0, sizeof(war_desire));
1026 
1027  // Calculate our desires, and find desired war target
1028  players_iterate_alive(aplayer)
1029  {
1030  // We don't hate ourselves, those we don't know or team members.
1031  if (aplayer == pplayer || NEVER_MET(pplayer, aplayer)
1032  || players_on_same_team(pplayer, aplayer)) {
1033  continue;
1034  }
1035  war_desire[player_index(aplayer)] =
1036  dai_war_desire(ait, pplayer, aplayer);
1037  if (war_desire[player_index(aplayer)] > best_desire) {
1038  best_desire = war_desire[player_index(aplayer)];
1039  best_target = aplayer;
1040  }
1041  }
1043 
1044  /* Time to make love. If we've been wronged, hold off that love
1045  * for a while. Also, cool our head each turn with love_coeff. */
1046  players_iterate_alive(aplayer)
1047  {
1048  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1049  int amount = 0;
1050 
1051  if (pplayer == aplayer) {
1052  continue;
1053  }
1054 
1055  if ((pplayers_non_attack(pplayer, aplayer)
1056  || pplayers_allied(pplayer, aplayer))
1057  && player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel == 0
1058  && adip->countdown == -1 && !adip->is_allied_with_enemy
1059  && !adip->at_war_with_ally && aplayer != best_target
1060  && adip->ally_patience >= 0) {
1061  amount += ai->diplomacy.love_incr / 2;
1062  if (pplayers_allied(pplayer, aplayer)) {
1063  amount += ai->diplomacy.love_incr / 3;
1064  }
1065  // Increase love by each enemy he is at war with
1066  players_iterate(eplayer)
1067  {
1068  if (WAR(eplayer, aplayer) && WAR(pplayer, eplayer)) {
1069  amount += ai->diplomacy.love_incr / 4;
1070  }
1071  }
1073  pplayer->ai_common.love[player_index(aplayer)] += amount;
1074  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "Increased love by %d",
1075  amount);
1076  } else if (WAR(pplayer, aplayer)) {
1077  amount -= ai->diplomacy.love_incr / 2;
1078  pplayer->ai_common.love[player_index(aplayer)] += amount;
1079  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "%d love lost to war",
1080  amount);
1081  } else if (player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel
1082  != 0) {
1083  // Provoked in time of peace
1084  if (pplayer->ai_common.love[player_index(aplayer)] > 0) {
1085  amount -= pplayer->ai_common.love[player_index(aplayer)] / 2;
1086  }
1087  amount -= ai->diplomacy.love_incr * 6;
1088  pplayer->ai_common.love[player_index(aplayer)] += amount;
1089  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "Provoked! %d love lost!",
1090  amount);
1091  }
1092  if (pplayer->ai_common.love[player_index(aplayer)] > MAX_AI_LOVE * 8 / 10
1093  && !pplayers_allied(pplayer, aplayer)) {
1094  int love_change = ai->diplomacy.love_incr / 3;
1095 
1096  // Upper levels of AI trust and love is reserved for allies.
1097  pplayer->ai_common.love[player_index(aplayer)] -= love_change;
1098  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer, "%d love lost from excess",
1099  love_change);
1100  }
1101  amount = 0;
1102 
1103  /* Reduce love due to units in our territory.
1104  * AI is so naive, that we have to count it even if players are allied */
1105  amount -=
1106  MIN(player_in_territory(pplayer, aplayer) * (MAX_AI_LOVE / 200),
1107  ai->diplomacy.love_incr
1108  * ((adip->is_allied_with_enemy != nullptr) + 1));
1109  pplayer->ai_common.love[player_index(aplayer)] += amount;
1110  if (amount != 0) {
1111  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer,
1112  "%d love lost due to units inside "
1113  "our borders",
1114  amount);
1115  }
1116 
1117  /* Increase the love if aplayer has got a building that makes
1118  * us love him more. Typically it's Eiffel Tower */
1119  if (!NEVER_MET(pplayer, aplayer)) {
1120  pplayer->ai_common.love[player_index(aplayer)] +=
1121  get_player_bonus(aplayer, EFT_GAIN_AI_LOVE) * MAX_AI_LOVE / 1000;
1122  }
1123  }
1125 
1126  // Can we win by space race?
1127  if (adv->dipl.spacerace_leader == pplayer) {
1128  log_base(LOG_DIPL2, "%s going for space race victory!",
1129  player_name(pplayer));
1130  ai->diplomacy.strategy = WIN_SPACE; // Yes!
1131  } else {
1132  if (ai->diplomacy.strategy == WIN_SPACE) {
1133  ai->diplomacy.strategy = WIN_OPEN;
1134  }
1135  }
1136 
1137  players_iterate(aplayer)
1138  {
1139  int *love = &pplayer->ai_common.love[player_index(aplayer)];
1140 
1141  if (aplayer == best_target && best_desire > 0) {
1142  int reduction = MIN(best_desire, MAX_AI_LOVE / 20);
1143 
1144  *love -= reduction;
1145  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer,
1146  "Wants war, reducing "
1147  "love by %d ",
1148  reduction);
1149  }
1150 
1151  // Edge love towards zero
1152  *love -= *love * (static_cast<double>(ai->diplomacy.love_coeff) / 100.0);
1153 
1154  // ai love should always be in range [-MAX_AI_LOVE..MAX_AI_LOVE]
1155  *love = MAX(-MAX_AI_LOVE, MIN(MAX_AI_LOVE, *love));
1156  }
1158 }
1159 
1163 static void suggest_tech_exchange(struct ai_type *ait,
1164  struct player *player1,
1165  struct player *player2)
1166 {
1167  struct research *presearch1 = research_get(player1);
1168  struct research *presearch2 = research_get(player2);
1169  int worth[advance_count()];
1170  bool is_dangerous;
1171 
1172  worth[A_NONE] = 0;
1173 
1175  {
1176  if (research_invention_state(presearch1, tech) == TECH_KNOWN) {
1177  if (research_invention_state(presearch2, tech) != TECH_KNOWN
1178  && research_invention_gettable(presearch2, tech,
1179  game.info.tech_trade_allow_holes)) {
1180  worth[tech] = -compute_tech_sell_price(ait, player1, player2, tech,
1181  &is_dangerous);
1182  if (is_dangerous) {
1183  // don't try to exchange
1184  worth[tech] = 0;
1185  }
1186  } else {
1187  worth[tech] = 0;
1188  }
1189  } else {
1190  if (research_invention_state(presearch2, tech) == TECH_KNOWN
1191  && research_invention_gettable(presearch1, tech,
1192  game.info.tech_trade_allow_holes)) {
1193  worth[tech] = compute_tech_sell_price(ait, player2, player1, tech,
1194  &is_dangerous);
1195  if (is_dangerous) {
1196  // don't try to exchange
1197  worth[tech] = 0;
1198  }
1199  } else {
1200  worth[tech] = 0;
1201  }
1202  }
1203  }
1205 
1207  {
1208  if (worth[tech] <= 0) {
1209  continue;
1210  }
1212  {
1213  int diff;
1214 
1215  if (worth[tech2] >= 0) {
1216  continue;
1217  }
1218  // tech2 is given by player1, tech is given by player2
1219  diff = worth[tech] + worth[tech2];
1220  if ((diff > 0 && player1->economic.gold >= diff)
1221  || (diff < 0 && player2->economic.gold >= -diff) || diff == 0) {
1222  dai_diplomacy_suggest(player1, player2, CLAUSE_ADVANCE, false,
1223  tech2);
1224  dai_diplomacy_suggest(player1, player2, CLAUSE_ADVANCE, true, tech);
1225  if (diff > 0) {
1226  dai_diplomacy_suggest(player1, player2, CLAUSE_GOLD, false, diff);
1227  } else if (diff < 0) {
1228  dai_diplomacy_suggest(player1, player2, CLAUSE_GOLD, true, -diff);
1229  }
1230  return;
1231  }
1232  }
1234  }
1236 }
1237 
1241 static void clear_old_treaty(struct player *pplayer, struct player *aplayer)
1242 {
1243  struct Treaty *old_treaty = find_treaty(pplayer, aplayer);
1244 
1245  if (old_treaty != nullptr) {
1246  // Remove existing clauses
1247  clause_list_iterate(old_treaty->clauses, pclause)
1248  {
1249  dlsend_packet_diplomacy_remove_clause(
1250  aplayer->connections, player_number(pplayer),
1251  player_number(pclause->from), pclause->type, pclause->value);
1252  delete pclause;
1253  }
1255  clause_list_destroy(old_treaty->clauses);
1256  old_treaty->clauses = clause_list_new();
1257  }
1258 }
1259 
1263 static void dai_share(struct ai_type *ait, struct player *pplayer,
1264  struct player *aplayer)
1265 {
1266  struct research *presearch = research_get(pplayer);
1267  struct research *aresearch = research_get(aplayer);
1268  bool gives_vision;
1269 
1270  clear_old_treaty(pplayer, aplayer);
1271 
1272  // Only share techs with team mates
1273  if (presearch != aresearch && players_on_same_team(pplayer, aplayer)) {
1275  {
1276  if (research_invention_state(presearch, idx) != TECH_KNOWN
1277  && research_invention_state(aresearch, idx) == TECH_KNOWN
1278  && research_invention_gettable(presearch, idx,
1279  game.info.tech_trade_allow_holes)) {
1280  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_ADVANCE, true, idx);
1281  } else if (research_invention_state(presearch, idx) == TECH_KNOWN
1282  && research_invention_state(aresearch, idx) != TECH_KNOWN
1284  aresearch, idx, game.info.tech_trade_allow_holes)) {
1285  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_ADVANCE, false, idx);
1286  }
1287  }
1289  }
1290 
1291  // Only give shared vision if safe. Only ask for shared vision if fair.
1292  gives_vision = gives_shared_vision(pplayer, aplayer);
1293  if (!gives_vision && shared_vision_is_safe(pplayer, aplayer)) {
1294  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_VISION, false, 0);
1295  gives_vision = true;
1296  }
1297  if (gives_vision && !gives_shared_vision(aplayer, pplayer)
1298  && (is_human(aplayer) || shared_vision_is_safe(aplayer, pplayer))) {
1299  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_VISION, true, 0);
1300  }
1301 
1302  if (!player_has_embassy(pplayer, aplayer)) {
1303  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_EMBASSY, true, 0);
1304  }
1305  if (!player_has_embassy(aplayer, pplayer)) {
1306  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_EMBASSY, false, 0);
1307  }
1308 
1309  if (!has_handicap(pplayer, H_DIPLOMACY) || is_human(aplayer)) {
1310  suggest_tech_exchange(ait, pplayer, aplayer);
1311  }
1312 }
1313 
1319 static void dai_go_to_war(struct ai_type *ait, struct player *pplayer,
1320  struct player *target, enum war_reason reason)
1321 {
1322  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, target);
1323 
1324  fc_assert_ret(pplayer != target);
1325  fc_assert_ret(target->is_alive);
1326 
1327  switch (reason) {
1328  case DAI_WR_SPACE:
1329  dai_diplo_notify(target, _("*%s (AI)* Space will never be yours. "),
1330  player_name(pplayer));
1331  adip->countdown = -10;
1332  break;
1333  case DAI_WR_BEHAVIOUR:
1334  dai_diplo_notify(target,
1335  _("*%s (AI)* I have tolerated your vicious antics "
1336  "long enough! To war!"),
1337  player_name(pplayer));
1338  adip->countdown = -20;
1339  break;
1340  case DAI_WR_NONE:
1341  dai_diplo_notify(target, _("*%s (AI)* Peace in ... some other time."),
1342  player_name(pplayer));
1343  adip->countdown = -10;
1344  break;
1345  case DAI_WR_HATRED:
1347  target,
1348  _("*%s (AI)* Finally I get around to you! Did "
1349  "you really think you could get away with your crimes?"),
1350  player_name(pplayer));
1351  adip->countdown = -20;
1352  break;
1353  case DAI_WR_EXCUSE:
1354  dai_diplo_notify(target,
1355  _("*%s (AI)* Your covert hostilities brought "
1356  "this war upon you!"),
1357  player_name(pplayer));
1358  adip->countdown = -20;
1359  break;
1360  case DAI_WR_ALLIANCE:
1361  if (adip->at_war_with_ally) {
1362  dai_diplo_notify(target,
1363  _("*%s (AI)* Your aggression against %s was "
1364  "your last mistake!"),
1365  player_name(pplayer),
1366  player_name(adip->at_war_with_ally));
1367  adip->countdown = -3;
1368  } else {
1369  // Ooops!
1370  DIPLO_LOG(ait, LOG_DIPL, pplayer, target,
1371  "Wanted to declare war "
1372  "for his war against an ally, but can no longer find "
1373  "this ally! War declaration aborted.");
1374  adip->countdown = -1;
1375  return;
1376  }
1377  break;
1378  }
1379 
1380  fc_assert_ret(adip->countdown < 0);
1381 
1382  if (gives_shared_vision(pplayer, target)) {
1383  remove_shared_vision(pplayer, target);
1384  }
1385 
1386  // Check for Senate obstruction. If so, dissolve it.
1387  if (pplayer_can_cancel_treaty(pplayer, target) == DIPL_SENATE_BLOCKING) {
1389  pplayer, game.info.government_during_revolution_id);
1390  }
1391 
1392  // This will take us straight to war.
1393  while (player_diplstate_get(pplayer, target)->type != DS_WAR) {
1394  if (pplayer_can_cancel_treaty(pplayer, target) != DIPL_OK) {
1395  DIPLO_LOG(ait, LOG_ERROR, pplayer, target,
1396  "Wanted to cancel treaty but "
1397  "was unable to.");
1398  return;
1399  }
1401  clause_type_invalid());
1402  }
1403 
1404  // Throw a tantrum
1405  if (pplayer->ai_common.love[player_index(target)] > 0) {
1406  pplayer->ai_common.love[player_index(target)] = -1;
1407  }
1408  pplayer->ai_common.love[player_index(target)] -= MAX_AI_LOVE / 8;
1409 
1410  fc_assert(!gives_shared_vision(pplayer, target));
1411  DIPLO_LOG(ait, LOG_DIPL, pplayer, target, "war declared");
1412 }
1413 
1426 static void war_countdown(struct ai_type *ait, struct player *pplayer,
1427  struct player *target, int countdown,
1428  enum war_reason reason)
1429 {
1430  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, target);
1431 
1432  DIPLO_LOG(ait, LOG_DIPL, pplayer, target, "countdown to war in %d",
1433  countdown);
1434 
1435  // Otherwise we're resetting an existing countdown, which is very bad
1436  fc_assert_ret(adip->countdown == -1);
1437 
1438  adip->countdown = countdown;
1439  adip->war_reason = reason;
1440 
1441  players_iterate_alive(ally)
1442  {
1443  if (!pplayers_allied(pplayer, ally) || ally == target
1444  || NEVER_MET(pplayer, ally)) {
1445  continue;
1446  }
1447 
1448  switch (reason) {
1449  case DAI_WR_SPACE:
1450  dai_diplo_notify(ally,
1451  PL_("*%s (AI)* We will be launching an all-out war "
1452  "against %s in %d turn to stop the spaceship "
1453  "launch.",
1454  "*%s (AI)* We will be launching an all-out war "
1455  "against %s in %d turns to stop the spaceship "
1456  "launch.",
1457  countdown),
1458  player_name(pplayer), player_name(target), countdown);
1460  ally,
1461  _("*%s (AI)* Your aid in this matter will be expected. "
1462  "Long live our glorious alliance!"),
1463  player_name(pplayer));
1464  break;
1465  case DAI_WR_BEHAVIOUR:
1466  case DAI_WR_EXCUSE:
1468  ally,
1469  PL_("*%s (AI)* %s has grossly violated his treaties "
1470  "with us for own gain. We will answer in force in "
1471  "%d turn and expect you to honor your alliance "
1472  "with us and do likewise!",
1473  "*%s (AI)* %s has grossly violated his treaties "
1474  "with us for own gain. We will answer in force in "
1475  "%d turns and expect you to honor your alliance "
1476  "with us and do likewise!",
1477  countdown),
1478  player_name(pplayer), player_name(target), countdown);
1479  break;
1480  case DAI_WR_NONE:
1482  ally,
1483  PL_("*%s (AI)* We intend to pillage and plunder the rich "
1484  "civilization of %s. We declare war in %d turn.",
1485  "*%s (AI)* We intend to pillage and plunder the rich "
1486  "civilization of %s. We declare war in %d turns.",
1487  countdown),
1488  player_name(pplayer), player_name(target), countdown);
1489  dai_diplo_notify(ally,
1490  _("*%s (AI)* If you want a piece of the loot, feel "
1491  "free to join in the action!"),
1492  player_name(pplayer));
1493  break;
1494  case DAI_WR_HATRED:
1496  ally,
1497  PL_("*%s (AI)* We have had it with %s. Let us tear this "
1498  "pathetic civilization apart. We declare war in "
1499  "%d turn.",
1500  "*%s (AI)* We have had it with %s. Let us tear this "
1501  "pathetic civilization apart. We declare war in "
1502  "%d turns.",
1503  countdown),
1504  player_name(pplayer), player_name(target), countdown);
1505  dai_diplo_notify(ally,
1506  _("*%s (AI)* As our glorious allies, we expect your "
1507  "help in this war."),
1508  player_name(pplayer));
1509  break;
1510  case DAI_WR_ALLIANCE:
1511  if (WAR(ally, target)) {
1513  ally,
1514  PL_("*%s (AI)* We will honor our alliance and declare "
1515  "war on %s in %d turn. Hold on - we are coming!",
1516  "*%s (AI)* We will honor our alliance and declare "
1517  "war on %s in %d turns. Hold on - we are coming!",
1518  countdown),
1519  player_name(pplayer), player_name(target), countdown);
1520  } else if (adip->at_war_with_ally) {
1522  ally,
1523  PL_("*%s (AI)* We will honor our alliance with %s and "
1524  "declare war on %s in %d turns. We expect you to "
1525  "do likewise.",
1526  "*%s (AI)* We will honor our alliance with %s and "
1527  "declare war on %s in %d turns. We expect you to "
1528  "do likewise.",
1529  countdown),
1530  player_name(pplayer), player_name(adip->at_war_with_ally),
1531  player_name(target), countdown);
1532  } else {
1533  fc_assert(false); // Huh?
1534  }
1535  break;
1536  }
1537  }
1539 }
1540 
1547 void dai_diplomacy_actions(struct ai_type *ait, struct player *pplayer)
1548 {
1549  struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
1550  bool need_targets = true;
1551  struct player *target = nullptr;
1552  int most_hatred = MAX_AI_LOVE;
1553  int war_threshold;
1554  int aggr;
1555  float aggr_sr;
1556  float max_sr;
1557 
1558  fc_assert_ret(is_ai(pplayer));
1559 
1560  if (!pplayer->is_alive) {
1561  return;
1562  }
1563 
1564  if (get_player_bonus(pplayer, EFT_NO_DIPLOMACY) > 0) {
1565  // Diplomacy disabled for this player
1566  return;
1567  }
1568 
1569  /*** If we are greviously insulted, go to war immediately. ***/
1570 
1571  players_iterate(aplayer)
1572  {
1573  if (pplayer->ai_common.love[player_index(aplayer)] < 0
1574  && player_diplstate_get(pplayer, aplayer)->has_reason_to_cancel >= 2
1575  && dai_diplomacy_get(ait, pplayer, aplayer)->countdown == -1) {
1576  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "Plans war in revenge");
1577  war_countdown(ait, pplayer, aplayer, map_size_checked(),
1578  DAI_WR_BEHAVIOUR);
1579  }
1580  }
1582 
1583  /*** Stop other players from winning by space race ***/
1584 
1585  if (ai->diplomacy.strategy != WIN_SPACE) {
1586  struct adv_data *adv = adv_data_get(pplayer, nullptr);
1587 
1588  players_iterate_alive(aplayer)
1589  {
1590  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1591  struct player_spaceship *ship = &aplayer->spaceship;
1592 
1593  if (aplayer == pplayer
1594  || adip->countdown >= 0 // already counting down to war
1595  || ship->state == SSHIP_NONE
1596  || players_on_same_team(pplayer, aplayer)
1597  || pplayers_at_war(pplayer, aplayer)) {
1598  continue;
1599  }
1600  /* A spaceship victory is always one single player's or team's victory
1601  */
1602  if (aplayer->spaceship.state == SSHIP_LAUNCHED
1603  && adv->dipl.spacerace_leader == aplayer
1604  && pplayers_allied(pplayer, aplayer)) {
1606  aplayer,
1607  _("*%s (AI)* Your attempt to conquer space for "
1608  "yourself alone betrays your true intentions, and I "
1609  "will have no more of our alliance!"),
1610  player_name(pplayer));
1611  handle_diplomacy_cancel_pact(pplayer, player_number(aplayer),
1612  CLAUSE_ALLIANCE);
1613  if (gives_shared_vision(pplayer, aplayer)) {
1614  remove_shared_vision(pplayer, aplayer);
1615  }
1616  // Never forgive this
1617  pplayer->ai_common.love[player_index(aplayer)] = -MAX_AI_LOVE;
1618  } else if (ship->state == SSHIP_STARTED
1619  && adip->warned_about_space == 0) {
1620  pplayer->ai_common.love[player_index(aplayer)] -= MAX_AI_LOVE / 10;
1621  adip->warned_about_space = 10 + fc_rand(6);
1622  dai_diplo_notify(aplayer,
1623  _("*%s (AI)* Your attempt to unilaterally "
1624  "dominate outer space is highly offensive."),
1625  player_name(pplayer));
1626  dai_diplo_notify(aplayer,
1627  _("*%s (AI)* If you do not stop constructing your "
1628  "spaceship, I may be forced to take action!"),
1629  player_name(pplayer));
1630  }
1631  if (aplayer->spaceship.state == SSHIP_LAUNCHED
1632  && aplayer == adv->dipl.spacerace_leader) {
1633  // This means war!!!
1634  pplayer->ai_common.love[player_index(aplayer)] -= MAX_AI_LOVE / 2;
1635  DIPLO_LOG(ait, LOG_DIPL, pplayer, aplayer,
1636  "plans war due to spaceship");
1637  war_countdown(ait, pplayer, aplayer, 4 + map_size_checked(),
1638  DAI_WR_SPACE);
1639  }
1640  }
1642  }
1643 
1644  /*** Declare war against somebody if we are out of targets ***/
1645 
1646  players_iterate_alive(aplayer)
1647  {
1648  int turns; // turns since contact
1649 
1650  if (NEVER_MET(pplayer, aplayer)) {
1651  continue;
1652  }
1653  turns = game.info.turn;
1654  turns -= player_diplstate_get(pplayer, aplayer)->first_contact_turn;
1655  if (WAR(pplayer, aplayer)) {
1656  need_targets = false;
1657  } else if (pplayer->ai_common.love[player_index(aplayer)] < most_hatred
1658  && turns > TURNS_BEFORE_TARGET) {
1659  most_hatred = pplayer->ai_common.love[player_index(aplayer)];
1660  target = aplayer;
1661  }
1662  }
1664 
1665  aggr = ai_trait_get_value(TRAIT_AGGRESSIVE, pplayer);
1666  max_sr = TRAIT_MAX_VALUE_SR;
1667  aggr_sr = sqrt(aggr);
1668 
1669  war_threshold =
1670  (MAX_AI_LOVE * (0.70 + aggr_sr / max_sr / 2.0)) - MAX_AI_LOVE;
1671 
1672  if (need_targets && target && most_hatred < war_threshold
1673  && dai_diplomacy_get(ait, pplayer, target)->countdown == -1) {
1674  enum war_reason war_reason;
1675 
1676  if (pplayers_allied(pplayer, target)) {
1677  DIPLO_LOG(ait, LOG_DEBUG, pplayer, target,
1678  "Plans war against an ally!");
1679  }
1680  if (player_diplstate_get(pplayer, target)->has_reason_to_cancel > 0) {
1681  // We have good reason
1682  war_reason = DAI_WR_EXCUSE;
1683  } else if (pplayer->ai_common.love[player_index(target)] < 0) {
1684  // We have a reason of sorts from way back, maybe?
1685  war_reason = DAI_WR_HATRED;
1686  } else {
1687  // We have no legimitate reason... So what?
1688  war_reason = DAI_WR_NONE;
1689  }
1690  DIPLO_LOG(ait, LOG_DEBUG, pplayer, target, "plans war for spoils");
1691  war_countdown(ait, pplayer, target, 4 + map_size_checked(), war_reason);
1692  }
1693 
1694  /*** Declare war - against enemies of allies ***/
1695 
1696  players_iterate_alive(aplayer)
1697  {
1698  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1699 
1700  if (adip->at_war_with_ally && adip->countdown == -1
1701  && !adip->is_allied_with_ally && !pplayers_at_war(pplayer, aplayer)
1702  && (player_diplstate_get(pplayer, aplayer)->type != DS_CEASEFIRE
1703  || fc_rand(5) < 1)) {
1704  DIPLO_LOG(ait, LOG_DEBUG, pplayer, aplayer,
1705  "plans war to help ally %s",
1706  player_name(adip->at_war_with_ally));
1707  war_countdown(ait, pplayer, aplayer, 2 + map_size_checked(),
1708  DAI_WR_ALLIANCE);
1709  }
1710  }
1712 
1713  /*** Actually declare war (when we have moved units into position) ***/
1714 
1715  players_iterate(aplayer)
1716  {
1717  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1718 
1719  if (!aplayer->is_alive) {
1720  adip->countdown = -1;
1721  continue;
1722  }
1723  if (adip->countdown > 0) {
1724  adip->countdown--;
1725  } else if (adip->countdown == 0) {
1726  if (!WAR(pplayer, aplayer)) {
1727  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer, "Declaring war!");
1728  dai_go_to_war(ait, pplayer, aplayer, adip->war_reason);
1729  }
1730  } else if (adip->countdown < -1) {
1731  // negative countdown less than -1 is war stubbornness
1732  adip->countdown++;
1733  }
1734  }
1736 
1737  /*** Try to make peace with everyone we love ***/
1738 
1739  players_iterate_alive(aplayer)
1740  {
1741  if (get_player_bonus(aplayer, EFT_NO_DIPLOMACY) <= 0
1742  && diplomacy_possible(pplayer, aplayer)) {
1743  enum diplstate_type ds = player_diplstate_get(pplayer, aplayer)->type;
1744  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
1745  struct Clause clause;
1746 
1747  // Meaningless values, but rather not have them unset.
1748  clause.from = pplayer;
1749  clause.value = 0;
1750 
1751  /* Remove shared vision if we are not allied or it is no longer safe.
1752  */
1753  if (gives_shared_vision(pplayer, aplayer)) {
1754  if (!pplayers_allied(pplayer, aplayer)) {
1755  remove_shared_vision(pplayer, aplayer);
1756  } else if (!shared_vision_is_safe(pplayer, aplayer)) {
1757  dai_diplo_notify(aplayer,
1758  _("*%s (AI)* Sorry, sharing vision with you "
1759  "is no longer safe."),
1760  player_name(pplayer));
1761  remove_shared_vision(pplayer, aplayer);
1762  }
1763  }
1764 
1765  // No peace to enemies of our allies... or pointless peace.
1766  if (is_barbarian(aplayer) || aplayer == pplayer
1767  || aplayer == target // no mercy
1768  || adip->countdown >= 0
1769  || !could_meet_with_player(pplayer, aplayer)) {
1770  continue;
1771  }
1772 
1773  // Spam control
1774  adip->asked_about_peace = MAX(adip->asked_about_peace - 1, 0);
1775  adip->asked_about_alliance = MAX(adip->asked_about_alliance - 1, 0);
1776  adip->asked_about_ceasefire = MAX(adip->asked_about_ceasefire - 1, 0);
1777  adip->warned_about_space = MAX(adip->warned_about_space - 1, 0);
1778  adip->spam = MAX(adip->spam - 1, 0);
1779  if (adip->spam > 0 && is_ai(aplayer)) {
1780  // Don't spam
1781  continue;
1782  }
1783 
1784  /* Canvass support from existing friends for our war, and try to
1785  * make friends with enemies. Then we wait some turns until next time
1786  * we spam them with our gibbering chatter. */
1787  if (adip->spam <= 0) {
1788  if (!pplayers_allied(pplayer, aplayer)) {
1789  adip->spam = fc_rand(4) + 3; // Bugger allies often.
1790  } else {
1791  adip->spam = fc_rand(8) + 6; // Others are less important.
1792  }
1793  }
1794 
1795  switch (ds) {
1796  case DS_TEAM:
1797  dai_share(ait, pplayer, aplayer);
1798  break;
1799  case DS_ALLIANCE:
1800  /* See if our allies are diligently declaring war on our enemies...
1801  */
1802  if (adip->at_war_with_ally) {
1803  break;
1804  }
1805  target = nullptr;
1806  players_iterate(eplayer)
1807  {
1808  if (WAR(pplayer, eplayer) && !pplayers_at_war(aplayer, eplayer)) {
1809  target = eplayer;
1810  break;
1811  }
1812  }
1814 
1815  if ((players_on_same_team(pplayer, aplayer)
1816  || pplayer->ai_common.love[player_index(aplayer)]
1817  > MAX_AI_LOVE / 2)) {
1818  // Share techs only with team mates and allies we really like.
1819  dai_share(ait, pplayer, aplayer);
1820  }
1821  if (!target || !target->is_alive) {
1822  adip->ally_patience = 0;
1823  break; // No need to nag our ally
1824  }
1825 
1826  if (adip->spam <= 0) {
1827  /* Count down patience toward AI player (one that can have spam >
1828  * 0) at the same speed as toward human players. */
1829  if (adip->ally_patience == 0) {
1831  aplayer,
1832  _("*%s (AI)* Greetings our most trustworthy "
1833  "ally. We call upon you to destroy our enemy, %s."),
1834  player_name(pplayer), player_name(target));
1835  adip->ally_patience--;
1836  } else if (adip->ally_patience == -1) {
1837  if (fc_rand(5) == 1) {
1839  aplayer,
1840  _("*%s (AI)* Greetings ally, I see you have not yet "
1841  "made war with our enemy, %s. Why do I need to remind "
1842  "you of your promises?"),
1843  player_name(pplayer), player_name(target));
1844  adip->ally_patience--;
1845  }
1846  } else {
1847  if (fc_rand(5) == 1) {
1849  aplayer,
1850  _("*%s (AI)* Dishonored one, we made a pact of "
1851  "alliance, and yet you remain at peace with our mortal "
1852  "enemy, %s! This is unacceptable; our alliance is no "
1853  "more!"),
1854  player_name(pplayer), player_name(target));
1855  DIPLO_LOG(ait, LOG_DIPL2, pplayer, aplayer,
1856  "breaking useless alliance");
1857  // to peace
1858  handle_diplomacy_cancel_pact(pplayer, player_number(aplayer),
1859  CLAUSE_ALLIANCE);
1860  pplayer->ai_common.love[player_index(aplayer)] =
1861  MIN(pplayer->ai_common.love[player_index(aplayer)], 0);
1862  if (gives_shared_vision(pplayer, aplayer)) {
1863  remove_shared_vision(pplayer, aplayer);
1864  }
1865  fc_assert(!gives_shared_vision(pplayer, aplayer));
1866  }
1867  }
1868  }
1869  break;
1870 
1871  case DS_PEACE:
1872  clause.type = CLAUSE_ALLIANCE;
1873  if (adip->at_war_with_ally
1874  || (is_human(aplayer) && adip->asked_about_alliance > 0)
1875  || dai_goldequiv_clause(ait, pplayer, aplayer, &clause, false,
1876  DS_ALLIANCE)
1877  < 0) {
1878  break;
1879  }
1880  clear_old_treaty(pplayer, aplayer);
1881  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_ALLIANCE, false, 0);
1882  adip->asked_about_alliance = is_human(aplayer) ? 13 : 0;
1883  dai_diplo_notify(aplayer,
1884  _("*%s (AI)* Greetings friend, may we suggest "
1885  "making a common cause and join in an alliance?"),
1886  player_name(pplayer));
1887  break;
1888 
1889  case DS_CEASEFIRE:
1890  clause.type = CLAUSE_PEACE;
1891  if (adip->at_war_with_ally
1892  || (is_human(aplayer) && adip->asked_about_peace > 0)
1893  || dai_goldequiv_clause(ait, pplayer, aplayer, &clause, false,
1894  DS_PEACE)
1895  < 0) {
1896  break;
1897  }
1898  clear_old_treaty(pplayer, aplayer);
1899  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_PEACE, false, 0);
1900  adip->asked_about_peace = is_human(aplayer) ? 12 : 0;
1901  dai_diplo_notify(aplayer,
1902  _("*%s (AI)* Greetings neighbor, may we suggest "
1903  "more peaceful relations?"),
1904  player_name(pplayer));
1905  break;
1906 
1907  case DS_NO_CONTACT: // but we do have embassy! weird.
1908  case DS_WAR:
1909  clause.type = CLAUSE_CEASEFIRE;
1910  if ((is_human(aplayer) && adip->asked_about_ceasefire > 0)
1911  || dai_goldequiv_clause(ait, pplayer, aplayer, &clause, false,
1912  DS_CEASEFIRE)
1913  < 0) {
1914  break; // Fight until the end!
1915  }
1916  clear_old_treaty(pplayer, aplayer);
1917  dai_diplomacy_suggest(pplayer, aplayer, CLAUSE_CEASEFIRE, false, 0);
1918  adip->asked_about_ceasefire = is_human(aplayer) ? 9 : 0;
1920  aplayer,
1921  _("*%s (AI)* We grow weary of this constant "
1922  "bloodshed. May we suggest a cessation of hostilities?"),
1923  player_name(pplayer));
1924  break;
1925 
1926  case DS_ARMISTICE:
1927  break;
1928  default:
1929  fc_assert_msg(false, "Unknown pact type %d.", ds);
1930  break;
1931  }
1932  }
1933  }
1935 }
1936 
1941 bool dai_on_war_footing(struct ai_type *ait, struct player *pplayer)
1942 {
1943  players_iterate(plr)
1944  {
1945  if (dai_diplomacy_get(ait, pplayer, plr)->countdown >= 0) {
1946  return true;
1947  }
1948  }
1950 
1951  return false;
1952 }
1953 
1957 // AI attitude call-backs
1958 void dai_incident(struct ai_type *ait, enum incident_type type,
1959  enum casus_belli_range scope, const struct action *paction,
1960  struct player *receiver, struct player *violator,
1961  struct player *victim)
1962 {
1963  if (!is_ai(receiver) || violator == receiver) {
1964  return;
1965  }
1966 
1967  // Can only handle victim only and international incidents.
1968  fc_assert_ret(scope == CBR_VICTIM_ONLY
1969  || scope == CBR_INTERNATIONAL_OUTRAGE);
1970 
1971  switch (type) {
1972  case INCIDENT_ACTION:
1973  /* Feel free the change how the action results are grouped and how bad
1974  * the ai considers each group. */
1975  switch (paction->result) {
1976  case ACTRES_SPY_NUKE:
1977  case ACTRES_NUKE:
1978  case ACTRES_NUKE_CITY:
1979  case ACTRES_NUKE_UNITS:
1980  if (receiver == victim) {
1981  // Tell the victim
1982  dai_incident_nuclear(receiver, violator, victim);
1983  } else if (violator == victim) {
1984  dai_incident_nuclear_self(receiver, violator, victim);
1985  } else {
1986  dai_incident_nuclear_not_target(receiver, violator, victim);
1987  }
1988  break;
1989  case ACTRES_ESTABLISH_EMBASSY:
1990  case ACTRES_SPY_INVESTIGATE_CITY:
1991  // Snoping
1992  dai_incident_simple(receiver, violator, victim, scope, 2);
1993  break;
1994  case ACTRES_SPY_STEAL_GOLD:
1995  case ACTRES_SPY_STEAL_TECH:
1996  case ACTRES_SPY_TARGETED_STEAL_TECH:
1997  case ACTRES_STEAL_MAPS:
1998  // Theft
1999  dai_incident_simple(receiver, violator, victim, scope, 5);
2000  break;
2001  case ACTRES_EXPEL_UNIT:
2002  // Unit position loss
2003  dai_incident_simple(receiver, violator, victim, scope, 1);
2004  break;
2005  case ACTRES_SPY_SABOTAGE_UNIT:
2006  // Unit weakening
2007  dai_incident_simple(receiver, violator, victim, scope, 3);
2008  break;
2009  case ACTRES_SPY_BRIBE_UNIT:
2010  case ACTRES_CAPTURE_UNITS:
2011  case ACTRES_BOMBARD:
2012  case ACTRES_ATTACK:
2013  case ACTRES_SPY_ATTACK:
2014  // Unit loss
2015  dai_incident_simple(receiver, violator, victim, scope, 5);
2016  break;
2017  case ACTRES_SPY_INCITE_CITY:
2018  case ACTRES_DESTROY_CITY:
2019  case ACTRES_CONQUER_CITY:
2020  // City loss
2021  dai_incident_simple(receiver, violator, victim, scope, 10);
2022  break;
2023  case ACTRES_SPY_SABOTAGE_CITY:
2024  case ACTRES_SPY_TARGETED_SABOTAGE_CITY:
2025  case ACTRES_SPY_SABOTAGE_CITY_PRODUCTION:
2026  case ACTRES_STRIKE_BUILDING:
2027  case ACTRES_STRIKE_PRODUCTION:
2028  // Building loss
2029  dai_incident_simple(receiver, violator, victim, scope, 5);
2030  break;
2031  case ACTRES_SPY_POISON:
2032  case ACTRES_SPY_SPREAD_PLAGUE:
2033  // Population loss
2034  dai_incident_simple(receiver, violator, victim, scope, 5);
2035  break;
2036  case ACTRES_FOUND_CITY:
2037  // Terrain loss
2038  dai_incident_simple(receiver, violator, victim, scope, 4);
2039  break;
2040  case ACTRES_PILLAGE:
2041  // Extra loss
2042  dai_incident_simple(receiver, violator, victim, scope, 2);
2043  break;
2044  case ACTRES_TRADE_ROUTE:
2045  case ACTRES_MARKETPLACE:
2046  case ACTRES_HELP_WONDER:
2047  case ACTRES_JOIN_CITY:
2048  case ACTRES_RECYCLE_UNIT:
2049  case ACTRES_DISBAND_UNIT:
2050  case ACTRES_HOME_CITY:
2051  case ACTRES_UPGRADE_UNIT:
2052  case ACTRES_PARADROP:
2053  case ACTRES_AIRLIFT:
2054  case ACTRES_HEAL_UNIT:
2055  case ACTRES_TRANSFORM_TERRAIN:
2056  case ACTRES_CULTIVATE:
2057  case ACTRES_PLANT:
2058  case ACTRES_CLEAN_POLLUTION:
2059  case ACTRES_CLEAN_FALLOUT:
2060  case ACTRES_FORTIFY:
2061  case ACTRES_ROAD:
2062  case ACTRES_CONVERT:
2063  case ACTRES_BASE:
2064  case ACTRES_MINE:
2065  case ACTRES_IRRIGATE:
2066  case ACTRES_TRANSPORT_ALIGHT:
2067  case ACTRES_TRANSPORT_UNLOAD:
2068  case ACTRES_TRANSPORT_DISEMBARK:
2069  case ACTRES_TRANSPORT_BOARD:
2070  case ACTRES_TRANSPORT_EMBARK:
2071  case ACTRES_NONE:
2072  // Various
2073  dai_incident_simple(receiver, violator, victim, scope, 1);
2074  break;
2075  }
2076  break;
2077  case INCIDENT_WAR:
2078  if (receiver == victim) {
2079  dai_incident_war(violator, victim);
2080  }
2081  break;
2082  case INCIDENT_LAST:
2083  // Assert that always fails, but with meaningfull message
2085  break;
2086  }
2087 }
2088 
2092 static void dai_incident_nuclear(struct player *receiver,
2093  const struct player *violator,
2094  const struct player *victim)
2095 {
2096  fc_assert_ret(receiver == victim);
2097 
2098  if (violator == victim) {
2099  return;
2100  }
2101 
2102  receiver->ai_common.love[player_index(violator)] -= 3 * MAX_AI_LOVE / 10;
2103 }
2104 
2108 static void dai_incident_nuclear_not_target(struct player *receiver,
2109  const struct player *violator,
2110  const struct player *victim)
2111 {
2112  fc_assert_ret(receiver != victim);
2113 
2114  receiver->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 10;
2115 }
2116 
2120 static void dai_incident_nuclear_self(struct player *receiver,
2121  const struct player *violator,
2122  const struct player *victim)
2123 {
2124  fc_assert_ret(receiver != victim);
2125  fc_assert_ret(violator == victim);
2126 
2127  receiver->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 20;
2128 }
2129 
2142 static void dai_incident_simple(struct player *receiver,
2143  const struct player *violator,
2144  const struct player *victim,
2145  enum casus_belli_range scope, int how_bad)
2146 {
2147  int displeasure = how_bad * MAX_AI_LOVE;
2148  if (victim == receiver) {
2149  if (scope == CBR_INTERNATIONAL_OUTRAGE) {
2150  /* The ruleset finds this bad enough to cause International Outrage.
2151  * Trust the ruleset author. Double the displeasure. */
2152  displeasure = displeasure * 2;
2153  }
2154  receiver->ai_common.love[player_index(violator)] -= displeasure / 35;
2155  } else if (violator == victim) {
2156  receiver->ai_common.love[player_index(violator)] -= displeasure / 1000;
2157  } else {
2158  receiver->ai_common.love[player_index(violator)] -= displeasure / 500;
2159  }
2160 }
2161 
2170 static void dai_incident_war(struct player *violator, struct player *victim)
2171 {
2172  players_iterate(pplayer)
2173  {
2174  if (!is_ai(pplayer)) {
2175  continue;
2176  }
2177 
2178  if (pplayer != violator) {
2179  // Dislike backstabbing bastards
2180  pplayer->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 30;
2181  if (player_diplstate_get(violator, victim)->max_state == DS_PEACE) {
2182  // Extra penalty if they once had a peace treaty
2183  pplayer->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 30;
2184  } else if (player_diplstate_get(violator, victim)->max_state
2185  == DS_ALLIANCE) {
2186  // Extra penalty if they once had an alliance
2187  pplayer->ai_common.love[player_index(violator)] -= MAX_AI_LOVE / 10;
2188  }
2189  if (victim == pplayer) {
2190  pplayer->ai_common.love[player_index(violator)] =
2191  MIN(pplayer->ai_common.love[player_index(violator)]
2192  - MAX_AI_LOVE / 3,
2193  -1);
2194  // Scream for help!!
2195  players_iterate_alive(ally)
2196  {
2197  if (!pplayers_allied(pplayer, ally)) {
2198  continue;
2199  }
2201  ally,
2202  _("*%s (AI)* We have been savagely attacked by "
2203  "%s, and we need your help! Honor our glorious "
2204  "alliance and your name will never be forgotten!"),
2205  player_name(victim), player_name(violator));
2206  }
2208  }
2209  }
2210  }
2212 }
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Return a pointer to our data.
Definition: advdata.cpp:591
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
There are some signs that a player might be dangerous: We are at war with him, he has done lots of ig...
Definition: advdata.cpp:1062
adv_want amortize(adv_want benefit, int delay)
Amortize means gradually paying off a cost or debt over time.
Definition: advtools.cpp:25
incident_type
Definition: ai.h:40
@ INCIDENT_LAST
Definition: ai.h:40
@ INCIDENT_WAR
Definition: ai.h:40
@ INCIDENT_ACTION
Definition: ai.h:40
struct ai_dip_intel * dai_diplomacy_get(struct ai_type *ait, const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: aidata.cpp:397
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Clean up ai data after phase finished.
Definition: aidata.cpp:281
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Get current default ai data related to player.
Definition: aidata.cpp:304
@ WIN_SPACE
Definition: aidata.h:27
@ WIN_CAPITAL
Definition: aidata.h:28
@ WIN_OPEN
Definition: aidata.h:26
#define DIPLO_LOG(ait, loglevel, pplayer, aplayer, msg,...)
Definition: ailog.h:49
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition: aiplayer.h:47
int player_distance_to_player(struct player *pplayer, struct player *target)
Calculate average distances to other players.
Definition: aisupport.cpp:68
int city_gold_worth(struct city *pcity)
Rough calculation of the worth of pcity in gold.
Definition: aisupport.cpp:104
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Get current value of player trait.
Definition: aitraits.cpp:62
#define WAR(plr1, plr2)
Definition: aiunit.h:68
#define NEVER_MET(plr1, plr2)
Definition: aiunit.h:69
#define ATTACK_POWER(ptype)
Definition: aiunit.h:73
struct city * city_list_find_number(struct city_list *This, int id)
Find city with given id from list.
Definition: city.cpp:1598
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
bool is_capital(const struct city *pcity)
Return TRUE iff this city is its nation's capital.
Definition: city.cpp:1495
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define city_list_iterate_end
Definition: city.h:484
#define city_built_iterate(_pcity, _p)
Definition: city.h:752
#define city_built_iterate_end
Definition: city.h:759
void dai_treaty_accepted(struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
This function is called when a treaty has been concluded, to deal with followup issues like comments ...
static void dai_incident_nuclear_not_target(struct player *receiver, const struct player *violator, const struct player *victim)
Nuclear strike against someone else.
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)
Handle incident caused by violator.
bool dai_on_war_footing(struct ai_type *ait, struct player *pplayer)
Are we going to be declaring war in a few turns time? If so, go on a war footing, and try to buy out ...
void dai_diplomacy_actions(struct ai_type *ait, struct player *pplayer)
Do diplomatic actions.
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)
Evaluate gold worth of a single clause in a treaty.
static void dai_incident_simple(struct player *receiver, const struct player *violator, const struct player *victim, enum casus_belli_range scope, int how_bad)
Called when someone caused an incident to make the receiver lose AI love.
static void dai_incident_nuclear_self(struct player *receiver, const struct player *violator, const struct player *victim)
Somebody else than victim did nuclear strike against self.
void dai_diplomacy_begin_new_phase(struct ai_type *ait, struct player *pplayer)
Calculate our diplomatic predispositions here.
static void suggest_tech_exchange(struct ai_type *ait, struct player *player1, struct player *player2)
Find two techs that can be exchanged and suggest that.
void dai_treaty_evaluate(struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Treaty *ptreaty)
pplayer is AI player, aplayer is the other player involved, treaty is the treaty being considered.
#define LOG_DIPL2
static const struct player * get_allied_with_enemy_player(const struct player *us, const struct player *them)
Returns an enemy player to 'us' allied with 'them' if there is one.
static int greed(int missing_love)
This is your typical human reaction.
static bool dai_players_can_agree_on_ceasefire(struct ai_type *ait, struct player *player1, struct player *player2)
Checks if player1 can agree on ceasefire with player2 This function should only be used for ai player...
static void dai_go_to_war(struct ai_type *ait, struct player *pplayer, struct player *target, enum war_reason reason)
Go to war.
static int dai_goldequiv_tech(struct ai_type *ait, struct player *pplayer, Tech_type_id tech)
How much is a tech worth to player measured in gold.
#define BIG_NUMBER
static enum diplstate_type pact_clause_to_diplstate_type(enum clause_type type)
Convert clause into diplomatic state.
static void dai_diplomacy_suggest(struct player *pplayer, struct player *aplayer, enum clause_type what, bool to_pplayer, int value)
Suggest a treaty.
static void dai_treaty_react(struct ai_type *ait, struct player *pplayer, struct player *aplayer, struct Clause *pclause)
Comments to player from AI on clauses being agreed on.
static int compute_tech_sell_price(struct ai_type *ait, struct player *giver, struct player *taker, int tech_id, bool *is_dangerous)
Calculate a price of a tech.
static void clear_old_treaty(struct player *pplayer, struct player *aplayer)
Clear old clauses from the treaty between players.
static void dai_diplo_notify(struct player *pplayer, const char *text,...)
Send a diplomatic message.
static void dai_incident_nuclear(struct player *receiver, const struct player *violator, const struct player *victim)
Nuclear strike against victim.
static void dai_incident_war(struct player *violator, struct player *victim)
War declared against a player.
static void war_countdown(struct ai_type *ait, struct player *pplayer, struct player *target, int countdown, enum war_reason reason)
Do diplomatic actions.
static void dai_share(struct ai_type *ait, struct player *pplayer, struct player *aplayer)
Offer techs and stuff to other player and ask for techs we need.
static bool shared_vision_is_safe(struct player *pplayer, struct player *aplayer)
Avoid giving pplayer's vision to non-allied player through aplayer (shared vision is transitive).
static int dai_war_desire(struct ai_type *ait, struct player *pplayer, struct player *target)
Calculate our desire to go to war against aplayer.
#define TURNS_BEFORE_TARGET
static bool diplomacy_verbose
void dai_diplomacy_first_contact(struct ai_type *ait, struct player *pplayer, struct player *aplayer)
What to do when we first meet.
struct Treaty * find_treaty(struct player *plr0, struct player *plr1)
Find currently active treaty between two players.
Definition: diplhand.cpp:105
void handle_diplomacy_create_clause_req(struct player *pplayer, int counterpart, int giver, enum clause_type type, int value)
Handle request to add clause to treaty between two players.
Definition: diplhand.cpp:736
void handle_diplomacy_init_meeting_req(struct player *pplayer, int counterpart)
Handle meeting opening request.
Definition: diplhand.cpp:823
void handle_diplomacy_accept_treaty_req(struct player *pplayer, int counterpart)
pplayer clicked the accept button.
Definition: diplhand.cpp:146
bool diplomacy_possible(const struct player *pplayer1, const struct player *pplayer2)
Returns TRUE iff pplayer could do diplomancy in the game at all.
Definition: diptreaty.cpp:28
bool could_meet_with_player(const struct player *pplayer, const struct player *aplayer)
Returns TRUE iff pplayer could do diplomatic meetings with aplayer.
Definition: diptreaty.cpp:56
#define clause_list_iterate_end
Definition: diptreaty.h:61
#define clause_list_iterate(clauselist, pclause)
Definition: diptreaty.h:59
#define is_pact_clause(x)
Definition: diptreaty.h:43
int get_city_bonus(const struct city *pcity, enum effect_type effect_type, enum vision_layer vlayer)
Returns the effect bonus at a city.
Definition: effects.cpp:688
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Returns the effect bonus for a player.
Definition: effects.cpp:673
int Tech_type_id
Definition: fc_types.h:294
#define MAX_NUM_PLAYER_SLOTS
Definition: fc_types.h:24
@ O_SHIELD
Definition: fc_types.h:86
@ O_TRADE
Definition: fc_types.h:87
#define PL_(String1, String2, n)
Definition: fcintl.h:54
#define _(String)
Definition: fcintl.h:50
const struct ft_color ftc_chat_private
struct civ_game game
Definition: game.cpp:47
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
Definition: handicaps.cpp:62
@ H_DIPLOMACY
Definition: handicaps.h:28
@ H_AWAY
Definition: handicaps.h:18
@ H_CEASEFIRE
Definition: handicaps.h:32
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
Returns the number of shields it takes to build this improvement.
bool is_great_wonder(const struct impr_type *pimprove)
Is this building a great wonder?
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
Returns TRUE if the improvement or wonder is obsolete.
bool is_small_wonder(const struct impr_type *pimprove)
Is this building a small wonder?
#define fc_assert_msg(condition, message,...)
Definition: log.h:96
constexpr auto LOG_DEBUG
Definition: log.h:27
#define fc_assert_ret(condition)
Definition: log.h:112
constexpr auto LOG_ERROR
Definition: log.h:23
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define log_base(level, message,...)
Definition: log.h:41
#define map_size_checked()
Definition: map.h:200
void remove_shared_vision(struct player *pfrom, struct player *pto)
Removes shared vision from between two players.
Definition: maphand.cpp:1634
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)
Fill a packet_chat_msg structure for common server event.
Definition: notify.cpp:145
void event_cache_add_for_player(const struct packet_chat_msg *packet, const struct player *pplayer)
Add an event to the cache for one player.
Definition: notify.cpp:636
struct city_list * cities
Definition: packhand.cpp:122
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Return TRUE if players are in the same team.
Definition: player.cpp:1405
int player_number(const struct player *pplayer)
Return the player index/number/id.
Definition: player.cpp:756
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
Definition: player.cpp:1317
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
int player_in_territory(const struct player *pplayer, const struct player *pplayer2)
Return the number of pplayer2's visible units in pplayer's territory, from the point of view of pplay...
Definition: player.cpp:1761
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
enum dipl_reason pplayer_can_cancel_treaty(const struct player *p1, const struct player *p2)
The senate may not allow you to break the treaty.
Definition: player.cpp:90
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
Definition: player.cpp:1334
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players have peace, cease-fire, or armistice.
Definition: player.cpp:1388
bool player_has_embassy(const struct player *pplayer, const struct player *pplayer2)
Check if pplayer has an embassy with pplayer2.
Definition: player.cpp:195
bool gives_shared_vision(const struct player *me, const struct player *them)
Return TRUE iff the player me gives shared vision to player them.
Definition: player.cpp:1414
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied or at peace.
Definition: player.cpp:1355
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: player.cpp:288
#define players_iterate_end
Definition: player.h:520
@ DIPL_SENATE_BLOCKING
Definition: player.h:186
@ DIPL_OK
Definition: player.h:184
#define players_iterate(_pplayer)
Definition: player.h:514
#define MAX_AI_LOVE
Definition: player.h:546
static bool is_barbarian(const struct player *pplayer)
Definition: player.h:474
#define is_ai(plr)
Definition: player.h:227
#define players_iterate_alive_end
Definition: player.h:532
#define is_human(plr)
Definition: player.h:226
#define players_iterate_alive(_pplayer)
Definition: player.h:526
void handle_player_change_government(struct player *pplayer, Government_type_id government)
Called by the client or AI to change government.
Definition: plrhand.cpp:427
void handle_diplomacy_cancel_pact(struct player *pplayer, int other_player_id, enum clause_type clause)
Handles a player cancelling a "pact" with another player.
Definition: plrhand.cpp:745
#define fc_rand(_size)
Definition: rand.h:16
bool research_goal_tech_req(const struct research *presearch, Tech_type_id goal, Tech_type_id tech)
Returns if the given tech has to be researched to reach the goal.
Definition: research.cpp:794
int player_tech_upkeep(const struct player *pplayer)
Calculate the bulb upkeep needed for all techs of a player.
Definition: research.cpp:1037
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
int research_goal_bulbs_required(const struct research *presearch, Tech_type_id goal)
Function to determine cost (in bulbs) of reaching goal technology.
Definition: research.cpp:767
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Returns state of the tech for current research.
Definition: research.cpp:609
bool research_invention_gettable(const struct research *presearch, const Tech_type_id tech, bool allow_holes)
Returns TRUE iff the given tech can be given to the players sharing the research immediately.
Definition: research.cpp:686
#define MIN(x, y)
Definition: shared.h:49
#define MAX(x, y)
Definition: shared.h:48
@ SSHIP_STARTED
Definition: spaceship.h:79
@ SSHIP_LAUNCHED
Definition: spaceship.h:80
@ SSHIP_NONE
Definition: spaceship.h:78
enum clause_type type
Definition: diptreaty.h:64
struct player * from
Definition: diptreaty.h:65
int value
Definition: diptreaty.h:66
struct player * plr0
Definition: diptreaty.h:70
struct clause_list * clauses
Definition: diptreaty.h:72
enum action_result result
Definition: actions.h:308
struct adv_data::@88 dipl
struct player * spacerace_leader
Definition: advdata.h:94
struct player * at_war_with_ally
Definition: aidata.h:49
signed char asked_about_alliance
Definition: aidata.h:58
enum war_reason war_reason
Definition: aidata.h:55
signed char spam
Definition: aidata.h:52
signed char asked_about_peace
Definition: aidata.h:57
int distance
Definition: aidata.h:53
struct player * is_allied_with_ally
Definition: aidata.h:50
signed char warned_about_space
Definition: aidata.h:60
int countdown
Definition: aidata.h:54
signed char ally_patience
Definition: aidata.h:56
struct player * is_allied_with_enemy
Definition: aidata.h:48
signed char asked_about_ceasefire
Definition: aidata.h:59
Definition: aidata.h:63
struct ai_plr::@156 diplomacy
adv_want tech_want[A_LAST+1]
Definition: aidata.h:95
int req_love_for_alliance
Definition: aidata.h:88
char love_incr
Definition: aidata.h:86
char love_coeff
Definition: aidata.h:85
int req_love_for_peace
Definition: aidata.h:87
enum winning_strategy strategy
Definition: aidata.h:83
Definition: ai.h:42
Definition: city.h:291
struct player * original
Definition: city.h:295
struct civ_game::@28::@32 server
struct packet_game_info info
Definition: game.h:80
int love[MAX_NUM_PLAYER_SLOTS]
Definition: player.h:117
int has_reason_to_cancel
Definition: player.h:197
int first_contact_turn
Definition: player.h:195
enum diplstate_type type
Definition: player.h:193
enum spaceship_state state
Definition: spaceship.h:105
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
struct player_ai ai_common
Definition: player.h:270
struct player::@65::@67 server
struct team * team
Definition: player.h:243
const struct ai_type * ai
Definition: player.h:271
struct unit_list * units
Definition: player.h:264
struct conn_list * connections
Definition: player.h:280
bool is_alive
Definition: player.h:250
struct player_economic economic
Definition: player.h:266
struct player_spaceship spaceship
Definition: player.h:268
Tech_type_id researching
Definition: research.h:45
Tech_type_id tech_goal
Definition: research.h:78
int techs_researched
Definition: research.h:35
struct unit_type::@82 adv
const char * advance_rule_name(const struct advance *padvance)
Return the (untranslated) rule name of the advance/technology.
Definition: tech.cpp:283
struct advance * advance_by_number(const Tech_type_id atype)
Return the advance for the given advance index.
Definition: tech.cpp:94
Tech_type_id advance_count()
Return the number of advances/technologies.
Definition: tech.cpp:68
#define advance_index_iterate_end
Definition: tech.h:226
#define A_FIRST
Definition: tech.h:37
#define A_NONE
Definition: tech.h:36
#define advance_index_iterate(_start, _index)
Definition: tech.h:221
#define TRAIT_MAX_VALUE_SR
Definition: traits.h:29
bool unit_is_cityfounder(const struct unit *punit)
Is a cityfounder unit?
Definition: unit.cpp:2389
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
bool utype_is_cityfounder(const struct unit_type *utype)
Is cityfounder type.
Definition: unittype.cpp:2549