Code viewer for World: Complex World (clone by La...

// Cloned by Lakshmi Prasad on 10 Aug 2022 from World "Complex World (clone by Lakshmi Prasad) (clone by Lakshmi Prasad)" by Lakshmi Prasad 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/lprasad/256px-Wavy_fence_and_a_wall_BW_version.jpg",TEXTURE_MAZE="/uploads/lprasad/Picture_maze_unsolved.png",TEXTURE_AGENT="/uploads/lprasad/128px-Helios_Main_figure_Johannes_Benk_at_the_Naturhistorisches_Museum_Wien-9958.jpg",TEXTURE_ENEMY="/uploads/lprasad/128px-Enemy.jpg",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=100,NOBOXES=Math.trunc(gridsize*gridsize/3),squaresize=500,MAXPOS=gridsize*squaresize,SKYCOLOR=200,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/lprasad/st.posx.jpg","/uploads/lprasad/st.negx.jpg","/uploads/lprasad/st.posy.jpg","/uploads/lprasad/st.negy.jpg","/uploads/lprasad/st.posz.jpg","/uploads/lprasad/st.negz.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_ENEMY=10,GRID_AGENT=20;var BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,GRID=new Array(gridsize);class coordinates{constructor(e,a){this.i=e,this.j=a}}var badsteps,goodsteps,start=new coordinates,target=new coordinates;const MAX_OPENSET=gridsize*gridsize;var diagonal=!0;const PROOF=!0;var proofArray=[];function loadResources(){var e=new THREE.TextureLoader,a=new THREE.TextureLoader,t=new THREE.TextureLoader,i=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()}),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,a){if(e<=gridsize&&a<=gridsize&&e>=0&&a>=0)switch(GRID[e][a]){case GRID_WALL:case GRID_MAZE:case GRID_ENEMY:case GRID_AGENT:return!0;default:return!1}return!0}function iswall(e,a){switch(GRID[e][a]){case GRID_WALL:case GRID_MAZE:return!0;default:return!1}}function translate(e,a){var t=new THREE.Vector3;return t.y=-squaresize,t.x=e*squaresize-MAXPOS/2,t.z=a*squaresize-MAXPOS/2,t}function initScene(){var e,a,t,i;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),(i=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:wall_texture}),i.position.copy(translate(e,a)),ABWorld.scene.add(i)):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),(i=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:maze_texture}),i.position.copy(translate(e,a)),ABWorld.scene.add(i);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/2,BOXHEIGHT/2,squaresize/2),(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/2,BOXHEIGHT/2,squaresize/2),(theagent=new THREE.Mesh(t)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),GRID[ai][aj]=GRID_AGENT,GRID[ei][ej]=GRID_ENEMY,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 gridSpot(e,a){this.i=e,this.j=a,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.parent=void 0,this.closed=!1,this.addNeighbors=function(){var e=this.i,a=this.j;e<gridsize-1&&this.neighbors.push(aStarGrid[e+1][a]),e>0&&this.neighbors.push(aStarGrid[e-1][a]),a<gridsize-1&&this.neighbors.push(aStarGrid[e][a+1]),a>0&&this.neighbors.push(aStarGrid[e][a-1]),diagonal&&(e>0&&a>0&&this.neighbors.push(aStarGrid[e-1][a-1]),e<gridsize-1&&a>0&&this.neighbors.push(aStarGrid[e+1][a-1]),e>0&&a<gridsize-1&&this.neighbors.push(aStarGrid[e-1][a+1]),e<gridsize-1&&a<gridsize-1&&this.neighbors.push(aStarGrid[e+1][a+1]))}}var aStarGrid=[];function aStarInit(){var e,a;for(e=0;e<gridsize;e++)for(aStarGrid[e]=[],a=0;a<gridsize;a++)aStarGrid[e][a]=new gridSpot(e,a);for(e=0;e<gridsize;e++)for(a=0;a<gridsize;a++)aStarGrid[e][a].addNeighbors();console.info("A* Initialise: Complete")}function aStar(e,a,t){var r,o,s=[],d=!1,u=aStarGrid[t.i][t.j],l=aStarGrid[a.i][a.j],c=new coordinates(a.i,a.j);for(s.push(l),console.info("cleaning up previous results"),i=0;i<gridsize;i++)for(j=0;j<gridsize;j++)(o=aStarGrid[i][j]).f=o.h=o.g=0,o.closed=!1,o.parent=null;for(;s.length>0&&!d;){if(s.length>MAX_OPENSET)return console.error("there is a problem with the openset, bail out!"),d=!0,c;if(s.sort((e,a)=>e.f>a.f?1:-1),(o=s.shift()).closed=!0,console.info("Current: "+o.i+", "+o.j+" - (O/C) is "+s.length),o==u){for(PROOF&&proofArray.push(u);o.parent!=l;)PROOF&&proofArray.push(o),o=o.parent;d=!0,PROOF&&proofArray.push(l)}else for(n=0;n<o.neighbors.length;n++)(r=o.neighbors[n]).closed||(iswall(r.i,r.j)?r.closed=!0:(r.i==o.i||r.j==o.j?r.g=o.g+10:r.g=o.g+14,r.h=aStarHeuristics(r,u),r.f=r.g+r.h,r.parent=o,s.indexOf(r)<0&&s.push(r)))}return c.i=o.i,c.j=o.j,console.info("returning form a*: "),c}function aStarHeuristics(e,a){return diagonal?Math.pow(e.i-a.i,2)+Math.pow(e.j-a.j,2):Math.abs(e.i-e.i)+Math.abs(e.j-a.j)}function moveLogicalEnemy(){start.i=ei,start.j=ej,target.i=ai,target.j=aj,bestPath=aStar(GRID,start,target),occupied(bestPath.i,bestPath.j)||(GRID[ei][ej]=GRID_BLANK,ei=bestPath.i,ej=bestPath.j,GRID[ei][ej]=GRID_ENEMY),console.info("enemy moving to : "+ei+","+ej)}function moveLogicalAgent(e){var a=ai,t=aj;switch(e){case ACTION_LEFT:a--,theagent.rotation.y=0;break;case ACTION_RIGHT:a++,theagent.rotation.y=-1;break;case ACTION_UP:t++,theagent.rotation.y=-2;break;case ACTION_DOWN:t--,theagent.rotation.y=2}occupied(a,t)||(GRID[ai][aj]=GRID_BLANK,aj=t,GRID[ai=a][aj]=GRID_AGENT)}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+"<br>x = ("+a.toString()+")")}function updateStatusAfter(){AB.world.getState();var e=goodsteps/AB.step*100;AB.msg(" Bad steps: "+badsteps+"   Good steps: "+goodsteps+"   Score: "+e.toFixed(2)+"% ",2)}function playSound(){var e=new Audio("/uploads/brendanb/click.ogg"),a=1+(Math.abs(ei-ai)+Math.abs(ej-aj))/gridsize;console.log("PlaybackRate: "+a),e.playbackRate=a,e.volume=1/a,Math.abs(ei-ai)+Math.abs(ej-aj)<2&&(e.playbackRate=2*a,e.play()),e.play()}AB.world.newRun=function(){AB.loadingScreen(),AB.runReady=!1,badsteps=0,goodsteps=0,aStarInit(),BOXHEIGHT=squaresize,ABWorld.init3d(startRadiusConst,maxRadiusConst,200),loadResources(),document.onkeydown=keyHandler},AB.world.getState=function(){return[ai,aj,ei,ej]};var cameraAngle,enemyAngle=0;AB.world.takeAction=function(e){updateStatusBefore(e),console.info("-: Agent: "+ai+","+aj+" Enemy: "+ei+","+ej),moveLogicalAgent(e),AB.step%2==0&&(playSound(),moveLogicalEnemy()),console.info("+: Agent: "+ai+","+aj+" Enemy: "+ei+","+ej),badstep()?badsteps++:goodsteps++,enemyAngle+=.2,theenemy.rotation.y=enemyAngle,drawAgent(),drawEnemy(),updateStatusAfter(),agentBlocked()&&(AB.abortRun=!0,goodsteps=0),PROOF&&drawProof()},AB.world.endRun=function(){AB.abortRun?AB.msg(" <br> <font color=red> <B> Agent trapped.</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 previousLine,lastProofLine=[];function drawProof(){new THREE.LineBasicMaterial({color:2236962,linewidth:1});var e,a,t=new THREE.LineBasicMaterial({color:"green",linewidth:2}),r=new THREE.Geometry;for(i=proofArray.length;i>1;i--)(e=proofArray.shift()).parent?(e=e.parent,r.vertices.push(translate(e.i,e.j)),console.log("Line between "+e.i+","+e.j+" and ")):(r.vertices.push(translate(e.i,e.j)),console.log("Line between "+e.i+","+e.j+" and "));a=new THREE.Line(r,t),lastProofLine.push(a),ABWorld.scene.add(a)}