hack.makemon.c revision 1.7 1 1.7 dholland /* $NetBSD: hack.makemon.c,v 1.7 2008/01/28 06:55:41 dholland Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.6 jsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 1.6 jsm * Amsterdam
6 1.6 jsm * All rights reserved.
7 1.6 jsm *
8 1.6 jsm * Redistribution and use in source and binary forms, with or without
9 1.6 jsm * modification, are permitted provided that the following conditions are
10 1.6 jsm * met:
11 1.6 jsm *
12 1.6 jsm * - Redistributions of source code must retain the above copyright notice,
13 1.6 jsm * this list of conditions and the following disclaimer.
14 1.6 jsm *
15 1.6 jsm * - Redistributions in binary form must reproduce the above copyright
16 1.6 jsm * notice, this list of conditions and the following disclaimer in the
17 1.6 jsm * documentation and/or other materials provided with the distribution.
18 1.6 jsm *
19 1.6 jsm * - Neither the name of the Stichting Centrum voor Wiskunde en
20 1.6 jsm * Informatica, nor the names of its contributors may be used to endorse or
21 1.6 jsm * promote products derived from this software without specific prior
22 1.6 jsm * written permission.
23 1.6 jsm *
24 1.6 jsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 1.6 jsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.6 jsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 1.6 jsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 1.6 jsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 1.6 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 1.6 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 1.6 jsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 1.6 jsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 1.6 jsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 1.6 jsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.6 jsm */
36 1.6 jsm
37 1.6 jsm /*
38 1.6 jsm * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 1.6 jsm * All rights reserved.
40 1.6 jsm *
41 1.6 jsm * Redistribution and use in source and binary forms, with or without
42 1.6 jsm * modification, are permitted provided that the following conditions
43 1.6 jsm * are met:
44 1.6 jsm * 1. Redistributions of source code must retain the above copyright
45 1.6 jsm * notice, this list of conditions and the following disclaimer.
46 1.6 jsm * 2. Redistributions in binary form must reproduce the above copyright
47 1.6 jsm * notice, this list of conditions and the following disclaimer in the
48 1.6 jsm * documentation and/or other materials provided with the distribution.
49 1.6 jsm * 3. The name of the author may not be used to endorse or promote products
50 1.6 jsm * derived from this software without specific prior written permission.
51 1.6 jsm *
52 1.6 jsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 1.6 jsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 1.6 jsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 1.6 jsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 1.6 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 1.6 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 1.6 jsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 1.6 jsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 1.6 jsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 1.6 jsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.2 mycroft */
63 1.2 mycroft
64 1.4 christos #include <sys/cdefs.h>
65 1.2 mycroft #ifndef lint
66 1.7 dholland __RCSID("$NetBSD: hack.makemon.c,v 1.7 2008/01/28 06:55:41 dholland Exp $");
67 1.4 christos #endif /* not lint */
68 1.1 cgd
69 1.1 cgd #include "hack.h"
70 1.4 christos #include "extern.h"
71 1.4 christos
72 1.1 cgd struct monst zeromonst;
73 1.1 cgd
74 1.1 cgd /*
75 1.1 cgd * called with [x,y] = coordinates;
76 1.1 cgd * [0,0] means anyplace
77 1.1 cgd * [u.ux,u.uy] means: call mnexto (if !in_mklev)
78 1.1 cgd *
79 1.1 cgd * In case we make an Orc or killer bee, we make an entire horde (swarm);
80 1.1 cgd * note that in this case we return only one of them (the one at [x,y]).
81 1.1 cgd */
82 1.4 christos struct monst *
83 1.5 jsm makemon(const struct permonst *ptr, int x, int y)
84 1.1 cgd {
85 1.4 christos struct monst *mtmp;
86 1.4 christos int tmp, ct;
87 1.7 dholland unsigned i;
88 1.4 christos boolean anything = (!ptr);
89 1.4 christos
90 1.4 christos if (x != 0 || y != 0)
91 1.4 christos if (m_at(x, y))
92 1.4 christos return ((struct monst *) 0);
93 1.4 christos if (ptr) {
94 1.4 christos if (strchr(fut_geno, ptr->mlet))
95 1.4 christos return ((struct monst *) 0);
96 1.1 cgd } else {
97 1.1 cgd ct = CMNUM - strlen(fut_geno);
98 1.4 christos if (strchr(fut_geno, 'm'))
99 1.4 christos ct++; /* make only 1 minotaur */
100 1.4 christos if (strchr(fut_geno, '@'))
101 1.4 christos ct++;
102 1.4 christos if (ct <= 0)
103 1.4 christos return (0); /* no more monsters! */
104 1.4 christos tmp = rn2(ct * dlevel / 24 + 7);
105 1.4 christos if (tmp < dlevel - 4)
106 1.4 christos tmp = rn2(ct * dlevel / 24 + 12);
107 1.4 christos if (tmp >= ct)
108 1.4 christos tmp = rn1(ct - ct / 2, ct / 2);
109 1.4 christos for (ct = 0; ct < CMNUM; ct++) {
110 1.1 cgd ptr = &mons[ct];
111 1.4 christos if (strchr(fut_geno, ptr->mlet))
112 1.1 cgd continue;
113 1.4 christos if (!tmp--)
114 1.4 christos goto gotmon;
115 1.1 cgd }
116 1.1 cgd panic("makemon?");
117 1.1 cgd }
118 1.1 cgd gotmon:
119 1.1 cgd mtmp = newmonst(ptr->pxlth);
120 1.1 cgd *mtmp = zeromonst; /* clear all entries in structure */
121 1.7 dholland for (i = 0; i < ptr->pxlth; i++)
122 1.7 dholland ((char *) &(mtmp->mextra[0]))[i] = 0;
123 1.1 cgd mtmp->nmon = fmon;
124 1.1 cgd fmon = mtmp;
125 1.1 cgd mtmp->m_id = flags.ident++;
126 1.1 cgd mtmp->data = ptr;
127 1.1 cgd mtmp->mxlth = ptr->pxlth;
128 1.4 christos if (ptr->mlet == 'D')
129 1.4 christos mtmp->mhpmax = mtmp->mhp = 80;
130 1.4 christos else if (!ptr->mlevel)
131 1.4 christos mtmp->mhpmax = mtmp->mhp = rnd(4);
132 1.4 christos else
133 1.4 christos mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
134 1.1 cgd mtmp->mx = x;
135 1.1 cgd mtmp->my = y;
136 1.1 cgd mtmp->mcansee = 1;
137 1.4 christos if (ptr->mlet == 'M') {
138 1.1 cgd mtmp->mimic = 1;
139 1.1 cgd mtmp->mappearance = ']';
140 1.1 cgd }
141 1.4 christos if (!in_mklev) {
142 1.4 christos if (x == u.ux && y == u.uy && ptr->mlet != ' ')
143 1.1 cgd mnexto(mtmp);
144 1.4 christos if (x == 0 && y == 0)
145 1.1 cgd rloc(mtmp);
146 1.1 cgd }
147 1.4 christos if (ptr->mlet == 's' || ptr->mlet == 'S') {
148 1.1 cgd mtmp->mhide = mtmp->mundetected = 1;
149 1.4 christos if (in_mklev)
150 1.4 christos if (mtmp->mx && mtmp->my)
151 1.4 christos (void) mkobj_at(0, mtmp->mx, mtmp->my);
152 1.1 cgd }
153 1.4 christos if (ptr->mlet == ':') {
154 1.1 cgd mtmp->cham = 1;
155 1.4 christos (void) newcham(mtmp, &mons[dlevel + 14 + rn2(CMNUM - 14 - dlevel)]);
156 1.1 cgd }
157 1.4 christos if (ptr->mlet == 'I' || ptr->mlet == ';')
158 1.1 cgd mtmp->minvis = 1;
159 1.4 christos if (ptr->mlet == 'L' || ptr->mlet == 'N'
160 1.4 christos || (in_mklev && strchr("&w;", ptr->mlet) && rn2(5))
161 1.4 christos )
162 1.4 christos mtmp->msleep = 1;
163 1.1 cgd
164 1.1 cgd #ifndef NOWORM
165 1.4 christos if (ptr->mlet == 'w' && getwn(mtmp))
166 1.1 cgd initworm(mtmp);
167 1.4 christos #endif /* NOWORM */
168 1.1 cgd
169 1.4 christos if (anything)
170 1.4 christos if (ptr->mlet == 'O' || ptr->mlet == 'k') {
171 1.4 christos coord mm;
172 1.4 christos int cnt = rnd(10);
173 1.4 christos mm.x = x;
174 1.4 christos mm.y = y;
175 1.4 christos while (cnt--) {
176 1.4 christos mm = enexto(mm.x, mm.y);
177 1.4 christos (void) makemon(ptr, mm.x, mm.y);
178 1.4 christos }
179 1.1 cgd }
180 1.4 christos return (mtmp);
181 1.1 cgd }
182 1.1 cgd
183 1.1 cgd coord
184 1.4 christos enexto(xx, yy)
185 1.4 christos xchar xx, yy;
186 1.1 cgd {
187 1.4 christos xchar x, y;
188 1.4 christos coord foo[15], *tfoo;
189 1.4 christos int range;
190 1.1 cgd
191 1.1 cgd tfoo = foo;
192 1.1 cgd range = 1;
193 1.4 christos do { /* full kludge action. */
194 1.4 christos for (x = xx - range; x <= xx + range; x++)
195 1.4 christos if (goodpos(x, yy - range)) {
196 1.1 cgd tfoo->x = x;
197 1.4 christos tfoo++->y = yy - range;
198 1.4 christos if (tfoo == &foo[15])
199 1.4 christos goto foofull;
200 1.1 cgd }
201 1.4 christos for (x = xx - range; x <= xx + range; x++)
202 1.4 christos if (goodpos(x, yy + range)) {
203 1.1 cgd tfoo->x = x;
204 1.4 christos tfoo++->y = yy + range;
205 1.4 christos if (tfoo == &foo[15])
206 1.4 christos goto foofull;
207 1.1 cgd }
208 1.4 christos for (y = yy + 1 - range; y < yy + range; y++)
209 1.4 christos if (goodpos(xx - range, y)) {
210 1.4 christos tfoo->x = xx - range;
211 1.1 cgd tfoo++->y = y;
212 1.4 christos if (tfoo == &foo[15])
213 1.4 christos goto foofull;
214 1.1 cgd }
215 1.4 christos for (y = yy + 1 - range; y < yy + range; y++)
216 1.4 christos if (goodpos(xx + range, y)) {
217 1.4 christos tfoo->x = xx + range;
218 1.1 cgd tfoo++->y = y;
219 1.4 christos if (tfoo == &foo[15])
220 1.4 christos goto foofull;
221 1.1 cgd }
222 1.1 cgd range++;
223 1.4 christos } while (tfoo == foo);
224 1.1 cgd foofull:
225 1.4 christos return (foo[rn2(tfoo - foo)]);
226 1.1 cgd }
227 1.1 cgd
228 1.4 christos int
229 1.5 jsm goodpos(int x, int y)
230 1.4 christos { /* used only in mnexto and rloc */
231 1.4 christos return (
232 1.4 christos !(x < 1 || x > COLNO - 2 || y < 1 || y > ROWNO - 2 ||
233 1.4 christos m_at(x, y) || !ACCESSIBLE(levl[x][y].typ)
234 1.4 christos || (x == u.ux && y == u.uy)
235 1.4 christos || sobj_at(ENORMOUS_ROCK, x, y)
236 1.4 christos ));
237 1.1 cgd }
238 1.1 cgd
239 1.4 christos void
240 1.1 cgd rloc(mtmp)
241 1.4 christos struct monst *mtmp;
242 1.1 cgd {
243 1.4 christos int tx, ty;
244 1.4 christos char ch = mtmp->data->mlet;
245 1.1 cgd
246 1.1 cgd #ifndef NOWORM
247 1.4 christos if (ch == 'w' && mtmp->mx)
248 1.4 christos return; /* do not relocate worms */
249 1.4 christos #endif /* NOWORM */
250 1.1 cgd do {
251 1.4 christos tx = rn1(COLNO - 3, 2);
252 1.1 cgd ty = rn2(ROWNO);
253 1.4 christos } while (!goodpos(tx, ty));
254 1.1 cgd mtmp->mx = tx;
255 1.1 cgd mtmp->my = ty;
256 1.4 christos if (u.ustuck == mtmp) {
257 1.4 christos if (u.uswallow) {
258 1.1 cgd u.ux = tx;
259 1.1 cgd u.uy = ty;
260 1.1 cgd docrt();
261 1.4 christos } else
262 1.4 christos u.ustuck = 0;
263 1.1 cgd }
264 1.1 cgd pmon(mtmp);
265 1.1 cgd }
266 1.1 cgd
267 1.4 christos struct monst *
268 1.4 christos mkmon_at(let, x, y)
269 1.4 christos char let;
270 1.4 christos int x, y;
271 1.1 cgd {
272 1.4 christos int ct;
273 1.5 jsm const struct permonst *ptr;
274 1.1 cgd
275 1.4 christos for (ct = 0; ct < CMNUM; ct++) {
276 1.1 cgd ptr = &mons[ct];
277 1.4 christos if (ptr->mlet == let)
278 1.4 christos return (makemon(ptr, x, y));
279 1.1 cgd }
280 1.4 christos return (0);
281 1.1 cgd }
282