EvaluateSystemScript("pathfind.js");

var LogDebug = false;
if(LogDebug) LL = OpenLog('pathfind.log');


function game()
{
	CreatePerson("car","car.rss",false);
	AttachCamera("car");
	MapEngine ("pathfind.rmp", 80);  
	// In this demo, the function doAStar() is called from the map entry script
}


// Check for Obstructions is used inside AStarPathFind()
function checkObstruction(point)
{
	var layer = GetPersonLayer("car");
	var MaxX = GetLayerWidth(layer)-1; // maximum tiles wide
	var MaxY = GetLayerHeight(layer)-1; // maximum tiles high
	
	// point must not be outside the map
	if (point.x > MaxX || point.y > MaxY || point.x < 0 || point.y < 0)
		return true;
	
	// We will be asked many times if a tile is obstructed, lets cache it in visited.
	if(!this.visited) 
		this.visited = new Array(); // Could hold up to GetLayerWidth()*GetLayerHeight(layer) entries.

	var index = point.x+point.y*GetLayerWidth(layer); // Convert (x,y) into a unique number
	if(typeof(this.visited[index]) == 'boolean')
		return index; // If we already cached the answer, return it.

	PB = GetPersonBase("car");
	PB.ox = (PB.x2 - PB.x1)>>1;
	PB.oy = (PB.y2 - PB.y1)>>1;
	
	if(LogDebug) LL.write( (PB.ox + point.x * GetTileWidth()) +","+ (PB.oy + point.y * GetTileHeight()));

	// is "car" obstructed at tile (point.x,point.y) ?
	this.visited[index] = IsPersonObstructed("car", PB.ox + point.x * GetTileWidth(), PB.oy + point.y * GetTileHeight() );

	// Some eyecandy: replace the tiles we evaluate so we can see how we pathfinded
	if(GetTile(point.x,point.y,layer) != 2)
	{
		if(this.visited[index])
			SetTile(point.x, point.y, layer, 3);
		else
			SetTile(point.x, point.y, layer, 4);
	}

	return this.visited[index];
}

// using manhattan distance
function distance(pointA, pointB)
{ 
   return Math.abs(pointA.x - pointB.x) + Math.abs(pointA.y-pointB.y);
}


function doAStar()
{
	var directionsArray = AStarPathFind(PFPoint(0,0),PFPoint(9,9),checkObstruction,distance);
	if(!directionsArray) 
		return false; // Whoops, could not find a path.
  
	while (directionsArray.length > 0)
	{
		var dir = directionsArray.shift();
 
		switch (dir) {
  
			case KEY_UP:
				if(LogDebug) LL.write("UP");
				QueuePersonCommand("car",COMMAND_FACE_NORTH,false);
				for (var i=GetTileHeight(); i>0; --i)
					QueuePersonCommand("car",COMMAND_MOVE_NORTH,false);
				break;

			case KEY_DOWN:
				if(LogDebug) LL.write("DOWN");
				QueuePersonCommand("car",COMMAND_FACE_SOUTH,false);
				for (var i=GetTileHeight(); i>0; --i)
					QueuePersonCommand("car",COMMAND_MOVE_SOUTH,false);
				break;
  
			case KEY_LEFT:
				if(LogDebug) LL.write("LEFT");
				QueuePersonCommand("car",COMMAND_FACE_WEST,false);
				for (var i=GetTileHeight(); i>0; --i)
					QueuePersonCommand("car",COMMAND_MOVE_WEST,false);
				break;

			case KEY_RIGHT:
				if(LogDebug) LL.write("RIGHT");
				QueuePersonCommand("car",COMMAND_FACE_EAST,false)
				for (var i=GetTileHeight(); i>0; --i)
					QueuePersonCommand("car",COMMAND_MOVE_EAST,false)
				break;
		}
	}
	return true; // Yes, we could pathfind
}


