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