Code viewer for World: A Star Practical (Complex ...

// Cloned by rohan on 13 Nov 2021 from World "A Star Practical (Complex World (clone by Ger Gleeson))" by Ger Gleeson 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/gerryg80/shutterstock-289828547___21093705684.jpg",TEXTURE_MAZE="/uploads/gerryg80/easy_guide_grass.jpg",TEXTURE_AGENT="/uploads/gerryg80/images.png",TEXTURE_ENEMY="/uploads/gerryg80/600px-EnemyGG.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=20,NOBOXES=Math.trunc(gridsize*gridsize/10),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=7396208,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,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize),diagonal=!1;function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,i=new THREE.TextureLoader,n=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()}),i.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),n.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 isWall(e,t){return GRID[e][t]==GRID_WALL||GRID[e][t]==GRID_MAZE}function translate(e,t){var i=new THREE.Vector3;return i.y=0,i.x=e*squaresize-MAXPOS/2,i.z=t*squaresize-MAXPOS/2,i}function initScene(){var e,t,i,n;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,i=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(n=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:wall_texture}),n.position.copy(translate(e,t)),ABWorld.scene.add(n)):GRID[e][t]=GRID_BLANK;for(var r=1;r<=NOBOXES;r++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,i=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(n=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:maze_texture}),n.position.copy(translate(e,t)),ABWorld.scene.add(n);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,i=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(i)).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,i=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(i)).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(e.i-t.i)+Math.abs(e.j-t.j)}function Spot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.wall=isWall(e,t),this.previous=void 0,this.addNeighbors=function(e){var t=this.i,i=this.j;t<gridsize-1&&this.neighbors.push(e[t+1][i]),t>0&&this.neighbors.push(e[t-1][i]),i<gridsize-1&&this.neighbors.push(e[t][i+1]),i>0&&this.neighbors.push(e[t][i-1]),diagonal&&(t>0&&i>0&&this.neighbors.push(e[t-1][i-1]),t<gridsize-1&&i>0&&this.neighbors.push(e[t+1][i-1]),t>0&&i<gridsize-1&&this.neighbors.push(e[t-1][i+1]),t<gridsize-1&&i<gridsize-1&&this.neighbors.push(e[t+1][i+1]))}}function moveLogicalEnemy(){for(var e=new Array(gridsize),t=[],i=[],n=0;n<gridsize;n++)e[n]=new Array(gridsize);for(var n=0;n<gridsize;n++)for(var r=0;r<gridsize;r++)e[n][r]=new Spot(n,r);for(n=0;n<gridsize;n++)for(var r=0;r<gridsize;r++)e[n][r].addNeighbors(e);var a=e[ei][ej],s=e[ai][aj];for(t.push(a);t.length>0;){var o=0;for(n=0;n<t.length;n++)t[n].f<t[o].f&&(o=n);var d=t[o];if(d===s){var u=[],g=d;for(u.push(g);g.previous;)u.push(g.previous),g=g.previous;var c=u.length-1;occupied(u[c-1].i,u[c-1].j)||(ei=u[c-1].i,ej=u[c-1].j);break}t.splice(o,1),i.push(d);var A=d.neighbors;for(n=0;n<A.length;n++){var p=A[n];if(p[n]===s){p.previous=d;break}if(!i.includes(p)&&!p.wall){var l=d.g+heuristic(p,d),h=!1;t.includes(p)?l<p.g&&(p.g=l,h=!0):(p.g=l,h=!0,t.push(p)),h&&(p.h=heuristic(p,s),p.f=p.g+p.h,p.previous=d)}}}}function moveLogicalAgent(e){var t=ai,i=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?i++:e==ACTION_DOWN&&i--,occupied(t,i)||(ai=t,aj=i)}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,7396208),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()}