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

// Cloned by Abdelshafa Abdala on 6 Nov 2021 from World "Complex World (clone by Gary Collins)" by Gary Collins 
// Please leave this clone trail here.
 
AB.clockTick=10,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!1,TEXTURE_WALL="/uploads/gary/blue-sky-2.jpg",TEXTURE_MAZE="/uploads/gary/walter-dollar.jpg",TEXTURE_AGENT="/uploads/gary/walter-2.jpg",TEXTURE_ENEMY="/uploads/gary/hank.jpg",MUSIC_BACK="/uploads/gary/Benny-hill-theme.mp3",SOUND_ALARM="/uploads/niamhbyrne/winner.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/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);function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,n=new THREE.TextureLoader,a=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()}),n.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),a.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||(1==GRID[e][t].wall||1==GRID[e][t].maze))}function translate(e,t){var n=new THREE.Vector3;return n.y=0,n.x=e*squaresize-MAXPOS/2,n.z=t*squaresize-MAXPOS/2,n}function initScene(){var e,t;for(n=0;n<gridsize;n++)GRID[n]=new Array(gridsize);for(var n=0;n<gridsize;n++)for(var a=0;a<gridsize;a++)GRID[n][a]=new Spot(n,a);for(n=0;n<gridsize;n++)for(a=0;a<gridsize;a++)GRID[n][a].addNeighbors(GRID);for(console.log(".... setup GRID ...."),console.log(GRID),n=0;n<gridsize;n++)for(a=0;a<gridsize;a++)0!=n&&n!=gridsize-1&&0!=a&&a!=gridsize-1||(GRID[n][a].wall=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(n,a)),ABWorld.scene.add(t));for(var i=1;i<=NOBOXES;i++)n=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2),GRID[n][a].maze=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(n,a)),ABWorld.scene.add(t);do{n=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(n,a));ei=n,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{n=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(n,a));ai=n,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 aStar(){console.log(".... Run A* ...."),console.log(".... Enemy Current Position: ",ei,ej),console.log(".... Agent Current Position: ",ai,aj);var e,t=[],n=GRID[ei][ej],a=GRID[ai][aj];if(t.push(n),!(t.length>0))return console.log("fail - no path exists"),void noLoop();t.sort((e,t)=>e.f<t.f?1:-1),(e=t.shift()).i==a.i&&e.j==a.j&&(AB.abortRun=!0,console.log("success - found path"));for(var i=e.neighbors,o=0;o<i.length;o++){var r=i[o];r.wall||r.maze||(r.h=heuristic(r,a),r.g=e.g+AB.randomIntAtoB(0,10),r.f=r.g+r.h,r.previous=e,t.push(r))}t.sort((e,t)=>e.f>t.f?1:-1);var s=t.shift(),d=new THREE.Geometry;d.vertices.push(translate(e.i,e.j)),d.vertices.push(translate(s.i,s.j));var u=new THREE.LineBasicMaterial({color:16711680}),l=new THREE.LineSegments(d,u);return ABWorld.scene.add(l),s}function heuristic(e,t){return Math.abs(e.i-t.i)+Math.abs(e.j-t.j)}function moveLogicalEnemy(){var e=aStar();occupied(e.i,e.j)||(ei=e.i,ej=e.j)}function moveLogicalAgent(e){var t=ai,n=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?n++:e==ACTION_DOWN&&n--,occupied(t,n)||(ai=t,aj=n)}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+"   x = ("+t.toString()+")   a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),t=goodsteps/AB.step*100;AB.msg("   y = ("+e.toString()+") <br> Bad steps: "+badsteps+"   Good steps: "+goodsteps+"   Score: "+t.toFixed(2)+"% ",2)}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=!1,this.maze=!1,this.show=function(e){this.wall?(fill(wallcolor),noStroke(),rect(this.i*w,this.j*h,w,h)):e&&(fill(e),rect(this.i*w,this.j*h,w,h))},this.addNeighbors=function(e){var t=this.i,n=this.j;t<gridsize-1&&this.neighbors.push(e[t+1][n]),t>0&&this.neighbors.push(e[t-1][n]),n<gridsize-1&&this.neighbors.push(e[t][n+1]),n>0&&this.neighbors.push(e[t][n-1])}}AB.world.newRun=function(){AB.loadingScreen(),AB.runReady=!1,badsteps=0,goodsteps=0,BOXHEIGHT=1,ABWorld.init2d(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()}