Code viewer for World: Pac World (clone by test)

// Cloned by test on 12 Nov 2019 from World "Pac World" by J Faj 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/jfajou/TronCircuitry.jpg",TEXTURE_MAZE="/uploads/jfajou/TronVector.jpg",TEXTURE_AGENT="/uploads/starter/pacman.jpg",TEXTURE_ENEMY="/uploads/jfajou/Ghost.jpg",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=20,NOBOXES=Math.trunc(gridsize*gridsize/30),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"],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,startNode,endNode,GRID=new Array(gridsize),trappedSet=[],winnerSet=[],isASolution=!1,grid=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,i=new THREE.TextureLoader,o=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()}),i.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),o.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 i=new THREE.Vector3;return i.y=0,i.x=e*squaresize-MAXPOS/2,i.z=t*squaresize-MAXPOS/2,i}function initScene(){var e,t;for(o=0;o<gridsize;o++)GRID[o]=new Array(gridsize);for(o=0;o<gridsize;o++)for(r=0;r<gridsize;r++)0==o||o==gridsize-1||0==r||r==gridsize-1?(GRID[o][r]=GRID_WALL,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(o,r)),ABWorld.scene.add(t)):GRID[o][r]=GRID_BLANK;for(var i=1;i<=NOBOXES;i++)o=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2),GRID[o][r]=GRID_MAZE,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(o,r)),ABWorld.scene.add(t);do{o=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2)}while(occupied(o,r));ei=o,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{o=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2)}while(occupied(o,r));ai=o,aj=r,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});for(var o=0;o<gridsize;o++)grid[o]=new Array(gridsize);for(o=0;o<gridsize;o++)for(var r=0;r<gridsize;r++)grid[o][r]=new Node(o,r);for(o=0;o<gridsize;o++)for(r=0;r<gridsize;r++)grid[o][r].addNeighbors(grid),1==grid[o][r].canBeStoppedHere&&(isASolution=!0);0==isASolution&&(AB.abortRun=!0,console.log("There is no square available for the agent to be trapped, so we can finish here!"))}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 Node(e,t){this.i=e,this.j=t,this.g=0,this.h=0,this.f=this.g+this.h,this.neighbors=[],this.previous=void 0,GRID[e][t]==GRID_BLANK?this.wall=!1:this.wall=!0,this.canBeStoppedHere=!1,this.addNeighbors=function(e){var t,i=this.i,o=this.j,r=0;i<gridsize-1&&(this.neighbors.push(e[i+1][o]),1==e[i+1][o].wall?r++:t=e[i+1][o]),i>0&&(this.neighbors.push(e[i-1][o]),1==e[i-1][o].wall?r++:t=e[i-1][o]),o<gridsize-1&&(this.neighbors.push(e[i][o+1]),1==e[i][o+1].wall?r++:t=e[i][o+1]),o>0&&(this.neighbors.push(e[i][o-1]),1==e[i][o-1].wall?r++:t=e[i][o-1]),3==r&&0==e[i][o].wall&&(console.log("***Can be stopped at "+i+", "+o),this.canBeStoppedHere=!0,trappedSet.push(e[i][o]),winnerSet.push(t)),i>0&&o>0&&this.neighbors.push(e[i-1][o-1]),i<gridsize-1&&o>0&&this.neighbors.push(e[i+1][o-1]),i>0&&o<gridsize-1&&this.neighbors.push(e[i-1][o+1]),i<gridsize-1&&o<gridsize-1&&this.neighbors.push(e[i+1][o+1])}}function getDist(e,t){var i=Math.pow(e.i-t.i,2)+Math.pow(e.j-t.j,2);return Math.sqrt(i)}function moveLogicalEnemy(){var e,t=[],i=[];for(startNode=grid[ei][ej],endNode=grid[ai][aj],t.push(startNode);t.length>0;){for(var o=0,r=0;r<t.length;r++)t[r].f<t[o].f&&(o=r);if((e=t[o])==endNode)break;for(r=t.length-1;r>=0;r--)t[r]==e&&t.splice(r,1);i.push(e);var n=e.neighbors;for(r=0;r<n.length;r++){var a=n[r];if(!i.includes(a)&&!a.wall){var s=e.g+getDist(a,e),d=!1;t.includes(a)?s<a.g&&(a.g=s,d=!0):(a.g=s,d=!0,t.push(a)),d&&(a.h=getDist(a,endNode),a.previous=e)}}}for(var u=e;u.previous&&(0,u.previous!=startNode);)u=u.previous;if(u!==endNode)ei=u.i,ej=u.j;else if(trappedSet.includes(endNode)){var g=winnerSet[trappedSet.indexOf(endNode)];startNode.neighbors.includes(g)?(console.log("*****In the TRAP!!!!"),console.log("Winner location "+trappedSet.indexOf(endNode)),ei=g.i,ej=g.j):moveToRandomNeighbour()}else moveToRandomNeighbour()}function moveToRandomNeighbour(){var e,t=startNode.neighbors;do{e=AB.randomIntAtoB(0,t.length-1)}while(occupied(t[e].i,t[e].j));ei=t[e].i,ej=t[e].j,console.log("New end Node is "+ei+","+ej)}function moveLogicalAgent(e){var t=ai,i=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?i++:e==ACTION_DOWN&&i--,occupied(t,i)||(ai=t,aj=i)}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(),0==isASolution?AB.msg(" <br> <font color=red> <B> The Agent cannot be trapped. The game has ended. </B> </font>   ",3):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()}