hack.worm.c 7.4 KB

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