// Cloned by rohan on 13 Nov 2021 from World "SacryEnemy_DefaultAgent_By_RajeshDande" by Rajesh Dande
// Please leave this clone trail here.
AB.clockTick=100,AB.maxSteps=250,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/rajeshdande9/wall_pintrest.jpg",TEXTURE_MAZE="/uploads/rajeshdande9/Brick_white_wall_wikipedia_commons.jpg",TEXTURE_AGENT="/uploads/rajeshdande9/human_mind_vectorstock.jpg",TEXTURE_ENEMY="/uploads/rajeshdande9/AI_Scary_ghost_Pintrest.jpg",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/rajeshdande9/Created1.jpg","/uploads/rajeshdande9/test2.jpg","/uploads/rajeshdande9/grass_texture241_Bgfons.jpg","/uploads/rajeshdande9/grass_texture241_Bgfons.jpg","/uploads/rajeshdande9/Created1.jpg","/uploads/rajeshdande9/test2.jpg"],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,theenemypath,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,drawLine,GRID=new Array(gridsize),invalidRun=!1;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].wallmaze)}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 heuristic1(e,t){const a=new THREE.Vector3(e.i,0,e.j),i=new THREE.Vector3(t.i,0,t.j);return a.distanceTo(i)}function Spot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.wallmaze=!1,this.neighbors=[],this.previous=void 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]),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])},this.removelinks=function(e){var t=this.i,a=this.j;e[t][a].previous=void 0}}function removeFromArray(e,t){for(var a=e.length-1;a>=0;a--)e[a]==t&&e.splice(a,1)}function initScene(){var e,t;for(a=0;a<gridsize;a++)GRID[a]=new Array(gridsize);for(var a=0;a<gridsize;a++)for(var i=0;i<gridsize;i++)GRID[a][i]=new Spot(a,i);for(a=0;a<gridsize;a++)for(i=0;i<gridsize;i++)GRID[a][i].addNeighbors(GRID);for(a=0;a<gridsize;a++)for(i=0;i<gridsize;i++)0===a||a==gridsize-1||0===i||i==gridsize-1?(GRID[a][i].wallmaze=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(a,i)),ABWorld.scene.add(t)):GRID[a][i].wallmaze=!1;for(var r=1;r<=NOBOXES;r++)a=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2),GRID[a][i].wallmaze=!0,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(a,i)),ABWorld.scene.add(t);do{a=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(a,i));ei=a,ej=i,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{a=AB.randomIntAtoB(1,gridsize-2),i=AB.randomIntAtoB(1,gridsize-2)}while(occupied(a,i));ai=a,aj=i,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(e)).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 drawPathToAgent(e){var t,a,i=[];for(l=e.length-1;l>0;l--)t=e[l-1].i,a=e[l-1].j,i.push(translate(t,a));var r=(new THREE.Geometry).setFromPoints(i),n=new THREE.LineBasicMaterial({color:"red"}),s=new THREE.Line(r,n);return ABWorld.scene.add(s),s}function moveLogicalEnemy(){var e,t=GRID[ei][ej],a=GRID[ai][aj],i=[],r=[],n=[],s=gridsize*gridsize;drawLine&&ABWorld.scene.remove(drawLine),i.push(t),console.log("start search"),console.log("Enemy and agents are at EI : "+ei+" EJ: "+ej+" AI : "+ai+" AJ: "+aj);for(var o=0;o<s;o++)if(i.length>0){for(var d=0,u=0;u<i.length;u++)i[u].f<i[d].f&&(d=u);var l=i[d];if(1==heuristic1(l,a)){console.log("Success - found path till agent !!"),e=l;break}removeFromArray(i,l),r.push(l);var c=l.neighbors;for(u=0;u<c.length;u++){var g=c[u];if(!r.includes(g)&&!g.wallmaze){var h=l.g+heuristic1(g,l),A=!1;i.includes(g)?h<g.g&&(g.g=h,A=!0):(g.g=h,A=!0,i.push(g)),A&&(g.h=heuristic1(g,a),g.f=g.g+g.h,g.previous=l)}}}else console.log("Fail - no path exists till Agent :( "),invalidRun=!0,AB.abortRun=!0;for(n.push(e);e.previous;)n.push(e.previous),e=e.previous;drawLine=drawPathToAgent(n);var p=n.length-2,E=0,m=0;0==Math.abs(ei-ai)&&Math.abs(ej-aj)<=1||Math.abs(ei-ai)<=1&&0==Math.abs(ej-aj)?(E=ei,m=ej):(E=n[p].i,m=n[p].j),ei=E,ej=m;for(u=0;u<gridsize;u++)for(var B=0;B<gridsize;B++)GRID[u][B].removelinks(GRID)}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+" x = ("+t.toString()+") a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),t=goodsteps/AB.step*100;AB.msg(" y = ("+e.toString()+") <br> Bad steps: "+badsteps+" Good steps: "+goodsteps+" 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?invalidRun?AB.msg(" <br> <font color=blue> <B> Enemy to Agent path does not exist. Please reload.</B> </font> ",3):AB.msg(" <br> <font color=red> <B> Agent trapped. Final score zero.</B> </font> ",3):AB.msg(" <br> <font color=green> <B> Run over. Max steps reached.</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()}