// Cloned by David Jusev on 1 Dec 2022 from Mind "Complex Mind (clone by Vineet Sajwan) (clone by Vineet Sajwan)" by Vineet Sajwan
// Please leave this clone trail here.
AB.clockTick=100,AB.maxSteps=1e3,AB.screenshotStep=50;var theagent,theenemy,wall_texture,agent_texture,enemy_texture,ei,ej,ai,aj,badsteps,goodsteps,GRID=new Array(gridsize);function loadResources(){var e=new THREE.TextureLoader,t=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()}),n.load(TEXTURE_ENEMY,function(e){e.minFilter=THREE.LinearFilter,enemy_texture=e,asynchFinished()&&initScene()})}function asynchFinished(){return!!(wall_texture&&agent_texture&&enemy_texture)}function occupied(e,t){return ei==e&&ej==t||(ai==e&&aj==t||1==GRID[e][t])}function translate(e,t){var n=new THREE.Vector3;return n.y=0,n.x=e*squaresize-MAXPOS/2,n.z=t*squaresize-MAXPOS/2,n}function initScene(){var e,t,n,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]=!0,n=new THREE.BoxGeometry(squaresize,squaresize,squaresize),(a=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:wall_texture}),a.position.copy(translate(e,t)),ABWorld.scene.add(a)):GRID[e][t]=!1;ei=Math.trunc(gridsize/2),ej=Math.trunc(gridsize/2),n=new THREE.BoxGeometry(squaresize,squaresize,squaresize),(theenemy=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:enemy_texture}),ABWorld.scene.add(theenemy),drawEnemy(),ai=Math.trunc(gridsize/2)+1,aj=Math.trunc(gridsize/2)+1,n=new THREE.BoxGeometry(squaresize,squaresize,squaresize),(theagent=new THREE.Mesh(n)).material=new THREE.MeshBasicMaterial({map:agent_texture}),ABWorld.scene.add(theagent),drawAgent(),ABWorld.render(),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(){var e=AB.randomIntAtoB(ei-1,ei+1),t=AB.randomIntAtoB(ej-1,ej+1);occupied(e,t)||(ei=e,ej=t)}function moveLogicalAgent(e){var t=ai,n=aj;e==ACTION_LEFT?t--:e==ACTION_RIGHT?t++:e==ACTION_UP?n++:e==ACTION_DOWN&&n--,occupied(t,n)||(ai=t,aj=n)}function badstep(){return Math.abs(ei-ai)<2&&Math.abs(ej-aj)<2}function updateStatus(){var e=AB.world.getScore();AB.msg(" Step: "+AB.step+" out of "+AB.maxSteps+". Score: "+e)}AB.world.newRun=function(){AB.runReady=!1,badsteps=0,goodsteps=0,ABWorld.init3d(startRadiusConst,maxRadiusConst,SKYCOLOR),loadResources()},AB.world.getState=function(){return[ai,aj,ei,ej]},AB.world.takeAction=function(e){moveLogicalAgent(e),moveLogicalEnemy(),badstep()?badsteps++:goodsteps++,drawAgent(),drawEnemy(),updateStatus()},AB.world.getScore=function(){return goodsteps};