Code viewer for World: Aggressive Enemy Complex W...

// Cloned by Nihar Behera on 6 Nov 2019 from World "Aggressive Enemy Complex World" by Shamsul Abedin 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,WALL="/uploads/sam007/door.jpg",MAZE="/uploads/sam007/latin.jpg",AGENT="/uploads/sam007/pacman.jpg",ENEMY="/uploads/sam007/new_ghost.JPG",MUSIC_BACK="/uploads/sam007/SuspenseStrings.mp3",SOUND_ALARM="/uploads/sam007/buzzer-soundbible.com-188422102.mp3",gridsize=25,NOBOXES=Math.trunc(gridsize*gridsize/5),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/sam007/dawnmountain-xpos.png","/uploads/sam007/dawnmountain-xneg.png","/uploads/sam007/dawnmountain-ypos.png","/uploads/sam007/dawnmountain-yneg.png","/uploads/sam007/dawnmountain-zpos.png","/uploads/sam007/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;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,a=new THREE.TextureLoader,t=new THREE.TextureLoader;e.load(WALL,function(e){e.minFilter=THREE.LinearFilter,wall_texture=e,asynchFinished()&&initScene()}),n.load(AGENT,function(e){e.minFilter=THREE.LinearFilter,agent_texture=e,asynchFinished()&&initScene()}),a.load(ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),t.load(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 a=new THREE.Vector3;return a.y=0,a.x=e*squaresize-MAXPOS/2,a.z=n*squaresize-MAXPOS/2,a}function initScene(){var e,n,a,t;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,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(e,n)),ABWorld.scene.add(t)):GRID[e][n]=GRID_BLANK;for(var o=1;o<=NOBOXES;o++)e=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2),GRID[e][n]=GRID_MAZE,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(e,n)),ABWorld.scene.add(t);do{e=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,n));ei=e,ej=n,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),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,n));ai=e,aj=n,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(a)).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 moveLogicalEnemy(){var e,n;ei<ai&&(e=AB.randomIntAtoB(ei,ei+1)),ei==ai&&(e=ei),ei>ai&&(e=AB.randomIntAtoB(ei-1,ei)),ej<aj&&(n=AB.randomIntAtoB(ej,ej+1)),ej==aj&&(n=ej),ej>aj&&(n=AB.randomIntAtoB(ej-1,ej)),occupied(e,n)||(ei=e,ej=n)}function moveLogicalAgent(e){var n=ai,a=aj;e==ACTION_LEFT?n--:e==ACTION_RIGHT?n++:e==ACTION_UP?a++:e==ACTION_DOWN&&a--,occupied(n,a)||(ai=n,aj=a)}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)<1&&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+") "),$("#user_3").html(status)}function updateStatusAfter(){var e=AB.world.getState(),n=goodsteps/AB.step*10;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+n.toFixed(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%1==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()}