Code viewer for World: Complex World A* Test

// Cloned by arpit on 9 Nov 2022 from World "Complex World A*" by arpit 
// 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/axd10/password-infinity-123276.mp3",SOUND_ALARM="/uploads/axd10/ToBeContinuedSoundEffectPerfectCut.mp3",gridsize=40,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/axd10/Milky_Way_Arch.jpg","/uploads/axd10/Milky_Way_Arch.jpg","/uploads/axd10/Milky_Way_Arch.jpg","/uploads/axd10/Milky_Way_Arch.jpg","/uploads/axd10/Hyades.jpg","/uploads/axd10/Hyades.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,GRID_ENEMY=10,GRID_AGENT=20;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,GRID=new Array(gridsize);class coordinates{constructor(e,t){this.i=e,this.j=t}}var start=new coordinates,target=new coordinates;const MAX_OPENSET=gridsize*gridsize;var badsteps,goodsteps;const PROOF=!0;var proofArray=[];function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,a=new THREE.TextureLoader,r=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()}),a.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),r.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]==GRID_WALL||GRID[e][t]==GRID_MAZE))}function iswall(e,t){switch(GRID[e][t]){case GRID_WALL:case GRID_MAZE:return!0;default:return!1}}function translate(e,t){var a=new THREE.Vector3;return a.y=0,a.x=e*squaresize-MAXPOS/2,a.z=t*squaresize-MAXPOS/2,a}function initScene(){var e,t,a,r;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]=GRID_WALL,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:wall_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r)):GRID[e][t]=GRID_BLANK;for(var i=1;i<=NOBOXES;i++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:maze_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,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),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ai=e,aj=t,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),GRID[ai][aj]=GRID_AGENT,GRID[ei][ej]=GRID_ENEMY,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 gridSpot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.parent=void 0,this.closed=!1,this.addNeighbors=function(){var e=this.i,t=this.j;e<gridsize-1&&this.neighbors.push(aStarGrid[e+1][t]),e>0&&this.neighbors.push(aStarGrid[e-1][t]),t<gridsize-1&&this.neighbors.push(aStarGrid[e][t+1]),t>0&&this.neighbors.push(aStarGrid[e][t-1])}}var aStarGrid=[];function aStarInit(){var e,t;for(e=0;e<gridsize;e++)for(aStarGrid[e]=[],t=0;t<gridsize;t++)aStarGrid[e][t]=new gridSpot(e,t);for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)aStarGrid[e][t].addNeighbors()}function aStar(e,t,a){aStarInit();var r,o,s=[],d=!1,u=aStarGrid[a.i][a.j],c=aStarGrid[t.i][t.j],A=new coordinates(t.i,t.j);for(s.push(c),console.info("cleaning up previous results"),i=0;i<gridsize;i++)for(j=0;j<gridsize;j++)(o=aStarGrid[i][j]).f=o.h=o.g=0,o.closed=!1,o.parent=null;for(;s.length>0&&!d;){if(s.length>MAX_OPENSET)return console.error("there is a problem with the openset, bail out!"),d=!0,A;if(s.sort((e,t)=>e.f>t.f?1:-1),(o=s.shift()).closed=!0,console.info("Current: "+o.i+", "+o.j+" - (O/C) is "+s.length),o==u){for(PROOF&&proofArray.push(u);o.parent!=c;)PROOF&&proofArray.push(o),o=o.parent;d=!0,PROOF&&proofArray.push(c)}else for(n=0;n<o.neighbors.length;n++)(r=o.neighbors[n]).closed||(iswall(r.i,r.j)?r.closed=!0:(r.i==o.i||r.j==o.j?r.g=o.g+10:r.g=o.g+14,r.h=aStarHeuristics(r,u),r.f=r.g+r.h,r.parent=o,s.indexOf(r)<0&&s.push(r)))}return A.i=o.i,A.j=o.j,console.info("returning form a*: "),A}function aStarHeuristics(e,t){return Math.abs(e.i-e.i)+Math.abs(e.j-t.j)}function moveLogicalEnemy(){start.i=ei,start.j=ej,target.i=ai,target.j=aj,bestPath=aStar(GRID,start,target),occupied(bestPath.i,bestPath.j)||(GRID[ei][ej]=GRID_BLANK,ei=bestPath.i,ej=bestPath.j,GRID[ei][ej]=GRID_ENEMY),console.info("enemy moving to : "+ei+","+ej)}function moveLogicalAgent(e){var t=ai,a=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?a++:e==ACTION_DOWN&&a--,occupied(t,a)||(ai=t,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)<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]},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()}