File: actiontools.c

package info (click to toggle)
freeciv 3.2.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 286,492 kB
  • sloc: ansic: 484,452; cpp: 37,766; sh: 10,374; makefile: 7,425; python: 2,938; xml: 652; sed: 11
file content (1206 lines) | stat: -rw-r--r-- 47,331 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
/***********************************************************************
 Freeciv - Copyright (C) 1996-2015 - Freeciv Development Team
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

/* utility */
#include "rand.h"

/* common */
#include "actions.h"

/* server */
#include "aiiface.h"
#include "nation.h"
#include "notify.h"
#include "plrhand.h"
#include "unithand.h"
#include "unittools.h"

#include "actiontools.h"

typedef void (*action_notify)(struct player *,
                              const struct action *,
                              struct player *,
                              struct player *,
                              const struct tile *,
                              const char *);

/**********************************************************************//**
  Wipe an actor if the action it successfully performed consumed it.
**************************************************************************/
static void action_success_actor_consume(struct action *paction,
                                         int actor_id, struct unit *actor)
{
  if (unit_is_alive(actor_id)
      && utype_is_consumed_by_action(paction, unit_type_get(actor))) {
    if (action_has_result(paction, ACTRES_DISBAND_UNIT)
        || action_has_result(paction, ACTRES_DISBAND_UNIT_RECOVER)) {
      wipe_unit(actor, ULR_DISBANDED, NULL);
    } else if (action_has_result(paction, ACTRES_NUKE)
               || action_has_result(paction, ACTRES_NUKE_UNITS)) {
      wipe_unit(actor, ULR_DETONATED, NULL);
    } else if (action_has_result(paction, ACTRES_ATTACK)
               || action_has_result(paction, ACTRES_WIPE_UNITS)) {
      wipe_unit(actor, ULR_MISSILE, NULL);
    } else {
      wipe_unit(actor, ULR_USED, NULL);
    }
  }
}

/**********************************************************************//**
  Pay the movement point cost of success.
**************************************************************************/
static void action_success_pay_mp(struct action *paction,
                                  int actor_id, struct unit *actor)
{
  if (unit_is_alive(actor_id)) {
    int spent_mp = unit_pays_mp_for_action(paction, actor);
    actor->moves_left = MAX(0, actor->moves_left - spent_mp);
    send_unit_info(NULL, actor);
  }
}

/**********************************************************************//**
  Pay the movement point price of being the target of an action.
**************************************************************************/
void action_success_target_pay_mp(struct action *paction,
                                  int target_id, struct unit *target)
{
  if (unit_is_alive(target_id)) {
    int spent_mp = get_target_bonus_effects(
        NULL,
        &(const struct req_context) {
          .player = unit_owner(target),
          .city = unit_tile(target) ? tile_city(unit_tile(target)) : NULL,
          .tile = unit_tile(target),
          .unit = target,
          .unittype = unit_type_get(target),
          .action = paction,
        },
        NULL,
        EFT_ACTION_SUCCESS_TARGET_MOVE_COST);

    target->moves_left = MAX(0, target->moves_left - spent_mp);
    send_unit_info(NULL, target);
  }
}

/**********************************************************************//**
  Make the actor that successfully performed the action pay the price.
**************************************************************************/
void action_success_actor_price(struct action *paction,
                                int actor_id, struct unit *actor)
{
  action_success_actor_consume(paction, actor_id, actor);
  action_success_pay_mp(paction, actor_id, actor);
}

/**********************************************************************//**
  Give the victim a casus belli against the offender.
**************************************************************************/
static void action_give_casus_belli(struct player *offender,
                                    struct player *victim_player,
                                    const bool int_outrage)
{
  if (int_outrage) {
    /* This action is seen as a reason for any other player, no matter who
     * the victim was, to declare war on the actor. It could be used to
     * label certain actions atrocities in rule sets where international
     * outrage over an action fits the setting. */

    players_iterate(oplayer) {
      if (oplayer != offender) {
        player_diplstate_get(oplayer, offender)->has_reason_to_cancel = 2;
        player_update_last_war_action(oplayer);
      }
    } players_iterate_end;
  } else if (victim_player && offender != victim_player) {
    /* If an unclaimed tile is nuked there is no victim to give casus
     * belli. If an actor nukes their own tile, they are more than willing
     * to forgive themself. */

    /* Give the victim player a casus belli. */
    player_diplstate_get(victim_player, offender)->has_reason_to_cancel =
        2;
    player_update_last_war_action(victim_player);
  }
  player_update_last_war_action(offender);
}

