home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsf / gol / !Gol / c / life < prev   
Text File  |  1993-10-22  |  2KB  |  98 lines

  1. #include "$.Apps.C.DeskTopC.RISC_OSLib.h.akbd" 
  2. #include "kernel.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "life.h"
  7.  
  8. extern void GOL_err(const char *format,...);
  9.  
  10. #define bit_collect(bmap, bits) \
  11.         ((bmap[((bits>>0)&7) | ((bits>>3)&56) | ((bits>>6)&448)]?1:0) \
  12.         |(bmap[((bits>>1)&7) | ((bits>>4)&56) | ((bits>>7)&448)]?2:0) \
  13.         |(bmap[((bits>>2)&7) | ((bits>>5)&56) | ((bits>>8)&448)]?4:0) \
  14.         |(bmap[((bits>>3)&7) | ((bits>>6)&56) | ((bits>>9)&448)]?8:0))
  15.  
  16. void life_table(rules, table)
  17.      char  *rules;
  18.      char  *table;
  19.  
  20.      {Value count;
  21.  
  22.       for (count=0x40000; count--; ) table[count]=bit_collect(rules, count);
  23.      }
  24.  
  25. void life_count(rules, counts, mask)
  26.      char  *rules;
  27.      Value *counts;
  28.      Value  mask;
  29.  
  30.      {Value count;
  31.       Value bits;
  32.  
  33.       for (count=512; count--; )
  34.           {bits=count&mask;
  35.            bits=((bits>>0)&1)+((bits>>1)&1)+((bits>>2)&1)+
  36.                 ((bits>>3)&1)+((bits>>4)&1)+((bits>>5)&1)+
  37.                 ((bits>>6)&1)+((bits>>7)&1)+((bits>>8)&1);
  38.            if (counts[bits]==lSET)        rules[count]=1;
  39.            else if (counts[bits]==lLEAVE) rules[count]=(count>>4)&1;
  40.            else                           rules[count]=0;
  41.           }
  42.      }
  43.  
  44. void life_wrap(wh, base)
  45.      u_int    wh;
  46.      u_int *base;
  47.  
  48.      {u_int  *temp;
  49.       u_int  t1,t2;
  50.       u_int  count;
  51.       u_int  width=(wh&0xffff)>>2;
  52.       u_int height=(wh>>16);
  53.  
  54.       temp=base;
  55.       for (count=height-2; count--;)
  56.           {temp=&(temp[width]);
  57.            t1=temp[0];  t2=temp[width-1];
  58.            t1|=(t2>>30)&1; t2|=(t1&2)<<30;
  59.            temp[0]=t1;  temp[width-1]=t2;
  60.           }
  61.       temp=base;
  62.       memcpy(temp, &(temp[(height-2)*width]), width*4); temp=&(temp[width]);
  63.       memcpy(&(temp[(height-2)*width]), temp, width*4);
  64.      }
  65.  
  66. void life_unwrap(wh, base)
  67.      u_int    wh;
  68.      u_int *base;
  69.  
  70.      {u_int  width=(wh&0xffff)>>2;
  71.       u_int height=(wh>>16);
  72.  
  73.       memset((char *)&(base[0]), 0, width<<2);
  74.       base=&(base[width]);
  75.       for (wh=height-2; wh--; )
  76.           {*(base+0)&=0xfffffffe;
  77.            base+=width;
  78.            *(base-1)&=0x7fffffff;
  79.           }
  80.       memset((char *)&(base[0]), 0, width<<2);
  81.      }
  82.  
  83. void life_random(wh, base, seed)
  84.      u_int    wh;
  85.      u_int *base;
  86.      u_int  seed;
  87.  
  88.      {u_int  size=((wh&0xffff)>>2)*(wh>>16);
  89.       u_int  v,c;
  90.  
  91.       RANDOMIZE(v, c, seed);
  92.       while (size--) base[size]=RANDOM(v, c);
  93.      }
  94.  
  95.  
  96.  
  97.  
  98.