Code viewer for World: A* - Catch me if you can ...

// Cloned by test on 12 Nov 2019 from World "A*  - Catch me if you can" by Harpreet Singh 
// Please leave this clone trail here.
 
const show3d=!0,slowmotion=!1,diagonalmoves=!1,tracesearch=1,TEXTURE_WALL="/uploads/hsingh/brick3.jpg",TEXTURE_MAZE="/uploads/hsingh/patternmaze.jpg",TEXTURE_AGENT="/uploads/hsingh/smiley2.jpg",TEXTURE_ENEMY="/uploads/hsingh/1572624791.png",MUSIC_BACK="/uploads/hsingh/ailoop.wav",SOUND_ALARM="/uploads/hsingh/jolly-fanfares.wav",gridsize=20,NOBOXES=Math.trunc(gridsize*gridsize/10),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50,AB.headerRHS(),AB.headerWidth(300),ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/hsingh/pos-x.jpg","/uploads/hsingh/neg-x.jpg","/uploads/hsingh/pos-y.jpg","/uploads/hsingh/neg-y.jpg","/uploads/hsingh/pos-z.jpg","/uploads/hsingh/neg-z.jpg"],ACTION_LEFT=0,ACTION_RIGHT=1,ACTION_UP=2,ACTION_DOWN=3,ACTION_STAYSTILL=4;var BOXHEIGHT,the_agent,the_enemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,start,end,grid=new Array(gridsize),openSet=[],closedSet=[],path=[];function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,i=new THREE.TextureLoader,n=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()}),i.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),n.load(TEXTURE_MAZE,function(e){e.minFilter=THREE.LinearFilter,maze_texture=e,asynchFinished()&&initScene()})}function initScene(){var e,t;for(i=0;i<gridsize;i++)grid[i]=new Array(gridsize);for(i=0;i<gridsize;i++)for(n=0;n<gridsize;n++)grid[i][n]=new GridCell(i,n),0!=i&&i!=gridsize-1&&0!=n&&n!=gridsize-1||(grid[i][n].external_wall=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(i,n)),ABWorld.scene.add(t)),Math.random(1)<.2&&!grid[i][n].external_wall&&(grid[i][n].internal_maze_block=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(i,n)),ABWorld.scene.add(t));for(var i=0;i<gridsize;i++)for(var n=0;n<gridsize;n++)grid[i][n].addNeighbours(grid);do{i=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(i,n));ei=i,ej=n,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(the_enemy=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(the_enemy),drawEnemy();do{i=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(i,n));ai=i,aj=n,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(the_agent=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(the_agent),drawAgent(),ABWorld.scene.background=(new THREE.CubeTextureLoader).load(SKYBOX_ARRAY,function(){ABWorld.render(),AB.removeLoading(),AB.runReady=!0})}function drawAgent(){the_agent.position.copy(translate(ai,aj)),ABWorld.follow.copy(the_agent.position)}function drawEnemy(){the_enemy.position.copy(translate(ei,ej)),ABWorld.lookat.copy(the_enemy.position)}function asynchFinished(){return wall_texture&&agent_texture&&enemy_texture&&maze_texture}function occupied(e,t){return ei==e&&ej==t||(ai==e&&aj==t||(!!grid[e][t].external_wall||!!grid[e][t].internal_maze_block))}function updateStatusBefore(e){var t=AB.world.getState();AB.msg(" Step: "+AB.step+" &nbsp; x = ("+t.toString()+") &nbsp; a = ("+e+")")}function updateStatusAfter(){var e=AB.world.getState(),t=goodsteps/AB.step*100;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+t.toFixed(2)+"% ",2)}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 moveLogicalAgent(e){var t=ai,i=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?i++:e==ACTION_DOWN&&i--,occupied(t,i)||(ai=t,aj=i)}function moveLogicalEnemy(){var e,t,i=findShortestPath();if(void 0!==i&&(e=i.previous.i,t=i.previous.j,tracesearch)){var n=i;for((path=[]).push(n);n.previous;)path.push(n.previous),n=n.previous}occupied(e,t)||(ei=e,ej=t)}function findShortestPath(){for(openSet=[],closedSet=[],start=grid[ei][ej],end=grid[ai][aj],openSet.push(start);openSet.length>0;){for(var e=0,t=0;t<openSet.length;t++)openSet[t].f<openSet[e].f&&(e=t);var i=openSet[e];if(i===end)return i;removeFromArray(openSet,i),closedSet.push(i);var n=i.neighbours;for(t=0;t<n.length;t++){var a=n[t];if(!closedSet.includes(a)&&!a.external_wall&&!a.internal_maze_block){var r=i.g+heuristic(a,i),o=!1;openSet.includes(a)?r<a.g&&(a.g=r,o=!0):(a.g=r,o=!0,openSet.push(a)),o&&(a.h=heuristic(a,end),a.f=a.g+a.h,a.previous=i)}}}return i}function GridCell(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbours=[],this.previous=void 0,this.external_wall=!1,this.internal_maze_block=!1,this.addNeighbours=function(e){var t=this.i,i=this.j;t<gridsize-1&&this.neighbours.push(e[t+1][i]),t>0&&this.neighbours.push(e[t-1][i]),i<gridsize-1&&this.neighbours.push(e[t][i+1]),i>0&&this.neighbours.push(e[t][i-1]),diagonalmoves&&(t>0&&i>0&&this.neighbours.push(e[t-1][i-1]),t<gridsize-1&&i>0&&this.neighbours.push(e[t+1][i-1]),t>0&&i<gridsize-1&&this.neighbours.push(e[t-1][i+1]),t<gridsize-1&&i<gridsize-1&&this.neighbours.push(e[t+1][i+1]))}}function translate(e,t){var i=new THREE.Vector3;return i.y=0,i.x=e*squaresize-MAXPOS/2,i.z=t*squaresize-MAXPOS/2,i}function removeFromArray(e,t){for(var i=e.length-1;i>=0;i--)e[i]==t&&e.splice(i,1)}function heuristic(e,t){return diagonalmoves?Math.abs(e.i-t.i)+Math.abs(e.j-t.j):AB.distance2D(e.i,e.j,t.i,t.j)}AB.world.newRun=function(){AB.loadingScreen(),AB.runReady=!1,badsteps=0,goodsteps=0,BOXHEIGHT=squaresize,ABWorld.init3d(startRadiusConst,maxRadiusConst,14548957),loadResources()},AB.world.getState=function(){return[ai,aj,ei,ej]},AB.world.takeAction=function(e){updateStatusBefore(e),moveLogicalAgent(e),AB.step%2==0&&moveLogicalEnemy(),badstep()?badsteps++:goodsteps++,drawAgent(),drawEnemy(),updateStatusAfter(),agentBlocked()&&(AB.abortRun=!0,goodsteps=0,musicPause(),soundAlarm())},AB.world.endRun=function(){musicPause(),AB.abortRun?AB.msg(" <br> <font color=red> <B> Agent trapped. Final score zero. </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 backmusic=AB.backgroundMusic(MUSIC_BACK);function musicPlay(){backmusic.play()}function musicPause(){backmusic.pause()}function soundAlarm(){new Audio(SOUND_ALARM).play()}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))}