/**********************************************************************//**
  Take care of any consequences (like casus belli) of the given action
  when the situation was as specified.

  victim_player can be NULL
**************************************************************************/
static void action_consequence_common(const struct action *paction,
                                      struct player *offender,
                                      const struct unit_type *offender_utype,
                                      struct player *victim_player,
                                      const struct tile *victim_tile,
                                      const char *victim_link,
                                      const action_notify notify_actor,
                                      const action_notify notify_victim,
                                      const action_notify notify_global,
                                      const enum effect_type eft)
{
  enum casus_belli_range cbr;

  cbr = casus_belli_range_for(offender, offender_utype, victim_player,
                              eft, paction, victim_tile);

  if (cbr >= CBR_VICTIM_ONLY) {
    /* In this situation the specified action provides a casus belli
     * against the actor. */

    /* International outrage: This isn't just between the offender and the
     * victim. */
    const bool int_outrage = (cbr == CBR_INTERNATIONAL_OUTRAGE);

    /* Notify the involved players by sending them a message. */
    notify_actor(offender, paction, offender, victim_player,
                 victim_tile, victim_link);
    notify_victim(victim_player, paction, offender, victim_player,
                  victim_tile, victim_link);

    if (int_outrage) {
      /* Every other player gets a casus belli against the actor. Tell each
       * players about it. */
      players_iterate(oplayer) {
        notify_global(oplayer, paction, offender,
                      victim_player, victim_tile, victim_link);
      } players_iterate_end;
    }

    /* Give casus belli. */
    action_give_casus_belli(offender, victim_player, int_outrage);

    /* Notify players controlled by the built in AI. */
    call_incident(INCIDENT_ACTION, cbr, paction, offender, victim_player);

    /* Update the clients. */
    send_player_all_c(offender, NULL);

    if (victim_player != NULL && victim_player != offender) {
      /* The actor player was just sent. */
      /* An action against an ownerless tile is victimless. */
      send_player_all_c(victim_player, NULL);
    }
  }
}

/**********************************************************************//**
  Notify the actor that the failed action gave the victim a casus belli
  against the actor.
**************************************************************************/
static void notify_actor_caught(struct player *receiver,
                                const struct action *paction,
                                struct player *offender,
                                struct player *victim_player,
                                const struct tile *victim_tile,
                                const char *victim_link)
{
  if (!victim_player || offender == victim_player) {
    /* There is no victim or the actor did this to themself. */
    return;
  }

  /* Custom message based on action type. */
  switch (action_get_target_kind(paction)) {
  case ATK_CITY:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Suitcase Nuke ... San Francisco */
                  _("You have caused an diplomatic incident getting caught"
                    " trying to do %s to %s."),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_UNIT:
  case ATK_UNITS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Bribe Enemy Unit ... American ... Partisan */
                  _("You have caused an diplomatic incident getting caught"
                    " trying to do %s to %s %s."),
                  action_name_translation(paction),
                  nation_adjective_for_player(victim_player),
                  victim_link);
    break;
  case ATK_TILE:
  case ATK_EXTRAS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Explode Nuclear ... (54, 26) */
                  _("You have caused an diplomatic incident getting caught"
                    " trying to do %s at %s."),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_SELF:
    /* Special actor notice not needed. Actor is victim. */
    break;
  case ATK_COUNT:
    fc_assert(ATK_COUNT != ATK_COUNT);
    break;
  }
}

