Code viewer for World: Planet chase - Gelmis Bart...

// Cloned by test on 24 Aug 2021 from World "Planet chase - Gelmis Bartulis" by Gelmis Bartulis 
// Please leave this clone trail here.
 
AB.clockTick=5,AB.maxSteps=500,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/deadlock/closed.png",TEXTURE_MAZE="/uploads/deadlock/bkg1_left.png",TEXTURE_AGENT="/uploads/deadlock/maze_wall.png",TEXTURE_ENEMY="/uploads/deadlock/sunt.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=50,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/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,enemySpot,agentSpot,start,end,GRID=new Array(gridsize),neighbors=[],openSet=[],closedSet=[],newGRID=new Array(gridsize),openPath=[];const diagonal=!0,objectsize=50;var shape=new THREE.SphereGeometry(50,50,50),material=new THREE.MeshBasicMaterial,theobject=new THREE.Mesh(shape,material),pathFailed=!1;function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,n=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()}),n.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 initScene(){var e,t;for(a=0;a<gridsize;a++)GRID[a]=new Array(gridsize),newGRID[a]=new Array(gridsize);var n=new THREE.GridHelper(MAXPOS-squaresize-2,squaresize,8388608,8388608);for(n.position.copy(translateBox(gridsize/2,gridsize/2)),ABWorld.scene.add(n),a=0;a<gridsize;a++)for(i=0;i<gridsize;i++)newGRID[a][i]=new Spot(a,i),newGRID[a][i].wall=!0;for(a=0;a<gridsize;a++)for(i=0;i<gridsize;i++)0===a||a==gridsize-1||0===i||i==gridsize-1?(GRID[a][i]=GRID_WALL,e=new THREE.ConeGeometry(50,BOXHEIGHT,100),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(a,i)),ABWorld.scene.add(t)):GRID[a][i]=GRID_BLANK;for(var o=1;o<=NOBOXES;o++)a=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2),GRID[a][i]=GRID_MAZE,e=new THREE.SphereGeometry(50,BOXHEIGHT,50),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(a,i)),ABWorld.scene.add(t);do{a=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(a,i));(enemySpot=new Spot(ei=a,ej=i)).addNeighbors(gridsize),e=new THREE.SphereGeometry(50,BOXHEIGHT,50),(theenemy=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{a=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(a,i));ai=a,aj=i,(agentSpot=new Spot(ei,ej)).addNeighbors(gridsize),e=new THREE.SphereGeometry(50,BOXHEIGHT,50),(theagent=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent();for(var a=0;a<gridsize-2;a++)for(var i=0;i<gridsize-2;i++)newGRID[a][i].addNeighbors(gridsize);start=translate(newGRID[ei][ej]),end=translate(newGRID[ai][aj]),openSet.push(start),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 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 translateBox(e,t){var n=new THREE.Vector3;return n.y=-50,n.x=e*squaresize-MAXPOS/2,n.z=t*squaresize-MAXPOS/2,n}function basicDistance(e,t){const n=new THREE.Vector2(e.i,e.j),o=new THREE.Vector2(t.i,t.j);return n.distanceTo(o)}function chebyshevDistance(e,t){return Math.max(e,t)}function manhattanDistance(e,t){return Math.abs(e.i-e.j)+Math.abs(t.i-t.j)}function diagonalDistance(e,t){return dx=Math.abs(t.i-e.i),dy=Math.abs(t.j-e.j),1*(dx+dy)+-1*Math.min(dx,dy)}function euclideanDistance(e,t){return Math.sqrt(Math.pow(e.i-e.j,2)+Math.pow(t.i-t.j,2))}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.wall=!1,this.previous=void 0,this.addNeighbors=function(e){var t=this.i,n=this.j;t<e-1&&this.neighbors.push(newGRID[t+1][n]),t>0&&this.neighbors.push(newGRID[t-1][n]),n<e-1&&this.neighbors.push(newGRID[t][n+1]),n>0&&this.neighbors.push(newGRID[t][n-1]),diagonal&&(t>0&&n>0&&this.neighbors.push(newGRID[t-1][n-1]),t<e-1&&n>0&&this.neighbors.push(newGRID[t+1][n-1]),t>0&&n<e-1&&this.neighbors.push(newGRID[t-1][n+1]),t<e-1&&n<e-1&&this.neighbors.push(newGRID[t+1][n+1]))}}function findPath(){for(openSet=[],closedSet=[],(enemySpot=new Spot(ei,ej)).addNeighbors(gridsize),openSet.push(enemySpot);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];if(agentSpot.neighbors.includes(n)){var o=n;for((openPath=[]).push(o);o.previous;)openPath.push(o.previous),o=o.previous;return openPath}removeFromArray(openSet,n),closedSet.push(n);var a=n.neighbors;for(t=0;t<a.length;t++){var i=a[t];if(!occupied(i.i,i.j)&&!closedSet.includes(i)){var r=n.g+diagonalDistance(i,n),s=!1;openSet.includes(i)?r<i.g&&(i.g=r,s=!0):(i.g=r,s=!0,openSet.push(i)),s&&(i.h=diagonalDistance(i,agentSpot),i.f=i.g+i.h,i.previous=n)}}}var d=n;for((openPath=[]).push(d);d.previous;)openPath.push(d.previous),d=d.previous}function moveLogicalEnemy(){var e=findPath();if(e){showDistance(e),console.log(e);for(var t=0;t<e.length-1;t++)ei=e[t].i,ej=e[t].j}else console.log("__________________ Path has been lost ____________________")}function moveLogicalAgent(e){var t=ai,n=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?n++:e==ACTION_DOWN&&n--,occupied(t,n)||(agentSpot=new Spot(ai=t,aj=n)).addNeighbors(gridsize)}function showDistance(e){var t,n,o,a=[];if(e){for(let t=0;t<e.length-1;t++)a.push(translateBox(e[t].i,e[t].j));t=(new THREE.BufferGeometry).setFromPoints(a),o=new THREE.LineBasicMaterial({color:16777215}),n=new THREE.Line(t,o),ABWorld.scene.add(n),setTimeout(()=>{t.dispose(),o.dispose(),ABWorld.scene.remove(n)},AB.clockTick)}}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(),AB.abortRun?AB.msg(" <br> <font color=red> <B> Agent trapped. Final score zero. </B> </font>   ",3):pathFailed?AB.msg(" <br> <font color=purple> <B> Path lost - Game over</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()}