#include "empire.h"

setunit mapunit[200];

extern Bitmap *mapunits[72];

void draw_units(int scrollx, int scrolly);
void draw_unit(int num, int x, int y);
void new_unit(int x, int y, int type, int player);
void new_unit(int x, int y, int type, int player, int sound);
void clear_all_units();
void remove_unit(int n);
int find_free_unit();
int check_for_unit(int x, int y);
int munit_exists(int playernum);
int units_for_player(int plyr);

void draw_units(int scrollx, int scrolly)
{
  int i = 0;
  while (i < 200)
  {
    if (mapunit[i].exist == 1)
    {
      if ((mapunit[i].x >= scrollx) && (mapunit[i].x < scrollx + XTILES))
      {
        if ((mapunit[i].y >= scrolly) && (mapunit[i].y < scrolly + YTILES - 3))
        {
          draw_unit(i, (mapunit[i].x - scrollx) * MAP_TILE_SIZE, (mapunit[i].y - scrolly) * MAP_TILE_SIZE);
        }
      }
    }
    i++;
  }
}

void clear_all_units()
{
  int i = 0;
  while (i < 200)
  {
    mapunit[i].exist = 0;
    i++;
  }
}

void draw_unit(int num, int x, int y)
{
  setunit t = mapunit[num];
  buffer_draw_sprite(mapunits[(t.type * 4) + t.player], x, y);
}

void new_unit(int x, int y, int type, int player)
{
  new_unit(x, y, type, player, 1);
}

void remove_unit(int n)
{
  int i = n + 1;
  while (i < 199)
  {
    mapunit[n] = mapunit[i];
    n++;
    i++;
  }
}

void new_unit(int x, int y, int type, int player, int sound)
{
  int n;
  int z = check_for_unit(x, y);
  if (z == -1)  //if no unit in this spot
  {
    n = find_free_unit();  //initialize a new one
    if (n != -1)
    {
      if (units_for_player(player) < 50)
      {
        if (sound == 1)
        {
          play_sound(S_UNITDOWN);
        }
        mapunit[n].exist = 1;
        mapunit[n].x = x;
        mapunit[n].y = y;
        mapunit[n].type = type;
        mapunit[n].player = player;
      }
    }
  }
  else  //if a unit is already there, redefine its attributes to what was selected
  {
    if (!((mapunit[z].type == type) && (mapunit[z].player == player)))
    {  //if a unit of the exact same type is here already, it won't do anything
      if (sound == 1)
      {
        play_sound(S_UNITDOWN);
      }
      mapunit[z].type = type;
      mapunit[z].player = player;
    }
  }
}

int check_for_unit(int x, int y)
{
  int i = 0;
  while (i < 200)
  {
    if (mapunit[i].exist == 1)
    {
      if ((mapunit[i].x == x) && (mapunit[i].y == y))
      {
        return i;
      }
    }
    i++;
  }
  return -1;
}

int find_free_unit()
{
  int i = 0;
  while (i < 200)
  {
    if (mapunit[i].exist == 1)
    {
      i++;
    }
    else
    {
      return i;
    }
  }
  return -1;
}

int munit_exists(int playernum)
{
  int i = 0;
  while (i < 200)
  {
    if (mapunit[i].exist == 1)
    {
      if (mapunit[i].player == playernum)
      {
        return 1;
      }
    }
    i++;
  }
  return 0;
}

int units_for_player(int plyr)
{
  int n = 0;
  int i = 0;
  while (i < 200)
  {
    if (mapunit[i].exist == 1)
    {
      if (mapunit[i].player == plyr)
      {
        n++;
      }
    }
    i++;
  }
  return n;
}
