-
Notifications
You must be signed in to change notification settings - Fork 6
/
artifact.c
1813 lines (1614 loc) · 62.6 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
/* vim:set cin ft=c sw=4 sts=4 ts=8 et ai cino=Ls\:0t0(0 : -*- mode:c;fill-column:80;tab-width:8;c-basic-offset:4;indent-tabs-mode:nil;c-file-style:"k&r" -*-*/
/* Last modified by Fredrik Ljungdahl, 2020-08-30 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#define ARTIFACT_C
#include "artilist.h"
#include "lev.h"
/*
* Note: both artilist[] and artigen[] 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.
*/
#define get_artifact(o) \
(((o)&&(o)->oartifact) ? &artilist[(int) (o)->oartifact] : 0)
static int spec_applies(const struct artifact *, const struct monst *);
static int arti_invoke(struct obj *);
static boolean magicbane_hit(struct monst *magr, struct monst *mdef,
struct obj *, int *, int, boolean,
const char *, int);
static boolean spfx_provides_extrinsic(const struct obj *,
unsigned long, int, int *);
static long spec_m2(const struct obj *);
/* 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
/* flags including which artifacts have already been created */
static boolean artigen[1 + NROFARTIFACTS + 1];
/* and a discovery list for them (no dummy first entry here) */
static xchar artidisco[NROFARTIFACTS];
static void hack_artifacts(void);
static boolean attacks(int, struct obj *);
void
init_artilist(void)
{
artilist = malloc(sizeof (const_artilist));
memcpy(artilist, const_artilist, sizeof (const_artilist));
}
/* handle some special cases; must be called after u_init() */
static void
hack_artifacts(void)
{
struct artifact *art;
int alignmnt = aligns[u.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(artigen, 0, sizeof artigen);
memset(artidisco, 0, sizeof artidisco);
hack_artifacts();
}
void
save_artifacts(struct memfile *mf)
{
/* artigen and artidisco are arrays of bytes, so writing them in one go
is safe and portable */
mtag(mf, 0, MTAG_ARTIFACT);
mwrite(mf, artigen, sizeof artigen);
mwrite(mf, artidisco, sizeof artidisco);
}
void
restore_artifacts(struct memfile *mf)
{
mread(mf, artigen, sizeof artigen);
mread(mf, artidisco, sizeof artidisco);
hack_artifacts(); /* redo non-saved special cases */
}
const char *
artiname(int artinum)
{
if (artinum <= 0 || artinum > NROFARTIFACTS)
return "";
return artilist[artinum].name;
}
int
artityp(int artinum)
{
if (artinum <= 0 || artinum > NROFARTIFACTS)
return 0;
return artilist[artinum].otyp;
}
/*
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(lev, obj, A_NONE);''
for the 1st, ``obj = mk_artifact(lev, NULL, some_alignment);''.
*/
struct obj *
mk_artifact(
struct level *lev, /* level to create artifact on if otmp not given */
struct obj *otmp, /* existing object; ignored if alignment specified */
aligntyp alignment, /* target alignment, or A_NONE */
enum rng rng /* which random number generator to use */
) {
const struct artifact *a;
int n, m;
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];
static const int round_number = 27720; /* divisible by all numbers 1..12 */
int artif_number = rn2_on_rng(27720, rng);
/* gather eligible artifacts */
for (n = 0, a = artilist + 1, m = 1; a->otyp; a++, m++)
if ((!by_align ? a->otyp ==
o_typ : (a->alignment == alignment ||
(a->alignment == A_NONE && u.ugifts > 0))) &&
(!(a->spfx & SPFX_NOGEN) || unique) && artigen[m] == ag_none) {
if (by_align && a->race != NON_PM && race_hostile(&mons[a->race]))
continue; /* skip enemies' equipment */
if (by_align && !(a->spfx & SPFX_RESTR))
continue; /* don't generate nameable artifacts as gift */
else if (by_align && Role_if(a->role))
goto make_artif; /* 'a' points to the desired one */
else
eligible[n++] = m;
}
/* If we didn't get any eligible artifacts, it's possible that the only
valid first sacrifice gift was already generated. Let's try again
with looser restrictions. */
if (n == 0)
for (n = 0, a = artilist + 1, m = 1; a->otyp; a++, m++)
if ((!by_align ? a->otyp ==
o_typ : (a->alignment == alignment || a->alignment == A_NONE)) &&
(!(a->spfx & SPFX_NOGEN) || unique) && artigen[m] == ag_none) {
if (by_align && a->race != NON_PM && race_hostile(&mons[a->race]))
continue; /* skip enemies' equipment */
else if (by_align && Role_if(a->role))
goto make_artif; /* 'a' points to the desired one */
else
eligible[n++] = m;
}
if (n) { /* found at least one candidate */
m = eligible[artif_number / (round_number / n)]; /* [0..n-1] */
a = &artilist[m];
/* make an appropriate object if necessary, then christen it */
make_artif:
/* note: not rng, that'd desync during level creation
i.e. the /identity/ of the artifact is on the given RNG, but the
/stats/ of the artifact (+1, etc.) are on rng_main */
if (by_align)
otmp = mksobj(lev, (int)a->otyp, TRUE, FALSE, rng_main);
otmp = oname(otmp, a->name);
} else {
/* nothing appropriate could be found; return the 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(const char *name, short *otyp)
{
const struct artifact *a;
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 NULL;
}
boolean
exist_artifact(int otyp, const char *name)
{
const struct artifact *a;
boolean *arex;
if (otyp && *name)
for (a = artilist + 1, arex = artigen + 1; a->otyp; a++, arex++)
if ((int)a->otyp == otyp && !strcmp(a->name, name))
return *arex;
return FALSE;
}
void
artifact_exists(struct obj *otmp, const char *name, int value)
{
const struct artifact *a;
if (otmp && *name)
for (a = artilist + 1; a->otyp; a++)
if (a->otyp == otmp->otyp && !strcmp(a->name, name)) {
int m = a - artilist;
otmp->oartifact = (char)(value ? m : 0);
otmp->age = 0;
artigen[m] = value;
break;
}
return;
}
int
nartifact_exist(void)
{
int a = 0;
int n = SIZE(artigen);
while (n > 1)
if (artigen[--n] != ag_none)
a++;
return a;
}
int
nartifact_value(int value)
{
int a = 0;
int n = SIZE(artigen);
while (n > 1)
if (artigen[--n] == value)
a++;
return a;
}
boolean
spec_ability(struct obj * otmp, unsigned long abil)
{
const struct artifact *arti = get_artifact(otmp);
return (boolean) (arti && (arti->spfx & abil));
}
/* used so that callers don't need to known about SPFX_ codes */
boolean
confers_luck(struct obj * obj)
{
/* might as well check for this too */
if (obj->otyp == LUCKSTONE)
return TRUE;
return obj->oartifact && spec_ability(obj, SPFX_LUCK);
}
/* used to check whether a monster is getting reflection from an artifact */
boolean
arti_reflects(struct obj * obj)
{
const struct artifact *arti = get_artifact(obj);
if (arti) {
/* while being worn */
if ((obj->owornmask & W_MASKABLE) &&
(arti->spfx & SPFX_REFLECT))
return TRUE;
/* just being carried */
if (arti->cspfx & SPFX_REFLECT)
return TRUE;
}
return FALSE;
}
boolean
restrict_name(struct obj * otmp, const char *name)
{ /* returns 1 if name is restricted for otmp->otyp */
const struct artifact *a;
const char *aname;
if (!*name)
return FALSE;
if (!strncmpi(name, "the ", 4))
name += 4;
/* 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 (a->otyp != otmp->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 boolean
attacks(int adtyp, struct obj *otmp)
{
if (!otmp)
return FALSE;
if (!otmp->oartifact) {
/* don't let armor of fire res do fire damage/similar */
if (otmp->oclass != WEAPON_CLASS && !is_weptool(otmp))
return FALSE;
uint64_t props = obj_properties(otmp);
if ((adtyp == AD_FIRE && (props & opm_fire)) ||
(adtyp == AD_COLD && (props & opm_frost)) ||
(adtyp == AD_ELEC && (props & opm_shock)) ||
(adtyp == AD_DRLI && (props & opm_drain)))
return TRUE;
return FALSE;
}
const struct artifact *weap;
if ((weap = get_artifact(otmp)) != 0)
return (boolean) (weap->attk.adtyp == adtyp);
return FALSE;
}
boolean
defends(int adtyp, struct obj * otmp)
{
const struct artifact *weap;
if ((weap = get_artifact(otmp)) != 0)
return (boolean) (weap->defn.adtyp == adtyp);
return FALSE;
}
/* used for monsters */
boolean
protects(int adtyp, struct obj * otmp)
{
const struct artifact *weap;
if ((weap = get_artifact(otmp)) != 0)
return (boolean) (weap->cary.adtyp == adtyp);
return FALSE;
}
/* Returns extrinsics the item provides and in which slot.
For warntypes, returns the result in the provided *warntype
pointer. */
long
item_provides_extrinsic(const struct obj *otmp, int extrinsic,
int *warntype)
{
long res;
res = item_provides_extrinsic_before_oprop(otmp, extrinsic,
warntype);
if (res == W_WORN || res == W_EQUIP)
res &= otmp->owornmask;
if (res && res != W_MASK(os_quiver) &&
(res != W_MASK(os_swapwep) ||
(otmp->where == OBJ_INVENT && u.twoweap)))
return res;
enum objslot slot = which_slot(otmp);
if (slot == os_invalid) {
if (otmp->oclass == WEAPON_CLASS || is_weptool(otmp)) {
res = W_EQUIP;
/* these are only offensive on weapons */
if (extrinsic == FIRE_RES ||
extrinsic == COLD_RES ||
extrinsic == SHOCK_RES ||
extrinsic == DRAIN_RES)
return 0L;
}
else
res = W_MASK(os_carried);
} else
res = W_WORN;
if (obj_oprops_provides_property(otmp, extrinsic)) {
if (res == W_WORN || res == W_EQUIP)
res &= otmp->owornmask;
res &= ~W_MASK(os_quiver);
if (res == W_MASK(os_swapwep) &&
(otmp->where != OBJ_INVENT || !u.twoweap))
res = 0L;
return res;
}
return 0L;
}
/* Checks everything except for object properties. Called
directly by obj_properties() to figure out which ones are
potentially redundant. This returns if the item provides
an extrinsic for a slot *in general*, not if it does it
*right now*. */
long
item_provides_extrinsic_before_oprop(const struct obj *otmp,
int extrinsic,
int *warntype)
{
if (!otmp) {
panic("Trying to check non-existing item?");
return 0L;
}
const struct artifact *oart = get_artifact(otmp);
uchar dtyp;
long equipmask;
/* Don't allow non-weapon equippables to provide an extrinsic
if wielded */
enum objslot slot = which_slot(otmp);
if (slot == os_invalid) {
if (otmp->oclass == WEAPON_CLASS || is_weptool(otmp))
equipmask = W_EQUIP;
else
equipmask = W_MASK(os_carried);
} else
equipmask = W_WORN;
/* Does the base item (artifact or not) provide the property in question?
Skip WARN_OF_MON for paranoia reasons; it wouldn't work if it were
used, because there'd be no way to communicate which monster is being
warned against. Potions don't grant their innate properties. */
if (objects[otmp->otyp].oc_oprop == extrinsic && extrinsic != WARN_OF_MON && otmp->oclass != POTION_CLASS)
return equipmask;
/* Non-artifact item properties go here. */
if (otmp->otyp == AMULET_OF_RESTFUL_SLEEP && extrinsic == SLEEP_RES)
return equipmask; /* gives a sleep resistance, but not a sleep immunity. */
if (otmp->otyp == ALCHEMY_SMOCK && extrinsic == ACID_RES)
return equipmask;
if ((otmp->otyp == SHIMMERING_DRAGON_SCALE_MAIL ||
otmp->otyp == SHIMMERING_DRAGON_SCALES) && extrinsic == STUN_RES)
return equipmask;
if ((otmp->otyp == RED_DRAGON_SCALE_MAIL ||
otmp->otyp == RED_DRAGON_SCALES) &&
(extrinsic == INFRAVISION || extrinsic == WARNING ||
extrinsic == SEE_INVIS))
return equipmask;
if ((otmp->otyp == WHITE_DRAGON_SCALE_MAIL ||
otmp->otyp == WHITE_DRAGON_SCALES) &&
(extrinsic == WATERPROOF || extrinsic == SEARCHING ||
extrinsic == SWIMMING || extrinsic == MAGICAL_BREATHING))
return equipmask;
if ((otmp->otyp == ORANGE_DRAGON_SCALE_MAIL ||
otmp->otyp == ORANGE_DRAGON_SCALES) && extrinsic == FREE_ACTION)
return equipmask;
if ((otmp->otyp == BLACK_DRAGON_SCALE_MAIL ||
otmp->otyp == BLACK_DRAGON_SCALES) && extrinsic == DRAIN_RES)
return equipmask;
if ((otmp->otyp == BLUE_DRAGON_SCALE_MAIL ||
otmp->otyp == BLUE_DRAGON_SCALES) && extrinsic == FAST)
return equipmask;
if ((otmp->otyp == GREEN_DRAGON_SCALE_MAIL ||
otmp->otyp == GREEN_DRAGON_SCALES) && extrinsic == SICK_RES)
return equipmask;
if ((otmp->otyp == YELLOW_DRAGON_SCALE_MAIL ||
otmp->otyp == YELLOW_DRAGON_SCALES) && extrinsic == STONE_RES)
return equipmask;
if (otmp->otyp == AMULET_OF_YENDOR &&
(extrinsic == CLAIRVOYANT ||
extrinsic == AGGRAVATE_MONSTER))
return W_MASK(os_carried);
if (!oart)
return 0L;
if (oart->inv_prop == extrinsic &&
(otmp->owornmask & W_MASK(os_invoked)))
return W_MASK(os_invoked);
dtyp = oart->cary.adtyp;
if ((dtyp == AD_FIRE && extrinsic == FIRE_RES) ||
(dtyp == AD_COLD && extrinsic == COLD_RES) ||
(dtyp == AD_ELEC && extrinsic == SHOCK_RES) ||
(dtyp == AD_MAGM && extrinsic == ANTIMAGIC) ||
(dtyp == AD_DISN && extrinsic == DISINT_RES) ||
(dtyp == AD_DRST && extrinsic == POISON_RES) ||
(dtyp == AD_DISE && extrinsic == SICK_RES) ||
(dtyp == AD_DRLI && extrinsic == DRAIN_RES))
return W_MASK(os_carried);
dtyp = oart->defn.adtyp;
if ((dtyp == AD_FIRE && extrinsic == FIRE_RES) ||
(dtyp == AD_COLD && extrinsic == COLD_RES) ||
(dtyp == AD_ELEC && extrinsic == SHOCK_RES) ||
(dtyp == AD_MAGM && extrinsic == ANTIMAGIC) ||
(dtyp == AD_DISN && extrinsic == DISINT_RES) ||
(dtyp == AD_DRST && extrinsic == POISON_RES) ||
(dtyp == AD_DISE && extrinsic == SICK_RES) ||
(dtyp == AD_DRLI && extrinsic == DRAIN_RES))
return equipmask;
if (spfx_provides_extrinsic(otmp, oart->cspfx, extrinsic,
warntype))
return W_MASK(os_carried);
else if (spfx_provides_extrinsic(otmp, oart->spfx, extrinsic,
warntype))
return equipmask;
return 0L;
}
/* Returns true if the given artifact spfx field provides
extrinsic. Also messes with warntype if it's WARN_OF_MON.
obj is needed to properly grab the warn of mon info. */
static boolean
spfx_provides_extrinsic(const struct obj *obj, unsigned long spfx,
int extrinsic, int *warntype)
{
if ((spfx & SPFX_FLYING && extrinsic == FLYING) ||
(spfx & SPFX_SEARCH && extrinsic == SEARCHING) ||
(spfx & SPFX_ESP && extrinsic == TELEPAT) ||
(spfx & SPFX_DISPLACE && extrinsic == DISPLACED) ||
(spfx & SPFX_REGEN && extrinsic == REGENERATION) ||
(spfx & SPFX_TCTRL && extrinsic == TELEPORT_CONTROL) ||
(spfx & SPFX_EREGEN && extrinsic == ENERGY_REGENERATION) ||
(spfx & SPFX_HSPDAM && extrinsic == HALF_SPDAM) ||
(spfx & SPFX_HPHDAM && extrinsic == HALF_PHDAM) ||
(spfx & SPFX_HALRES && extrinsic == HALLUC_RES) ||
(spfx & SPFX_REFLECT && extrinsic == REFLECTING) ||
(spfx & SPFX_XRAY && extrinsic == XRAY_VISION) ||
(spfx & SPFX_FREEACT && extrinsic == FREE_ACTION))
return TRUE;
if (spfx & SPFX_WARN) {
if (spec_m2(obj)) {
if (extrinsic == WARN_OF_MON) {
*warntype = spec_m2(obj);
return TRUE;
}
} else {
if (extrinsic == WARNING)
return TRUE;
}
}
return FALSE;
}
/*
* creature (usually player) 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(struct obj *obj, const struct monst *mon)
{
const struct artifact *oart = get_artifact(obj);
boolean badclass, badalign, self_willed, yours;
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 != sgn(mon->data->maligntyp));
} 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 for `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 && (oart->spfx & SPFX_DBONUS) != 0) {
struct artifact tmp;
tmp = *oart;
tmp.spfx &= SPFX_DBONUS;
badalign = spec_applies(&tmp, mon);
/* Only blast bane targets, not mons lacking an elemental resistance */
if (badalign < 3)
badalign = 0;
}
if (((badclass || badalign) && self_willed) ||
(badalign && (!yours || !rn2(4)))) {
int dmg;
const char *buf;
if (!yours)
return 0;
pline(msgc_substitute, "You are blasted by %s power!",
s_suffix(the(xname(obj))));
dmg = dice((Antimagic ? 2 : 4), (self_willed ? 10 : 4));
buf = msgprintf("touching %s", oart->name);
losehp(dmg, killer_msg(DIED, buf));
exercise(A_WIS, FALSE);
}
/* can pick it up unless you're totally non-synch'd with the artifact */
if (badclass && badalign && self_willed) {
/* not msgc_cancelled1, because you might not know your own numerical
alignment record */
if (yours)
pline(msgc_failcurse, "%s your grasp!", Tobjnam(obj, "evade"));
return 0;
}
return 1;
}
/* Decide whether an artifact's special attacks apply against mtmp.
Returns 0 (doesn't apply), 1 (applies partially), 2 (applies completely),
3 (apply completely + blast user) */
static int
spec_applies(const struct artifact *weap, const struct monst *mtmp)
{
const struct permonst *ptr;
boolean yours;
if (!(weap->spfx & (SPFX_DBONUS | SPFX_ATTK)))
return (weap->attk.adtyp == AD_PHYS) ? 2 : 0;
yours = (mtmp == &youmonst);
ptr = mtmp->data;
if (DBONUS(weap, SPFX_DMONS)) {
return (ptr == &mons[(int)weap->mtype]) ? 3 : 0;
} else if (DBONUS(weap, SPFX_DCLAS)) {
return (weap->mtype == (unsigned long)ptr->mlet) ? 3 : 0;
} else if (DBONUS(weap, SPFX_DFLAG1)) {
return ((ptr->mflags1 & weap->mtype) != 0L) ? 3 : 0;
} else if (DBONUS(weap, SPFX_DFLAG2)) {
return (((ptr->mflags2 & weap->mtype) ||
(yours &&
((!Upolyd && (urace.selfmask & weap->mtype)) ||
((weap->mtype & M2_WERE) && u.ulycn >= LOW_PM))))) ? 3 : 0;
} else if (DBONUS(weap, SPFX_DALIGN)) {
return (yours ? (u.ualign.type != weap->alignment) :
(ptr->maligntyp == A_NONE ||
sgn(ptr->maligntyp) != weap->alignment)) ? 3 : 0;
} else if (DBONUS(weap, 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 0;
switch (weap->attk.adtyp) {
case AD_FIRE:
return (!resists_fire(mtmp) ? 2 :
!immune_to_fire(mtmp) ? 1 : 0);
case AD_COLD:
return (!resists_cold(mtmp) ? 2 :
!immune_to_cold(mtmp) ? 1 : 0);
case AD_ELEC:
return (!resists_elec(mtmp) ? 2 :
!immune_to_elec(mtmp) ? 1 : 0);
case AD_MAGM:
return (!resists_magm(mtmp) ? 2 : 0);
case AD_STUN:
return (!resists_stun(mtmp) ? 2 : 0);
case AD_DRST:
return (!resists_poison(mtmp) ? 2 :
!immune_to_poison(mtmp) ? 1 : 0);
case AD_DRLI:
return (!resists_drli(mtmp) ? 2 : 0);
case AD_STON:
return (!resists_ston(mtmp) ? 2 : 0);
default:
impossible("Weird weapon special attack.");
}
}
return 0;
}
/* return the M2 flags of monster that an artifact's special attacks apply
* against */
static long
spec_m2(const struct obj *otmp)
{
const struct artifact *artifact = get_artifact(otmp);
if (artifact)
return artifact->mtype;
return 0L;
}
/* special attack bonus */
int
spec_abon(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(struct obj *otmp, struct monst *mon, int tmp,
int *spec_dbon_applies)
{
if (!otmp->oartifact) {
uint64_t props = obj_properties(otmp);
int bonus = 0;
*spec_dbon_applies = 0;
if ((props & opm_drain) && !resists_drli(mon)) {
*spec_dbon_applies = 2;
if (resists_drli(mon))
*spec_dbon_applies = 1;
}
if (props & opm_vorpal)
*spec_dbon_applies = 2;
if ((props & opm_fire) && !immune_to_fire(mon)) {
*spec_dbon_applies = 2;
if (resists_fire(mon)) {
*spec_dbon_applies = 1;
bonus += rnd(3);
} else
bonus += rnd(6);
}
if ((props & opm_frost) && !immune_to_cold(mon)) {
*spec_dbon_applies = 2;
if (resists_cold(mon)) {
*spec_dbon_applies = 1;
bonus += rnd(3);
} else
bonus += rnd(6);
}
if ((props & opm_shock) && !immune_to_elec(mon)) {
*spec_dbon_applies = 2;
if (resists_elec(mon)) {
*spec_dbon_applies = 1;
bonus += rnd(3);
} else
bonus += rnd(6);
}
return bonus;
}
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 = 0;
else
*spec_dbon_applies = spec_applies(weap, mon);
if (*spec_dbon_applies) {
int res = weap->attk.damd ? rnd((int)weap->attk.damd) : max(tmp, 1);
if (*spec_dbon_applies == 1)
res = (res + 1) / 2;
return res;
}
return 0;
}
/* add identified artifact to discoveries list */
void
discover_artifact(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(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(struct nh_menulist *menu)
{
int i, m, otyp;
const char *buf;
for (i = 0; i < NROFARTIFACTS; i++) {
if (artidisco[i] == 0)
break; /* empty slot implies end of list */
if (i == 0)
add_menuheading(menu, "Artifacts");
m = artidisco[i];
otyp = artilist[m].otyp;
buf = msgprintf(" %s [%s %s]", artiname(m),
align_str(artilist[m].alignment),
simple_typename(otyp));
add_menutext(menu, 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 boolean
magicbane_hit(struct monst *magr, /* attacker */
struct monst *mdef, /* 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 */
const char *hittee, /* target's name: "you" or mon_nam(mdef) */
int spec_dbon_applies
)
{
const struct permonst *old_uasmon;
const char *verb;
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 */
}
/* give the hit message prior to inflicting the effects */
verb = mb_verb[! !Hallucination][attack_indx];
if (youattack || youdefend || vis) {
result = TRUE;
pline(combat_msgc(magr, mdef, cr_hit),
"The magic-absorbing %s %s %s!",
mb->otyp == QUARTERSTAFF ? "staff" : "blade", /* old saves */
vtense(NULL, verb), hittee);
/* assume probing has some sort of noticeable feedback even if it is
being done by one monster to another */
if (attack_indx == MB_INDEX_PROBE)
reveal_monster_at(mdef->mx, mdef->my, FALSE);
}
/* now perform special effects */
switch (attack_indx) {
case MB_INDEX_CANCEL:
old_uasmon = youmonst.data;
/* No mdef->mcan check: even a cancelled monster can be polymorphed
into a golem, and the "cancel" effect acts as if some magical energy
remains in spellcasting defenders to be absorbed later. */
if (!cancel_monst(mdef, mb, magr, FALSE, FALSE)) {
resisted = TRUE;
} else {
do_stun = FALSE;
if (youdefend) {
if (youmonst.data != old_uasmon)