+static int
+draw_worm(WINDOW *win, void *data)
+{
+ WORM *w = (WORM *) data;
+ const struct options *op;
+
+ int x;
+ int y;
+ int h;
+
+ int bottom = LINES - 1;
+ int last = COLS - 1;
+
+ bool done = FALSE;
+
+ if ((x = w->xpos[h = w->head]) < 0) {
+ wmove(win, y = w->ypos[h] = bottom, x = w->xpos[h] = 0);
+ waddch(win, w->attrs);
+ refs[y][x]++;
+ } else {
+ y = w->ypos[h];
+ }
+
+ if (x > last)
+ x = last;
+ if (y > bottom)
+ y = bottom;
+
+ if (++h == length)
+ h = 0;
+
+ if (w->xpos[w->head = h] >= 0) {
+ int x1, y1;
+ x1 = w->xpos[h];
+ y1 = w->ypos[h];
+ if (y1 < LINES
+ && x1 < COLS
+ && --refs[y1][x1] == 0) {
+ wmove(win, y1, x1);
+ waddch(win, trail);
+ }
+ }
+
+ op = &(x == 0
+ ? (y == 0
+ ? upleft
+ : (y == bottom
+ ? lowleft
+ : left))
+ : (x == last
+ ? (y == 0
+ ? upright
+ : (y == bottom
+ ? lowright
+ : right))
+ : (y == 0
+ ? upper
+ : (y == bottom
+ ? lower
+ : normal))))[w->orientation];
+
+ switch (op->nopts) {
+ case 0:
+ done = TRUE;
+ break;
+ case 1:
+ w->orientation = op->opts[0];
+ break;
+ default:
+ w->orientation = op->opts[(int) (ranf() * (float) op->nopts)];
+ break;
+ }
+
+ if (!done) {
+ x += xinc[w->orientation];
+ y += yinc[w->orientation];
+ wmove(win, y, x);
+
+ if (y < 0)
+ y = 0;
+ waddch(win, w->attrs);
+
+ w->ypos[h] = y;
+ w->xpos[h] = x;
+ refs[y][x]++;
+ }
+
+ return done;
+}
+
+#if !defined(NCURSES_VERSION_PATCH) || (NCURSES_VERSION_PATCH < 20070915)
+static int
+use_window(WINDOW *win, int (*func)(WINDOW *, void *), void *data)
+{
+ return func(win, data);
+}
+#endif
+