Source:NetHack 3.4.3/src/worm.c

From NetHackWiki
(Redirected from Source:Ref/worm move)
Jump to: navigation, search

Below is the full text to src/worm.c from NetHack 3.4.3. To link to a particular line, write [[worm.c#line123]], for example.

Top of file

  1. /*	SCCS Id: @(#)worm.c	3.4	1995/01/28	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */

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. #include "hack.h"
  2. #include "lev.h"
  3.  
  4. #define newseg()		(struct wseg *) alloc(sizeof(struct wseg))
  5. #define dealloc_seg(wseg)	free((genericptr_t) (wseg))
  6.  
  7. /* worm segment structure */
  8. struct wseg {
  9. struct wseg *nseg;
  10. xchar  wx, wy;	/* the segment's position */
  11. };
  12.  
  13. STATIC_DCL void FDECL(toss_wsegs, (struct wseg *,BOOLEAN_P));
  14. STATIC_DCL void FDECL(shrink_worm, (int));
  15. STATIC_DCL void FDECL(random_dir, (XCHAR_P,XCHAR_P,xchar *,xchar *));
  16. STATIC_DCL struct wseg *FDECL(create_worm_tail, (int));
  17.  
  18. /*  Description of long worm implementation.
  19. *
  20. *  Each monst struct of the head of a tailed worm has a wormno set to
  21. *			1 <= wormno < MAX_NUM_WORMS
  22. *  If wormno == 0 this does not mean that the monster is not a worm,
  23. *  it just means that the monster does not have a long worm tail.
  24. *
  25. *  The actual segments of a worm are not full blown monst structs.
  26. *  They are small wseg structs, and their position in the levels.monsters[][]
  27. *  array is held by the monst struct of the head of the worm.  This makes
  28. *  things like probing and hit point bookkeeping much easier.
  29. *
  30. *  The segments of the long worms on a level are kept as an array of
  31. *  singly threaded linked lists.  The wormno variable is used as an index
  32. *  for these segment arrays.
  33. *
  34. *  wtails:	The first (starting struct) of a linked list.  This points
  35. *		to the tail (last) segment of the worm.
  36. *
  37. *  wheads:	The last (end) of a linked list of segments.  This points to
  38. *		the segment that is at the same position as the real monster
  39. *		(the head).  Note that the segment that wheads[wormno] points
  40. *		to, is not displayed.  It is simply there to keep track of
  41. *		where the head came from, so that worm movement and display are
  42. *		simplified later.
  43. *		Keeping the head segment of the worm at the end of the list
  44. *		of tail segments is an endless source of confusion, but it is
  45. *		necessary.
  46. *		From now on, we will use "start" and "end" to refer to the
  47. *		linked list and "head" and "tail" to refer to the worm.
  48. *
  49. *  One final worm array is:
  50. *
  51. *  wgrowtime:	This tells us when to add another segment to the worm.
  52. *
  53. *  When a worm is moved, we add a new segment at the head, and delete the
  54. *  segment at the tail (unless we want it to grow).  This new head segment is
  55. *  located in the same square as the actual head of the worm.  If we want
  56. *  to grow the worm, we don't delete the tail segment, and we give the worm
  57. *  extra hit points, which possibly go into its maximum.
  58. *
  59. *  Non-moving worms (worm_nomove) are assumed to be surrounded by their own
  60. *  tail, and, thus, shrink instead of grow (as their tails keep going while
  61. *  their heads are stopped short).  In this case, we delete the last tail
  62. *  segment, and remove hit points from the worm.
  63. */
  64.  
  65. struct wseg *wheads[MAX_NUM_WORMS]   = DUMMY, *wtails[MAX_NUM_WORMS] = DUMMY;
  66. long	    wgrowtime[MAX_NUM_WORMS] = DUMMY;

get_wormno

  1. /*
  2. *  get_wormno()
  3. *
  4. *  Find an unused worm tail slot and return the index.  A zero means that
  5. *  there are no slots available.  This means that the worm head can exist,
  6. *  it just cannot ever grow a tail.
  7. *
  8. *  It, also, means that there is an optimisation to made.  The [0] positions
  9. *  of the arrays are never used.  Meaning, we really *could* have one more
  10. *  tailed worm on the level, or use a smaller array (using wormno - 1).
  11. *
  12. *  Implementation is left to the interested hacker.
  13. */
  14. int
  15. get_wormno()
  16. {
  17. register int new_wormno = 1;
  18.  
  19. while (new_wormno < MAX_NUM_WORMS) {
  20. 	if (!wheads[new_wormno])
  21. 	    return new_wormno; /* found an empty wtails[] slot at new_wormno */
  22. 	new_wormno++;
  23. }
  24.  
  25. return(0);	/* level infested with worms */
  26. }

initworm

  1. /*
  2. *  initworm()
  3. *
  4. *  Use if (mon->wormno = get_wormno()) before calling this function!
  5. *
  6. *  Initialize the worm entry.  This will set up the worm grow time, and
  7. *  create and initialize the dummy segment for wheads[] and wtails[].
  8. *
  9. *  If the worm has no tail (ie get_wormno() fails) then this function need
  10. *  not be called.
  11. */
  12. void
  13. initworm(worm, wseg_count)
  14. struct monst *worm;
  15. int wseg_count;
  16. {
  17. register struct wseg *seg, *new_tail = create_worm_tail(wseg_count);
  18. register int wnum = worm->wormno;
  19.  
  20. /*  if (!wnum) return;  bullet proofing */
  21.  
  22. if (new_tail) {
  23. 	wtails[wnum] = new_tail;
  24. 	for (seg = new_tail; seg->nseg; seg = seg->nseg);
  25. 	wheads[wnum] = seg;
  26. } else {
  27. 	wtails[wnum] = wheads[wnum] = seg = newseg();
  28. 	seg->nseg    = (struct wseg *) 0;
  29. 	seg->wx      = worm->mx;
  30. 	seg->wy      = worm->my;
  31. }
  32. wgrowtime[wnum] = 0L;
  33. }

toss_wsegs

  1. /*
  2. *  toss_wsegs()
  3. *
  4. *  Get rid of all worm segments on and following the given pointer curr.
  5. *  The display may or may not need to be updated as we free the segments.
  6. */
  7. STATIC_OVL
  8. void
  9. toss_wsegs(curr, display_update)
  10. register struct wseg *curr;
  11. register boolean display_update;
  12. {
  13. register struct wseg *seg;
  14.  
  15. while (curr) {
  16. 	seg = curr->nseg;
  17.  
  18. 	/* remove from level.monsters[][] */
  19.  
  20. 	/* need to check curr->wx for genocided while migrating_mon */
  21. 	if (curr->wx) {
  22. 	    remove_monster(curr->wx, curr->wy);
  23.  
  24. 	    /* update screen before deallocation */
  25. 	    if (display_update) newsym(curr->wx,curr->wy);
  26. 	}
  27.  
  28. 	/* free memory used by the segment */
  29. 	dealloc_seg(curr);
  30. 	curr = seg;
  31. }
  32. }

shrink_worm

  1. /*
  2. *  shrink_worm()
  3. *
  4. *  Remove the tail segment of the worm (the starting segment of the list).
  5. */
  6. STATIC_OVL
  7. void
  8. shrink_worm(wnum)
  9. int wnum;	/* worm number */
  10. {
  11. struct wseg *seg;
  12.  
  13. if (wtails[wnum] == wheads[wnum]) return;	/* no tail */
  14.  
  15. seg = wtails[wnum];
  16. wtails[wnum] = seg->nseg;
  17. seg->nseg = (struct wseg *) 0;
  18. toss_wsegs(seg, TRUE);
  19. }

worm_move

  1. /*
  2. *  worm_move()
  3. *
  4. *  Check for mon->wormno before calling this function!
  5. *
  6. *  Move the worm.  Maybe grow.
  7. */
  8. void
  9. worm_move(worm)
  10. struct monst *worm;
  11. {
  12. register struct wseg *seg, *new_seg;	/* new segment */
  13. register int	 wnum = worm->wormno;	/* worm number */
  14.  
  15.  
  16. /*  if (!wnum) return;  bullet proofing */
  17.  
  18. /*
  19. *  Place a segment at the old worm head.  The head has already moved.
  20. */
  21. seg = wheads[wnum];
  22. place_worm_seg(worm, seg->wx, seg->wy);
  23. newsym(seg->wx,seg->wy);		/* display the new segment */
  24.  
  25. /*
  26. *  Create a new dummy segment head and place it at the end of the list.
  27. */
  28. new_seg       = newseg();
  29. new_seg->wx   = worm->mx;
  30. new_seg->wy   = worm->my;
  31. new_seg->nseg = (struct wseg *) 0;
  32. seg->nseg     = new_seg;		/* attach it to the end of the list */
  33. wheads[wnum]  = new_seg;		/* move the end pointer */
  34.  
  35.  
  36. if (wgrowtime[wnum] <= moves) {
  37. 	if (!wgrowtime[wnum])
  38. 	    wgrowtime[wnum] = moves + rnd(5);
  39. 	else
  40. 	    wgrowtime[wnum] += rn1(15, 3);
  41. 	worm->mhp += 3;
  42. 	if (worm->mhp > MHPMAX) worm->mhp = MHPMAX;
  43. 	if (worm->mhp > worm->mhpmax) worm->mhpmax = worm->mhp;
  44. } else
  45. 	/* The worm doesn't grow, so the last segment goes away. */
  46. 	shrink_worm(wnum);
  47. }

worm_nomove

  1. /*
  2. *  worm_nomove()
  3. *
  4. *  Check for mon->wormno before calling this function!
  5. *
  6. *  The worm don't move so it should shrink.
  7. */
  8. void
  9. worm_nomove(worm)
  10. register struct monst *worm;
  11. {
  12. shrink_worm((int) worm->wormno);	/* shrink */
  13.  
  14. if (worm->mhp > 3)
  15. 	worm->mhp -= 3;		/* mhpmax not changed ! */
  16. else
  17. 	worm->mhp = 1;
  18. }

wormgone

  1. /*
  2. *  wormgone()
  3. *
  4. *  Check for mon->wormno before calling this function!
  5. *
  6. *  Kill a worm tail.
  7. */
  8. void
  9. wormgone(worm)
  10. register struct monst *worm;
  11. {
  12. register int wnum = worm->wormno;
  13.  
  14. /*  if (!wnum) return;  bullet proofing */
  15.  
  16. worm->wormno = 0;
  17.  
  18. /*  This will also remove the real monster (ie 'w') from the its
  19. *  position in level.monsters[][].
  20. */
  21. toss_wsegs(wtails[wnum], TRUE);
  22.  
  23. wheads[wnum] = wtails[wnum] = (struct wseg *) 0;
  24. }

wormhitu

  1. /*
  2. *  wormhitu()
  3. *
  4. *  Check for mon->wormno before calling this function!
  5. *
  6. *  If the hero is near any part of the worm, the worm will try to attack.
  7. */
  8. void
  9. wormhitu(worm)
  10. register struct monst *worm;
  11. {
  12. register int wnum = worm->wormno;
  13. register struct wseg *seg;
  14.  
  15. /*  if (!wnum) return;  bullet proofing */
  16.  
  17. /*  This does not work right now because mattacku() thinks that the head is
  18. *  out of range of the player.  We might try to kludge, and bring the head
  19. *  within range for a tiny moment, but this needs a bit more looking at
  20. *  before we decide to do this.
  21. */
  22. for (seg = wtails[wnum]; seg; seg = seg->nseg)
  23. 	if (distu(seg->wx, seg->wy) < 3)
  24. 	    (void) mattacku(worm);
  25. }

cutworm

  1. /*  cutworm()
  2. *
  3. *  Check for mon->wormno before calling this function!
  4. *
  5. *  When hitting a worm (worm) at position x, y, with a weapon (weap),
  6. *  there is a chance that the worm will be cut in half, and a chance
  7. *  that both halves will survive.
  8. */
  9. void
  10. cutworm(worm, x, y, weap)
  11. struct monst *worm;
  12. xchar x,y;
  13. struct obj *weap;
  14. {
  15. register struct wseg  *curr, *new_tail;
  16. register struct monst *new_worm;
  17. int wnum = worm->wormno;
  18. int cut_chance, new_wnum;
  19.  
  20. if (!wnum) return; /* bullet proofing */
  21.  
  22. if (x == worm->mx && y == worm->my) return;		/* hit on head */
  23.  
  24. /* cutting goes best with a bladed weapon */
  25. cut_chance = rnd(20);	/* Normally  1-16 does not cut */
  26. 				/* Normally 17-20 does */
  27.  
  28. if (weap && is_blade(weap))	/* With a blade 1- 6 does not cut */
  29. 	cut_chance += 10;	/*		7-20 does */
  30.  
  31. if (cut_chance < 17) return;	/* not good enough */
  32.  
  33. /* Find the segment that was attacked. */
  34. curr = wtails[wnum];
  35.  
  36. while ( (curr->wx != x) || (curr->wy != y) ) {
  37. 	curr = curr->nseg;
  38. 	if (!curr) {
  39. 	    impossible("cutworm: no segment at (%d,%d)", (int) x, (int) y);
  40. 	    return;
  41. 	}
  42. }
  43.  
  44. /* If this is the tail segment, then the worm just loses it. */
  45. if (curr == wtails[wnum]) {
  46. 	shrink_worm(wnum);
  47. 	return;
  48. }
  49.  
  50. /*
  51. *  Split the worm.  The tail for the new worm is the old worm's tail.
  52. *  The tail for the old worm is the segment that follows "curr",
  53. *  and "curr" becomes the dummy segment under the new head.
  54. */
  55. new_tail = wtails[wnum];
  56. wtails[wnum] = curr->nseg;
  57. curr->nseg = (struct wseg *) 0;	/* split the worm */
  58.  
  59. /*
  60. *  At this point, the old worm is correct.  Any new worm will have
  61. *  it's head at "curr" and its tail at "new_tail".
  62. */
  63.  
  64. /* Sometimes the tail end dies. */
  65. if (rn2(3) || !(new_wnum = get_wormno())) {
  66. 	if (flags.mon_moving)
  67. 	    pline("Part of the tail of %s is cut off.", mon_nam(worm));
  68. 	else
  69. 	    You("cut part of the tail off of %s.", mon_nam(worm));
  70. 	toss_wsegs(new_tail, TRUE);
  71. 	if (worm->mhp > 1) worm->mhp /= 2;
  72. 	return;
  73. }
  74.  
  75. remove_monster(x, y);		/* clone_mon puts new head here */
  76. new_worm = clone_mon(worm, x, y);
  77. new_worm->wormno = new_wnum;	/* affix new worm number */
  78.  
  79. /* Devalue the monster level of both halves of the worm. */
  80. worm->m_lev = ((unsigned)worm->m_lev <= 3) ?
  81. 		   (unsigned)worm->m_lev : max((unsigned)worm->m_lev - 2, 3);
  82. new_worm->m_lev = worm->m_lev;
  83.  
  84. /* Calculate the mhp on the new_worm for the (lower) monster level. */
  85. new_worm->mhpmax = new_worm->mhp = d((int)new_worm->m_lev, 8);
  86.  
  87. /* Calculate the mhp on the old worm for the (lower) monster level. */
  88. if (worm->m_lev > 3) {
  89. 	worm->mhpmax = d((int)worm->m_lev, 8);
  90. 	if (worm->mhpmax < worm->mhp) worm->mhp = worm->mhpmax;
  91. }
  92.  
  93. wtails[new_wnum] = new_tail;	/* We've got all the info right now */
  94. wheads[new_wnum] = curr;		/* so we can do this faster than    */
  95. wgrowtime[new_wnum] = 0L;		/* trying to call initworm().       */
  96.  
  97. /* Place the new monster at all the segment locations. */
  98. place_wsegs(new_worm);
  99.  
  100. if (flags.mon_moving)
  101. 	pline("%s is cut in half.", Monnam(worm));
  102. else
  103. 	You("cut %s in half.", mon_nam(worm));
  104. }

see_wsegs

  1. /*
  2. *  see_wsegs()
  3. *
  4. *  Refresh all of the segments of the given worm.  This is only called
  5. *  from see_monster() in display.c or when a monster goes minvis.  It
  6. *  is located here for modularity.
  7. */
  8. void
  9. see_wsegs(worm)
  10. struct monst *worm;
  11. {
  12. struct wseg *curr = wtails[worm->wormno];
  13.  
  14. /*  if (!mtmp->wormno) return;  bullet proofing */
  15.  
  16. while (curr != wheads[worm->wormno]) {
  17. 	newsym(curr->wx,curr->wy);
  18. 	curr = curr->nseg;
  19. }
  20. }

detect_wsegs

  1. /*
  2. *  detect_wsegs()
  3. *
  4. *  Display all of the segments of the given worm for detection.
  5. */
  6. void
  7. detect_wsegs(worm, use_detection_glyph)
  8. struct monst *worm;
  9. boolean use_detection_glyph;
  10. {
  11. int num;
  12. struct wseg *curr = wtails[worm->wormno];
  13.  
  14. /*  if (!mtmp->wormno) return;  bullet proofing */
  15.  
  16. while (curr != wheads[worm->wormno]) {
  17. 	num = use_detection_glyph ?
  18. 		detected_monnum_to_glyph(what_mon(PM_LONG_WORM_TAIL)) :
  19. 		monnum_to_glyph(what_mon(PM_LONG_WORM_TAIL));
  20. 	show_glyph(curr->wx,curr->wy,num);
  21. 	curr = curr->nseg;
  22. }
  23. }

save_worm

  1. /*
  2. *  save_worm()
  3. *
  4. *  Save the worm information for later use.  The count is the number
  5. *  of segments, including the dummy.  Called from save.c.
  6. */
  7. void
  8. save_worm(fd, mode)
  9. int fd, mode;
  10. {
  11. int i;
  12. int count;
  13. struct wseg *curr, *temp;
  14.  
  15. if (perform_bwrite(mode)) {
  16. 	for (i = 1; i < MAX_NUM_WORMS; i++) {
  17. 	    for (count = 0, curr = wtails[i]; curr; curr = curr->nseg) count++;
  18. 	    /* Save number of segments */
  19. 	    bwrite(fd, (genericptr_t) &count, sizeof(int));
  20. 	    /* Save segment locations of the monster. */
  21. 	    if (count) {
  22. 		for (curr = wtails[i]; curr; curr = curr->nseg) {
  23. 		    bwrite(fd, (genericptr_t) &(curr->wx), sizeof(xchar));
  24. 		    bwrite(fd, (genericptr_t) &(curr->wy), sizeof(xchar));
  25. 		}
  26. 	    }
  27. 	}
  28. 	bwrite(fd, (genericptr_t) wgrowtime, sizeof(wgrowtime));
  29. }
  30.  
  31. if (release_data(mode)) {
  32. 	/* Free the segments only.  savemonchn() will take care of the
  33. 	 * monsters. */
  34. 	for (i = 1; i < MAX_NUM_WORMS; i++) {
  35. 	    if (!(curr = wtails[i])) continue;
  36.  
  37. 	    while (curr) {
  38. 		temp = curr->nseg;
  39. 		dealloc_seg(curr);		/* free the segment */
  40. 		curr = temp;
  41. 	    }
  42. 	    wheads[i] = wtails[i] = (struct wseg *) 0;
  43. 	}
  44. }
  45.  
  46. }

rest_worm

  1. /*
  2. *  rest_worm()
  3. *
  4. *  Restore the worm information from the save file.  Called from restore.c
  5. */
  6. void
  7. rest_worm(fd)
  8. int fd;
  9. {
  10. int i, j, count;
  11. struct wseg *curr, *temp;
  12.  
  13. for (i = 1; i < MAX_NUM_WORMS; i++) {
  14. 	mread(fd, (genericptr_t) &count, sizeof(int));
  15. 	if (!count) continue;	/* none */
  16.  
  17. 	/* Get the segments. */
  18. 	for (curr = (struct wseg *) 0, j = 0; j < count; j++) {
  19. 	    temp = newseg();
  20. 	    temp->nseg = (struct wseg *) 0;
  21. 	    mread(fd, (genericptr_t) &(temp->wx), sizeof(xchar));
  22. 	    mread(fd, (genericptr_t) &(temp->wy), sizeof(xchar));
  23. 	    if (curr)
  24. 		curr->nseg = temp;
  25. 	    else
  26. 		wtails[i] = temp;
  27. 	    curr = temp;
  28. 	}
  29. 	wheads[i] = curr;
  30. }
  31. mread(fd, (genericptr_t) wgrowtime, sizeof(wgrowtime));
  32. }

place_wsegs

  1. /*
  2. *  place_wsegs()
  3. *
  4. *  Place the segments of the given worm.  Called from restore.c
  5. */
  6. void
  7. place_wsegs(worm)
  8. struct monst *worm;
  9. {
  10. struct wseg *curr = wtails[worm->wormno];
  11.  
  12. /*  if (!mtmp->wormno) return;  bullet proofing */
  13.  
  14. while (curr != wheads[worm->wormno]) {
  15. 	place_worm_seg(worm,curr->wx,curr->wy);
  16. 	curr = curr->nseg;
  17. }
  18. }

remove_worm

  1. /*
  2. *  remove_worm()
  3. *
  4. *  This function is equivalent to the remove_monster #define in
  5. *  rm.h, only it will take the worm *and* tail out of the levels array.
  6. *  It does not get rid of (dealloc) the worm tail structures, and it does
  7. *  not remove the mon from the fmon chain.
  8. */
  9. void
  10. remove_worm(worm)
  11. register struct monst *worm;
  12. {
  13. register struct wseg *curr = wtails[worm->wormno];
  14.  
  15. /*  if (!mtmp->wormno) return;  bullet proofing */
  16.  
  17. while (curr) {
  18. 	remove_monster(curr->wx, curr->wy);
  19. 	newsym(curr->wx, curr->wy);
  20. 	curr = curr->nseg;
  21. }
  22. }

place_worm_tail_randomly

  1. /*
  2. *  place_worm_tail_randomly()
  3. *
  4. *  Place a worm tail somewhere on a level behind the head.
  5. *  This routine essentially reverses the order of the wsegs from head
  6. *  to tail while placing them.
  7. *  x, and y are most likely the worm->mx, and worm->my, but don't *need* to
  8. *  be, if somehow the head is disjoint from the tail.
  9. */
  10. void
  11. place_worm_tail_randomly(worm, x, y)
  12. struct monst *worm;
  13. xchar x, y;
  14. {
  15. int wnum = worm->wormno;
  16. struct wseg *curr = wtails[wnum];
  17. struct wseg *new_tail;
  18. register xchar ox = x, oy = y;
  19.  
  20. /*  if (!wnum) return;  bullet proofing */
  21.  
  22. if (wnum && (!wtails[wnum] || !wheads[wnum]) ) {
  23. 	impossible("place_worm_tail_randomly: wormno is set without a tail!");
  24. 	return;
  25. }
  26.  
  27. wheads[wnum] = new_tail = curr;
  28. curr = curr->nseg;
  29. new_tail->nseg = (struct wseg *) 0;
  30. new_tail->wx = x;
  31. new_tail->wy = y;
  32.  
  33. while(curr)  {
  34. 	xchar nx, ny;
  35. 	char tryct = 0;
  36.  
  37. 	/* pick a random direction from x, y and search for goodpos() */
  38.  
  39. 	do {
  40. 	    random_dir(ox, oy, &nx, &ny);
  41. 	} while (!goodpos(nx, ny, worm, 0) && (tryct++ < 50));
  42.  
  43. 	if (tryct < 50)  {
  44. 	    place_worm_seg(worm, nx, ny);
  45. 	    curr->wx = ox = nx;
  46. 	    curr->wy = oy = ny;
  47. 	    wtails[wnum] = curr;
  48. 	    curr = curr->nseg;
  49. 	    wtails[wnum]->nseg = new_tail;
  50. 	    new_tail = wtails[wnum];
  51. 	    newsym(nx, ny);
  52. 	} else {			/* Oops.  Truncate because there was */
  53. 	    toss_wsegs(curr, FALSE);    /* no place for the rest of it */
  54. 	    curr = (struct wseg *) 0;
  55. 	}
  56. }
  57. }

random_dir

  1. /*
  2. * Given a coordinate x, y.
  3. * return in *nx, *ny, the coordinates of one of the <= 8 squares adjoining.
  4. *
  5. * This function, and the loop it serves, could be eliminated by coding
  6. * enexto() with a search radius.
  7. */
  8. STATIC_OVL
  9. void
  10. random_dir(x, y, nx, ny)
  11. register xchar   x,   y;
  12. register xchar *nx, *ny;
  13. {
  14. *nx = x;
  15. *ny = y;
  16.  
  17. *nx += (x > 1 ?			/* extreme left ? */
  18. 		(x < COLNO ?		 /* extreme right ? */
  19. 			(rn2(3) - 1)	  /* neither so +1, 0, or -1 */
  20. 		:	-rn2(2))	 /* 0, or -1 */
  21. 	   :	rn2(2));		/* 0, or 1 */
  22.  
  23. *ny += (*nx == x ?			/* same kind of thing with y */
  24. 		(y > 1 ?
  25. 		    (y < ROWNO ?
  26. 			(rn2(2) ?
  27. 			    1
  28. 			:   -1)
  29. 		    :	-1)
  30. 		:   1)
  31. 	    :	(y > 1 ?
  32. 		    (y < ROWNO ?
  33. 			(rn2(3) - 1)
  34. 		    :	-rn2(2))
  35. 		:   rn2(2)));
  36. }

count_wsegs

  1. /*  count_wsegs()
  2. *
  3. *  returns
  4. *  the number of visible segments that a worm has.
  5. */
  6.  
  7. int
  8. count_wsegs(mtmp)
  9. struct monst *mtmp;
  10. {
  11. register int i=0;
  12. register struct wseg *curr = (wtails[mtmp->wormno])->nseg;
  13.  
  14. /*  if (!mtmp->wormno) return 0;  bullet proofing */
  15.  
  16. while (curr) {
  17. 	i++;
  18. 	curr = curr->nseg;
  19. }
  20.  
  21. return i;
  22. }

create_worm_tail

  1. /*  create_worm_tail()
  2. *
  3. *  will create a worm tail chain of (num_segs + 1) and return a pointer to it.
  4. */
  5. STATIC_OVL
  6. struct wseg *
  7. create_worm_tail(num_segs)
  8. int num_segs;
  9. {
  10. register int i=0;
  11. register struct wseg *new_tail, *curr;
  12.  
  13. if (!num_segs) return (struct wseg *)0;
  14.  
  15. new_tail = curr = newseg();
  16. curr->nseg = (struct wseg *)0;
  17. curr->wx = 0;
  18. curr->wy = 0;
  19.  
  20. while (i < num_segs) {
  21. 	curr->nseg = newseg();
  22. 	curr = curr->nseg;
  23. 	curr->nseg = (struct wseg *)0;
  24. 	curr->wx = 0;
  25. 	curr->wy = 0;
  26. 	i++;
  27. }
  28.  
  29. return (new_tail);
  30. }

worm_known

  1. /*  worm_known()
  2. *
  3. *  Is any segment of this worm in viewing range?  Note: caller must check
  4. *  invisibility and telepathy (which should only show the head anyway).
  5. *  Mostly used in the canseemon() macro.
  6. */
  7. boolean
  8. worm_known(worm)
  9. struct monst *worm;
  10. {
  11. struct wseg *curr = wtails[worm->wormno];
  12.  
  13. while (curr) {
  14. 	if(cansee(curr->wx,curr->wy)) return TRUE;
  15. 	curr = curr->nseg;
  16. }
  17. return FALSE;
  18. }
  19.  
  20. /*worm.c*/