Code viewer for World: Assignment 1 (clone by Abd...

// Cloned by Abdelshafa Abdala on 6 Nov 2021 from World "Assignment 1" by Dheeraj Putta 
// Please leave this clone trail here.
 
AB.clockTick=20,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/starter/door.jpg",TEXTURE_MAZE="/uploads/starter/latin.jpg",TEXTURE_AGENT="/uploads/starter/pacman.jpg",TEXTURE_ENEMY="/uploads/djputta/ghost.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/djputta/indigo_bk.jpg","/uploads/djputta/indigo_ft.jpg","/uploads/djputta/indigo_up.jpg","/uploads/djputta/indigo_dn.jpg","/uploads/djputta/indigo_lf.jpg","/uploads/djputta/indigo_rt.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,diagonal=!1;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize),agentLastVisited=[0,0],agentStuck=0;function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,i=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()}),i.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].type==GRID_WALL||GRID[e][t].type==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,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]=new Spot(e,t),GRID[e][t].type=GRID_WALL,i=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(o=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:wall_texture}),o.position.copy(translate(e,t)),ABWorld.scene.add(o)):(GRID[e][t]=new Spot(e,t),GRID[e][t].type=GRID_BLANK);for(var n=1;n<=NOBOXES;n++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t].type=GRID_MAZE,i=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(o=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:maze_texture}),o.position.copy(translate(e,t)),ABWorld.scene.add(o);for(var r=0;r<gridsize;r++)for(var a=0;a<gridsize;a++)GRID[r][a].addNeighbors(GRID);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 moveLogicalEnemy(){spot=aStar(),ei=spot.i,ej=spot.j}function moveLogicalAgent(e){ai=e.i,aj=e.j}function Spot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.previous=void 0,this.type=void 0,this.addNeighbors=function(e){var t=this.i,i=this.j;t<gridsize-1&&!occupied(t+1,i)&&this.neighbors.push(e[t+1][i]),t>0&&!occupied(t-1,i)&&this.neighbors.push(e[t-1][i]),i<gridsize-1&&!occupied(t,i+1)&&this.neighbors.push(e[t][i+1]),i>0&&!occupied(t,i-1)&&this.neighbors.push(e[t][i-1]),diagonal&&(!(t>0&&i>0)||occupied(t-1,i-1)||occupied(t-1,i)&&occupied(t,i-1)||this.neighbors.push(e[t-1][i-1]),!(t<gridsize-1&&i>0)||occupied(t+1,i-1)||occupied(t+1,i)&&occupied(t,i-1)||this.neighbors.push(e[t+1][i-1]),!(t>0&&i<gridsize-1)||occupied(t-1,i+1)||occupied(t-1,i)&&occupied(t,i+1)||this.neighbors.push(e[t-1][i+1]),!(t<gridsize-1&&i<gridsize-1)||occupied(t+1,i+1)||occupied(t+1,i)&&occupied(t,i+1)||this.neighbors.push(e[t+1][i+1]))},this.reset=function(){this.f=0,this.g=0,this.h=0,this.previous=void 0}}function heuristic(e,t){var i=new THREE.Vector3(e.i,0,e.j),o=new THREE.Vector3(t.i,0,t.j);return i.distanceTo(o)}function removeFromArray(e,t){e.splice(e.indexOf(t),1)}function aStar(){var e=[],t=[],i=GRID[ei][ej],o=GRID[ai][aj];e.push(i),console.log("Start: ",i),console.log("End: ",o),console.log("Reset started");for(var n=0;n<gridsize;n++)for(var r=0;r<gridsize;r++)GRID[n][r].reset();for(;e.length>0;){var a=0;for(n=0;n<e.length;n++)e[n].f<e[a].f&&(a=n);var s=e[a];if(s===o){console.log("success - found path");break}removeFromArray(e,s),t.push(s);var d=s.neighbors;for(n=0;n<d.length;n++){var u=d[n];if(!t.includes(u)){var c=s.g+heuristic(u,s),p=!1;e.includes(u)?c<u.g&&(u.g=c,p=!0):(u.g=c,p=!0,e.push(u)),p&&(u.h=heuristic(u,o),u.f=u.g+u.h,u.previous=s)}}}var g=[],l=o;for(g.push(l);l.previous;)g.push(l.previous),l=l.previous;return console.log(g),console.log(g[g.length-2]),draw(g),g[g.length-2]}function draw(e){for(var t=[],i=0;i<e.length;i++)t.push(translate(e[i].i,e[i].j));var o=new THREE.LineBasicMaterial({color:16711680,linewidth:1.5}),n=(new THREE.BufferGeometry).setFromPoints(t),r=new THREE.Line(n,o);r.name="astarpath",ABWorld.scene.add(r)}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+") "),ABWorld.scene.remove(ABWorld.scene.getObjectByName("astarpath"))}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);var t=[ai,aj];moveLogicalAgent(e),AB.step%2==0&&moveLogicalEnemy(),badstep()?badsteps++:goodsteps++,drawAgent(),drawEnemy(),updateStatusAfter(),agentLastVisited=t,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()}