MCPcopy Create free account
hub / github.com/NetHack/NetHack / random_dir

Function random_dir

src/worm.c:802–822  ·  view source on GitHub ↗

* Given a coordinate x, y. * return in *nx, *ny, the coordinates of one of the <= 8 squares adjoining. * * This function, and the loop it serves, could be eliminated by coding * enexto() with a search radius. */

Source from the content-addressed store, hash-verified

800 * enexto() with a search radius.
801 */
802staticfn void
803random_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 */

Callers

nothing calls this directly

Calls 1

rn2Function · 0.85

Tested by

no test coverage detected