hack.worm.c revision 1.9 1 1.9 dholland /* $NetBSD: hack.worm.c,v 1.9 2011/08/06 20:29:37 dholland 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.9 dholland __RCSID("$NetBSD: hack.worm.c,v 1.9 2011/08/06 20:29:37 dholland 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.8 dholland static void remseg(struct wseg *);
80 1.8 dholland
81 1.4 christos int
82 1.7 dholland getwn(struct monst *mtmp)
83 1.4 christos {
84 1.4 christos int tmp;
85 1.4 christos for (tmp = 1; tmp < 32; tmp++)
86 1.4 christos if (!wsegs[tmp]) {
87 1.4 christos mtmp->wormno = tmp;
88 1.4 christos return (1);
89 1.4 christos }
90 1.4 christos return (0); /* level infested with worms */
91 1.1 cgd }
92 1.1 cgd
93 1.1 cgd /* called to initialize a worm unless cut in half */
94 1.4 christos void
95 1.7 dholland initworm(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.7 dholland worm_move(struct monst *mtmp)
111 1.4 christos {
112 1.4 christos struct wseg *wtmp, *whd = NULL;
113 1.4 christos int tmp = mtmp->wormno;
114 1.1 cgd wtmp = newseg();
115 1.1 cgd wtmp->wx = mtmp->mx;
116 1.1 cgd wtmp->wy = mtmp->my;
117 1.1 cgd wtmp->nseg = 0;
118 1.4 christos /* wtmp->wdispl = 0; */
119 1.1 cgd (whd = wheads[tmp])->nseg = wtmp;
120 1.1 cgd wheads[tmp] = wtmp;
121 1.4 christos if (cansee(whd->wx, whd->wy)) {
122 1.1 cgd unpmon(mtmp);
123 1.1 cgd atl(whd->wx, whd->wy, '~');
124 1.1 cgd whd->wdispl = 1;
125 1.4 christos } else
126 1.4 christos whd->wdispl = 0;
127 1.4 christos if (wgrowtime[tmp] <= moves) {
128 1.4 christos if (!wgrowtime[tmp])
129 1.4 christos wgrowtime[tmp] = moves + rnd(5);
130 1.4 christos else
131 1.4 christos wgrowtime[tmp] += 2 + rnd(15);
132 1.1 cgd mtmp->mhpmax += 3;
133 1.1 cgd mtmp->mhp += 3;
134 1.1 cgd return;
135 1.1 cgd }
136 1.1 cgd whd = wsegs[tmp];
137 1.1 cgd wsegs[tmp] = whd->nseg;
138 1.1 cgd remseg(whd);
139 1.1 cgd }
140 1.1 cgd
141 1.4 christos void
142 1.7 dholland worm_nomove(struct monst *mtmp)
143 1.4 christos {
144 1.4 christos int tmp;
145 1.4 christos struct wseg *wtmp;
146 1.1 cgd tmp = mtmp->wormno;
147 1.1 cgd wtmp = wsegs[tmp];
148 1.4 christos if (wtmp == wheads[tmp])
149 1.4 christos return;
150 1.4 christos if (wtmp == 0 || wtmp->nseg == 0)
151 1.4 christos panic("worm_nomove?");
152 1.1 cgd wsegs[tmp] = wtmp->nseg;
153 1.1 cgd remseg(wtmp);
154 1.4 christos mtmp->mhp -= 3; /* mhpmax not changed ! */
155 1.1 cgd }
156 1.1 cgd
157 1.4 christos void
158 1.7 dholland wormdead(struct monst *mtmp)
159 1.4 christos {
160 1.4 christos int tmp = mtmp->wormno;
161 1.4 christos struct wseg *wtmp, *wtmp2;
162 1.4 christos if (!tmp)
163 1.4 christos return;
164 1.1 cgd mtmp->wormno = 0;
165 1.4 christos for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
166 1.1 cgd wtmp2 = wtmp->nseg;
167 1.1 cgd remseg(wtmp);
168 1.1 cgd }
169 1.1 cgd wsegs[tmp] = 0;
170 1.1 cgd }
171 1.1 cgd
172 1.4 christos void
173 1.7 dholland wormhit(struct monst *mtmp)
174 1.4 christos {
175 1.4 christos int tmp = mtmp->wormno;
176 1.4 christos struct wseg *wtmp;
177 1.4 christos if (!tmp)
178 1.4 christos return; /* worm without tail */
179 1.4 christos for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
180 1.4 christos (void) hitu(mtmp, 1);
181 1.1 cgd }
182 1.1 cgd
183 1.4 christos void
184 1.7 dholland wormsee(unsigned tmp)
185 1.4 christos {
186 1.4 christos struct wseg *wtmp = wsegs[tmp];
187 1.4 christos if (!wtmp)
188 1.4 christos panic("wormsee: wtmp==0");
189 1.4 christos for (; wtmp->nseg; wtmp = wtmp->nseg)
190 1.4 christos if (!cansee(wtmp->wx, wtmp->wy) && wtmp->wdispl) {
191 1.1 cgd newsym(wtmp->wx, wtmp->wy);
192 1.1 cgd wtmp->wdispl = 0;
193 1.1 cgd }
194 1.1 cgd }
195 1.1 cgd
196 1.4 christos void
197 1.7 dholland pwseg(struct wseg *wtmp)
198 1.4 christos {
199 1.4 christos if (!wtmp->wdispl) {
200 1.1 cgd atl(wtmp->wx, wtmp->wy, '~');
201 1.1 cgd wtmp->wdispl = 1;
202 1.1 cgd }
203 1.1 cgd }
204 1.1 cgd
205 1.7 dholland /* weptyp is uwep->otyp or 0 */
206 1.4 christos void
207 1.7 dholland cutworm(struct monst *mtmp, xchar x, xchar y, uchar weptyp)
208 1.4 christos {
209 1.4 christos struct wseg *wtmp, *wtmp2;
210 1.4 christos struct monst *mtmp2;
211 1.4 christos int tmp, tmp2;
212 1.4 christos if (mtmp->mx == x && mtmp->my == y)
213 1.4 christos return; /* hit headon */
214 1.1 cgd
215 1.1 cgd /* cutting goes best with axe or sword */
216 1.1 cgd tmp = rnd(20);
217 1.4 christos if (weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
218 1.4 christos weptyp == AXE)
219 1.4 christos tmp += 5;
220 1.4 christos if (tmp < 12)
221 1.4 christos return;
222 1.1 cgd
223 1.1 cgd /* if tail then worm just loses a tail segment */
224 1.1 cgd tmp = mtmp->wormno;
225 1.1 cgd wtmp = wsegs[tmp];
226 1.4 christos if (wtmp->wx == x && wtmp->wy == y) {
227 1.1 cgd wsegs[tmp] = wtmp->nseg;
228 1.1 cgd remseg(wtmp);
229 1.1 cgd return;
230 1.1 cgd }
231 1.1 cgd /* cut the worm in two halves */
232 1.1 cgd mtmp2 = newmonst(0);
233 1.1 cgd *mtmp2 = *mtmp;
234 1.1 cgd mtmp2->mxlth = mtmp2->mnamelth = 0;
235 1.1 cgd
236 1.1 cgd /* sometimes the tail end dies */
237 1.4 christos if (rn2(3) || !getwn(mtmp2)) {
238 1.1 cgd monfree(mtmp2);
239 1.1 cgd tmp2 = 0;
240 1.1 cgd } else {
241 1.1 cgd tmp2 = mtmp2->wormno;
242 1.1 cgd wsegs[tmp2] = wsegs[tmp];
243 1.1 cgd wgrowtime[tmp2] = 0;
244 1.1 cgd }
245 1.1 cgd do {
246 1.4 christos if (wtmp->nseg->wx == x && wtmp->nseg->wy == y) {
247 1.4 christos if (tmp2)
248 1.4 christos wheads[tmp2] = wtmp;
249 1.1 cgd wsegs[tmp] = wtmp->nseg->nseg;
250 1.1 cgd remseg(wtmp->nseg);
251 1.1 cgd wtmp->nseg = 0;
252 1.4 christos if (tmp2) {
253 1.1 cgd pline("You cut the worm in half.");
254 1.1 cgd mtmp2->mhpmax = mtmp2->mhp =
255 1.1 cgd d(mtmp2->data->mlevel, 8);
256 1.1 cgd mtmp2->mx = wtmp->wx;
257 1.1 cgd mtmp2->my = wtmp->wy;
258 1.1 cgd mtmp2->nmon = fmon;
259 1.1 cgd fmon = mtmp2;
260 1.1 cgd pmon(mtmp2);
261 1.1 cgd } else {
262 1.1 cgd pline("You cut off part of the worm's tail.");
263 1.1 cgd remseg(wtmp);
264 1.6 christos monfree(mtmp2);
265 1.1 cgd }
266 1.1 cgd mtmp->mhp /= 2;
267 1.1 cgd return;
268 1.1 cgd }
269 1.1 cgd wtmp2 = wtmp->nseg;
270 1.4 christos if (!tmp2)
271 1.4 christos remseg(wtmp);
272 1.1 cgd wtmp = wtmp2;
273 1.4 christos } while (wtmp->nseg);
274 1.1 cgd panic("Cannot find worm segment");
275 1.1 cgd }
276 1.1 cgd
277 1.8 dholland static void
278 1.7 dholland remseg(struct wseg *wtmp)
279 1.4 christos {
280 1.4 christos if (wtmp->wdispl)
281 1.1 cgd newsym(wtmp->wx, wtmp->wy);
282 1.9 dholland free(wtmp);
283 1.1 cgd }
284 1.4 christos #endif /* NOWORM */
285