// Cloned by Abdelshafa Abdala on 30 Oct 2022 from World "mine" by Abdelshafa Abdala
// Please leave this clone trail here.
// Cloned by Abdelshafa Abdala on 5 Nov 2021 from World "Star Wars World" by Dom
// Please leave this clone trail here.
AB.clockTick=50,
AB.maxSteps=1e3,
AB.screenshotStep=50;
var start,end,diagonal=!1,
openSet=[],
closedSet=[],
neighbors=[],
path=[];
const show3d=!0,
TEXTURE_WALL="/uploads/dom84/walls.jpg",
TEXTURE_MAZE="/uploads/dom84/trooper.jpg",
TEXTURE_AGENT="/uploads/dom84/LukeSkywalker.jpg",
TEXTURE_ENEMY="/uploads/dom84/Vader_headshot.jpg",
MUSIC_BACK="/uploads/dom84/ImperialsongJohnWilliams.mp3",
SOUND_ALARM="/uploads/dom84/Thepowerofthedarkside.mp3",
gridsize=20,
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/dom84/deathstar2.jpg",
"/uploads/dom84/deathstar2.jpg",
"/uploads/dom84/deathstar2.jpg",
"/uploads/dom84/deathstar2.jpg",
"/uploads/dom84/deathstar2.jpg",
"/uploads/dom84/deathstar2.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 BOXHEIGHT,theagent,theenemy,wall_texture,agent_texture,enemy_texture,maze_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize);
function loadResources()
{
var e=new THREE.TextureLoader();
var t=new THREE.TextureLoader();
var o=new THREE.TextureLoader();
var a=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()
}),
o.load(TEXTURE_ENEMY,function(e)
{
e.minFilter = THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()
}),
a.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||(e>=19||t>=19||(1==GRID[e][t].wall||1==GRID[e][t].wall)))
}
function translate(e,t)
{
var o = new THREE.Vector3();
return o.y =0,
o.x = e*squaresize - MAXPOS/2,
o.z=t*squaresize-MAXPOS/2,
o}
function initScene()
{
var e,t,o,a;
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,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(o)).material=new THREE.MeshBasicMaterial({map:wall_texture}),a.position.copy(translate(e,t)),ABWorld.scene.add(a),GRID[e][t]=new Spot(e,t,!0)):(GRID[e][t]=GRID_BLANK,GRID[e][t]=new Spot(e,t,!1));for(var i=1;i<=NOBOXES;i++)e=AB.randomIntAtoB(1,gridsize-2),t=AB.randomIntAtoB(1,gridsize-2),GRID[e][t]=GRID_MAZE,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(a=new THREE.Mesh(o)).material=new THREE.MeshBasicMaterial({map:maze_texture}),a.position.copy(translate(e,t)),ABWorld.scene.add(a),GRID[e][t]=new Spot(e,t,!0);do{e=AB.randomIntAtoB(1,gridsize-3),t=AB.randomIntAtoB(1,gridsize-3)}while(occupied(e,t));ei=e,ej=t,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theenemy=new THREE.Mesh(o)).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,o=new THREE.BoxGeometry(squaresize,BOXHEIGHT,squaresize),(theagent=new THREE.Mesh(o)).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)}function heuristic(e,t){return diagonal?Math.hypot(e.i-t.i,e.j-t.j):Math.abs(e.i-t.i)+Math.abs(e.j-t.j)}function removeFromArray(e,t){for(var o=e.length-1;o>=0;o--)e[o]==t&&e.splice(o,1)}function Spot(e,t,o){this.wall=o,this.i=e,this.j=t,this.f=0,this.g=0,this.h=0,this.neighbors=[],this.previous=void 0}function moveLogicalEnemy(){for(var e=0;e<gridsize;e++)for(var t=0;t<gridsize;t++){var o=GRID[e][t].wall;GRID[e][t]=new Spot(e,t,o)}for(e=0;e<gridsize;e++)for(t=0;t<gridsize;t++)GRID.addNeighbors(e,t);for(openSet=[],closedSet=[],start=GRID[ei][ej],end=GRID[ai][aj],openSet.push(start);openSet.length>0;){var a=0;for(e=0;e<openSet.length;e++)openSet[e].f<openSet[a].f&&(a=e);var i=openSet[a];if(i===end){path=[];for(var n=i;n.previous&&n.previous.previous;)path.push(n),n=n.previous;path.length>=1&&(ei==ai&&ej==aj||(ei=n.i,ej=n.j)),
console.log("success - found path")}removeFromArray(openSet,i),closedSet.push(i);
var r=GRID[i.i][i.j].neighbors;
for(e=0;e<r.length;e++)
{
var s=r[e];
if(!closedSet.includes(s)&&!s.wall)
{
var d=i.g+heuristic(s,i),u=!1;openSet.includes(s)?d<s.g&&(s.g=d,u=!0):(s.g=d,u=!0,openSet.push(s)),u&&(s.h=heuristic(s,end),s.f=s.g+s.h,s.previous=i)
}}}}function moveLogicalAgent(e)
{
var t=ai,o=aj;e==ACTION_LEFT?t--:
e==ACTION_RIGHT?t++:e==ACTION_UP?o++:
e==ACTION_DOWN&&o--,occupied(t,o)||(ai=t,aj=o)}GRID.addNeighbors=function(e,t){e<gridsize-1&&GRID[e][t].neighbors.push(GRID[e+1][t]),e>0&&GRID[e][t].neighbors.push(GRID[e-1][t]),t<gridsize-1&&GRID[e][t].neighbors.push(GRID[e][t+1]),t>0&&GRID[e][t].neighbors.push(GRID[e][t-1]),diagonal&&(e>0&&t>0&&GRID[e][t].neighbors.push(GRID[e-1][t-1]),e<gridsize-1&&t>0&&GRID[e][t].neighbors.push(GRID[e+1][t-1]),e>0&&t<gridsize-1&&GRID[e][t].neighbors.push(GRID[e-1][t+1]),e<gridsize-1&&t<gridsize-1&&GRID[e][t].neighbors.push(GRID[e+1][t+1]))};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+" x = ("+t.toString()+") a = ("+e+") ")
}
function updateStatusAfter()
{
var e=AB.world.getState(),t=goodsteps/AB.step*100;
AB.msg(" y = ("+e.toString()+") <br> Bad steps: "+badsteps+" Good steps: "+goodsteps+" 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())},
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()}