home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / VGAKIT60.ZIP / grav.c < prev    next >
Text File  |  1994-01-01  |  5KB  |  330 lines

  1. /*
  2.     VGAKIT Version 6.0
  3.  
  4.     Copyright 1988,89,90,91,92,93,94 John Bridges
  5.     Free for use in commercial, shareware or freeware applications
  6.  
  7.     GRAV.C
  8. */
  9.  
  10. extern long atol();
  11.  
  12. extern int maxx,maxy;
  13.  
  14. #define TICKS  (*(unsigned int far *)0x0040006cl)
  15. #define KBHEAD (*(unsigned int far *)0x0040001al)
  16. #define KBTAIL (*(unsigned int far *)0x0040001cl)
  17.  
  18. #define MAXOBJS (256)
  19.  
  20. double topspeed=100000000.;
  21.  
  22. long steps=1000000l;
  23.  
  24. long speed=1000;
  25.  
  26. #define XSIZE maxx
  27. #define YSIZE maxy
  28.  
  29. int MOVOBJS=7;
  30. int FIXOBJS=1;
  31.  
  32. #define OBJS (MOVOBJS+FIXOBJS)
  33.  
  34. extern double sqrt(double v);
  35.  
  36. double xpos[MAXOBJS];
  37. double ypos[MAXOBJS];
  38. double mass[MAXOBJS];
  39.  
  40. double speeddiv=1000000.;
  41.  
  42. int massbase=40;
  43. int massrnd=50;
  44.  
  45. int charge[MAXOBJS];    /* Can be 1 or -1, or 0 (always) or 2 (never) */
  46.  
  47. int ox[MAXOBJS];
  48. int oy[MAXOBJS];
  49.  
  50. double deltax[MAXOBJS];
  51. double deltay[MAXOBJS];
  52.  
  53. double scale=1.;
  54.  
  55. unsigned char cols[]={9,10,11,12,13,14,1,2,3,4,5,6,7,15};
  56.  
  57. #define MAX (10000.)
  58.  
  59. void repos(int i,int ia)
  60. {
  61.     double dx;
  62.     double dy;
  63.     double d;
  64.     double addx,addy;
  65.     double v,mm;
  66.  
  67.     if(ia>=OBJS) ia-=OBJS;
  68.     dx=xpos[ia]-xpos[i];
  69.     dy=ypos[ia]-ypos[i];
  70.     d=sqrt(dx*dx+dy*dy);
  71.     if(d<MAX)
  72.     {
  73.         mm=((MAX*MAX-d*d)*mass[ia])/(MAX*MAX*speeddiv);
  74.         addx=mm*dx;
  75.         addy=mm*dy;
  76.         if((charge[ia] && charge[i]==charge[ia]) || charge[ia]==2)
  77.         {
  78.             deltax[i]-=addx;
  79.             deltay[i]-=addy;
  80.         }
  81.         else
  82.         {
  83.             deltax[i]+=addx;
  84.             deltay[i]+=addy;
  85.         }
  86.     }
  87. }
  88.  
  89. static int midx,midy;
  90.  
  91. blob(int i)
  92. {
  93.     int xx,yy;
  94.     int col;
  95.  
  96.     if(i<sizeof(cols))
  97.         col=cols[i];
  98.     else
  99.         col=(i-sizeof(cols))+32;
  100.     xx=ox[i];
  101.     yy=oy[i];
  102.     xx+=midx;
  103.     yy+=midy;
  104.     xpoint(xx+4,yy+3,col);
  105.     xpoint(xx+3,yy+3,col);
  106.     xpoint(xx+2,yy+3,col);
  107.     xpoint(xx+1,yy+3,col);
  108.     xpoint(xx,yy+3,col);
  109.     xpoint(xx-1,yy+3,col);
  110.     xpoint(xx-2,yy+3,col);
  111.     xpoint(xx-3,yy+3,col);
  112.     xpoint(xx-4,yy+3,col);
  113.  
  114.     xpoint(xx+3,yy+2,col);
  115.     xpoint(xx+2,yy+2,col);
  116.     xpoint(xx+1,yy+2,col);
  117.     xpoint(xx,yy+2,col);
  118.     xpoint(xx-1,yy+2,col);
  119.     xpoint(xx-2,yy+2,col);
  120.     xpoint(xx-3,yy+2,col);
  121.  
  122.     xpoint(xx+2,yy+1,col);
  123.     xpoint(xx+1,yy+1,col);
  124.     xpoint(xx,yy+1,col);
  125.     xpoint(xx-1,yy+1,col);
  126.     xpoint(xx-2,yy+1,col);
  127.  
  128.     xpoint(xx+1,yy,col);
  129.     xpoint(xx,yy,col);
  130.     xpoint(xx-1,yy,col);
  131.     
  132.     xpoint(xx,yy-1,col);
  133. }
  134.  
  135. extern unsigned int rand(void);
  136.  
  137. randomize()
  138. {
  139.     srand(TICKS);
  140. }
  141.  
  142. #define rchoose(i) (rand()%(i))
  143.  
  144.  
  145. main(argc,argv)
  146. int argc;
  147. char **argv;
  148. {
  149.     int n;
  150.     int i;
  151.     int key;
  152.     int svga;
  153.     int chand;
  154.     int first;
  155.     unsigned long oticks;
  156.     long frames;
  157.     long clocks;
  158.     long count;
  159.     int xx,yy;
  160.  
  161.     if(--argc)
  162.     {
  163.         MOVOBJS=atol(*++argv);
  164.         if(--argc)
  165.         {
  166.             FIXOBJS=atol(*++argv);
  167.             if(--argc)
  168.             {
  169.                 speed=atol(*++argv);
  170.                 if(--argc)
  171.                 {
  172.                     massbase=atol(*++argv);
  173.                     if(--argc)
  174.                     {
  175.                         massrnd=atol(*++argv);
  176.                         if(--argc)
  177.                         {
  178.                             steps=atol(*++argv);
  179.                         }
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.     }
  185.     speeddiv=topspeed/speed;
  186.     chand=1;
  187.     if(MOVOBJS<0)
  188.     {
  189.         chand=0;
  190.         MOVOBJS=-MOVOBJS;
  191.     }
  192.     if(!FIXOBJS)
  193.         scale=100;
  194.     svga=whichvga();    
  195.     if(svga)
  196.     {
  197.         svga640();
  198.     }
  199.     else
  200.     {
  201.         mode13x();
  202.     }
  203.     midx=XSIZE/2;
  204.     midy=YSIZE/2;
  205.     first=1;
  206.     frames=0;
  207. redo:    key=0;
  208.     for(i=0;i<OBJS;i++)
  209.     {
  210.         deltax[i]=0;
  211.         deltay[i]=0;
  212.     }
  213.     count=steps;
  214.     for(i=0;i<MOVOBJS;i++)
  215.     {
  216.         xx=rchoose(XSIZE)-midx;
  217.         yy=rchoose(YSIZE)-midy;
  218.         xpos[i]=xx;
  219.         ypos[i]=yy;
  220.         xx=xpos[i]/scale;
  221.         yy=ypos[i]/scale;
  222.         ox[i]=xx;
  223.         oy[i]=yy;
  224.         mass[i]=(rchoose(massrnd)+massbase)/100.;
  225.         charge[i]=0;
  226.         if(chand)
  227.         {
  228.             if(i&1)
  229.                 charge[i]=1;
  230.             else
  231.                 charge[i]=-1;
  232.         }
  233.         blob(i);
  234.     }
  235.     if(FIXOBJS)
  236.     {
  237.         xpos[MOVOBJS]=0;
  238.         ypos[MOVOBJS]=0;
  239.         mass[MOVOBJS]=2.;
  240.         charge[MOVOBJS]=0;
  241.     }
  242.     for(i=MOVOBJS+1;i<OBJS;i++)
  243.     {
  244.         xpos[i]=rchoose(XSIZE)-midx;
  245.         ypos[i]=rchoose(YSIZE)-midy;
  246.         mass[i]=2.;
  247.         charge[i]=0;
  248.         ++i;
  249.     }
  250.     oticks=TICKS;
  251.     while(oticks==TICKS)
  252.         ;
  253.     while(key!=27 && key!=13 && count)
  254.     {
  255.         --count;
  256.         for(i=0;i<MOVOBJS;i++)
  257.         {
  258.             xx=xpos[i]/scale;
  259.             yy=ypos[i]/scale;
  260.             if(xx!=ox[i] || yy!=oy[i])
  261.             {
  262.                 blob(i);
  263.                 ox[i]=xx;
  264.                 oy[i]=yy;
  265.                 blob(i);
  266.             }
  267.             for(n=1;n<OBJS;n++)
  268.                 repos(i,i+n);
  269.             xpos[i]+=deltax[i];
  270.             ypos[i]+=deltay[i];
  271.         }
  272.         if(first)
  273.         {
  274.             ++frames;
  275.             clocks=TICKS-oticks;
  276.             if(clocks<0)
  277.                 clocks+=0x001800b0l;
  278.             if(clocks>50)
  279.             {
  280.                 speed=(speed*1238.)/frames;
  281.                 speeddiv=topspeed/speed;
  282.                 first=0;
  283.                 randomize();
  284.                 count=0;
  285.                 break;
  286.             }
  287.         }
  288.         else
  289.         {
  290.             if(KBHEAD!=KBTAIL)
  291.             {
  292.                 key=getch();
  293.             }
  294.             if(key)    switch(key)
  295.             {
  296.                 case 13:
  297.                     break;
  298.                 case 27:
  299.                     break;
  300.                 case '+':
  301.                     scale=scale/1.414213562;
  302.                     key=0;
  303.                     break;
  304.                 case '-':
  305.                     scale=scale*1.414213562;
  306.                     key=0;
  307.                     break;
  308.                 case '[':
  309.                     scale=scale*1.001;
  310.                     break;
  311.                 case ']':
  312.                     scale=scale/1.001;
  313.                     break;
  314.                 default:
  315.                     key=0;
  316.             }
  317.         }
  318.     }
  319.     for(i=0;i<MOVOBJS;i++)
  320.     {
  321.         blob(i);
  322.     }
  323.     if(!count || key!=27)
  324.         goto redo;
  325.     txtmode();
  326. }
  327.  
  328.  
  329.  
  330.