Code viewer for World: Final version 2

// Cloned by Mohamed Hafez on 24 Nov 2020 from World "Final version" by Mohamed Hafez 
// Please leave this clone trail here.
 
const enemyalgorithm=2,agentalgorithm=2,heuristicmethod=2;AB.clockTick=10,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/mhafez/redfence.jpg",TEXTURE_MAZE="/uploads/mhafez/flame.jpg",TEXTURE_AGENT="/uploads/mhafez/mouse.jpg",TEXTURE_ENEMY="/uploads/mhafez/cat.jpg",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=60,NOBOXES=Math.trunc(gridsize*gridsize/3),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;var BOXHEIGHT,theagent,theenemy,thepath,theagentpath,astarpath,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,start,end,GRID=new Array(gridsize),GRIDMIND=new Array(gridsize),fi=0,fim=0,openSet=[],closedSet=[],path=[],newpath2=[],previousei=0,previousej=0,previousai=0,previousaj=0,ariko=[];function heuristic(e,a){return 1==heuristicmethod?Math.abs(e.i-a.i)+Math.abs(e.j-a.j):2==heuristicmethod?Math.sqrt(Math.pow(e.i-a.i,2)+Math.pow(e.j-a.j,2)):3==heuristicmethod?0:void 0}function removeFromArray(e,a){for(var t=e.length-1;t>=0;t--)e[t]==a&&e.splice(t,1)}function Spot(e,a){this.i=e,this.j=a,this.value=0,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.previou=void 0,this.addNeighbors=function(e){var a=this.i,t=this.j;a<gridsize-1&&this.neighbors.push(e[a+1][t]),a>0&&this.neighbors.push(e[a-1][t]),t<gridsize-1&&this.neighbors.push(e[a][t+1]),t>0&&this.neighbors.push(e[a][t-1])}}function loadResources(){var e=new THREE.TextureLoader,a=new THREE.TextureLoader,t=new THREE.TextureLoader,i=new THREE.TextureLoader;e.load(TEXTURE_WALL,function(e){e.minFilter=THREE.LinearFilter,wall_texture=e,asynchFinished()&&initScene()}),a.load(TEXTURE_AGENT,function(e){e.minFilter=THREE.LinearFilter,agent_texture=e,asynchFinished()&&initScene()}),t.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,a){return ei==e&&ej==a||(ai==e&&aj==a||(GRID[e][a].value==GRID_WALL||GRID[e][a].value==GRID_MAZE))}function translate(e,a){var t=new THREE.Vector3;return t.y=0,t.x=e*squaresize-MAXPOS/2,t.z=a*squaresize-MAXPOS/2,t}function initScene(){var e,a;for(t=0;t<gridsize;t++)GRID[t]=new Array(gridsize),GRIDMIND[t]=new Array(gridsize);for(t=0;t<gridsize;t++)for(i=0;i<gridsize;i++)GRID[t][i]=new Spot(t,i),GRIDMIND[t][i]=new Spot(t,i);for(var t=0;t<gridsize;t++)for(var i=0;i<gridsize;i++)GRID[t][i].addNeighbors(GRID),GRIDMIND[t][i].addNeighbors(GRIDMIND);for(t=0;t<gridsize;t++)for(i=0;i<gridsize;i++)0==t||t==gridsize-1||0==i||i==gridsize-1?(GRID[t][i].value=GRID_WALL,GRIDMIND[t][i].value=GRID_WALL,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),a.position.copy(translate(t,i)),ABWorld.scene.add(a)):(GRID[t][i].value=GRID_BLANK,GRIDMIND[t][i].value=GRID_BLANK);for(var o=1;o<=NOBOXES;o++)t=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2),GRID[t][i].value=GRID_MAZE,GRIDMIND[t][i].value=GRID_MAZE,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),a.position.copy(translate(t,i)),ABWorld.scene.add(a);do{t=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(t,i));ei=t,ej=i,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy(),start=GRID[ei][ej];do{t=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(t,i));ai=t,aj=i,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),end=GRID[ai][aj],openSet.push(start),ABWorld.scene.background=(new THREE.CubeTextureLoader).load(SKYBOX_ARRAY,function(){ABWorld.render(),AB.removeLoading(),AB.runReady=!0})}function drawpath(e,a){shape=new THREE.BoxGeometry(squaresize,0,squaresize),(astarpath=new THREE.Mesh(shape)).material=new THREE.MeshBasicMaterial({color:255}),ABWorld.scene.add(astarpath),astarpath.position.copy(translate(e,a)),ABWorld.lookat.copy(astarpath.position)}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 drawenemypath(){shape=new THREE.BoxGeometry(squaresize,0,squaresize),(thepath=new THREE.Mesh(shape)).material=new THREE.MeshBasicMaterial({color:65280}),ABWorld.scene.add(thepath),thepath.position.copy(translate(previousei,previousej)),ABWorld.lookat.copy(thepath.position)}function drawagentpath(){shape=new THREE.BoxGeometry(squaresize,0,squaresize),(theagentpath=new THREE.Mesh(shape)).material=new THREE.MeshBasicMaterial({color:16711680}),ABWorld.scene.add(theagentpath),theagentpath.position.copy(translate(previousai,previousaj)),ABWorld.lookat.copy(theagentpath.position)}function moveLogicalEnemy(){if(2==enemyalgorithm){for(var e=0;e<ariko.length;e++)ABWorld.scene.remove(ariko[e]);for(openSet=[],closedSet=[],start=GRID[ei][ej],end=GRID[ai][aj],openSet.push(start),e=0;e<gridsize;e++)for(u=0;u<gridsize;u++)GRID[e][u].g=0,GRID[e][u].f=0,GRID[e][u].h=0,GRID[e][u].previou=void 0;for(;openSet.length>0;){var a=0;for(e=0;e<openSet.length;e++)openSet[e].f<openSet[a].f&&(a=e);var t=openSet[a];if(t===end){console.log("success - found path");break}removeFromArray(openSet,t),closedSet.push(t);var i=t.neighbors;for(e=0;e<i.length;e++){var o=i[e];if(!closedSet.includes(o)&&2!==o.value&&1!==o.value){var n=t.g+heuristic(o,t),r=!1;openSet.includes(o)?n<o.g&&(o.g=n,r=!0):(o.g=n,r=!0,openSet.push(o)),r&&(o.h=heuristic(o,end),o.f=o.g+o.h,o.previou=t)}}}var s=t;for((path=[]).push(s);s.previou;)path.push(s.previou),s=s.previou;e=path[path.length-2].i,u=path[path.length-2].j,previousei=ei,previousej=ej,occupied(e,u)||(ei=e,ej=u);for(var d=0;d<path.length-1;d++)drawpath(path[d].i,path[d].j),ariko.push(astarpath)}1==enemyalgorithm&&(ei<ai&&(e=AB.randomIntAtoB(ei,ei+1)),ei==ai&&(e=ei),ei>ai&&(e=AB.randomIntAtoB(ei-1,ei)),ej<aj&&(u=AB.randomIntAtoB(ej,ej+1)),ej==aj&&(u=ej),ej>aj&&(u=AB.randomIntAtoB(ej-1,ej)),occupied(e,u)||(ei=e,ej=u));3==enemyalgorithm&&(occupied(e=ei+AB.randomIntAtoB(-1,1),u=ej+AB.randomIntAtoB(-1,1))||(ei=e,ej=u));if(4==enemyalgorithm){var u,p=[],h=[];p[0]=Math.sqrt(Math.pow(ei-ai,2)+Math.pow(ej+1-aj,2)),p[1]=Math.sqrt(Math.pow(ei+1-ai,2)+Math.pow(ej-aj,2)),p[2]=Math.sqrt(Math.pow(ei-ai,2)+Math.pow(ej-1-aj,2)),p[3]=Math.sqrt(Math.pow(ei-1-ai,2)+Math.pow(ej-aj,2)),console.log(p);var l=Math.min.apply(null,p);for(d=0;d<p.length;d++)p[d]==l&&h.push(d);d=AB.randomElementOfArray(h),fi>=2&&(d=AB.randomIntAtoB(0,3)),console.log(d),0==d&&(e=ei,u=ej+1),1==d&&(e=ei+1,u=ej),2==d&&(e=ei,u=ej-1),3==d&&(e=ei-1,u=ej),occupied(e,u)?fi++:(ei=e,ej=u,fi=0)}}function moveLogicalAgent(e){var a=ai,t=aj;previousai=ai,previousaj=aj,agentalgorithm<3&&(e==ACTION_LEFT?a--:e==ACTION_RIGHT?a++:e==ACTION_UP?t++:e==ACTION_DOWN&&t--,occupied(a,t)||(ai=a,aj=t)),3==agentalgorithm&&(fim>=2&&(e=AB.randomIntAtoB(0,3)),0==e&&(a=ai,t=aj+1),1==e&&(a=ai+1,t=aj),2==e&&(a=ai,t=aj-1),3==e&&(a=ai-1,t=aj),occupied(a,t)?fim++:(ai=a,aj=t,fim=0))}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 a=AB.world.getState();AB.msg(" Step: "+AB.step+" &nbsp; x = ("+a.toString()+") &nbsp; a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),a=goodsteps/AB.step*100;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+a.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,agentalgorithm,GRID[ai][aj-1].value,GRID[ai+1][aj].value,GRID[ai-1][aj].value,GRID[ai][aj+1],gridsize,GRIDMIND]},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()}