-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoblin.cpp
More file actions
1450 lines (1297 loc) · 49.3 KB
/
Goblin.cpp
File metadata and controls
1450 lines (1297 loc) · 49.3 KB
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
// Goblin.cpp : Defines the entry point for the application.
//
#include <iostream>
#include "Goblin.h"
#include <fstream>
using namespace std;
void saveGame();
void loadGame();
void processSaveLoad();
void saveLoadScreen();
void encryptSaveFile();
void decryptSaveFile();
char getch();
void pauseGame();
void printDisplay();
void displayTitle();
string generateName();
void processTerrain();
void processShop();
void processSkillShop();
void processMerchant();
void processInventory();
void processBattle(string TYPE, string enemyType);
void generatePlayer(string object, string TILE);
void processInput();
void processMovement(int dx, int dy);
void generateWorldMap();
void generateDungeon();
int getRandom(int low, int high);
void printBoard();
void initBoard();
void cellularAutomataMaze(int iterations);
void cellularAutomata(int iterations);
void cellularAuto(int iterations);
int countNeighbors(int row, int column, string TYPE);
void randGen();
void cellularAutomataMiniMaze(int iterations);
void villageSpawn(int iterations);
void cellularAutomataBorder(int iterations);
void randomWalk(int iterations, string TYPE);
void runRuleTile(string TYPE, string vertTYPE, string horTYPE);
void destroyIslands(string TYPE);
void crossRiver(int iterations, string TYPE, string changeTYPE, string clearTYPE);
void changeTiles(string TYPE, string changeTYPE);
//gloabal colors
const string RED = "\033[31m";
const string GREEN = "\033[32m";
const string YELLOW = "\033[33m";
const string BLUE = "\033[34m";
const string RESET = "\033[0m";
const string BLACK = "\033[30m";
const string bgWHITE = "\033[47m";
const string bgRED = "\033[41m";
const string bgGREEN = "\033[42m";
const string bgYELLOW = "\033[43m";
const string bgBLUE = "\033[44m";
const string MAGENTA = "\033[35m";
const string bgMAGENTA = "\033[45m";
const string CYAN = "\033[36m";
const string bgCYAN = "\033[46m";
const string BLINK = "\033[5m";
const string REVERSED = "\033[7m";
string GROUND = "/\\" + RESET;
string EXIT = "()" + RESET;
string BLOCKER = "##" + RESET;
string gGROUND = YELLOW + "^ " + RESET;
string WATER = YELLOW + " " + RESET;
string GROUNDrand = GREEN + ". " + RESET;
string gGROUNDrand = MAGENTA + ", " + RESET;
string WATERrand = " " + RESET;
string CASTLE = RED + "[]" + RESET;
string CASTLEwall = YELLOW + "==" + RESET;
string CASTLEwallUP = YELLOW + "||" + RESET;
string VILLA = " ";
string SHORE = GREEN + ";:" + RESET;
string WALKER = BLUE + "~ " + RESET;
string crossWATER = bgCYAN + "~ " + RESET;
string tempTileA = " ";
string tempTileB = " ";
string tempTileC = " ";
string currentTerrain = " ";
string terrainMessage = " ";
string terrainMessageTemp = " ";
//global vars
int SIZE = 35;
int gridWidth = SIZE;
int gridHeight = SIZE;
int row = 0;
int column = 0;
vector<vector<string>> board(gridWidth, vector<string>(gridHeight, ""));
vector<vector<string>> boardTemp(gridWidth, vector<string>(gridHeight, ""));
bool bLoadGame = false;
//Player variables
string PLAYER = BLINK + "db" + RESET;
int playerX = 0;
int playerY = 0;
int playerPOS[2] = { playerX,playerY };
int tempPlayerX = 0;
int tempPlayerY = 0;
int tempPlayerPOS[2] = { tempPlayerX,tempPlayerY };
int pHealth = 30;
int pAttack = 1;
int pDefense = 5;
int pExp = 0;
int pLevel = 0;
int money = 0;
int steps = 0;
int goblinsKilled = 0;
//inventory variables
string itemNames[3] = { "skulls","bones","meat" };
int itemAmount[3] = { 0, 0, 0 };
int itemBasePrice[3] = { 4, 2, 1 };
string playerAttackMessage[5] = { "You attack ","You swing at ","You strike in the general area of ","You throw a rock at ","You trip into " };
//MAIN
int main()
{
char input;
bool gameOver = false;
displayTitle();
generateWorldMap();
//game loop
while (gameOver == false)
{
printBoard();
printDisplay();
processInput();
processTerrain();
if (pHealth < 1)
{
cout << "you have died\n";
gameOver = true;
}
}
cout << "Game Over!" << endl;
return 0;
}
//function to process save/load screen
void processSaveLoad(){
// display the save/load screen
cout << "Save/Load Screen" << endl;
cout << "1. Save Game" << endl;
cout << "2. Load Game" << endl;
cout << "3. New Game" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
saveGame();
break;
case 2:
loadGame();
break;
case 3:
break;
default:
cout << "Invalid choice" << endl;
break;
}
}
//function to save the game to a file
void saveGame() {
// open the file for writing
ofstream file("gamesave");
if (!file.is_open()) {
cout << "Error: could not open save game file" << endl;
return;
}
// write the game state to the file
file << playerX << " " << playerY << " " << pHealth << " " << pAttack << " " << pDefense << " " << pExp << " " << pLevel << " " << money << " " << steps << " " << goblinsKilled;
// write the inventory to the file
for (int i = 0; i < 3; i++) {
file << " " << itemAmount[i];
}
// close the file
file.close();
cout << "Game saved successfully!" << endl;
encryptSaveFile();
pauseGame();
}
// function to load the game from a file
void loadGame(){
decryptSaveFile();
// open the file for reading
ifstream file("gamesave");
if (!file.is_open()) {
cout << "Error: could not open save game file" << endl;
return;
}
// read the game state from the file
file >> playerX >> playerY >> pHealth >> pAttack >> pDefense >> pExp >> pLevel >> money >> steps >> goblinsKilled;
// read the inventory from the file
for (int i = 0; i < 3; i++) {
file >> itemAmount[i];
}
// close the file
file.close();
// update the game state
//playerPOS[0] = playerX;
//playerPOS[1] = playerY;
// update the inventory
for (int i = 0; i < 3; i++) {
itemAmount[i] = itemAmount[i];
itemBasePrice[i] = itemBasePrice[i];
}
encryptSaveFile();
}
//function to bring up the save/load screen
void saveLoadScreen() {
system("clear");
printDisplay();
cout << "\n\n";
// display the save/load screen
cout << "Save/Load Screen" << endl;
cout << "1. Save Game" << endl;
cout << "2. Load Game" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
saveGame();
break;
case 2:
loadGame();
break;
default:
cout << "Invalid choice" << endl;
break;
}
}
/**
* Reads a single character from the standard input without echoing it to the console.
* This function is a Linux-specific implementation of the Windows `getch()` function.
* It temporarily disables canonical mode and echo on the terminal, reads a single character,
* and then restores the original terminal settings.
*
* @return The character read from the standard input.
*/
char getch() {
char buf = 0;
struct termios old = { 0 };
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
return (buf);
}
// function that works like system("pause") but for linux
void pauseGame() {
cout << "Press any key to continue...\n";
getch();
}
// function that encrypts save file using XOR encryption
void encryptSaveFile() {
// open the file for reading
ifstream file("gamesave");
if (!file.is_open()) {
cout << "Error: could not open save game file" << endl;
}
// read the game state from the file
string gameState;
getline(file, gameState);
// close the file
file.close();
// encrypt the game state using XOR encryption
for (int i = 0; i < gameState.length(); i++) {
gameState[i] = gameState[i] ^ 0x55;
}
// open the file for writing
ofstream file2("gamesave");
if (!file2.is_open()) {
cout << "Error: could not open save game file" << endl;
}
// write the encrypted game
file2 << gameState;
// close the file
file2.close();
cout << "Game saved successfully!" << endl;
}
// function that decrypts save file using XOR encryption
void decryptSaveFile() {
// open the file for reading
ifstream file("gamesave");
if (!file.is_open()) {
cout << "Error: could not open save game file" << endl;
}
// read the game state from the file
string gameState;
getline(file, gameState);
// close the file
file.close();
// decrypt the game
for (int i = 0; i < gameState.length(); i++) {
gameState[i] = gameState[i] ^ 0x55;
}
// open the file for writing
ofstream file2("gamesave");
if (!file2.is_open()) {
cout << "Error: could not open save game file" << endl;
}
// write the decrypted
file2 << gameState;
// close the file
file2.close();
cout << "Game loaded successfully!" << endl;
}
void displayTitle()
{
cout << RED + " " + " ______ __ ___ \n";
cout << RED + " " + " / ____/___ / /_ / (_)___ \n";
cout << RED + " " + " / / __/ __ \\/ __ \\/ / / __ \\ \n";
cout << RED + " " + "/ /_/ / /_/ / /_/ / / / / / / \n";
cout << RED + " " + "\\____/\\____/_.___/_/_/_/ /_/ \n";
cout << RESET;
processSaveLoad();
}
/// Prints the game display information, including the player's health, level, gold, strength, steps taken, and goblins killed. Also prints any terrain-specific messages.
void printDisplay()
{
for (int x = 0; x < SIZE * 2; x++)
cout << bgRED + BLACK + "=" + RESET;
cout << endl;
cout << "HP: " << pHealth << " Level: " << pLevel << " Gold: " << money << " Strength: " << pAttack << " Steps: " << steps << " Goblins killed: " << goblinsKilled;
cout << "\n" << terrainMessageTemp << "\n";
cout << terrainMessage << "\n";
terrainMessageTemp = terrainMessage;
terrainMessage = " ";
}
/// Generates a random goblin name by combining first and last name components.
/// The name is generated using arrays of first and last name prefixes and suffixes.
/// The function returns the generated goblin name as a string.
string generateName()
{
//string eTypeName[5] = { "","","","","" };
string fname1[5] = { "Sn","Sk","Kr","Gr","B" };
string fname2[5] = { "um","ar","ay","ee","oo" };
string fname3[5] = { "po","py","bs","ble","gle" };
string lname1[5] = { "Rub","Bum","Tum","Hum","Grum" };
string lname2[5] = { "thum","bo","rum","tum","bum" };
string lname3[5] = { "o","bles","gles","po","py" };
string fNAME = fname1[getRandom(1, 5) - 1] + fname2[getRandom(1, 5) - 1] + fname3[getRandom(1, 5) - 1];
string lNAME = lname1[getRandom(1, 5) - 1] + lname2[getRandom(1, 5) - 1] + lname3[getRandom(1, 5) - 1];
string goblinNAME = (fNAME + " " + lNAME);
return goblinNAME;
}
/// Processes the terrain and triggers various events based on the current terrain type.
/// This function checks the current terrain type and performs the following actions:
/// - If the terrain is gGROUNDrand, there is a 25% chance of triggering a battle with a randomly generated goblin.
/// - If the terrain is gGROUND, there is a 5% chance of gaining 1-2 gold plus the player's level.
/// - If the terrain is CASTLE, there is a 5% chance of opening the skill shop and a 25% chance of opening the regular shop.
/// - If the terrain is VILLA, there is a 25% chance of triggering a merchant event.
/// - If the terrain is GROUND, there is a 25% chance of leveling up the player, generating a new dungeon, and blocking the current tile.
/// - If the terrain is EXIT, the player is returned to the previous location.
/// The function also sets the appropriate terrain message based on the current terrain type.
void processTerrain()
{
//string name = generateName();
if (currentTerrain == gGROUNDrand && getRandom(1, 100) > 75)
processBattle(gGROUNDrand, generateName());
if (currentTerrain == gGROUND && getRandom(1, 100) > 95)
money = money + getRandom(0, 1) + pLevel;
if (currentTerrain == CASTLE && getRandom(1, 100) > 95)
processSkillShop();
if (currentTerrain == CASTLE && getRandom(1, 100) > 75)
processShop();
if (currentTerrain == VILLA && getRandom(1, 100) > 75)
processMerchant();
if (currentTerrain == GROUND && getRandom(1, 100) > 75)
{
board[playerPOS[0]][playerPOS[1]] = BLOCKER;
board[playerPOS[0]][playerPOS[1]] = boardTemp[playerPOS[0]][playerPOS[1]];
boardTemp = board;
tempPlayerX = playerPOS[0];
tempPlayerY = playerPOS[1];
pLevel = pLevel + 1;
generateDungeon();
}
if (currentTerrain == EXIT)
{
board = boardTemp;
playerX = tempPlayerX;
playerY = tempPlayerY;
printBoard();
}
//terrainMessages
if (currentTerrain == gGROUNDrand)
terrainMessage = RED + "this area is infested with goblins" + RESET;
if (currentTerrain == VILLA)
terrainMessage = GREEN + "there are merchants in the area" + RESET;
if (currentTerrain == GROUND)
terrainMessage = RED + "the ground is unstable" + RESET;
if (currentTerrain == CASTLE)
terrainMessage = GREEN + "this area is has services" + RESET;
if (currentTerrain == gGROUND)
terrainMessage = YELLOW + "gold is in the area" + RESET;
}
/**
* Processes the player's interaction with the shop, allowing them to purchase health for a random amount of gold.
* The shop will remain open until the player chooses to exit or successfully purchases health.
*/
void processShop()
{
bool shopOpen = true;
char input = ' ';
system("clear");
printDisplay();
cout << "\n\nWelcome to the Inn\n";
while (shopOpen == true)
{
int randHP = getRandom(10, 25) * (pLevel + 1);
int randGold = getRandom(10, 25) * (pLevel + 1);
cout << "\n\nPurchase " << randHP << " HP for " << randGold << " gold ? (A) for YES/ (D) for NO \n\n";
input = getch();
input = toupper(input);
if (input == 'A' && money >= randGold)
{
money = money - randGold;
pHealth = pHealth + randHP;
shopOpen = false;
break;
}
else
{
shopOpen = false;
}
}
}
/**
* Processes the player's interaction with a skill shop, allowing them to purchase a strength upgrade for a random amount of gold.
* The shop will remain open until the player chooses to exit or successfully purchases the upgrade.
*/
void processSkillShop()
{
bool shopOpen = true;
char input = ' ';
system("clear");
printDisplay();
cout << "\n\nA Mentor approaches\n";
while (shopOpen == true)
{
int randHP = getRandom(10, 25);
int randGold = (getRandom(10, 25) * (pLevel + 1));
cout << "\n\nPurchase strength upgrade for " << randGold << " gold ? (A) for YES/ (D) for NO \n\n";
input = getch();
input = toupper(input);
if (input == 'A' && money >= randGold)
{
money = money - randGold;
pAttack = pAttack + 1;
shopOpen = false;
break;
}
else
{
shopOpen = false;
}
}
}
/**
* Processes the player's interaction with a merchant, allowing them to purchase or sell items for a random amount of gold.
* The shop will remain open until the player chooses to exit or successfully completes a transaction.
*/
void processMerchant()
{
bool shopOpen = true;
char input = ' ';
system("clear");
printDisplay();
cout << "\n\nA merchant approaches\n";
while (shopOpen == true)
{
int randAmount = getRandom(10, 25);
int randItem = getRandom(1, 3);
int randGold = getRandom(10, 25);
int itemPriceLow = (itemBasePrice[randItem - 1] * randAmount) / 2;
int itemPriceHigh = (itemBasePrice[randItem - 1] * randAmount) * 2;
int itemPrice = getRandom(itemPriceLow, itemPriceHigh);
cout << "(A)Purchase / (S)Sell / (D)Exit \n\n";
input = getch();
input = toupper(input);
switch (input)
{
case 'A':
cout << "Purchase " << randAmount << " " << itemNames[randItem - 1] << " for " << itemPrice << " gold ? (A) for YES / (D) for NO \n\n";
input = getch();
input = toupper(input);
if (input == 'A' && money > itemPrice)
{
money = money - itemPrice;
itemAmount[randItem - 1] = itemAmount[randItem - 1] + randAmount;
shopOpen = false;
break;
}
else
{
shopOpen = false;
}
break;
case 'S':
if (itemAmount[randItem - 1] > 0)
{
cout << "Sell " << itemAmount[randItem - 1] << " " << itemNames[randItem - 1] << " for " << itemPrice << " gold ? (A) for YES / (D) for NO \n\n";
input = getch();
input = toupper(input);
if (input == 'A')
{
money = money + itemPrice;
itemAmount[randItem - 1] = itemAmount[randItem - 1] - itemAmount[randItem - 1];
shopOpen = false;
break;
}
else
{
shopOpen = false;
}
}
else
{
cout << "They aren't buying.\n";
}
break;
case 'D':
shopOpen = false;
break;
}
}
}
/**
* Displays the player's inventory, showing the name and amount of each item.
* This function clears the screen, prints the display, and then lists all the
* items in the player's inventory along with the amount of each item.
* After displaying the inventory, the function pauses the game to allow the
* player to view the information.
*/
void processInventory()
{
system("clear");
printDisplay();
cout << "\n\n";
int size = *(&itemNames + 1) - itemNames;
for (int i = 0; i < size; i++)
{
cout << itemNames[i] << " " << itemAmount[i] << "\n";
}
pauseGame();
}
/**
* Handles the logic for a battle between the player and an enemy.
*
* This function is responsible for the core gameplay loop of a battle encounter.
* It displays the enemy's health, prompts the player for an action (attack or run away),
* and calculates the results of the player's and enemy's attacks. The function continues
* the battle loop until either the player or the enemy is defeated.
*
* @param TYPE The type of the enemy being encountered (e.g. "Goblin").
* @param enemyType A more specific description of the enemy type.
*/
void processBattle(string TYPE, string enemyType)
{
char input = ' ';
bool battleOver = false;
int eHealth = (getRandom(10, 20) * (pLevel + 1));
system("clear");
while (battleOver == false)
{
string randPlayerAttackMessage = playerAttackMessage[getRandom(1, 5) - 1];
system("clear");
printDisplay();
int attack = (pAttack + getRandom(1, 5)) * (pLevel + 1);
int eAttackLow = (getRandom(1, 3) * (pLevel + 1));
int eAttackHigh = (getRandom(3, 7) * (pLevel + 1));
int eAttack = getRandom(eAttackLow, eAttackHigh);
cout << " Goblin Encounter \n\n" + enemyType + " HP: " << eHealth << "\n\n";
cout << "(A)attack, (D)run away\n";
input = getch();
input = toupper(input);
if (input == 'A')
{
cout << randPlayerAttackMessage + enemyType + " for " << attack << " damage.\n";
eHealth = eHealth - attack;
if (eHealth < 1)
{
int gold = (getRandom(1, 10) * (pLevel + 1));
int dropChance = getRandom(1, 10);
int dropItem = getRandom(1, 3);
int dropAmount = getRandom(1, 3) * (pLevel + 1);
cout << "you killed the pitiful " + enemyType + ".\n";
cout << "you found " << gold << " gold.\n";
money = money + gold;
goblinsKilled = goblinsKilled + 1;
if (dropChance > 7)
{
cout << "you found " << dropAmount << " " << itemNames[dropItem - 1] << ".\n";
itemAmount[dropItem - 1] += dropAmount;
}
battleOver = true;
break;
}
}
if (input == 'D')
{
int gold = (getRandom(1, 5) * (pLevel + 1));
if (money < (money - gold))
{
cout << "you run away.\n";
battleOver = true;
break;
}
cout << "you run away and drop " << gold << " gold.\n";
money = money - gold;
if (money < 1)
money = 0;
battleOver = true;
break;
}
pauseGame();
system("clear");
printDisplay();
cout << enemyType + " attacks you for " << eAttack << " damage.\n";
pHealth = pHealth - eAttack;
if (pHealth < 1)
{
cout << "you have fallen to the mighty " + enemyType + ".\n";
battleOver = true;
break;
}
pauseGame();
}
pauseGame();
}
/**
* Generates a player object at a random valid position on the game board.
*
* This function searches the game board for a valid tile (not a blocker) and
* places the player object on that tile. It also updates the playerPOS array
* with the coordinates of the player's position.
*
* @param object The object to be placed on the board (e.g. "PLAYER").
* @param TILE The tile type that the object can be placed on (e.g. "TILE").
*/
void generatePlayer(string object, string TILE)
{
bool playerPlaced = false;
int randomChance = getRandom(1, 10000);
while (playerPlaced == false) {
for (int i = 3; i < gridWidth - 3; i++) {
for (int j = 3; j < gridHeight - 3; j++) {
if (board[i][j] == TILE && (getRandom(1, 10000) > 9990) && playerPlaced == false)
{
board[i][j] = object;
if (object == PLAYER)
{
playerPOS[0] = i;
playerPOS[1] = j;
}
playerPlaced = true;
}
}
}
}
}
/**
* Processes user input for player movement and inventory.
*
* This function is called to handle user input for the game. It reads a character
* from the keyboard using the `getch()` function, converts it to uppercase, and
* then performs the appropriate action based on the input character.
*
* The supported input characters are:
* - 'W': Move the player up, if possible
* - 'A': Move the player left, if possible
* - 'S': Move the player down, if possible
* - 'D': Move the player right, if possible
* - 'I': Process the player's inventory
*
* The function updates the player's position on the game board and the `tempTileA`,
* `tempTileB`, and `tempTileC` variables accordingly. It also increments the `steps`
* counter when the player moves.
*/
void processInput()
{
char input = getch();
input = toupper(input);
switch (input)
{
case 'W':
if ((playerPOS[0] != 0) && (board[playerPOS[0] - 1][playerPOS[1]] != BLOCKER))
{
tempTileA = board[playerPOS[0] - 1][playerPOS[1]];
tempTileC = tempTileB;
board[playerPOS[0] - 1][playerPOS[1]] = PLAYER;
board[playerPOS[0]][playerPOS[1]] = tempTileC;
tempTileB = tempTileA;
playerPOS[0] = playerPOS[0] - 1;
steps++;
}
break;
case 'A':
if (playerPOS[1] != 0 && board[playerPOS[0]][playerPOS[1] - 1] != BLOCKER)
{
tempTileA = board[playerPOS[0]][playerPOS[1] - 1];
tempTileC = tempTileB;
board[playerPOS[0]][playerPOS[1] - 1] = PLAYER;
board[playerPOS[0]][playerPOS[1]] = tempTileC;
tempTileB = tempTileA;
playerPOS[1] = playerPOS[1] - 1;
steps++;
}
break;
case 'S':
if (playerPOS[0] != SIZE - 1 && board[playerPOS[0] + 1][playerPOS[1]] != BLOCKER)
{
tempTileA = board[playerPOS[0] + 1][playerPOS[1]];
tempTileC = tempTileB;
board[playerPOS[0] + 1][playerPOS[1]] = PLAYER;
board[playerPOS[0]][playerPOS[1]] = tempTileC;
tempTileB = tempTileA;
playerPOS[0] = playerPOS[0] + 1;
steps++;
}
break;
case 'D':
if (playerPOS[1] != SIZE - 1 && board[playerPOS[0]][playerPOS[1] + 1] != BLOCKER)
{
tempTileA = board[playerPOS[0]][playerPOS[1] + 1];
tempTileC = tempTileB;
board[playerPOS[0]][playerPOS[1] + 1] = PLAYER;
board[playerPOS[0]][playerPOS[1]] = tempTileC;
tempTileB = tempTileA;
playerPOS[1] = playerPOS[1] + 1;
steps++;
}
break;
case 'I':
processInventory();
break;
case 'Q':
saveLoadScreen();
break;
}
currentTerrain = tempTileA;
}
/**
* Processes the movement of the player on the game board.
*
* @param dx The change in the x-coordinate of the player's position.
* @param dy The change in the y-coordinate of the player's position.
*/
void processMovement(int dx, int dy)
{
tempTileA = board[playerPOS[0] + dx][playerPOS[1] + dy];
tempTileC = tempTileB;
board[playerPOS[0] + dx][playerPOS[1] + dy] = PLAYER;
board[playerPOS[0]][playerPOS[1]] = tempTileC;
tempTileB = tempTileA;
playerPOS[1] = playerPOS[1] + 1;
}
/**
* Generates a world map for the game by initializing the game board, applying cellular automata algorithms to create a maze-like structure, adding random walkers, and generating the player's starting position.
*
* This function is responsible for the initial setup and generation of the game world. It calls various helper functions to create the game board, apply cellular automata algorithms, add random walkers, and generate the player's starting position.
*/
void generateWorldMap()
{
initBoard();
cellularAutomataMaze(4);
cellularAutomata(2);
cellularAuto(1);
randGen();
randomWalk(SIZE / 2, WALKER);
randomWalk(SIZE / 2, WALKER);
randomWalk(SIZE / 2, WALKER);
cellularAutomataMiniMaze(7);
cellularAutomataMiniMaze(5);
cellularAutomataMiniMaze(3);
cellularAutomataMiniMaze(2);
cellularAutomataMiniMaze(2);
runRuleTile(CASTLE, CASTLEwallUP, CASTLEwall);
cellularAutomataBorder(5);
destroyIslands(WALKER);
crossRiver(4, WALKER, crossWATER, WATERrand);
generatePlayer(PLAYER, WATERrand);
//randomWalk(30, WALKER);
printBoard();
}
/**
* Generates a dungeon map for the game by initializing the game board, applying cellular automata algorithms to create a maze-like structure, adding random walkers, and generating the player's starting position and the exit.
*
* This function is responsible for the initial setup and generation of the game dungeon. It calls various helper functions to create the game board, apply cellular automata algorithms, add random walkers, and generate the player's starting position and the exit.
*/
void generateDungeon()
{
system("clear");
initBoard();
cellularAutomataMaze(4);
randomWalk(SIZE / 2, WALKER);
randomWalk(SIZE / 2, WALKER);
randomWalk(SIZE / 2, WALKER);
randomWalk(SIZE / 2, WALKER);
randomWalk(SIZE / 2, WALKER);
randomWalk(SIZE / 2, WALKER);
changeTiles(GROUND, BLOCKER);
//changeTiles(WALKER, WATER);
generatePlayer(PLAYER, WALKER);
tempTileB = WALKER;
generatePlayer(EXIT, WATER);
changeTiles(WALKER, gGROUNDrand);
printBoard();
}
/**
* Generates a random integer between the specified low and high values (inclusive).
*
* This function uses a random device to seed a Mersenne Twister pseudo-random number
* generator, and then generates a random integer within the specified range using a
* uniform integer distribution.
*
* @param low The lower bound of the range (inclusive).
* @param high The upper bound of the range (inclusive).
* @return A random integer between low and high (inclusive).
*/
int getRandom(int low, int high)
{
// Set up a random device to seed the random number generator
random_device rd;
mt19937 engine(rd());
uniform_int_distribution<int> distribution(low, high);
int randomInteger = distribution(engine);
return randomInteger;
}
/**
* Prints the current state of the game board to the console.
*
* This function clears the console and then prints the contents of the `board` array
* to the console, with each element of the array represented as a string. The function
* uses a nested loop to iterate over the rows and columns of the board, and the `printf`
* function to output the board elements.
*/
void printBoard()
{
system("clear");
// print game board
for (int i = 0; i < gridWidth; ++i) {
for (int j = 0; j < gridHeight; ++j) {
//cout << board[i][j];
printf("%s", board[i][j].c_str());
}
//cout << endl;
printf("%s\n");
}
//this_thread::sleep_for(chrono::milliseconds(10));
}
/**
* Initializes the game board by randomly setting each cell to either GROUND or WATER.
*
* This function iterates over the grid and sets each cell to either GROUND or WATER based on a random chance.
* The chance of a cell being set to GROUND is 50% (randChance > 5).
*/
void initBoard()
{
int randChance = 0;
//initialize grid
for (int x = 0;x < gridWidth;x++) {
for (int y = 0;y < gridHeight;y++) {
randChance = getRandom(1, 10);
if (randChance > 5)
board[x][y] = GROUND;
else
board[x][y] = WATER;
}
}