Code viewer for World: CA686 Assignment 1 - 2021
// ===================================================================================================================
// ======================================== start of tweaker's box ===================================================
// ===================================================================================================================

AB.clockTick       =   50;    // 200 
AB.maxSteps        = 1000;    

//----------------------------------------- global constants: -------------------------------------------------------

const show3d = true;					

const wall_touch 	= '/uploads/harshakulkarni/mario_wall.jpg' ;
const maze_touch 	= '/uploads/harshakulkarni/mario_block.png' ;
const agent_touch 	= '/uploads/harshakulkarni/mario_agent2.jpeg' ;
const enemy_touch 	= '/uploads/harshakulkarni/mario_enemy2.jpg' ;

// credits:

const MUSIC_BACK  = '/uploads/harshakulkarni/circus.mp3' ;
const SOUND_ALARM = '/uploads/harshakulkarni/game_over.mp3' ;

const gridsize = 50;						   

const NOBOXES =  Math.trunc ( (gridsize * gridsize) / 3 );

const squaresize = 100;					

const MAXPOS = gridsize * squaresize;	
const SKYCOLOR 	= 0xddffdd;
 
const startRadiusConst	 	= MAXPOS * 0.8 ;		
const maxRadiusConst 		= MAXPOS * 10  ;		 
var draw_path = [];

ABHandler.MAXCAMERAPOS 	= maxRadiusConst ;

ABHandler.GROUNDZERO		= true;

// https://threejs.org/docs/#api/en/loaders/CubeTextureLoader 

// mountain skybox, credit:
// http://stemkoski.github.io/Three.js/Skybox.html

 const SKYBOX_ARRAY = [										 
                "/uploads/harshakulkarni/skyrender0001.jpg",
                "/uploads/harshakulkarni/skyrender0004.jpg",
                "/uploads/harshakulkarni/skyrender0003.jpg",
                "/uploads/harshakulkarni/skyrender0006.jpg",
                "/uploads/harshakulkarni/skyrender0005.jpg",
                "/uploads/harshakulkarni/skyrender0002.jpg"
                ];

// ===================================================================================================================
// ========================================= End of tweaker's box ====================================================
// ===================================================================================================================


//--- Mind can pick one of these actions -----------------

const ACTION_LEFT 			= 0;		   
const ACTION_RIGHT 			= 1;
const ACTION_UP 			= 2;		 
const ACTION_DOWN 			= 3;
const ACTION_STAYSTILL 		= 4;

const GRID_BLANK 	= 0;
const GRID_WALL 	= 1;
const GRID_MAZE 	= 2;

var BOXHEIGHT;	

var GRID = new Array(gridsize);

var theagent, theenemy;
  
var wall_texture, agent_texture, enemy_texture, maze_texture; 

// enemy and agent position on squares
var ei, ej, ai, aj;

var badsteps;
var goodsteps;

function make_everything_ready()
{
	var loader_1 = new THREE.TextureLoader();
	var loader_2 = new THREE.TextureLoader();
	var loader_3 = new THREE.TextureLoader();
	var loader_4 = new THREE.TextureLoader();
	
	loader_1.load ( wall_touch, function ( texture )  		
	{
		texture.minFilter  = THREE.LinearFilter;
		wall_texture = texture;
		if (load_uploaded_files() === true)
		{
		    setup_scene();
		}
	});
		
	loader_2.load ( agent_touch, function ( texture )  	 
	{
		texture.minFilter  = THREE.LinearFilter;
		agent_texture = texture;
		if (load_uploaded_files() === true)
		{
		    setup_scene();
		}	 
	});	

	loader_3.load ( enemy_touch, function ( texture )  
	{
		texture.minFilter  = THREE.LinearFilter;
		enemy_texture = texture;
		if (load_uploaded_files() === true)
		{
		    setup_scene();
		}
	});

	loader_4.load ( maze_touch, function ( texture )  
	{
		texture.minFilter  = THREE.LinearFilter;
		maze_texture = texture;
		if (load_uploaded_files() === true)
		{
		    setup_scene();
		}	 
	});

}

function load_uploaded_files()
{
	if ( wall_texture && agent_texture && enemy_texture && maze_texture )   
	{
	    return true; 
	}
	else
	{
	    return false;
    }
}	

function occupied ( i, j )
{
 if ( ( ei == i ) && ( ej == j ) ) return true;
 if ( ( ai == i ) && ( aj == j ) ) return true;

 if ( GRID[i][j] == GRID_WALL ) return true;	 
 if ( GRID[i][j] == GRID_MAZE ) return true;		 
 return false;
}

