4 public List<Integer> neighbors;
6 public CubeState(Integer index, boolean alive, List<Integer> neighbors) {
9 this.neighbors = neighbors;
13 class Life extends SCPattern {
14 public final int TIME_BETWEEN_RUNS = 100;
15 public final int MAX_DEAD_BRIGHTNESS = 40;
16 private final SinLFO xPos = new SinLFO(0, model.xMax, 5000);
17 private final SinLFO yPos = new SinLFO(0, model.yMax, 5000);
19 public List<CubeState> cube_states;
20 public int time_since_last_run;
21 public boolean any_changes_this_run;
23 public Life(GLucose glucose) {
27 time_since_last_run = 0;
28 any_changes_this_run = false;
29 addModulator(xPos).trigger();
30 addModulator(yPos).trigger();
33 public void run(double deltaMs) {
37 time_since_last_run += deltaMs;
39 if(time_since_last_run < TIME_BETWEEN_RUNS) {
42 any_changes_this_run = false;
44 for (Cube cube : model.cubes) {
45 cube_state = this.cube_states.get(i);
47 if(shouldBeAlive(i)) {
55 if(!any_changes_this_run) {
56 randomizeCubeStates();
59 time_since_last_run = 0;
62 public void outputCubeInfo() {
64 for (Cube c : model.cubes) {
65 print("Cube " + i + ": " + c.x + "," + c.y + "," + c.z + "\n");
68 print("Edgeheight: " + Cube.EDGE_HEIGHT + "\n");
69 print("Edgewidth: " + Cube.EDGE_WIDTH + "\n");
70 print("Channelwidth: " + Cube.CHANNEL_WIDTH + "\n");
73 private void initCubeStates() {
74 List<Integer> neighbors;
75 boolean alive = false;
77 this.cube_states = new LinkedList<CubeState>();
80 for (Cube c : model.cubes) {
81 neighbors = findCubeNeighbors(c, i);
83 cube_state = new CubeState(i, alive, neighbors);
84 this.cube_states.add(cube_state);
89 private void randomizeCubeStates() {
90 print("randomizing!\n");
92 float f = (xPos.getValuef() / model.xMax) * 10;
93 int mod_value = max(2, (int) f);
95 for (CubeState cube_state: this.cube_states) {
96 if( (cube_state.index % mod_value == 0) == cube_state.alive) {
97 cube_state.alive = !cube_state.alive;
102 public List<Integer> findCubeNeighbors(Cube cube, Integer index) {
103 List<Integer> neighbors = new LinkedList<Integer>();
106 for (Cube c : model.cubes) {
112 if(abs(c.x - cube.x) < (Cube.EDGE_WIDTH * 2) && abs(c.y - cube.y) < (Cube.EDGE_HEIGHT * 2)) {
113 print("Cube " + i + " is a neighbor of " + index + "\n");
123 public boolean shouldBeAlive(Integer index) {
124 CubeState cube_state = this.cube_states.get(index);
125 Integer alive_neighbor_count = countLiveNeighbors(cube_state);
127 boolean before_alive = cube_state.alive;
129 if(cube_state.alive) {
130 if(alive_neighbor_count < 2 || alive_neighbor_count > 3) {
131 cube_state.alive = false;
133 cube_state.alive = true;
137 if(alive_neighbor_count == 3) {
138 cube_state.alive = true;
140 cube_state.alive = false;
144 this.cube_states.set(index, cube_state);
146 if(before_alive != cube_state.alive) {
147 any_changes_this_run = true;
149 return cube_state.alive;
152 public Integer countLiveNeighbors(CubeState cube_state) {
154 CubeState neighbor_cube_state;
156 for(Integer neighbor_index: cube_state.neighbors) {
157 neighbor_cube_state = this.cube_states.get(neighbor_index);
158 if(neighbor_cube_state.alive) {
166 public void lightLiveCube(Cube cube) {
167 for (LXPoint p : cube.points) {
168 float hv = max(0, lx.getBaseHuef() - abs(p.x - xPos.getValuef()));
169 float bv = max(0, 100 - abs(p.y - yPos.getValuef()));
170 colors[p.index] = lx.hsb(
179 public void lightDeadCube(Cube cube) {
180 for (LXPoint p : cube.points) {
181 float hv = max(0, lx.getBaseHuef() - abs(p.x - xPos.getValuef()));
182 float bv = max(0, MAX_DEAD_BRIGHTNESS - abs(p.y - yPos.getValuef()));
184 colors[p.index] = lx.hsb(