-
Notifications
You must be signed in to change notification settings - Fork 1
/
arena.c
218 lines (169 loc) · 5.6 KB
/
arena.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
#include "include/items.h"
#include "include/game.h"
#include "include/combat.h"
#include "include/enemy.h"
#include "include/arena.h"
#include "raylib.h"
int ArenaConstructor(Arena *arena, ArenaPlayer *player, ArenaEnemy *enemy) {
arena->round = 0;
arena->roundMax = 10;
arena->player = player;
arena->enemy = enemy;
arena->playerSelectedOption = NoneChoice;
arena->enemySelectedOption = NoneChoice;
arena->playerItemCooldown = 0;
arena->enemyItemCooldown = 0;
return 0;
}
int PlayerConstructor(ArenaPlayer *player) {
player->life = PLAYER_MAX_LIFE;
player->maxLife = PLAYER_MAX_LIFE;
for (int i = 0; i < MAX_INV_SIZE; i++) {
player->inventory[i] = NoneItem;
}
CombatWheelConstructor(&player->MyCombatWheel);
for (int i = 0; i < 9; i++) {
(*player).MyCombatWheel.options[i].isActivated = 1;
}
return 0;
}
int nextRound(Arena *arena) {
arena->round++;
arena->playerSelectedOption = NoneChoice;
arena->enemySelectedOption = NoneChoice;
arena->UsedPlayerItemIdx = NoneItem;
arena->UsedEnemyItemIdx = NoneItem;
if (arena->playerItemCooldown > 0) {
arena->playerItemCooldown--;
}
if (arena->enemyItemCooldown > 0) {
arena->enemyItemCooldown--;
}
return 0;
}
int UpdateArena(Arena *arena, RoundResult *result) {
// bot choose action
selectEnemyAction(arena, EnemyChooseAction(arena->enemy));
// item use
if (arena->UsedPlayerItemIdx != NoneItem && arena->playerItemCooldown <= 0)
doPlayerItemAction(arena);
if (arena->UsedEnemyItemIdx != NoneItem && arena->enemyItemCooldown <= 0)
doEnemyItemAction(arena);
// adjust life to max life if over
if ((arena->player)->life >= (arena->player)->maxLife)
(arena->player)->life = (arena->player)->maxLife;
if ((arena->enemy)->life >= (arena->enemy)->maxLife)
(arena->enemy)->life = (arena->enemy)->maxLife;
// check if someone died
if ((arena->player)->life <= 0) {
result->gameResult = 2;
return 0;
//GameOver();
}
if ((arena->enemy)->life <= 0) {
result->gameResult = 1;
return 0;
//NextMap();
}
if (arena->round >= arena->roundMax) {
arena->round = 0;
//check who has more life
//NextMap();
}
if (arena->playerSelectedOption != NoneChoice &&
arena->enemySelectedOption != NoneChoice)
{
printf("enemy choice: %d\n", arena->enemySelectedOption);
doCombat(arena, result);
nextRound(arena);
}
return 0;
}
////////////////////////////////////////////////////////////
int selectPlayerAction(Arena *arena, Choice playerChoice) {
if (arena->playerSelectedOption == NoneChoice) {
arena->playerSelectedOption = playerChoice;
return 0;
} else {
return 1;
}
}
int selectEnemyAction(Arena *arena, Choice enemyChoice) {
if (arena->enemySelectedOption) {
arena->enemySelectedOption = enemyChoice;
return 0;
} else {
return 1;
}
}
int selectPlayerItem(Arena *arena, int ItemInvIdx) {
if (arena->playerSelectedOption == NoneChoice) {
arena->UsedPlayerItemIdx = ItemInvIdx;
return 0;
} else {
return 1;
}
}
int selectEnemyItem(Arena *arena, int ItemInvIdx) {
if (arena->enemySelectedOption == NoneChoice) {
arena->UsedEnemyItemIdx = ItemInvIdx;
return 0;
} else {
return 1;
}
}
////////////////////////////////////////////////////////////
int doPlayerItemAction(Arena *arena) {
int itemInvIdx = arena->UsedPlayerItemIdx;
ItemID ChoiceItemID = (arena->player)->inventory[itemInvIdx];
arena->playerItemCooldown = getItem(ChoiceItemID)->cooldown;
if (getItem(ChoiceItemID)->Effect(arena->player, arena->enemy)){
//remove item from inventory and organize it
(arena->player)->inventory[itemInvIdx] = NoneItem;
for (int i = itemInvIdx; (arena->player)->inventory[i + 1] != NoneItem && i < MAX_INV_SIZE; i++) {
(arena->player)->inventory[i] = (arena->player)->inventory[i+1];
}
}
return 0;
}
int doEnemyItemAction(Arena *arena) {
int itemInvIdx = arena->UsedEnemyItemIdx;
ItemID ChoiceItemID = (arena->enemy)->inventory[itemInvIdx];
arena->enemyItemCooldown = getItem(ChoiceItemID)->cooldown;
if (getItem(ChoiceItemID)->Effect(arena->player, arena->enemy)){
//remove item from inventory and organize it
(arena->enemy)->inventory[itemInvIdx] = NoneItem;
for (int i = itemInvIdx; (arena->enemy)->inventory[i + 1] != NoneItem && i < MAX_INV_SIZE; i++) {
(arena->enemy)->inventory[i] = (arena->enemy)->inventory[i+1];
}
}
return 0;
}
int doCombat(Arena *arena, RoundResult *result) {
Choice playerChoice = arena->playerSelectedOption;
Choice enemyChoice = arena->enemySelectedOption;
int winner = 0; // 0 -> draw
// 1 -> player win
// 2 -> enemy win
for (int i = 1; i <= 4; i++) {
if ((playerChoice + i) % 8 == enemyChoice) {
winner = 1;
break;
} else if ((enemyChoice + i) % 8 == playerChoice) {
winner = 2;
break;
}
}
if (winner == 1) {
printf("player win\n");
(arena->enemy)->life -= MIN_DMG;
}
else if (winner == 2) {
printf("enemy win\n");
(arena->player)->life -= MIN_DMG;
}
result->roundResult = winner;
result->playerChoice = playerChoice;
result->enemyChoice = enemyChoice;
return 0;
}