Code viewer for World: Bot_bussiness3 (clone by A...

// Cloned by AMARA on 27 Nov 2020 from World "Bot_bussiness3" by AMARA 
// 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/3),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/akshara/white1.png","/uploads/akshara/white1.png","/uploads/akshara/white1.png","/uploads/akshara/white1.png","/uploads/akshara/white1.png","/uploads/akshara/white1.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,a=new THREE.TextureLoader,n=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()}),a.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()}),n.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 a=new THREE.Vector3;return a.y=0,a.x=e*squaresize-MAXPOS/2,a.z=t*squaresize-MAXPOS/2,a}function initScene(){var e,t,a,n;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,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(n=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:wall_texture}),n.position.copy(translate(e,t)),ABWorld.scene.add(n)):GRID[e][t]=GRID_BLANK;for(var o=1;o<=NOBOXES;o++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(n=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:maze_texture}),n.position.copy(translate(e,t)),ABWorld.scene.add(n);do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));ei=e,ej=t,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(a)).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,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(a)).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 moveLogicalEnemy(){if(pathFound)var e=path[path.length-2].i,t=path[path.length-2].j;occupied(e,t)||(ei=e,ej=t)}function moveLogicalAgent(e){var t=ai,a=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?a++:e==ACTION_DOWN&&a--,occupied(t,a)||(ai=t,aj=a)}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)}var start,end,w,h,rows=gridsize,cols=gridsize,canvasW=MAXPOS,canvasH=MAXPOS,grid=new Array(cols),openSet=[],closedSet=[],path=[],pathFound=!1;function removeFromArray(e,t){for(var a=e.length-1;a>=0;a--)e[a]==t&&e.splice(a,1)}function heuristic(e,t){return Math.abs(e.i-t.i)+Math.abs(e.j-t.j)}function Spot(e,t){this.f=0,this.g=0,this.h=0,this.i=e,this.j=t,this.wall=!1,GRID[e][t]!=GRID_WALL&&GRID[e][t]!=GRID_MAZE||(this.wall=!0),this.neighbors=[],this.addNeighbors=function(){var e=this.i,t=this.j;e<cols-1&&this.neighbors.push(grid[e+1][t]),e>0&&this.neighbors.push(grid[e-1][t]),t<rows-1&&this.neighbors.push(grid[e][t+1]),t>0&&this.neighbors.push(grid[e][t-1])}}function setup(){console.log("A*"),w=canvasW/cols,h=canvasH/rows;for(var e=0;e<cols;e++)grid[e]=new Array(rows);for(e=0;e<cols;e++)for(var t=0;t<rows;t++)grid[e][t]=new Spot(e,t);for(e=0;e<cols;e++)for(t=0;t<rows;t++)grid[e][t].addNeighbors(grid);start=grid[ei][ej],end=grid[ai][aj],openSet.push(start)}function draw(){console.log("enetered");for(var e=!0;e;){if(!(openSet.length>0)){console.log("no solution"),e=!1;break}for(var t=0,a=0;a<openSet.length;a++)openSet[a].f<openSet[t].f&&(t=a);var n=openSet[t];if(n===end){e=!1,pathFound=!0;patH();console.log(path),console.log(path[path.length-2]),console.log("DONE");break}removeFromArray(openSet,n),closedSet.push(n);var o=n.neighbors;for(a=0;a<o.length;a++){var r=o[a];r.i,r.j;if(!closedSet.includes(r)&&!r.wall){var i=n.g+1,s=!1;openSet.includes(r)?i<r.g&&(r.g=i,s=!0):(r.g=i,s=!0,openSet.push(r)),s&&(r.h=heuristic(r,end),r.f=r.g+r.h,r.previous=n)}}}}function patH(){path=[];var e=grid[ai][aj];for(path.push(e);e.previous;)path.push(e.previous),e=e.previous;return path}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&&(setup(),draw(),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()}