#include "empire.h"

void make_new_units(int plyr)
{
  static int price[18];
  static int first = 1;
  unsigned int i, j, min;
  int built_unit, z;
  int can_build;
  bool conserve_money = false;
  vector<_buildorder> order;
  _buildorder temp;
  _unit t;
  _loc l;
  int num[18];
  if (first == 1)
  {
    i = 0;
    while (i < 18)
    {
      t.create(i, -1, -1, -1, -1);
      price[i] = t.price;
      i++;
    }
    first = 0;
  }
  i = 0;
  while (i < 18)
  {
    num[i] = 0;
    i++;
  }
  order.resize(18);
  i = 0;
  while (i < 50)
  {  //this loop finds how many units of each type the player has on the field
    if (player[plyr].unit[i].exists == 1)
    {
      num[player[plyr].unit[i].type]++;
    }
    i++;
  }
  can_build = check_can_build(plyr, price, num);
  while (can_build == 1)
  {
    i = 0;
    while (i < 18)
    {
      order[i].unit = i;
      order[i].ratio = get_ratio(i, theai.weight[i], theai.limit[i], num[i]);
      i++;
    }
    i = 0;
    while (i < order.size() - 1)
    {  //arrange order in order of smallest ratio to largest
      min = i;
      j = i;
      while (j < order.size())
      {
        if (order[j].ratio < order[min].ratio)
        {
          min = j;
        }
        j++;
      }
      if (min != i)
      {
        temp = order[i];
        order[i] = order[min];
        order[min] = temp;
      }
      i++;
    }
    built_unit = -1;
    i = 0;
    while ((built_unit == -1) && (conserve_money == false))
    {
      if (price[order[i].unit] <= player[plyr].cash)
      {  //if the player can afford the unit
        if (num[i] <= theai.limit[i])
        {  //if there aren't already too many of that type
          l = open_build_loc(plyr, order[i].unit);
          if (l.x != -1)  //if there is a building that can make this unit
          {
            z = player[plyr].create_unit(order[i].unit, l.x, l.y, plyr);
            player[plyr].cash -= price[order[i].unit];
            built_unit = order[i].unit;
          }
        }
      }
      else
      {  //if the player can't afford the unit
        if (num[i] <= theai.limit[i])
        {  //if it hasn't hit the limit for this unit type
          l = open_build_loc(plyr, order[i].unit);
          if (l.x != -1)
          {  //if there is an open build location for the unit
            if (enemy_threat(plyr) == false)
            {  //if the enemy is far away, build up cash for a better unit
              conserve_money = true;
              if (num[INFANTRY] + num[MECH] < 4)
              {  //unless there are few or no building cappers
                l = open_build_loc(plyr, INFANTRY);
                if (l.x != -1)
                {  //and if an infantry or mech could be built
                  if (player[plyr].cash >= price[MECH])
                  {  //build a mech
                    conserve_money = false;
                    z = player[plyr].create_unit(MECH, l.x, l.y, plyr);
                    player[plyr].cash -= price[MECH];
                    built_unit = MECH;                    
                  }
                  else if (player[plyr].cash >= price[INFANTRY])
                  {  //or an infantry, depending on the financial situation
                    conserve_money = false;
                    z = player[plyr].create_unit(INFANTRY, l.x, l.y, plyr);
                    player[plyr].cash -= price[INFANTRY];
                    built_unit = INFANTRY;
                  }
                }
              }
            }
          }
        }
      }
      i++;
    }
    if (conserve_money == false)
    {
      num[built_unit]++;  //increase unit counter to include new build
      can_build = check_can_build(plyr, price, num);
    }
    else
    {
      can_build = 0;
    }
  }
}

int check_can_build(int plyr, int price[], int num[])
{
  int i = 0;
  _loc l;
  if (player[plyr].units_in_play() >= 50)
  {  //if the player has reached the unit limit
    return 0;
  }
  while (i < 18)
  {  //for every type of unit:
    if (num[i] < theai.limit[i])
    {  //if the limit for this unit hasn't been reached
      if (price[i] <= player[plyr].cash)
      {  //if the player can afford the unit
        l = open_build_loc(plyr, i);
        if (l.x != -1)
        {  //if there is a building that can build the unit this turn
          return 1;  //then the player can build
        }
      }
    }
    i++;
  }
  return 0;
}

float get_ratio(int i, int weight, int limit, int num)
{
  float r = 100;
  if ((weight > 0) && (limit > num))
  {  //the lower the ratio, the higher the build priority
    r = float(num + 1) / float(weight);
  }  //if the weight is 0 or the limit is reached, the ratio stays at 100;
  return r;
}
