#include "ranges.h"

extern int moves_needed(int mtype, int tx, int ty);
extern int any_unit_here(int tx, int ty);
extern 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 get_rangemin(int type)
{
  switch(type)
  {
    case ARTILLERY:
    case B_SHIP:    
      return 2;
      break;
    case MISSILES:
    case ROCKETS:
      return 3;
      break;
  }
  return 0;
}

int get_rangemax(int type)
{
  switch(type)
  {
    case ARTILLERY:
      return 3;
      break;
    case B_SHIP:
      return 6;
      break;
    case MISSILES:
    case ROCKETS:
      return 5;
      break;
  }
  return 0;
}

int enemy_in_range(_unit *u)
{
  int i, j, z;
  int min = get_rangemin(u->type);
  int max = get_rangemax(u->type);
  j = u->tiley - max;
  if (j < 0) j = 0;
  while ((j <= u->tiley + max) && (j < worldmap.h))
  {
    i = u->tilex - max;
    if (i < 0) i = 0;
    while ((i <= u->tilex + max) && (i < worldmap.l))
    {
      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.0;
	static float alphachange = 0.015;
	int rangemin, rangemax, d, lx, ly, x, y, max_x, max_y;
	bool arange[50][50];
	
	if (u->attacktype == RANGED)
	{
  	rangemin = get_rangemin(u->type);
  	rangemax = get_rangemax(u->type);
  	x = u->tilex - rangemax;
  	y = u->tiley - rangemax;
  	max_x = u->tilex + rangemax + 1;
  	max_y = u->tiley + rangemax + 1;
  	if (max_x > worldmap.scroll_x + XTILES) max_x = worldmap.scroll_x + XTILES;
  	if (max_y > worldmap.scroll_y + YTILES) max_y = worldmap.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.scroll_x) * MAP_TILE_SIZE + worldmap.offset_x;
        	ly = (y - worldmap.scroll_y) * MAP_TILE_SIZE + worldmap.offset_y;
        	buffer_rectfill(lx, ly, lx + MAP_TILE_SIZE, ly + MAP_TILE_SIZE, RED, alpha);
      	}
      	y++;
    	}
    	x++;
  	}
	}
	else if (u->attacktype == DIRECT)
	{
		worldmap.backup_pathmap();
		worldmap.create_limited_pathmap(u->color, u->movetype, u->tilex, u->tiley, intmin(u->move, u->gas));
		y = 0;
		while (y < worldmap.h)
		{
			x = 0;
			while (x < worldmap.l)
			{
				arange[x][y] = false;
				x++;
			}
			y++;
		}
		
		y = 0;
		while (y < worldmap.h)
		{
			x = 0;
			while (x < worldmap.l)
			{
				if (worldmap.tile[x][y].get_step() < 99)
				{
					if (any_unit_here(x, y) == -1)
					{
						if (x > 0)
						{
							arange[x - 1][y] = true;
						}
						if (x < worldmap.l - 1)
						{
							arange[x + 1][y] = true;
						}
						if (y > 0)
						{
							arange[x][y - 1] = true;
						}
						if (y < worldmap.h - 1)
						{
							arange[x][y + 1] = true;
						}
					}
				}
				x++;
			}
			y++;
		}
		
		y = 0;
		while (y < worldmap.h)
		{
			x = 0;
			while (x < worldmap.l)
			{
				if (arange[x][y])
				{
					lx = (x - worldmap.scroll_x) * MAP_TILE_SIZE + worldmap.offset_x;
        	ly = (y - worldmap.scroll_y) * MAP_TILE_SIZE + worldmap.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)) alphachange *= -1;
}
