Source:NetHack 5.0.0/src/worm.c
Jump to navigation
Jump to search
Below is the full text to worm.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/worm.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 5.0 worm.c $NHDT-Date: 1652689653 2022/05/16 08:27:33 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.56 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2009. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. 8. #define newseg() (struct wseg *) alloc(sizeof (struct wseg)) 9. #define dealloc_seg(wseg) free((genericptr_t) (wseg)) 10. 11. /* worm segment structure */ 12. struct wseg { 13. struct wseg *nseg; 14. coordxy wx, wy; /* the segment's position */ 15. }; 16. 17. #ifndef SFCTOOL 18. staticfn void toss_wsegs(struct wseg *, boolean) NO_NNARGS; 19. staticfn void shrink_worm(int); 20. 21. #if 0 22. staticfn void random_dir(int, int, int *, int *); 23. #endif 24. staticfn struct wseg *create_worm_tail(int); /* may return NULL */ 25. #endif /* !SFCTOOL */ 26. 27. /* Description of long worm implementation. 28. * 29. * Each monst struct of the head of a tailed worm has a wormno set to 30. * 1 <= wormno < MAX_NUM_WORMS 31. * If wormno == 0 this does not mean that the monster is not a worm, 32. * it just means that the monster does not have a long worm tail. 33. * 34. * The actual segments of a worm are not full-blown monst structs. 35. * They are small wseg structs, and their position in the levels.monsters[][] 36. * array is held by the monst struct of the head of the worm. This makes 37. * things like probing and hit point bookkeeping much easier. 38. * 39. * The segments of the long worms on a level are kept as an array of 40. * singly threaded linked lists. The wormno variable is used as an index 41. * for these segment arrays. 42. * 43. * wtails: The first (starting struct) of a linked list. This points 44. * to the tail (last) segment of the worm. 45. * 46. * wheads: The last (end) of a linked list of segments. This points to 47. * the segment that is at the same position as the real monster 48. * (the head). Note that the segment that wheads[wormno] points 49. * to is not displayed. It is simply there to keep track of 50. * where the head came from, so that worm movement and display 51. * are simplified later. 52. * Keeping the head segment of the worm at the end of the list 53. * of tail segments is an endless source of confusion, but it is 54. * necessary. 55. * From now on, we will use "start" and "end" to refer to the 56. * linked list and "head" and "tail" to refer to the worm. 57. * 58. * One final worm array is: 59. * 60. * wgrowtime: This tells us when to add another segment to the worm. 61. * 62. * When a worm is moved, we add a new segment at the head, and delete the 63. * segment at the tail (unless we want it to grow). This new head segment is 64. * located in the same square as the actual head of the worm. If we want 65. * to grow the worm, we don't delete the tail segment, and we give the worm 66. * extra hit points, which possibly go into its maximum. 67. * 68. * Non-moving worms (worm_nomove) are assumed to be surrounded by their own 69. * tail, and, thus, shrink instead of grow (as their tails keep going while 70. * their heads are stopped short). In this case, we delete the last tail 71. * segment, and remove hit points from the worm. 72. */ 73. 74. /* restart: worm removal resets these so they don't need to be incorporated 75. into 'struct instance_globals g' for potential reinitialization provided 76. that old game disposes of monsters properly before starting a new one */ 77. static struct wseg *wheads[MAX_NUM_WORMS] = DUMMY, 78. *wtails[MAX_NUM_WORMS] = DUMMY; 79. static long wgrowtime[MAX_NUM_WORMS] = DUMMY; 80. 81. #ifndef SFCTOOL 82. /* 83. * get_wormno() 84. * 85. * Find an unused worm tail slot and return the index. A zero means that 86. * there are no slots available. This means that the worm head can exist, 87. * it just cannot ever grow a tail. 88. * 89. * It, also, means that there is an optimization to made. The [0] positions 90. * of the arrays are never used. Meaning, we really *could* have one more 91. * tailed worm on the level, or use a smaller array (using wormno - 1). 92. * 93. * Implementation is left to the interested hacker. 94. */ 95. int 96. get_wormno(void) 97. { 98. int new_wormno = 1; 99. 100. while (new_wormno < MAX_NUM_WORMS) { 101. if (!wheads[new_wormno]) 102. return new_wormno; /* found empty wtails[] slot at new_wormno */ 103. new_wormno++; 104. } 105. return 0; /* level infested with worms */ 106. } 107. 108. /* 109. * initworm() 110. * 111. * Use if (mon->wormno = get_wormno()) before calling this function! 112. * 113. * Initialize the worm entry. This will set up the worm grow time, and 114. * create and initialize the dummy segment for wheads[] and wtails[]. 115. * 116. * If the worm has no tail (ie get_wormno() fails) then this function 117. * need not be called. 118. */ 119. void 120. initworm(struct monst *worm, int wseg_count) 121. { 122. struct wseg *seg, *new_tail = create_worm_tail(wseg_count); 123. int wnum = worm->wormno; 124. 125. if (new_tail) { 126. wtails[wnum] = new_tail; 127. for (seg = new_tail; seg->nseg; seg = seg->nseg) 128. continue; 129. wheads[wnum] = seg; 130. } else { 131. wtails[wnum] = wheads[wnum] = seg = newseg(); 132. seg->nseg = (struct wseg *) 0; 133. } 134. seg->wx = worm->mx; 135. seg->wy = worm->my; 136. wgrowtime[wnum] = 0L; 137. } 138. 139. /* 140. * toss_wsegs() 141. * 142. * Get rid of all worm segments on and following the given pointer curr. 143. * The display may or may not need to be updated as we free the segments. 144. */ 145. staticfn void 146. toss_wsegs(struct wseg *curr, boolean display_update) 147. { 148. struct wseg *nxtseg; 149. 150. while (curr) { 151. nxtseg = curr->nseg; 152. 153. /* remove from level.monsters[][]; 154. need to check curr->wx for genocided while migrating_mon */ 155. if (curr->wx) { 156. remove_monster(curr->wx, curr->wy); 157. 158. /* update screen before deallocation */ 159. if (display_update) 160. newsym(curr->wx, curr->wy); 161. } 162. 163. /* free memory used by the segment */ 164. dealloc_seg(curr); 165. curr = nxtseg; 166. } 167. } 168. 169. /* 170. * shrink_worm() 171. * 172. * Remove the tail segment of the worm (the starting segment of the list). 173. */ 174. staticfn void 175. shrink_worm(int wnum) /* worm number */ 176. { 177. struct wseg *seg; 178. 179. if (wtails[wnum] == wheads[wnum]) 180. return; /* no tail */ 181. 182. seg = wtails[wnum]; 183. wtails[wnum] = seg->nseg; 184. seg->nseg = (struct wseg *) 0; 185. toss_wsegs(seg, TRUE); 186. } 187. 188. /* 189. * worm_move() 190. * 191. * Check for mon->wormno before calling this function! 192. * 193. * Move the worm. Maybe grow. 194. */ 195. void 196. worm_move(struct monst *worm) 197. { 198. struct wseg *seg, *new_seg; /* new segment */ 199. int wnum = worm->wormno; /* worm number */ 200. 201. /* 202. * Place a segment at the old worm head. The head has already moved. 203. */ 204. seg = wheads[wnum]; 205. place_worm_seg(worm, seg->wx, seg->wy); 206. newsym(seg->wx, seg->wy); /* display the new segment */ 207. 208. /* 209. * Create a new dummy segment head and place it at the end of the list. 210. */ 211. new_seg = newseg(); 212. new_seg->wx = worm->mx; 213. new_seg->wy = worm->my; 214. new_seg->nseg = (struct wseg *) 0; 215. seg->nseg = new_seg; /* attach it to the end of the list */ 216. wheads[wnum] = new_seg; /* move the end pointer */ 217. 218. if (wgrowtime[wnum] <= svm.moves) { 219. int whplimit, whpcap, prev_mhp, wsegs = count_wsegs(worm); 220. 221. /* first set up for the next time to grow */ 222. if (!wgrowtime[wnum]) { 223. /* new worm; usually grow a tail segment on its next turn */ 224. wgrowtime[wnum] = svm.moves + rnd(5); 225. } else { 226. int mmove = mcalcmove(worm, FALSE), 227. /* prior to 5.0.0,, next-grow increment was 3..17 but since 228. it got checked every 4th turn when the speed 3 worm got 229. to move, it was effectively 0..5; also, its usage was 230. 'wgrowtime += incr', so often 'wgrowtime' would be 231. exceeded by 'moves' on consecutive turns for the worm, 232. resulting in an excessively rapid growth cycle */ 233. incr = rn1(10, 2); /* 2..12; after adjusting for long worn 234. * speed of 3, effective value is 8..48 */ 235. 236. incr = (incr * NORMAL_SPEED) / max(mmove, 1); 237. wgrowtime[wnum] = svm.moves + incr; 238. } 239. 240. /* increase HP based on number of segments; if it has shrunk, it 241. won't gain new HP until regaining previous peak segment count; 242. when wounded (whether from damage or from shrinking), the HP 243. which might have been 'new' will heal */ 244. whplimit = !worm->m_lev ? 4 : (8 * (int) worm->m_lev); 245. /* note: wsegs includes the hidden segment co-located with the head */ 246. if (wsegs > 33) 247. whplimit += 2 * (wsegs - 33), wsegs = 33; 248. if (wsegs > 22) 249. whplimit += 4 * (wsegs - 22), wsegs = 22; 250. if (wsegs > 11) 251. whplimit += 6 * (wsegs - 11), wsegs = 11; 252. whplimit += 8 * wsegs; 253. if (whplimit > MHPMAX) 254. whplimit = MHPMAX; 255. 256. prev_mhp = worm->mhp; 257. worm->mhp += d(2, 2); /* 2..4, average 3 */ 258. whpcap = max(whplimit, worm->mhpmax); 259. if (worm->mhp < whpcap) { 260. /* can't exceed segment-derived limit unless level increase after 261. peak tail growth has already done so; when that isn't the case, 262. if segment growth exceeds current max HP then increase it */ 263. if (worm->mhp > whplimit) 264. worm->mhp = max(prev_mhp, whplimit); 265. if (worm->mhp > worm->mhpmax) 266. worm->mhpmax = worm->mhp; 267. } else { 268. if (worm->mhp > worm->mhpmax) 269. worm->mhp = worm->mhpmax; 270. } 271. } else { 272. /* The worm doesn't grow, so the last segment goes away. 273. (Done after inserting an extra segment at the head, so it 274. isn't getting smaller here, just changing location without 275. having to move any of the intermediate segments.) */ 276. shrink_worm(wnum); 277. } 278. } 279. 280. /* 281. * worm_nomove() 282. * 283. * Check for mon->wormno before calling this function! 284. * 285. * The worm doesn't move, so it should shrink. 286. */ 287. void 288. worm_nomove(struct monst *worm) 289. { 290. shrink_worm((int) worm->wormno); /* shrink */ 291. 292. if (worm->mhp > count_wsegs(worm)) { 293. worm->mhp -= d(2, 2); /* 2..4, average 3; note: mhpmax not changed! */ 294. if (worm->mhp < 1) 295. worm->mhp = 1; 296. } 297. } 298. 299. /* 300. * wormgone() 301. * 302. * Kill a worm tail. Also takes the head off the map. Caller needs to 303. * keep track of what its coordinates were if planning to put it back. 304. * 305. * Should only be called when mon->wormno is non-zero. 306. */ 307. void 308. wormgone(struct monst *worm) 309. { 310. int wnum = worm->wormno; 311. 312. if (!wnum) /* note: continuing with wnum==0 runs to completion */ 313. impossible("wormgone: wormno is 0"); 314. 315. worm->wormno = 0; /* still a long worm but doesn't grow/shrink anymore */ 316. /* 317. * This will also remove the real monster (ie 'w') from the its 318. * position in level.monsters[][]. (That happens when removing 319. * the hidden tail segment which is co-located with the head.) 320. */ 321. toss_wsegs(wtails[wnum], TRUE); 322. 323. wheads[wnum] = wtails[wnum] = (struct wseg *) 0; 324. wgrowtime[wnum] = 0L; 325. 326. /* we don't expect to encounter this here but check for it anyway; 327. when a long worm gets created by a polymorph zap, it gets flagged 328. with MCORPSENM()==PM_LONG_WORM so that the same zap won't trigger 329. another polymorph if it hits the new tail */ 330. if (worm->data == &mons[PM_LONG_WORM] && has_mcorpsenm(worm)) 331. MCORPSENM(worm) = NON_PM; /* no longer polymorph-proof */ 332. } 333. 334. /* 335. * wormhitu() 336. * 337. * Check for mon->wormno before calling this function! 338. * 339. * If the hero is near any part of the worm, the worm will try to attack. 340. * Returns 1 if the worm dies (poly'd hero with passive counter-attack) 341. * or 0 if it doesn't. 342. */ 343. int 344. wormhitu(struct monst *worm) 345. { 346. int wnum = worm->wormno; 347. struct wseg *seg; 348. 349. /* This does not work right now because mattacku() thinks that the head 350. * is out of range of the player. We might try to kludge, and bring 351. * the head within range for a tiny moment, but this needs a bit more 352. * looking at before we decide to do this. 353. * 354. * Head has already had a chance to attack, so the dummy tail segment 355. * sharing its location should be skipped. 356. */ 357. for (seg = wtails[wnum]; seg != wheads[wnum]; seg = seg->nseg) 358. if (distu(seg->wx, seg->wy) < 3) 359. if (mattacku(worm)) 360. return 1; /* your passive ability killed the worm */ 361. return 0; 362. } 363. 364. /* cutworm() 365. * 366. * Check for mon->wormno before calling this function! 367. * 368. * When hitting a worm (worm) at position x, y, with a weapon (weap), 369. * there is a chance that the worm will be cut in half, and a chance 370. * that both halves will survive. 371. */ 372. void 373. cutworm(struct monst *worm, coordxy x, coordxy y, 374. boolean cuttier) /* hit is by wielded blade or axe or by thrown axe */ 375. { 376. struct wseg *curr, *new_tail; 377. struct monst *new_worm; 378. int wnum = worm->wormno; 379. int cut_chance, new_wnum; 380. 381. if (!wnum) 382. return; /* bullet-proofing */ 383. 384. if (x == worm->mx && y == worm->my) 385. return; /* hit on head */ 386. 387. /* cutting goes best with a cuttier weapon */ 388. cut_chance = rnd(20); /* Normally 1-16 does not cut, 17-20 does, */ 389. if (cuttier) 390. cut_chance += 10; /* with a blade 1- 6 does not cut, 7-20 does. */ 391. 392. if (cut_chance < 17) 393. return; /* not good enough */ 394. 395. /* Find the segment that was attacked. */ 396. curr = wtails[wnum]; 397. 398. while ((curr->wx != x) || (curr->wy != y)) { 399. curr = curr->nseg; 400. if (!curr) { 401. impossible("cutworm: no segment at (%d,%d)", (int) x, (int) y); 402. return; 403. } 404. } 405. 406. /* If this is the tail segment, then the worm just loses it. */ 407. if (curr == wtails[wnum]) { 408. shrink_worm(wnum); 409. return; 410. } 411. 412. /* 413. * Split the worm. The tail for the new worm is the old worm's tail. 414. * The tail for the old worm is the segment that follows "curr", 415. * and "curr" becomes the dummy segment under the new head. 416. */ 417. new_tail = wtails[wnum]; 418. wtails[wnum] = curr->nseg; 419. curr->nseg = (struct wseg *) 0; /* split the worm */ 420. 421. /* 422. * At this point, the old worm is correct. Any new worm will have 423. * its head at "curr" and its tail at "new_tail". The old worm 424. * must be at least level 3 in order to produce a new worm. 425. */ 426. new_worm = 0; 427. new_wnum = (worm->m_lev >= 3 && !rn2(3)) ? get_wormno() : 0; 428. if (new_wnum) { 429. remove_monster(x, y); /* clone_mon puts new head here */ 430. /* clone_mon() will fail if enough long worms have been 431. created to have them be marked as extinct or if the hit 432. that cut the current one has dropped it down to 1 HP */ 433. new_worm = clone_mon(worm, x, y); 434. } 435. 436. /* Sometimes the tail end dies. */ 437. if (!new_worm) { 438. place_worm_seg(worm, x, y); /* place the "head" segment back */ 439. if (svc.context.mon_moving) { 440. if (canspotmon(worm)) 441. pline("Part of %s tail has been cut off.", 442. s_suffix(mon_nam(worm))); 443. } else 444. You("cut part of the tail off of %s.", mon_nam(worm)); 445. toss_wsegs(new_tail, TRUE); 446. if (worm->mhp > 1) 447. worm->mhp /= 2; 448. return; 449. } 450. 451. new_worm->wormno = new_wnum; /* affix new worm number */ 452. new_worm->mcloned = 0; /* treat second worm as a normal monster */ 453. 454. /* Devalue the monster level of both halves of the worm. 455. Note: m_lev is always at least 3 in order to get this far. */ 456. worm->m_lev = max((unsigned) worm->m_lev - 2, 3); 457. new_worm->m_lev = worm->m_lev; 458. 459. /* Calculate the lower-level mhp; use <N>d8 for long worms. 460. Can't use newmonhp() here because it would reset m_lev. */ 461. new_worm->mhpmax = new_worm->mhp = d((int) new_worm->m_lev, 8); 462. worm->mhpmax = d((int) worm->m_lev, 8); /* new maxHP for old worm */ 463. if (worm->mhpmax < worm->mhp) 464. worm->mhp = worm->mhpmax; 465. 466. wtails[new_wnum] = new_tail; /* We've got all the info right now */ 467. wheads[new_wnum] = curr; /* so we can do this faster than */ 468. wgrowtime[new_wnum] = 0L; /* trying to call initworm(). */ 469. 470. /* Place the new monster at all the segment locations. */ 471. place_wsegs(new_worm, worm); 472. 473. if (svc.context.mon_moving) 474. pline("%s is cut in half.", Monnam(worm)); 475. else 476. You("cut %s in half.", mon_nam(worm)); 477. } 478. 479. /* 480. * see_wsegs() 481. * 482. * Refresh all of the segments of the given worm. This is only called 483. * from see_monster() in display.c or when a monster goes minvis. It 484. * is located here for modularity. 485. */ 486. void 487. see_wsegs(struct monst *worm) 488. { 489. struct wseg *curr = wtails[worm->wormno]; 490. 491. while (curr != wheads[worm->wormno]) { 492. newsym(curr->wx, curr->wy); 493. curr = curr->nseg; 494. } 495. } 496. 497. /* 498. * detect_wsegs() 499. * 500. * Display all of the segments of the given worm for detection. 501. */ 502. void 503. detect_wsegs(struct monst *worm, boolean use_detection_glyph) 504. { 505. int num; 506. struct wseg *curr = wtails[worm->wormno]; 507. int what_tail = what_mon(PM_LONG_WORM_TAIL, newsym_rn2); 508. 509. while (curr != wheads[worm->wormno]) { 510. num = use_detection_glyph ? detected_monnum_to_glyph(what_tail, 511. worm->female ? FEMALE : MALE) 512. : worm->mtame ? petnum_to_glyph(what_tail, 513. worm->female ? FEMALE : MALE) 514. : monnum_to_glyph(what_tail, 515. worm->female ? FEMALE : MALE); 516. show_glyph(curr->wx, curr->wy, num); 517. curr = curr->nseg; 518. } 519. } 520. 521. /* 522. * save_worm() 523. * 524. * Save the worm information for later use. The count is the number 525. * of segments, including the dummy. Called from save.c. 526. */ 527. void 528. save_worm(NHFILE *nhfp) 529. { 530. int i; 531. int count; 532. struct wseg *curr, *temp; 533. 534. if (update_file(nhfp)) { 535. for (i = 1; i < MAX_NUM_WORMS; i++) { 536. for (count = 0, curr = wtails[i]; curr; curr = curr->nseg) 537. count++; 538. /* Save number of segments */ 539. Sfo_int(nhfp, &count, "worm-segment_count"); 540. /* Save segment locations of the monster. */ 541. if (count) { 542. for (curr = wtails[i]; curr; curr = curr->nseg) { 543. Sfo_coordxy(nhfp, &(curr->wx), "worm-wx"); 544. Sfo_coordxy(nhfp, &(curr->wy), "worm-wy"); 545. } 546. } 547. } 548. for (i = 0; i < MAX_NUM_WORMS; ++i) 549. Sfo_long(nhfp, &wgrowtime[i], "worm-wgrowtime"); 550. } 551. 552. if (release_data(nhfp)) { 553. /* Free the segments only. savemonchn() will take care of the 554. * monsters. */ 555. for (i = 1; i < MAX_NUM_WORMS; i++) { 556. if (!(curr = wtails[i])) 557. continue; 558. 559. while (curr) { 560. temp = curr->nseg; 561. dealloc_seg(curr); /* free the segment */ 562. curr = temp; 563. } 564. wheads[i] = wtails[i] = (struct wseg *) 0; 565. wgrowtime[i] = 0L; 566. } 567. } 568. } 569. #endif /* !SFCTOOL */ 570. 571. /* 572. * rest_worm() 573. * 574. * Restore the worm information from the save file. Called from restore.c 575. */ 576. void 577. rest_worm(NHFILE *nhfp) 578. { 579. int i, j; 580. int count = 0; 581. struct wseg *curr, *temp; 582. 583. for (i = 1; i < MAX_NUM_WORMS; i++) { 584. Sfi_int(nhfp, &count, "worm-segment_count"); 585. 586. /* Get the segments. */ 587. for (curr = (struct wseg *) 0, j = 0; j < count; j++) { 588. temp = newseg(); 589. temp->nseg = (struct wseg *) 0; 590. Sfi_coordxy(nhfp, &(temp->wx), "worm-wx"); 591. Sfi_coordxy(nhfp, &(temp->wy), "worm-wy"); 592. if (curr) 593. curr->nseg = temp; 594. else 595. wtails[i] = temp; 596. curr = temp; 597. } 598. wheads[i] = curr; 599. } 600. for (i = 0; i < MAX_NUM_WORMS; ++i) { 601. Sfi_long(nhfp, &wgrowtime[i], "worm-wgrowtime"); 602. } 603. } 604. 605. #ifndef SFCTOOL 606. /* 607. * place_wsegs() 608. * 609. * Place the segments of the given worm. Called from restore.c 610. * and from replmon() in mon.c. 611. * If oldworm is not NULL, assumes the oldworm segments are on map 612. * in the same location as worm segments 613. */ 614. void 615. place_wsegs(struct monst *worm, struct monst *oldworm) 616. { 617. struct wseg *curr = wtails[worm->wormno]; 618. 619. while (curr != wheads[worm->wormno]) { 620. coordxy x = curr->wx, y = curr->wy; 621. struct monst *mtmp = m_at(x, y); 622. 623. if (oldworm && mtmp == oldworm) 624. remove_monster(x, y); 625. else if (mtmp) 626. impossible("placing worm seg <%d,%d> over another mon", x, y); 627. else if (oldworm) 628. impossible("replacing worm seg <%d,%d> on empty spot", x, y); 629. 630. place_worm_seg(worm, x, y); 631. curr = curr->nseg; 632. } 633. /* head segment is co-located with worm itself so not placed on the map */ 634. curr->wx = worm->mx, curr->wy = worm->my; 635. } 636. 637. /* called from mon_sanity_check(mon.c) */ 638. void 639. sanity_check_worm(struct monst *worm) 640. { 641. struct wseg *curr; 642. int wnum, x, y; 643. 644. if (!worm) { 645. impossible("worm_sanity: null monster!"); 646. return; 647. } 648. /* note: wormno can't be less than 0 (unsigned bit field) and can't 649. be greater that MAX_NUM_WORMS - 1 (which uses all available bits) 650. so checking for 0 is all we can manage for wormno validation; 651. since caller has already done that, this is rather pointless... */ 652. if (!worm->wormno) { 653. impossible("worm_sanity: not a worm!"); 654. return; 655. } 656. 657. wnum = worm->wormno; 658. if (!wtails[wnum] || !wheads[wnum]) { 659. impossible("wormno %d is set without proper tail", wnum); 660. return; 661. } 662. /* if worm is migrating, we can't check its segments against the map */ 663. if (!worm->mx) 664. return; 665. 666. curr = wtails[wnum]; 667. while (curr != wheads[wnum]) { 668. x = curr->wx, y = curr->wy; 669. if (!isok(x, y)) 670. impossible("worm seg not isok <%d,%d>", x, y); 671. else if (svl.level.monsters[x][y] != worm) 672. impossible("mon (%s) at seg location is not worm (%s)", 673. fmt_ptr((genericptr_t) svl.level.monsters[x][y]), 674. fmt_ptr((genericptr_t) worm)); 675. 676. curr = curr->nseg; 677. } 678. } 679. 680. /* called from mon_sanity_check(mon.c) */ 681. void 682. wormno_sanity_check(void) 683. { 684. #ifdef EXTRA_SANITY_CHECKS 685. struct wseg *seg; 686. int wh = 0, wt = 0; 687. 688. /* checking tail management, not a particular monster; since wormno==0 689. means 'not a worm', wheads[0] and wtails[0] should always be empty; 690. note: if erroneously non-Null, tail segment count will include the 691. extra segment for the worm's head that isn't shown on the map */ 692. for (seg = wheads[0]; seg; seg = seg->nseg) 693. ++wh; 694. for (seg = wtails[0]; seg; seg = seg->nseg) 695. ++wt; 696. if (wh || wt) { 697. impossible( 698. "phantom worm tail #0 [head=%s, %d segment%s; tail=%s, %d segment%s]", 699. fmt_ptr(wheads[0]), wh, plur(wh), 700. fmt_ptr(wtails[0]), wt, plur(wt)); 701. } 702. #endif /* EXTRA_SANITY_CHECKS */ 703. } 704. 705. /* 706. * remove_worm() 707. * 708. * This function is equivalent to the remove_monster #define in 709. * rm.h, only it will take the worm *and* tail out of the levels array. 710. * It does not get rid of (dealloc) the worm tail structures, and it does 711. * not remove the mon from the fmon chain. 712. */ 713. void 714. remove_worm(struct monst *worm) 715. { 716. struct wseg *curr = wtails[worm->wormno]; 717. 718. while (curr) { 719. if (curr->wx) { 720. remove_monster(curr->wx, curr->wy); 721. newsym(curr->wx, curr->wy); 722. curr->wx = 0; 723. } 724. curr = curr->nseg; 725. } 726. } 727. 728. /* 729. * place_worm_tail_randomly() 730. * 731. * Place a worm tail somewhere on a level behind the head. 732. * This routine essentially reverses the order of the wsegs from head 733. * to tail while placing them. 734. * x, and y are most likely the worm->mx, and worm->my, but don't *need* to 735. * be, if somehow the head is disjoint from the tail. 736. */ 737. void 738. place_worm_tail_randomly(struct monst *worm, coordxy x, coordxy y) 739. { 740. int wnum = worm->wormno; 741. struct wseg *curr = wtails[wnum]; 742. struct wseg *new_tail; 743. int ox = x, oy = y; 744. 745. if (wnum && (!wtails[wnum] || !wheads[wnum])) { 746. impossible("place_worm_tail_randomly: wormno is set without a tail!"); 747. return; 748. } 749. if (wtails[wnum] == wheads[wnum]) { 750. /* single segment, co-located with worm; 751. should either have same coordinates or have seg->wx==0 752. to indicate that it is not currently on the map */ 753. if (curr->wx && (curr->wx != worm->mx || curr->wy != worm->my)) { 754. impossible( 755. "place_worm_tail_randomly: tail segment at <%d,%d>, worm at <%d,%d>", 756. curr->wx, curr->wy, worm->mx, worm->my); 757. if (m_at(curr->wx, curr->wy) == worm) 758. remove_monster(curr->wx, curr->wy); 759. } 760. curr->wx = worm->mx, curr->wy = worm->my; 761. return; 762. } 763. /* remove head segment from map in case we end up calling toss_wsegs(); 764. if it doesn't get tossed, it will become the final tail segment and 765. get new coordinates */ 766. wheads[wnum]->wx = wheads[wnum]->wy = 0; 767. 768. wheads[wnum] = new_tail = curr; 769. curr = curr->nseg; 770. new_tail->nseg = (struct wseg *) 0; 771. new_tail->wx = x; 772. new_tail->wy = y; 773. 774. while (curr) { 775. coordxy nx = ox, ny = oy; 776. 777. if (rnd_nextto_goodpos(&nx, &ny, worm)) { 778. place_worm_seg(worm, nx, ny); 779. curr->wx = (coordxy) (ox = nx); 780. curr->wy = (coordxy) (oy = ny); 781. wtails[wnum] = curr; 782. curr = curr->nseg; 783. wtails[wnum]->nseg = new_tail; 784. new_tail = wtails[wnum]; 785. newsym(nx, ny); 786. } else { 787. /* Oops. Truncate because there is no place for rest of it. */ 788. toss_wsegs(curr, FALSE); 789. curr = (struct wseg *) 0; 790. } 791. } 792. } 793. 794. #if 0 795. /* 796. * Given a coordinate x, y. 797. * return in *nx, *ny, the coordinates of one of the <= 8 squares adjoining. 798. * 799. * This function, and the loop it serves, could be eliminated by coding 800. * enexto() with a search radius. 801. */ 802. staticfn void 803. random_dir(int x, int y, int *nx, int *ny) 804. { 805. *nx = x + (x > 1 /* extreme left ? */ 806. ? (x < COLNO - 1 /* extreme right ? */ 807. ? (rn2(3) - 1) /* neither, so +1, 0, or -1 */ 808. : -rn2(2)) /* right edge, use -1 or 0 */ 809. : rn2(2)); /* left edge, use 0 or 1 */ 810. if (*nx != x) /* if x has changed, do same thing with y */ 811. *ny = y + (y > 0 /* y==0 is ok (x==0 is not) */ 812. ? (y < ROWNO - 1 813. ? (rn2(3) - 1) 814. : -rn2(2)) 815. : rn2(2)); 816. else /* when x has remained the same, force y to change */ 817. *ny = y + (y > 0 818. ? (y < ROWNO - 1 819. ? (rn2(2) ? 1 : -1) /* not at edge, so +1 or -1 */ 820. : -1) /* bottom, use -1 */ 821. : 1); /* top, use +1 */ 822. } 823. #endif 824. 825. /* for size_monst(cmd.c) to support #stats */ 826. int 827. size_wseg(struct monst *worm) 828. { 829. return (int) (count_wsegs(worm) * sizeof (struct wseg)); 830. } 831. 832. /* count_wsegs() 833. * returns the number of segments that a worm has. 834. */ 835. int 836. count_wsegs(struct monst *mtmp) 837. { 838. int i = 0; 839. struct wseg *curr; 840. 841. if (mtmp->wormno) { 842. for (curr = wtails[mtmp->wormno]->nseg; curr; curr = curr->nseg) 843. i++; 844. } 845. return i; 846. } 847. 848. /* create_worm_tail() 849. * will create a worm tail chain of (num_segs + 1) and return pointer to it. 850. */ 851. staticfn struct wseg * 852. create_worm_tail(int num_segs) 853. { 854. int i = 0; 855. struct wseg *new_tail, *curr; 856. 857. if (!num_segs) 858. return (struct wseg *) 0; 859. 860. new_tail = curr = newseg(); 861. curr->nseg = (struct wseg *) 0; 862. curr->wx = 0; 863. curr->wy = 0; 864. 865. while (i < num_segs) { 866. curr->nseg = newseg(); 867. curr = curr->nseg; 868. curr->nseg = (struct wseg *) 0; 869. curr->wx = 0; 870. curr->wy = 0; 871. i++; 872. } 873. 874. return new_tail; 875. } 876. 877. /* worm_known() 878. * Is any segment of this worm in viewing range? Note: caller must check 879. * invisibility and telepathy (which should only show the head anyway). 880. * Mostly used in the canseemon() macro. 881. */ 882. boolean 883. worm_known(struct monst *worm) 884. { 885. struct wseg *curr = wtails[worm->wormno]; 886. 887. while (curr) { 888. if (cansee(curr->wx, curr->wy)) 889. return TRUE; 890. curr = curr->nseg; 891. } 892. return FALSE; 893. } 894. 895. /* would moving from <x1,y1> to <x2,y2> involve passing between two 896. consecutive segments of the same worm? */ 897. boolean 898. worm_cross(int x1, int y1, int x2, int y2) 899. { 900. struct monst *worm; 901. struct wseg *curr, *wnxt; 902. 903. /* 904. * With digits representing relative sequence number of the segments, 905. * returns true when testing between @ and ? (passes through worm's 906. * body), false between @ and ! (stays on same side of worm). 907. * .w1?.. 908. * ..@2.. 909. * .65!3. 910. * ...4.. 911. */ 912. 913. if (distmin(x1, y1, x2, y2) != 1) { 914. impossible("worm_cross checking for non-adjacent location?"); 915. return FALSE; 916. } 917. /* attempting to pass between worm segs is only relevant for diagonal */ 918. if (x1 == x2 || y1 == y2) 919. return FALSE; 920. 921. /* is the same monster at <x1,y2> and at <x2,y1>? */ 922. worm = m_at(x1, y2); 923. if (!worm || m_at(x2, y1) != worm) 924. return FALSE; 925. 926. /* same monster is at both adjacent spots, so must be a worm; we need 927. to figure out if the two spots are occupied by consecutive segments */ 928. for (curr = wtails[worm->wormno]; curr; curr = wnxt) { 929. wnxt = curr->nseg; 930. if (!wnxt) 931. break; /* no next segment; can't continue */ 932. 933. /* we don't know which of <x1,y2> or <x2,y1> we'll hit first, but 934. whichever it is, they're consecutive iff next seg is the other */ 935. if (curr->wx == x1 && curr->wy == y2) 936. return (boolean) (wnxt->wx == x2 && wnxt->wy == y1); 937. if (curr->wx == x2 && curr->wy == y1) 938. return (boolean) (wnxt->wx == x1 && wnxt->wy == y2); 939. } 940. /* should never reach here... */ 941. return FALSE; 942. } 943. 944. /* construct an index number for a worm tail segment */ 945. int 946. wseg_at(struct monst *worm, int x, int y) 947. { 948. int res = 0; 949. 950. if (worm && worm->wormno && m_at(x, y) == worm) { 951. struct wseg *curr; 952. int i, n; 953. coordxy wx = (coordxy) x, wy = (coordxy) y; 954. 955. for (i = 0, curr = wtails[worm->wormno]; curr; curr = curr->nseg) { 956. if (curr->wx == wx && curr->wy == wy) 957. break; 958. ++i; 959. } 960. for (n = i; curr; curr = curr->nseg) 961. ++n; 962. res = n - i; 963. } 964. return res; 965. } 966. 967. void 968. flip_worm_segs_vertical(struct monst *worm, int miny, int maxy) 969. { 970. struct wseg *curr = wtails[worm->wormno]; 971. 972. while (curr) { 973. curr->wy = (maxy - curr->wy + miny); 974. curr = curr->nseg; 975. } 976. } 977. 978. void 979. flip_worm_segs_horizontal(struct monst *worm, int minx, int maxx) 980. { 981. struct wseg *curr = wtails[worm->wormno]; 982. 983. while (curr) { 984. curr->wx = (maxx - curr->wx + minx); 985. curr = curr->nseg; 986. } 987. } 988. 989. void 990. redraw_worm(struct monst *worm) 991. { 992. struct wseg *curr = wtails[worm->wormno]; 993. 994. while (curr) { 995. newsym(curr->wx, curr->wy); 996. curr = curr->nseg; 997. } 998. } 999. #endif /* !SFCTOOL */ 1000. 1001. /*worm.c*/