Code viewer for World: Upstream at Twilight (clon...

// Cloned by Abdelshafa Abdala on 6 Nov 2021 from World "Upstream at Twilight" by Patrick Travers 
// Please leave this clone trail here.
 
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;const show3d=!0,TEXTURE_WALL="/uploads/ptraver/1635504236.png",TEXTURE_MAZE="/uploads/ptraver/1635504506.png",TEXTURE_AGENT="/uploads/ptraver/1635515031.png",TEXTURE_ENEMY="/uploads/ptraver/1635515529.png",MUSIC_BACK="/uploads/starter/Defense.Line.mp3",SOUND_ALARM="/uploads/starter/air.horn.mp3",gridsize=50,NOBOXES=Math.trunc(gridsize*gridsize/3),squaresize=100,MAXPOS=gridsize*squaresize,SKYCOLOR=14548957,startRadiusConst=.8*MAXPOS,maxRadiusConst=10*MAXPOS;ABHandler.MAXCAMERAPOS=maxRadiusConst,ABHandler.GROUNDZERO=!0;const SKYBOX_ARRAY=["/uploads/ptraver/1635512764.png","/uploads/ptraver/1635512764.png","/uploads/ptraver/1635512764.png","/uploads/ptraver/1635512764.png","/uploads/ptraver/1635512764.png","/uploads/ptraver/1635512764.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,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,path_found,root_heuristic,root_node,graph_obj,unexplored_nodes_holder,unexplored_nodes,GRID=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,o=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()}),o.load(TEXTURE_AGENT,function(e){e.minFilter=THREE.LinearFilter,agent_texture=e,asynchFinished()&&initScene()}),n.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,o){return ei==e&&ej==o||(ai==e&&aj==o||(GRID[e][o]==GRID_WALL||GRID[e][o]==GRID_MAZE))}function translate(e,o){var n=new THREE.Vector3;return n.y=0,n.x=e*squaresize-MAXPOS/2,n.z=o*squaresize-MAXPOS/2,n}function initScene(){var e,o,n,r;for(e=0;e<gridsize;e++)GRID[e]=new Array(gridsize);for(e=0;e<gridsize;e++)for(o=0;o<gridsize;o++)0==e||e==gridsize-1||0==o||o==gridsize-1?(GRID[e][o]=GRID_WALL,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:wall_texture}),r.position.copy(translate(e,o)),ABWorld.scene.add(r)):GRID[e][o]=GRID_BLANK;for(var t=1;t<=NOBOXES;t++)e=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2),GRID[e][o]=GRID_MAZE,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(r=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:maze_texture}),r.position.copy(translate(e,o)),ABWorld.scene.add(r);do{e=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,o));ei=e,ej=o,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy();do{e=AB.randomIntAtoB(1,gridsize-2),o=AB.randomIntAtoB(1,gridsize-2)}while(occupied(e,o));ai=e,aj=o,n=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(n)).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 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)}class Node{constructor(e,o,n,r,t,a,i){this.i=e,this.j=o,this.parent_loc=[n,r],this.g=t,this.h=a,this.f=i}}class Graph_obj{constructor(){this.graph={}}check_node_present(e,o){return e in this.graph&&o in this.graph[e]}add_node(e){e.i in this.graph?this.graph[e.i][e.j]=e:this.graph[e.i]={},this.graph[e.i][e.j]=e}}function heuristic(e,o){return Math.max(Math.abs(e-ai),Math.abs(o-aj))}function score_node(e,o,n){return[n.g+1,heuristic(e,o)]}function traverse_path(e){var o=e,n=[];for(n.push([o.i,o.j]);o.g>0;){var r=o.parent_loc,t=r[0],a=r[1];o=graph_obj.graph[t][a],n.push([o.i,o.j])}return n}function node_operations(e,o,n){var r=score_node(e,o,n),t=r.reduce((e,o)=>e+o,0),a=new Node(e,o,n.i,n.j,r[0],r[1],t);if(graph_obj.add_node(a),1==a.h){path_found=!0,console.log("path found!");var i=traverse_path(a);draw_path(i);var s=i.slice(-2,-1);ei=s[0][0],ej=s[0][1]}t in unexplored_nodes_holder?unexplored_nodes_holder[t].push([e,o]):unexplored_nodes_holder[t]=[[e,o]]}function expand_children(e){var o=e.i,n=e.j;for(a=o-1;a<o+2;a++)for(b=n-1;b<n+2;b++)occupied(a,b)||(graph_obj.check_node_present(a,b)?graph_obj.graph[a][b].g-e.g>=2&&(graph_obj.graph[a][b].g=e.g+1,graph_obj.graph[a][b].f=graph_obj.graph[a][b].g+graph_obj.graph[a][b].h):node_operations(a,b,e))}function commonKeys(e,o){var n=[];for(var r in e)r in o&&n.push(r);return n}function find_path(){if(expand_children(root_node),0===Object.keys(unexplored_nodes_holder).length)return console.log(Object.keys(unexplored_nodes_holder)),void console.log("no path found");for(let e in unexplored_nodes_holder)unexplored_nodes[e]=unexplored_nodes_holder[e];for(let e in unexplored_nodes_holder)delete unexplored_nodes_holder[e];for(;!path_found;){var e=Math.min.apply(null,Object.keys(unexplored_nodes));ls=unexplored_nodes[e];for(let e in ls){let o=ls[e][0],n=ls[e][1];expand_children(graph_obj.graph[o][n])}if(delete unexplored_nodes[e],0===Object.keys(unexplored_nodes).length&&0===Object.keys(unexplored_nodes_holder).length){console.log("no path found");break}com_keys=commonKeys(unexplored_nodes_holder,unexplored_nodes);for(let e in unexplored_nodes_holder)if(com_keys.includes(e))for(let o in unexplored_nodes_holder[e])unexplored_nodes[e].push(unexplored_nodes_holder[e][o]);else unexplored_nodes[e]=unexplored_nodes_holder[e];for(var o in unexplored_nodes_holder)delete unexplored_nodes_holder[o]}}function draw_path(e){const o=new THREE.LineBasicMaterial({color:0});for(points=[],i=0;i<e.length;i++)points.push(new THREE.Vector3(e[i][0],0,e[i][1]));const n=(new THREE.BufferGeometry).setFromPoints(points),r=new THREE.Line(n,o);ABWorld.scene.add(r)}function moveLogicalEnemy(){path_found=!1,root_heuristic=heuristic(ei,ej),root_node=new Node(ei,ej,void 0,void 0,0,root_heuristic,root_heuristic),(graph_obj=new Graph_obj).add_node(root_node),unexplored_nodes_holder={},unexplored_nodes={},find_path()}function moveLogicalAgent(e){var o=ai,n=aj;e==ACTION_LEFT?o--:e==ACTION_RIGHT?o++:e==ACTION_UP?n++:e==ACTION_DOWN&&n--,occupied(o,n)||(ai=o,aj=n)}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 o=AB.world.getState();AB.msg(" Step: "+AB.step+" &nbsp; x = ("+o.toString()+") &nbsp; a = ("+e+") ")}function updateStatusAfter(){var e=AB.world.getState(),o=goodsteps/AB.step*100;AB.msg(" &nbsp; y = ("+e.toString()+") <br> Bad steps: "+badsteps+" &nbsp; Good steps: "+goodsteps+" &nbsp; Score: "+o.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(){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()}