Code viewer for World: Practical 1 Complex World ...

// Cloned by Kilian C on 26 Oct 2022 from World "Practical 1 Complex World (clone by Kilian C) add second grid for a*(clone by Kilian C)" by Kilian C 
// Please leave this clone trail here.
 
AB.clockTick=1e3,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!1,TEXTURE_WALL="/uploads/starter/door.jpg",TEXTURE_MAZE="/uploads/starter/latin.jpg",TEXTURE_AGENT="uploads/kcarolan96/white_pebble.jpg",TEXTURE_ENEMY="uploads/kcarolan96/blackpebble.jpg",gridsize=25,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/starter/sky_pos_z.jpg","/uploads/starter/sky_neg_z.jpg","/uploads/starter/sky_pos_y.jpg","/uploads/starter/sky_neg_y.jpg","/uploads/starter/sky_pos_x.jpg","/uploads/starter/sky_neg_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 start,end,BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,Draw,openSet=[],closedSet=[],path=[],GRID=new Array(gridsize),grid2AStar=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,r=new THREE.TextureLoader,n=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()}),r.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 manhatHeuristic(e,t){return dist=Math.abs(e.i-t.i)+Math.abs(e.j-t.j),console.log("manhat dist is: "+dist),dist}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 r=new THREE.Vector3;return r.y=0,r.x=e*squaresize-MAXPOS/2,r.z=t*squaresize-MAXPOS/2,r}function removeFromArray(e,t){for(var r=e.length-1;r>=0;r--)e[r]==t&&e.splice(r,1)}function Spot(e,t){this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbours=[],this.previous=void 0,this.addNeighbours=function(e){var t=this.i,r=this.j;t<gridsize-1&&this.neighbours.push(e[t+1][r]),t>0&&this.neighbours.push(e[t-1][r]),r<gridsize-1&&this.neighbours.push(e[t][r+1]),r>0&&this.neighbours.push(e[t][r-1])},this.checkForObstavle}function initScene(){var e,t;for(r=0;r<gridsize;r++)GRID[r]=new Array(gridsize);for(r=0;r<gridsize;r++)grid2AStar[r]=new Array(gridsize);for(var r=0;r<gridsize;r++)for(var n=0;n<gridsize;n++)grid2AStar[r][n]=new Spot(r,n);for(r=0;r<gridsize;r++)for(n=0;n<gridsize;n++)grid2AStar[r][n].addNeighbours(GRID);for(r=0;r<gridsize;r++)for(n=0;n<gridsize;n++)0===r||r==gridsize-1||0===n||n==gridsize-1?(GRID[r][n]=GRID_WALL,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:wall_texture}),t.position.copy(translate(r,n)),ABWorld.scene.add(t)):GRID[r][n]=GRID_BLANK;for(var a=1;a<=NOBOXES;a++)r=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2),GRID[r][n]=GRID_MAZE,e=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(t=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:maze_texture}),t.position.copy(translate(r,n)),ABWorld.scene.add(t);do{r=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(r,n));ej=n,start=grid2AStar[ei=r][ej],e=new THREE.CylinderGeometry(squaresize/2,squaresize/2,BOXHEIGHT),(theenemy=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{r=AB.randomIntAtoB(1,gridsize-2),n=AB.randomIntAtoB(1,gridsize-2)}while(occupied(r,n));aj=n,end=grid2AStar[ai=r][aj],e=new THREE.OctahedronGeometry(.5*squaresize),(theagent=new THREE.Mesh(e)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),openSet.push(start),ABWorld.scene.background=(new THREE.CubeTextureLoader).load(SKYBOX_ARRAY,function(){ABWorld.render(),AB.removeLoading(),AB.runReady=!0})}function drawPath(e){var t,r,n=[];pathCol="#2FF924";for(var a=e.length;a>0;a--)t=e.i,r=e.j,n=n.push(t,r);const o=new THREE.LineBasicMaterial({color:pathCol,linewidth:1});var i=(new THREE.BufferGeometry).setFromPoints(n);return line=new Three.Line(i,o),console.log(n)}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 moveLogicalEnemy(){for(Draw&&ABWorld.scene.remove(Draw);openSet.length>0;){for(var e=0,t=0;t<openSet.length;t++)openSet[t].f<openSet[e].f&&(e=t);var r=openSet[e];if(r===end){var n=r;for((path=[]).push(n);n.previous;)path.push(n.previous),n=n.previous;drawPath(path),console.log("Done")}removeFromArray(openSet,r),closedSet.push(r);var a=r.neighbours;console.log(a.length);for(t=0;t<a.length;t++){var o=a[t];if(!closedSet.includes(o)){var i=r.g+1;openSet.includes(o)&&(i<o.g?o.g=i:(o.g=i,openSet.push(o)),console.log("the end "+end),o.h=manhatHeuristic(o,end),o.f=o.g+o.h,o.previous=r)}}}}function moveLogicalAgent(e){var t=ai,r=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?r++:e==ACTION_DOWN&&r--,occupied(t,r)||(ai=t,aj=r)}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(),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()}