forked from lakshyajain-0291/Vindication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICS_Project.c
More file actions
1746 lines (1497 loc) · 54.3 KB
/
ICS_Project.c
File metadata and controls
1746 lines (1497 loc) · 54.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
#include"ICS_Project.h"
// #include"cJSON.c"
int getNpcNumber()//This works checked
{
FILE *file=fopen("characters.json","r");
if(file==NULL)
{
fprintf(stderr,"Failed to open file 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) {
printf("Error parsing JSON data.\n");
free(fileContent);
return -1;
}
cJSON *charactersArray=cJSON_GetObjectItem(root,"characters");
return cJSON_GetArraySize(charactersArray);
}
//FROM world.c
void delay(int milliseconds) {
clock_t start_time = clock();
while (clock() < start_time + milliseconds * CLOCKS_PER_SEC / 1000);
}
void printStory(const char *sentence,int color,int style) {
// Print color code
printf("\033[%d;%dm", style,color);
// 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
delay(50); // Adjust delay as needed (50 milliseconds in this example)
}
// Reset color to default
printf("\033[0m");
}
int Falconry(Player *player){return 0;}
// 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},
// {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) {
// return functions[i].func(); // Call the function associated with the input string
// }
// }
// printf("No function found for input: %s\n", gameName);
// }
Player *createNewPlayer(char *playerID)//This works checked
{
Player *newPlayer = (Player*)malloc(sizeof(Player));
if (newPlayer == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->id=(char*)malloc(MAX_PLAYER_ID_LENGTH);
if (newPlayer->id == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->name=(char*)malloc(25);
if (newPlayer->name == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->stats = (Stats*)malloc(sizeof(Stats));
if (newPlayer->stats == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->inventory = (Inventory*)malloc(sizeof(Inventory));
if (newPlayer->inventory == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->inventory->items=(char**)malloc(sizeof(char*)*11); //initially only one string which will be NULL
if (newPlayer->inventory->items == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->inventory->activeItems=(int*)calloc(sizeof(int),11); // Equal to items and has 0/1/-1 value(equiped or not or null)
if (newPlayer->inventory->items == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->currentLocation = (char*)malloc(sizeof(char)*100);
if (newPlayer->currentLocation == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newPlayer->activeQuests=(char**)malloc(sizeof(char*)); //initially only one string which will be NULL
if (newPlayer->inventory->items == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
int numOfNpcs=getNpcNumber();//reading chharcters.json for no of npcs
newPlayer->NPCInfo=(int**)malloc(sizeof(int*)*numOfNpcs);
if (newPlayer->NPCInfo == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
for(int i=0;i<numOfNpcs;i++)
{
newPlayer->NPCInfo[i]=(int*)calloc(sizeof(int),3);
}
// Initialize player properties
strcpy(newPlayer->id, playerID);
strcpy(newPlayer->name, "Gaius Marcus Agrippa");
newPlayer->level = 1;
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;
newPlayer->inventory->size = 10;
strcpy(newPlayer->inventory->items[0],"Gladiator's Sword");
newPlayer->inventory->items[1]=NULL;
newPlayer->inventory->activeItems[0]=0;//not equiped
newPlayer->inventory->activeItems[1]=-1;//NULL
// Initialize inventory items as needed
// For simplicity, let's leave them empty for now
newPlayer->wtdLevel = 0;
newPlayer->xp = 0;
newPlayer->gold = 50;
strcpy(newPlayer->currentLocation,"WORLD/CITY");
// char *token=strtok("WORLD/CITY","/");
// for(int i=0;token!=NULL;i++)
// {
// newPlayer->currentLocation[i]=token;
// token=strtok(NULL,"/");
// }
newPlayer->activeQuests[0]=NULL;
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][RELATONSHIP]);
}
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;
fileName=(char*)malloc(sizeof(char)*50);
strcpy(fileName,player->id);
strcat(fileName,".json");
FILE *file = fopen(fileName, "w");
if (file == NULL) {
fprintf(stderr, "Failed to open file for writing\n");
return;
}
char *jsonData = cJSON_Print(root);
fprintf(file, "%s", jsonData);
fclose(file);
free(jsonData);
cJSON_Delete(root);
}
Player *loadPlayerData(char *playerID)//This works checked
{
int i;
char *fileName;
strcpy(fileName,playerID);
strcat(fileName,".json");
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);
char *fileContent = (char*)malloc(fileSize + 1);
if (fileContent == NULL) {
fprintf(stderr, "Memory allocation failed for file content\n");
fclose(file);
return NULL;
}
fread(fileContent,1,fileSize,file);
fclose(file);
fileContent[fileSize]=0;
cJSON *root=cJSON_Parse(fileContent);
cJSON *playerObject=cJSON_GetObjectItem(root,"player");
Player *player=createNewPlayer(playerID);
// 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;
strcpy(player->currentLocation,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;
i=0;
cJSON_ArrayForEach(item,itemsArray)
{
player->inventory->items[i]=(char*)malloc(50);
strcpy(player->inventory->items[i], 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][RELATONSHIP]= 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]=(char*)malloc(10);
strcpy(player->activeQuests[i],cJSON_GetObjectItem(quest,"QuestID")->valuestring);
i++;
}
cJSON_Delete(root);
free(fileContent);
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;
printf("\nEnter (1) to Start a New Game");
printf("\nEnter (2) Load an Old Game");
printf("\nEnter a Choice : ");
scanf("%d",&input);
if (input == 1)
{
// Start a new game
// Create a new player object and initialize its properties
player=createNewPlayer(PlayerID);
savePlayerData(player);
}
else if (input == 2)
{
// Load an old game
player = loadPlayerData(PlayerID);
if (player == NULL)
{
printf("No saved game found. Starting a new game...\n");
player = createNewPlayer(PlayerID);
savePlayerData(player);
}
else
{
printf("Old game loaded successfully\n");
}
}
else
{
printf("Invalid choice. Starting a new game...\n");
player = createNewPlayer(PlayerID);
savePlayerData(player);
}
return player;
}
void selectState(int *state)//This works checked
{
printf("\nEnter (0) to Choose Navigation Mode");
printf("\nEnter (1) to Choose Interaction Mode");
printf("\nEnter (2) to Choose Quest Mode");
printf("\nChoose a Mode to continue your Journey : ");
scanf("%d",state);
}
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) {
printf("Error parsing JSON data.\n");
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);
printf("\n Enter (%d) to go to %s",i+1,cJSON_GetObjectItem(child,"name")->valuestring);
}
printf("\nOr Enter (i/I) for Interaction Mode");
printf("\nEnter (q/Q) for Quest Mode");
printf("\nEnter (e/E) to Exit the Game");
printf("\nEnter your choice : ");
getchar();
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=='i'||input=='I')
{
*state = 1;
return;
}
if(input=='q' || input=='Q')
{
*state=2;
return;
}
}
else
{
// printf("\nin else");
printf("\nEnter (0) to go out of %s",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);
}
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");
printf("\nEnter your choice : ");
getchar();
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();
}
if(input=='e'||input=='E')
{
*state =-1;
return;
}
if(input=='i'||input=='I')
{
*state = 1;
return;
}
if(input=='q' || input=='Q')
{
*state=2;
return;
}
}
if(input-'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
{
// 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;
}
// 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
printf("Error: Memory allocation failed\n");
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
printf("Error: Memory reallocation failed\n");
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
int npcIndex = npcID; // For simplicity, assuming the NPC ID corresponds to its index in NPCInfo
// Find the Quest level based on the given NPCQuestID
int questLevel = getQuestLevel(player, npc, NPCQuestID);
// Update the quest status of the relevant NPC for the given quest level
player->NPCInfo[npcIndex][1] = 1; // Set quest status to 1 (active)
// Add the activated quest to the activeQuests array
addActiveQuest(player, NPCQuestID);
}
// Function to parse quests.json and get the quest location
char *getQuestLocation(char *NPCQuestID)//This works checked (A)
{
char *location = NULL;
// Read the contents of the quests.json file
FILE *file = fopen("quests.json", "r");
if (file != NULL)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *fileContent = (char *)malloc(fileSize + 1);
if (fileContent != NULL)
{
fread(fileContent, 1, fileSize, file);
fileContent[fileSize] = '\0';
// Parse the JSON content
cJSON *root = cJSON_Parse(fileContent);
if (root != NULL)
{
cJSON *quests = cJSON_GetObjectItem(root, "quests");
if (quests != NULL && cJSON_IsArray(quests))
{
cJSON *quest;
cJSON_ArrayForEach(quest, quests)
{
cJSON *questIDJSON = cJSON_GetObjectItem(quest, "QuestID");
if (questIDJSON != NULL && cJSON_IsString(questIDJSON) && strcmp(questIDJSON->valuestring, NPCQuestID) == 0)
{
// Quest found, get its location
cJSON *questLocation = cJSON_GetObjectItem(quest, "Location");
if (questLocation != NULL && cJSON_IsString(questLocation))
{
location = strdup(questLocation->valuestring);
}
break; // No need to continue searching
}
}
}
cJSON_Delete(root);
}
free(fileContent);
}
fclose(file);
}
return location;
}
// Function to parse quests.json and get the quest description
char *getQuestDescription(char *NPCQuestID)//This works checked (A)
{
char *description = NULL;
// Read the contents of the quests.json file
FILE *file = fopen("quests.json", "r");
if (file != NULL)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *fileContent = (char *)malloc(fileSize + 1);
if (fileContent != NULL)
{
fread(fileContent, 1, fileSize, file);
fileContent[fileSize] = '\0';
// Parse the JSON content
cJSON *root = cJSON_Parse(fileContent);
if (root != NULL)
{
cJSON *quests = cJSON_GetObjectItem(root, "quests");
if (quests != NULL && cJSON_IsArray(quests))
{
cJSON *quest;
cJSON_ArrayForEach(quest, quests)
{
cJSON *questIDJSON = cJSON_GetObjectItem(quest, "QuestID");
if (questIDJSON != NULL && cJSON_IsString(questIDJSON) && strcmp(questIDJSON->valuestring, NPCQuestID) == 0)
{
// Quest found, get its description
cJSON *questDesc = cJSON_GetObjectItem(quest, "Description");
if (questDesc != NULL && cJSON_IsString(questDesc))
{
description = strdup(questDesc->valuestring);
}
break; // No need to continue searching
}
}
}
cJSON_Delete(root);
}
free(fileContent);
}
fclose(file);
}
return description;
}
void giveQuestReward(Player *player, char *questID)//This works checked (A)
{
// Read the contents of the quests.json file
FILE *file = fopen("quests.json", "r");
if (file != NULL)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *fileContent = (char *)malloc(fileSize + 1);
if (fileContent != NULL)
{
fread(fileContent, 1, fileSize, file);
fileContent[fileSize] = '\0';
// Parse the JSON content
cJSON *root = cJSON_Parse(fileContent);
if (root != NULL)
{
cJSON *quests = cJSON_GetObjectItem(root, "quests");
if (quests != NULL && cJSON_IsArray(quests))
{
cJSON *quest;
cJSON_ArrayForEach(quest, quests)
{
cJSON *questIDJSON = cJSON_GetObjectItem(quest, "QuestID");
if (questIDJSON != NULL && cJSON_IsString(questIDJSON) && strcmp(questIDJSON->valuestring, questID) == 0)
{
// Quest found, get its reward
cJSON *reward = cJSON_GetObjectItem(quest, "Reward");
if (reward != NULL && cJSON_IsObject(reward))
{
// Update player's attributes based on the reward
cJSON *xp = cJSON_GetObjectItem(reward, "xp");
cJSON *gold = cJSON_GetObjectItem(reward, "gold");
cJSON *item = cJSON_GetObjectItem(reward, "item");
cJSON *relationship = cJSON_GetObjectItem(reward, "relationship");
if (xp != NULL && cJSON_IsNumber(xp))
{
player->xp += xp->valueint;
}
if (gold != NULL && cJSON_IsNumber(gold))
{
player->gold += gold->valueint;
}
if (item != NULL && cJSON_IsString(item))
{
// Append the item to the inventory
if (player->inventory->items == NULL)
{
player->inventory->items = (char **)malloc(sizeof(char *));
player->inventory->size = 1;
}
else
{
player->inventory->items = (char **)realloc(player->inventory->items, (player->inventory->size + 1) * sizeof(char *));
player->inventory->size++;
}
player->inventory->items[player->inventory->size - 1] = strdup(item->valuestring);
}
if (relationship != NULL && cJSON_IsNumber(relationship))
{
// Update the relationship with the NPC
int npcID = atoi(strtok(questID, "_"));
player->NPCInfo[npcID][2] += relationship->valueint;
}
// Increase quest level and reset quest status
int npcID = atoi(strtok(questID, "_"));
player->NPCInfo[npcID][0]++; // Increase quest level
player->NPCInfo[npcID][1] = 0; // Reset quest status to 0 (incomplete/inactive)
}
break; // No need to continue searching
}
}
}
cJSON_Delete(root);
}
free(fileContent);
}
fclose(file);
}
}
// Function to check if there is any active quest for the NPC
bool anyActiveQuest(Player *player, char *npc)//This works checked (A)
{
char *questID = getQuestID(player, npc);
// printf("in anyActiveQuest, questID = %s\n", questID);
// Check if the NPC has any active quests
for (int i = 0; player->activeQuests[i] != NULL; i++)
{
// printf("Active = %s, Here = %s", player->activeQuests[i], questID);
if (strcmp(player->activeQuests[i], questID) == 0)
{
// Active quest found
// printf("TRUE\n");
return true;
}
}
// No active quest for the NPC
// printf("FALSE\n");
return false;
}
// Function to check if there is any quest to submit to the NPC
bool anyQuesttoSubmit(Player *player, char *npcName)//This works checked(A)
{
// printf("Debug anyQuesttoSubmit1\n");
int npcID = getNPCID(npcName);
if (npcID == -1)
{
// NPC not found
return false;
}
// Get the quest ID for the NPC
char *questID = getQuestID(player, npcName);
if (questID == NULL)
{
// Quest ID not found
return false;
}
// Check if the quest status is "to-submit" (status = 2)
if (player->NPCInfo[npcID][1] == 2)
{