Update TestPerformance to use all methods of getting geometry
[SugarCubes.git] / DanHorwitz.pde
CommitLineData
a41f334c
MS
1//----------------------------------------------------------------------------------------------------------------------------------
2public class Pong extends DPat {
3 SinLFO x,y,z,dx,dy,dz;
4 float cRad; DParam pSize;
5 Pick pChoose;
6 xyz v = new xyz(), vMir = new xyz();
7
8 Pong(GLucose glucose) {
9 super(glucose);
10 cRad = mMax.x/10;
11 addModulator(dx = new SinLFO(6000, 500, 30000 )).trigger();
12 addModulator(dy = new SinLFO(3000, 500, 22472 )).trigger();
13 addModulator(dz = new SinLFO(1000, 500, 18420 )).trigger();
14 addModulator(x = new SinLFO(cRad, mMax.x - cRad, 0)).trigger(); x.modulateDurationBy(dx);
15 addModulator(y = new SinLFO(cRad, mMax.y - cRad, 0)).trigger(); y.modulateDurationBy(dy);
16 addModulator(z = new SinLFO(cRad, mMax.z - cRad, 0)).trigger(); z.modulateDurationBy(dz);
17 pSize = addParam ("Size" , 0.4 );
18 pChoose = addPick ("Animiation" , 0, 2, new String[] {"Pong", "Ball", "Cone"} );
19 }
20
21 void StartRun(double deltaMs) { cRad = mMax.x*pSize.Val()/6; }
22 color CalcPoint(xyz p) {
23 v.set(x.getValuef(), y.getValuef(), z.getValuef());
24 v.z=0;p.z=0;// ignore z dimension
25 switch(pChoose.Cur()) {
26 case 0: vMir.set(mMax); vMir.subtract(p);
27 return lx.hsb(0,0,c1c(1 - min(v.distance(p), v.distance(vMir))*.5/cRad)); // balls
28 case 1: return lx.hsb(0,0,c1c(1 - v.distance(p)*.5/cRad)); // ball
29 case 2: vMir.set(mMax.x/2,0,mMax.z/2);
30 return lx.hsb(0,0,c1c(1 - CalcCone(p,v,vMir) * max(.02,.45-pSize.Val()))); // spot
31 }
32 return lx.hsb(0,0,0);
33 }
34}
35//----------------------------------------------------------------------------------------------------------------------------------
36public class NDat {
37 float xz, yz, zz, hue, sat, speed, angle, den;
38 float xoff,yoff,zoff;
39 float sinAngle, cosAngle;
40 boolean isActive;
41 NDat () { isActive=false; }
42 boolean Active() { return isActive; }
43 void set (float _hue, float _sat, float _xz, float _yz, float _zz, float _den, float _speed, float _angle) {
44 isActive = true;
45 hue=_hue; sat=_sat; xz=_xz; yz=_yz; zz =_zz; den=_den; speed=_speed; angle=_angle;
46 xoff = random(100e3); yoff = random(100e3); zoff = random(100e3);
47 }
48}
49
50public class Noise extends DPat
51{
52 int CurAnim, iSymm;
53 int XSym=1,YSym=2,RadSym=3;
54 float zTime , zTheta=0, zSin, zCos, rtime, ttime, transAdd;
55 DParam pSpeed , pDensity;
56 Pick pChoose, pSymm;
57 int _ND = 4;
58 NDat N[] = new NDat[_ND];
59
60 Noise(GLucose glucose) {
61 super(glucose);
62 pSpeed = addParam("Fast" , .55);
63 pDensity = addParam("Dens" , .5);
64 pSymm = addPick("Symmetry" , 0, 3, new String[] {"None", "X", "Y", "Radial"} );
65 pChoose = addPick("Animation", 6, 7, new String[] {"Drip", "Cloud", "Rain", "Fire", "Machine", "Spark","VWave", "Wave"} );
66 for (int i=0; i<_ND; i++) N[i] = new NDat();
67 }
68
69 void StartPattern() { zTime = random(500); zTheta=0; rtime = 0; ttime = 0; transAdd=0; }
70 void StartRun(double deltaMs) {
71 zTime += deltaMs*(pSpeed.Val()-.5)*.002 ;
72 zTheta += deltaMs*(pSpin .Val()-.5)*.01 ;
73 rtime += deltaMs;
74 iSymm = pSymm.Cur();
75 transAdd = 1*(1 - constrain(rtime - ttime,0,1000)/1000);
76 zSin = sin(zTheta);
77 zCos = cos(zTheta);
78
79 if (pChoose.Cur() != CurAnim) {
80 CurAnim = pChoose.Cur(); ttime = rtime;
81 pSpin .reset(); zTheta = 0;
82 pDensity .reset(); pSpeed .reset();
83 for (int i=0; i<_ND; i++) { N[i].isActive = false; }
84
85 switch(CurAnim) {
86 // hue sat xz yz zz den mph angle
87 case 0: N[0].set(0 ,0 ,75 ,75 ,150,45 ,3 ,0 ); pSharp.set(1 ); break; // drip
88 case 1: N[0].set(0 ,0 ,100,100,200,45 ,3 ,180); pSharp.set(0 ); break; // clouds
89 case 2: N[0].set(0 ,0 ,2 ,400,2 ,20 ,3 ,0 ); pSharp.set(.5); break; // rain
90 case 3: N[0].set(40 ,1 ,100,100,200,10 ,1 ,180);
91 N[1].set(0 ,1 ,100,100,200,10 ,5 ,180); pSharp.set(0 ); break; // fire 1
92 case 4: N[0].set(0 ,1 ,40 ,40 ,40 ,15 ,2.5,180);
93 N[1].set(20 ,1 ,40 ,40 ,40 ,15 ,4 ,0 );
94 N[2].set(40 ,1 ,40 ,40 ,40 ,15 ,2 ,90 );
95 N[3].set(60 ,1 ,40 ,40 ,40 ,15 ,3 ,-90); pSharp.set(.5); break; // machine
96 case 5: N[0].set(0 ,1 ,400,100,2 ,15 ,3 ,90 );
97 N[1].set(20 ,1 ,400,100,2 ,15 ,2.5,0 );
98 N[2].set(40 ,1 ,100,100,2 ,15 ,2 ,180);
99 N[3].set(60 ,1 ,100,100,2 ,15 ,1.5,270); pSharp.set(.5); break; // spark
100 }
101
102 DG.UpdateLights();
103 }
104
105 for (int i=0; i<_ND; i++) if (N[i].Active()) {
106 N[i].sinAngle = sin(radians(N[i].angle));
107 N[i].cosAngle = cos(radians(N[i].angle));
108 }
109 }
110
111 color CalcPoint(xyz P) {
112 color c = 0;
113 P.RotateZ(mCtr, zSin, zCos);
114
115 if (CurAnim == 6 || CurAnim == 7) {
116 P.setNorm();
117 return lx.hsb(0,0, 100 * (
118 constrain(1-50*(1-pDensity.Val())*abs(P.y-sin(zTime*10 + P.x*(300))*.5 - .5),0,1) +
119 (CurAnim == 7 ? constrain(1-50*(1-pDensity.Val())*abs(P.x-sin(zTime*10 + P.y*(300))*.5 - .5),0,1) : 0))
120 );
121 }
122
123 if (iSymm == XSym && P.x > mMax.x/2) P.x = mMax.x-P.x;
124 if (iSymm == YSym && P.y > mMax.y/2) P.y = mMax.y-P.y;
125
126 for (int i=0;i<_ND; i++) if (N[i].Active()) {
127 NDat n = N[i];
128 float zx = zTime * n.speed * n.sinAngle,
129 zy = zTime * n.speed * n.cosAngle;
130
131 float b = (iSymm==RadSym ? noise(zTime*n.speed+n.xoff-Dist(P,mCtr)/n.xz)
132 : noise(P.x/n.xz+zx+n.xoff,P.y/n.yz+zy+n.yoff,P.z/n.zz+n.zoff))
133 *1.8;
134
135 b += n.den/100 -.4 + pDensity.Val() -1;
136 b += transAdd;
137 c = blendColor(c,lx.hsb(n.hue,100*n.sat,c1c(b)),ADD);
138 }
139 return c;
140 }
141}
142//----------------------------------------------------------------------------------------------------------------------------------
143public class Play extends DPat
144{
145 public class rAngle {
146 float prvA, dstA, c;
147 float prvR, dstR, r;
148 float _cos, _sin, x, y;
149 float fixAngle (float a, float b) { return a<b ?
150 (abs(a-b) > abs(a+2*PI-b) ? a : a+2*PI) :
151 (abs(a-b) > abs(a-2*PI-b) ? a : a-2*PI) ; }
152 float getX(float r) { return mCtr.x + _cos*r; }
153 float getY(float r) { return mCtr.y + _sin*r; }
154 void move() { c = interp(t,prvA,dstA);
155 r = interp(t,prvR,dstR);
156 _cos = cos(c); _sin = sin(c);
157 x = getX(r); y = getY(r); }
158 void set() { prvA = dstA; dstA = random(2*PI); prvA = fixAngle(prvA, dstA);
159 prvR = dstR; dstR = random(mCtr.y); }
160 }
161
162 int nBeats = 0;
163 DParam pAmp, pRadius, pBounce;
164
165 float t,amp,rad,bnc;
166 float zTheta=0;
167 ArrayList<rWave> waves = new ArrayList<rWave>(10);
168
169 rAngle a1 = new rAngle(), a2 = new rAngle(),
170 a3 = new rAngle(), a4 = new rAngle();
171 xyz cPrev = new xyz(), cRand = new xyz(),
172 cMid = new xyz(), V = new xyz(),
173 Theta = new xyz(), TSin = new xyz(),
174 TCos = new xyz(), cMidNorm = new xyz(),
175 Pn = new xyz();
176 float LastBeat=3, LastMeasure=3;
177 int CurRandTempo = 1, CurRandTPat = 1;
178
179 Pick pTimePattern, pTempoMult, pShape;
180 int RandCube;
181
182 Play(GLucose glucose) {
183 super(glucose);
184 pRadius = addParam("Rad" , .1 );
185 pBounce = addParam("Bnc" , .2 );
186 pAmp = addParam("Amp" , .2 );
187 pTempoMult = addPick ("TMult" , 0 , 5 , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand" } );
188 pTimePattern= addPick ("TPat" , 6 , 7 , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"} );
189 pShape = addPick ("Shape" , 3 , 15 , new String[] {"Line", "Tap", "V", "RandV",
190 "Pyramid", "Wings", "W2", "Clock",
191 "Triangle", "Quad", "Sphere", "Cone",
192 "Noise", "Wave", "?", "?"} );
193 }
194
195 public class rWave {
196 float v0, a0, x0, t,damp,a;
197 boolean bDone=false;
198 final float len=8;
199 rWave(float _x0, float _a0, float _v0, float _damp) { x0=_x0*len; a0=_a0; v0=_v0; t=0; damp = _damp; }
200 void move(double deltaMs) {
201 t += deltaMs*.001;
202 if (t>4) bDone=true;
203 }
204 float val(float _x) {
205 _x*=len;
206 float dist = t*v0 - abs(_x-x0);
207 if (dist<0) { a=1; return 0; }
208 a = a0*exp(-dist*damp) * exp(-abs(_x-x0)/(.2*len)); // * max(0,1-t/dur)
209 return -a*sin(dist);
210 }
211 }
212
213 void StartPattern() { zTheta=0; }
214 void StartRun(double deltaMs) {
215 t = lx.tempo.rampf();
216 amp = pAmp.Val();
217 rad = pRadius.getValuef();
218 bnc = pBounce.getValuef();
219 zTheta += deltaMs*(pSpin .Val()-.5)*.01;
220
221 Theta .set(pRotX.Val()*PI*2, pRotY.Val()*PI*2, pRotZ.Val()*PI*2 + zTheta);
222 TSin .set(sin(Theta.x), sin(Theta.y), sin(Theta.z));
223 TCos .set(cos(Theta.x), cos(Theta.y), cos(Theta.z));
224
225 if (t<LastMeasure) {
226 if (random(3) < 1) { CurRandTempo = int(random(4)); if (CurRandTempo == 3) CurRandTempo = int(random(4)); }
227 if (random(3) < 1) { CurRandTPat = pShape.Cur() > 6 ? 2+int(random(5)) : int(random(7)); }
228 } LastMeasure = t;
229
230 int nTempo = pTempoMult .Cur(); if (nTempo == 5) nTempo = CurRandTempo;
231 int nTPat = pTimePattern.Cur(); if (nTPat == 7) nTPat = CurRandTPat ;
232
233 switch (nTempo) {
234 case 0: t = t; break;
235 case 1: t = (t*2. )%1.; break;
236 case 2: t = (t*4. )%1.; break;
237 case 3: t = (t*8. )%1.; break;
238 case 4: t = (t*16.)%1.; break;
239 }
240
241 int i=0; while (i< waves.size()) {
242 rWave w = waves.get(i);
243 w.move(deltaMs); if (w.bDone) waves.remove(i); else i++;
244 }
245
246 if ((t<LastBeat && !pKey.b) || DG.KeyPressed>-1) {
247 waves.add(new rWave(
248 pKey.b ? map(DG.KeyPressed,0,7,0,1) : random(1), // location
249 bnc*10, // bounciness
250 7, // velocity
251 2*(1-amp))); // dampiness
252 DG.KeyPressed=-1;
253 if (waves.size() > 5) waves.remove(0);
254 }
255
256 if (t<LastBeat) {
257 cPrev.set(cRand); cRand.setRand();
258 a1.set(); a2.set(); a3.set(); a4.set();
259 } LastBeat = t;
260
261 switch (nTPat) {
262 case 0: t = sin(PI*t); break; // bounce
263 case 1: t = norm(sin(2*PI*(t+PI/2)),-1,1); break; // sin
264 case 2: t = t; break; // roll
265 case 3: t = constrain(int(t*8)/7.,0,1); break; // quant
266 case 4: t = t*t*t; break; // accel
267 case 5: t = sin(PI*t*.5); break; // deccel
268 case 6: t = .5*(1-cos(PI*t)); break; // slide
269 }
270
271 cMid.set (cPrev); cMid.interpolate (t,cRand);
272 cMidNorm.set (cMid); cMidNorm.setNorm();
273 a1.move(); a2.move(); a3.move(); a4.move();
274 }
275
276 color CalcPoint(xyz Px) {
277 if (Theta.x != 0) Px.RotateX(mCtr, TSin.x, TCos.x);
278 if (Theta.y != 0) Px.RotateY(mCtr, TSin.y, TCos.y);
279 if (Theta.z != 0) Px.RotateZ(mCtr, TSin.z, TCos.z);
280
281 Pn.set(Px); Pn.setNorm();
282
283 float mp = min(Pn.x, Pn.z);
284 float yt = map(t,0,1,.5-bnc/2,.5+bnc/2);
285 float r,d;
286
287 switch (pShape.Cur()) {
288 case 0: V.set(Pn.x, yt , Pn.z); break; // bouncing line
289 case 1: V.set(Pn.x, map(cos(PI*t * Pn.x),-1,1,0,1) , Pn.z); break; // top tap
290 case 2: V.set(Pn.x, bnc*map(Pn.x<.5?Pn.x:1-Pn.x,0,.5 ,0,t-.5)+.5, Pn.z); break; // V shape
291 case 3: V.set(Pn.x, Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
292 map(Pn.x,cMidNorm.x,1, yt,.5), Pn.z); break; // Random V shape
293
294 case 4: V.set(Pn.x, .5*(Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
295 map(Pn.x,cMidNorm.x,1, yt,.5)) +
296 .5*(Pn.z < cMidNorm.z ? map(Pn.z,0,cMidNorm.z, .5,yt) :
297 map(Pn.z,cMidNorm.z,1, yt,.5)), Pn.z); break; // Random Pyramid shape
298
299 case 5: V.set(Pn.x, bnc*map((Pn.x-.5)*(Pn.x-.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
300 case 6: V.set(Pn.x, bnc*map((mp -.5)*(mp -.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
301
302 case 7: d = min(
303 distToSeg(Px.x, Px.y, a1.getX(70),a1.getY(70), mCtr.x, mCtr.y),
304 distToSeg(Px.x, Px.y, a2.getX(40),a2.getY(40), mCtr.x, mCtr.y));
305 d = constrain(30*(rad*40-d),0,100);
306 return lx.hsb(0,max(0,150-d), d); // clock
307
308 case 8: r = amp*200 * map(bnc,0,1,1,sin(PI*t));
309 d = min(
310 distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
311 distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
312 distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a1.getX(r),a1.getY(r)) // triangle
313 );
314 d = constrain(30*(rad*40-d),0,100);
315 return lx.hsb(0,max(0,150-d), d); // clock
316
317 case 9: r = amp*200 * map(bnc,0,1,1,sin(PI*t));
318 d = min(
319 distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
320 distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
321 distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a4.getX(r),a4.getY(r)),
322 distToSeg(Px.x, Px.y, a4.getX(r),a4.getY(r), a1.getX(r),a1.getY(r)) // quad
323 );
324 d = constrain(30*(rad*40-d),0,100);
325 return lx.hsb(0,max(0,150-d), d); // clock
326
327 case 10:
328 r = map(bnc,0,1,a1.r,amp*200*sin(PI*t));
329 return lx.hsb(0,0,c1c(.9+2*rad - dist(Px.x,Px.y,a1.getX(r),a1.getY(r))*.03) ); // sphere
330
331 case 11:
332 Px.z=mCtr.z; cMid.z=mCtr.z;
333 return lx.hsb(0,0,c1c(1 - CalcCone(Px,cMid,mCtr) * 0.02 > .5?1:0)); // cone
334
335 case 12: return lx.hsb(100 + noise(Pn.x,Pn.y,Pn.z + (NoiseMove+50000)/1000.)*200,
336 85,c1c(Pn.y < noise(Pn.x + NoiseMove/2000.,Pn.z)*(1+amp)-amp/2.-.1 ? 1 : 0)); // noise
337
338 case 13: float y=0; for (rWave w : waves) y += .5*w.val(Pn.x);
339 V.set(Pn.x, .7+y, Pn.z);
340 break;
341
342 default: return lx.hsb(0,0,0);
343 }
344
345 return lx.hsb(0,
346 150-c1c(1 - V.distance(Pn)/rad),
347 c1c(1 - V.distance(Pn)/rad));
348 }
349}
350//----------------------------------------------------------------------------------------------------------------------------------
351// 0 - TLB, L (b), BLB, B (l) // Fwd , Down, Back, Up
352// 4 - TLF, F (l), BLF, L (f) // Fwd , Down, Back, Up
353// 8 - TRF, R (f), BRF, F (r) // Back, Down, Fwd , Up
354// 12- TRB, B (r), BRB, R (b) // Back, Down, Fwd , Up
355// 1->7, 15->9
356
357class dBolt {
358 dStrip v, h;
359 int vpos, hpos;
360 dBolt(dStrip _v, dStrip _h, int _vpos, int _hpos) {
361 v=_v; h=_h; vpos=_vpos; hpos=_hpos;
362 if (v.b0 == null) { v.b0=this; h.b0=this; }
363 else { v.b1=this; h.b1=this; }
364 }
365}
366
367class dVertex {
368 dStrip s1 , s2 ;
369 int dir1, dir2 ;
370 dVertex(dStrip s, int d) {
371 int _a = (s.iS%4==1)? (d==1? 5: 3) :
372 (s.iS%4==3)? (d==1? 9:11) :
373 (d==1) ? (s.Top()?4:12) : (s.Top()?12:4);
374 dir1 = d * (s.isVert() ? -1 : 1);
375 dir2 = d;
376 s1 = DL_.DS[s.iCube() + ((s.iS+_a) % 16)];
377 s2 = DL_.DS[d == 1 ? (s.idx == s.iFace()+3 ? s.idx-3 : s.idx+1):
378 (s.idx == s.iFace() ? s.idx+3 : s.idx-1)];
379 swapout(1 , 6);
380 swapout(15,-6);
381 }
382 void swapout(int a, int b) {
383 if (s1.iS == a) { s1 = DL_.DS[s1.idx + b]; dir1 = -dir1; }
384 if (s2.iS == a) { s2 = DL_.DS[s2.idx + b]; dir2 = -dir2; }
385 }
386
387}
388
389class dStrip { // THIS WAS SUCH A PAIN!
390 int row, col, ci, idx, iS, axis; // 1-y, 2-left, 3-right
391 Strip s;
392 boolean bTop; // direction: top ccw, bottom cw.
393
394 boolean Top (){ return axis!=1 && bTop ; }
395 boolean Bottom(){ return axis!=1 && !bTop; }
396 boolean isVert(){ return axis==1; }
397 boolean isHorz(){ return axis!=1; }
398 int iCube (){ return 16*floor(idx/16); }
399 int iFace (){ return iCube() + 4*floor(iS/4); }
400
401 void init(Strip _s, int _i, int _row, int _col) {
402 idx = _i; row = _row; col = _col; s = _s;
403 iS = idx%16; bTop = (iS%4==0);
404 ci = s.points.get(0).index;
405 DL_.DQ[col][row] = iCube();
406 switch (iS) {
407 case 4: case 6 : case 12: case 14: axis=2; break;
408 case 0: case 2 : case 8 : case 10: axis=3; break;
409 default: axis=1; break;
410 }
411 }
412
413 void addBolts() {
414 v0 = new dVertex(this, 1);
415 v1 = new dVertex(this,-1);
416
417
418 if (iS == 7 && col != 0 && row != 0) // left bottom
419 new dBolt(this, DL_.GetStrip(row-1,col-1,(col % 2 == 1) ? 8 : 12),
420 4, (col % 2 == 1) ? 6 : 9);
421
422 if (iS == 7 && col != 0 && row < MaxCubeHeight*2-2) // left top
423 new dBolt(this, DL_.GetStrip(row+1,col-1,(col % 2 == 1) ? 10 : 14),
424 11, (col % 2 == 1) ? 9 : 6);
425
426 if (iS == 9 && col < NumBackTowers-1 && row < MaxCubeHeight*2-2) // right top
427 new dBolt(this, DL_.GetStrip(row+1,col+1,(col % 2 == 1) ? 6 : 2),
428 4, (col % 2 == 1) ? 6 : 9);
429
430 if (iS == 9 && col < NumBackTowers-1 && row != 0) // right bottom
431 new dBolt(this, DL_.GetStrip(row-1,col+1,(col % 2 == 1) ? 4 : 0),
432 11, (col % 2 == 1) ? 9 : 6);
433 }
434
435 dBolt b0, b1;
436 dVertex v0, v1;
437}
438
439class dCursor {
440 dStrip s, sNext;
441 int nLast,pos,posNext,end; // 0 - 65535
442 int dir; // 1 or -1
443 color clr;
444
445 dCursor(color _c) { clr=_c;}
446
447 boolean isDone() { return pos==end; }
448 void set(dStrip _s, int _dir) {
449 s=_s; dir=_dir; pos = 0; end=65536; nLast=-1; sNext=null;
450 }
451
452 boolean MakeTurn(dBolt b) {
453 int nEnd= (s.isVert() ? b.vpos : b.hpos) <<12;
454 nEnd= (dir==1 ? nEnd : 65536-nEnd);
455 if (nEnd < pos) return false;
456 if (s.isVert()) { sNext = b.h; posNext = b.hpos<<12; end = nEnd; }
457 else { sNext = b.v; posNext = b.vpos<<12; end = nEnd; }
458 return true;
459 }
460
461 void PickNext() {
462 if (sNext != null) {
463 if (end == 65536) exit();
464 end = 65536;
465 pos = posNext;
466 dir = randDir();
467 if (dir<0) pos = end-pos;
468 s = sNext; sNext = null;
469 nLast = -1;
470 return;// could switch again!!
471 } else {
472 dVertex v = (dir == 1 ? s.v0 : s.v1);
473 int r = floor(random(2));
474 set(r==0 ? v.s1 : v.s2,r==0 ? v.dir1 : v.dir2);
475 }
476
477 // plan to turn the corner
478 if (random(6)<1 && s.b0 != null && MakeTurn(s.b0)) return;
479 if (random(6)<1 && s.b1 != null && MakeTurn(s.b1)) return;
480 }
481}
482
483int randDir() { return round(random(1))*2-1; }
484
485class dLattice {
486 int iTowerStrips=-1;
487 dStrip[] DS = new dStrip[glucose.model.strips.size()];
488 int[][] DQ = new int[NumBackTowers][MaxCubeHeight*2];
489
490 int nStrips() { return iTowerStrips; }
491 dStrip GetStrip (int row, int col, int off) { return DS[DQ[col][row]+off]; }
492 dLattice() {
493 DL_=this;
494 int col = 0, row = -2, i=-1;
495 for (Strip strip : glucose.model.strips ) { i++;
496 if (i % 16 == 0) row+=2;
497 if (row >= MaxCubeHeight*2-1) { col++; row = (col%2==1)?1:0; }
498 if (col >= NumBackTowers) continue;
499 iTowerStrips++ ;
500 dStrip s = DS[iTowerStrips] = new dStrip();
501 s.init(strip, iTowerStrips, row, col);
502 }
503
504 for (int j=0; j<iTowerStrips; j++) DS[j].addBolts();
505 }
506 dStrip rand() { return DS[floor(random(iTowerStrips))]; }
507 void setRand(dCursor c) { c.set(rand(),randDir()); }
508}
509
510dLattice DL_;
511//----------------------------------------------------------------------------------------------------------------------------------
512class Worms extends SCPattern {
513
514 int draw(dCursor c, int nAmount) {
515 int nFrom = max(c.nLast+1,c.pos >> 12);
516 int nTo = min(15,(c.pos+nAmount) >> 12); c.nLast=nTo;
517 int nMv = min(nAmount, c.end-c.pos);
518 c.pos += nMv;
519 for (int i = nFrom; i <= nTo; i++) {
520 int n = c.s.ci + (c.dir>0 ? i : 15-i);
521 colors[n] = c.clr;
522 }
523 return nAmount - nMv;
524 }
525
526 float StripsPerSec = 6;
527 float TrailTime = 1500;
528 int Cursors = 40;
529 dCursor cur[] = new dCursor[Cursors];
530
531 Worms(GLucose glucose) {
532 super(glucose);
533 if (DL_ == null) DL_ = new dLattice();
534 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]); }
535 }
536
537 void run(double deltaMs) {
538 //Test Joints
539 if (false) {
540 for (int j=0; j<DL_.nStrips(); j++) {
541 dStrip s =DL_.DS[j]; dBolt d = s.b0;
542 if (d != null) {for (int i=0;i<16;i++) {
543 if (s == d.v && i <= d.vpos) colors[d.v.ci+i] = lx.hsb(0,0,30);
544 if (s == d.h && i <= d.hpos) colors[d.h.ci+i] = lx.hsb(0,0,30);
545 }}
546
547 d = s.b1;
548 if (d != null) {for (int i=0;i<16;i++) {
549 if (s == d.v && i >= d.vpos) colors[d.v.ci+i] = lx.hsb(0,0,30);
550 if (s == d.h && i >= d.hpos) colors[d.h.ci+i] = lx.hsb(0,0,30);
551 }}
552 }
553 } else {
554 for (int i=0; i<Cursors; i++) {
555 int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536);
556 while(nLeft > 0) {
557 nLeft = draw(cur[i], nLeft);
558 if (cur[i].isDone()) cur[i].PickNext();
559 }
560 }
561
562 for (int i=0,s=model.points.size(); i<s; i++) {
563 color c = colors[i];
564 float b = lx.b(c);
565 if (b>0) colors[i] = lx.hsb(lx.h(c), lx.s(c),
566 max(0, (float)(b-100*deltaMs/TrailTime)));
567 }
568 }
569 }
570}
571//----------------------------------------------------------------------------------------------------------------------------------