xref: /openbsd/games/hack/hack.makemon.c (revision df930be7)
1 /*
2  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
3  */
4 
5 #ifndef lint
6 static char rcsid[] = "$NetBSD: hack.makemon.c,v 1.3 1995/03/23 08:30:38 cgd Exp $";
7 #endif /* not lint */
8 
9 #include	"hack.h"
10 extern char fut_geno[];
11 extern char *index();
12 extern struct obj *mkobj_at();
13 struct monst zeromonst;
14 
15 /*
16  * called with [x,y] = coordinates;
17  *	[0,0] means anyplace
18  *	[u.ux,u.uy] means: call mnexto (if !in_mklev)
19  *
20  *	In case we make an Orc or killer bee, we make an entire horde (swarm);
21  *	note that in this case we return only one of them (the one at [x,y]).
22  */
23 struct monst *
24 makemon(ptr,x,y)
25 register struct permonst *ptr;
26 {
27 	register struct monst *mtmp;
28 	register tmp, ct;
29 	boolean anything = (!ptr);
30 	extern boolean in_mklev;
31 
32 	if(x != 0 || y != 0) if(m_at(x,y)) return((struct monst *) 0);
33 	if(ptr){
34 		if(index(fut_geno, ptr->mlet)) return((struct monst *) 0);
35 	} else {
36 		ct = CMNUM - strlen(fut_geno);
37 		if(index(fut_geno, 'm')) ct++;  /* make only 1 minotaur */
38 		if(index(fut_geno, '@')) ct++;
39 		if(ct <= 0) return(0); 		  /* no more monsters! */
40 		tmp = rn2(ct*dlevel/24 + 7);
41 		if(tmp < dlevel - 4) tmp = rn2(ct*dlevel/24 + 12);
42 		if(tmp >= ct) tmp = rn1(ct - ct/2, ct/2);
43 		for(ct = 0; ct < CMNUM; ct++){
44 			ptr = &mons[ct];
45 			if(index(fut_geno, ptr->mlet))
46 				continue;
47 			if(!tmp--) goto gotmon;
48 		}
49 		panic("makemon?");
50 	}
51 gotmon:
52 	mtmp = newmonst(ptr->pxlth);
53 	*mtmp = zeromonst;	/* clear all entries in structure */
54 	for(ct = 0; ct < ptr->pxlth; ct++)
55 		((char *) &(mtmp->mextra[0]))[ct] = 0;
56 	mtmp->nmon = fmon;
57 	fmon = mtmp;
58 	mtmp->m_id = flags.ident++;
59 	mtmp->data = ptr;
60 	mtmp->mxlth = ptr->pxlth;
61 	if(ptr->mlet == 'D') mtmp->mhpmax = mtmp->mhp = 80;
62 	else if(!ptr->mlevel) mtmp->mhpmax = mtmp->mhp = rnd(4);
63 	else mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
64 	mtmp->mx = x;
65 	mtmp->my = y;
66 	mtmp->mcansee = 1;
67 	if(ptr->mlet == 'M'){
68 		mtmp->mimic = 1;
69 		mtmp->mappearance = ']';
70 	}
71 	if(!in_mklev) {
72 		if(x == u.ux && y == u.uy && ptr->mlet != ' ')
73 			mnexto(mtmp);
74 		if(x == 0 && y == 0)
75 			rloc(mtmp);
76 	}
77 	if(ptr->mlet == 's' || ptr->mlet == 'S') {
78 		mtmp->mhide = mtmp->mundetected = 1;
79 		if(in_mklev)
80 		if(mtmp->mx && mtmp->my)
81 			(void) mkobj_at(0, mtmp->mx, mtmp->my);
82 	}
83 	if(ptr->mlet == ':') {
84 		mtmp->cham = 1;
85 		(void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
86 	}
87 	if(ptr->mlet == 'I' || ptr->mlet == ';')
88 		mtmp->minvis = 1;
89 	if(ptr->mlet == 'L' || ptr->mlet == 'N'
90 	    || (in_mklev && index("&w;", ptr->mlet) && rn2(5))
91 	) mtmp->msleep = 1;
92 
93 #ifndef NOWORM
94 	if(ptr->mlet == 'w' && getwn(mtmp))
95 		initworm(mtmp);
96 #endif NOWORM
97 
98 	if(anything) if(ptr->mlet == 'O' || ptr->mlet == 'k') {
99 		coord enexto();
100 		coord mm;
101 		register int cnt = rnd(10);
102 		mm.x = x;
103 		mm.y = y;
104 		while(cnt--) {
105 			mm = enexto(mm.x, mm.y);
106 			(void) makemon(ptr, mm.x, mm.y);
107 		}
108 	}
109 
110 	return(mtmp);
111 }
112 
113 coord
114 enexto(xx,yy)
115 register xchar xx,yy;
116 {
117 	register xchar x,y;
118 	coord foo[15], *tfoo;
119 	int range;
120 
121 	tfoo = foo;
122 	range = 1;
123 	do {	/* full kludge action. */
124 		for(x = xx-range; x <= xx+range; x++)
125 			if(goodpos(x, yy-range)) {
126 				tfoo->x = x;
127 				tfoo++->y = yy-range;
128 				if(tfoo == &foo[15]) goto foofull;
129 			}
130 		for(x = xx-range; x <= xx+range; x++)
131 			if(goodpos(x,yy+range)) {
132 				tfoo->x = x;
133 				tfoo++->y = yy+range;
134 				if(tfoo == &foo[15]) goto foofull;
135 			}
136 		for(y = yy+1-range; y < yy+range; y++)
137 			if(goodpos(xx-range,y)) {
138 				tfoo->x = xx-range;
139 				tfoo++->y = y;
140 				if(tfoo == &foo[15]) goto foofull;
141 			}
142 		for(y = yy+1-range; y < yy+range; y++)
143 			if(goodpos(xx+range,y)) {
144 				tfoo->x = xx+range;
145 				tfoo++->y = y;
146 				if(tfoo == &foo[15]) goto foofull;
147 			}
148 		range++;
149 	} while(tfoo == foo);
150 foofull:
151 	return( foo[rn2(tfoo-foo)] );
152 }
153 
154 goodpos(x,y)	/* used only in mnexto and rloc */
155 {
156 	return(
157 	! (x < 1 || x > COLNO-2 || y < 1 || y > ROWNO-2 ||
158 	   m_at(x,y) || !ACCESSIBLE(levl[x][y].typ)
159 	   || (x == u.ux && y == u.uy)
160 	   || sobj_at(ENORMOUS_ROCK, x, y)
161 	));
162 }
163 
164 rloc(mtmp)
165 struct monst *mtmp;
166 {
167 	register tx,ty;
168 	register char ch = mtmp->data->mlet;
169 
170 #ifndef NOWORM
171 	if(ch == 'w' && mtmp->mx) return;	/* do not relocate worms */
172 #endif NOWORM
173 	do {
174 		tx = rn1(COLNO-3,2);
175 		ty = rn2(ROWNO);
176 	} while(!goodpos(tx,ty));
177 	mtmp->mx = tx;
178 	mtmp->my = ty;
179 	if(u.ustuck == mtmp){
180 		if(u.uswallow) {
181 			u.ux = tx;
182 			u.uy = ty;
183 			docrt();
184 		} else	u.ustuck = 0;
185 	}
186 	pmon(mtmp);
187 }
188 
189 struct monst *
190 mkmon_at(let,x,y)
191 char let;
192 register int x,y;
193 {
194 	register int ct;
195 	register struct permonst *ptr;
196 
197 	for(ct = 0; ct < CMNUM; ct++) {
198 		ptr = &mons[ct];
199 		if(ptr->mlet == let)
200 			return(makemon(ptr,x,y));
201 	}
202 	return(0);
203 }
204