#include "robot.hpp"


//------------------------ ROBOT Module -------------------------//
//						  v 08 . 03 .  2007						 //
//																 //
//								MT								 //
//---------------------------------------------------------------//



/*
300$ - raw/month
500$ - prod/month
1000$ - fabric/month
*/

/*
.tell <nick> <message>
say <message>


prod <n>	(2000$ by fabric, 2000$ * 2 - auto)
sell <n> <price>
buy <n> <price>

build	(2500$ + 2500$ / 5 month)
abuild	(5000$ + 5000$ / 7 month)
upgrade	(3500$ + 3500$ / 9 month)

*/

#define PRICE_PLANT 2500
#define PRICE_AUTO 5000
#define PRICE_UPGRADE 3500

#define PRICE_KEEPING_RAW 300
#define PRICE_KEEPING_PROD 500
#define PRICE_KEEPING_PLANT 1000
#define PRICE_KEEPING_AUTO 1500
	
//--- Is Player Playing?
int IsPlayerPlaying(CGamer &p)
{
	if (p.money != -1) return 1;
	else return 0;
}
	

// --- Constructor ROBOT --- //
CRobot :: CRobot (CData *d, CSocket *s)
{
	Data = d;
	Socket = s;
	Data -> win = 0;
		
	turn_ = 0;
	make_ = 0;
};


void CRobot :: Pass()
{
	Turn();
};


void CRobot :: Turn()
{
	printf("	~Robot: Turn! \n");
	Socket->SendCommand(COMMAND_ROBOT_TURN);
};


void CRobot :: Upgrade()
{
	printf("	~Robot: Upgrade. \n");
	Socket->SendCommand(COMMAND_ROBOT_UPGRADE);
};


void CRobot :: ABuild()
{
	printf("	~Robot: Auto plant build. \n");
	Socket->SendCommand(COMMAND_ROBOT_ABUILD);
};


void CRobot :: Prod(int n)
{
	printf("	~Robot: prod %d.\n", n);
	Socket->SendCommandWithInt(COMMAND_ROBOT_PROD, n);
}


void CRobot :: Sell(int n, int cost)
{
	printf("	~Robot: sell %d by %d$.\n", n, cost);
	Socket->SendCommandWithInt2(COMMAND_ROBOT_SELL, n, cost);
}


void CRobot :: Buy(int n, int cost)
{
	printf("	~Robot: buy %d by %d$.\n", n, cost);
	Socket->SendCommandWithInt2(COMMAND_ROBOT_BUY, n, cost);
}



//--- MACROSES ---//
int CRobot :: IsWin()
{
	if (Data -> win == 1) printf("~Game: %s won! :-) \n", Data->you.gamer_name);
	if (Data -> win == -1) printf("~Game: %s looser! :'-( \n", Data->you.gamer_name);
	if (Data -> win ==  0)  printf("~Game: %s is playing... :-* \n", Data->you.gamer_name);
 return Data -> win;
};


int CRobot :: IsNeedInAutoPlants()
{
	return Data->you.fabrics;
};


int CRobot :: GetAllPlayers_Prod()
{
	int tmp = 0;
	
	for (int i = 0; i<Data->players_count; i++)
		if (IsPlayerPlaying(Data->players[i]))
			tmp += Data->players[i].prod;

	tmp += Data->you.prod;

 return tmp;
};


int CRobot :: GetAllPlayers_Raw()
{
	int tmp = 0;
	
	for (int i = 0; i<Data->players_count; i++)
		if (IsPlayerPlaying(Data->players[i]))	
			tmp += Data->players[i].raw;

	tmp += Data->you.raw;

 return tmp;
};


int CRobot :: GetAllPlayers_Producing()
{
	int tmp = 0;
	
	for (int i = 0; i<Data->players_count; i++)
	  if (IsPlayerPlaying(Data->players[i]))
		if (MaxProducing(Data->players[i]) > Data->players[i].raw)
		 tmp += Data->players[i].raw;
		else 
		 tmp += MaxProducing(Data->players[i]);

	tmp += MaxProducing(Data->you);

 return tmp;
};


int CRobot :: GetPlayerOptimalBuyN(CGamer &player, int to_prod)
{
	int max = MaxProducing(player);
	if (to_prod < max)
			return max;
		else  
			return 2 * max - to_prod;
};


int CRobot :: GetPlayerOptimalProdN(CGamer &player)
{
	int tmp = MaxProducing(player);
	int prod_n = 0;
		
		if (player.raw < tmp) 
			prod_n = player.raw;
				else  
					prod_n = tmp;
	
 return prod_n;
};




int CRobot :: GetPlayingGamers()
{ 
	int tmp = 0;
	for(int i = 0; i < Data->players_count; i++)
		tmp += IsPlayerPlaying(Data->players[i]);
	
 return tmp+1;
}
int CRobot :: MaxProducing(CGamer &player)
{
	return player.fabrics + 2*player.auto_plants;
}

