Code viewer for World: Marvel A* - World 1 (clone...

// Cloned by joey on 17 Nov 2021 from World "Marvel A* - World 1" by Sophie Renshaw 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/50ph/grey.jpeg",TEXTURE_MAZE="/uploads/50ph/steel-texture.jpeg",TEXTURE_AGENT="/uploads/50ph/ironman.jpg",TEXTURE_ENEMY="/uploads/50ph/loki.jpeg",MUSIC_BACK="/uploads/50ph/The-Avengers-Theme-Song.mp3",SOUND_ALARM="/uploads/50ph/fall.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,GRID=new Array(gridsize);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 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 initScene(){var e,t,n,o;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,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(o=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:wall_texture}),o.position.copy(translate(e,t)),ABWorld.scene.add(o)):GRID[e][t]=GRID_BLANK;for(var i=1;i<=NOBOXES;i++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(o=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:maze_texture}),o.position.copy(translate(e,t)),ABWorld.scene.add(o);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(n)).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,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(n)).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})}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 heuristic(e,t){return Math.abs(t.i-e.i)+Math.abs(t.j-e.j)}function node(e,t){this.i=e,this.j=t,this.g=0,this.h=0,this.f=0,this.previous}function removeFromArray(e,t){const n=e.indexOf(t);n>-1&&e.splice(n,1)}function drawNode(e,t){var n=new THREE.BoxGeometry(100,100,100),o=new THREE.MeshBasicMaterial({color:t}),i=new THREE.MeshBasicMaterial({opacity:"0, 0.1, 0.01"}),r=new THREE.Mesh(n,o,i);r.name="PathObject",r.position.copy(translate(e.i,e.j)),ABWorld.scene.add(r)}function removePath(e){for(i=0;i<=e.length;i++){var t=ABWorld.scene.getObjectByName("PathObject");ABWorld.scene.remove(t)}}function getNeighbors(e,t){var n=[];return e.i>0&&!occupied(e.i-1,e.j)&&!1===isNodeInList(e.i-1,e.j,t)&&n.push(new node(e.i-1,e.j)),e.i<gridsize-1&&!occupied(e.i+1,e.j)&&!1===isNodeInList(e.i+1,e.j,t)&&n.push(new node(e.i+1,e.j)),e.j>0&&!occupied(e.i,e.j-1)&&!1===isNodeInList(e.i,e.j-1,t)&&n.push(new node(e.i,e.j-1)),e.j<gridsize-1&&!occupied(e.i,e.j+1)&&!1===isNodeInList(e.i,e.j+1,t)&&n.push(new node(e.i,e.j+1)),n}function getNextBestStep(e){for(var t=0,n=0;n<e.length;n++)e[n].f<=e[t].f&&(t=n);return t}function calculations(e,t,n){return e.h=heuristic(t,e),e.f=e.g+e.h,e.previous=n,e}function findPath(e,t){var n=e;for(t.push(n);n.previous;)t.push(n.previous),n=n.previous;return t.pop(),t.reverse()}function isNodeInList(e,t,n){var o=!1;if(n.length>0)for(var i=0;i<n.length;i++)n[i].i==e&&n[i].j==t&&(o=!0);return o}function endNodeNeighbors(e){var t=[];return t.push(new node(e.i-1,e.j)),t.push(new node(e.i+1,e.j)),t.push(new node(e.i,e.j-1)),t.push(new node(e.i,e.j+1)),t}function aStar(){var e=[],t=[],n=[],o=new node(ei,ej),i=new node(ai,aj);for(e.push(o);e.length>0;){var r=e[getNextBestStep(e)];if(isNodeInList(r.i,r.j,endNodeNeighbors(i))){findPath(r,n);break}removeFromArray(e,r),t.push(r);for(var a=getNeighbors(r,t),s=0;s<a.length;s++){var d=a[s];if(!t.includes(d)){var u=r.g+1,c=!1;e.includes(d)?u<d.g&&(d.g=u,c=!0):(d.g=u,c=!0,e.push(d)),c&&calculations(d,i,r)}}}removePath(n);for(var p=1;p<n.length;p++)drawNode(n[p],"#32CD32");return n}function moveLogicalEnemy(){var e=aStar();e.length>0?(ei=e[0].i,ej=e[0].j):console.log("NO PATH FOUND")}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)||(ai=t,aj=n)}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()&&(console.log("AGENT BLOCKED"),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()}