SCPattern moved out of GLucose
[SugarCubes.git] / DanHorwitz.pde
index ca829ee6e893c31abe9514d432e09f9b557bbb99..cdec36b6b1d160b44e8e3aeeda1a1d1c62a76621 100644 (file)
@@ -1,12 +1,12 @@
 //----------------------------------------------------------------------------------------------------------------------------------
 public class Pong extends DPat {
-       SinLFO x,y,z,dx,dy,dz; 
-       float cRad;     DParam pSize;
+       SinLFO x,y,z,dx,dy,dz;
+       float cRad;     BasicParameter pSize;
        Pick    pChoose;
-       xyz     v = new xyz(), vMir =  new xyz();
+       PVector v = new PVector(), vMir =  new PVector();
 
-       Pong(GLucose glucose) {
-               super(glucose);
+       Pong(LX lx) {
+               super(lx);
                cRad = mMax.x/10;
                addModulator(dx = new SinLFO(6000,  500, 30000  )).trigger();
                addModulator(dy = new SinLFO(3000,  500, 22472  )).trigger();
@@ -15,64 +15,65 @@ public class Pong extends DPat {
                addModulator(y  = new SinLFO(cRad, mMax.y - cRad, 0)).trigger();        y.modulateDurationBy(dy);
                addModulator(z  = new SinLFO(cRad, mMax.z - cRad, 0)).trigger();        z.modulateDurationBy(dz);
            pSize       = addParam      ("Size"                 , 0.4   );
-           pChoose = addPick   ("Animiation"   , 0, 2, new String[] {"Pong", "Ball", "Cone"}   );
+           pChoose = addPick   ("Animiation"   , 2, 2, new String[] {"Pong", "Ball", "Cone"}   );
        }
 
-       void    StartRun(double deltaMs)        { cRad = mMax.x*pSize.Val()/6; }
-       color   CalcPoint(xyz p)                {
+       void    StartRun(double deltaMs)        { cRad = mMax.x*val(pSize)/6; }
+       color   CalcPoint(PVector p)            {
                v.set(x.getValuef(), y.getValuef(), z.getValuef());
                v.z=0;p.z=0;// ignore z dimension
                switch(pChoose.Cur()) {
-               case 0: vMir.set(mMax); vMir.subtract(p);
-                               return lx.hsb(0,0,c1c(1 - min(v.distance(p), v.distance(vMir))*.5/cRad));       // balls
-               case 1: return lx.hsb(0,0,c1c(1 - v.distance(p)*.5/cRad));                                                      // ball
+               case 0: vMir.set(mMax); vMir.sub(p);
+                               return lx.hsb(lxh(),100,c1c(1 - min(v.dist(p), v.dist(vMir))*.5/cRad));         // balls
+               case 1: return lx.hsb(lxh(),100,c1c(1 - v.dist(p)*.5/cRad));                                                    // ball
                case 2: vMir.set(mMax.x/2,0,mMax.z/2);
-                               return lx.hsb(0,0,c1c(1 - CalcCone(p,v,vMir) * max(.02,.45-pSize.Val())));      // spot
+                               return lx.hsb(lxh(),100,c1c(1 - calcCone(p,v,vMir) * max(.02,.45-val(pSize))));         // spot
                }
                return lx.hsb(0,0,0);
        }
 }
 //----------------------------------------------------------------------------------------------------------------------------------
 public class NDat {
-       float   xz, yz, zz, hue, sat, speed, angle, den;
+       float   xz, yz, zz, hue, speed, angle, den;
        float   xoff,yoff,zoff;
        float   sinAngle, cosAngle;
        boolean isActive;
        NDat              () { isActive=false; }
        boolean Active() { return isActive; }
-       void    set     (float _hue, float _sat, float _xz, float _yz, float _zz, float _den, float _speed, float _angle) {
+       void    set     (float _hue, float _xz, float _yz, float _zz, float _den, float _speed, float _angle) {
                isActive = true;
-               hue=_hue; sat=_sat; xz=_xz; yz=_yz; zz =_zz; den=_den; speed=_speed; angle=_angle;
+               hue=_hue; xz=_xz; yz=_yz; zz =_zz; den=_den; speed=_speed; angle=_angle;
                xoff = random(100e3); yoff = random(100e3); zoff = random(100e3);
        }
 }
 
 public class Noise extends DPat
 {
-       int                     CurAnim, iSymm;
-       int             XSym=1,YSym=2,RadSym=3;
-       float           zTime , zTheta=0, zSin, zCos, rtime, ttime, transAdd;
-       DParam          pSpeed , pDensity;
-       Pick            pChoose, pSymm;
-       int                     _ND = 4;
-       NDat            N[] = new NDat[_ND];
-
-       Noise(GLucose glucose) {
-               super(glucose);
+       int                             CurAnim, iSymm;
+       int                     XSym=1,YSym=2,RadSym=3;
+       float                   zTime , zTheta=0, zSin, zCos, rtime, ttime;
+       BasicParameter  pSpeed , pDensity, pSharp;
+       Pick                    pChoose, pSymm;
+       int                             _ND = 4;
+       NDat                    N[] = new NDat[_ND];
+
+       Noise(LX lx) {
+               super(lx);
                pSpeed          = addParam("Fast"       , .55);
                pDensity        = addParam("Dens"        , .5);
+               pSharp          = addParam("Shrp"        ,  0);
                pSymm           = addPick("Symmetry" , 0, 3, new String[] {"None", "X", "Y", "Radial"}  );
                pChoose         = addPick("Animation", 6, 7, new String[] {"Drip", "Cloud", "Rain", "Fire", "Machine", "Spark","VWave", "Wave"} );
                for (int i=0; i<_ND; i++) N[i] = new NDat();
        }
 
-       void StartPattern() { zTime = random(500); zTheta=0; rtime = 0; ttime = 0; transAdd=0; }
+       void onActive() { zTime = random(500); zTheta=0; rtime = 0; ttime = 0; }
+
        void StartRun(double deltaMs) {
-               zTime   += deltaMs*(pSpeed.Val()-.5)*.002       ;
-               zTheta  += deltaMs*(pSpin .Val()-.5)*.01        ;
+               zTime   += deltaMs*(val(pSpeed)-.5)*.002        ;
+               zTheta  += deltaMs*(spin()-.5)*.01      ;
                rtime   += deltaMs;
                iSymm    = pSymm.Cur();
-               transAdd = 1*(1 - constrain(rtime - ttime,0,1000)/1000);
                zSin    = sin(zTheta);
                zCos    = cos(zTheta);
 
@@ -83,23 +84,21 @@ public class Noise extends DPat
                        for (int i=0; i<_ND; i++) { N[i].isActive = false; }
                        
                        switch(CurAnim) {
-                       //                          hue sat xz  yz  zz  den mph angle
-                       case 0: N[0].set(0  ,0  ,75 ,75 ,150,45 ,3  ,0  ); pSharp.set(1 ); break;       // drip
-                       case 1: N[0].set(0  ,0  ,100,100,200,45 ,3  ,180); pSharp.set(0 ); break;       // clouds
-                       case 2: N[0].set(0  ,0  ,2  ,400,2  ,20 ,3  ,0  ); pSharp.set(.5); break;       // rain
-                       case 3: N[0].set(40 ,1  ,100,100,200,10 ,1  ,180); 
-                                       N[1].set(0  ,1  ,100,100,200,10 ,5  ,180); pSharp.set(0 ); break;       // fire 1
-                       case 4: N[0].set(0  ,1  ,40 ,40 ,40 ,15 ,2.5,180);
-                                       N[1].set(20 ,1  ,40 ,40 ,40 ,15 ,4  ,0  );
-                                       N[2].set(40 ,1  ,40 ,40 ,40 ,15 ,2  ,90 );
-                                       N[3].set(60 ,1  ,40 ,40 ,40 ,15 ,3  ,-90); pSharp.set(.5); break; // machine
-                       case 5: N[0].set(0  ,1  ,400,100,2  ,15 ,3  ,90 );
-                                       N[1].set(20 ,1  ,400,100,2  ,15 ,2.5,0  );
-                                       N[2].set(40 ,1  ,100,100,2  ,15 ,2  ,180);
-                                       N[3].set(60 ,1  ,100,100,2  ,15 ,1.5,270); pSharp.set(.5); break; // spark
+                       //                          hue xz yz zz den mph angle
+                       case 0: N[0].set(0  ,75 ,75 ,150,45 ,3  ,0  ); pSharp.setValue(1 ); break;      // drip
+                       case 1: N[0].set(0  ,100,100,200,45 ,3  ,180); pSharp.setValue(0 ); break;      // clouds
+                       case 2: N[0].set(0  ,2  ,400,2  ,20 ,3  ,0  ); pSharp.setValue(.5); break;      // rain
+                       case 3: N[0].set(40 ,100,100,200,10 ,1  ,180); 
+                                       N[1].set(0  ,100,100,200,10 ,5  ,180); pSharp.setValue(0 ); break;      // fire 1
+                       case 4: N[0].set(0  ,40 ,40 ,40 ,15 ,2.5,180);
+                                       N[1].set(20 ,40 ,40 ,40 ,15 ,4  ,0  );
+                                       N[2].set(40 ,40 ,40 ,40 ,15 ,2  ,90 );
+                                       N[3].set(60 ,40 ,40 ,40 ,15 ,3  ,-90); pSharp.setValue(.5); break; // machine
+                       case 5: N[0].set(0  ,400,100,2  ,15 ,3  ,90 );
+                                       N[1].set(20 ,400,100,2  ,15 ,2.5,0  );
+                                       N[2].set(40 ,100,100,2  ,15 ,2  ,180);
+                                       N[3].set(60 ,100,100,2  ,15 ,1.5,270); pSharp.setValue(.5); break; // spark
                        }
-
-                       DG.UpdateLights();
                }
                
                for (int i=0; i<_ND; i++) if (N[i].Active()) {
@@ -108,33 +107,32 @@ public class Noise extends DPat
                }
        }
 
-       color CalcPoint(xyz P) {
+       color CalcPoint(PVector p) {
                color c = 0;
-               P.RotateZ(mCtr, zSin, zCos);
-               
+               rotateZ(p, mCtr, zSin, zCos);
+
                if (CurAnim == 6 || CurAnim == 7) {
-                       P.setNorm();
-                       return lx.hsb(0,0, 100 * (
-                                                       constrain(1-50*(1-pDensity.Val())*abs(P.y-sin(zTime*10  + P.x*(300))*.5 - .5),0,1) + 
-                       (CurAnim == 7 ? constrain(1-50*(1-pDensity.Val())*abs(P.x-sin(zTime*10  + P.y*(300))*.5 - .5),0,1) : 0))
+                       setNorm(p);
+                       return lx.hsb(lxh(),100, 100 * (
+                                                       constrain(1-50*(1-val(pDensity))*abs(p.y-sin(zTime*10  + p.x*(300))*.5 - .5),0,1) + 
+                       (CurAnim == 7 ? constrain(1-50*(1-val(pDensity))*abs(p.x-sin(zTime*10  + p.y*(300))*.5 - .5),0,1) : 0))
                        );
                }                       
-                       
-               if (iSymm == XSym && P.x > mMax.x/2) P.x = mMax.x-P.x;
-               if (iSymm == YSym && P.y > mMax.y/2) P.y = mMax.y-P.y;
+
+               if (iSymm == XSym && p.x > mMax.x/2) p.x = mMax.x-p.x;
+               if (iSymm == YSym && p.y > mMax.y/2) p.y = mMax.y-p.y;
 
                for (int i=0;i<_ND; i++) if (N[i].Active()) {
                        NDat  n     = N[i];
                        float zx    = zTime * n.speed * n.sinAngle,
                                  zy    = zTime * n.speed * n.cosAngle;
-                       
-                       float b     = (iSymm==RadSym ? noise(zTime*n.speed+n.xoff-Dist(P,mCtr)/n.xz)
-                                                                                : noise(P.x/n.xz+zx+n.xoff,P.y/n.yz+zy+n.yoff,P.z/n.zz+n.zoff))
+
+                       float b     = (iSymm==RadSym ? noise(zTime*n.speed+n.xoff-p.dist(mCtr)/n.xz)
+                                                                                : noise(p.x/n.xz+zx+n.xoff,p.y/n.yz+zy+n.yoff,p.z/n.zz+n.zoff))
                                                        *1.8;
 
-                       b +=    n.den/100 -.4 + pDensity.Val() -1;
-                       b +=    transAdd;
-                       c =     blendColor(c,lx.hsb(n.hue,100*n.sat,c1c(b)),ADD);
+                       b +=    n.den/100 -.4 + val(pDensity) -1;
+                       c =     blendColor(c,lx.hsb(lxh()+n.hue,100,c1c(b)),ADD);
                }
                return c;
        }
@@ -159,34 +157,32 @@ public class Play extends DPat
                                                                        prvR    = dstR;         dstR    = random(mCtr.y);                                                                       }
        }
 
-       int             nBeats  =       0;
-       DParam  pAmp, pRadius, pBounce;
+       BasicParameter  pAmp, pRadius, pBounce;
+       Pick                    pTimePattern, pTempoMult, pShape;
 
-       float   t,amp,rad,bnc;
-       float   zTheta=0;
        ArrayList<rWave> waves = new ArrayList<rWave>(10);
 
-       rAngle  a1 = new rAngle(), a2 = new rAngle(),
-                       a3 = new rAngle(), a4 = new rAngle();
-       xyz             cPrev   = new xyz(), cRand      = new xyz(),
-                       cMid    = new xyz(), V          = new xyz(),
-                       Theta   = new xyz(), TSin       = new xyz(),
-                       TCos    = new xyz(), cMidNorm = new xyz(),
-                       Pn              = new xyz();
+       int             nBeats  =       0;
+       float   t,amp,rad,bnc,zTheta=0;
+
+       rAngle  a1              = new rAngle(), a2                      = new rAngle(),
+                       a3              = new rAngle(), a4                      = new rAngle();
+       PVector cPrev   = new PVector(), cRand          = new PVector(),
+                       cMid    = new PVector(), V                      = new PVector(),
+                       theta   = new PVector(), tSin           = new PVector(),
+                       tCos    = new PVector(), cMidNorm       = new PVector(),
+                       Pn              = new PVector();
        float   LastBeat=3, LastMeasure=3;
-       int             CurRandTempo = 1, CurRandTPat = 1;
-
-       Pick    pTimePattern, pTempoMult, pShape;
-       int             RandCube;
+       int             curRandTempo = 1, curRandTPat = 1;
 
-       Play(GLucose glucose) {
-               super(glucose);
+       Play(LX lx) {
+               super(lx);
            pRadius             = addParam("Rad"        , .1    );
                pBounce         = addParam("Bnc"        , .2    );
            pAmp                = addParam("Amp"        , .2    );
-               pTempoMult      = addPick ("TMult"      , 0 , 5         , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand"   }       );
-               pTimePattern= addPick ("TPat"   , 6 , 7         , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"}   );
-               pShape          = addPick ("Shape"      , 3 , 15        , new String[] {"Line", "Tap", "V", "RandV",
+               pTempoMult      = addPick ("TMult"      , 5 , 5         , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand"   }       );
+               pTimePattern= addPick ("TPat"   , 7 , 7         , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"}   );
+               pShape          = addPick ("Shape"      , 7 , 15        , new String[] {"Line", "Tap", "V", "RandV",
                                                                                                                                        "Pyramid", "Wings", "W2", "Clock",
                                                                                                                                        "Triangle", "Quad", "Sphere", "Cone",
                                                                                                                                        "Noise", "Wave", "?", "?"}                                              );
@@ -210,25 +206,37 @@ public class Play extends DPat
                }
        }
 
-       void StartPattern() { zTheta=0; }
+       void onReset()  { zTheta=0; super.onReset(); }
+       void onActive() { 
+               zTheta=0; 
+               while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2);
+       }
+
+       int KeyPressed = -1;
+       boolean noteOn(Note note) {
+               int row = note.getPitch(), col = note.getChannel();
+               if (row == 57) {KeyPressed = col; return true; }
+               return super.noteOn(note);
+       }
+
        void StartRun(double deltaMs) {
                t       = lx.tempo.rampf();
-               amp = pAmp.Val();
-               rad     = pRadius.getValuef();
-               bnc     = pBounce.getValuef();          
-               zTheta  += deltaMs*(pSpin .Val()-.5)*.01;
+               amp = pAmp              .getValuef();
+               rad     = pRadius       .getValuef();
+               bnc     = pBounce       .getValuef();           
+               zTheta  += deltaMs*(val(pSpin)-.5)*.01;
 
-               Theta   .set(pRotX.Val()*PI*2, pRotY.Val()*PI*2, pRotZ.Val()*PI*2 + zTheta);
-               TSin    .set(sin(Theta.x), sin(Theta.y), sin(Theta.z));
-               TCos    .set(cos(Theta.x), cos(Theta.y), cos(Theta.z));
+               theta   .set(val(pRotX)*PI*2, val(pRotY)*PI*2, val(pRotZ)*PI*2 + zTheta);
+               tSin    .set(sin(theta.x), sin(theta.y), sin(theta.z));
+               tCos    .set(cos(theta.x), cos(theta.y), cos(theta.z));
 
                if (t<LastMeasure) {
-                       if (random(3) < 1) { CurRandTempo = int(random(4)); if (CurRandTempo == 3) CurRandTempo = int(random(4));       }
-                       if (random(3) < 1) { CurRandTPat  = pShape.Cur() > 6 ? 2+int(random(5)) : int(random(7));                                       }
+                       if (random(3) < 1) { curRandTempo = int(random(4)); if (curRandTempo == 3) curRandTempo = int(random(4));       }
+                       if (random(3) < 1) { curRandTPat  = pShape.Cur() > 6 ? 2+int(random(5)) : int(random(7));                                       }
                } LastMeasure = t;
                        
-               int nTempo = pTempoMult  .Cur(); if (nTempo == 5) nTempo = CurRandTempo;
-               int nTPat  = pTimePattern.Cur(); if (nTPat  == 7) nTPat  = CurRandTPat ;
+               int nTempo = pTempoMult  .Cur(); if (nTempo == 5) nTempo = curRandTempo;
+               int nTPat  = pTimePattern.Cur(); if (nTPat  == 7) nTPat  = curRandTPat ;
 
                switch (nTempo) {
                        case 0:         t = t;                                                          break;
@@ -243,18 +251,18 @@ public class Play extends DPat
                        w.move(deltaMs); if (w.bDone) waves.remove(i); else i++;
                }
 
-               if ((t<LastBeat && !pKey.b) || DG.KeyPressed>-1) {
+               if ((t<LastBeat && pShape.Cur()!=14) || KeyPressed>-1) {
                        waves.add(new rWave(
-                                               pKey.b ? map(DG.KeyPressed,0,7,0,1) : random(1),                // location
+                                               KeyPressed>-1 ? map(KeyPressed,0,7,0,1) : random(1),            // location
                                                bnc*10,                 // bounciness
                                                7,                              // velocity
                                                2*(1-amp)));    // dampiness
-                       DG.KeyPressed=-1;
+                       KeyPressed=-1;
                        if (waves.size() > 5) waves.remove(0);
                }
                
                if (t<LastBeat) {
-                       cPrev.set(cRand); cRand.setRand();
+                       cPrev.set(cRand); setRand(cRand);
                        a1.set(); a2.set(); a3.set(); a4.set();
                } LastBeat = t;
 
@@ -268,17 +276,17 @@ public class Play extends DPat
                        case 6:         t = .5*(1-cos(PI*t));                                   break;  // slide
                }
                
-               cMid.set                (cPrev);        cMid.interpolate        (t,cRand);
-               cMidNorm.set    (cMid);         cMidNorm.setNorm();
+               cMid.set                (cPrev);        interpolate(t,cMid,cRand);
+               cMidNorm.set    (cMid);         setNorm(cMidNorm);
                a1.move(); a2.move(); a3.move(); a4.move();
        }
 
-       color CalcPoint(xyz Px) {
-               if (Theta.x != 0) Px.RotateX(mCtr, TSin.x, TCos.x);
-               if (Theta.y != 0) Px.RotateY(mCtr, TSin.y, TCos.y);
-               if (Theta.z != 0) Px.RotateZ(mCtr, TSin.z, TCos.z);
+       color CalcPoint(PVector Px) {
+               if (theta.x != 0) rotateX(Px, mCtr, tSin.x, tCos.x);
+               if (theta.y != 0) rotateY(Px, mCtr, tSin.y, tCos.y);
+               if (theta.z != 0) rotateZ(Px, mCtr, tSin.z, tCos.z);
                
-               Pn.set(Px); Pn.setNorm();
+               Pn.set(Px); setNorm(Pn);
 
                float mp        = min(Pn.x, Pn.z);
                float yt        = map(t,0,1,.5-bnc/2,.5+bnc/2);
@@ -303,7 +311,7 @@ public class Play extends DPat
                                                distToSeg(Px.x, Px.y, a1.getX(70),a1.getY(70), mCtr.x, mCtr.y),
                                                distToSeg(Px.x, Px.y, a2.getX(40),a2.getY(40), mCtr.x, mCtr.y));
                                        d = constrain(30*(rad*40-d),0,100);
-                                       return lx.hsb(0,max(0,150-d), d); // clock
+                                       return lx.hsb(lxh(),100, d); // clock
 
                case 8:         r = amp*200 * map(bnc,0,1,1,sin(PI*t));
                                        d = min(
@@ -312,7 +320,7 @@ public class Play extends DPat
                                                distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a1.getX(r),a1.getY(r))                             // triangle
                                                );
                                        d = constrain(30*(rad*40-d),0,100);
-                                       return lx.hsb(0,max(0,150-d), d); // clock
+                                       return lx.hsb(lxh(),100, d); // clock
 
                case 9:         r = amp*200 * map(bnc,0,1,1,sin(PI*t));
                                        d = min(
@@ -322,250 +330,223 @@ public class Play extends DPat
                                                distToSeg(Px.x, Px.y, a4.getX(r),a4.getY(r), a1.getX(r),a1.getY(r))                             // quad
                                        );
                                        d = constrain(30*(rad*40-d),0,100);
-                                       return lx.hsb(0,max(0,150-d), d); // clock
+                                       return lx.hsb(lxh(),100, d); // clock
 
                case 10:
                                        r = map(bnc,0,1,a1.r,amp*200*sin(PI*t));
-                                       return lx.hsb(0,0,c1c(.9+2*rad - dist(Px.x,Px.y,a1.getX(r),a1.getY(r))*.03) );          // sphere
+                                       return lx.hsb(lxh(),100,c1c(.9+2*rad - dist(Px.x,Px.y,a1.getX(r),a1.getY(r))*.03) );            // sphere
 
                case 11:
                                        Px.z=mCtr.z; cMid.z=mCtr.z;
-                                       return lx.hsb(0,0,c1c(1 - CalcCone(Px,cMid,mCtr) * 0.02 > .5?1:0));                             // cone
+                                       return lx.hsb(lxh(),100,c1c(1 - calcCone(Px,cMid,mCtr) * 0.02 > .5?1:0));                               // cone
 
-               case 12:        return lx.hsb(100 + noise(Pn.x,Pn.y,Pn.z + (NoiseMove+50000)/1000.)*200,
+               case 12:        return lx.hsb(lxh() + noise(Pn.x,Pn.y,Pn.z + (NoiseMove+50000)/1000.)*200,
                                                85,c1c(Pn.y < noise(Pn.x + NoiseMove/2000.,Pn.z)*(1+amp)-amp/2.-.1 ? 1 : 0));   // noise
 
-               case 13:        float y=0; for (rWave w : waves) y += .5*w.val(Pn.x);
+               case 13:        
+               case 14:        float y=0; for (rWave w : waves) y += .5*w.val(Pn.x);   // wave
                                        V.set(Pn.x, .7+y, Pn.z);
                                        break;
 
                default:        return lx.hsb(0,0,0);
                }
 
-               return lx.hsb(0,
-                               150-c1c(1 - V.distance(Pn)/rad),
-                               c1c(1 - V.distance(Pn)/rad));
+               return lx.hsb(lxh(), 100, c1c(1 - V.dist(Pn)/rad));
        }
 }
 //----------------------------------------------------------------------------------------------------------------------------------
-// 0 - TLB, L (b), BLB, B (l)          // Fwd , Down, Back, Up
-// 4 - TLF, F (l), BLF, L (f)          // Fwd , Down, Back, Up
-// 8 - TRF, R (f), BRF, F (r)          // Back, Down, Fwd , Up
-// 12- TRB, B (r), BRB, R (b)          // Back, Down, Fwd , Up
-// 1->7, 15->9
-
-class dBolt {
-       dStrip v, h;
-       int    vpos, hpos;
-       dBolt(dStrip _v, dStrip _h, int _vpos, int _hpos) {
-               v=_v; h=_h; vpos=_vpos; hpos=_hpos;
-               if (v.b0 == null) { v.b0=this; h.b0=this; } 
-               else                      { v.b1=this; h.b1=this; }
-       }
-}
-
-class dVertex {
-       dStrip  s1      , s2    ;
-       int     dir1, dir2      ;
-       dVertex(dStrip s, int d) {
-               int _a = (s.iS%4==1)? (d==1? 5: 3) :
-                                (s.iS%4==3)? (d==1? 9:11) :
-                                (d==1)         ? (s.Top()?4:12)   : (s.Top()?12:4);
-               dir1 = d * (s.isVert() ? -1 : 1);
-               dir2 = d;
-               s1       = DL_.DS[s.iCube() + ((s.iS+_a) % 16)];
-               s2       = DL_.DS[d == 1 ?      (s.idx == s.iFace()+3 ? s.idx-3 : s.idx+1):
-                                                               (s.idx == s.iFace()   ? s.idx+3 : s.idx-1)];
-               swapout(1 , 6);
-               swapout(15,-6);
-       }
-       void swapout(int a, int b) {
-               if (s1.iS == a) { s1 = DL_.DS[s1.idx + b]; dir1 = -dir1; }
-               if (s2.iS == a) { s2 = DL_.DS[s2.idx + b]; dir2 = -dir2; }
-       }
-
-}
+boolean dDebug = false;
+class dCursor {
+       dVertex vCur, vNext, vDest;
+       float   destSpeed;
+       int     posStop, pos,posNext;   // 0 - 65535
+       color   clr;
 
-class dStrip  { // THIS WAS SUCH A PAIN!
-       int row, col, ci, idx, iS, axis;        // 1-y, 2-left, 3-right
-       Strip s; 
-       boolean bTop;   // direction: top ccw, bottom cw.
-
-       boolean Top   (){       return axis!=1 &&  bTop ;               }
-       boolean Bottom(){       return axis!=1 && !bTop;                }
-       boolean isVert(){       return axis==1;                                 }
-       boolean isHorz(){       return axis!=1;                                 }
-       int     iCube (){       return 16*floor(idx/16);                }
-       int     iFace (){       return iCube() + 4*floor(iS/4); }
-
-       void    init(Strip _s, int _i, int _row, int _col)  {
-               idx = _i; row = _row; col = _col; s = _s;
-               iS      = idx%16; bTop = (iS%4==0);
-               ci  = s.points.get(0).index;
-               DL_.DQ[col][row] = iCube();
-               switch (iS) {
-                       case 4: case 6 : case 12: case 14:      axis=2; break;
-                       case 0: case 2 : case 8 : case 10:      axis=3; break;
-                       default:                                                        axis=1; break;
+       dCursor() {}
+
+       boolean isDone  ()                                                                      { return pos==posStop;                                                                           }
+       boolean atDest  ()                                                                      { return vCur.s==vDest.s || 
+                                                                                                                                xyDist(vCur.getPoint(0), vDest.getPoint(0)) < 12 || 
+                                                                                                                                xyDist(vCur.getPoint(0), vDest.getPoint(15))< 12;}
+       void    setCur  (dVertex _v, int _p)                            { p2=null; vCur=_v; pos=_p; pickNext();                                          }
+       void    setCur  (dPixel  _p)                                            { setCur(_p.v, _p.pos);                                                                          }
+       void    setNext (dVertex _v, int _p, int _s)            { vNext = _v; posNext = _p<<12; posStop = _s<<12;                        }
+       void    setDest (dVertex _v, float _speed)                      { vDest = _v; destSpeed = _speed;                                                        }
+       void    onDone  ()                                                                      { setCur(vNext, posNext); pickNext();                                            }
+
+       float   minDist;
+       int     nTurns;
+       boolean bRandEval;
+
+       void    evaluate(dVertex v, int p, int s) {
+               if (v == null) return; ++nTurns;
+               if (bRandEval) {
+                       if (random(nTurns) < 1) setNext(v,p,s); return; }
+               else {
+                       float d = xyDist(v.getPoint(15), vDest.getPoint(0));
+                       if (d <  minDist)                                       { minDist=d; setNext(v,p,s); }
+                       if (d == minDist && random(2)<1)        { minDist=d; setNext(v,p,s); }
                }
        }
 
-       void addBolts() {
-               v0 = new dVertex(this, 1);
-               v1 = new dVertex(this,-1);
+       void    evalTurn(dTurn t) { 
+               if (t == null || t.pos0<<12 <= pos) return; 
+               evaluate(t.v    ,    t.pos1, t.pos0);
+               evaluate(t.v.opp, 16-t.pos1, t.pos0);
+       }
 
+       void    pickNext()      {
+               bRandEval = random(.05+destSpeed) < .05; minDist=500; nTurns=0;
+               evaluate(vCur.c0, 0, 16);       evaluate(vCur.c1, 0, 16);
+               evaluate(vCur.c2, 0, 16);       evaluate(vCur.c3, 0, 16);
+               evalTurn(vCur.t0);                      evalTurn(vCur.t1);
+               evalTurn(vCur.t2);                      evalTurn(vCur.t3);
+       }
 
-               if (iS == 7 && col != 0 && row != 0)                                                                    // left bottom
-                       new dBolt(this, DL_.GetStrip(row-1,col-1,(col % 2 == 1) ? 8 : 12),  
-                                                       4, (col % 2 == 1) ? 6 : 9);     
+       LXPoint         p1, p2; int i2;
 
-               if (iS == 7 && col != 0 && row < MaxCubeHeight*2-2)                                     // left top
-                       new dBolt(this, DL_.GetStrip(row+1,col-1,(col % 2 == 1) ? 10 : 14), 
-                                                       11, (col % 2 == 1) ? 9 : 6);
+       int draw(int nAmount, SCPattern pat) {
+               int nFrom       = (pos    ) >> 12;
+               int     nMv     = min(nAmount, posStop-pos);
+               int     nTo     = min(15,(pos+nMv) >> 12);
+               dVertex v       = vCur;
 
-               if (iS == 9 && col < NumBackTowers-1 && row < MaxCubeHeight*2-2)                // right top
-                       new dBolt(this, DL_.GetStrip(row+1,col+1,(col % 2 == 1) ? 6 : 2), 
-                                                       4, (col % 2 == 1) ? 6 : 9);
+               if (dDebug) {   p1 = v.getPoint(nFrom); float d = (p2 == null ? 0 : pointDist(p1,p2)); if (d>5) { println("too wide! quitting: " + d); exit(); }}
+                                                               for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.ci          + v.dir*i     ] = clr; }
+               if (v.same != null)             for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.same.ci + v.same.dir*i] = clr; }
 
-               if (iS == 9 && col < NumBackTowers-1 && row != 0)                                               // right bottom
-                       new dBolt(this, DL_.GetStrip(row-1,col+1,(col % 2 == 1) ? 4 : 0), 
-                                                       11, (col % 2 == 1) ? 9 : 6);
-       }
+               if (dDebug) {   p2 = v.getPoint(nTo); i2 = nTo; }
 
-       dBolt   b0, b1;
-       dVertex v0, v1;
+               pos += nMv; return nAmount - nMv;
+                       }       
 }
 
-class dCursor {
-       dStrip  s, sNext;
-       int     nLast,pos,posNext,end;  // 0 - 65535
-       int     dir;                    // 1 or -1
-       color   clr;
-       
-       dCursor(color _c) { clr=_c;}
-
-       boolean isDone()                                        { return pos==end; }
-       void    set(dStrip _s, int _dir)        { 
-                       s=_s; dir=_dir; pos = 0; end=65536; nLast=-1; sNext=null;
+//----------------------------------------------------------------------------------------------------------------------------------
+class Worms extends SCPattern {
+       float   StripsPerSec    = 10;
+       float   TrailTime               = 3000;
+       int     numCursors              = 50;
+       ArrayList<dCursor> cur  = new ArrayList<dCursor>(30);
+
+       private GraphicEQ eq = null;
+
+       private BasicParameter pBeat      = new BasicParameter("BEAT",  0);
+       private BasicParameter pSpeed     = new BasicParameter("FAST", .2);
+       private BasicParameter pBlur      = new BasicParameter("BLUR", .3);
+       private BasicParameter pWorms     = new BasicParameter("WRMS", .3);
+       private BasicParameter pConfusion = new BasicParameter("CONF", .1);
+       private BasicParameter pEQ        = new BasicParameter("EQ"  ,  0);
+       private BasicParameter pSpawn     = new BasicParameter("DIR" ,  0);
+       private BasicParameter pColor     = new BasicParameter("CLR" ,  .1);
+
+       float   zMidLat = 82.;
+       float   nConfusion;
+       private final Click moveChase = new Click(1000);
+
+       PVector middle;
+       int     AnimNum() { return floor(pSpawn.getValuef()*(4-.01));   }
+       float   randX() { return random(model.xMax-model.xMin)+model.xMin; }
+       float   randY() { return random(model.yMax-model.yMin)+model.yMin; }
+       PVector randEdge() { 
+               return random(2) < 1 ?  new PVector(random(2)<1 ? model.xMin:model.xMax, randY(), zMidLat)      :
+                                                               new PVector(randX(), random(2)<1 ? model.yMin:model.yMax, zMidLat)      ;
        }
 
-       boolean MakeTurn(dBolt b) {
-               int nEnd=       (s.isVert() ? b.vpos : b.hpos) <<12;
-                       nEnd=   (dir==1 ? nEnd : 65536-nEnd);
-               if (nEnd < pos) return false;
-               if (s.isVert()) { sNext = b.h; posNext = b.hpos<<12; end = nEnd; }
-               else                    { sNext = b.v; posNext = b.vpos<<12; end = nEnd; }
-               return true;
+       Worms(LX lx) {
+               super(lx); 
+           addModulator(moveChase).start();
+           addParameter(pBeat);    addParameter(pSpeed);
+           addParameter(pBlur);    addParameter(pWorms);
+           addParameter(pEQ);      addParameter(pConfusion);
+               addParameter(pSpawn);   addParameter(pColor);
+
+           middle = new PVector(1.5*model.cx, 1.5*model.cy, 71);
+               if (lattice == null) lattice = new dLattice();
+               for (int i=0; i<numCursors; i++) { dCursor c = new dCursor(); reset(c); cur.add(c); }
+               onParameterChanged(pEQ); setNewDest();
        }
 
-       void    PickNext()      {
-               if (sNext != null) {
-                       if (end == 65536) exit();
-                       end                     = 65536;        
-                       pos                     = posNext;
-                       dir                     = randDir(); 
-                       if (dir<0) pos = end-pos;
-                       s                       = sNext; sNext = null;
-                       nLast           = -1;
-                       return;// could switch again!!
-               } else {
-                       dVertex v = (dir == 1 ? s.v0 : s.v1);
-                       int r = floor(random(2));
-                       set(r==0 ? v.s1 : v.s2,r==0 ? v.dir1 : v.dir2);
+       void onParameterChanged(LXParameter parameter) {
+               super.onParameterChanged(parameter);
+               nConfusion = 1-pConfusion.getValuef();
+               for (int i=0; i<numCursors; i++) {
+                       if (parameter==pSpawn) reset(cur.get(i));
+                       cur.get(i).destSpeed = nConfusion;
                }
-
-               // plan to turn the corner
-               if (random(6)<1 && s.b0 != null && MakeTurn(s.b0)) return;
-               if (random(6)<1 && s.b1 != null && MakeTurn(s.b1)) return;
        }
-}
 
-int randDir() { return round(random(1))*2-1; }
-
-class dLattice {
-       int             iTowerStrips=-1;
-       dStrip[]        DS = new dStrip[glucose.model.strips.size()];
-       int[][]         DQ = new int[NumBackTowers][MaxCubeHeight*2];
-
-       int             nStrips() { return iTowerStrips; }
-       dStrip GetStrip (int row, int col, int off) { return DS[DQ[col][row]+off]; }
-       dLattice() {
-               DL_=this;
-               int   col = 0, row = -2, i=-1;
-               for (Strip strip : glucose.model.strips  ) { i++; 
-                       if (i % 16 == 0) row+=2;
-                       if (row >= MaxCubeHeight*2-1) { col++; row = (col%2==1)?1:0; }
-                       if (col >= NumBackTowers) continue;
-                       iTowerStrips++  ;
-                       dStrip s = DS[iTowerStrips] = new dStrip();
-                       s.init(strip, iTowerStrips, row, col);
-               }
+       float getClr() { return lx.getBaseHuef() + random(pColor.getValuef()*300); }
+       void reset(dCursor c) {
+               switch(AnimNum()) {
+                       case 0: c.clr = lx.hsb(getClr(),100,100);                       // middle to edges
+                                       c.setDest(lattice.getClosest(randEdge()).v, nConfusion);
+                                       c.setCur (lattice.getClosest(middle));
+                                       break;
 
-               for (int j=0; j<iTowerStrips; j++) DS[j].addBolts();
-       }
-       dStrip  rand()                          { return DS[floor(random(iTowerStrips))];               }
-       void    setRand(dCursor c)      { c.set(rand(),randDir());                      }
-}
+                       case 1: c.clr = lx.hsb(getClr(),100,100);                               // top to bottom
+                                       float xLin = randX();
+                                       c.setDest(lattice.getClosest(new PVector(xLin, 0         , zMidLat)).v, nConfusion);
+                                       c.setCur (lattice.getClosest(new PVector(xLin, model.yMax, zMidLat)));
+                                       break;
 
-dLattice DL_;
-//----------------------------------------------------------------------------------------------------------------------------------
-class Worms extends SCPattern {
+                       case 2: c.clr = lx.hsb(getClr(),100,100); break;                // chase a point around
 
-       int draw(dCursor c, int nAmount) {
-               int nFrom       = max(c.nLast+1,c.pos >> 12);
-               int     nTo     = min(15,(c.pos+nAmount) >> 12); c.nLast=nTo;
-               int     nMv     = min(nAmount, c.end-c.pos);
-                       c.pos   += nMv;
-               for (int i = nFrom; i <= nTo; i++) {
-                       int n = c.s.ci + (c.dir>0 ? i : 15-i);
-                       colors[n] = c.clr;
+                       case 3: boolean bLeft = random(2)<1;
+                                       c.clr = lx.hsb(getClr()+random(120),100,100);                           // sideways
+                                       float yLin = randX();
+                                       c.setDest(lattice.getClosest(new PVector(bLeft ? 0 : model.xMax,yLin,zMidLat)).v, nConfusion);
+                                       c.setCur (lattice.getClosest(new PVector(bLeft ? model.xMax : 0,yLin,zMidLat)));
+                                       break;
                }
-               return nAmount - nMv;
+               if (pBlur.getValuef() == 1 && random(2)<1) c.clr = lx.hsb(0,0,0);
        }
 
-       float   StripsPerSec    = 6;
-       float   TrailTime               = 1500;
-       int     Cursors                 = 40;
-       dCursor cur[] = new dCursor[Cursors];
-
-       Worms(GLucose glucose) { 
-               super(glucose); 
-               if (DL_ == null) DL_ = new dLattice();
-               for (int i=0; i<Cursors; i++) { cur[i] = new dCursor(lx.hsb(random(360),50+random(50),50+random(50))); DL_.setRand(cur[i]); }
+       void setNewDest() {
+               if (AnimNum() != 2) return;
+               PVector dest = new PVector(randX(), randY(), zMidLat);
+               for (int i=0; i<numCursors; i++) {
+                       cur.get(i).setDest(lattice.getClosest(dest).v, nConfusion);
+                       cur.get(i).clr = lx.hsb(getClr()+75,100,100);   // chase a point around
+               }
        }
 
-       void run(double deltaMs) {
-                       //Test Joints
-               if (false) {
-                       for (int j=0; j<DL_.nStrips(); j++) {
-                                               dStrip s =DL_.DS[j]; dBolt d = s.b0;
-                                               if (d != null) {for (int i=0;i<16;i++) {
-                                                       if (s == d.v && i <= d.vpos) colors[d.v.ci+i]   = lx.hsb(0,0,30);
-                                                       if (s == d.h && i <= d.hpos) colors[d.h.ci+i]   = lx.hsb(0,0,30);
-                                               }}
-
-                                               d = s.b1; 
-                                               if (d != null) {for (int i=0;i<16;i++) {
-                                                       if (s == d.v && i >= d.vpos) colors[d.v.ci+i]   = lx.hsb(0,0,30);
-                                                       if (s == d.h && i >= d.hpos) colors[d.h.ci+i]   = lx.hsb(0,0,30);
-                                               }}
-                       }
-               } else {
-                       for (int i=0; i<Cursors; i++) {
-                               int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536);
-                               while(nLeft > 0) {      
-                                       nLeft = draw(cur[i], nLeft);
-                                       if (cur[i].isDone()) cur[i].PickNext(); 
-                               }
-                       }
+       void run(double deltaMs) { 
+               if (deltaMs > 100) return;
+           if (moveChase.click()) setNewDest();
+
+           float fBass=0, fTreble=0;
+           if (pEQ.getValuef()>0) {            // EQ
+                   eq.run(deltaMs);
+                   fBass       = eq.getAverageLevel(0, 4);
+                   fTreble = eq.getAverageLevel(eq.numBands-7, 7);
+               }
 
+               if (pBlur.getValuef() < 1) {    // trails
                        for (int i=0,s=model.points.size(); i<s; i++) {
-                               color c = colors[i];
-                               float b = lx.b(c);
-                               if (b>0) colors[i] = lx.hsb(lx.h(c), lx.s(c), 
-                                               max(0, (float)(b-100*deltaMs/TrailTime)));
+                               color c = colors[i]; float b = lx.b(c); 
+                               if (b>0) colors[i] = lx.hsb(lx.h(c), lx.s(c), constrain((float)(b-100*deltaMs/(pBlur.getValuef()*TrailTime)),0,100));
+                       }
+               }
+
+               int nWorms = floor(pWorms.getValuef() * numCursors * 
+                                        map(pEQ.getValuef(),0,1,1,constrain(2*fTreble,0,1)));
+
+               for (int i=0; i<nWorms; i++) {
+                       dCursor c = cur.get(i);
+                       int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536 * (5*pSpeed.getValuef()));
+                       nLeft *= (1 - lx.tempo.rampf()*pBeat.getValuef());
+                       while(nLeft > 0) { 
+                               nLeft = c.draw(nLeft,this); if (!c.isDone()) continue;
+                               c.onDone(); if (c.atDest()) reset(c);
                        }
                }
        }
+
+
+       public void onActive() { if (eq == null) {
+               eq = new GraphicEQ(lx, 16);             eq.slope.setValue(0.6);
+               eq.level.setValue(0.65);                eq.range.setValue(0.35);
+               eq.release.setValue(0.4);
+       }}
 }
 //----------------------------------------------------------------------------------------------------------------------------------