xref: /csrg-svn/games/hack/hack.worm.c (revision 41274)
1*41274Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2*41274Sbostic /* hack.worm.c - version 1.0.2 */
3*41274Sbostic 
4*41274Sbostic #include "hack.h"
5*41274Sbostic #ifndef NOWORM
6*41274Sbostic #include "def.wseg.h"
7*41274Sbostic 
8*41274Sbostic struct wseg *wsegs[32];	/* linked list, tail first */
9*41274Sbostic struct wseg *wheads[32];
10*41274Sbostic long wgrowtime[32];
11*41274Sbostic 
12*41274Sbostic getwn(mtmp) struct monst *mtmp; {
13*41274Sbostic register tmp;
14*41274Sbostic 	for(tmp=1; tmp<32; tmp++) if(!wsegs[tmp]) {
15*41274Sbostic 		mtmp->wormno = tmp;
16*41274Sbostic 		return(1);
17*41274Sbostic 	}
18*41274Sbostic 	return(0);	/* level infested with worms */
19*41274Sbostic }
20*41274Sbostic 
21*41274Sbostic /* called to initialize a worm unless cut in half */
22*41274Sbostic initworm(mtmp) struct monst *mtmp; {
23*41274Sbostic register struct wseg *wtmp;
24*41274Sbostic register tmp = mtmp->wormno;
25*41274Sbostic 	if(!tmp) return;
26*41274Sbostic 	wheads[tmp] = wsegs[tmp] = wtmp = newseg();
27*41274Sbostic 	wgrowtime[tmp] = 0;
28*41274Sbostic 	wtmp->wx = mtmp->mx;
29*41274Sbostic 	wtmp->wy = mtmp->my;
30*41274Sbostic /*	wtmp->wdispl = 0; */
31*41274Sbostic 	wtmp->nseg = 0;
32*41274Sbostic }
33*41274Sbostic 
34*41274Sbostic worm_move(mtmp) struct monst *mtmp; {
35*41274Sbostic register struct wseg *wtmp, *whd;
36*41274Sbostic register tmp = mtmp->wormno;
37*41274Sbostic 	wtmp = newseg();
38*41274Sbostic 	wtmp->wx = mtmp->mx;
39*41274Sbostic 	wtmp->wy = mtmp->my;
40*41274Sbostic 	wtmp->nseg = 0;
41*41274Sbostic /*	wtmp->wdispl = 0; */
42*41274Sbostic 	(whd = wheads[tmp])->nseg = wtmp;
43*41274Sbostic 	wheads[tmp] = wtmp;
44*41274Sbostic 	if(cansee(whd->wx,whd->wy)){
45*41274Sbostic 		unpmon(mtmp);
46*41274Sbostic 		atl(whd->wx, whd->wy, '~');
47*41274Sbostic 		whd->wdispl = 1;
48*41274Sbostic 	} else	whd->wdispl = 0;
49*41274Sbostic 	if(wgrowtime[tmp] <= moves) {
50*41274Sbostic 		if(!wgrowtime[tmp]) wgrowtime[tmp] = moves + rnd(5);
51*41274Sbostic 		else wgrowtime[tmp] += 2+rnd(15);
52*41274Sbostic 		mtmp->mhpmax += 3;
53*41274Sbostic 		mtmp->mhp += 3;
54*41274Sbostic 		return;
55*41274Sbostic 	}
56*41274Sbostic 	whd = wsegs[tmp];
57*41274Sbostic 	wsegs[tmp] = whd->nseg;
58*41274Sbostic 	remseg(whd);
59*41274Sbostic }
60*41274Sbostic 
worm_nomove(mtmp)61*41274Sbostic worm_nomove(mtmp) register struct monst *mtmp; {
62*41274Sbostic register tmp;
63*41274Sbostic register struct wseg *wtmp;
64*41274Sbostic 	tmp = mtmp->wormno;
65*41274Sbostic 	wtmp = wsegs[tmp];
66*41274Sbostic 	if(wtmp == wheads[tmp]) return;
67*41274Sbostic 	if(wtmp == 0 || wtmp->nseg == 0) panic("worm_nomove?");
68*41274Sbostic 	wsegs[tmp] = wtmp->nseg;
69*41274Sbostic 	remseg(wtmp);
70*41274Sbostic 	mtmp->mhp -= 3;	/* mhpmax not changed ! */
71*41274Sbostic }
72*41274Sbostic 
wormdead(mtmp)73*41274Sbostic wormdead(mtmp) register struct monst *mtmp; {
74*41274Sbostic register tmp = mtmp->wormno;
75*41274Sbostic register struct wseg *wtmp, *wtmp2;
76*41274Sbostic 	if(!tmp) return;
77*41274Sbostic 	mtmp->wormno = 0;
78*41274Sbostic 	for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
79*41274Sbostic 		wtmp2 = wtmp->nseg;
80*41274Sbostic 		remseg(wtmp);
81*41274Sbostic 	}
82*41274Sbostic 	wsegs[tmp] = 0;
83*41274Sbostic }
84*41274Sbostic 
wormhit(mtmp)85*41274Sbostic wormhit(mtmp) register struct monst *mtmp; {
86*41274Sbostic register tmp = mtmp->wormno;
87*41274Sbostic register struct wseg *wtmp;
88*41274Sbostic 	if(!tmp) return;	/* worm without tail */
89*41274Sbostic 	for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
90*41274Sbostic 		(void) hitu(mtmp,1);
91*41274Sbostic }
92*41274Sbostic 
wormsee(tmp)93*41274Sbostic wormsee(tmp) register unsigned tmp; {
94*41274Sbostic register struct wseg *wtmp = wsegs[tmp];
95*41274Sbostic 	if(!wtmp) panic("wormsee: wtmp==0");
96*41274Sbostic 	for(; wtmp->nseg; wtmp = wtmp->nseg)
97*41274Sbostic 		if(!cansee(wtmp->wx,wtmp->wy) && wtmp->wdispl){
98*41274Sbostic 			newsym(wtmp->wx, wtmp->wy);
99*41274Sbostic 			wtmp->wdispl = 0;
100*41274Sbostic 		}
101*41274Sbostic }
102*41274Sbostic 
pwseg(wtmp)103*41274Sbostic pwseg(wtmp) register struct wseg *wtmp; {
104*41274Sbostic 	if(!wtmp->wdispl){
105*41274Sbostic 		atl(wtmp->wx, wtmp->wy, '~');
106*41274Sbostic 		wtmp->wdispl = 1;
107*41274Sbostic 	}
108*41274Sbostic }
109*41274Sbostic 
cutworm(mtmp,x,y,weptyp)110*41274Sbostic cutworm(mtmp,x,y,weptyp)
111*41274Sbostic register struct monst *mtmp;
112*41274Sbostic register xchar x,y;
113*41274Sbostic register uchar weptyp;		/* uwep->otyp or 0 */
114*41274Sbostic {
115*41274Sbostic 	register struct wseg *wtmp, *wtmp2;
116*41274Sbostic 	register struct monst *mtmp2;
117*41274Sbostic 	register tmp,tmp2;
118*41274Sbostic 	if(mtmp->mx == x && mtmp->my == y) return;	/* hit headon */
119*41274Sbostic 
120*41274Sbostic 	/* cutting goes best with axe or sword */
121*41274Sbostic 	tmp = rnd(20);
122*41274Sbostic 	if(weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
123*41274Sbostic 		weptyp == AXE) tmp += 5;
124*41274Sbostic 	if(tmp < 12) return;
125*41274Sbostic 
126*41274Sbostic 	/* if tail then worm just loses a tail segment */
127*41274Sbostic 	tmp = mtmp->wormno;
128*41274Sbostic 	wtmp = wsegs[tmp];
129*41274Sbostic 	if(wtmp->wx == x && wtmp->wy == y){
130*41274Sbostic 		wsegs[tmp] = wtmp->nseg;
131*41274Sbostic 		remseg(wtmp);
132*41274Sbostic 		return;
133*41274Sbostic 	}
134*41274Sbostic 
135*41274Sbostic 	/* cut the worm in two halves */
136*41274Sbostic 	mtmp2 = newmonst(0);
137*41274Sbostic 	*mtmp2 = *mtmp;
138*41274Sbostic 	mtmp2->mxlth = mtmp2->mnamelth = 0;
139*41274Sbostic 
140*41274Sbostic 	/* sometimes the tail end dies */
141*41274Sbostic 	if(rn2(3) || !getwn(mtmp2)){
142*41274Sbostic 		monfree(mtmp2);
143*41274Sbostic 		tmp2 = 0;
144*41274Sbostic 	} else {
145*41274Sbostic 		tmp2 = mtmp2->wormno;
146*41274Sbostic 		wsegs[tmp2] = wsegs[tmp];
147*41274Sbostic 		wgrowtime[tmp2] = 0;
148*41274Sbostic 	}
149*41274Sbostic 	do {
150*41274Sbostic 		if(wtmp->nseg->wx == x && wtmp->nseg->wy == y){
151*41274Sbostic 			if(tmp2) wheads[tmp2] = wtmp;
152*41274Sbostic 			wsegs[tmp] = wtmp->nseg->nseg;
153*41274Sbostic 			remseg(wtmp->nseg);
154*41274Sbostic 			wtmp->nseg = 0;
155*41274Sbostic 			if(tmp2){
156*41274Sbostic 				pline("You cut the worm in half.");
157*41274Sbostic 				mtmp2->mhpmax = mtmp2->mhp =
158*41274Sbostic 					d(mtmp2->data->mlevel, 8);
159*41274Sbostic 				mtmp2->mx = wtmp->wx;
160*41274Sbostic 				mtmp2->my = wtmp->wy;
161*41274Sbostic 				mtmp2->nmon = fmon;
162*41274Sbostic 				fmon = mtmp2;
163*41274Sbostic 				pmon(mtmp2);
164*41274Sbostic 			} else {
165*41274Sbostic 				pline("You cut off part of the worm's tail.");
166*41274Sbostic 				remseg(wtmp);
167*41274Sbostic 			}
168*41274Sbostic 			mtmp->mhp /= 2;
169*41274Sbostic 			return;
170*41274Sbostic 		}
171*41274Sbostic 		wtmp2 = wtmp->nseg;
172*41274Sbostic 		if(!tmp2) remseg(wtmp);
173*41274Sbostic 		wtmp = wtmp2;
174*41274Sbostic 	} while(wtmp->nseg);
175*41274Sbostic 	panic("Cannot find worm segment");
176*41274Sbostic }
177*41274Sbostic 
remseg(wtmp)178*41274Sbostic remseg(wtmp) register struct wseg *wtmp; {
179*41274Sbostic 	if(wtmp->wdispl)
180*41274Sbostic 		newsym(wtmp->wx, wtmp->wy);
181*41274Sbostic 	free((char *) wtmp);
182*41274Sbostic }
183*41274Sbostic #endif NOWORM
184