Code viewer for World: Hound 2 - Moving walls (cl...

// Cloned by Abdelshafa Abdala on 5 Nov 2022 from World "Hound 2 - Moving walls" by Paul R 
// Please leave this clone trail here.
 
AB.clockTick=1e3,AB.maxSteps=1e3,AB.screenshotStep=100;const show3d=!0,TEXTURE_WALL="/uploads/paul79/wall2.jpg",TEXTURE_MAZE="/uploads/starter/latin.jpg",TEXTURE_AGENT="/uploads/paul79/rabbit-128.png",TEXTURE_ENEMY="/uploads/paul79/greyhound-silhouette.jpg",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",WALLMOVENO=150,WALLMOVETICK=4,gridsize=40,NOBOXES=Math.trunc(gridsize*gridsize/5),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=1*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/starter/dawnmountain-xpos.png","/uploads/starter/dawnmountain-xneg.png","/uploads/starter/dawnmountain-ypos.png","/uploads/starter/dawnmountain-yneg.png","/uploads/starter/dawnmountain-zpos.png","/uploads/starter/dawnmountain-zneg.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,VERBOSE_LOG=!1;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize),MAZE_BLOCKS=[];function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,a=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()}),r.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,r;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),(r=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:wall_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r)):GRID[e][t]=GRID_BLANK;for(var i=1;i<=NOBOXES;i++){do{e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,t));GRID[e][t]=GRID_MAZE,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:maze_texture}),r.position.copy(translate(e,t)),ABWorld.scene.add(r),MAZE_BLOCKS.push({i:e,j:t,cube:r})}if(VERBOSE_LOG){let e="Initial block pos: ";for(var n=0;n<MAZE_BLOCKS.length;n++)e=e+"["+MAZE_BLOCKS[n].i+","+MAZE_BLOCKS[n].j+"]";console.log(e)}do{e=AB.randomIntAtoB(1,10),t=AB.randomIntAtoB(1,10)}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(gridsize-10,gridsize-2),t=AB.randomIntAtoB(gridsize-10,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(),aStarSetup(),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)}var trail_lights=new Array;function moveEnemyA_Star(){console.log("Search for shortest path");let e=getPathToAgent(ai,aj,ei,ej);if(VERBOSE_LOG){console.log("agent pos:"+ai+" "+aj+"  enemy pos:"+ei+" "+ej);for(var t="[",a=0;a<e.length;a++){let i=!1;for(var r=0;r<MAZE_BLOCKS.length;r++)MAZE_BLOCKS[r].i==e[a].i&&MAZE_BLOCKS[r].j==e[a].j&&(i=!0);t=t+"["+e[a].i+","+e[a].j+","+i+"]"}t+="]",console.log(t)}for(e.length>=2&&!occupied(e[1].i,e[1].j)&&(ei=e[1].i,ej=e[1].j);trail_lights.length>0;){let e=trail_lights.pop();ABWorld.scene.remove(e)}for(let t=1;t<e.length-1;t++){const a=new THREE.SphereGeometry(20,32),r=new THREE.MeshBasicMaterial({color:255});let i=new THREE.Mesh(a,r);ABWorld.scene.add(i),i.position.copy(translate(e[t].i,e[t].j)),trail_lights.push(i)}}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){var t;if(updateStatusBefore(e),console.log("Move some blocks"),AB.step%4==0){for(var a=0;a<Math.min(150,NOBOXES);a++){let e,r,i=MAZE_BLOCKS[a];do{e=AB.randomIntAtoB(1,gridsize-2),r=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,r));GRID[e][r]=GRID_MAZE,GRID[i.i][i.j]=GRID_BLANK,VERBOSE_LOG&&(t=t+"["+i.i+","+i.j+"] -> ["+e+","+r+"]"),i.cube.position.copy(translate(e,r)),i.i=e,i.j=r}if(VERBOSE_LOG){console.log("Position changes: "+t),t="New block pos: ";for(a=0;a<MAZE_BLOCKS.length;a++)t=t+"["+MAZE_BLOCKS[a].i+","+MAZE_BLOCKS[a].j+"]";console.log(t)}}moveLogicalAgent(e),moveEnemyA_Star(),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()}const aStarGrid=new Array(gridsize-2);function aStarSetup(){for(var e=0;e<gridsize-2;e++)aStarGrid[e]=new Array(gridsize-2);for(e=0;e<gridsize-2;e++)for(var t=0;t<gridsize-2;t++)aStarGrid[e][t]=new AStarCell(e,t,GRID[e+1][t+1]==GRID_MAZE),e>0&&(aStarGrid[e][t].addNeighbour(aStarGrid[e-1][t]),aStarGrid[e-1][t].addNeighbour(aStarGrid[e][t])),t>0&&(aStarGrid[e][t].addNeighbour(aStarGrid[e][t-1]),aStarGrid[e][t-1].addNeighbour(aStarGrid[e][t]))}function AStarCell(e,t,a){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbours=[],this.previous=void 0,this.maze=a,this.addNeighbour=function(e){this.neighbours.push(e)},this.reset=function(e){this.f=0,this.g=0,this.h=0,this.previous=void 0,this.maze=e}}function heuristic(e,t){return Math.abs(e.i-t.i)+Math.abs(e.j-t.j)}function getPathToAgent(e,t,a,r){let i,n=e-1,o=t-1,s=r-1,d=[],u=[],l=aStarGrid[a-1][s],c=aStarGrid[n][o];for(let e=0;e<gridsize-2;e++)for(let t=0;t<gridsize-2;t++)aStarGrid[e][t].reset(GRID[e+1][t+1]==GRID_MAZE);d.push(l);let A=e=>e==i;for(;d.length>0;){let e=0;for(let t=0;t<d.length;t++)d[t].f<d[e].f&&(e=t);if((i=d[e])===c){console.log("success - found path");break}d.splice(d.findIndex(A),1),u.push(i);for(let e=0;e<i.neighbours.length;e++){let t=i.neighbours[e];if(!u.includes(t)&&!t.maze){let e=i.g+heuristic(t,i),a=d.includes(t);(!a||e<t.g)&&(t.g=e,t.h=heuristic(t,c),t.f=t.g+t.h,t.previous=i),a||d.push(t)}}}if(0==d.length)return console.log("failure - no path found."),[];let g=[],p=i;for(g.push(p);p.previous;)g.push(p.previous),p=p.previous;let E=new Array;for(let e=g.length-1;e>=0;e--)E.push({i:g[e].i+1,j:g[e].j+1});return E}