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