home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / rule_src / bbmm4.c next >
Encoding:
C/C++ Source or Header  |  1990-02-26  |  1.6 KB  |  77 lines

  1. /*
  2.  * File: bbm.M4.c
  3.  *   By: Dave Hiebeler
  4.  *       hiebeler@turing.cs.rpi.edu
  5.  *       August 1989
  6.  *
  7.  * The "BBM" ("billiard-ball model") as described in the _Cellular
  8.  * Automata Machines_ book by Toffoli and Margolus
  9.  *
  10.  * Particles reside on plane 0 (value of 1), wall cells reside on
  11.  * plane 1 (value of 2).
  12.  */
  13.  
  14.  
  15. #include "nborhood.h"
  16.  
  17. byte BBM_update();
  18.  
  19. /* a couple of handy definitions */
  20. #define PARTICLE 1    /* particles are on plane 0 (low bit) */
  21. #define WALL 2        /* walls are on the next higher bit */
  22.  
  23.  
  24. void
  25. init_function()
  26. {
  27.     update_function = BBM_update;
  28. }
  29.  
  30.     
  31. byte
  32. BBM_update(nbors)
  33. margolus_nbors *nbors;
  34. {
  35.     unsigned char next; /* new value for cell is computed into this variable */
  36.     int wallsum;    /* how many wall cells are nearby */
  37.  
  38.     Get_margolus_nbors;
  39.  
  40.     /* If I'm a wall, I stay a wall */
  41.     if (c & WALL)
  42.     next = WALL;
  43.     /* Clear out next */
  44.     else next = 0;
  45.     /* Count up how many wall-particles are in this
  46.      * block
  47.      */
  48.     wallsum = (!!(c&WALL)) + (!!(ccw&WALL)) +
  49.     (!!(opp&WALL)) + (!!(cw&WALL));
  50.     switch (wallsum) {
  51.       case 0:    /* just do particle collisions */
  52.     if ((c == opp) && (cw == ccw))
  53.         next |= cw&PARTICLE;
  54.     else
  55.         next |= opp&PARTICLE;
  56.     break;
  57.       case 1:
  58.     next |= c&PARTICLE;
  59.     break;
  60.       case 2:
  61.     /* If hitting a flat wall, bounce off with specular reflection */
  62.     if ((cw&WALL) && (opp&WALL))
  63.         next |= ccw&PARTICLE;
  64.     else if ((ccw&WALL) && (opp&WALL))
  65.         next |= cw&PARTICLE;
  66.     /* otherwise, just bounce back where we came from */
  67.     else next |= c&PARTICLE;
  68.     break;
  69.       case 3:
  70.     next |= c&PARTICLE;
  71.     break;
  72.       default:
  73.     break;
  74.     }
  75.     return next;
  76. }
  77.