Code viewer for World: Complex World trap the age...

// Cloned by Chinmay on 7 Nov 2021 from World "Complex World trap the agent (by archy yu)" by archy yu 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/starter/door.jpg",TEXTURE_MAZE="/uploads/starter/latin.jpg",TEXTURE_AGENT="/uploads/starter/pacman.jpg",TEXTURE_ENEMY="/uploads/starter/ghost.3.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=30,NOBOXES=Math.trunc(gridsize*gridsize/4),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,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;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize);function Boxgrid(e,t,n){this.type=e,this.x=t,this.y=n,this.g=0,this.h=0,this.f=0,this.previous=null}function aStarPath(e,t){this.start=e,this.end=t,this.path=[],this.openSet=[],this.closeSet=[],this.line=null;for(var n=0;n<gridsize;n++)for(var i=0;i<gridsize;i++)GRID[n][i].previous=null,GRID[n][i].f=0,GRID[n][i].g=0,GRID[n][i].h=0;this.draw=function(){const e=new THREE.LineBasicMaterial({color:16777215}),t=[];for(var n=0;n<this.path.length;n++)t.push(translate(this.path[n].x,this.path[n].y));const i=(new THREE.BufferGeometry).setFromPoints(t);this.line=new THREE.Line(i,e),ABWorld.scene.add(this.line)},this.findHoles=function(){for(var e=[],t=0;t<gridsize;t++)for(var n=0;n<gridsize;n++)GRID[t][n].type==GRID_BLANK&&this.checkIsHole(GRID[t][n])&&e.push(GRID[t][n]);return e},this.checkIsHole=function(e){for(var t=this.getNeighbors(e.x,e.y),n=0,i=0;i<t.length;i++)t[i].type===GRID_BLANK&&n++;return 1==n},this.clean=function(){null!=this.line&&ABWorld.scene.remove(this.line)},this.logPath=function(){for(var e=0;e<this.path.length;e++)console.log("x:"+this.path[e].x+"y:"+this.path[e].y)},this.getNeighbors=function(e,t){var n=[];return e-1>0&&e-1<gridsize&&n.push(GRID[e-1][t]),e+1>0&&e+1<gridsize&&n.push(GRID[e+1][t]),t-1>0&&t<gridsize&&n.push(GRID[e][t-1]),t+1>0&&t+1<gridsize&&n.push(GRID[e][t+1]),n},this.heuristic=function(e,t){return Math.abs(e.x-t.x)+Math.abs(e.y-t.y)},this.removeFromList=function(e,t){var n=e.indexOf(t);-1!=n&&e.splice(n,1)},this.findPathByAstar=function(){this.openSet.push(this.start);for(var e=!1,t=null;0==e;){var n=0;if(0!=this.openSet.length){for(var i=0;i<this.openSet.length;i++)this.openSet[i].f<this.openSet[n].f&&(n=i);if((t=this.openSet[n]).x!=this.end.x||t.y!=this.end.y){var a=this.getNeighbors(t.x,t.y);this.removeFromList(this.openSet,t),this.closeSet.push(t);for(i=0;i<a.length;i++){var r=a[i];if(r.type!=GRID_WALL&&r.type!=GRID_MAZE){var s=!1;if(!this.closeSet.includes(r)){var o=t.g+this.heuristic(r,t);this.openSet.includes(r)?o<r.g&&(r.g=o,s=!0):(r.g=o,s=!0,this.openSet.push(r)),1==s&&(r.h=this.heuristic(r,this.end),r.f=r.g+r.h,r.previous=t)}}}}else e=!0}else e=!0}for(this.path=[];t;)this.path.push(t),t=t.previous;this.path=this.path.reverse()}}function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,n=new THREE.TextureLoader,i=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()}),n.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),i.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){return ei==e&&ej==t||(ai==e&&aj==t||(GRID[e][t].type==GRID_WALL||GRID[e][t].type==GRID_MAZE))}function translate(e,t){var n=new THREE.Vector3;return n.y=0,n.x=e*squaresize-MAXPOS/2,n.z=t*squaresize-MAXPOS/2,n}function initScene(){var e,t,n,i;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]=new Boxgrid(GRID_WALL,e,t),n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:wall_texture}),i.position.copy(translate(e,t)),ABWorld.scene.add(i)):GRID[e][t]=new Boxgrid(GRID_BLANK,e,t);for(var a=1;a<=NOBOXES;a++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=new Boxgrid(GRID_MAZE,e,t),n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:maze_texture}),i.position.copy(translate(e,t)),ABWorld.scene.add(i);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(n)).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,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(n)).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,t;ei<ai&&(e=AB.randomIntAtoB(ei,ei+1)),ei==ai&&(e=ei),ei>ai&&(e=AB.randomIntAtoB(ei-1,ei)),ej<aj&&(t=AB.randomIntAtoB(ej,ej+1)),ej==aj&&(t=ej),ej>aj&&(t=AB.randomIntAtoB(ej-1,ej)),occupied(e,t)||(ei=e,ej=t)}function moveLogicalAgent(e){var t=ai,n=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?n++:e==ACTION_DOWN&&n--,occupied(t,n)||(ai=t,aj=n)}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+" &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)}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]};var line=null;AB.world.takeAction=function(e){updateStatusBefore(e),moveLogicalAgent(e),badstep()?badsteps++:goodsteps++,drawAgent(),null!=line&&ABWorld.scene.remove(line);var t=new aStarPath(GRID[ei][ej],GRID[ai][aj]);t.findPathByAstar(),t.draw(),line=t.line,AB.step%2==0&&t.path.length>2&&(ei=t.path[1].x,ej=t.path[1].y),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()}