aboutsummaryrefslogtreecommitdiff
path: root/hack/hack.worm.c
blob: 3a8d3603902f27bc7bb149c8617dcf306be76616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* hack.worm.c - version 1.0.2 */
/* $FreeBSD: src/games/hack/hack.worm.c,v 1.4 1999/11/16 10:26:38 marcel Exp $ */

#include "hack.h"
#ifndef NOWORM

struct wseg *wsegs[32];	/* linked list, tail first */
struct wseg *wheads[32];
long wgrowtime[32];

static void remseg(struct wseg *);

bool
getwn(struct monst *mtmp)
{
	int tmp;

	for (tmp = 1; tmp < 32; tmp++)
		if (!wsegs[tmp]) {
			mtmp->wormno = tmp;
			return (1);
		}
	return (0);		/* level infested with worms */
}

/* called to initialize a worm unless cut in half */
void
initworm(struct monst *mtmp)
{
	struct wseg *wtmp;
	int tmp = mtmp->wormno;

	if (!tmp)
		return;
	wheads[tmp] = wsegs[tmp] = wtmp = newseg();
	wgrowtime[tmp] = 0;
	wtmp->wx = mtmp->mx;
	wtmp->wy = mtmp->my;
	wtmp->nseg = 0;
}

void
worm_move(struct monst *mtmp)
{
	struct wseg *wtmp, *whd;
	int tmp = mtmp->wormno;

	wtmp = newseg();
	wtmp->wx = mtmp->mx;
	wtmp->wy = mtmp->my;
	wtmp->nseg = 0;
	(whd = wheads[tmp])->nseg = wtmp;
	wheads[tmp] = wtmp;
	if (cansee(whd->wx, whd->wy)) {
		unpmon(mtmp);
		atl(whd->wx, whd->wy, '~');
		whd->wdispl = 1;
	} else
		whd->wdispl = 0;
	if (wgrowtime[tmp] <= moves) {
		if (!wgrowtime[tmp])
			wgrowtime[tmp] = moves + rnd(5);
		else
			wgrowtime[tmp] += 2 + rnd(15);
		mtmp->mhpmax += 3;
		mtmp->mhp += 3;
		return;
	}
	whd = wsegs[tmp];
	wsegs[tmp] = whd->nseg;
	remseg(whd);
}

void
worm_nomove(struct monst *mtmp)
{
	int tmp;
	struct wseg *wtmp;

	tmp = mtmp->wormno;
	wtmp = wsegs[tmp];
	if (wtmp == wheads[tmp])
		return;
	if (wtmp == NULL || wtmp->nseg == 0)
		panic("worm_nomove?");
	wsegs[tmp] = wtmp->nseg;
	remseg(wtmp);
	mtmp->mhp -= 3;		/* mhpmax not changed ! */
}

void
wormdead(struct monst *mtmp)
{
	int tmp = mtmp->wormno;
	struct wseg *wtmp, *wtmp2;

	if (!tmp)
		return;
	mtmp->wormno = 0;
	for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
		wtmp2 = wtmp->nseg;
		remseg(wtmp);
	}
	wsegs[tmp] = NULL;
}

void
wormhit(struct monst *mtmp)
{
	int tmp = mtmp->wormno;
	struct wseg *wtmp;

	if (!tmp)	/* worm without tail */
		return;
	for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
		hitu(mtmp, 1);
}

void
wormsee(unsigned int tmp)
{
	struct wseg *wtmp = wsegs[tmp];

	if (!wtmp)
		panic("wormsee: wtmp==0");
	for (; wtmp->nseg; wtmp = wtmp->nseg)
		if (!cansee(wtmp->wx, wtmp->wy) && wtmp->wdispl) {
			newsym(wtmp->wx, wtmp->wy);
			wtmp->wdispl = 0;
		}
}

void
pwseg(struct wseg *wtmp)
{
	if (!wtmp->wdispl) {
		atl(wtmp->wx, wtmp->wy, '~');
		wtmp->wdispl = 1;
	}
}

/* weptyp: uwep->otyp or 0 */
void
cutworm(struct monst *mtmp, xchar x, xchar y, uchar weptyp)
{
	struct wseg *wtmp, *wtmp2;
	struct monst *mtmp2;
	int tmp, tmp2;

	if (mtmp->mx == x && mtmp->my == y)	/* hit headon */
		return;

	/* cutting goes best with axe or sword */
	tmp = rnd(20);
	if (weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
	    weptyp == AXE)
		tmp += 5;
	if (tmp < 12)
		return;

	/* if tail then worm just loses a tail segment */
	tmp = mtmp->wormno;
	wtmp = wsegs[tmp];
	if (wtmp->wx == x && wtmp->wy == y) {
		wsegs[tmp] = wtmp->nseg;
		remseg(wtmp);
		return;
	}

	/* cut the worm in two halves */
	mtmp2 = newmonst(0);
	*mtmp2 = *mtmp;
	mtmp2->mxlth = mtmp2->mnamelth = 0;

	/* sometimes the tail end dies */
	if (rn2(3) || !getwn(mtmp2)) {
		monfree(mtmp2);
		tmp2 = 0;
	} else {
		tmp2 = mtmp2->wormno;
		wsegs[tmp2] = wsegs[tmp];
		wgrowtime[tmp2] = 0;
	}
	do {
		if (wtmp->nseg->wx == x && wtmp->nseg->wy == y) {
			if (tmp2)
				wheads[tmp2] = wtmp;
			wsegs[tmp] = wtmp->nseg->nseg;
			remseg(wtmp->nseg);
			wtmp->nseg = 0;
			if (tmp2) {
				pline("You cut the worm in half.");
				mtmp2->mhpmax = mtmp2->mhp =
				    d(mtmp2->data->mlevel, 8);
				mtmp2->mx = wtmp->wx;
				mtmp2->my = wtmp->wy;
				mtmp2->nmon = fmon;
				fmon = mtmp2;
				pmon(mtmp2);
			} else {
				pline("You cut off part of the worm's tail.");
				remseg(wtmp);
			}
			mtmp->mhp /= 2;
			return;
		}
		wtmp2 = wtmp->nseg;
		if (!tmp2)
			remseg(wtmp);
		wtmp = wtmp2;
	} while (wtmp->nseg);
	panic("Cannot find worm segment");
}

static void
remseg(struct wseg *wtmp)
{
	if (wtmp->wdispl)
		newsym(wtmp->wx, wtmp->wy);
	free(wtmp);
}
#endif /* NOWORM */