This repository has been archived by the owner on Apr 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCombat.c
1909 lines (1674 loc) · 68.5 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
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
/*
* Combat.c
* Brogue
*
* Created by Brian Walker on 6/11/09.
* Copyright 2012. All rights reserved.
*
* This file is part of Brogue.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "Rogue.h"
#include "IncludeGlobals.h"
/* Combat rules:
* Each combatant has an accuracy rating. This is the percentage of their attacks that will ordinarily hit;
* higher numbers are better for them. Numbers over 100 are permitted.
*
* Each combatant also has a defense rating. The "hit probability" is calculated as given by this formula:
*
* hit probability = (accuracy) * 0.987 ^ (defense)
*
* when hit determinations are made. Negative numbers and numbers over 100 are permitted.
* The hit is then randomly determined according to this final percentage.
*
* Some environmental factors can modify these numbers. An unaware, sleeping, stuck or paralyzed
* combatant is always hit. An unaware, sleeping or paralyzed combatant also takes treble damage.
*
* If the hit lands, damage is calculated in the range provided. However, the clumping factor affects the
* probability distribution. If the range is 0-10 with a clumping factor of 1, it's a uniform distribution.
* With a clumping factor of 2, it's calculated as 2d5 (with d5 meaing a die numbered from 0 through 5).
* With 3, it's 3d3, and so on. Note that a range not divisible by the clumping factor is defective,
* as it will never be resolved in the top few numbers of the range. In fact, the top
* (rangeWidth % clumpingFactor) will never succeed. Thus we increment the maximum of the first
* (rangeWidth % clumpingFactor) die by 1, so that in fact 0-10 with a CF of 3 would be 1d4 + 2d3. Similarly,
* 0-10 with CF 4 would be 2d3 + 2d2. By playing with the numbers, one can approximate a gaussian
* distribution of any mean and standard deviation.
*
* Player combatants take their base defense value of their actual armor. Their accuracy is a combination of weapon, armor
* and strength.
*
* Players have a base accuracy value of 100 throughout the game. Each point of weapon enchantment (net of
* strength penalty/benefit) increases
*/
float strengthModifier(item *theItem) {
short difference = (rogue.strength - player.weaknessAmount) - theItem->strengthRequired;
if (difference > 0) {
return (float) 0.25 * difference;
} else {
return (float) 2.5 * difference;
}
}
float netEnchant(item *theItem) {
if (theItem->category & (WEAPON | ARMOR)) {
return ((float) theItem->enchant1) + strengthModifier(theItem);
} else {
return ((float) theItem->enchant1);
}
}
// does NOT account for auto-hit from sleeping or unaware defenders; does account for auto-hit from
// stuck or captive defenders and from weapons of slaying.
short hitProbability(creature *attacker, creature *defender) {
short accuracy = monsterAccuracyAdjusted(attacker);
short defense = monsterDefenseAdjusted(defender);
short hitProbability;
if (defender->status[STATUS_STUCK] || (defender->bookkeepingFlags & MB_CAPTIVE)) {
return 100;
}
if ((defender->bookkeepingFlags & MB_SEIZED)
&& (attacker->bookkeepingFlags & MB_SEIZING)) {
return 100;
}
if (attacker == &player && rogue.weapon) {
if ((rogue.weapon->flags & ITEM_RUNIC)
&& rogue.weapon->enchant2 == W_SLAYING
&& monsterIsInClass(defender, rogue.weapon->vorpalEnemy)) {
return 100;
}
accuracy = (double) player.info.accuracy * pow(WEAPON_ENCHANT_ACCURACY_FACTOR, netEnchant(rogue.weapon) + FLOAT_FUDGE);
}
hitProbability = accuracy * pow(DEFENSE_FACTOR, defense);
if (hitProbability > 100) {
hitProbability = 100;
} else if (hitProbability < 0) {
hitProbability = 0;
}
return hitProbability;
}
boolean attackHit(creature *attacker, creature *defender) {
// automatically hit if the monster is sleeping or captive or stuck in a web
if (defender->status[STATUS_STUCK]
|| defender->status[STATUS_PARALYZED]
|| (defender->bookkeepingFlags & MB_CAPTIVE)) {
return true;
}
return rand_percent(hitProbability(attacker, defender));
}
void addMonsterToContiguousMonsterGrid(short x, short y, creature *monst, char grid[DCOLS][DROWS]) {
short newX, newY;
enum directions dir;
creature *tempMonst;
grid[x][y] = true;
for (dir=0; dir<4; dir++) {
newX = x + nbDirs[dir][0];
newY = y + nbDirs[dir][1];
if (coordinatesAreInMap(newX, newY) && !grid[newX][newY]) {
tempMonst = monsterAtLoc(newX, newY);
if (tempMonst && monstersAreTeammates(monst, tempMonst)) {
addMonsterToContiguousMonsterGrid(newX, newY, monst, grid);
}
}
}
}
// Splits a monster in half.
// The split occurs only if there is a spot adjacent to the contiguous
// group of monsters that the monster would not avoid.
// The contiguous group is supplemented with the given (x, y) coordinates, if any;
// this is so that jellies et al. can spawn behind the player in a hallway.
void splitMonster(creature *monst, short x, short y) {
short i, j, b, dir, newX, newY, eligibleLocationCount, randIndex;
char buf[DCOLS * 3];
char monstName[DCOLS];
char monsterGrid[DCOLS][DROWS], eligibleGrid[DCOLS][DROWS];
creature *clone;
zeroOutGrid(monsterGrid);
zeroOutGrid(eligibleGrid);
eligibleLocationCount = 0;
// Add the (x, y) location to the contiguous group, if any.
if (x > 0 && y > 0) {
monsterGrid[x][y] = true;
}
// Find the contiguous group of monsters.
addMonsterToContiguousMonsterGrid(monst->xLoc, monst->yLoc, monst, monsterGrid);
// Find the eligible edges around the group of monsters.
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
if (monsterGrid[i][j]) {
for (dir=0; dir<4; dir++) {
newX = i + nbDirs[dir][0];
newY = j + nbDirs[dir][1];
if (coordinatesAreInMap(newX, newY)
&& !eligibleGrid[newX][newY]
&& !monsterGrid[newX][newY]
&& !(pmap[newX][newY].flags & (HAS_PLAYER | HAS_MONSTER))
&& !monsterAvoids(monst, newX, newY)) {
eligibleGrid[newX][newY] = true;
eligibleLocationCount++;
}
}
}
}
}
// DEBUG {
// hiliteCharGrid(eligibleGrid, &green, 75);
// hiliteCharGrid(monsterGrid, &blue, 75);
// temporaryMessage("Jelly spawn possibilities (green = eligible, blue = monster):", true);
// displayLevel();
// }
// Pick a random location on the eligibleGrid and add the clone there.
if (eligibleLocationCount) {
randIndex = rand_range(1, eligibleLocationCount);
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
if (eligibleGrid[i][j] && !--randIndex) {
// Found the spot!
monsterName(monstName, monst, true);
monst->currentHP = (monst->currentHP + 1) / 2;
clone = cloneMonster(monst, false, false);
// Split monsters don't inherit the learnings of their parents.
// Sorry, but self-healing jelly armies are too much.
// Mutation effects can be inherited, however; they're not learned abilities.
if (monst->mutationIndex) {
clone->info.flags &= (monsterCatalog[clone->info.monsterID].flags | mutationCatalog[monst->mutationIndex].monsterFlags);
clone->info.abilityFlags &= (monsterCatalog[clone->info.monsterID].abilityFlags | mutationCatalog[monst->mutationIndex].monsterAbilityFlags);
} else {
clone->info.flags &= monsterCatalog[clone->info.monsterID].flags;
clone->info.abilityFlags &= monsterCatalog[clone->info.monsterID].abilityFlags;
}
for (b = 0; b < 20; b++) {
clone->info.bolts[b] = monsterCatalog[clone->info.monsterID].bolts[b];
}
if (!(clone->info.flags & MONST_FLIES)
&& clone->status[STATUS_LEVITATING] == 1000) {
clone->status[STATUS_LEVITATING] = 0;
}
clone->xLoc = i;
clone->yLoc = j;
pmap[i][j].flags |= HAS_MONSTER;
clone->ticksUntilTurn = max(clone->ticksUntilTurn, 101);
fadeInMonster(clone);
refreshSideBar(-1, -1, false);
if (canDirectlySeeMonster(monst)) {
sprintf(buf, "%s splits in two!", monstName);
message(buf, false);
}
return;
}
}
}
}
}
short alliedCloneCount(creature *monst) {
short count;
creature *temp;
count = 0;
for (temp = monsters->nextCreature; temp != NULL; temp = temp->nextCreature) {
if (temp != monst
&& temp->info.monsterID == monst->info.monsterID
&& monstersAreTeammates(temp, monst)) {
count++;
}
}
if (rogue.depthLevel > 0) {
for (temp = levels[rogue.depthLevel - 2].monsters; temp != NULL; temp = temp->nextCreature) {
if (temp != monst
&& temp->info.monsterID == monst->info.monsterID
&& monstersAreTeammates(temp, monst)) {
count++;
}
}
}
if (rogue.depthLevel < DEEPEST_LEVEL) {
for (temp = levels[rogue.depthLevel].monsters; temp != NULL; temp = temp->nextCreature) {
if (temp != monst
&& temp->info.monsterID == monst->info.monsterID
&& monstersAreTeammates(temp, monst)) {
count++;
}
}
}
return count;
}
// This function is called whenever one creature acts aggressively against another in a way that directly causes damage.
// This can be things like melee attacks, fire/lightning attacks or throwing a weapon.
void moralAttack(creature *attacker, creature *defender) {
if (attacker == &player) {
rogue.featRecord[FEAT_PACIFIST] = false;
if (defender->creatureState != MONSTER_TRACKING_SCENT) {
rogue.featRecord[FEAT_PALADIN] = false;
}
}
if (defender->currentHP > 0
&& !(defender->bookkeepingFlags & MB_IS_DYING)) {
if (defender->status[STATUS_PARALYZED]) {
defender->status[STATUS_PARALYZED] = 0;
// Paralyzed creature gets a turn to react before the attacker moves again.
defender->ticksUntilTurn = min(attacker->attackSpeed, 100) - 1;
}
if (defender->status[STATUS_MAGICAL_FEAR]) {
defender->status[STATUS_MAGICAL_FEAR] = 1;
}
defender->status[STATUS_ENTRANCED] = 0;
if ((defender->info.abilityFlags & MA_AVOID_CORRIDORS)
&& (distanceBetween(attacker->xLoc, attacker->yLoc, defender->xLoc, defender->yLoc) > 1)) {
defender->status[STATUS_ENRAGED] = defender->maxStatus[STATUS_ENRAGED] = 8;
//yes, a spear or pike attack can enrage a monster, but it will only affect one of them
}
if (attacker == &player
&& defender->creatureState == MONSTER_ALLY
&& !defender->status[STATUS_DISCORDANT]
&& !attacker->status[STATUS_CONFUSED]
&& !(attacker->bookkeepingFlags & MB_IS_DYING)) {
unAlly(defender);
}
if ((attacker == &player || attacker->creatureState == MONSTER_ALLY)
&& defender != &player
&& defender->creatureState != MONSTER_ALLY) {
alertMonster(defender); // this alerts the monster that you're nearby
}
if (!(defender->status[STATUS_INVISIBLE]) && (defender->info.abilityFlags & MA_DEFEND_INVISIBLE)) {
ninjaVanish(attacker, defender, 10);
}
if ((defender->info.abilityFlags & MA_CLONE_SELF_ON_DEFEND) && alliedCloneCount(defender) < 100) {
if (distanceBetween(defender->xLoc, defender->yLoc, attacker->xLoc, attacker->yLoc) <= 1) {
splitMonster(defender, attacker->xLoc, attacker->yLoc);
} else {
splitMonster(defender, 0, 0);
}
}
}
}
void ninjaVanish(creature *attacker, creature *defender, short duration) {
short i, x, y, xLocus, yLocus, dirs[8];
enum directions dir;
char buf[COLS], monstName[DCOLS];
if (defender != &player) {
monsterName(monstName, defender, true);
sprintf(buf, "%s vanishes!", monstName);
buf[DCOLS] = '\0';
message(buf, false);
}
defender->status[STATUS_INVISIBLE] = defender->maxStatus[STATUS_INVISIBLE] = duration;
createFlare(defender->xLoc, defender->yLoc, POTION_STRENGTH_LIGHT);
if (distanceBetween(attacker->xLoc, attacker->yLoc, defender->xLoc, defender->yLoc) == 1) {
xLocus = attacker->xLoc;
yLocus = attacker->yLoc;
} else {
xLocus = defender->xLoc;
yLocus = defender->yLoc;
}
fillSequentialList(dirs, 8);
shuffleList(dirs, 8);
for (i=0; i<8; i++) {
dir = dirs[i];
x = xLocus + nbDirs[dir][0];
y = yLocus + nbDirs[dir][1];
if (!cellHasTerrainFlag(x, y, T_OBSTRUCTS_PASSABILITY) &&
(!cellHasTerrainFlag(x, y, T_AUTO_DESCENT) || defender->status[STATUS_LEVITATING]) &&
(!cellHasTerrainFlag(x, y, T_LAVA_INSTA_DEATH) || defender->status[STATUS_LEVITATING] || defender->status[STATUS_IMMUNE_TO_FIRE]) &&
!(pmap[x][y].flags & HAS_MONSTER)) {
pmap[defender->xLoc][defender->yLoc].flags &= ~HAS_MONSTER;
refreshDungeonCell(defender->xLoc, defender->yLoc);
if(defender == &player){
pmap[player.xLoc][player.yLoc].flags &= ~HAS_PLAYER;
refreshDungeonCell(player.xLoc, player.yLoc);
defender->xLoc = x;
defender->yLoc = y;
pmap[player.xLoc][player.yLoc].flags |= HAS_PLAYER;
updateVision(true);
// get any items at the destination location
if (pmap[player.xLoc][player.yLoc].flags & HAS_ITEM) {
pickUpItemAt(player.xLoc, player.yLoc);
}
attacker->creatureState = MONSTER_WANDERING;
} else {
pmap[defender->xLoc][defender->yLoc].flags &= ~HAS_MONSTER;
refreshDungeonCell(defender->xLoc, defender->yLoc);
defender->xLoc = x;
defender->yLoc = y;
pmap[defender->xLoc][defender->yLoc].flags |= HAS_MONSTER;
refreshDungeonCell(defender->xLoc, defender->yLoc);
}
}
}
}
boolean playerImmuneToMonster(creature *monst) {
if (monst != &player
&& rogue.armor
&& (rogue.armor->flags & ITEM_RUNIC)
&& (rogue.armor->enchant2 == A_IMMUNITY)
&& monsterIsInClass(monst, rogue.armor->vorpalEnemy)) {
return true;
} else {
return false;
}
}
void specialHit(creature *attacker, creature *defender, short damage) {
short itemCandidates, randItemIndex, stolenQuantity;
item *theItem = NULL, *itemFromTopOfStack;
char buf[COLS], buf2[COLS], buf3[COLS];
if (!(attacker->info.abilityFlags & SPECIAL_HIT)) {
return;
}
// Special hits that can affect only the player:
if (defender == &player) {
if (playerImmuneToMonster(attacker)) {
return;
}
if (attacker->info.abilityFlags & MA_HIT_DEGRADE_ARMOR
&& defender == &player
&& rogue.armor
&& !(rogue.armor->flags & ITEM_PROTECTED)) {
rogue.armor->enchant1--;
equipItem(rogue.armor, true);
itemName(rogue.armor, buf2, false, false, NULL);
sprintf(buf, "your %s weakens!", buf2);
messageWithColor(buf, &itemMessageColor, false);
checkForDisenchantment(rogue.armor);
}
if (attacker->info.abilityFlags & MA_HIT_HALLUCINATE) {
if (!player.status[STATUS_HALLUCINATING]) {
combatMessage("you begin to hallucinate", 0);
}
if (!player.status[STATUS_HALLUCINATING]) {
player.maxStatus[STATUS_HALLUCINATING] = 0;
}
player.status[STATUS_HALLUCINATING] += 20;
player.maxStatus[STATUS_HALLUCINATING] = max(player.maxStatus[STATUS_HALLUCINATING], player.status[STATUS_HALLUCINATING]);
}
if (attacker->info.abilityFlags & MA_HIT_STEAL_FLEE
&& !(attacker->carriedItem)
&& (packItems->nextItem)
&& attacker->currentHP > 0
&& !attacker->status[STATUS_CONFUSED] // No stealing from the player if you bump him while confused.
&& attackHit(attacker, defender)) {
itemCandidates = numberOfMatchingPackItems(ALL_ITEMS, 0, (ITEM_EQUIPPED), false);
if (itemCandidates) {
randItemIndex = rand_range(1, itemCandidates);
for (theItem = packItems->nextItem; theItem != NULL; theItem = theItem->nextItem) {
if (!(theItem->flags & (ITEM_EQUIPPED))) {
if (randItemIndex == 1) {
break;
} else {
randItemIndex--;
}
}
}
if (theItem) {
if (theItem->category & WEAPON) { // Monkeys will steal half of a stack of weapons, and one of any other stack.
if (theItem->quantity > 3) {
stolenQuantity = (theItem->quantity + 1) / 2;
} else {
stolenQuantity = theItem->quantity;
}
} else {
stolenQuantity = 1;
}
if (stolenQuantity < theItem->quantity) { // Peel off stolen item(s).
itemFromTopOfStack = generateItem(ALL_ITEMS, -1);
*itemFromTopOfStack = *theItem; // Clone the item.
theItem->quantity -= stolenQuantity;
itemFromTopOfStack->quantity = stolenQuantity;
theItem = itemFromTopOfStack; // Redirect pointer.
} else {
removeItemFromChain(theItem, packItems);
}
theItem->flags &= ~ITEM_PLAYER_AVOIDS; // Explore will seek the item out if it ends up on the floor again.
attacker->carriedItem = theItem;
attacker->creatureMode = MODE_PERM_FLEEING;
attacker->creatureState = MONSTER_FLEEING;
monsterName(buf2, attacker, true);
itemName(theItem, buf3, false, true, NULL);
sprintf(buf, "%s stole %s!", buf2, buf3);
messageWithColor(buf, &badMessageColor, false);
rogue.autoPlayingLevel = false; //get your bearings
}
}
}
}
if ((attacker->info.abilityFlags & MA_POISONS)
&& damage > 0
&& !(defender->info.flags & (MONST_INANIMATE | MONST_INVULNERABLE))) {
addPoison(defender, damage, 1);
}
if ((attacker->info.abilityFlags & MA_CAUSES_WEAKNESS)
&& damage > 0
&& !(defender->info.flags & (MONST_INANIMATE | MONST_INVULNERABLE))) {
weaken(defender, 300);
}
if ((attacker->info.abilityFlags & MA_HIT_DISCORD)
&& defender != &player
&& damage > 0
&& !(defender->info.flags & (MONST_INANIMATE | MONST_INVULNERABLE))) {
if (canSeeMonster(defender)) {
flashMonster(defender, &discordColor, 100);
}
defender->status[STATUS_DISCORDANT] = defender->maxStatus[STATUS_DISCORDANT] = 30;
}
}
short runicWeaponChance(item *theItem, boolean customEnchantLevel, float enchantLevel) {
const float effectChances[NUMBER_WEAPON_RUNIC_KINDS] = {
0.16, // W_SPEED
0.06, // W_QUIETUS
0.07, // W_PARALYSIS
0.15, // W_MULTIPLICITY
0.14, // W_SLOWING
0.11, // W_CONFUSION
0.15, // W_FORCE
0, // W_SLAYING
0.18, // W_ENERVATION
0, // W_MERCY
0}; // W_PLENTY
float rootChance, modifier;
short runicType = theItem->enchant2;
short chance, adjustedBaseDamage;
if (runicType == W_SLAYING) {
return 0;
}
if (runicType >= NUMBER_GOOD_WEAPON_ENCHANT_KINDS) { // bad runic
return 15;
}
if (!customEnchantLevel) {
enchantLevel = (float) netEnchant(theItem);
}
rootChance = effectChances[runicType];
// Innately high-damage weapon types are less likely to trigger runic effects.
adjustedBaseDamage = (theItem->damage.lowerBound + theItem->damage.upperBound) / 2;
if (theItem->flags & ITEM_ATTACKS_HIT_SLOWLY) {
adjustedBaseDamage /= 2; // Normalize as though they attacked once per turn instead of every other turn.
}
// if (theItem->flags & ITEM_ATTACKS_QUICKLY) {
// adjustedBaseDamage *= 2; // Normalize as though they attacked once per turn instead of twice per turn.
// } // Testing disabling this for balance reasons...
modifier = 1.0 - min(0.99, ((float) adjustedBaseDamage) / 18.0);
rootChance *= modifier;
chance = 100 - (short) (100 * pow(1.0 - rootChance, enchantLevel) + FLOAT_FUDGE); // good runic
// Slow weapons get an adjusted chance of 1 - (1-p)^2 to reflect two bites at the apple instead of one.
if (theItem->flags & ITEM_ATTACKS_HIT_SLOWLY) {
chance = 100 - (100 - chance) * (100 - chance) / 100;
}
// Fast weapons get an adjusted chance of 1 - sqrt(1-p) to reflect one bite at the apple instead of two.
if (theItem->flags & ITEM_ATTACKS_QUICKLY) {
chance = 100 * (1.0 - sqrt(1 - ((double)(chance)/100.0)));
}
// The lowest percent change that a weapon will ever have is its enchantment level (if greater than 0).
// That is so that even really heavy weapons will improve at least 1% per enchantment.
chance = clamp(chance, max(1, (short) enchantLevel), 100);
return chance;
}
boolean forceWeaponHit(creature *defender, item *theItem) {
short oldLoc[2], newLoc[2], forceDamage;
char buf[DCOLS*3], buf2[COLS], monstName[DCOLS];
creature *otherMonster = NULL;
boolean knowFirstMonsterDied = false, autoID = false;
bolt theBolt;
monsterName(monstName, defender, true);
oldLoc[0] = defender->xLoc;
oldLoc[1] = defender->yLoc;
newLoc[0] = defender->xLoc + clamp(defender->xLoc - player.xLoc, -1, 1);
newLoc[1] = defender->yLoc + clamp(defender->yLoc - player.yLoc, -1, 1);
if (canDirectlySeeMonster(defender)
&& !cellHasTerrainFlag(newLoc[0], newLoc[1], T_OBSTRUCTS_PASSABILITY | T_OBSTRUCTS_VISION)
&& !(pmap[newLoc[0]][newLoc[1]].flags & (HAS_MONSTER | HAS_PLAYER))) {
sprintf(buf, "you launch %s backward with the force of your blow", monstName);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
}
theBolt = boltCatalog[BOLT_BLINKING];
theBolt.magnitude = max(1, netEnchant(theItem) + FLOAT_FUDGE);
zap(oldLoc, newLoc, &theBolt, false, NULL);
if (!(defender->bookkeepingFlags & MB_IS_DYING)
&& distanceBetween(oldLoc[0], oldLoc[1], defender->xLoc, defender->yLoc) > 0
&& distanceBetween(oldLoc[0], oldLoc[1], defender->xLoc, defender->yLoc) < weaponForceDistance(netEnchant(theItem))) {
if (pmap[defender->xLoc + newLoc[0] - oldLoc[0]][defender->yLoc + newLoc[1] - oldLoc[1]].flags & (HAS_MONSTER | HAS_PLAYER)) {
otherMonster = monsterAtLoc(defender->xLoc + newLoc[0] - oldLoc[0], defender->yLoc + newLoc[1] - oldLoc[1]);
monsterName(buf2, otherMonster, true);
} else {
otherMonster = NULL;
strcpy(buf2, tileCatalog[pmap[defender->xLoc + newLoc[0] - oldLoc[0]][defender->yLoc + newLoc[1] - oldLoc[1]].layers[highestPriorityLayer(defender->xLoc + newLoc[0] - oldLoc[0], defender->yLoc + newLoc[1] - oldLoc[1], true)]].description);
}
forceDamage = distanceBetween(oldLoc[0], oldLoc[1], defender->xLoc, defender->yLoc);
if (!(defender->info.flags & (MONST_IMMUNE_TO_WEAPONS | MONST_INVULNERABLE))
&& inflictDamage(NULL, defender, forceDamage, &white, false)) {
if (canDirectlySeeMonster(defender)) {
knowFirstMonsterDied = true;
sprintf(buf, "%s %s on impact with %s",
monstName,
(defender->info.flags & MONST_INANIMATE) ? "is destroyed" : "dies",
buf2);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
}
} else {
if (canDirectlySeeMonster(defender)) {
sprintf(buf, "%s slams against %s",
monstName,
buf2);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
}
}
moralAttack(&player, defender);
if (otherMonster
&& !(defender->info.flags & (MONST_IMMUNE_TO_WEAPONS | MONST_INVULNERABLE))) {
if (inflictDamage(NULL, otherMonster, forceDamage, &white, false)) {
if (canDirectlySeeMonster(otherMonster)) {
sprintf(buf, "%s %s%s when %s slams into $HIMHER",
buf2,
(knowFirstMonsterDied ? "also " : ""),
(defender->info.flags & MONST_INANIMATE) ? "is destroyed" : "dies",
monstName);
resolvePronounEscapes(buf, otherMonster);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(otherMonster));
autoID = true;
}
}
if (otherMonster->creatureState != MONSTER_ALLY) {
// Allies won't defect if you throw another monster at them, even though it hurts.
moralAttack(&player, otherMonster);
}
}
}
return autoID;
}
void magicWeaponHit(creature *defender, item *theItem, boolean backstabbed) {
char buf[DCOLS*3], monstName[DCOLS], theItemName[DCOLS];
color *effectColors[NUMBER_WEAPON_RUNIC_KINDS] = {&white, &black,
&yellow, &pink, &green, &confusionGasColor, NULL, NULL, &darkRed, &rainbow};
// W_SPEED, W_QUIETUS, W_PARALYSIS, W_MULTIPLICITY, W_SLOWING, W_CONFUSION, W_FORCE, W_SLAYING, W_MERCY, W_PLENTY
short chance, i;
float enchant;
enum weaponEnchants enchantType = theItem->enchant2;
creature *newMonst;
boolean autoID = false;
// If the defender is already dead, proceed only if the runic is speed or multiplicity.
// (Everything else acts on the victim, which would literally be overkill.)
if ((defender->bookkeepingFlags & MB_IS_DYING)
&& theItem->enchant2 != W_SPEED
&& theItem->enchant2 != W_MULTIPLICITY) {
return;
}
enchant = netEnchant(theItem);
if (theItem->enchant2 == W_SLAYING) {
chance = (monsterIsInClass(defender, theItem->vorpalEnemy) ? 100 : 0);
} else if (defender->info.flags & (MONST_INANIMATE | MONST_INVULNERABLE)) {
chance = 0;
} else {
chance = runicWeaponChance(theItem, false, 0);
if (backstabbed && chance < 100) {
chance = min(chance * 2, (chance + 100) / 2);
}
}
if (chance > 0 && rand_percent(chance)) {
if (!(defender->bookkeepingFlags & MB_SUBMERGED)) {
switch (enchantType) {
case W_SPEED:
createFlare(player.xLoc, player.yLoc, SCROLL_ENCHANTMENT_LIGHT);
break;
case W_QUIETUS:
createFlare(defender->xLoc, defender->yLoc, QUIETUS_FLARE_LIGHT);
break;
case W_SLAYING:
createFlare(defender->xLoc, defender->yLoc, SLAYING_FLARE_LIGHT);
break;
default:
flashMonster(defender, effectColors[enchantType], 100);
break;
}
autoID = true;
}
rogue.disturbed = true;
monsterName(monstName, defender, true);
itemName(theItem, theItemName, false, false, NULL);
switch (enchantType) {
case W_ENERVATION:
weaken(defender, 300);
if (canDirectlySeeMonster(defender)) {
sprintf(buf, "%s grows weaker", monstName);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
}
break;
case W_SPEED:
if (player.ticksUntilTurn != -1) {
sprintf(buf, "your %s trembles and time freezes for a moment", theItemName);
buf[DCOLS] = '\0';
combatMessage(buf, 0);
player.ticksUntilTurn = -1; // free turn!
autoID = true;
}
break;
case W_SLAYING:
case W_QUIETUS:
inflictLethalDamage(&player, defender);
sprintf(buf, "%s suddenly %s",
monstName,
(defender->info.flags & MONST_INANIMATE) ? "shatters" : "dies");
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
break;
case W_PARALYSIS:
defender->status[STATUS_PARALYZED] = max(defender->status[STATUS_PARALYZED], weaponParalysisDuration(enchant));
defender->maxStatus[STATUS_PARALYZED] = defender->status[STATUS_PARALYZED];
if (canDirectlySeeMonster(defender)) {
sprintf(buf, "%s is frozen in place", monstName);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
}
break;
case W_MULTIPLICITY:
sprintf(buf, "Your %s emits a flash of light, and %sspectral duplicate%s appear%s!",
theItemName,
(weaponImageCount(enchant) == 1 ? "a " : ""),
(weaponImageCount(enchant) == 1 ? "" : "s"),
(weaponImageCount(enchant) == 1 ? "s" : ""));
buf[DCOLS] = '\0';
for (i = 0; i < (weaponImageCount(enchant)); i++) {
newMonst = generateMonster(MK_SPECTRAL_IMAGE, true, false);
getQualifyingPathLocNear(&(newMonst->xLoc), &(newMonst->yLoc), defender->xLoc, defender->yLoc, true,
T_DIVIDES_LEVEL & avoidedFlagsForMonster(&(newMonst->info)), HAS_PLAYER,
avoidedFlagsForMonster(&(newMonst->info)), (HAS_PLAYER | HAS_MONSTER | HAS_UP_STAIRS | HAS_DOWN_STAIRS), false);
newMonst->bookkeepingFlags |= (MB_FOLLOWER | MB_BOUND_TO_LEADER | MB_DOES_NOT_TRACK_LEADER | MB_TELEPATHICALLY_REVEALED);
newMonst->bookkeepingFlags &= ~MB_JUST_SUMMONED;
newMonst->leader = &player;
newMonst->creatureState = MONSTER_ALLY;
if (theItem->flags & ITEM_ATTACKS_HIT_SLOWLY) {
newMonst->info.attackSpeed *= 2;
}
if (theItem->flags & ITEM_ATTACKS_QUICKLY) {
newMonst->info.attackSpeed /= 2;
}
if (theItem->flags & ITEM_ATTACKS_PENETRATE) {
newMonst->info.abilityFlags |= MA_ATTACKS_PENETRATE;
}
if (theItem->flags & ITEM_ATTACKS_ALL_ADJACENT) {
newMonst->info.abilityFlags |= MA_ATTACKS_ALL_ADJACENT;
}
if (theItem->flags & ITEM_ATTACKS_EXTEND) {
newMonst->info.abilityFlags |= MA_ATTACKS_EXTEND;
}
newMonst->ticksUntilTurn = 100;
newMonst->info.accuracy = player.info.accuracy + 5 * netEnchant(theItem);
newMonst->info.damage = player.info.damage;
newMonst->status[STATUS_LIFESPAN_REMAINING] = newMonst->maxStatus[STATUS_LIFESPAN_REMAINING] = weaponImageDuration(enchant);
if (strLenWithoutEscapes(theItemName) <= 8) {
sprintf(newMonst->info.monsterName, "spectral %s", theItemName);
} else {
switch (rogue.weapon->kind) {
case BROADSWORD:
strcpy(newMonst->info.monsterName, "spectral sword");
break;
case HAMMER:
strcpy(newMonst->info.monsterName, "spectral hammer");
break;
case PIKE:
strcpy(newMonst->info.monsterName, "spectral pike");
break;
case WAR_AXE:
strcpy(newMonst->info.monsterName, "spectral axe");
break;
default:
strcpy(newMonst->info.monsterName, "spectral weapon");
break;
}
}
pmap[newMonst->xLoc][newMonst->yLoc].flags |= HAS_MONSTER;
fadeInMonster(newMonst);
}
updateVision(true);
message(buf, false);
autoID = true;
break;
case W_SLOWING:
slow(defender, weaponSlowDuration(enchant));
if (canDirectlySeeMonster(defender)) {
sprintf(buf, "%s slows down", monstName);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
}
break;
case W_CONFUSION:
defender->status[STATUS_CONFUSED] = max(defender->status[STATUS_CONFUSED], weaponConfusionDuration(enchant));
defender->maxStatus[STATUS_CONFUSED] = defender->status[STATUS_CONFUSED];
if (canDirectlySeeMonster(defender)) {
sprintf(buf, "%s looks very confused", monstName);
buf[DCOLS] = '\0';
combatMessage(buf, messageColorFromVictim(defender));
autoID = true;
}
break;
case W_FORCE:
autoID = forceWeaponHit(defender, theItem);
break;
case W_MERCY:
heal(defender, 50, false);
if (canSeeMonster(defender)) {
autoID = true;
}
break;
case W_PLENTY:
newMonst = cloneMonster(defender, true, true);
if (newMonst) {
flashMonster(newMonst, effectColors[enchantType], 100);
if (canSeeMonster(newMonst)) {
autoID = true;
}
}
break;
default:
break;
}
}
if (autoID) {
autoIdentify(theItem);
}
}
void attackVerb(char returnString[DCOLS], creature *attacker, short hitPercentile) {
short verbCount, increment;
if (attacker != &player && (player.status[STATUS_HALLUCINATING] || !canSeeMonster(attacker))) {
strcpy(returnString, "hits");
return;
}
if (attacker == &player && !rogue.weapon) {
strcpy(returnString, "punch");
return;
}
for (verbCount = 0; verbCount < 4 && monsterText[attacker->info.monsterID].attack[verbCount + 1][0] != '\0'; verbCount++);
increment = (100 / (verbCount + 1));
hitPercentile = max(0, min(hitPercentile, increment * (verbCount + 1) - 1));
strcpy(returnString, monsterText[attacker->info.monsterID].attack[hitPercentile / increment]);
resolvePronounEscapes(returnString, attacker);
}
void applyArmorRunicEffect(char returnString[DCOLS], creature *attacker, short *damage, boolean melee) {
char armorName[DCOLS], attackerName[DCOLS], monstName[DCOLS], buf[DCOLS * 3];
boolean runicKnown;
boolean runicDiscovered;
short newDamage, dir, newX, newY, count, i;
float enchant;
creature *monst, *hitList[8];
returnString[0] = '\0';
if (!(rogue.armor && rogue.armor->flags & ITEM_RUNIC)) {
return; // just in case
}
enchant = netEnchant(rogue.armor);
runicKnown = rogue.armor->flags & ITEM_RUNIC_IDENTIFIED;
runicDiscovered = false;
itemName(rogue.armor, armorName, false, false, NULL);
monsterName(attackerName, attacker, true);
switch (rogue.armor->enchant2) {
case A_VANISHING:
if (melee && !(attacker->info.flags & (MONST_INANIMATE | MONST_INVULNERABLE)) && rand_percent(armorVanishChance(enchant))) {
if (!(player.status[STATUS_INVISIBLE])){
ninjaVanish(attacker, &player, armorVanishDuration(enchant));
}
}
break;
case A_MULTIPLICITY:
if (melee && !(attacker->info.flags & (MONST_INANIMATE | MONST_INVULNERABLE)) && rand_percent(33)) {
for (i = 0; i < armorImageCount(enchant); i++) {
monst = cloneMonster(attacker, false, true);
monst->bookkeepingFlags |= (MB_FOLLOWER | MB_BOUND_TO_LEADER | MB_DOES_NOT_TRACK_LEADER | MB_TELEPATHICALLY_REVEALED);
monst->info.flags |= MONST_DIES_IF_NEGATED;
monst->bookkeepingFlags &= ~(MB_JUST_SUMMONED | MB_SEIZED | MB_SEIZING);
monst->info.abilityFlags &= ~(MA_CAST_SUMMON | MA_DF_ON_DEATH); // No summoning by spectral images. Gotta draw the line!
// Also no exploding or infecting by spectral clones.
monst->leader = &player;
monst->creatureState = MONSTER_ALLY;
monst->status[STATUS_DISCORDANT] = 0; // Otherwise things can get out of control...
monst->ticksUntilTurn = 100;
monst->info.monsterID = MK_SPECTRAL_IMAGE;
if (monst->carriedMonster) {
killCreature(monst->carriedMonster, true); // Otherwise you can get infinite phoenices from a discordant phoenix.
monst->carriedMonster = NULL;
}
// Give it the glowy red light and color.
monst->info.intrinsicLightType = SPECTRAL_IMAGE_LIGHT;
monst->info.foreColor = &spectralImageColor;
// Temporary guest!
monst->status[STATUS_LIFESPAN_REMAINING] = monst->maxStatus[STATUS_LIFESPAN_REMAINING] = 3;
monst->currentHP = monst->info.maxHP = 1;
monst->info.defense = 0;
if (strLenWithoutEscapes(attacker->info.monsterName) <= 6) {
sprintf(monst->info.monsterName, "spectral %s", attacker->info.monsterName);
} else {
strcpy(monst->info.monsterName, "spectral clone");
}
fadeInMonster(monst);
}
updateVision(true);
runicDiscovered = true;
sprintf(returnString, "Your %s flashes, and spectral images of %s appear!", armorName, attackerName);
}
break;
case A_MUTUALITY:
if (*damage > 0) {
count = 0;
for (i=0; i<8; i++) {
hitList[i] = NULL;
dir = i % 8;
newX = player.xLoc + nbDirs[dir][0];
newY = player.yLoc + nbDirs[dir][1];
if (coordinatesAreInMap(newX, newY) && (pmap[newX][newY].flags & HAS_MONSTER)) {
monst = monsterAtLoc(newX, newY);
if (monst
&& monst != attacker
&& monstersAreEnemies(&player, monst)
&& !(monst->info.flags & (MONST_IMMUNE_TO_WEAPONS | MONST_INVULNERABLE))
&& !(monst->bookkeepingFlags & MB_IS_DYING)) {
hitList[i] = monst;
count++;
}
}
}
if (count) {
for (i=0; i<8; i++) {
if (hitList[i] && !(hitList[i]->bookkeepingFlags & MB_IS_DYING)) {
monsterName(monstName, hitList[i], true);
if (inflictDamage(&player, hitList[i], (*damage + count) / (count + 1), &blue, true)