home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / bomb.tar.gz / bomb.tar / bomb / brain.c < prev    next >
C/C++ Source or Header  |  1997-04-25  |  7KB  |  283 lines

  1. /*
  2.     bomb - automatic interactive visual stimulation
  3.     Copyright (C) 1994  Scott Draves <spot@cs.cmu.edu>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "defs.h"
  21.  
  22. typedef struct {
  23.    int subrule;
  24.    int mask;
  25. } sub_brain_t;
  26.  
  27. /* ordering could be much better */
  28. static sub_brain_t sub_brain_table[] =
  29. #ifdef wbomb
  30. {
  31.  {0, 0x0070}, /* 18 */
  32.  {0, 0x4004}, /* 5  */
  33.  {2, 0},      /* 19 */
  34.  {0, 0x8080}, /* 9  */
  35.  {3, 0x800},  /* 12 */
  36. /* {3, 0x200},  /* 37 */
  37.  {5, 0x06},   /* 10 */
  38.  {3, 0x010},  /* 25 */
  39. };
  40. #else
  41. {{6, 0},
  42.  {0, 0x2008},
  43.  {5, 0x12},
  44.  {0, 0x0808},
  45.  {0, 0x0208},
  46.  {0, 0x4004},
  47.  {4, 0},
  48.  {0, 0x8008},
  49.  {5, 0x12},
  50.  {0, 0x8080},
  51.  {5, 0x06},
  52.  {0, 0x8800},
  53.  {3, 0x800},
  54.  {0, 0x6000},
  55.  {4, 0},
  56.  {0, 0x0600},
  57.  {0, 0x0060},
  58.  {5, 0x12},
  59.  {0, 0x0070},
  60.  {2, 0},
  61.  {0, 0x0090},
  62.  {3, 0x004},
  63.  {0, 0x0900},
  64.  {3, 0x008},
  65.  {0, 0x9000},
  66.  {3, 0x010},
  67.  {0, 0x5000},
  68.  {5, 0xa0},
  69.  {3, 0x020},
  70.  {0, 0x0014},
  71.  {3, 0x040},
  72.  {0, 0x0500},
  73.  {4, 0},
  74.  {0, 0x0050},
  75.  {0, 0x0014},
  76.  {3, 0x080},
  77.  {0, 0x0104},
  78.  {3, 0x200},
  79.  {0, 0x1040},
  80.  {0, 0x10400},
  81.  {0, 0x104000},
  82.  {1, 0},
  83.  {5, 0x06},
  84.  {4, 0},
  85.  };
  86. #endif
  87.  
  88. /* p1 toggles direct display of brain (bypass heat), good for debugging */
  89. #define SHOW_DRIVER 0
  90.  
  91.  
  92.  
  93. void step_rule_rug_brain(int frame, rule_t *p, image8_t *fb)
  94. {
  95.    int x,y;
  96.    board_t *s_heat_board, *d_heat_board;
  97.    board_t *s_game_board, *d_game_board;
  98.    u_char *lp0, *lp1;
  99.    int brain = iclamp(p->brain, alen(sub_brain_table));
  100.    int mask = sub_brain_table[brain].mask;
  101.    int shift_bits = iclamp(p->brain_shift, 26);
  102.    int live_bit = 1<<(shift_bits);
  103.    int phase_mask = (1<<(shift_bits+1))-1;
  104.    static int phase = 0;
  105.    if (++phase > shift_bits)
  106.       phase = 0;
  107.  
  108.    s_heat_board=&board[dbuf];
  109.    d_heat_board=&board[1-dbuf];
  110.    s_game_board=&board2[dbuf];
  111.    d_game_board=&board2[1-dbuf];
  112.    dbuf = 1-dbuf;
  113.  
  114.    // sprintf(status_string+20, "%d %d %d %d", p->mask, p->speed, brain, shift_bits);
  115.  
  116.  
  117.    for(y=0;y<=SMALL_YSIZE+1;y++) {
  118.       (*s_game_board)[0][y] = (*s_game_board)[SMALL_XSIZE][y];
  119.       (*s_game_board)[SMALL_XSIZE+1][y] = (*s_game_board)[1][y];
  120.       (*s_heat_board)[0][y] = (*s_heat_board)[SMALL_XSIZE][y];
  121.       (*s_heat_board)[SMALL_XSIZE+1][y] = (*s_heat_board)[1][y];
  122.    }
  123.    for(x=0;x<=SMALL_XSIZE+1;x++) {
  124.       (*s_game_board)[x][0] = (*s_game_board)[x][SMALL_YSIZE];
  125.       (*s_game_board)[x][SMALL_YSIZE+1] = (*s_game_board)[x][1];
  126.       (*s_heat_board)[x][0] = (*s_heat_board)[x][SMALL_YSIZE];
  127.       (*s_heat_board)[x][SMALL_YSIZE+1] = (*s_heat_board)[x][1];
  128.    }
  129.  
  130.    for (y=1;y<=SMALL_YSIZE;y++) {
  131.       lp0 = fb->p + (fb->stride * (y - 1));
  132.       lp1 = lp0 + fb->stride * SMALL_YSIZE;
  133.       for (x=1;x<=SMALL_XSIZE;x++) {
  134.      int heat, t, c, dh;
  135.      int nneigh;
  136.      int centerhist;
  137.      int newbit;
  138.      int driver;
  139.      int hn, hs, he, hw, hc;
  140.      int denom;
  141.      nneigh =
  142.         (((*s_game_board)[x+1][y+1]&live_bit)+
  143.          ((*s_game_board)[x+1][y-1]&live_bit)+
  144.          ((*s_game_board)[x-1][y+1]&live_bit)+
  145.          ((*s_game_board)[x-1][y-1]&live_bit)+
  146.          ((*s_game_board)[x  ][y-1]&live_bit)+
  147.          ((*s_game_board)[x-1][y  ]&live_bit)+
  148.          ((*s_game_board)[x+1][y  ]&live_bit)+
  149.          ((*s_game_board)[x  ][y+1]&live_bit)) >> shift_bits;
  150.  
  151.      centerhist = (*s_game_board)[x][y];
  152.      driver = (centerhist >> phase) & phase_mask;
  153.  
  154.      hn = (*s_heat_board)[x  ][y-1];
  155.      hs = (*s_heat_board)[x  ][y+1];
  156.      he = (*s_heat_board)[x+1][y  ];
  157.      hw = (*s_heat_board)[x-1][y  ];
  158.      hc = (*s_heat_board)[x  ][y  ];
  159.      denom = 5;
  160.      heat = hn + hs + he + hw + hc + p->speed;
  161.      dh = 0;
  162.      driver = driver | (driver >> 8);
  163.      if (driver&0x1)     dh += 10;
  164.      if (driver&0x2)     dh -= 5;
  165.      if (driver&0x4)   { heat -= hn; denom--;}
  166.      if (driver&0x8)   { heat -= hs; denom--;}
  167.      if (driver&0x10)    dh += 20;
  168.      if (driver&0x20)  { heat -= he; denom--;}
  169.      if (driver&0x40)  { heat -= hw; denom--;}
  170.      if (driver&0x80 && heat < 0) heat = 0;
  171.      heat = (heat / denom) & p->mask;
  172.      heat += dh;
  173.  
  174.  
  175.      c = centerhist;
  176.      t = nneigh;
  177.      switch (sub_brain_table[brain].subrule) {
  178.       case 0:
  179.         newbit = (t == 3) || ((t > 3) && (((c & mask) == 0)));
  180.         break;
  181.       case 1:
  182.         newbit = (t == 3) || ((t > 3) && (((c & 0x007) == 0) ^
  183.                           ((c & 0x060) == 0) |
  184.                           ((c & 0x700) == 0)));
  185.         break;
  186.       case 2:
  187.         newbit = (t == 3) || ((t > 2) && (((c&6) == 0) ^ ((c&0x60) == 0)));
  188.         break;
  189.       case 3:
  190.         newbit = ((t == 2) || (t > 3)) && ((c&mask) == 0);
  191.         break;
  192.       case 4:
  193.         newbit = (t == 2) && ((c&6) == 0);
  194.         break;
  195.       case 5:
  196.         newbit = (t > 1) && (t < 6) && ((c&mask) == 0);
  197.         break;
  198.       case 6:
  199.         newbit = (t == 1) || ((t > 3) && (((c & 0x8008) == 0)));
  200.         break;
  201.      }
  202.      centerhist = (centerhist << 1) | newbit;
  203.  
  204.      (*d_game_board)[x][y] = centerhist;
  205.      (*d_heat_board)[x][y] = heat;
  206.  
  207. #if SHOW_DRIVER
  208.      p1 = iclamp(p1, 3 + shift_bits);
  209.      switch (p1) {
  210.       case 0:
  211.         // if (p->remap)
  212.            heat = remap[heat];
  213.         break;
  214.       case 1:
  215.         heat = driver ^ (driver >> 8) ^ (driver >> 16);
  216.         break;
  217.       default:
  218.         heat = (driver & (1 << (p1 - 2))) ? 0 : 100;
  219.         break;
  220.      }
  221. #else
  222.     // if (p->remap)
  223.         heat = remap[heat];
  224. #endif
  225.  
  226.      lp0[SMALL_XSIZE] = heat;
  227.      *lp0++ = heat;
  228.      lp1[SMALL_XSIZE] = heat;
  229.      *lp1++ = heat;
  230.       }
  231.    }
  232. }
  233.  
  234. #if 0
  235. /* this this development code and notes, pasted down here to make the above
  236.    more readable */
  237. #if 1
  238.      t = (t == 1) || ((t > 3) && (((c & 0x8008) == 0)));
  239. #elif 0
  240.      /* 0x700 0x70 0x38 */
  241.      /* 0x8010 0x8008 */
  242.      /* any two bit pattern (except 0x0c melts away, others?)  */
  243.      /* 0x4004 wavey blobs, but die out slowly, usually */
  244.      t = (t == 3) || ((t > 3) && (((c & 0x8008) == 0)));
  245. #elif 0
  246.      t = (t == 3) || ((t > 3) &&
  247.               (((c & 0x007) == 0) ^
  248.                ((c & 0x060) == 0) |
  249.                ((c & 0x700) == 0)));
  250. #elif 0
  251.      /* noise with floating holes */
  252.      t = (t == 3) || ((t > 2) && (((c&6) == 0) ^ ((c&0x60) == 0)));
  253. #elif 0
  254.      /* all: rough edges, bits of vert/horiz stability
  255.         in the lower areas, near circular (octagonal?) shape
  256.         2 almost noise
  257.         4 near-static rectilinear mosaic
  258.         8,10 swirly texture
  259.         20 unstable z-reaction
  260.         40 semi-stable z
  261.         80 stable z, one eventually dominates
  262.         400 time tunnel, horns less apparent
  263.         4000 same, even thicker
  264.         */
  265.      t = ((t == 2) || (t > 3)) && ((c&0x20) == 0);
  266. #elif 0
  267.      /* any two bit constant works */
  268.      /* diamond shape, quickly stabilizes */
  269.      /* 6 stable, no large features
  270.         30 fair sized features
  271.         c0 only a few left
  272.         300 often fails
  273.         1 bit gap (5/a/14 etc) interesting, otherwise
  274.         lower bit not very important.  can effect stripe-width
  275.         */
  276.      t = (t > 1) && (t < 6) && ((c&0x12) == 0);
  277. #else
  278.      /* like brian's brain.  files' namesake */
  279.      t = (t == 2) && ((c&6) == 0);
  280. #endif
  281.  
  282. #endif
  283.