#include "ranges.h"

int moves_needed(int mtype, int tx, int ty);
int any_unit_here(int tx, int ty);
int attackable_unit_here(_unit *attacker, int tx, int ty, bool ignore_teams);

int tile_distance(int ax, int ay, int bx, int by)
{
	int dx, dy;
	dx = abs(bx - ax);
	dy = abs(by - ay);
	return dx + dy;
}

int enemy_in_range(_unit *u)
{
	int i, j, z;
	int min = u->rangemin;
	int max = u->rangemax;
	j = u->tiley - max;
	if (j < 0) j = 0;
	while ((j <= u->tiley + max) && (j < worldmap.get_height()))
	{
		i = u->tilex - max;
		if (i < 0) i = 0;
		while ((i <= u->tilex + max) && (i < worldmap.get_length()))
		{
			if (tile_distance(u->tilex, u->tiley, i, j) >= min)
			{
				z = attackable_unit_here(u, i, j, true);
				if (z != -1)
				{
					return z;
				}
			}
			i++;
		}
		j++;
	}
	return -1;
}

void draw_attackrange(_unit *u)
{
	static float alpha = 0.3;
	static float alphachange = 0.01;
	int rangemin, rangemax, d, lx, ly, x, y, max_x, max_y, m;
	bool arange[MAP_MAX_L][MAP_MAX_H];

	if (unitstats[u->type].attacktype == RANGED)
	{
		rangemin = u->rangemin;
		rangemax = u->rangemax;
		x = u->tilex - rangemax;
		y = u->tiley - rangemax;
		max_x = u->tilex + rangemax + 1;
		max_y = u->tiley + rangemax + 1;
		if (max_x > worldmap.camera.get_scroll_x() + XTILES) max_x = worldmap.camera.get_scroll_x() + XTILES;
		if (max_y > worldmap.camera.get_scroll_y() + YTILES) max_y = worldmap.camera.get_scroll_y() + YTILES;
		if (x < 0) x = 0;
		while (x < max_x)
		{
			y = u->tiley - rangemax;
			if (y < 0) y = 0;
			while (y < max_y)
			{
				d = tile_distance(x, y, u->tilex, u->tiley);
				if ((d >= rangemin) && (d <= rangemax))
				{
					lx = (x - worldmap.camera.get_scroll_x()) * MAP_TILE_SIZE + worldmap.camera.get_offset_x();
					ly = (y - worldmap.camera.get_scroll_y()) * MAP_TILE_SIZE + worldmap.camera.get_offset_y();
					buffer_rectfill(lx, ly, lx + MAP_TILE_SIZE, ly + MAP_TILE_SIZE, RED, alpha);
				}
				y++;
			}
			x++;
		}
	}
	else if ((unitstats[u->type].attacktype == DIRECT) || (unitstats[u->type].name == "Mechanic"))
	{
		m = u->move;
		if (!u->canmove) m = 0;  //if it can't move, it can only attack adjacent tiles
		//worldmap.backup_pathmap();
		//worldmap.create_limited_pathmap(u->color, unitstats[u->type].movetype, u->tilex, u->tiley, m);
		y = 0;
		while (y < worldmap.get_height())
		{
			x = 0;
			while (x < worldmap.get_length())
			{
				arange[x][y] = false;
				x++;
			}
			y++;
		}

		y = 0;
		while (y < worldmap.get_height())
		{
			x = 0;
			while (x < worldmap.get_length())
			{
				if (worldmap.get_tile(x, y)->get_step() < UNREACHABLE)
				{
					if ((any_unit_here(x, y) == -1) || (any_unit_here(x, y) == u->owner * 100 + u->number))
					{
						if (x > 0)
						{
							arange[x - 1][y] = true;
						}
						if (x < worldmap.get_length() - 1)
						{
							arange[x + 1][y] = true;
						}
						if (y > 0)
						{
							arange[x][y - 1] = true;
						}
						if (y < worldmap.get_height() - 1)
						{
							arange[x][y + 1] = true;
						}
					}
				}
				x++;
			}
			y++;
		}

		y = 0;
		while (y < worldmap.get_height())
		{
			x = 0;
			while (x < worldmap.get_length())
			{
				if (arange[x][y])
				{
					lx = (x - worldmap.camera.get_scroll_x()) * MAP_TILE_SIZE + worldmap.camera.get_offset_x();
					ly = (y - worldmap.camera.get_scroll_y()) * MAP_TILE_SIZE + worldmap.camera.get_offset_y();
					buffer_rectfill(lx, ly, lx + MAP_TILE_SIZE, ly + MAP_TILE_SIZE, RED, alpha);
				}
				x++;
			}
			y++;
		}

		//worldmap.restore_pathmap();
	}
	alpha += alphachange;
	if ((alpha >= 0.66) || (alpha <= 0.2)) alphachange *= -1;
}
