Make sure we look at the correct sub op codes when
[dragonfly.git] / games / hack / hack.worm.c
blob1db4e7c6b37bd7874fb8c7d516630ec42a29fe9b
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.worm.c - version 1.0.2 */
3 /* $FreeBSD: src/games/hack/hack.worm.c,v 1.4 1999/11/16 10:26:38 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.worm.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $ */
6 #include "hack.h"
7 #ifndef NOWORM
9 struct wseg *wsegs[32]; /* linked list, tail first */
10 struct wseg *wheads[32];
11 long wgrowtime[32];
13 static void remseg(struct wseg *);
15 bool
16 getwn(struct monst *mtmp)
18 int tmp;
19 for(tmp=1; tmp<32; tmp++) if(!wsegs[tmp]) {
20 mtmp->wormno = tmp;
21 return(1);
23 return(0); /* level infested with worms */
26 /* called to initialize a worm unless cut in half */
27 void
28 initworm(struct monst *mtmp)
30 struct wseg *wtmp;
31 int tmp = mtmp->wormno;
32 if(!tmp) return;
33 wheads[tmp] = wsegs[tmp] = wtmp = newseg();
34 wgrowtime[tmp] = 0;
35 wtmp->wx = mtmp->mx;
36 wtmp->wy = mtmp->my;
37 wtmp->nseg = 0;
40 void
41 worm_move(struct monst *mtmp)
43 struct wseg *wtmp, *whd;
44 int tmp = mtmp->wormno;
45 wtmp = newseg();
46 wtmp->wx = mtmp->mx;
47 wtmp->wy = mtmp->my;
48 wtmp->nseg = 0;
49 (whd = wheads[tmp])->nseg = wtmp;
50 wheads[tmp] = wtmp;
51 if(cansee(whd->wx,whd->wy)){
52 unpmon(mtmp);
53 atl(whd->wx, whd->wy, '~');
54 whd->wdispl = 1;
55 } else whd->wdispl = 0;
56 if(wgrowtime[tmp] <= moves) {
57 if(!wgrowtime[tmp]) wgrowtime[tmp] = moves + rnd(5);
58 else wgrowtime[tmp] += 2+rnd(15);
59 mtmp->mhpmax += 3;
60 mtmp->mhp += 3;
61 return;
63 whd = wsegs[tmp];
64 wsegs[tmp] = whd->nseg;
65 remseg(whd);
68 void
69 worm_nomove(struct monst *mtmp)
71 int tmp;
72 struct wseg *wtmp;
73 tmp = mtmp->wormno;
74 wtmp = wsegs[tmp];
75 if(wtmp == wheads[tmp]) return;
76 if(wtmp == 0 || wtmp->nseg == 0) panic("worm_nomove?");
77 wsegs[tmp] = wtmp->nseg;
78 remseg(wtmp);
79 mtmp->mhp -= 3; /* mhpmax not changed ! */
82 void
83 wormdead(struct monst *mtmp)
85 int tmp = mtmp->wormno;
86 struct wseg *wtmp, *wtmp2;
87 if(!tmp) return;
88 mtmp->wormno = 0;
89 for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
90 wtmp2 = wtmp->nseg;
91 remseg(wtmp);
93 wsegs[tmp] = 0;
96 void
97 wormhit(struct monst *mtmp)
99 int tmp = mtmp->wormno;
100 struct wseg *wtmp;
101 if(!tmp) return; /* worm without tail */
102 for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
103 hitu(mtmp,1);
106 void
107 wormsee(unsigned int tmp)
109 struct wseg *wtmp = wsegs[tmp];
110 if(!wtmp) panic("wormsee: wtmp==0");
111 for(; wtmp->nseg; wtmp = wtmp->nseg)
112 if(!cansee(wtmp->wx,wtmp->wy) && wtmp->wdispl){
113 newsym(wtmp->wx, wtmp->wy);
114 wtmp->wdispl = 0;
118 void
119 pwseg(struct wseg *wtmp)
121 if(!wtmp->wdispl){
122 atl(wtmp->wx, wtmp->wy, '~');
123 wtmp->wdispl = 1;
127 void
128 cutworm(struct monst *mtmp, xchar x, xchar y, uchar weptyp)
129 /* weptyp: uwep->otyp or 0 */
131 struct wseg *wtmp, *wtmp2;
132 struct monst *mtmp2;
133 int tmp,tmp2;
134 if(mtmp->mx == x && mtmp->my == y) return; /* hit headon */
136 /* cutting goes best with axe or sword */
137 tmp = rnd(20);
138 if(weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
139 weptyp == AXE) tmp += 5;
140 if(tmp < 12) return;
142 /* if tail then worm just loses a tail segment */
143 tmp = mtmp->wormno;
144 wtmp = wsegs[tmp];
145 if(wtmp->wx == x && wtmp->wy == y){
146 wsegs[tmp] = wtmp->nseg;
147 remseg(wtmp);
148 return;
151 /* cut the worm in two halves */
152 mtmp2 = newmonst(0);
153 *mtmp2 = *mtmp;
154 mtmp2->mxlth = mtmp2->mnamelth = 0;
156 /* sometimes the tail end dies */
157 if(rn2(3) || !getwn(mtmp2)){
158 monfree(mtmp2);
159 tmp2 = 0;
160 } else {
161 tmp2 = mtmp2->wormno;
162 wsegs[tmp2] = wsegs[tmp];
163 wgrowtime[tmp2] = 0;
165 do {
166 if(wtmp->nseg->wx == x && wtmp->nseg->wy == y){
167 if(tmp2) wheads[tmp2] = wtmp;
168 wsegs[tmp] = wtmp->nseg->nseg;
169 remseg(wtmp->nseg);
170 wtmp->nseg = 0;
171 if(tmp2){
172 pline("You cut the worm in half.");
173 mtmp2->mhpmax = mtmp2->mhp =
174 d(mtmp2->data->mlevel, 8);
175 mtmp2->mx = wtmp->wx;
176 mtmp2->my = wtmp->wy;
177 mtmp2->nmon = fmon;
178 fmon = mtmp2;
179 pmon(mtmp2);
180 } else {
181 pline("You cut off part of the worm's tail.");
182 remseg(wtmp);
184 mtmp->mhp /= 2;
185 return;
187 wtmp2 = wtmp->nseg;
188 if(!tmp2) remseg(wtmp);
189 wtmp = wtmp2;
190 } while(wtmp->nseg);
191 panic("Cannot find worm segment");
194 static void
195 remseg(struct wseg *wtmp)
197 if(wtmp->wdispl)
198 newsym(wtmp->wx, wtmp->wy);
199 free((char *) wtmp);
201 #endif /* NOWORM */