hack.worm.c revision 1.6 1 1.6 christos /* $NetBSD: hack.worm.c,v 1.6 2006/05/13 22:43:02 christos Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.5 jsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 1.5 jsm * Amsterdam
6 1.5 jsm * All rights reserved.
7 1.5 jsm *
8 1.5 jsm * Redistribution and use in source and binary forms, with or without
9 1.5 jsm * modification, are permitted provided that the following conditions are
10 1.5 jsm * met:
11 1.5 jsm *
12 1.5 jsm * - Redistributions of source code must retain the above copyright notice,
13 1.5 jsm * this list of conditions and the following disclaimer.
14 1.5 jsm *
15 1.5 jsm * - Redistributions in binary form must reproduce the above copyright
16 1.5 jsm * notice, this list of conditions and the following disclaimer in the
17 1.5 jsm * documentation and/or other materials provided with the distribution.
18 1.5 jsm *
19 1.5 jsm * - Neither the name of the Stichting Centrum voor Wiskunde en
20 1.5 jsm * Informatica, nor the names of its contributors may be used to endorse or
21 1.5 jsm * promote products derived from this software without specific prior
22 1.5 jsm * written permission.
23 1.5 jsm *
24 1.5 jsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 1.5 jsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.5 jsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 1.5 jsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 1.5 jsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 1.5 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 1.5 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 1.5 jsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 1.5 jsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 1.5 jsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 1.5 jsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.5 jsm */
36 1.5 jsm
37 1.5 jsm /*
38 1.5 jsm * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 1.5 jsm * All rights reserved.
40 1.5 jsm *
41 1.5 jsm * Redistribution and use in source and binary forms, with or without
42 1.5 jsm * modification, are permitted provided that the following conditions
43 1.5 jsm * are met:
44 1.5 jsm * 1. Redistributions of source code must retain the above copyright
45 1.5 jsm * notice, this list of conditions and the following disclaimer.
46 1.5 jsm * 2. Redistributions in binary form must reproduce the above copyright
47 1.5 jsm * notice, this list of conditions and the following disclaimer in the
48 1.5 jsm * documentation and/or other materials provided with the distribution.
49 1.5 jsm * 3. The name of the author may not be used to endorse or promote products
50 1.5 jsm * derived from this software without specific prior written permission.
51 1.5 jsm *
52 1.5 jsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 1.5 jsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 1.5 jsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 1.5 jsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 1.5 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 1.5 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 1.5 jsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 1.5 jsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 1.5 jsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 1.5 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.6 christos __RCSID("$NetBSD: hack.worm.c,v 1.6 2006/05/13 22:43:02 christos Exp $");
67 1.4 christos #endif /* not lint */
68 1.1 cgd
69 1.4 christos #include <stdlib.h>
70 1.1 cgd #include "hack.h"
71 1.4 christos #include "extern.h"
72 1.1 cgd #ifndef NOWORM
73 1.1 cgd #include "def.wseg.h"
74 1.1 cgd
75 1.4 christos struct wseg *wsegs[32]; /* linked list, tail first */
76 1.4 christos struct wseg *wheads[32];
77 1.4 christos long wgrowtime[32];
78 1.4 christos
79 1.4 christos int
80 1.4 christos getwn(mtmp)
81 1.4 christos struct monst *mtmp;
82 1.4 christos {
83 1.4 christos int tmp;
84 1.4 christos for (tmp = 1; tmp < 32; tmp++)
85 1.4 christos if (!wsegs[tmp]) {
86 1.4 christos mtmp->wormno = tmp;
87 1.4 christos return (1);
88 1.4 christos }
89 1.4 christos return (0); /* level infested with worms */
90 1.1 cgd }
91 1.1 cgd
92 1.1 cgd /* called to initialize a worm unless cut in half */
93 1.4 christos void
94 1.4 christos initworm(mtmp)
95 1.4 christos struct monst *mtmp;
96 1.4 christos {
97 1.4 christos struct wseg *wtmp;
98 1.4 christos int tmp = mtmp->wormno;
99 1.4 christos if (!tmp)
100 1.4 christos return;
101 1.1 cgd wheads[tmp] = wsegs[tmp] = wtmp = newseg();
102 1.1 cgd wgrowtime[tmp] = 0;
103 1.1 cgd wtmp->wx = mtmp->mx;
104 1.1 cgd wtmp->wy = mtmp->my;
105 1.4 christos /* wtmp->wdispl = 0; */
106 1.1 cgd wtmp->nseg = 0;
107 1.1 cgd }
108 1.1 cgd
109 1.4 christos void
110 1.4 christos worm_move(mtmp)
111 1.4 christos struct monst *mtmp;
112 1.4 christos {
113 1.4 christos struct wseg *wtmp, *whd = NULL;
114 1.4 christos int tmp = mtmp->wormno;
115 1.1 cgd wtmp = newseg();
116 1.1 cgd wtmp->wx = mtmp->mx;
117 1.1 cgd wtmp->wy = mtmp->my;
118 1.1 cgd wtmp->nseg = 0;
119 1.4 christos /* wtmp->wdispl = 0; */
120 1.1 cgd (whd = wheads[tmp])->nseg = wtmp;
121 1.1 cgd wheads[tmp] = wtmp;
122 1.4 christos if (cansee(whd->wx, whd->wy)) {
123 1.1 cgd unpmon(mtmp);
124 1.1 cgd atl(whd->wx, whd->wy, '~');
125 1.1 cgd whd->wdispl = 1;
126 1.4 christos } else
127 1.4 christos whd->wdispl = 0;
128 1.4 christos if (wgrowtime[tmp] <= moves) {
129 1.4 christos if (!wgrowtime[tmp])
130 1.4 christos wgrowtime[tmp] = moves + rnd(5);
131 1.4 christos else
132 1.4 christos wgrowtime[tmp] += 2 + rnd(15);
133 1.1 cgd mtmp->mhpmax += 3;
134 1.1 cgd mtmp->mhp += 3;
135 1.1 cgd return;
136 1.1 cgd }
137 1.1 cgd whd = wsegs[tmp];
138 1.1 cgd wsegs[tmp] = whd->nseg;
139 1.1 cgd remseg(whd);
140 1.1 cgd }
141 1.1 cgd
142 1.4 christos void
143 1.4 christos worm_nomove(mtmp)
144 1.4 christos struct monst *mtmp;
145 1.4 christos {
146 1.4 christos int tmp;
147 1.4 christos struct wseg *wtmp;
148 1.1 cgd tmp = mtmp->wormno;
149 1.1 cgd wtmp = wsegs[tmp];
150 1.4 christos if (wtmp == wheads[tmp])
151 1.4 christos return;
152 1.4 christos if (wtmp == 0 || wtmp->nseg == 0)
153 1.4 christos panic("worm_nomove?");
154 1.1 cgd wsegs[tmp] = wtmp->nseg;
155 1.1 cgd remseg(wtmp);
156 1.4 christos mtmp->mhp -= 3; /* mhpmax not changed ! */
157 1.1 cgd }
158 1.1 cgd
159 1.4 christos void
160 1.4 christos wormdead(mtmp)
161 1.4 christos struct monst *mtmp;
162 1.4 christos {
163 1.4 christos int tmp = mtmp->wormno;
164 1.4 christos struct wseg *wtmp, *wtmp2;
165 1.4 christos if (!tmp)
166 1.4 christos return;
167 1.1 cgd mtmp->wormno = 0;
168 1.4 christos for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
169 1.1 cgd wtmp2 = wtmp->nseg;
170 1.1 cgd remseg(wtmp);
171 1.1 cgd }
172 1.1 cgd wsegs[tmp] = 0;
173 1.1 cgd }
174 1.1 cgd
175 1.4 christos void
176 1.4 christos wormhit(mtmp)
177 1.4 christos struct monst *mtmp;
178 1.4 christos {
179 1.4 christos int tmp = mtmp->wormno;
180 1.4 christos struct wseg *wtmp;
181 1.4 christos if (!tmp)
182 1.4 christos return; /* worm without tail */
183 1.4 christos for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
184 1.4 christos (void) hitu(mtmp, 1);
185 1.1 cgd }
186 1.1 cgd
187 1.4 christos void
188 1.4 christos wormsee(tmp)
189 1.4 christos unsigned tmp;
190 1.4 christos {
191 1.4 christos struct wseg *wtmp = wsegs[tmp];
192 1.4 christos if (!wtmp)
193 1.4 christos panic("wormsee: wtmp==0");
194 1.4 christos for (; wtmp->nseg; wtmp = wtmp->nseg)
195 1.4 christos if (!cansee(wtmp->wx, wtmp->wy) && wtmp->wdispl) {
196 1.1 cgd newsym(wtmp->wx, wtmp->wy);
197 1.1 cgd wtmp->wdispl = 0;
198 1.1 cgd }
199 1.1 cgd }
200 1.1 cgd
201 1.4 christos void
202 1.4 christos pwseg(wtmp)
203 1.4 christos struct wseg *wtmp;
204 1.4 christos {
205 1.4 christos if (!wtmp->wdispl) {
206 1.1 cgd atl(wtmp->wx, wtmp->wy, '~');
207 1.1 cgd wtmp->wdispl = 1;
208 1.1 cgd }
209 1.1 cgd }
210 1.1 cgd
211 1.4 christos void
212 1.4 christos cutworm(mtmp, x, y, weptyp)
213 1.4 christos struct monst *mtmp;
214 1.4 christos xchar x, y;
215 1.4 christos uchar weptyp; /* uwep->otyp or 0 */
216 1.4 christos {
217 1.4 christos struct wseg *wtmp, *wtmp2;
218 1.4 christos struct monst *mtmp2;
219 1.4 christos int tmp, tmp2;
220 1.4 christos if (mtmp->mx == x && mtmp->my == y)
221 1.4 christos return; /* hit headon */
222 1.1 cgd
223 1.1 cgd /* cutting goes best with axe or sword */
224 1.1 cgd tmp = rnd(20);
225 1.4 christos if (weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
226 1.4 christos weptyp == AXE)
227 1.4 christos tmp += 5;
228 1.4 christos if (tmp < 12)
229 1.4 christos return;
230 1.1 cgd
231 1.1 cgd /* if tail then worm just loses a tail segment */
232 1.1 cgd tmp = mtmp->wormno;
233 1.1 cgd wtmp = wsegs[tmp];
234 1.4 christos if (wtmp->wx == x && wtmp->wy == y) {
235 1.1 cgd wsegs[tmp] = wtmp->nseg;
236 1.1 cgd remseg(wtmp);
237 1.1 cgd return;
238 1.1 cgd }
239 1.1 cgd /* cut the worm in two halves */
240 1.1 cgd mtmp2 = newmonst(0);
241 1.1 cgd *mtmp2 = *mtmp;
242 1.1 cgd mtmp2->mxlth = mtmp2->mnamelth = 0;
243 1.1 cgd
244 1.1 cgd /* sometimes the tail end dies */
245 1.4 christos if (rn2(3) || !getwn(mtmp2)) {
246 1.1 cgd monfree(mtmp2);
247 1.1 cgd tmp2 = 0;
248 1.1 cgd } else {
249 1.1 cgd tmp2 = mtmp2->wormno;
250 1.1 cgd wsegs[tmp2] = wsegs[tmp];
251 1.1 cgd wgrowtime[tmp2] = 0;
252 1.1 cgd }
253 1.1 cgd do {
254 1.4 christos if (wtmp->nseg->wx == x && wtmp->nseg->wy == y) {
255 1.4 christos if (tmp2)
256 1.4 christos wheads[tmp2] = wtmp;
257 1.1 cgd wsegs[tmp] = wtmp->nseg->nseg;
258 1.1 cgd remseg(wtmp->nseg);
259 1.1 cgd wtmp->nseg = 0;
260 1.4 christos if (tmp2) {
261 1.1 cgd pline("You cut the worm in half.");
262 1.1 cgd mtmp2->mhpmax = mtmp2->mhp =
263 1.1 cgd d(mtmp2->data->mlevel, 8);
264 1.1 cgd mtmp2->mx = wtmp->wx;
265 1.1 cgd mtmp2->my = wtmp->wy;
266 1.1 cgd mtmp2->nmon = fmon;
267 1.1 cgd fmon = mtmp2;
268 1.1 cgd pmon(mtmp2);
269 1.1 cgd } else {
270 1.1 cgd pline("You cut off part of the worm's tail.");
271 1.1 cgd remseg(wtmp);
272 1.6 christos monfree(mtmp2);
273 1.1 cgd }
274 1.1 cgd mtmp->mhp /= 2;
275 1.1 cgd return;
276 1.1 cgd }
277 1.1 cgd wtmp2 = wtmp->nseg;
278 1.4 christos if (!tmp2)
279 1.4 christos remseg(wtmp);
280 1.1 cgd wtmp = wtmp2;
281 1.4 christos } while (wtmp->nseg);
282 1.1 cgd panic("Cannot find worm segment");
283 1.1 cgd }
284 1.1 cgd
285 1.4 christos void
286 1.4 christos remseg(wtmp)
287 1.4 christos struct wseg *wtmp;
288 1.4 christos {
289 1.4 christos if (wtmp->wdispl)
290 1.1 cgd newsym(wtmp->wx, wtmp->wy);
291 1.1 cgd free((char *) wtmp);
292 1.1 cgd }
293 1.4 christos #endif /* NOWORM */
294