jocly
Version:
Board games 3D/2D UI and AI web library
1 lines • 26.3 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 r=0;r<this.cbExtraLights.length;r++)i.updateGadget("extralights#"+r,{"3d":{visible:!0}});i.updateGadget("board",$.extend({base:{visible:!0}},this.cbView.board));for(var n=0;n<this.g.boardSize;n++)!function(e){var a=t.cbMakeDisplaySpec(e,0),r=$.extend(!0,{},a,{base:{visible:!0}},t.cbView.clicker,t.cbView.cell);i.updateGadget("cell#"+e,r),$.extend(!0,a,t.cbView.clicker),i.updateGadget("clicker#"+e,a)}(n);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 r=0;r<this.g.pTypes.length;r++)i.updateGadget("promo#"+r,{base:{visible:!1}})},View.Game.cbDisplayBoardFn=function(e){var a=this;return function(i,r,n){var s=e.style+"_"+e.margins.x+"_"+e.margins.y+"_"+a.mNotation+"_"+a.mViewAs,c=this;s!=this._cbKey&&(this._cbKey=s,e.display.call(t,e,c,function(e){c.replaceMesh(e,r,n)}))}},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 r=this.cbView.coords[i],n=r.call(this,e);t[i]={x:n.x||0,y:n.y||0,z:n.z||0,rotateX:n.rx||0,rotateY:(n.ry||0)*("3d"==i?this.mViewAs*a<0?-1:1:0),rotate:(n.rz||0)+("3d"==i&&this.mViewAs*a<0?180:0)}}return t},View.Game.cbMakeDisplaySpecForPiece=function(t,i,r){function n(e,a,t){return a?$.extend(!0,e,a.default,a[t]):{}}var s=this.cbMakeDisplaySpec(i,r.s);if(void 0===e.pieceTypes[r.t])return void console.warn("Piece type",r.t,"not defined in model");var c=e.pieceTypes[r.t].aspect||e.pieceTypes[r.t].name;return c?(a.pieces&&(s=n(s,a.pieces,c),a.pieces[r.s]&&(s=n(s,a.pieces[r.s],c))),s):void console.warn("Piece type",r.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 r=a.cbMakeDisplaySpecForPiece(a,i.p,i);r=$.extend(!0,{base:{visible:!0},"2d":{opacity:1},"3d":{positionEasingUpdate:null}},r),e.updateGadget("piece#"+t,r)}}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 r=new THREE.Object3D;i.add(r),t.target=r,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(r,n){var s={};if(null==n.f)r.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==n.t)r.forEach(function(r){var n=void 0===r.cg?r.t:r.cg;void 0===s[n]&&(s[n]={t:r.t,moves:[],click:["piece#"+this.board[n],"clicker#"+n],view:["clicker#"+n],highlight:function(e){a.updateGadget("cell#"+n,{"2d":{classes:"select"==e?"cb-cell-select":"cb-cell-cancel",opacity:t.mShowMoves||"cancel"==e?1:0}}),a.updateGadget("clicker#"+n,{"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#"+n,{"2d":{classes:""}})},validate:{t:r.t},execute:function(i){var n=this;this.cbAnimate(a,t,r,function(){var c=s[r.t].moves;c.length>1&&(a.updateGadget("promo-board",{base:{visible:!0,width:t.cbPromoSize*(c.length+1)}}),a.updateGadget("promo-cancel",{base:{visible:!0,x:c.length*t.cbPromoSize/2}}),c.forEach(function(i,r){var n=e.pieceTypes[i.pr].aspect||e.pieceTypes[i.pr].name,s=$.extend(!0,{},t.cbView.pieces.default,t.cbView.pieces[n]);t.cbView.pieces[this.mWho]&&(s=$.extend(!0,s,t.cbView.pieces[this.mWho].default,t.cbView.pieces[this.mWho][n])),a.updateGadget("promo#"+i.pr,{base:$.extend(s["2d"],{x:(r-c.length/2)*t.cbPromoSize})})},n)),i()})},unexecute:function(){if(null!=r.c){var e=this.pieces[r.c],n=t.cbMakeDisplaySpecForPiece(t,e.p,e);n=$.extend(!0,{base:{visible:!0},"2d":{opacity:1},"3d":{positionEasingUpdate:null}},n),a.updateGadget("piece#"+r.c,n)}var s=this.pieces[this.board[r.f]],c=t.cbMakeDisplaySpecForPiece(t,s.p,s);a.updateGadget("piece#"+s.i,c),i()}}),void 0!==r.cg&&(s[n].validate.cg=r.cg,s[n].execute=function(e){this.cbAnimate(a,t,r,function(){e()})}),s[n].moves.push(r)},this);else if(null==n.pr){var c=[];r.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},c.push(e.pr)),s[e.pr].moves.push(e))},this),c.length>1&&c.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 r(){0==--s&&(c&&a.PlaySound("tac"+(1+Math.floor(3*Math.random()))),i())}var n=this,s=1,c=!1,o=this.pieces[this.board[t.f]],l=a.cbMakeDisplaySpec(t.f,o.s),h=a.cbMakeDisplaySpecForPiece(a,t.t,o);for(var d in l){var p=l[d];void 0!==p.z&&function(e){var i=p.z,r=h[e].z,s=n.cbMoveMidZ(a,t,i,r,e),o=i,l=o-s,d=o-r;s!=(i+r)/2&&(c=!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+o)*this.SCALE3D;this.object3d.position.y=a}}(d)}if(c||a.PlaySound("move"+(1+Math.floor(4*Math.random()))),e.updateGadget("piece#"+o.i,h,600,function(){r()}),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,r)}if(void 0!==t.cg){var p=a.cbVar.castle[t.f+"/"+t.cg],f=p.r[p.r.length-1],o=this.pieces[this.board[t.cg]],h=a.cbMakeDisplaySpecForPiece(a,f,o);s++,e.updateGadget("piece#"+o.i,h,600,function(){r()})}},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(r){var n=["diffuse"].concat(e.extraChannels||[]),s={};n.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(n){var c=new THREE.Mesh(a,n);e.modifyMesh.call(i,e,c,function(a){e.paint.call(i,e,s,r,function(){t(a)})})})})})},createTextureImages:function(e,a){var t=this,i={},r=0,n=e.texturesImg||{};for(var s in n)r++;if(0==r)a(i);else for(var s in n)!function(s){e.getResource("image|"+t.g.fullPath+n[s],function(e){i[s]=e,0==--r&&a(i)})}(s)},createMaterial:function(e,a,t){var i=new THREE.Texture(a.diffuse);i.needsUpdate=!0;var r={specular:"#050505",shininess:30,map:i};if(a.bump){var n=new THREE.Texture(a.bump);n.needsUpdate=!0,r.bumpMap=n,r.bumpScale=.05}t(new THREE.MeshPhongMaterial(r))},modifyMesh:function(e,a,t){t(a)},prePaint:function(e,a,t,i,r){r()},paint:function(e,a,t,i,r){r()},postPaint:function(e,a,t,i,r){r()},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,r=e(t);return function(e,n,s){a=this.getResource;var c=/^piece#([0-9]+)$/.exec(this.gadget.id);if(!c)return null;var o=parseInt(c[1]),l=i.cbCurrentGame();if(o>=l.mBoard.pieces.length)return null;var h=l.mBoard.pieces[o],d=l.cbVar.pieceTypes[h.t].aspect||l.cbVar.pieceTypes[h.t].name,p=d+"_"+r+"_"+h.s,m=this;p!=this._cbKey&&(this._cbKey=p,m.options=n,l.cbMakePiece(t,d,h.s,function(e){m.replaceMesh(e,m.options,s)}))}},View.Game.cbMakePiece=function(a,i,r,n){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 c=s({},a,i);a[r]&&(c=s(c,a[r],i));var o=e(c),l=t[o];Array.isArray(l)?l.push(n):l?n(new THREE.Mesh(l.geometry,l.material)):(t[o]=[n],c.loadResources.call(this,c,function(e){c.displayPiece.call(this,c,e,function(){var a=t[o];t[o]={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==--n&&t(s)}var r=this,n=1,s={};for(var c in e.materials){var o=e.materials[c].channels;for(var l in o)if(o[l].texturesImg)for(var h in o[l].texturesImg)!function(e,t){n++,a("image|"+r.g.fullPath+t,function(a){s[e]=a,i()})}(h,o[l].texturesImg[h])}i()},loadResources:function(e,a){function t(){0==--s&&a({geometry:r,images:i,textures:{},loadedMaterials:n})}var i,r,n,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}r=a,n=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,r,n,s,c,o){var l=a.canvas.width,h=a.canvas.height;if(r.patternFill&&r.patternFill[n]){var d=r.patternFill[n];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,c.x,c.y,c.cx,c.cy,0,0,l,h),a.drawImage(p,0,0,l,h,0,0,l,h),a.restore()}else a.drawImage(s,c.x,c.y,c.cx,c.cy,0,0,l,h)},paintTextureImage:function(e,a,t,i,r,n,s,c){var o;o=r.clipping&&r.clipping[n]?r.clipping[n]:{x:0,y:0,cx:s.width,cy:s.height},e.paintTextureImageClip.call(this,e,a,t,i,r,n,s,o,c)},paintTexture:function(e,a,t,i,r){var n=e.materials[t].channels[i];for(var s in n.texturesImg){var c=r.images[s];e.paintTextureImage.call(this,e,a,t,i,n,s,c,r)}},makeMaterialTextures:function(e,a,t){for(var i in e.materials[a].channels){var r=e.materials[a].channels[i],n=document.createElement("canvas");n.width=r.size.cx,n.height=r.size.cy;var s=n.getContext("2d");e.paintTexture.call(this,e,s,a,i,t);var c=new THREE.Texture(n);c.needsUpdate=!0,t.textures[a][i]=c}},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 r in a.loadedMaterials){var n=a.loadedMaterials[r].clone(),s=n.name;if(e.materials[s]){$.extend(!0,n,e.materials[s].params);for(var c in e.materials[s].channels)switch(c){case"diffuse":n.map=a.textures[s][c];break;case"bump":n.bumpMap=a.textures[s][c]}}i.push(n)}var o=new THREE.MultiMaterial(i);a.material=o}}}),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 r=e.materials[a].channels.normal.normalScale||1;i.normalScale=new THREE.Vector2(r,r);var n=new THREE.MeshPhongMaterial(i);t.material=n,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 r=t[i.margins.x+"_"+i.margins.y];if(!r){var n,s,c,o,l=e+2*i.margins.x,h=a+2*i.margins.y;n=l/h,o=n<1?12e3*n/l:12e3/n/h,s=(e+2*i.margins.x)*o,c=(a+2*i.margins.y)*o,r={cx:o,cy:o,pieceCx:o,pieceCy:o,ratio:n,width:s,height:c},t[i.margins.x+"_"+i.margins.y]=r}return r},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 r=this.cbCSize(t),n=i%e,s=(i-n)/e;return 1==this.mViewAs&&(s=a-1-s),-1==this.mViewAs&&(n=e-1-n),{x:(n-(e-1)/2)*r.cx,y:(s-(a-1)/2)*r.cy,z:0}}},createGeometry:function(e,a){var t=this.cbCSize(e),i=t.width/1e3,r=t.height/1e3,n=new THREE.PlaneGeometry(i,r),s=new THREE.Matrix4;s.makeRotationX(-Math.PI/2),n.applyMatrix(s);for(var c=n.faceVertexUvs[0],o=0;o<c.length;o++)for(var l=0;l<c[o].length;l++)t.ratio<1&&(c[o][l].x=c[o][l].x*t.ratio+(1-t.ratio)/2),t.ratio>1&&(c[o][l].y=c[o][l].y/t.ratio+(1-1/t.ratio)/2);a(n)},paintBackground:function(e,a,t,i,r,n){t.boardBG&&a.drawImage(t.boardBG,-r/2,-n/2,r,n)},paintChannel:function(e,a,t,i){var r=this.cbCSize(e);e.paintBackground.call(this,e,a,t,i,r.width,r.height)},paint:function(e,a,t,i){for(var r in a){var n=a[r].getContext("2d");n.save(),n.scale(e.TEXTURE_CANVAS_CX/12e3,e.TEXTURE_CANVAS_CY/12e3),n.translate(6e3,6e3),e.paintChannel.call(this,e,n,t,r),n.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 r=this.cbCSize(e),n=r.width/1e3,s=r.height/1e3,c=i(n+.5+.1,s+.5+.1),o=i(n+.1,s+.1);c.holes.push(o);var l={amount:.4,steps:1,bevelSize:.1,bevelThickness:.04,bevelSegments:1},h=new THREE.ExtrudeGeometry(c,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(n,s,.1),blackMat);m.rotation.x=Math.PI/2,m.position.y=-.1,a.add(m),t(a)},paintCell:function(e,a,t,i,r,n,s,c,o){a.strokeStyle="rgba(0,0,0,1)",a.lineWidth=15,a.fillStyle="bump"==i?"#ffffff":e.colorFill[r],a.fillRect(n-c/2,s-o/2,c,o),a.rect(n-c/2,s-o/2,c,o)},paintCells:function(t,i,r,n){for(var s=this.cbCSize(t),c=t.coordsFn(t),o=0;o<a;o++)for(var l=0;l<e;l++){var h=1==this.mViewAs?l+o*e:e*a-(1+l+o*e),d=c.call(this,h),p=this.cbView.boardLayout[a-o-1][l],m=d.x,u=d.y,f=s.cx,g=s.cy;t.paintCell.call(this,t,i,r,n,p,m,u,f,g)}},paintLines:function(t,i,r,n){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 r=this.cbCSize(e);e.paintBackground.call(this,e,a,t,i,r.width,r.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,r){for(var n=this.cbCSize(t),s=0;s<a;s++){var c=a-s;this.mViewAs<0&&(c=s+1);var o=-(e/2+t.margins.x/2)*n.cx,l=(s-a/2+.5)*n.cy;i.fillText(c,o,l)}for(var h=0;h<e;h++){var d=h;this.mViewAs<0&&(d=e-h-1);var o=(h-e/2+.5)*n.cx,l=(a/2+t.margins.y/2)*n.cy;i.fillText(String.fromCharCode(97+d),o,l)}},paintInNotation:function(t,i,r){var n=this.cbCSize(t),s=t.coordsFn(t),c=t.colorFill;i.font=Math.ceil(n.cx/5)+"px Monospace";for(var o=0;o<a;o++)for(var l=0;l<e;l++){var h=a-o,d=l;this.mViewAs<0?d=e-l-1:h=o+1;var p=1==this.mViewAs?l+o*e:e*a-(1+l+o*e),m=s.call(this,p);switch(i.fillStyle="rgba(0,0,0,0)","bump"==r&&(i.fillStyle=c["."]),this.cbView.boardLayout[a-o-1][l]){case".":i.fillStyle="bump"==r?c["."]:c["#"];break;case"#":i.fillStyle=c["."]}var u=m.x-n.cx/3,f=m.y-n.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 r=e.cbVar.geometry,n=r.C(a.f),s=r.C(a.t),c=r.R(a.f),o=r.R(a.t);return s-n==0||o-c==0||Math.abs(s-n)==Math.abs(o-c)?(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(){View.Game.cbShatranjBoard=$.extend({},View.Game.cbGridBoardClassic,{colorFill:{".":"rgba(0,0,0,0)"},paintLines:function(e,a,t,i){a.strokeStyle="rgba(0,0,0,1)",a.lineWidth=30,a.stroke()}}),View.Game.cbShatranjBoard3DMargin=$.extend({},View.Game.cbShatranjBoard,{margins:{x:.47,y:.47},extraChannels:["bump"],texturesImg:{boardBG:"/res/images/wood-chipboard-2.jpg"}}),View.Game.cbShatranjBoard2DMargin=$.extend({},View.Game.cbShatranjBoard,{margins:{x:.47,y:.47},colorFill:{".":"rgba(0,0,0,0)"},texturesImg:{boardBG:"/res/images/wood-chipboard-2.jpg"}})}(),function(){var e={cx:512,cy:512};View.Game.cbNishapurPieceStyle=function(e){return $.extend(!0,{1:{default:{"2d":{clipy:0}}},"-1":{default:{"2d":{clipy:100}}},default:{"3d":{display:this.cbDisplayPieceFn(this.cbNishapurPieceStyle3D)},"2d":{file:this.mViewOptions.fullPath+"/res/nishapur/nishapur-2d-sprites.png",clipwidth:100,clipheight:100}},"np-pawn":{"2d":{clipx:0}},"np-knight":{"2d":{clipx:300}},"np-elephant":{"2d":{clipx:200}},"np-rook":{"2d":{clipx:100}},"np-general":{"2d":{clipx:400}},"np-king":{"2d":{clipx:500}}},e)},View.Game.cbNishapurPieceStyle3D=$.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:1118481,shininess:100}}},paintTextureImageClip:function(e,a,t,i,r,n,s,c,o){var l=a.canvas.width,h=a.canvas.height;"diffuse"==i?(a.globalCompositeOperation="normal",a.drawImage(s,c.x,c.y,c.cx,c.cy,0,0,l,h),a.globalCompositeOperation="overlay",a.fillStyle="rgba(123,61,27,0.7)",a.fillRect(0,0,512,512)):a.drawImage(s,c.x,c.y,c.cx,c.cy,0,0,l,h)}}},"-1":{default:{materials:{mat0:{params:{specular:3355443,shininess:100}}},paintTextureImageClip:function(e,a,t,i,r,n,s,c,o){var l=a.canvas.width,h=a.canvas.height;"diffuse"==i?(a.globalCompositeOperation="normal",a.drawImage(s,c.x,c.y,c.cx,c.cy,0,0,l,h),a.globalCompositeOperation="overlay",a.fillStyle="rgba(127,127,68,0.7)",a.fillRect(0,0,512,512)):a.drawImage(s,c.x,c.y,c.cx,c.cy,0,0,l,h)}}},"np-pawn":{mesh:{jsFile:"/res/nishapur/pawn/pawn.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/nishapur/pawn/pawn-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/nishapur/pawn/pawn-normalmap.jpg"}}}}}},"np-knight":{mesh:{jsFile:"/res/nishapur/knight/knight.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/nishapur/knight/knight-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/nishapur/knight/knight-normalmap.jpg"}}}}}},"np-elephant":{mesh:{jsFile:"/res/nishapur/elephant/elephant.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/nishapur/elephant/elephant-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/nishapur/elephant/elephant-normalmap.jpg"}}}}}},"np-rook":{mesh:{jsFile:"/res/nishapur/rook/rook.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/nishapur/rook/rook-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/nishapur/rook/rook-normalmap.jpg"}}}}}},"np-general":{mesh:{jsFile:"/res/nishapur/general/general.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/nishapur/general/general-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/nishapur/general/general-normalmap.jpg"}}}}}},"np-king":{mesh:{jsFile:"/res/nishapur/king/king.js"},materials:{mat0:{channels:{diffuse:{texturesImg:{diffImg:"/res/nishapur/king/king-diffusemap.jpg"}},normal:{texturesImg:{normalImg:"/res/nishapur/king/king-normalmap.jpg"}}}}}}})}(),function(){View.Game.cbDefineView=function(){return{coords:{"2d":this.cbGridBoard.coordsFn.call(this,this.cbShatranjBoard2DMargin),"3d":this.cbGridBoard.coordsFn.call(this,this.cbShatranjBoard3DMargin)},boardLayout:["........","........","........","........","........","........","........","........"],board:{"2d":{draw:this.cbDrawBoardFn(this.cbShatranjBoard2DMargin)},"3d":{display:this.cbDisplayBoardFn(this.cbShatranjBoard3DMargin)}},clicker:{"2d":{width:1400,height:1400},"3d":{scale:[.9,.9,.9]}},pieces:this.cbNishapurPieceStyle({default:{"2d":{width:1400,height:1400},"3d":{scale:[.6,.6,.6]}}})}},View.Board.cbMoveMidZ=function(e,a,t,i){return"N"==a.a||"E"==a.a?Math.max(t,i)+1500:(t+i)/2}}();