Code viewer for World: Complex World (2020-C686I:...

// Cloned by Abdelshafa Abdala on 3 Nov 2022 from World "Complex World (2020-C686I: Assignment 1)" by Andrey Totev 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/atotev/stone.png",TEXTURE_MAZE="/uploads/atotev/timber.png",TEXTURE_AGENT="/uploads/atotev/spot-centaur3.png",TEXTURE_ENEMY="/uploads/atotev/zeus2.png",TEXTURE_TILE="/uploads/atotev/tile.png",TEXTURE_TILE_OPEN="/uploads/atotev/tile-open.png",TEXTURE_TILE_CLOSED="/uploads/atotev/tile-closed.png",TEXTURE_TILE_PATH="/uploads/atotev/tile-path2.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/posx.jpg","/uploads/starter/negx.jpg","/uploads/starter/posy.jpg","/uploads/starter/negy.jpg","/uploads/starter/posz.jpg","/uploads/starter/negz.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,tile_texture,tile_open_texture,tile_closed_texture,tile_path_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize),GRID_TILES=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,i=new THREE.TextureLoader,r=new THREE.TextureLoader,a=new THREE.TextureLoader,n=new THREE.TextureLoader,o=new THREE.TextureLoader,s=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()}),r.load(TEXTURE_MAZE,function(e){e.minFilter=THREE.LinearFilter,maze_texture=e,asynchFinished()&&initScene()}),a.load(TEXTURE_TILE,function(e){e.minFilter=THREE.LinearFilter,tile_texture=e,asynchFinished()&&initScene()}),n.load(TEXTURE_TILE_OPEN,function(e){e.minFilter=THREE.LinearFilter,tile_open_texture=e,asynchFinished()&&initScene()}),o.load(TEXTURE_TILE_CLOSED,function(e){e.minFilter=THREE.LinearFilter,tile_closed_texture=e,asynchFinished()&&initScene()}),s.load(TEXTURE_TILE_PATH,function(e){e.minFilter=THREE.LinearFilter,tile_path_texture=e,asynchFinished()&&initScene()})}function asynchFinished(){return!!(wall_texture&&agent_texture&&enemy_texture&&maze_texture&&tile_texture&&tile_open_texture&&tile_closed_texture&&tile_path_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,i){var r=new THREE.Vector3;return r.y=i?-squaresize/2-1:0,r.x=e*squaresize-MAXPOS/2,r.z=t*squaresize-MAXPOS/2,r}function initScene(){var e,t,i,r;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize),GRID_TILES[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)GRID_TILES[e][t]=new Array(4),i=new THREE.BoxGeometry(squaresize,1,squaresize),(r=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:tile_texture}),r.position.copy(translate(e,t,!0)),GRID_TILES[e][t][0]=r,i=new THREE.BoxGeometry(squaresize,1,squaresize),(r=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:tile_open_texture}),r.position.copy(translate(e,t,!0)),GRID_TILES[e][t][1]=r,i=new THREE.BoxGeometry(squaresize,1,squaresize),(r=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:tile_closed_texture}),r.position.copy(translate(e,t,!0)),GRID_TILES[e][t][2]=r,i=new THREE.BoxGeometry(squaresize,1,squaresize),(r=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:tile_path_texture}),r.position.copy(translate(e,t,!0)),GRID_TILES[e][t][3]=r,ABWorld.scene.add(GRID_TILES[e][t][0]);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),(r=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:wall_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r)):GRID[e][t]=GRID_BLANK;for(var a=1;a<=NOBOXES;a++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,i=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(i)).material=new THREE.MeshBasicMaterial({map:maze_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r);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(){runAStar()}function showTile(e,t,i){for(hide=0;hide<4;hide++)ABWorld.scene.remove(GRID_TILES[e][t][hide]);ABWorld.scene.add(GRID_TILES[e][t][i])}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.wall=GRID[e][t]!==GRID_BLANK,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]),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 heuristic(e,t){return Math.sqrt(Math.pow(e.i-t.i,2)+Math.pow(e.j-t.j,2))}function removeFromArray(e,t){for(var i=e.length-1;i>=0;i--)e[i]==t&&e.splice(i,1)}function runAStar(){for(t=0;t<gridsize;t++)for(i=0;i<gridsize;i++)showTile(t,i,0);for(var e=[],t=0;t<gridsize;t++)e[t]=new Array(gridsize);for(t=0;t<gridsize;t++)for(var i=0;i<gridsize;i++)e[t][i]=new Spot(t,i);for(t=0;t<gridsize;t++)for(i=0;i<gridsize;i++)e[t][i].addNeighbors(e);var r=[],a=[];r.push(e[ei][ej]);for(var n=void 0;r.length>0;){var o=0;for(t=0;t<r.length;t++)r[t].f<r[o].f&&(o=t);var s=r[o];if(s.i===ai&&s.j===aj){n=s;break}removeFromArray(r,s),a.push(s);var d=s.neighbors;for(t=0;t<d.length;t++){var u=d[t];if(!a.includes(u)&&!u.wall){var l=s.g+heuristic(u,s),E=!1;r.includes(u)?l<u.g&&(u.g=l,E=!0):(u.g=l,E=!0,r.push(u)),E&&(u.h=heuristic(u,e[ai][aj]),u.f=u.g+u.h,u.previous=s)}}}for(t=0;t<r.length;t++)showTile(r[t].i,r[t].j,1);for(t=0;t<a.length;t++)showTile(a[t].i,a[t].j,2);if(n)if(n.previous!==e[ei][ej]){for(var c=void 0,p=n;p.previous;)c=p,p=p.previous,showTile(c.i,c.j,3);ei=c.i,ej=c.j}else{ei<ai&&(t=AB.randomIntAtoB(ei,ei+1)),ei==ai&&(t=ei),ei>ai&&(t=AB.randomIntAtoB(ei-1,ei)),ej<aj&&(i=AB.randomIntAtoB(ej,ej+1)),ej==aj&&(i=ej),ej>aj&&(i=AB.randomIntAtoB(ej-1,ej)),occupied(t,i)||(ei=t,ej=i)}}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,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()}