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