+ return ((double) r / 32768.);
+}
+
+static int
+draw_worm(WINDOW *win, void *data)
+{
+ WORM *w = (WORM *) data;
+ const struct options *op;
+ unsigned mask = (unsigned) (~(1 << (w - worm)));
+ chtype attrs = w->attrs | ((mask & pending) ? A_REVERSE : 0);
+
+ int x;
+ int y;
+ int h;
+
+ bool done = FALSE;
+
+ if ((x = w->xpos[h = w->head]) < 0) {
+ wmove(win, y = w->ypos[h] = last_y, x = w->xpos[h] = 0);
+ waddch(win, attrs);
+ refs[y][x]++;
+ } else {
+ y = w->ypos[h];
+ }
+
+ if (x > last_x)
+ x = last_x;
+ if (y > last_y)
+ y = last_y;
+
+ 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 == last_y
+ ? lowleft
+ : left))
+ : (x == last_x
+ ? (y == 0
+ ? upright
+ : (y == last_y
+ ? lowright
+ : right))
+ : (y == 0
+ ? upper
+ : (y == last_y
+ ? 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() * (double) op->nopts)];
+ break;
+ }
+
+ if (!done) {
+ x += xinc[w->orientation];
+ y += yinc[w->orientation];
+ wmove(win, y, x);
+
+ if (y < 0)
+ y = 0;
+ waddch(win, attrs);
+
+ w->ypos[h] = y;
+ w->xpos[h] = x;
+ refs[y][x]++;
+ }
+
+ return done;
+}
+
+#ifdef USE_PTHREADS
+static bool
+quit_worm(int bitnum)
+{
+ pending |= (1 << bitnum);
+ napms(10); /* let the other thread(s) have a chance */
+ pending &= ~(1 << bitnum);
+ return quitting;
+}
+
+static void *
+start_worm(void *arg)
+{
+ unsigned long compare = 0;
+ Trace(("start_worm"));
+ while (!quit_worm(((struct worm *) arg) - worm)) {
+ while (compare < sequence) {
+ ++compare;
+#if HAVE_USE_WINDOW
+ use_window(stdscr, draw_worm, arg);
+#else
+ draw_worm(stdscr, arg);
+#endif
+ }
+ }
+ Trace(("...start_worm (done)"));
+ return NULL;
+}
+#endif
+
+static bool
+draw_all_worms(void)
+{
+ bool done = FALSE;
+ int n;
+ struct worm *w;
+
+#ifdef USE_PTHREADS
+ static bool first = TRUE;
+ if (first) {
+ first = FALSE;
+ for (n = 0, w = &worm[0]; n < number; n++, w++) {
+ (void) pthread_create(&(w->thread), NULL, start_worm, w);
+ }
+ }
+#else
+ for (n = 0, w = &worm[0]; n < number; n++, w++) {
+ if (
+#if HAVE_USE_WINDOW
+ USING_WINDOW2(stdscr, draw_worm, w)
+#else
+ draw_worm(stdscr, w)
+#endif
+ )
+ done = TRUE;
+ }
+#endif
+ return done;
+}
+
+static int
+get_input(void)
+{
+ int ch;
+ ch = USING_WINDOW(stdscr, wgetch);
+ return ch;