/*
	worms.C

	Version 1.0

	A Peculiar Demonstration of the Hercules Library

	Based on a program developed in the Computer Recreations
	column of Scientific American (December 1987).

	Developed with: Borland C++ 3.1
*/

#if !defined(__LARGE__)
#error Large Memory Model Expected
#endif

#include <conio.H>
#include <dos.H>
#include <math.H>
#include <stdlib.H>

#include "hlib.H"

void main (void)
{
   double dir = 0.0;
   int newx, newy, otail, tail = 0, x, xcirc [25], y, ycirc [25];

   if (!hexist ())
   {
       cputs ("worms: Hercules adaptor not found.\n");
       return;
   }

   xcirc [0] = 360;
   ycirc [0] = VSIZE/2;

   hon ();
   _hsetstate (BLACK_ON_WHITE);

   do
   {
      otail = tail;
      tail = (tail+1) % 25;
      hcircle (xcirc [tail], ycirc [tail], 4);
      dir = dir + ((random(2)) ? 0.1745 : -0.1745);
      x = xcirc [otail];
      y = ycirc [otail];
      newx = x+4*cos (dir);
      newy = y+4*sin (dir);
      if (newx < 0)
	  newx += 720;
      if (newy < 0)
	  newy += VSIZE;
      newx %= 720;
      newy %= VSIZE;
      xcirc [tail] = newx;
      ycirc [tail] = newy;
      _hsetstate (WHITE_ON_BLACK);
      hcircle (newx, newy, 4);
      _hsetstate (BLACK_ON_WHITE);
      delay (15);
   }
   while (!kbhit ());

   if (!getch ())
       (void) getch ();

   hoff ();
}