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

// Cloned by Sophie Renshaw on 10 Nov 2021 from World "Complex World (clone by Sophie Renshaw) - V5" by Sophie Renshaw 
// Please leave this clone trail here.
 
AB.clockTick=1e3,AB.maxSteps=10,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",TEXTURE_PATH="/uploads/50ph/mjolnir.jpeg",gridsize=10,NOBOXES=Math.trunc(gridsize*gridsize/10),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,thepath,wall_texture,agent_texture,enemy_texture,maze_texture,path_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,o=new THREE.TextureLoader,n=new THREE.TextureLoader,t=new THREE.TextureLoader,a=new THREE.TextureLoader;e.load(TEXTURE_WALL,function(e){e.minFilter=THREE.LinearFilter,wall_texture=e,asynchFinished()&&initScene()}),o.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()}),t.load(TEXTURE_MAZE,function(e){e.minFilter=THREE.LinearFilter,maze_texture=e,asynchFinished()&&initScene()}),a.load(TEXTURE_PATH,function(e){e.minFilter=THREE.LinearFilter,path_texture=e})}function asynchFinished(){return!!(wall_texture&&agent_texture&&enemy_texture&&maze_texture)}function occupied(e,o){return ei==e&&ej==o||(ai==e&&aj==o||(GRID[e][o]==GRID_WALL||GRID[e][o]==GRID_MAZE))}function translate(e,o){var n=new THREE.Vector3;return n.y=0,n.x=e*squaresize-MAXPOS/2,n.z=o*squaresize-MAXPOS/2,n}function initScene(){var e,o,n,t;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(o=0;o<gridsize;o++)0==e||e==gridsize-1||0==o||o==gridsize-1?(GRID[e][o]={f:0,g:0,h:0,previous:void 0},GRID[e][o]=GRID_WALL,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(e,o)),ABWorld.scene.add(t)):GRID[e][o]=GRID_BLANK;for(var a=1;a<=NOBOXES;a++)e=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2),GRID[e][o]=GRID_MAZE,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(e,o)),ABWorld.scene.add(t);do{e=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,o));ei=e,ej=o,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),o=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,o));ai=e,aj=o,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(n)).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 drawPath(e,o){thepath.position.copy(translate(e,o))}function clearPath(e,o){thepath.position.copy(translate(e,o))}function heuristic(e,o){return Math.abs(o.i-e.i)+Math.abs(o.j-e.j)}function node(e,o){this.i=e,this.j=o,this.g=0,this.h=0,this.f=0}function removeFromArray(e,o){for(var n=e.length-1;n>=0;n--)e[n]==o&&e.splice(n,1)}function getNeighbors(e){var o=[];return o.push({i:e.i,j:e.j-1}),o.push({i:e.i,j:e.j+1}),o.push({i:e.i-1,j:e.j}),o.push({i:e.i+1,j:e.j}),o}function aStar(){var e=[],o=[],n=[],t=new node(ei,ej),a=new node(ai,aj);for(console.log("startNode",t),console.log("endNode",a),e.push(t);e.length>0;)for(var r=0,i=0;i<e.length;i++){e[i].f<e[r].f&&(r=i,console.log("nextBestStep",r));var s=e[r];if(console.log("currentNode",s),s.i===a.i&&s.j===a.j){for(var u=s;u.previous;)n.push(u),u=u.previous;return n.reverse()}console.log("openNodes.length",e.length),removeFromArray(e,s),o.includes(s)||o.push(s),console.log("closedNodes.length",o.length);var d=getNeighbors(s);console.log("neighbors.length",d.length),console.log("neighbors",d);for(i=0;i<d.length;i++){var c=d[i];if(console.log("neighbor",c),!o.includes(c)&&!occupied(c.i,c.j)){var l=s.g+heuristic(c,s);console.log("currentNode.g",s.g),console.log("heuristic(currentNode, neighbor)",heuristic(c,s)),console.log("tempG",l);var g=!1;e.includes(c)?l<c.g&&(c.g=l,g=!0):(console.log("neighbor",c),c.g=l,g=!0,e.push(c)),console.log("newPath",g),g&&(c.h=heuristic(a,c),console.log("neighbor.h",c.h),console.log("heuristic(endNode, neighbor)",heuristic(c,a)),c.f=c.g+c.h,console.log("neighbor.g",c.g),console.log("neighbor.f",c.f),c.previous=s,console.log("neighbor.previous",c.previous))}}}return n}function moveLogicalEnemy(){console.log("STARTING ENEMY TURN");var e,o,n=aStar();console.log("path",n),ei<ai&&(e=AB.randomIntAtoB(ei,ei+1)),ei==ai&&(e=ei),ei>ai&&(e=AB.randomIntAtoB(ei-1,ei)),ej<aj&&(o=AB.randomIntAtoB(ej,ej+1)),ej==aj&&(o=ej),ej>aj&&(o=AB.randomIntAtoB(ej-1,ej)),occupied(e,o)||(ei=e,ej=o),console.log("ENDING ENEMY TURN")}function moveLogicalAgent(e){var o=ai,n=aj;e==ACTION_LEFT?o--:e==ACTION_RIGHT?o++:e==ACTION_UP?n++:e==ACTION_DOWN&&n--,occupied(o,n)||(ai=o,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 o=AB.world.getState();AB.msg(" Step: "+AB.step+" &nbsp; x = ("+o.toString()+") &nbsp; a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),o=goodsteps/AB.step*100;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+o.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()}