Code viewer for World: Tom and Jerry in Complex W...

// Cloned by rohan on 13 Nov 2021 from World "Tom and Jerry in Complex World" by bo 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/bow2/Spike.png",TEXTURE_MAZE="/uploads/bow2/shrubs-trees.jpg",TEXTURE_AGENT="/uploads/bow2/Jerry.png",TEXTURE_ENEMY="/uploads/bow2/Tom.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=50,NOBOXES=Math.trunc(gridsize*gridsize/3);console.log("Box density"+NOBOXES);const squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/starter/posx.jpg","/uploads/starter/negx.jpg","/uploads/starter/posy.jpg","/uploads/starter/negy.jpg","/uploads/starter/posz.jpg","/uploads/starter/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,GRID_ENEMY=3,GRID_AGENT=4;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,t=new THREE.TextureLoader,r=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()}),r.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 GRID[e][t]==GRID_ENEMY||(GRID[e][t]==GRID_AGENT||(GRID[e][t]==GRID_WALL||GRID[e][t]==GRID_MAZE))}function translate(e,t){var r=new THREE.Vector3;return r.y=0,r.x=e*squaresize-MAXPOS/2,r.z=t*squaresize-MAXPOS/2,r}function initScene(){var e,t,r,i;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,r=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(r)).material=new THREE.MeshBasicMaterial({map:wall_texture}),i.position.copy(translate(e,t)),ABWorld.scene.add(i)):GRID[e][t]=GRID_BLANK;for(var a=1;a<=NOBOXES;a++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,r=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(r)).material=new THREE.MeshBasicMaterial({map:maze_texture}),i.position.copy(translate(e,t)),ABWorld.scene.add(i);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,console.log("Set up enemy"+ei+"**"+ej),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),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ai=e,aj=t,r=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(r)).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})}var diagonal=!0,showPath=!0,showPathArray=[];class coordinates{constructor(e,t){this.i=e,this.j=t}}var closedSet,openSet,start=new coordinates,target=new coordinates,aStarGrid=[];function aStarSpot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.parent=void 0,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]),diagonal&&(e>0&&t>0&&this.neighbors.push(aStarGrid[e-1][t-1]),e<gridsize-1&&t>0&&this.neighbors.push(aStarGrid[e+1][t-1]),e>0&&t<gridsize-1&&this.neighbors.push(aStarGrid[e-1][t+1]),e<gridsize-1&&t<gridsize-1&&this.neighbors.push(aStarGrid[e+1][t+1]))}}function initialAStarGrid(){var e,t;for(console.log("initialAStarGrid function"),e=0;e<gridsize;e++)for(aStarGrid[e]=[],t=0;t<gridsize;t++)aStarGrid[e][t]=new aStarSpot(e,t);for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)aStarGrid[e][t].addNeighbors()}function isOccupied(e,t){switch(GRID[e][t]){case GRID_WALL:case GRID_MAZE:return!0;default:return!1}}function sortFunction(e,t){return e.f===t.f?0:e.f>t.f?1:-1}function heuristic(e,t){return diagonal?Math.pow(e.i-t.i,2)+Math.pow(e.j-t.j,2):Math.abs(e.i-e.i)+Math.abs(e.j-t.j)}function nextMove(e,t){var r,a,o=aStarGrid[t.i][t.j],s=aStarGrid[e.i][e.j];openSet=[],closedSet=[];var d=new coordinates(e.i,e.j);for(openSet.push(s),i=0;i<gridsize;i++)for(j=0;j<gridsize;j++)(r=aStarGrid[i][j]).f=r.h=r.g=0,r.parent=null;for(;openSet.length>0&&r!==o;)for(openSet.sort(sortFunction),r=openSet.shift(),closedSet.push(r),n=0;n<r.neighbors.length;n++)a=r.neighbors[n],closedSet.includes(a)||(isOccupied(a.i,a.j)?closedSet.push(a):(a.i==r.i||a.j==r.j?a.g=r.g+10:a.g=r.g+14,a.h=heuristic(a,o),a.f=a.g+a.h,a.parent=r,openSet.includes(a)||openSet.push(a)));if(r!=o)return d;for(;r.parent!=s;)showPath&&showPathArray.push(r),r=r.parent;return showPath&&showPathArray.push(s),d.i=r.i,d.j=r.j,d}function drawPath(){var e,t,r=new THREE.LineBasicMaterial({color:"red",linewidth:2}),a=new THREE.Geometry;for(i=showPathArray.length;i>1;i--)(e=showPathArray.shift()).parent?(e=e.parent,a.vertices.push(translate(e.i,e.j))):a.vertices.push(translate(e.i,e.j));t=new THREE.Line(a,r),ABWorld.scene.add(t)}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(){start.i=ei,start.j=ej,target.i=ai,target.j=aj,nextMoveByAStar=nextMove(start,target),occupied(nextMoveByAStar.i,nextMoveByAStar.j)||(GRID[ei][ej]=GRID_BLANK,ei=nextMoveByAStar.i,ej=nextMoveByAStar.j,GRID[ei][ej]=GRID_ENEMY)}function moveLogicalAgent(e){var t=ai,r=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?r++:e==ACTION_DOWN&&r--,occupied(t,r)||(GRID[ai][aj]=GRID_BLANK,aj=r,GRID[ai=t][aj]=GRID_AGENT)}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,initialAStarGrid(),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()),showPath&&drawPath()},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()}