Code viewer for Mind: World 1 (clone by David Jusev)

// Cloned by David Jusev on 1 Dec 2022 from Mind "World 1" by Vineet Sajwan 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/xlr8pro/stone_wall.jpg",TEXTURE_MAZE="/uploads/xlr8pro/asteroid.PNG",TEXTURE_AGENT="/uploads/xlr8pro/ufo_2.jpg",TEXTURE_ENEMY="/uploads/xlr8pro/chip.jpg",MUSIC_BACK="/uploads/xlr8pro/no_surprises.mp3",SOUND_ALARM="/uploads/xlr8pro/buzzer_1.mp3",gridsize=20,NOBOXES=55,squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/xlr8pro/pz_5.png","/uploads/xlr8pro/nz_5.png","/uploads/xlr8pro/py_5.png","/uploads/xlr8pro/ny_5.png","/uploads/xlr8pro/px_5.png","/uploads/xlr8pro/nx_5.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,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize);diagonal=!1,Draw=!0,shownPath=[];const pathcolor="darkred";function loadResources(){var e=new THREE.TextureLoader,r=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()}),r.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 heuristic(e,r){return Math.abs(e.i-r.i)+Math.abs(e.j-r.j)}function asynchFinished(){return!!(wall_texture&&agent_texture&&enemy_texture&&maze_texture)}function occupied(e,r){return ei==e&&ej==r||(ai==e&&aj==r||(GRID[e][r]==GRID_WALL||GRID[e][r]==GRID_MAZE))}function translate(e,r){var t=new THREE.Vector3;return t.y=0,t.x=e*squaresize-MAXPOS/2,t.z=r*squaresize-MAXPOS/2,t}function initScene(){var e,r,t,i;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(r=0;r<gridsize;r++)0==e||e==gridsize-1||0==r||r==gridsize-1?(GRID[e][r]=GRID_WALL,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:wall_texture}),i.position.copy(translate(e,r)),ABWorld.scene.add(i)):GRID[e][r]=GRID_BLANK;for(var a=1;a<=NOBOXES;a++)e=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2),GRID[e][r]=GRID_MAZE,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:maze_texture}),i.position.copy(translate(e,r)),maze_Location.push(i),ABWorld.scene.add(i);do{e=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,r));ei=e,ej=r,t=new THREE.SphereGeometry(squaresize-50,BOXHEIGHT-50,squaresize-50),(theenemy=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{e=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,r));ai=e,aj=r,t=new THREE.ConeGeometry(squaresize-30,squaresize-30,BOXHEIGHT-20),(theagent=new THREE.Mesh(t)).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 isWall(e,r){return GRID[e][r]==GRID_WALL||GRID[e][r]==GRID_MAZE}function Spot(e,r){this.i=e,this.j=r,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.wall=isWall(e,r),this.previous=void 0,this.addNeighbors=function(e){var r=this.i,t=this.j;r<gridsize-1&&this.neighbors.push(e[r+1][t]),r>0&&this.neighbors.push(e[r-1][t]),t<gridsize-1&&this.neighbors.push(e[r][t+1]),t>0&&this.neighbors.push(e[r][t-1]),diagonal&&(r>0&&t>0&&this.neighbors.push(e[r-1][t-1]),r<gridsize-1&&t>0&&this.neighbors.push(e[r+1][t-1]),r>0&&t<gridsize-1&&this.neighbors.push(e[r-1][t+1]),r<gridsize-1&&t<gridsize-1&&this.neighbors.push(e[r+1][t+1]))}}function highlightPath(e,r){for(var t=0;t<e.length-1;t++)shape=new THREE.BoxGeometry(squaresize-50,.1,squaresize-50),ShownPath=new THREE.Mesh(shape),ShownPath.material=new THREE.MeshBasicMaterial({color:16711680}),ShownPath.position.copy(translate(e[t].i,e[t].j)),ABWorld.scene.add(ShownPath),shownPath.push(ShownPath)}function moveLogicalEnemy(){for(var e=new Array(gridsize),r=[],t=0;t<shownPath.length;t++)ABWorld.scene.remove(shownPath[t]);e=new Array(gridsize),r=[],o=[];for(var i=0;i<gridsize;i++)e[i]=new Array(gridsize);for(var a=0;a<gridsize;a++)for(var n=0;n<gridsize;n++)e[a][n]=new Spot(a,n);for(var s=0;s<gridsize;s++)for(var u=0;u<gridsize;u++)e[s][u].addNeighbors(e);var d=e[ei][ej],l=e[ai][aj];r.push(d);for(var p=0;r.length>0;){for(path=[],i=0;i<r.length;i++)r[i].f<r[p].f&&(p=i);var c=r[p];if(c===l){var h=[],A=c;for(h.push(A);A.previous;)h.push(A.previous),A=A.previous;highlightPath(h,190);var g=h.length-1;occupied(h[g-1].i,h[g-1].j)||(ei=h[g-1].i,ej=h[g-1].j);break}r.splice(p,1),o.push(c);var E=c.neighbors;for(i=0;i<E.length;i++){var R=E[i];if(R[i]===l){R.previous=c;break}if(!o.includes(R)&&!R.wall){var B=c.g+heuristic(R,c),T=!1;r.includes(R)?B<R.g&&(R.g=B,T=!0):(R.g=B,T=!0,r.push(R)),T&&(R.h=heuristic(R,l),R.f=R.g+R.h,R.previous=c)}}}}function moveLogicalAgent(e){var r=ai,t=aj;e==ACTION_LEFT?r--:e==ACTION_RIGHT?r++:e==ACTION_UP?t++:e==ACTION_DOWN&&t--,occupied(r,t)||(ai=r,aj=t)}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 r=AB.world.getState();AB.msg(" Step: "+AB.step+" &nbsp; x = ("+r.toString()+") &nbsp; a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),r=goodsteps/AB.step*100;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+r.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):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()}function inverse_translate(e){var r=new THREE.Vector2;return r.x=(e.x+MAXPOS/2)/squaresize,r.y=(e.z+MAXPOS/2)/squaresize,r}Array.prototype.sample=function(){return this[Math.floor(Math.random()*this.length)]};