#include "csim.h"
#include   <bios.h>
#include   <conio.h>
#include <graphics.h>
#include   <stdio.h>
#include   <stdlib.h>
#include   <time.h>

void      mover(void);
void      ender(void);

int      color = 1;
int      maxx,maxy;

int      main(void)
   {
      int   loop;
      int   gr_driver,gr_mode,gr_error;

      clrscr();
      randomize();

      for(loop = 0;loop < 4;loop++)
         if(start_process(mover,0x0080))
            exit_processing(loop);

      if(start_kb_process(ender,0x0040))
         exit_processing(3);

      gr_driver = DETECT;
      gr_mode = VGAHI;

      initgraph(&gr_driver,&gr_mode,"");
      gr_error = graphresult();
      if (gr_error != 0)
         {
            printf("%s \n",grapherrormsg(gr_error));
            exit(1);
         }
      maxx = getmaxx();
      maxy = getmaxy();

      sim_start();
      return(0);
   }

void   mover(void)
   {
      int   mycolor;
      int   x,y,oldx,oldy,dir;
      if(color == 7)
         color += 2;
      mycolor = color++;
      color %= 16;
      if(color == 0)
         color++;
      x = oldx = (rand() % (maxx-10)) + 5;
      y = oldy = (rand() % (maxy-10)) + 5;
      dir = rand() % 4;
      while(1)
         {
            switch(rand()%6)
               {
                  case   0:
                     dir+=7;
                     dir = dir % 8;
                     break;
                  case  1:
                     dir++;
                     dir = dir % 8;
                     break;
                  case  2:
                     dir+=2;
                     dir = dir % 8;
                     break;
                  case  3:
                     dir+=6;
                     dir = dir % 8;
                     break;
                  default:
                     break;

               }
            switch(dir)
               {
                  case 0:
                     x += 4;
                     if(x>(maxx-5))
                        dir = 4;
                     break;
                  case 1:
                     y += 3;
                     if(y>(maxy-5))
                        dir = 6;
                     x += 3;
                     if(x>(maxx-5))
                        dir = 5;
                     break;
                  case 2:
                     y += 4;
                     if(y>(maxy-5))
                        dir = 6;
                     break;
                  case 3:
                     x -= 3;
                     if(x<5)
                        dir = 0;
                     y += 3;
                     if(y>(maxy-5))
                        dir = 7;
                     break;
                  case 4:
                     x -= 4;
                     if(x<5)
                        dir = 0;
                     break;
                  case 5:
                     y -= 3;
                     if(y<5)
                        dir = 2;
                     x -= 3;
                     if(x<5)
                        dir = 3;
                     break;
                  case 6:
                     y -= 4;
                     if(y<5)
                        dir = 2;
                     break;
                  case 7:
                     x += 3;
                     if(x>maxx-5)
                        dir = 4;
                     y -= 3;
                     if(y<5)
                        dir = 3;
                     break;
               }
            setlinestyle(SOLID_LINE,0,3);
            setcolor(BLACK);
            line(oldx,oldy,x,y);
            setlinestyle(SOLID_LINE,0,1);
            setcolor(mycolor);
            line(oldx,oldy,x,y);
            oldx = x;
            oldy = y;
            wait_for_time(0);
         }
   }
void   ender(void)
   {
      while(!bioskey(1))
         wait_for_time(100);
      closegraph();
      exit_processing(0);
   }