/**********************************************************************//**
  Notify the victim that the failed action gave the victim a
  casus belli against the actor.
**************************************************************************/
static void notify_victim_caught(struct player *receiver,
                                 const struct action *paction,
                                 struct player *offender,
                                 struct player *victim_player,
                                 const struct tile *victim_tile,
                                 const char *victim_link)
{
  if (!victim_player || offender == victim_player) {
    /* There is no victim or the actor did this to themself. */
    return;
  }

  /* Custom message based on action type. */
  switch (action_get_target_kind(paction)) {
  case ATK_CITY:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Suitcase Nuke ... San Francisco */
                  _("The %s have caused an diplomatic incident getting caught"
                    " trying to do %s to %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_UNIT:
  case ATK_UNITS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Bribe Enemy Unit ... Partisan */
                  _("The %s have caused an diplomatic incident getting caught"
                    " trying to do %s to your %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_TILE:
  case ATK_EXTRAS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Explode Nuclear ... (54, 26) */
                  _("The %s have caused an diplomatic incident getting caught"
                    " trying to do %s at %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_SELF:
    /* Special victim notice not needed. Actor is victim. */
    break;
  case ATK_COUNT:
    fc_assert(ATK_COUNT != ATK_COUNT);
    break;
  }
}

/**********************************************************************//**
  Notify the world that the failed action gave the everyone a casus belli
  against the actor.
**************************************************************************/
static void notify_global_caught(struct player *receiver,
                                 const struct action *paction,
                                 struct player *offender,
                                 struct player *victim_player,
                                 const struct tile *victim_tile,
                                 const char *victim_link)
{
  if (receiver == offender) {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Suitcase Nuke */
                  _("Getting caught while trying to do %s gives "
                    "everyone a casus belli against you."),
                  action_name_translation(paction));
  } else if (receiver == victim_player) {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Suitcase Nuke ... Europeans */
                  _("Getting caught while trying to do %s to you gives "
                    "everyone a casus belli against the %s."),
                  action_name_translation(paction),
                  nation_plural_for_player(offender));
  } else if (victim_player == NULL) {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Suitcase Nuke */
                  _("You now have a casus belli against the %s. "
                    "They got caught trying to do %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction));
  } else {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Suitcase Nuke ... Americans */
                  _("You now have a casus belli against the %s. "
                    "They got caught trying to do %s to the %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  nation_plural_for_player(victim_player));
  }
}

/**********************************************************************//**
  Take care of any consequences (like casus belli) of getting caught while
  trying to perform the given action.

  victim_player can be NULL
**************************************************************************/
void action_consequence_caught(const struct action *paction,
                               struct player *offender,
                               const struct unit_type *offender_utype,
                               struct player *victim_player,
                               const struct tile *victim_tile,
                               const char *victim_link)
{
  action_consequence_common(paction, offender, offender_utype,
                            victim_player, victim_tile, victim_link,
                            notify_actor_caught,
                            notify_victim_caught,
                            notify_global_caught,
                            EFT_CASUS_BELLI_CAUGHT);
}

/**********************************************************************//**
  Notify the actor that the performed action gave the victim a casus belli
  against the actor.
**************************************************************************/
static void notify_actor_success(struct player *receiver,
                                 const struct action *paction,
                                 struct player *offender,
                                 struct player *victim_player,
                                 const struct tile *victim_tile,
                                 const char *victim_link)
{
  if (!victim_player || offender == victim_player) {
    /* There is no victim or the actor did this to themself. */
    return;
  }

  /* Custom message based on action type. */
  switch (action_get_target_kind(paction)) {
  case ATK_CITY:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Suitcase Nuke ... San Francisco */
                  _("You have caused an diplomatic incident doing %s to %s."),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_UNIT:
  case ATK_UNITS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRAND: Bribe Enemy Unit ... American ... Partisan */
                  _("You have caused an diplomatic incident doing %s to %s %s."),
                  action_name_translation(paction),
                  nation_adjective_for_player(victim_player),
                  victim_link);
    break;
  case ATK_TILE:
  case ATK_EXTRAS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Explode Nuclear ... (54, 26) */
                  _("You have caused an diplomatic incident doing %s at %s."),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_SELF:
    /* Special actor notice not needed. Actor is victim. */
    break;
  case ATK_COUNT:
    fc_assert(ATK_COUNT != ATK_COUNT);
    break;
  }
}

/**********************************************************************//**
  Notify the victim that the performed action gave the victim a casus
  belli against the actor.
**************************************************************************/
static void notify_victim_success(struct player *receiver,
                                  const struct action *paction,
                                  struct player *offender,
                                  struct player *victim_player,
                                  const struct tile *victim_tile,
                                  const char *victim_link)
{
  if (!victim_player || offender == victim_player) {
    /* There is no victim or the actor did this to themself. */
    return;
  }

  /* Custom message based on action type. */
  switch (action_get_target_kind(paction)) {
  case ATK_CITY:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Suitcase Nuke ... San Francisco */
                  _("The %s have caused an diplomatic incident doing %s to %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_UNIT:
  case ATK_UNITS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Bribe Enemy Unit ... Partisan */
                  _("The %s have caused an diplomatic incident doing "
                    "%s to your %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_TILE:
  case ATK_EXTRAS:
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Explode Nuclear ... (54, 26) */
                  _("The %s have caused an diplomatic incident doing %s at %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  victim_link);
    break;
  case ATK_SELF:
    /* Special victim notice not needed. Actor is victim. */
    break;
  case ATK_COUNT:
    fc_assert(ATK_COUNT != ATK_COUNT);
    break;
  }
}

/**********************************************************************//**
  Notify the world that the performed action gave the everyone a casus
  belli against the actor.
**************************************************************************/
static void notify_global_success(struct player *receiver,
                                  const struct action *paction,
                                  struct player *offender,
                                  struct player *victim_player,
                                  const struct tile *victim_tile,
                                  const char *victim_link)
{
  if (receiver == offender) {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Suitcase Nuke */
                  _("Doing %s gives everyone a casus belli against you."),
                  action_name_translation(paction));
  } else if (receiver == victim_player) {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Suitcase Nuke ... Europeans */
                  _("Doing %s to you gives everyone a casus belli against "
                    "the %s."),
                  action_name_translation(paction),
                  nation_plural_for_player(offender));
  } else if (victim_player == NULL) {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Suitcase Nuke */
                  _("You now have a casus belli against the %s. "
                    "They did %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction));
  } else {
    notify_player(receiver, victim_tile,
                  E_DIPLOMATIC_INCIDENT, ftc_server,
                  /* TRANS: Europeans ... Suitcase Nuke ... Americans */
                  _("You now have a casus belli against the %s. "
                    "They did %s to the %s."),
                  nation_plural_for_player(offender),
                  action_name_translation(paction),
                  nation_plural_for_player(victim_player));
  }
}

/**********************************************************************//**
  Take care of any consequences (like casus belli) of successfully
  performing the given action.

  victim_player can be NULL
**************************************************************************/
void action_consequence_success(const struct action *paction,
                                struct player *offender,
                                const struct unit_type *offender_utype,
                                struct player *victim_player,
                                const struct tile *victim_tile,
                                const char *victim_link)
{
  action_consequence_common(paction, offender, offender_utype,
                            victim_player, victim_tile, victim_link,
                            notify_actor_success,
                            notify_victim_success,
                            notify_global_success,
                            EFT_CASUS_BELLI_SUCCESS);
}

/**************************************************************************
  Take care of any consequences (like casus belli) of successfully
  completing the given action.

  victim_player can be NULL
**************************************************************************/
void action_consequence_complete(const struct action *paction,
                                 struct player *offender,
                                 const struct unit_type *offender_utype,
                                 struct player *victim_player,
                                 const struct tile *victim_tile,
                                 const char *victim_link)
{
  action_consequence_common(paction, offender, offender_utype,
                            victim_player, victim_tile, victim_link,
                            notify_actor_success,
                            notify_victim_success,
                            notify_global_success,
                            EFT_CASUS_BELLI_COMPLETE);
}

/**********************************************************************//**
  Returns TRUE iff, from the point of view of the owner of the actor unit,
  it looks like the actor unit may be able to do any action to the target
  city.

  If the owner of the actor unit doesn't have the knowledge needed to know
  for sure if the unit can act TRUE will be returned.

  If the only action(s) that can be performed against a target has the
  rare_pop_up property the target will only be considered valid if the
  accept_all_actions argument is TRUE.
**************************************************************************/
static bool may_unit_act_vs_city(struct unit *actor, struct city *target,
                                 bool accept_all_actions)
{
  const struct civ_map *nmap = &(wld.map);

  if (actor == NULL || target == NULL) {
    /* Can't do any actions if actor or target are missing. */
    return FALSE;
  }

  action_iterate(act) {
    if (!(action_id_get_actor_kind(act) == AAK_UNIT
        && action_id_get_target_kind(act) == ATK_CITY)) {
      /* Not a relevant action. */
      continue;
    }

    if (action_id_is_rare_pop_up(act) && !accept_all_actions) {
      /* Not relevant since not accepted here. */
      continue;
    }

    if (action_prob_possible(action_prob_vs_city(nmap, actor, act, target))) {
      /* The actor unit may be able to do this action to the target
       * city. */
      return TRUE;
    }
  } action_iterate_end;

  return FALSE;
}

/**********************************************************************//**
  Find a city to target for an action on the specified tile.

  Returns NULL if no proper target is found.

  If the only action(s) that can be performed against a target has the
  rare_pop_up property the target will only be considered valid if the
  accept_all_actions argument is TRUE.
**************************************************************************/
struct city *action_tgt_city(struct unit *actor, struct tile *target_tile,
                             bool accept_all_actions)
{
  struct city *target = tile_city(target_tile);

  if (target && may_unit_act_vs_city(actor, target, accept_all_actions)) {
    /* It may be possible to act against this city. */
    return target;
  }

  return NULL;
}

/**********************************************************************//**
  Returns TRUE iff, from the point of view of the owner of the actor unit,
  it looks like the actor unit may be able to do any action to the target
  unit.

  If the owner of the actor unit doesn't have the knowledge needed to know
  for sure if the unit can act TRUE will be returned.

  If the only action(s) that can be performed against a target has the
  rare_pop_up property the target will only be considered valid if the
  accept_all_actions argument is TRUE.
**************************************************************************/
static bool may_unit_act_vs_unit(struct unit *actor, struct unit *target,
                                 bool accept_all_actions)
{
  const struct civ_map *nmap = &(wld.map);

  if (actor == NULL || target == NULL) {
    /* Can't do any actions if actor or target are missing. */
    return FALSE;
  }

  action_iterate(act) {
    if (!(action_id_get_actor_kind(act) == AAK_UNIT
        && action_id_get_target_kind(act) == ATK_UNIT)) {
      /* Not a relevant action. */
      continue;
    }

    if (action_id_is_rare_pop_up(act) && !accept_all_actions) {
      /* Not relevant since not accepted here. */
      continue;
    }

    if (action_prob_possible(action_prob_vs_unit(nmap, actor, act, target))) {
      /* The actor unit may be able to do this action to the target
       * unit. */
      return TRUE;
    }
  } action_iterate_end;

  return FALSE;
}

/**********************************************************************//**
  Find a unit to target for an action at the specified tile.

  Returns the first unit found at the tile that the actor may act against
  or NULL if no proper target is found.

  If the only action(s) that can be performed against a target has the
  rare_pop_up property the target will only be considered valid if the
  accept_all_actions argument is TRUE.
**************************************************************************/
struct unit *action_tgt_unit(struct unit *actor, struct tile *target_tile,
                             bool accept_all_actions)
{
  unit_list_iterate(target_tile->units, target) {
    if (may_unit_act_vs_unit(actor, target, accept_all_actions)) {
      return target;
    }
  } unit_list_iterate_end;

  return NULL;
}

/**********************************************************************//**
  Returns the tile iff it, from the point of view of the owner of the
  actor unit, looks like a target tile.

  Returns NULL if the player knows that the actor unit can't do any
  action (that specifies its target as a tile) to the tile.

  If the owner of the actor unit doesn't have the knowledge needed to know
  for sure if the unit can act the tile will be returned.

  If the only action(s) that can be performed against a target has the
  rare_pop_up property the target will only be considered valid if the
  accept_all_actions argument is TRUE.
**************************************************************************/
struct tile *action_tgt_tile(struct unit *actor,
                             struct tile *target,
                             const struct extra_type *target_extra,
                             bool accept_all_actions)
{
  const struct civ_map *nmap = &(wld.map);

  if (actor == NULL || target == NULL) {
    /* Can't do any actions if actor or target are missing. */
    return NULL;
  }

  action_iterate(act) {
    struct act_prob prob;

    if (action_id_get_actor_kind(act) != AAK_UNIT) {
      /* Not a relevant action. */
      continue;
    }

    if (action_id_is_rare_pop_up(act) && !accept_all_actions) {
      /* Not relevant since not accepted here. */
      continue;
    }

    switch (action_id_get_target_kind(act)) {
    case ATK_TILE:
      prob = action_prob_vs_tile(nmap, actor, act, target, target_extra);
      break;
    case ATK_EXTRAS:
      prob = action_prob_vs_extras(nmap, actor, act, target, target_extra);
      break;
    case ATK_UNITS:
      prob = action_prob_vs_stack(nmap, actor, act, target);
      break;
    case ATK_CITY:
    case ATK_UNIT:
    case ATK_SELF:
      /* Target not specified by tile. */
      continue;
    case ATK_COUNT:
      /* Invalid target kind */
      fc_assert(action_id_get_target_kind(act) != ATK_COUNT);
      continue;
    }

    if (action_prob_possible(prob)) {
      /* The actor unit may be able to do this action to the target
       * tile. */
      return target;
    }
  } action_iterate_end;

  return NULL;
}

/**********************************************************************//**
  Returns TRUE iff, from the point of view of the owner of the actor unit,
  it looks like the actor unit may be able to do any action to the target
  extra located at the target tile.

  If the owner of the actor unit doesn't have the knowledge needed to know
  for sure if the unit can act TRUE will be returned.

  If the only action(s) that can be performed against a target has the
  rare_pop_up property the target will only be considered valid if the
  accept_all_actions argument is TRUE.
**************************************************************************/
static bool may_unit_act_vs_tile_extra(const struct unit *actor,
                                       const struct tile *tgt_tile,
                                       const struct extra_type *tgt_extra,
                                       bool accept_all_actions)
{
  const struct civ_map *nmap = &(wld.map);

  if (actor == NULL || tgt_tile == NULL || tgt_extra == NULL) {
    /* Can't do any actions if actor or target are missing. */
    return FALSE;
  }

  action_iterate(act) {
    if (!(action_id_get_actor_kind(act) == AAK_UNIT
          && (action_id_get_target_kind(act) == ATK_TILE
              || action_id_get_target_kind(act) == ATK_EXTRAS)
          && action_id_has_complex_target(act))) {
      /* Not a relevant action. */
      continue;
    }

    if (action_id_is_rare_pop_up(act) && !accept_all_actions) {
      /* Not relevant since not accepted here. */
      continue;
    }

    switch (action_id_get_target_kind(act)) {
    case ATK_TILE:
      if (action_prob_possible(action_prob_vs_tile(nmap, actor, act,
                                                   tgt_tile, tgt_extra))) {
        /* The actor unit may be able to do this action to the target
         * extra. */
        return TRUE;
      }
      break;
    case ATK_EXTRAS:
      if (action_prob_possible(action_prob_vs_extras(nmap, actor, act,
                                                     tgt_tile,
                                                     tgt_extra))) {
        /* The actor unit may be able to do this action to the target
         * extra. */
        return TRUE;
      }
      break;
    case ATK_CITY:
    case ATK_UNIT:
    case ATK_UNITS:
    case ATK_SELF:
      /* Not supported. */
      break;
    case ATK_COUNT:
      fc_assert(action_id_get_target_kind(act) != ATK_COUNT);
      break;
    }
  } action_iterate_end;

  return FALSE;
}

/**********************************************************************//**
  Find an extra to target for an action at the specified tile.

  Returns the first extra found that the actor may act against at the tile
  or NULL if no proper target is found. (Note that some actions requires
  the absence of an extra since they result in its creation while other
  requires its presence.)

  If the only action(s) that can be performed against a target has the
  rare_pop_up property the target will only be considered valid if the
  accept_all_actions argument is TRUE.
**************************************************************************/
struct extra_type *action_tgt_tile_extra(const struct unit *actor,
                                         const struct tile *target_tile,
                                         bool accept_all_actions)
{
  extra_type_re_active_iterate(target) {
    if (may_unit_act_vs_tile_extra(actor, target_tile, target,
                                   accept_all_actions)) {
      return target;
    }
  } extra_type_re_active_iterate_end;

  return NULL;
}

/**********************************************************************//**
  Find an sub target for the specified action.
**************************************************************************/
int action_sub_target_id_for_action(const struct action *paction,
                                    struct unit *actor_unit)
{
  const struct tile *tgt_tile = unit_tile(actor_unit);
  const struct civ_map *nmap = &(wld.map);

  fc_assert_ret_val(paction->target_complexity == ACT_TGT_COMPL_FLEXIBLE,
                    NO_TARGET);

  switch (action_get_sub_target_kind(paction)) {
  case ASTK_NONE:
    /* Should not be reached */
    fc_assert_ret_val(action_get_sub_target_kind(paction) != ASTK_NONE,
                      NO_TARGET);
    break;
  case ASTK_BUILDING:
    /* Implement if a building sub targeted action becomes flexible */
    fc_assert_ret_val(paction->target_complexity == ACT_TGT_COMPL_FLEXIBLE,
                      NO_TARGET);
    break;
  case ASTK_TECH:
    /* Implement if a tech sub targeted action becomes flexible */
    fc_assert_ret_val(paction->target_complexity == ACT_TGT_COMPL_FLEXIBLE,
                      NO_TARGET);
    break;
  case ASTK_EXTRA:
  case ASTK_EXTRA_NOT_THERE:
    if (action_has_result(paction, ACTRES_PILLAGE)) {
      /* Special treatment for "Pillage" */
      struct extra_type *pextra = NULL;
      enum unit_activity activity = action_get_activity(paction);

      unit_assign_specific_activity_target(actor_unit, &activity, &pextra);

      if (pextra != NULL) {
        return extra_number(pextra);
      }
    }
    extra_type_re_active_iterate(tgt_extra) {
      if (action_prob_possible(action_prob_vs_tile(nmap, actor_unit, paction->id,
                                                   tgt_tile, tgt_extra))) {
        /* The actor unit may be able to do this action to the target
         * extra. */
        return extra_number(tgt_extra);
      }
    } extra_type_re_active_iterate_end;
    break;
  case ASTK_COUNT:
    /* Should not exist. */
    fc_assert_ret_val(action_get_sub_target_kind(paction) != ASTK_COUNT,
                      NO_TARGET);
    break;
  }

  return NO_TARGET;
}

/**********************************************************************//**
  Returns the action auto performer that the specified cause can force the
  specified actor to perform. Returns NULL if no such action auto performer
  exists.
**************************************************************************/
const struct action_auto_perf *
action_auto_perf_unit_sel(const enum action_auto_perf_cause cause,
                          const struct unit *actor,
                          const struct player *other_player,
                          const struct output_type *eval_output,
                          const struct action *eval_action)
{
  const struct req_context actor_ctxt = {
    .player = unit_owner(actor),
    .tile = unit_tile(actor),
    .unit = actor,
    .unittype = unit_type_get(actor),
    .output = eval_output,
    .action = eval_action,
  };

  action_auto_perf_by_cause_iterate(cause, autoperformer) {
    if (are_reqs_active(&actor_ctxt, other_player,
                        &autoperformer->reqs, RPT_CERTAIN)) {
      /* Select this action auto performer. */
      return autoperformer;
    }
  } action_auto_perf_by_cause_iterate_end;

  /* Can't even try to force an action. */
  return NULL;
}

#define action_auto_perf_acquire_targets(_target_extra_)                   \
  tgt_city = (target_city ? target_city                                    \
                          : action_tgt_city(actor, unit_tile(actor),       \
                                            TRUE));                        \
  tgt_tile = (target_tile ? target_tile                                    \
                          : action_tgt_tile(actor, unit_tile(actor),       \
                                            _target_extra_,                \
                                            TRUE));                        \
  tgt_unit = (target_unit ? target_unit                                    \
                          : action_tgt_unit(actor, unit_tile(actor),       \
                                            TRUE));

/**********************************************************************//**
  Make the specified actor unit perform an action because of cause.

  Returns the action the actor unit was forced to perform.
  Returns NULL if that didn't happen.

  Note that the return value doesn't say anything about survival.
**************************************************************************/
const struct action *
action_auto_perf_unit_do(const enum action_auto_perf_cause cause,
                         struct unit *actor,
                         const struct player *other_player,
                         const struct output_type *eval_output,
                         const struct action *eval_action,
                         const struct tile *target_tile,
                         const struct city *target_city,
                         const struct unit *target_unit,
                         const struct extra_type *target_extra)
{
  int actor_id;

  const struct city *tgt_city;
  const struct tile *tgt_tile;
  const struct unit *tgt_unit;
  const struct civ_map *nmap = &(wld.map);

  const struct action_auto_perf *autoperf
    = action_auto_perf_unit_sel(cause, actor, other_player,
                                eval_output, eval_action);

  if (!autoperf) {
    /* No matching Action Auto Performer. */
    return NULL;
  }

  actor_id = actor->id;

  /* Acquire the targets. */
  action_auto_perf_acquire_targets(target_extra);

  action_auto_perf_actions_iterate(autoperf, act) {
    if (action_id_get_actor_kind(act) == AAK_UNIT) {
      /* This action can be done by units. */

#define perform_action_to(act, actor, tgtid, tgt_extra)                   \
  if (unit_perform_action(unit_owner(actor),                              \
                          actor->id, tgtid, tgt_extra,                    \
                          NULL, act, ACT_REQ_RULES)) {                    \
    return action_by_number(act);                                         \
  }

      switch (action_id_get_target_kind(act)) {
      case ATK_UNITS:
        if (tgt_tile
            && is_action_enabled_unit_on_stack(nmap, act, actor, tgt_tile)) {
          perform_action_to(act, actor, tgt_tile->index, EXTRA_NONE);
        }
        break;
      case ATK_TILE:
        if (tgt_tile
            && is_action_enabled_unit_on_tile(nmap, act, actor,
                                              tgt_tile, target_extra)) {
          perform_action_to(act, actor, tgt_tile->index,
                            target_extra ? extra_number(target_extra) : -1);
        }
        break;
      case ATK_EXTRAS:
        if (tgt_tile
            && is_action_enabled_unit_on_extras(nmap, act, actor,
                                                tgt_tile, target_extra)) {
          perform_action_to(act, actor, tgt_tile->index,
                            target_extra ? extra_number(target_extra) : -1);
        }
        break;
      case ATK_CITY:
        if (tgt_city
            && is_action_enabled_unit_on_city(nmap, act, actor, tgt_city)) {
          perform_action_to(act, actor, tgt_city->id, EXTRA_NONE)
        }
        break;
      case ATK_UNIT:
        if (tgt_unit
            && is_action_enabled_unit_on_unit(nmap, act, actor, tgt_unit)) {
          perform_action_to(act, actor, tgt_unit->id, EXTRA_NONE);
        }
        break;
      case ATK_SELF:
        if (actor
            && is_action_enabled_unit_on_self(nmap, act, actor)) {
          perform_action_to(act, actor, actor->id, EXTRA_NONE);
        }
        break;
      case ATK_COUNT:
        fc_assert(action_id_get_target_kind(act) != ATK_COUNT);
      }

      if (!unit_is_alive(actor_id)) {
        /* The unit is gone. Maybe it was killed in Lua? */
        return NULL;
      }
    }
  } action_auto_perf_actions_iterate_end;

  return NULL;
}

/**********************************************************************//**
  Returns the probability for the specified actor unit to be forced to
  perform an action by the specified cause.
**************************************************************************/
struct act_prob
action_auto_perf_unit_prob(const enum action_auto_perf_cause cause,
                           struct unit *actor,
                           const struct player *other_player,
                           const struct output_type *eval_output,
                           const struct action *eval_action,
                           const struct tile *target_tile,
                           const struct city *target_city,
                           const struct unit *target_unit,
                           const struct extra_type *target_extra)
{
  struct act_prob out;

  const struct city *tgt_city;
  const struct tile *tgt_tile;
  const struct unit *tgt_unit;
  const struct civ_map *nmap = &(wld.map);

  const struct action_auto_perf *autoperf
    = action_auto_perf_unit_sel(cause, actor, other_player,
                                eval_output, eval_action);

  if (!autoperf) {
    /* No matching Action Auto Performer. */
    return ACTPROB_IMPOSSIBLE;
  }

  out = ACTPROB_IMPOSSIBLE;

  /* Acquire the targets. */
  action_auto_perf_acquire_targets(target_extra);

  action_auto_perf_actions_iterate(autoperf, act) {
    struct act_prob current = ACTPROB_IMPOSSIBLE;

    if (action_id_get_actor_kind(act) == AAK_UNIT) {
      /* This action can be done by units. */

      switch (action_id_get_target_kind(act)) {
      case ATK_UNITS:
        if (tgt_tile
            && is_action_enabled_unit_on_stack(nmap, act, actor, tgt_tile)) {
          current = action_prob_vs_stack(nmap, actor, act, tgt_tile);
        }
        break;
      case ATK_TILE:
        if (tgt_tile
            && is_action_enabled_unit_on_tile(nmap, act, actor,
                                              tgt_tile, target_extra)) {
          current = action_prob_vs_tile(nmap, actor, act, tgt_tile, target_extra);
        }
        break;
      case ATK_EXTRAS:
        if (tgt_tile
            && is_action_enabled_unit_on_extras(nmap, act, actor,
                                                tgt_tile, target_extra)) {
          current = action_prob_vs_extras(nmap, actor, act,
                                          tgt_tile, target_extra);
        }
        break;
      case ATK_CITY:
        if (tgt_city
            && is_action_enabled_unit_on_city(nmap, act, actor, tgt_city)) {
          current = action_prob_vs_city(nmap, actor, act, tgt_city);
        }
        break;
      case ATK_UNIT:
        if (tgt_unit
            && is_action_enabled_unit_on_unit(nmap, act, actor, tgt_unit)) {
          current = action_prob_vs_unit(nmap, actor, act, tgt_unit);
        }
        break;
      case ATK_SELF:
        if (actor
            && is_action_enabled_unit_on_self(nmap, act, actor)) {
          current = action_prob_self(nmap, actor, act);
        }
        break;
      case ATK_COUNT:
        fc_assert(action_id_get_target_kind(act) != ATK_COUNT);
      }
    }

    out = action_prob_fall_back(&out, &current);
  } action_auto_perf_actions_iterate_end;

  return out;
}

/************************************************************************//**
  Returns TRUE iff the spy/diplomat was caught outside of a diplomatic
  battle.
****************************************************************************/
bool action_failed_dice_roll(const struct player *act_player,
                             const struct unit *act_unit,
                             const struct city *tgt_city,
                             const struct player *tgt_player,
                             const struct action *paction)
{
  int odds = action_dice_roll_odds(act_player, act_unit,
                                   tgt_city, tgt_player,
                                   paction);

  /* Roll the dice. */
  return fc_rand (100) >= odds;
}