Repository navigation
Expand file tree
/
Copy pathartifact.c
More file actions
2628 lines (2390 loc) · 92.8 KB
/
Copy pathartifact.c
File metadata and controls
2628 lines (2390 loc) · 92.8 KB
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.7 artifact.c $NHDT-Date: 1715889721 2024/05/16 20:02:01 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.236 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2013. */
/* 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.
*/
staticfn struct artifact *get_artifact(struct obj *) NONNULL; /* never returns null */
/* #define get_artifact(o) \
(((o) && ((o)->artifact > 0 && (o)->artifact < AFTER_LAST_ARTIFACT)) \
? &artilist[(int) (o)->oartifact] \
: &artilist[ART_NONARTIFACT]) */
staticfn boolean bane_applies(const struct artifact *, struct monst *)
NONNULLARG12;
staticfn int spec_applies(const struct artifact *, struct monst *)
NONNULLARG12;
staticfn int invoke_ok(struct obj *);
staticfn void nothing_special(struct obj *) NONNULLARG1;
staticfn int arti_invoke(struct obj *);
staticfn boolean Mb_hit(struct monst * magr, struct monst *mdef,
struct obj *, int *, int, boolean, char *);
staticfn unsigned long abil_to_spfx(long *) NONNULLARG1;
staticfn uchar abil_to_adtyp(long *) NONNULLARG1;
staticfn int glow_strength(int);
staticfn boolean untouchable(struct obj *, boolean);
staticfn int count_surround_traps(coordxy, coordxy);
/* 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
/* artifact tracking; gift and wish imply found; it also gets set for items
seen on the floor, in containers, and wielded or dropped by monsters */
struct arti_info {
Bitfield(exists, 1); /* 1 if corresponding artifact has been created */
Bitfield(found, 1); /* 1 if artifact is known by hero to exist */
Bitfield(gift, 1); /* 1 iff artifact was created as a prayer reward */
Bitfield(wish, 1); /* 1 iff artifact was created via wish */
Bitfield(named, 1); /* 1 iff artifact was made by naming an item */
Bitfield(viadip, 1); /* 1 iff dipped long sword became Excalibur */
Bitfield(lvldef, 1); /* 1 iff created by special level definition */
Bitfield(bones, 1); /* 1 iff came from bones file */
Bitfield(rndm, 1); /* 1 iff randomly generated */
};
/* array of flags tracking which artifacts exist, indexed by ART_xx;
ART_xx values are 1..N, element [0] isn't used; no terminator needed */
static struct arti_info artiexist[1 + NROFARTIFACTS];
/* discovery list; for N discovered artifacts, the first N entries are ART_xx
values in discovery order, the remaining (NROFARTIFACTS-N) slots are 0 */
static xint16 artidisco[NROFARTIFACTS];
/* note: artiexist[] and artidisco[] don't need to be in struct g; they
* get explicitly initialized at game start so don't need to be part of
* bulk re-init if game restart ever gets implemented. They are saved
* and restored but that is done through this file so they can be local.
*/
static const struct arti_info zero_artiexist = {0}; /* all bits zero */
staticfn void hack_artifacts(void);
/* handle some special cases; must be called after u_init() */
staticfn void
hack_artifacts(void)
{
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 (gu.urole.questarti) {
artilist[gu.urole.questarti].alignment = alignmnt;
artilist[gu.urole.questarti].role = Role_switch;
}
return;
}
/* zero out the artifact existence list */
void
init_artifacts(void)
{
(void) memset((genericptr_t) artiexist, 0, sizeof artiexist);
(void) memset((genericptr_t) artidisco, 0, sizeof artidisco);
hack_artifacts();
}
void
save_artifacts(NHFILE *nhfp)
{
if (nhfp->structlevel) {
bwrite(nhfp->fd, (genericptr_t) artiexist, sizeof artiexist);
bwrite(nhfp->fd, (genericptr_t) artidisco, sizeof artidisco);
}
}
void
restore_artifacts(NHFILE *nhfp)
{
if (nhfp->structlevel) {
mread(nhfp->fd, (genericptr_t) artiexist, sizeof artiexist);
mread(nhfp->fd, (genericptr_t) 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;
}
/*
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(
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].exists)
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) {
/* prevent erosion from generating */
otmp->oeroded = otmp->oeroded2 = 0;
otmp = oname(otmp, a->name, ONAME_NO_FLAGS);
otmp->oartifact = m;
/* set existence and reason for creation bits */
artifact_origin(otmp, ONAME_RANDOM); /* 'random' is default */
}
} 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(
const char *name, /* string from player that might be an artifact name */
short *otyp_p, /* secondary output */
boolean fuzzy) /* whether to allow extra or omitted spaces or dashes */
{
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 (!fuzzy ? !strcmpi(name, aname)
: fuzzymatch(name, aname, " -", TRUE)) {
if (otyp_p)
*otyp_p = a->otyp;
return a->name;
}
}
return (char *) 0;
}
boolean
exist_artifact(int otyp, const char *name)
{
const struct artifact *a;
struct arti_info *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->exists ? TRUE : FALSE;
return FALSE;
}
/* an artifact has just been created or is being "un-created" for a chance
to be created again later */
void
artifact_exists(
struct obj *otmp,
const char *name,
boolean mod, /* True: exists, False: being un-created */
unsigned flgs) /* ONAME_xyz flags; not relevant if !mod */
{
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 = (int) (a - artilist);
otmp->oartifact = (char) (mod ? m : 0);
otmp->age = 0;
if (otmp->otyp == RIN_INCREASE_DAMAGE)
otmp->spe = 0;
if (mod) { /* means being created rather than un-created */
/* one--and only one--of these should always be set */
if ((flgs & (ONAME_VIA_NAMING | ONAME_WISH | ONAME_GIFT
| ONAME_VIA_DIP | ONAME_LEVEL_DEF
| ONAME_BONES | ONAME_RANDOM)) == 0)
flgs |= ONAME_RANDOM; /* the default origin */
/* 'exists' bit will become set (in artifact_origin();
there's no ONAME_ flag) and flgs might also contain
the know_arti bit (hero knows that artifact exists) */
artifact_origin(otmp, flgs);
} else { /* uncreate */
/* clear all the flag bits */
artiexist[m] = zero_artiexist;
}
break;
}
return;
}
/* mark an artifact as 'found' */
void
found_artifact(int a)
{
if (a < 1 || a > NROFARTIFACTS)
impossible("found_artifact: invalid artifact index! (%d)", a);
else if (!artiexist[a].exists)
impossible("found_artifact: artifact doesn't exist yet? (%d)", a);
else
artiexist[a].found = 1;
}
/* if an artifact hasn't already been designated 'found', do that now
and generate a livelog event about finding it */
void
find_artifact(struct obj *otmp)
{
int a = otmp->oartifact;
if (a && !artiexist[a].found) {
const char *where;
found_artifact(a); /* artiexist[a].found = 1 */
/*
* Unlike costly_spot(), inside_shop() includes the "free spot"
* in front of the door. And it doesn't care whether or not
* there is a shopkeeper present.
*
* If hero sees a monster pick up a not-yet-found artifact, it
* will have its dknown flag set even if far away and will be
* described as 'found on the floor'. Similarly for dropping
* (possibly upon monster's death), dknown will be set and the
* artifact will be described as 'carried by a monster'.
* That's handled by caller: dog_invent(), mpickstuff(), or
* mdrop_obj() so that we get called before obj->where changes.
*/
where = ((otmp->where == OBJ_FLOOR)
? ((inside_shop(otmp->ox, otmp->oy) != NO_ROOM)
? " in a shop"
: " on the floor")
/* artifacts aren't created in containers but could be
inside one if it comes from a bones level */
: (otmp->where == OBJ_CONTAINED) ? " in a container"
/* perhaps probing, or seeing monster wield artifact */
: (otmp->where == OBJ_MINVENT) ? " carried by a monster"
/* catchall: probably in inventory, picked up while
blind but now seen; there's no previous_where to
figure out how it got here */
: "");
livelog_printf(LL_ARTIFACT, "found %s%s",
bare_artifactname(otmp), where);
}
}
int
nartifact_exist(void)
{
int i, a = 0;
for (i = 1; i <= NROFARTIFACTS; ++i)
if (artiexist[i].exists)
++a;
return a;
}
/* set artifact tracking flags;
calling sequence: oname() -> artifact_exists() -> artifact_origin() or
mksobj(),others -> mk_artifact() -> artifact_origin(random) possibly
followed by mksobj(),others -> artifact_origin(non-random origin) */
void
artifact_origin(
struct obj *arti, /* new artifact */
unsigned aflags) /* ONAME_xxx flags, shared by artifact_exists() */
{
int ct, a = arti->oartifact;
if (a) {
/* start by clearing all bits; most are mutually exclusive */
artiexist[a] = zero_artiexist;
/* set 'exists' bit back on; not specified via flag bit in aflags */
artiexist[a].exists = 1;
/* 'hero knows it exists' is expected for wish, gift, viadip, or
named and could eventually become set for any of the others */
if ((aflags & ONAME_KNOW_ARTI) != 0)
artiexist[a].found = 1;
/* should be exactly one of wish, gift, via_dip, via_naming,
level_def (quest), bones, and random (floor or monst's minvent) */
ct = 0;
if ((aflags & ONAME_WISH) != 0)
artiexist[a].wish = 1, ++ct;
if ((aflags & ONAME_GIFT) != 0)
artiexist[a].gift = 1, ++ct;
if ((aflags & ONAME_VIA_DIP) != 0)
artiexist[a].viadip = 1, ++ct;
if ((aflags & ONAME_VIA_NAMING) != 0)
artiexist[a].named = 1, ++ct;
if ((aflags & ONAME_LEVEL_DEF) != 0)
artiexist[a].lvldef = 1, ++ct;
if ((aflags & ONAME_BONES) != 0)
artiexist[a].bones = 1, ++ct;
if ((aflags & ONAME_RANDOM) != 0)
artiexist[a].rndm = 1, ++ct;
if (ct != 1)
impossible("invalid artifact origin: %4o", aflags);
}
}
boolean
spec_ability(struct obj *otmp, unsigned long abil)
{
const struct artifact *arti = get_artifact(otmp);
return (boolean) (arti != &artilist[ART_NONARTIFACT]
&& (arti->spfx & abil) != 0L);
}
/* 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 (boolean) (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 != &artilist[ART_NONARTIFACT]) {
/* 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(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 != &artilist[ART_NONARTIFACT] && (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(struct obj *otmp, const char *name)
{
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 = svb.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;
}
boolean
attacks(int adtyp, struct obj *otmp)
{
const struct artifact *weap;
if ((weap = get_artifact(otmp)) != &artilist[ART_NONARTIFACT])
return (boolean) (weap->attk.adtyp == adtyp);
return FALSE;
}
boolean
defends(int adtyp, struct obj *otmp)
{
const struct artifact *weap;
if (!otmp)
return FALSE;
if ((weap = get_artifact(otmp)) != &artilist[ART_NONARTIFACT])
return (boolean) (weap->defn.adtyp == adtyp);
if (Is_dragon_armor(otmp)) {
int otyp = otmp->otyp;
/* convert mail to scales to simplify testing */
if (Is_dragon_mail(otmp))
otyp += GRAY_DRAGON_SCALES - GRAY_DRAGON_SCALE_MAIL;
switch (adtyp) {
case AD_MAGM: /* magic missiles => general magic resistance */
return (otyp == GRAY_DRAGON_SCALES);
case AD_HALU: /* confers hallucination resistance */
return (otyp == GOLD_DRAGON_SCALES);
case AD_FIRE:
/*case AD_BLND: -- gives infravision but does not prevent blindness */
return (otyp == RED_DRAGON_SCALES); /* red but not gold */
case AD_COLD:
/*case AD_FAMN: -- slows digestion but does not override Famine */
return (otyp == WHITE_DRAGON_SCALES); /* white but not silver */
case AD_DRST: /* drain strength => poison */
case AD_DISE: /* blocks disease but not slime */
return (otyp == GREEN_DRAGON_SCALES);
case AD_SLEE: /* sleep */
case AD_PLYS: /* paralysis => free action */
return (otyp == ORANGE_DRAGON_SCALES);
case AD_DISN: /* disintegration */
case AD_DRLI: /* level drain resistance */
return (otyp == BLACK_DRAGON_SCALES);
case AD_ELEC: /* electricity == lightning */
case AD_SLOW: /* confers speed so blocks speed removal */
return (otyp == BLUE_DRAGON_SCALES);
case AD_ACID:
case AD_STON: /* petrification resistance */
return (otyp == YELLOW_DRAGON_SCALES);
default:
/* SILVER_DRAGON_SCALES don't resist any particular attack type */
break;
}
}
return FALSE;
}
/* used for monsters */
boolean
defends_when_carried(int adtyp, struct obj *otmp)
{
const struct artifact *weap;
if ((weap = get_artifact(otmp)) != &artilist[ART_NONARTIFACT])
return (boolean) (weap->cary.adtyp == adtyp);
return FALSE;
}
/* determine whether an item confers Protection */
boolean
protects(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 == &artilist[ART_NONARTIFACT])
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(struct obj *otmp, boolean on, long wp_mask)
{
long *mask = 0;
const struct artifact *art, *oart = get_artifact(otmp);
struct obj *obj;
uchar dtyp;
long spfx;
if (oart == &artilist[ART_NONARTIFACT])
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;
else if (dtyp == AD_DRLI)
mask = &EDrain_resistance;
if (mask && wp_mask == W_ART && !on) {
/* find out if some other artifact also confers this intrinsic;
if so, leave the mask alone */
for (obj = gi.invent; obj; obj = obj->nobj) {
if (obj != otmp && obj->oartifact) {
art = get_artifact(obj);
if (art != &artilist[ART_NONARTIFACT]
&& 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 */
for (obj = gi.invent; obj; obj = obj->nobj)
if (obj != otmp && obj->oartifact) {
art = get_artifact(obj);
if (art != &artilist[ART_NONARTIFACT])
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,
program_state.restoring ? FALSE : TRUE,
wp_mask);
}
if (spfx & SPFX_ESP) {
if (on)
ETelepat |= wp_mask;
else
ETelepat &= ~wp_mask;
recalc_telepat_range();
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;
svc.context.warntype.obj |= spec_m2(otmp);
} else {
EWarn_of_mon &= ~wp_mask;
svc.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;
gv.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 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(struct obj *obj, struct monst *mon)
{
const struct artifact *oart = get_artifact(obj);
boolean badclass, badalign, self_willed, yours;
touch_blasted = FALSE;
if (oart == &artilist[ART_NONARTIFACT])
return 1;
yours = (mon == &gy.youmonst);
/* all quest artifacts are self-willed; if 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) != 0
&& 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(struct obj *obj, int dtyp)
{
const struct artifact *weap = get_artifact(obj);
if (weap == &artilist[ART_NONARTIFACT])
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);
}
staticfn boolean
bane_applies(const struct artifact *oart, struct monst *mon)
{
struct artifact atmp;
if (oart != &artilist[ART_NONARTIFACT]
&& (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 */
staticfn int
spec_applies(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 == &gy.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 && (gu.urace.selfmask & weap->mtype))
|| ((weap->mtype & M2_WERE) && ismnum(u.ulycn)))));
} 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) {
if (defended(mtmp, (int) weap->attk.adtyp))
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(struct obj *otmp)
{
const struct artifact *artifact = get_artifact(otmp);
if (artifact != &artilist[ART_NONARTIFACT])
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 != &artilist[ART_NONARTIFACT]
&& 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)
{
const struct artifact *weap = get_artifact(otmp);