-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc3k.cc
1100 lines (946 loc) · 33.3 KB
/
cc3k.cc
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cc3k.h"
#include "level.h"
#include "defaultlevel.h"
#include "player/player.h"
#include "player/drow.h"
#include "player/shade.h"
#include "player/goblin.h"
#include "player/troll.h"
#include "player/vampire.h"
#include "enemy/enemy.h"
#include "enemy/orc.h"
#include "enemy/human.h"
#include "enemy/dragon.h"
#include "enemy/dwarf.h"
#include "enemy/halfling.h"
#include "enemy/elf.h"
#include "enemy/merchant.h"
#include "enemy/pathfinder.h"
#include "potion/restoreHealth.h"
#include "potion/poisonHealth.h"
#include "potion/boostAtk.h"
#include "potion/woundAtk.h"
#include "potion/boostDef.h"
#include "potion/woundDef.h"
#include "treasure/gold.h"
#include "treasure/smallPile.h"
#include "treasure/normalPile.h"
#include "treasure/dragonHoard.h"
#include "treasure/merchantHoard.h"
#include "potion/potion.h"
#include "stairway.h"
#include "floor.h"
#include "cell.h"
#include "utils/color.h"
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <memory>
#include <map>
#include <sstream>
#include <fstream>
using namespace std;
CC3K::CC3K() : levelNum{1}, theFloor{make_shared<Floor>()},
theLevel{make_shared<DefaultLevel>(theFloor, *this)},
theStairway{nullptr}, playerGold{0},
startingRace{Player::RaceTypes::SHADE}, stopEnemies{false}, isHostileMerchants{false}, isCustom{false},
isFog{false}, isDLC{false}, isGameOver{false}, isGameComplete{false}
{
}
CC3K::~CC3K()
{
for (int i = 0; i < observers.size(); i++)
{
detach(observers[i]);
}
observers.clear();
}
void CC3K::parseCustomLevels(std::string filename)
{
isCustom = true;
// Reads a file containing five levels and potions and gold placed already
string fileStr;
std::ifstream infile(filename);
// Read five levels
int rowNum = 0;
// Read lines from a level
vector<vector<char>> singleRawLevel;
for (string line; getline(infile, line);)
{
// Read a char from the line
vector<char> levelLine;
for (int j = 0; j < line.size(); j++)
{
levelLine.push_back(line[j]);
}
singleRawLevel.push_back(levelLine);
if (theFloor->getHeight() - 1 == rowNum)
{
// Make a temporary copy of the vector
vector<vector<char>> tempLine;
for (int z = 0; z < singleRawLevel.size(); z++)
{
tempLine.push_back(singleRawLevel[z]);
}
rawCustomLevels.push_back(tempLine);
singleRawLevel.clear();
rowNum = 0;
}
else
{
rowNum += 1;
}
}
}
void CC3K::loadCustomLevel(int customLevelNum)
{
// Creates a new level
// With the raw level data at customLevelNum
vector<vector<char>> customLevelRaw = rawCustomLevels[customLevelNum];
thePotions.clear();
theGold.clear();
theEnemies.clear();
// Get the map and read it into a floor
theFloor->readMap("map.txt");
// If we're not on the first level, the player may have some permanent potions
// So we should apply them and discard the effects of the temporary potions
thePlayer->applyPermanentPotions();
// Make two passes
// First pass to generate potions + non-dragon-hoard gold
// Second pass to generate dragon hoard
for (int passType = 0; passType < 2; passType++)
{
// Loop through and generate potions and gold
for (int i = 0; i < customLevelRaw.size(); i++)
{
for (int j = 0; j < customLevelRaw[0].size(); j++)
{
// Spawn potions last
if (passType == 0)
{
switch (customLevelRaw[i][j])
{
// Spawn RH
case '0':
{
thePotions.push_back(theLevel->spawnPotionAt(Potion::PotionTypes::RESTOREHEALTH, j, i));
break;
}
// Spawn BA
case '1':
{
thePotions.push_back(theLevel->spawnPotionAt(Potion::PotionTypes::BOOSTATK, j, i));
break;
}
// Spawn BD
case '2':
{
thePotions.push_back(theLevel->spawnPotionAt(Potion::PotionTypes::BOOSTDEF, j, i));
break;
}
// Spawn PH
case '3':
{
thePotions.push_back(theLevel->spawnPotionAt(Potion::PotionTypes::POISONHEALTH, j, i));
break;
}
// Spawn WA
case '4':
{
thePotions.push_back(theLevel->spawnPotionAt(Potion::PotionTypes::WOUNDATK, j, i));
break;
}
// Spawn WD
case '5':
{
thePotions.push_back(theLevel->spawnPotionAt(Potion::PotionTypes::WOUNDDEF, j, i));
break;
}
// Spawn Normal gold pile
case '6':
{
theGold.push_back(theLevel->spawnGoldAt(Gold::GoldTypes::MEDIUM, j, i, nullptr));
break;
}
// Spawn small hoard
case '7':
{
theGold.push_back(theLevel->spawnGoldAt(Gold::GoldTypes::SMALL, j, i, nullptr));
break;
}
// Spawn merchant hoard
case '8':
{
theGold.push_back(theLevel->spawnGoldAt(Gold::GoldTypes::MERCHANT_HOARD, j, i, nullptr));
break;
}
default:
break;
}
}
else
{
switch (customLevelRaw[i][j])
{
// Spawn dragon hoard
case '9':
{
auto dragon = make_shared<Dragon>(nullptr);
theGold.push_back(theLevel->spawnGoldAt(Gold::GoldTypes::DRAGON_HOARD, j, i, dragon));
if (dragon)
{
theEnemies.push_back(dragon);
}
break;
}
default:
{
break;
}
}
}
}
}
}
// Generate enemies
for (int i = 0; i < NUM_ENEMIES; i++)
{
theEnemies.push_back(theLevel->generateEnemy(isHostileMerchants));
}
// Place the player at a random chamber
theLevel->placePlayer(thePlayer);
// Generate stairway
int chamberPlayer = theFloor->chamberAt(thePlayer);
theStairway = theLevel->generateStairway(chamberPlayer);
}
void CC3K::newGame()
{
levelNum = 1;
playerGold = 0;
isGameOver = false;
isGameComplete = false;
// Generate the player
messages.emplace_back("Player character has spawned. ", Color::GREEN);
thePlayer = theLevel->generatePlayer(startingRace);
// Generate the level
newLevel();
}
void CC3K::newLevel()
{
// If the game uses custom levels, load them instead
if (isCustom)
{
loadCustomLevel(levelNum-1);
return;
}
/*
20 enemies are spawned per floor (this number does not include dragons). Every
chamber is equally likely to spawn any particular monster (similarly for floor
tiles). We require that generation happens in the following order: player
character location, stairway location, potions, gold, enemies. This is to allow
us to more easily evaluate that your random generation is correctly implemented.
Note that multiple objects (enemies, gold, and potions) cannot occupy the same
cell on the game board. That is, no two objects can ever occupy the same space.
The one exception to this is the case of gold. Typically, when a player
character walks over gold, it is picked up. The exception to this is if the gold
is associated with a still alive dragon; in this case, the player simply walks
over the gold, without picking it. When the PC attempts to move on to a
stairway, the next level is instead generated and displayed, with the PC
spawning in a random position on the new level. Items and enemies should only
ever spawn on a floor tile and never in a doorway, passage, or the stairs
leading down to the next floor.
*/
thePotions.clear();
theGold.clear();
theEnemies.clear();
// Get the map and read it into a floor
theFloor->readMap("map.txt");
// If we're not on the first level, the player may have some permanent potions
// So we should apply them and discard the effects of the temporary potions
thePlayer->applyPermanentPotions();
// Place the player at a random chamber
theLevel->placePlayer(thePlayer);
// Generate stairway
int chamberPlayer = theFloor->chamberAt(thePlayer);
theStairway = theLevel->generateStairway(chamberPlayer);
// Generate potions
for (int i = 0; i < NUM_POTIONS; i++)
{
thePotions.push_back(theLevel->generatePotion());
}
// Generate gold
for (int i = 0; i < NUM_GOLD; i++)
{
auto newDragon = make_shared<Dragon>(nullptr);
shared_ptr<Gold> newGold = theLevel->generateGold(newDragon);
theGold.push_back(newGold);
if (newDragon->getHoard())
{
theEnemies.push_back(newDragon);
}
}
// Generate enemies
for (int i = 0; i < NUM_ENEMIES; i++)
{
theEnemies.push_back(theLevel->generateEnemy(isHostileMerchants));
}
}
// Helper function to compute new position after moving one unit in the specified direction
pair<pair<int, int>, string> getPosAtDirection(int x, int y, string dir)
{
int dX = 0;
int dY = 0;
string direction;
if (dir == "no")
{
dY = -1;
direction = "North";
}
else if (dir == "so")
{
dY = 1;
direction = "South";
}
else if (dir == "ea")
{
dX = 1;
direction = "East";
}
else if (dir == "we")
{
dX = -1;
direction = "West";
}
else if (dir == "ne")
{
dY = -1;
dX = 1;
direction = "Northeast";
}
else if (dir == "nw")
{
dY = -1;
dX = -1;
direction = "Northwest";
}
else if (dir == "se")
{
dY = 1;
dX = 1;
direction = "Southeast";
}
else if (dir == "sw")
{
dY = 1;
dX = -1;
direction = "Southwest";
}
else
{
cerr << "Invalid direction" << endl;
}
return make_pair(make_pair(x + dX, y + dY), direction);
}
// Returns true if an entity is occupying a cell
bool CC3K::isOccupied(int x, int y) const
{
auto occupies = [&x, &y](shared_ptr<Entity> e)
{ return e->occupies(x, y); };
return ((thePlayer && thePlayer->occupies(x, y)) ||
(theStairway && theStairway->occupies(x, y)) ||
any_of(thePotions.begin(), thePotions.end(), occupies) ||
any_of(theGold.begin(), theGold.end(), occupies) ||
any_of(theEnemies.begin(), theEnemies.end(), occupies));
}
bool CC3K::isOccupiedOrNotChamber(int x, int y) const
{
return isOccupied(x, y) || theFloor->chamberAt(x,y) == -1;
}
void CC3K::checkPlayerDead()
{
if (thePlayer->getHP() <= 0)
{
isGameOver = true;
messages.emplace_back("You died! Press 'r' to play again or any other key to quit.", Color::RED);
}
}
void CC3K::movePlayer(string dir)
{
// Apply any passive ability from the player
thePlayer->abilityPassive();
pair<pair<int, int>, string> newPos = getPosAtDirection(thePlayer->getX(), thePlayer->getY(), dir);
// Compute the new position
int newX = newPos.first.first;
int newY = newPos.first.second;
// Check if the new position is a stairway FIRST
// In this case, go to next level
if (newX == theStairway->getX() && newY == theStairway->getY())
{
// Increase level number
levelNum += 1;
// Check if we are past 5 levels
if (levelNum > 5)
{
isGameComplete = true;
messages.emplace_back("Congratulations, you've beaten the game! Press 'r' to play again or any other key to quit. \n", Color::GREEN);
messages.emplace_back("Your final score is: " + to_string(computeFinalScore()), Color::YELLOW);
return;
}
// Respawn everything
newLevel();
messages.emplace_back("Now entering level " + to_string(levelNum) + "!", Color::BOLDYELLOW);
return;
}
// Next, check if player moved onto gold
// Check if there exists a potion in the specified direction
int foundGold = -1;
for (int i = 0; i < theGold.size(); i++)
{
if (newX == theGold[i]->getX() && newY == theGold[i]->getY())
{
foundGold = i;
break;
}
}
// If the player is walking on gold
if (foundGold != -1)
{
messages.emplace_back("PC moves " + newPos.second + ".", Color::BOLDMAGENTA);
// Check dragon hoard
// The condition will be true whenever gold is not a dragon hoard
// Or when it is a dragon hoard AND the associated dragon is slain
if (!(theGold[foundGold]->getName() == "Dragon Hoard" && !theGold[foundGold]->getPickup()))
{
// If normal gold, we collect it and remove it
// Add the gold to player
playerGold += theGold[foundGold]->getValue();
// Add the message
messages.emplace_back("PC picks up a " +
theGold[foundGold]->getName() + " worth " +
to_string(theGold[foundGold]->getValue()) + " gold.",
Color::GREEN);
// Remove the gold from the map
theGold.erase(theGold.begin() + foundGold);
}
// Move the player over the gold
thePlayer->setX(newX);
thePlayer->setY(newY);
// The enemies now move and attack the player if in range
moveAndAttackEnemies();
return;
}
// Else, not a stairway or gold, we can use isOccupied() properly
// Check if the new position is a tile
// (i.e. don't move into a wall or empty space)
if (isOccupied(newX, newY) ||
(theFloor->cellAt(newX, newY).getChar() != Cell::TILE &&
theFloor->cellAt(newX, newY).getChar() != Cell::DOOR &&
theFloor->cellAt(newX, newY).getChar() != Cell::PASSAGE))
{
messages.emplace_back("You cannot move there.", Color::RED);
// The enemies now move and attack the player if in range
moveAndAttackEnemies();
return;
}
// Else the new position is valid, move player there
thePlayer->setX(newX);
thePlayer->setY(newY);
messages.emplace_back("PC moves " + newPos.second + ".", Color::BOLDMAGENTA);
// The enemies now move and attack the player if in range
moveAndAttackEnemies();
}
// Use a potion in the direction specified (from the player)
void CC3K::usePotion(string dir)
{
// Apply any passive ability from the player
thePlayer->abilityPassive();
// Get the coordinates to check
pair<pair<int, int>, string> checkPotion = getPosAtDirection(thePlayer->getX(), thePlayer->getY(), dir);
pair<int, int> checkPotionPos = checkPotion.first;
// Check if there exists a potion in the specified direction
int found = -1;
for (int i = 0; i < thePotions.size(); i++)
{
if (thePotions[i]->getX() == checkPotionPos.first && thePotions[i]->getY() == checkPotionPos.second)
{
found = i;
break;
}
}
// If we did not find a potion at that location
if (found == -1)
{
messages.emplace_back("There is no potion in that direction.", Color::RED);
return;
}
// Potion is found, apply the potion and erase it from the potion vector
else
{
// Apply the effect to the player
thePlayer->applyPotion(thePotions[found]);
// Output message
// E.g. "You used a Potion of Restore Health"
messages.emplace_back("PC used a " + thePotions[found]->getName() + ". " + thePotions[found]->getDescription(),
Color::BOLDMAGENTA);
// Erase the potion from the vector
thePotions.erase(thePotions.begin() + found);
}
}
// Buy from a merchant in a direction specified (from the player)
void CC3K::useMerchant(string dir)
{
// Apply any passive ability from the player
thePlayer->abilityPassive();
// Get the coordinates to check
pair<pair<int, int>, string> checkMerchant = getPosAtDirection(thePlayer->getX(), thePlayer->getY(), dir);
pair<int, int> checkMerchantPos = checkMerchant.first;
// Check if there exists a potion in the specified direction
int found = -1;
for (int i = 0; i < theEnemies.size(); i++)
{
if (theEnemies[i]->getX() == checkMerchantPos.first && theEnemies[i]->getY() == checkMerchantPos.second && theEnemies[i]->getName() == "Merchant")
{
found = i;
break;
}
}
// If we did not find a Merchant at that location
if (found == -1)
{
messages.emplace_back("There is no Merchant in that direction.", Color::RED);
return;
}
// Merchant is found, buy
else
{
// If we have insufficient funds
if (playerGold < 3)
{
messages.emplace_back("You need at least 3 Gold to purchase from the Merchant.",
Color::RED);
return;
}
if (isHostileMerchants)
{
messages.emplace_back("You've already angered all merchants and they refuse to sell to you!",
Color::RED);
return;
}
else
{
auto boughtPotion = make_shared<RestoreHealth>();
// Apply the effect to the player
thePlayer->applyPotion(boughtPotion);
playerGold -= 3;
// Output message
messages.emplace_back("You bought a potion from the Merchant for 3 Gold.",
Color::BOLDMAGENTA);
messages.emplace_back("PC used a " + boughtPotion->getName() + ". " + boughtPotion->getDescription(),
Color::BOLDMAGENTA);
}
}
}
void CC3K::render()
{
notifyObservers();
// Clear the messages
messages.clear();
}
int CC3K::computeFinalScore()
{
if (startingRace == Player::RaceTypes::SHADE)
{
return playerGold * 1.5;
}
else
{
return playerGold;
}
}
string CC3K::getGameStatus()
{
string ret = "";
ret += "Race: " + thePlayer->getName() + " ";
// Print out player gold
ret += "Gold: " + to_string(playerGold);
ret += " ";
ret += "Floor: " + to_string(levelNum) + "\n";
// Print out HP
ret += "HP: " + to_string(thePlayer->getHP()) + "\n";
// Print out Atk
ret += "Atk: " + to_string(thePlayer->getAtk()) + "\n";
// Print out Def
ret += "Def: " + to_string(thePlayer->getDef()) + "\n";
// DLC Feature: Show exp
if (getDLC())
{
ret += "XP: " + to_string(thePlayer->getExp()) + " Lvl: " + to_string(thePlayer->getLvl()) + "\n";
}
// Print out action (for any actions) that occur
ret += "Action:";
return ret;
}
pair<char, string> CC3K::getState(int x, int y)
{
// Check the player coordinates
if (thePlayer->getX() == x && thePlayer->getY() == y)
{
return make_pair(thePlayer->getSymbol(), thePlayer->getColor());
}
// Check the staircase coordinates
if (theStairway->getX() == x && theStairway->getY() == y)
{
return make_pair(theStairway->getSymbol(), theStairway->getColor());
}
// Check all potions
for (auto potion : thePotions)
{
if (potion->getX() == x && potion->getY() == y)
{
return make_pair(potion->getSymbol(), potion->getColor());
}
}
// Check all gold
for (auto gold : theGold)
{
if (gold->getX() == x && gold->getY() == y)
{
return make_pair(gold->getSymbol(), gold->getColor());
}
}
// Check enemies
for (auto enemy : theEnemies)
{
if (enemy->getX() == x && enemy->getY() == y)
{
return make_pair(enemy->getSymbol(), enemy->getColor());
}
}
// Else, return the floor element
return make_pair(theFloor->cellAt(x, y).getChar(), Color::RESET);
}
void CC3K::toggleStopEnemies()
{
stopEnemies = !stopEnemies;
if (stopEnemies)
{
messages.emplace_back("Enemies are frozen now!", Color::BLUE);
}
else
{
messages.emplace_back("Enemies are unfrozen now!", Color::BLUE);
}
}
// Move all the enemies and attack the player if they are in range
void CC3K::moveAndAttackEnemies()
{
if (stopEnemies)
{
return;
}
// "starting at the leftmost enemy, move all enemies on that row and then move
// to the next row starting with the leftmost. Any particular enemy should only be moved once per player action (e.g. moving
// to a line that has not been processed does not grant an extra move)."
// This makes the implementation more complicated than simply looping through the vector of enemies
vector<shared_ptr<Enemy>> enemiesCopy = theEnemies;
// Create a hash map to mark the xy coordinates of the new enemy locations
std::map<std::pair<int, int>, bool> checkXY;
for (int i = 0; i < theFloor->getHeight(); i++)
{
for (int j = 0; j < theFloor->getWidth(); j++)
{
// Skip non-chamber tiles since enemies cannot be there
if (theFloor->chamberAt(j, i) == -1)
{
continue;
}
// Else search for an enemy
else
{
for (int k = 0; k < theEnemies.size(); k++)
{
if (checkXY.count(make_pair(j, i)) == 0 && i == theEnemies[k]->getY() && j == theEnemies[k]->getX())
{
moveAndAttackEnemy(theEnemies[k]);
checkXY[make_pair(theEnemies[k]->getX(), theEnemies[k]->getY())] = true;
}
}
}
}
}
// Check if the player has died from enemy attacks
checkPlayerDead();
}
void CC3K::moveAndAttackEnemy(shared_ptr<Enemy> enemy)
{
// Pathfinder logic
if (enemy->getName() == "Pathfinder" &&
theFloor->chamberAt(enemy->getX(), enemy->getY()) == theFloor->chamberAt(thePlayer->getX(), thePlayer->getY()))
{
pair<int, int> newPos = Pathfinder::bfs(*this, enemy->getX(), enemy->getY());
int pathfinderDx = newPos.first - enemy->getX();
int pathfinderDy = newPos.second - enemy->getY();
if (!isOccupied(newPos.first, newPos.second))
{
enemy->move(pathfinderDx, pathfinderDy );
}
}
else
{
// Move the enemy
// Generate a random -1, 0, 1
int deltaX = rand() % 3 - 1;
int deltaY = rand() % 3 - 1;
int newX = enemy->getX() + deltaX;
int newY = enemy->getY() + deltaY;
if (!isOccupied(newX, newY) && theFloor->cellAt(newX, newY).getChar() == Cell::TILE)
{
enemy->move(deltaX, deltaY);
}
}
// Attack the player if they are in range
// If merchant and not triggered hostile, skip (don't attack)
if (enemy->getName() == "Merchant" && !isHostileMerchants)
{
return;
}
if (enemy->inRange(thePlayer))
{
// Number of times that the enemy can attack the player
// Default is 1, 2 if enemy is elf
int numAttacks = 1;
if (enemy->getName() == "Elf" && thePlayer->getName() != "Drow")
{
numAttacks = 2;
}
for (int numAttack = 0; numAttack < numAttacks; numAttack++)
{
// Attack the pc if it is in range
int dmg = enemy->attack(thePlayer);
if (dmg == 0)
{
messages.emplace_back(enemy->getName() + " tried to attack PC but missed.", Color::RED);
}
else
{
messages.emplace_back(enemy->getName() + " deals " + to_string(dmg) + " damage to PC.", Color::RED);
}
}
}
}
void CC3K::playerAttack(string cmd)
{
// Apply any passive ability from the player
thePlayer->abilityPassive();
pair<pair<int, int>, string> pos = getPosAtDirection(thePlayer->getX(), thePlayer->getY(), cmd);
// The coordinates where the player is striking the possible enemy
int attackX = pos.first.first;
int attackY = pos.first.second;
// To keep track of if player hit an enemy
int haveHit = false;
for (int i = 0; i < theEnemies.size(); i++)
{
// Check if the player is in range of attacking the enemy
if (theEnemies[i]->inRange(thePlayer) && theEnemies[i]->getX() == attackX && theEnemies[i]->getY() == attackY)
{
int dmg = thePlayer->attack(theEnemies[i]);
haveHit = true;
// Extra logic for vampire: If successful attack, it gains 5hp
if (thePlayer->getName() == "Vampire" && dmg != 0)
{
if (theEnemies[i]->getName() == "Dwarf")
{
messages.emplace_back("PC is allergic to Dwarf and lost 5 HP.", Color::BLUE);
}
else
{
messages.emplace_back("PC gains 5 HP as a vampire from the successful attack.", Color::BLUE);
}
}
// If player misses (this is possible if halfling attacks player)
if (dmg == 0)
{
messages.emplace_back("PC attacked but missed.", Color::MAGENTA);
}
// If the enemy's HP drops below 0, it is dead
else if (theEnemies[i]->getHP() <= 0)
{
// Extra logic for merchant
if (theEnemies[i]->getName() == "Merchant" && !isHostileMerchants)
{
// All merchants are hostile now
isHostileMerchants = true;
messages.emplace_back("PC has slain " + theEnemies[i]->getName() + " (" + to_string(dmg) + " damage).", Color::YELLOW);
messages.emplace_back("You have drawn the ire of all merchants. They will be hostile to you from now on!", Color::MAGENTA);
}
// Extra logic for dragon: allow pickup the gold
else if (theEnemies[i]->getName() == "Dragon")
{
// Get the gold
Dragon &theDragon = dynamic_cast<Dragon &>(*theEnemies[i]);
auto dragonGold = theDragon.getHoard();
// Set the gold to allow pickup
dragonGold->setPickup(true);
messages.emplace_back("PC has slain " + theEnemies[i]->getName() + " (" + to_string(dmg) + " damage).", Color::YELLOW);
messages.emplace_back("The dragon's hoard is now yours to take. ", Color::GREEN);
}
// Otherwise standard enemy was slain
else
{
messages.emplace_back("PC has slain " + theEnemies[i]->getName() + " (" + to_string(dmg) + " damage).", Color::YELLOW);
}
// Humans drop two normal piles of gold
if (theEnemies[i]->getName() == "Human")
{
spawnGoldPileAt(Gold::GoldTypes::MEDIUM, theEnemies[i]->getX(), theEnemies[i]->getY());
spawnGoldPileAt(Gold::GoldTypes::MEDIUM, theEnemies[i]->getX(), theEnemies[i]->getY());
messages.emplace_back("The slain Human dropped two normal piles of gold.", Color::GREEN);
}
if (theEnemies[i]->getName() == "Merchant")
{
spawnGoldPileAt(Gold::GoldTypes::MERCHANT_HOARD, theEnemies[i]->getX(), theEnemies[i]->getY());
messages.emplace_back("The slain Merchant dropped one Merchant Hoard.", Color::GREEN);
}
// Upon their demise, any enemy that is not a dragon, human, or merchant will drop either a small pile or normal pile of gold.
// This gold is immediately added to the player character’s total.
if (theEnemies[i]->getName() != "Dragon" && theEnemies[i]->getName() != "Human" && theEnemies[i]->getName() != "Merchant")
{
int goldCollect = SmallPile::value;
if (rand() % 2)
{
goldCollect = NormalPile::value;
}
playerGold += goldCollect;
messages.emplace_back("PC collected " + to_string(goldCollect) + " gold from the slain " + theEnemies[i]->getName() + ".", Color::YELLOW);
}
// Goblin steals 5 gold from every slain enemy.
if (thePlayer->getName() == "Goblin")
{
playerGold += 5;
messages.emplace_back("PC's Goblin skills collect an extra 5 gold from the slain " + theEnemies[i]->getName() + ".", Color::YELLOW);
}
// DLC Feature: Add to player's exp
if (getDLC())
{
std::string result = thePlayer->addExp(5);
// If we levelled up, the result string is non-empty
if (result != "")
{
messages.emplace_back("You leveled up and permanently increased your max " + result + "!", Color::YELLOW);
}
}
theEnemies[i] = theEnemies.back();
theEnemies.pop_back();
}
// Player attacks enemy but enemy did not die yet
else
{
messages.emplace_back("PC deals " + to_string(dmg) + " damage to " +
theEnemies[i]->getName() + " (" + to_string(theEnemies[i]->getHP()) +
" HP)" + ".",
Color::CYAN);
}
}
}
if (!haveHit)
{
messages.emplace_back("There is nothing to attack there.", Color::BOLDMAGENTA);
}
moveAndAttackEnemies();
}
void CC3K::setStartingRace(int newRace)
{
startingRace = newRace;
}
vector<Message> CC3K::getMessages()
{
return messages;
}
void CC3K::addMessage(string text, string color)
{
messages.emplace_back(text, color);