Code viewer for World: Kick Panda - A Star Algori...

// Cloned by Shamsul Abedin on 13 Jul 2020 from World "Kick Panda - A Star Algorithm" by Zhensheng Tan 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const heuristicModifier=0,allowEnemyDiagonalMove=!0;var openSetMin=Number.MAX_VALUE,openSetMax=Number.MIN_VALUE,openSetTotal=0,searchRun=0,openSetMoveRatioMin=Number.MAX_VALUE,openSetMoveRatioMax=Number.MIN_VALUE,moveTotal=0;const show3d=!0,TEXTURE_WALL="/uploads/zhensheng/heuristic_assignment_material_bamboo.jpg",TEXTURE_MAZE="/uploads/zhensheng/heuristic_assignment_material_yinyang.png",TEXTURE_AGENT="/uploads/zhensheng/heuristic_assignment_material_panda.png",TEXTURE_ENEMY="/uploads/zhensheng/heuristic_assignment_material_boss.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=30,NOBOXES=Math.trunc(gridsize*gridsize/10),actualMazeSize=(gridsize-1)*(gridsize-1)-NOBOXES,squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/zhensheng/heuristic_assignment_material_forbidden_city_posx.jpg","/uploads/zhensheng/heuristic_assignment_material_forbidden_city_negx.jpg","/uploads/zhensheng/heuristic_assignment_material_forbidden_city_posy.jpg","/uploads/zhensheng/heuristic_assignment_material_forbidden_city_negy.jpg","/uploads/zhensheng/heuristic_assignment_material_forbidden_city_posz.jpg","/uploads/zhensheng/heuristic_assignment_material_forbidden_city_negz.jpg"],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,chasingGrid,GRID=new Array(gridsize);function removeFromArray(e,i){for(var t=e.length-1;t>=0;t--)e[t]==i&&e.splice(t,1)}function Spot(e,i,t){this.i=e,this.j=i,this.f=0,this.g=0,this.h=0,this.isWall=GRID[e][i]===GRID_WALL||GRID[e][i]===GRID_MAZE,this.neighbors=[],this.previous=void 0,this.addNeighbors=function(e){var i=this.i,t=this.j;i<gridsize-1&&this.neighbors.push(e[i+1][t]),i>0&&this.neighbors.push(e[i-1][t]),t<gridsize-1&&this.neighbors.push(e[i][t+1]),t>0&&this.neighbors.push(e[i][t-1]),allowEnemyDiagonalMove&&(i>0&&t>0&&this.neighbors.push(e[i-1][t-1]),i<gridsize-1&&t>0&&this.neighbors.push(e[i+1][t-1]),i>0&&t<gridsize-1&&this.neighbors.push(e[i-1][t+1]),i<gridsize-1&&t<gridsize-1&&this.neighbors.push(e[i+1][t+1]))}}function heuristic(e,i){return allowEnemyDiagonalMove?Math.sqrt((e.i-i.i)*(e.i-i.i)+(e.j-i.j)*(e.j-i.j)):Math.abs(e.i-i.i)+Math.abs(e.j-i.j)}function loadResources(){var e=new THREE.TextureLoader,i=new THREE.TextureLoader,t=new THREE.TextureLoader,n=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()}),t.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 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 translate(e,i){var t=new THREE.Vector3;return t.y=0,t.x=e*squaresize-MAXPOS/2,t.z=i*squaresize-MAXPOS/2,t}function initScene(){var e,i;for(n=0;n<gridsize;n++)GRID[n]=new Array(gridsize);for(n=0;n<gridsize;n++)for(a=0;a<gridsize;a++)0==n||n==gridsize-1||0==a||a==gridsize-1?(GRID[n][a]=GRID_WALL,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),i.position.copy(translate(n,a)),ABWorld.scene.add(i)):GRID[n][a]=GRID_BLANK;for(var t=1;t<=NOBOXES;t++)n=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2),GRID[n][a]=GRID_MAZE,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),i.position.copy(translate(n,a)),ABWorld.scene.add(i);do{n=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(n,a));ei=n,ej=a,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{n=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(n,a));ai=n,aj=a,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),chasingGrid=new Array(gridsize);for(var n=0;n<gridsize;n++)chasingGrid[n]=new Array(gridsize);for(n=0;n<gridsize;n++)for(var a=0;a<gridsize;a++)chasingGrid[n][a]=new Spot(n,a,chasingGrid);for(n=0;n<gridsize;n++)for(a=0;a<gridsize;a++)chasingGrid[n][a].addNeighbors(chasingGrid);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=[],i=[],t=0,n=chasingGrid[ei][ej],a=chasingGrid[ai][aj];for(n.isWall=!1,a.isWall=!1,e.push(n);e.length>0;){t++;for(var o=0,r=0;r<e.length;r++)e[r].f<e[o].f&&(o=r);var s;if((s=e[o])===a)break;removeFromArray(e,s),i.push(s);var d=s.neighbors;for(r=0;r<d.length;r++){var u=d[r];if(!i.includes(u)&&!u.isWall){var c=s.g+heuristicModifier*heuristic(u,s),g=!1;e.includes(u)?c<u.g&&(u.g=c,g=!0):(u.g=c,g=!0,e.push(u)),g&&(u.h=heuristicModifier*heuristic(u,a),u.f=u.g+u.h,u.previous=s)}}}var l=1;if(s!==a)console.log("No A* Path Found, Stay here!");else{for(var h=s;h.previous!==n;)l++,h=h.previous;occupied(h.i,h.j)||(ei=h.i,ej=h.j)}badstep()||(openSetMin=Math.min(openSetMin,t),openSetMax=Math.max(openSetMax,t),openSetTotal+=t,searchRun++,openSetMoveRatioMin=Math.min(openSetMoveRatioMin,t/l),openSetMoveRatioMax=Math.max(openSetMoveRatioMax,t/l),moveTotal+=l)}function moveLogicalAgent(e){var i=ai,t=aj;e==ACTION_LEFT?i--:e==ACTION_RIGHT?i++:e==ACTION_UP?t++:e==ACTION_DOWN&&t--,occupied(i,t)||(ai=i,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 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,t=actualMazeSize*searchRun;AB.msg(" &nbsp; y = ("+e.toString()+") <br> OverallOpenSetMin% ("+(100*openSetMin/actualMazeSize).toFixed(2)+") &nbsp; OveraOpenSetMax% ("+(100*openSetMax/actualMazeSize).toFixed(2)+")  &nbsp; AverageOpenSet% ("+(100*openSetTotal/t).toFixed(2)+") <br>  OverallOpenSetMoveRatioMin ("+openSetMoveRatioMin.toFixed(2)+") &nbsp; OverallOpenSetMoveRatioMax ("+openSetMoveRatioMax.toFixed(3)+")  &nbsp; AverageOpenSetMovieRatio ("+(openSetTotal/moveTotal).toFixed(2)+")  <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()}