-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattle.cpp
331 lines (322 loc) · 10 KB
/
battle.cpp
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
#include "inv.h"
#include "enemies.h"
#include "menus.h"
#include "battle.h"
int madechoice = 0;
int xp = 0;
int playerchoice = 0;
int monsterHealth = 0;
int monsterAC = 0;
int battle(int monsterspeed, int monsterattack, string enemyType)
{
int battlerun = 1;
//Battle Loop
while (battlerun == 1)
{
madechoice = 0;
while (madechoice == 0)
{
clearScreen();
//Main Battle Menu
cout << menuBar;
cout << " " << enemyType << "Battle \n\n";
displayEnemy(enemyType);
// healthMessage();
// magicMessage();
drunkMessage();
cout << "Your attack is " << playersAttack << "\n";
cout << "Your speed is " << playersSpeed << "\n";
cout << "Your AC is " << playersAC << "\n";
cout << " (1) Attack\n";
cout << " (2) Item Menu\n";
cout << " (3) Run Away\n\n";
cout << menuBar;
cin >> playerchoice;
if (playerchoice == 2)
{
itemMenu();
}
else if (playerchoice == 3)
{
return 0;
}
else
{
madechoice = 1;
}
}
//Checks if player's alive after player attack function
bool stillAlive = playerAttack(playersSpeed, playersAttack, enemyType);
if (stillAlive == false)
{
battlerun = 0;
return 0;
}
//Checks if monster's alive after monster attack function
stillAlive = monsterAttack(monsterspeed, monsterattack, enemyType);
if (stillAlive == false)
{
battlerun = 0;
return 1;
}
}
}
//monster Attack function. determines if monster hits and how much damage is done
int monsterAttack(int speed, int attack, string enemyType)
{
//findhit is a booleon determined by whether or not thac0 roll is successfl
bool findhit = thacoRoll(playersAC, speed);
if (findhit)
{
//damageDealt is the output of the damage function which is then modified by the game's difficulty setting
int damageDealt = damage(attack);
if ((difficultySetting == "Medium") && (damageDealt < 100))
{
damageDealt = damageDealt + 10;
}
else if ((difficultySetting == "Medium") && (damageDealt >=100))
{
damageDealt = damageDealt * 1.5;
}
else if ((difficultySetting == "Hard") && (damageDealt < 100))
{
damageDealt = damageDealt + 25;
}
else if ((difficultySetting == "Hard") && (damageDealt >=100))
{
damageDealt = damageDealt * 2.5;
}
clearScreen();
cout << menuBar << "\n\n";
displayEnemy(enemyType);
cout << " You get hit for "<< damageDealt << " damage!\n\n";
playerHealth = playerHealth - damageDealt;
//on successful hit by monster a use is taken away from a random armor piece on the player
srand(static_cast<unsigned int>(time(0)));
int randomNumber = rand();
int armorSelection = (randomNumber % 4);
if (armorSelection == 0)
{
itemUses = player_ehelm[0][2];
player_ehelm[0][2] = itemUses - 1;
if (player_ehelm[0][2] = 0)
{
cout << findItemName(player_ehelm[0][0], player_ehelm[0][1]) << " has been destroyed!\n";
player_ehelm[0][0] = 9;
player_ehelm[0][1] = 9;
player_ehelm[0][2] = 9;
}
}
else if (armorSelection == 1)
{
itemUses = player_eplate[0][2];
player_eplate[0][2] = itemUses - 1;
if (player_eplate[0][2] == 0)
{
cout << findItemName(player_eplate[0][0], player_eplate[0][1]) << " has been destroyed!\n";
player_eplate[0][0] = 9;
player_eplate[0][1] = 9;
player_eplate[0][2] = 9;
}
}
else if (armorSelection == 2)
{
itemUses = player_egauntlets[0][2];
player_egauntlets[0][2] = itemUses - 1;
if (player_egauntlets[0][2] = 0)
{
cout << findItemName(player_egauntlets[0][0], player_egauntlets[0][1]) << " has been destroyed!\n";
player_egauntlets[0][0] = 9;
player_egauntlets[0][1] = 9;
player_egauntlets[0][2] = 9;
}
}
else if (armorSelection == 3)
{
itemUses = player_eboots[0][2];
player_eboots[0][2] = itemUses;
if (player_eboots[0][2] = 0)
{
cout << findItemName(player_eboots[0][0], player_eboots[0][1]) << " has been destroyed!\n";
player_eboots[0][0] = 9;
player_eboots[0][1] = 9;
player_eboots[0][2] = 9;
}
}
cout << " Health is at "<< playerHealth << "!\n\n";
if (playerHealth <= 0)
{
cout << " Oh no! You died!\n";
cout << " Press '1' to return to the Main Menu\n\n\n";
cout << menuBar;
cin >> playerchoice;
return false;
}
cout << " Press '1' to continue!\n\n\n";
cout << menuBar;
cin >> playerchoice;
}
else
{
clearScreen();
cout << menuBar << "\n\n";;
displayEnemy(enemyType);
cout << " He missed!\n\n\n";
cout << " Press '1' to continue!\n\n\n\n";
cout << menuBar;
cin >> playerchoice;
}
}
int playerAttack(int speed, int attack, string enemyType)
{
bool findhit = thacoRoll(monsterAC, speed);
if (findhit)
{
int damageDealt = damage(attack);
clearScreen();
cout << menuBar << "\n\n";
displayEnemyHit(enemyType);
cout << " You hit the monster for " << damageDealt << " damage!\n\n\n";
xp = xp + damageDealt;
itemUses = player_eweapon[0][2];
player_eweapon[0][2] = itemUses - 1;
if (player_eweapon[0][2] == 0)
{
cout << findItemName(player_eweapon[0][0], player_eweapon[0][1]) << " has been destroyed!\n";
player_eweapon[0][0] = 9;
player_eweapon[0][1] = 9;
player_eweapon[0][2] = 9;
playersAttack = 1;
}
monsterHealth = monsterHealth - damageDealt;
if (monsterHealth <= 0)
{
madechoice = 0;
xp = xp + 100;
while (madechoice == 0)
{
clearScreen();
cout << menuBar << "\n\n";
cout << " You defeated him!\n";
cout << " Press '1' to continue!\n";
cout << " Press '2' to loot for items!\n\n\n";
cout << menuBar;
cin >> playerchoice;
if (playerchoice == 2)
{
madechoice = lootEnemy(enemyType);
return false;
}
else if (playerchoice == 1)
{
madechoice = 1;
return false;
}
}
}
cout << " Press '1' to continue!\n\n\n\n";
cout << menuBar;
cin >> playerchoice;
}
else
{
clearScreen();
cout << menuBar << "\n\n";
displayEnemy(enemyType);
cout << " You missed!\n\n\n";
cout << " Press '1' to continue!\n\n\n\n";
cout << menuBar;
cin >> playerchoice;
}
}
//determines damage done on successful attack
int damage(int attack)
{
srand(static_cast<unsigned int>(time(0)));
int randomNumber = rand();
int damage = (randomNumber % attack + 1);
return damage;
}
int findAttack(int itemNumber)
{
int attack = weapon_prop[itemNumber][2];
return attack;
}
//determines if character hits. classic 2e "to hit armor class 0"
bool thacoRoll(int ac, int speed)
{
int thaco = (30 - speed - ac);
srand(static_cast<unsigned int>(time(0)));
int randomNumber = rand();
int thacoRoll = (randomNumber % 20);
if (thacoRoll < thaco)
{
return true;
xp = xp + 1;
}
else
{
return false;
}
}
//creates an inventory for the enemy and then gives and option to the player to loot them.
int lootEnemy(string enemy)
{
makeEnemyInventory(enemy);
int doneLooting = 0;
while (doneLooting == 0)
{
clearScreen();
cout << menuBar;
cout << " Press '0' to exit\n";
cout << " Choose Item to loot:\n\n";
int startNumber = 0;
for (int i = 0; i < enemyInventorySize; i++)
{
string tableName;
for (int j = 0; j < 2; j++)
{
if (j == 0)
{
pos1 = enemy_inv[i][j];
}
else if (j == 1)
{
pos2 = enemy_inv[i][j];
startNumber++;
cout << " (" << startNumber << ") " << findItemName(pos1,pos2) << "\n";
}
}
}
cout << "\n";
cout << menuBar;
cin >> userInput;
if (userInput == 0)
{
doneLooting = 1;
return 1;
}
else if (userInput > 0)
{
itemNumber = userInput - 1;
pos1 = enemy_inv[itemNumber][0];
pos2 = enemy_inv[itemNumber][1];
itemUses = (findItemUses(pos1,pos2) / 2);
addInventory(pos1,pos2, itemUses);
enemyDropItem(itemNumber);
}
}
}
//managing enemy's inventory
void enemyDropItem(int itemNumber)
{
for (int i = itemNumber; i < enemyInventorySize + 1; i++ )
{
int movingI = i + 1;
for (int j = 0; j < 2; j++)
{
enemy_inv[i][j] = enemy_inv[movingI][j];
}
}
enemyInventorySize = enemyInventorySize - 1;
}