xref: /openbsd/games/hack/hack.worm.c (revision aed906e4)
1 /*	$OpenBSD: hack.worm.c,v 1.8 2016/01/09 18:33:15 mestre 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 
66 #include "hack.h"
67 
68 #ifndef NOWORM
69 struct wseg *wsegs[32];	/* linked list, tail first */
70 struct wseg *wheads[32];
71 long wgrowtime[32];
72 
73 static void remseg(struct wseg *);
74 
75 int
getwn(struct monst * mtmp)76 getwn(struct monst *mtmp)
77 {
78 	int tmp;
79 
80 	for(tmp=1; tmp<32; tmp++) if(!wsegs[tmp]) {
81 		mtmp->wormno = tmp;
82 		return(1);
83 	}
84 	return(0);	/* level infested with worms */
85 }
86 
87 /* called to initialize a worm unless cut in half */
88 void
initworm(struct monst * mtmp)89 initworm(struct monst *mtmp)
90 {
91 	struct wseg *wtmp;
92 	int tmp = mtmp->wormno;
93 
94 	if(!tmp) return;
95 	wheads[tmp] = wsegs[tmp] = wtmp = newseg();
96 	wgrowtime[tmp] = 0;
97 	wtmp->wx = mtmp->mx;
98 	wtmp->wy = mtmp->my;
99 /*	wtmp->wdispl = 0; */
100 	wtmp->nseg = 0;
101 }
102 
103 void
worm_move(struct monst * mtmp)104 worm_move(struct monst *mtmp)
105 {
106 	struct wseg *wtmp, *whd;
107 	int tmp = mtmp->wormno;
108 
109 	wtmp = newseg();
110 	wtmp->wx = mtmp->mx;
111 	wtmp->wy = mtmp->my;
112 	wtmp->nseg = 0;
113 /*	wtmp->wdispl = 0; */
114 	(whd = wheads[tmp])->nseg = wtmp;
115 	wheads[tmp] = wtmp;
116 	if(cansee(whd->wx,whd->wy)){
117 		unpmon(mtmp);
118 		atl(whd->wx, whd->wy, '~');
119 		whd->wdispl = 1;
120 	} else	whd->wdispl = 0;
121 	if(wgrowtime[tmp] <= moves) {
122 		if(!wgrowtime[tmp]) wgrowtime[tmp] = moves + rnd(5);
123 		else wgrowtime[tmp] += 2+rnd(15);
124 		mtmp->mhpmax += 3;
125 		mtmp->mhp += 3;
126 		return;
127 	}
128 	whd = wsegs[tmp];
129 	wsegs[tmp] = whd->nseg;
130 	remseg(whd);
131 }
132 
133 void
worm_nomove(struct monst * mtmp)134 worm_nomove(struct monst *mtmp)
135 {
136 	int tmp;
137 	struct wseg *wtmp;
138 
139 	tmp = mtmp->wormno;
140 	wtmp = wsegs[tmp];
141 	if(wtmp == wheads[tmp]) return;
142 	if(wtmp == 0 || wtmp->nseg == 0) panic("worm_nomove?");
143 	wsegs[tmp] = wtmp->nseg;
144 	remseg(wtmp);
145 	mtmp->mhp -= 3;	/* mhpmax not changed ! */
146 }
147 
148 void
wormdead(struct monst * mtmp)149 wormdead(struct monst *mtmp)
150 {
151 	int tmp = mtmp->wormno;
152 	struct wseg *wtmp, *wtmp2;
153 
154 	if(!tmp) return;
155 	mtmp->wormno = 0;
156 	for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
157 		wtmp2 = wtmp->nseg;
158 		remseg(wtmp);
159 	}
160 	wsegs[tmp] = 0;
161 }
162 
163 void
wormhit(struct monst * mtmp)164 wormhit(struct monst *mtmp)
165 {
166 	int tmp = mtmp->wormno;
167 	struct wseg *wtmp;
168 
169 	if(!tmp) return;	/* worm without tail */
170 	for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp->nseg)
171 		(void) hitu(mtmp,1);
172 }
173 
174 void
wormsee(unsigned tmp)175 wormsee(unsigned tmp)
176 {
177 	struct wseg *wtmp = wsegs[tmp];
178 
179 	if(!wtmp) panic("wormsee: wtmp==0");
180 	for(; wtmp->nseg; wtmp = wtmp->nseg)
181 		if(!cansee(wtmp->wx,wtmp->wy) && wtmp->wdispl){
182 			newsym(wtmp->wx, wtmp->wy);
183 			wtmp->wdispl = 0;
184 		}
185 }
186 
187 void
pwseg(struct wseg * wtmp)188 pwseg(struct wseg *wtmp)
189 {
190 	if(!wtmp->wdispl){
191 		atl(wtmp->wx, wtmp->wy, '~');
192 		wtmp->wdispl = 1;
193 	}
194 }
195 
196 /* uchar weptyp;		uwep->otyp or 0 */
197 void
cutworm(struct monst * mtmp,xchar x,xchar y,uchar weptyp)198 cutworm(struct monst *mtmp, xchar x, xchar y, uchar weptyp)
199 {
200 	struct wseg *wtmp, *wtmp2;
201 	struct monst *mtmp2;
202 	int tmp,tmp2;
203 	if(mtmp->mx == x && mtmp->my == y) return;	/* hit headon */
204 
205 	/* cutting goes best with axe or sword */
206 	tmp = rnd(20);
207 	if(weptyp == LONG_SWORD || weptyp == TWO_HANDED_SWORD ||
208 		weptyp == AXE) tmp += 5;
209 	if(tmp < 12) return;
210 
211 	/* if tail then worm just loses a tail segment */
212 	tmp = mtmp->wormno;
213 	wtmp = wsegs[tmp];
214 	if(wtmp->wx == x && wtmp->wy == y){
215 		wsegs[tmp] = wtmp->nseg;
216 		remseg(wtmp);
217 		return;
218 	}
219 
220 	/* cut the worm in two halves */
221 	mtmp2 = newmonst(0);
222 	*mtmp2 = *mtmp;
223 	mtmp2->mxlth = mtmp2->mnamelth = 0;
224 
225 	/* sometimes the tail end dies */
226 	if(rn2(3) || !getwn(mtmp2)){
227 		monfree(mtmp2);
228 		tmp2 = 0;
229 	} else {
230 		tmp2 = mtmp2->wormno;
231 		wsegs[tmp2] = wsegs[tmp];
232 		wgrowtime[tmp2] = 0;
233 	}
234 	do {
235 		if(wtmp->nseg->wx == x && wtmp->nseg->wy == y){
236 			if(tmp2) wheads[tmp2] = wtmp;
237 			wsegs[tmp] = wtmp->nseg->nseg;
238 			remseg(wtmp->nseg);
239 			wtmp->nseg = 0;
240 			if(tmp2){
241 				pline("You cut the worm in half.");
242 				mtmp2->mhpmax = mtmp2->mhp =
243 					d(mtmp2->data->mlevel, 8);
244 				mtmp2->mx = wtmp->wx;
245 				mtmp2->my = wtmp->wy;
246 				mtmp2->nmon = fmon;
247 				fmon = mtmp2;
248 				pmon(mtmp2);
249 			} else {
250 				pline("You cut off part of the worm's tail.");
251 				remseg(wtmp);
252 			}
253 			mtmp->mhp /= 2;
254 			return;
255 		}
256 		wtmp2 = wtmp->nseg;
257 		if(!tmp2) remseg(wtmp);
258 		wtmp = wtmp2;
259 	} while(wtmp->nseg);
260 	panic("Cannot find worm segment");
261 }
262 
263 static void
remseg(struct wseg * wtmp)264 remseg(struct wseg *wtmp)
265 {
266 	if(wtmp->wdispl)
267 		newsym(wtmp->wx, wtmp->wy);
268 	free(wtmp);
269 }
270 #endif /* NOWORM */
271