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