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