Code viewer for World: assignment1_astar_chase (c...

// Cloned by rohan on 13 Nov 2021 from World "assignment1_astar_chase" by Yuhao Qiao 
// 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/yqiao2/1606584944.png",TEXTURE_AGENT="/uploads/yqiao2/1606585119.png",TEXTURE_ENEMY="/uploads/yqiao2/1606585170.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=50,NOBOXES=Math.trunc(gridsize*gridsize/3),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/starter/sky_pos_z.jpg","/uploads/starter/sky_neg_z.jpg","/uploads/starter/sky_pos_y.jpg","/uploads/starter/sky_neg_y.jpg","/uploads/starter/sky_pos_x.jpg","/uploads/starter/sky_neg_x.jpg"],scene=new THREE.Scene,Linematerial=new THREE.LineBasicMaterial({color:255}),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,i=new THREE.TextureLoader,r=new THREE.TextureLoader,s=new THREE.TextureLoader;e.load(TEXTURE_WALL,function(e){e.minFilter=THREE.LinearFilter,wall_texture=e,asynchFinished()&&initScene()}),i.load(TEXTURE_AGENT,function(e){e.minFilter=THREE.LinearFilter,agent_texture=e,asynchFinished()&&initScene()}),r.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),s.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,i){return ei==e&&ej==i||(ai==e&&aj==i||(GRID[e][i]==GRID_WALL||GRID[e][i]==GRID_MAZE))}function occupied_block(e,i){return GRID[e][i]==GRID_WALL||GRID[e][i]==GRID_MAZE}function translate(e,i){var r=new THREE.Vector3;return r.y=0,r.x=e*squaresize-MAXPOS/2,r.z=i*squaresize-MAXPOS/2,r}function initScene(){var e,i,r,s;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(i=0;i<gridsize;i++)0==e||e==gridsize-1||0==i||i==gridsize-1?(GRID[e][i]=GRID_WALL,r=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(s=new THREE.Mesh(r)).material=new THREE.MeshBasicMaterial({map:wall_texture}),s.position.copy(translate(e,i)),ABWorld.scene.add(s)):GRID[e][i]=GRID_BLANK;for(var t=1;t<=NOBOXES;t++)e=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2),GRID[e][i]=GRID_MAZE,r=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(s=new THREE.Mesh(r)).material=new THREE.MeshBasicMaterial({map:maze_texture}),s.position.copy(translate(e,i)),ABWorld.scene.add(s);do{e=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,i));ei=e,ej=i,r=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(r)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{e=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,i));ai=e,aj=i,r=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(r)).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 heuristic(e,i){return Math.sqrt((e.i-i.i)*(e.i-i.i)+(e.j-i.j)*(e.j-i.j))}function removeFromArray(e,i){for(var r=e.length-1;r>=0;r--)e[r]==i&&e.splice(r,1)}function astar(e,i,r,s){for(var t,n,o=new Array(gridsize),a=[],u=[],d=[],g=0;g<gridsize;g++)o[g]=new Array(gridsize);for(g=0;g<gridsize;g++)for(var c=0;c<gridsize;c++)o[g][c]=new h(g,c);for(g=0;g<gridsize;g++)for(c=0;c<gridsize;c++)o[g][c].addNeighbors(o);function h(e,i){this.i=e,this.j=i,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.addNeighbors=function(r){e<gridsize-1&&this.neighbors.push(r[e+1][i]),e>0&&this.neighbors.push(r[e-1][i]),i<gridsize-1&&this.neighbors.push(r[e][i+1]),i>0&&this.neighbors.push(r[e][i-1]),e>0&&i>0&&this.neighbors.push(r[e-1][i-1]),e<gridsize-1&&i>0&&this.neighbors.push(r[e+1][i-1]),e>0&&i<gridsize-1&&this.neighbors.push(r[e-1][i+1]),e<gridsize-1&&i<gridsize-1&&this.neighbors.push(r[e+1][i+1])},this.previous=void 0}for(t=o[e][i],n=o[r][s],a.push(t);a.length>0;){var p=0;for(g=0;g<a.length;g++)a[g].f<a[p].f&&(p=g);var A=a[p];if(A.i===r&&A.j===s)break;removeFromArray(a,A),u.push(A);var l=A.neighbors;for(g=0;g<l.length;g++){var E=l[g];if(!u.includes(E)&&!occupied_block(E.i,E.j)){var f=A.g+heuristic(E,A),B=!1;a.includes(E)?f<E.g&&(E.g=f,B=!0):(E.g=f,B=!0,a.push(E)),B&&(E.h=heuristic(E,n),E.f=E.g+E.h,E.previous=A)}}}var R=A;for((d=[]).push(R);R.previous;)d.push(R.previous),R=R.previous;return d.reverse()[1]}function astar_agent(e,i,r,s){for(var t,n,o=new Array(gridsize),a=[],u=[],d=[],g=0;g<gridsize;g++)o[g]=new Array(gridsize);for(g=0;g<gridsize;g++)for(var c=0;c<gridsize;c++)o[g][c]=new h(g,c);for(g=0;g<gridsize;g++)for(c=0;c<gridsize;c++)o[g][c].addNeighbors(o);function h(e,i){this.i=e,this.j=i,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.addNeighbors=function(r){e<gridsize-1&&this.neighbors.push(r[e+1][i]),e>0&&this.neighbors.push(r[e-1][i]),i<gridsize-1&&this.neighbors.push(r[e][i+1]),i>0&&this.neighbors.push(r[e][i-1]),e>0&&i>0&&this.neighbors.push(r[e-1][i-1]),e<gridsize-1&&i>0&&this.neighbors.push(r[e+1][i-1]),e>0&&i<gridsize-1&&this.neighbors.push(r[e-1][i+1]),e<gridsize-1&&i<gridsize-1&&this.neighbors.push(r[e+1][i+1])},this.previous=void 0}for(t=o[e][i],n=o[r][s],a.push(t);a.length>0;){var p=0;for(g=0;g<a.length;g++)a[g].f>a[p].f&&(p=g);var A=a[p];if(A.i===r&&A.j===s)break;removeFromArray(a,A),u.push(A);var l=A.neighbors;for(g=0;g<l.length;g++){var E=l[g];if(!u.includes(E)&&!occupied_block(E.i,E.j)){var f=A.g+heuristic(E,A),B=!1;a.includes(E)?f<E.g&&(E.g=f,B=!0):(E.g=f,B=!0,a.push(E)),B&&(E.h=heuristic(E,n),E.f=E.g+E.h,E.previous=A)}}}var R=A;for((d=[]).push(R);R.previous;)d.push(R.previous),R=R.previous;return d.reverse()[1]}function moveLogicalEnemy(){var e,i;e_next=astar(ei,ej,ai,aj),e=e_next.i,i=e_next.j,last_step_i=ei,last_step_j=ej,occupied(e,i)||(ei=e,ej=i)}function moveLogicalAgent(e){var i,r;a_next=astar_agent(ai,aj,ei,ej),occupied(i=a_next.i,r=a_next.j)||(ai=i,aj=r)}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 i=AB.world.getState();AB.msg(" Step: "+AB.step+" &nbsp; x = ("+i.toString()+") &nbsp; a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),i=goodsteps/AB.step*100;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+i.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(MUSIC_BACK);function musicPlay(){backmusic.play()}function musicPause(){backmusic.pause()}function soundAlarm(){new Audio(SOUND_ALARM).play()}