1*8e73b3adSdholland /* $NetBSD: hack.worm.c,v 1.9 2011/08/06 20:29:37 dholland Exp $ */
23ea4a95cSchristos
3210cab45Smycroft /*
41c7f94e5Sjsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
51c7f94e5Sjsm * Amsterdam
61c7f94e5Sjsm * All rights reserved.
71c7f94e5Sjsm *
81c7f94e5Sjsm * Redistribution and use in source and binary forms, with or without
91c7f94e5Sjsm * modification, are permitted provided that the following conditions are
101c7f94e5Sjsm * met:
111c7f94e5Sjsm *
121c7f94e5Sjsm * - Redistributions of source code must retain the above copyright notice,
131c7f94e5Sjsm * this list of conditions and the following disclaimer.
141c7f94e5Sjsm *
151c7f94e5Sjsm * - Redistributions in binary form must reproduce the above copyright
161c7f94e5Sjsm * notice, this list of conditions and the following disclaimer in the
171c7f94e5Sjsm * documentation and/or other materials provided with the distribution.
181c7f94e5Sjsm *
191c7f94e5Sjsm * - Neither the name of the Stichting Centrum voor Wiskunde en
201c7f94e5Sjsm * Informatica, nor the names of its contributors may be used to endorse or
211c7f94e5Sjsm * promote products derived from this software without specific prior
221c7f94e5Sjsm * written permission.
231c7f94e5Sjsm *
241c7f94e5Sjsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
251c7f94e5Sjsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
261c7f94e5Sjsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
271c7f94e5Sjsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
281c7f94e5Sjsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
291c7f94e5Sjsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
301c7f94e5Sjsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
311c7f94e5Sjsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
321c7f94e5Sjsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
331c7f94e5Sjsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
341c7f94e5Sjsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
351c7f94e5Sjsm */
361c7f94e5Sjsm
371c7f94e5Sjsm /*
381c7f94e5Sjsm * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
391c7f94e5Sjsm * All rights reserved.
401c7f94e5Sjsm *
411c7f94e5Sjsm * Redistribution and use in source and binary forms, with or without
421c7f94e5Sjsm * modification, are permitted provided that the following conditions
431c7f94e5Sjsm * are met:
441c7f94e5Sjsm * 1. Redistributions of source code must retain the above copyright
451c7f94e5Sjsm * notice, this list of conditions and the following disclaimer.
461c7f94e5Sjsm * 2. Redistributions in binary form must reproduce the above copyright
471c7f94e5Sjsm * notice, this list of conditions and the following disclaimer in the
481c7f94e5Sjsm * documentation and/or other materials provided with the distribution.
491c7f94e5Sjsm * 3. The name of the author may not be used to endorse or promote products
501c7f94e5Sjsm * derived from this software without specific prior written permission.
511c7f94e5Sjsm *
521c7f94e5Sjsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
531c7f94e5Sjsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
541c7f94e5Sjsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
551c7f94e5Sjsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
561c7f94e5Sjsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
571c7f94e5Sjsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
581c7f94e5Sjsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
591c7f94e5Sjsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
601c7f94e5Sjsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
611c7f94e5Sjsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62210cab45Smycroft */
63210cab45Smycroft
643ea4a95cSchristos #include <sys/cdefs.h>
65210cab45Smycroft #ifndef lint
66*8e73b3adSdholland __RCSID("$NetBSD: hack.worm.c,v 1.9 2011/08/06 20:29:37 dholland Exp $");
67210cab45Smycroft #endif /* not lint */
6861f28255Scgd
693ea4a95cSchristos #include <stdlib.h>
7061f28255Scgd #include "hack.h"
713ea4a95cSchristos #include "extern.h"
7261f28255Scgd #ifndef NOWORM
7361f28255Scgd #include "def.wseg.h"
7461f28255Scgd
7561f28255Scgd struct wseg *wsegs[32]; /* linked list, tail first */
7661f28255Scgd struct wseg *wheads[32];
7761f28255Scgd long wgrowtime[32];
7861f28255Scgd
799b92b189Sdholland static void remseg(struct wseg *);
809b92b189Sdholland
813ea4a95cSchristos int
getwn(struct monst * mtmp)821fa8a9a6Sdholland getwn(struct monst *mtmp)
833ea4a95cSchristos {
843ea4a95cSchristos int tmp;
853ea4a95cSchristos for (tmp = 1; tmp < 32; tmp++)
863ea4a95cSchristos if (!wsegs[tmp]) {
8761f28255Scgd mtmp->wormno = tmp;
8861f28255Scgd return (1);
8961f28255Scgd }
9061f28255Scgd return (0); /* level infested with worms */
9161f28255Scgd }
9261f28255Scgd
9361f28255Scgd /* called to initialize a worm unless cut in half */
943ea4a95cSchristos void
initworm(struct monst * mtmp)951fa8a9a6Sdholland initworm(struct monst *mtmp)
963ea4a95cSchristos {
973ea4a95cSchristos struct wseg *wtmp;
983ea4a95cSchristos int tmp = mtmp->wormno;
993ea4a95cSchristos if (!tmp)
1003ea4a95cSchristos return;
10161f28255Scgd wheads[tmp] = wsegs[tmp] = wtmp = newseg();
10261f28255Scgd wgrowtime[tmp] = 0;
10361f28255Scgd wtmp->wx = mtmp->mx;
10461f28255Scgd wtmp->wy = mtmp->my;
10561f28255Scgd /* wtmp->wdispl = 0; */
10661f28255Scgd wtmp->nseg = 0;
10761f28255Scgd }
10861f28255Scgd
1093ea4a95cSchristos void
worm_move(struct monst * mtmp)1101fa8a9a6Sdholland worm_move(struct monst *mtmp)
1113ea4a95cSchristos {
1123ea4a95cSchristos struct wseg *wtmp, *whd = NULL;
1133ea4a95cSchristos int tmp = mtmp->wormno;
11461f28255Scgd wtmp = newseg();
11561f28255Scgd wtmp->wx = mtmp->mx;
11661f28255Scgd wtmp->wy = mtmp->my;
11761f28255Scgd wtmp->nseg = 0;
11861f28255Scgd /* wtmp->wdispl = 0; */
11961f28255Scgd (whd = wheads[tmp])->nseg = wtmp;
12061f28255Scgd wheads[tmp] = wtmp;
12161f28255Scgd if (cansee(whd->wx, whd->wy)) {
12261f28255Scgd unpmon(mtmp);
12361f28255Scgd atl(whd->wx, whd->wy, '~');
12461f28255Scgd whd->wdispl = 1;
1253ea4a95cSchristos } else
1263ea4a95cSchristos whd->wdispl = 0;
12761f28255Scgd if (wgrowtime[tmp] <= moves) {
1283ea4a95cSchristos if (!wgrowtime[tmp])
1293ea4a95cSchristos wgrowtime[tmp] = moves + rnd(5);
1303ea4a95cSchristos else
1313ea4a95cSchristos wgrowtime[tmp] += 2 + rnd(15);
13261f28255Scgd mtmp->mhpmax += 3;
13361f28255Scgd mtmp->mhp += 3;
13461f28255Scgd return;
13561f28255Scgd }
13661f28255Scgd whd = wsegs[tmp];
13761f28255Scgd wsegs[tmp] = whd->nseg;
13861f28255Scgd remseg(whd);
13961f28255Scgd }
14061f28255Scgd
1413ea4a95cSchristos void
worm_nomove(struct monst * mtmp)1421fa8a9a6Sdholland worm_nomove(struct monst *mtmp)
1433ea4a95cSchristos {
1443ea4a95cSchristos int tmp;
1453ea4a95cSchristos struct wseg *wtmp;
14661f28255Scgd tmp = mtmp->wormno;
14761f28255Scgd wtmp = wsegs[tmp];
1483ea4a95cSchristos if (wtmp == wheads[tmp])
1493ea4a95cSchristos return;
1503ea4a95cSchristos if (wtmp == 0 || wtmp->nseg == 0)
1513ea4a95cSchristos panic("worm_nomove?");
15261f28255Scgd wsegs[tmp] = wtmp->nseg;
15361f28255Scgd remseg(wtmp);
15461f28255Scgd mtmp->mhp -= 3; /* mhpmax not changed ! */
15561f28255Scgd }
15661f28255Scgd
1573ea4a95cSchristos void
wormdead(struct monst * mtmp)1581fa8a9a6Sdholland wormdead(struct monst *mtmp)
1593ea4a95cSchristos {
1603ea4a95cSchristos int tmp = mtmp->wormno;
1613ea4a95cSchristos struct wseg *wtmp, *wtmp2;
1623ea4a95cSchristos if (!tmp)
1633ea4a95cSchristos return;
16461f28255Scgd mtmp->wormno = 0;
16561f28255Scgd for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
16661f28255Scgd wtmp2 = wtmp->nseg;
16761f28255Scgd remseg(wtmp);
16861f28255Scgd }
16961f28255Scgd wsegs[tmp] = 0;
17061f28255Scgd }
17161f28255Scgd
1723ea4a95cSchristos void
wormhit(struct monst * mtmp)1731fa8a9a6Sdholland wormhit(struct monst *mtmp)
1743ea4a95cSchristos {
1753ea4a95cSchristos int tmp = mtmp->wormno;
1763ea4a95cSchristos struct wseg *wtmp;
1773ea4a95cSchristos if (!tmp)
1783ea4a95cSchristos return; /* worm without tail */
17961f28255Scgd for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
18061f28255Scgd (void) hitu(mtmp, 1);
18161f28255Scgd }
18261f28255Scgd
1833ea4a95cSchristos void
wormsee(unsigned tmp)1841fa8a9a6Sdholland wormsee(unsigned tmp)
1853ea4a95cSchristos {
1863ea4a95cSchristos struct wseg *wtmp = wsegs[tmp];
1873ea4a95cSchristos if (!wtmp)
1883ea4a95cSchristos panic("wormsee: wtmp==0");
18961f28255Scgd for (; wtmp->nseg; wtmp = wtmp->nseg)
19061f28255Scgd if (!cansee(wtmp->wx, wtmp->wy) && wtmp->wdispl) {
19161f28255Scgd newsym(wtmp->wx, wtmp->wy);
19261f28255Scgd wtmp->wdispl = 0;
19361f28255Scgd }
19461f28255Scgd }
19561f28255Scgd
1963ea4a95cSchristos void
pwseg(struct wseg * wtmp)1971fa8a9a6Sdholland pwseg(struct wseg *wtmp)
1983ea4a95cSchristos {
19961f28255Scgd if (!wtmp->wdispl) {
20061f28255Scgd atl(wtmp->wx, wtmp->wy, '~');
20161f28255Scgd wtmp->wdispl = 1;
20261f28255Scgd }
20361f28255Scgd }
20461f28255Scgd
2051fa8a9a6Sdholland /* weptyp is uwep->otyp or 0 */
2063ea4a95cSchristos void
cutworm(struct monst * mtmp,xchar x,xchar y,uchar weptyp)2071fa8a9a6Sdholland cutworm(struct monst *mtmp, xchar x, xchar y, uchar weptyp)
20861f28255Scgd {
2093ea4a95cSchristos struct wseg *wtmp, *wtmp2;
2103ea4a95cSchristos struct monst *mtmp2;
2113ea4a95cSchristos int tmp, tmp2;
2123ea4a95cSchristos if (mtmp->mx == x && mtmp->my == y)
2133ea4a95cSchristos return; /* hit headon */
21461f28255Scgd
21561f28255Scgd /* cutting goes best with axe or sword */
21661f28255Scgd tmp = rnd(20);
21761f28255Scgd if (weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
2183ea4a95cSchristos weptyp == AXE)
2193ea4a95cSchristos tmp += 5;
2203ea4a95cSchristos if (tmp < 12)
2213ea4a95cSchristos return;
22261f28255Scgd
22361f28255Scgd /* if tail then worm just loses a tail segment */
22461f28255Scgd tmp = mtmp->wormno;
22561f28255Scgd wtmp = wsegs[tmp];
22661f28255Scgd if (wtmp->wx == x && wtmp->wy == y) {
22761f28255Scgd wsegs[tmp] = wtmp->nseg;
22861f28255Scgd remseg(wtmp);
22961f28255Scgd return;
23061f28255Scgd }
23161f28255Scgd /* cut the worm in two halves */
23261f28255Scgd mtmp2 = newmonst(0);
23361f28255Scgd *mtmp2 = *mtmp;
23461f28255Scgd mtmp2->mxlth = mtmp2->mnamelth = 0;
23561f28255Scgd
23661f28255Scgd /* sometimes the tail end dies */
23761f28255Scgd if (rn2(3) || !getwn(mtmp2)) {
23861f28255Scgd monfree(mtmp2);
23961f28255Scgd tmp2 = 0;
24061f28255Scgd } else {
24161f28255Scgd tmp2 = mtmp2->wormno;
24261f28255Scgd wsegs[tmp2] = wsegs[tmp];
24361f28255Scgd wgrowtime[tmp2] = 0;
24461f28255Scgd }
24561f28255Scgd do {
24661f28255Scgd if (wtmp->nseg->wx == x && wtmp->nseg->wy == y) {
2473ea4a95cSchristos if (tmp2)
2483ea4a95cSchristos wheads[tmp2] = wtmp;
24961f28255Scgd wsegs[tmp] = wtmp->nseg->nseg;
25061f28255Scgd remseg(wtmp->nseg);
25161f28255Scgd wtmp->nseg = 0;
25261f28255Scgd if (tmp2) {
25361f28255Scgd pline("You cut the worm in half.");
25461f28255Scgd mtmp2->mhpmax = mtmp2->mhp =
25561f28255Scgd d(mtmp2->data->mlevel, 8);
25661f28255Scgd mtmp2->mx = wtmp->wx;
25761f28255Scgd mtmp2->my = wtmp->wy;
25861f28255Scgd mtmp2->nmon = fmon;
25961f28255Scgd fmon = mtmp2;
26061f28255Scgd pmon(mtmp2);
26161f28255Scgd } else {
26261f28255Scgd pline("You cut off part of the worm's tail.");
26361f28255Scgd remseg(wtmp);
2644b679b2aSchristos monfree(mtmp2);
26561f28255Scgd }
26661f28255Scgd mtmp->mhp /= 2;
26761f28255Scgd return;
26861f28255Scgd }
26961f28255Scgd wtmp2 = wtmp->nseg;
2703ea4a95cSchristos if (!tmp2)
2713ea4a95cSchristos remseg(wtmp);
27261f28255Scgd wtmp = wtmp2;
27361f28255Scgd } while (wtmp->nseg);
27461f28255Scgd panic("Cannot find worm segment");
27561f28255Scgd }
27661f28255Scgd
2779b92b189Sdholland static void
remseg(struct wseg * wtmp)2781fa8a9a6Sdholland remseg(struct wseg *wtmp)
2793ea4a95cSchristos {
28061f28255Scgd if (wtmp->wdispl)
28161f28255Scgd newsym(wtmp->wx, wtmp->wy);
282*8e73b3adSdholland free(wtmp);
28361f28255Scgd }
2843ea4a95cSchristos #endif /* NOWORM */
285