Source:NetHack 3.6.5/src/worm.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to worm.c from the source code of NetHack 3.6.5. To link to a particular line, write [[Source:NetHack 3.6.5/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 3.6	worm.c	$NHDT-Date: 1561340880 2019/06/24 01:48:00 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.30 $ */
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. #include "lev.h"
8. 
9. #define newseg() (struct wseg *) alloc(sizeof (struct wseg))
10. #define dealloc_seg(wseg) free((genericptr_t) (wseg))
11. 
12. /* worm segment structure */
13. struct wseg {
14.     struct wseg *nseg;
15.     xchar wx, wy; /* the segment's position */
16. };
17. 
18. STATIC_DCL void FDECL(toss_wsegs, (struct wseg *, BOOLEAN_P));
19. STATIC_DCL void FDECL(shrink_worm, (int));
20. STATIC_DCL void FDECL(random_dir, (XCHAR_P, XCHAR_P, xchar *, xchar *));
21. STATIC_DCL struct wseg *FDECL(create_worm_tail, (int));
22. 
23. /*  Description of long worm implementation.
24.  *
25.  *  Each monst struct of the head of a tailed worm has a wormno set to
26.  *                      1 <= wormno < MAX_NUM_WORMS
27.  *  If wormno == 0 this does not mean that the monster is not a worm,
28.  *  it just means that the monster does not have a long worm tail.
29.  *
30.  *  The actual segments of a worm are not full blown monst structs.
31.  *  They are small wseg structs, and their position in the levels.monsters[][]
32.  *  array is held by the monst struct of the head of the worm.  This makes
33.  *  things like probing and hit point bookkeeping much easier.
34.  *
35.  *  The segments of the long worms on a level are kept as an array of
36.  *  singly threaded linked lists.  The wormno variable is used as an index
37.  *  for these segment arrays.
38.  *
39.  *  wtails:     The first (starting struct) of a linked list.  This points
40.  *              to the tail (last) segment of the worm.
41.  *
42.  *  wheads:     The last (end) of a linked list of segments.  This points to
43.  *              the segment that is at the same position as the real monster
44.  *              (the head).  Note that the segment that wheads[wormno] points
45.  *              to, is not displayed.  It is simply there to keep track of
46.  *              where the head came from, so that worm movement and display
47.  *              are simplified later.
48.  *              Keeping the head segment of the worm at the end of the list
49.  *              of tail segments is an endless source of confusion, but it is
50.  *              necessary.
51.  *              From now on, we will use "start" and "end" to refer to the
52.  *              linked list and "head" and "tail" to refer to the worm.
53.  *
54.  *  One final worm array is:
55.  *
56.  *  wgrowtime:  This tells us when to add another segment to the worm.
57.  *
58.  *  When a worm is moved, we add a new segment at the head, and delete the
59.  *  segment at the tail (unless we want it to grow).  This new head segment is
60.  *  located in the same square as the actual head of the worm.  If we want
61.  *  to grow the worm, we don't delete the tail segment, and we give the worm
62.  *  extra hit points, which possibly go into its maximum.
63.  *
64.  *  Non-moving worms (worm_nomove) are assumed to be surrounded by their own
65.  *  tail, and, thus, shrink instead of grow (as their tails keep going while
66.  *  their heads are stopped short).  In this case, we delete the last tail
67.  *  segment, and remove hit points from the worm.
68.  */
69. 
70. struct wseg *wheads[MAX_NUM_WORMS] = DUMMY, *wtails[MAX_NUM_WORMS] = DUMMY;
71. long wgrowtime[MAX_NUM_WORMS] = DUMMY;
72. 
73. /*
74.  *  get_wormno()
75.  *
76.  *  Find an unused worm tail slot and return the index.  A zero means that
77.  *  there are no slots available.  This means that the worm head can exist,
78.  *  it just cannot ever grow a tail.
79.  *
80.  *  It, also, means that there is an optimisation to made.  The [0] positions
81.  *  of the arrays are never used.  Meaning, we really *could* have one more
82.  *  tailed worm on the level, or use a smaller array (using wormno - 1).
83.  *
84.  *  Implementation is left to the interested hacker.
85.  */
86. int
87. get_wormno()
88. {
89.     register int new_wormno = 1;
90. 
91.     while (new_wormno < MAX_NUM_WORMS) {
92.         if (!wheads[new_wormno])
93.             return new_wormno; /* found empty wtails[] slot at new_wormno */
94.         new_wormno++;
95.     }
96.     return 0; /* level infested with worms */
97. }
98. 
99. /*
100.  *  initworm()
101.  *
102.  *  Use if (mon->wormno = get_wormno()) before calling this function!
103.  *
104.  *  Initialize the worm entry.  This will set up the worm grow time, and
105.  *  create and initialize the dummy segment for wheads[] and wtails[].
106.  *
107.  *  If the worm has no tail (ie get_wormno() fails) then this function need
108.  *  not be called.
109.  */
110. void
111. initworm(worm, wseg_count)
112. struct monst *worm;
113. int wseg_count;
114. {
115.     register struct wseg *seg, *new_tail = create_worm_tail(wseg_count);
116.     register int wnum = worm->wormno;
117. 
118.     /*  if (!wnum) return;  bullet proofing */
119. 
120.     if (new_tail) {
121.         wtails[wnum] = new_tail;
122.         for (seg = new_tail; seg->nseg; seg = seg->nseg)
123.             ;
124.         wheads[wnum] = seg;
125.     } else {
126.         wtails[wnum] = wheads[wnum] = seg = newseg();
127.         seg->nseg = (struct wseg *) 0;
128.         seg->wx = worm->mx;
129.         seg->wy = worm->my;
130.     }
131.     wgrowtime[wnum] = 0L;
132. }
133. 
134. /*
135.  *  toss_wsegs()
136.  *
137.  *  Get rid of all worm segments on and following the given pointer curr.
138.  *  The display may or may not need to be updated as we free the segments.
139.  */
140. STATIC_OVL
141. void
142. toss_wsegs(curr, display_update)
143. register struct wseg *curr;
144. register boolean display_update;
145. {
146.     register struct wseg *seg;
147. 
148.     while (curr) {
149.         seg = curr->nseg;
150. 
151.         /* remove from level.monsters[][] */
152. 
153.         /* need to check curr->wx for genocided while migrating_mon */
154.         if (curr->wx) {
155.             remove_monster(curr->wx, curr->wy);
156. 
157.             /* update screen before deallocation */
158.             if (display_update)
159.                 newsym(curr->wx, curr->wy);
160.         }
161. 
162.         /* free memory used by the segment */
163.         dealloc_seg(curr);
164.         curr = seg;
165.     }
166. }
167. 
168. /*
169.  *  shrink_worm()
170.  *
171.  *  Remove the tail segment of the worm (the starting segment of the list).
172.  */
173. STATIC_OVL
174. void
175. shrink_worm(wnum)
176. int wnum; /* worm number */
177. {
178.     struct wseg *seg;
179. 
180.     if (wtails[wnum] == wheads[wnum])
181.         return; /* no tail */
182. 
183.     seg = wtails[wnum];
184.     wtails[wnum] = seg->nseg;
185.     seg->nseg = (struct wseg *) 0;
186.     toss_wsegs(seg, TRUE);
187. }
188. 
189. /*
190.  *  worm_move()
191.  *
192.  *  Check for mon->wormno before calling this function!
193.  *
194.  *  Move the worm.  Maybe grow.
195.  */
196. void
197. worm_move(worm)
198. struct monst *worm;
199. {
200.     register struct wseg *seg, *new_seg; /* new segment */
201.     register int wnum = worm->wormno;    /* worm number */
202. 
203.     /*  if (!wnum) return;  bullet proofing */
204. 
205.     /*
206.      *  Place a segment at the old worm head.  The head has already moved.
207.      */
208.     seg = wheads[wnum];
209.     place_worm_seg(worm, seg->wx, seg->wy);
210.     newsym(seg->wx, seg->wy); /* display the new segment */
211. 
212.     /*
213.      *  Create a new dummy segment head and place it at the end of the list.
214.      */
215.     new_seg = newseg();
216.     new_seg->wx = worm->mx;
217.     new_seg->wy = worm->my;
218.     new_seg->nseg = (struct wseg *) 0;
219.     seg->nseg = new_seg;    /* attach it to the end of the list */
220.     wheads[wnum] = new_seg; /* move the end pointer */
221. 
222.     if (wgrowtime[wnum] <= moves) {
223.         if (!wgrowtime[wnum])
224.             wgrowtime[wnum] = moves + rnd(5);
225.         else
226.             wgrowtime[wnum] += rn1(15, 3);
227.         worm->mhp += 3;
228.         if (worm->mhp > MHPMAX)
229.             worm->mhp = MHPMAX;
230.         if (worm->mhp > worm->mhpmax)
231.             worm->mhpmax = worm->mhp;
232.     } else
233.         /* The worm doesn't grow, so the last segment goes away. */
234.         shrink_worm(wnum);
235. }
236. 
237. /*
238.  *  worm_nomove()
239.  *
240.  *  Check for mon->wormno before calling this function!
241.  *
242.  *  The worm don't move so it should shrink.
243.  */
244. void
245. worm_nomove(worm)
246. register struct monst *worm;
247. {
248.     shrink_worm((int) worm->wormno); /* shrink */
249. 
250.     if (worm->mhp > 3)
251.         worm->mhp -= 3; /* mhpmax not changed ! */
252.     else
253.         worm->mhp = 1;
254. }
255. 
256. /*
257.  *  wormgone()
258.  *
259.  *  Check for mon->wormno before calling this function!
260.  *
261.  *  Kill a worm tail.
262.  */
263. void
264. wormgone(worm)
265. register struct monst *worm;
266. {
267.     register int wnum = worm->wormno;
268. 
269.     /*  if (!wnum) return;  bullet proofing */
270. 
271.     worm->wormno = 0;
272. 
273.     /*  This will also remove the real monster (ie 'w') from the its
274.      *  position in level.monsters[][].
275.      */
276.     toss_wsegs(wtails[wnum], TRUE);
277. 
278.     wheads[wnum] = wtails[wnum] = (struct wseg *) 0;
279. }
280. 
281. /*
282.  *  wormhitu()
283.  *
284.  *  Check for mon->wormno before calling this function!
285.  *
286.  *  If the hero is near any part of the worm, the worm will try to attack.
287.  */
288. void
289. wormhitu(worm)
290. register struct monst *worm;
291. {
292.     register int wnum = worm->wormno;
293.     register struct wseg *seg;
294. 
295.     /*  if (!wnum) return;  bullet proofing */
296. 
297.     /*  This does not work right now because mattacku() thinks that the head
298.      *  is out of range of the player.  We might try to kludge, and bring
299.      *  the head within range for a tiny moment, but this needs a bit more
300.      *  looking at before we decide to do this.
301.      */
302.     for (seg = wtails[wnum]; seg; seg = seg->nseg)
303.         if (distu(seg->wx, seg->wy) < 3)
304.             if (mattacku(worm))
305.                 return; /* your passive ability killed the worm */
306. }
307. 
308. /*  cutworm()
309.  *
310.  *  Check for mon->wormno before calling this function!
311.  *
312.  *  When hitting a worm (worm) at position x, y, with a weapon (weap),
313.  *  there is a chance that the worm will be cut in half, and a chance
314.  *  that both halves will survive.
315.  */
316. void
317. cutworm(worm, x, y, cuttier)
318. struct monst *worm;
319. xchar x, y;
320. boolean cuttier; /* hit is by wielded blade or axe or by thrown axe */
321. {
322.     register struct wseg *curr, *new_tail;
323.     register struct monst *new_worm;
324.     int wnum = worm->wormno;
325.     int cut_chance, new_wnum;
326. 
327.     if (!wnum)
328.         return; /* bullet proofing */
329. 
330.     if (x == worm->mx && y == worm->my)
331.         return; /* hit on head */
332. 
333.     /* cutting goes best with a cuttier weapon */
334.     cut_chance = rnd(20); /* Normally     1-16 does not cut, 17-20 does, */
335.     if (cuttier)
336.         cut_chance += 10; /* with a blade 1- 6 does not cut,  7-20 does. */
337. 
338.     if (cut_chance < 17)
339.         return; /* not good enough */
340. 
341.     /* Find the segment that was attacked. */
342.     curr = wtails[wnum];
343. 
344.     while ((curr->wx != x) || (curr->wy != y)) {
345.         curr = curr->nseg;
346.         if (!curr) {
347.             impossible("cutworm: no segment at (%d,%d)", (int) x, (int) y);
348.             return;
349.         }
350.     }
351. 
352.     /* If this is the tail segment, then the worm just loses it. */
353.     if (curr == wtails[wnum]) {
354.         shrink_worm(wnum);
355.         return;
356.     }
357. 
358.     /*
359.      *  Split the worm.  The tail for the new worm is the old worm's tail.
360.      *  The tail for the old worm is the segment that follows "curr",
361.      *  and "curr" becomes the dummy segment under the new head.
362.      */
363.     new_tail = wtails[wnum];
364.     wtails[wnum] = curr->nseg;
365.     curr->nseg = (struct wseg *) 0; /* split the worm */
366. 
367.     /*
368.      *  At this point, the old worm is correct.  Any new worm will have
369.      *  it's head at "curr" and its tail at "new_tail".  The old worm
370.      *  must be at least level 3 in order to produce a new worm.
371.      */
372.     new_worm = 0;
373.     new_wnum = (worm->m_lev >= 3 && !rn2(3)) ? get_wormno() : 0;
374.     if (new_wnum) {
375.         remove_monster(x, y); /* clone_mon puts new head here */
376.         /* clone_mon() will fail if enough long worms have been
377.            created to have them be marked as extinct or if the hit
378.            that cut the current one has dropped it down to 1 HP */
379.         new_worm = clone_mon(worm, x, y);
380.     }
381. 
382.     /* Sometimes the tail end dies. */
383.     if (!new_worm) {
384.         place_worm_seg(worm, x, y); /* place the "head" segment back */
385.         if (context.mon_moving) {
386.             if (canspotmon(worm))
387.                 pline("Part of %s tail has been cut off.",
388.                       s_suffix(mon_nam(worm)));
389.         } else
390.             You("cut part of the tail off of %s.", mon_nam(worm));
391.         toss_wsegs(new_tail, TRUE);
392.         if (worm->mhp > 1)
393.             worm->mhp /= 2;
394.         return;
395.     }
396. 
397.     new_worm->wormno = new_wnum; /* affix new worm number */
398.     new_worm->mcloned = 0;       /* treat second worm as a normal monster */
399. 
400.     /* Devalue the monster level of both halves of the worm.
401.        Note: m_lev is always at least 3 in order to get this far. */
402.     worm->m_lev = max((unsigned) worm->m_lev - 2, 3);
403.     new_worm->m_lev = worm->m_lev;
404. 
405.     /* Calculate the lower-level mhp; use <N>d8 for long worms.
406.        Can't use newmonhp() here because it would reset m_lev. */
407.     new_worm->mhpmax = new_worm->mhp = d((int) new_worm->m_lev, 8);
408.     worm->mhpmax = d((int) worm->m_lev, 8); /* new maxHP for old worm */
409.     if (worm->mhpmax < worm->mhp)
410.         worm->mhp = worm->mhpmax;
411. 
412.     wtails[new_wnum] = new_tail; /* We've got all the info right now */
413.     wheads[new_wnum] = curr;     /* so we can do this faster than    */
414.     wgrowtime[new_wnum] = 0L;    /* trying to call initworm().       */
415. 
416.     /* Place the new monster at all the segment locations. */
417.     place_wsegs(new_worm, worm);
418. 
419.     if (context.mon_moving)
420.         pline("%s is cut in half.", Monnam(worm));
421.     else
422.         You("cut %s in half.", mon_nam(worm));
423. }
424. 
425. /*
426.  *  see_wsegs()
427.  *
428.  *  Refresh all of the segments of the given worm.  This is only called
429.  *  from see_monster() in display.c or when a monster goes minvis.  It
430.  *  is located here for modularity.
431.  */
432. void
433. see_wsegs(worm)
434. struct monst *worm;
435. {
436.     struct wseg *curr = wtails[worm->wormno];
437. 
438.     /*  if (!mtmp->wormno) return;  bullet proofing */
439. 
440.     while (curr != wheads[worm->wormno]) {
441.         newsym(curr->wx, curr->wy);
442.         curr = curr->nseg;
443.     }
444. }
445. 
446. /*
447.  *  detect_wsegs()
448.  *
449.  *  Display all of the segments of the given worm for detection.
450.  */
451. void
452. detect_wsegs(worm, use_detection_glyph)
453. struct monst *worm;
454. boolean use_detection_glyph;
455. {
456.     int num;
457.     struct wseg *curr = wtails[worm->wormno];
458. 
459.     /*  if (!mtmp->wormno) return;  bullet proofing */
460.     int what_tail = what_mon(PM_LONG_WORM_TAIL, newsym_rn2);
461. 
462.     while (curr != wheads[worm->wormno]) {
463.         num = use_detection_glyph
464.             ? detected_monnum_to_glyph(what_tail)
465.             : (worm->mtame
466.                ? petnum_to_glyph(what_tail)
467.                : monnum_to_glyph(what_tail));
468.         show_glyph(curr->wx, curr->wy, num);
469.         curr = curr->nseg;
470.     }
471. }
472. 
473. /*
474.  *  save_worm()
475.  *
476.  *  Save the worm information for later use.  The count is the number
477.  *  of segments, including the dummy.  Called from save.c.
478.  */
479. void
480. save_worm(fd, mode)
481. int fd, mode;
482. {
483.     int i;
484.     int count;
485.     struct wseg *curr, *temp;
486. 
487.     if (perform_bwrite(mode)) {
488.         for (i = 1; i < MAX_NUM_WORMS; i++) {
489.             for (count = 0, curr = wtails[i]; curr; curr = curr->nseg)
490.                 count++;
491.             /* Save number of segments */
492.             bwrite(fd, (genericptr_t) &count, sizeof(int));
493.             /* Save segment locations of the monster. */
494.             if (count) {
495.                 for (curr = wtails[i]; curr; curr = curr->nseg) {
496.                     bwrite(fd, (genericptr_t) & (curr->wx), sizeof(xchar));
497.                     bwrite(fd, (genericptr_t) & (curr->wy), sizeof(xchar));
498.                 }
499.             }
500.         }
501.         bwrite(fd, (genericptr_t) wgrowtime, sizeof(wgrowtime));
502.     }
503. 
504.     if (release_data(mode)) {
505.         /* Free the segments only.  savemonchn() will take care of the
506.          * monsters. */
507.         for (i = 1; i < MAX_NUM_WORMS; i++) {
508.             if (!(curr = wtails[i]))
509.                 continue;
510. 
511.             while (curr) {
512.                 temp = curr->nseg;
513.                 dealloc_seg(curr); /* free the segment */
514.                 curr = temp;
515.             }
516.             wheads[i] = wtails[i] = (struct wseg *) 0;
517.         }
518.     }
519. }
520. 
521. /*
522.  *  rest_worm()
523.  *
524.  *  Restore the worm information from the save file.  Called from restore.c
525.  */
526. void
527. rest_worm(fd)
528. int fd;
529. {
530.     int i, j, count;
531.     struct wseg *curr, *temp;
532. 
533.     for (i = 1; i < MAX_NUM_WORMS; i++) {
534.         mread(fd, (genericptr_t) &count, sizeof(int));
535.         if (!count)
536.             continue; /* none */
537. 
538.         /* Get the segments. */
539.         for (curr = (struct wseg *) 0, j = 0; j < count; j++) {
540.             temp = newseg();
541.             temp->nseg = (struct wseg *) 0;
542.             mread(fd, (genericptr_t) & (temp->wx), sizeof(xchar));
543.             mread(fd, (genericptr_t) & (temp->wy), sizeof(xchar));
544.             if (curr)
545.                 curr->nseg = temp;
546.             else
547.                 wtails[i] = temp;
548.             curr = temp;
549.         }
550.         wheads[i] = curr;
551.     }
552.     mread(fd, (genericptr_t) wgrowtime, sizeof(wgrowtime));
553. }
554. 
555. /*
556.  *  place_wsegs()
557.  *
558.  *  Place the segments of the given worm.  Called from restore.c
559.  *  If oldworm is not NULL, assumes the oldworm segments are on map
560.  *  in the same location as worm segments
561.  */
562. void
563. place_wsegs(worm, oldworm)
564. struct monst *worm, *oldworm;
565. {
566.     struct wseg *curr = wtails[worm->wormno];
567. 
568.     /*  if (!mtmp->wormno) return;  bullet proofing */
569. 
570.     while (curr != wheads[worm->wormno]) {
571.         xchar x = curr->wx;
572.         xchar y = curr->wy;
573. 
574.         if (oldworm) {
575.             if (m_at(x,y) == oldworm)
576.                 remove_monster(x, y);
577.             else
578.                 impossible("placing worm seg <%i,%i> over another mon", x, y);
579.         }
580.         place_worm_seg(worm, x, y);
581.         curr = curr->nseg;
582.     }
583. }
584. 
585. void
586. sanity_check_worm(worm)
587. struct monst *worm;
588. {
589.     struct wseg *curr;
590. 
591.     if (!worm)
592.         panic("no worm!");
593.     if (!worm->wormno)
594.         panic("not a worm?!");
595. 
596.     curr = wtails[worm->wormno];
597. 
598.     while (curr != wheads[worm->wormno]) {
599.         if (curr->wx) {
600.             if (!isok(curr->wx, curr->wy))
601.                 panic("worm seg not isok");
602.             if (level.monsters[curr->wx][curr->wy] != worm)
603.                 panic("worm not at seg location");
604.         }
605.         curr = curr->nseg;
606.     }
607. }
608. 
609. /*
610.  *  remove_worm()
611.  *
612.  *  This function is equivalent to the remove_monster #define in
613.  *  rm.h, only it will take the worm *and* tail out of the levels array.
614.  *  It does not get rid of (dealloc) the worm tail structures, and it does
615.  *  not remove the mon from the fmon chain.
616.  */
617. void
618. remove_worm(worm)
619. register struct monst *worm;
620. {
621.     register struct wseg *curr = wtails[worm->wormno];
622. 
623.     /*  if (!mtmp->wormno) return;  bullet proofing */
624. 
625.     while (curr) {
626.         if (curr->wx) {
627.             remove_monster(curr->wx, curr->wy);
628.             newsym(curr->wx, curr->wy);
629.             curr->wx = 0;
630.         }
631.         curr = curr->nseg;
632.     }
633. }
634. 
635. /*
636.  *  place_worm_tail_randomly()
637.  *
638.  *  Place a worm tail somewhere on a level behind the head.
639.  *  This routine essentially reverses the order of the wsegs from head
640.  *  to tail while placing them.
641.  *  x, and y are most likely the worm->mx, and worm->my, but don't *need* to
642.  *  be, if somehow the head is disjoint from the tail.
643.  */
644. void
645. place_worm_tail_randomly(worm, x, y)
646. struct monst *worm;
647. xchar x, y;
648. {
649.     int wnum = worm->wormno;
650.     struct wseg *curr = wtails[wnum];
651.     struct wseg *new_tail;
652.     register xchar ox = x, oy = y;
653. 
654.     /*  if (!wnum) return;  bullet proofing */
655. 
656.     if (wnum && (!wtails[wnum] || !wheads[wnum])) {
657.         impossible("place_worm_tail_randomly: wormno is set without a tail!");
658.         return;
659.     }
660. 
661.     wheads[wnum] = new_tail = curr;
662.     curr = curr->nseg;
663.     new_tail->nseg = (struct wseg *) 0;
664.     new_tail->wx = x;
665.     new_tail->wy = y;
666. 
667.     while (curr) {
668.         xchar nx, ny;
669.         char tryct = 0;
670. 
671.         /* pick a random direction from x, y and search for goodpos() */
672.         do {
673.             random_dir(ox, oy, &nx, &ny);
674.         } while (!goodpos(nx, ny, worm, 0) && (tryct++ < 50));
675. 
676.         if (tryct < 50) {
677.             place_worm_seg(worm, nx, ny);
678.             curr->wx = ox = nx;
679.             curr->wy = oy = ny;
680.             wtails[wnum] = curr;
681.             curr = curr->nseg;
682.             wtails[wnum]->nseg = new_tail;
683.             new_tail = wtails[wnum];
684.             newsym(nx, ny);
685.         } else {                     /* Oops.  Truncate because there was */
686.             toss_wsegs(curr, FALSE); /* no place for the rest of it */
687.             curr = (struct wseg *) 0;
688.         }
689.     }
690. }
691. 
692. /*
693.  * Given a coordinate x, y.
694.  * return in *nx, *ny, the coordinates of one of the <= 8 squares adjoining.
695.  *
696.  * This function, and the loop it serves, could be eliminated by coding
697.  * enexto() with a search radius.
698.  */
699. STATIC_OVL
700. void
701. random_dir(x, y, nx, ny)
702. xchar x, y;
703. xchar *nx, *ny;
704. {
705.     *nx = x + (x > 1                /* extreme left ? */
706.                ? (x < COLNO - 1     /* extreme right ? */
707.                   ? (rn2(3) - 1)    /* neither so +1, 0, or -1 */
708.                   : -rn2(2))        /* right edge, use -1 or 0 */
709.                : rn2(2));           /* left edge, use 0 or 1 */
710.     if (*nx != x) /* if x has changed, do same thing with y */
711.         *ny = y + (y > 0             /* y==0 is ok (x==0 is not) */
712.                    ? (y < ROWNO - 1
713.                       ? (rn2(3) - 1)
714.                       : -rn2(2))
715.                    : rn2(2));
716.     else /* when x has remained the same, force y to change */
717.         *ny = y + (y > 0
718.                    ? (y < ROWNO - 1
719.                       ? (rn2(2) ? 1 : -1)   /* not at edge, so +1 or -1 */
720.                       : -1)                 /* bottom, use -1 */
721.                    : 1);                    /* top, use +1 */
722. }
723. 
724. /* for size_monst(cmd.c) to support #stats */
725. int
726. size_wseg(worm)
727. struct monst *worm;
728. {
729.     return (int) (count_wsegs(worm) * sizeof (struct wseg));
730. }
731. 
732. /*  count_wsegs()
733.  *  returns the number of segments that a worm has.
734.  */
735. int
736. count_wsegs(mtmp)
737. struct monst *mtmp;
738. {
739.     register int i = 0;
740.     register struct wseg *curr;
741. 
742.     if (mtmp->wormno) {
743.         for (curr = wtails[mtmp->wormno]->nseg; curr; curr = curr->nseg)
744.             i++;
745.     }
746.     return i;
747. }
748. 
749. /*  create_worm_tail()
750.  *  will create a worm tail chain of (num_segs + 1) and return pointer to it.
751.  */
752. STATIC_OVL
753. struct wseg *
754. create_worm_tail(num_segs)
755. int num_segs;
756. {
757.     register int i = 0;
758.     register struct wseg *new_tail, *curr;
759. 
760.     if (!num_segs)
761.         return (struct wseg *) 0;
762. 
763.     new_tail = curr = newseg();
764.     curr->nseg = (struct wseg *) 0;
765.     curr->wx = 0;
766.     curr->wy = 0;
767. 
768.     while (i < num_segs) {
769.         curr->nseg = newseg();
770.         curr = curr->nseg;
771.         curr->nseg = (struct wseg *) 0;
772.         curr->wx = 0;
773.         curr->wy = 0;
774.         i++;
775.     }
776. 
777.     return new_tail;
778. }
779. 
780. /*  worm_known()
781.  *  Is any segment of this worm in viewing range?  Note: caller must check
782.  *  invisibility and telepathy (which should only show the head anyway).
783.  *  Mostly used in the canseemon() macro.
784.  */
785. boolean
786. worm_known(worm)
787. struct monst *worm;
788. {
789.     struct wseg *curr = wtails[worm->wormno];
790. 
791.     while (curr) {
792.         if (cansee(curr->wx, curr->wy))
793.             return TRUE;
794.         curr = curr->nseg;
795.     }
796.     return FALSE;
797. }
798. 
799. /* would moving from <x1,y1> to <x2,y2> involve passing between two
800.    consecutive segments of the same worm? */
801. boolean
802. worm_cross(x1, y1, x2, y2)
803. int x1, y1, x2, y2;
804. {
805.     struct monst *worm;
806.     struct wseg *curr, *wnxt;
807. 
808.     /*
809.      * With digits representing relative sequence number of the segments,
810.      * returns true when testing between @ and ? (passes through worm's
811.      * body), false between @ and ! (stays on same side of worm).
812.      *  .w1?..
813.      *  ..@2..
814.      *  .65!3.
815.      *  ...4..
816.      */
817. 
818.     if (distmin(x1, y1, x2, y2) != 1) {
819.         impossible("worm_cross checking for non-adjacent location?");
820.         return FALSE;
821.     }
822.     /* attempting to pass between worm segs is only relevant for diagonal */
823.     if (x1 == x2 || y1 == y2)
824.         return FALSE;
825. 
826.     /* is the same monster at <x1,y2> and at <x2,y1>? */
827.     worm = m_at(x1, y2);
828.     if (!worm || m_at(x2, y1) != worm)
829.         return FALSE;
830. 
831.     /* same monster is at both adjacent spots, so must be a worm; we need
832.        to figure out if the two spots are occupied by consecutive segments */
833.     for (curr = wtails[worm->wormno]; curr; curr = wnxt) {
834.         wnxt = curr->nseg;
835.         if (!wnxt)
836.             break; /* no next segment; can't continue */
837. 
838.         /* we don't know which of <x1,y2> or <x2,y1> we'll hit first, but
839.            whichever it is, they're consecutive iff next seg is the other */
840.         if (curr->wx == x1 && curr->wy == y2)
841.             return (boolean) (wnxt->wx == x2 && wnxt->wy == y1);
842.         if (curr->wx == x2 && curr->wy == y1)
843.             return (boolean) (wnxt->wx == x1 && wnxt->wy == y2);
844.     }
845.     /* should never reach here... */
846.     return FALSE;
847. }
848. 
849. /* construct an index number for a worm tail segment */
850. int
851. wseg_at(worm, x, y)
852. struct monst *worm;
853. int x, y;
854. {
855.     int res = 0;
856. 
857.     if (worm && worm->wormno && m_at(x, y) == worm) {
858.         struct wseg *curr;
859.         int i, n;
860.         xchar wx = (xchar) x, wy = (xchar) y;
861. 
862.         for (i = 0, curr = wtails[worm->wormno]; curr; curr = curr->nseg) {
863.             if (curr->wx == wx && curr->wy == wy)
864.                 break;
865.             ++i;
866.         }
867.         for (n = i; curr; curr = curr->nseg)
868.             ++n;
869.         res = n - i;
870.     }
871.     return res;
872. }
873. 
874. /*worm.c*/