-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHero.cpp
565 lines (543 loc) · 23.9 KB
/
Hero.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include "Hero.h"
using namespace std;
Hero::Hero() {
string name;
int strength, charisma, speed;
cout << "Choose a name for your hero: ";
while (!(cin >> name)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ivalid name, try again: ";
}
this->setName(name);
while(true) {
int remaining_power = this->max_start_power;
strength = -1;
charisma = -1;
speed = -1;
cout << "You can spend " << remaining_power << " points on powers";
cout << endl;
while (strength < 0 or strength > remaining_power) {
cout << "Starting strength should be (max: " << remaining_power;
cout << "): ";
if (!(cin >> strength)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
remaining_power -= strength;
while (charisma < 0 or charisma > remaining_power) {
cout << "Starting charisma should be (max: " << remaining_power;
cout << "): ";
if (!(cin >> charisma)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
remaining_power -= charisma;
while (speed < 0 or speed > remaining_power) {
cout << "Starting speed should be (max: " << remaining_power;
cout << "): ";
if (!(cin >> speed)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
remaining_power -= speed;
cout << "You set strength " << strength << ", charisma " << charisma;
cout << " and speed " << speed << endl;
cout << "You have " << remaining_power << " remaning points" << endl;
char answer = 'a';
while (answer != 'y' and answer != 'n') {
cout << "Would like to keep this setting? (y/n): ";
if (!(cin >> answer)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
if (answer == 'y') break;
}
cout << endl;
this->setStrength(strength);
this->setCharisma(charisma);
this->setSpeed(speed);
this->inventory = new Inventory(2*strength);
money = 0;
this->equipped.reserve(5);
}
int Hero::getMoney() {
return this->money;
}
void Hero::addMoney(int money) {
this->money += money;
}
int Hero::getStrength() {
int strength = Person::getStrength();
for (unsigned int i = 0; i < this->equipped.size(); ++i)
strength += this->equipped[i]->getStrengthMod();
return strength;
}
int Hero::getCharisma() {
int charisma = Person::getCharisma();
for (unsigned int i = 0; i < this->equipped.size(); ++i)
charisma += this->equipped[i]->getCharismaMod();
return charisma;
}
int Hero::getSpeed() {
int speed = Person::getSpeed();
for (unsigned int i = 0; i < this->equipped.size(); ++i)
speed += this->equipped[i]->getSpeedMod();
return speed;
}
vector<Item*> Hero::getEquipped() {
return this->equipped;
}
void Hero::setEquipped(Item* item) {
for (unsigned int i = 0; i < this->equipped.size(); ++i)
if (item->getName() == this->equipped[i]->getName())
return;
bool set = false;
for (unsigned int i = 0; i < this->equipped.size(); ++i) {
if (item->getType() == "hand") {
if (this->equipped[i]->getType() == "hand")
for (unsigned int j = i + 1; j < this->equipped.size(); ++j)
if (this->equipped[j]->getType() == "hand") {
cout << "Your hands are already full, do you wish to ";
cout << "switch new item:" << endl;
item->printInfo();
cout << "For item" << endl << "1) ";
this->equipped[i]->printInfo();
cout << "2) ";
this->equipped[j]->printInfo();
char answer = 'a';
while (answer != '1' and answer != '2' and
answer != 'n') {
cout << "Your choice is (1/2/n): ";
if (!(cin >> answer)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
if (answer == '1')
this->equipped[i] = item;
else if (answer == '2')
this->equipped[j] = item;
set = true;
break;
}
}
else if (item->getType() == this->equipped[i]->getType()) {
cout << "Your " << item->getType() << " is already being used";
cout << ", do you want to use the new item:" << endl;
item->printInfo();
cout << "Details of item being used: " << endl;
this->equipped[i]->printInfo();
char answer = 'a';
while (answer != 'y' and answer != 'n') {
cout << "Your choice is (y/n): ";
if (!(cin >> answer)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
if (answer == 'y')
this->equipped[i] = item;
set = true;
}
if (set == true) break;
}
if (not set)
this->equipped.push_back(item);
}
void Hero::unsetEquipped(string name) {
for (unsigned int i = 0; i < this->equipped.size(); ++i)
if (this->equipped[i]->getName() == name)
this->equipped.erase(this->equipped.begin()+i);
}
void Hero::printEquipped() {
cout << endl << "==================================================";
cout << endl << "These items you have equipped:" << endl;
cout << "Item name: Strength/Charisma/Speed/Special/Type" << endl;
for (unsigned int i = 0; i < this->equipped.size(); ++i)
this->equipped[i]->printInfo();
cout << "==================================================" << endl;
cout << endl;
}
int Hero::getAction() {
int answer = 0;
cout << "Do you want to:" << endl;
if (GameVariables::gameDifficulty/5 < 3) {
cout << "1) Attack" << endl;
cout << "2) Defend" << endl;
cout << "3) Flee from fight" << endl;
while (answer < 1 or answer > 3) {
cout << "Pick one option (1-3): ";
if (!(cin >> answer)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
}
else {
cout << "1) Attack upper body" << endl;
cout << "2) Attack lower body" << endl;
cout << "3) Defend upper body" << endl;
cout << "4) Defend lower body" << endl;
cout << "5) Flee from fight" << endl;
while (answer < 1 or answer > 5) {
cout << "Pick one option (1-5): ";
if (!(cin >> answer)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
}
return answer;
}
bool Hero::fightWithNPC(NPC* being) {
cout << "You are entering the duel." << endl;
int npc_life = 100, hero_life = 100;
if (being->getWeakness() != NULL and
this->inventory->isItemInInventory(being->getWeakness()->getName())) {
cout << "You have an item that NPC has weakness to." << endl;
npc_life = 0;
}
bool easy = (GameVariables::gameDifficulty/5 < 3) ? true : false;
while (npc_life > 0 and hero_life > 0) {
int hero_action = this->getAction();
if (hero_action == 5 or (easy and hero_action == 3)) {
cout << "You are fleeing a fight and might loose some money." << endl;
if (this->getSpeed() <= being->getSpeed())
this->money -= being->getStrength();
break;
}
int npc_action = (easy) ? rand() % 2 + 1 : rand() % 4 + 1;
if (easy) {
if (this->getSpeed() > being->getSpeed()) {
if (hero_action == 1 and npc_action == 2) {
npc_life -= (int) round((double)this->getStrength() *
((double)this->getSpeed() /
(this->getSpeed() +
being->getSpeed())));
cout << "NPC didn't cover fast enough" << endl;
}
else if (hero_action == 1 and npc_action == 1) {
npc_life -= this->getStrength();
cout << "NPC didn't cover" << endl;
}
else if (npc_action == 1) {
hero_life -= (int) round((double)being->getStrength() *
((double)being->getSpeed() /
(being->getSpeed() +
this->getSpeed())));
cout << "Hero didn't cover fast enough" << endl;
}
else cout << "Both combatants are hiding" << endl;
}
else if (this->getSpeed() < being->getSpeed()) {
if (npc_action == 1 and hero_action == 2) {
hero_life -= (int) round((double)being->getStrength() *
((double)being->getSpeed() /
(being->getSpeed() +
this->getSpeed())));
cout << "Hero didn't cover fast enough" << endl;
}
else if (npc_action == 1 and hero_action == 1) {
hero_life -= being->getStrength();
cout << "Hero didn't cover" << endl;
}
else if (hero_action == 1) {
npc_life -= (int) round((double)this->getStrength() *
((double)this->getSpeed() /
(this->getSpeed() +
being->getSpeed())));
cout << "NPC didn't cover fast enough" << endl;
}
else cout << "Both combatants are hiding" << endl;
}
else {
if (hero_action == npc_action and hero_action == 1)
cout << "Weapons clashed in the air" << endl;
else if (hero_action == npc_action and hero_action == 2)
cout << "Both combatants are hiding" << endl;
else if (hero_action == 1)
cout << "Hero didn't attack fast enough" << endl;
else cout << "NPC didn't attack fast enough" << endl;
}
}
else if (this->getSpeed() > being->getSpeed())
switch (hero_action) {
case 1:
if (npc_action == 3) {
npc_life -= (int) round((double)this->getStrength() *
((double)this->getSpeed() /
(this->getSpeed() +
being->getSpeed())));
cout << "NPC didn't cover fast enough" << endl;
}
else {
npc_life -= this->getStrength();
cout << "NPC didn't cover the right place" << endl;
}
break;
case 2:
if (npc_action != 4) {
npc_life -= this->getStrength();
cout << "NPC didn't cover the right place" << endl;
}
else {
npc_life -= (int) round((double)this->getStrength() *
((double)this->getSpeed() /
(this->getSpeed() +
being->getSpeed())));
cout << "NPC didn't cover fast enough" << endl;
}
break;
case 3:
if (npc_action == 2) {
hero_life -= being->getStrength();
cout << "Hero didn't cover the right place" << endl;
}
else if (npc_action == 1)
cout << "NPC didn't attack fast enough" << endl;
else cout << "Both combatants are hiding" << endl;
break;
case 4:
if (npc_action == 1) {
hero_life -= being->getStrength();
cout << "Hero didn't cover the right place" << endl;
}
else if (npc_action == 2)
cout << "NPC didn't attack fast enough" << endl;
else cout << "Both combatants are hiding" << endl;
break;
}
else if (this->getSpeed() < being->getSpeed())
switch (npc_action) {
case 1:
if (hero_action == 3) {
hero_life -= (int) round((double)being->getStrength() *
((double)being->getSpeed() /
(being->getSpeed() +
this->getSpeed())));
cout << "Hero didn't cover fast enough" << endl;
}
else {
hero_life -= being->getStrength();
cout << "Hero didn't cover the right place" << endl;
}
break;
case 2:
if (hero_action != 4) {
hero_life -= being->getStrength();
cout << "Hero didn't cover the right place" << endl;
}
else {
hero_life -= (int) round((double)being->getStrength() *
((double)being->getSpeed() /
(being->getSpeed() +
this->getSpeed())));
cout << "Hero didn't cover fast enough" << endl;
}
break;
case 3:
if (hero_action == 2) {
npc_life -= this->getStrength();
cout << "NPC didn't cover the right place" << endl;
}
else if (hero_action == 1)
cout << "Hero didn't attack fast enough" << endl;
else cout << "Both combatants are hiding." << endl;
break;
case 4:
if (hero_action == 1) {
npc_life -= this->getStrength();
cout << "NPC didn't cover the right place" << endl;
}
else if (hero_action == 2)
cout << "Hero didn't attack fast enough" << endl;
else cout << "Both combatants are hiding." << endl;
break;
}
else {
if (hero_action == npc_action and hero_action < 3)
cout << "Weapons clashed in the air." << endl;
else if (hero_action > 2 and npc_action > 2)
cout << "Both combatants are hiding." << endl;
else if ((hero_action == 1 and npc_action != 3) or
(hero_action == 2 and npc_action != 4)) {
npc_life -= this->getStrength();
cout << "NPC didn't cover the right place" << endl;
}
else if ((npc_action == 1 and hero_action != 3) or
(npc_action == 2 and hero_action != 4)) {
hero_life -= being->getStrength();
cout << "Hero didn't cover the right place" << endl;
}
else cout << "Damage was deflected" << endl;
}
cout << "Current health is:" << endl;
cout << "Hero: "<< hero_life << endl;
cout << "NPC: " << npc_life << endl;
} // while loop
cout << endl;
if (hero_life > 0 and npc_life <= 0) {
int random_money = rand() % 50 + 10;
cout << "NPC dropped " << random_money << " money." << endl;
this->money += random_money;
Item* dropped_item = being->getSpecialItem();
if (dropped_item) {
cout << "NPC also dropped this item:" << endl;
dropped_item->printInfo();
this->inventory->addItem(dropped_item);
}
}
else if (hero_life <= 0)
this->resetHero();
if (npc_life > 0) return false;
else return true;
}
bool Hero::talkWithNPC(NPC* being) {
cout << "Interaction with NPC is about to begin." << endl;
int hostility = being->getHostility();
bool npc_solved = false, fight = true;
Item* beings_item = being->getSpecialItem();
int beings_item_value;
if (beings_item != NULL)
beings_item_value = beings_item->getStrengthMod() +
beings_item->getCharismaMod() + beings_item->getSpeedMod();
else beings_item_value = 0;
while (hostility < this->getCharisma() * 2) {
if (hostility < 0) {
cout << "NPC is now your friend and you can continue." << endl << endl;
npc_solved = true;
break;
}
cout << endl << this->questions[rand() % this->questions.size()] << endl;
pair<string, int> option1, option2, option3;
option1 = this->answers[rand() % this->answers.size()];
option2 = this->answers[rand() % this->answers.size()];
option3 = this->answers[rand() % this->answers.size()];
cout << "1) " << option1.first << endl;
cout << "2) " << option2.first << endl;
cout << "3) " << option3.first << endl;
cout << "4) Leave this conversation." << endl;
bool trade = false;
if (hostility < this->getCharisma() and beings_item != NULL) {
cout << "5) Buy item that this creature holds." << endl;
cout << "Item: ";
beings_item->printInfo();
cout << "For price: " << beings_item_value * hostility;
trade = true;
}
int answer = 0;
while (answer < 1 or (answer > 4 and !trade) or (answer > 5 and trade)) {
cout << "Choose answer (1-";
if (trade) cout << "5): ";
else cout << "4): ";
if (!(cin >> answer)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
switch (answer)
{
case 1:
hostility += option1.second;
break;
case 2:
hostility += option2.second;
break;
case 3:
hostility += option3.second;
break;
case 4:
fight = false;
break;
case 5:
if (this->money < beings_item_value * hostility)
cout << "Inssuficient funds" << endl;
else {
this->money -= beings_item_value * hostility;
this->inventory->addItem(beings_item);
fight = false;
npc_solved = true;
}
break;
}
if (!fight) break;
}
if (!npc_solved and fight) {
cout << "NPC is too angry at you now, so he's attacking you." << endl;
npc_solved = this->fightWithNPC(being);
}
return npc_solved;
}
void Hero::resetHero() {
cout << "Hero was reset to original state, you can continue ";
cout << "with your adventure." << endl;
delete this->inventory;
this->inventory = new Inventory(2*this->getStrength());
this->money = 0;
this->equipped.erase(this->equipped.begin(), this->equipped.end());
this->equipped.reserve(5);
}
Hero::~Hero() {
delete this->inventory;
}
vector<string> Hero::questions =
{"What are doing in here?", "What do you want from me?",
"Who dares to bother me?", "Look who's there.",
"What a weird creature!", "Who are you, mortal?",
"Are you looking for something?", "How can I help you?",
"?#~đĐ['$Ł*>##&&@đĐ?!?!?!", "Oook?", "Wassssssssuuuuuuuuuup?",
"Would you like a jelly baby?", "What do people mean when "
"they talk about things?",
"What does this mean to the average man on the street?",
"Do you always walk like that?", "Welcome to 101st competition of "
"Upper Class Twit of the Year.",
"My brain hurts.", "Nice day, isn't it?",
"I wonder what other people use for aftershave lotion.",
"One of the cross beams has gone out askew on the treadle.",
"I didn't expect this kind of spanish inquisition.",
"We were wondering if we could borrow your head for a piece "
"of animation.",
"It certainly makes charted acountancy a much more interesting job.",
"What kind of sorcery is this?", "Stop it, it makes me giddy.",
"Are you suggesting coconuts migrate?", "None shall pass.",
"We want...a shrubbery.", "What is your name?", "What is your quest.",
"What is your favorite color.", "What is the air speed velocity of "
"an unladen swallow?"}; // 32
vector<pair<string, int>> Hero::answers =
{{"Oook!", -4}, {"My brain hurts too!", -2}, {"Hello sweetie.", 3},
{"Nobody expects the spanish inquisition!", 5},
{"Yes it is a very nice day", -9}, {"Can point me to the end?", 3},
{"What?!", 1}, {"Yes, I would like a jelly baby.", -7},
{"Do you have something to eat?", 3}, {"You have something I want.", 4},
{"Your Highness is very kind.", -5}, {"Burn with me!", 10},
{"Would you like to see my... Nudge, nudge, wink, wink.", 2},
{"Indeed.", -1}, {"Very funny.", -2}, {"Christmas crackers!", -3},
{"I'am looking for princess Peach.", 1}, {"Did you see that dragon?", -2},
{"I'm sorry, I'm so so sorry.", -3}, {"Are you an Upper Class Twit?", 4},
{"Are you my mummy?", -3}, {"Can we be friends on facebook?", 5},
{"Would you like a cup of tea?", -1}, {"This is a late parrot.", 1},
{"And now for something completely different.", -4},
{"A larch.", -1}, {"I'am Arthur, king of the Brittons", 1},
{"I seek the finest and bravest soldiers to join me in my cause.", -4},
{"'Tis but a scratch", 1}, {"All right, we call it a draw.", -2},
{"Would you like a banana? Bananas are good.", -3},
{"On the second thought, let's not go to Camelot, it's a silly place", -4},
{"Message for you sir.", -1}, {"Hahaaa!!", 1}, {"Oh bloody hell.", -1},
{"Bless this holy handgranade.", -2}, {"Seek the holy grail.", -1},
{"What do you mean, African or European swallow?", -2},
{"It is elementary, my dear Watson.", -2}, {"Bite my shiny metal ass", 2},
{"Quite correct.", -1}, {"Resistance is futile.", 2},
{"God save the Queen!", -2}, {"Carrot juice, carrot juice, carrot juice.", -2},
{"Spam, spam, spam!", -1}, {"Me don't cares", 2}, {"Exterminate!", 3},
{"Delete!", 2}, {"It's bigger on the inside.", -1},
{"You are lowering the IQ of the whole street.", 3},
{"Would you like to see my server room?", 2}, {"You shall not pass.", 1},
{"I will lend you my hamster.", -2}, {"Let's have some wine.", -3},
{"Let's have some whine.", 3}, {"This is not Jim Beam.", 1},
{"He's dead, Jim.", -1}, {"I cannae change the laws of physics.", -2},
{"Gosh, I hate killing people.", 2}}; // 59