home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 November / PCO_1198.ISO / filesbbs / os2 / tspg202s.arj / TSPG202S.ZIP / Scripts / LIFE.CWX < prev    next >
Encoding:
Text File  |  1997-05-04  |  5.5 KB  |  174 lines

  1. /*
  2.  
  3.   life.cwx
  4.  
  5.   An implementation of Conway's Game of Life using object graphics.
  6.   
  7.  Copyright 1997 by TrueSpectra Inc.                                  
  8.                                                                      
  9.  This code is provided purely for demonstration purposes and is not  
  10.  supported or under warranty.  Feel free to modify and examine this  
  11.  example script for your own purposes.                               
  12.  
  13. */
  14.  
  15.  
  16. initial_cells = 30;        /* number of cells to create at start */
  17. max_generation = 200;    /* number of generations */
  18. output_count = 10;        /* number of cells to fit in the output area */
  19.  
  20. /*************************************************************************/
  21. call CwNewProject;
  22.  
  23. /* scale the object appropriately so that output_count objects fit in the
  24.  * output area.  Objects may, of course, be created outseide of the output
  25.  * area as generations go by */
  26. scale = 0;
  27.  
  28. outp = CwGetAppHandle( "output settings" );
  29. width = CwGetProperty( outp, "output size:width" );
  30. height = CwGetProperty( outp, "output size:height" );
  31.  
  32. /* make sure the scales are such that the cells are square by scaling to
  33.  * the limiting value */
  34. if width < height then;
  35.     scale = width / output_count;
  36. else;
  37.     scale = height / output_count;
  38.  
  39. /* zoom to something reasonable */
  40. call CwSetViewZoom width/2, height/2, width*2, height*2;
  41.  
  42. /*************************************************************************/
  43. /* Create the initial generation randomly.  We could also create a way to
  44.  * read data from a file and generate from that. */
  45.  
  46. gen = 0;    /* handle of current generation */
  47. do c = 1 to initial_cells;
  48.     x = TRUNC( rand( 1, output_count ) );
  49.     y = TRUNC( rand( 1, output_count ) );
  50.  
  51.     /* we're doing this randomly, so we don't want to stack multiple cells
  52.      * in one place.  It won't cause any serious problems, but more cells
  53.      * do drop performance. */
  54.     if \find_cell( x, y, gen ) then do;
  55.         cell = create_cell( x, y );
  56.         gen = CwAddObjectToGroup( cell, gen );
  57.     end;
  58. end;
  59.  
  60. call CwWaitOnRender CwGetCurrentView();
  61.  
  62. /*************************************************************************/
  63. p1 = 0;
  64. p2 = 0;
  65. p3 = 0;
  66. do generation = 1 to max_generation;
  67.     p3 = p2;
  68.     p2 = p1;
  69.     p1 = gen;
  70.     gen = create_generation( p1 );
  71.     /* could be that everything died */
  72.     if \CwIsHandleValid( gen ) then;
  73.         exit;
  74.  
  75.     call age_generation( p1 );
  76.     call age_generation( p2 );
  77.     call age_generation( p3 );
  78.  
  79.     call CwWaitOnRender CwGetCurrentView();
  80. end;
  81. exit;
  82.  
  83. /*************************************************************************/
  84. rand:procedure
  85. arg from,to
  86.     r = random(0,999)/1000;
  87.     return (to - from) * r + from;
  88.  
  89. /*************************************************************************/
  90. /* This can do all kinds of cool things.  For example, we could stick a
  91.  * blur on top of the entire group to fade out a generation, or we can just
  92.  * delete it.  Some interesting effects are possible... */
  93. age_generation:procedure
  94. arg parent
  95.     if \CwIsHandleValid( parent ) then
  96.         return;
  97.     
  98.     call CwDeleteObject parent;
  99.     return;
  100.  
  101. /*************************************************************************/
  102. create_cell:procedure expose scale
  103. arg x, y
  104.     call CwSetSelectionRectangle (x*scale), (y*scale), scale, scale;
  105.     cell = CwCreateEffect( "ellipse", "solid color" );
  106.     tool = CwGetTool( cell );
  107.     call CwSetProperty tool, "color", "#b00000";
  108.     name = 'X:'||x||' Y:'||y;
  109.     call CwSetName cell, name;
  110.  
  111.     return cell;
  112.  
  113. /*************************************************************************/
  114. find_cell:procedure
  115. arg x, y, parent;
  116.     if \CwIsHandleValid( parent ) then;
  117.         return 0;
  118.  
  119.     name = 'X:'||x||' Y:'||y;
  120.     n = CwGetHandleFromObjectName( parent, name );
  121.     if CwIsHandleValid( n ) then do;
  122.         return 1;
  123.     end;
  124.     return 0;
  125.  
  126. /*************************************************************************/
  127. evolve_cell:procedure expose scale;
  128. arg x, y, old_parent;
  129.     old = find_cell( x, y, old_parent );
  130.     neighbours = 0;
  131.  
  132.     neighbours = neighbours + find_cell( x-1, y-1, old_parent );
  133.     neighbours = neighbours + find_cell( x-1, y, old_parent );
  134.     neighbours = neighbours + find_cell( x-1, y+1, old_parent );
  135.     neighbours = neighbours + find_cell( x, y-1, old_parent );
  136.     neighbours = neighbours + find_cell( x, y+1, old_parent );
  137.     neighbours = neighbours + find_cell( x+1, y-1, old_parent );
  138.     neighbours = neighbours + find_cell( x+1, y, old_parent );
  139.     neighbours = neighbours + find_cell( x+1, y+1, old_parent );
  140.  
  141.     if old & (neighbours = 2 | neighbours = 3) then;
  142.         cell = create_cell( x, y );
  143.     else if \old & neighbours = 3 then;
  144.         cell = create_cell( x, y );
  145.     return cell;
  146.  
  147. /*************************************************************************/
  148. create_generation:procedure expose scale
  149. arg old_parent;
  150.  
  151.     /* first, figure out the area of the generation to create.  It will be
  152.      * just slightly (one cell) larger than the area covered by the last
  153.      * generation.  We also put in a generous safety margin. */
  154.     call CwGetPosition old_parent, pos;
  155.     left = TRUNC((pos.x - (pos.width/2))/scale) - 3;
  156.     right = TRUNC((pos.x + (pos.width/2))/scale) + 3;
  157.     top = TRUNC((pos.y + (pos.height/2))/scale) + 3;
  158.     bottom = TRUNC((pos.y - (pos.height/2))/scale) - 3;
  159.  
  160.     parent = 0;    /* the new generation has a new group */
  161.  
  162.     do x = left to right;
  163.         do y = bottom to top;
  164.             cell = evolve_cell( x, y, old_parent );
  165.             if CwIsHandleValid( cell ) then do;
  166.                 parent = CwAddObjectToGroup( cell, parent );
  167.             end;
  168.         end;
  169.     end;
  170.  
  171.     return parent;
  172.  
  173. /*************************************************************************/
  174.