1 #include "BattleTest.h"
3 #include "../../src/battle/Battle.h"
4 #include "../../src/battle/PartyLayout.h"
8 CPPUNIT_TEST_SUITE_REGISTRATION(test_battle::BattleTest);
10 using battle::AttackChoice;
12 using battle::PartyLayout;
16 namespace test_battle {
18 void BattleTest::setUp() {
19 positions[0] = Vector<int>(0, 0);
20 positions[1] = Vector<int>(1, 1);
21 twoLayout = new PartyLayout;
22 twoLayout->SetPositions(positions, 2);
24 positions[2] = Vector<int>(0, 0);
25 positions[3] = Vector<int>(1, 1);
26 positions[4] = Vector<int>(2, 2);
27 positions[5] = Vector<int>(3, 3);
28 positions[6] = Vector<int>(4, 4);
29 fiveLayout = new PartyLayout;
30 fiveLayout->SetPositions(positions + 2, 5);
34 hero.GetStats().SetAgility(10);
36 monster.SetMaxHealth(8);
38 monster.GetStats().SetAgility(20);
41 void BattleTest::tearDown() {
42 monster = battle::Monster();
43 hero = common::Hero();
44 capsule = common::Capsule();
50 void BattleTest::testSetup() {
51 CPPUNIT_ASSERT_THROW(Battle battle(0, twoLayout), std::invalid_argument);
52 CPPUNIT_ASSERT_THROW(Battle battle(fiveLayout, 0), std::invalid_argument);
54 Battle battle(fiveLayout, twoLayout);
55 CPPUNIT_ASSERT_EQUAL(0, battle.NumHeroes());
56 CPPUNIT_ASSERT_EQUAL(4, battle.MaxHeroes());
57 CPPUNIT_ASSERT(!battle.HeroPositionOccupied(0));
58 // number of monsters is always max
59 CPPUNIT_ASSERT_EQUAL(2, battle.NumMonsters());
60 CPPUNIT_ASSERT_EQUAL(2, battle.MaxMonsters());
61 CPPUNIT_ASSERT(!battle.MonsterPositionOccupied(0));
62 CPPUNIT_ASSERT(!battle.HasCapsule());
65 CPPUNIT_ASSERT_EQUAL(1, battle.NumHeroes());
66 CPPUNIT_ASSERT_EQUAL(4, battle.MaxHeroes());
67 CPPUNIT_ASSERT(battle.HeroPositionOccupied(0));
68 CPPUNIT_ASSERT_EQUAL(2, battle.NumMonsters());
69 CPPUNIT_ASSERT_EQUAL(2, battle.MaxMonsters());
70 CPPUNIT_ASSERT(!battle.HasCapsule());
73 CPPUNIT_ASSERT(battle.HeroPositionOccupied(1));
75 CPPUNIT_ASSERT(battle.HeroPositionOccupied(2));
77 CPPUNIT_ASSERT(battle.HeroPositionOccupied(3));
78 CPPUNIT_ASSERT_EQUAL(4, battle.NumHeroes());
79 CPPUNIT_ASSERT_THROW(battle.AddHero(hero), std::overflow_error);
80 CPPUNIT_ASSERT_EQUAL(4, battle.NumHeroes());
82 battle.AddMonster(monster);
83 CPPUNIT_ASSERT_EQUAL(4, battle.NumHeroes());
84 CPPUNIT_ASSERT_EQUAL(4, battle.MaxHeroes());
85 CPPUNIT_ASSERT_EQUAL(2, battle.NumMonsters());
86 CPPUNIT_ASSERT_EQUAL(2, battle.MaxMonsters());
87 CPPUNIT_ASSERT(battle.MonsterPositionOccupied(0));
88 CPPUNIT_ASSERT(!battle.HasCapsule());
90 battle.AddMonster(monster);
91 CPPUNIT_ASSERT(battle.MonsterPositionOccupied(1));
92 CPPUNIT_ASSERT_EQUAL(2, battle.NumMonsters());
93 CPPUNIT_ASSERT_THROW(battle.AddMonster(monster), std::overflow_error);
94 CPPUNIT_ASSERT_EQUAL(2, battle.NumMonsters());
97 void BattleTest::testHeroAttackSelection() {
98 Battle battle(fiveLayout, twoLayout);
101 // turns start out with the cursor before the first hero
103 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
104 CPPUNIT_ASSERT(!battle.HasChosenAttackType());
106 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::SWORD);
107 battle.ActiveHero().GetAttackChoice().Selection().SetSingle();
108 battle.ActiveHero().GetAttackChoice().Selection().Select();
109 CPPUNIT_ASSERT(battle.HasChosenAttackType());
112 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
113 CPPUNIT_ASSERT(!battle.HasChosenAttackType());
115 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
116 CPPUNIT_ASSERT(battle.HasChosenAttackType());
117 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
120 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
121 CPPUNIT_ASSERT(!battle.HasChosenAttackType());
122 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
124 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
125 CPPUNIT_ASSERT(battle.HasChosenAttackType());
128 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
129 CPPUNIT_ASSERT(!battle.HasChosenAttackType());
130 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
132 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
133 CPPUNIT_ASSERT(battle.HasChosenAttackType());
134 CPPUNIT_ASSERT(!battle.AttackSelectionDone());
137 CPPUNIT_ASSERT(battle.AttackSelectionDone());
140 void BattleTest::testBattleRound() {
141 Battle battle(fiveLayout, twoLayout);
143 selectHeroAttacks(battle);
145 battle.CalculateAttackOrder();
147 // 2 monsters with agl 20, 4 heroes with agl 10
148 // atk of all participants is 0, so no damage should be dealt
151 battle.CalculateDamage();
152 const Battle::Order *attack = &battle.CurrentAttack();
153 CPPUNIT_ASSERT(!battle.AttacksFinished());
154 CPPUNIT_ASSERT(attack->IsMonster());
155 CPPUNIT_ASSERT(attack->index >= 0 && attack->index < battle.MaxMonsters());
156 battle.ApplyDamage();
159 battle.CalculateDamage();
160 attack = &battle.CurrentAttack();
161 CPPUNIT_ASSERT(!battle.AttacksFinished());
162 CPPUNIT_ASSERT(attack->IsMonster());
163 CPPUNIT_ASSERT(attack->index >= 0 && attack->index < battle.MaxMonsters());
164 battle.ApplyDamage();
167 battle.CalculateDamage();
168 attack = &battle.CurrentAttack();
169 CPPUNIT_ASSERT(!battle.AttacksFinished());
170 CPPUNIT_ASSERT(attack->IsHero());
171 CPPUNIT_ASSERT(attack->index >= 0 && attack->index < battle.NumHeroes());
172 battle.ApplyDamage();
175 battle.CalculateDamage();
176 attack = &battle.CurrentAttack();
177 CPPUNIT_ASSERT(!battle.AttacksFinished());
178 CPPUNIT_ASSERT(attack->IsHero());
179 CPPUNIT_ASSERT(attack->index >= 0 && attack->index < battle.NumHeroes());
180 battle.ApplyDamage();
183 battle.CalculateDamage();
184 attack = &battle.CurrentAttack();
185 CPPUNIT_ASSERT(!battle.AttacksFinished());
186 CPPUNIT_ASSERT(attack->IsHero());
187 CPPUNIT_ASSERT(attack->index >= 0 && attack->index < battle.NumHeroes());
188 battle.ApplyDamage();
191 battle.CalculateDamage();
192 attack = &battle.CurrentAttack();
193 CPPUNIT_ASSERT(!battle.AttacksFinished());
194 CPPUNIT_ASSERT(attack->IsHero());
195 CPPUNIT_ASSERT(attack->index >= 0 && attack->index < battle.NumHeroes());
196 battle.ApplyDamage();
199 CPPUNIT_ASSERT(battle.AttacksFinished());
203 void BattleTest::loadBattle(Battle &battle) {
204 battle.AddHero(hero);
205 battle.AddHero(hero);
206 battle.AddHero(hero);
207 battle.AddHero(hero);
208 battle.AddMonster(monster);
209 battle.AddMonster(monster);
212 void BattleTest::selectHeroAttacks(Battle &battle) {
214 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::SWORD);
215 battle.ActiveHero().GetAttackChoice().Selection().SetSingle();
216 battle.ActiveHero().GetAttackChoice().Selection().Select();
218 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
220 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
222 battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);