function translate ( i, j )			
{
	var v = new THREE.Vector3();
	v.y = 0;	
	v.x = ( i * squaresize ) - ( MAXPOS/2 );   		 
	v.z = ( j * squaresize ) - ( MAXPOS/2 );   	
	return v;
}

function setup_scene()		// all file loads have returned 
{
	 var i,j, shape, thecube;

	 for ( i = 0; i < gridsize ; i++ ) 
		GRID[i] = new Array(gridsize);		 

	 for ( i = 0; i < gridsize ; i++ ) 
	  for ( j = 0; j < gridsize ; j++ ) 
		if ( ( i===0 ) || ( i===gridsize-1 ) || ( j===0 ) || ( j===gridsize-1 ) )
		{
			GRID[i][j] = GRID_WALL;		 
			shape    = new THREE.BoxGeometry ( squaresize, BOXHEIGHT, squaresize );			 
			thecube  = new THREE.Mesh( shape );
			thecube.material = new THREE.MeshBasicMaterial( { map: wall_texture } );
			
			thecube.position.copy ( translate(i,j) ); 	
			ABWorld.scene.add(thecube);
		}
		else 
   			GRID[i][j] = GRID_BLANK;
   
    for ( var c=1 ; c <= NOBOXES ; c++ )
	{
		i = AB.randomIntAtoB(1,gridsize-2);	
		j = AB.randomIntAtoB(1,gridsize-2);

		GRID[i][j] = GRID_MAZE ;

		shape    = new THREE.BoxGeometry ( squaresize, BOXHEIGHT, squaresize );			 
		thecube  = new THREE.Mesh( shape );
		thecube.material = new THREE.MeshBasicMaterial( { map: maze_texture } );		  

		thecube.position.copy ( translate(i,j) ); 
		ABWorld.scene.add(thecube);		
	}
	// start the Enemy in random location
	 do
	 {
	  i = AB.randomIntAtoB(1,gridsize-2);
	  j = AB.randomIntAtoB(1,gridsize-2);
	 }
	 while ( occupied(i,j) );  	  // search for any empty node 

	 ei = i;
	 ej = j;
	 
	 shape    = new THREE.BoxGeometry ( squaresize, BOXHEIGHT, squaresize );			 
	 theenemy = new THREE.Mesh( shape );
 	 theenemy.material =  new THREE.MeshBasicMaterial( { map: enemy_texture } );
	 ABWorld.scene.add(theenemy);
	 update_Enemy();		  

	// start the Agent in random location
	
	 do
	 {
	  i = AB.randomIntAtoB(1,gridsize-2);
	  j = AB.randomIntAtoB(1,gridsize-2);
	 }
	 while ( occupied(i,j) );        // search for any empty node

	 ai = i;
	 aj = j;
 
	 shape    = new THREE.BoxGeometry ( squaresize, BOXHEIGHT, squaresize );			 
	 theagent = new THREE.Mesh( shape );
	 theagent.material =  new THREE.MeshBasicMaterial( { map: agent_texture } );
	 ABWorld.scene.add(theagent);
	 update_Agent(); 
  
  	 ABWorld.scene.background = new THREE.CubeTextureLoader().load ( SKYBOX_ARRAY, 	function() 
	 { 
		ABWorld.render(); 
	 
		AB.removeLoading();
	
		AB.runReady = true; 
	 });
 		
}
 
// ------------------------------- moving Agent and Enemy on Screen -----------------------------------


function update_Enemy()	
{
	theenemy.position.copy ( translate(ei,ej) ); 

	ABWorld.lookat.copy ( theenemy.position ); 
}

function update_Agent()
{
	theagent.position.copy ( translate(ai,aj) ); 

	ABWorld.follow.copy ( theagent.position );	
}

// -----------------------------------start of our A* Algorithm -----------------------------------

//var missedpath = Boolean(false);
//create block to store feature of block
function block(x,y)
{
    this.x = x;
    this.y = y;
    this.g =0;
    this.h = 0;
    this.f = 0;
    this.neib = [];
}

// remove a element from array    
function remove(arr, ele){
    for (var i = arr.length -1; i >= 0 ; i--){
        if(arr[i] == ele){
            arr.splice(i,1); 
        }
    }
}

//calculate distance between current node and destination use Manhattan Distance
function calFGH(CurrBlock, StartBlock, EndBlock)
{
    CurrBlock.h = Math.abs(EndBlock.x-CurrBlock.x) + Math.abs(EndBlock.y-CurrBlock.y);
    CurrBlock.f = CurrBlock.g + CurrBlock.h;
}

function existBlock(x,y,arr){
    var aa = 0;
    if(arr.length > 0)
    {
    for(var i =0;i<arr.length; i++)
      {
        if(arr[i].x == x && arr[i].y == y)
        {
            aa= 1;
        }
      }
    }
    return aa;
}

