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