1/*	SCCS Id: @(#)makemon.c	3.4	2003/09/06	*/
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 "epri.h"
7#include "emin.h"
8#include "edog.h"
9#ifdef REINCARNATION
10#include <ctype.h>
11#endif
12
13STATIC_VAR NEARDATA struct monst zeromonst;
14
15/* this assumes that a human quest leader or nemesis is an archetype
16   of the corresponding role; that isn't so for some roles (tourist
17   for instance) but is for the priests and monks we use it for... */
18#define quest_mon_represents_role(mptr,role_pm) \
19		(mptr->mlet == S_HUMAN && Role_if(role_pm) && \
20		  (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS))
21
22#ifdef OVL0
23STATIC_DCL boolean FDECL(uncommon, (int));
24STATIC_DCL int FDECL(align_shift, (struct permonst *));
25#endif /* OVL0 */
26STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
27STATIC_DCL void FDECL(m_initgrp,(struct monst *,int,int,int));
28STATIC_DCL void FDECL(m_initthrow,(struct monst *,int,int));
29STATIC_DCL void FDECL(m_initweap,(struct monst *));
30#ifdef OVL1
31STATIC_DCL void FDECL(m_initinv,(struct monst *));
32#endif /* OVL1 */
33
34extern const int monstr[];
35
36#define m_initsgrp(mtmp, x, y)	m_initgrp(mtmp, x, y, 3)
37#define m_initlgrp(mtmp, x, y)	m_initgrp(mtmp, x, y, 10)
38#define toostrong(monindx, lev) (monstr[monindx] > lev)
39#define tooweak(monindx, lev)	(monstr[monindx] < lev)
40
41#ifdef OVLB
42boolean
43is_home_elemental(ptr)
44register struct permonst *ptr;
45{
46	if (ptr->mlet == S_ELEMENTAL)
47	    switch (monsndx(ptr)) {
48		case PM_AIR_ELEMENTAL: return Is_airlevel(&u.uz);
49		case PM_FIRE_ELEMENTAL: return Is_firelevel(&u.uz);
50		case PM_EARTH_ELEMENTAL: return Is_earthlevel(&u.uz);
51		case PM_WATER_ELEMENTAL: return Is_waterlevel(&u.uz);
52	    }
53	return FALSE;
54}
55
56/*
57 * Return true if the given monster cannot exist on this elemental level.
58 */
59STATIC_OVL boolean
60wrong_elem_type(ptr)
61    register struct permonst *ptr;
62{
63    if (ptr->mlet == S_ELEMENTAL) {
64	return((boolean)(!is_home_elemental(ptr)));
65    } else if (Is_earthlevel(&u.uz)) {
66	/* no restrictions? */
67    } else if (Is_waterlevel(&u.uz)) {
68	/* just monsters that can swim */
69	if(!is_swimmer(ptr)) return TRUE;
70    } else if (Is_firelevel(&u.uz)) {
71	if (!pm_resistance(ptr,MR_FIRE)) return TRUE;
72    } else if (Is_airlevel(&u.uz)) {
73	if(!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
74	   && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
75	    return TRUE;
76    }
77    return FALSE;
78}
79
80STATIC_OVL void
81m_initgrp(mtmp, x, y, n)	/* make a group just like mtmp */
82register struct monst *mtmp;
83register int x, y, n;
84{
85	coord mm;
86	register int cnt = rnd(n);
87	struct monst *mon;
88#if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
89	/* There is an unresolved problem with several people finding that
90	 * the game hangs eating CPU; if interrupted and restored, the level
91	 * will be filled with monsters.  Of those reports giving system type,
92	 * there were two DG/UX and two HP-UX, all using gcc as the compiler.
93	 * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
94	 * problem went away for him and another reporter-to-newsgroup
95	 * after adding this debugging code.  This has almost got to be a
96	 * compiler bug, but until somebody tracks it down and gets it fixed,
97	 * might as well go with the "but it went away when I tried to find
98	 * it" code.
99	 */
100	int cnttmp,cntdiv;
101
102	cnttmp = cnt;
103# ifdef DEBUG
104	pline("init group call x=%d,y=%d,n=%d,cnt=%d.", x, y, n, cnt);
105# endif
106	cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1);
107#endif
108	/* Tuning: cut down on swarming at low character levels [mrs] */
109	cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
110#if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
111	if (cnt != (cnttmp/cntdiv)) {
112		pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
113			(u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1,
114			cnttmp, cntdiv);
115	}
116#endif
117	if(!cnt) cnt++;
118#if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
119	if (cnt < 0) cnt = 1;
120	if (cnt > 10) cnt = 10;
121#endif
122
123	mm.x = x;
124	mm.y = y;
125	while(cnt--) {
126		if (peace_minded(mtmp->data)) continue;
127		/* Don't create groups of peaceful monsters since they'll get
128		 * in our way.  If the monster has a percentage chance so some
129		 * are peaceful and some are not, the result will just be a
130		 * smaller group.
131		 */
132		if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
133		    mon = makemon(mtmp->data, mm.x, mm.y, NO_MM_FLAGS);
134		    mon->mpeaceful = FALSE;
135		    mon->mavenge = 0;
136		    set_malign(mon);
137		    /* Undo the second peace_minded() check in makemon(); if the
138		     * monster turned out to be peaceful the first time we
139		     * didn't create it at all; we don't want a second check.
140		     */
141		}
142	}
143}
144
145STATIC_OVL
146void
147m_initthrow(mtmp,otyp,oquan)
148struct monst *mtmp;
149int otyp,oquan;
150{
151	register struct obj *otmp;
152
153	otmp = mksobj(otyp, TRUE, FALSE);
154	otmp->quan = (long) rn1(oquan, 3);
155	otmp->owt = weight(otmp);
156	if (otyp == ORCISH_ARROW) otmp->opoisoned = TRUE;
157	(void) mpickobj(mtmp, otmp);
158}
159
160#endif /* OVLB */
161#ifdef OVL2
162
163STATIC_OVL void
164m_initweap(mtmp)
165register struct monst *mtmp;
166{
167	register struct permonst *ptr = mtmp->data;
168	register int mm = monsndx(ptr);
169	struct obj *otmp;
170
171#ifdef REINCARNATION
172	if (Is_rogue_level(&u.uz)) return;
173#endif
174/*
175 *	first a few special cases:
176 *
177 *		giants get a boulder to throw sometimes.
178 *		ettins get clubs
179 *		kobolds get darts to throw
180 *		centaurs get some sort of bow & arrows or bolts
181 *		soldiers get all sorts of things.
182 *		kops get clubs & cream pies.
183 */
184	switch (ptr->mlet) {
185	    case S_GIANT:
186		if (rn2(2)) (void)mongets(mtmp, (mm != PM_ETTIN) ?
187				    BOULDER : CLUB);
188		break;
189	    case S_HUMAN:
190		if(is_mercenary(ptr)) {
191		    int w1 = 0, w2 = 0;
192		    switch (mm) {
193
194			case PM_WATCHMAN:
195			case PM_SOLDIER:
196			  if (!rn2(3)) {
197			      w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
198			      w2 = rn2(2) ? DAGGER : KNIFE;
199			  } else w1 = rn2(2) ? SPEAR : SHORT_SWORD;
200			  break;
201			case PM_SERGEANT:
202			  w1 = rn2(2) ? FLAIL : MACE;
203			  break;
204			case PM_LIEUTENANT:
205			  w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
206			  break;
207			case PM_CAPTAIN:
208			case PM_WATCH_CAPTAIN:
209			  w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
210			  break;
211			default:
212			  if (!rn2(4)) w1 = DAGGER;
213			  if (!rn2(7)) w2 = SPEAR;
214			  break;
215		    }
216		    if (w1) (void)mongets(mtmp, w1);
217		    if (!w2 && w1 != DAGGER && !rn2(4)) w2 = KNIFE;
218		    if (w2) (void)mongets(mtmp, w2);
219		} else if (is_elf(ptr)) {
220		    if (rn2(2))
221			(void) mongets(mtmp,
222				   rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
223		    if (rn2(2)) (void)mongets(mtmp, ELVEN_LEATHER_HELM);
224		    else if (!rn2(4)) (void)mongets(mtmp, ELVEN_BOOTS);
225		    if (rn2(2)) (void)mongets(mtmp, ELVEN_DAGGER);
226		    switch (rn2(3)) {
227			case 0:
228			    if (!rn2(4)) (void)mongets(mtmp, ELVEN_SHIELD);
229			    if (rn2(3)) (void)mongets(mtmp, ELVEN_SHORT_SWORD);
230			    (void)mongets(mtmp, ELVEN_BOW);
231			    m_initthrow(mtmp, ELVEN_ARROW, 12);
232			    break;
233			case 1:
234			    (void)mongets(mtmp, ELVEN_BROADSWORD);
235			    if (rn2(2)) (void)mongets(mtmp, ELVEN_SHIELD);
236			    break;
237			case 2:
238			    if (rn2(2)) {
239				(void)mongets(mtmp, ELVEN_SPEAR);
240				(void)mongets(mtmp, ELVEN_SHIELD);
241			    }
242			    break;
243		    }
244		    if (mm == PM_ELVENKING) {
245			if (rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
246			    (void)mongets(mtmp, PICK_AXE);
247			if (!rn2(50)) (void)mongets(mtmp, CRYSTAL_BALL);
248		    }
249		} else if (ptr->msound == MS_PRIEST ||
250			quest_mon_represents_role(ptr,PM_PRIEST)) {
251		    otmp = mksobj(MACE, FALSE, FALSE);
252		    if(otmp) {
253			otmp->spe = rnd(3);
254			if(!rn2(2)) curse(otmp);
255			(void) mpickobj(mtmp, otmp);
256		    }
257		}
258		break;
259
260	    case S_ANGEL:
261		{
262		    int spe2;
263
264		    /* create minion stuff; can't use mongets */
265		    otmp = mksobj(LONG_SWORD, FALSE, FALSE);
266
267		    /* maybe make it special */
268		    if (!rn2(20) || is_lord(ptr))
269			otmp = oname(otmp, artiname(
270				rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
271		    bless(otmp);
272		    otmp->oerodeproof = TRUE;
273		    spe2 = rn2(4);
274		    otmp->spe = max(otmp->spe, spe2);
275		    (void) mpickobj(mtmp, otmp);
276
277		    otmp = mksobj(!rn2(4) || is_lord(ptr) ?
278				  SHIELD_OF_REFLECTION : LARGE_SHIELD,
279				  FALSE, FALSE);
280		    otmp->cursed = FALSE;
281		    otmp->oerodeproof = TRUE;
282		    otmp->spe = 0;
283		    (void) mpickobj(mtmp, otmp);
284		}
285		break;
286
287	    case S_HUMANOID:
288		if (mm == PM_HOBBIT) {
289		    switch (rn2(3)) {
290			case 0:
291			    (void)mongets(mtmp, DAGGER);
292			    break;
293			case 1:
294			    (void)mongets(mtmp, ELVEN_DAGGER);
295			    break;
296			case 2:
297			    (void)mongets(mtmp, SLING);
298			    break;
299		      }
300		    if (!rn2(10)) (void)mongets(mtmp, ELVEN_MITHRIL_COAT);
301		    if (!rn2(10)) (void)mongets(mtmp, DWARVISH_CLOAK);
302		} else if (is_dwarf(ptr)) {
303		    if (rn2(7)) (void)mongets(mtmp, DWARVISH_CLOAK);
304		    if (rn2(7)) (void)mongets(mtmp, IRON_SHOES);
305		    if (!rn2(4)) {
306			(void)mongets(mtmp, DWARVISH_SHORT_SWORD);
307			/* note: you can't use a mattock with a shield */
308			if (rn2(2)) (void)mongets(mtmp, DWARVISH_MATTOCK);
309			else {
310				(void)mongets(mtmp, AXE);
311				(void)mongets(mtmp, DWARVISH_ROUNDSHIELD);
312			}
313			(void)mongets(mtmp, DWARVISH_IRON_HELM);
314			if (!rn2(3))
315			    (void)mongets(mtmp, DWARVISH_MITHRIL_COAT);
316		    } else {
317			(void)mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
318		    }
319		}
320		break;
321# ifdef KOPS
322	    case S_KOP:		/* create Keystone Kops with cream pies to
323				 * throw. As suggested by KAA.	   [MRS]
324				 */
325		if (!rn2(4)) m_initthrow(mtmp, CREAM_PIE, 2);
326		if (!rn2(3)) (void)mongets(mtmp,(rn2(2)) ? CLUB : RUBBER_HOSE);
327		break;
328# endif
329	    case S_ORC:
330		if(rn2(2)) (void)mongets(mtmp, ORCISH_HELM);
331		switch (mm != PM_ORC_CAPTAIN ? mm :
332			rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
333		    case PM_MORDOR_ORC:
334			if(!rn2(3)) (void)mongets(mtmp, SCIMITAR);
335			if(!rn2(3)) (void)mongets(mtmp, ORCISH_SHIELD);
336			if(!rn2(3)) (void)mongets(mtmp, KNIFE);
337			if(!rn2(3)) (void)mongets(mtmp, ORCISH_CHAIN_MAIL);
338			break;
339		    case PM_URUK_HAI:
340			if(!rn2(3)) (void)mongets(mtmp, ORCISH_CLOAK);
341			if(!rn2(3)) (void)mongets(mtmp, ORCISH_SHORT_SWORD);
342			if(!rn2(3)) (void)mongets(mtmp, IRON_SHOES);
343			if(!rn2(3)) {
344			    (void)mongets(mtmp, ORCISH_BOW);
345			    m_initthrow(mtmp, ORCISH_ARROW, 12);
346			}
347			if(!rn2(3)) (void)mongets(mtmp, URUK_HAI_SHIELD);
348			break;
349		    default:
350			if (mm != PM_ORC_SHAMAN && rn2(2))
351			  (void)mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
352						   ? ORCISH_DAGGER : SCIMITAR);
353		}
354		break;
355	    case S_OGRE:
356		if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
357		    (void) mongets(mtmp, BATTLE_AXE);
358		else
359		    (void) mongets(mtmp, CLUB);
360		break;
361	    case S_TROLL:
362		if (!rn2(2)) switch (rn2(4)) {
363		    case 0: (void)mongets(mtmp, RANSEUR); break;
364		    case 1: (void)mongets(mtmp, PARTISAN); break;
365		    case 2: (void)mongets(mtmp, GLAIVE); break;
366		    case 3: (void)mongets(mtmp, SPETUM); break;
367		}
368		break;
369	    case S_KOBOLD:
370		if (!rn2(4)) m_initthrow(mtmp, DART, 12);
371		break;
372
373	    case S_CENTAUR:
374		if (rn2(2)) {
375		    if(ptr == &mons[PM_FOREST_CENTAUR]) {
376			(void)mongets(mtmp, BOW);
377			m_initthrow(mtmp, ARROW, 12);
378		    } else {
379			(void)mongets(mtmp, CROSSBOW);
380			m_initthrow(mtmp, CROSSBOW_BOLT, 12);
381		    }
382		}
383		break;
384	    case S_WRAITH:
385		(void)mongets(mtmp, KNIFE);
386		(void)mongets(mtmp, LONG_SWORD);
387		break;
388	    case S_ZOMBIE:
389		if (!rn2(4)) (void)mongets(mtmp, LEATHER_ARMOR);
390		if (!rn2(4))
391			(void)mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
392		break;
393	    case S_LIZARD:
394		if (mm == PM_SALAMANDER)
395			(void)mongets(mtmp, (rn2(7) ? SPEAR : rn2(3) ?
396					     TRIDENT : STILETTO));
397		break;
398	    case S_DEMON:
399		switch (mm) {
400		    case PM_BALROG:
401			(void)mongets(mtmp, BULLWHIP);
402			(void)mongets(mtmp, BROADSWORD);
403			break;
404		    case PM_ORCUS:
405			(void)mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
406			break;
407		    case PM_HORNED_DEVIL:
408			(void)mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
409			break;
410		    case PM_DISPATER:
411			(void)mongets(mtmp, WAN_STRIKING);
412			break;
413		    case PM_YEENOGHU:
414			(void)mongets(mtmp, FLAIL);
415			break;
416		}
417		/* prevent djinnis and mail daemons from leaving objects when
418		 * they vanish
419		 */
420		if (!is_demon(ptr)) break;
421		/* fall thru */
422/*
423 *	Now the general case, Some chance of getting some type
424 *	of weapon for "normal" monsters.  Certain special types
425 *	of monsters will get a bonus chance or different selections.
426 */
427	    default:
428	      {
429		int bias;
430
431		bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
432		switch(rnd(14 - (2 * bias))) {
433		    case 1:
434			if(strongmonst(ptr)) (void) mongets(mtmp, BATTLE_AXE);
435			else m_initthrow(mtmp, DART, 12);
436			break;
437		    case 2:
438			if(strongmonst(ptr))
439			    (void) mongets(mtmp, TWO_HANDED_SWORD);
440			else {
441			    (void) mongets(mtmp, CROSSBOW);
442			    m_initthrow(mtmp, CROSSBOW_BOLT, 12);
443			}
444			break;
445		    case 3:
446			(void) mongets(mtmp, BOW);
447			m_initthrow(mtmp, ARROW, 12);
448			break;
449		    case 4:
450			if(strongmonst(ptr)) (void) mongets(mtmp, LONG_SWORD);
451			else m_initthrow(mtmp, DAGGER, 3);
452			break;
453		    case 5:
454			if(strongmonst(ptr))
455			    (void) mongets(mtmp, LUCERN_HAMMER);
456			else (void) mongets(mtmp, AKLYS);
457			break;
458		    default:
459			break;
460		}
461	      }
462	      break;
463	}
464	if ((int) mtmp->m_lev > rn2(75))
465		(void) mongets(mtmp, rnd_offensive_item(mtmp));
466}
467
468#endif /* OVL2 */
469#ifdef OVL1
470
471#ifdef GOLDOBJ
472/*
473 *   Makes up money for monster's inventory.
474 *   This will change with silver & copper coins
475 */
476void
477mkmonmoney(mtmp, amount)
478struct monst *mtmp;
479long amount;
480{
481    struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
482    gold->quan = amount;
483    add_to_minv(mtmp, gold);
484}
485#endif
486
487STATIC_OVL void
488m_initinv(mtmp)
489register struct	monst	*mtmp;
490{
491	register int cnt;
492	register struct obj *otmp;
493	register struct permonst *ptr = mtmp->data;
494#ifdef REINCARNATION
495	if (Is_rogue_level(&u.uz)) return;
496#endif
497/*
498 *	Soldiers get armour & rations - armour approximates their ac.
499 *	Nymphs may get mirror or potion of object detection.
500 */
501	switch(ptr->mlet) {
502
503	    case S_HUMAN:
504		if(is_mercenary(ptr)) {
505		    register int mac;
506
507		    switch(monsndx(ptr)) {
508			case PM_GUARD: mac = -1; break;
509			case PM_SOLDIER: mac = 3; break;
510			case PM_SERGEANT: mac = 0; break;
511			case PM_LIEUTENANT: mac = -2; break;
512			case PM_CAPTAIN: mac = -3; break;
513			case PM_WATCHMAN: mac = 3; break;
514			case PM_WATCH_CAPTAIN: mac = -2; break;
515			default: impossible("odd mercenary %d?", monsndx(ptr));
516				mac = 0;
517				break;
518		    }
519
520		    if (mac < -1 && rn2(5))
521			mac += 7 + mongets(mtmp, (rn2(5)) ?
522					   PLATE_MAIL : CRYSTAL_PLATE_MAIL);
523		    else if (mac < 3 && rn2(5))
524			mac += 6 + mongets(mtmp, (rn2(3)) ?
525					   SPLINT_MAIL : BANDED_MAIL);
526		    else if (rn2(5))
527			mac += 3 + mongets(mtmp, (rn2(3)) ?
528					   RING_MAIL : STUDDED_LEATHER_ARMOR);
529		    else
530			mac += 2 + mongets(mtmp, LEATHER_ARMOR);
531
532		    if (mac < 10 && rn2(3))
533			mac += 1 + mongets(mtmp, HELMET);
534		    else if (mac < 10 && rn2(2))
535			mac += 1 + mongets(mtmp, DENTED_POT);
536		    if (mac < 10 && rn2(3))
537			mac += 1 + mongets(mtmp, SMALL_SHIELD);
538		    else if (mac < 10 && rn2(2))
539			mac += 2 + mongets(mtmp, LARGE_SHIELD);
540		    if (mac < 10 && rn2(3))
541			mac += 1 + mongets(mtmp, LOW_BOOTS);
542		    else if (mac < 10 && rn2(2))
543			mac += 2 + mongets(mtmp, HIGH_BOOTS);
544		    if (mac < 10 && rn2(3))
545			mac += 1 + mongets(mtmp, LEATHER_GLOVES);
546		    else if (mac < 10 && rn2(2))
547			mac += 1 + mongets(mtmp, LEATHER_CLOAK);
548
549		    if(ptr != &mons[PM_GUARD] &&
550			ptr != &mons[PM_WATCHMAN] &&
551			ptr != &mons[PM_WATCH_CAPTAIN]) {
552			if (!rn2(3)) (void) mongets(mtmp, K_RATION);
553			if (!rn2(2)) (void) mongets(mtmp, C_RATION);
554			if (ptr != &mons[PM_SOLDIER] && !rn2(3))
555				(void) mongets(mtmp, BUGLE);
556		    } else
557			   if (ptr == &mons[PM_WATCHMAN] && rn2(3))
558				(void) mongets(mtmp, TIN_WHISTLE);
559		} else if (ptr == &mons[PM_SHOPKEEPER]) {
560		    (void) mongets(mtmp,SKELETON_KEY);
561		    switch (rn2(4)) {
562		    /* MAJOR fall through ... */
563		    case 0: (void) mongets(mtmp, WAN_MAGIC_MISSILE);
564		    case 1: (void) mongets(mtmp, POT_EXTRA_HEALING);
565		    case 2: (void) mongets(mtmp, POT_HEALING);
566		    case 3: (void) mongets(mtmp, WAN_STRIKING);
567		    }
568		} else if (ptr->msound == MS_PRIEST ||
569			quest_mon_represents_role(ptr,PM_PRIEST)) {
570		    (void) mongets(mtmp, rn2(7) ? ROBE :
571					     rn2(3) ? CLOAK_OF_PROTECTION :
572						 CLOAK_OF_MAGIC_RESISTANCE);
573		    (void) mongets(mtmp, SMALL_SHIELD);
574#ifndef GOLDOBJ
575		    mtmp->mgold = (long)rn1(10,20);
576#else
577		    mkmonmoney(mtmp,(long)rn1(10,20));
578#endif
579		} else if (quest_mon_represents_role(ptr,PM_MONK)) {
580		    (void) mongets(mtmp, rn2(11) ? ROBE :
581					     CLOAK_OF_MAGIC_RESISTANCE);
582		}
583		break;
584	    case S_NYMPH:
585		if(!rn2(2)) (void) mongets(mtmp, MIRROR);
586		if(!rn2(2)) (void) mongets(mtmp, POT_OBJECT_DETECTION);
587		break;
588	    case S_GIANT:
589		if (ptr == &mons[PM_MINOTAUR]) {
590		    if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
591			(void) mongets(mtmp, WAN_DIGGING);
592		} else if (is_giant(ptr)) {
593		    for (cnt = rn2((int)(mtmp->m_lev / 2)); cnt; cnt--) {
594			otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL,LUCKSTONE-1),
595				      FALSE, FALSE);
596			otmp->quan = (long) rn1(2, 3);
597			otmp->owt = weight(otmp);
598			(void) mpickobj(mtmp, otmp);
599		    }
600		}
601		break;
602	    case S_WRAITH:
603		if (ptr == &mons[PM_NAZGUL]) {
604			otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
605			curse(otmp);
606			(void) mpickobj(mtmp, otmp);
607		}
608		break;
609	    case S_LICH:
610		if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
611			(void)mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
612		else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
613			otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF,
614				      TRUE, rn2(13) ? FALSE : TRUE);
615			if (otmp->spe < 2) otmp->spe = rnd(3);
616			if (!rn2(4)) otmp->oerodeproof = 1;
617			(void) mpickobj(mtmp, otmp);
618		}
619		break;
620	    case S_MUMMY:
621		if (rn2(7)) (void)mongets(mtmp, MUMMY_WRAPPING);
622		break;
623	    case S_QUANTMECH:
624		if (!rn2(20)) {
625			otmp = mksobj(LARGE_BOX, FALSE, FALSE);
626			otmp->spe = 1; /* flag for special box */
627			otmp->owt = weight(otmp);
628			(void) mpickobj(mtmp, otmp);
629		}
630		break;
631	    case S_LEPRECHAUN:
632#ifndef GOLDOBJ
633		mtmp->mgold = (long) d(level_difficulty(), 30);
634#else
635		mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
636#endif
637		break;
638	    case S_DEMON:
639	    	/* moved here from m_initweap() because these don't
640		   have AT_WEAP so m_initweap() is not called for them */
641		if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
642			(void)mongets(mtmp, SPEAR);
643		} else if (ptr == &mons[PM_ASMODEUS]) {
644			(void)mongets(mtmp, WAN_COLD);
645			(void)mongets(mtmp, WAN_FIRE);
646		}
647		break;
648	    default:
649		break;
650	}
651
652	/* ordinary soldiers rarely have access to magic (or gold :-) */
653	if (ptr == &mons[PM_SOLDIER] && rn2(13)) return;
654
655	if ((int) mtmp->m_lev > rn2(50))
656		(void) mongets(mtmp, rnd_defensive_item(mtmp));
657	if ((int) mtmp->m_lev > rn2(100))
658		(void) mongets(mtmp, rnd_misc_item(mtmp));
659#ifndef GOLDOBJ
660	if (likes_gold(ptr) && !mtmp->mgold && !rn2(5))
661		mtmp->mgold =
662		      (long) d(level_difficulty(), mtmp->minvent ? 5 : 10);
663#else
664	if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
665		mkmonmoney(mtmp, (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
666#endif
667}
668
669/* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
670struct monst *
671clone_mon(mon, x, y)
672struct monst *mon;
673xchar x, y;	/* clone's preferred location or 0 (near mon) */
674{
675	coord mm;
676	struct monst *m2;
677
678	/* may be too weak or have been extinguished for population control */
679	if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
680	    return (struct monst *)0;
681
682	if (x == 0) {
683	    mm.x = mon->mx;
684	    mm.y = mon->my;
685	    if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
686		return (struct monst *)0;
687	} else if (!isok(x, y)) {
688	    return (struct monst *)0;	/* paranoia */
689	} else {
690	    mm.x = x;
691	    mm.y = y;
692	    if (MON_AT(mm.x, mm.y)) {
693		if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
694		    return (struct monst *)0;
695	    }
696	}
697	m2 = newmonst(0);
698	*m2 = *mon;			/* copy condition of old monster */
699	m2->nmon = fmon;
700	fmon = m2;
701	m2->m_id = flags.ident++;
702	if (!m2->m_id) m2->m_id = flags.ident++;	/* ident overflowed */
703	m2->mx = mm.x;
704	m2->my = mm.y;
705
706	m2->minvent = (struct obj *) 0; /* objects don't clone */
707	m2->mleashed = FALSE;
708#ifndef GOLDOBJ
709	m2->mgold = 0L;
710#endif
711	/* Max HP the same, but current HP halved for both.  The caller
712	 * might want to override this by halving the max HP also.
713	 * When current HP is odd, the original keeps the extra point.
714	 */
715	m2->mhpmax = mon->mhpmax;
716	m2->mhp = mon->mhp / 2;
717	mon->mhp -= m2->mhp;
718
719	/* since shopkeepers and guards will only be cloned if they've been
720	 * polymorphed away from their original forms, the clone doesn't have
721	 * room for the extra information.  we also don't want two shopkeepers
722	 * around for the same shop.
723	 */
724	if (mon->isshk) m2->isshk = FALSE;
725	if (mon->isgd) m2->isgd = FALSE;
726	if (mon->ispriest) m2->ispriest = FALSE;
727	m2->mxlth = 0;
728	place_monster(m2, m2->mx, m2->my);
729	if (emits_light(m2->data))
730	    new_light_source(m2->mx, m2->my, emits_light(m2->data),
731			     LS_MONSTER, (genericptr_t)m2);
732	if (m2->mnamelth) {
733	    m2->mnamelth = 0; /* or it won't get allocated */
734	    m2 = christen_monst(m2, NAME(mon));
735	} else if (mon->isshk) {
736	    m2 = christen_monst(m2, shkname(mon));
737	}
738
739	/* not all clones caused by player are tame or peaceful */
740	if (!flags.mon_moving) {
741	    if (mon->mtame)
742		m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
743	    else if (mon->mpeaceful)
744		m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
745	}
746
747	newsym(m2->mx,m2->my);	/* display the new monster */
748	if (m2->mtame) {
749	    struct monst *m3;
750
751	    if (mon->isminion) {
752		m3 = newmonst(sizeof(struct epri) + mon->mnamelth);
753		*m3 = *m2;
754		m3->mxlth = sizeof(struct epri);
755		if (m2->mnamelth) Strcpy(NAME(m3), NAME(m2));
756		*(EPRI(m3)) = *(EPRI(mon));
757		replmon(m2, m3);
758		m2 = m3;
759	    } else {
760		/* because m2 is a copy of mon it is tame but not init'ed.
761		 * however, tamedog will not re-tame a tame dog, so m2
762		 * must be made non-tame to get initialized properly.
763		 */
764		m2->mtame = 0;
765		if ((m3 = tamedog(m2, (struct obj *)0)) != 0) {
766		    m2 = m3;
767		    *(EDOG(m2)) = *(EDOG(mon));
768		}
769	    }
770	}
771	set_malign(m2);
772
773	return m2;
774}
775
776/*
777 * Propagate a species
778 *
779 * Once a certain number of monsters are created, don't create any more
780 * at random (i.e. make them extinct).  The previous (3.2) behavior was
781 * to do this when a certain number had _died_, which didn't make
782 * much sense.
783 *
784 * Returns FALSE propagation unsuccessful
785 *         TRUE  propagation successful
786 */
787boolean
788propagate(mndx, tally, ghostly)
789int mndx;
790boolean tally;
791boolean ghostly;
792{
793	boolean result;
794	uchar lim = mbirth_limit(mndx);
795	boolean gone = (mvitals[mndx].mvflags & G_GONE); /* genocided or extinct */
796
797	result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
798
799	/* if it's unique, don't ever make it again */
800	if (mons[mndx].geno & G_UNIQ) mvitals[mndx].mvflags |= G_EXTINCT;
801
802	if (mvitals[mndx].born < 255 && tally && (!ghostly || (ghostly && result)))
803		 mvitals[mndx].born++;
804	if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN) &&
805		!(mvitals[mndx].mvflags & G_EXTINCT)) {
806#if defined(DEBUG) && defined(WIZARD)
807		if (wizard) pline("Automatically extinguished %s.",
808					makeplural(mons[mndx].mname));
809#endif
810		mvitals[mndx].mvflags |= G_EXTINCT;
811		reset_rndmonst(mndx);
812	}
813	return result;
814}
815
816/*
817 * called with [x,y] = coordinates;
818 *	[0,0] means anyplace
819 *	[u.ux,u.uy] means: near player (if !in_mklev)
820 *
821 *	In case we make a monster group, only return the one at [x,y].
822 */
823struct monst *
824makemon(ptr, x, y, mmflags)
825register struct permonst *ptr;
826register int	x, y;
827register int	mmflags;
828{
829	register struct monst *mtmp;
830	int mndx, mcham, ct, mitem, xlth;
831	boolean anymon = (!ptr);
832	boolean byyou = (x == u.ux && y == u.uy);
833	boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
834	boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
835	unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
836
837	/* if caller wants random location, do it here */
838	if(x == 0 && y == 0) {
839		int tryct = 0;	/* careful with bigrooms */
840		struct monst fakemon;
841
842		fakemon.data = ptr;	/* set up for goodpos */
843		do {
844			x = rn1(COLNO-3,2);
845			y = rn2(ROWNO);
846		} while(!goodpos(x, y, ptr ? &fakemon : (struct monst *)0, gpflags) ||
847			(!in_mklev && tryct++ < 50 && cansee(x, y)));
848	} else if (byyou && !in_mklev) {
849		coord bypos;
850
851		if(enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
852			x = bypos.x;
853			y = bypos.y;
854		} else
855			return((struct monst *)0);
856	}
857
858	/* Does monster already exist at the position? */
859	if(MON_AT(x, y)) {
860		if ((mmflags & MM_ADJACENTOK) != 0) {
861			coord bypos;
862			if(enexto_core(&bypos, x, y, ptr, gpflags)) {
863				x = bypos.x;
864				y = bypos.y;
865			} else
866				return((struct monst *) 0);
867		} else
868			return((struct monst *) 0);
869	}
870
871	if(ptr){
872		mndx = monsndx(ptr);
873		/* if you are to make a specific monster and it has
874		   already been genocided, return */
875		if (mvitals[mndx].mvflags & G_GENOD) return((struct monst *) 0);
876#if defined(WIZARD) && defined(DEBUG)
877		if (wizard && (mvitals[mndx].mvflags & G_EXTINCT))
878		    pline("Explicitly creating extinct monster %s.",
879			mons[mndx].mname);
880#endif
881	} else {
882		/* make a random (common) monster that can survive here.
883		 * (the special levels ask for random monsters at specific
884		 * positions, causing mass drowning on the medusa level,
885		 * for instance.)
886		 */
887		int tryct = 0;	/* maybe there are no good choices */
888		struct monst fakemon;
889		do {
890			if(!(ptr = rndmonst())) {
891#ifdef NETHACK_DEBUG
892			    pline("Warning: no monster.");
893#endif
894			    return((struct monst *) 0);	/* no more monsters! */
895			}
896			fakemon.data = ptr;	/* set up for goodpos */
897		} while(!goodpos(x, y, &fakemon, gpflags) && tryct++ < 50);
898		mndx = monsndx(ptr);
899	}
900	(void) propagate(mndx, countbirth, FALSE);
901	xlth = ptr->pxlth;
902	if (mmflags & MM_EDOG) xlth += sizeof(struct edog);
903	else if (mmflags & MM_EMIN) xlth += sizeof(struct emin);
904	mtmp = newmonst(xlth);
905	*mtmp = zeromonst;		/* clear all entries in structure */
906	(void)memset((genericptr_t)mtmp->mextra, 0, xlth);
907	mtmp->nmon = fmon;
908	fmon = mtmp;
909	mtmp->m_id = flags.ident++;
910	if (!mtmp->m_id) mtmp->m_id = flags.ident++;	/* ident overflowed */
911	set_mon_data(mtmp, ptr, 0);
912	if (mtmp->data->msound == MS_LEADER)
913	    quest_status.leader_m_id = mtmp->m_id;
914	mtmp->mxlth = xlth;
915	mtmp->mnum = mndx;
916
917	mtmp->m_lev = adj_lev(ptr);
918	if (is_golem(ptr)) {
919	    mtmp->mhpmax = mtmp->mhp = golemhp(mndx);
920	} else if (is_rider(ptr)) {
921	    /* We want low HP, but a high mlevel so they can attack well */
922	    mtmp->mhpmax = mtmp->mhp = d(10,8);
923	} else if (ptr->mlevel > 49) {
924	    /* "special" fixed hp monster
925	     * the hit points are encoded in the mlevel in a somewhat strange
926	     * way to fit in the 50..127 positive range of a signed character
927	     * above the 1..49 that indicate "normal" monster levels */
928	    mtmp->mhpmax = mtmp->mhp = 2*(ptr->mlevel - 6);
929	    mtmp->m_lev = mtmp->mhp / 4;	/* approximation */
930	} else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
931	    /* adult dragons */
932	    mtmp->mhpmax = mtmp->mhp = (int) (In_endgame(&u.uz) ?
933		(8 * mtmp->m_lev) : (4 * mtmp->m_lev + d((int)mtmp->m_lev, 4)));
934	} else if (!mtmp->m_lev) {
935	    mtmp->mhpmax = mtmp->mhp = rnd(4);
936	} else {
937	    mtmp->mhpmax = mtmp->mhp = d((int)mtmp->m_lev, 8);
938	    if (is_home_elemental(ptr))
939		mtmp->mhpmax = (mtmp->mhp *= 3);
940	}
941
942	if (is_female(ptr)) mtmp->female = TRUE;
943	else if (is_male(ptr)) mtmp->female = FALSE;
944	else mtmp->female = rn2(2);	/* ignored for neuters */
945
946	if (In_sokoban(&u.uz) && !mindless(ptr))  /* know about traps here */
947	    mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
948	if (ptr->msound == MS_LEADER)		/* leader knows about portal */
949	    mtmp->mtrapseen |= (1L << (MAGIC_PORTAL-1));
950
951	place_monster(mtmp, x, y);
952	mtmp->mcansee = mtmp->mcanmove = TRUE;
953	mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
954
955	switch(ptr->mlet) {
956		case S_MIMIC:
957			set_mimic_sym(mtmp);
958			break;
959		case S_SPIDER:
960		case S_SNAKE:
961			if(in_mklev)
962			    if(x && y)
963				(void) mkobj_at(0, x, y, TRUE);
964			if(hides_under(ptr) && OBJ_AT(x, y))
965			    mtmp->mundetected = TRUE;
966			break;
967		case S_LIGHT:
968		case S_ELEMENTAL:
969			if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
970			    mtmp->perminvis = TRUE;
971			    mtmp->minvis = TRUE;
972			}
973			break;
974		case S_EEL:
975			if (is_pool(x, y))
976			    mtmp->mundetected = TRUE;
977			break;
978		case S_LEPRECHAUN:
979			mtmp->msleeping = 1;
980			break;
981		case S_JABBERWOCK:
982		case S_NYMPH:
983			if (rn2(5) && !u.uhave.amulet) mtmp->msleeping = 1;
984			break;
985		case S_ORC:
986			if (Race_if(PM_ELF)) mtmp->mpeaceful = FALSE;
987			break;
988		case S_UNICORN:
989			if (is_unicorn(ptr) &&
990					sgn(u.ualign.type) == sgn(ptr->maligntyp))
991				mtmp->mpeaceful = TRUE;
992			break;
993		case S_BAT:
994			if (Inhell && is_bat(ptr))
995			    mon_adjust_speed(mtmp, 2, (struct obj *)0);
996			break;
997	}
998	if ((ct = emits_light(mtmp->data)) > 0)
999		new_light_source(mtmp->mx, mtmp->my, ct,
1000				 LS_MONSTER, (genericptr_t)mtmp);
1001	mitem = 0;	/* extra inventory item for this monster */
1002
1003	if ((mcham = pm_to_cham(mndx)) != CHAM_ORDINARY) {
1004		/* If you're protected with a ring, don't create
1005		 * any shape-changing chameleons -dgk
1006		 */
1007		if (Protection_from_shape_changers)
1008			mtmp->cham = CHAM_ORDINARY;
1009		else {
1010			mtmp->cham = mcham;
1011			(void) newcham(mtmp, rndmonst(), FALSE, FALSE);
1012		}
1013	} else if (mndx == PM_WIZARD_OF_YENDOR) {
1014		mtmp->iswiz = TRUE;
1015		flags.no_of_wizards++;
1016		if (flags.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1017			mitem = SPE_DIG;
1018	} else if (mndx == PM_DJINNI) {
1019		flags.djinni_count++;
1020	} else if (mndx == PM_GHOST) {
1021		flags.ghost_count++;
1022		if (!(mmflags & MM_NONAME))
1023			mtmp = christen_monst(mtmp, rndghostname());
1024	} else if (mndx == PM_VLAD_THE_IMPALER) {
1025		mitem = CANDELABRUM_OF_INVOCATION;
1026	} else if (mndx == PM_CROESUS) {
1027		mitem = TWO_HANDED_SWORD;
1028	} else if (ptr->msound == MS_NEMESIS) {
1029		mitem = BELL_OF_OPENING;
1030	} else if (mndx == PM_PESTILENCE) {
1031		mitem = POT_SICKNESS;
1032	}
1033	if (mitem && allow_minvent) (void) mongets(mtmp, mitem);
1034
1035	if(in_mklev) {
1036		if(((is_ndemon(ptr)) ||
1037		    (mndx == PM_WUMPUS) ||
1038		    (mndx == PM_LONG_WORM) ||
1039		    (mndx == PM_GIANT_EEL)) && !u.uhave.amulet && rn2(5))
1040			mtmp->msleeping = TRUE;
1041	} else {
1042		if(byyou) {
1043			newsym(mtmp->mx,mtmp->my);
1044			set_apparxy(mtmp);
1045		}
1046	}
1047	if(is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1048	    mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1049	    mtmp->mavenge = 0;
1050	    if (uwep && uwep->oartifact == ART_EXCALIBUR)
1051		mtmp->mpeaceful = mtmp->mtame = FALSE;
1052	}
1053#ifndef DCC30_BUG
1054	if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1055#else
1056	/* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1057	 * same expression.
1058	 */
1059	if (mndx == PM_LONG_WORM &&
1060		(mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1061#endif
1062	{
1063	    /* we can now create worms with tails - 11/91 */
1064	    initworm(mtmp, rn2(5));
1065	    if (count_wsegs(mtmp)) place_worm_tail_randomly(mtmp, x, y);
1066	}
1067	set_malign(mtmp);		/* having finished peaceful changes */
1068	if(anymon) {
1069	    if ((ptr->geno & G_SGROUP) && rn2(2)) {
1070		m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1071	    } else if (ptr->geno & G_LGROUP) {
1072		if(rn2(3))  m_initlgrp(mtmp, mtmp->mx, mtmp->my);
1073		else	    m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1074	    }
1075	}
1076
1077	if (allow_minvent) {
1078	    if(is_armed(ptr))
1079		m_initweap(mtmp);	/* equip with weapons / armor */
1080	    m_initinv(mtmp);  /* add on a few special items incl. more armor */
1081	    m_dowear(mtmp, TRUE);
1082	} else {
1083	    /* no initial inventory is allowed */
1084	    if (mtmp->minvent) discard_minvent(mtmp);
1085	    mtmp->minvent = (struct obj *)0;    /* caller expects this */
1086	}
1087	if ((ptr->mflags3 & M3_WAITMASK) && !(mmflags & MM_NOWAIT)) {
1088		if (ptr->mflags3 & M3_WAITFORU)
1089			mtmp->mstrategy |= STRAT_WAITFORU;
1090		if (ptr->mflags3 & M3_CLOSE)
1091			mtmp->mstrategy |= STRAT_CLOSE;
1092	}
1093
1094	if (!in_mklev)
1095	    newsym(mtmp->mx,mtmp->my);	/* make sure the mon shows up */
1096
1097	return(mtmp);
1098}
1099
1100int
1101mbirth_limit(mndx)
1102int mndx;
1103{
1104	/* assert(MAXMONNO < 255); */
1105	return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1106}
1107
1108/* used for wand/scroll/spell of create monster */
1109/* returns TRUE iff you know monsters have been created */
1110boolean
1111create_critters(cnt, mptr)
1112int cnt;
1113struct permonst *mptr;		/* usually null; used for confused reading */
1114{
1115	coord c;
1116	int x, y;
1117	struct monst *mon;
1118	boolean known = FALSE;
1119#ifdef WIZARD
1120	boolean ask = wizard;
1121#endif
1122
1123	while (cnt--) {
1124#ifdef WIZARD
1125	    if (ask) {
1126		if (create_particular()) {
1127		    known = TRUE;
1128		    continue;
1129		}
1130		else ask = FALSE;	/* ESC will shut off prompting */
1131	    }
1132#endif
1133	    x = u.ux,  y = u.uy;
1134	    /* if in water, try to encourage an aquatic monster
1135	       by finding and then specifying another wet location */
1136	    if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1137		x = c.x,  y = c.y;
1138
1139	    mon = makemon(mptr, x, y, NO_MM_FLAGS);
1140	    if (mon && canspotmon(mon)) known = TRUE;
1141	}
1142	return known;
1143}
1144
1145#endif /* OVL1 */
1146#ifdef OVL0
1147
1148STATIC_OVL boolean
1149uncommon(mndx)
1150int mndx;
1151{
1152	if (mons[mndx].geno & (G_NOGEN | G_UNIQ)) return TRUE;
1153	if (mvitals[mndx].mvflags & G_GONE) return TRUE;
1154	if (Inhell)
1155		return(mons[mndx].maligntyp > A_NEUTRAL);
1156	else
1157		return((mons[mndx].geno & G_HELL) != 0);
1158}
1159
1160/*
1161 *	shift the probability of a monster's generation by
1162 *	comparing the dungeon alignment and monster alignment.
1163 *	return an integer in the range of 0-5.
1164 */
1165STATIC_OVL int
1166align_shift(ptr)
1167register struct permonst *ptr;
1168{
1169    static NEARDATA long oldmoves = 0L;	/* != 1, starting value of moves */
1170    static NEARDATA s_level *lev;
1171    register int alshift;
1172
1173    if(oldmoves != moves) {
1174	lev = Is_special(&u.uz);
1175	oldmoves = moves;
1176    }
1177    switch((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1178    default:	/* just in case */
1179    case AM_NONE:	alshift = 0;
1180			break;
1181    case AM_LAWFUL:	alshift = (ptr->maligntyp+20)/(2*ALIGNWEIGHT);
1182			break;
1183    case AM_NEUTRAL:	alshift = (20 - abs(ptr->maligntyp))/ALIGNWEIGHT;
1184			break;
1185    case AM_CHAOTIC:	alshift = (-(ptr->maligntyp-20))/(2*ALIGNWEIGHT);
1186			break;
1187    }
1188    return alshift;
1189}
1190
1191static NEARDATA struct {
1192	int choice_count;
1193	char mchoices[SPECIAL_PM];	/* value range is 0..127 */
1194} rndmonst_state = { -1, {0} };
1195
1196/* select a random monster type */
1197struct permonst *
1198rndmonst()
1199{
1200	register struct permonst *ptr;
1201	register int mndx, ct;
1202
1203	if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1204	    return ptr;
1205
1206	if (rndmonst_state.choice_count < 0) {	/* need to recalculate */
1207	    int zlevel, minmlev, maxmlev;
1208	    boolean elemlevel;
1209#ifdef REINCARNATION
1210	    boolean upper;
1211#endif
1212
1213	    rndmonst_state.choice_count = 0;
1214	    /* look for first common monster */
1215	    for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1216		if (!uncommon(mndx)) break;
1217		rndmonst_state.mchoices[mndx] = 0;
1218	    }
1219	    if (mndx == SPECIAL_PM) {
1220		/* evidently they've all been exterminated */
1221#ifdef NETHACK_DEBUG
1222		pline("rndmonst: no common mons!");
1223#endif
1224		return (struct permonst *)0;
1225	    } /* else `mndx' now ready for use below */
1226	    zlevel = level_difficulty();
1227	    /* determine the level of the weakest monster to make. */
1228	    minmlev = zlevel / 6;
1229	    /* determine the level of the strongest monster to make. */
1230	    maxmlev = (zlevel + u.ulevel) / 2;
1231#ifdef REINCARNATION
1232	    upper = Is_rogue_level(&u.uz);
1233#endif
1234	    elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1235
1236/*
1237 *	Find out how many monsters exist in the range we have selected.
1238 */
1239	    /* (`mndx' initialized above) */
1240	    for ( ; mndx < SPECIAL_PM; mndx++) {
1241		ptr = &mons[mndx];
1242		rndmonst_state.mchoices[mndx] = 0;
1243		if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1244		    continue;
1245#ifdef REINCARNATION
1246		if (upper && !isupper(def_monsyms[(int)(ptr->mlet)])) continue;
1247#endif
1248		if (elemlevel && wrong_elem_type(ptr)) continue;
1249		if (uncommon(mndx)) continue;
1250		if (Inhell && (ptr->geno & G_NOHELL)) continue;
1251		ct = (int)(ptr->geno & G_FREQ) + align_shift(ptr);
1252		if (ct < 0 || ct > 127)
1253		    panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1254		rndmonst_state.choice_count += ct;
1255		rndmonst_state.mchoices[mndx] = (char)ct;
1256	    }
1257/*
1258 *	    Possible modification:  if choice_count is "too low",
1259 *	    expand minmlev..maxmlev range and try again.
1260 */
1261	} /* choice_count+mchoices[] recalc */
1262
1263	if (rndmonst_state.choice_count <= 0) {
1264	    /* maybe no common mons left, or all are too weak or too strong */
1265#ifdef NETHACK_DEBUG
1266	    Norep("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1267#endif
1268	    return (struct permonst *)0;
1269	}
1270
1271/*
1272 *	Now, select a monster at random.
1273 */
1274	ct = rnd(rndmonst_state.choice_count);
1275	for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1276	    if ((ct -= (int)rndmonst_state.mchoices[mndx]) <= 0) break;
1277
1278	if (mndx == SPECIAL_PM || uncommon(mndx)) {	/* shouldn't happen */
1279	    impossible("rndmonst: bad `mndx' [#%d]", mndx);
1280	    return (struct permonst *)0;
1281	}
1282	return &mons[mndx];
1283}
1284
1285/* called when you change level (experience or dungeon depth) or when
1286   monster species can no longer be created (genocide or extinction) */
1287void
1288reset_rndmonst(mndx)
1289int mndx;	/* particular species that can no longer be created */
1290{
1291	/* cached selection info is out of date */
1292	if (mndx == NON_PM) {
1293	    rndmonst_state.choice_count = -1;	/* full recalc needed */
1294	} else if (mndx < SPECIAL_PM) {
1295	    rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1296	    rndmonst_state.mchoices[mndx] = 0;
1297	} /* note: safe to ignore extinction of unique monsters */
1298}
1299
1300#endif /* OVL0 */
1301#ifdef OVL1
1302
1303/*	The routine below is used to make one of the multiple types
1304 *	of a given monster class.  The second parameter specifies a
1305 *	special casing bit mask to allow the normal genesis
1306 *	masks to be deactivated.  Returns 0 if no monsters
1307 *	in that class can be made.
1308 */
1309
1310struct permonst *
1311mkclass(class,spc)
1312char	class;
1313int	spc;
1314{
1315	register int	first, last, num = 0;
1316	int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1317
1318	maxmlev = level_difficulty() >> 1;
1319	if(class < 1 || class >= MAXMCLASSES) {
1320	    impossible("mkclass called with bad class!");
1321	    return((struct permonst *) 0);
1322	}
1323/*	Assumption #1:	monsters of a given class are contiguous in the
1324 *			mons[] array.
1325 */
1326	for (first = LOW_PM; first < SPECIAL_PM; first++)
1327	    if (mons[first].mlet == class) break;
1328	if (first == SPECIAL_PM) return (struct permonst *) 0;
1329
1330	for (last = first;
1331		last < SPECIAL_PM && mons[last].mlet == class; last++)
1332	    if (!(mvitals[last].mvflags & G_GONE) && !(mons[last].geno & mask)
1333					&& !is_placeholder(&mons[last])) {
1334		/* consider it */
1335		if(num && toostrong(last, maxmlev) &&
1336		   monstr[last] != monstr[last-1] && rn2(2)) break;
1337		num += mons[last].geno & G_FREQ;
1338	    }
1339
1340	if(!num) return((struct permonst *) 0);
1341
1342/*	Assumption #2:	monsters of a given class are presented in ascending
1343 *			order of strength.
1344 */
1345	for(num = rnd(num); num > 0; first++)
1346	    if (!(mvitals[first].mvflags & G_GONE) && !(mons[first].geno & mask)
1347					&& !is_placeholder(&mons[first])) {
1348		/* skew towards lower value monsters at lower exp. levels */
1349		num -= mons[first].geno & G_FREQ;
1350		if (num && adj_lev(&mons[first]) > (u.ulevel*2)) {
1351		    /* but not when multiple monsters are same level */
1352		    if (mons[first].mlevel != mons[first+1].mlevel)
1353			num--;
1354		}
1355	    }
1356	first--; /* correct an off-by-one error */
1357
1358	return(&mons[first]);
1359}
1360
1361int
1362adj_lev(ptr)	/* adjust strength of monsters based on u.uz and u.ulevel */
1363register struct permonst *ptr;
1364{
1365	int	tmp, tmp2;
1366
1367	if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1368		/* does not depend on other strengths, but does get stronger
1369		 * every time he is killed
1370		 */
1371		tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1372		if (tmp > 49) tmp = 49;
1373		return tmp;
1374	}
1375
1376	if((tmp = ptr->mlevel) > 49) return(50); /* "special" demons/devils */
1377	tmp2 = (level_difficulty() - tmp);
1378	if(tmp2 < 0) tmp--;		/* if mlevel > u.uz decrement tmp */
1379	else tmp += (tmp2 / 5);		/* else increment 1 per five diff */
1380
1381	tmp2 = (u.ulevel - ptr->mlevel);	/* adjust vs. the player */
1382	if(tmp2 > 0) tmp += (tmp2 / 4);		/* level as well */
1383
1384	tmp2 = (3 * ((int) ptr->mlevel))/ 2;	/* crude upper limit */
1385	if (tmp2 > 49) tmp2 = 49;		/* hard upper limit */
1386	return((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1387}
1388
1389#endif /* OVL1 */
1390#ifdef OVLB
1391
1392struct permonst *
1393grow_up(mtmp, victim)	/* `mtmp' might "grow up" into a bigger version */
1394struct monst *mtmp, *victim;
1395{
1396	int oldtype, newtype, max_increase, cur_increase,
1397	    lev_limit, hp_threshold;
1398	struct permonst *ptr = mtmp->data;
1399
1400	/* monster died after killing enemy but before calling this function */
1401	/* currently possible if killing a gas spore */
1402	if (mtmp->mhp <= 0)
1403	    return ((struct permonst *)0);
1404
1405	/* note:  none of the monsters with special hit point calculations
1406	   have both little and big forms */
1407	oldtype = monsndx(ptr);
1408	newtype = little_to_big(oldtype);
1409	if (newtype == PM_PRIEST && mtmp->female) newtype = PM_PRIESTESS;
1410
1411	/* growth limits differ depending on method of advancement */
1412	if (victim) {		/* killed a monster */
1413	    /*
1414	     * The HP threshold is the maximum number of hit points for the
1415	     * current level; once exceeded, a level will be gained.
1416	     * Possible bug: if somehow the hit points are already higher
1417	     * than that, monster will gain a level without any increase in HP.
1418	     */
1419	    hp_threshold = mtmp->m_lev * 8;		/* normal limit */
1420	    if (!mtmp->m_lev)
1421		hp_threshold = 4;
1422	    else if (is_golem(ptr))	/* strange creatures */
1423		hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1424	    else if (is_home_elemental(ptr))
1425		hp_threshold *= 3;
1426	    lev_limit = 3 * (int)ptr->mlevel / 2;	/* same as adj_lev() */
1427	    /* If they can grow up, be sure the level is high enough for that */
1428	    if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1429		lev_limit = (int)mons[newtype].mlevel;
1430	    /* number of hit points to gain; unlike for the player, we put
1431	       the limit at the bottom of the next level rather than the top */
1432	    max_increase = rnd((int)victim->m_lev + 1);
1433	    if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1434		max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1435	    cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1436	} else {
1437	    /* a gain level potion or wraith corpse; always go up a level
1438	       unless already at maximum (49 is hard upper limit except
1439	       for demon lords, who start at 50 and can't go any higher) */
1440	    max_increase = cur_increase = rnd(8);
1441	    hp_threshold = 0;	/* smaller than `mhpmax + max_increase' */
1442	    lev_limit = 50;		/* recalc below */
1443	}
1444
1445	mtmp->mhpmax += max_increase;
1446	mtmp->mhp += cur_increase;
1447	if (mtmp->mhpmax <= hp_threshold)
1448	    return ptr;		/* doesn't gain a level */
1449
1450	if (is_mplayer(ptr)) lev_limit = 30;	/* same as player */
1451	else if (lev_limit < 5) lev_limit = 5;	/* arbitrary */
1452	else if (lev_limit > 49) lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1453
1454	if ((int)++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1455	    ptr = &mons[newtype];
1456	    if (mvitals[newtype].mvflags & G_GENOD) {	/* allow G_EXTINCT */
1457		if (sensemon(mtmp))
1458		    pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1459			an(ptr->mname), mhe(mtmp),
1460			nonliving(ptr) ? "expires" : "dies");
1461		set_mon_data(mtmp, ptr, -1);	/* keep mvitals[] accurate */
1462		mondied(mtmp);
1463		return (struct permonst *)0;
1464	    }
1465	    set_mon_data(mtmp, ptr, 1);		/* preserve intrinsics */
1466	    newsym(mtmp->mx, mtmp->my);		/* color may change */
1467	    lev_limit = (int)mtmp->m_lev;	/* never undo increment */
1468	}
1469	/* sanity checks */
1470	if ((int)mtmp->m_lev > lev_limit) {
1471	    mtmp->m_lev--;	/* undo increment */
1472	    /* HP might have been allowed to grow when it shouldn't */
1473	    if (mtmp->mhpmax == hp_threshold + 1) mtmp->mhpmax--;
1474	}
1475	if (mtmp->mhpmax > 50*8) mtmp->mhpmax = 50*8;	  /* absolute limit */
1476	if (mtmp->mhp > mtmp->mhpmax) mtmp->mhp = mtmp->mhpmax;
1477
1478	return ptr;
1479}
1480
1481#endif /* OVLB */
1482#ifdef OVL1
1483
1484int
1485mongets(mtmp, otyp)
1486register struct monst *mtmp;
1487register int otyp;
1488{
1489	register struct obj *otmp;
1490	int spe;
1491
1492	if (!otyp) return 0;
1493	otmp = mksobj(otyp, TRUE, FALSE);
1494	if (otmp) {
1495	    if (mtmp->data->mlet == S_DEMON) {
1496		/* demons never get blessed objects */
1497		if (otmp->blessed) curse(otmp);
1498	    } else if(is_lminion(mtmp)) {
1499		/* lawful minions don't get cursed, bad, or rusting objects */
1500		otmp->cursed = FALSE;
1501		if(otmp->spe < 0) otmp->spe = 0;
1502		otmp->oerodeproof = TRUE;
1503	    } else if(is_mplayer(mtmp->data) && is_sword(otmp)) {
1504		otmp->spe = (3 + rn2(4));
1505	    }
1506
1507	    if(otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1508		otmp->spe = 0;
1509		otmp->age = 0L;
1510		otmp->lamplit = FALSE;
1511		otmp->blessed = otmp->cursed = FALSE;
1512	    } else if (otmp->otyp == BELL_OF_OPENING) {
1513		otmp->blessed = otmp->cursed = FALSE;
1514	    } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1515		otmp->blessed = FALSE;
1516		otmp->cursed = TRUE;
1517	    }
1518
1519	    /* leaders don't tolerate inferior quality battle gear */
1520	    if (is_prince(mtmp->data)) {
1521		if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1522		    otmp->spe = 1;
1523		else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1524		    otmp->spe = 0;
1525	    }
1526
1527	    spe = otmp->spe;
1528	    (void) mpickobj(mtmp, otmp);	/* might free otmp */
1529	    return(spe);
1530	} else return(0);
1531}
1532
1533#endif /* OVL1 */
1534#ifdef OVLB
1535
1536int
1537golemhp(type)
1538int type;
1539{
1540	switch(type) {
1541		case PM_STRAW_GOLEM: return 20;
1542		case PM_PAPER_GOLEM: return 20;
1543		case PM_ROPE_GOLEM: return 30;
1544		case PM_LEATHER_GOLEM: return 40;
1545		case PM_GOLD_GOLEM: return 40;
1546		case PM_WOOD_GOLEM: return 50;
1547		case PM_FLESH_GOLEM: return 40;
1548		case PM_CLAY_GOLEM: return 50;
1549		case PM_STONE_GOLEM: return 60;
1550		case PM_GLASS_GOLEM: return 60;
1551		case PM_IRON_GOLEM: return 80;
1552		default: return 0;
1553	}
1554}
1555
1556#endif /* OVLB */
1557#ifdef OVL1
1558
1559/*
1560 *	Alignment vs. yours determines monster's attitude to you.
1561 *	( some "animal" types are co-aligned, but also hungry )
1562 */
1563boolean
1564peace_minded(ptr)
1565register struct permonst *ptr;
1566{
1567	aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
1568
1569	if (always_peaceful(ptr)) return TRUE;
1570	if (always_hostile(ptr)) return FALSE;
1571	if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
1572		return TRUE;
1573	if (ptr->msound == MS_NEMESIS)	return FALSE;
1574
1575	if (race_peaceful(ptr)) return TRUE;
1576	if (race_hostile(ptr)) return FALSE;
1577
1578	/* the monster is hostile if its alignment is different from the
1579	 * player's */
1580	if (sgn(mal) != sgn(ual)) return FALSE;
1581
1582	/* Negative monster hostile to player with Amulet. */
1583	if (mal < A_NEUTRAL && u.uhave.amulet) return FALSE;
1584
1585	/* minions are hostile to players that have strayed at all */
1586	if (is_minion(ptr)) return((boolean)(u.ualign.record >= 0));
1587
1588	/* Last case:  a chance of a co-aligned monster being
1589	 * hostile.  This chance is greater if the player has strayed
1590	 * (u.ualign.record negative) or the monster is not strongly aligned.
1591	 */
1592	return((boolean)(!!rn2(16 + (u.ualign.record < -15 ? -15 : u.ualign.record)) &&
1593		!!rn2(2 + abs(mal))));
1594}
1595
1596/* Set malign to have the proper effect on player alignment if monster is
1597 * killed.  Negative numbers mean it's bad to kill this monster; positive
1598 * numbers mean it's good.  Since there are more hostile monsters than
1599 * peaceful monsters, the penalty for killing a peaceful monster should be
1600 * greater than the bonus for killing a hostile monster to maintain balance.
1601 * Rules:
1602 *   it's bad to kill peaceful monsters, potentially worse to kill always-
1603 *	peaceful monsters
1604 *   it's never bad to kill a hostile monster, although it may not be good
1605 */
1606void
1607set_malign(mtmp)
1608struct monst *mtmp;
1609{
1610	schar mal = mtmp->data->maligntyp;
1611	boolean coaligned;
1612
1613	if (mtmp->ispriest || mtmp->isminion) {
1614		/* some monsters have individual alignments; check them */
1615		if (mtmp->ispriest)
1616			mal = EPRI(mtmp)->shralign;
1617		else if (mtmp->isminion)
1618			mal = EMIN(mtmp)->min_align;
1619		/* unless alignment is none, set mal to -5,0,5 */
1620		/* (see align.h for valid aligntyp values)     */
1621		if(mal != A_NONE)
1622			mal *= 5;
1623	}
1624
1625	coaligned = (sgn(mal) == sgn(u.ualign.type));
1626	if (mtmp->data->msound == MS_LEADER) {
1627		mtmp->malign = -20;
1628	} else if (mal == A_NONE) {
1629		if (mtmp->mpeaceful)
1630			mtmp->malign = 0;
1631		else
1632			mtmp->malign = 20;	/* really hostile */
1633	} else if (always_peaceful(mtmp->data)) {
1634		int absmal = abs(mal);
1635		if (mtmp->mpeaceful)
1636			mtmp->malign = -3*max(5,absmal);
1637		else
1638			mtmp->malign = 3*max(5,absmal); /* renegade */
1639	} else if (always_hostile(mtmp->data)) {
1640		int absmal = abs(mal);
1641		if (coaligned)
1642			mtmp->malign = 0;
1643		else
1644			mtmp->malign = max(5,absmal);
1645	} else if (coaligned) {
1646		int absmal = abs(mal);
1647		if (mtmp->mpeaceful)
1648			mtmp->malign = -3*max(3,absmal);
1649		else	/* renegade */
1650			mtmp->malign = max(3,absmal);
1651	} else	/* not coaligned and therefore hostile */
1652		mtmp->malign = abs(mal);
1653}
1654
1655#endif /* OVL1 */
1656#ifdef OVLB
1657
1658static NEARDATA char syms[] = {
1659	MAXOCLASSES, MAXOCLASSES+1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
1660	FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
1661	AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
1662	S_MIMIC_DEF, S_MIMIC_DEF, S_MIMIC_DEF,
1663};
1664
1665void
1666set_mimic_sym(mtmp)		/* KAA, modified by ERS */
1667register struct monst *mtmp;
1668{
1669	int typ, roomno, rt;
1670	unsigned appear, ap_type;
1671	int s_sym;
1672	struct obj *otmp;
1673	int mx, my;
1674
1675	if (!mtmp) return;
1676	mx = mtmp->mx; my = mtmp->my;
1677	typ = levl[mx][my].typ;
1678					/* only valid for INSIDE of room */
1679	roomno = levl[mx][my].roomno - ROOMOFFSET;
1680	if (roomno >= 0)
1681		rt = rooms[roomno].rtype;
1682#ifdef SPECIALIZATION
1683	else if (IS_ROOM(typ))
1684		rt = OROOM,  roomno = 0;
1685#endif
1686	else	rt = 0;	/* roomno < 0 case for GCC_WARN */
1687
1688	if (OBJ_AT(mx, my)) {
1689		ap_type = M_AP_OBJECT;
1690		appear = level.objects[mx][my]->otyp;
1691	} else if (IS_DOOR(typ) || IS_WALL(typ) ||
1692		   typ == SDOOR || typ == SCORR) {
1693		ap_type = M_AP_FURNITURE;
1694		/*
1695		 *  If there is a wall to the left that connects to this
1696		 *  location, then the mimic mimics a horizontal closed door.
1697		 *  This does not allow doors to be in corners of rooms.
1698		 */
1699		if (mx != 0 &&
1700			(levl[mx-1][my].typ == HWALL    ||
1701			 levl[mx-1][my].typ == TLCORNER ||
1702			 levl[mx-1][my].typ == TRWALL   ||
1703			 levl[mx-1][my].typ == BLCORNER ||
1704			 levl[mx-1][my].typ == TDWALL   ||
1705			 levl[mx-1][my].typ == CROSSWALL||
1706			 levl[mx-1][my].typ == TUWALL    ))
1707		    appear = S_hcdoor;
1708		else
1709		    appear = S_vcdoor;
1710
1711		if(!mtmp->minvis || See_invisible)
1712		    block_point(mx,my);	/* vision */
1713	} else if (level.flags.is_maze_lev && rn2(2)) {
1714		ap_type = M_AP_OBJECT;
1715		appear = STATUE;
1716	} else if (roomno < 0) {
1717		ap_type = M_AP_OBJECT;
1718		appear = BOULDER;
1719		if(!mtmp->minvis || See_invisible)
1720		    block_point(mx,my);	/* vision */
1721	} else if (rt == ZOO || rt == VAULT) {
1722		ap_type = M_AP_OBJECT;
1723		appear = GOLD_PIECE;
1724	} else if (rt == DELPHI) {
1725		if (rn2(2)) {
1726			ap_type = M_AP_OBJECT;
1727			appear = STATUE;
1728		} else {
1729			ap_type = M_AP_FURNITURE;
1730			appear = S_fountain;
1731		}
1732	} else if (rt == TEMPLE) {
1733		ap_type = M_AP_FURNITURE;
1734		appear = S_altar;
1735	/*
1736	 * We won't bother with beehives, morgues, barracks, throne rooms
1737	 * since they shouldn't contain too many mimics anyway...
1738	 */
1739	} else if (rt >= SHOPBASE) {
1740		s_sym = get_shop_item(rt - SHOPBASE);
1741		if (s_sym < 0) {
1742			ap_type = M_AP_OBJECT;
1743			appear = -s_sym;
1744		} else {
1745			if (s_sym == RANDOM_CLASS)
1746				s_sym = syms[rn2((int)sizeof(syms)-2) + 2];
1747			goto assign_sym;
1748		}
1749	} else {
1750		s_sym = syms[rn2((int)sizeof(syms))];
1751assign_sym:
1752		if (s_sym >= MAXOCLASSES) {
1753			ap_type = M_AP_FURNITURE;
1754			appear = s_sym == MAXOCLASSES ? S_upstair : S_dnstair;
1755		} else if (s_sym == COIN_CLASS) {
1756			ap_type = M_AP_OBJECT;
1757			appear = GOLD_PIECE;
1758		} else {
1759			ap_type = M_AP_OBJECT;
1760			if (s_sym == S_MIMIC_DEF) {
1761				appear = STRANGE_OBJECT;
1762			} else {
1763				otmp = mkobj( (char) s_sym, FALSE );
1764				appear = otmp->otyp;
1765				/* make sure container contents are free'ed */
1766				obfree(otmp, (struct obj *) 0);
1767			}
1768		}
1769	}
1770	mtmp->m_ap_type = ap_type;
1771	mtmp->mappearance = appear;
1772}
1773
1774/* release a monster from a bag of tricks */
1775void
1776bagotricks(bag)
1777struct obj *bag;
1778{
1779    if (!bag || bag->otyp != BAG_OF_TRICKS) {
1780	impossible("bad bag o' tricks");
1781    } else if (bag->spe < 1) {
1782	pline(nothing_happens);
1783    } else {
1784	boolean gotone = FALSE;
1785	int cnt = 1;
1786
1787	consume_obj_charge(bag, TRUE);
1788
1789	if (!rn2(23)) cnt += rn1(7, 1);
1790	while (cnt-- > 0) {
1791	    if (makemon((struct permonst *)0, u.ux, u.uy, NO_MM_FLAGS))
1792		gotone = TRUE;
1793	}
1794	if (gotone) makeknown(BAG_OF_TRICKS);
1795    }
1796}
1797
1798#endif /* OVLB */
1799
1800/*makemon.c*/
1801