function currNeib(currBlock,closed_list){
    var neib = []
    if (currBlock.x > 1 && !occupied(currBlock.x - 1,currBlock.y) && 
    existBlock(currBlock.x - 1, currBlock.y,closed_list)===0){
        neib.push(new block(currBlock.x - 1, currBlock.y));
    }
    if (currBlock.x < gridsize - 2 && !occupied(currBlock.x +1 ,currBlock.y) &&
   existBlock(currBlock.x + 1, currBlock.y,closed_list)===0){
        neib.push(new block(currBlock.x + 1, currBlock.y));
    }
    if (currBlock.y > 1 && !occupied(currBlock.x,currBlock.y - 1) 
    && existBlock(currBlock.x, currBlock.y - 1,closed_list)===0){
        neib.push(new block(currBlock.x, currBlock.y - 1));
    }
    if (currBlock.y < gridsize - 2 && !occupied(currBlock.x,currBlock.y + 1) 
     && existBlock(currBlock.x, currBlock.y + 1,closed_list)===0){
        neib.push(new block(currBlock.x, currBlock.y + 1));
    }
    return neib;
    
}

// create array to store neighbor of node
function neibs(end){
   var neib = [];
   neib.push(new block(end.x - 1, end.y));
   neib.push(new block(end.x + 1, end.y));
   neib.push(new block(end.x, end.y - 1));
   neib.push(new block(end.x, end.y + 1));
   return neib;
}

//Function to draw paths on the screen
function draw_new_path(block, colors)
{
    var geometry = new THREE.BoxGeometry( 20, 20, 20 );
    var material = new THREE.LineBasicMaterial( { color: 0xFF0000 } );
    var cube = new THREE.Mesh( geometry, material );
    cube.position.copy(translate(block.x,block.y));  
    ABWorld.scene.add( cube );  
    draw_path.push(cube);
}

// main function to implement A* algorithm will return a shortest path
function getpath()
{
    var open_list = [];
    var closed_list = [];
    var path =[];      
    var end = new block(ai,aj); 
    var start = new block(ei,ej);
    
    open_list.push(start);    
    var curr = open_list.pop(); 
    
    while(true)
    {
        
        var neib = currNeib(curr,closed_list);
        
        for( i=0;i <neib.length; i++ ){
            
            calFGH(neib[i],start, end); 
            neib[i].g = curr.g + 1; 
            open_list.push(neib[i]);
            curr.neib.push(neib[i]);
         }

        if (open_list.length > 0){
            var low = 0;
            for (i= 0; i< open_list.length; i++){
            if(open_list[i].f < open_list[low].f){
                low = i;
            }
        }
        curr = open_list[low]; 
    
        remove(open_list,curr); 
        closed_list.push(curr); 
       }

       if(existBlock(curr.x,curr.y,neibs(end))==1 || open_list.length == 0 || existBlock(start.x,start.y, neibs(end))==1)
       {
           break; 
       }
    }
    // to draw path that can be searched
    // for(i = 0; i < open_list.length; i++){
    //     draw_new_path(open_list[i],0x0000FF);
    //}
    // to draw path which are already searched 
    // for(i = 0; i < closed_list.length; i++){
    //     draw_new_path(closed_list[i],0XFF0000);
    // }

    if(closed_list.length>0)
    {
        path.push(closed_list[closed_list.length-1])
        
        i = 0; 
        while(true)
        {
            var pathneib = neibs(path[i]);
            for(var a=0;a<closed_list.length; a++)
            {
            for(var q=0;q<pathneib.length;q++)
                { 
                if(closed_list[a].x == pathneib[q].x && closed_list[a].y == pathneib[q].y && closed_list[a].g < path[i].g)
                    {
                        path.push(closed_list[a]);
                    }
                }
  
            }
           if(existBlock(start.x,start.y,neibs(path[i])) == 1 || existBlock(start.x,start.y, neibs(end))==1 || open_list.length == 0)
            {
               break;
            }
            i=i+1;
        }
    }
    console.log(path);
   if(existBlock(start.x,start.y, neibs(end))==1){
        path = [];
    }

    return path;
}

// ----------------------------------------------  end of A* Algorithm ------------------------------------------

function move_Enemy()
{ 
// remove last path that's printed
for(i = 0; i < draw_path.length; i++)
{
    ABWorld.scene.remove(draw_path[i])
}

var path = getpath();
console.log("here")
console.log(path)

//draw the new path on the screen
for(i = 0; i < path.length; i++)
     {
         draw_new_path(path[i],0X00FF00);
     }
     
if(path.length>0)
{
 ei = path[path.length-1].x;
 ej = path[path.length-1].y;
}
if (path.length>0 && if_agent_blocked() === true)
{
  console.log("Finally reached!");
  ei = ei;
  ej = ej;
  console.log("stop!!!");
}
 }

