-
Notifications
You must be signed in to change notification settings - Fork 452
/
artifact.c
2047 lines (1865 loc) · 67.7 KB
/
artifact.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
/* NetHack 3.6 artifact.c $NHDT-Date: 1446369462 2015/11/01 09:17:42 $ $NHDT-Branch: master $:$NHDT-Revision: 1.96 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "artifact.h"
#include "artilist.h"
/*
* Note: both artilist[] and artiexist[] have a dummy element #0,
* so loops over them should normally start at #1. The primary
* exception is the save & restore code, which doesn't care about
* the contents, just the total size.
*/
extern boolean notonhead; /* for long worms */
#define get_artifact(o) \
(((o) && (o)->oartifact) ? &artilist[(int) (o)->oartifact] : 0)
STATIC_DCL boolean
FDECL(bane_applies, (const struct artifact *, struct monst *));
STATIC_DCL int FDECL(spec_applies, (const struct artifact *, struct monst *));
STATIC_DCL int FDECL(arti_invoke, (struct obj *));
STATIC_DCL boolean
FDECL(Mb_hit, (struct monst * magr, struct monst *mdef, struct obj *, int *,
int, BOOLEAN_P, char *));
STATIC_DCL unsigned long FDECL(abil_to_spfx, (long *));
STATIC_DCL uchar FDECL(abil_to_adtyp, (long *));
STATIC_DCL boolean FDECL(untouchable, (struct obj *, BOOLEAN_P));
/* The amount added to the victim's total hit points to insure that the
victim will be killed even after damage bonus/penalty adjustments.
Most such penalties are small, and 200 is plenty; the exception is
half physical damage. 3.3.1 and previous versions tried to use a very
large number to account for this case; now, we just compute the fatal
damage by adding it to 2 times the total hit points instead of 1 time.
Note: this will still break if they have more than about half the number
of hit points that will fit in a 15 bit integer. */
#define FATAL_DAMAGE_MODIFIER 200
/* coordinate effects from spec_dbon() with messages in artifact_hit() */
STATIC_OVL int spec_dbon_applies = 0;
/* flags including which artifacts have already been created */
static boolean artiexist[1 + NROFARTIFACTS + 1];
/* and a discovery list for them (no dummy first entry here) */
STATIC_OVL xchar artidisco[NROFARTIFACTS];
STATIC_DCL void NDECL(hack_artifacts);
STATIC_DCL boolean FDECL(attacks, (int, struct obj *));
/* handle some special cases; must be called after u_init() */
STATIC_OVL void
hack_artifacts()
{
struct artifact *art;
int alignmnt = aligns[flags.initalign].value;
/* Fix up the alignments of "gift" artifacts */
for (art = artilist + 1; art->otyp; art++)
if (art->role == Role_switch && art->alignment != A_NONE)
art->alignment = alignmnt;
/* Excalibur can be used by any lawful character, not just knights */
if (!Role_if(PM_KNIGHT))
artilist[ART_EXCALIBUR].role = NON_PM;
/* Fix up the quest artifact */
if (urole.questarti) {
artilist[urole.questarti].alignment = alignmnt;
artilist[urole.questarti].role = Role_switch;
}
return;
}
/* zero out the artifact existence list */
void
init_artifacts()
{
(void) memset((genericptr_t) artiexist, 0, sizeof artiexist);
(void) memset((genericptr_t) artidisco, 0, sizeof artidisco);
hack_artifacts();
}
void
save_artifacts(fd)
int fd;
{
bwrite(fd, (genericptr_t) artiexist, sizeof artiexist);
bwrite(fd, (genericptr_t) artidisco, sizeof artidisco);
}
void
restore_artifacts(fd)
int fd;
{
mread(fd, (genericptr_t) artiexist, sizeof artiexist);
mread(fd, (genericptr_t) artidisco, sizeof artidisco);
hack_artifacts(); /* redo non-saved special cases */
}
const char *
artiname(artinum)
int artinum;
{
if (artinum <= 0 || artinum > NROFARTIFACTS)
return "";
return artilist[artinum].name;
}
/*
Make an artifact. If a specific alignment is specified, then an object of
the appropriate alignment is created from scratch, or 0 is returned if
none is available. (If at least one aligned artifact has already been
given, then unaligned ones also become eligible for this.)
If no alignment is given, then 'otmp' is converted
into an artifact of matching type, or returned as-is if that's not
possible.
For the 2nd case, caller should use ``obj = mk_artifact(obj, A_NONE);''
for the 1st, ``obj = mk_artifact((struct obj *)0, some_alignment);''.
*/
struct obj *
mk_artifact(otmp, alignment)
struct obj *otmp; /* existing object; ignored if alignment specified */
aligntyp alignment; /* target alignment, or A_NONE */
{
const struct artifact *a;
int m, n, altn;
boolean by_align = (alignment != A_NONE);
short o_typ = (by_align || !otmp) ? 0 : otmp->otyp;
boolean unique = !by_align && otmp && objects[o_typ].oc_unique;
short eligible[NROFARTIFACTS];
n = altn = 0; /* no candidates found yet */
eligible[0] = 0; /* lint suppression */
/* gather eligible artifacts */
for (m = 1, a = &artilist[m]; a->otyp; a++, m++) {
if (artiexist[m])
continue;
if ((a->spfx & SPFX_NOGEN) || unique)
continue;
if (!by_align) {
/* looking for a particular type of item; not producing a
divine gift so we don't care about role's first choice */
if (a->otyp == o_typ)
eligible[n++] = m;
continue; /* move on to next possibility */
}
/* we're looking for an alignment-specific item
suitable for hero's role+race */
if ((a->alignment == alignment || a->alignment == A_NONE)
/* avoid enemies' equipment */
&& (a->race == NON_PM || !race_hostile(&mons[a->race]))) {
/* when a role-specific first choice is available, use it */
if (Role_if(a->role)) {
/* make this be the only possibility in the list */
eligible[0] = m;
n = 1;
break; /* skip all other candidates */
}
/* found something to consider for random selection */
if (a->alignment != A_NONE || u.ugifts > 0) {
/* right alignment, or non-aligned with at least 1
previous gift bestowed, makes this one viable */
eligible[n++] = m;
} else {
/* non-aligned with no previous gifts;
if no candidates have been found yet, record
this one as a[nother] fallback possibility in
case all aligned candidates have been used up
(via wishing, naming, bones, random generation) */
if (!n)
eligible[altn++] = m;
/* [once a regular candidate is found, the list
is overwritten and `altn' becomes irrelevant] */
}
}
}
/* resort to fallback list if main list was empty */
if (!n)
n = altn;
if (n) {
/* found at least one candidate; pick one at random */
m = eligible[rn2(n)]; /* [0..n-1] */
a = &artilist[m];
/* make an appropriate object if necessary, then christen it */
if (by_align)
otmp = mksobj((int) a->otyp, TRUE, FALSE);
if (otmp) {
otmp = oname(otmp, a->name);
otmp->oartifact = m;
artiexist[m] = TRUE;
}
} else {
/* nothing appropriate could be found; return original object */
if (by_align)
otmp = 0; /* (there was no original object) */
}
return otmp;
}
/*
* Returns the full name (with articles and correct capitalization) of an
* artifact named "name" if one exists, or NULL, it not.
* The given name must be rather close to the real name for it to match.
* The object type of the artifact is returned in otyp if the return value
* is non-NULL.
*/
const char *
artifact_name(name, otyp)
const char *name;
short *otyp;
{
register const struct artifact *a;
register const char *aname;
if (!strncmpi(name, "the ", 4))
name += 4;
for (a = artilist + 1; a->otyp; a++) {
aname = a->name;
if (!strncmpi(aname, "the ", 4))
aname += 4;
if (!strcmpi(name, aname)) {
*otyp = a->otyp;
return a->name;
}
}
return (char *) 0;
}
boolean
exist_artifact(otyp, name)
int otyp;
const char *name;
{
register const struct artifact *a;
boolean *arex;
if (otyp && *name)
for (a = artilist + 1, arex = artiexist + 1; a->otyp; a++, arex++)
if ((int) a->otyp == otyp && !strcmp(a->name, name))
return *arex;
return FALSE;
}
void
artifact_exists(otmp, name, mod)
struct obj *otmp;
const char *name;
boolean mod;
{
register const struct artifact *a;
if (otmp && *name)
for (a = artilist + 1; a->otyp; a++)
if (a->otyp == otmp->otyp && !strcmp(a->name, name)) {
register int m = (int) (a - artilist);
otmp->oartifact = (char) (mod ? m : 0);
otmp->age = 0;
if (otmp->otyp == RIN_INCREASE_DAMAGE)
otmp->spe = 0;
artiexist[m] = mod;
break;
}
return;
}
int
nartifact_exist()
{
int a = 0;
int n = SIZE(artiexist);
while (n > 1)
if (artiexist[--n])
a++;
return a;
}
boolean
spec_ability(otmp, abil)
struct obj *otmp;
unsigned long abil;
{
const struct artifact *arti = get_artifact(otmp);
return (boolean) (arti && (arti->spfx & abil) != 0L);
}
/* used so that callers don't need to known about SPFX_ codes */
boolean
confers_luck(obj)
struct obj *obj;
{
/* might as well check for this too */
if (obj->otyp == LUCKSTONE)
return TRUE;
return (boolean) (obj->oartifact && spec_ability(obj, SPFX_LUCK));
}
/* used to check whether a monster is getting reflection from an artifact */
boolean
arti_reflects(obj)
struct obj *obj;
{
const struct artifact *arti = get_artifact(obj);
if (arti) {
/* while being worn */
if ((obj->owornmask & ~W_ART) && (arti->spfx & SPFX_REFLECT))
return TRUE;
/* just being carried */
if (arti->cspfx & SPFX_REFLECT)
return TRUE;
}
return FALSE;
}
/* decide whether this obj is effective when attacking against shades;
does not consider the bonus for blessed objects versus undead */
boolean
shade_glare(obj)
struct obj *obj;
{
const struct artifact *arti;
/* any silver object is effective */
if (objects[obj->otyp].oc_material == SILVER)
return TRUE;
/* non-silver artifacts with bonus against undead also are effective */
arti = get_artifact(obj);
if (arti && (arti->spfx & SPFX_DFLAG2) && arti->mtype == M2_UNDEAD)
return TRUE;
/* [if there was anything with special bonus against noncorporeals,
it would be effective too] */
/* otherwise, harmless to shades */
return FALSE;
}
/* returns 1 if name is restricted for otmp->otyp */
boolean
restrict_name(otmp, name)
struct obj *otmp;
const char *name;
{
register const struct artifact *a;
const char *aname, *odesc, *other;
boolean sametype[NUM_OBJECTS];
int i, lo, hi, otyp = otmp->otyp, ocls = objects[otyp].oc_class;
if (!*name)
return FALSE;
if (!strncmpi(name, "the ", 4))
name += 4;
/* decide what types of objects are the same as otyp;
if it's been discovered, then only itself matches;
otherwise, include all other undiscovered objects
of the same class which have the same description
or share the same pool of shuffled descriptions */
(void) memset((genericptr_t) sametype, 0, sizeof sametype); /* FALSE */
sametype[otyp] = TRUE;
if (!objects[otyp].oc_name_known
&& (odesc = OBJ_DESCR(objects[otyp])) != 0) {
obj_shuffle_range(otyp, &lo, &hi);
for (i = bases[ocls]; i < NUM_OBJECTS; i++) {
if (objects[i].oc_class != ocls)
break;
if (!objects[i].oc_name_known
&& (other = OBJ_DESCR(objects[i])) != 0
&& (!strcmp(odesc, other) || (i >= lo && i <= hi)))
sametype[i] = TRUE;
}
}
/* Since almost every artifact is SPFX_RESTR, it doesn't cost
us much to do the string comparison before the spfx check.
Bug fix: don't name multiple elven daggers "Sting".
*/
for (a = artilist + 1; a->otyp; a++) {
if (!sametype[a->otyp])
continue;
aname = a->name;
if (!strncmpi(aname, "the ", 4))
aname += 4;
if (!strcmp(aname, name))
return (boolean) ((a->spfx & (SPFX_NOGEN | SPFX_RESTR)) != 0
|| otmp->quan > 1L);
}
return FALSE;
}
STATIC_OVL boolean
attacks(adtyp, otmp)
int adtyp;
struct obj *otmp;
{
register const struct artifact *weap;
if ((weap = get_artifact(otmp)) != 0)
return (boolean) (weap->attk.adtyp == adtyp);
return FALSE;
}
boolean
defends(adtyp, otmp)
int adtyp;
struct obj *otmp;
{
register const struct artifact *weap;
if ((weap = get_artifact(otmp)) != 0)
return (boolean) (weap->defn.adtyp == adtyp);
return FALSE;
}
/* used for monsters */
boolean
defends_when_carried(adtyp, otmp)
int adtyp;
struct obj *otmp;
{
register const struct artifact *weap;
if ((weap = get_artifact(otmp)) != 0)
return (boolean) (weap->cary.adtyp == adtyp);
return FALSE;
}
/* determine whether an item confers Protection */
boolean
protects(otmp, being_worn)
struct obj *otmp;
boolean being_worn;
{
const struct artifact *arti;
if (being_worn && objects[otmp->otyp].oc_oprop == PROTECTION)
return TRUE;
arti = get_artifact(otmp);
if (!arti)
return FALSE;
return (boolean) ((arti->cspfx & SPFX_PROTECT) != 0
|| (being_worn && (arti->spfx & SPFX_PROTECT) != 0));
}
/*
* a potential artifact has just been worn/wielded/picked-up or
* unworn/unwielded/dropped. Pickup/drop only set/reset the W_ART mask.
*/
void
set_artifact_intrinsic(otmp, on, wp_mask)
register struct obj *otmp;
boolean on;
long wp_mask;
{
long *mask = 0;
register const struct artifact *oart = get_artifact(otmp);
register uchar dtyp;
register long spfx;
if (!oart)
return;
/* effects from the defn field */
dtyp = (wp_mask != W_ART) ? oart->defn.adtyp : oart->cary.adtyp;
if (dtyp == AD_FIRE)
mask = &EFire_resistance;
else if (dtyp == AD_COLD)
mask = &ECold_resistance;
else if (dtyp == AD_ELEC)
mask = &EShock_resistance;
else if (dtyp == AD_MAGM)
mask = &EAntimagic;
else if (dtyp == AD_DISN)
mask = &EDisint_resistance;
else if (dtyp == AD_DRST)
mask = &EPoison_resistance;
if (mask && wp_mask == W_ART && !on) {
/* find out if some other artifact also confers this intrinsic */
/* if so, leave the mask alone */
register struct obj *obj;
for (obj = invent; obj; obj = obj->nobj)
if (obj != otmp && obj->oartifact) {
register const struct artifact *art = get_artifact(obj);
if (art->cary.adtyp == dtyp) {
mask = (long *) 0;
break;
}
}
}
if (mask) {
if (on)
*mask |= wp_mask;
else
*mask &= ~wp_mask;
}
/* intrinsics from the spfx field; there could be more than one */
spfx = (wp_mask != W_ART) ? oart->spfx : oart->cspfx;
if (spfx && wp_mask == W_ART && !on) {
/* don't change any spfx also conferred by other artifacts */
register struct obj *obj;
for (obj = invent; obj; obj = obj->nobj)
if (obj != otmp && obj->oartifact) {
register const struct artifact *art = get_artifact(obj);
spfx &= ~art->cspfx;
}
}
if (spfx & SPFX_SEARCH) {
if (on)
ESearching |= wp_mask;
else
ESearching &= ~wp_mask;
}
if (spfx & SPFX_HALRES) {
/* make_hallucinated must (re)set the mask itself to get
* the display right */
/* restoring needed because this is the only artifact intrinsic
* that can print a message--need to guard against being printed
* when restoring a game
*/
(void) make_hallucinated((long) !on, restoring ? FALSE : TRUE,
wp_mask);
}
if (spfx & SPFX_ESP) {
if (on)
ETelepat |= wp_mask;
else
ETelepat &= ~wp_mask;
see_monsters();
}
if (spfx & SPFX_STLTH) {
if (on)
EStealth |= wp_mask;
else
EStealth &= ~wp_mask;
}
if (spfx & SPFX_REGEN) {
if (on)
ERegeneration |= wp_mask;
else
ERegeneration &= ~wp_mask;
}
if (spfx & SPFX_TCTRL) {
if (on)
ETeleport_control |= wp_mask;
else
ETeleport_control &= ~wp_mask;
}
if (spfx & SPFX_WARN) {
if (spec_m2(otmp)) {
if (on) {
EWarn_of_mon |= wp_mask;
context.warntype.obj |= spec_m2(otmp);
} else {
EWarn_of_mon &= ~wp_mask;
context.warntype.obj &= ~spec_m2(otmp);
}
see_monsters();
} else {
if (on)
EWarning |= wp_mask;
else
EWarning &= ~wp_mask;
}
}
if (spfx & SPFX_EREGEN) {
if (on)
EEnergy_regeneration |= wp_mask;
else
EEnergy_regeneration &= ~wp_mask;
}
if (spfx & SPFX_HSPDAM) {
if (on)
EHalf_spell_damage |= wp_mask;
else
EHalf_spell_damage &= ~wp_mask;
}
if (spfx & SPFX_HPHDAM) {
if (on)
EHalf_physical_damage |= wp_mask;
else
EHalf_physical_damage &= ~wp_mask;
}
if (spfx & SPFX_XRAY) {
/* this assumes that no one else is using xray_range */
if (on)
u.xray_range = 3;
else
u.xray_range = -1;
vision_full_recalc = 1;
}
if ((spfx & SPFX_REFLECT) && (wp_mask & W_WEP)) {
if (on)
EReflecting |= wp_mask;
else
EReflecting &= ~wp_mask;
}
if (spfx & SPFX_PROTECT) {
if (on)
EProtection |= wp_mask;
else
EProtection &= ~wp_mask;
}
if (wp_mask == W_ART && !on && oart->inv_prop) {
/* might have to turn off invoked power too */
if (oart->inv_prop <= LAST_PROP
&& (u.uprops[oart->inv_prop].extrinsic & W_ARTI))
(void) arti_invoke(otmp);
}
}
/* touch_artifact()'s return value isn't sufficient to tell whether it
dished out damage, and tracking changes to u.uhp, u.mh, Lifesaved
when trying to avoid second wounding is too cumbersome */
STATIC_VAR boolean touch_blasted; /* for retouch_object() */
/*
* creature (usually hero) tries to touch (pick up or wield) an artifact obj.
* Returns 0 if the object refuses to be touched.
* This routine does not change any object chains.
* Ignores such things as gauntlets, assuming the artifact is not
* fooled by such trappings.
*/
int
touch_artifact(obj, mon)
struct obj *obj;
struct monst *mon;
{
register const struct artifact *oart = get_artifact(obj);
boolean badclass, badalign, self_willed, yours;
touch_blasted = FALSE;
if (!oart)
return 1;
yours = (mon == &youmonst);
/* all quest artifacts are self-willed; it this ever changes, `badclass'
will have to be extended to explicitly include quest artifacts */
self_willed = ((oart->spfx & SPFX_INTEL) != 0);
if (yours) {
badclass = self_willed
&& ((oart->role != NON_PM && !Role_if(oart->role))
|| (oart->race != NON_PM && !Race_if(oart->race)));
badalign =
(oart->spfx & SPFX_RESTR) && oart->alignment != A_NONE
&& (oart->alignment != u.ualign.type || u.ualign.record < 0);
} else if (!is_covetous(mon->data) && !is_mplayer(mon->data)) {
badclass = self_willed && oart->role != NON_PM
&& oart != &artilist[ART_EXCALIBUR];
badalign = (oart->spfx & SPFX_RESTR) && oart->alignment != A_NONE
&& (oart->alignment != mon_aligntyp(mon));
} else { /* an M3_WANTSxxx monster or a fake player */
/* special monsters trying to take the Amulet, invocation tools or
quest item can touch anything except `spec_applies' artifacts */
badclass = badalign = FALSE;
}
/* weapons which attack specific categories of monsters are
bad for them even if their alignments happen to match */
if (!badalign)
badalign = bane_applies(oart, mon);
if (((badclass || badalign) && self_willed)
|| (badalign && (!yours || !rn2(4)))) {
int dmg, tmp;
char buf[BUFSZ];
if (!yours)
return 0;
You("are blasted by %s power!", s_suffix(the(xname(obj))));
touch_blasted = TRUE;
dmg = d((Antimagic ? 2 : 4), (self_willed ? 10 : 4));
/* add half (maybe quarter) of the usual silver damage bonus */
if (objects[obj->otyp].oc_material == SILVER && Hate_silver)
tmp = rnd(10), dmg += Maybe_Half_Phys(tmp);
Sprintf(buf, "touching %s", oart->name);
losehp(dmg, buf, KILLED_BY); /* magic damage, not physical */
exercise(A_WIS, FALSE);
}
/* can pick it up unless you're totally non-synch'd with the artifact */
if (badclass && badalign && self_willed) {
if (yours) {
if (!carried(obj))
pline("%s your grasp!", Tobjnam(obj, "evade"));
else
pline("%s beyond your control!", Tobjnam(obj, "are"));
}
return 0;
}
return 1;
}
/* decide whether an artifact itself is vulnerable to a particular type
of erosion damage, independent of the properties of its bearer */
boolean
arti_immune(obj, dtyp)
struct obj *obj;
int dtyp;
{
register const struct artifact *weap = get_artifact(obj);
if (!weap)
return FALSE;
if (dtyp == AD_PHYS)
return FALSE; /* nothing is immune to phys dmg */
return (boolean) (weap->attk.adtyp == dtyp
|| weap->defn.adtyp == dtyp
|| weap->cary.adtyp == dtyp);
}
STATIC_OVL boolean
bane_applies(oart, mon)
const struct artifact *oart;
struct monst *mon;
{
struct artifact atmp;
if (oart && (oart->spfx & SPFX_DBONUS) != 0) {
atmp = *oart;
atmp.spfx &= SPFX_DBONUS; /* clear other spfx fields */
if (spec_applies(&atmp, mon))
return TRUE;
}
return FALSE;
}
/* decide whether an artifact's special attacks apply against mtmp */
STATIC_OVL int
spec_applies(weap, mtmp)
register const struct artifact *weap;
struct monst *mtmp;
{
struct permonst *ptr;
boolean yours;
if (!(weap->spfx & (SPFX_DBONUS | SPFX_ATTK)))
return (weap->attk.adtyp == AD_PHYS);
yours = (mtmp == &youmonst);
ptr = mtmp->data;
if (weap->spfx & SPFX_DMONS) {
return (ptr == &mons[(int) weap->mtype]);
} else if (weap->spfx & SPFX_DCLAS) {
return (weap->mtype == (unsigned long) ptr->mlet);
} else if (weap->spfx & SPFX_DFLAG1) {
return ((ptr->mflags1 & weap->mtype) != 0L);
} else if (weap->spfx & SPFX_DFLAG2) {
return ((ptr->mflags2 & weap->mtype)
|| (yours
&& ((!Upolyd && (urace.selfmask & weap->mtype))
|| ((weap->mtype & M2_WERE) && u.ulycn >= LOW_PM))));
} else if (weap->spfx & SPFX_DALIGN) {
return yours ? (u.ualign.type != weap->alignment)
: (ptr->maligntyp == A_NONE
|| sgn(ptr->maligntyp) != weap->alignment);
} else if (weap->spfx & SPFX_ATTK) {
struct obj *defending_weapon = (yours ? uwep : MON_WEP(mtmp));
if (defending_weapon && defending_weapon->oartifact
&& defends((int) weap->attk.adtyp, defending_weapon))
return FALSE;
switch (weap->attk.adtyp) {
case AD_FIRE:
return !(yours ? Fire_resistance : resists_fire(mtmp));
case AD_COLD:
return !(yours ? Cold_resistance : resists_cold(mtmp));
case AD_ELEC:
return !(yours ? Shock_resistance : resists_elec(mtmp));
case AD_MAGM:
case AD_STUN:
return !(yours ? Antimagic : (rn2(100) < ptr->mr));
case AD_DRST:
return !(yours ? Poison_resistance : resists_poison(mtmp));
case AD_DRLI:
return !(yours ? Drain_resistance : resists_drli(mtmp));
case AD_STON:
return !(yours ? Stone_resistance : resists_ston(mtmp));
default:
impossible("Weird weapon special attack.");
}
}
return 0;
}
/* return the M2 flags of monster that an artifact's special attacks apply
* against */
long
spec_m2(otmp)
struct obj *otmp;
{
const struct artifact *artifact = get_artifact(otmp);
if (artifact)
return artifact->mtype;
return 0L;
}
/* special attack bonus */
int
spec_abon(otmp, mon)
struct obj *otmp;
struct monst *mon;
{
const struct artifact *weap = get_artifact(otmp);
/* no need for an extra check for `NO_ATTK' because this will
always return 0 for any artifact which has that attribute */
if (weap && weap->attk.damn && spec_applies(weap, mon))
return rnd((int) weap->attk.damn);
return 0;
}
/* special damage bonus */
int
spec_dbon(otmp, mon, tmp)
struct obj *otmp;
struct monst *mon;
int tmp;
{
register const struct artifact *weap = get_artifact(otmp);
if (!weap || (weap->attk.adtyp == AD_PHYS /* check for `NO_ATTK' */
&& weap->attk.damn == 0 && weap->attk.damd == 0))
spec_dbon_applies = FALSE;
else if (otmp->oartifact == ART_GRIMTOOTH)
/* Grimtooth has SPFX settings to warn against elves but we want its
damage bonus to apply to all targets, so bypass spec_applies() */
spec_dbon_applies = TRUE;
else
spec_dbon_applies = spec_applies(weap, mon);
if (spec_dbon_applies)
return weap->attk.damd ? rnd((int) weap->attk.damd) : max(tmp, 1);
return 0;
}
/* add identified artifact to discoveries list */
void
discover_artifact(m)
xchar m;
{
int i;
/* look for this artifact in the discoveries list;
if we hit an empty slot then it's not present, so add it */
for (i = 0; i < NROFARTIFACTS; i++)
if (artidisco[i] == 0 || artidisco[i] == m) {
artidisco[i] = m;
return;
}
/* there is one slot per artifact, so we should never reach the
end without either finding the artifact or an empty slot... */
impossible("couldn't discover artifact (%d)", (int) m);
}
/* used to decide whether an artifact has been fully identified */
boolean
undiscovered_artifact(m)
xchar m;
{
int i;
/* look for this artifact in the discoveries list;
if we hit an empty slot then it's undiscovered */
for (i = 0; i < NROFARTIFACTS; i++)
if (artidisco[i] == m)
return FALSE;
else if (artidisco[i] == 0)
break;
return TRUE;
}
/* display a list of discovered artifacts; return their count */
int
disp_artifact_discoveries(tmpwin)
winid tmpwin; /* supplied by dodiscover() */
{
int i, m, otyp;
char buf[BUFSZ];
for (i = 0; i < NROFARTIFACTS; i++) {
if (artidisco[i] == 0)
break; /* empty slot implies end of list */
if (tmpwin == WIN_ERR)
continue; /* for WIN_ERR, we just count */
if (i == 0)
putstr(tmpwin, iflags.menu_headings, "Artifacts");
m = artidisco[i];
otyp = artilist[m].otyp;
Sprintf(buf, " %s [%s %s]", artiname(m),
align_str(artilist[m].alignment), simple_typename(otyp));
putstr(tmpwin, 0, buf);
}
return i;
}
/*
* Magicbane's intrinsic magic is incompatible with normal
* enchantment magic. Thus, its effects have a negative
* dependence on spe. Against low mr victims, it typically
* does "double athame" damage, 2d4. Occasionally, it will
* cast unbalancing magic which effectively averages out to
* 4d4 damage (3d4 against high mr victims), for spe = 0.
*
* Prior to 3.4.1, the cancel (aka purge) effect always
* included the scare effect too; now it's one or the other.
* Likewise, the stun effect won't be combined with either
* of those two; it will be chosen separately or possibly
* used as a fallback when scare or cancel fails.
*
* [Historical note: a change to artifact_hit() for 3.4.0
* unintentionally made all of Magicbane's special effects
* be blocked if the defender successfully saved against a
* stun attack. As of 3.4.1, those effects can occur but
* will be slightly less likely than they were in 3.3.x.]
*/
#define MB_MAX_DIEROLL 8 /* rolls above this aren't magical */
static const char *const mb_verb[2][4] = {
{ "probe", "stun", "scare", "cancel" },
{ "prod", "amaze", "tickle", "purge" },
};
#define MB_INDEX_PROBE 0
#define MB_INDEX_STUN 1
#define MB_INDEX_SCARE 2
#define MB_INDEX_CANCEL 3
/* called when someone is being hit by Magicbane */
STATIC_OVL boolean
Mb_hit(magr, mdef, mb, dmgptr, dieroll, vis, hittee)
struct monst *magr, *mdef; /* attacker and defender */
struct obj *mb; /* Magicbane */
int *dmgptr; /* extra damage target will suffer */
int dieroll; /* d20 that has already scored a hit */
boolean vis; /* whether the action can be seen */
char *hittee; /* target's name: "you" or mon_nam(mdef) */
{
struct permonst *old_uasmon;
const char *verb, *fakename;
boolean youattack = (magr == &youmonst), youdefend = (mdef == &youmonst),
resisted = FALSE, do_stun, do_confuse, result;
int attack_indx, scare_dieroll = MB_MAX_DIEROLL / 2;
result = FALSE; /* no message given yet */
/* the most severe effects are less likely at higher enchantment */
if (mb->spe >= 3)
scare_dieroll /= (1 << (mb->spe / 3));
/* if target successfully resisted the artifact damage bonus,
reduce overall likelihood of the assorted special effects */
if (!spec_dbon_applies)
dieroll += 1;
/* might stun even when attempting a more severe effect, but
in that case it will only happen if the other effect fails;
extra damage will apply regardless; 3.4.1: sometimes might
just probe even when it hasn't been enchanted */
do_stun = (max(mb->spe, 0) < rn2(spec_dbon_applies ? 11 : 7));
/* the special effects also boost physical damage; increments are
generally cumulative, but since the stun effect is based on a
different criterium its damage might not be included; the base
damage is either 1d4 (athame) or 2d4 (athame+spec_dbon) depending
on target's resistance check against AD_STUN (handled by caller)
[note that a successful save against AD_STUN doesn't actually
prevent the target from ending up stunned] */
attack_indx = MB_INDEX_PROBE;
*dmgptr += rnd(4); /* (2..3)d4 */
if (do_stun) {
attack_indx = MB_INDEX_STUN;
*dmgptr += rnd(4); /* (3..4)d4 */
}
if (dieroll <= scare_dieroll) {
attack_indx = MB_INDEX_SCARE;
*dmgptr += rnd(4); /* (3..5)d4 */
}
if (dieroll <= (scare_dieroll / 2)) {
attack_indx = MB_INDEX_CANCEL;
*dmgptr += rnd(4); /* (4..6)d4 */
}