Source:NetHack 3.1.0/artifact.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to artifact.c from the source code of NetHack 3.1.0.

Warning! This is the source code from an old release. For newer releases, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1.    /*	SCCS Id: @(#)artifact.c	3.1	93/01/17	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    #include "hack.h"
6.    #include "artifact.h"
7.    #ifdef OVLB
8.    #include "artilist.h"
9.    #else
10.   STATIC_DCL const struct artifact artilist[];
11.   #endif
12.   /*
13.    * Note:  both artilist[] and artiexist[] have a dummy element #0,
14.    *	  so loops over them should normally start at #1.  The primary
15.    *	  exception is the save & restore code, which doesn't care about
16.    *	  the contents, just the total size.
17.    */
18.   
19.   extern boolean notonhead;	/* for long worms */
20.   
21.   #define get_artifact(o) \
22.   		(((o)&&(o)->oartifact) ? &artilist[(int) (o)->oartifact] : 0)
23.   STATIC_DCL int FDECL(spec_applies, (const struct artifact*,struct permonst*));
24.   STATIC_DCL int FDECL(arti_invoke, (struct obj*));
25.   
26.   #ifndef OVLB
27.   STATIC_DCL int spec_dbon_applies;
28.   
29.   #else	/* OVLB */
30.   /* coordinate effects from spec_dbon() with messages in artifact_hit() */
31.   STATIC_OVL int spec_dbon_applies = 0;
32.   
33.   /* flags including which artifacts have already been created */
34.   static boolean artiexist[1+NROFARTIFACTS+1];
35.   
36.   static boolean FDECL(attacks, (int,struct obj *));
37.   
38.   /* zero out the artifact existence list */
39.   void
40.   init_artifacts()
41.   {
42.   	(void) memset((genericptr_t) artiexist, 0, sizeof artiexist);
43.   }
44.   
45.   void
46.   save_artifacts(fd)
47.   int fd;
48.   {
49.   	bwrite(fd, (genericptr_t) artiexist, sizeof artiexist);
50.   }
51.   
52.   void
53.   restore_artifacts(fd)
54.   int fd;
55.   {
56.   	mread(fd, (genericptr_t) artiexist, sizeof artiexist);
57.   }
58.   
59.   const char *
60.   artiname(artinum)
61.   int artinum;
62.   {
63.   	if (artinum <= 0 || artinum > NROFARTIFACTS) return("");
64.   	return(artilist[artinum].name);
65.   }
66.   
67.   /*
68.      Make an artifact.  If a specific alignment is specified, then an object of
69.      the appropriate alignment is created from scratch, or NULL is returned if
70.      none is available.  If no alignment is given, then 'otmp' is converted
71.      into an artifact of matching type, or returned as-is if that's not possible.
72.      For the 2nd case, caller should use ``obj = mk_artifact(obj, A_NONE);
73.      for the 1st, ``obj = mk_artifact(NULL, some_alignment);.
74.    */
75.   struct obj *
76.   mk_artifact(otmp, alignment)
77.   struct obj *otmp;	/* existing object; ignored if alignment specified */
78.   aligntyp alignment;	/* target alignment, or A_NONE */
79.   {
80.   	register const struct artifact *a;
81.   	register int n = 0, m;
82.   	register boolean by_align = (alignment != A_NONE);
83.   	register short o_typ = (by_align || !otmp) ? 0 : otmp->otyp;
84.   	boolean unique = !by_align && otmp && objects[o_typ].oc_unique;
85.   
86.   	/* count eligible artifacts */
87.   	for (a = artilist+1,m = 1; a->otyp; a++,m++)
88.   	    if ((by_align ? a->alignment == alignment : a->otyp == o_typ) &&
89.   		(!(a->spfx & SPFX_NOGEN) || unique) && !artiexist[m]) {
90.   		if (by_align && a->class == pl_character[0])
91.   		    goto make_artif;	/* 'a' points to the desired one */
92.   		else
93.   		    n++;
94.   	    }
95.   
96.   	if (n) {		/* found at least one candidate */
97.   	    /* select one, then find it again */
98.   	    if (n > 1) n = rnd(n);	/* [1..n] */
99.   	    for (a = artilist+1,m = 1; a->otyp; a++,m++)
100.  		if ((by_align ? a->alignment == alignment : a->otyp == o_typ)&&
101.  		    (!(a->spfx & SPFX_NOGEN) || unique) && !artiexist[m]) {
102.  		    if (!--n) break;	/* stop when chosen one reached */
103.  		}
104.  
105.  	    /* make an appropriate object if necessary, then christen it */
106.  make_artif: if (by_align) otmp = mksobj((int)a->otyp, TRUE, FALSE);
107.  	    otmp = oname(otmp, a->name, 0);
108.  	    otmp->oartifact = m;
109.  	    artiexist[m] = TRUE;
110.  	} else {
111.  	    /* nothing appropriate could be found; return the original object */
112.  	    if (by_align) otmp = 0;	/* (there was no original object) */
113.  	}
114.  	return otmp;
115.  }
116.  
117.  /*
118.   * Returns the full name (with articles and correct capitalization) of an
119.   * artifact named "name" if one exists, or NULL, it not.
120.   * The given name must be rather close to the real name for it to match.
121.   * The object type of the artifact is returned in otyp if the return value
122.   * is non-NULL.
123.   */
124.  const char*
125.  artifact_name(name, otyp)
126.  const char *name;
127.  short *otyp;
128.  {
129.      register const struct artifact *a;
130.      register const char *aname;
131.  
132.      if(!strncmpi(name, "the ", 4)) name += 4;
133.  
134.      for (a = artilist+1; a->otyp; a++) {
135.  	aname = a->name;
136.  	if(!strncmpi(aname, "the ", 4)) aname += 4;
137.  	if(!strcmpi(name, aname)) {
138.  	    *otyp = a->otyp;
139.  	    return a->name;
140.  	}
141.      }
142.  
143.      return NULL;
144.  }
145.  
146.  boolean
147.  exist_artifact(otyp, name)
148.  register int otyp;
149.  register const char *name;
150.  {
151.  	register const struct artifact *a;
152.  	register boolean *arex;
153.  
154.  	if (otyp && *name)
155.  	    for (a = artilist+1,arex = artiexist+1; a->otyp; a++,arex++)
156.  		if ((int) a->otyp == otyp && !strcmp(a->name, name))
157.  		    return *arex;
158.  	return FALSE;
159.  }
160.  
161.  void
162.  artifact_exists(otmp, name, mod)
163.  register struct obj *otmp;
164.  register const char *name;
165.  register boolean mod;
166.  {
167.  	register const struct artifact *a;
168.  
169.  	if (otmp && *name)
170.  	    for (a = artilist+1; a->otyp; a++)
171.  		if (a->otyp == otmp->otyp && !strcmp(a->name, name)) {
172.  		    register int m = a - artilist;
173.  		    otmp->oartifact = (char)(mod ? m : 0);
174.  		    otmp->age = 0;
175.  		    if(otmp->otyp == RIN_INCREASE_DAMAGE)
176.  			otmp->spe = 0;
177.  		    artiexist[m] = mod;
178.  		    break;
179.  		}
180.  	return;
181.  }
182.  
183.  int
184.  nartifact_exist()
185.  {
186.      int a = 0;
187.      int n = SIZE(artiexist);
188.  
189.      while(n > 1)
190.  	if(artiexist[--n]) a++;
191.  
192.      return a;
193.  }
194.  
195.  #endif /* OVLB */
196.  #ifdef OVL0
197.  
198.  boolean
199.  spec_ability(otmp, abil)
200.  struct obj *otmp;
201.  unsigned abil;
202.  {
203.  	const struct artifact *arti = get_artifact(otmp);
204.  
205.  	return(arti && (arti->spfx & abil));
206.  }
207.  
208.  #endif /* OVL0 */
209.  #ifdef OVLB
210.  
211.  boolean
212.  restrict_name(otmp, name)  /* returns 1 if name is restricted for otmp->otyp */
213.  register struct obj *otmp;
214.  register const char *name;
215.  {
216.  	register const struct artifact *a;
217.  
218.  	if (!*name) return FALSE;
219.  
220.  		/* Since almost every artifact is SPFX_RESTR, it doesn't cost
221.  		   us much to do the string comparison before the spfx check.
222.  		   Bug fix:  don't name multiple elven daggers "Sting".
223.  		 */
224.  	for (a = artilist+1; a->otyp; a++)
225.  	    if (a->otyp == otmp->otyp && !strcmp(a->name, name))
226.  		return ((a->spfx & (SPFX_NOGEN|SPFX_RESTR)) != 0 ||
227.  			otmp->quan > 1L);
228.  
229.  	return FALSE;
230.  }
231.  
232.  static boolean
233.  attacks(adtyp, otmp)
234.  register int adtyp;
235.  register struct obj *otmp;
236.  {
237.  	register const struct artifact *weap;
238.  
239.  	if ((weap = get_artifact(otmp)) != 0)
240.  		return(weap->attk.adtyp == adtyp);
241.  	return(0);
242.  }
243.  
244.  boolean
245.  defends(adtyp, otmp)
246.  register int adtyp;
247.  register struct obj *otmp;
248.  {
249.  	register const struct artifact *weap;
250.  
251.  	if ((weap = get_artifact(otmp)) != 0)
252.  		return(weap->defn.adtyp == adtyp);
253.  	return(0);
254.  }
255.  
256.  /*
257.   * a potential artifact has just been worn/wielded/picked-up or
258.   * unworn/unwielded/dropped.  Pickup/drop only set/reset the W_ART mask.
259.   */
260.  void
261.  set_artifact_intrinsic(otmp,on,wp_mask)
262.  register struct obj *otmp;
263.  boolean on;
264.  long wp_mask;
265.  {
266.  	long *mask = 0;
267.  	register const struct artifact *oart = get_artifact(otmp);
268.  	uchar dtyp;
269.  	long spfx;
270.  	
271.  	if (!oart) return;
272.  
273.  	/* effects from the defn field */
274.  	dtyp = (wp_mask != W_ART) ? oart->defn.adtyp : oart->cary.adtyp;
275.  
276.  	if (dtyp == AD_FIRE)
277.  	    mask = &HFire_resistance;
278.  	else if (dtyp == AD_COLD)
279.  	    mask = &HCold_resistance;
280.  	else if (dtyp == AD_ELEC)
281.  	    mask = &HShock_resistance;
282.  	else if (dtyp == AD_MAGM)
283.  	    mask = &Antimagic;
284.  	else if (dtyp == AD_DISN)
285.  	    mask = &HDisint_resistance;
286.  
287.  	if(mask && wp_mask == W_ART && !on) {
288.  	    /* find out if some other artifact also confers this intrinsic */
289.  	    /* if so, leave the mask alone */
290.  	    register struct obj* obj;
291.  	    for(obj = invent; obj; obj = obj->nobj)
292.  		if(obj != otmp && obj->oartifact) {
293.  		    register const struct artifact *art = get_artifact(obj);
294.  		    if(art->cary.adtyp == dtyp) {
295.  			mask = (long *) 0;
296.  			break;
297.  		    }
298.  		}
299.  	}
300.  	if(mask) {
301.  	    if (on) *mask |= wp_mask;
302.  	    else *mask &= ~wp_mask;
303.  	}
304.  
305.  	/* intrinsics from the spfx field; there could be more than one */
306.  	spfx = (wp_mask != W_ART) ? oart->spfx : oart->cspfx;
307.  	if(spfx && wp_mask == W_ART && !on) {
308.  	    /* don't change any spfx also conferred by other artifacts */
309.  	    register struct obj* obj;
310.  	    for(obj = invent; obj; obj = obj->nobj)
311.  		if(obj != otmp && obj->oartifact) {
312.  		    register const struct artifact *art = get_artifact(obj);
313.  		    spfx &= ~art->cspfx;
314.  		}
315.  	}
316.  
317.  	if (spfx & SPFX_SEARCH) {
318.  	    if(on) Searching |= wp_mask;
319.  	    else Searching &= ~wp_mask;
320.  	}
321.  	if (spfx & SPFX_HALRES) {
322.  	    /* make_hallucinated must (re)set the mask itself to get
323.  	     * the display right */
324.  	    make_hallucinated((long)!on, TRUE, wp_mask);
325.  	}
326.  	if (spfx & SPFX_ESP) {
327.  	    if(on) HTelepat |= wp_mask;
328.  	    else HTelepat &= ~wp_mask;
329.  	    see_monsters();
330.  	}
331.  	if (spfx & SPFX_STLTH) {
332.  	    if (on) Stealth |= wp_mask;
333.  	    else Stealth &= ~wp_mask;
334.  	}
335.  	if (spfx & SPFX_REGEN) {
336.  	    if (on) HRegeneration |= wp_mask;
337.  	    else HRegeneration &= ~wp_mask;
338.  	}
339.  	if (spfx & SPFX_TCTRL) {
340.  	    if (on) HTeleport_control |= wp_mask;
341.  	    else HTeleport_control &= ~wp_mask;
342.  	}
343.  	if (spfx & SPFX_WARN) {
344.  	    if (on) Warning |= wp_mask;
345.  	    else Warning &= ~wp_mask;
346.  	}
347.  	if (spfx & SPFX_EREGEN) {
348.  	    if (on) Energy_regeneration |= wp_mask;
349.  	    else Energy_regeneration &= ~wp_mask;
350.  	}
351.  	if (spfx & SPFX_HSPDAM) {
352.  	    if (on) Half_spell_damage |= wp_mask;
353.  	    else Half_spell_damage &= ~wp_mask;
354.  	}
355.  	if (spfx & SPFX_HPHDAM) {
356.  	    if (on) Half_physical_damage |= wp_mask;
357.  	    else Half_physical_damage &= ~wp_mask;
358.  	}
359.  
360.  	if(wp_mask == W_ART && !on && oart->inv_prop) {
361.  	    /* might have to turn off invoked power too */
362.  	    if (oart->inv_prop <= LAST_PROP &&
363.  		(u.uprops[oart->inv_prop].p_flgs & W_ARTI))
364.  		(void) arti_invoke(otmp);
365.  	}
366.  }
367.  
368.  /*
369.   * creature (usually player) tries to touch (pick up or wield) an artifact obj.
370.   * Returns 0 if the object refuses to be touched.
371.   * This routine does not change any object chains.
372.   * Ignores such things as gauntlets, assuming the artifact is not
373.   * fooled by such trappings.
374.   */
375.  int
376.  touch_artifact(obj,mon)
377.      struct obj *obj;
378.      struct monst *mon;
379.  {
380.      register const struct artifact *oart = get_artifact(obj);
381.      boolean badclass, badalign;
382.      boolean yours = (mon == &youmonst);
383.  
384.      if(!oart) return 1;
385.  
386.      badclass = (oart->class && (!yours || oart->class != pl_character[0]));
387.      badalign = (oart->spfx & SPFX_RESTR) &&
388.  	((oart->alignment !=
389.  	  (yours ? u.ualign.type : sgn(mon->data->maligntyp))) ||
390.  	 (yours && u.ualign.record < 0));
391.      /*
392.       * hack: Excalibur allows all lawfuls to touch it, but "class" is
393.       * set to 'K' to allow Knights to get it via sacrifice.  This requires an
394.       * additional artifact field to fix, or some similar treatment. -dlc
395.       */
396.      if (obj->oartifact == ART_EXCALIBUR && !badalign) badclass = FALSE;
397.  
398.      if(((badclass || badalign) && (oart->spfx & SPFX_INTEL)) ||
399.         (badalign && (!yours || !rn2(4))))  {
400.  	int dmg;
401.  	char buf[BUFSZ];
402.  
403.  	if (!yours) return 0;
404.  	You("feel a blast of power flow from %s!", the(xname(obj)));
405.  	dmg = d((Antimagic ? 2 : 4) , ((oart->spfx & SPFX_INTEL) ? 10 : 4));
406.  	Sprintf(buf, "touching %s", oart->name);
407.  	losehp(dmg, buf, KILLED_BY);
408.  	exercise(A_WIS, FALSE);
409.      }
410.  
411.      /* can pick it up unless you're totally non-synch'd with the artifact */
412.      if(badclass && badalign && (oart->spfx & SPFX_INTEL)) {
413.  	if (yours) pline("%s refuses to be held by you!", The(xname(obj)));
414.  	return 0;
415.      }
416.  
417.      return 1;
418.  }
419.  
420.  #endif /* OVLB */
421.  #ifdef OVL1
422.  
423.  STATIC_OVL int
424.  spec_applies(weap, ptr)
425.  register const struct artifact *weap;
426.  struct permonst *ptr;
427.  {
428.  	boolean yours = (ptr == &playermon);
429.  
430.  	if(!(weap->spfx & (SPFX_DBONUS | SPFX_ATTK)))
431.  	    return(weap->attk.adtyp == AD_PHYS);
432.  
433.  	if(weap->spfx & SPFX_DMONS)
434.  	    return((ptr == &mons[(int)weap->mtype]));
435.  	else if(weap->spfx & SPFX_DCLAS)
436.  	    return((weap->mtype == ptr->mlet));
437.  	else if(weap->spfx & SPFX_DFLAG1)
438.  	    return((ptr->mflags1 & weap->mtype) != 0L);
439.  	else if(weap->spfx & SPFX_DFLAG2)
440.  	    return((ptr->mflags2 & weap->mtype) != 0L);
441.  	else if(weap->spfx & SPFX_DALIGN)
442.  	    return(ptr->maligntyp == A_NONE ||
443.  		   sgn(ptr->maligntyp) != sgn(weap->alignment));
444.  	else if(weap->spfx & SPFX_ATTK) {
445.  	    switch(weap->attk.adtyp) {
446.  		case AD_FIRE:
447.  			return(!(yours ? Fire_resistance : resists_fire(ptr)));
448.  		case AD_COLD:
449.  			return(!(yours ? Cold_resistance : resists_cold(ptr)));
450.  		case AD_ELEC:
451.  			return(!(yours ? Shock_resistance : resists_elec(ptr)));
452.  		case AD_MAGM:
453.  		case AD_STUN:
454.  			return(!(yours ? Antimagic : (rn2(101) < ptr->mr)));
455.  		case AD_DRLI:
456.  			if (!yours) return(!resists_drli(ptr));
457.  			else return(
458.  #ifdef POLYSELF
459.  				resists_drli(uasmon) ||
460.  #endif
461.  				defends(AD_DRLI, uwep));
462.  		case AD_STON:
463.  #ifdef POLYSELF
464.  			if (yours) return(!resists_ston(uasmon));
465.  			else
466.  #endif
467.  				return(!resists_ston(ptr));
468.  		default:	impossible("Weird weapon special attack.");
469.  	    }
470.  	}
471.  	return(0);
472.  }
473.  
474.  int
475.  spec_abon(otmp, ptr)
476.  struct obj *otmp;
477.  struct permonst *ptr;
478.  {
479.  	register const struct artifact *weap;
480.  
481.  	if ((weap = get_artifact(otmp)) != 0)
482.  		if(spec_applies(weap, ptr))
483.  		    return((weap->attk.damn) ? rnd((int)weap->attk.damn) : 0);
484.  	return(0);
485.  }
486.  
487.  int
488.  spec_dbon(otmp, ptr, tmp)
489.  register struct obj *otmp;
490.  register struct permonst *ptr;
491.  register int	tmp;
492.  {
493.  	register const struct artifact *weap;
494.  
495.  	if ((weap = get_artifact(otmp)) != 0)
496.  	    if ((spec_dbon_applies = spec_applies(weap, ptr)) != 0)
497.  		return((weap->attk.damd) ? rnd((int)weap->attk.damd) : tmp);
498.  	else spec_dbon_applies = 0;
499.  	return(0);
500.  }
501.  
502.  #endif /* OVL1 */
503.  
504.  #ifdef OVLB
505.  
506.  /* Function used when someone attacks someone else with an artifact
507.   * weapon.  Only adds the special (artifact) damage, and returns a 1 if it
508.   * did something special (in which case the caller won't print the normal
509.   * hit message).  This should be called once upon every artifact attack;
510.   * dmgval() no longer takes artifact bonuses into account.  Possible
511.   * extension: change the killer so that when an orc kills you with
512.   * Stormbringer it's "killed by Stormbringer" instead of "killed by an orc".
513.   */
514.  boolean
515.  artifact_hit(magr, mdef, otmp, dmgptr, dieroll)
516.  struct monst *magr, *mdef;
517.  struct obj *otmp;
518.  int *dmgptr;
519.  int dieroll; /* needed for Magicbane and vorpal blades */
520.  {
521.  	boolean youattack = (magr == &youmonst);
522.  	boolean youdefend = (mdef == &youmonst);
523.  	boolean vis = (!youattack && magr && cansee(magr->mx, magr->my))
524.  		|| (!youdefend && cansee(mdef->mx, mdef->my));
525.  	boolean realizes_damage;
526.  
527.  	static const char you[] = "you";
528.  	const char *hittee = youdefend ? you : mon_nam(mdef);
529.  
530.  	/* The following takes care of most of the damage, but not all--
531.  	 * the exception being for level draining, which is specially
532.  	 * handled.  Messages are done in this function, however.
533.  	 */
534.  	*dmgptr += spec_dbon(otmp, youdefend ? &playermon
535.  		: mdef->data, *dmgptr);
536.  
537.  	if (youattack && youdefend) {
538.  		impossible("attacking yourself with weapon?");
539.  		return FALSE;
540.  	}
541.  
542.  	realizes_damage = (youdefend || vis) && spec_dbon_applies;
543.  
544.  	/* the four basic attacks: fire, cold, shock and missiles */
545.  	if (attacks(AD_FIRE, otmp)) {
546.  		if (realizes_damage) {
547.  			pline("The fiery blade burns %s!", hittee);
548.  			return TRUE;
549.  		}
550.  	}
551.  	if (attacks(AD_COLD, otmp)) {
552.  		if (realizes_damage) {
553.  			pline("The chilling blade freezes %s!", hittee);
554.  			return TRUE;
555.  		}
556.  	}
557.  	if (attacks(AD_ELEC, otmp)) {
558.  		if (realizes_damage) {
559.  			if(youattack && otmp != uwep)
560.  			    pline("%s hits %s!", The(xname(otmp)), hittee);
561.  			pline("A bolt of lightning zaps %s!", hittee);
562.  			return TRUE;
563.  		}
564.  	}
565.  	if (attacks(AD_MAGM, otmp)) {
566.  		if (realizes_damage) {
567.  			if(youattack && otmp != uwep)
568.  			    pline("%s hits %s!", The(xname(otmp)), hittee);
569.  			pline("A hail of magic missiles strikes %s!", hittee);
570.  			return TRUE;
571.  		}
572.  	}
573.  
574.  	/*
575.  	 * Magicbane's intrinsic magic is incompatible with normal
576.  	 * enchantment magic.  Thus, its effects have a negative
577.  	 * dependence on spe.  Against low mr victims, it typically
578.  	 * does "double athame" damage, 2d4.  Occasionally, it will
579.  	 * cast unbalancing magic which effectively averages out to
580.  	 * 4d4 damage (2.5d4 against high mr victims), for spe = 0.
581.  	 */
582.  
583.  #define MB_MAX_DIEROLL		8    /* rolls above this aren't magical */
584.  #define MB_INDEX_INIT		(-1)
585.  #define MB_INDEX_PROBE		0
586.  #define MB_INDEX_STUN		1
587.  #define MB_INDEX_SCARE		2
588.  #define MB_INDEX_PURGE		3
589.  #define MB_RESIST_ATTACK	(resist_index = attack_index)
590.  #define MB_RESISTED_ATTACK	(resist_index == attack_index)
591.  #define MB_UWEP_ATTACK		(youattack && (otmp == uwep))
592.  
593.  	if (attacks(AD_STUN, otmp) && (dieroll <= MB_MAX_DIEROLL)) {
594.  		int attack_index = MB_INDEX_INIT;
595.  		int resist_index = MB_INDEX_INIT;
596.  		int scare_dieroll = MB_MAX_DIEROLL / 2;
597.  
598.  		if (otmp->spe >= 3)
599.  			scare_dieroll /= (1 << (otmp->spe / 3));
600.  
601.  		*dmgptr += rnd(4);			/* 3d4 */
602.  
603.  		if (otmp->spe > rn2(10))		/* probe */
604.  			attack_index = MB_INDEX_PROBE;
605.  		else {					/* stun */
606.  			attack_index = MB_INDEX_STUN;
607.  			*dmgptr += rnd(4);		/* 4d4 */
608.  
609.  			if (youdefend)
610.  				make_stunned((HStun + 3), FALSE);
611.  			else
612.  				mdef->mstun = 1;
613.  		}
614.  		if (dieroll <= scare_dieroll) {		/* scare */
615.  			attack_index = MB_INDEX_SCARE;
616.  			*dmgptr += rnd(4);		/* 5d4 */
617.  
618.  			if (youdefend) {
619.  				if (Antimagic)
620.  					MB_RESIST_ATTACK;
621.  				else {
622.  					nomul(-3);
623.  					nomovemsg = "";
624.  #ifdef POLYSELF
625.  					if ((magr == u.ustuck)
626.  						&& sticks(uasmon)) {
627.  					    u.ustuck = (struct monst *)0;
628.  					    You("release %s!", mon_nam(magr));
629.  					}
630.  #endif
631.  				}
632.  			} else if (youattack) {
633.  				if (rn2(2) && resist(mdef,SPBOOK_CLASS,0,0)) {
634.  				    MB_RESIST_ATTACK;
635.  				} else {
636.  				    if (mdef == u.ustuck) {
637.  					if (u.uswallow)
638.  					    expels(mdef,mdef->data,TRUE);
639.  					else {
640.  #ifdef POLYSELF
641.  					    if (!sticks(uasmon))
642.  #endif
643.  					    {
644.  						u.ustuck = (struct monst *)0;
645.  						You("get released!");
646.  					    }
647.  					}
648.  				    }
649.  				    mdef->mflee = 1;
650.  				    mdef->mfleetim += 3;
651.  				}
652.  			}
653.  		}
654.  		if (dieroll <= (scare_dieroll / 2)) {	/* purge */
655.  			struct obj *ospell;
656.  #ifdef POLYSELF
657.  			struct permonst *old_uasmon = uasmon;
658.  #endif
659.  			attack_index = MB_INDEX_PURGE;
660.  			*dmgptr += rnd(4);		/* 6d4 */
661.  
662.  			/* Create a fake spell object, ala spell.c */
663.  			ospell = mksobj(SPE_CANCELLATION, FALSE, FALSE);
664.  			ospell->blessed = ospell->cursed = 0;
665.  			ospell->quan = 20L;
666.  
667.  			cancel_monst(mdef, ospell, youattack, FALSE, FALSE);
668.  
669.  #ifdef POLYSELF
670.  			if (youdefend && (old_uasmon != uasmon))
671.  				/* rehumanized, no more damage */
672.  				*dmgptr = 0;
673.  #endif
674.  			if (youdefend) {
675.  				if (Antimagic)
676.  					MB_RESIST_ATTACK;
677.  			} else {
678.  				if (!mdef->mcan)
679.  					MB_RESIST_ATTACK;
680.  
681.  				/* cancelled clay golems will die ... */
682.  				else if (mdef->data == &mons[PM_CLAY_GOLEM])
683.  					mdef->mhp = 1;
684.  			}
685.  
686.  			obfree(ospell, (struct obj *)0);
687.  		}
688.  
689.  		if (youdefend || mdef->mhp > 0) {  /* ??? -dkh- */
690.  			static const char *mb_verb[4] =
691.  				{"probe", "stun", "scare", "purge"};
692.  
693.  			if (youattack || youdefend || vis) {
694.  				pline("The magic-absorbing blade %ss %s!",
695.  					mb_verb[attack_index], hittee);
696.  
697.  				if (MB_RESISTED_ATTACK) {
698.  					pline("%s resist%s!",
699.  					youdefend ? "You" : Monnam(mdef),
700.  					youdefend ? "" : "s");
701.  
702.  					shieldeff(youdefend ? u.ux : mdef->mx,
703.  						youdefend ? u.uy : mdef->my);
704.  				}
705.  			}
706.  
707.  			/* Much ado about nothing.  More magic fanfare! */
708.  			if (MB_UWEP_ATTACK) {
709.  				if (attack_index == MB_INDEX_PURGE) {
710.  				    if (!MB_RESISTED_ATTACK &&
711.  					attacktype(mdef->data, AT_MAGC)) {
712.  					You("absorb magical energy!");
713.  					u.uenmax++;
714.  					u.uen++;
715.  					flags.botl = 1;
716.  				    }
717.  				} else if (attack_index == MB_INDEX_PROBE) {
718.  				    if (!rn2(4 * otmp->spe)) {
719.  					pline("The probe is insightful!");
720.  					/* pre-damage status */
721.  					mstatusline(mdef);
722.  				    }
723.  				}
724.  			} else if (youdefend && !MB_RESISTED_ATTACK
725.  				   && (attack_index == MB_INDEX_PURGE)) {
726.  				You("lose some magical energy!");
727.  				if (u.uenmax > 0) u.uenmax--;
728.  				if (u.uen > 0) u.uen--;
729.  					flags.botl = 1;
730.  			}
731.  
732.  			/* all this magic is confusing ... */
733.  			if (!rn2(12)) {
734.  			    if (youdefend)
735.  				make_confused((HConfusion + 4), FALSE);
736.  			    else
737.  				mdef->mconf = 1;
738.  
739.  			    if (youattack || youdefend || vis)
740.  				pline("%s %s confused.",
741.  				      youdefend ? "You" : Monnam(mdef),
742.  				      youdefend ? "are" : "is");
743.  			}
744.  		}
745.  		return TRUE;
746.  	}
747.  	/* end of Magicbane code */
748.  
749.  	/* We really want "on a natural 19 or 20" but Nethack does it in */
750.  	/* reverse from AD&D. */
751.  	if (spec_ability(otmp, SPFX_BEHEAD)) {
752.  #ifdef MULDGN
753.  	    if (otmp->oartifact == ART_TSURUGI_OF_MURAMASA && dieroll <= 2) {
754.  		/* not really beheading, but so close, why add another SPFX */
755.  		if (youattack && u.uswallow && mdef == u.ustuck) {
756.  		    You("slice %s wide open!", mon_nam(mdef));
757.  		    *dmgptr = mdef->mhp;
758.  		    return TRUE;
759.  		}
760.  		if (!youdefend) {
761.  			/* allow normal cutworm() call to add extra damage */
762.  			if(notonhead)
763.  			    return FALSE;
764.  
765.  			if (bigmonst(mdef->data)) {
766.  				if (youattack)
767.  					You("slice deeply into %s!",
768.  						mon_nam(mdef));
769.  				else if (vis)
770.  					pline("%s cuts deeply into %s!",
771.  					      Monnam(magr), mon_nam(mdef));
772.  				*dmgptr *= 2;
773.  				return TRUE;
774.  			}
775.  			*dmgptr = mdef->mhp;
776.  			pline("The razorsharp blade cuts %s in half!",
777.  			      mon_nam(mdef));
778.  			otmp->dknown = TRUE;
779.  			return TRUE;
780.  		} else {
781.  #ifdef POLYSELF
782.  			if (bigmonst(uasmon)) {
783.  				pline("%s cuts deeply into you!",
784.  					Monnam(magr));
785.  				*dmgptr *= 2;
786.  				return TRUE;
787.  			}
788.  #endif
789.  			*dmgptr = u.uhp;
790.  			pline("The razorsharp blade cuts you in half!");
791.  			otmp->dknown = TRUE;
792.  			return TRUE;
793.  		}
794.  	    } else 
795.  #endif /* MULDGN */
796.  	        if (otmp->oartifact == ART_VORPAL_BLADE &&
797.  			(dieroll <= 2 || mdef->data == &mons[PM_JABBERWOCK])) {
798.  		if (youattack && u.uswallow && mdef == u.ustuck)
799.  			return FALSE;
800.  		if (!youdefend) {
801.  			if (!has_head(mdef->data) || notonhead) {
802.  				if (youattack)
803.  					pline("Somehow you miss %s wildly.",
804.  						mon_nam(mdef));
805.  				else if (vis)
806.  					pline("Somehow %s misses wildly.",
807.  						mon_nam(magr));
808.  				*dmgptr = 0;
809.  				return (youattack || vis);
810.  			}
811.  			*dmgptr = mdef->mhp;
812.  			pline("%s cuts off %s head!",
813.  					artilist[ART_VORPAL_BLADE].name,
814.  					s_suffix(mon_nam(mdef)));
815.  			otmp->dknown = TRUE;
816.  			return TRUE;
817.  		} else {
818.  #ifdef POLYSELF
819.  			if (!has_head(uasmon)) {
820.  				pline("Somehow %s misses you wildly.",
821.  					mon_nam(magr));
822.  				*dmgptr = 0;
823.  				return TRUE;
824.  			}
825.  #endif
826.  			*dmgptr = u.uhp;
827.  			pline("%s cuts off your head!",
828.  					artilist[ART_VORPAL_BLADE].name);
829.  			otmp->dknown = TRUE;
830.  			/* Should amulets fall off? */
831.  			return TRUE;
832.  		}
833.  	    }
834.  	}
835.  	if (spec_ability(otmp, SPFX_DRLI)) {
836.  		if (!youdefend && !resists_drli(mdef->data)) {
837.  			if (vis) {
838.  			    if(otmp->oartifact == ART_STORMBRINGER)
839.  				pline("The %s blade draws the life from %s!",
840.  				      Hallucination ? hcolor() : Black,
841.  				      mon_nam(mdef));
842.  			    else
843.  				pline("%s draws the life from %s!",
844.  				      The(distant_name(otmp, xname)),
845.  				      mon_nam(mdef));
846.  			}
847.  			if (mdef->m_lev == 0) *dmgptr = mdef->mhp;
848.  			else {
849.  			    int drain = rnd(8);
850.  			    *dmgptr += drain;
851.  			    mdef->mhpmax -= drain;
852.  			    mdef->m_lev--;
853.  			    drain /= 2;
854.  			    if (drain) healup(drain, 0, FALSE, FALSE);
855.  			}
856.  			return vis;
857.  		} else if (youdefend
858.  #ifdef POLYSELF
859.  					&& !resists_drli(uasmon)
860.  #endif
861.  					&& !defends(AD_DRLI, uwep)) {
862.  			if (Blind)
863.  				You("feel an %s drain your life!",
864.  				    otmp->oartifact == ART_STORMBRINGER ?
865.  				    "unholy blade" : "object");
866.  			else {
867.  			    if(otmp->oartifact == ART_STORMBRINGER)
868.  				pline("The %s blade drains your life!",
869.  					Hallucination ? hcolor() : Black);
870.  			    else
871.  				pline("%s drains your life!",
872.  				      The(distant_name(otmp, xname)));
873.  			}
874.  			losexp();
875.  			if (magr->mhp < magr->mhpmax) {
876.  			    magr->mhp += rnd(4);
877.  			    /* TODO: Should be related to # of HP you lost. */
878.  			    if (magr->mhp > magr->mhpmax) magr->mhp = magr->mhpmax;
879.  			}
880.  			return TRUE;
881.  		}
882.  	}
883.  	return FALSE;
884.  }
885.  
886.  static const char recharge_type[] = { ALLOW_COUNT, ALL_CLASSES, 0 };
887.  static const char NEARDATA invoke_types[] =
888.  	{ ALL_CLASSES, WEAPON_CLASS, ARMOR_CLASS, RING_CLASS, AMULET_CLASS,
889.  	      TOOL_CLASS, 0 };
890.  
891.  int
892.  doinvoke()
893.  {
894.      register struct obj *obj;
895.  
896.      obj = getobj(invoke_types, "invoke");
897.      if(!obj) return 0;
898.      return arti_invoke(obj);
899.  }
900.  
901.  STATIC_OVL int
902.  arti_invoke(obj)
903.      register struct obj *obj;
904.  {
905.      register const struct artifact *oart = get_artifact(obj);
906.  
907.      if(!oart || !oart->inv_prop) {
908.  	if(obj->otyp == CRYSTAL_BALL)
909.  	    use_crystal_ball(obj);
910.  	else
911.  	    pline("Nothing happens.");
912.  	return 1;
913.      }
914.  
915.      if(oart->inv_prop > LAST_PROP) {
916.  	/* It's a special power, not "just" a property */
917.  	if(obj->age > monstermoves) {
918.  	    /* the artifact is tired :-) */
919.  	    You("feel that %s is ignoring you.", the(xname(obj)));
920.  	    return 1;
921.  	}
922.  	obj->age = monstermoves + rnz(100);
923.  
924.  	switch(oart->inv_prop) {
925.  	case TAMING: {
926.  	    struct obj *pseudo = mksobj(SPE_CHARM_MONSTER, FALSE, FALSE);
927.  	    pseudo->blessed = pseudo->cursed = 0;
928.  	    pseudo->quan = 20L;			/* do not let useup get it */
929.  	    (void) seffects(pseudo);
930.  	    obfree(pseudo, (struct obj *)0);	/* now, get rid of it */
931.  	    break;
932.  	  }
933.  	case HEALING: {
934.  	    int healamt = (u.uhpmax + 1 - u.uhp) / 2;
935.  	    if(healamt || Sick || (Blinded > 1))
936.  		You("feel better.");
937.  	    else
938.  		goto nothing_special;
939.  	    if(healamt) u.uhp += healamt;
940.  	    if(Sick) make_sick(0L,FALSE);
941.  	    if(Blinded > 1) make_blinded(0L,FALSE);
942.  	    flags.botl = 1;
943.  	    break;
944.  	  }
945.  	case ENERGY_BOOST: {
946.  	    int epboost = (u.uenmax + 1 - u.uen) / 2;
947.  	    if(epboost) {
948.  		You("feel re-energized.");
949.  		u.uen += epboost;
950.  	    } else
951.  		goto nothing_special;
952.  	    break;
953.  	  }
954.  	case UNTRAP: {
955.  	    if(!untrap(TRUE)) {
956.  		obj->age = 0; /* don't charge for changing their mind */
957.  		return 0;
958.  	    }
959.  	    break;
960.  	  }
961.  	case CHARGE_OBJ: {
962.  	    struct obj *otmp = getobj(recharge_type, "charge");
963.  	    if (!otmp) {
964.  		obj->age = 0;
965.  		return 0;
966.  	    }
967.  	    recharge(otmp, obj->blessed ? 1 : obj->cursed ? -1 : 0);
968.  	    break;
969.  	  }
970.  	case LEV_TELE:
971.  	    level_tele();
972.  	    break;
973.  	case CREATE_PORTAL: {
974.  	    register int i;
975.  	    d_level newlev;
976.  	    char buf[BUFSIZ];
977.  	    extern int n_dgns; /* from dungeon.c */
978.  	    winid tmpwin = create_nhwindow(NHW_MENU);
979.  	    char hc;
980.  
981.  	    start_menu(tmpwin);
982.  	    add_menu(tmpwin, 0, 0, "Dungeons:");
983.  	    add_menu(tmpwin, 0, 0, "");
984.  	    for (i = 0, hc = 'a'; i < n_dgns; i++) {
985.  		if (!dungeons[i].dunlev_ureached) continue;
986.  		Sprintf(buf, "%c - %s", hc, dungeons[i].dname);
987.  		add_menu(tmpwin, hc, 0, buf);
988.  		hc++;
989.  	    }
990.  	    add_menu(tmpwin, 0, 0, "");
991.  	    end_menu(tmpwin, '\033', "\033","Open a portal to which dungeon?");
992.  	    if (hc > 'b') {
993.  		/* more than one entry; display menu for choices */
994.  		hc = select_menu(tmpwin);
995.  	    } else
996.  		hc = 'a';
997.  	    destroy_nhwindow(tmpwin);
998.  
999.  	    /* assume there won't be more than 26 dungeons */
1000. 	    if (hc < 'a' || hc > 'z')
1001. 		goto nothing_special;
1002. 
1003. 	    /* run thru dungeon array to find the one they selected */
1004. 	    for (i = 0; hc >= 'a'; i++)
1005. 		if (dungeons[i].dunlev_ureached) hc--;
1006. 	    i--; /* we added one extra */
1007. 
1008. 	    /*
1009. 	     * i is now index into dungeon structure for the new dungeon.
1010. 	     * Find the closest level in the given dungeon, open
1011. 	     * a use-once portal to that dungeon and go there.
1012. 	     * The closest level is either the entry or dunlev_ureached.
1013. 	     */
1014. 	    newlev.dnum = i;
1015. 	    if(dungeons[i].depth_start >= depth(&u.uz))
1016. 		newlev.dlevel = dungeons[i].entry_lev;
1017. 	    else
1018. 		newlev.dlevel = dungeons[i].dunlev_ureached;
1019. 	    if(u.uhave.amulet || In_endgame(&u.uz) || In_endgame(&newlev) ||
1020. 	       newlev.dnum == u.uz.dnum) {
1021. 		You("feel very disoriented for a moment.");
1022. 	    } else {
1023. 		if(!Blind) You("are surrounded by a shimmering sphere!");
1024. 		else You("momentarily feel weightless.");
1025. 		goto_level(&newlev, FALSE, FALSE, FALSE);
1026. 	    }
1027. 	    break;
1028. 	  }
1029. 	}
1030.     } else {
1031. 	boolean on;
1032. 	unsigned long cprop;
1033. 	cprop = u.uprops[oart->inv_prop].p_flgs ^= W_ARTI;
1034. 	on = (cprop & W_ARTI) != 0; /* did we just turn on the invoked prop? */
1035. 
1036. 	if(on && obj->age > monstermoves) {
1037. 	    /* the artifact is tired :-) */
1038. 	    u.uprops[oart->inv_prop].p_flgs ^= W_ARTI;
1039. 	    You("feel that %s is ignoring you.", the(xname(obj)));
1040. 	    return 1;
1041. 	} else if(!on) {
1042. 	    /* when turning off property, determine downtime */
1043. 	    /* arbitrary for now until we can tune this -dlc */
1044. 	    obj->age = monstermoves + rnz(100);
1045. 	}
1046. 
1047. 	if(cprop & ~W_ARTI) {
1048. 	nothing_special:
1049. 	    /* you had the property from some other source too */
1050. 	    if (carried(obj))
1051. 		You("feel a surge of power, but notice no effect.");
1052. 	    return 1;
1053. 	}
1054. 	switch(oart->inv_prop) {
1055. 	case CONFLICT:
1056. 	    if(on) You("feel like a rabble-rouser.");
1057. 	    else You("feel the tension decrease in your vicinity.");
1058. 	    break;
1059. 	case LEVITATION:
1060. 	    if(on) float_up();
1061. 	    else (void) float_down();
1062. 	    break;
1063. 	case INVIS:
1064. 	    if (!See_invisible && !Blind) {
1065. 		newsym(u.ux,u.uy);
1066. 		if (on) {
1067. 		    Your("body takes on a %s transparency...",
1068. 			 Hallucination ? "normal" : "strange");
1069. 		} else {
1070. 		    Your("body seems to unfade...");
1071. 		}
1072. 	    } else goto nothing_special;
1073. 	    break;
1074. 	}
1075.     }
1076. 
1077.     return 1;
1078. }
1079. 
1080. #endif /* OVLB */
1081. 
1082. /*artifact.c*/