Code viewer for World: Can the developer fix the ...

// Cloned by Daniel Peres on 13 Jul 2020 from World "Can the developer fix the bug? " by Daniel Peres 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=9999,AB.screenshotStep=50;const show3d=!1,TEXTURE_WALL="/uploads/developdaniels/code.png",TEXTURE_MAZE="/uploads/developdaniels/error.jpg",TEXTURE_AGENT="/uploads/developdaniels/bug.png",TEXTURE_ENEMY="/uploads/developdaniels/developer.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=20,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/developdaniels/backgroundcode.jpg","/uploads/developdaniels/backgroundcode.jpg","/uploads/developdaniels/backgroundcode.jpg","/uploads/developdaniels/backgroundcode.jpg","/uploads/developdaniels/backgroundcode.jpg","/uploads/developdaniels/backgroundcode.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,enemy,agent,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize),SpotGrid=new Array(gridsize),openSet=[],closedSet=[],path=[],diagonal=!0,autoTrap=!0,keepAllPathsDone=!0;function dist(e,t,o,i){var r=Math.abs(e-o),a=Math.abs(t-i);return Math.sqrt(r*r+a*a)}function heuristic(e,t){return dist(e.i,e.j,t.i,t.j)}function removeFromArray(e,t){for(var o=e.length-1;o>=0;o--)e[o]==t&&e.splice(o,1)}function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,o=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()}),o.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]==GRID_WALL||GRID[e][t]==GRID_MAZE))}function translate(e,t){var o=new THREE.Vector3;return o.y=0,o.x=e*squaresize-MAXPOS/2,o.z=t*squaresize-MAXPOS/2,o}function Spot(e,t){this.i=e,this.j=t,this.wall=!1,this.neighbors=[],this.f=0,this.g=0,this.h=0,this.previous=void 0,this.addNeighbors=function(){var e=this.i,t=this.j;e<gridsize-1&&this.neighbors.push(SpotGrid[e+1][t]),e>0&&this.neighbors.push(SpotGrid[e-1][t]),t<gridsize-1&&this.neighbors.push(SpotGrid[e][t+1]),t>0&&this.neighbors.push(SpotGrid[e][t-1]),diagonal&&(e>0&&t>0&&this.neighbors.push(SpotGrid[e-1][t-1]),e<gridsize-1&&t>0&&this.neighbors.push(SpotGrid[e+1][t-1]),e>0&&t<gridsize-1&&this.neighbors.push(SpotGrid[e-1][t+1]),e<gridsize-1&&t<gridsize-1&&this.neighbors.push(SpotGrid[e+1][t+1]))}}function initScene(){var e,t;for(SpotGrid=new Array(gridsize),i=0;i<gridsize;i++)GRID[i]=new Array(gridsize),SpotGrid[i]=new Array(gridsize);for(i=0;i<gridsize;i++)for(r=0;r<gridsize;r++)SpotGrid[i][r]=new Spot(i,r),0==i||i==gridsize-1||0==r||r==gridsize-1?(GRID[i][r]=GRID_WALL,SpotGrid[i][r].wall=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(i,r)),ABWorld.scene.add(t)):(GRID[i][r]=GRID_BLANK,SpotGrid[i][r].wall=!1);for(var o=1;o<=NOBOXES;o++)i=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2),GRID[i][r]=GRID_MAZE,SpotGrid[i][r].wall=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(i,r)),ABWorld.scene.add(t);for(var i=0;i<gridsize;i++)for(var r=0;r<gridsize;r++)SpotGrid[i][r].addNeighbors();console.log("start app"),console.log(SpotGrid);do{i=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2)}while(occupied(i,r));ei=i,ej=r,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{i=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2)}while(occupied(i,r));ai=i,aj=r,SetupAStar(),e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(e)).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 SetupAStar(){for(var e=0;e<gridsize;e++)for(var t=0;t<gridsize;t++)SpotGrid[e][t].f=0,SpotGrid[e][t].g=0,SpotGrid[e][t].h=0,SpotGrid[e][t].previous=void 0;closedSet=[],openSet=[]}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 isTraped(e,t){var o=0;return SpotGrid[e][t-1].wall&&o++,SpotGrid[e-1][t].wall&&o++,SpotGrid[e+1][t].wall&&o++,SpotGrid[e][t+1].wall&&o++,3==o}function trapDoorLocation(e,t){return SpotGrid[e][t-1].wall?SpotGrid[e-1][t].wall?SpotGrid[e+1][t].wall?SpotGrid[e][t+1].wall?void 0:SpotGrid[e][t+1]:SpotGrid[e+1][t]:SpotGrid[e-1][t]:SpotGrid[e][t-1]}function moveLogicalEnemy(){var e;enemy=SpotGrid[ei][ej],agent=SpotGrid[ai][aj],SetupAStar();var t=isTraped(agent.i,agent.j);if(t){console.log("Agent is trapped, RUN to the trap door!");var o=trapDoorLocation(agent.i,agent.j);e=getAStarPath(enemy,o)}else e=getAStarPath(enemy,agent);if(0==e.length||1==e.length)console.log("Illegal move?");else if(2==e.length){if(t&&autoTrap){var i=e[1];ei=i.i,ej=i.j}}else{i=e[1];ei=i.i,ej=i.j}return e}function moveLogicalAgent(e){var t=ai,o=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?o++:e==ACTION_DOWN&&o--,occupied(t,o)||(ai=t,aj=o),SpotGrid[ai][aj].wall=!1}var pathDraw,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+"   x = ("+t.toString()+")   a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),t=goodsteps/AB.step*100;AB.msg("   y = ("+e.toString()+") <br> Bad steps: "+badsteps+"   Good steps: "+goodsteps+"   Score: "+t.toFixed(2)+"% ",2)}function getAStarPath(e,t){for(openSet.push(e);openSet.length>0;){for(var o=0,i=0;i<openSet.length;i++)openSet[i].f<openSet[o].f&&(o=i);var r=openSet[o];if(removeFromArray(openSet,r),closedSet.push(r),r===t)return console.log("success - path found"),printPath(r),getPath(r);var a=r.neighbors;for(i=0;i<a.length;i++){var n=a[i];if(!closedSet.includes(n)&&!n.wall){var s=r.g+heuristic(n,r),d=!1,u=!1;openSet.includes(n)?s<n.g&&(n.g=s,d=!0):(n.g=s,u=!0),(d||u)&&(n.h=heuristic(n,agent),n.f=n.g+n.h,n.previous=r),openSet.includes(n)||openSet.push(n)}}}}function printPath(e){var t=e;for((path=[]).push(t);t.previous;)path.push(t.previous),t=t.previous;var o="";for(i=path.length-1;i>=0;i--)o=o+"("+path[i].i+","+path[i].j+")";console.log(o)}function getPath(e){var t=e;for((path=[]).push(t);t.previous;)path.push(t.previous),t=t.previous;var o=[];for(i=path.length-1;i>=0;i--)o.push(path[i]);return o}AB.world.newRun=function(){AB.loadingScreen(),AB.runReady=!1,badsteps=0,goodsteps=0,BOXHEIGHT=1,ABWorld.init2d(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);var t=moveLogicalEnemy(),o=new THREE.Geometry;for(i=1;i<t.length;i++)o.vertices.push(translate(t[i].i,t[i].j));var r=new THREE.LineBasicMaterial({color:16777215}),a=new THREE.Line(o,r);keepAllPathsDone||ABWorld.scene.remove(pathDraw),pathDraw=a,ABWorld.scene.add(a),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()}