Code viewer for World: Complex World (clone by Ak...

// Cloned by Akash Gupta on 27 Oct 2022 from World "Complex World" by Akash Gupta 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/akash037/walls.jpg",TEXTURE_MAZE="/uploads/starter/latin.jpg",TEXTURE_AGENT="/uploads/akash037/luke.jpg",TEXTURE_ENEMY="/uploads/akash037/darth.jpeg",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=50,NOBOXES=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),grid=new Array(gridsize),count=0;const diagonal=!0,pathColor="darkgreen";function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,i=new THREE.TextureLoader,r=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()})}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 i=new THREE.Vector3;return i.y=0,i.x=e*squaresize-MAXPOS/2,i.z=t*squaresize-MAXPOS/2,i}var path=[];function Node(e,t,i){this.i=e,this.j=t,this.f=Number.MAX_VALUE,this.g=Number.MAX_VALUE,this.h=0,this.neighbors=[],this.wall=!1,this.previous=null,i&&(this.wall=!0),this.addNeighbors=function(e){var t=this.i,i=this.j;t<gridsize-1&&!e[t+1][i].wall&&this.neighbors.push(e[t+1][i]),t>0&&!e[t-1][i].wall&&this.neighbors.push(e[t-1][i]),i<gridsize-1&&!e[t][i+1].wall&&this.neighbors.push(e[t][i+1]),i>0&&!e[t][i-1].wall&&this.neighbors.push(e[t][i-1]),diagonal&&(t>0&&i>0&&!e[t-1][i-1].wall&&this.neighbors.push(e[t-1][i-1]),t<gridsize-1&&i>0&&!e[t+1][i-1].wall&&this.neighbors.push(e[t+1][i-1]),t>0&&i<gridsize-1&&!e[t-1][i+1].wall&&this.neighbors.push(e[t-1][i+1]),t<gridsize-1&&i<gridsize-1&&!e[t+1][i+1].wall&&this.neighbors.push(e[t+1][i+1]))}}var openList=[],closedList=[];function initScene(){var e,t;for(r=0;r<gridsize;r++)GRID[r]=new Array(gridsize),grid[r]=new Array(gridsize);for(r=0;r<gridsize;r++)for(a=0;a<gridsize;a++)0==r||r==gridsize-1||0==a||a==gridsize-1?(GRID[r][a]=GRID_WALL,grid[r][a]=new Node(r,a,!0),e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(r,a)),ABWorld.scene.add(t)):(GRID[r][a]=GRID_BLANK,grid[r][a]=new Node(r,a,!1));for(var i=1;i<=NOBOXES;i++)r=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2),GRID[r][a]=GRID_MAZE,grid[r][a].wall=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(r,a)),ABWorld.scene.add(t);for(var r=0;r<gridsize;r++)for(var a=0;a<gridsize;a++)grid[r][a].addNeighbors(grid);do{r=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(r,a));ei=r,ej=a,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{r=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(r,a));ai=r,aj=a,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(e)).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 removeFromArray(e,t){for(var i=e.length-1;i>=0;i--)e[i]==t&&e.splice(i,1)}function heuristic(e,t){return Math.pow(Math.abs(e.i-t.i),2)+Math.pow(Math.abs(e.j-t.j),2)}function moveLogicalEnemy(){for(;openList.length>0;)openList.pop();for(;closedList.length>0;)closedList.pop();for(;path.length>0;)path.pop();clearPrevious(),printPath(aStar(grid[ei][ej],grid[ai][aj]))}function clearPrevious(){for(var e=0;e<gridsize-1;e++)for(var t=0;t<gridsize-1;t++)grid[e][t].previous=null}function aStar(e,t){for(e.f=e.g+heuristic(e,t),openList.push(e);openList.length>0;){for(var i=0,r=0;r<openList.length;r++)openList[r].f<openList[i].f&&(i=r);var a=openList[i];if(a==t)return a;for(r=0;r<a.neighbors.length;r++){var n=a.neighbors[r],s=a.g+heuristic(a,n);openList.includes(n)||closedList.includes(n)||n.wall?s<n.g&&(n.previous=a,n.g=s,n.f=n.g+heuristic(n,t),closedList.includes(n)&&(removeFromArray(closedList,n),openList.push(n))):(n.previous=a,n.g=a.g+heuristic(a,n),n.f=n.g+heuristic(n,t),openList.push(n))}removeFromArray(openList,a),closedList.push(a)}return null}function printPath(e){var t=e;if(null!==t){for(;null!==t.previous;)path.push(t),t=t.previous;path.push(t),path.length>1&&(occupied(path[path.length-2].i,path[path.length-2].j)||(drawPath(ei,ej,path[path.length-2].i,path[path.length-2].j),ei=path[path.length-2].i,ej=path[path.length-2].j))}}var line,material=new THREE.LineBasicMaterial({color:pathColor,linewidth:100}),points=[];function drawPath(e,t,i,r){points.push(translate(e,t)),points.push(translate(i,r)),geometry=(new THREE.BufferGeometry).setFromPoints(points),line=new THREE.Line(geometry,material),ABWorld.scene.add(line)}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()}