Automata and life is good
[SugarCubes.git] / L8onWallace.pde
index 80f8b8dfd3c888d75be884d26552aeb41763ceab..1c9d849dce6df340a5fcb92e9325521bb1508317 100644 (file)
@@ -1,21 +1,6 @@
-class CubeState {  
- // Index of cube in glucose.model.cubes
- public Integer index;
- // Boolean which describes if cube is alive.
- public boolean alive;
- // List of this cubes neighbors
- public List<Integer> neighbors;
- public CubeState(Integer index, boolean alive, List<Integer> neighbors) {
-   this.index = index;
-   this.alive = alive;
-   this.neighbors = neighbors;
- }
-}
-
 class L8onLife extends SCPattern {
   // Controls the rate of life algorithm ticks, in milliseconds
-  private BasicParameter rateParameter = new BasicParameter("RATE", 122.5, 0.0, 2000.0);
+  private BasicParameter rateParameter = new BasicParameter("RATE", 122.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.
@@ -23,17 +8,35 @@ class L8onLife extends SCPattern {
   // Controls the saturation.
   private BasicParameter saturationParameter = new BasicParameter("SAT", 90.0, 0.0, 100.0);
     
-  public final double MIN_ALIVE_PROBABILITY = 0.1;
-  public final double MAX_ALIVE_PROBABILITY = 0.8;
-  
-  private final SinLFO xPos = new SinLFO(0, model.xMax, 3500);
+  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);
+
+  class CubeState {
+     // Index of cube in glucose.model.cubes
+     public Integer index;
+     // Boolean which describes if cube is alive.
+     public boolean alive;
+     // List of this cubes neighbors
+     public List<Integer> neighbors;
+
+     public CubeState(Integer index, boolean alive, List<Integer> neighbors) {
+       this.index = index;
+       this.alive = alive;
+       this.neighbors = neighbors;
+     }
+  }
+
   // Contains the state of all cubes by index.
   private List<CubeState> cube_states;
   // Contains the amount of time since the last cycle of life.
   private int time_since_last_run;
   // Boolean describing if life changes were made during the current run.
   private boolean any_changes_this_run;
+  // Hold the new lives
+  private List<Boolean> new_lives;
   
   public L8onLife(GLucose glucose) {
      super(glucose);  
@@ -44,12 +47,14 @@ class L8onLife extends SCPattern {
      initCubeStates();
      time_since_last_run = 0;
      any_changes_this_run = false;
+     new_lives = new ArrayList<Boolean>();
      
      addParameter(rateParameter);     
      addParameter(randomParameter);   
      addParameter(deadParameter);
-     addParameter(saturationParameter);  
+     addParameter(saturationParameter);
      addModulator(xPos).trigger();
+     addModulator(zPos).trigger();
   }
   
   public void run(double deltaMs) {        
@@ -57,11 +62,12 @@ class L8onLife extends SCPattern {
     CubeState cube_state;    
     
     any_changes_this_run = false;        
+    new_lives.clear();
     time_since_last_run += deltaMs;
     
     for (Cube cube : model.cubes) {
-      cube_state = this.cube_states.get(i);  
-      
+      cube_state = this.cube_states.get(i);
+
       if(shouldLightCube(cube_state)) {
         lightLiveCube(cube);
       } else {
@@ -74,6 +80,8 @@ class L8onLife extends SCPattern {
     boolean should_randomize_anyway = (randomParameter.getValuef() > 0.5);
     if(should_randomize_anyway || !any_changes_this_run) {
       randomizeCubeStates();  
+    } else {
+      applyNewLives();
     }
     
     if(time_since_last_run >= rateParameter.getValuef()) {
@@ -83,7 +91,7 @@ class L8onLife extends SCPattern {
   
   public void lightLiveCube(Cube cube) {    
     for (LXPoint p : cube.points) {
-      float hv = max(0, lx.getBaseHuef() - abs(p.x - xPos.getValuef()));
+      float hv = max(0, lx.getBaseHuef() - abs(p.z - zPos.getValuef()));
       colors[p.index] = lx.hsb(
         hv,
         saturationParameter.getValuef(),        
@@ -120,7 +128,7 @@ class L8onLife extends SCPattern {
     List<Integer> neighbors;
     boolean alive = false;  
     CubeState cube_state;      
-    this.cube_states = new LinkedList<CubeState>(); 
+    this.cube_states = new ArrayList<CubeState>();
     Integer i = 0;     
     
     for (Cube c : model.cubes) {      
@@ -165,39 +173,49 @@ class L8onLife extends SCPattern {
     // Respect rate parameter.
     if(time_since_last_run < rateParameter.getValuef()) {
       any_changes_this_run = true;
-      return cube_state.alive;   
+      return cube_state.alive;
     } else {
-      return cycleOfLife(cube_state);
-    }    
+      boolean new_life = cycleOfLife(cube_state);
+      new_lives.add(new_life);
+      return new_life;
+    }
+  }
+
+  public void applyNewLives() {
+    int index = 0;
+    for(boolean liveliness: new_lives) {
+      CubeState cube_state = this.cube_states.get(index);
+      cube_state.alive = new_lives.get(index);
+      index++;
+    }
   }
       
   public boolean cycleOfLife(CubeState cube_state) {
     Integer index = cube_state.index;
     Integer alive_neighbor_count = countLiveNeighbors(cube_state);               
     boolean before_alive = cube_state.alive;
+    boolean after_alive = before_alive;
               
     if(cube_state.alive) {
       if(alive_neighbor_count < 2 || alive_neighbor_count > 3) {
-        cube_state.alive = false;
+        after_alive = false;
       } else {
-        cube_state.alive = true;
+        after_alive = true;
       }
       
     } else {
-      if(alive_neighbor_count == 3) {
-        cube_state.alive = true;
+      if(alive_neighbor_count == 2) {
+        after_alive = true;
       } else {
-        cube_state.alive = false;   
+        after_alive = false;
       }
-    }  
-    
-    this.cube_states.set(index, cube_state);
-       
-    if(before_alive != cube_state.alive) {
+    }
+
+    if(before_alive != after_alive) {
       any_changes_this_run = true;
-    }   
-    
-    return cube_state.alive;
+    }
+
+    return after_alive;
   }
       
   public Integer countLiveNeighbors(CubeState cube_state) {
@@ -214,3 +232,200 @@ class L8onLife extends SCPattern {
     return count;
   }         
 }
+
+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 rate of life algorithm ticks, in milliseconds
+  private BasicParameter rateParameter = new BasicParameter("RATE", 75.0, 0.0, 1000.0);
+
+  private final SinLFO zPos = new SinLFO(0, model.zMax, 2500);
+
+  public final double MIN_ALIVE_PROBABILITY = 0.2;
+  public final double MAX_ALIVE_PROBABILITY = 0.9;
+
+  class PointState {
+     // Index of cube in glucose.model.cubes
+     public Integer index;
+     // Boolean which describes if cube is alive.
+     public boolean alive;
+
+     public PointState(Integer index, boolean alive) {
+       this.index = index;
+       this.alive = alive;
+     }
+  }
+
+  // Contains the state of all cubes by index.
+  private List<PointState> point_states;
+  // Contains the amount of time since the last cycle of life.
+  private int time_since_last_run;
+  // Boolean describing if life changes were made during the current run.
+  private boolean any_changes_this_run;
+  // Hold the new lives
+  private List<Boolean> new_states;
+
+  public L8onAutomata(GLucose glucose) {
+     super(glucose);
+
+     //Print debug info about the cubes.
+     //outputCubeInfo();
+
+     initPointStates();
+     randomizePointStates();
+     time_since_last_run = 0;
+     any_changes_this_run = false;
+     new_states = new ArrayList<Boolean>();
+
+     addParameter(randomParameter);
+     addParameter(rateParameter);
+     addModulator(zPos).trigger();
+  }
+
+  private void initPointStates() {
+    boolean alive = false;
+    PointState point_state;
+    this.point_states = new ArrayList<PointState>();
+    Integer i = 0;
+
+    for (LXPoint p : model.points) {
+      alive = true;
+      point_state = new PointState(i, alive);
+      this.point_states.add(point_state);
+      ++i;
+    }
+  }
+
+  public void run(double deltaMs) {
+    Integer i = 0;
+    PointState point_state;
+
+    any_changes_this_run = false;
+    new_states.clear();
+    time_since_last_run += deltaMs;
+
+    for (LXPoint p : model.points) {
+      point_state = this.point_states.get(i);
+
+      if(shouldLightPoint(point_state)) {
+        lightLivePoint(p);
+      } else {
+        lightDeadPoint(p);
+      }
+
+      i++;
+    }
+
+    boolean should_randomize_anyway = (randomParameter.getValuef() > 0.5);
+    if(should_randomize_anyway || !any_changes_this_run) {
+      randomizePointStates();
+    } else {
+      applyNewStates();
+    }
+
+    if(time_since_last_run >= rateParameter.getValuef()) {
+      time_since_last_run = 0;
+    }
+  }
+
+  public void lightLivePoint(LXPoint p) {
+    float hv = max(0, lx.getBaseHuef() - abs(p.z - zPos.getValuef()));
+    colors[p.index] = lx.hsb(
+      hv,
+      90,
+      80
+    );
+  }
+
+  public void lightDeadPoint(LXPoint p) {
+    colors[p.index] = lx.hsb(
+      lx.getBaseHuef(),
+      0,
+      0
+    );
+  }
+
+  public boolean shouldLightPoint(PointState point_state) {
+    // Respect rate parameter.
+    if(time_since_last_run < rateParameter.getValuef()) {
+      any_changes_this_run = true;
+      return point_state.alive;
+    } else {
+      boolean new_state = cycleOfAutomata(point_state);
+      new_states.add(new_state);
+      return new_state;
+    }
+  }
+
+  public boolean cycleOfAutomata(PointState point_state) {
+    Integer index = point_state.index;
+    Integer alive_neighbor_count = countLiveNeighbors(point_state);
+    boolean before_alive = point_state.alive;
+    boolean after_alive = before_alive;
+
+    if(point_state.alive) {
+      if(alive_neighbor_count == 1) {
+        after_alive = true;
+      } else {
+        after_alive = false;
+      }
+
+    } else {
+      if(alive_neighbor_count == 1) {
+        after_alive = true;
+      } else {
+        after_alive = false;
+      }
+    }
+
+    if(before_alive != after_alive) {
+      any_changes_this_run = true;
+    }
+
+    return after_alive;
+  }
+
+  public int countLiveNeighbors(PointState point_state) {
+    Integer index = point_state.index;
+    PointState before_neighbor;
+    PointState after_neighbor;
+
+    int count = 0;
+    if (index > 0) {
+      before_neighbor = point_states.get(index - 1);
+      if(before_neighbor.alive) {
+        count++;
+      }
+    }
+
+    if (index < (point_states.size() - 1)) {
+      after_neighbor = point_states.get(index + 1);
+      if(after_neighbor.alive) {
+        count++;
+      }
+    }
+
+    return count;
+  }
+
+  private void applyNewStates() {
+    int index = 0;
+    for(boolean new_state: new_states) {
+      PointState point_state = this.point_states.get(index);
+      point_state.alive = new_states.get(index);
+      index++;
+    }
+  }
+
+  private void randomizePointStates() {
+    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");
+
+    for (PointState point_state: this.point_states) {
+      point_state.alive = (Math.random() <= prob);
+    }
+  }
+
+}