]> git.localhorst.tv Git - l2e.git/blob - battle/BattleTest.cpp
new language, new compiler
[l2e.git] / battle / BattleTest.cpp
1 #include "BattleTest.h"
2
3 #include "../../src/battle/Battle.h"
4 #include "../../src/battle/PartyLayout.h"
5
6 #include <stdexcept>
7
8 CPPUNIT_TEST_SUITE_REGISTRATION(test_battle::BattleTest);
9
10 using battle::AttackChoice;
11 using battle::Battle;
12 using battle::PartyLayout;
13 using math::Vector;
14
15
16 namespace test_battle {
17
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);
23
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);
31
32         hero.SetMaxHealth(8);
33         hero.SetHealth(8);
34         hero.GetStats().SetAgility(10);
35
36         monster.SetMaxHealth(8);
37         monster.SetHealth(8);
38         monster.GetStats().SetAgility(20);
39 }
40
41 void BattleTest::tearDown() {
42         monster = battle::Monster();
43         hero = common::Hero();
44         capsule = common::Capsule();
45         delete fiveLayout;
46         delete twoLayout;
47 }
48
49
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);
53
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());
63
64         battle.AddHero(hero);
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());
71
72         battle.AddHero(hero);
73         CPPUNIT_ASSERT(battle.HeroPositionOccupied(1));
74         battle.AddHero(hero);
75         CPPUNIT_ASSERT(battle.HeroPositionOccupied(2));
76         battle.AddHero(hero);
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());
81
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());
89
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());
95 }
96
97 void BattleTest::testHeroAttackSelection() {
98         Battle battle(fiveLayout, twoLayout);
99         loadBattle(battle);
100
101         // turns start out with the cursor before the first hero
102         battle.NextHero();
103         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
104         CPPUNIT_ASSERT(!battle.HasChosenAttackType());
105
106         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::SWORD);
107         battle.ActiveHero().GetAttackChoice().Selection().SetSingle();
108         battle.ActiveHero().GetAttackChoice().Selection().Select();
109         CPPUNIT_ASSERT(battle.HasChosenAttackType());
110
111         battle.NextHero();
112         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
113         CPPUNIT_ASSERT(!battle.HasChosenAttackType());
114
115         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
116         CPPUNIT_ASSERT(battle.HasChosenAttackType());
117         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
118
119         battle.NextHero();
120         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
121         CPPUNIT_ASSERT(!battle.HasChosenAttackType());
122         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
123
124         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
125         CPPUNIT_ASSERT(battle.HasChosenAttackType());
126
127         battle.NextHero();
128         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
129         CPPUNIT_ASSERT(!battle.HasChosenAttackType());
130         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
131
132         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
133         CPPUNIT_ASSERT(battle.HasChosenAttackType());
134         CPPUNIT_ASSERT(!battle.AttackSelectionDone());
135
136         battle.NextHero();
137         CPPUNIT_ASSERT(battle.AttackSelectionDone());
138 }
139
140 void BattleTest::testBattleRound() {
141         Battle battle(fiveLayout, twoLayout);
142         loadBattle(battle);
143         selectHeroAttacks(battle);
144
145         battle.CalculateAttackOrder();
146
147         // 2 monsters with agl 20, 4 heroes with agl 10
148         // atk of all participants is 0, so no damage should be dealt
149
150         battle.NextAttack();
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();
157
158         battle.NextAttack();
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();
165
166         battle.NextAttack();
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();
173
174         battle.NextAttack();
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();
181
182         battle.NextAttack();
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();
189
190         battle.NextAttack();
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();
197
198         battle.NextAttack();
199         CPPUNIT_ASSERT(battle.AttacksFinished());
200 }
201
202
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);
210 }
211
212 void BattleTest::selectHeroAttacks(Battle &battle) {
213         battle.NextHero();
214         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::SWORD);
215         battle.ActiveHero().GetAttackChoice().Selection().SetSingle();
216         battle.ActiveHero().GetAttackChoice().Selection().Select();
217         battle.NextHero();
218         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
219         battle.NextHero();
220         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
221         battle.NextHero();
222         battle.ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
223         battle.NextHero();
224 }
225
226
227 }