summaryrefslogtreecommitdiff
path: root/src/simulation/lattice_rules.js
blob: db027a39cc93dc030dee99aa74400d92de95a48a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { pairs } from '../util.js';

function mod(k, n) {
  return ((k % n) + n) % n;
}

function pos_wrap(lattice, x, y) {
  const height = lattice.length;
  const width = lattice[0].length;
  return [mod(x, width), mod(y, height)];
}


function neighbors(lattice, x, y) {
  const offsets = [-1, 0, 1];
  const positions = pairs(offsets, offsets)
    .filter(([dx, dy]) => dx !== 0 || dy !== 0)
    .map(([dx, dy]) => pos_wrap(lattice, x+dx, y+dy));
  const neighbors = positions
    .map(([x, y]) => [x, y, lattice[y][x]]);
  return neighbors;
}


export const lattice_rules = {

  empty: (lattice, x, y) => {
    const num_active_neighbors = neighbors(lattice, x, y)
      .map(([x, y, cell]) => cell.type)
      .filter(type => type === 'mutable' || type === 'active')
      .length;
    if (num_active_neighbors === 3) {
      return { world_updates: [{ 
        x, y, from: 'empty', to: 'active',
        flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] },
      }]};
    }
  },

  active: (lattice, x, y) => {
    const num_active_neighbors = neighbors(lattice, x, y)
      .map(([x, y, cell]) => cell.type)
      .filter(type => type === 'mutable' || type === 'active')
      .length;
    const die = { world_updates: [{ 
      x, y, from: 'active', to: 'empty', 
      flags: { emit: [0, 0, 0, 0, 0, 0, 0, -1] }, 
    }]};
    if (num_active_neighbors < 2) {
      return die;
    } else if (num_active_neighbors > 3) {
      return die;
    }
  },

  mutable: () => {},
  immutable: () => {},
  flag: () => {},

};