function move_Agent( a )
{ 
 var i = ai;
 var j = aj;		 

      if ( a == ACTION_LEFT ) 	i--;
 else if ( a == ACTION_RIGHT ) 	i++;
 else if ( a == ACTION_UP ) 	j++;
 else if ( a == ACTION_DOWN ) 	j--;

 if ( ! occupied(i,j) ) 
 {
  ai = i;
  aj = j;
 }
}

// --- key handling --------------------------------------------------------------------------------------
// This is hard to see while the Mind is also moving the agent:
// AB.mind.getAction() and AB.world.takeAction() are constantly running in a loop at the same time 
// have to turn off Mind actions to really see user key control 

// we will handle these keys: 

var OURKEYS = [ 37, 38, 39, 40 ];

function ourKeys ( event ) { return ( OURKEYS.includes ( event.keyCode ) ); }
	

function keyHandler ( event )		
{
	if ( ! AB.runReady ) return true; 		// not ready yet 

   // if not one of our special keys, send it to default key handling:
	
	if ( ! ourKeys ( event ) ) return true;
	
	// else handle key and prevent default handling:
	
	if ( event.keyCode == 37 )   move_Agent ( ACTION_LEFT 	);   
    if ( event.keyCode == 38 )   move_Agent ( ACTION_DOWN  	); 	 
    if ( event.keyCode == 39 )   move_Agent ( ACTION_RIGHT 	); 	 
    if ( event.keyCode == 40 )   move_Agent ( ACTION_UP		);   
	
	// when the World is embedded in an iframe in a page, we want arrow key events handled by World and not passed up to parent 

	event.stopPropagation(); event.preventDefault(); return false;
}

// --- score: -----------------------------------


function badstep()
{
 if ( ( Math.abs(ei - ai) < 2 ) && ( Math.abs(ej - aj) < 2 ) ) 
 {
     return true;
 }
 else
 {
     return false;
 }
}

function if_agent_blocked()			// returns is agent is blocked on all sides
{
 //var enemy_neibs
 return ( occupied (ai-1,aj) && occupied (ai+1,aj) && occupied (  ai,aj+1)	&& occupied (  ai,aj-1) );		
} 

function stop_run()
{
    AB.abortRun = true;
	goodsteps = 0; 			 
	musicPause();
	soundAlarm();
}

function updateStatusBefore(a)
{
 var x 		= AB.world.getState();
 AB.msg ( " Step: " + AB.step + " &nbsp; x = (" + x.toString() + ") &nbsp; a = (" + a + ") " ); 
}

function   updateStatusAfter()
{
 // new state after both have moved
 
 var y 		= AB.world.getState();
 var score = ( goodsteps / AB.step ) * 100; 

 AB.msg ( " &nbsp; y = (" + y.toString() + ") <br>" +
		" Bad steps: " + badsteps + 
		" &nbsp; Good steps: " + goodsteps + 
		" &nbsp; Score: " + score.toFixed(2) + "% ", 2 ); 
}

AB.world.newRun = function() 
{
    document.onkeydown = keyHandler;
	AB.runReady = false;  
	AB.loadingScreen();
	if ( show3d )
	{
	 BOXHEIGHT = squaresize;
	 ABWorld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 	
	}
	else
	{
	BOXHEIGHT = 1;
	ABWorld.init2d ( startRadiusConst, maxRadiusConst);
	}
	make_everything_ready();
	badsteps = 0;	
	goodsteps = 0;
};

AB.world.getState = function()
{
  return ( [ai, aj, ei, ej] );  
};

AB.world.takeAction = function ( a )
{
  updateStatusBefore(a);

  move_Agent(a);

  move_Enemy();
  
  if ( badstep() )  
  {
      badsteps++;
  }
  else
  {
      goodsteps++;
  }
   update_Agent();
   update_Enemy();
   updateStatusAfter();

  if ( if_agent_blocked() )
  {
    console.log("block")
	stop_run()
  }
};

AB.world.endRun = function()
{ 
  if ( AB.abortRun === true)
  {
      AB.msg ( "<B><br>GAME OVER!! Either Agent is finally trapped from all directions OR There is no path to reach agent</br></B>\n", 6 );
  }
  else
  {
      AB.msg ( "<B><br>Run over</br></B>\n", 6 );
  }
  
};

var backmusic = AB.backgroundMusic ( MUSIC_BACK );

function musicPlay()   { backmusic.play();  }
function musicPause()  { backmusic.pause(); }

											 
function soundAlarm()
{
	var alarm = new Audio ( SOUND_ALARM );
	alarm.play();
}