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

// Cloned by Fergus on 23 Nov 2020 from World "Complex World (clone by Fergus)" by Fergus 
// Please leave this clone trail here.
 
AB.clockTick=100,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/starter/ghost.3.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=20;var cols=gridsize,rows=gridsize;const NOBOXES=0,squaresize=100;var w=squaresize,h=squaresize;const 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,thepath,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,pi,pj,badsteps,goodsteps,GRID=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,o=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()}),o.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||(GRID[e][t]==GRID_WALL||GRID[e][t]==GRID_MAZE))}function translate(e,t){var o=new THREE.Vector3;return o.y=0,o.x=e*squaresize-MAXPOS/2,o.z=t*squaresize-MAXPOS/2,o}function initScene(){var e,t,o,a;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,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(o)).material=new THREE.MeshBasicMaterial({map:wall_texture}),a.position.copy(translate(e,t)),ABWorld.scene.add(a)):GRID[e][t]=GRID_BLANK;for(var n=1;n<=NOBOXES;n++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(o)).material=new THREE.MeshBasicMaterial({map:maze_texture}),a.position.copy(translate(e,t)),ABWorld.scene.add(a);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(o)).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,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(o)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),pi[0]=ei+1,pj[0]=ej+1,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(thepath=new THREE.Mesh(o)).material=new THREE.MeshBasicMaterial({color:16776960}),ABWorld.scene.add(thepath),drawPath(),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 drawPath(){thepath.position.copy(translate(pi,pj)),ABWorld.lookat.copy(thepath.position)}const diagonal=!0,backcolor="white",wallcolor="black",pathcolor="darkred",opencolor="lightgreen",closedcolor="lightpink";var start,end,openSet=[],closedSet=[],path=[],agrid=new Array(cols);function heuristic(e,t){return diagonal?AB.distance2D(e.i,e.j,t.i,t.j):abs(e.i-t.i)+abs(e.j-t.j)}function removeFromArray(e,t){for(var o=e.length-1;o>=0;o--)e[o]==t&&e.splice(o,1)}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.addNeighbors=function(e){var t=this.i,o=this.j;t<cols-1&&this.neighbors.push(e[t+1][o]),t>0&&this.neighbors.push(e[t-1][o]),o<rows-1&&this.neighbors.push(e[t][o+1]),o>0&&this.neighbors.push(e[t][o-1]),diagonal&&(t>0&&o>0&&this.neighbors.push(e[t-1][o-1]),t<cols-1&&o>0&&this.neighbors.push(e[t+1][o-1]),t>0&&o<rows-1&&this.neighbors.push(e[t-1][o+1]),t<cols-1&&o<rows-1&&this.neighbors.push(e[t+1][o+1]))}}function a_star_setup(e,t){for(var o=0;o<cols;o++)agrid[o]=new Array(rows);for(o=0;o<cols;o++)for(var a=0;a<rows;a++)agrid[o][a]=new Spot(o,a);for(o=0;o<cols;o++)for(a=0;a<rows;a++)agrid[o][a].addNeighbors(agrid);start=agrid[e.i][e.j],end=agrid[t.i][t.j],start.wall=!1,end.wall=!1,openSet.push(start),console.log("start search"),draw()}function draw(){if(!(openSet.length>0))return console.log("fail - no path exists"),void noLoop();for(var e=0,t=0;t<openSet.length;t++)openSet[t].f<openSet[e].f&&(e=t);var o=openSet[e];o===end&&(noLoop(),console.log("success - found path")),removeFromArray(openSet,o),closedSet.push(o);var a=o.neighbors;for(t=0;t<a.length;t++){var n=a[t];if(!closedSet.includes(n)&&!n.wall){var r=o.g+heuristic(n,o),s=!1;openSet.includes(n)?r<n.g&&(n.g=r,s=!0):(n.g=r,s=!0,openSet.push(n)),s&&(n.h=heuristic(n,end),n.f=n.g+n.h,n.previous=o)}}for(t=0;t<cols;t++)for(var i=0;i<rows;i++)for(t=0;t<closedSet.length;t++)for(t=0;t<openSet.length;t++)path=[];var d=o;for(path.push(d);d.previous;)path.push(d.previous),d=d.previous}function moveLogicalEnemy(){var e,t;enemy_pos=new Array(2),agent_pos=new Array(2),enemy_pos.i=ei,enemy_pos.j=ej,agent_pos.i=ai,agent_pos.j=aj,f=heuristic(enemy_pos,agent_pos),a_star_setup(enemy_pos,agent_pos),AB.msg(" distance a and e: "+f+"&nbsp;",4),ei<ai&&(e=AB.randomIntAtoB(ei,ei+1)),ei==ai&&(e=ei),ei>ai&&(e=AB.randomIntAtoB(ei-1,ei)),ej<aj&&(t=AB.randomIntAtoB(ej,ej+1)),ej==aj&&(t=ej),ej>aj&&(t=AB.randomIntAtoB(ej-1,ej)),occupied(e,t)||(ei=e,ej=t)}function moveLogicalAgent(e){var t=ai,o=aj;occupied(t,o)||(ai=t,aj=o)}AB.clockTick=100,AB.maxSteps=1e3;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()}