//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014

function SpawnArea()
{
	this.area = new Array();
	
	this.AddTile = function(x, y)
	{
		this.area.push(new SpawnPoint(x, y));
	}
	
	this.AddArea = function(x, y, width, height)
	{
		for(var i = x; i < (x + width); i++)
		{
			for(var j = y; j < (y + height); j++)
			{
				this.AddTile(i, j);
			}
		}
	}
	
	this.IsPointInside = function(x, y)
	{
		for(var i = 0; i < this.area.length; i++)
		{
			var area = this.area[i];
			if(area.IsPointInside(x, y))
			{
				return true;
			}
		}
		
		return false;
	}
	
	this.IsValid = function()
	{
		return (this.area.length > 0);
	}
	
	this.GetPoint = function(block)
	{
		gConsole.Assert(this.IsValid());
		
		this.UpdateScores(block);
		
		this.area.sort(SpawnPointCompare);
		
		for(var i = 0; i < this.area.length; i++)
		{
			var area = this.area[i];
			if(area.CanUsePoint())
			{
				area.GenerateRand();
				return area.vec2;
			}
		}
		
		return null;
	}
	
	this.UpdateScores = function(block)
	{
		for(var i = 0; i < this.area.length; i++)
		{
			this.area[i].UpdateScores(block);
		}
	}
	
	this.Debug = function(block)
	{
		this.UpdateScores(block);
		for(var i = 0; i < this.area.length; i++)
		{
			var area = this.area[i];
			var x = (area.vec2.x * kTileSize);
			var y = (area.vec2.y * kTileSize) + 7;
			gDraw.WatchAt(x, y, area.score);
		}
	}
}

function SpawnPointCompare(a, b)
{
	return b.score - a.score;
}


function SpawnPoint(x, y)
{
	this.GenerateRand = function()
	{
		this.rand = Math.floor(Math.random() * 5);
	}
	
	this.vec2 = new Vec2(x, y);
	this.score = 0;
	this.GenerateRand();
	
	this.IsPointInside = function(x, y)
	{
		return this.vec2.x == x && this.vec2.y == y;
	}
	
	this.UpdateScores = function(block)
	{
		if(!this.CanUsePoint())
		{
			this.score = 0;
		}
		else
		{
			this.score = this.CalculateScore(block);
		}
	}
	
	this.CanUsePoint = function()
	{
		if(gTileSystem.GetTileAt(this.vec2.x, this.vec2.y))
		{
			if(!gBlockSystem.GetBlockAtOrMovingTo(this.vec2.x, this.vec2.y))
			{
				if(!gPlayerSystem.GetPlayerAt(this.vec2.x, this.vec2.y))
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	this.CalculateScore = function(block)
	{
		var score = this.CalculateScoreForOffset(block, 1, 0);
		score += this.CalculateScoreForOffset(block, -1, 0);
		score += this.CalculateScoreForOffset(block, 0, 1);
		score += this.CalculateScoreForOffset(block, 0, -1);
		score += this.CalculateScoreForPlayerPosition();
		score += this.rand;
		return score;
	}
	
	this.CalculateScoreForPlayerPosition = function()
	{
		var count = gPlayerSystem.players.length;
		for(var i = 0; i < count; i++)
		{
			var player = gPlayerSystem.players[i];
			if(this.IsInlineWithPlayer(player, this.vec2.x, this.vec2.y))
			{
				return -5;
			}
		}
		
		return 0;
	}
	
	this.IsInlineWithPlayer = function(player, x, y)
	{
		var px = player.GetX();
		var py = player.GetY();
		return x == px || y == py;
	}
	
	this.CalculateScoreForOffset = function(block, dx, dy)
	{
		var score = 0;
		
		if(gPlayerSystem.GetPlayerAt(this.vec2.x + dx, this.vec2.y + dy))
		{
			return 0;
		}

		if(gTileSystem.GetTileAt(this.vec2.x + dx, this.vec2.y + dy))
		{
			score += 20;
		}

		var offsetBlock = gBlockSystem.GetBlockAt(this.vec2.x + dx, this.vec2.y + dy);
		if(offsetBlock && offsetBlock.value == block.value)
		{
			score -= 1;
		}

		return score;
	}
}