L8onStrips fade in and out
[SugarCubes.git] / L8onWallace.pde
index 675f52eea529a0654cd5d1e8574ea9864a2d829f..eddf37587969637ef1e5f134356f590cdf0b1a90 100644 (file)
@@ -1,8 +1,8 @@
 class L8onLife extends SCPattern {
   // Controls the rate of life algorithm ticks, in milliseconds
   private BasicParameter rateParameter = new BasicParameter("DELAY", 112.5, 0.0, 1000.0);
-  // Controls if the cubes should be randomized even if something changes. Set above 0.5 to randomize cube aliveness.
-  private BasicParameter randomParameter = new BasicParameter("RAND", 0.0);
+  // Controls the probability of a mutation in the cycleOfLife
+  private BasicParameter randomParameter = new BasicParameter("RAND", 0.000000011, 0.0, 0.1);
   // Controls the brightness of dead cubes.
   private BasicParameter deadParameter = new BasicParameter("DEAD", 25.0, 0.0, 100.0);
   // Controls the saturation.
@@ -11,8 +11,7 @@ class L8onLife extends SCPattern {
   public final double MIN_ALIVE_PROBABILITY = 0.2;
   public final double MAX_ALIVE_PROBABILITY = 0.9;
   
-  private final SinLFO xPos = new SinLFO(0, model.xMax, 4500);
-  private final SinLFO zPos = new SinLFO(0, model.zMax, 2500);
+  private final SawLFO cubePos = new SawLFO(0, model.cubes.size(), 2500);
 
   class CubeState {
      // Index of cube in glucose.model.cubes
@@ -53,8 +52,7 @@ class L8onLife extends SCPattern {
      addParameter(randomParameter);   
      addParameter(deadParameter);
      addParameter(saturationParameter);
-     addModulator(xPos).trigger();
-     addModulator(zPos).trigger();
+     addModulator(cubePos).trigger();
   }
   
   public void run(double deltaMs) {        
@@ -69,16 +67,15 @@ class L8onLife extends SCPattern {
       cube_state = this.cube_states.get(i);
 
       if(shouldLightCube(cube_state)) {
-        lightLiveCube(cube);
+        lightLiveCube(cube, i);
       } else {
-        lightDeadCube(cube);
+        lightDeadCube(cube, i);
       } 
       
       i++;      
     }
     
-    boolean should_randomize_anyway = (randomParameter.getValuef() > 0.5);
-    if(should_randomize_anyway || !any_changes_this_run) {
+    if(!any_changes_this_run) {
       randomizeCubeStates();  
     } else {
       applyNewLives();
@@ -89,9 +86,11 @@ class L8onLife extends SCPattern {
     }    
   }
   
-  public void lightLiveCube(Cube cube) {    
+  public void lightLiveCube(Cube cube, Integer index) {
+    float cube_dist = LXUtils.wrapdistf((float) index, cubePos.getValuef(), model.cubes.size());
+    float hv = (cube_dist / model.cubes.size()) * 360;
+
     for (LXPoint p : cube.points) {
-      float hv = max(0, lx.getBaseHuef() - abs(p.z - zPos.getValuef()));
       colors[p.index] = lx.hsb(
         hv,
         saturationParameter.getValuef(),        
@@ -100,11 +99,13 @@ class L8onLife extends SCPattern {
     }        
   }
   
-  public void lightDeadCube(Cube cube) {
+  public void lightDeadCube(Cube cube, Integer index) {
+    float cube_dist = LXUtils.wrapdistf((float) index, cubePos.getValuef(), model.cubes.size());
+    float dist_proportion = (cube_dist / (float) model.cubes.size());
+    float hv = dist_proportion * 360;
+    float dead_bright = deadParameter.getValuef() * dist_proportion;
+
     for (LXPoint p : cube.points) {
-      float hv = max(0, lx.getBaseHuef() - abs(p.x - xPos.getValuef()));
-      double dead_bright = deadParameter.getValuef() * Math.random();
-      
       colors[p.index] = lx.hsb(
         hv,
         saturationParameter.getValuef(),        
@@ -195,6 +196,7 @@ class L8onLife extends SCPattern {
     Integer alive_neighbor_count = countLiveNeighbors(cube_state);               
     boolean before_alive = cube_state.alive;
     boolean after_alive = before_alive;
+    double mutation = Math.random();
               
     if(cube_state.alive) {
       if(alive_neighbor_count < 2 || alive_neighbor_count > 3) {
@@ -211,6 +213,10 @@ class L8onLife extends SCPattern {
       }
     }
 
+    if(mutation <= randomParameter.getValuef()) {
+      after_alive = !after_alive;
+    }
+
     if(before_alive != after_alive) {
       any_changes_this_run = true;
     }
@@ -234,12 +240,12 @@ class L8onLife extends SCPattern {
 }
 
 class L8onAutomata extends SCPattern {
-  // Controls if the points should be randomized even if something changes. Set above 0.5 to randomize cube aliveness.
-  private BasicParameter randomParameter = new BasicParameter("RAND", 0.0);
+  // Controls the probability of a mutation in the cycleOfStripperLife
+  private BasicParameter randomParameter = new BasicParameter("RAND", 0.000000011, 0.0, 0.1);
   // Controls the rate of life algorithm ticks, in milliseconds
   private BasicParameter rateParameter = new BasicParameter("DELAY", 75.0, 0.0, 1000.0);
 
-  private final SinLFO zPos = new SinLFO(0, model.zMax, 2500);
+  private final SawLFO pointPos = new SawLFO(0, model.points.size(), 3000);
 
   public final double MIN_ALIVE_PROBABILITY = 0.2;
   public final double MAX_ALIVE_PROBABILITY = 0.9;
@@ -279,7 +285,7 @@ class L8onAutomata extends SCPattern {
 
      addParameter(randomParameter);
      addParameter(rateParameter);
-     addModulator(zPos).trigger();
+     addModulator(pointPos).trigger();
   }
 
   private void initPointStates() {
@@ -308,16 +314,15 @@ class L8onAutomata extends SCPattern {
       point_state = this.point_states.get(i);
 
       if(shouldLightPoint(point_state)) {
-        lightLivePoint(p);
+        lightLivePoint(p, i);
       } else {
-        lightDeadPoint(p);
+        lightDeadPoint(p, i);
       }
 
       i++;
     }
 
-    boolean should_randomize_anyway = (randomParameter.getValuef() > 0.5);
-    if(should_randomize_anyway || !any_changes_this_run) {
+    if(!any_changes_this_run) {
       randomizePointStates();
     } else {
       applyNewStates();
@@ -328,8 +333,10 @@ class L8onAutomata extends SCPattern {
     }
   }
 
-  public void lightLivePoint(LXPoint p) {
-    float hv = max(0, lx.getBaseHuef() - abs(p.z - zPos.getValuef()));
+  public void lightLivePoint(LXPoint p, Integer index) {
+    float point_dist = LXUtils.wrapdistf((float) index, pointPos.getValuef(), model.points.size());
+    float hv = (point_dist / model.points.size()) * 360;
+
     colors[p.index] = lx.hsb(
       hv,
       90,
@@ -337,9 +344,9 @@ class L8onAutomata extends SCPattern {
     );
   }
 
-  public void lightDeadPoint(LXPoint p) {
+  public void lightDeadPoint(LXPoint p, Integer index) {
     colors[p.index] = lx.hsb(
-      lx.getBaseHuef(),
+      120,
       0,
       0
     );
@@ -362,6 +369,7 @@ class L8onAutomata extends SCPattern {
     Integer alive_neighbor_count = countLiveNeighbors(point_state);
     boolean before_alive = point_state.alive;
     boolean after_alive = before_alive;
+    double mutation = Math.random();
 
     if(point_state.alive) {
       if(alive_neighbor_count == 1) {
@@ -378,6 +386,10 @@ class L8onAutomata extends SCPattern {
       }
     }
 
+    if(mutation < randomParameter.getValuef()) {
+      after_alive = !after_alive;
+    }
+
     if(before_alive != after_alive) {
       any_changes_this_run = true;
     }
@@ -421,7 +433,7 @@ class L8onAutomata extends SCPattern {
     double prob_range = (1.0 - MIN_ALIVE_PROBABILITY) - (1.0 - MAX_ALIVE_PROBABILITY);
     double prob = MIN_ALIVE_PROBABILITY + (prob_range * Math.random());
 
-    print("Randomizing points! p = " + prob + "\n");
+    //print("Randomizing points! p = " + prob + "\n");
 
     for (PointState point_state: this.point_states) {
       point_state.alive = (Math.random() <= prob);
@@ -431,29 +443,32 @@ class L8onAutomata extends SCPattern {
 
 class L8onStrips extends SCPattern {
   // Controls the rate of life algorithm ticks, in milliseconds
-  private BasicParameter rateParameter = new BasicParameter("DELAY", 112.5, 0.0, 1000.0);
-  // Controls if the cubes should be randomized even if something changes. Set above 0.5 to randomize cube aliveness.
-  private BasicParameter randomParameter = new BasicParameter("RAND", 0.0);
-  // Controls the brightness of dead cubes.
-  private BasicParameter deadParameter = new BasicParameter("DEAD", 25.0, 0.0, 100.0);
+  private BasicParameter rateParameter = new BasicParameter("DELAY", 112.5, 1.0, 1000.0);
+  // Controls the probability of a mutation in the cycleOfStripperLife
+  private BasicParameter randomParameter = new BasicParameter("RAND", 0.000000011, 0.0, 0.1);
   // Controls the saturation.
   private BasicParameter saturationParameter = new BasicParameter("SAT", 90.0, 0.0, 100.0);
 
   public final double MIN_ALIVE_PROBABILITY = 0.4;
   public final double MAX_ALIVE_PROBABILITY = 0.9;
 
-  private final SinLFO xPos = new SinLFO(0, model.xMax, 4500);
-  private final SinLFO zPos = new SinLFO(0, model.zMax, 2500);
+  public final float MAX_ALIVE_BRIGHTNESS = 95.0;
+
+  private final SawLFO stripPos = new SawLFO(0, model.strips.size(), 3000);
 
   class StripState {
      // Index of strip in glucose.model.strips
      public Integer index;
      // Boolean which describes if strip is alive.
      public boolean alive;
+     // Boolean which describes if strip was just changed;
+     public boolean just_changed;
+     // Current brightness
+     public float current_brightness;
      // List of this cubes neighbors
      public List<Integer> neighbors;
 
-     public StripState(Integer index, boolean alive, List<Integer> neighbors) {
+     public StripState(Integer index, boolean alive, float current_brightness, List<Integer> neighbors) {
        this.index = index;
        this.alive = alive;
        this.neighbors = neighbors;
@@ -483,11 +498,9 @@ class L8onStrips extends SCPattern {
 
      addParameter(rateParameter);
      addParameter(randomParameter);
-     addParameter(deadParameter);
      addParameter(saturationParameter);
 
-     addModulator(xPos).trigger();
-     addModulator(zPos).trigger();
+     addModulator(stripPos).trigger();
   }
 
   public void run(double deltaMs) {
@@ -502,16 +515,14 @@ class L8onStrips extends SCPattern {
       strip_state = this.strip_states.get(i);
 
       if(shouldLightStrip(strip_state)) {
-        lightLiveStrip(strip);
+        lightLiveStrip(strip, strip_state, deltaMs);
       } else {
-        lightDeadStrip(strip);
+        lightDeadStrip(strip, strip_state, deltaMs);
       }
-
       i++;
     }
 
-    boolean should_randomize_anyway = (randomParameter.getValuef() > 0.5);
-    if(should_randomize_anyway || !any_changes_this_run) {
+    if(!any_changes_this_run) {
       randomizeStripStates();
     } else {
       applyNewLives();
@@ -522,26 +533,71 @@ class L8onStrips extends SCPattern {
     }
   }
 
-  public void lightLiveStrip(Strip strip) {
+  public void lightLiveStrip(Strip strip, StripState strip_state, double deltaMs) {
+    Integer index = strip_state.index;
+    float strip_dist = LXUtils.wrapdistf((float) 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 = min(((float) time_since_last_run / rateParameter.getValuef()), 1.0);
+      bv = min(MAX_ALIVE_BRIGHTNESS, bright_prop * MAX_ALIVE_BRIGHTNESS);
+
+      if(index == 100) {
+        print("live prop: " + bright_prop + " bv: " + bv + " current: " + strip_state.current_brightness + "\n");
+      }
+
+      if(strip_state.current_brightness < bv) {
+        strip_state.current_brightness = bv;
+      } else {
+        bv = strip_state.current_brightness;
+      }
+
+      if(index == 100) {
+        print("live bv: " + bv + " current: " + strip_state.current_brightness + "\n");
+      }
+    }
+
     for (LXPoint p : strip.points) {
-      float hv = max(0, lx.getBaseHuef() - abs(p.z - zPos.getValuef()));
       colors[p.index] = lx.hsb(
         hv,
         saturationParameter.getValuef(),
-        75
+        bv
       );
     }
   }
 
-  public void lightDeadStrip(Strip strip) {
-    for (LXPoint p : strip.points) {
-      float hv = max(0, lx.getBaseHuef() - abs(p.x - xPos.getValuef()));
-      double dead_bright = deadParameter.getValuef() * Math.random();
+  public void lightDeadStrip(Strip strip, StripState strip_state, double deltaMs) {
+    Integer index = strip_state.index;
+    float strip_dist = LXUtils.wrapdistf((float) index, stripPos.getValuef(), model.strips.size());
+    float dist_proportion = (strip_dist / (float) model.strips.size());
+    float hv = dist_proportion * 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(index == 100) {
+        print("dead prop: " + bright_prop + " bv: " + bv + " current: " + strip_state.current_brightness + "\n");
+      }
+
+      if(strip_state.current_brightness > bv) {
+        strip_state.current_brightness = bv;
+      } else {
+        bv = strip_state.current_brightness;
+      }
+
+      if(index == 100) {
+        print("dead bv: " + bv + " current: " + strip_state.current_brightness + "\n");
+      }
+    }
+
+    for (LXPoint p : strip.points) {
       colors[p.index] = lx.hsb(
         hv,
         saturationParameter.getValuef(),
-        dead_bright
+        bv
       );
     }
   }
@@ -557,6 +613,7 @@ class L8onStrips extends SCPattern {
   private void initStripStates() {
     List<Integer> neighbors;
     boolean alive = false;
+    float current_brightness;
     StripState strip_state;
     this.strip_states = new ArrayList<StripState>();
     Integer i = 0;
@@ -566,7 +623,8 @@ class L8onStrips extends SCPattern {
     for (Strip strip : model.strips) {
       neighbors = findStripNeighbors(strip, i);
       alive = true;
-      strip_state = new StripState(i, alive, neighbors);
+      current_brightness = 0.0;
+      strip_state = new StripState(i, alive, current_brightness, neighbors);
       this.strip_states.add(strip_state);
 
       total_neighbors += neighbors.size();
@@ -585,6 +643,11 @@ class L8onStrips extends SCPattern {
 
     for (StripState strip_state : this.strip_states) {
       strip_state.alive = (Math.random() <= prob);
+      if(strip_state.alive) {
+        strip_state.current_brightness = 0;
+      } else{
+        strip_state.current_brightness = MAX_ALIVE_BRIGHTNESS;
+      }
     }
   }
 
@@ -613,6 +676,7 @@ class L8onStrips extends SCPattern {
     // 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);
@@ -635,6 +699,7 @@ class L8onStrips extends SCPattern {
     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) {
@@ -651,8 +716,13 @@ class L8onStrips extends SCPattern {
       }
     }
 
+    if(mutation < randomParameter.getValuef()) {
+      after_alive = !after_alive;
+    }
+
     if(before_alive != after_alive) {
       any_changes_this_run = true;
+      strip_state.just_changed = true;
     }
 
     return after_alive;