Wikia

Wikihack

Source:NetHack 3.2.0/makemon.c

2,032pages on
this wiki
Talk0

Below is the full text to makemon.c from the source code of NetHack 3.2.0. To link to a particular line, write [[NetHack 3.2.0/makemon.c#line123]], for example.

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

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

Around Wikia's network

Random Wiki