Code viewer for World: Complex World clone: The A...

// Cloned by Shamsul Abedin on 13 Jul 2020 from World "Complex World clone: The A* Terminator -v- Daniel O'Donnell " by K.Ellis 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=500,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/ke737/ivy-hedging.jpg",TEXTURE_MAZE="/uploads/ke737/grass.jpg",TEXTURE_AGENT="/uploads/ke737/danOdonnell.jpg",TEXTURE_ENEMY="/uploads/ke737/terminator22.jpg",MUSIC_BACK="/uploads/ke737/CARBONKILLER-Terminator2theme.mp3",SOUND_ARNOLD="/uploads/ke737/t2_hasta_la_vista.wav",SOUND_BBACK="/uploads/ke737/t1_be_back.wav",SOUND_TERM="/uploads/ke737/t2_terminator.wav",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/starter/dawnmountain-xpos.png","/uploads/starter/dawnmountain-xneg.png","/uploads/starter/dawnmountain-ypos.png","/uploads/starter/dawnmountain-yneg.png","/uploads/starter/dawnmountain-zpos.png","/uploads/starter/dawnmountain-zneg.png"],ACTION_LEFT=0,ACTION_RIGHT=1,ACTION_UP=2,ACTION_DOWN=3,ACTION_STAYSTILL=4,GRID_BLANK=0,GRID_WALL=1,GRID_MAZE=2,diagonal=!0;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize),openSet=[],closedSet=[],counter1=0,counter2=0;function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,n=new THREE.TextureLoader,a=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()}),n.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),a.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 n=new THREE.Vector3;return n.y=0,n.x=e*squaresize-MAXPOS/2,n.z=t*squaresize-MAXPOS/2,n}function heuristic(e,t){return Math.abs(t.i-e.i)+Math.abs(t.j-e.j)}function removeFromArray(e,t){for(var n=e.length-1;n>=0;n--)e[n]==t&&e.splice(n,1)}function Spot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.previous=void 0,GRID[e][t]==GRID_WALL?(this.wall=!0,counter1++):this.wall=!1,GRID[e][t]==GRID_MAZE?(this.maze=!0,counter2++):this.maze=!1,this.addNeighbors=function(e){var t=this.i,n=this.j;t<gridsize-1&&this.neighbors.push(e[t+1][n]),t>0&&this.neighbors.push(e[t-1][n]),n<gridsize-1&&this.neighbors.push(e[t][n+1]),n>0&&this.neighbors.push(e[t][n-1]),diagonal&&(t>0&&n>0&&this.neighbors.push(e[t-1][n-1]),t<gridsize-1&&n>0&&this.neighbors.push(e[t+1][n-1]),t>0&&n<gridsize-1&&this.neighbors.push(e[t-1][n+1]),t<gridsize-1&&n<gridsize-1&&this.neighbors.push(e[t+1][n+1]))}}function aStar(){if(openSet.length>0){for(var e=0,t=0;t<openSet.length;t++)openSet[t].f<openSet[e].f&&(e=t);var n=openSet[e];console.log(" [[ Terminator ]]  ["+n.i+"]  ["+n.j+"]  --- f: "+n.f+" g:"+n.g+" h: "+n.h),n==target&&(ei=n.i,ej=n.j,theenemy.material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy(ei,ej),AB.abortRun=!0,console.log(" Daniel has been Terminated"),AB.msg(" <font color=red> <B> Daniel has been Terminated </B> </font> <br>But his dynamic dancing can win, the A* is an old model",3),soundArnold()),removeFromArray(openSet,n),closedSet.push(n);var a=n.neighbors;for(t=0;t<a.length;t++){var o=a[t];if(!closedSet.includes(o)&&!o.wall&&!o.maze){var i=n.g+heuristic(o,n),r=!1;openSet.includes(o)?i<o.g&&(o.g=i,r=!0):(o.g=i,r=!0,openSet.push(o)),r&&(o.h=heuristic(o,target),o.f=o.g+o.h,ei=o.i,ej=o.j,o.previous=n)}}}else console.log(" Daniel dodged the Terminator this time!!"),AB.msg(" <font color=green> <B> Daniel dodged the Terminator this time!! </B> </font> <br>His dynamic dancing confuses the A* Terminator <br> Hopefully the D* Terminator is not sent back!!  ",3),soundBback(),AB.abortRun=!0}function soundArnold(){new Audio(SOUND_ARNOLD).play()}function soundBback(){new Audio(SOUND_BBACK).play()}function soundTerminator(){new Audio(SOUND_TERM).play()}function initScene(){var e,t;for(a=0;a<gridsize;a++)GRID[a]=new Array(gridsize);for(a=0;a<gridsize;a++)for(o=0;o<gridsize;o++)0===a||a==gridsize-1||0===o||o==gridsize-1?(GRID[a][o]=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(a,o)),ABWorld.scene.add(t)):GRID[a][o]=GRID_BLANK;for(var n=1;n<=NOBOXES;n++)a=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2),GRID[a][o]=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(a,o)),ABWorld.scene.add(t);do{a=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2)}while(occupied(a,o));ei=a,ej=o,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{a=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2)}while(occupied(a,o));ai=a,aj=o,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent();for(var a=0;a<gridsize;a++)for(var o=0;o<gridsize;o++)GRID[a][o]=new Spot(a,o);for(a=0;a<gridsize;a++)for(o=0;o<gridsize;o++)GRID[a][o].addNeighbors(GRID);console.log(GRID),start=GRID[ei][ej],target=GRID[ai][aj],console.log("A* Terminator start = "+start.i+", "+start.j),soundTerminator(),console.log("Daniel O'D start = "+target.i+", "+target.j),console.log("num of boundary walls "+counter1+", num of maze walls "+counter2),start.wall=!1,start.maze=!1,target.wall=!1,target.maze=!1,openSet.push(start),console.log(start),console.log(openSet),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(){aStar()}function moveLogicalAgent(e){var t=ai,n=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?n++:e==ACTION_DOWN&&n--,GRID[t][n].wall||GRID[t][n].maze||(ai=t,aj=n,target=GRID[t][n],console.log("Target: "+target.i+", "+target.j))}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){AB.world.getState()}function updateStatusAfter(){AB.world.getState();var e=AB.step/289*100,t=100-e;AB.msg(" &nbsp;A* Terminator Score: "+t.toFixed(2)+"%  &nbsp;Daniel's Score: "+e.toFixed(2)+"% <br>",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),moveLogicalEnemy(),badstep()?badsteps++:goodsteps++,drawAgent(),drawEnemy(),updateStatusAfter(),agentBlocked()&&(AB.abortRun=!0,goodsteps=0,musicPause(),soundArnold())},AB.world.endRun=function(){musicPause(),AB.abortRun?AB.abortRun=!0:(AB.msg(" <br> <font color=green> <B> Daniel beat the Terminator. </B> </font>   ",3),soundBback())},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()}