-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathAdjudicator.cpp
More file actions
2233 lines (1876 loc) · 64.7 KB
/
Copy pathAdjudicator.cpp
File metadata and controls
2233 lines (1876 loc) · 64.7 KB
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
//==============================================================================
//
// Adjudicator.cpp
//
// Diplomacy AI Client - Part of the DAIDE project (www.daide.org.uk).
//
// (C) David Norman 2002 david@ellought.demon.co.uk
// (C) Greg Utas 2019-2025 greg@pentennea.com
//
// This software may be reused for non-commercial purposes without charge,
// and without notifying the authors. Use of any part of this software for
// commercial purposes without permission from the authors is prohibited.
//
#include "MapAndUnits.h"
#include <cstdint>
#include <set>
#include "Debug.h"
using namespace NodeBase;
// Open questions. Search on "<a>" etc for the source code locations.
// <a> This line checks if a unit is supporting itself. But isn't its own
// province unreachable? can_move_to_province should have returned false.
// <b> Shouldn't all invocations of ConvoySubversions.clear() be replaced
// by decrement()?
// <c> This line implies that an adjacent location could be in the same
// province.
// <d> This line was absent from the original source but is present in
// what is basically the same loop in process_order().
// <e> If the unit is still in the province from which it was dislodged,
// wouldn't can_move_to have returned false?
// <f> If this is true, wouldn't the check that precedes it also be true?
// <g> How could the dislodger be in the other province of a balanced
// head-to-head battle?
// <h> How could the dislodger of the stronger unit be the weaker one in
// an unbalanced head-to-head battle?
//
//------------------------------------------------------------------------------
namespace Diplomacy
{
fn_name MapAndUnits_adjudicate = "MapAndUnits.adjudicate";
void MapAndUnits::adjudicate()
{
Debug::ft(MapAndUnits_adjudicate);
switch(curr_season.all())
{
case TOKEN_SEASON_SPR:
case TOKEN_SEASON_FAL:
adjudicate_moves();
break;
case TOKEN_SEASON_SUM:
case TOKEN_SEASON_AUT:
adjudicate_retreats();
break;
case TOKEN_SEASON_WIN:
adjudicate_builds();
break;
default:
Debug::SwLog(MapAndUnits_adjudicate, "invalid season", curr_season.all());
}
}
//------------------------------------------------------------------------------
void MapAndUnits::adjudicate_builds()
{
Debug::ft("MapAndUnits.adjudicate_builds");
// Check that each power has ordered enough builds or disbands.
//
for(PowerId p = 0; p < number_of_powers; ++p)
{
auto& orders = winter_orders[p];
if(orders.is_building)
{
if(orders.adjustments.size() +
orders.number_of_waives < orders.number_of_orders_required)
{
// Too few builds ordered. Waive the remaining builds.
//
orders.number_of_waives =
orders.number_of_orders_required - orders.adjustments.size();
}
}
else if(orders.adjustments.size() < orders.number_of_orders_required)
{
// Too few disbands ordered. Disband using the default rules.
//
generate_cd_disbands(p, orders);
}
}
// The builds are all valid now, so mark them as such.
//
for(PowerId p = 0; p < number_of_powers; ++p)
{
auto& orders = winter_orders[p];
for(auto a = orders.adjustments.begin();
a != orders.adjustments.end(); ++a)
{
a->second = TOKEN_RESULT_SUC;
}
}
}
//------------------------------------------------------------------------------
void MapAndUnits::adjudicate_moves()
{
Debug::ft("MapAndUnits.adjudicate_moves");
auto changes_made = true;
auto futile_convoys_checked = false;
auto futile_and_indomitable_convoys_checked = false;
initialise_move_adjudication();
if(check_on_adjudication)
{
check_for_illegal_move_orders();
}
cancel_inconsistent_convoys();
cancel_inconsistent_supports();
direct_attacks_cut_support();
build_support_lists();
build_convoy_subversion_list();
while(changes_made)
{
changes_made = resolve_attacks_on_unsubverted_convoys();
if(!changes_made && !futile_convoys_checked)
{
changes_made = check_for_futile_convoys();
futile_convoys_checked = true;
}
if(!changes_made && !futile_and_indomitable_convoys_checked)
{
changes_made = check_for_indomitable_and_futile_convoys();
futile_and_indomitable_convoys_checked = true;
}
}
resolve_circles_of_subversion();
identify_attack_rings_and_head_to_head_battles();
advance_attack_rings();
resolve_unbalanced_head_to_head_battles();
resolve_balanced_head_to_head_battles();
fight_ordinary_battles();
}
//------------------------------------------------------------------------------
void MapAndUnits::adjudicate_retreats()
{
Debug::ft("MapAndUnits.adjudicate_retreats");
AttackMap retreat_map;
// Initialise each dislodged unit.
//
for(auto u = dislodged_units.begin(); u != dislodged_units.end(); ++u)
{
auto& unit = u->second;
unit.order_type_copy = unit.order;
unit.bounce = false;
unit.unit_moves = false;
}
if(check_on_adjudication)
{
check_for_illegal_retreat_orders();
}
// Check each dislodged unit that was ordered to retreat.
//
for(auto u = dislodged_units.begin(); u != dislodged_units.end(); ++u)
{
auto& unit = u->second;
if(unit.order_type_copy == RETREAT_ORDER)
{
// See if another unit is trying to retreat to the same province.
//
auto r = retreat_map.find(unit.dest.province);
if(r != retreat_map.end())
{
// Yes, so bounce both units.
//
unit.bounce = true;
auto& bouncing_unit = dislodged_units[r->second];
bouncing_unit.unit_moves = false;
bouncing_unit.bounce = true;
}
else
{
// No, so assume the unit moves for now. However, we may later
// discover another unit which is trying to retreat to the same
// province.
//
retreat_map.insert(AttackMap::value_type
(unit.dest.province, unit.loc.province));
unit.unit_moves = true;
}
}
}
}
//------------------------------------------------------------------------------
void MapAndUnits::advance_attack_rings()
{
Debug::ft("MapAndUnits.advance_attack_rings");
UnitList::iterator ring_member_iterator;
for(auto r = attack_rings.begin(); r != attack_rings.end(); ++r)
{
// Build the list of units in the ring in reverse order.
//
auto first_province = *r;
auto& ring_unit = units[first_province];
auto ring_breaking_prov = NIL_PROVINCE;
UnitList units_in_ring;
do
{
units_in_ring.push_front(ring_unit.loc.province);
// This unit is the ring breaker if it can't advance.
//
ring_unit.ring_status =
calc_ring_status(ring_unit.dest.province, ring_unit.loc.province);
if((ring_unit.ring_status != RING_ADVANCES_REGARDLESS) &&
(ring_unit.ring_status != RING_ADVANCES_IF_VACANT))
{
ring_breaking_prov = ring_unit.loc.province;
ring_member_iterator = units_in_ring.begin();
}
ring_unit = units[ring_unit.dest.province];
}
while(ring_unit.loc.province != first_province);
if(ring_breaking_prov == NIL_PROVINCE)
{
// Each unit in the ring advances.
//
for(auto u = units_in_ring.begin(); u != units_in_ring.end(); ++u)
{
advance_unit(*u);
}
continue; // on to next ring
}
// Check the status of the ring breaker.
//
ring_unit = units[ring_breaking_prov];
if(ring_unit.ring_status == STANDOFF_REGARDLESS)
{
bounce_all_attacks_on_province(ring_unit.dest.province);
}
else if(ring_unit.ring_status == SIDE_ADVANCES_REGARDLESS)
{
bounce_attack(ring_unit);
}
else
{
// We don't know what happens in the province that this unit
// is moving to, so try the previous unit in the ring.
//
if(++ring_member_iterator == units_in_ring.end())
{
ring_member_iterator = units_in_ring.begin();
}
ring_unit = units[*ring_member_iterator];
// The unit after this one is not moving, so check this one.
//
if(ring_unit.ring_status == SIDE_ADVANCES_REGARDLESS)
{
bounce_attack(ring_unit);
}
else if(ring_unit.ring_status != RING_ADVANCES_REGARDLESS)
{
bounce_all_attacks_on_province(ring_unit.dest.province);
}
else
{
// This unit will advance. Work backwards until we find one
// that won't.
do
{
if(++ring_member_iterator == units_in_ring.end())
{
ring_member_iterator = units_in_ring.begin();
}
ring_unit = units[*ring_member_iterator];
if((ring_unit.ring_status == SIDE_ADVANCES_REGARDLESS) ||
(ring_unit.ring_status == SIDE_ADVANCES_IF_VACANT))
{
bounce_attack(ring_unit);
}
else if(ring_unit.ring_status == STANDOFF_REGARDLESS)
{
bounce_all_attacks_on_province(ring_unit.dest.province);
}
}
while((ring_unit.ring_status == RING_ADVANCES_IF_VACANT) ||
(ring_unit.ring_status == RING_ADVANCES_REGARDLESS));
}
}
}
}
//------------------------------------------------------------------------------
void MapAndUnits::advance_unit(ProvinceId from_province)
{
Debug::ft("MapAndUnits.advance_unit");
// The unit in FROM_PROVINCE will move to its DEST, and all
// other units trying to move to DEST will be bounced.
//
auto& attacker = units[from_province];
auto dest = attacker.dest.province;
attacker.unit_moves = true;
for(auto a = attacks.lower_bound(dest); a != attacks.upper_bound(dest); ++a)
{
auto& unit = units[a->second];
if(unit.loc.province != from_province)
{
unit.mark_move_bounced();
}
}
// All attempts to move to DEST have now been resolved.
//
attacks.erase(dest);
}
//------------------------------------------------------------------------------
fn_name MapAndUnits_apply_adjudication = "MapAndUnits.apply_adjudication";
bool MapAndUnits::apply_adjudication()
{
Debug::ft(MapAndUnits_apply_adjudication);
switch(curr_season.all())
{
case TOKEN_SEASON_SPR:
case TOKEN_SEASON_FAL:
apply_moves();
break;
case TOKEN_SEASON_SUM:
case TOKEN_SEASON_AUT:
apply_retreats();
break;
case TOKEN_SEASON_WIN:
apply_builds();
break;
default:
Debug::SwLog(MapAndUnits_apply_adjudication,
"invalid season", curr_season.all());
}
return move_to_next_turn();
}
//------------------------------------------------------------------------------
void MapAndUnits::apply_builds()
{
Debug::ft("MapAndUnits.apply_builds");
for(PowerId p = 0; p < number_of_powers; ++p)
{
auto& orders = winter_orders[p];
for(auto a = orders.adjustments.begin();
a != orders.adjustments.end(); ++a)
{
if(orders.is_building)
{
// Add the newly constructed unit to the global set of units.
//
UnitOrder unit;
unit.loc = a->first;
unit.owner = p;
if(unit.loc.coast == TOKEN_UNIT_AMY)
unit.unit_type = TOKEN_UNIT_AMY;
else
unit.unit_type = TOKEN_UNIT_FLT;
units.insert(UnitOrderMap::value_type(unit.loc.province, unit));
}
else
{
// Erase the removed unit from the global set of units.
//
units.erase(a->first.province);
}
}
}
}
//------------------------------------------------------------------------------
void MapAndUnits::apply_moves()
{
Debug::ft("MapAndUnits.apply_moves");
UnitOrderMap moved_units;
// Run through all the units. Erase those whose locations changed, adding
// them to the list of moved or dislodged units as appropriate.
//
dislodged_units.clear();
auto u = units.begin();
while(u != units.end())
{
auto& unit = u->second;
unit.order = NO_ORDER;
if(unit.unit_moves)
{
moved_units.insert(UnitOrderMap::value_type(unit.dest.province, unit));
u = units.erase(u);
}
else if(unit.dislodged)
{
dislodged_units.insert
(UnitOrderMap::value_type(unit.loc.province, unit));
u = units.erase(u);
}
else
{
++u;
}
}
// Put the moved units in their new locations. The dislodged units
// will await their retreat orders.
//
for(u = moved_units.begin(); u != moved_units.end(); ++u)
{
auto& unit = u->second;
unit.loc = unit.dest;
units.insert(UnitOrderMap::value_type(unit.dest.province, unit));
}
// Provide the retreat options for each dislodged unit.
//
for(u = dislodged_units.begin(); u != dislodged_units.end(); ++u)
{
u->second.open_retreats.clear();
auto& neighbours =
game_map[u->second.loc.province].neighbours[u->second.loc.coast];
for(auto n = neighbours.begin(); n != neighbours.end(); ++n)
{
if((n->province != u->second.dislodged_from) && // <c>
(units.find(n->province) == units.end()) &&
(bounce_provinces.find(n->province) == bounce_provinces.end()))
{
u->second.open_retreats.insert(*n);
}
}
}
}
//------------------------------------------------------------------------------
void MapAndUnits::apply_retreats()
{
Debug::ft("MapAndUnits.apply_retreats");
// Clear the order for all dislodged units. Put each one that moved in
// its new location and clear the set of dislodged units when done.
//
for(auto u = dislodged_units.begin(); u != dislodged_units.end(); ++u)
{
auto& unit = u->second;
unit.order = NO_ORDER;
if(unit.unit_moves)
{
unit.loc = unit.dest;
units.insert(UnitOrderMap::value_type(unit.dest.province, unit));
}
}
dislodged_units.clear();
}
//------------------------------------------------------------------------------
void MapAndUnits::bounce_all_attacks_on_province(ProvinceId dest)
{
Debug::ft("MapAndUnits.bounce_all_attacks_on_province");
// Bounce all moves to DEST.
//
for(auto a = attacks.lower_bound(dest); a != attacks.upper_bound(dest); ++a)
{
auto& unit = units[a->second];
unit.mark_move_bounced();
}
// Remove all bounced units from the attacker map and add DEST
// to the list of provinces to which moves were bounced.
//
attacks.erase(dest);
bounce_provinces.insert(dest);
}
//------------------------------------------------------------------------------
void MapAndUnits::bounce_attack(UnitOrder& unit)
{
Debug::ft("MapAndUnits.bounce_attack");
// Mark UNIT's move as bouncing and remove it from the list of
// attacks on its destination now that it has been resolved.
//
unit.mark_move_bounced();
auto a = attacks.lower_bound(unit.dest.province);
while(a != attacks.upper_bound(unit.dest.province))
{
if(a->second == unit.loc.province)
a = attacks.erase(a);
else
++a;
}
}
//------------------------------------------------------------------------------
void MapAndUnits::build_convoy_subversion_list()
{
Debug::ft("MapAndUnits.build_convoy_subversion_list");
// Check each convoying army to see if it subverts another convoy.
// If it does, record the army whose convoy is being subverted.
//
for(auto a = convoyed_units.begin(); a != convoyed_units.end(); ++a)
{
ConvoySubversion subversion;
auto& army = units[*a];
auto d = units.find(army.dest.province);
if(d != units.end())
{
auto& defender = d->second;
if(defender.owner != army.owner)
{
if(defender.order_type_copy == SUPPORT_TO_HOLD_ORDER)
{
auto& client = units[defender.client_loc];
if(client.order_type_copy == CONVOY_ORDER) // (a)
{
subversion.subverted_army = client.client_loc;
}
}
else if(defender.order_type_copy == SUPPORT_TO_MOVE_ORDER)
{
auto u = units.find(defender.client_dest);
if(u != units.end())
{
auto& client_target = u->second;
if(client_target.order_type_copy == CONVOY_ORDER) // (b)
{
subversion.subverted_army = client_target.client_loc;
}
}
}
}
}
// Record the subversion against the convoying army that would cut
// a support as described above. If the army would not cut such a
// support, the subverted_army field is still NIL_PROVINCE.
//
subversions[*a] = subversion;
}
// Find each army that is subverting a convoy and mark that convoy as
// subverted (its key is its own convoying army).
//
for(auto s = subversions.begin(); s != subversions.end(); ++s)
{
if(s->second.subverted_army != NIL_PROVINCE)
{
auto subverted = subversions.find(s->second.subverted_army);
subverted->second.subversion_type = SUBVERTED_CONVOY;
subverted->second.number_of_subversions++;
}
}
// We're now finished with the convoyed_units set. All further work on
// convoyed units is done through the subversion map. There is no need
// to update convoyed_units for the rest of the adjudicator.
}
//------------------------------------------------------------------------------
void MapAndUnits::build_support_lists()
{
Debug::ft("MapAndUnits.build_support_lists");
// Add each supporting unit to the set of supports for its client.
//
for(auto s = supporting_units.begin(); s != supporting_units.end(); ++s)
{
auto& supporter = units[*s];
auto& client = units[supporter.client_loc];
client.supports.insert(*s);
// A support to move is valid for dislodgement if
// o the attacked province is empty, or
// o the unit in the attacked province belongs to
// neither the supporter nor its client.
//
if(supporter.order_type_copy == SUPPORT_TO_MOVE_ORDER)
{
auto dest = units.find(supporter.client_dest);
if(dest == units.end())
{
supporter.is_support_to_dislodge = true;
++client.supports_to_dislodge;
}
else
{
auto& defender = dest->second;
if((supporter.owner != defender.owner) &&
(client.owner != defender.owner))
{
supporter.is_support_to_dislodge = true;
++client.supports_to_dislodge;
}
}
}
}
// We're now finished with the supporting_units set. All further work on
// supporting units is done through their clients, so there is no need to
// update supporting_units for the rest of the adjudicator.
}
//------------------------------------------------------------------------------
RingUnitStatus MapAndUnits::calc_ring_status
(ProvinceId to_prov, ProvinceId from_prov)
{
Debug::ft("MapAndUnits.calc_ring_status");
size_t most_supports = 0;
size_t most_supports_to_dislodge = 0;
size_t second_most_supports = 0;
ProvinceId most_supported_unit = NIL_PROVINCE;
// Find the strength of the most and second most supported units.
//
for(auto a = attacks.lower_bound(to_prov);
a != attacks.upper_bound(to_prov); ++a)
{
auto& attacker = units[a->second];
auto supports = attacker.supports.size();
if(supports > most_supports)
{
second_most_supports = most_supports;
most_supports = supports;
most_supports_to_dislodge = attacker.supports_to_dislodge;
most_supported_unit = a->second;
}
else if(supports > second_most_supports)
{
second_most_supports = attacker.supports.size();
}
}
// The status of the ring depends on the strength of the two strongest
// units that are trying to enter TO_PROV.
//
if(most_supports == second_most_supports)
{
return STANDOFF_REGARDLESS; // standoff in TO_PROV
}
if(most_supported_unit == from_prov)
{
if((most_supports_to_dislodge > 0) &&
(most_supports_to_dislodge > second_most_supports))
{
return RING_ADVANCES_REGARDLESS; // FROM_PROV enters TO_PROV
}
else
{
return RING_ADVANCES_IF_VACANT; // FROM_PROV enters TO_PROV only
// if TO_PROV also moves
}
}
if((most_supports_to_dislodge > 0) &&
(most_supports_to_dislodge > second_most_supports))
{
return SIDE_ADVANCES_REGARDLESS; // a unit outside ring enters TO_PROV
}
return SIDE_ADVANCES_IF_VACANT; // a unit outside ring enters TO_PROV
// only if TO_PROV also moves
}
//------------------------------------------------------------------------------
void MapAndUnits::cancel_inconsistent_convoys()
{
Debug::ft("MapAndUnits.cancel_inconsistent_convoys");
// For all armies moving by convoy, check that all required fleets
// were ordered to convoy it.
//
auto a = convoyed_units.begin();
while(a != convoyed_units.end())
{
auto order_ok = true;
auto& army = units[*a];
for(auto f = army.convoyers.begin(); f != army.convoyers.end(); ++f)
{
auto u = units.find(*f);
if(u == units.end())
{
order_ok = false;
}
else
{
auto& fleet = u->second;
if((fleet.order_type_copy != CONVOY_ORDER) ||
(fleet.client_loc != army.loc.province) ||
(fleet.client_dest != army.dest.province))
{
order_ok = false;
}
}
}
if(!order_ok)
{
army.order_type_copy = HOLD_NO_SUPPORT_ORDER;
army.no_convoy = true;
a = convoyed_units.erase(a);
}
else
{
++a;
}
}
// For all convoying fleets, check that the army was ordered to
// make use of the convoy.
//
auto f = convoying_units.begin();
while(f != convoying_units.end())
{
auto order_ok = true;
auto& fleet = units[*f];
auto u = units.find(fleet.client_loc);
if(u == units.end())
{
order_ok = false;
}
else
{
auto& army = u->second;
if((army.order != MOVE_BY_CONVOY_ORDER) ||
(army.loc.province != fleet.client_loc) ||
(army.dest.province != fleet.client_dest))
{
order_ok = false;
}
else if(army.order_type_copy != MOVE_BY_CONVOY_ORDER)
{
// The army was ordered to convoy, but other fleets
// failed to complete the chain.
//
order_ok = false;
}
}
if(!order_ok)
{
fleet.no_army_to_convoy = true;
fleet.order_type_copy = HOLD_ORDER;
f = convoying_units.erase(f);
}
else
{
++f;
}
}
// We're now finished with the convoying_units set. All further work on
// convoying units is done through each army's convoyers list. There is
// no need to update convoying_units for the rest of the adjudicator.
}
//------------------------------------------------------------------------------
void MapAndUnits::cancel_inconsistent_supports()
{
Debug::ft("MapAndUnits.cancel_inconsistent_supports");
// For all supports to hold, check that the client isn't moving. For
// all supports to move, check that the client is moving as expected.
//
auto s = supporting_units.begin();
while(s != supporting_units.end())
{
auto order_ok = true;
auto& supporter = units[*s];
auto c = units.find(supporter.client_loc);
if(c == units.end())
{
// The client does not exist.
//
order_ok = false;
supporter.support_void = true;
}
else
{
auto& client = c->second;
switch(supporter.order_type_copy)
{
case SUPPORT_TO_HOLD_ORDER:
switch(client.order_type_copy)
{
case MOVE_ORDER:
case MOVE_BY_CONVOY_ORDER:
case HOLD_NO_SUPPORT_ORDER: // client tried to move but failed
order_ok = false;
supporter.support_void = true;
}
break;
case SUPPORT_TO_MOVE_ORDER:
if((client.order != MOVE_ORDER) &&
(client.order != MOVE_BY_CONVOY_ORDER))
{
// The client wasn't ordered to move.
//
order_ok = false;
supporter.support_void = true;
}
else if(client.dest.province != supporter.client_dest)
{
// The client was ordered to move to a different location.
//
order_ok = false;
supporter.support_void = true;
}
else if((client.order_type_copy != MOVE_ORDER) &&
(client.order_type_copy != MOVE_BY_CONVOY_ORDER))
{
// The client was ordered as supported, but its move failed.
//
order_ok = false;
}
}
}
if(!order_ok)
{
// The support failed, so the supporter will just hold.
//
supporter.order_type_copy = HOLD_ORDER;
s = supporting_units.erase(s);
}
else
{
++s;
}
}
}
//------------------------------------------------------------------------------
bool MapAndUnits::check_for_futile_convoys()
{
Debug::ft("MapAndUnits.check_for_futile_convoys");
// Find each subverted convoy and try to resolve it by checking its
// fleets for dislodgement.
//
auto changes_made = false;
auto s = subversions.begin();
while(s != subversions.end())
{
auto subverting_army_province = s->first;
auto subverted_army_province = s->second.subverted_army;
if(subverted_army_province != NIL_PROVINCE)
{
auto& subverting_army = units[subverting_army_province];
auto& defender = units[subverting_army.dest.province];
auto subverted_client_province = defender.client_loc;
auto& subverted_army = units[subverted_army_province];
auto disrupted = false;
// Resolve the attacks on each fleet except the subverted one.
//
for(auto f = subverted_army.convoyers.begin();
f != subverted_army.convoyers.end(); ++f)
{
if(*f != subverted_client_province)
{
if(resolve_attacks_on_occupied_province(*f))
{
disrupted = true;
}
}
}
// If the convoy was disrupted, revert all of its units to hold.
//
if(disrupted)
{
subverted_army.mark_convoy_disrupted();
// The subverted convoy was disrupted, so it cannot subvert a
// convoy itself.
//
auto& subverted_convoy = subversions[subverted_army_province];
auto nonsubverted =
subversions.find(subverted_convoy.subverted_army);
auto& nonsubverted_convoy = nonsubverted->second;
nonsubverted_convoy.clear(); // <b>
// The convoy that disrupted this one has had its subversion
// resolved.
//
s->second.subverted_army = NIL_PROVINCE;
subversions.erase(s);
changes_made = true;
}
}