* place_worm_tail_randomly() * * Place a worm tail somewhere on a level behind the head. * This routine essentially reverses the order of the wsegs from head * to tail while placing them. * x, and y are most likely the worm->mx, and worm->my, but don't *need* to * be, if somehow the head is disjoint from the tail. */
| 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 |
no test coverage detected