+ public void lightLiveStrip(Strip strip, StripState strip_state, double deltaMs) {
+ float strip_dist = LXUtils.wrapdistf((float) strip_state.index, stripPos.getValuef(), model.strips.size());
+ float hv = (strip_dist / model.strips.size()) * 360;
+ float bv = strip_state.current_brightness;
+
+ if(deltaMs >= rateParameter.getValuef() || !strip_state.just_changed) {
+ float bright_prop = min(((float) time_since_last_run / rateParameter.getValuef()), 1.0);
+ bv = min(MAX_ALIVE_BRIGHTNESS, bright_prop * MAX_ALIVE_BRIGHTNESS);
+
+ if(strip_state.current_brightness < bv) {
+ strip_state.current_brightness = bv;
+ } else {
+ bv = strip_state.current_brightness;
+ }
+ }
+
+ for (LXPoint p : strip.points) {
+ colors[p.index] = lx.hsb(
+ hv,
+ saturationParameter.getValuef(),
+ bv
+ );
+ }
+ }
+
+ public void lightDeadStrip(Strip strip, StripState strip_state, double deltaMs) {
+ float strip_dist = LXUtils.wrapdistf((float) strip_state.index, stripPos.getValuef(), model.strips.size());
+ float hv = (strip_dist / model.strips.size()) * 360;
+ float bv = strip_state.current_brightness;
+
+ if(!strip_state.just_changed || deltaMs >= rateParameter.getValuef()) {
+ float bright_prop = 1.0 - min(((float) time_since_last_run / rateParameter.getValuef()), 1.0);
+ bv = max(0.0, bright_prop * MAX_ALIVE_BRIGHTNESS);
+
+ if(strip_state.current_brightness > bv) {
+ strip_state.current_brightness = bv;
+ } else {
+ bv = strip_state.current_brightness;
+ }
+ }
+
+ for (LXPoint p : strip.points) {
+ colors[p.index] = lx.hsb(
+ hv,
+ saturationParameter.getValuef(),
+ bv
+ );
+ }
+ }
+
+ public void outputStripInfo() {
+ int i = 0;
+ for (Strip strip : model.strips) {
+ print("Strip " + i + ": " + strip.cx + "," + strip.cy + "," + strip.cz + "\n");
+ ++i;
+ }
+ }
+
+ private void initStripStates() {
+ List<Integer> neighbors;
+ boolean alive = false;
+ float current_brightness = 0.0;
+ StripState strip_state;
+ this.strip_states = new ArrayList<StripState>();
+ Integer i = 0;
+
+ int total_neighbors = 0;
+
+ for (Strip strip : model.strips) {
+ neighbors = findStripNeighbors(strip, i);
+ alive = true;
+ strip_state = new StripState(i, alive, current_brightness, neighbors);
+ this.strip_states.add(strip_state);
+
+ total_neighbors += neighbors.size();
+ ++i;
+ }
+
+ float average_neighbor_count = (float) total_neighbors / (float) model.strips.size();
+ //print("Average neighbor count: " + average_neighbor_count + "\n");
+ }
+
+ private void randomizeStripStates() {
+ double prob_range = (1.0 - MIN_ALIVE_PROBABILITY) - (1.0 - MAX_ALIVE_PROBABILITY);
+ double prob = MIN_ALIVE_PROBABILITY + (prob_range * Math.random());
+
+ //print("Randomizing strips! p = " + prob + "\n");
+
+ for (StripState strip_state : this.strip_states) {
+ strip_state.alive = (Math.random() <= prob);
+ strip_state.just_changed = true;
+ }
+ }
+
+ public List<Integer> findStripNeighbors(Strip strip, Integer index) {
+ List<Integer> neighbors = new LinkedList<Integer>();
+ Integer i = 0;
+ int neighbor_count = 0;
+ double distance = 0.0;
+
+ for (Strip s : model.strips) {
+ if( (int)index != (int)i ) {
+ distance = Math.sqrt( Math.pow((s.cx - strip.cx), 2) + Math.pow((s.cy - strip.cy), 2) + Math.pow((s.cz - strip.cz), 2) );
+
+ if(distance < ( (double) Cube.EDGE_WIDTH) ) {
+ //print("Strip " + i + " is a neighbor of " + index + "\n");
+ neighbors.add(i);
+ }
+ }
+ i++;
+ }
+
+ return neighbors;
+ }
+
+ public boolean shouldLightStrip(StripState strip_state) {
+ // Respect rate parameter.
+ if(time_since_last_run < rateParameter.getValuef()) {
+ any_changes_this_run = true;
+ strip_state.just_changed = false;
+ return strip_state.alive;
+ } else {
+ boolean new_life = cycleOfStripperLife(strip_state);
+ new_lives.add(new_life);
+ return new_life;
+ }
+ }
+
+ public void applyNewLives() {
+ int index = 0;
+ for(boolean liveliness: new_lives) {
+ StripState strip_state = this.strip_states.get(index);
+ strip_state.alive = new_lives.get(index);
+ index++;
+ }
+ }
+
+ public boolean cycleOfStripperLife(StripState strip_state) {
+ Integer alive_neighbor_count = countLiveNeighbors(strip_state);
+ boolean before_alive = strip_state.alive;
+ boolean after_alive = before_alive;
+ double mutation = Math.random();
+
+ if(strip_state.alive) {
+ if(alive_neighbor_count < 2 || alive_neighbor_count > 6) {
+ after_alive = false;
+ } else {
+ after_alive = true;
+ }
+
+ } else {
+ if(alive_neighbor_count == 5) {
+ after_alive = true;
+ } else {
+ after_alive = false;
+ }
+ }
+
+ if(mutation < mutationParameter.getValuef()) {
+ after_alive = !after_alive;
+ }
+
+ if(before_alive != after_alive) {
+ any_changes_this_run = true;
+ strip_state.just_changed = true;
+ }
+
+ return after_alive;
+ }
+
+ public Integer countLiveNeighbors(StripState strip_state) {
+ Integer count = 0;
+ StripState neighbor_strip_state;
+
+ for(Integer neighbor_index: strip_state.neighbors) {
+ neighbor_strip_state = this.strip_states.get(neighbor_index);
+ if(neighbor_strip_state.alive) {
+ count++;
+ }
+ }
+
+ return count;
+ }