// Cloned by rohan on 13 Nov 2021 from World "A Star in Space" by Andrew Merrigan
// Please leave this clone trail here.
AB.clockTick=80,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/andrewmerrigan/wall1_edited.jpg",TEXTURE_MAZE="/uploads/andrewmerrigan/wall2_edited.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=30,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/andrewmerrigan/space_neg_z.jpg","/uploads/andrewmerrigan/space_neg_x.jpg","/uploads/andrewmerrigan/space_neg_x.jpg","/uploads/andrewmerrigan/space_pos_z.jpg","/uploads/andrewmerrigan/space_pos_x.jpg","/uploads/andrewmerrigan/space_pos_x.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,geometry,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,a=new THREE.TextureLoader,t=new THREE.TextureLoader,n=new THREE.TextureLoader;e.load(TEXTURE_WALL,function(e){e.minFilter=THREE.LinearFilter,wall_texture=e,asynchFinished()&&initScene()}),a.load(TEXTURE_AGENT,function(e){e.minFilter=THREE.LinearFilter,agent_texture=e,asynchFinished()&&initScene()}),t.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,a){return ei==e&&ej==a||(ai==e&&aj==a||(GRID[e][a]==GRID_WALL||GRID[e][a]==GRID_MAZE))}function translate(e,a){var t=new THREE.Vector3;return t.y=0,t.x=e*squaresize-MAXPOS/2,t.z=a*squaresize-MAXPOS/2,t}function initScene(){var e,a,t,n;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(a=0;a<gridsize;a++)0==e||e==gridsize-1||0==a||a==gridsize-1?(GRID[e][a]=GRID_WALL,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(n=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:wall_texture}),n.position.copy(translate(e,a)),ABWorld.scene.add(n)):GRID[e][a]=GRID_BLANK;for(var r=1;r<=NOBOXES;r++)e=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2),GRID[e][a]=GRID_MAZE,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(n=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:maze_texture}),n.position.copy(translate(e,a)),ABWorld.scene.add(n);do{e=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,a));ei=e,ej=a,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{e=AB.randomIntAtoB(1,gridsize-2),a=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,a));ai=e,aj=a,t=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(t)).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 canMove(e,a){return GRID[e][a]!=GRID_WALL&&GRID[e][a]!=GRID_MAZE}function aStar(e,a,t,n){var r=new Set,o=new Set,i=(e,a)=>{var r=Math.abs(e.i-a.i),o=Math.abs(e.i-a.i),i=r+o,s=e.i===t&&e.j===n||a.i===t&&e.j===n;return i<3&&1===r&&1===o&&s?3:i},s=[],d=(e,a)=>(s[e]||(s[e]=[]),s[e][a]||(s[e][a]={i:e,j:a,f:0,g:0,h:0,neighbors:[],previous:void 0}),s[e][a]),u=d(e,a),c=d(t,n);for(r.add(u);r.size>0;){var p=r.values().next().value;for(let e of r)e.f<p.f&&(p=e);var l=p.i,A=p.j;if(p.i===c.i&&p.j===c.j){var g=[];for(previous=p.previous;previous;)g.unshift(previous),previous=previous.previous;return g}r.delete(p),o.add(p);var E=[];canMove(l+1,A)&&E.push(d(l+1,A)),canMove(l-1,A)&&E.push(d(l-1,A)),canMove(l,A+1)&&E.push(d(l,A+1)),canMove(l,A-1)&&E.push(d(l,A-1)),canMove(l-1,A-1)&&E.push(d(l-1,A-1)),canMove(l+1,A-1)&&E.push(d(l+1,A-1)),canMove(l-1,A+1)&&E.push(d(l-1,A+1)),canMove(l+1,A+1)&&E.push(d(l+1,A+1));for(let e of E)if(!o.has(e)){var m=p.g+i(e,p),B=!1;r.has(e)?m<e.g&&(e.g=m,B=!0):(e.g=m,B=!0,r.add(e)),B&&(e.h=i(e,c),e.f=e.g+e.h,e.previous=p)}}}var path,line,oldAgentI=-1,oldAgentJ=-1,pathIndex=1;function showPath(){ABWorld.scene.remove(line),geometry=new THREE.Geometry,(line=new THREE.Line(geometry)).material=new THREE.LineBasicMaterial({color:16458755,linewidth:1e3});for(let e of path)geometry.vertices.push(translate(e.i,e.j));geometry.vertices.push(translate(ai,aj)),ABWorld.scene.add(line)}function moveLogicalEnemy(){ai===oldAgentI&&aj===oldAgentJ||(path=aStar(ei,ej,ai,aj),pathIndex=1,showPath()),oldAgentI=ai,oldAgentJ=aj;var e=path[pathIndex];e&&!occupied(e.i,e.j)?(ei=e.i,ej=e.j,pathIndex+=1):console.log("Skipping this turn")}function moveLogicalAgent(e){var a=ai,t=aj;e==ACTION_LEFT?a--:e==ACTION_RIGHT?a++:e==ACTION_UP?t++:e==ACTION_DOWN&&t--,occupied(a,t)||(ai=a,aj=t)}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 a=AB.world.getState();AB.msg(" Step: "+AB.step+" x = ("+a.toString()+") a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),a=goodsteps/AB.step*100;AB.msg(" y = ("+e.toString()+") <br> Bad steps: "+badsteps+" Good steps: "+goodsteps+" Score: "+a.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()}