-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
440 lines (388 loc) · 12.3 KB
/
player.c
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
#include "player.h"
#include "boolean.h"
#include "listdpUnit.h"
#include "unit.h"
#include "point.h"
#include "queuelist.h"
#include "MatriksMap.h"
#include "stackt.h"
#include <stdio.h>
#include <stdlib.h>
void InitPlayer (Player *P1, Player *P2, int NB, int NK)
//Membuat player pada kondisi awal
{ /* ALGORITMA */
//Player 1 Initial data
PlayNumber(*P1) = 1;
PGold(*P1) = StartingGold;
CurrentUnitPos(*P1) = MakePOINT(NB-1,KolMin+1); // Posisi King pada awal
PIncome(*P1) = 0;
PUpKeep(*P1) = 0;
CreateEmptyList(&UnitList(*P1));
InsVFirst(&UnitList(*P1), CurrentUnitPos(*P1));
CreateEmptyList(&VillageList(*P1));
PlayerCastle(*P1, 1) = MakePOINT(NB-2,KolMin+1);
PlayerCastle(*P1, 2) = MakePOINT(NB-1,KolMin+2);
PlayerCastle(*P1, 3) = MakePOINT(NB,KolMin+1);
PlayerCastle(*P1, 4) = MakePOINT(NB-1,KolMin);
TabTower(*P1) = MakePOINT(NB-1,KolMin+1);
//Player 2 Initial data
PlayNumber(*P2) = 2;
PGold(*P2) = StartingGold;
CurrentUnitPos(*P2) = MakePOINT(BrsMin+1,NK-1); // Posisi King pada awal
PIncome(*P2) = 0;
PUpKeep(*P2) = 0;
CreateEmptyList(&UnitList(*P2));
InsVFirst(&UnitList(*P2), CurrentUnitPos(*P2));
CreateEmptyList(&VillageList(*P2));
PlayerCastle(*P2, 1) = MakePOINT(BrsMin+2,NK-1);
PlayerCastle(*P2, 2) = MakePOINT(BrsMin+1,NK-2);
PlayerCastle(*P2, 3) = MakePOINT(BrsMin,NK-1);
PlayerCastle(*P2, 4) = MakePOINT(BrsMin+1,NK);
TabTower(*P2) = MakePOINT(BrsMin+1,NK-1);
}
void PrintInfoPlayer (Player P, MatriksMap M)
/*
F.S : Cash: 50G | Income: 2G | Upkeep: 1G
++++
*/
{
addressList Po;
int i=1;
printf("Cash : %dG | Income : %dG | Upkeep : %dG\n", PGold(P), PIncome(P), PUpKeep(P));
//List Unit
printf("== List Of Units ==\n");
Po = First(UnitList(P));
Unit UU;
while (Po!=Nil) {
UU = getUnit(Info(Po), M);
printf("%d. ", i);
PrintInfoUnit(UU);
printf("\n");
Po=Next(Po);
i++;
}
//List Village
i=1;
printf("== List Of Villages ==\n");
Po = First(VillageList(P));
Build BB;
while (Po!=Nil) {
BB = BuildIn(Elmt(M,Absis(Info(Po)),Ordinat(Info(Po))));
printf("%d. Tipe : %s (%d,%d) | Owner : %d\n", i, UnitTranslation(TipeB(BB)), Absis(Info(Po)),Ordinat(Info(Po)), OwnerB(BB));
Po=Next(Po);
i++;
}
//Current Unit Pos
printf("== Current Unit ==\n");
}
void WinningPlayer(Player P)
// Menyerahkan kemenangan permainan kepada Player P
{
printf("Congratulations to Player %d who valiantly defeated the forces of Player %d\n", PlayNumber(P), (PlayNumber(P) == 1)? 2 : 1);
printf("\n");
printf("Press any key to exit the game...\n");
getchar();
exit(EXIT_SUCCESS);
}
List EnemyCanBeAttacked(Unit U, MatriksMap M) {
//Mengembalikan List enemy yg bisa di attack.
boolean Top, Bottom, Right, Left;
POINT PTop,PBottom, PRight, PLeft;
List L;
CreateEmptyList(&L);
PTop = NextY(Locate(U));
PRight = NextX(Locate(U));
PLeft = PlusDelta(Locate(U), -1, 0);
PBottom = PlusDelta(Locate(U), 0, -1);
Top = IsUnitIn(PTop,M) && IsEnemy(U,getUnit(PTop,M));
if (Top){
InsVFirst(&L,PTop);
}
Bottom = IsUnitIn(PBottom,M) && IsEnemy(U,getUnit(PBottom,M));
if (Bottom) {
InsVFirst(&L,PBottom);
}
Right = IsUnitIn(PRight,M) && IsEnemy(U,getUnit(PRight,M));
if (Right) {
InsVFirst(&L,PRight);
}
Left = IsUnitIn(PLeft,M) && IsEnemy(U,getUnit(PLeft,M));
if (Left) {
InsVFirst(&L,PLeft);
}
return (L);
}
void PrintListUnit (List L, MatriksMap M, Unit U)
/* I.S. List mungkin kosong */
/* F.S.
== List of Units ==
1. King (2,1) | Health 20
2. Swordsman (3,5) | Health 20
*/
{
printf("== List Of Unit ==\n");
addressList p = First(L);
int i=1;
Unit UU;
while (p!=Nil) {
UU = (UnitIn(Elmt(M, Absis(Info(p)), Ordinat(Info(p)))));
printf("%d. ",i);
PrintInfoUnit(UU);
if (AttackType(U) == AttackType(UnitIn(Elmt(M, Absis(Info(p)), Ordinat(Info(p)))))) {
printf(" (Retaliates)");
}
printf("\n");
p = Next(p);
i++;
}
}
POINT ChooseAttack (List L, int Choice)
{
addressList P = First(L);
int i = 1;
do {
if (i == Choice) {
return Info(P);
}
i++;
P = Next(P);
} while ((P != Nil) && (i <= Choice));
POINT Po = MakePOINT(0,0);
return Po;
}
void Attack (MatriksMap *M, Player *P1, Player *P2, Stack *SUndo)
/* Menyerang */
{
if (CanAttack(getUnit(CurrentUnitPos(*P1), *M))) {
List L;
L = EnemyCanBeAttacked(getUnit(CurrentUnitPos(*P1), *M), *M);
if (NbElmtList(L) > 0 ) {
PrintListUnit(L, *M, getUnit(CurrentUnitPos(*P1), *M));
printf("Select enemy you want to attack: ");
int AttackChoice;
do {
scanf("%d", &AttackChoice);
if (AttackChoice <= 0 || AttackChoice > NbElmtList(L)) {
printf("Your input was wrong. Try again.\n");
}
} while (AttackChoice <= 0 || AttackChoice > NbElmtList(L));
AttackU(M, P1, P2, ChooseAttack(L, AttackChoice));
CanAttack(UnitIn(Elmt(*M, Absis(CurrentUnitPos(*P1)), Ordinat(CurrentUnitPos(*P1))))) = false;
MP(UnitIn(Elmt(*M, Absis(CurrentUnitPos(*P1)), Ordinat(CurrentUnitPos(*P1))))) = 0;
CreateEmpty(SUndo);
} else {
printf("There are no enemy units nearby\n");
}
} else {
printf("This unit has attacked someone and need to have some rest\n");
}
}
void AttackU (MatriksMap *M, Player *P1, Player *P2, POINT PU2)
//Membuat Unit 1 Menyerang Unit 2
{
HP(UnitIn(Elmt(*M, Absis(PU2), Ordinat(PU2)))) -= DamagePoints(getUnit(CurrentUnitPos(*P1), *M));
printf("Enemy's %s is damaged by %d.\n", UnitTranslation(Tipe(getUnit(PU2, *M))), DamagePoints(getUnit(CurrentUnitPos(*P1), *M)));
if (HP(getUnit(PU2, *M)) <= 0) { // Jika HP dari U2 habis
printf("Enemy's %s is dead.\n", UnitTranslation(Tipe(getUnit(PU2, *M))));
PUpKeep(*P2) -= UnitSalary;
if (Tipe(getUnit(PU2, *M)) == 'K') {
WinningPlayer(*P1); // Jika karakter yang diserang dan HPnya habis adalah king maka player yang menyerang memenangkan permainan
}
DelP(&UnitList(*P2), PU2);
UnitIn(Elmt(*M, Absis(PU2), Ordinat(PU2))) = NullUnit();
} else if ( (Tipe(getUnit(PU2, *M)) == 'K') || (AttackType(getUnit(CurrentUnitPos(*P1), *M)) == AttackType(getUnit(PU2, *M)) && (HP(getUnit(PU2, *M)) > 0) ) ) {
printf("Enemy's %s retaliates.\n", UnitTranslation(Tipe(getUnit(PU2, *M))));
HP(UnitIn(Elmt(*M, Absis(CurrentUnitPos(*P1)), Ordinat(CurrentUnitPos(*P1))))) -= DamagePoints(getUnit(PU2, *M));
printf("Your %s is damaged by %d.\n", UnitTranslation(Tipe(getUnit(CurrentUnitPos(*P1), *M))), DamagePoints(getUnit(PU2, *M)));
if (HP(getUnit(CurrentUnitPos(*P1), *M)) <= 0) { // Jika HP dari U1 habis setelah counter attack
printf("Your %s is dead.\n", UnitTranslation(Tipe(getUnit(CurrentUnitPos(*P1), *M))));
PUpKeep(*P1) -= UnitSalary;
if (Tipe(getUnit(CurrentUnitPos(*P1), *M)) == 'K') {
WinningPlayer(*P2); // Jika karakter yang menyerang dan terkena counter attack lalu HPnya habis adalah king maka player yang diserang memenangkan permainan
}
DelP(&UnitList(*P1), CurrentUnitPos(*P1));
UnitIn(Elmt(*M, Absis(CurrentUnitPos(*P1)), Ordinat(CurrentUnitPos(*P1)))) = NullUnit();
}
}
}
/* ********* Prototype ********* */
boolean IsQEmpty (Queue Q)
/* Mengirim true jika Q kosong: lihat definisi di atas */
{ /* ALGORTIMA */
return ((Head(Q) == Nol) && (Tail(Q) == Nol));
}
boolean IsQFull (Queue Q)
/* Mengirim true jika tabel penampung elemen Q sudah penuh */
/* yaitu mengandung elemen sebanyak MaxElmt */
{ /* ALGORTIMA */
return ((Head(Q) == (Tail(Q) + 1)) || ((Head(Q) == 1) && (Tail(Q) == MaxElmt(Q))));
}
int NBElmtQ (Queue Q)
/* Mengirimkan banyaknya elemen queue. Mengirimkan 0 jika Q kosong. */
{ /* ALGORTIMA */
if (IsQEmpty(Q)) {
return 0;
}
else if (Tail(Q) > Head(Q)) {
return (Tail(Q) - Head(Q) + 1);
}
else {
return (MaxElmt(Q) - Head (Q) + 1 + Tail(Q));
}
}
/* *** Kreator *** */
void CreateEmptyQ (Queue * Q, int Max)
/* I.S. sembarang */
/* F.S. Sebuah Q kosong terbentuk dan salah satu kondisi sbb: */
/* Jika alokasi berhasil, Tabel memori dialokasi berukuran Max+1 */
/* atau : jika alokasi gagal, Q kosong dg MaxElmt=0 */
/* Proses : Melakukan alokasi, membuat sebuah Q kosong */
{ /* ALGORTIMA */
(* Q).T = (infotypeQ *) malloc ((Max + 1) * sizeof(infotypeQ));
if ((*Q).T != NULL) {
MaxElmt(*Q) = Max;
Head(*Q) = Nol;
Tail(*Q) = Nol;
}
else {
MaxElmt(*Q) = 0;
}
}
/* *** Destruktor *** */
void DeAlokasiQ(Queue * Q)
/* Proses: Mengembalikan memori Q */
/* I.S. Q pernah dialokasi */
/* F.S. Q menjadi tidak terdefinisi lagi, MaxElmt(Q) diset 0 */
{ /* ALGORTIMA */
MaxElmt(* Q) = 0;
free((* Q).T);
}
/* *** Primitif Add/Delete *** */
void AddQ (Queue * Q, infotypeQ X)
/* Proses: Menambahkan X pada Q dengan aturan FIFO */
/* I.S. Q mungkin kosong, tabel penampung elemen Q TIDAK penuh */
/* F.S. X menjadi TAIL yang baru, TAIL "maju" dengan mekanisme circular buffer */
{ /* ALGORTIMA */
if (IsQEmpty(* Q)) {
Head(* Q) = 1;
Tail(* Q) = 1;
}
else if (Tail(* Q) == MaxElmt(* Q)) {
Tail(* Q) = 1;
}
else {
Tail(* Q) = Tail(* Q) + 1;
}
InfoTail(* Q) = X;
}
void DelQ (Queue * Q, infotypeQ * X)
/* Proses: Menghapus X pada Q dengan aturan FIFO */
/* I.S. Q tidak mungkin kosong */
/* F.S. X = nilai elemen HEAD pd I.S., HEAD "maju" dengan mekanisme circular buffer;
Q mungkin kosong */
{ /* ALGORTIMA */
(* X) = InfoHead(* Q);
if (NBElmtQ(* Q) == 1) {
Head(* Q) = 0;
Tail(* Q) = 0;
}
else if (Head(* Q) == MaxElmt(* Q)) {
Head(* Q) = 1;
}
else {
++Head(* Q);
}
}
void CreateTurn (Queue * Q) {
/* Membuat turn dengan urutan 1 dan 2 */
/* ALGORITMA */
CreateEmptyQ(Q, 2);
AddQ(Q, 1);
AddQ(Q, 2);
}
void NextTurn (MatriksMap *M, Queue * Q, Player * CurrentPlayer, Player * CurrentEnemy, Stack *S) {
/* Mengubah head -> tail dan tail -> head */
infotypeQ X;
/* ALGORITMA */
addressList P = First(UnitList(*CurrentPlayer));
while (P != Nil) {
if (TipeB(BuildIn(Elmt(*M, Absis(Info(P)), Ordinat(Info(P))))) == 'V') {
if (OwnerB(BuildIn(Elmt(*M, Absis(Info(P)), Ordinat(Info(P))))) == PlayNumber(*CurrentEnemy)) {
PIncome(*CurrentEnemy) -= GoldPerVillage;
DelP(&VillageList(*CurrentEnemy), Info(P));
}
if (Search(VillageList(*CurrentPlayer), Info(P)) == Nil) {
OwnerB(BuildIn(Elmt(*M, Absis(Info(P)), Ordinat(Info(P))))) = PlayNumber(*CurrentPlayer);
InsVFirst(&VillageList(*CurrentPlayer), Info(P));
PIncome(*CurrentPlayer) += GoldPerVillage;
}
}
P = Next(P);
}
/* PENERAPAN QUEUE PADA TURN */
DelQ(Q, &X);
AddQ(Q, X);
Player PTemp;
PTemp = *CurrentPlayer;
*CurrentPlayer = *CurrentEnemy;
*CurrentEnemy = PTemp;
// Updating gold...
PGold(*CurrentPlayer) += (PIncome(*CurrentPlayer) - PUpKeep(*CurrentPlayer));
// Healing units in villages and restoring units' Movement Points
P = First(UnitList(*CurrentPlayer));
while (P != Nil) {
// Heal every unit in village
if (Search(VillageList(*CurrentPlayer), Info(P)) != Nil) {
// HP(UnitIn(Elmt(*M, Absis(Info(P)), Ordinat(Info(P))))) = (HP(getUnit(Info(P), *M)) + BaseHealVillage) % ((Tipe(getUnit(Info(P), *M)) == 'W')? BaseMaxHPMage : (Tipe(getUnit(Info(P), *M)) == 'A')? BaseMaxHPArcher : (Tipe(getUnit(Info(P), *M)) == 'S')? BaseMaxHPSwordsman : BaseMaxHPKing);
Heal(Info(P), BaseHealVillage, M);
}
// Heal every unit adjacent to white mage
if (Tipe(getUnit(Info(P), *M)) == 'W') {
HealUnitsAround(UnitIn(Elmt(*M, Absis(Info(P)), Ordinat(Info(P)))), M);
}
MP(UnitIn(Elmt(*M, Absis(Info(P)), Ordinat(Info(P))))) = MaxMP(getUnit(Info(P), *M));
CanAttack(UnitIn(Elmt(*M, Absis(Info(P)), Ordinat(Info(P))))) = true;
P = Next(P);
}
//Delete Stack Undo
CreateEmpty(S);
}
infotypeQ CurrentTurn (Queue Q) {
/* Mengambilkan turn saat ini */
return InfoHead(Q);
}
void Heal (POINT P, int HealP, MatriksMap *M)
/* Menyembuhkan unit yang berada di titik P sebesar HealP */
{
HP(UnitIn(Elmt(*M, Absis(P), Ordinat(P)))) += HealP;
if (HP(getUnit(P, *M)) > MaxHP(getUnit(P, *M))) {
HP(UnitIn(Elmt(*M, Absis(P), Ordinat(P)))) = MaxHP(getUnit(P, *M));
}
}
void HealUnitsAround (Unit U, MatriksMap *M) {
/* Menyembuhkan unit-unit yang berada di sekitar U */
boolean Top, Bottom, Right, Left;
POINT PTop,PBottom, PRight, PLeft;
List L;
CreateEmptyList(&L);
PTop = NextY(Locate(U));
PRight = NextX(Locate(U));
PLeft = PlusDelta(Locate(U), -1, 0);
PBottom = PlusDelta(Locate(U), 0, -1);
Top = IsUnitIn(PTop,*M) && !IsEnemy(U,getUnit(PTop,*M));
if (Top){
Heal(PTop, HealPoints(U), M);
}
Bottom = IsUnitIn(PBottom,*M) && !IsEnemy(U,getUnit(PBottom,*M));
if (Bottom) {
Heal(PBottom, HealPoints(U), M);
}
Right = IsUnitIn(PRight,*M) && !IsEnemy(U,getUnit(PRight,*M));
if (Right) {
Heal(PRight, HealPoints(U), M);
}
Left = IsUnitIn(PLeft,*M) && !IsEnemy(U,getUnit(PLeft,*M));
if (Left) {
Heal(PLeft, HealPoints(U), M);
}
}