Code viewer for World: Part 1: Capture Baby Yoda!...

// Cloned by Przemyslaw Majda on 29 Nov 2022 from World "Part 1: Capture Baby Yoda!! (clone by Przemyslaw Majda)" by Przemyslaw Majda 
// Please leave this clone trail here.
 


// Cloned by Przemyslaw Majda on 29 Nov 2022 from World "Part 1: Capture Baby Yoda!!" by Laura Campbell 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/lauracampbell26/wall_starwars.jpeg",TEXTURE_MAZE="/uploads/lauracampbell26/spacewall.jpeg",TEXTURE_MAZE2="/uploads/lauracampbell26/spacewall2.jpeg",TEXTURE_PATH="/uploads/lauracampbell26/red.jpeg",TEXTURE_AGENT="/uploads/lauracampbell26/grogu.jpeg",TEXTURE_ENEMY="/uploads/lauracampbell26/troop.jpeg",MUSIC_BACK="/uploads/lauracampbell26/imperial.mp3",SOUND_ALARM="/uploads/lauracampbell26/chewie.mp3",gridsize=25,NOBOXES=Math.trunc(gridsize*gridsize/15),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=1*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/lauracampbell26/indigo_ft.jpg","/uploads/lauracampbell26/indigo_bk.jpg","/uploads/lauracampbell26/indigo_up.jpg","/uploads/lauracampbell26/indigo_dn.jpg","/uploads/lauracampbell26/indigo_rt.jpg","/uploads/lauracampbell26/indigo_lf.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,GRID_MAZE2=3,GRID_MAZE3=4,sphereRadius=45,sphereHeight=10,sphereWidth=10,pathRadius=20,pathHeight=5,pathWidth=5;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,maze_texture2,path_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize),openSet=[],closedSet=[],path=[];function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,a=new THREE.TextureLoader,i=new THREE.TextureLoader,n=new THREE.TextureLoader,r=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()}),n.load(TEXTURE_MAZE2,function(e){e.minFilter=THREE.LinearFilter,maze_texture2=e,asynchFinished()&&initScene()}),r.load(TEXTURE_PATH,function(e){e.minFilter=THREE.LinearFilter,path_texture=e,asynchFinished()&&initScene()})}function asynchFinished(){return wall_texture&&agent_texture&&enemy_texture&&maze_texture&&maze_texture2&&path_texture}function occupied(e,t){return ai==e&&aj==t||(ei==e&&ej==t||(GRID[e][t].obstacle==GRID_WALL||(GRID[e][t].obstacle==GRID_MAZE||(GRID[e][t].obstacle==GRID_MAZE2||GRID[e][t].obstacle==GRID_MAZE3))))}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;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,!0),GRID[e][t].show()):GRID[e][t]=new Spot(e,t,GRID_BLANK,!0);for(var i=1;i<=NOBOXES;i++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=new Spot(e,t,GRID_MAZE,!0),GRID[e][t].show();for(var n=1;n<=NOBOXES;n++)e=AB.randomIntAtoB(1,gridsize-3),t=AB.randomIntAtoB(1,gridsize-3),GRID[e][t]=new Spot(e,t,GRID_MAZE2,!0),GRID[e][t].show();for(var r=1;r<=NOBOXES;r++)e=AB.randomIntAtoB(1,gridsize-4),t=AB.randomIntAtoB(1,gridsize-4),GRID[e][t]=new Spot(e,t,GRID_MAZE3,!0),a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),frame=new THREE.WireframeGeometry(a),lines=new THREE.LineBasicMaterial({color:"green"}),thecube=new THREE.LineSegments(frame,lines),thecube.material.transpaent=!0,thecube.material.opacity=.01,thecube.position.copy(translate(e,t)),ABWorld.scene.add(thecube);for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)GRID[e][t].addNeighbors(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.SphereGeometry(sphereRadius,sphereHeight,sphereWidth),(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.SphereGeometry(sphereRadius,sphereHeight,sphereWidth),(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 Spot(e,t,a){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.previous=void 0,this.obstacle=a,this.uniqueId,this.show=function(){shape=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),thecube=new THREE.Mesh(shape),this.obstacle==GRID_WALL?thecube.material=new THREE.MeshBasicMaterial({map:wall_texture}):this.obstacle==GRID_MAZE?thecube.material=new THREE.MeshBasicMaterial({map:maze_texture}):this.obstacle==GRID_MAZE2?thecube.material=new THREE.MeshBasicMaterial({map:maze_texture2}):this.obstacle,thecube.position.copy(translate(this.i,this.j)),ABWorld.scene.add(thecube)},this.highlight=function(e){if(e)shape=new THREE.SphereGeometry(pathRadius,pathHeight,pathWidth),theSphere=new THREE.Mesh(shape),theSphere.material=new THREE.MeshBasicMaterial({map:path_texture}),theSphere.position.copy(translate(this.i,this.j)),ABWorld.scene.add(theSphere),this.uniqueId=theSphere.uuid;else{var t=ABWorld.scene.getObjectByProperty("uuid",this.uniqueId);ABWorld.scene.remove(t),this.uniqueId=null}},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])}}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 heuristic(e,t){return Math.sqrt((ai-e)*(ai-e)+(aj-t)*(aj-t))}function heuristicTrooper(){for(const e of path)e.highlight(!1);openSet.splice(0,openSet.length),closedSet.splice(0,closedSet.length),path.splice(0,path.length);var e=GRID[ei][ej];for(openSet.push(e);openSet.length>0;){for(var t=0,a=0;a<openSet.length;a++)openSet[a].f<openSet[t].f&&(t=a);var i=openSet[t],n=GRID[ai][aj];if(i===n){for(var r=i;r.previous!==e;)r.highlight(!0),path.push(r),r=r.previous;return r}openSet.splice(openSet.indexOf(i),1),closedSet.push(i);var o=i.neighbors;for(a=0;a<o.length;a++){var s=o[a];if(!closedSet.includes(s)&&!s.obstacle){var u=i.g+heuristic(s,i),d=!1;openSet.includes(s)?u<s.g&&(s.g=u,d=!0):(s.g=u,d=!0,openSet.push(s)),d&&(s.h=heuristic(s,n),s.f=s.g+s.h,s.previous=i)}}}}function moveLogicalEnemy(){var e=heuristicTrooper();occupied(e.i,e.j)||(ei=e.i,ej=e.j)}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)}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(){const 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()}