Code viewer for World: Tom and Jerry Chase A* Se...

// Cloned by bo on 22 Nov 2020 from World " Tom and Jerry Chase A* Search on Complex World (clone by test)" by test 
// Please leave this clone trail here.
 


// Cloned by test on 12 Nov 2019 from World " Tom and Jerry Chase A* Search on Complex World" by Nihar Behera 
// Please leave this clone trail here.
 
AB.clockTick=2000,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/starter/door.jpg",TEXTURE_MAZE="/uploads/starter/latin.jpg",TEXTURE_AGENT="/uploads/nihar/mouse_1111.jpg",TEXTURE_ENEMY="/uploads/nihar/cat_2222.jpg",gridsize=20,NOBOXES=Math.trunc(gridsize*gridsize/10),squaresize=20,MAXPOS=gridsize*squaresize,SKYCOLOR=65280,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/starter/dawnmountain-xpos.png","/uploads/starter/dawnmountain-xneg.png","/uploads/starter/dawnmountain-ypos.png","/uploads/starter/dawnmountain-yneg.png","/uploads/starter/dawnmountain-zpos.png","/uploads/starter/dawnmountain-zneg.png"],ACTION_LEFT=0,ACTION_RIGHT=1,ACTION_UP=2,ACTION_DOWN=3,ACTION_STAYSTILL=4,GRID_BLANK=0,GRID_WALL=1,GRID_MAZE=2,GRID_ENEMY=10,GRID_AGENT=20;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,GRID=new Array(gridsize);class coordinates{constructor(e,t){this.i=e,this.j=t}}var badsteps,goodsteps,start=new coordinates,target=new coordinates;const MAX_OPENSET=gridsize*gridsize;var diagonal=!0;const PROOF=!0;var proofArray=[];function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,a=new THREE.TextureLoader,r=new THREE.TextureLoader;e.load(TEXTURE_WALL,function(e){e.minFilter=THREE.LinearFilter,wall_texture=e,asynchFinished()&&initScene()}),t.load(TEXTURE_AGENT,function(e){e.minFilter=THREE.LinearFilter,agent_texture=e,asynchFinished()&&initScene()}),a.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),r.load(TEXTURE_MAZE,function(e){e.minFilter=THREE.LinearFilter,maze_texture=e,asynchFinished()&&initScene()})}function asynchFinished(){return!!(wall_texture&&agent_texture&&enemy_texture&&maze_texture)}function occupied(e,t){if(e<=gridsize&&t<=gridsize&&e>=0&&t>=0)switch(GRID[e][t]){case GRID_WALL:case GRID_MAZE:case GRID_ENEMY:case GRID_AGENT:return!0;default:return!1}return!0}function iswall(e,t){switch(GRID[e][t]){case GRID_WALL:case GRID_MAZE:return!0;default:return!1}}function translate(e,t){var a=new THREE.Vector3;return a.y=-squaresize,a.x=e*squaresize-MAXPOS/2,a.z=t*squaresize-MAXPOS/2,a}function initScene(){var e,t,a,r;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)0===e||e==gridsize-1||0===t||t==gridsize-1?(GRID[e][t]=GRID_WALL,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:wall_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r)):GRID[e][t]=GRID_BLANK;for(var i=1;i<=NOBOXES;i++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:maze_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ai=e,aj=t,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),GRID[ai][aj]=GRID_AGENT,GRID[ei][ej]=GRID_ENEMY,ABWorld.scene.background=(new THREE.CubeTextureLoader).load(SKYBOX_ARRAY,function(){ABWorld.render(),AB.removeLoading(),AB.runReady=!0})}function drawEnemy(){theenemy.position.copy(translate(ei,ej)),ABWorld.lookat.copy(theenemy.position)}function drawAgent(){theagent.position.copy(translate(ai,aj)),ABWorld.follow.copy(theagent.position)}function gridSpot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.parent=void 0,this.closed=!1,this.addNeighbors=function(){var e=this.i,t=this.j;e<gridsize-1&&this.neighbors.push(aStarGrid[e+1][t]),e>0&&this.neighbors.push(aStarGrid[e-1][t]),t<gridsize-1&&this.neighbors.push(aStarGrid[e][t+1]),t>0&&this.neighbors.push(aStarGrid[e][t-1]),diagonal&&(e>0&&t>0&&this.neighbors.push(aStarGrid[e-1][t-1]),e<gridsize-1&&t>0&&this.neighbors.push(aStarGrid[e+1][t-1]),e>0&&t<gridsize-1&&this.neighbors.push(aStarGrid[e-1][t+1]),e<gridsize-1&&t<gridsize-1&&this.neighbors.push(aStarGrid[e+1][t+1]))}}var aStarGrid=[];function aStarInit(){var e,t;for(e=0;e<gridsize;e++)for(aStarGrid[e]=[],t=0;t<gridsize;t++)aStarGrid[e][t]=new gridSpot(e,t);for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)aStarGrid[e][t].addNeighbors();console.info("A* Initialise: Complete")}function aStar(e,t,a){var r,o,s=[],d=!1,u=aStarGrid[a.i][a.j],c=aStarGrid[t.i][t.j],l=new coordinates(t.i,t.j);for(s.push(c),console.info("cleaning up previous results"),i=0;i<gridsize;i++)for(j=0;j<gridsize;j++)(o=aStarGrid[i][j]).f=o.h=o.g=0,o.closed=!1,o.parent=null;for(;s.length>0&&!d;){if(s.length>MAX_OPENSET)return console.error("there is a problem with the openset, bail out!"),d=!0,l;if(s.sort((e,t)=>e.f>t.f?1:-1),(o=s.shift()).closed=!0,PROOF&&(proofArray=[]),console.info("Current: "+o.i+", "+o.j+" - (O/C) is "+s.length),o==u){for(PROOF&&proofArray.push(u);o.parent!=c;)PROOF&&proofArray.push(o),o=o.parent;d=!0,PROOF&&proofArray.push(c)}else for(n=0;n<o.neighbors.length;n++)(r=o.neighbors[n]).closed||(iswall(r.i,r.j)?r.closed=!0:(r.i==o.i||r.j==o.j?r.g=o.g+10:r.g=o.g+14,r.h=aStarHeuristics(r,u),r.f=r.g+r.h,r.parent=o,s.indexOf(r)<0&&s.push(r)))}return l.i=o.i,l.j=o.j,console.info("returning form a*: "),l}function aStarHeuristics(e,t){return diagonal?Math.pow(e.i-t.i,2)+Math.pow(e.j-t.j,2):Math.abs(e.i-e.i)+Math.abs(e.j-t.j)}function moveLogicalEnemy(){start.i=ei,start.j=ej,target.i=ai,target.j=aj,bestPath=aStar(GRID,start,target),occupied(bestPath.i,bestPath.j)||(GRID[ei][ej]=GRID_BLANK,ei=bestPath.i,ej=bestPath.j,GRID[ei][ej]=GRID_ENEMY),console.info("enemy moving to : "+ei+","+ej)}function moveLogicalAgent(e){var t=ai,a=aj;switch(e){case ACTION_LEFT:t--,theagent.rotation.y=0;break;case ACTION_RIGHT:t++,theagent.rotation.y=-1;break;case ACTION_UP:a++,theagent.rotation.y=-2;break;case ACTION_DOWN:a--,theagent.rotation.y=2}occupied(t,a)||(GRID[ai][aj]=GRID_BLANK,aj=a,GRID[ai=t][aj]=GRID_AGENT)}var OURKEYS=[37,38,39,40];function ourKeys(e){return OURKEYS.includes(e.keyCode)}function keyHandler(e){return!AB.runReady||(!ourKeys(e)||(37==e.keyCode&&moveLogicalAgent(ACTION_LEFT),38==e.keyCode&&moveLogicalAgent(ACTION_DOWN),39==e.keyCode&&moveLogicalAgent(ACTION_RIGHT),40==e.keyCode&&moveLogicalAgent(ACTION_UP),e.stopPropagation(),e.preventDefault(),!1))}function badstep(){return Math.abs(ei-ai)<2&&Math.abs(ej-aj)<2}function agentBlocked(){return occupied(ai-1,aj)&&occupied(ai+1,aj)&&occupied(ai,aj+1)&&occupied(ai,aj-1)}function updateStatusBefore(e){var t=AB.world.getState();AB.msg("Step: "+AB.step+"<br>x = ("+t.toString()+")")}function updateStatusAfter(){AB.world.getState();var e=goodsteps/AB.step*100;AB.msg(" Bad steps: "+badsteps+"   Good steps: "+goodsteps+"   Score: "+e.toFixed(2)+"% ",2)}function playSound(){var e=new Audio("/uploads/brendanb/click.ogg"),t=1+(Math.abs(ei-ai)+Math.abs(ej-aj))/gridsize;console.log("PlaybackRate: "+t),e.playbackRate=t,e.volume=1/t,Math.abs(ei-ai)+Math.abs(ej-aj)<2&&(e.playbackRate=2*t,e.play()),e.play()}function consoleGrid(){for(var e="",t=gridsize-1;t>=0;t--){e="";for(var a=0;a<gridsize;a++)switch(GRID[t][a]){case GRID_WALL:e+=".";break;case GRID_MAZE:e+="+";break;case GRID_AGENT:e+="A";break;case GRID_ENEMY:e+="E";break;default:e+=" "}console.log(e)}start.i=ei,start.j=ej,target.i=ai,target.j=aj}AB.world.newRun=function(){AB.loadingScreen(),AB.runReady=!1,badsteps=0,goodsteps=0,aStarInit(),BOXHEIGHT=squaresize,ABWorld.init3d(startRadiusConst,maxRadiusConst,65280),loadResources(),document.onkeydown=keyHandler},AB.world.getState=function(){return[ai,aj,ei,ej]};var cameraAngle,enemyAngle=0;AB.world.takeAction=function(e){updateStatusBefore(e),console.info("-: Agent: "+ai+","+aj+" Enemy: "+ei+","+ej),moveLogicalAgent(e),AB.step%2==0&&(playSound(),moveLogicalEnemy()),console.info("+: Agent: "+ai+","+aj+" Enemy: "+ei+","+ej),badstep()?badsteps++:goodsteps++,enemyAngle+=.2,theenemy.rotation.y=enemyAngle,drawAgent(),drawEnemy(),updateStatusAfter(),(agentBlocked()||goodsteps/AB.step<.1)&&(AB.abortRun=!0,goodsteps=0),PROOF&&drawProof()},AB.world.endRun=function(){AB.abortRun?AB.msg(" <br> <font color=red> <B> Agent trapped.</B> </font>   ",3):AB.msg(" <br> <font color=green> <B> Run over. </B> </font>   ",3)},AB.world.getScore=function(){var e=goodsteps/AB.maxSteps*100;return Math.round(100*e)/100};var lastProofLine=[];function drawProof(){var e,t,a=new THREE.LineBasicMaterial({color:"lightblue",linewidth:5});lastProofLine.length>0&&(lastProofLine[0].material=a,ABWorld.scene.remove(lastProofLine.shift()));var r=new THREE.LineBasicMaterial({color:"blue",linewidth:5}),n=new THREE.Geometry;for(i=proofArray.length;i>1;i--){(e=proofArray.shift()).parent&&(t=e.parent,n.vertices.push(translate(e.i,e.j)),n.vertices.push(translate(t.i,t.j)),console.warn("Line between "+e.i+","+e.j+" and "+t.i+","+t.j));var o=new THREE.Line(n,r);lastProofLine.push(o),ABWorld.scene.add(o)}}