jocly
Version:
Board games 3D/2D UI and AI web library
2 lines • 32 kB
JavaScript
exports.view=View={Game:{},Board:{},Move:{}},function(){var e,a,t;View.Game.cbTargetMesh="/res/ring-target.js",View.Game.cbTargetSelectColor=16777215,View.Game.cbTargetCancelColor=16746496,View.Game.cbPromoSize=2e3,View.Game.xdInit=function(t){this.g.fullPath=this.mViewOptions.fullPath,this.cbPieceByType={},e=this.cbVar,a=this.cbDefineView(),this.cbView=a,this.cbClearPieces(),this.cbCreateLights(t),this.cbCreateScreens(t),this.cbCreateBoard(t),this.cbCreatePromo(t),this.cbCreatePieces(t),this.cbCreateCells(t)},View.Game.cbMakeDummyMesh=function(e){return"undefined"!=typeof THREE?new THREE.Mesh(new THREE.CubeGeometry(1e-4,1e-4,1e-4),new THREE.MeshLambertMaterial):null},View.Game.cbCurrentGame=function(){return t},View.Game.cbCreatePieces=function(e){for(var a=this.cbMakeDummyMesh(e),t=0;t<this.cbPiecesCount;t++)e.createGadget("piece#"+t,{base:{},"2d":{type:"sprite"},"3d":{type:"custommesh3d",create:function(e,t,i){return a}}})},View.Game.cbCreateBoard=function(e){var a=this.cbMakeDummyMesh(e);e.createGadget("board",{base:{},"2d":{type:"canvas",width:12e3,height:12e3,draw:function(e){console.warn("board draw must be overridden")}},"3d":{type:"custommesh3d",receiveShadow:!0,create:function(e,t,i){return a}}})},View.Game.cbCreateCells=function(e){for(var a=this,t=0;t<this.g.boardSize;t++)!function(t){e.createGadget("cell#"+t,{"2d":{z:101,type:"element",initialClasses:a.cbCellClass(e,t),width:1300,height:1300}}),e.createGadget("clicker#"+t,$.extend(!0,{"2d":{z:103,type:"element",initialClasses:"cb-clicker"},"3d":{type:"meshfile",file:a.g.fullPath+a.cbTargetMesh,flatShading:!0,castShadow:!0,smooth:0,scale:[.9,.9,.9],materials:{square:{transparent:!0,opacity:0},ring:{color:a.cbTargetSelectColor,opacity:1}}}},a.cbView.clicker))}(t)},View.Game.cbCreatePromo=function(e){e.createGadget("promo-board",{base:{type:"element",x:0,y:0,width:2e3,height:2e3,z:108,css:{"background-color":"White"}}}),e.createGadget("promo-cancel",{base:{type:"image",file:this.g.fullPath+"/res/images/cancel.png",x:0,y:0,width:800,height:800,z:109}});for(var a=0;a<this.g.pTypes.length;a++)e.createGadget("promo#"+a,{base:{y:0,z:109,type:"sprite",clipwidth:100,clipheight:100,width:1200,height:1200}})},View.Game.xdBuildScene=function(i){t=this,e=this.cbVar,a=this.cbDefineView(),this.cbView=a;for(var n=0;n<this.cbExtraLights.length;n++)i.updateGadget("extralights#"+n,{"3d":{visible:!0}});i.updateGadget("board",$.extend({base:{visible:!0}},this.cbView.board));for(var r=0;r<this.g.boardSize;r++)!function(e){var a=t.cbMakeDisplaySpec(e,0),n=$.extend(!0,{},a,{base:{visible:!0}},t.cbView.clicker,t.cbView.cell);i.updateGadget("cell#"+e,n),$.extend(!0,a,t.cbView.clicker),i.updateGadget("clicker#"+e,a)}(r);i.updateGadget("videoa",{"3d":{visible:!0,playerSide:1,z:3e3,y:1==this.mViewAs?1e4:-1e4,rotate:1==this.mViewAs?-180:-0,rotateX:1==this.mViewAs?25:-25,scale:[3,3,3]}}),i.updateGadget("videoabis",{"3d":{visible:!0,playerSide:-1,z:1500,x:1==this.mViewAs?-5500:5500,y:1==this.mViewAs?8900:-8900,rotate:1==this.mViewAs?-180:-0,rotateX:1==this.mViewAs?25:-25,scale:[.75,.75,.75]}}),i.updateGadget("videob",{"3d":{visible:!0,playerSide:-1,z:3e3,y:1==this.mViewAs?-1e4:1e4,rotate:1==this.mViewAs?-0:-180,rotateX:1==this.mViewAs?-25:25,scale:[3,3,3]}}),i.updateGadget("videobbis",{"3d":{visible:!0,playerSide:1,z:1500,x:1==this.mViewAs?5500:-5500,y:1==this.mViewAs?-8900:8900,rotate:1==this.mViewAs?-0:-180,rotateX:1==this.mViewAs?-25:25,scale:[.75,.75,.75]}}),i.updateGadget("promo-board",{base:{visible:!1}}),i.updateGadget("promo-cancel",{base:{visible:!1}});for(var n=0;n<this.g.pTypes.length;n++)i.updateGadget("promo#"+n,{base:{visible:!1}})},View.Game.cbDisplayBoardFn=function(e){var a=this;return function(i,n,r){var s=e.style+"_"+e.margins.x+"_"+e.margins.y+"_"+a.mNotation+"_"+a.mViewAs,o=this;s!=this._cbKey&&(this._cbKey=s,e.display.call(t,e,o,function(e){o.replaceMesh(e,n,r)}))}},View.Game.cbDrawBoardFn=function(e){return function(a){e.draw.call(t,e,this,a)}},View.Game.cbMakeDisplaySpec=function(e,a){var t={};for(var i in this.cbView.coords){var n=this.cbView.coords[i],r=n.call(this,e);t[i]={x:r.x||0,y:r.y||0,z:r.z||0,rotateX:r.rx||0,rotateY:(r.ry||0)*("3d"==i?this.mViewAs*a<0?-1:1:0),rotate:(r.rz||0)+("3d"==i&&this.mViewAs*a<0?180:0)}}return t},View.Game.cbMakeDisplaySpecForPiece=function(t,i,n){function r(e,a,t){return a?$.extend(!0,e,a.default,a[t]):{}}var s=this.cbMakeDisplaySpec(i,n.s);if(void 0===e.pieceTypes[n.t])return void console.warn("Piece type",n.t,"not defined in model");var o=e.pieceTypes[n.t].aspect||e.pieceTypes[n.t].name;return o?(a.pieces&&(s=r(s,a.pieces,o),a.pieces[n.s]&&(s=r(s,a.pieces[n.s],o))),s):void console.warn("Piece type",n.t,"has no aspect defined")},View.Board.xdDisplay=function(e,a){for(var t=0;t<this.pieces.length;t++){var i=this.pieces[t];if(i.p<0)e.updateGadget("piece#"+t,{base:{visible:!1}});else{var n=a.cbMakeDisplaySpecForPiece(a,i.p,i);n=$.extend(!0,{base:{visible:!0},"2d":{opacity:1},"3d":{positionEasingUpdate:null}},n),e.updateGadget("piece#"+t,n)}}for(;t<a.cbPiecesCount;t++)e.updateGadget("piece#"+t,{base:{visible:!1}})},View.Game.cbExtraLights=[{color:16777215,intensity:.8,position:[9,14,-9],props:{shadowCameraNear:10,shadowCameraFar:25,castShadow:!0,shadowMapWidth:2048,shadowMapHeight:2048}}],View.Game.cbCreateLights=function(e){for(var a=0;a<this.cbExtraLights.length;a++)!function(a,t){e.createGadget("extralights#"+t,{"3d":{type:"custommesh3d",create:function(e){var t=new THREE.SpotLight(a.color,a.intensity);t.shadow.camera.far=a.props.shadowCameraFar,t.shadow.camera.near=a.props.shadowCameraNear,t.shadow.mapSize.width=a.props.shadowMapWidth,t.shadow.mapSize.height=a.props.shadowMapHeight,t.position.set.apply(t.position,a.position);var i=new THREE.Mesh;i.add(t);var n=new THREE.Object3D;i.add(n),t.target=n,e(i)}}})}(this.cbExtraLights[a],a)},View.Game.cbCreateScreen=function(e){var a=new THREE.PlaneGeometry(4,3,1,1),t=new THREE.MeshPhongMaterial({color:16777215,map:e,shading:THREE.FlatShading,emissive:{r:1,g:1,b:1}}),i=new THREE.Mesh(a,t);return this.objectReady(i),null},View.Game.cbCreateScreens=function(e){var a=this;e.createGadget("videoa",{"3d":{type:"video3d",makeMesh:function(e){return a.cbCreateScreen.call(this,e)}}}),e.createGadget("videoabis",{"3d":{type:"video3d",makeMesh:function(e){return a.cbCreateScreen.call(this,e)}}}),e.createGadget("videob",{"3d":{type:"video3d",makeMesh:function(e){return a.cbCreateScreen.call(this,e)}}}),e.createGadget("videobbis",{"3d":{type:"video3d",makeMesh:function(e){return a.cbCreateScreen.call(this,e)}}})},View.Board.xdInput=function(a,t){function i(){a.updateGadget("promo-board",{base:{visible:!1}}),a.updateGadget("promo-cancel",{base:{visible:!1}})}return{initial:{f:null,t:null,pr:null},getActions:function(n,r){var s={};if(null==r.f)n.forEach(function(e){void 0===s[e.f]&&(s[e.f]={f:e.f,moves:[],click:["piece#"+this.board[e.f],"clicker#"+e.f],view:["clicker#"+e.f],highlight:function(i){a.updateGadget("cell#"+e.f,{"2d":{classes:"select"==i?"cb-cell-select":"cb-cell-cancel",opacity:t.mShowMoves||"cancel"==i?1:0}}),a.updateGadget("clicker#"+e.f,{"3d":{materials:{ring:{color:"select"==i?t.cbTargetSelectColor:t.cbTargetCancelColor,opacity:t.mShowMoves||"cancel"==i?1:0,transparent:!t.mShowMoves&&"cancel"!=i}},castShadow:t.mShowMoves||"cancel"==i}})},unhighlight:function(){a.updateGadget("cell#"+e.f,{"2d":{classes:""}})},validate:{f:e.f}}),s[e.f].moves.push(e)},this);else if(null==r.t)n.forEach(function(n){var r=void 0===n.cg?n.t:n.cg;void 0===s[r]&&(s[r]={t:n.t,moves:[],click:["piece#"+this.board[r],"clicker#"+r],view:["clicker#"+r],highlight:function(e){a.updateGadget("cell#"+r,{"2d":{classes:"select"==e?"cb-cell-select":"cb-cell-cancel",opacity:t.mShowMoves||"cancel"==e?1:0}}),a.updateGadget("clicker#"+r,{"3d":{materials:{ring:{color:"select"==e?t.cbTargetSelectColor:t.cbTargetCancelColor,opacity:t.mShowMoves||"cancel"==e?1:0,transparent:!t.mShowMoves&&"cancel"!=e}},castShadow:t.mShowMoves||"cancel"==e}})},unhighlight:function(e){a.updateGadget("cell#"+r,{"2d":{classes:""}})},validate:{t:n.t},execute:function(i){var r=this;this.cbAnimate(a,t,n,function(){var o=s[n.t].moves;o.length>1&&(a.updateGadget("promo-board",{base:{visible:!0,width:t.cbPromoSize*(o.length+1)}}),a.updateGadget("promo-cancel",{base:{visible:!0,x:o.length*t.cbPromoSize/2}}),o.forEach(function(i,n){var r=e.pieceTypes[i.pr].aspect||e.pieceTypes[i.pr].name,s=$.extend(!0,{},t.cbView.pieces.default,t.cbView.pieces[r]);t.cbView.pieces[this.mWho]&&(s=$.extend(!0,s,t.cbView.pieces[this.mWho].default,t.cbView.pieces[this.mWho][r])),a.updateGadget("promo#"+i.pr,{base:$.extend(s["2d"],{x:(n-o.length/2)*t.cbPromoSize})})},r)),i()})},unexecute:function(){if(null!=n.c){var e=this.pieces[n.c],r=t.cbMakeDisplaySpecForPiece(t,e.p,e);r=$.extend(!0,{base:{visible:!0},"2d":{opacity:1},"3d":{positionEasingUpdate:null}},r),a.updateGadget("piece#"+n.c,r)}var s=this.pieces[this.board[n.f]],o=t.cbMakeDisplaySpecForPiece(t,s.p,s);a.updateGadget("piece#"+s.i,o),i()}}),void 0!==n.cg&&(s[r].validate.cg=n.cg,s[r].execute=function(e){this.cbAnimate(a,t,n,function(){e()})}),s[r].moves.push(n)},this);else if(null==r.pr){var o=[];n.forEach(function(e){void 0!==e.pr&&(void 0===s[e.pr]&&(s[e.pr]={pr:e.pr,moves:[],click:["promo#"+e.pr],validate:{pr:e.pr},cancel:["promo-cancel"],post:i,skipable:!0},o.push(e.pr)),s[e.pr].moves.push(e))},this),o.length>1&&o.forEach(function(e){s[e].view=["promo#"+e]})}return s}}},View.Game.cbCellClass=function(e,a){return"classic-cell "+((a+(a-a%this.g.NBCOLS)/this.g.ROWS)%2?"classic-cell-black":"classic-cell-white")},View.Board.xdPlayedMove=function(e,a,t){a.mOldBoard.cbAnimate(e,a,t,function(){a.MoveShown()})},View.Board.cbAnimate=function(e,a,t,i){function n(){0==--s&&(o&&a.PlaySound("tac"+(1+Math.floor(3*Math.random()))),i())}var r=this,s=1,o=!1,c=this.pieces[this.board[t.f]],l=a.cbMakeDisplaySpec(t.f,c.s),h=a.cbMakeDisplaySpecForPiece(a,t.t,c);for(var d in l){var p=l[d];void 0!==p.z&&function(e){var i=p.z,n=h[e].z,s=r.cbMoveMidZ(a,t,i,n,e),c=i,l=c-s,d=c-n;s!=(i+n)/2&&(o=!0);var m=4*l-2*d,u=-d*d,f=Math.abs(m*m- -4*u),g=(-m-Math.sqrt(f))/-2,b=(-m+Math.sqrt(f))/-2,w=g,v=-w-d;(0==w||-v/(2*w)<0||-v/(2*w)>1)&&(w=b,v=-w-d),h[e].positionEasingUpdate=function(e){var a=(w*e*e+v*e+c)*this.SCALE3D;this.object3d.position.y=a}}(d)}if(o||a.PlaySound("move"+(1+Math.floor(4*Math.random()))),e.updateGadget("piece#"+c.i,h,600,function(){n()}),null!=t.c){s++;var m={positionEasingUpdate:null};switch(a.cbView.captureAnim3d||"movedown"){case"movedown":m.z=-2e3;break;case"scaledown":m.scale=[0,0,0]}var u=this.pieces[t.c];e.updateGadget("piece#"+u.i,{"2d":{opacity:0},"3d":m},600,n)}if(void 0!==t.cg){var p=a.cbVar.castle[t.f+"/"+t.cg],f=p.r[p.r.length-1],c=this.pieces[this.board[t.cg]],h=a.cbMakeDisplaySpecForPiece(a,f,c);s++,e.updateGadget("piece#"+c.i,h,600,function(){n()})}},View.Board.cbMoveMidZ=function(e,a,t,i){return(t+i)/2}}(),function(){View.Game.cbBaseBoard={TEXTURE_CANVAS_CX:1024,TEXTURE_CANVAS_CY:1024,display:function(e,a,t){var i=this;e.getResource=a.getResource,e.createGeometry.call(this,e,function(a){e.createTextureImages.call(i,e,function(n){var r=["diffuse"].concat(e.extraChannels||[]),s={};r.forEach(function(a){var t=document.createElement("canvas");t.width=e.TEXTURE_CANVAS_CX,t.height=e.TEXTURE_CANVAS_CY,s[a]=t}),e.createMaterial.call(i,e,s,function(r){var o=new THREE.Mesh(a,r);e.modifyMesh.call(i,e,o,function(a){e.paint.call(i,e,s,n,function(){t(a)})})})})})},createTextureImages:function(e,a){var t=this,i={},n=0,r=e.texturesImg||{};for(var s in r)n++;if(0==n)a(i);else for(var s in r)!function(s){e.getResource("image|"+t.g.fullPath+r[s],function(e){i[s]=e,0==--n&&a(i)})}(s)},createMaterial:function(e,a,t){var i=new THREE.Texture(a.diffuse);i.needsUpdate=!0;var n={specular:"#050505",shininess:30,map:i};if(a.bump){var r=new THREE.Texture(a.bump);r.needsUpdate=!0,n.bumpMap=r,n.bumpScale=.05}t(new THREE.MeshPhongMaterial(n))},modifyMesh:function(e,a,t){t(a)},prePaint:function(e,a,t,i,n){n()},paint:function(e,a,t,i,n){n()},postPaint:function(e,a,t,i,n){n()},paintChannel:function(e,a,t,i){},draw:function(e,a,t){var i=this;e.getResource=a.getResource,e.createTextureImages.call(this,e,function(a){e.paintChannel.call(i,e,t,a,"diffuse")})}}}(),function(){function e(e){for(var a=JSON.stringify(e),t=0,i=0;i<a.length;i++)t=(t<<5)-t+a.charCodeAt(i),t&=t;return t}var a,t={};View.Game.cbDisplayPieceFn=function(t){var i=this,n=e(t);return function(e,r,s){a=this.getResource;var o=/^piece#([0-9]+)$/.exec(this.gadget.id);if(!o)return null;var c=parseInt(o[1]),l=i.cbCurrentGame();if(c>=l.mBoard.pieces.length)return null;var h=l.mBoard.pieces[c],d=l.cbVar.pieceTypes[h.t].aspect||l.cbVar.pieceTypes[h.t].name,p=d+"_"+n+"_"+h.s,m=this;p!=this._cbKey&&(this._cbKey=p,m.options=r,l.cbMakePiece(t,d,h.s,function(e){m.replaceMesh(e,m.options,s)}))}},View.Game.cbMakePiece=function(a,i,n,r){function s(e,a,t){return a?$.extend(!0,e,a.default,a[t]):{}}if(!a)return void console.error("piece-view: style is not defined");var o=s({},a,i);a[n]&&(o=s(o,a[n],i));var c=e(o),l=t[c];Array.isArray(l)?l.push(r):l?r(new THREE.Mesh(l.geometry,l.material)):(t[c]=[r],o.loadResources.call(this,o,function(e){o.displayPiece.call(this,o,e,function(){var a=t[c];t[c]={geometry:e.geometry,material:e.material},a.forEach(function(a){a(new THREE.Mesh(e.geometry,e.material))})})}))},View.Game.cbClearPieces=function(){t={}},View.Game.cbBasePieceStyle={default:{mesh:{jsFile:function(e,a){a(new THREE.CubeGeometry(1,1,1),new THREE.MeshPhongMaterial({}))},smooth:0,rotateZ:0},loadMesh:function(e,t){"function"==typeof e.mesh.jsFile?e.mesh.jsFile(e,t):a("smoothedfilegeo|"+e.mesh.smooth+"|"+this.g.fullPath+e.mesh.jsFile,t)},loadImages:function(e,t){function i(){0==--r&&t(s)}var n=this,r=1,s={};for(var o in e.materials){var c=e.materials[o].channels;for(var l in c)if(c[l].texturesImg)for(var h in c[l].texturesImg)!function(e,t){r++,a("image|"+n.g.fullPath+t,function(a){s[e]=a,i()})}(h,c[l].texturesImg[h])}i()},loadResources:function(e,a){function t(){0==--s&&a({geometry:n,images:i,textures:{},loadedMaterials:r})}var i,n,r,s=2;e.loadMesh.call(this,e,function(a,i){if(!a._cbZRotated){var s=new THREE.Matrix4;s.makeRotationY(e.mesh.rotateZ*Math.PI/180),a.applyMatrix(s),a._cbZRotated=!0}n=a,r=i,t()}),e.loadImages.call(this,e,function(e){i=e,t()})},displayPiece:function(e,a,t){e.makeMaterials.call(this,e,a),t()},paintTextureImageClip:function(e,a,t,i,n,r,s,o,c){var l=a.canvas.width,h=a.canvas.height;if(n.patternFill&&n.patternFill[r]){var d=n.patternFill[r];a.save();var p=document.createElement("canvas");p.width=l,p.height=h,ctxTmp=p.getContext("2d"),ctxTmp.fillStyle=d,ctxTmp.fillRect(0,0,l,h),ctxTmp.globalCompositeOperation="destination-in",ctxTmp.drawImage(s,o.x,o.y,o.cx,o.cy,0,0,l,h),a.drawImage(p,0,0,l,h,0,0,l,h),a.restore()}else a.drawImage(s,o.x,o.y,o.cx,o.cy,0,0,l,h)},paintTextureImage:function(e,a,t,i,n,r,s,o){var c;c=n.clipping&&n.clipping[r]?n.clipping[r]:{x:0,y:0,cx:s.width,cy:s.height},e.paintTextureImageClip.call(this,e,a,t,i,n,r,s,c,o)},paintTexture:function(e,a,t,i,n){var r=e.materials[t].channels[i];for(var s in r.texturesImg){var o=n.images[s];e.paintTextureImage.call(this,e,a,t,i,r,s,o,n)}},makeMaterialTextures:function(e,a,t){for(var i in e.materials[a].channels){var n=e.materials[a].channels[i],r=document.createElement("canvas");r.width=n.size.cx,r.height=n.size.cy;var s=r.getContext("2d");e.paintTexture.call(this,e,s,a,i,t);var o=new THREE.Texture(r);o.needsUpdate=!0,t.textures[a][i]=o}},makeMaterials:function(e,a){a.textures={};for(var t in e.materials)a.textures[t]={},e.makeMaterialTextures.call(this,e,t,a),e.makeMaterial.call(this,e,t,a)}}},View.Game.cbTokenPieceStyle3D=$.extend(!0,{},View.Game.cbBasePieceStyle,{default:{makeMaterials:function(e,a){a.textures={};for(var t in e.materials)a.textures[t]={},e.makeMaterialTextures.call(this,e,t,a);var i=[];for(var n in a.loadedMaterials){var r=a.loadedMaterials[n].clone(),s=r.name;if(e.materials[s]){$.extend(!0,r,e.materials[s].params);for(var o in e.materials[s].channels)switch(o){case"diffuse":r.map=a.textures[s][o];break;case"bump":r.bumpMap=a.textures[s][o]}}i.push(r)}var c=new THREE.MultiMaterial(i);a.material=c}}}),View.Game.cbUniformPieceStyle3D=$.extend(!0,{},View.Game.cbBasePieceStyle,{default:{makeMaterial:function(e,a,t){var i=e.materials[a].params;i.map=t.textures[a].diffuse,i.normalMap=t.textures[a].normal;var n=e.materials[a].channels.normal.normalScale||1;i.normalScale=new THREE.Vector2(n,n);var r=new THREE.MeshPhongMaterial(i);t.material=r,t.geometry.mergeVertices(),t.geometry.computeVertexNormals()}}}),View.Game.cbPhongPieceStyle3D=$.extend(!0,{},View.Game.cbBasePieceStyle,{default:{phongProperties:{color:"#ffffff",shininess:300,specular:"#ffffff",emissive:"#222222",shading:"undefined"!=typeof THREE?THREE.FlatShading:0},makeMaterials:function(e,a){var t=new THREE.MeshPhongMaterial(e.phongProperties);a.material=t}}})}(),function(){var e=0,a=0,t={};View.Game.cbEnsureConstants=function(){a||(a=this.cbVar.geometry.height,e=this.cbVar.geometry.width)},View.Game.cbCSize=function(i){this.cbEnsureConstants();var n=t[i.margins.x+"_"+i.margins.y];if(!n){var r,s,o,c,l=e+2*i.margins.x,h=a+2*i.margins.y;r=l/h,c=r<1?12e3*r/l:12e3/r/h,s=(e+2*i.margins.x)*c,o=(a+2*i.margins.y)*c,n={cx:c,cy:c,pieceCx:c,pieceCy:c,ratio:r,width:s,height:o},t[i.margins.x+"_"+i.margins.y]=n}return n},View.Game.cbGridBoard=$.extend({},View.Game.cbBaseBoard,{notationMode:"out",coordsFn:function(t){return t=t||{},t.margins=t.margins||{x:0,y:0},function(i){var n=this.cbCSize(t),r=i%e,s=(i-r)/e;return 1==this.mViewAs&&(s=a-1-s),-1==this.mViewAs&&(r=e-1-r),{x:(r-(e-1)/2)*n.cx,y:(s-(a-1)/2)*n.cy,z:0}}},createGeometry:function(e,a){var t=this.cbCSize(e),i=t.width/1e3,n=t.height/1e3,r=new THREE.PlaneGeometry(i,n),s=new THREE.Matrix4;s.makeRotationX(-Math.PI/2),r.applyMatrix(s);for(var o=r.faceVertexUvs[0],c=0;c<o.length;c++)for(var l=0;l<o[c].length;l++)t.ratio<1&&(o[c][l].x=o[c][l].x*t.ratio+(1-t.ratio)/2),t.ratio>1&&(o[c][l].y=o[c][l].y/t.ratio+(1-1/t.ratio)/2);a(r)},paintBackground:function(e,a,t,i,n,r){t.boardBG&&a.drawImage(t.boardBG,-n/2,-r/2,n,r)},paintChannel:function(e,a,t,i){var n=this.cbCSize(e);e.paintBackground.call(this,e,a,t,i,n.width,n.height)},paint:function(e,a,t,i){for(var n in a){var r=a[n].getContext("2d");r.save(),r.scale(e.TEXTURE_CANVAS_CX/12e3,e.TEXTURE_CANVAS_CY/12e3),r.translate(6e3,6e3),e.paintChannel.call(this,e,r,t,n),r.restore()}i()}}),View.Game.cbGridBoardClassic=$.extend({},View.Game.cbGridBoard,{colorFill:{".":"rgba(160,150,150,0.9)","#":"rgba(0,0,0,1)"," ":"rgba(0,0,0,0)"},texturesImg:{boardBG:"/res/images/wood.jpg"},modifyMesh:function(e,a,t){function i(e,a){var t=new THREE.Shape;return t.moveTo(-e/2,-a/2),t.lineTo(e/2,-a/2),t.lineTo(e/2,a/2),t.lineTo(-e/2,a/2),t}var n=this.cbCSize(e),r=n.width/1e3,s=n.height/1e3,o=i(r+.5+.1,s+.5+.1),c=i(r+.1,s+.1);o.holes.push(c);var l={amount:.4,steps:1,bevelSize:.1,bevelThickness:.04,bevelSegments:1},h=new THREE.ExtrudeGeometry(o,l),d=new THREE.Matrix4;d.makeRotationX(-Math.PI/2),h.applyMatrix(d),blackMat=new THREE.MeshPhongMaterial({color:"#000000",shininess:500,specular:"#888888",emissive:"#000000"});var p=new THREE.Mesh(h,blackMat);p.position.y=-l.amount-.01,a.add(p);var m=new THREE.Mesh(new THREE.BoxGeometry(r,s,.1),blackMat);m.rotation.x=Math.PI/2,m.position.y=-.1,a.add(m),t(a)},paintCell:function(e,a,t,i,n,r,s,o,c){a.strokeStyle="rgba(0,0,0,1)",a.lineWidth=15,a.fillStyle="bump"==i?"#ffffff":e.colorFill[n],a.fillRect(r-o/2,s-c/2,o,c),a.rect(r-o/2,s-c/2,o,c)},paintCells:function(t,i,n,r){for(var s=this.cbCSize(t),o=t.coordsFn(t),c=0;c<a;c++)for(var l=0;l<e;l++){var h=1==this.mViewAs?l+c*e:e*a-(1+l+c*e),d=o.call(this,h),p=this.cbView.boardLayout[a-c-1][l],m=d.x,u=d.y,f=s.cx,g=s.cy;t.paintCell.call(this,t,i,n,r,p,m,u,f,g)}},paintLines:function(t,i,n,r){var s=this.cbCSize(t);i.strokeStyle="#000000",i.lineWidth=40,i.strokeRect(-e*s.cx/2,-a*s.cy/2,e*s.cx,a*s.cy)},paintChannel:function(e,a,t,i){var n=this.cbCSize(e);e.paintBackground.call(this,e,a,t,i,n.width,n.height),e.paintCells.call(this,e,a,t,i),e.paintLines.call(this,e,a,t,i),this.mNotation&&e.paintNotation.call(this,e,a,i)},paintNotation:function(e,a,t){var i=this.cbCSize(e);switch(a.textAlign="center",a.textBaseline="middle",a.fillStyle="#000000",a.font=Math.ceil(i.cx/3)+"px Monospace",e.notationMode){case"out":e.paintOutNotation.apply(this,arguments);break;case"in":e.paintInNotation.apply(this,arguments)}},paintOutNotation:function(t,i,n){for(var r=this.cbCSize(t),s=0;s<a;s++){var o=a-s;this.mViewAs<0&&(o=s+1);var c=-(e/2+t.margins.x/2)*r.cx,l=(s-a/2+.5)*r.cy;i.fillText(o,c,l)}for(var h=0;h<e;h++){var d=h;this.mViewAs<0&&(d=e-h-1);var c=(h-e/2+.5)*r.cx,l=(a/2+t.margins.y/2)*r.cy;i.fillText(String.fromCharCode(97+d),c,l)}},paintInNotation:function(t,i,n){var r=this.cbCSize(t),s=t.coordsFn(t),o=t.colorFill;i.font=Math.ceil(r.cx/5)+"px Monospace";for(var c=0;c<a;c++)for(var l=0;l<e;l++){var h=a-c,d=l;this.mViewAs<0?d=e-l-1:h=c+1;var p=1==this.mViewAs?l+c*e:e*a-(1+l+c*e),m=s.call(this,p);switch(i.fillStyle="rgba(0,0,0,0)","bump"==n&&(i.fillStyle=o["."]),this.cbView.boardLayout[a-c-1][l]){case".":i.fillStyle="bump"==n?o["."]:o["#"];break;case"#":i.fillStyle=o["."]}var u=m.x-r.cx/3,f=m.y-r.cy/3;t.notationDebug?i.fillText(p,u,f):i.fillText(String.fromCharCode(97+d)+h,u,f)}}}),View.Board.cbMoveMidZ=function(e,a,t,i){var n=e.cbVar.geometry,r=n.C(a.f),s=n.C(a.t),o=n.R(a.f),c=n.R(a.t);return s-r==0||c-o==0||Math.abs(s-r)==Math.abs(c-o)?(t+i)/2:Math.max(t,i)+1500},View.Game.cbGridBoardClassic2D=$.extend({},View.Game.cbGridBoardClassic,{colorFill:{".":"#F1D9B3","#":"#C7885D"," ":"rgba(0,0,0,0)"}}),View.Game.cbGridBoardClassic3DMargin=$.extend({},View.Game.cbGridBoardClassic,{margins:{x:.67,y:.67},extraChannels:["bump"]}),View.Game.cbGridBoardClassic2DMargin=$.extend({},View.Game.cbGridBoardClassic2D,{margins:{x:.67,y:.67}}),View.Game.cbGridBoardClassic2DNoMargin=$.extend({},View.Game.cbGridBoardClassic2D,{margins:{x:0,y:0},notationMode:"in",texturesImg:{boardBG:"/res/images/whitebg.png"}})}(),function(){var e=0,a=0,t={};View.Game.cbTargetMesh="/res/ring-target-cylinder-v2.js",View.Game.cbTargetSelectColor=3377152,View.Game.cbTargetCancelColor=16746496,View.Game.cbCylinderEnsureConstants=function(){a||(a=this.cbVar.geometry.height,e=this.cbVar.geometry.width)};var i=View.Game.cbCSize;View.Game.cbCSize=function(n){if(this.cbCylinderEnsureConstants(),!n.cylinder)return i.apply(this,arguments);var r=t[n.margins.x+"_"+n.margins.y];if(!r){var s,o,c,l,h=e+2*n.margins.x,d=a+2*n.margins.y;s=h/d,l=s<1?12e3*s/h:12e3/s/d,o=(e+2*n.margins.x)*l,c=(a+2*n.margins.y)*l;var p=2*Math.PI/e,m=l/1e3,u=Math.cos(p),f=4*u*u+4*m*m,g=u+Math.sqrt(f)/2;r={cx:l,cy:l,pieceCx:l,pieceCy:l,ratio:s,width:o,height:c,radius:g,innerRadius:g*Math.cos(p/2),angle:p,cosAngle:Math.cos(p),sinAngle:Math.sin(p)},console.log("CSIZE",n.margins.x+"_"+n.margins.y,r),t[n.margins.x+"_"+n.margins.y]=r}return r},View.Game.cbCylinderBoard=$.extend({},View.Game.cbBaseBoard,{notationMode:"in",cylinder:!0,colShift:.5,coordsFn:function(t){return t=t||{},t.margins=t.margins||{x:0,y:0},function(i){var n,r,s=this.cbCSize(t),o=i%e,c=(i-o)/e;return 1==this.mViewAs?(c=a-1-c,n=(o-(e-1)/2)*s.cx,r=(c-(a-1)/2)*s.cy,o=e-1-o,o=(o+2)%e):(n=(e-o-1-(e-1)/2)*s.cx,r=(c-(a-1)/2)*s.cy,o=(o+2)%e),{x:1.02*s.innerRadius*Math.cos(s.angle*(o+t.colShift))*1e3,y:1.02*s.innerRadius*Math.sin(s.angle*(o+t.colShift))*1e3,z:s.cy*(a/2-.5-c),ry:s.angle*(o+t.colShift)*180/Math.PI-90,rx:90,rz:0,xb:n,yb:r}}},easingFn:function(e){return function(e){console.log("easing",e)}},createGeometry:function(t,i){for(var n=this.cbCSize(t),r=n.cx/1e3,s=new THREE.CylinderGeometry(n.radius,n.radius,r*a,e,r*a,!0),o=s.faceVertexUvs[0],c=0;c<o.length;c++)for(var l=0;l<o[c].length;l++)n.ratio<1&&(o[c][l].x=o[c][l].x*n.ratio+(1-n.ratio)/2),n.ratio>1&&(o[c][l].y=o[c][l].y/n.ratio+(1-1/n.ratio)/2);i(s)},paintChannel:function(e,a,t,i){this.cbCSize(e)},paint:function(e,a,t,i){for(var n in a){var r=a[n].getContext("2d");r.save(),r.scale(e.TEXTURE_CANVAS_CX/12e3,e.TEXTURE_CANVAS_CY/12e3),r.translate(6e3,6e3),e.paintChannel.call(this,e,r,t,n),r.restore()}i()}}),View.Game.cbCylinderBoardClassic=$.extend({},View.Game.cbCylinderBoard,{margins:{x:0,y:0},colorFill:{".":"rgba(255,255,255,1)","#":"rgba(0,0,0,1)"," ":"rgba(0,0,0,0)"},texturesImg:{boardBG:"/res/images/wood.jpg"},extraChannels:["bump"],paintCell:function(e,a,t,i,n,r,s,o,c){a.strokeStyle="rgba(0,0,0,1)",a.lineWidth=15,a.fillStyle="bump"==i?"#ffffff":e.colorFill[n],a.fillRect(r-o/2,s-c/2,o,c),a.rect(r-o/2,s-c/2,o,c)},paintCells:function(t,i,n,r){for(var s=this.cbCSize(t),o=t.coordsFn(t),c=0;c<a;c++)for(var l=0;l<e;l++){var h=1==this.mViewAs?l+c*e:e*a-(1+l+c*e),d=o.call(this,h),p=this.cbView.boardLayout[a-c-1][l],m=d.xb,u=d.yb,f=s.cx,g=s.cy;t.paintCell.call(this,t,i,n,r,p,m,u,f,g)}},paintChannel:function(e,a,t,i){this.cbCSize(e);e.paintCells.call(this,e,a,t,i),this.mNotation&&e.paintNotation.call(this,e,a,i)},paintNotation:function(e,a,t){var i=this.cbCSize(e);switch(a.textAlign="center",a.textBaseline="middle",a.fillStyle="#000000",a.font=Math.ceil(i.cx/3)+"px Monospace",e.notationMode){case"out":e.paintOutNotation.apply(this,arguments);break;case"in":e.paintInNotation.apply(this,arguments)}},paintOutNotation:function(t,i,n){for(var r=this.cbCSize(t),s=0;s<a;s++){var o=a-s;this.mViewAs<0&&(o=s+1);var c=-(e/2+t.margins.x/2)*r.cx,l=(s-a/2+.5)*r.cy;i.fillText(o,c,l)}for(var h=0;h<e;h++){var d=h;this.mViewAs<0&&(d=e-h-1);var c=(h-e/2+.5)*r.cx,l=(a/2+t.margins.y/2)*r.cy;i.fillText(String.fromCharCode(97+d),c,l)}},paintInNotation:function(t,i,n){var r=this.cbCSize(t),s=t.coordsFn(t),o=t.colorFill;i.font=Math.ceil(r.cx/5)+"px Monospace";for(var c=0;c<a;c++)for(var l=0;l<e;l++){var h=a-c,d=l;this.mViewAs<0?d=e-l-1:h=c+1;var p=1==this.mViewAs?l+c*e:e*a-(1+l+c*e),m=s.call(this,p);switch(i.fillStyle="rgba(0,0,0,0)","bump"==n&&(i.fillStyle=o["."]),this.cbView.boardLayout[a-c-1][l]){case".":i.fillStyle="bump"==n?o["."]:o["#"];break;case"#":i.fillStyle=o["."]}var u=m.xb-r.cx/3,f=m.yb-r.cy/3;t.notationDebug?i.fillText(p,u,f):i.fillText(String.fromCharCode(97+d)+h,u,f)}},createMaterial:function(e,a,t){var i=new THREE.Texture(a.diffuse);i.needsUpdate=!0;var n={specular:"#050505",emissive:"#000000",shininess:20,shading:THREE.FlatShading,map:i};if(a.bump){var r=new THREE.Texture(a.bump);r.needsUpdate=!0,n.bumpMap=r,n.bumpScale=.1}var s=new THREE.MeshPhongMaterial(n);s.side=THREE.DoubleSide,s.opacity=1,t(s)}}),View.Board.cbAnimate=function(e,a,t,i){function n(){0==--r&&(s&&a.PlaySound("tac"+(1+Math.floor(3*Math.random()))),i())}var r=1,s=!1,o=this.pieces[this.board[t.f]],c=a.cbMakeDisplaySpec(t.f,o.s),l=a.cbMakeDisplaySpecForPiece(a,t.t,o);for(var h in c){var d=c[h];void 0!==d.z&&function(e){for(var a=l[e],t=Math.atan2(d.y,d.x),i=Math.atan2(a.y,a.x);i<t;)i+=2*Math.PI;i-t>Math.PI&&(i-=2*Math.PI);var n=Math.sqrt(d.y*d.y+d.x*d.x),r=n,o=n+1500,c=n,h=c-o,p=c-r;s=!0;var m=4*h-2*p,u=-p*p,f=Math.abs(m*m- -4*u),g=(-m-Math.sqrt(f))/-2,b=(-m+Math.sqrt(f))/-2,w=g,v=-w-p;(0==w||-v/(2*w)<0||-v/(2*w)>1)&&(w=b,v=-w-p),a.positionEasingUpdate=function(e){var a=w*e*e+v*e+c,n=t+(i-t)*e,r=a*Math.cos(n)*this.SCALE3D,s=a*Math.sin(n)*this.SCALE3D;this.object3d.position.x=r,this.object3d.position.z=s}}(h)}if(s||a.PlaySound("move"+(1+Math.floor(4*Math.random()))),e.updateGadget("piece#"+o.i,l,600,function(){n()}),null!=t.c){r++;var p={positionEasingUpdate:null};switch(a.cbView.captureAnim3d||"movedown"){case"movedown":p.z=-2e3;break;case"scaledown":p.scale=[0,0,0]}var m=this.pieces[t.c];e.updateGadget("piece#"+m.i,{"2d":{opacity:0},"3d":p},600,n)}if(void 0!==t.cg){var d=a.cbVar.castle[t.f+"/"+t.cg],u=d.r[d.r.length-1],o=this.pieces[this.board[t.cg]],l=a.cbMakeDisplaySpecForPiece(a,u,o);r++,e.updateGadget("piece#"+o.i,l,600,function(){n()})}}}(),function(){var e={cx:512,cy:512};View.Game.cbStauntonWoodenPieceStyle=function(e){return this.cbStauntonPieceStyle($.extend(!0,{default:{"2d":{file:this.mViewOptions.fullPath+"/res/images/woodenpieces2d2.png"}}},e))},View.Game.cbStauntonPieceStyle=function(e){return $.extend(!0,{1:{default:{"2d":{clipy:0}}},"-1":{default:{"2d":{clipy:100}}},default:{"3d":{display:this.cbDisplayPieceFn(this.cbStauntonPieceStyle3D)},"2d":{file:this.mViewOptions.fullPath+"/res/images/wikipedia.png",clipwidth:100,clipheight:100}},pawn:{"2d":{clipx:0}},knight:{"2d":{clipx:100}},bishop:{"2d":{clipx:200}},rook:{"2d":{clipx:300}},queen:{"2d":{clipx:400}},king:{"2d":{clipx:500}}},e)},View.Game.cbStauntonPieceStyle3D=$.extend(!0,{},View.Game.cbUniformPieceStyle3D,{default:{mesh:{normalScale:1,rotateZ:180},materials:{mat0:{channels:{diffuse:{size:e},normal:{size:e}}}}},1:{default:{materials:{mat0:{params:{specular:131586,shininess:150}}}}},"-1":{default:{materials:{mat0:{params:{specular:526344,shininess:100}}},paintTextureImageClip:function(e,a,t,i,n,r,s,o,c){var l=a.canvas.width,h=a.canvas.height;"diffuse"==i?(a.globalCompositeOperation="normal",a.drawImage(s,o.x,o.y,o.cx,o.cy,0,0,l,h),a.globalCompositeOperation="multiply",a.drawImage(s,o.x,o.y,o.cx,o.cy,0,0,l,h),a.drawImage(s,o.x,o.y,o.cx,o.cy,0,0,l,h),a.globalCompositeOperation="hue",a.fillStyle="rgba(0,0,0,0.7)",a.fillRect(0,0,512,512)):a.drawImage(s,o.x,o.y,o.cx,o.cy,0,0,l,h)}}},pawn:{mesh:{jsFile:"/res/staunton/pawn/pawn-classic.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/staunton/pawn/pawn-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/staunton/pawn/pawn-normalmap.jpg"}}}}}},knight:{mesh:{jsFile:"/res/staunton/knight/knight.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/staunton/knight/knight-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/staunton/knight/knight-normalmap.jpg"}}}}}},bishop:{mesh:{jsFile:"/res/staunton/bishop/bishop.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/staunton/bishop/bishop-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/staunton/bishop/bishop-normalmap.jpg"}}}}}},rook:{mesh:{jsFile:"/res/staunton/rook/rook.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/staunton/rook/rook-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/staunton/rook/rook-normalmap.jpg"}}}}}},queen:{mesh:{jsFile:"/res/staunton/queen/queen.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/staunton/queen/queen-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/staunton/queen/queen-normalmap.jpg"}}}}}},king:{mesh:{jsFile:"/res/staunton/king/king.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/staunton/king/king-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/staunton/king/king-normalmap.jpg"}}}}}}})}(),function(){View.Game.cbExtraLights=[{color:16777215,intensity:.5,position:[-14,14,-14],props:{shadowCameraNear:15,shadowCameraFar:40,castShadow:!0,shadowDarkness:.25,shadowMapWidth:2048,shadowMapHeight:2048}},{color:16777215,intensity:.5,position:[14,14,14],props:{shadowCameraNear:15,shadowCameraFar:40,castShadow:!1,shadowDarkness:.25,shadowMapWidth:2048,shadowMapHeight:2048}},{color:16777215,intensity:.5,position:[14,-14,14],props:{shadowCameraNear:15,shadowCameraFar:40,castShadow:!0,shadowDarkness:.25,shadowMapWidth:2048,shadowMapHeight:2048}},{color:16777215,intensity:.5,position:[-14,-14,-14],props:{shadowCameraNear:15,shadowCameraFar:40,castShadow:!1,shadowDarkness:.25,shadowMapWidth:2048,shadowMapHeight:2048}}],View.Game.cbDefineView=function(){var e=$.extend(!0,{},this.cbCylinderBoardClassic,{});return{coords:{"2d":this.cbGridBoard.coordsFn.call(this,this.cbGridBoardClassic2DNoMargin),"3d":this.cbCylinderBoard.coordsFn.call(this,e)},boardLayout:[".#.#.#.#","#.#.#.#.",".#.#.#.#","#.#.#.#.",".#.#.#.#","#.#.#.#.",".#.#.#.#","#.#.#.#."],board:{"2d":{draw:this.cbDrawBoardFn(this.cbGridBoardClassic2DNoMargin)},"3d":{display:this.cbDisplayBoardFn(e)}},clicker:{"2d":{width:1400,height:1400},"3d":{scale:[.9,.9,.9]}},pieces:this.cbStauntonPieceStyle({default:{"2d":{width:1300,height:1300},"3d":{
scale:[.6,.6,.6]}}}),captureAnim3d:"scaledown"}}}();