home *** CD-ROM | disk | FTP | other *** search
/ Supergames for Windows 3 / wingames3.zip / wingames3 / U-Z / YALIFE / CELL.H < prev    next >
Text File  |  1993-09-08  |  3KB  |  76 lines

  1. // (c) Jean MICHEL June 1993 -- any modifications must be signaled to the
  2. // author
  3.  
  4. // cell.h: classes to compute life generations. This is completely
  5. // independant from windows, and can be used on any system
  6.  
  7. typedef int BOOL;
  8. #define FALSE 0
  9. #define TRUE 1
  10.  
  11. #define INTCELL unsigned long
  12. #define BASE 0x10000l
  13. #define HALFBASE 0x8000l
  14. /* point (x,y) coded as (x+HALFBASE)+BASE*(y+HALFBASE)
  15.              (origin (-HALFBASE,-HALFBASE)),
  16.    sorted, end marked by INFINI */
  17.  
  18. class cell_type
  19. { public:
  20.   unsigned x,y;
  21.   cell_type(){x=y=0;}
  22.   cell_type(INTCELL l){*(INTCELL*)this=l;}
  23.   cell_type(unsigned xx,unsigned yy){x=xx+HALFBASE;y=yy+HALFBASE;}
  24.   cell_type operator +(cell_type c){return cell_type(x+c.x,y+c.y);}
  25.   cell_type operator -(cell_type c){return cell_type(x-c.x,y-c.y);}
  26.   operator INTCELL(){return *(INTCELL*)this;}
  27.   BOOL operator ==(cell_type c){return *(INTCELL*)this==(INTCELL)c;}
  28.   BOOL operator !=(cell_type c){return *(INTCELL*)this!=(INTCELL)c;}
  29.   BOOL operator <(cell_type c){return *(INTCELL*)this<(INTCELL)c;}
  30.   BOOL operator >(cell_type c){return *(INTCELL*)this>(INTCELL)c;}
  31.   int cellx(){return (int)(x-HALFBASE);}
  32.   int celly(){return (int)(y-HALFBASE);}
  33. };
  34.  
  35. enum {UNSET,SET,RESET};
  36. enum {LAY_ON=1,LAY_OFF=2,LAY_XOR=3};
  37.  
  38. // cellpop: a population of cells
  39.  
  40. class cellpop
  41. { public:
  42.   cell_type *v;
  43.   int pop;                // number of cells
  44.   cellpop();              // default constructor (empty population)
  45.   cellpop(cell_type *);   // constructor from a vector of cell_type
  46.   cellpop(char *);        // constructor from a string
  47.   cellpop(int n);         // constructs a line of n cells
  48.   ~cellpop();
  49.   cellpop(cellpop&w);     // copy constructor
  50.   void operator=(const cellpop& w);  // assignment operator
  51.   BOOL operator==(cellpop& w);       // equality test
  52.   void nextgen(cellpop& w);          // w is next generation from this
  53.   BOOL in(cell_type c);              // tests if c is in this
  54.   BOOL error(){return v==0;}
  55.   void erase_rect(cell_type start,cell_type end);
  56.                                      // erase all cells between bounds
  57.   void save(char *FileName);
  58.                                      // save population to file
  59.   void save_rect(char *FileName,cell_type start,cell_type end);
  60.                                      // save cells within bounds to file
  61.   cell_type firstx(void);            // find leftmost cell
  62.   cell_type firsty(void);            // find topmots cell
  63.   cell_type lastx(void);             // find rightmost cell
  64.   cell_type lasty(void);             // find bottommost cell
  65.   void transform(char orientation);  // rotate pop. or mirror it
  66.   void add(cellpop&v,cellpop &shape,cell_type offset,int mode);
  67.                                      // this=v+shape added at given offset
  68.                      // where mode is one of ON, OFF,XOR
  69.   void clear_pop();
  70. };
  71.  
  72. cellpop readfile(char *filename);    // read pop. from file
  73. void readshapelib(char *FileName);   // load shape library
  74. cellpop getshape(int i);             // find ith shape in library
  75. char *getshapename(int i);           // find name of ith shape in library
  76.