xref: /openbsd-src/games/hack/hack.worm.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: hack.worm.c,v 1.6 2009/10/27 23:59:25 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5  * Amsterdam
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * - Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * - Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * - Neither the name of the Stichting Centrum voor Wiskunde en
20  * Informatica, nor the names of its contributors may be used to endorse or
21  * promote products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <stdlib.h>
65 #include "hack.h"
66 #ifndef NOWORM
67 struct wseg *wsegs[32];	/* linked list, tail first */
68 struct wseg *wheads[32];
69 long wgrowtime[32];
70 
71 static void remseg(struct wseg *);
72 
73 int
74 getwn(struct monst *mtmp)
75 {
76 	int tmp;
77 
78 	for(tmp=1; tmp<32; tmp++) if(!wsegs[tmp]) {
79 		mtmp->wormno = tmp;
80 		return(1);
81 	}
82 	return(0);	/* level infested with worms */
83 }
84 
85 /* called to initialize a worm unless cut in half */
86 void
87 initworm(struct monst *mtmp)
88 {
89 	struct wseg *wtmp;
90 	int tmp = mtmp->wormno;
91 
92 	if(!tmp) return;
93 	wheads[tmp] = wsegs[tmp] = wtmp = newseg();
94 	wgrowtime[tmp] = 0;
95 	wtmp->wx = mtmp->mx;
96 	wtmp->wy = mtmp->my;
97 /*	wtmp->wdispl = 0; */
98 	wtmp->nseg = 0;
99 }
100 
101 void
102 worm_move(struct monst *mtmp)
103 {
104 	struct wseg *wtmp, *whd;
105 	int tmp = mtmp->wormno;
106 
107 	wtmp = newseg();
108 	wtmp->wx = mtmp->mx;
109 	wtmp->wy = mtmp->my;
110 	wtmp->nseg = 0;
111 /*	wtmp->wdispl = 0; */
112 	(whd = wheads[tmp])->nseg = wtmp;
113 	wheads[tmp] = wtmp;
114 	if(cansee(whd->wx,whd->wy)){
115 		unpmon(mtmp);
116 		atl(whd->wx, whd->wy, '~');
117 		whd->wdispl = 1;
118 	} else	whd->wdispl = 0;
119 	if(wgrowtime[tmp] <= moves) {
120 		if(!wgrowtime[tmp]) wgrowtime[tmp] = moves + rnd(5);
121 		else wgrowtime[tmp] += 2+rnd(15);
122 		mtmp->mhpmax += 3;
123 		mtmp->mhp += 3;
124 		return;
125 	}
126 	whd = wsegs[tmp];
127 	wsegs[tmp] = whd->nseg;
128 	remseg(whd);
129 }
130 
131 void
132 worm_nomove(struct monst *mtmp)
133 {
134 	int tmp;
135 	struct wseg *wtmp;
136 
137 	tmp = mtmp->wormno;
138 	wtmp = wsegs[tmp];
139 	if(wtmp == wheads[tmp]) return;
140 	if(wtmp == 0 || wtmp->nseg == 0) panic("worm_nomove?");
141 	wsegs[tmp] = wtmp->nseg;
142 	remseg(wtmp);
143 	mtmp->mhp -= 3;	/* mhpmax not changed ! */
144 }
145 
146 void
147 wormdead(struct monst *mtmp)
148 {
149 	int tmp = mtmp->wormno;
150 	struct wseg *wtmp, *wtmp2;
151 
152 	if(!tmp) return;
153 	mtmp->wormno = 0;
154 	for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
155 		wtmp2 = wtmp->nseg;
156 		remseg(wtmp);
157 	}
158 	wsegs[tmp] = 0;
159 }
160 
161 void
162 wormhit(struct monst *mtmp)
163 {
164 	int tmp = mtmp->wormno;
165 	struct wseg *wtmp;
166 
167 	if(!tmp) return;	/* worm without tail */
168 	for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
169 		(void) hitu(mtmp,1);
170 }
171 
172 void
173 wormsee(unsigned tmp)
174 {
175 	struct wseg *wtmp = wsegs[tmp];
176 
177 	if(!wtmp) panic("wormsee: wtmp==0");
178 	for(; wtmp->nseg; wtmp = wtmp->nseg)
179 		if(!cansee(wtmp->wx,wtmp->wy) && wtmp->wdispl){
180 			newsym(wtmp->wx, wtmp->wy);
181 			wtmp->wdispl = 0;
182 		}
183 }
184 
185 void
186 pwseg(struct wseg *wtmp)
187 {
188 	if(!wtmp->wdispl){
189 		atl(wtmp->wx, wtmp->wy, '~');
190 		wtmp->wdispl = 1;
191 	}
192 }
193 
194 /* uchar weptyp;		uwep->otyp or 0 */
195 void
196 cutworm(struct monst *mtmp, xchar x, xchar y, uchar weptyp)
197 {
198 	struct wseg *wtmp, *wtmp2;
199 	struct monst *mtmp2;
200 	int tmp,tmp2;
201 	if(mtmp->mx == x && mtmp->my == y) return;	/* hit headon */
202 
203 	/* cutting goes best with axe or sword */
204 	tmp = rnd(20);
205 	if(weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
206 		weptyp == AXE) tmp += 5;
207 	if(tmp < 12) return;
208 
209 	/* if tail then worm just loses a tail segment */
210 	tmp = mtmp->wormno;
211 	wtmp = wsegs[tmp];
212 	if(wtmp->wx == x && wtmp->wy == y){
213 		wsegs[tmp] = wtmp->nseg;
214 		remseg(wtmp);
215 		return;
216 	}
217 
218 	/* cut the worm in two halves */
219 	mtmp2 = newmonst(0);
220 	*mtmp2 = *mtmp;
221 	mtmp2->mxlth = mtmp2->mnamelth = 0;
222 
223 	/* sometimes the tail end dies */
224 	if(rn2(3) || !getwn(mtmp2)){
225 		monfree(mtmp2);
226 		tmp2 = 0;
227 	} else {
228 		tmp2 = mtmp2->wormno;
229 		wsegs[tmp2] = wsegs[tmp];
230 		wgrowtime[tmp2] = 0;
231 	}
232 	do {
233 		if(wtmp->nseg->wx == x && wtmp->nseg->wy == y){
234 			if(tmp2) wheads[tmp2] = wtmp;
235 			wsegs[tmp] = wtmp->nseg->nseg;
236 			remseg(wtmp->nseg);
237 			wtmp->nseg = 0;
238 			if(tmp2){
239 				pline("You cut the worm in half.");
240 				mtmp2->mhpmax = mtmp2->mhp =
241 					d(mtmp2->data->mlevel, 8);
242 				mtmp2->mx = wtmp->wx;
243 				mtmp2->my = wtmp->wy;
244 				mtmp2->nmon = fmon;
245 				fmon = mtmp2;
246 				pmon(mtmp2);
247 			} else {
248 				pline("You cut off part of the worm's tail.");
249 				remseg(wtmp);
250 			}
251 			mtmp->mhp /= 2;
252 			return;
253 		}
254 		wtmp2 = wtmp->nseg;
255 		if(!tmp2) remseg(wtmp);
256 		wtmp = wtmp2;
257 	} while(wtmp->nseg);
258 	panic("Cannot find worm segment");
259 }
260 
261 static void
262 remseg(struct wseg *wtmp)
263 {
264 	if(wtmp->wdispl)
265 		newsym(wtmp->wx, wtmp->wy);
266 	free((char *) wtmp);
267 }
268 #endif /* NOWORM */
269