V Z=new V(),Y=new V(),X=new V(),W=new V(),U=new V();int N=128,M=64,R=50,u=128,v=2000,A,C,D,E,F,G,H,I,J,i,j,t=100,h;V[] n=new V[u],f=new V[M];O[] x=new O[N];B[] z=new B[5];ArrayList<P> y=new ArrayList<P>();class V{float x,y,z,w;V a(float a,float b,float c,float d){x=a;y=b;z=c;w=d;return this;}V b(V v){x=v.x;y=v.y;z=v.z;w=v.w;return this;}V c(V v){x+=v.x;y+=v.y;z+=v.z;w+=v.w;return this;}V d(float s){x*=s;y*=s;z*=s;w*=s;return this;}V e(V q){return a(w*q.x+x*q.w+y*q.z-z*q.y,w*q.y-x*q.z+y*q.w+z*q.x,w*q.z+x*q.y-y*q.x+z*q.w,w*q.w-x*q.x-y*q.y-z*q.z);}V f(V q){Y.b(this);b(q).e(Y);q.w*=-1;e(q);q.w*=-1;return this;}V g(float a){w=0;h(1).d(sin(a/2)).w=cos(a/2);return this;}V h(float a){float m=i();return d(m==0?0:a/m);}float i(){return sqrt(k(this));}V j(V v){return this.a(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x,v.w);}float k(V v){return x*v.x+y*v.y+z*v.z+w*v.w;}float l(V v){return acos(max(min(k(v)/i()/v.i(),1),-1));}float m(V v){return Y.b(this).d(-1).c(v).i();}V n(float r){return a(random(2)-1,random(2)-1,random(2)-1,0).h(1).d(r);}}class S{V a=new V(),b=new V(),c=new V(),d=new V(),e=new V(),f=new V(),g=new V().a(0,0,0,1),h=new V(),i=new V(),j=new V().a(0,0,0,1);float m,p=1,q=1;void k(){Z.a(0,0,-1,0);i.b(h).z=0;float q=max(i.i()-5,0)*1e-4;if(q<1e-30)e.a(0,0,0,1);else{Z.j(i);Z.g(q);e.b(Z);}e.e(Z.a(0,0,-1,0).g((I-J)*.03));Z.b(g).w*=-1;Z.e(e);q=(acos(min(max(Z.w,-1),1))*2+PI/2)%PI-PI/2;q=min(.001,abs(q))*(q<0?-1:1);if(abs(q)>1e-30){Z.g(q);g.e(Z);}if(abs(g.w)>1-1e-30)g.a(0,0,0,1);j.e(g);float p=H>0?1:c.i();Z.d(0);if(p>0){Z.b(c).d(8).f(j).c(b.d(-1));b.d(-1);q=Z.i();Z.h(min(1.28*p*min(1,b.i()*.01+.02),q));b.c(Z);L.j.w*=-1;Z.f(L.j);L.j.w*=-1;}f.d(.9).c(Z.d(.1));m=max(0,max(m-.05,m*.9+abs(f.y)*5.5));a.c(b);}}S L;class P{V a=new V(),b=new V();int k;}class B{V a=new V().n(5e3),g=new V().n(1),b=new V();int c,d=10,e;float o=random(5);}class O{V a=new V().n(300+random(5e3));float c=random(200)+10;}void s(){pushMatrix();}void r(){popMatrix();}void Q(int a,int b,int c){float d=1-max(L.m-3,0)/4;fill(a*d,b*d,c*d);}void K(float x,float y,float z){translate(x,y,z);}void setup(){L=new S();size(800,800,P3D);frameRate(60);for(i=0;i<N;i++)x[i]=new O();for(i=0;i<M;i++)f[i]=new V().n(t);for(i=0;i<5;i++)z[i]=new B();for(i=0;i<u;i++)n[i]=new V();}void die(){L=new S();L.m=10;}void draw(){h=frameCount;background(0);L.h.a(width/2-mouseX,mouseY-height/2,-height*.866,0);L.c.a(A-C,E-D,G-F,0).h(H<1?1:0);L.k();if(L.q<1)L.q+=.005;else{L.p=min(L.p+.001,1);}if(L.p<=0)die();X.a(0,1,0,0).f(L.j);W.a(0,0,-1,0).f(L.j);if(mousePressed&&h%6==0){P p=new P();p.a.b(X).d(j=20).c(L.a).c(Z.a(h%12==0?j:-j,0,0,0).f(L.j));p.b.b(L.h).h(60).f(L.j);y.add(p);}perspective(1,1.*width/height,30,3e4);camera(L.a.x,L.a.y,L.a.z,L.a.x+W.x,L.a.y+W.y,L.a.z+W.z,X.x,X.y,X.z);int l=0;for(i=0;i<u;i++)n[i].d(0);noStroke();Q(0,255,180);for(i=0;i<y.size();i++){P p=y.get(i);p.a.c(p.b);int q=0;if(Z.b(L.a).d(-1).c(p.a).l(W)<1){s();K(p.a.x,p.a.y,p.a.z);box(5,5,5);r();}if(p.a.m(L.a)>5e3)q=1;if(p.k>0&&p.a.m(L.a)<30){L.p-=.05;L.q=0;q=1;}for(j=0;j<N;j++){O o=x[j];if(p.a.m(o.a)<o.c){q=1;}}for(j=0;j<5;j++){B e=z[j];if(p.k<1&&p.a.m(e.a)<R){e.d--;e.e=10;q=1;}}if(q>0)y.remove(i--);}Q(j=200,j,j);s();K(L.a.x,L.a.y,L.a.z);for(i=0;i<M;i++){V o=f[i];Z.b(o).d(-1).c(L.a).d(-1).a((Z.x%j+j)%j-t,(Z.y%j+j)%j-t,(Z.z%j+j)%j-t,0);if(Z.l(W)<1){s();K(Z.x,Z.y,Z.z);box(.3,.3,.3);r();}}r();ambientLight(50,50,50);directionalLight(j=255,j,j,.5,-.5,.5);s();K(L.a.x,L.a.y-3e4,L.a.z);Q(0,200,j);sphere(24e3);K(0,5e4,0);Q(t,50,0);sphere(8e3);r();Q(t,t,t);for(i=0;i<N;i++){O o=x[i];Z.b(L.a).d(-1).c(o.a);if(Z.i()<v&&l<u)n[l++].b(Z).w=-1;if(Z.i()<o.c*.9)die();if(Z.l(W)<1){s();K(o.a.x,o.a.y,o.a.z);rotateX(o.c*1e4);rotateY(o.c*1e5);box(o.c,o.c,o.c);r();}}for(i=0;i<5;i++){B e=z[i];Z.b(L.a).d(-1).c(e.a);if(Z.i()<v&&l<u)n[l++].b(Z).w=1;if(Z.i()<R)e.d=0;e.c=Z.i()<1500?1:0;if(e.c>0){if(int(e.o*t+h)%30==0){P p=new P();p.a.b(e.a);p.b.b(Z).h(-50);p.k=1;y.add(p);}}e.e=max(0,e.e-1);if(Z.l(W)<1){s();K(e.a.x,e.a.y,e.a.z);Q(255,e.e*10,0);box(R,R,R);r();}if(e.d<=0){for(int j=0;j<50;j++){P p=new P();p.a.b(e.a);p.b.n(10);y.add(p);}z[i]=new B();}Z.h(e.c*3-3);U.b(e.g).j(Z).h(1+e.c*10);e.a.c(U.c(Z));}noLights();Q(j=50,50,50);camera(0,0,-j,0,0,0,0,1,0);K(L.f.x*j,L.f.y*j,-L.f.z*j);s();rotateX(PI/2);K(0,0,35);ellipse(0,0,t,j);K(0,0,-70);ellipse(0,0,t,j);r();s();K(0,20,0);rotateX(PI/2);Q(t,t,255);box(.5,.5,.5);L.j.w*=-1;for(i=0;i<u;i++){V p=n[i].d(.005);float w=p.w;p.w=0;if(p.f(L.j).i()>0){s();Q(j=255,j,j);if(w>0){Q(255,0,0);}K(p.x,-p.z,-p.y);box(.2,.2,.2);K(0,0,p.y/2);box(.05,.05,p.y);r();}}L.j.w*=-1;float d=1-max(L.m-3,0)/4;j=200;fill(j*d,j*d,j*d,50);ellipse(0,0,20,20);K(0,0,.1);float b=(h%t);b/=t;j=255;fill(t*d,t*d,j*d,t*(1-b));ellipse(0,0,20*b,20*b);r();s();K(0,-t*.45,t/2);fill(j*d,j*d,j*d,50);rect(t/4,0,-t/2,2);K(0,0,-.01);Q(t,t,j);rect(t/4,0,-t*L.p/2,2);r();}void keyPressed(){T(1);}void keyReleased(){T(0);}void T(int e){j=key;if(j=='a')A=e;if(j=='d')C=e;if(j==' ')D=e;if(j=='c')E=e;if(j=='w')F=e;if(j=='s')G=e;if(j=='x')H=e;if(j=='e')I=e;if(j=='q')J=e;}