-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattle.cpp
More file actions
343 lines (302 loc) · 8.25 KB
/
Battle.cpp
File metadata and controls
343 lines (302 loc) · 8.25 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
#include "Battle.h"
#include <fstream>
#include <Windows.h>
#include <mmsystem.h>
#include <conio.h>
Battle::Battle()//needed initilization
{
CurrentTimeStamp = 0;
EnemyCount = 0;
TotalEnemiesCount = 0;
DeadCount = 0;
for (int i = 0; i < MaxEnemyCount; i++)
{
BEnemiesForDraw[i] = NULL;
}
IsThereDead = false;
IsThereLive = false;
X[0]=241;
X[1]=960;
X[2]=960;
X[3]=241;
}
void Battle::AddEnemy(Enemy* Ptr)//used to enemied to active list only!!!!!!!!!
{
if (EnemyCount < MaxEnemyCount)
BEnemiesForDraw[EnemyCount++] = Ptr;
}
void Battle::DrawEnemies(GUI * pGUI)
{
pGUI->DrawEnemies(BEnemiesForDraw, EnemyCount);
}
Castle * Battle::GetCastle()
{
return &BCastle;
}
void Battle::IncrementTimeStamp()
{
++CurrentTimeStamp;
}
void Battle::LoadFile(GUI *ptr) // load the input file which contains the towers' and enemies' info
{
ptr->DecrementTextHeight(1);
string label = ptr->GetString();
ifstream inputfile;
inputfile.open(label);
bool exit = false;
while (!exit)
{
if (inputfile.is_open())
{
exit = true;
}
else
{
ptr->ResetTextHeight();
ptr->ClearStatusBar();
ptr->PrintMessage("input file :"+label+" wasn't found");
ptr->DecrementTextHeight(1);
label = ptr->GetString();
inputfile.open(label);
}
}
double TowerHealth,TowerNoEnemies,TowerAttack,Constants[3],EnemyHealth;
int EnemyNumber,Type,ArriveTime,FirePower,ReloadTime,EnemySpeed;
char CharRegion;
REGION Region;
if(inputfile.is_open())//first we read the towers and send them the constants
{
inputfile>>TowerHealth>>TowerNoEnemies>>TowerAttack;
for(int i = 0; i < 3;i++)
inputfile>>Constants[i];
BCastle.SetTowersHealth(TowerHealth);
BCastle.SetTowersPower(TowerAttack);
BCastle.SetTowersENum(TowerNoEnemies);
BCastle.SetConstants(Constants);
while(!inputfile.eof())
{
inputfile>>EnemyNumber>>Type>>ArriveTime>>EnemyHealth>>FirePower>>ReloadTime>>EnemySpeed>>CharRegion;
if(EnemyNumber == -1) //to check if i have reched the end of my file
break;
Enemy *ptr;
switch(CharRegion)//switch case on regions to assign each enemy to it's appropiate one
{
case 'A':
Region = A_REG;
break;
case 'B':
Region = B_REG;
break;
case 'C':
Region = C_REG;
break;
case 'D':
Region = D_REG;
break;
}
switch (Type)//switch case on type to allocate the enemy with it's right type
{
case 0:
ptr = new Paver(Region, ArriveTime, EnemyHealth, FirePower, ReloadTime, EnemyNumber,EnemySpeed);
break;
case 1:
ptr = new Fighter(Region, ArriveTime, EnemyHealth, FirePower, ReloadTime, EnemyNumber,EnemySpeed);
break;
case 2:
ptr = new Shielded(Region, ArriveTime, EnemyHealth, FirePower, ReloadTime, EnemyNumber,EnemySpeed);
break;
}
if (true)
{
}
if (ptr->GetArrivalTime() <= CurrentTimeStamp) //if it's arrival time is zero then it's avtive already
{
BEnemiesForDraw[EnemyCount++] = ptr;
BCastle.Disribute(ptr);//distrbutes enemies on towers
}
InactiveQueue.enqueue(ptr);//in both cases it will be added here
TotalEnemiesCount++;
}
}
BCastle.intial_health = TowerHealth;
}
int Battle::GetCurrentTimeStamp()const
{
return CurrentTimeStamp;
}
void Battle::Update()//to update active list
{
int i = 0;
bool increment = true;
int no_ofkilled_at_instant = 0;
while(i<EnemyCount)//while loop to remove dead enemies from active
{
if (BEnemiesForDraw[i]->GetEnemyHealth() <=0)
{
BEnemiesForDraw[i]->SetKillTime(CurrentTimeStamp);
Killed.enqueue(BEnemiesForDraw[i]);
BEnemiesForDraw[i] = BEnemiesForDraw[EnemyCount-1];//if found a dead one delete and bring the last and stay in
// BEnemiesForDraw[EnemyCount - 1];
BEnemiesForDraw[EnemyCount - 1] = NULL; //the same postion (3lshan m3mlsh skip le 2lly 2na lsa gaybo)
EnemyCount--;
DeadCount++;
TotalEnemiesCount--;
increment = false;
IsThereDead = true;
no_ofkilled_at_instant++;
}
else
increment = true;
if (increment)
{
i++;
}
}
if (no_ofkilled_at_instant > 1)
sortkilledlist(Killed,no_ofkilled_at_instant);
Enemy* temp;
bool exit = false;
while (!InactiveQueue.isEmpty()&&!exit)//for loop 3lshan 2n2l 2lly msh active le 2l active 3lshan ytrsmo
{
exit = true;
temp = InactiveQueue.peekFront();
int ArrivalTime = temp->GetArrivalTime();
if (ArrivalTime <= CurrentTimeStamp && temp->GetEnemyHealth()>0)
{
AddEnemy(temp);
IsThereLive = true;
InactiveQueue.dequeue();
BCastle.Disribute(temp);
exit = false;
}
}
BCastle.Update();//b update 2l active list fe kol 2l towers
for(int i=0; i<4; i++)
{
X[i]=BCastle.GetX(i);
}
}
void Battle::DisplaySound()
{
if(IsThereLive)
{
PlaySound(TEXT("EnemyLive.wav"),NULL,SND_ASYNC);
IsThereLive = false;
}
if(IsThereDead)
{
PlaySound(TEXT("Death.wav"),NULL,SND_ASYNC);
IsThereDead = false;
}
if(IsAllEnemiesDead())
PlaySound(TEXT("Victory.wav"),NULL,SND_ASYNC);
if(IsAllTowersDestroyed())
PlaySound(TEXT("Defeat.wav"),NULL,SND_ASYNC);
}
void Battle::OutputFile()
{
double TotalShotdelay=0, Totalkilldelay=0;
int count = 0;
ofstream outfile("OutputFile.txt");
outfile << "KTS " << " S " << " FD " <<" KD "<< " LT " << endl;
while(count<DeadCount)
{
Killed.enqueue(Killed.peekFront());
outfile << Killed.peekFront()->GetKillTime() << " " << Killed.peekFront()->GetID() << " " << Killed.peekFront()->FirstShotDelay() << " " << Killed.peekFront()->KillDelay() << " " << Killed.peekFront()->LifeTime() << endl;
Totalkilldelay += Killed.peekFront()->KillDelay();
TotalShotdelay += Killed.peekFront()->FirstShotDelay();
Killed.dequeue();
count++;
}
outfile << "T1_Total_Damage " << " " << " T2_Total_Damage " << " " << "T3_Total_Damage " << " " << "T4_Total_Damage" << endl;
outfile << " "<<BCastle.GetTowerdamage(0) << " " << BCastle.GetTowerdamage(1) << " " << BCastle.GetTowerdamage(2) << " " << BCastle.GetTowerdamage(3) << endl;
outfile << "R1_Unpaved_Dist " << " " << " R2_Unpaved_Dist " << " " << "R3_Unpaved_Dist " << " " << "R4_Unpaved_Dist" << endl;
outfile << " "<<BCastle.Get_Unpaved(0) << " " << BCastle.Get_Unpaved(1) << " " << BCastle.Get_Unpaved(2) << " " << BCastle.Get_Unpaved(3) << endl;
if (!BCastle.IsAllTowersDead())
{
outfile << " Game is won \n" << endl;
outfile << " 1) Total number of enemy = " << DeadCount << endl;
outfile << " 2) i) Average shot delay = " << TotalShotdelay / DeadCount << endl << " ii) Average kill delay = " << Totalkilldelay / DeadCount << endl;
}
else
{
if(DeadCount == 0)
DeadCount = 1;
outfile << " Game is Lost \n" << endl;
outfile << " 1) Number of killed enemy = " << DeadCount << endl << " 2) Number of alive enemy = " << EnemyCount <<endl;
outfile << " 3) i) Average shot delay = " << TotalShotdelay / DeadCount << endl << " ii) Average kill delay = " << Totalkilldelay / DeadCount << endl;
}
}
void Battle::SimpleSimulator()
{
BCastle.SetTimeStampForTowers(CurrentTimeStamp);//btde 2l time ll towers 3lshan y2dro y update 2l lists bta3thom
BCastle.TowersAttack();
BCastle.AttackEnemies();
Update();//b update 2l BEnemiesToDraw[]
BCastle.Enemies_to_Attack();
IncrementTimeStamp();//btzwed time
for (int i = 0; i < EnemyCount; i++)
{
BEnemiesForDraw[i]->SetDistance(BEnemiesForDraw[i]->GetDistance());
}
DisplaySound();
}
bool Battle::IsAllEnemiesDead()
{
if((EnemyCount == 0) && (DeadCount > EnemyCount))
return true;
return false;
}
bool Battle::IsAllTowersDestroyed()
{
return BCastle.IsAllTowersDead();
}
int Battle::GetX(int i)const
{
return X[i];
}
void Battle::sortkilledlist(Queue & k, int n)
{
Enemy **a = new Enemy*[n];
for (int i = 0; i < DeadCount-n; i++)
{
k.enqueue(k.peekFront());
k.dequeue();
}
for (int i = 0; i < n; i++)
{
a[i] = k.peekFront();
k.dequeue();
}
for (int i = 0; i < n - 1; i++)
{
bool inserted = false;
for (int j = 0; j < n - 1 - i; j++)
{
if (a[j]->FirstShotDelay() > a[j + 1]->FirstShotDelay())
{
swap(a[j], a[j + 1]);
inserted = true;
}
}
if (!inserted)
break;
}
for (int i = 0; i < n; i++)
k.enqueue(a[i]);
delete[] a;
}
Battle::~Battle()
{
for (int i = 0; i < EnemyCount; i++)
{
if(BEnemiesForDraw[i] != NULL)
delete BEnemiesForDraw[i];
BEnemiesForDraw[i] = NULL;
}
while (!Killed.isEmpty())
{
delete Killed.peekFront();
Killed.dequeue();
}
}