-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtestAttacking.cpp
More file actions
1479 lines (1334 loc) · 63.6 KB
/
Copy pathtestAttacking.cpp
File metadata and controls
1479 lines (1334 loc) · 63.6 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
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "GameEvent.h"
#include "PointOutput.h"
#include "Ware.h"
#include "buildings/nobBaseWarehouse.h"
#include "buildings/nobMilitary.h"
#include "factories/BuildingFactory.h"
#include "figures/nofAggressiveDefender.h"
#include "figures/nofAttacker.h"
#include "figures/nofCarrier.h"
#include "figures/nofDefender.h"
#include "figures/nofPassiveSoldier.h"
#include "helpers/Range.h"
#include "helpers/containerUtils.h"
#include "helpers/pointerContainerUtils.h"
#include "pathfinding/FindPathForRoad.h"
#include "worldFixtures/WorldWithGCExecution.h"
#include "worldFixtures/initGameRNG.hpp"
#include "worldFixtures/terrainHelpers.h"
#include "world/GameWorldViewer.h"
#include "nodeObjs/noFlag.h"
#include "gameTypes/GameTypesOutput.h"
#include "gameData/MilitaryConsts.h"
#include "gameData/SettingTypeConv.h"
#include "rttr/test/random.hpp"
#include <rttr/test/testHelpers.hpp>
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include <array>
#include <iostream>
#include <numeric>
using SoldierState = nofActiveSoldier::SoldierState;
// LCOV_EXCL_START
static std::ostream& operator<<(std::ostream& out, const SoldierState s)
{
return out << static_cast<unsigned>(rttr::enum_cast(s));
}
// LCOV_EXCL_STOP
BOOST_AUTO_TEST_SUITE(AttackSuite)
namespace {
namespace dataset = boost::unit_test::data;
struct AttackDefaults
{
static constexpr unsigned width = 20;
static constexpr unsigned height = 12;
};
template<typename T>
auto calcSum(const T& collection)
{
return std::accumulate(std::begin(collection), std::end(collection), 0u);
}
std::array<unsigned, NUM_SOLDIER_RANKS> CountTroopsByRank(const nobMilitary& bld)
{
std::array<unsigned, NUM_SOLDIER_RANKS> counts{};
for(const auto& soldier : bld.GetTroops())
++counts[soldier.GetRank()];
return counts;
}
void DeliverCoin(nobMilitary& bld, GameWorld& world, const MapPoint hqPos)
{
auto* hq = world.GetSpecObj<nobBaseWarehouse>(hqPos);
BOOST_TEST_REQUIRE(hq);
hq->AddToInventory(GoodCounts::make(GoodType::Coins, 1), true);
bld.SearchCoins();
}
/// Reschedule the walk event of the obj to be executed in numGFs GFs
void rescheduleWalkEvent(TestEventManager& em, noMovable& obj, unsigned numGFs)
{
std::vector<const GameEvent*> evts = em.GetObjEvents(obj);
for(const GameEvent* ev : evts)
{
if(ev->id == 0)
{
em.RescheduleEvent(ev, em.GetCurrentGF() + numGFs);
return;
}
}
BOOST_TEST_FAIL("Event not found"); // LCOV_EXCL_LINE
}
/// Move the object next to the given point. The next walk event will make it reach that point
// LCOV_EXCL_START
void moveObjTo(GameWorldBase& world, noFigure& obj, const MapPoint& pos)
{
std::unique_ptr<noFigure> ownedObj;
if(world.HasFigureAt(obj.GetPos(), obj))
ownedObj = world.RemoveFigure(obj.GetPos(), obj);
else
ownedObj = world.RemoveFigure(world.GetNeighbour(obj.GetPos(), obj.GetCurMoveDir() + 3u), obj);
obj.SetPos(world.GetNeighbour(pos, Direction::West));
world.AddFigure(obj.GetPos(), std::move(ownedObj));
if(obj.IsMoving())
obj.FaceDir(Direction::East);
else
obj.StartWalking(Direction::East);
}
// LCOV_EXCL_STOP
template<unsigned T_numPlayers, unsigned T_width, unsigned T_height>
struct AttackFixtureBase : public WorldWithGCExecution<T_numPlayers, T_width, T_height>
{
/// Positions of the players HQ
std::array<MapPoint, T_numPlayers> hqPos;
using Parent = WorldWithGCExecution<T_numPlayers, T_width, T_height>;
using Parent::curPlayer;
using Parent::world;
AttackFixtureBase()
{
for(unsigned i = 0; i < T_numPlayers; i++)
{
curPlayer = i;
hqPos[i] = world.GetPlayer(i).GetHQPos();
MakeVisible(hqPos[i]);
this->ChangeMilitary(MILITARY_SETTINGS_SCALE);
}
curPlayer = 0;
initGameRNG();
}
void MakeVisible(const MapPoint& pt)
{
for(unsigned i = 0; i < T_numPlayers; i++)
world.MakeVisibleAroundPoint(pt, 1, i);
}
void AddSoldiers(MapPoint bldPos, unsigned numSoldiers, Job soldierType, unsigned numArmoredSoldiers = 0)
{
auto const rank = getSoldierRank(soldierType);
BOOST_TEST_REQUIRE(rank <= world.GetGGS().GetMaxMilitaryRank());
auto* bld = world.template GetSpecObj<nobMilitary>(bldPos);
BOOST_TEST_REQUIRE(bld);
const unsigned oldNumSoldiers = bld->GetNumTroops();
for(unsigned i = 0; i < numSoldiers; i++)
{
auto& soldier =
world.AddFigure(bldPos, std::make_unique<nofPassiveSoldier>(bldPos, bld->GetPlayer(), bld, bld, rank));
world.GetPlayer(bld->GetPlayer()).IncreaseInventoryJob(soldier.GetJobType(), 1);
if(numArmoredSoldiers > 0)
{
world.GetPlayer(bld->GetPlayer())
.IncreaseInventoryJob(jobEnumToAmoredSoldierEnum(soldier.GetJobType()), 1);
soldier.SetArmor(true);
numArmoredSoldiers--;
}
// Let him "walk" to goal -> Already reached -> Added and all internal states set correctly
soldier.WalkToGoal();
BOOST_TEST_REQUIRE(!soldier.GetGoal());
}
BOOST_TEST_REQUIRE(bld->GetNumTroops() == oldNumSoldiers + numSoldiers);
}
void AddSoldiers(MapPoint bldPos, unsigned numWeak, unsigned numStrong)
{
AddSoldiers(bldPos, numWeak, Job::Private);
AddSoldiers(bldPos, numStrong, Job::General);
}
// Named constants for indices to make access to the returned array easier to read
static constexpr unsigned real = 0;
static constexpr unsigned visual = 1;
static auto getNumSoldiers(const nobBaseWarehouse& wh)
{
std::array<std::array<unsigned, NUM_SOLDIER_RANKS>, 2> soldiers{};
for(const auto job : SOLDIER_JOBS)
{
soldiers[real][getSoldierRank(job)] = wh.GetNumRealFigures(job);
soldiers[visual][getSoldierRank(job)] = wh.GetNumVisualFigures(job);
}
return soldiers;
}
// Get counts of soldiers
auto getTotalSoldiers(unsigned player)
{
const auto& inv = world.GetPlayer(player).GetInventory();
std::array<unsigned, NUM_SOLDIER_RANKS> soldiers{};
for(const auto job : SOLDIER_JOBS)
soldiers[getSoldierRank(job)] = inv[job];
return soldiers;
}
};
// Size is chosen based on current maximum attacking distances!
struct NumSoldierTestFixture : public AttackFixtureBase<3, 56, 38>
{
/// Tested positions for military buildings
MapPoint milBld0Pos, milBld1NearPos, milBld1FarPos;
/// Military buildings of players 0 and 1 (for 1, one is close and one far away from player 0)
const nobMilitary *milBld0, *milBld1Near, *milBld1Far;
GameWorldViewer gwv;
NumSoldierTestFixture() : gwv(curPlayer, world)
{
// Add some soldiers to the HQs
for(const auto pos : hqPos)
{
world.GetSpecObj<nobBaseWarehouse>(pos)->AddToInventory(
PeopleCounts::make(Job::General, rttr::test::randomValue(3, 10)), true);
}
// Assert player positions: 0: Top-Left, 1: Top-Right, 2: Bottom-Middle
BOOST_TEST_REQUIRE(hqPos[0].x < hqPos[1].x);
BOOST_TEST_REQUIRE(hqPos[0].y < hqPos[2].y);
BOOST_TEST_REQUIRE(hqPos[1].y < hqPos[2].y);
// Build some military buildings
milBld0Pos = hqPos[0] + MapPoint(7, 0);
BOOST_TEST_REQUIRE(world.GetBQ(milBld0Pos, 0) == BuildingQuality::Castle);
milBld0 = dynamic_cast<nobMilitary*>(
BuildingFactory::CreateBuilding(world, BuildingType::Watchtower, milBld0Pos, 0, Nation::Babylonians));
BOOST_TEST_REQUIRE(milBld0);
milBld1NearPos = hqPos[1] - MapPoint(7, 0);
BOOST_TEST_REQUIRE(world.GetBQ(milBld1NearPos, 1) == BuildingQuality::Castle);
milBld1Near = dynamic_cast<nobMilitary*>(
BuildingFactory::CreateBuilding(world, BuildingType::Watchtower, milBld1NearPos, 1, Nation::Romans));
BOOST_TEST_REQUIRE(milBld1Near);
milBld1FarPos = hqPos[1] + MapPoint(3, 1);
BOOST_TEST_REQUIRE(world.GetBQ(milBld1FarPos, 1) == BuildingQuality::Castle);
milBld1Far = dynamic_cast<nobMilitary*>(
BuildingFactory::CreateBuilding(world, BuildingType::Watchtower, milBld1FarPos, 1, Nation::Romans));
BOOST_TEST_REQUIRE(milBld1Far);
MakeVisible(milBld0Pos);
MakeVisible(milBld1NearPos);
MakeVisible(milBld1FarPos);
}
void SetCurPlayer(unsigned playerIdx)
{
curPlayer = playerIdx;
gwv.ChangePlayer(playerIdx, false);
}
};
template<unsigned T_numPlayers = 2, unsigned T_width = AttackDefaults::width,
unsigned T_height = AttackDefaults::height>
struct AttackFixture : public AttackFixtureBase<T_numPlayers, T_width, T_height>
{
using Parent = AttackFixtureBase<T_numPlayers, T_width, T_height>;
using Parent::curPlayer;
using Parent::hqPos;
using Parent::MakeVisible;
using Parent::world;
/// Tested positions for military buildings
MapPoint milBld0Pos, milBld1Pos;
/// Military buildings of players 0 and 1
nobMilitary *milBld0, *milBld1;
AttackFixture()
{
// Build some military buildings far away enough for holding some area outside HQ
milBld0Pos = world.MakeMapPoint(hqPos[0] + Position(0, 6));
BOOST_TEST_REQUIRE(world.GetBQ(milBld0Pos, 0) == BuildingQuality::Castle);
milBld0 = static_cast<nobMilitary*>(
BuildingFactory::CreateBuilding(world, BuildingType::Watchtower, milBld0Pos, 0, Nation::Babylonians));
BOOST_TEST_REQUIRE(milBld0);
milBld1Pos = world.MakeMapPoint(hqPos[1] + Position(0, 6));
BOOST_TEST_REQUIRE(world.GetBQ(milBld1Pos, 1) == BuildingQuality::Castle);
milBld1 = static_cast<nobMilitary*>(
BuildingFactory::CreateBuilding(world, BuildingType::Watchtower, milBld1Pos, 1, Nation::Romans));
BOOST_TEST_REQUIRE(milBld1);
MakeVisible(milBld0Pos);
MakeVisible(milBld1Pos);
}
/// Assert that attacking the given building from attackSrc fails
void TestFailingAttack(const GameWorldViewer& gwv, const MapPoint& bldPos, const nobMilitary& attackSrc,
unsigned numSoldiersLeft = 6u)
{
BOOST_TEST_REQUIRE(attackSrc.GetNumTroops() == numSoldiersLeft);
// No available soldiers
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(bldPos) == 0u);
this->Attack(bldPos, 1, true);
// Same left
BOOST_TEST_REQUIRE(attackSrc.GetNumTroops() == numSoldiersLeft);
}
};
} // namespace
BOOST_FIXTURE_TEST_CASE(NumSoldiersForAttack, NumSoldierTestFixture)
{
AddSoldiers(milBld0Pos, 5, Job::General);
AddSoldiers(milBld0Pos, 1, Job::Sergeant);
AddSoldiers(milBld1NearPos, 6, Job::General);
AddSoldiers(milBld1FarPos, 6, Job::Private);
// Functions related to stationed soldiers
BOOST_TEST_REQUIRE(milBld0->HasMaxRankSoldier());
BOOST_TEST_REQUIRE(milBld1Near->HasMaxRankSoldier());
BOOST_TEST_REQUIRE(!milBld1Far->HasMaxRankSoldier());
BOOST_TEST_REQUIRE(milBld1Near->GetSoldiersStrength() > milBld1Far->GetSoldiersStrength());
BOOST_TEST_REQUIRE(milBld1Near->GetSoldiersStrength() > milBld0->GetSoldiersStrength());
// Player 2 has no military blds -> Can't attack
SetCurPlayer(2);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[0]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[1]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld0Pos) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld1NearPos) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld1FarPos) == 0u);
SetCurPlayer(1);
// No self attack
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[1]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld1NearPos) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld1FarPos) == 0u);
// Attack both others
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[0]) == 5u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[2]) == 5u);
// This is in the extended range of the far bld -> 2 more (with current range)
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld0Pos) == 7u);
SetCurPlayer(0);
// No self attack
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[0]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld0Pos) == 0u);
// Attack both others
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[1]) == 5u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[2]) == 5u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld1NearPos) == 5u);
// Counterpart: 2 possible for far bld
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld1FarPos) == 2u);
// Test in peaceful mode -- no attacks should be possible
this->ggs.setSelection(AddonId::PEACEFULMODE, 1);
SetCurPlayer(1);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[0]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[2]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld0Pos) == 0u);
SetCurPlayer(0);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[1]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(hqPos[2]) == 0u);
BOOST_TEST_REQUIRE(gwv.GetNumSoldiersForAttack(milBld1NearPos) == 0u);
}
BOOST_FIXTURE_TEST_CASE(StartAttack, AttackFixture<>)
{
GameWorldViewer gwv(curPlayer, world);
// Add soldiers (3 strong, 3 weak)
AddSoldiers(milBld0Pos, 3, 3);
const nobMilitary& attackSrc = *milBld0;
const MapPoint usualBldPos = hqPos[1] - MapPoint(2, 3);
BOOST_TEST_REQUIRE(world.GetBQ(usualBldPos, 1) >= BuildingQuality::Hut);
const noBuilding* usualBld =
BuildingFactory::CreateBuilding(world, BuildingType::Woodcutter, usualBldPos, 1, Nation::Romans);
BOOST_TEST_REQUIRE(usualBld);
const MapPoint storehousePos = hqPos[1] - MapPoint(2, 0);
BOOST_TEST_REQUIRE(world.GetBQ(storehousePos, 1) >= BuildingQuality::House);
const noBuilding* storeHouse =
BuildingFactory::CreateBuilding(world, BuildingType::Storehouse, storehousePos, 1, Nation::Romans);
BOOST_TEST_REQUIRE(storeHouse);
MakeVisible(usualBldPos);
MakeVisible(storehousePos);
world.GetPlayer(0).team = Team::Team1;
world.GetPlayer(1).team = Team::Team1;
for(unsigned i = 0; i < 2; i++)
world.GetPlayer(i).MakeStartPacts();
// Try to attack ally -> Fail
TestFailingAttack(gwv, milBld1Pos, attackSrc);
world.GetPlayer(0).team = Team::Team1;
world.GetPlayer(1).team = Team::Team2;
for(unsigned i = 0; i < 2; i++)
world.GetPlayer(i).MakeStartPacts();
// Try to attack non-military bld -> Fail
TestFailingAttack(gwv, usualBldPos, attackSrc);
// Try to attack storehouse -> Fail
TestFailingAttack(gwv, storehousePos, attackSrc);
// Try to attack newly build bld -> Fail
BOOST_TEST_REQUIRE(world.CalcVisiblityWithAllies(milBld1Pos, curPlayer) == Visibility::Visible);
BOOST_TEST_REQUIRE(milBld1->IsNewBuilt());
TestFailingAttack(gwv, milBld1Pos, attackSrc);
// Add soldier
AddSoldiers(milBld1Pos, 1, 0);
BOOST_TEST_REQUIRE(!milBld1->IsNewBuilt());
// Try to attack invisible bld -> Fail
MapNode& node = world.GetNodeWriteable(milBld1Pos);
node.fow[0].visibility = Visibility::FogOfWar;
BOOST_TEST_REQUIRE(world.CalcVisiblityWithAllies(milBld1Pos, curPlayer) == Visibility::FogOfWar);
TestFailingAttack(gwv, milBld1Pos, attackSrc);
// Attack it
node.fow[0].visibility = Visibility::Visible;
BOOST_TEST_REQUIRE(attackSrc.GetNumTroops() == 6u);
auto itTroops = attackSrc.GetTroops().begin();
for(int i = 0; i < 3; i++, ++itTroops)
BOOST_TEST_REQUIRE(itTroops->GetRank() == 0u);
for(int i = 3; i < 6; i++, ++itTroops)
BOOST_TEST_REQUIRE(itTroops->GetRank() == 4u);
this->Attack(milBld1Pos, 1, true);
// 1 strong soldier has left
BOOST_TEST_REQUIRE(attackSrc.GetNumTroops() == 5u);
itTroops = attackSrc.GetTroops().begin();
for(int i = 0; i < 3; i++, ++itTroops)
BOOST_TEST_REQUIRE(itTroops->GetRank() == 0u);
for(int i = 3; i < 5; i++, ++itTroops)
BOOST_TEST_REQUIRE(itTroops->GetRank() == 4u);
// Attack with 1 weak soldier
this->Attack(milBld1Pos, 1, false);
// 1 weak soldier has left
BOOST_TEST_REQUIRE(attackSrc.GetNumTroops() == 4u);
itTroops = attackSrc.GetTroops().begin();
for(int i = 0; i < 2; i++, ++itTroops)
BOOST_TEST_REQUIRE(itTroops->GetRank() == 0u);
for(int i = 2; i < 4; i++, ++itTroops)
BOOST_TEST_REQUIRE(itTroops->GetRank() == 4u);
// -> 2 strong, 2 weak remaining, attack with 3 weak ones -> 1 strong remaining
this->Attack(milBld1Pos, 3, false);
BOOST_TEST_REQUIRE(attackSrc.GetNumTroops() == 1u);
BOOST_TEST(attackSrc.GetTroops().front().GetRank() == 4u);
// None left
TestFailingAttack(gwv, milBld1Pos, attackSrc, 1u);
}
BOOST_FIXTURE_TEST_CASE(TroopLimitCommandRejectsInvalidSerializedRank, AttackFixture<>)
{
std::array<unsigned, NUM_SOLDIER_RANKS> expectedLimits{};
const auto firstLimit = rttr::test::randomValue(1u, 20u);
const auto limitStep = rttr::test::randomValue(1u, 5u);
// Use unique values to detect overwriting
for(unsigned rank = 0; rank < expectedLimits.size(); ++rank)
{
expectedLimits[rank] = firstLimit + rank * limitStep;
this->SetTroopLimit(milBld0Pos, rank, expectedLimits[rank]);
}
for(unsigned rank = 0; rank < expectedLimits.size(); ++rank)
BOOST_TEST_REQUIRE(milBld0->GetTroopLimit(rank) == expectedLimits[rank]);
BOOST_REQUIRE_THROW(this->SetTroopLimit(milBld0Pos, NUM_SOLDIER_RANKS, expectedLimits.back() + limitStep),
std::range_error);
for(unsigned rank = 0; rank < expectedLimits.size(); ++rank)
BOOST_TEST_REQUIRE(milBld0->GetTroopLimit(rank) == expectedLimits[rank]);
}
BOOST_FIXTURE_TEST_CASE(ConquerBld, AttackFixture<>)
{
AddSoldiers(milBld0Pos, 1, 5);
AddSoldiers(milBld1Pos, 1, Job::Private);
AddSoldiers(milBld1Pos, 1, Job::PrivateFirstClass);
// Add soldiers and build road so that HQ and send additional soldiers to the building starting the attack
PeopleCounts soldiers;
soldiers[Job::Sergeant] = 10;
ensureNonNull(world.GetSpecObj<nobBaseWarehouse>(hqPos[curPlayer])).AddToInventory(soldiers, true);
BuildRoadForBlds(milBld0Pos, hqPos[0]);
// Start attack ->1 (weak one first)
this->Attack(milBld1Pos, 1, false);
this->Attack(milBld1Pos, 5, false);
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 1u);
BOOST_TEST_REQUIRE(milBld1->GetNumTroops() == 2u);
// Run till attackers reach bld. 1 Soldier will leave for them.
// 1 stays inside till an attacker is at door
// 20 GFs/node + 30 GFs for leaving
const unsigned distance = world.CalcDistance(milBld0Pos, milBld1Pos);
RTTR_EXEC_TILL(distance * 20 + 30, milBld1->GetNumTroops() == 1);
BOOST_TEST_REQUIRE(milBld1->GetNumTroops() + milBld1->GetLeavingFigures().size() == 2u);
const Inventory& attackedPlInventory = world.GetPlayer(1).GetInventory();
const unsigned oldWeakSoldierCt = attackedPlInventory.people[Job::Private];
const unsigned oldStrongerSoldierCt = attackedPlInventory.people[Job::PrivateFirstClass];
const unsigned oldAttackerStrongSoldierCt = world.GetPlayer(curPlayer).GetInventory().people[Job::General];
// 1st soldier will walk towards attacker and will be killed
// Once an attacker reaches the flag, the bld will send a defender
BOOST_TEST_REQUIRE(!milBld1->GetDefender());
RTTR_EXEC_TILL(300, milBld1->GetNumTroops() == 0);
// Defender deployed, attacker at flag
BOOST_TEST_REQUIRE(milBld1->GetDefender());
{
const auto figures = world.GetFigures(milBld1->GetFlagPos());
BOOST_TEST_REQUIRE(figures.size() == 1u);
const auto& attacker = dynamic_cast<const nofAttacker&>(*figures.begin());
BOOST_TEST_REQUIRE(static_cast<const nofAttacker&>(attacker).GetPlayer() == curPlayer);
}
// Lets fight
RTTR_EXEC_TILL(1000, milBld1->IsBeingCaptured());
// Let others in
RTTR_EXEC_TILL(200, !milBld1->IsBeingCaptured());
// Building conquered
BOOST_TEST_REQUIRE(milBld1->GetPlayer() == curPlayer);
// 1 soldier must be inside
BOOST_TEST_REQUIRE(milBld1->GetNumTroops() > 1u);
// Weak soldier must be dead
BOOST_TEST_REQUIRE(attackedPlInventory.people[Job::Private] == oldWeakSoldierCt - 1);
// Src building gets refilled
RTTR_EXEC_TILL(800, milBld0->GetNumTroops() == 6u);
// We may have lost soldiers
BOOST_TEST_REQUIRE(world.GetPlayer(curPlayer).GetInventory().people[Job::General] <= oldAttackerStrongSoldierCt);
// The enemy may have lost his stronger soldier
BOOST_TEST_REQUIRE(attackedPlInventory.people[Job::PrivateFirstClass] <= oldStrongerSoldierCt);
// But only one
BOOST_TEST_REQUIRE(attackedPlInventory.people[Job::PrivateFirstClass] >= oldStrongerSoldierCt - 1);
// At least 2 survivors
BOOST_TEST_REQUIRE(milBld1->GetNumTroops() > 2u);
// Points around bld should be ours
const std::vector<MapPoint> pts = world.GetPointsInRadius(milBld1Pos, 3);
for(const MapPoint& pt : pts)
{
BOOST_TEST_REQUIRE(world.GetNode(pt).owner == curPlayer + 1u);
}
}
BOOST_FIXTURE_TEST_CASE(ArmoredSoldierLosesArmorInFight, AttackFixture<>)
{
AddSoldiers(milBld0Pos, 3, Job::General, 2);
AddSoldiers(milBld1Pos, 1, Job::Private, 1);
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 3u);
BOOST_TEST_REQUIRE(milBld1->GetNumTroops() == 1u);
// Start attack ->1 (strong one first)
this->Attack(milBld1Pos, 1, true);
RTTR_EXEC_TILL(30, milBld0->GetNumTroops() == 2);
auto countArmoredSoldiers = [](nobMilitary* bld) {
const auto troops = bld->GetTroops();
return std::count_if(std::begin(troops), std::end(troops),
[](const auto& soldier) { return soldier.HasArmor(); });
};
auto countNotArmoredSoldiers = [](nobMilitary* bld) {
const auto troops = bld->GetTroops();
return std::count_if(std::begin(troops), std::end(troops),
[](const auto& soldier) { return !soldier.HasArmor(); });
};
BOOST_TEST_REQUIRE(countArmoredSoldiers(milBld0) == 1u);
BOOST_TEST_REQUIRE(countNotArmoredSoldiers(milBld0) == 1u);
// Start attack ->1 (weak one first)
this->Attack(milBld1Pos, 1, false);
RTTR_EXEC_TILL(30, milBld0->GetNumTroops() == 1);
BOOST_TEST_REQUIRE(countArmoredSoldiers(milBld0) == 1u);
BOOST_TEST_REQUIRE(countNotArmoredSoldiers(milBld0) == 0u);
const Inventory& attackedPlInventory = world.GetPlayer(1).GetInventory();
const unsigned numOldWeakSoldiers = attackedPlInventory.people[Job::Private];
const unsigned numOldWeakSoldiersWithArmor = attackedPlInventory[jobEnumToAmoredSoldierEnum(Job::Private)];
// Run till attackers reach flag of bld. The bld will send a defender.
// 20 GFs/node + 60 GFs for leaving
const unsigned distance = world.CalcDistance(milBld0Pos, milBld1Pos);
RTTR_EXEC_TILL(distance * 20 + 60, milBld1->GetDefender() != nullptr);
const auto soldiers = world.GetFigures(milBld1->GetFlagPos());
BOOST_TEST_REQUIRE(soldiers.size() == 1u);
const auto& attacker = dynamic_cast<const nofAttacker&>(*soldiers.begin());
BOOST_TEST_REQUIRE(static_cast<const nofAttacker&>(attacker).GetPlayer() == curPlayer);
// Lets fight until defender has no armor anymore
// He should not lose a hitpoint but the armor
RTTR_EXEC_TILL(1000, milBld1->GetDefender()->HasArmor() == false);
BOOST_TEST(attackedPlInventory[jobEnumToAmoredSoldierEnum(Job::Private)] == numOldWeakSoldiersWithArmor - 1);
BOOST_TEST(attackedPlInventory.people[Job::Private] == numOldWeakSoldiers);
BOOST_TEST(milBld1->GetDefender()->GetHitpoints() == HITPOINTS[milBld1->GetDefender()->GetRank()]);
}
BOOST_FIXTURE_TEST_CASE(CoinTrainingUpgradesRankChainByDefault, AttackFixture<>)
{
AddSoldiers(milBld0Pos, 1, Job::Private);
AddSoldiers(milBld0Pos, 1, Job::PrivateFirstClass);
AddSoldiers(milBld0Pos, 1, Job::Sergeant);
BuildRoadForBlds(milBld0Pos, hqPos[0]);
DeliverCoin(*milBld0, world, hqPos[0]);
RTTR_EXEC_TILL(5000, CountTroopsByRank(*milBld0)[3] == 1u);
const auto counts = CountTroopsByRank(*milBld0);
BOOST_TEST_REQUIRE(counts[0] == 0u);
BOOST_TEST_REQUIRE(counts[1] == 1u);
BOOST_TEST_REQUIRE(counts[2] == 1u);
BOOST_TEST_REQUIRE(counts[3] == 1u);
}
BOOST_FIXTURE_TEST_CASE(SingleSoldierCoinTrainingUpgradesOnlyLowestRankSoldier, AttackFixture<>)
{
this->ggs.setSelection(AddonId::SINGLE_SOLDIER_COIN_TRAINING, 1);
AddSoldiers(milBld0Pos, 1, Job::Private);
AddSoldiers(milBld0Pos, 1, Job::PrivateFirstClass);
AddSoldiers(milBld0Pos, 1, Job::Sergeant);
BuildRoadForBlds(milBld0Pos, hqPos[0]);
DeliverCoin(*milBld0, world, hqPos[0]);
RTTR_EXEC_TILL(5000, CountTroopsByRank(*milBld0)[1] == 2u);
const auto counts = CountTroopsByRank(*milBld0);
BOOST_TEST_REQUIRE(counts[0] == 0u);
BOOST_TEST_REQUIRE(counts[1] == 2u);
BOOST_TEST_REQUIRE(counts[2] == 1u);
BOOST_TEST_REQUIRE(counts[3] == 0u);
}
BOOST_FIXTURE_TEST_CASE(TroopLimitKeepsOrderedRestrictedSoldier, AttackFixture<>)
{
MilitarySettings milSettings = MILITARY_SETTINGS_SCALE;
milSettings[4 + rttr::enum_cast(FrontierDistance::Far)] = 0;
this->ChangeMilitary(milSettings);
auto* hq = world.GetSpecObj<nobBaseWarehouse>(hqPos[0]);
BOOST_TEST_REQUIRE(hq);
const Job soldierJob = Job::Private;
const unsigned soldierRank = getSoldierRank(soldierJob);
hq->AddToInventory(PeopleCounts::make(soldierJob, 2), true);
const unsigned hqSoldiersBefore = hq->GetNumRealFigures(soldierJob);
BOOST_TEST_REQUIRE(hqSoldiersBefore > 0u);
BuildRoadForBlds(milBld0Pos, hqPos[0]);
milBld0->RegulateTroops();
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 0u);
BOOST_TEST_REQUIRE(hq->GetLeavingFigures().size() == 1u);
BOOST_TEST_REQUIRE(hq->GetNumRealFigures(soldierJob) + 1 == hqSoldiersBefore);
this->SetInventorySetting(hqPos[0], soldierJob, EInventorySetting::Stop);
milBld0->SetTroopLimit(soldierRank, 0);
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 0u);
BOOST_TEST_REQUIRE(hq->GetLeavingFigures().size() == 1u);
BOOST_TEST_REQUIRE(hq->GetNumRealFigures(soldierJob) + 1 == hqSoldiersBefore);
RTTR_EXEC_TILL(500, milBld0->GetNumTroops() == 1u);
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 1u);
BOOST_TEST_REQUIRE(hq->GetNumRealFigures(soldierJob) + 1 == hqSoldiersBefore);
BOOST_TEST_REQUIRE(milBld0->GetLeavingFigures().empty());
}
BOOST_FIXTURE_TEST_CASE(ConquerBldCoinAddonEnable, AttackFixture<>)
{
this->ggs.setSelection(AddonId::COINS_CAPTURED_BLD, 1); // addon is active on second run
AddSoldiers(milBld0Pos, 1, 5);
AddSoldiers(milBld1Pos, 1, Job::Private);
// ensure that coins are disabled
milBld1->SetCoinsAllowed(false);
BOOST_TEST_REQUIRE(milBld1->IsGoldDisabled());
// Start attack -> 1
this->Attack(milBld1Pos, 6, false);
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 1u);
RTTR_EXEC_TILL(2000, milBld1->GetPlayer() == curPlayer);
// check if coins were enabled after building was captured
BOOST_TEST_REQUIRE(!milBld1->IsGoldDisabled());
}
BOOST_FIXTURE_TEST_CASE(ConquerBldCoinAddonDisable, AttackFixture<>)
{
this->ggs.setSelection(AddonId::COINS_CAPTURED_BLD, 2); // addon is active on second run
AddSoldiers(milBld0Pos, 1, 5);
AddSoldiers(milBld1Pos, 1, Job::Private);
// ensure that coins are enabled
milBld1->SetCoinsAllowed(true);
BOOST_TEST_REQUIRE(!milBld1->IsGoldDisabled());
// Start attack -> 1
this->Attack(milBld1Pos, 6, false);
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 1u);
RTTR_EXEC_TILL(2000, milBld1->GetPlayer() == curPlayer);
// check if coins were disabled after building was captured
BOOST_TEST_REQUIRE(milBld1->IsGoldDisabled());
}
enum Case
{
EnableAfterDisable,
DisableAfterEnable,
KeepEnabled,
KeepDisabled
};
BOOST_DATA_TEST_CASE_F(AttackFixture<>, ConquerBldArmorAddon,
dataset::make(std::array{Case::EnableAfterDisable, Case::DisableAfterEnable, Case::KeepEnabled,
Case::KeepDisabled}))
{
if(sample == Case::EnableAfterDisable)
this->ggs.setSelection(AddonId::ARMOR_CAPTURED_BLD, 1);
else if(sample == Case::DisableAfterEnable)
this->ggs.setSelection(AddonId::ARMOR_CAPTURED_BLD, 2);
else
this->ggs.setSelection(AddonId::ARMOR_CAPTURED_BLD, 0);
AddSoldiers(milBld0Pos, 1, 5);
AddSoldiers(milBld1Pos, 1, Job::Private);
// ensure that armor are enabled/disabled
bool armorAllowedBeforeAttack = (sample == Case::KeepEnabled || sample == Case::DisableAfterEnable);
milBld1->SetArmorAllowed(armorAllowedBeforeAttack);
BOOST_TEST_REQUIRE(milBld1->IsArmorAllowed() == armorAllowedBeforeAttack);
// Start attack -> 1
this->Attack(milBld1Pos, 5, false);
BOOST_TEST_REQUIRE(milBld0->GetNumTroops() == 1u);
RTTR_EXEC_TILL(2000, milBld1->GetPlayer() == curPlayer);
// check if armor were enabled/disabled after building was captured
bool armorAlloweAfterCaptured = (sample == Case::KeepEnabled || sample == Case::EnableAfterDisable);
BOOST_TEST_REQUIRE(milBld1->IsArmorAllowed() == armorAlloweAfterCaptured);
}
using AttackFixture4P = AttackFixture<4, 32, 34>;
BOOST_FIXTURE_TEST_CASE(ConquerWithMultipleWalkingIn, AttackFixture4P)
{
world.GetPlayer(0).team = Team::Team1; //-V525
world.GetPlayer(1).team = Team::None;
world.GetPlayer(2).team = Team::Team1; // Allied to 0
world.GetPlayer(3).team = Team::Team2; // Hostile to 0
for(unsigned i = 0; i < 4; i++)
world.GetPlayer(i).MakeStartPacts();
MilitarySettings milSettings = MILITARY_SETTINGS_SCALE;
milSettings[2] = 0; // No aggressive defenders for attacker
this->ChangeMilitary(milSettings);
AddSoldiers(milBld0Pos, 0, 6);
AddSoldiers(milBld1Pos, 1, Job::Private);
MapPoint milBld1FlagPos = world.GetNeighbour(milBld1Pos, Direction::SouthEast);
// Scenario 1: Attack with one soldier.
// Once enemy is defeated we walk in with another soldier of the enemy who wants to occupy its building.
// The other soldier is faster -> we have to fight him
this->Attack(milBld1Pos, 1, true);
BOOST_TEST_REQUIRE(milBld0->GetLeavingFigures().size() == 1u); //-V807
auto& attacker = dynamic_cast<nofAttacker&>(milBld0->GetLeavingFigures().front());
// Let him come out
RTTR_EXEC_TILL(70, milBld0->GetLeavingFigures().empty()); //-V807
moveObjTo(world, attacker, milBld1FlagPos);
BOOST_TEST_REQUIRE(!milBld1->IsDoorOpen());
const auto flagFigs = world.GetFigures(milBld1FlagPos);
RTTR_EXEC_TILL(70, flagFigs.size() == 1u && flagFigs.begin()->GetGOT() == GO_Type::Fighting); //-V807
BOOST_TEST_REQUIRE(!milBld1->IsDoorOpen());
// Speed up fight by reducing defenders HP to 1
auto* defender = const_cast<nofDefender*>(milBld1->GetDefender());
while(defender->GetHitpoints() > 1u)
defender->TakeHit();
RTTR_EXEC_TILL(500, milBld1->GetDefender() == nullptr);
// Defender defeated. Attacker moving in.
BOOST_TEST_REQUIRE(attacker.IsMoving());
BOOST_TEST_REQUIRE(attacker.GetCurMoveDir() == Direction::NorthWest);
// Door opened
BOOST_TEST_REQUIRE(milBld1->IsDoorOpen());
// New soldiers walked in
AddSoldiers(milBld1Pos, 4, Job::Private);
// Let attacker walk in (try it at least)
RTTR_EXEC_TILL(20, attacker.GetPos() == milBld1Pos);
RTTR_EXEC_TILL(20, attacker.GetPos() == milBld1FlagPos);
// New fight and door closed
RTTR_EXEC_TILL(70, flagFigs.size() == 1u && flagFigs.front().GetGOT() == GO_Type::Fighting);
BOOST_TEST_REQUIRE(!milBld1->IsDoorOpen());
// Scenario 2: Attacker successful
// We want all possible troops:
// 1. Attackers from this building
// 2. Aggressive defenders from this building
// 3. Allied aggressor towards this bld
// 4. Hostile aggressor towards this bld
// 5. Occupying soldier of the player on the way in the building
// 1.
curPlayer = 1;
this->Attack(milBld0Pos, 1, false);
BOOST_TEST_REQUIRE(milBld1->GetLeavingFigures().size() == 1u);
auto& attackerFromPl0 = dynamic_cast<nofAttacker&>(milBld1->GetLeavingFigures().front());
// 2.
curPlayer = 0;
this->Attack(milBld1Pos, 1, true);
// Move him directly out
BOOST_TEST_REQUIRE(milBld0->GetLeavingFigures().size() == 1u);
auto& secAttacker = dynamic_cast<nofAttacker&>(milBld0->GetLeavingFigures().front());
RTTR_EXEC_TILL(70, milBld0->GetLeavingFigures().empty()); //-V807
moveObjTo(world, secAttacker, world.MakeMapPoint(milBld1FlagPos - Position(15, 0)));
nofAggressiveDefender& aggDefender = ensureNonNull(milBld1->SendAggressiveDefender(secAttacker));
secAttacker.LetsFight(aggDefender);
// 3.
curPlayer = 2;
MapPoint bldPos = hqPos[curPlayer] + MapPoint(3, 0);
auto* alliedBld = static_cast<nobMilitary*>(
BuildingFactory::CreateBuilding(world, BuildingType::Guardhouse, bldPos, curPlayer, Nation::Africans));
AddSoldiers(bldPos, 2, Job::Private);
this->Attack(milBld1Pos, 1, false);
BOOST_TEST_REQUIRE(alliedBld->GetLeavingFigures().size() == 1u);
auto& alliedAttacker = dynamic_cast<nofAttacker&>(alliedBld->GetLeavingFigures().front());
// 4.
curPlayer = 3;
bldPos = hqPos[curPlayer] + MapPoint(3, 0);
auto* hostileBld = static_cast<nobMilitary*>(
BuildingFactory::CreateBuilding(world, BuildingType::Guardhouse, bldPos, curPlayer, Nation::Africans));
AddSoldiers(bldPos, 2, Job::Private);
this->Attack(milBld1Pos, 1, false);
BOOST_TEST_REQUIRE(hostileBld->GetLeavingFigures().size() == 1u);
auto& hostileAttacker = dynamic_cast<nofAttacker&>(hostileBld->GetLeavingFigures().front());
// Make sure all other soldiers left their buildings (<=30GFs each + 20 for walking to flag and a bit further)
RTTR_SKIP_GFS(30 + 20 + 10);
// And suspend them to inspect them later on
rescheduleWalkEvent(em, attackerFromPl0, 10000);
rescheduleWalkEvent(em, secAttacker, 10000);
rescheduleWalkEvent(em, alliedAttacker, 10000);
rescheduleWalkEvent(em, hostileAttacker, 10000);
// We got 2 from milBld1
RTTR_SKIP_GFS(30);
rescheduleWalkEvent(em, aggDefender, 10000);
// Let defenders (2!) die
defender = const_cast<nofDefender*>(milBld1->GetDefender());
while(defender->GetHitpoints() > 1u)
defender->TakeHit();
RTTR_EXEC_TILL(500, milBld1->GetNumTroops() == 0u);
defender = const_cast<nofDefender*>(milBld1->GetDefender());
while(defender->GetHitpoints() > 1u)
defender->TakeHit();
RTTR_EXEC_TILL(500, milBld1->GetDefender() == nullptr);
// Defender defeated. Attacker moving in.
BOOST_TEST_REQUIRE(attacker.IsMoving());
BOOST_TEST_REQUIRE(attacker.GetCurMoveDir() == Direction::NorthWest);
// Door opened
BOOST_TEST_REQUIRE(milBld1->IsDoorOpen());
// Give him a bit of a head start
RTTR_SKIP_GFS(1);
// 5. Create new soldier who walks in after the attacker
auto& newSld =
world.AddFigure(milBld1FlagPos, std::make_unique<nofPassiveSoldier>(milBld1FlagPos, 1, milBld1, milBld1, 0));
;
milBld1->GotWorker(newSld.GetJobType(), newSld);
newSld.ActAtFirst();
// Attacker faster -> Bld taken
RTTR_EXEC_TILL(30, milBld1->GetPlayer() == 0u);
// New soldier should be wandering
RTTR_EXEC_TILL(10, newSld.IsWandering());
// And door should be closed (latest after wandering soldier left flag)
RTTR_EXEC_TILL(40, !milBld1->IsDoorOpen());
// 1. Attackers from this building
// No home -> Wander
BOOST_TEST_REQUIRE(!attackerFromPl0.GetHomeBld());
rescheduleWalkEvent(em, attackerFromPl0, 1);
RTTR_EXEC_TILL(2, attackerFromPl0.IsWandering());
// 2. Aggressive defenders from this building
// No further attack (unless already fighting) and wander
// The attacker proceeds to the building and occupies it
rescheduleWalkEvent(em, secAttacker, 1);
rescheduleWalkEvent(em, aggDefender, 2);
RTTR_SKIP_GFS(2);
BOOST_TEST_REQUIRE(aggDefender.GetAttacker() == nullptr);
BOOST_TEST_REQUIRE(secAttacker.GetHuntingDefender() == nullptr);
BOOST_TEST_REQUIRE(aggDefender.IsWandering());
RTTR_EXEC_TILL(270, milBld1->GetNumTroops() == 2u);
// 3. Allied aggressor towards this bld
// Abort attack and return home
rescheduleWalkEvent(em, alliedAttacker, 1);
RTTR_EXEC_TILL(1, alliedAttacker.GetAttackedGoal() == nullptr);
RTTR_EXEC_TILL(90, alliedBld->GetNumTroops() == 2u);
// 4. Hostile aggressor towards this bld
// Continue attack and fight
rescheduleWalkEvent(em, hostileAttacker, 1);
BOOST_TEST_REQUIRE(hostileAttacker.GetAttackedGoal() != nullptr);
RTTR_EXEC_TILL(220, hostileAttacker.GetPos() == milBld1FlagPos);
RTTR_EXEC_TILL(50, world.GetFigures(milBld1FlagPos).begin()->GetGOT() == GO_Type::Fighting);
}
BOOST_FIXTURE_TEST_CASE(ConquerWithCarriersWalkingIn, AttackFixture<2>)
{
// 1. Carrier with coin walking in the building
// 2. Carrier with coin walking out of the building
AddSoldiers(milBld0Pos, 0, 6);
AddSoldiers(milBld1Pos, 1, Job::Private);
MapPoint milBld1FlagPos = world.GetNeighbour(milBld1Pos, Direction::SouthEast);
curPlayer = 1;
MapPoint flagPos = world.MakeMapPoint(milBld1FlagPos - Position(2, 0));
this->BuildRoad(milBld1FlagPos, false, std::vector<Direction>(2, Direction::West));
auto* flag = world.GetSpecObj<noFlag>(flagPos);
BOOST_TEST_REQUIRE(flag);
RoadSegment* rs = flag->GetRoute(Direction::East);
BOOST_TEST_REQUIRE(rs);
auto& carrierIn =
world.AddFigure(flagPos, std::make_unique<nofCarrier>(CarrierType::Normal, flagPos, curPlayer, rs, flag));
auto& carrierOut =
world.AddFigure(flagPos, std::make_unique<nofCarrier>(CarrierType::Donkey, flagPos, curPlayer, rs, flag));
rs->setCarrier(0, &carrierIn);
rs->setCarrier(1, &carrierOut);
// Add 2 coins for the bld
for(unsigned i = 0; i < 2; i++)
{
auto coin = std::make_unique<Ware>(GoodType::Coins, milBld1, flag);
coin->WaitAtFlag(flag);
coin->RecalcRoute();
flag->AddWare(std::move(coin));
}
world.GetPlayer(1).IncreaseInventoryWare(GoodType::Coins, 2);
carrierIn.ActAtFirst();
carrierOut.ActAtFirst();
// Both picked up
BOOST_TEST_REQUIRE(flag->GetNumWares() == 0u);
// Move carriers to flag
for(unsigned i = 0; i < 2; i++)
{
rescheduleWalkEvent(em, carrierIn, 1);
rescheduleWalkEvent(em, carrierOut, 1);
RTTR_SKIP_GFS(1);
}
// And pause them
rescheduleWalkEvent(em, carrierIn, 10000);
// After the out-walking was in
rescheduleWalkEvent(em, carrierOut, 1);
RTTR_SKIP_GFS(1);
rescheduleWalkEvent(em, carrierOut, 10000);
BOOST_TEST_REQUIRE(carrierIn.GetCurMoveDir() == Direction::NorthWest);
BOOST_TEST_REQUIRE(carrierOut.GetCurMoveDir() == Direction::SouthEast);
// Add another for later
MapPoint flagPosE = world.MakeMapPoint(milBld1FlagPos + Position(2, 0));
this->BuildRoad(milBld1FlagPos, false, std::vector<Direction>(2, Direction::East));
auto* flagE = world.GetSpecObj<noFlag>(flagPosE);
BOOST_TEST_REQUIRE(flagE);
RoadSegment* rsE = flagE->GetRoute(Direction::West);
BOOST_TEST_REQUIRE(rsE);
auto& carrierInE =
world.AddFigure(flagPosE, std::make_unique<nofCarrier>(CarrierType::Normal, flagPosE, curPlayer, rsE, flagE));
rsE->setCarrier(0, &carrierInE);
// He also gets 1 coin
auto coin = std::make_unique<Ware>(GoodType::Coins, milBld1, flagE);
coin->WaitAtFlag(flagE);
coin->RecalcRoute();
flagE->AddWare(std::move(coin));
world.GetPlayer(1).IncreaseInventoryWare(GoodType::Coins, 1);
carrierInE.ActAtFirst();
// Picked up
BOOST_TEST_REQUIRE(flagE->GetNumWares() == 0u);
// And pause him
rescheduleWalkEvent(em, carrierInE, 10000);
curPlayer = 0;
this->Attack(milBld1Pos, 1, true);
BOOST_TEST_REQUIRE(milBld0->GetLeavingFigures().size() == 1u);
auto& attacker = dynamic_cast<nofAttacker&>(milBld0->GetLeavingFigures().front());
// Move him directly out
RTTR_EXEC_TILL(70, milBld0->GetLeavingFigures().empty()); //-V807
moveObjTo(world, attacker, milBld1FlagPos);
RTTR_EXEC_TILL(20, attacker.GetPos() == milBld1FlagPos);
// Carriers on pos or to pos get send away as soon as soldier arrives
rescheduleWalkEvent(em, carrierIn, 1);
rescheduleWalkEvent(em, carrierOut, 1);
RTTR_SKIP_GFS(1);
BOOST_TEST_REQUIRE(carrierIn.IsWandering());
BOOST_TEST_REQUIRE(carrierOut.IsWandering());
// Let east carrier walk
rescheduleWalkEvent(em, carrierInE, 1);
// Start fight
const auto flagFigs = world.GetFigures(milBld1FlagPos);
RTTR_EXEC_TILL(50, flagFigs.size() == 1u && flagFigs.front().GetGOT() == GO_Type::Fighting);
// East carrier gets blocked
BOOST_TEST_REQUIRE(!carrierInE.IsMoving());
// Door closed latest after other carriers are gone
RTTR_EXEC_TILL(20, !milBld1->IsDoorOpen());
// Speed up fight by reducing defenders HP to 1
auto* defender = const_cast<nofDefender*>(milBld1->GetDefender());
while(defender->GetHitpoints() > 1u)
defender->TakeHit();
RTTR_EXEC_TILL(500, milBld1->GetDefender() == nullptr);
// Defender defeated. Attacker moving in.