-
Notifications
You must be signed in to change notification settings - Fork 0
/
combat.c
executable file
·940 lines (843 loc) · 28.2 KB
/
combat.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
/*******************************************************************************
Filename: combat.c
Author: David C. Drake (https://davidcdrake.com)
Description: Functions governing enemies and combat for the text-based RPG
"Words of Power."
*******************************************************************************/
#include "wop.h"
/*******************************************************************************
Function: AddEnemy
Description: Given an already existing game character, adds a pointer to it in
the global "g_enemies" array.
Inputs: p_gc - Pointer to the game character to be added.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int AddEnemy(game_character_t *p_gc) {
int i;
if (p_gc == NULL) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
for (i = 0; i < MAX_ENEMIES; i++) {
if (g_enemies[i] == NULL) {
g_enemies[i] = p_gc;
p_gc->status[IN_COMBAT] = true;
return SUCCESS;
}
}
// If we reach this point, "g_enemies" was full and the add failed.
PRINT_ERROR_MESSAGE;
return FAILURE;
}
/*******************************************************************************
Function: AddRandomEnemy
Description: Creates a new game character and adds a pointer to it in the local
"inhabitants" array as well as the the global "g_enemies" array,
unless no random enemy is associated with the given location, in
which case nothing happens.
Inputs: location - Pointer to the location in which to add a random enemy.
Outputs: SUCCESS if an enemy is added, otherwise FAILURE.
*******************************************************************************/
int AddRandomEnemy(location_t *location) {
game_character_t *newEnemy = NULL;
if (location == NULL) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
switch (location->id) {
case ILLARUM_ENTRANCE:
newEnemy = AddInhabitant(location, THIEF);
break;
case ILLARUM_MARKET:
newEnemy = AddInhabitant(location, THIEF);
break;
case ILLARUM_INN:
newEnemy = AddInhabitant(location, THIEF);
break;
case ILLARUM_SCHOOL:
break;
case ILLARUM_TEMPLE:
break;
case ILLARUM_PALACE:
break;
case ILLARUM_PRISON:
break;
case VENTARRIS_ENTRANCE:
newEnemy = AddInhabitant(location, THIEF);
break;
case VENTARRIS_MARKET:
newEnemy = AddInhabitant(location, THIEF);
break;
case VENTARRIS_INN:
newEnemy = AddInhabitant(location, THIEF);
break;
case VENTARRIS_SCHOOL:
break;
case VENTARRIS_TEMPLE:
break;
case VENTARRIS_PALACE:
break;
case VENTARRIS_PRISON:
break;
case VENTARRIS_DOCKS:
newEnemy = AddInhabitant(location, THIEF);
break;
case PLAINS_NORTH:
newEnemy = AddInhabitant(location, WOLF);
break;
case NORTHERN_FARMS:
break;
case BRILL_OUTSKIRTS:
newEnemy = AddInhabitant(location, THIEF);
break;
case BRILL_MARKET:
newEnemy = AddInhabitant(location, THIEF);
break;
case BRILL_INN:
newEnemy = AddInhabitant(location, THIEF);
break;
case BRILL_DOCKS:
newEnemy = AddInhabitant(location, THIEF);
break;
case PLAINS_SOUTH:
newEnemy = AddInhabitant(location, WOLF);
break;
case SOUTHERN_FARMS:
break;
case SILENT_SAGE_HOME:
break;
case FOREST:
if (g_player.soul <= EVIL) {
newEnemy = AddInhabitant(location, ELF);
} else {
newEnemy = AddInhabitant(location, BEAR);
}
break;
case DRUIDS_GROVE:
break;
case HERMIT_HUT:
break;
case WYNNFAER_ENTRANCE:
if (g_player.soul <= EVIL) {
newEnemy = AddInhabitant(location, ELF);
}
break;
case WYNNFAER_PLAZA:
if (g_player.soul <= EVIL) {
newEnemy = AddInhabitant(location, ELF);
}
break;
case WYNNFAER_PALACE:
if (g_player.soul <= EVIL) {
newEnemy = AddInhabitant(location, ELF);
}
break;
case MOUNTAINS:
newEnemy = AddInhabitant(location, GOBLIN);
break;
case GESHTAL:
if (g_player.soul <= EVIL) {
newEnemy = AddInhabitant(location, BARBARIAN);
}
break;
case TORR_ENTRANCE:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case TORR_MARKET:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case TORR_SCHOOL:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case TORR_TEMPLE:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case TORR_THRONE_ROOM:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case TORR_MINE:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case TORR_VAULT:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case TORR_PRISON:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, DWARF);
}
break;
case GUGGENHOLM_ENTRANCE:
newEnemy = AddInhabitant(location, GOBLIN);
break;
case GUGGENHOLM_MAIN:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, GNOME);
}
break;
case GUGGENHOLM_MINE:
if (g_player.soul <= VERY_EVIL) {
newEnemy = AddInhabitant(location, GNOME);
}
break;
case SWAMP:
newEnemy = AddInhabitant(location, ZOMBIE);
break;
case NECROMANCERS_CIRCLE:
newEnemy = AddInhabitant(location, ZOMBIE);
break;
case ISHTARR_ENTRANCE:
newEnemy = AddInhabitant(location, ZOMBIE);
break;
case ISHTARR_EAST_WING:
newEnemy = AddInhabitant(location, ZOMBIE);
break;
case ISHTARR_WEST_WING:
newEnemy = AddInhabitant(location, ZOMBIE);
break;
case ISHTARR_CENTRAL_TOWER:
newEnemy = AddInhabitant(location, ZOMBIE);
break;
case ISHTARR_DUNGEON:
newEnemy = AddInhabitant(location, ZOMBIE);
break;
case SHORE_NE:
break;
case SHORE_EAST:
break;
case SHORE_SE:
break;
case OCEAN_SURFACE:
break;
case OCEAN_SHALLOW:
newEnemy = AddInhabitant(location, SHARK);
break;
case OCEAN_DEEP:
newEnemy = AddInhabitant(location, GIANT_SQUID);
break;
case OCEAN_TRENCH:
newEnemy = AddInhabitant(location, GIANT_SQUID);
break;
case QUELACENTUS_ENTRANCE:
newEnemy = AddInhabitant(location, MERFOLK_SOLDIER);
break;
case QUELACENTUS_PLAZA:
newEnemy = AddInhabitant(location, MERFOLK_SOLDIER);
break;
case QUELACENTUS_TEMPLE:
newEnemy = AddInhabitant(location, MERFOLK_SOLDIER);
break;
case QUELACENTUS_PALACE:
newEnemy = AddInhabitant(location, MERFOLK_SOLDIER);
break;
default:
PRINT_ERROR_MESSAGE;
break;
}
if (newEnemy != NULL) {
AddEnemy(newEnemy);
return SUCCESS;
}
return FAILURE;
}
/*******************************************************************************
Function: RemoveEnemy
Description: Removes a pointer to a given enemy from the global "g_enemies"
array (does NOT remove the enemy from the "world" or deallocate
associated memory).
Inputs: p_gc - Pointer to the game character to be removed.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int RemoveEnemy(game_character_t *p_gc) {
int i, j;
if (p_gc == NULL) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
p_gc->status[IN_COMBAT] = false;
for (i = 0; i < MAX_ENEMIES; i++) {
if (g_enemies[i] == p_gc) {
g_enemies[i] = NULL;
// If any active pointers existed past "p_gc", shift them left:
for (j = i + 1;
j < MAX_ENEMIES && g_enemies[j] != NULL;
j++, i++) {
g_enemies[i] = g_enemies[j];
g_enemies[j] = NULL;
}
return SUCCESS;
}
}
// If we reach this point, "p_gc" was not found.
PRINT_ERROR_MESSAGE;
return FAILURE;
}
/*******************************************************************************
Function: DeleteEnemy
Description: Removes a pointer to a given enemy from the global "g_enemies"
array, then removes the enemy from the "world" and deallocates
associated memory.
Inputs: p_gc - Pointer to the game character to be removed.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int DeleteEnemy(game_character_t *p_gc) {
if (RemoveEnemy(p_gc) == SUCCESS) {
DeleteInhabitant(g_world[p_gc->location], p_gc);
} else {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
return SUCCESS;
}
/*******************************************************************************
Function: NumberOfEnemies
Description: Returns the number of enemies pointed to by "g_enemies".
Inputs: None.
Outputs: The number of active pointers in "g_enemies".
*******************************************************************************/
int NumberOfEnemies(void) {
int i;
for (i = 0; i < MAX_ENEMIES && g_enemies[i] != NULL; i++)
;
return i;
}
/*******************************************************************************
Function: VisibleEnemies
Description: Returns the number of visible enemies.
Inputs: None.
Outputs: The number of visible enemies.
*******************************************************************************/
int VisibleEnemies(void) {
int i, count = 0;
if (g_player.status[IN_COMBAT] == false) {
PRINT_ERROR_MESSAGE;
return count;
} else {
for (i = 0; i < MAX_ENEMIES && g_enemies[i] != NULL; i++) {
if (g_enemies[i]->status[INVISIBLE] == false) {
count++;
}
}
}
return count;
}
/*******************************************************************************
Function: Combat
Description: Handles combat situations by displaying relevant information,
presenting options, etc.
Inputs: None.
Outputs: Returns the number of enemies remaining (usually 0).
*******************************************************************************/
int Combat(void) {
int i, round = 1;
char input;
bool repeat_options, playerFirst;
game_character_t *p_gc;
if (NumberOfEnemies() == 0) {
PRINT_ERROR_MESSAGE;
return 0;
}
g_player.status[IN_COMBAT] = true;
playerFirst = RandomBool(); // Determine who gets the first round.
do {
printf(" ___________\n_/ENEMY STATS\\_____________________________________"
"____________________________\n");
for (i = 0; i < MAX_ENEMIES; i++) {
if (g_enemies[i] == NULL) {
break;
}
PrintCombatStatus(g_enemies[i]);
if (g_enemies[i]->summoned_creature != NULL) {
printf("%s's summoned creature: ", g_enemies[i]->name);
PrintCombatStatus(g_enemies[i]->summoned_creature);
}
}
printf(" ____________\n_/PLAYER STATS\\___________________________________"
"_____________________________\n");
for (p_gc = &g_player; p_gc != NULL; p_gc = p_gc->next) {
PrintCombatStatus(p_gc);
}
if (g_player.summoned_creature != NULL) {
PrintCombatStatus(g_player.summoned_creature);
}
printf("\n");
if (round > 1 || playerFirst) { // Check initiative only on 1st round.
/* --PLAYER'S TURN-- */
do {
repeat_options = false;
printf("Choose an action:\n"
"[A]ttack\n"
"[C]ast a Spell\n"
"[U]se an Item\n"
"[F]lee\n"
"[Q]uit (Return to Main Menu)\n");
GetCharInput(&input);
switch (input) {
case 'A': // Attack
if (HandleAttackMenuInput() == FAILURE) {
repeat_options = true;
}
break;
case 'C': // Cast a Spell
if (HandleSpellMenuInput() == FAILURE) {
repeat_options = true;
}
break;
case 'U': // Use an Item
if (HandleItemMenuInput() == FAILURE) {
repeat_options = true;
}
break;
case 'F': // Flee
if (g_enemies[0]->type == DUMMY) { // Indicates tutorial mode.
printf("%s: \"Come on, destroy the dummy already!\"\n",
FindInhabitant(ARCHWIZARD_OF_ELEMENTS)->name);
FlushInput();
repeat_options = true;
} else if (RandomInt(1, 3) == 1) {
printf("You have successfully fled.\n");
FlushInput();
while (NumberOfEnemies() > 0) {
RemoveEnemy(g_enemies[0]);
}
g_player.status[IN_COMBAT] = false;
HandleMovementMenuInput();
} else {
printf("Your attempt to escape has failed.\n");
FlushInput();
}
break;
case 'Q': // Quit
if (GetExitConfirmation()) {
return 0;
} else {
repeat_options = true;
}
break;
default:
printf("Invalid response.\n\n");
repeat_options = true;
break;
}
}while (repeat_options);
// Increment "round" here if the NPCs had the initiative:
if (!playerFirst) {
round++;
}
}
/* -- ENEMY'S TURN-- */
if (CheckStatus()) {
for (i = 0; i < NumberOfEnemies(); i++) {
EnemyAI(i);
}
printf("\n");
}
// Increment "round" here if the player had the initiative:
if (playerFirst) {
round++;
}
}while (CheckStatus() && NumberOfEnemies() > 0);
g_player.status[IN_COMBAT] = false;
return NumberOfEnemies();
}
/*******************************************************************************
Function: PrintCombatStatus
Description: Prints combat-relevant information about a given game character.
Inputs: p_gc - Pointer to the game character of interest.
Outputs: None.
*******************************************************************************/
void PrintCombatStatus(game_character_t *p_gc) {
if (p_gc == NULL) {
PRINT_ERROR_MESSAGE;
return;
}
printf("%s (%d/%d", p_gc->name, p_gc->hp, p_gc->max_hp);
// Code for printing status will go here.
printf(")\n");
}
/*******************************************************************************
Function: EnemyAI
Description: Handles enemy NPC decision-making during combat.
Inputs: index - Index value of the active enemy in the "g_enemies" array.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int EnemyAI(int index) {
int i;
bool actionPerformed = false;
game_character_t *targets[MAX_TARGETS] = {NULL};
if (g_enemies[index] == NULL) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
if (g_enemies[index]->status[INANIMATE] == true) {
return SUCCESS;
} else if (IsSpellcaster(g_enemies[index])) {
if (RandomInt(1, 10) > 1) { // 90% chance of casting a spell.
for (i = 0; i < NumberOfEnemies(); i++) {
if (g_enemies[i]->hp <= (g_enemies[i]->max_hp / 4) &&
g_enemies[i]->words[WORD_OF_HEALTH] == KNOWN) {
targets[0] = g_enemies[i];
CastSpell(g_enemies[index], "Y", targets); // Healing spell.
actionPerformed = true;
}
}
while (actionPerformed == false) {
if (g_enemies[i]->words[WORD_OF_FIRE] == KNOWN ||
g_enemies[i]->words[WORD_OF_AIR] == KNOWN ||
g_enemies[i]->words[WORD_OF_WATER] == KNOWN ||
g_enemies[i]->words[WORD_OF_EARTH] == KNOWN)
switch (RandomInt(1, 4)) {
case 1:
if (g_enemies[i]->words[WORD_OF_AIR] == KNOWN) {
targets[0] = &g_player;
CastSpell(g_enemies[index], "E", targets); // Wind spell.
actionPerformed = true;
}
break;
case 2:
if (g_enemies[i]->words[WORD_OF_WATER] == KNOWN) {
targets[0] = &g_player;
CastSpell(g_enemies[index], "S", targets); // Water spell.
actionPerformed = true;
}
break;
case 3:
if (g_enemies[i]->words[WORD_OF_EARTH] == KNOWN) {
targets[0] = &g_player;
CastSpell(g_enemies[index], "P", targets); // Earth spell.
actionPerformed = true;
}
break;
default:
if (g_enemies[i]->words[WORD_OF_FIRE] == KNOWN) {
targets[0] = &g_player;
CastSpell(g_enemies[index], "B", targets); // Fire spell.
actionPerformed = true;
}
break;
}
}
} else {
Attack(g_enemies[index], &g_player);
}
} else { // The active NPC is not a spellcaster.
Attack(g_enemies[index], &g_player);
}
return SUCCESS;
}
/*******************************************************************************
Function: HandleAttackMenuInput
Description: Takes the player through the process of selecting whom to attack.
Inputs: None.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int HandleAttackMenuInput(void) {
int i, input, temp;
bool repeat_options;
game_character_t *target;
if (g_enemies[0] != NULL && g_enemies[0]->type == DUMMY) {
printf("%s: \"You're a wizard, not a warrior. Cast a spell!\"\n",
FindInhabitant(ARCHWIZARD_OF_ELEMENTS)->name);
FlushInput();
return FAILURE;
}
UpdateVisibleGameCharCounter();
temp = 0;
for (i = 0; i < NUM_GC_TYPES; i++) {
g_character_type_described[i] = false;
}
// Display potential targets (unless only one is available):
if (g_player.status[IN_COMBAT]) {
if (VisibleEnemies() == 1) {
Attack(&g_player, g_enemies[0]);
return SUCCESS;
} else {
printf("Select a target:\n");
for (i = 0; i < NumberOfEnemies(); i++) {
if (g_enemies[i]->status[INVISIBLE] == false &&
g_character_type_described[g_enemies[i]->type] == false) {
temp++;
printf("[%d] %s", temp, g_enemies[i]->name);
if (g_num_visible_of_type[g_enemies[i]->type] > 1) {
printf(" (%d available)",
g_num_visible_of_type[g_enemies[i]->type]);
}
printf("\n");
g_character_type_described[g_enemies[i]->type] = true;
}
}
}
} else { // Not in combat mode: player attacks a local inhabitant.
if (VisibleInhabitants(g_world[g_player.location]) == 0) {
printf("There is nobody here to attack.\n");
FlushInput();
return FAILURE;
}
if (VisibleInhabitants(g_world[g_player.location]) == 1) {
for (target = g_world[g_player.location]->inhabitants;
target != NULL;
target = target->next) {
if (target->status[INVISIBLE] == false) {
Attack(&g_player, target);
return SUCCESS;
}
}
} else { // Multiple visible inhabitants to choose from.
printf("Select a target:\n");
for (target = g_world[g_player.location]->inhabitants;
target != NULL;
target = target->next) {
if (target->status[INVISIBLE] == false &&
g_character_type_described[target->type] == false) {
temp++;
printf("[%d] %s", temp, target->name);
if (g_num_visible_of_type[target->type] > 1) {
printf(" (%d available)", g_num_visible_of_type[target->type]);
}
printf("\n");
g_character_type_described[target->type] = true;
}
}
}
}
// Player chooses a target by number:
GetIntInput(&input, 1, temp);
// Target is now found, and attacked, by matching it with the input:
temp = 0;
for (i = 0; i < NUM_GC_TYPES; i++) {
g_character_type_described[i] = false;
}
if (g_player.status[IN_COMBAT]) {
for (i = 0; i < NumberOfEnemies(); i++) {
if (g_enemies[i]->status[INVISIBLE] == false &&
g_character_type_described[g_enemies[i]->type] == false) {
temp++;
if (temp == input) {
Attack(&g_player, g_enemies[i]);
return SUCCESS;
}
g_character_type_described[g_enemies[i]->type] = true;
}
}
} else { // Not in combat mode: player attacks a local inhabitant.
for (target = g_world[g_player.location]->inhabitants;
target != NULL;
target = target->next) {
if (target->status[INVISIBLE] == false &&
g_character_type_described[target->type] == false) {
temp++;
if (temp == input) {
Attack(&g_player, target);
if (g_player.status[IN_COMBAT] == false) {
if (target->hp > 0) {
target->relationship = HOSTILE_ENEMY;
AddEnemy(target);
} else {
printf("%s is dead.\n", Capitalize(GetNameDefinite(target)));
FlushInput();
}
for (target = g_world[g_player.location]->inhabitants;
target != NULL;
target = target->next) {
if (WillingToFight(target) &&
target->status[IN_COMBAT] == false) {
target->relationship = HOSTILE_ENEMY;
AddEnemy(target);
}
}
}
if (g_player.status[IN_COMBAT] == false && NumberOfEnemies() > 0) {
printf("Prepare for battle!\n");
FlushInput();
Combat();
}
return SUCCESS;
}
g_character_type_described[target->type] = true;
}
}
}
PRINT_ERROR_MESSAGE;
return FAILURE;
}
/*******************************************************************************
Function: Attack
Description: Handles a physical attack between two game characters.
Inputs: attacker - The GC performing the attack.
defender - The GC being attacked.
Outputs: SUCCESS or FAILURE.
*******************************************************************************/
int Attack(game_character_t *attacker, game_character_t *defender) {
int damage;
if (attacker == NULL || defender == NULL) {
PRINT_ERROR_MESSAGE;
return FAILURE;
}
printf("%s attacks %s", attacker->name, defender->name);
if (RandomInt(1, 10) > 2) { // 80% chance of a successful hit.
damage = RandomInt(1, attacker->physical_power);
damage -= RandomInt(0, defender->physical_defense);
if (damage <= 0) {
damage = 1;
}
defender->hp -= damage;
printf(" for %d damage.", damage);
} else {
printf(", but misses.");
}
FlushInput();
return SUCCESS;
}
/*******************************************************************************
Function: WillingToFight
Description: Determines whether a given NPC will fight the player if the player
is openly hostile to another NPC in the area.
Inputs: p_gc - Pointer to the game character of interest.
Outputs: 'true' or 'false'
*******************************************************************************/
bool WillingToFight(game_character_t *p_gc) {
if (p_gc->type == SOLDIER ||
p_gc->type == KNIGHT ||
p_gc->type == WIZARD ||
p_gc->type == INNKEEPER ||
p_gc->type == FISHERMAN ||
p_gc->type == SAILOR ||
p_gc->type == FARMER ||
p_gc->type == ILLARUM_PRIEST ||
p_gc->type == ILLARUM_HIGH_PRIEST ||
p_gc->type == ARCHWIZARD_OF_ELEMENTS ||
p_gc->type == COURT_WIZARD ||
p_gc->type == WIZARD_OF_ELEMENTS ||
p_gc->type == WIZARD_OF_MIND ||
p_gc->type == ARCHWIZARD_OF_MIND ||
p_gc->type == VENTARRIS_PRIEST ||
p_gc->type == BARBARIAN_WARRIOR ||
p_gc->type == VENTARRIS_HIGH_PRIEST ||
p_gc->type == BARBARIAN ||
p_gc->type == BARBARIAN_SHAMAN ||
p_gc->type == BARBARIAN_CHIEFTAIN ||
p_gc->type == ELF ||
p_gc->type == DWARF_MERCHANT ||
p_gc->type == DWARF_PRIEST ||
p_gc->type == ELF_LOREMASTER ||
p_gc->type == DWARF || p_gc->type == DWARF_KING ||
p_gc->type == DWARF_MINER ||
p_gc->type == DWARF_GUARDIAN ||
p_gc->type == DWARF_LOREMASTER ||
p_gc->type == DWARF_HIGH_PRIEST ||
p_gc->type == GNOME ||
p_gc->type == GNOME_MINER || p_gc->type == DRUID ||
p_gc->type == ARCHDRUID ||
p_gc->type == MERFOLK_SOLDIER ||
p_gc->type == MERFOLK_PRIESTESS ||
p_gc->type == MERFOLK_HIGH_PRIESTESS ||
p_gc->type == MERFOLK_QUEEN ||
p_gc->type == VENTARRIS_KING ||
p_gc->type == ILLARUM_KING ||
p_gc->type == LICH || p_gc->type == NECROMANCER ||
p_gc->type == ARCHNECROMANCER ||
p_gc->type == SKELETAL_KNIGHT) {
return true;
}
return false;
}
/*******************************************************************************
Function: WillingToFlee
Description: Determines whether a given NPC will flee when losing a fight.
Inputs: p_gc - Pointer to the game character of interest.
Outputs: 'true' or 'false'
*******************************************************************************/
bool WillingToFlee(game_character_t *p_gc) {
return p_gc->type == PEASANT ||
p_gc->type == SOLDIER ||
p_gc->type == WIZARD ||
p_gc->type == INNKEEPER ||
p_gc->type == FISHERMAN ||
p_gc->type == SAILOR ||
p_gc->type == FARMER ||
p_gc->type == ILLARUM_PRIEST ||
p_gc->type == ILLARUM_HIGH_PRIEST ||
p_gc->type == ARCHWIZARD_OF_ELEMENTS ||
p_gc->type == COURT_WIZARD ||
p_gc->type == WIZARD_OF_ELEMENTS ||
p_gc->type == WIZARD_OF_MIND ||
p_gc->type == ARCHWIZARD_OF_MIND ||
p_gc->type == VENTARRIS_PRIEST ||
p_gc->type == VENTARRIS_HIGH_PRIEST ||
p_gc->type == ELF ||
p_gc->type == DWARF_MERCHANT ||
p_gc->type == DWARF_PRIEST ||
p_gc->type == ELF_LOREMASTER ||
p_gc->type == GNOME ||
p_gc->type == GNOME_MINER ||
p_gc->type == DRUID ||
p_gc->type == ARCHDRUID ||
p_gc->type == VENTARRIS_KING ||
p_gc->type == NECROMANCER ||
p_gc->type == ARCHNECROMANCER;
}
/*******************************************************************************
Function: WillingToHelp
Description: Determines whether a given NPC will assist allies during combat
(by healing them, for example).
Inputs: p_gc - Pointer to the game character of interest.
Outputs: 'true' or 'false'
*******************************************************************************/
bool WillingToHelp(game_character_t *p_gc) {
if (p_gc->type == SOLDIER ||
p_gc->type == KNIGHT ||
p_gc->type == WIZARD ||
p_gc->type == INNKEEPER ||
p_gc->type == FISHERMAN ||
p_gc->type == SAILOR ||
p_gc->type == FARMER ||
p_gc->type == ILLARUM_PRIEST ||
p_gc->type == ILLARUM_HIGH_PRIEST ||
p_gc->type == ARCHWIZARD_OF_ELEMENTS ||
p_gc->type == COURT_WIZARD ||
p_gc->type == WIZARD_OF_ELEMENTS ||
p_gc->type == WIZARD_OF_MIND ||
p_gc->type == ARCHWIZARD_OF_MIND ||
p_gc->type == VENTARRIS_PRIEST ||
p_gc->type == BARBARIAN_WARRIOR ||
p_gc->type == VENTARRIS_HIGH_PRIEST ||
p_gc->type == BARBARIAN ||
p_gc->type == BARBARIAN_SHAMAN ||
p_gc->type == BARBARIAN_CHIEFTAIN ||
p_gc->type == ELF ||
p_gc->type == DWARF_MERCHANT ||
p_gc->type == DWARF_PRIEST ||
p_gc->type == ELF_LOREMASTER ||
p_gc->type == DWARF ||
p_gc->type == DWARF_KING ||
p_gc->type == DWARF_MINER ||
p_gc->type == DWARF_GUARDIAN ||
p_gc->type == DWARF_LOREMASTER ||
p_gc->type == DWARF_HIGH_PRIEST ||
p_gc->type == GNOME ||
p_gc->type == GNOME_MINER ||
p_gc->type == DRUID ||
p_gc->type == ARCHDRUID ||
p_gc->type == MERFOLK_SOLDIER ||
p_gc->type == MERFOLK_PRIESTESS ||
p_gc->type == MERFOLK_HIGH_PRIESTESS ||
p_gc->type == MERFOLK_QUEEN ||
p_gc->type == VENTARRIS_KING ||
p_gc->type == ILLARUM_KING) {
return true;
}
return false;
}