#include "empire.h"

int move_priority(_unit* u);

int _player::create_unit(int type, int x, int y, int player)
{
  int i = 0;
  while (i < 50)
  {
    if (unit[i].exists == 0)
    {
      unit[i].create(type, x, y, player, i);
      return i;  //return the number of the newly created unit
    }
    i++;
  }
  return -1;
}

void _player::clear_units()
{
  int i = 0;
  int x, y;
  while (i < 50)
  {
    if (unit[i].exists == 1)
    {
      unit[i].kill();
    }
    unit[i].exists = 0;
    unit[i].ready = 0;
    x = (unit[i].tilex - scroll_x) * 40;
    y = (unit[i].tiley - scroll_y) * 40;
    rectfill(sprite_buffer, x, y, x + 40, y + 40, TRANS);
    unitmap[unit[i].tilex][unit[i].tiley] = -1;
    i++;
  }
}

void _player::ready_units()
{
  int i = 0;
  while (i < 50)
  {
    unit[i].ready = 1;
    i++;
  }
}

int _player::units_in_play()
{
  int i = 0;
  int n = 0;
  while (i < 50)
  {
    if (unit[i].exists == 1)
    {
      n++;
    }
    i++;
  }
  return n;
}

void _player::sort_units()
{
  mergesort_units(0, 49);
}

void _player::mergesort_units(int min, int max)
{
  vector<_unit> temp;
  int left, right, size, i, pivot, a, b;
  size = max - min + 1;
  pivot = (min + max) / 2;
  temp.clear();
  if (min == max)
  {
    return;
  }
  mergesort_units(min, pivot);
  mergesort_units(pivot + 1, max);
  i = 0;
  while (i < size)
  {
    temp.push_back(unit[min + i]);
    i++;
  }
  left = 0;
  right = pivot - min + 1;
  i = 0;
  while (i < size)
  {
    if (right <= max - min)
    {
      if (left <= pivot - min)
      {
        a = move_priority(&temp[left]);
		b = move_priority(&temp[right]);
        if (a > b)
        {
          unit[i + min] = temp[right];
          unit[i + min].number = i + min;
          if (unit[i].exists == 1)
          {
            unitmap[unit[i].tilex][unit[i].tiley] = (unit[i].color * 100) + unit[i].number;
          }
          right++;
        }
        else
        {
          unit[i + min] = temp[left];
          unit[i + min].number = i + min;
          if (unit[i].exists == 1)
          {
            unitmap[unit[i].tilex][unit[i].tiley] = (unit[i].color * 100) + unit[i].number;
          }
          left++;
        }
      }
      else
      {
        unit[i + min] = temp[right];
        unit[i + min].number = i + min;
        if (unit[i].exists == 1)
        {
          unitmap[unit[i].tilex][unit[i].tiley] = (unit[i].color * 100) + unit[i].number;
        }
        right++;
      }
    }
    else
    {
      unit[i + min] = temp[left];
      unit[i + min].number = i + min;
      if (unit[i].exists == 1)
      {
        unitmap[unit[i].tilex][unit[i].tiley] = (unit[i].color * 100) + unit[i].number;
      }
      left++;
    }
    i++;
  }
}

void _character::create(string n, string d, int s, int w, int p, string pn, BITMAP *pic)
{
  name = n;
  description = d;
  strength = s;
  weakness = w;
  power = p;
  powername = pn;
  picture = create_bitmap(80, 80);
  clear_to_color(picture, BLACK);
  stretch_sprite(picture, pic, 0, 0, 80, 80);
}

void _character::destroy()
{
  name = "";
  description = "";
  strength = 5;
  weakness = 5;
  power = 0;
  powername = "";
  destroy_bitmap(picture);
}

int move_priority(_unit* u)
{
  if (u->attacktype == RANGED)
  {
    return 0;
  }
  else if (u->attacktype == DIRECT)
  {
    if ((u->type == INFANTRY) || (u->type == MECH))
    {
      return 2;
    }
    else
    {
      return 1;
    }
  }
  else
  {
    if ((u->type == APC) || (u->type == T_COPTER))
    {
      return 3;
    }
    else
    {
      return 4;
    }
  }
  return 99;
}
