#include "Agent.h"
#include "rnorrexp.h"
#include"Group.h"

// Default Agent Constructor
Agent::Agent()
{
	capital = 100;
	groupIndex = -1;
	age = 0;
	bestStratIndex = 0;
	bestStratScore = 100;
	avgStratScore = 0;
	stratPter = (Strategy **)malloc(sizeof(Strategy)*10);	//new
	for(int i=0;i<10;i++)
	{
		stratPter[i] = new Strategy;
	}

	//different part
	death = -1;
	drop = -1;

}


Agent::Agent(int avgAge,int dropS)
{	
	//this part same as Agent()
	capital = 100;
	groupIndex = -1;
	age = 0;
	bestStratIndex = 0;
	bestStratScore = 100;
	avgStratScore = 0;
	stratPter = (Strategy **)malloc(sizeof(Strategy)*10);	//new
	for(int i=0;i<10;i++)
	{
		stratPter[i] = new Strategy;
	}

	//different part
	drop=dropS;
	//age=0;
	//zigset(time(NULL));
	zigset(1684623);
	float a = RNOR;	//provide a standard normal variate with mean zero, variance 1
	a = a*5;
	a = a+avgAge;
	if(avgAge<0)
	{
		
		death=-1;
	}
	else
	{
		if(a>100)
		{
			death=100;
		}
		else if(a<1)
		{
			death=1;
		}
		else
		{
			death=(int) a;
		}
	}
	drop = dropS;
}

int Agent::getGroupIndex()
{
	return groupIndex;
}

int Agent::getAge()
{
	return age;
}

int Agent::aging()
{
	int rt = 1;
	age++;
	if(age == death)
	{
		rt = 0;
	}
	return rt;
}

//void Agent::setGroupIndex(int numGrps)
void Agent::setGroupIndex(int newGroupIndex)
{
	groupIndex = newGroupIndex;
	//groupIndex = rand()%(numGrps);
}

void Agent::allocateGroup(int numGrps)
{
	groupIndex = rand()%(numGrps);
}


void Agent::setDecision(int newDecision)
{
	decision = newDecision;
}

int Agent::getCapital()
{
	return capital;
}

int Agent::getAvgStratScore()
{
	return avgStratScore;
}

int Agent::getBestStratScore()
{
	return bestStratScore;
}





/*Give the result to all the agents. Agents update their capital and strategy scores*/
int Agent::tellResult(int result,int STI)
{
	int rt;
	int tmpSum = 0;
	if(result == decision)	//win
	{
		capital++;
		rt = 1;
	}
	else			//lose
	{
		capital--;
		rt = 0;
	}
	//update strategy scores
	for(int i=0;i<10;i++)	//10 strategies
	{
		stratPter[i]->updateScore(result,STI);
		//remove low scored strat
		if(stratPter[i]->getScore() < 0)
		{
			stratPter[i] = new Strategy;
		}
		//update the new best strategy
		tmpSum += stratPter[i]->getScore();
		if(stratPter[i]->getScore() > bestStratScore)
		{
			bestStratIndex = i;
			bestStratScore = stratPter[i]->getScore();
		}
		//stratArray[i]->updateScore(result,STI);
	}
	

	avgStratScore = tmpSum/10;
	return rt;
}


int Agent::getDecision()
{
	return decision;
}

void Agent::makeOwnDecision(int STI)
{
	
	//get decision from the best strategy
	//int tmpDecision = stratPter[bestStratIndex]->getDecision(STI);
	decision = stratPter[bestStratIndex]->getDecision(STI);

}

bool Agent::isDead(pass_graph* stuff)
{
	if(age<=100)
	{
		stuff->age[age-1]++;			
		if(death<0)
		{
			return false;
		}
		else if(age>=death)
		{
			return true;
		}
		return false;
	}
}
	
//Agent Class Destructor
Agent::~Agent(){
	for(int i=0;i<10;i++)
	{
		delete stratPter[0];
	}
	delete stratPter;
}


