-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·580 lines (468 loc) · 15.7 KB
/
main.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
/*******************************************************************************
Filename: main.c
Author: David C. Drake (https://davidcdrake.com)
Description: Main C file for "Words of Power": a text-based fantasy RPG
emphasizing verbal spell-casting through the use of magic words.
*******************************************************************************/
#include "wop.h"
/*******************************************************************************
Function: main
Description: Main function for the "Words of Power" RPG.
Inputs: None.
Outputs: Number of errors encountered.
*******************************************************************************/
int main(void) {
srand((int) time(0));
g_world_exists = false;
g_player_has_quit = false;
PrintString("\nWelcome to WORDS OF POWER: a text-based fantasy RPG by David "
"C. Drake!\n\0");
while (!g_player_has_quit) {
if (!g_world_exists) {
HandleMainMenuInput();
} else if (CheckStatus()) {
DescribeSituation();
HandleStandardOptionsInput();
}
}
if (g_world_exists) {
DestroyWorld();
}
printf("Farewell!\n");
#if DEBUG
printf("End of main reached.\n");
#endif
return 0;
}
/*******************************************************************************
Function: HandleMainMenuInput
Description: Displays main menu options and does some handling of input.
Inputs: None.
Outputs: None.
*******************************************************************************/
void HandleMainMenuInput(void) {
char input;
bool repeat_options;
do {
repeat_options = false;
printf("Main Menu:\n"
"[N]ew Game\n"
"[L]oad Game\n"
"[Q]uit\n");
GetCharInput(&input);
switch (input) {
case 'N': // New Game
if (g_world_exists) {
DestroyWorld();
}
CreateWorld();
InitializeCharacter(&g_player, PLAYER, g_world[ILLARUM_SCHOOL]);
break;
case 'L': // Load Game
if (g_world_exists) {
DestroyWorld();
}
//LoadGame();
printf("Sorry, no save files found.\n\n");
break;
case 'Q': // Quit
if (g_world_exists) {
DestroyWorld();
}
g_player_has_quit = true;
break;
default:
printf("Invalid response.\n\n");
repeat_options = true;
break;
}
}while (repeat_options);
}
/*******************************************************************************
Function: HandleStandardOptionsInput
Description: Prints the standard (i.e., non-combat) player options and processes
corresponding player input.
Inputs: None.
Outputs: None.
*******************************************************************************/
void HandleStandardOptionsInput(void) {
char input;
bool repeat_options = true;
while (repeat_options) {
printf("What do you want to do?\n"
"[T]alk\n"
"[S]earch\n"
"[U]se an Item\n"
"[C]ast a Spell\n"
"[A]ttack\n"
"[V]iew Inventory and Status\n"
"[M]ove to Another Location\n"
"[Q]uit (Return to Main Menu)\n");
GetCharInput(&input);
switch (input) {
case 'T': // Talk
repeat_options = !HandleTalkMenuInput();
break;
case 'S': // Search
SearchLocation(g_world[g_player.location]);
break;
case 'U': // Use an Item
repeat_options = !HandleItemMenuInput();
break;
case 'C': // Cast a Spell
repeat_options = !HandleSpellMenuInput();
break;
case 'A': // Attack
repeat_options = !HandleAttackMenuInput();
break;
case 'M': // Move to Another Location
repeat_options = !HandleMovementMenuInput();
break;
case 'V': // View Inventory and Status
DisplayCharacterData(&g_player);
break;
case 'Q': // Quit
repeat_options = !GetExitConfirmation();
break;
default:
printf("Invalid response.\n\n");
break;
}
}
}
/*******************************************************************************
Function: SaveGame
Description: Attempts to save game data to a file of a given name in the same
directory as the WoP executable. If the file doesn't exist, it is
created; otherwise, it is overwritten.
Inputs: filename - Name of the file to be created or overwritten.
Outputs: Returns 'true' if game data are successfully saved.
*******************************************************************************/
bool SaveGame(const char *filename) {
FILE *outfile = fopen(filename, "wb");
if (outfile) {
fwrite(&g_player, sizeof(game_character_t), 1, outfile);
fclose(outfile);
return true;
}
PRINT_ERROR_MESSAGE;
return false;
}
/*******************************************************************************
Function: LoadGame
Description: Attempts to load game data from a file of a given name, which must
exist in the same directory as the WoP executable.
Inputs: filename - Name of the file to be loaded.
Outputs: Returns 'true' if game data are successfully loaded.
*******************************************************************************/
bool LoadGame(const char *filename) {
FILE *infile = fopen(filename, "rb");
if (infile) {
fread(&g_player, sizeof(game_character_t), 1, infile);
fclose(infile);
return true;
}
PRINT_ERROR_MESSAGE;
return false;
}
/*******************************************************************************
Function: CreateWorld
Description: Allocates memory for, and initializes, location structs for all
game locations as well as game character structs for each
location's default inhabitants. Also sets other global variables
to their default starting values.
Inputs: None.
Outputs: The number of failed location initializations.
*******************************************************************************/
int CreateWorld(void) {
int i, errors = 0;
#if DEBUG
printf("Creating world...\n\n");
#endif
if (g_world_exists) {
PRINT_ERROR_MESSAGE;
errors++;
DestroyWorld();
}
// Initialize each location (including its inhabitants):
for (i = 0; i < NUM_LOCATION_IDS; i++) {
g_world[i] = malloc(sizeof(location_t));
if (g_world[i] != NULL) {
errors += InitializeLocation(g_world[i], i);
} else {
PRINT_ERROR_MESSAGE;
exit(1);
}
}
// Set status of all missions to CLOSED:
for (i = 0; i < NUM_MISSION_TYPES; i++) {
g_missions[i] = CLOSED;
}
// Initialize player allegiances:
for (i = 0; i < NUM_GROUP_TYPES; i++) {
g_allegiances[i] = INDIFFERENT;
}
// Initialize player's "kill" history:
for (i = 0; i < NUM_GC_TYPES; i++) {
g_num_kills[i] = 0;
}
// Initialize remaining global variables:
g_num_secrets_found = 0;
g_world_exists = true;
return errors;
}
/*******************************************************************************
Function: DestroyWorld
Description: Deallocates all memory set aside for location structs and their
inhabitants.
Inputs: None.
Outputs: The number of errors detected.
*******************************************************************************/
int DestroyWorld(void) {
int i, errors = 0;
if (g_world_exists == false) {
PRINT_ERROR_MESSAGE;
errors++;
}
#if DEBUG
printf("Destroying world...\n\n");
#endif
for (i = 0; i < NUM_LOCATION_IDS; i++) {
if (g_world[i] != NULL) {
while (g_world[i]->inhabitants != NULL) {
if (DeleteInhabitant(g_world[i], g_world[i]->inhabitants) == FAILURE) {
errors++;
}
}
free(g_world[i]);
} else {
errors++;
}
}
for (i = 0; i < MAX_ENEMIES; i++) {
g_enemies[i] = NULL;
}
while (g_player.next != NULL) {
if (DeleteCompanion(g_player.next) == FAILURE) {
errors++;
}
}
if (g_player.summoned_creature != NULL) {
if (DeleteCreatureSummonedBy(&g_player) == FAILURE) {
errors++;
}
}
g_world_exists = false;
return errors;
}
/*******************************************************************************
Function: GetExitConfirmation
Description: Asks for confirmation of intent to exit to the main menu. If
confirmed, memory is freed via DestroyWorld.
Inputs: None.
Outputs: true if the player confirms they want to exit to the main menu.
*******************************************************************************/
bool GetExitConfirmation(void) {
char input;
do {
printf("Are you sure you want to exit to the main menu (and lose unsaved "
"data)? (Y/N) ");
GetCharInput(&input);
if (input == 'Y') {
DestroyWorld();
return true;
} else if (input != 'N') {
printf("Invalid response.\n\n");
}
}while (input != 'N');
return false;
}
/*******************************************************************************
Function: RandomInt
Description: Returns a random integer between specified values (inclusive).
Inputs: low - Lowest possible return value.
high - Highest possible return value.
Outputs: A randomly generated integer between "low" and "high" (inclusive).
*******************************************************************************/
int RandomInt(int low, int high) {
int temp;
if (low > high) {
temp = low;
low = high;
high = temp;
PRINT_ERROR_MESSAGE;
}
return rand() % (high - low + 1) + low;
}
/*******************************************************************************
Function: RandomBool
Description: Returns a random boolean value.
Inputs: None.
Outputs: 'true' or 'false'
*******************************************************************************/
int RandomBool(void) {
return rand() % 2;
}
/*******************************************************************************
Function: GetCharInput
Description: Takes in one character entered by the user, converts it to
uppercase, and flushes the input stream.
Inputs: c - Pointer to the variable that will store the input.
Outputs: Returns the character read in from the user, converted to
uppercase.
*******************************************************************************/
char GetCharInput(char *c) {
scanf(" %c", c);
FlushInput();
printf("\n");
*c = toupper(*c);
return *c;
}
/*******************************************************************************
Function: GetIntInput
Description: Reads in one integer, then repeats until an integer within given
parameters is read.
Inputs: i - Pointer to the variable that will store the input.
low - Lowest acceptable value.
high - Highest acceptable value.
Outputs: Returns the input value stored in "*i".
*******************************************************************************/
int GetIntInput(int *i, int low, int high) {
int temp;
bool repeat;
if (low > high) {
temp = low;
low = high;
high = temp;
PRINT_ERROR_MESSAGE;
}
do {
repeat = false;
scanf("%d", i);
FlushInput();
if (*i < low || *i > high) {
printf("Invalid response. Enter a number between %d and %d: ", low, high);
repeat = true;
}
}while (repeat);
printf("\n");
return *i;
}
/*******************************************************************************
Function: GetStrInput
Description: Takes in a string of one or more characters entered by the user.
Inputs: str - Pointer to the string that will store input.
n - Maximum number of characters to read in (if greater than
SHORT_STR_LEN or negative, it will be set to SHORT_STR_LEN).
Outputs: Returns a pointer to the string where the input is stored.
*******************************************************************************/
char *GetStrInput(char *str, int n)
{
int length;
if (n < 0 || n > (SHORT_STR_LEN + 1)) {
PRINT_ERROR_MESSAGE;
if (n > SHORT_STR_LEN) {
n = SHORT_STR_LEN;
}
}
fgets(str, n, stdin);
length = strlen(str);
if (length > 0 && str[length - 1] == '\n') {
str[length - 1] = '\0';
}
printf("\n");
return str;
}
/*******************************************************************************
Function: PrintString
Description: Prints a given string according to the maximum characters per
line, followed by a new line character. (Assumes the string is
NULL-terminated and contains no tabs. If no NULL-terminator is
encountered, assumes the size of the string is equal to
LONG_STR_LEN.)
Inputs: str - The string to be printed, which must be NULL-terminated.
Outputs: None.
*******************************************************************************/
void PrintString(char *str) {
int i, last_blank_space_index = 0, current_line_length = 0;
static char output[LONG_STR_LEN + 1];
strcpy(output, str);
for (i = 0; i < LONG_STR_LEN && output[i] != '\0'; i++) {
current_line_length++;
if (output[i] == ' ') {
last_blank_space_index = i;
}
if (output[i] == '\n') {
last_blank_space_index = 0;
current_line_length = 0;
} else if (current_line_length == MAX_LINE_LENGTH) {
if (last_blank_space_index == 0) {
output[i] = '\n';
current_line_length = 0;
} else {
i = last_blank_space_index;
output[i] = '\n';
last_blank_space_index = 0;
current_line_length = 0;
}
}
}
if (i == LONG_STR_LEN) {
output[i] = '\0';
}
printf("%s\n", output);
}
/*******************************************************************************
Function: Capitalize
Description: Capitalizes the first character of a given string.
Inputs: str - The string to be capitalized.
Outputs: Pointer to the capitalized string.
*******************************************************************************/
char *Capitalize(char *str) {
toupper(str[0]);
return str;
}
/*******************************************************************************
Function: AllCaps
Description: Capitalizes every character in a given string (which is assumed to
be NULL-terminated).
Inputs: str - The string to be modified.
Outputs: Pointer to the modified string.
*******************************************************************************/
char *AllCaps(char *str) {
int i;
for (i = 0; str[i] != '\0'; i++) {
toupper(str[i]);
}
return str;
}
/*******************************************************************************
Function: StrContains
Description: Determines whether a given string contains a given character.
Inputs: str - Pointer to the string of interest.
c - Character to search for in the string.
Outputs: true if the string contains "c", otherwise false.
*******************************************************************************/
bool StrContains(char *str, char c) {
int i;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == c) {
return true;
}
}
return false;
}
/*******************************************************************************
Function: FlushInput
Description: Removes remaining input up to and including an end of line or end
of file character. (Also generally an effective way of pausing the
game until the player hits the Enter key.)
Inputs: None.
Outputs: None.
*******************************************************************************/
void FlushInput(void) {
int c;
while((c = getchar()) != '\n' && c != EOF) {
/* discard */ ;
}
}