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

// Cloned by Musthaq Ahmed on 12 Nov 2022 from World "Complex World (clone by Musthaq Ahmed)" by Musthaq Ahmed 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const WALLMOVENO=10,WALLMOVETICK=0,show3d=!1,TEXTURE_WALL="/uploads/musthaqahmed4/star.jpg",TEXTURE_MAZE="/uploads/musthaqahmed4/brick.jpg",TEXTURE_AGENT="/uploads/musthaqahmed4/agent_.png",TEXTURE_ENEMY="/uploads/musthaqahmed4/enemy_.jpg",MUSIC_BACK="/uploads/musthaqahmed4/RushtotheEnd-StevenOBrienMustCreditCC-BYwww.steven-obrien.net.mp3",SOUND_ALARM="/uploads/musthaqahmed4/MLGAirHorn-SoundEffect.mp3",gridsize=20,NOBOXES=Math.trunc(gridsize*gridsize/5),squaresize=10,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;let initialGrid=[];ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/musthaqahmed4/posx.jpg","/uploads/musthaqahmed4/negx_.jpg","/uploads/musthaqahmed4/posy.jpg","/uploads/musthaqahmed4/negy.jpg","/uploads/musthaqahmed4/posz.jpg","/uploads/musthaqahmed4/negz.jpg"],DIAGONAL=!1,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,start,end,GRID=new Array(gridsize),openSet=[],bisSet=[],path=[],oldpath=[];function dist(e,t,a,i){var n=e-a,o=t-i;return Math.sqrt(n*n+o*o)}function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,a=new THREE.TextureLoader,i=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()}),i.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].wall&&!GRID[e][t].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 heuristic(e,t){return DIAGONAL?dist(e.i,e.j,t.i,t.j):Math.abs(e.i-t.i)+Math.abs(e.j-t.j)}function gfn(e,t){return DIAGONAL?dist(e.i,e.j,t.i,t.j):Math.abs(e.i-t.i)+Math.abs(e.j-t.j)}function removeFromArray(e,t){for(var a=e.length-1;a>=0;a--)e[a]==t&&e.splice(a,1)}function Spot(e,t,a=GRID_BLANK){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.wall=!1,this.maze=!1,this.previous=void 0,a==GRID_WALL&&(this.wall=!0),a==GRID_MAZE&&(this.maze=!0),this.addNeighbors=function(e){var t=this.i,a=this.j;t<gridsize-1&&this.neighbors.push(e[t+1][a]),t>0&&this.neighbors.push(e[t-1][a]),a<gridsize-1&&this.neighbors.push(e[t][a+1]),a>0&&this.neighbors.push(e[t][a-1]),DIAGONAL&&(t>0&&a>0&&this.neighbors.push(e[t-1][a-1]),t<gridsize-1&&a>0&&this.neighbors.push(e[t+1][a-1]),t>0&&a<gridsize-1&&this.neighbors.push(e[t-1][a+1]),t<gridsize-1&&a<gridsize-1&&this.neighbors.push(e[t+1][a+1]))}}function initScene(){var e,t,a,i;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]=new Spot(e,t,GRID_WALL),a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:wall_texture}),i.position.copy(translate(e,t)),ABWorld.scene.add(i)):GRID[e][t]=new Spot(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]=new Spot(e,t,GRID_MAZE),a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(i=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:maze_texture}),i.position.copy(translate(e,t)),ABWorld.scene.add(i);for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)GRID[e][t].addNeighbors(GRID);initialGrid=[...GRID];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,console.log("Start Position",ai,aj),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 resetSearch(){for(i=0;i<gridsize;i++)for(j=0;j<gridsize;j++)GRID[i][j].previous=void 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(){for(let t of oldPath){var e=ABWorld.scene.getObjectByName("PathObject"+t.i+t.j);ABWorld.scene.remove(e)}for(let e of path){var t=new THREE.BoxGeometry(squaresize,0,squaresize),a=new THREE.MeshBasicMaterial({color:"#ff0000"});a.transparent=!0,a.opacity=.5;var i=new THREE.Mesh(t,a);i.name="PathObject"+e.i+e.j,i.position.copy(translate(e.i,e.j)),ABWorld.scene.add(i)}}function moveLogicalEnemy(){closedSet=[],openSet=[],start=GRID[ei][ej],end=GRID[ai][aj],console.log(GRID[ei][ej],GRID[ai][aj],ai,aj,ei,ej),openSet.push(start);for(var e=null;;){if(!(openSet.length>0)){console.log("No Route found");break}console.log("open set -",openSet);for(var t=0,a=0;a<openSet.length;a++)openSet[a].f<openSet[t].f&&(t=a);if((e=openSet[t])===end){!0;break}removeFromArray(openSet,e),closedSet.push(e);var i=e.neighbors;for(a=0;a<i.length;a++){var n=i[a];if(!closedSet.includes(n)&&!n.wall&&!n.maze){var o=e.g+gfn(n,e),s=!1;openSet.includes(n)?o<n.g&&(n.g=o,s=!0):(n.g=o,s=!0,openSet.push(n)),s&&(n.h=heuristic(n,end),n.f=n.g+n.h,n.previous=e)}}}path.length>0&&(oldPath=[...path]);var r=e;for((path=[]).push(r);r.previous;)r.previous!=start&&r.previous!=end&&path.push(r.previous),r=r.previous;console.log("Path Found ",path),ei=path[path.length-1].i,ej=path[path.length-1].j,resetSearch()}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)}function movewalls(){i>width&&println("off the screen")}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=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(),path.length>0&&(console.log(path),drawPath(path)),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()}