View Single Post
  #1  
Old 08-13-2003, 12:25 AM
Wiz
Dragon
 
Join Date: Feb 2002
Posts: 583
Default Basic Line of Sight

This function will use the nodes in .map files to apply line of sight (prevents wall aggro if used right, among other things). It is not perfect yet, as it tends to fail on thin walls, or walls on top of other floors (such as in unrest or hole), but it's a start.

Code:
bool Mob::CheckLos(Mob* other) {
	if (zone->map == 0)
	{
		return true;
	}
	float tmp_x = GetX();
	float tmp_y = GetY();
	float trg_x = other->GetX();
	float trg_y = other->GetY();
	float perwalk_x = 0.2;
	float perwalk_y = 0.2;
	float dist_x = tmp_x - trg_x;
	if (dist_x < 0)
		dist_x *= -1;
	float dist_y = tmp_y - trg_y;
	if (dist_y < 0)
		dist_y *= -1;
	if (dist_x  < dist_y)
		perwalk_x /= (dist_y/dist_x);
	else if (dist_y < dist_x)
		perwalk_y /= (dist_x/dist_y);
	while (1) {
		if (tmp_x < trg_x)
		{
			if (tmp_x + perwalk_x < trg_x)
				tmp_x += perwalk_x;
			else
				tmp_x = trg_x;
		}
		if (tmp_y < trg_y)
		{
			if (tmp_y + perwalk_y < trg_y)
				tmp_y += perwalk_y;
			else
				tmp_y = trg_y;
		}
		if (tmp_x > trg_x)
		{
			if (tmp_x - perwalk_x > trg_x)
				tmp_x -= perwalk_x;
			else
				tmp_x = trg_x;
		}
		if (tmp_y > trg_y)
		{
			if (tmp_y - perwalk_y > trg_y)
				tmp_y -= perwalk_y;
			else
				tmp_y = trg_y;
		}
		if (tmp_y == trg_y && tmp_x == trg_x)
		{
			return true;
		}
		PNODE pnode = zone->map->SeekNode( zone->map->GetRoot(), tmp_x, tmp_y );
		if (pnode != 0)
		{
			int *iface = zone->map->SeekFace( pnode, tmp_x, tmp_y );
			if (*iface == -1) {
				return false;
			}							
		}
	}
	return true;
}
Reply With Quote