#include "empire.h"

_ai_type defaultai;
_ai_type theai;

extern string unit_name(int i);

void init_default_ai()
{
  int i = 0;
  char na[30];
  ifstream aifile("ai/default.ai");
  while (i < 18)
  {
    aifile >> na >> na >> defaultai.weight[i] >> na >> defaultai.limit[i];
    i++;
  }
  aifile.close();
  theai = defaultai;
}

void load_default_ai()
{
  theai = defaultai;
}

void load_ai(string mapname)
{
  char na[30];
  string path;
  int i = 0;
  path = "maps/" + mapname + ".ai";
  ifstream aifile(path.c_str());
  if (aifile.bad())
  {
    theai = defaultai;
    ofstream aidebug("ai_error.txt", ios::app);
    aidebug << "Couldn't find " << path.c_str() << ", using default instead.\n";
    aidebug.close();
    return;
  }
  while (i < 18)
  {
    aifile >> na >> na >> theai.weight[i] >> na >> theai.limit[i];
    i++;
  }
  aifile.close();
}

void load_ai_from_file(string path)
{
  char na[30];
  int i = 0;
  ifstream aifile(path.c_str());
  if (aifile.bad())
  {
    theai = defaultai;
    ofstream aidebug("ai_error.txt", ios::app);
    aidebug << "Couldn't find " << path.c_str() << ", using default instead.\n";
    aidebug.close();
    return;
  }
  while (i < 18)
  {
    aifile >> na >> na >> theai.weight[i] >> na >> theai.limit[i];
    i++;
  }
  aifile.close();
}

void save_new_aiprofile(string name)
{
  int i = 0;
  string path = "ai/" + name + ".ai";
  ofstream out("ai/ai_types.txt", ios::app);
  ofstream file(path.c_str());
  if ((!out.bad()) && (!file.bad()))
  {
    while (i < 18)
    {
      file << unit_name(i) << "\nweight " << theai.weight[i] << "\nlimit " << theai.limit[i] << "\n";
      i++;
    }
    out << "\n" << name.c_str() << "\n" << name.c_str() << ".ai";
  }
  out.close();
  file.close();
}
