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

// Cloned by Bob Chen on 28 Nov 2020 from World "Complex World (clone by Yuwei Chen)" by Yuwei Chen 
// 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=50,NOBOXES=Math.trunc(gridsize*gridsize/10),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,PATHCOLOR=65280,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,pathLine,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||(GRID[e][t]==GRID_WALL||GRID[e][t]==GRID_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,n,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,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:wall_texture}),a.position.copy(translate(e,t)),ABWorld.scene.add(a)):GRID[e][t]=GRID_BLANK;for(var r=1;r<=NOBOXES;r++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(n)).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,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(n)).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,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent);var o=new THREE.Geometry,i=new THREE.MeshBasicMaterial({color:PATHCOLOR});pathLine=new THREE.Line(o,i),ABWorld.scene.add(pathLine),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 drawPath(e){var t;if(pathLine&&ABWorld.scene.remove(pathLine),!(e.length<2)){var n=[];for(const a of e)(t=translate(a.x,a.y)).y=1,n.push(t);var a=(new THREE.BufferGeometry).setFromPoints(n),r=new THREE.LineBasicMaterial({color:PATHCOLOR});pathLine=new THREE.Line(a,r),ABWorld.scene.add(pathLine)}}function moveLogicalEnemy(){var e=searchRoad(ai,aj,ei,ej,GRID);drawPath(e),e.length>0&&(ei=e[e.length-1].x,ej=e[e.length-1].y)}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+" &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)}function searchRoad(e,t,n,a,r){var o,i=[],s=[],d=[];i.push({x:e,y:t,G:0});do{var u=i.pop();s.push(u);var c=SurroundPoint(u);for(var A in c){var l=c[A];if(l.x>=0&&l.y>=0&&l.x<gridsize&&l.y<gridsize&&0===r[l.x][l.y]&&!existList(l,s)&&0===r[l.x][u.y]&&0===r[u.x][l.y]){var p=u.G+((u.x-l.x)*(u.y-l.y)==0?10:14);if(existList(l,i)){var E=existList(l,i);p<i[E].G&&(i[E].parent=u,i[E].G=p,i[E].F=p+i[E].H)}else l.H=10*Math.abs(n-l.x)+10*Math.abs(a-l.y),l.G=p,l.F=l.H+l.G,l.parent=u,i.push(l)}}if(0===i.length)break;i.sort(sortF)}while(!(o=existList({x:n,y:a},i)));if(o){var g=i[o];do{d.unshift({x:g.x,y:g.y}),g=g.parent}while(g.x!=e||g.y!=t)}else d=[];return d}function sortF(e,t){return t.F-e.F}function SurroundPoint(e){var t=e.x,n=e.y;return[{x:t-1,y:n-1},{x:t,y:n-1},{x:t+1,y:n-1},{x:t+1,y:n},{x:t+1,y:n+1},{x:t,y:n+1},{x:t-1,y:n+1},{x:t-1,y:n}]}function existList(e,t){for(var n in t)if(e.x==t[n].x&&e.y==t[n].y)return n;return!1}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()}