-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cpp
More file actions
2044 lines (1829 loc) · 73.3 KB
/
Copy pathProgram.cpp
File metadata and controls
2044 lines (1829 loc) · 73.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
/*
PROGRAM INFO
Author: Jacob Garcia
Version: 1.09
*/
#include <iostream>
#include <limits.h>
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "heap.h"
#include "Pokedex.h"
#include "BattleAI.h"
//Constants
#define ROWS 21
#define COLUMNS 80
#define WORLDROWS 401
#define WORLDCOLUMNS 401
#define MAXNPC 500
#define COLOR_WBG_BC 101
//Structs
typedef enum {
hikerNPC,
rivalNPC,
pacerNPC,
wandererNPC,
sentryNPC,
explorerNPC,
NPCTYPES
} npc;
typedef class NPCs {
public:
int alive;
int x;
int y;
int dirX;
int dirY;
int turnTime;
npc type;
std::vector<pokemonDTO> team;
} NPCs;
class map {
public:
std::string map[ROWS][COLUMNS];
int hikerWeights[ROWS][COLUMNS];
int othersWeights[ROWS][COLUMNS];
NPCs npcs[MAXNPC];
heap_t turns;
int hikerMap[ROWS][COLUMNS];
int rivalMap[ROWS][COLUMNS];
int nmbOfNPCs;
int x;
int y;
int northEnt;
int southEnt;
int westEnt;
int eastEnt;
};
typedef struct path {
heap_node_t *hn;
int x;
int y;
int cost;
} path;
typedef class Bag {
public:
int potions;
int superPotions;
int revives;
int pokeballs;
} Bag;
typedef class PlayerChar {
public:
int x;
int y;
int worldX;
int worldY;
int turnTime;
std::vector<pokemonDTO> team;
Bag bag;
} PlayerChar;
//Prototypes
bool EncounterBattle(pokemonDTO encPokemon);
bool Battle(NPCs battleNPC);
bool OpenBag(bool encounter);
void PopupBox();
void PopupBoxNoAnim();
void SetupColors();
void MoveNPC(int worldX, int worldY, NPCs *currNPC);
int MoveNPC_CheckValid(int worldX, int worldY, int nextX, int nextY);
void SpawnNPCs(int number, int worldX, int worldY);
void PlacePC(int worldX, int worldY);
class map GenerateMap(int x, int y);
void PrintMap(int worldX, int worldY);
// void PrintExploredWorld(); NOTE: Make this method in the future por favor
std::string FindTerrain();
void DeleteWorld();
static int32_t path_cmp(const void *key, const void *with);
static int32_t npc_turn_cmp(const void *key, const void *with);
static void Dijkstra(class map *map, npc npcType, int playerX, int playerY);
//Elements and Characters
std::string MTN = "%";
std::string TREE = "^";
std::string ROAD = "#";
std::string LNGR = ":";
std::string CLRNG = ".";
std::string WATER = "~";
std::string CNTR = "C";
std::string PKMART = "M";
std::string HIKER = "h";
std::string RIVAL = "r";
std::string PACER = "p";
std::string WANDERER = "w";
std::string SENTRY = "s";
std::string EXPLORER = "e";
class map *worldMap[WORLDROWS][WORLDCOLUMNS];
PlayerChar *Player;
Pokedex pokedex;
int main(int argc, char *argv[]) {
int numTrainers = 5;
// --numtrainers switch
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--numtrainers") == 0) {
if (i + 1 < argc) {
numTrainers = atoi(argv[i + 1]);
if (numTrainers > MAXNPC) {
printw("The max number of npcs is %d. (Why would you want that many anyway!?)", MAXNPC);
printw("\nProgram closed.\n");
return 1;
}
} else {
printw("Error: --numtrainers switch requires an argument.\n");
return 1;
}
}
}
//Make seed and save into seeds.txt
//Odd seeds
//1708631599 - Endless loop, cant place buildings
//1708632100 - Buildings overlap
time_t seed = time(NULL);
srand(seed);
FILE *seedFile;
seedFile = fopen("seeds.txt", "a");
fprintf(seedFile, "%ld\n", seed);
fclose(seedFile);
Player = new PlayerChar;
pokedex = Pokedex();
for (int i = 0; i < WORLDROWS; i++) {
for (int j = 0; j < WORLDCOLUMNS; j++) {
worldMap[i][j] = NULL;
}
}
int x = 200;
int y = 200;
std::string statusMessage = "";
GenerateMap(x, y);
PlacePC(x, y);
SpawnNPCs(numTrainers, x, y);
bool hasStarter = false;
initscr();
//Check terminal is correct size before continuing
int termY, termX;
getmaxyx(stdscr, termY, termX);
if (termX != 80 || termY != 24) {
endwin();
system("clear");
std::cout << "YOUR TERMINAL SIZE IS CURRENTLY (" << termX << "x" << termY << ")" << std::endl;
std::cout << "PLEASE SET YOUR TERMINAL SIZE TO DEFAULT (80x24) AND RE LAUNCH PROGRAM" << std::endl;
exit(0);
}
raw();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
SetupColors();
// Start game
char command = 'c';
while (command != 'Q') {
Dijkstra(worldMap[x][y], hikerNPC, Player->x, Player->y);
Dijkstra(worldMap[x][y], rivalNPC, Player->x, Player->y);
// Check if it is PCs turn
NPCs *nextNPC = (NPCs *)heap_remove_min(&worldMap[x][y]->turns);
NPCs *battleNPC = NULL;
int battleTime = 0;
if (Player->turnTime > nextNPC->turnTime) {
MoveNPC(x, y, nextNPC);
// Check if battle time
battleTime = nextNPC->x == Player->x && nextNPC->y == Player->y && nextNPC->alive;
if (battleTime) {
battleNPC = nextNPC;
} else {
heap_insert(&worldMap[x][y]->turns, nextNPC);
continue;
}
}
heap_insert(&worldMap[x][y]->turns, nextNPC);
clear();
PrintMap(x, y);
attron(COLOR_PAIR(COLOR_MAGENTA));
mvprintw(22, 0, "%s", statusMessage.c_str());
attroff(COLOR_PAIR(COLOR_MAGENTA));
refresh();
statusMessage = "";
if (!hasStarter) {
std::vector<pokemonDTO> starters;
for (int i = 0; i < 3; i++) {
pokemonDTO pokemon = pokedex.pokemonData[rand() % (int) pokedex.pokemonData.size()];
//Check not already in list
bool found = false;
for (int j = 0; j < (int) starters.size(); j++) {
if (pokemon.id == starters[j].id) found = true;
}
if (found) {
i--;
continue;
}
starters.push_back(pokemon);
}
starters.push_back(pokedex.pokemonData[132]);
PopupBox();
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(5, (COLUMNS / 2) - (12 / 2), "PokeGame 327");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(7, 4, "Welcome to PokeGame 327! Please read the README if you haven't already.");
mvprintw(8, 4, "Select your starter Pokemon from below! (Type their number)");
mvprintw(10, 4, "| (1.) %s | (2.) %s |", starters[0].identifier.c_str(), starters[1].identifier.c_str());
mvprintw(11, 4, "| (3.) %s | (4.) %s |", starters[2].identifier.c_str(), starters[3].identifier.c_str());
refresh();
char selection = ' ';
while (selection != '1' && selection != '2' && selection != '3' && selection != '4') {
selection = getch();
}
starters[(selection - '0') - 1].InitializePokemon(Player->worldX, Player->worldY, pokedex);
Player->team.push_back(starters[(selection - '0') - 1]);
mvprintw(13, 4, "You have selected %s as your starter, hit q to start!", Player->team[0].identifier.c_str());
while (getch() != 'q');
clear();
PrintMap(x, y);
refresh();
hasStarter = true;
}
if (battleTime) {
bool won = Battle(*battleNPC);
statusMessage = won ? "You won the battle!" : "You lost. You have been sent back to spawn.";
if (!won) {
bool placed = false;
x = 200;
y = 200;
while (!placed) {
int row = rand() % (ROWS - 2) + 1;
int col = rand() % (COLUMNS - 2) + 1;
if (!strcmp(worldMap[200][200]->map[row][col].c_str(), ROAD.c_str())) {
Player->x = row;
Player->y = col;
Player->worldX = 200;
Player->worldY = 200;
Player->turnTime = 0;
Player->bag.pokeballs = 10;
Player->bag.potions = 10;
Player->bag.superPotions = 3;
Player->bag.revives = 3;
Player->turnTime = 0;
// Heal all pokemon
for (int i = 0; i < (int)Player->team.size(); i++) {
Player->team[i].currentHP = Player->team[i].HP;
}
placed = true;
}
}
continue;
}
refresh();
clear();
PrintMap(x, y);
refresh();
usleep(250000);
battleNPC->alive = 0;
battleTime = false;
// NPC death animation
attron(COLOR_PAIR(COLOR_YELLOW) | A_BOLD);
for (int i = 0; i < 2; i++) {
for (int j = -i; j <= i; j++) {
mvprintw(battleNPC->x + i, battleNPC->y + j, "O");
mvprintw(battleNPC->x - i, battleNPC->y + j, "O");
}
for (int j = -(i - 1); j <= (i - 1); j++) {
mvprintw(battleNPC->x + j, battleNPC->y + i, "O");
mvprintw(battleNPC->x + j, battleNPC->y - i, "O");
}
refresh();
usleep(110000);
}
attroff(COLOR_PAIR(COLOR_YELLOW) | A_BOLD);
attron(COLOR_PAIR(COLOR_RED) | A_BOLD);
for (int i = 0; i < 2; i++) {
for (int j = -i; j <= i; j++) {
mvprintw(battleNPC->x + i, battleNPC->y + j, "X");
mvprintw(battleNPC->x - i, battleNPC->y + j, "X");
}
for (int j = -(i - 1); j <= (i - 1); j++) {
mvprintw(battleNPC->x + j, battleNPC->y + i, "X");
mvprintw(battleNPC->x + j, battleNPC->y - i, "X");
}
refresh();
usleep(110000);
}
attroff(COLOR_PAIR(COLOR_RED) | A_BOLD);
usleep(150000);
continue;
}
command = getch();
// Movement commands
int moveX = 0;
int moveY = 0;
// Upper left
if (command == '7' || command == 'y') {
moveX--;
moveY--;
}
// Up
else if (command == '8' || command == 'k') {
moveX--;
}
// Upper right
else if (command == '9' || command == 'u') {
moveY++;
moveX--;
}
// Right
else if (command == '6' || command == 'l') {
moveY++;
}
// Lower right
else if (command == '3' || command == 'n') {
moveY++;
moveX++;
}
// Down
else if (command == '2' || command == 'j') {
moveX++;
}
// Lower left
else if (command == '1' || command == 'b') {
moveY--;
moveX++;
}
// Left
else if (command == '4' || command == 'h') {
moveY--;
}
// Run checks
if (!(moveX == 0 && moveY == 0)) {
if (Player->x + moveX > 0 && Player->y + moveY > 0 &&
Player->x + moveX < ROWS - 1 && Player->y + moveY < COLUMNS - 1 &&
strcmp(worldMap[x][y]->map[Player->x + moveX][Player->y + moveY].c_str(), WATER.c_str()) &&
strcmp(worldMap[x][y]->map[Player->x + moveX][Player->y + moveY].c_str(), TREE.c_str())) {
// Add time to Player from last terrain weight and move
Player->turnTime += worldMap[x][y]->othersWeights[Player->x][Player->y];
Player->x += moveX;
Player->y += moveY;
// Pokemon long grass encounter
if (worldMap[x][y]->map[Player->x][Player->y] == LNGR) {
int encProb = rand() % 101;
if (encProb <= 10) {
pokemonDTO pokemon = pokedex.pokemonData[rand() % (int)pokedex.pokemonData.size()];
pokemon.InitializePokemon(Player->worldX, Player->worldY, pokedex);
bool won = EncounterBattle(pokemon);
statusMessage = won ? "" : "You lost. You have been sent back to spawn.";
if (!won) {
bool placed = false;
x = 200;
y = 200;
while (!placed) {
int row = rand() % (ROWS - 2) + 1;
int col = rand() % (COLUMNS - 2) + 1;
if (!strcmp(worldMap[200][200]->map[row][col].c_str(), ROAD.c_str())) {
Player->x = row;
Player->y = col;
Player->worldX = 200;
Player->worldY = 200;
Player->turnTime = 0;
Player->bag.pokeballs = 10;
Player->bag.potions = 10;
Player->bag.superPotions = 3;
Player->bag.revives = 3;
Player->turnTime = 0;
// Heal all pokemon
for (int i = 0; i < (int)Player->team.size(); i++) {
Player->team[i].currentHP = Player->team[i].HP;
}
placed = true;
}
}
continue;
}
}
}
} else if ((Player->x + moveX == 0 || Player->y + moveY == 0 ||
Player->x + moveX == ROWS - 1 || Player->y + moveY == COLUMNS - 1) &&
!strcmp(worldMap[x][y]->map[Player->x + moveX][Player->y + moveY].c_str(), ROAD.c_str())) {
// Player is going to next map
int newWorldX = 0;
int newWorldY = 0;
int newPCX, newPCY;
if (Player->y + moveY == worldMap[x][y]->northEnt && Player->x + moveX == 0) {
// North
newWorldY++;
newPCX = ROWS - 2;
newPCY = worldMap[x][y]->northEnt;
} else if (Player->y + moveY == worldMap[x][y]->southEnt && Player->x + moveX == ROWS - 1) {
//South
newWorldY--;
newPCX = 1;
newPCY = worldMap[x][y]->southEnt;
} else if (Player->y + moveY == 0 && Player->x + moveX == worldMap[x][y]->westEnt) {
//West
newWorldX--;
newPCX = worldMap[x][y]->westEnt;
newPCY = COLUMNS - 2;
} else if (Player->y + moveY == COLUMNS - 1 && Player->x + moveX == worldMap[x][y]->eastEnt) {
//East
newWorldX++;
newPCX = worldMap[x][y]->eastEnt;
newPCY = 1;
}
if (!(newWorldX == 0 && newWorldY == 0) && !(x + newWorldX > 400 || y + newWorldY > 400 || x + newWorldX < 0 || y + newWorldY < 0)) {
x += newWorldX;
y += newWorldY;
Player->turnTime = 0;
Player->x = newPCX;
Player->y = newPCY;
Player->worldX = x;
Player->worldY = y;
GenerateMap(x, y);
if (!worldMap[x][y]->turns.initialized) {
SpawnNPCs(numTrainers, x, y);
} else {
// Set all npcs turn time back to zero
for (int i = 0; i < numTrainers; i++) {
worldMap[x][y]->npcs[i].turnTime = 0;
}
}
}
}
}
// Rest for a turn
if (command == '5' || command == ' ' || command == '.') {
Player->turnTime += worldMap[x][y]->othersWeights[Player->x][Player->y];
}
// Enter building
if (command == '>') {
//Pokemart
if (!strcmp(worldMap[x][y]->map[Player->x][Player->y].c_str(), PKMART.c_str())) {
attron(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
mvprintw(ROWS + 1, 0, "You have entered the Pokemart! All your items have been replenished!");
mvprintw(ROWS + 2, 0, "Type < to leave");
attroff(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
refresh();
Player->bag.pokeballs = 10;
Player->bag.potions = 10;
Player->bag.superPotions = 3;
Player->bag.revives = 3;
while (getch() != '<');
}
//Pokestop
if (!strcmp(worldMap[x][y]->map[Player->x][Player->y].c_str(), CNTR.c_str())) {
attron(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
mvprintw(ROWS + 1, 0, "You have entered the Pokemon Center! All your Pokemon were healed/revived!");
mvprintw(ROWS + 2, 0, "Type < to leave");
attroff(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
refresh();
for (int i = 0; i < (int) Player->team.size(); i++) {
// Set each pokemons currentHP to the max HP (heal)
Player->team[i].currentHP = Player->team[i].HP;
}
while (getch() != '<');
}
}
// Fly
if (command == 'f') {
PopupBox();
attron(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
mvprintw(4, 4, "You are about to fly!");
mvprintw(5, 4, "Please type your two coordinates (seperated by newlines)");
mvprintw(6, 4, "you would like to fly to or type q to cancel: ");
attroff(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
refresh();
echo();
char input[10];
move(7, 4);
getstr(input);
if (!strcmp(input, "q"))
continue;
char input2[10];
move(8, 4);
getstr(input2);
noecho();
int newX = atoi(input);
int newY = atoi(input2);
newX += 200;
newY += 200;
if (!(newX > 400 || newY > 400 || newX < 0 || newY < 0)) {
x = newX;
y = newY;
GenerateMap(x, y);
//Place PC on new map
int placed = 0;
while (placed == 0) {
int row = rand() % (ROWS - 2) + 1;
int col = rand() % (COLUMNS - 2) + 1;
if (!strcmp(worldMap[x][y]->map[row][col].c_str(), ROAD.c_str())) {
Player->x = row;
Player->y = col;
Player->turnTime = 0;
Player->worldX = x;
Player->worldY = y;
placed = 1;
}
}
if (!worldMap[x][y]->turns.initialized) {
SpawnNPCs(numTrainers, x, y);
} else {
// Set all npcs turn time back to zero
for (int i = 0; i < numTrainers; i++) {
worldMap[x][y]->npcs[i].turnTime = 0;
}
}
} else {
statusMessage = "Error flying to coordinates. Please make sure they are in the world bounds.";
}
}
// Show World Exploration Map
// if (command == 'E') {
// PrintExploredWorld();
// }
//Add random pokemon, for debugging purposes. Delete this dont forget please
// if (command == 'A') {
// pokemonDTO pokemon = pokedex.pokemonData[rand() % (int) pokedex.pokemonData.size()];
// pokemon.InitializePokemon(0, 0, pokedex);
// Player->team.push_back(pokemon);
// }
// Open bag
if (command == 'B') {
OpenBag(false);
}
// View trainer list
if (command == 't') {
//Set up box for info
PopupBox();
//Print info
attron(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
mvprintw(4, (COLUMNS/2) - 6, "TRAINER INFO");
attroff(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
//Set up trainer info string array
std::string trainerInfo[numTrainers];
for (int i = 0; i < numTrainers; i++) {
NPCs currNPC = worldMap[x][y]->npcs[i];
// Get name of npc
std::string name;
switch (currNPC.type) {
case hikerNPC:
name = "Hiker";
break;
case rivalNPC:
name = "Rival";
break;
case pacerNPC:
name = "Pacer";
break;
case wandererNPC:
name = "Wanderer";
break;
case sentryNPC:
name = "Sentry";
break;
case explorerNPC:
name = "Explorer";
break;
default:
name = "Unknown NPC";
break;
}
if (!currNPC.alive) {
trainerInfo[i] = name + " - (dead)";
continue;
}
std::string xStr, yStr;
int xDist = Player->x - currNPC.x;
int yDist = Player->y - currNPC.y;
if (xDist < 0) {
xStr = "South " + std::to_string(abs(xDist)) + " squares";
} else {
xStr = "North " + std::to_string(xDist) + " squares";
}
if (yDist < 0) {
yStr = "East " + std::to_string(abs(yDist)) + " squares";
} else {
yStr = "West " + std::to_string(yDist) + " squares";
}
trainerInfo[i] = name + " - Location: " + xStr + ", " + yStr;
}
// Print all trainers
int i, s;
int scroll = 0;
int intCommand = 0;
// 27 is esc
while (intCommand != 27 && intCommand != 'q') {
// Clear area
for (i = 0; i < (ROWS - 9); i++) {
for (int j = 0; j < COLUMNS - 8; j++) {
mvprintw(6 + i , 4 + j, " ");
}
}
// Print trainers
for (i = 0 + scroll, s = 0; i < numTrainers && (6 + s) < ROWS - 4; i++, s += 2) {
mvprintw(6 + s, 5, "%s", trainerInfo[i].c_str());
}
refresh();
intCommand = getch();
// Scroll logic
if (intCommand == KEY_UP) {
if ((scroll - 1) >= 0)
scroll--;
} else if (intCommand == KEY_DOWN) {
if ((scroll + 1) < numTrainers)
scroll++;
}
}
}
}
printw("Closing game...\n");
refresh();
DeleteWorld();
endwin();
return 0;
}
//Very similar to battle method
bool EncounterBattle(pokemonDTO encPokemon) {
PopupBox();
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(3, (COLUMNS / 2) - (13 / 2), "! ENCOUNTER !");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
// Line animation
for (int i = 0; i < (COLUMNS / 2) - 4; i++) {
mvprintw(ROWS / 2, i + 4, "#");
mvprintw(ROWS / 2, (COLUMNS - i) - 5, "#");
usleep(15000);
refresh();
}
bool found = false;
// Select the start pokemon.
pokemonDTO *currentPokemon = &Player->team[0];
for (int i = 0; i < (int)Player->team.size(); i++) {
if (Player->team[i].HP != 0) {
currentPokemon = &Player->team[i];
found = true;
break;
}
}
if (!found) {
return false;
}
bool battleDone = false;
bool caught = false;
char command = ' ';
std::string statusMessage;
std::string enemyStatusMessage;
// Battle loop
while (!battleDone) {
if (caught) {
if ((int)Player->team.size() == 6) {
statusMessage = "Failed to catch! Your are already at max pokemon!";
} else {
statusMessage = "You caught the pokemon! Type q to exit";
Player->team.push_back(encPokemon);
}
}
PopupBoxNoAnim();
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(3, (COLUMNS / 2) - (13 / 2), "! ENCOUNTER !");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
for (int i = 0; i < (COLUMNS / 2) - 4; i++) {
mvprintw(ROWS / 2, i + 4, "#");
mvprintw(ROWS / 2, (COLUMNS - i) - 5, "#");
}
attron(COLOR_PAIR(COLOR_WBG_BC));
mvprintw(ROWS / 2, (COLUMNS / 2) - (statusMessage.length() / 2), "%s", statusMessage.c_str());
mvprintw((ROWS / 2) + 1, (COLUMNS / 2) - (enemyStatusMessage.length() / 2), "%s", enemyStatusMessage.c_str());
attroff(COLOR_PAIR(COLOR_WBG_BC));
statusMessage = "";
enemyStatusMessage = "";
if (caught) {
while (getch() != 'q');
return true;
}
// Print Pokemon info for PC
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(ROWS - 8, 4, "You");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
std::string currentString = "Current Pokemon: " + currentPokemon->identifier + " (" + std::to_string(currentPokemon->currentHP) + "/" + std::to_string(currentPokemon->HP) + ")";
mvprintw(ROWS - 7, 4, "%s", currentString.c_str());
mvprintw(ROWS - 6, 4, "(1.) Fight (2.) Bag (3.) Run (4.) Pokemon");
// Print encounter pokemon info
std::string name = encPokemon.identifier + " (" + std::to_string(encPokemon.currentHP) + "/" + std::to_string(encPokemon.HP) + ")";
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw((ROWS/2) - 5, (COLUMNS - 4) - 13, "Enemy Pokemon");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw((ROWS/2) - 4, (COLUMNS - 4) - (name.length()), "%s", name.c_str());
command = getch();
movesDTO enemyMove = encPokemon.knownMoves[rand() % encPokemon.knownMoves.size()];
if (command == '1') { // Fight
PopupBoxNoAnim();
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(3, (COLUMNS / 2) - (11 / 2), "SELECT MOVE");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(9, 4, "Select the move you want to use");
for (int i = 0; i < (int)currentPokemon->knownMoves.size(); i++) {
mvprintw(10 + i, 4, "(%d.) %s", i + 1, currentPokemon->knownMoves[i].identifier.c_str());
}
int selection = (getch() - '0') - 1;
movesDTO move = currentPokemon->knownMoves[selection];
//Your Pokemon
bool moveHits = rand() % 100 <= move.accuracy;
double critical = 1;
if ((rand() % 255) < currentPokemon->base_speed / 2) {
critical = 1.5;
}
double random = (rand() % (100 - 85 + 1) + 85);
int stab = 1;
int type = 1;
int damage = ((((((2 * currentPokemon->level) / 5) + 2) * move.power * (currentPokemon->attack / encPokemon.defense)) / 50) + 2) * critical * (random / 100) * stab * type;
if (damage < 0) damage = 0;
//Encounter Pokemon
bool enemyMoveHits = rand() % 100 <= move.accuracy;
double enemyCritical = 1;
if ((rand() % 255) < currentPokemon->base_speed / 2) {
critical = 1.5;
}
double enemyRandom = (rand() % (100 - 85 + 1) + 85);
int enemyStab = 1;
int enemyType = 1;
int enemyDamage = ((((((2 * encPokemon.level) / 5) + 2) * enemyMove.power * (encPokemon.attack / currentPokemon->defense)) / 50) + 2) * enemyCritical * (enemyRandom / 100) * enemyStab * enemyType;
if (enemyDamage < 0) enemyDamage = 0;
int goesFirst = 0;
if (move.priority > enemyMove.priority) {
goesFirst = 1;
} else if (move.priority == enemyMove.priority) {
if (currentPokemon->speed > encPokemon.speed) {
goesFirst = 1;
} else if (currentPokemon->speed == encPokemon.speed) {
goesFirst = (rand() % 1) + 1;
} else {
goesFirst = 2;
}
} else {
goesFirst = 2;
}
if (goesFirst == 1) {
if (moveHits) {
statusMessage = "Your move hit successfully and did " + std::to_string(damage) + " damage!";
encPokemon.currentHP -= damage;
} else {
statusMessage = "Your move missed!";
}
if (encPokemon.currentHP > 0) {
if (enemyMoveHits) {
enemyStatusMessage = "The Enemy Pokemon used " + enemyMove.identifier + " for " + std::to_string(enemyDamage) + " damage!";
currentPokemon->currentHP -= enemyDamage;
} else {
enemyStatusMessage = "The Enemy Pokemon's move missed!";
}
}
} else {
if (enemyMoveHits) {
enemyStatusMessage = "The Enemy Pokemon used " + enemyMove.identifier + " for " + std::to_string(enemyDamage) + " damage!";
currentPokemon->currentHP -= enemyDamage;
} else {
enemyStatusMessage = "The Enemy Pokemon's move missed!";
}
if (currentPokemon->currentHP > 0) {
if (moveHits) {
statusMessage = "Your move hit successfully and did " + std::to_string(damage) + " damage!";
encPokemon.currentHP -= damage;
} else {
statusMessage = "Your move missed!";
}
}
}
// Check if either pokemon is dead
if (encPokemon.currentHP <= 0) {
enemyStatusMessage = "The Enemy Pokemon died!";
encPokemon.dead = true;
battleDone = true;
return true;
} else if (currentPokemon->currentHP <= 0) {
statusMessage = "Your Pokemon died! Switched to an alive Pokemon.";
currentPokemon->currentHP = 0;
currentPokemon->dead = true;
bool found = false;
for (int i = 0; i < (int)Player->team.size(); i++) {
if (!Player->team[i].dead) {
currentPokemon = &Player->team[i];
found = true;
}
}
if (!found) {
battleDone = true;
return false;
}
}
} else if (command == '2') { // Bag
caught = OpenBag(true);
} else if (command == '3') { // Run
if (rand() % 100 >= 80) {
statusMessage = "You attempt to run failed!";
} else {
return true;
}
} else if (command == '4') { // Pokemon
PopupBoxNoAnim();
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(3, (COLUMNS / 2) - (14 / 2), "SWITCH POKEMON");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(9, 4, "Select the Pokemon you want to switch to");
for (int i = 0; i < (int)Player->team.size(); i++) {
mvprintw(10 + i, 4, "(%d.) %s (%d/%d)", i + 1, Player->team[i].identifier.c_str(), Player->team[i].currentHP, Player->team[i].HP);
}
int selection = (getch() - '0');
if (selection <= 0 || selection > (int)Player->team.size()) continue;
currentPokemon = &Player->team[selection - 1];
}
refresh();
}
return true;
}
bool Battle(NPCs battleNPC) {
PopupBox();
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(3, (COLUMNS / 2) - (10 / 2), "! BATTLE !");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
// Line animation
for (int i = 0; i < (COLUMNS / 2) - 4; i++) {
mvprintw(ROWS / 2, i + 4, "#");
mvprintw(ROWS / 2, (COLUMNS - i) - 5, "#");
usleep(15000);
refresh();
}
bool found = false;
pokemonDTO *enemyPokemon = &battleNPC.team[0];
// Select the start pokemon.
pokemonDTO *currentPokemon = &Player->team[0];
for (int i = 0; i < (int)Player->team.size(); i++) {
if (Player->team[i].HP != 0) {
currentPokemon = &Player->team[i];
found = true;
break;
}
}
if (!found) {
// All pokemons are dead. Return. Change this up a tiny bit in the future probably.
return false;
}
bool battleDone = false;
char command = ' ';
std::string statusMessage;
std::string enemyStatusMessage;
// Battle loop
while (!battleDone) {
PopupBoxNoAnim();
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(3, (COLUMNS / 2) - (10 / 2), "! BATTLE !");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
for (int i = 0; i < (COLUMNS / 2) - 4; i++) {
mvprintw(ROWS / 2, i + 4, "#");
mvprintw(ROWS / 2, (COLUMNS - i) - 5, "#");
}
attron(COLOR_PAIR(COLOR_WBG_BC));
mvprintw(ROWS / 2, (COLUMNS / 2) - (statusMessage.length() / 2), "%s", statusMessage.c_str());
mvprintw((ROWS / 2) + 1, (COLUMNS / 2) - (enemyStatusMessage.length() / 2), "%s", enemyStatusMessage.c_str());
attroff(COLOR_PAIR(COLOR_WBG_BC));
statusMessage = "";
enemyStatusMessage = "";
// Print Pokemon info for PC
attron(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
mvprintw(ROWS - 8, 4, "You");
attroff(COLOR_PAIR(COLOR_MAGENTA | A_BOLD));
std::string currentString = "Current Pokemon: " + currentPokemon->identifier + " (" + std::to_string(currentPokemon->currentHP) + "/" + std::to_string(currentPokemon->HP) + ")";