home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Life Simulator / DoCellularAutomata.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-04  |  3.0 KB  |  142 lines  |  [TEXT/MMCC]

  1. /* Cell Proj 1.0 */
  2.  
  3. #include "Cell_Proto.h"
  4. #include "Cell_Definitions.h"
  5. #include "Cell_Variables.h"
  6.  
  7. DoCellularAutomata()
  8. {    
  9.     int        a, b;
  10.     int        count;
  11.     int        lifeSum[ NUMBER_OF_CELLS ];
  12.     short    itemType;
  13.     Rect    itemRect;
  14.     Handle    itemHandle;
  15.     Str255    textOverPop, textOverExp;
  16.     
  17.     GetDialogItem( gCellInfoDialog, OVER_POP_ITEM, &itemType, &itemHandle,
  18.                                                     &itemRect );
  19.     GetDialogItemText( itemHandle, textOverPop );
  20.     StringToNum( textOverPop, &gOverPop );
  21.     
  22.     GetDialogItem( gCellInfoDialog, OVER_EXP_ITEM, &itemType, &itemHandle,
  23.                                                     &itemRect );
  24.     GetDialogItemText( itemHandle, textOverExp );
  25.     StringToNum( textOverExp, &gOverExp );
  26.  
  27.     for ( count = 0; count < NUMBER_OF_CELLS; count++ )    /* add up pnts around the pixel */
  28.     {
  29.         lifeSum[ count ] = 0;
  30.         
  31.         if ( count - 100 < 0 )
  32.             lifeSum[ count ] += gCellStatus[ count + 900 ];
  33.         else
  34.             lifeSum[ count ] += gCellStatus[ count - 100 ];    /* pnt directly above it */
  35.  
  36.         if ( count - 99 < 0 )
  37.             lifeSum[ count ] += gCellStatus[ count + 901 ];
  38.         else
  39.             lifeSum[ count ] += gCellStatus[ count - 99 ];    /* pnt directly above it */
  40.  
  41.         if ( count - 101 < 0 )
  42.             lifeSum[ count ] += gCellStatus[ count + 899 ];
  43.         else
  44.             lifeSum[ count ] += gCellStatus[ count - 101 ];    /* pnt directly above it */
  45.  
  46.  
  47.  
  48.         if ( count + 99 > 9999 )
  49.             lifeSum[ count ] += gCellStatus[ count - 901 ];
  50.         else
  51.             lifeSum[ count ] += gCellStatus[ count + 99 ];    /* pnt directly above it */
  52.  
  53.         if ( count + 101 > 9999 )
  54.             lifeSum[ count ] += gCellStatus[ count - 899 ];
  55.         else
  56.             lifeSum[ count ] += gCellStatus[ count + 101 ];    /* pnt directly above it */
  57.  
  58.         if ( count + 100 > 9999 )
  59.             lifeSum[ count ] += gCellStatus[ count - 900 ];
  60.         else
  61.             lifeSum[ count ] += gCellStatus[ count + 100 ];    /* pnt directly above it */
  62.  
  63.  
  64.  
  65.         if ( count == 0 )
  66.             lifeSum[ count ] += gCellStatus[ 9999 ];
  67.         else
  68.             lifeSum[ count ] += gCellStatus[ count - 1 ];    /* pnt directly above it */
  69.  
  70.         if ( count == 9999 )
  71.             lifeSum[ count ] += gCellStatus[ 0 ];
  72.         else
  73.             lifeSum[ count ] += gCellStatus[ count + 1 ];    /* pnt directly above it */
  74.         
  75.         GraphResults( count, lifeSum );
  76.     }
  77.     MoveRsltsTo_gCellStatus( lifeSum );
  78. }
  79.  
  80. GraphResults( count, lifeSum )
  81. int            count;
  82. int            *lifeSum;
  83. {    
  84.     if ( lifeSum[ count ] <= gOverExp )
  85.     {
  86.         GraphDead( count );
  87.         lifeSum[ count ] = 0;
  88.     }
  89.     if ( lifeSum[ count ] > gOverExp && lifeSum[ count ] < gOverPop )
  90.     {
  91.         GraphAlive( count );
  92.         lifeSum[ count ] = 1;
  93.     }
  94.     if ( lifeSum[ count ] >= gOverPop )
  95.     {
  96.         GraphDead( count );
  97.         lifeSum[ count ] = 0;
  98.     }
  99. }
  100.  
  101. GraphDead( count )
  102. int        count;
  103. {
  104.     int        x, y;
  105.     
  106.     x = count / 100;
  107.     y = count - ( 100 * x );
  108.     
  109.     SetPort ( gCellWindow );
  110.     PenNormal ();
  111.     ForeColor ( whiteColor );
  112.     
  113.     MoveTo ( x, y );
  114.     LineTo ( x, y );
  115. }
  116.  
  117. GraphAlive( count )
  118. int        count;
  119. {
  120.     int        x, y;
  121.     
  122.     x = count / 100;
  123.     y = count - ( 100 * x );
  124.     
  125.     SetPort ( gCellWindow );
  126.     PenNormal ();
  127.     ForeColor ( blackColor );
  128.     
  129.     MoveTo ( x, y );
  130.     LineTo ( x, y );
  131. }
  132.  
  133. MoveRsltsTo_gCellStatus( lifeSum )
  134. int        lifeSum[];
  135. {
  136.     int        count;
  137.     
  138.     for ( count = 0; count < NUMBER_OF_CELLS; count++ )    /* add up pnts around the pixel */
  139.     {
  140.         gCellStatus[ count ] = lifeSum [ count ];
  141.     }
  142. }