Code viewer for World: A* Enemy vs Pac Man (clone...

// Cloned by Dillan de Langen on 1 Nov 2019 from World "A* Enemy vs Pac Man" by Dillan de Langen 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/dillan/grasstexture.png",TEXTURE_MAZE=TEXTURE_WALL,TEXTURE_AGENT="/uploads/starter/pacman.jpg",TEXTURE_ENEMY="/uploads/dillan/Ghost5.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=20,NOBOXES=Math.trunc(gridsize*gridsize/10),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/dillan/forestbackground.png","/uploads/dillan/forestbackground.png","/uploads/dillan/forestbackground.png","/uploads/dillan/forestbackground.png","/uploads/dillan/forestbackground.png","/uploads/dillan/forestbackground.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;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,n=new THREE.TextureLoader,t=new THREE.TextureLoader,o=new THREE.TextureLoader;e.load(TEXTURE_WALL,function(e){e.minFilter=THREE.LinearFilter,wall_texture=e,asynchFinished()&&initScene()}),n.load(TEXTURE_AGENT,function(e){e.minFilter=THREE.LinearFilter,agent_texture=e,asynchFinished()&&initScene()}),t.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),o.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,n){return ei==e&&ej==n||(ai==e&&aj==n||(GRID[e][n]==GRID_WALL||GRID[e][n]==GRID_MAZE))}function translate(e,n){var t=new THREE.Vector3;return t.y=0,t.x=e*squaresize-MAXPOS/2,t.z=n*squaresize-MAXPOS/2,t}function initScene(){var e,n,t,o;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(n=0;n<gridsize;n++)0==e||e==gridsize-1||0==n||n==gridsize-1?(GRID[e][n]=GRID_WALL,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(o=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:wall_texture}),o.position.copy(translate(e,n)),ABWorld.scene.add(o)):GRID[e][n]=GRID_BLANK;for(var r=1;r<=NOBOXES;r++)e=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2),GRID[e][n]=GRID_MAZE,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(o=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:maze_texture}),o.position.copy(translate(e,n)),ABWorld.scene.add(o);do{e=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,n));ei=e,ej=n,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{e=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,n));ai=e,aj=n,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),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 distance(e,n,t,o){return Math.max(Math.abs(e-t)+Math.abs(n-o),1)}function heuristic(e,n){return distance(e.x,e.y,n.x,n.y)}function Point(e,n){this.x=e,this.y=n,this.f=0,this.g=0,this.h=0,this.previous=null;var t=this;this.toString=function(){return"("+t.x+", "+t.y+")"}}function pointsAreEqual(e,n){return e.x==n.x&&e.y==n.y}function removePointFromArray(e,n){for(var t=-1,o=0;o<e.length;o++)pointsAreEqual(e[o],n)&&(t=o);t>=0&&(e.splice(t,1),console.log("Removed "+n.toString()+"from array"))}function arrayContainsPoint(e,n){for(var t=0;t<e.length;t++)if(pointsAreEqual(e[t],n))return!0;return!1}function isWall(e){return GRID[e.x][e.y]==GRID_WALL||GRID[e.x][e.y]==GRID_MAZE}function getNeighborsOfPoint(e){var n=new Point(e.x,e.y+1),t=new Point(e.x,e.y-1),o=new Point(e.x-1,e.y),r=new Point(e.x+1,e.y),a=[];return isWall(n)||a.push(n),isWall(t)||a.push(t),isWall(o)||a.push(o),isWall(r)||a.push(r),a}function getNextPosition(e,n){console.log("Enemy position",e),console.log("Agent position",n);for(var t=[e],o=[],r=[];t.length>0;){for(var a=0,i=0;i<t.length;i++)t[i].f<t[a].f&&(a=i);var s=t[a];if(console.log("Current",s),console.log("Removing "+s.toString()+"from open."),removePointFromArray(t,s),pointsAreEqual(s,n)){console.log("Shortest path found to agent found.");var u=s;for(r.push(u);u.previous;)r.unshift(u.previous),u=u.previous;console.log("Path",r);break}var d=getNeighborsOfPoint(s);console.log("Neighbors",d);for(i=0;i<d.length;i++){var l=d[i];if(!arrayContainsPoint(o,l)&&!isWall(l)){var c=s.g+heuristic(l,s),A=!1;arrayContainsPoint(t,l)?c<l.g&&(l.g=c,A=!0):(l.g=c,A=!0,t.push(l)),A&&(l.h=heuristic(l,n),l.f=l.g+l.h,l.previous=s)}}console.log("Adding current to closed."),o.push(s)}return r.length>1?r[1]:r[0]}function moveLogicalEnemy(){var e=getNextPosition(new Point(ei,ej),new Point(ai,aj));e&&(occupied(e.x,e.y)||(ei=e.x,ej=e.y))}function moveLogicalAgent(e){var n=ai,t=aj;e==ACTION_LEFT?n--:e==ACTION_RIGHT?n++:e==ACTION_UP?t++:e==ACTION_DOWN&&t--,occupied(n,t)||(ai=n,aj=t)}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 n=AB.world.getState();AB.msg(" Step: "+AB.step+" &nbsp; x = ("+n.toString()+") &nbsp; a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),n=goodsteps/AB.step*100;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+n.toFixed(2)+"% ",2)}AB.world.newRun=function(){AB.loadingScreen(),AB.runReady=!1,badsteps=0,goodsteps=0,BOXHEIGHT=squaresize,ABWorld.init3d(startRadiusConst,maxRadiusConst,14548957),loadResources(),document.onkeydown=keyHandler},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(null);function musicPlay(){}function musicPause(){backmusic.pause()}function soundAlarm(){new Audio(SOUND_ALARM).play()}