var pi=Math.PI;
var scx=512,scy=384;
var oBase,oTimer,oBrd1,oBrd2,oGra,gctx;
var clBg='#000000';
var snd=new Array(),sndo=new Array(),sndwt=new Array();

function bgcolor(co){clBg=co;
 if(oBase)oBase.image(Stage.canvas(function(ctx){
 this.size(scx,scy);
 ctx.fillStyle=clBg;
 ctx.fillRect(0,0,scx,scy);
}));else render.setClearColor(co);}
function box(x,y,w,h,c){gctx.fillStyle=c;gctx.fillRect(x,y,w,h);}
function rectangle(x,y,w,h,c){gctx.strokeStyle=c;gctx.strokeRect(x,y,w,h);}
function line(x1,y1,x2,y2,c){
 gctx.strokeStyle=c;
 gctx.lineWidth=1;
 gctx.beginPath();
 gctx.moveTo(x1,y1);
 gctx.lineTo(x2,y2);
 gctx.closePath();
 gctx.stroke();}
function circle(x,y,r,c,f){
 gctx.strokeStyle=c;
 gctx.lineWidth=2;
 gctx.beginPath();
 gctx.arc(x,y,r,0,Math.PI*2,false);
 if(f){gctx.fillStyle=c;gctx.fill();}else gctx.stroke();}
function hex(v){return ('0'+v.toString(16)).substr(-2);}
function rgb(r,g,b){return '#'+hex(r)+hex(g)+hex(b);}
function rand(a){return Math.floor(Math.random()*a);}
function rnd(){return Math.random();}
function chr(a){return String.fromCharCode(a);}
function asc(a){return a.charCodeAt(0);}
function mid(a,s,l){return a.substr(s,l);}
function len(a){return a.length;}
function abs(a){return Math.abs(a);}
function atan2(a,b){return Math.atan2(a,b);}
function cos(a){return Math.cos(a);}
function int(a){return Math.floor(a);}
function sin(a){return Math.sin(a);}
function tan(a){return Math.tan(a);}
function sqr(a){return Math.sqrt(a);}
function upper(a){return a.toUpperCase();}
function lower(a){return a.toLowerCase();}
function time(){var d=new Date();return d.getTime();}
function sndload(a,f,p){
 f="audio/"+f+".ogg";
 snd[a]=(p)?p-4:-1;sndwt.push(a);
 createjs.Sound.alternateExtensions=["mp3"];
 createjs.Sound.on("fileload",sndloaded);
 createjs.Sound.registerSound({src:f, id:a});
}
function sndloaded(e){
 switch(snd[e.id]){
  case -3:sndplay(e.id);break;
  case -2:sndloop(e.id);break;
  default:snd[e.id]=0;}}
function sndwa(){
 for(i=0;i<sndwt.length;i++){if(snd[sndwt[i]]==-1)return 0;}
 return 1;}
function sndplay(a){snd[a]=1;sndo[a]=createjs.Sound.play(a);}
function sndloop(a){snd[a]=1;sndo[a]=createjs.Sound.play(a,{loop:-1});}
function sndstatus(a){return snd[a];}
function sndstop(a){if(snd[a]==1){snd[a]=0;sndo[a].stop();}}

//3d
function makeim(n){
 var t=new THREE.TextureLoader().load(sp_fi[n]);
 t.repeat.x=(sp_w[n]-sp_z[n]*2)/sp_ww[n];
 t.repeat.y=(sp_h[n]-sp_z[n]*2)/sp_h[n];
 t.offset.x=sp_z[n]/sp_ww[n];
 t.offset.y=sp_z[n]/sp_h[n];
 imt[imn]=t;imsp[imn]=n;
 imm[imn]=new THREE.MeshBasicMaterial({map:t,transparent:true});
 ++imn;return imn-1;}

function makeCube(a){
 var c=new THREE.Mesh(geobox,a);scene.add(c);return c;}

function makePlane(a){
 var c=new THREE.Mesh(geoplane,a);scene.add(c);return c;}

function _setm(x,y,z,c,t1,n){
 var i=0;while(i<mm&&m[i])++i;if(i==mm)++mm;
 m[i]=t1;mx[i]=x;my[i]=y;mz[i]=z;mxx[i]=0;myy[i]=0;mzz[i]=0;mrx[i]=0;mry[i]=0;mrz[i]=0;ms[i]=1;mn[i]=(n)?n:0;mh[i]=0;ml[i]=0;ma[i]=255;msx[i]=x;msy[i]=y;msz[i]=z;
 mi[i]=makeim(c);return i;}

function setm(x,y,z,c,t1,n){
 var i=_setm(x,y,z,c,t1,n);
 mo[i]=makePlane(imm[mi[i]]);
 return i;}

function setmCube(x,y,z,c,t1,n, w,h,d){
 var i=_setm(x,y,z,c,t1,n);
 if(w){
  var geo=new THREE.BoxGeometry(w,h,d);
  mo[i]=new THREE.Mesh(geo,imm[mi[i]]);
  scene.add(mo[i]);}
 else mo[i]=makeCube(imm[mi[i]]);
 return i;}

function setmSphere(x,y,z,c,t1,n, r,w,h){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.SphereGeometry(r,w,h);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setmPlane(x,y,z,c,t1,n, w,h){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.PlaneGeometry(w,h);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setmCone(x,y,z,c,t1,n, r,h){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.ConeGeometry(r,h);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setmCylinder(x,y,z,c,t1,n, r1,r2,h){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.CylinderGeometry(r1,r2,h);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setm12s(x,y,z,c,t1,n, r){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.DodecahedronGeometry(r);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setm8s(x,y,z,c,t1,n, r){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.OctahedronGeometry(r);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setm4s(x,y,z,c,t1,n, r){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.TetrahedronGeometry(r);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setmTorusKnot(x,y,z,c,t1,n, r,t,sr,st,p,q){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.TorusKnotGeometry(r,t,sr,st,p,q);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setmTorus(x,y,z,c,t1,n, r,t,sr,st){
 var i=_setm(x,y,z,c,t1,n);var geo=new THREE.TorusGeometry(r,t,sr,st);
 mo[i]=new THREE.Mesh(geo,imm[mi[i]]);scene.add(mo[i]);return i;}

function setcam(x,y,z,rx,ry,rz){
 cax=x;cay=y;caz=z;carx=rx;cary=ry;carz=rz;}

function maprepeat(c,x,y){
 imt[mi[c]].repeat.set(x,y);
 imt[mi[c]].wrapS=THREE.RepeatWrapping;
 imt[mi[c]].wrapT=THREE.RepeatWrapping;}

function delm(i){
 scene.remove(mo[i]);m[i]=0;}

function chkm(a,ii,s){var i;
 for(i=0;i<mm;i++){
  if(i!=ii&&m[i]==a&&Math.abs(mx[i]-sx)<s&&Math.abs(my[i]-sy)<s&&Math.abs(mz[i]-sz)<s){
   rsi=i;rsx=mx[i];rsy=my[i];rsz=mz[i];return 1;}}return 0;}
