#include "empire.h"

int mapcolor(int set, int num);
extern int player_color(int c);

void _minimap::create()
{
  int x, y, s, n, z;
  time = 0;
  draw_called = false;
  a = 0;
  
	if (bmp)
	{
	  bmp->Destroy();
	}
	bmp = new Bitmap(worldmap.l * 4, worldmap.h * 4);
  
	Canvas::SetTo(*bmp);
	Settings::RestoreOldProjection();

	Rect(0, 0, worldmap.l * 4, worldmap.h * 4).Draw(Rgba(BLACK, 255));
  _tile *t;
  y = 0;
  while (y < worldmap.h)
  {
    x = 0;
    while (x < worldmap.l)
    {
      t = &worldmap.tile[x][y];
      s = t->get_set();
      n = t->get_number();
      z = x + y * worldmap.l;
      
			Rect(float(x * 4), float(y * 4), 4.0, 4.0).Draw(Rgba(mapcolor(s, n), 255));
			
      if (s == 4)  //buildings
      {
        if (n > 3)  //if a normal building
        {
					Rect(float(x * 4), float(y * 4), 3.0, 3.0).DrawOutline(Rgba(GREY, 255));
        }
        else  //if an HQ
        {
					Rect(float(x * 4), float(y * 4), 3.0, 3.0).DrawOutline(Rgba(WHITE, 255));
        }
      }
			if (s == 6)  //special tiles
			{
				if (n == 0) //teleporter
				{
					Point(float(x * 4 + 1), float(y * 4)).Draw(Rgba(WHITE, 255));
					Point(float(x * 4), float(y * 4 + 2)).Draw(Rgba(WHITE, 255));
					Point(float(x * 4 + 2), float(y * 4 + 3)).Draw(Rgba(WHITE, 255));
					Point(float(x * 4 + 3), float(y * 4 + 1)).Draw(Rgba(WHITE, 255));
				}
			}

      x++;
    }
    y++;
  }

	Canvas::SetTo(SCREEN_BACKBUF);
	Settings::SetOrthographicProjection(640, 480);
}

void _minimap::check()
{  //this function should be called during the main game's logic loop
  if (draw_called)
  {  //if the minimap was drawn last logic loop
    draw_called = false;
    a += 0.03;
    if (a > 0.7) a = 0.7;
  }
  else
  {
    a -= 0.05;
    if (a <= 0) a = 0;
    if (a > 0) draw(false);
    draw_called = false;
  }
}

void _minimap::draw(bool mapeditor, int x, int y, bool outline)
{
  int i, j, z;
  
  draw_called = true;

  buffer_blit(bmp, x, y, a);
  
  time++;
  if (time / 50 == 0)
  {  //show where the units are
    j = 0;
    while (j < worldmap.h)
    {
      i = 0;
      while (i < worldmap.l)
      {
        z = worldmap.tile[i][j].unit_here();
        if (z != -1)
        {
          buffer_mapblip(i * 4 + x, j * 4 + y, player_color(z / 100), a + 0.2);
        }
        i++;
      }
      j++;
    }
  }
  if (time > 100)
  {
    time = 0;
  }
  if (outline)
  {
    if (mapeditor)  //show where the viewable area is if in the map editor
    {
      buffer_rect((worldmap.scroll_x * 4) + 1 + x, (worldmap.scroll_y * 4) + 1 + y, (worldmap.scroll_x + XTILES) * 4 + x, (worldmap.scroll_y + YTILES - (96 / MAP_TILE_SIZE)) * 4 + y, BLACK, a + 0.2);
    }
    else
    {
      buffer_rect((worldmap.scroll_x * 4) + 1 + x, (worldmap.scroll_y * 4) + 1 + y, (worldmap.scroll_x + XTILES) * 4 + x, (worldmap.scroll_y + YTILES) * 4 + y, WHITE, a + 0.2);
    }
  }
  buffer_rect(x, y, (worldmap.l * 4) + 1 + x, (worldmap.h * 4) + 1 + y, BLACK, a + 0.2);
}

void _minimap::destroy()
{
  if (bmp)
  {
    bmp->Destroy();
  }
}

int mapcolor(int set, int num)
{
  int BRED = makecol(200, 0, 0);
  int BGREEN = makecol(0, 200, 0);
  int BBLUE = makecol(0, 0, 200);
  int BYELLOW = makecol(200, 200, 0);
  int color = BLUE;
  if (set == 0)
  {
    if ((num == 0) || (num == 1) || (num > 4))
    {  //if it is a road
      color = DGREY;
    }
    else if (num == 2)
    {
      color = makecol(0, 100, 0);
    }
    else if (num == 3)
    {
      color = GREEN;
    }
    else if (num == 4)
    {
      color = makecol(190, 180, 20);
    }
  }
  else if ((set == 2) && (num == 1))  //reef
  {
    color = makecol(200, 200, 250);
  }
  else if (set == 3)  //shoal
  {
    color = makecol(225, 220, 140);
  }
  else if (set == 4)  //buildings
  {
    if (num < 4)
    {
      switch(num % 4)  //capitals
      {
        case 0:
          color = BRED;
          break;
        case 1:
          color = BBLUE;
          break;
        case 2:
          color = BGREEN;
          break;
        case 3:
          color = BYELLOW;
          break;
      }
    }
    else
    {
      switch(num % 5)  //other buildings (which can be neutral)
      {
        case 0:
          color = BRED;
          break;
        case 1:
          color = BBLUE;
          break;
        case 2:
          color = BGREEN;
          break;
        case 3:
          color = BYELLOW;
          break;
        case 4:
          color = LGREY;
          break;
      }
    }
  }
  else if (set == 6)
  {
    switch (num)
    {
      case 0:  //teleport
        color = makecol(128, 128, 255);
        break;
      case 1:  //barrier
        color = BLACK;
        break;
    }
  }
  return color;
}
