Source:NetHack 3.2.0/artifact.c

From NetHackWiki
(Redirected from NetHack 3.2.0/artifact.c)
Jump to navigation Jump to search

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