Code viewer for World: Halloween A* failed experi...

// Cloned by Paul Jones on 9 Nov 2022 from World "Halloween A* failed experiment" by Deborah Djon 
// Please leave this clone trail here.
 
AB.clockTick=150,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/starter/door.jpg",TEXTURE_MAZE="/uploads/starter/latin.jpg",TEXTURE_AGENT="/uploads/starter/pacman.jpg",TEXTURE_ENEMY="/uploads/starter/ghost.3.png",GROUND_GRID="/uploads/tharealog/grid6.png",GROUND_DISPLACEMENT="/uploads/tharealog/displacement.png",MUSIC_BACK="/uploads/tharealog/let-the-mystery-unfold-122118.mp3",SOUND_ALARM="/uploads/tharealog/evil_laugh.mp3",gridsize=35,NOBOXES=Math.trunc(gridsize*gridsize/7),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["uploads/tharealog/sb11.png","uploads/tharealog/sb31.png","uploads/tharealog/sb_top1.png","uploads/tharealog/sb_top1.png","uploads/tharealog/sb41.png","uploads/tharealog/sb21.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;var BOXHEIGHT,GRID=new Array(gridsize);let mazeBoxes=[];var theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps;function loadResources(){var e=new THREE.TextureLoader,t=new THREE.TextureLoader,a=new THREE.TextureLoader,o=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()}),o.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,o;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/2,squaresize),s=new THREE.WireframeGeometry(a),lineMaterial=new THREE.LineBasicMaterial({color:"red"}),(o=new THREE.LineSegments(s,lineMaterial)).material.transpaent=!0,o.material.opacity=.01,o.position.copy(translate(e,t)),ABWorld.scene.add(o)):GRID[e][t]=GRID_BLANK;for(var n=1;n<=NOBOXES;n++){e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,r=squaresize/2+20,h=BOXHEIGHT,geo=new THREE.CylinderGeometry(0,r,h,4,1),material=new THREE.MeshPhongMaterial({color:16711680}),pyramidMesh=new THREE.Mesh(geo,material),geo2=new THREE.EdgesGeometry(pyramidMesh.geometry);var i=new THREE.LineBasicMaterial({color:"black"}),s=new THREE.LineSegments(geo,i);pyramidMesh.add(s),pyramidMesh.rotation.y=.25*-Math.PI,a=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(o=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({map:maze_texture}),pyramidMesh.position.copy(translate(e,t)),mazeBoxes.push(pyramidMesh),ABWorld.scene.add(pyramidMesh)}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(squaresize/2,32,16),(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(squaresize/2,32,16),(theagent=new THREE.Mesh(a)).material=new THREE.MeshBasicMaterial({color:"white"}),ABWorld.scene.add(theagent),drawAgent();const d=new THREE.TextureLoader,l=d.load(GROUND_GRID),c=d.load(GROUND_DISPLACEMENT);floorSize=3e4;const u=new THREE.PlaneGeometry(floorSize,floorSize,64,64);material=new THREE.MeshStandardMaterial({map:l,displacementMap:c,displacementScale:25e3});const p=new THREE.Mesh(u,material);p.position.copy(translate(17,17)),adjust=0,p.position.x+=adjust,p.position.y+=adjust,p.position.y=-BOXHEIGHT/2,p.rotation.x=.5*-Math.PI,ABWorld.scene.add(p);const g=new THREE.PointLight("red",2);g.position.copy(translate(10,10)),g.position.y=20*squaresize,ABWorld.scene.add(g),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 moveLogicalEnemy(){calculateAStarPath(),drawAStarPath(aStarPath),x=aStarPath[0][0],y=aStarPath[0][1],occupied(x,y)||(ei=x,ej=y)}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(),rainBlood())},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()}let aStarPath=[],aStarPathTiles=[],deadEnd=[],currentNode=null;class treeNode{constructor(e,t,a,o){this.position=e,this.goalHeuristic=t,this.pathLength=a,this.cost=t+a,this.parentNode=o,this.children=[]}}function heuristicDistance([e,t]){return Math.sqrt((ai-e)*(ai-e)+(aj-t)*(aj-t))}function resetStarPath(){aStarPath=[],visited=[],aStarPathTiles.forEach(e=>{ABWorld.scene.remove(e)}),aStarPathTiles=[]}function calculateAStarPath(){resetStarPath();let e=heuristicDistance([ei,ej]),t=[ei,ej],a=!1;const o=[ai,aj].toString();let n=new treeNode(t,e,0,null);for(currentNode=n,maxCount=0;!a;){maxCount++,150==maxCount&&(a=!0);const e=getNextSteps(currentNode.position,deadEnd);if(console.log("Next steps "+e),0==e.length)console.log("No next steps"),deadEnd.push(currentNode.position.toString()),aStarPath.pop(),currentNode=currentNode.parentNode;else{console.log("Finding best next step");let t=0;for(let n=0;n<e.length;n++){if(console.log("going through  next steps. Looking at step: "+n),console.log("next steps: "+e[n].toString()+"agent is at: "+o),child=new treeNode(e[n],heuristicDistance(e[n]),currentNode.pathLength+1,currentNode),e[n].toString()==o){a=!0,t=n,console.log("agent found"+a),console.log("A*P after agent found: "+aStarPath),currentNode.children.push(child);break}console.log("adding child at: "+e[n]),child=new treeNode(e[n],heuristicDistance(e[n]),currentNode.pathLength+1,currentNode),currentNode.children.push(child),child.cost<currentNode.children[t].cost&&(t=n),console.log("added to current node: "+currentNode.position+" a child at: "+child.position)}console.log(t),console.log(currentNode.children),aStarPath.push(currentNode.children[t].position),visited.push(currentNode.children[t].position.toString()),currentNode=currentNode.children[t]}}console.log("A*P after while loop: "+aStarPath)}function getNextSteps([e,t]){let a=[];return[[e+1,t],[e-1,t],[e,t+1],[e,t-1]].forEach(e=>{!(0<e[0]<gridsize&&0<e[1]<gridsize)||e.toString()!=[ai,aj].toString()&&occupied(e[0],e[1])||visited.includes(e.toString())||e.toString()==currentNode.position.toString()||a.push(e)}),a}function drawAStarPath(e){console.log("draw A* path: "+aStarPath),color=new THREE.Color("white"),aStarPath.forEach(e=>{shape=new THREE.BoxGeometry(squaresize,1,squaresize),material=new THREE.MeshBasicMaterial({color:color}),material.transparent=!0,material.opacity=.5,tile=new THREE.Mesh(shape,material),position=translate(e[0],e[1]),position.y=position.y-BOXHEIGHT/2,tile.position.copy(position),ABWorld.scene.add(tile),aStarPathTiles.push(tile)})}function untranslate(e){var t=new THREE.Vector2;return t.x=(e.x+MAXPOS/2)/squaresize,t.y=(e.z+MAXPOS/2)/squaresize,t}Array.prototype.sample=function(){return this[Math.floor(Math.random()*this.length)]};