#include "allegro.h"

#include "UnitTemplate.h"
#include "UnitInput.h"
#include "gameConstants.h"
#include "AddUnitTemplate.h"

#include <vector>
#include <string>
using namespace std;

UnitTemplate :: UnitTemplate ()
{
  vecPics = new vector <vector <BITMAP *> *> (charAnimNum);

  vector <vector <BITMAP *> *>::iterator the_iterator;
  the_iterator = vecPics -> begin ();
  while(the_iterator < vecPics -> end()) {
    (*the_iterator) = new vector <BITMAP *> ();
    the_iterator++;
  }

  holdFrameFor = new vector <vector <int> *> (charAnimNum);
  vector <vector <int> *>::iterator iter2;
  iter2 = holdFrameFor -> begin ();
  while(iter2 < holdFrameFor -> end()) {
    (*iter2) = new vector <int> ();
    iter2++;
  }
  holdFrameForCurrent = 0;

  numLoops = new vector <int> (charAnimNum, 0);
  numLoopsCurrent = 0;

  loopAfterFrame = new vector <int> (charAnimNum, -1);
  loopToFrame = new vector <int> (charAnimNum, 0);

  addedUnits = new vector <AddUnitTemplate *> (0);
  uI = new UnitInput;

  affiliation = 0;
  maxHP = 1000;
  timeToLive = -20;
  costOfAttack = 0;
  walkMag = ftofix (5.0);
  juiceForPlayer = 0;

  //unit's properties (ethereal, bounces, outofbounds, etc)
  unitPropertyHitbox = 0;
  unitPropertyDefense = 0;
  unitPropertyStatus = 0;
  unitPropertyPlayer = 0;

  //damage
  hitkbDir = ftofix (0.0);
  hitkbMag = ftofix (0.0);
  hitkbDur = 0;
  damageDealt = 0;
  goodStatusDamage = new vector <int> (numGoodStatus, 0);
  badStatusDamage = new vector <int> (numBadStatus, 0);

  //make the sound vector
  soundsToPlay = new vector <SAMPLE *> (numSoundToPlay);

  vector <SAMPLE *>::iterator iter1;
  iter1 = soundsToPlay -> begin ();
  while(iter1 < soundsToPlay -> end()) {
    (*iter1) = NULL;
    iter1++;
  }

  isThePC = false;

  alignStyle = alignStyleNormal;
  alignDist = itofix (0);
  alignAngleMin = itofix (0);
  alignAngleMax = itofix (0);
}

void UnitTemplate :: deleteBitmaps ()
{
  //delete bitmaps
  vector <vector <BITMAP *> *>::iterator iter1;
  vector <BITMAP *>::iterator iter2;

  iter1 = vecPics -> begin ();
  while(iter1 < vecPics -> end()) 
    {
      iter2 = (*iter1) -> begin ();

      while (iter2 < (*iter1) -> end ())
	{
	  destroy_bitmap (*iter2);
	  iter2++;
	}
      iter1++;
    }
}

UnitTemplate :: ~UnitTemplate ()
{
  deleteBitmaps ();

  vector <SAMPLE *>::iterator iter1;
  iter1 = soundsToPlay -> begin ();
  while(iter1 < soundsToPlay -> end()) {
    destroy_sample(*iter1);
    iter1++;
  }
}

void UnitTemplate :: setStatus (vector <string> ss, int status)
{
  int i = 0, iMax = 0;
  vector <int> * vec = NULL;

  if (status == 0 && ss.size () < goodStatusDamage -> size ()) iMax = ss.size ();
  if (status == 0 && ss.size () >= goodStatusDamage -> size ()) iMax = goodStatusDamage -> size ();
  if (status == 1 && ss.size () < badStatusDamage -> size ()) iMax = ss.size ();
  if (status == 1 && ss.size () >= badStatusDamage -> size ()) iMax = badStatusDamage -> size ();

  if (status == 0)
    vec = goodStatusDamage;
  else if (status == 1)
    vec = badStatusDamage;

  if (vec != NULL)
    { for (i = 0; i < iMax; i ++)
      vec -> at (i) = atoi (ss.at (i).c_str ());
    }
}

vector <vector <BITMAP *> *> * UnitTemplate :: getVecPics ()
{ return vecPics;}
