-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvindication.c
2045 lines (1790 loc) · 67.5 KB
/
vindication.c
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 "ICS_Project.h"
// #include"cJSON.c"
void flushInputBuffer() {
char c;
if (!feof(stdin) && !ferror(stdin)) {
do
{
c=getchar();
// printf("\n%d",c); // Read and discard characters until newline or EOF
}while(c != '\n');
}
}
void gameLost(Player *player,int *state)
{
player->currentLocation = strdup("WORLD/CITY");
player->gold = 0;
player->xp = 0;
player->inventory->items[1] = NULL;
player->inventory->activeItems[0] = 0;
player->inventory->activeItems[1] = -1;
*state=0;
}
int getNpcNumber() // This works checked
{
FILE *file = fopen("characters.json", "r");
if (file == NULL)
{
fprintf(stderr, "Failed to open file characters.json for reading\n");
return -1;
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *fileContent = (char *)malloc(fileSize + 1);
if (fileContent == NULL)
{
fprintf(stderr, "Memory allocation failed for file content\n");
fclose(file);
return -1;
}
size_t bytesRead = fread(fileContent, 1, fileSize, file);
// if (bytesRead != fileSize) {
// printf("Error reading file.\n");
// fclose(file);
// free(fileContent);
// return NULL;
// }
fileContent[fileSize] = 0;
fclose(file);
cJSON *root = cJSON_Parse(fileContent);
if (root == NULL)
{
printStory("Error parsing JSON data.\n",BMAG,HIG);
free(fileContent);
return -1;
}
cJSON *charactersArray = cJSON_GetObjectItem(root, "characters");
return cJSON_GetArraySize(charactersArray);
}
// FROM world.c
void delay2(int milliseconds)
{
clock_t start_time = clock();
while (clock() < start_time + milliseconds * CLOCKS_PER_SEC / 1000)
;
}
void printStory(const char *sentence, char *textStyle,int time)
{
// Print color code
printf("%s", textStyle);
// Print the sentence with typing effect
for (int i = 0; i < strlen(sentence); i++)
{
printf("%c", sentence[i]);
fflush(stdout); // Flush the output buffer to ensure immediate printing
delay2(time); // Adjust delay as needed (50 milliseconds in this example)
}
// Reset color to default
printf("\033[0m");
}
void printFormattedStringWithColorAndDelay(const char *format, char *textStyle, int delay, ...) {
va_list args;
va_start(args, delay);
printf("%s", textStyle);
vprintf(format, args);
printf("\033[0m");
va_end(args);
fflush(stdout); // Flush stdout to ensure immediate output
delay2(delay); // Delay in milliseconds
}
int playMiniGame(Player *player, char *gameName)
{
// Define an array of string-function pairs
// correct the names of minigame function to these
StringFunctionPair functions[] = {
// {"Combat", Combat},
{"falconry", falconry},
{"Combat", Combat},
{"PrisonEscape", PrisonEscape},
{"Lockpicking", Lockpicking},
{"Memory", Memory},
{"Minesweeper", Minesweeper},
{"Hangtheman", Hangtheman},
{"Snakegame", Snakegame},
{NULL, NULL}
// add more games
};
// to call a function based on input string
int i;
for (i = 0; functions[i].str != NULL; i++)
{
if (strcmp(gameName, functions[i].str) == 0)
{
printf("\e[1;1H\e[2J");
return functions[i].func(); // Call the function associated with the input string
}
}
printStory("No function found for input: ",BWHT,HIG);
printStory( gameName,BWHT,HIG);
printf("\n");
}
Player *createNewPlayer(char *playerID)
{
// printf("Creating new player...\n");
// Allocate memory for the player struct
Player *newPlayer = (Player *)malloc(sizeof(Player));
if (newPlayer == NULL)
{
fprintf(stderr, "Memory allocation failed for Player\n");
exit(EXIT_FAILURE);
}
// printf("Allocated memory for player struct.\n");
// Initialize player ID
newPlayer->id = strdup(playerID);
if (newPlayer->id == NULL)
{
fprintf(stderr, "Memory allocation failed for ID\n");
exit(EXIT_FAILURE);
}
// printf("Initialized player ID: %s\n", newPlayer->id);
// Initialize player name
newPlayer->name = strdup("Gaius Marcus Agrippa");
if (newPlayer->name == NULL)
{
fprintf(stderr, "Memory allocation failed for name\n");
exit(EXIT_FAILURE);
}
// printf("Initialized player name: %s\n", newPlayer->name);
// Allocate memory for player stats
newPlayer->stats = (Stats *)malloc(sizeof(Stats));
if (newPlayer->stats == NULL)
{
fprintf(stderr, "Memory allocation failed for stats\n");
exit(EXIT_FAILURE);
}
// printf("Allocated memory for player stats.\n");
// Initialize player inventory
newPlayer->inventory = (Inventory *)malloc(sizeof(Inventory));
if (newPlayer->inventory == NULL)
{
fprintf(stderr, "Memory allocation failed for inventory\n");
exit(EXIT_FAILURE);
}
// printf("Allocated memory for player inventory.\n");
// Allocate memory for inventory items and active items
newPlayer->inventory->items = (char **)malloc(11*sizeof(char *));
newPlayer->inventory->activeItems = (int *)malloc(11*sizeof(int));
if (newPlayer->inventory->items == NULL || newPlayer->inventory->activeItems == NULL)
{
fprintf(stderr, "Memory allocation failed for inventory items or active items\n");
exit(EXIT_FAILURE);
}
// printf("Allocated memory for inventory items and active items.\n");
// Initialize current location
newPlayer->currentLocation = strdup("WORLD/CITY");
if (newPlayer->currentLocation == NULL)
{
fprintf(stderr, "Memory allocation failed for current location\n");
exit(EXIT_FAILURE);
}
// printf("Initialized current location: %s\n", newPlayer->currentLocation);
// Allocate memory for active quests
newPlayer->activeQuests = (char **)malloc(sizeof(char *));
if (newPlayer->activeQuests == NULL)
{
fprintf(stderr, "Memory allocation failed for active quests\n");
exit(EXIT_FAILURE);
}
// printf("Allocated memory for active quests.\n");
// Get the number of NPCs
int numOfNpcs = getNpcNumber();
// Allocate memory for NPC info
newPlayer->NPCInfo = (int **)malloc(sizeof(int *) * numOfNpcs);
if (newPlayer->NPCInfo == NULL)
{
fprintf(stderr, "Memory allocation failed for NPC info\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < numOfNpcs; i++)
{
newPlayer->NPCInfo[i] = (int *)calloc(3, sizeof(int));
if (newPlayer->NPCInfo[i] == NULL)
{
fprintf(stderr, "Memory allocation failed for NPC info\n");
exit(EXIT_FAILURE);
}
}
// printf("Allocated memory for NPC info.\n");
// Initialize player properties
newPlayer->level = 1;
// Initialize player stats
newPlayer->stats->HP = 100;
newPlayer->stats->atk = 10;
newPlayer->stats->def = 5;
newPlayer->stats->agi = 8;
newPlayer->stats->str = 12;
newPlayer->stats->dex = 9;
newPlayer->stats->intel = 6;
newPlayer->stats->luck = 7;
// printf("Initialized player properties and stats.\n");
// Initialize player inventory size and items
newPlayer->inventory->size = 10;
newPlayer->inventory->items[0]=strdup("Gladiator's Sword");
newPlayer->inventory->items[1] = NULL;
newPlayer->inventory->activeItems[0] = 0; // not equiped
newPlayer->inventory->activeItems[1] = -1; // NULL
// printf("Initialized player inventory.\n");
// Initialize other player properties
newPlayer->wtdLevel = 0;
newPlayer->xp = 0;
newPlayer->gold = 50;
// printf("Initialized other player properties.\n");
// Initialize active quests
newPlayer->activeQuests[0] = NULL;
// printf("Initialized active quests.\n");
// printf("Player creation successful.\n");
return newPlayer;
}
void savePlayerData(Player *player) // This works checked
{
cJSON *root = cJSON_CreateObject();
cJSON *playerObject = cJSON_CreateObject();
cJSON_AddItemToObject(root, "player", playerObject);
cJSON_AddStringToObject(playerObject, "id", player->id);
cJSON_AddStringToObject(playerObject, "name", player->name);
cJSON_AddNumberToObject(playerObject, "level", player->level);
cJSON_AddNumberToObject(playerObject, "wtdLevel", player->wtdLevel);
cJSON_AddNumberToObject(playerObject, "xp", player->xp);
cJSON_AddNumberToObject(playerObject, "gold", player->gold);
cJSON_AddStringToObject(playerObject, "currentLocation", player->currentLocation);
cJSON *statsObject = cJSON_CreateObject();
cJSON_AddItemToObject(playerObject, "stats", statsObject);
cJSON_AddNumberToObject(statsObject, "hp", player->stats->HP);
cJSON_AddNumberToObject(statsObject, "atk", player->stats->atk);
cJSON_AddNumberToObject(statsObject, "def", player->stats->def);
cJSON_AddNumberToObject(statsObject, "agi", player->stats->agi);
cJSON_AddNumberToObject(statsObject, "str", player->stats->str);
cJSON_AddNumberToObject(statsObject, "dex", player->stats->dex);
cJSON_AddNumberToObject(statsObject, "intel", player->stats->intel);
cJSON_AddNumberToObject(statsObject, "luck", player->stats->luck);
cJSON *inventoryObject = cJSON_CreateObject();
cJSON_AddItemToObject(playerObject, "inventory", inventoryObject);
cJSON_AddNumberToObject(inventoryObject, "size", player->inventory->size);
cJSON *itemsArray = cJSON_AddArrayToObject(inventoryObject, "items");
for (int i = 0; player->inventory->items[i]; i++)
{
cJSON *item = cJSON_CreateObject();
cJSON_AddItemToArray(itemsArray, item);
cJSON_AddStringToObject(item, "item", player->inventory->items[i]);
cJSON_AddNumberToObject(item, "isEquiped", player->inventory->activeItems[i]);
}
cJSON *npcsArray = cJSON_AddArrayToObject(playerObject, "NPCInfo");
int n = getNpcNumber();
for (int i = 0; i < n; i++)
{
cJSON *npc = cJSON_CreateObject();
cJSON_AddItemToArray(npcsArray, npc);
cJSON_AddNumberToObject(npc, "npc_id", i);
cJSON_AddNumberToObject(npc, "quest_lvl", player->NPCInfo[i][QUEST_LVL]);
cJSON_AddNumberToObject(npc, "quest_status", player->NPCInfo[i][QUEST_STATUS]);
cJSON_AddNumberToObject(npc, "relationship", player->NPCInfo[i][RELATIONSHIP]);
// printf("\nNPC %d: quest_lvl=%d, quest_status=%d, relationship=%d\n", i, player->NPCInfo[i][QUEST_LVL], player->NPCInfo[i][QUEST_STATUS], player->NPCInfo[i][RELATIONSHIP]);
}
cJSON *activeQuestArray = cJSON_AddArrayToObject(playerObject, "activeQuests");
for (int i = 0; player->activeQuests[i]; i++)
{
cJSON *quest = cJSON_CreateObject();
cJSON_AddItemToArray(activeQuestArray, quest);
cJSON_AddStringToObject(quest, "QuestID", player->activeQuests[i]);
}
char *fileName = createFileName(player->id);
// printf("%s", fileName);
FILE *file = fopen(fileName, "w");
if (file == NULL) {
fprintf(stderr, "Failed to open file for reading\n");
return;
}
char *jsonData = cJSON_Print(root);
fprintf(file, "%s", jsonData);
fclose(file);
free(jsonData);
cJSON_Delete(root);
}
void freePlayer(Player *player)
{
if (player == NULL) {
return; // Nothing to free if player is NULL
}
// Free dynamically allocated members
free(player->id);
free(player->name);
free(player->currentLocation);
// Free Stats structure
free(player->stats);
// Free Inventory structure
if (player->inventory != NULL)
{
// Free items in inventory
for (int i = 0; i < player->inventory->size; i++)
{
free(player->inventory->items[i]);
}
free(player->inventory->items);
free(player->inventory->activeItems);
free(player->inventory);
}
int n=getNpcNumber();
// Free NPCInfo array
if (player->NPCInfo != NULL)
{
for (int i = 0; i<n; i++)
{
free(player->NPCInfo[i]);
}
free(player->NPCInfo);
}
// Free activeQuests array
if (player->activeQuests != NULL)
{
for (int i = 0; player->activeQuests[i] != NULL; i++)
{
free(player->activeQuests[i]);
}
free(player->activeQuests);
}
// Free the player structure itself
free(player);
}
void showPlayerStats(Player *player)
{
printf("\n");
printStory("Player Stats:\n",BYEL,MED);
printFormattedStringWithColorAndDelay("ID: %s\n",BYEL,MED, player->id);
printFormattedStringWithColorAndDelay("Name: %s\n", BYEL,MED,player->name);
printFormattedStringWithColorAndDelay("Level: %d\n", BYEL,MED,player->level);
printFormattedStringWithColorAndDelay("Experience Points: %d\n", BYEL,MED, player->xp);
printFormattedStringWithColorAndDelay("Gold: %d\n", BYEL,MED, player->gold);
printFormattedStringWithColorAndDelay("Current Location: %s\n", BYEL,MED, player->currentLocation);
printFormattedStringWithColorAndDelay("Health Points (HP): %d (100)\n", BYEL,MED, player->stats->HP);
printFormattedStringWithColorAndDelay("Attack (ATK): %d (10)\n", BYEL,MED, player->stats->atk);
printFormattedStringWithColorAndDelay("Defense (DEF): %d (5)\n", BYEL,MED, player->stats->def);
printFormattedStringWithColorAndDelay("Agility (AGI): %d (8)\n", BYEL,MED, player->stats->agi);
printFormattedStringWithColorAndDelay("Strength (STR): %d (12)\n", BYEL,MED, player->stats->str);
printFormattedStringWithColorAndDelay("Dexterity (DEX): %d (9)\n", BYEL,MED, player->stats->dex);
printFormattedStringWithColorAndDelay("Intelligence (INT): %d (6)\n", BYEL,MED, player->stats->intel);
printFormattedStringWithColorAndDelay("Luck: %d (7)\n", BYEL,MED,player->stats->luck);
}
void showPlayerInventory(Player *player)
{
printf("\n");
printStory("Player Inventory:\n", BYEL, MED);
for (int i = 0; player->inventory->items[i] != NULL; i++)
{
if (player->inventory->activeItems[i] == 1)
{
printFormattedStringWithColorAndDelay("%s (Equipped) \n",BYEL,MED, player->inventory->items[i]);
}
else
{
printFormattedStringWithColorAndDelay("%s\n",BYEL,MED, player->inventory->items[i]);
}
}
}
char *createFileName( char *name) {
// Define a constant extension
const char *extension = ".json";
// Calculate the length of the name and extension
size_t nameLength = strlen(name);
size_t extensionLength = strlen(extension);
// Allocate memory for the filename
char *fileName = (char *)malloc(nameLength + extensionLength + 1); // +1 for the null terminator
if (fileName == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// Copy the name to the filename buffer
strcpy(fileName, name);
// Concatenate the extension to the filename
strcat(fileName, extension);
return fileName;
}
Player *loadPlayerData(char *playerID)
{
char *fileName = createFileName(playerID);
// printf("%s", fileName);
FILE *file = fopen(fileName, "r");
if (file == NULL) {
fprintf(stderr, "Failed to open file for reading\n");
return NULL;
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
// printf("\nhi");
char *fileContent = (char *)malloc(fileSize + 1);
if (fileContent == NULL) {
fprintf(stderr, "Memory allocation failed for file content\n");
fclose(file);
return NULL;
}
// printf("\nhi");
fread(fileContent, 1, fileSize, file);
fclose(file);
fileContent[fileSize] = '\0';
// printf("\nhi");
cJSON *root = cJSON_Parse(fileContent);
cJSON *playerObject = cJSON_GetObjectItem(root, "player");
// printf("\nhi");
Player *player = createNewPlayer(playerID); // Create a new player structure
// printf("\nhi");
// Load player properties from JSON object
strcpy(player->id, cJSON_GetObjectItem(playerObject, "id")->valuestring);
strcpy(player->name, cJSON_GetObjectItem(playerObject, "name")->valuestring);
player->level = cJSON_GetObjectItem(playerObject, "level")->valueint;
player->wtdLevel = cJSON_GetObjectItem(playerObject, "wtdLevel")->valueint;
player->xp = cJSON_GetObjectItem(playerObject, "xp")->valueint;
player->gold = cJSON_GetObjectItem(playerObject, "gold")->valueint;
player->currentLocation = strdup(cJSON_GetObjectItem(playerObject, "currentLocation")->valuestring);
// Load player stats from JSON object
cJSON *statsObject = cJSON_GetObjectItem(playerObject, "stats");
player->stats->HP = cJSON_GetObjectItem(statsObject, "HP")->valueint;
player->stats->atk = cJSON_GetObjectItem(statsObject, "atk")->valueint;
player->stats->def = cJSON_GetObjectItem(statsObject, "def")->valueint;
player->stats->agi = cJSON_GetObjectItem(statsObject, "agi")->valueint;
player->stats->str = cJSON_GetObjectItem(statsObject, "str")->valueint;
player->stats->dex = cJSON_GetObjectItem(statsObject, "dex")->valueint;
player->stats->intel = cJSON_GetObjectItem(statsObject, "intel")->valueint;
player->stats->luck = cJSON_GetObjectItem(statsObject, "luck")->valueint;
cJSON *inventoryObject = cJSON_GetObjectItem(playerObject, "inventory");
player->inventory->size = cJSON_GetObjectItem(inventoryObject, "size")->valueint;
cJSON *itemsArray = cJSON_GetObjectItem(inventoryObject, "items");
cJSON *item = NULL;
int i = 0;
cJSON_ArrayForEach(item, itemsArray) {
player->inventory->items[i] = strdup(cJSON_GetObjectItem(item, "item")->valuestring);
player->inventory->activeItems[i] = cJSON_GetObjectItem(item, "isEquiped")->valueint;
i++;
}
player->inventory->items[i] = NULL;
cJSON *npcsArray = cJSON_GetObjectItem(playerObject, "NPCInfo");
cJSON *npc = NULL;
i = 0;
cJSON_ArrayForEach(npc, npcsArray) {
player->NPCInfo[i][QUEST_LVL] = cJSON_GetObjectItem(npc, "quest_lvl")->valueint;
player->NPCInfo[i][QUEST_STATUS] = cJSON_GetObjectItem(npc, "quest_status")->valueint;
player->NPCInfo[i][RELATIONSHIP] = cJSON_GetObjectItem(npc, "relationship")->valueint;
i++;
}
cJSON *activeQuestArray = cJSON_GetObjectItem(playerObject, "activeQuests");
cJSON *quest = NULL;
i = 0;
int n = cJSON_GetArraySize(activeQuestArray);
player->activeQuests = (char **)malloc(sizeof(char *) * (n + 1));
cJSON_ArrayForEach(quest, activeQuestArray) {
player->activeQuests[i] = strdup(cJSON_GetObjectItem(quest, "QuestID")->valuestring);
i++;
}
player->activeQuests[i] = NULL;
cJSON_Delete(root);
free(fileContent);
// printf("\nplayer data loaded successfully.");
return player;
}
Player *gameInitializer(char *PlayerID) // This works checked
{
// asks player wheather to start a new game
// (warns that load game will be deleted)
// or to load game and if the file does not exist
// it gives a prompt telling player to start a new game.
// It also makes a new json file or load json files based on
// player choise and returns a player variable
int input;
Player *player=NULL;
printStory("\nEnter (1) to Start a New Game",YEL,20);
printStory("\nEnter (2) Load an Old Game",YEL,20);
printStory("\nEnter a Choice : ",YEL,20);
scanf("%d", &input);
flushInputBuffer();
if (input == 1)
{
// Start a new game
// Create a new player object and initialize its properties
char *story = "\n In the aftermath of war, Marcus Agrippa sought solace in the tranquil embrace of his farm,\n where the earth's gentle rhythm promised respite from the horrors of battle.\n But peace was fleeting, shattered by the cruel hand of fate. Returning home one fateful eve,\n Marcus found his sanctuary engulfed in flames, a grim omen of the tragedy that awaited.\n Amidst the smoldering ruins, he discovered the lifeless bodies of his beloved wife and son,\n their innocence consumed by the merciless flames of war. With nothing left to lose,\n Marcus's heart ignited with a searing thirst for vengeance.\n His once-pure hands now hardened into fists, clenched in defiance against\n the darkness that had stolen his loved ones. Guided by the flickering flames\n of retribution, Marcus embarked on a perilous journey, his path illuminated by\n the glint of steel and the echoes of his anguished cries. And it was within\n the hallowed walls of the Colosseum, where blood flowed like wine and echoes\n of cheers mingled with the moans of the fallen, that Marcus's quest for justice began.\n In the heart of the arena, amidst the clash of swords and the roar of the crowd,\n Marcus Agrippa emerged as a beacon of hope, his resolve unyielding against the tide of despair.\n And as he faced his first opponent,\n the world watched in awe as a new legend was born: the legend of Marcus Agrippa, the Avenger.\n";
printStory(story, BMAG,LOW);
player = createNewPlayer(PlayerID);
// savePlayerData(player);
// showPlayerStats(player);
// showPlayerInventory(player);
}
else if (input == 2)
{
// printf("into load if");
// printStory("The Load Functionality is currently unavailable",31,0,100);
// return NULL;
// Load an old game
player = loadPlayerData(PlayerID);
if (player == NULL)
{
printf("No saved game found. Starting a new game...\n");
// printf("Invalid choice. Starting a new game...\n");
player = createNewPlayer(PlayerID);
savePlayerData(player);
}
else
{
printStory("Old game loaded successfully\n",YEL,LOW);
}
}
else
{
printf("Invalid choice. Starting a new game...\n");
player = createNewPlayer(PlayerID);
savePlayerData(player);
}
return player;
}
void selectState(Player *player,int *state) // This works checked
{
char input;
// printStory("\nEnter (n/N) to Choose Navigation Mode",GRN,MED);
// printStory("\nEnter (i/I) to Choose Interaction Mode",CYN,MED);
// printStory("\nEnter (q/Q) to Choose Quest Mode",MAG,MED);
printStory("\nChoose a Mode to continue your Journey (n/i/q) : ",YEL,LOW);
// getchar();
input=getc(stdin);
flushInputBuffer();
// if(getchar()!='\n')
// {
// printStory("\n\nLooks like you have stumbled and fell into Shadow. Hope you make it through the next time",WHT,BOLD,HIG);
// *state=-1;
// }
// else
if(input=='n'||input=='N') *state=0;
else if(input=='i'||input=='I') *state=1;
else if(input=='q'||input=='Q') *state=2;
else if(input=='e'||input=='E') *state=-1;
else
if(input=='p'||input=='P')
{
showPlayerStats(player);
showPlayerInventory(player);
printStory("Default state set to Navigation",YEL,LOW);
return;
}
else
{
printStory("\n\nLooks like you have stumbled and fell into Shadow. Hope you make it through the next time",BMAG,HIG);
*state=-1;
}
}
void navigationMode(Player *player, int *state) // This works checked BUT ADD SOME INTERACTIVE ELEMENTS ___VERY BLAND
{
// printf("\nEntered navigation");
FILE *file = fopen("navigations.json", "r");
if (file == NULL)
{
fprintf(stderr, "Failed to open file for reading\n");
return;
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *fileContent = (char *)malloc(fileSize + 1);
if (fileContent == NULL)
{
fprintf(stderr, "Memory allocation failed for file content\n");
fclose(file);
return;
}
size_t bytesRead = fread(fileContent, 1, fileSize, file);
// if (bytesRead != fileSize) {
// printf("Error reading file.\n");
// fclose(file);
// free(fileContent);
// return ;
// }
fileContent[fileSize] = 0;
fclose(file);
cJSON *root = cJSON_Parse(fileContent);
if (root == NULL)
{
printStory("Error parsing JSON data.\n",BWHT,HIG);
free(fileContent);
return;
}
cJSON *currentLocationInJson = cJSON_GetObjectItem(root, "locations");
cJSON *children = cJSON_GetObjectItem(currentLocationInJson, "children");
char *token_prev = NULL;
char *duplicate = strdup(player->currentLocation); // IMP BECAUSE STRTOK PUTS NULL WHERE IT FINDS LIMITERS
char *token = strtok(duplicate, "/");
while (token != NULL)
{
cJSON *child = children->child;
while (child != NULL)
{
cJSON *childName = cJSON_GetObjectItem(child, "name");
if (strcmp(token, childName->valuestring) == 0)
{
children = cJSON_GetObjectItem(child, "children");
currentLocationInJson = child;
break;
}
child = child->next;
}
token_prev = strdup(token);
token = strtok(NULL, "/");
}
char input;
// printf("HU");
// flushInputBuffer();
if (strcmp(token_prev, "WORLD") == 0)
{
// printf("\nin if");
for (int i = 0; i < cJSON_GetArraySize(children); i++)
{
cJSON *child = cJSON_GetArrayItem(children, i);
printFormattedStringWithColorAndDelay("\nEnter (%d) to go to %s",GRN,LOW, i + 1, cJSON_GetObjectItem(child, "name")->valuestring);
}
// printStory("\nOr Enter (i/I) for Interaction Mode",);
// printf("\nEnter (q/Q) for Quest Mode");
// printf("\nEnter (e/E) to Exit the Game");
printStory("\nEnter your choice : ",YEL,LOW);
// getchar();
// flushInputBuffer();
input = getc(stdin);
flushInputBuffer();
// printf("c=%c,",input);
// while (input <= '0' || input > (cJSON_GetArraySize(children) + '0'))
// {
// // printf("\nin loop");
// if (input == 'e' || input == 'E')
// {
// *state = -1;
// return;
// }
// if (input == 'i' || input == 'I')
// {
// *state = 1;
// return;
// }
// if (input == 'q' || input == 'Q')
// {
// *state = 2;
// return;
// }
// printf("\nSuch a place doesn't exist. Enter a Valid Location!");
// printf("\nChoose a Location to move to : ");
// getchar();
// input = getc(stdin);
// // flushInputBuffer();
// }
if (input == 'e' || input == 'E')
{
*state = -1;
return;
}
if(input=='p'||input=='P')
{
showPlayerStats(player);
showPlayerInventory(player);
return;
}
if (input == 'i' || input == 'I')
{
*state = 1;
return;
}
if (input == 'q' || input == 'Q')
{
*state = 2;
return;
}
}
else
{
// printf("\nin else");
printFormattedStringWithColorAndDelay("\nEnter (0) to go out of %s",GRN,LOW, token_prev);
for (int i = 0; i < cJSON_GetArraySize(children); i++)
{
cJSON *child = cJSON_GetArrayItem(children, i);
// printf("\nEnter (%d) to go to %s", (i + 1), cJSON_GetObjectItem(child, "name")->valuestring);
printFormattedStringWithColorAndDelay("\nEnter (%d) to go to %s",GRN,LOW, i + 1, cJSON_GetObjectItem(child, "name")->valuestring);
}
// printf("\n");
// printf("\nOr Enter (i/I) for Interaction Mode");
// printf("\nEnter (q/Q) for Quest Mode");
// printf("\nEnter (e/E) to Exit the Game");
printf("\n");
printStory("\nEnter your choice : ",YEL,LOW);
// getchar();
// flushInputBuffer();
input = getc(stdin);
flushInputBuffer();
// printf("\nc=%c",input);
// while (input < '0' || input > (cJSON_GetArraySize(children) + '0'))
// {
// printf("\nin loop");
// if (input == 'e' || input == 'E')
// {
// *state = -1;
// return;
// }
// if (input == 'i' || input == 'I')
// {
// *state = 1;
// return;
// }
// if (input == 'q' || input == 'Q')
// {
// *state = 2;
// return;
// }
// printf("\nSuch a place doesn't exist. Enter a Valid Location!");
// printf("\nChoose a Location to move to : ");
// getchar();
// input = getc(stdin);
// // flushInputBuffer();
// printf("\nc=%c",input);
// }
// printf("%c",input);
if (input == 'e' || input == 'E')
{
*state = -1;
return;
}
if(input=='p'||input=='P')
{
showPlayerStats(player);
showPlayerInventory(player);
return;
}
if (input == 'i' || input == 'I')
{
*state = 1;
return;
}
if (input == 'q' || input == 'Q')
{
*state = 2;
return;
}
}
// printf("%c",input);
if(input > '0' && input <= (cJSON_GetArraySize(children) + '0'))
{
// printf("\nin if2");
cJSON *child = cJSON_GetArrayItem(children, input - '1');
// printf("\n%s",cJSON_GetObjectItem(child,"name")->valuestring);
strcat(player->currentLocation, "/");
strcat(player->currentLocation, cJSON_GetObjectItem(child, "name")->valuestring);
}
else
if(input=='0')
{
// printf("\nin else2");
char *str = player->currentLocation;
int j = 0;
for (int i = 0; str[i]; i++)
{
if (str[i] == '/')
j = i;
}
if (j)
str[j] = 0;
}
else
{
printStory("\nYou walked into an dark and lonely Alley! ",BYEL,MED);
printStory("\nSomething or Someone zapped at you from behind. Hope you be careful next time.\n",BRED,MED);
gameLost(player,state);
return;
}
// printf("\n%s",player->currentLocation);
}
// Function to add activated quest to activeQuests array of strings
void addActiveQuest(Player *player, char *questID) // This works checked (A)
{
// Find the number of active quests
int numActiveQuests = 0;
while (player->activeQuests[numActiveQuests] != NULL)
{
// printf("+\n");
numActiveQuests++;
}
// printf("Number of active quests %d\n", numActiveQuests);
// Allocate memory for the new quest ID string
char *newQuestID = strdup(questID);
if (newQuestID == NULL)
{
// Handle memory allocation error
printStory("Error: Memory allocation failed\n",BWHT,HIG);
return;
}
// Reallocate memory for the activeQuests array to accommodate the new quest ID
player->activeQuests = (char **)realloc(player->activeQuests, (numActiveQuests + 2) * sizeof(char *));
if (player->activeQuests == NULL)
{
// Handle memory reallocation error
printStory("Error: Memory reallocation failed\n",BWHT,HIG);
free(newQuestID); // Free the allocated memory for the new quest ID
return;
}
// Add the new quest ID to the activeQuests array
player->activeQuests[numActiveQuests] = newQuestID;
player->activeQuests[numActiveQuests + 1] = NULL; // Null-terminate the array
// printf("Active Quests: ");
// for (int i = 0; i < numActiveQuests + 1; i++)
// {
// printf("%s ", player->activeQuests[i]);
// }
printf("\n");
}
// Function to find quest level
int getQuestLevel(Player *player, char *npc, char *questID) // This works checked (A)
{
// Find NPC ID
int npcID = getNPCID(npc);
// Find quest level in NPCInfo array
int questLevel = -1; // Initialize to an invalid value
if (npcID >= 0)
{
// Check if questID exists for the NPC
char *npcQuestID = getQuestID(player, npc);
if (npcQuestID != NULL && strcmp(npcQuestID, questID) == 0)
{
// Get quest level from NPCInfo array
questLevel = player->NPCInfo[npcID][0];
}
}
return questLevel;
}
// Function to activate quest
void activateQuest(Player *player, char *npc, char *NPCQuestID) // This works checked (A)
{
// Assume NPCInfo is properly initialized and contains valid data
// Find the NPC ID based on the given NPC name
int npcID = getNPCID(npc);
// Find the index corresponding to the NPC ID in NPCInfo