#include "Character.h"





//////////////////////////////////////////////////////////////
//void Unit::draw(BITMAP* n)
//{

//masked_blit(source,dest, source_X,source_Y,x,  y, w, h);
//masked_blit((BITMAP*)data[source].dat,n, source_X,source_Y,x,  y, w, h);
//}
//Character.cpp




void draw_bar(BITMAP *buf, int x, int y, int w, int h, int value, int range, int backcol, int startcol, int endcol)
 {
  /* l = low, h = high, i = inc */
 float lr,lb,lg,hr,hb,hg,ir,ib,ig,r,g,b;

  lr = getr(startcol); hr = getr(endcol);
  lg = getg(startcol); hg = getg(endcol);
  lb = getb(startcol); hb = getb(endcol);

  //drawing_mode(DRAW_MODE_TRANS,NULL,0,0);
  rectfill(buf,x+2,y+2,x+w+2,y+h+2,backcol);
  solid_mode();

  w = (int)( ((float)value/(float)range)*(float)w );

  ir = (hr-lr) / (float)w;
  ig = (hg-lg) / (float)w;
  ib = (hb-lb) / (float)w;

  int iy, ix;
  for (iy=y; iy<y+h; iy++) {
    r = lr; g = lg; b = lb;
    for (ix=x; ix<x+w; ix++) {
      fastputpixel(buf,ix,iy,makecol((int)r,(int)g,(int)b));
      r+=ir; g+=ig; b+=ib;
    }
  }
}

void drawRectangle(BITMAP *bmp,int x, int y, int w,int h,int color, int style, int bgcolor);
void draw_bar2(BITMAP *buf, int x, int y, int w, int h, int value, int range, int backcol, int startcol,int endcol)
 {
	value = ((double)value/range) * w;
	range = w; 
	drawRectangle(buf,x,y,range,h,backcol,1,backcol);
	drawRectangle(buf,x,y,value,h,startcol,1,startcol);
}




void Character::setuppath(Layer a)
{
//	Astar = new AstarPathfinder(a,0);
}

int Character::get_type()
{
	return type;
}
void Character::set_mesageLife(int t)
{
	msglife=t;
}

//gets
string Character::Get_unit()
{
return toString();
}

string Character::Get_dieolog()
{

	return dieolog;
}
int Character::Get_desty()
{

	return desty;
}
int Character::Get_destx()
{

	return destx;
}
int Character::Get_state()
{

	return state;
}


string Character::Get_race()
{
	return race;
}
gender Character::Get_sex()
{
	return sex;
}
//sets
void Character::Set_dieolog(string ch )
{
	dieolog = ch;
}
void Character::Set_desty(int ch )
{
	desty = ch;
}
void Character::Set_destx(int ch )
{
	destx = ch;
}
void Character::Set_state(int ch )
{
	state = ch;
}
void Character::Set_unit(string ch )
{
	character = ch;
}

void Character::Set_race(string New_race)
{
	race =  New_race ;
}
void Character::Set_sex(gender New_sex)
{
	sex = New_sex;
}
//construters
Character::Character()
{
	 x = 530 ;
	 y = 294 ;
	Set_inarea(1);
	/////////////////////rig
	itembag.usedslots=1;//rig
	itembag.items[0].something_in_slot=true;//rig
	itembag.items[0].itemid=0; //rig
	itembag.items[0].nuberofitems=300;//rig

 //////////////////////////////rig
	lefthand=0;
	righthand=0;
	torso=0;
	legs=0;
	feet=0;
	asscer1=0;
	asscer2=0;
 
	dmcon=0;
	t1=0;
	npcmovmenttime=520;
	t2=0;
	npcmovmenttime2=5200;
	type=1;
	damagetaken = -1;
	predamagetaken=-1;
	attributess = Statss();
	msglife=0;
	dieolog="";
	race = "none";
	character = "none";
	sex = none;
	state = 1;
	//destx = x;
	//desty = y;
}
//Character::~Character()
//{

//}
void Character::runStatScript()
{
	if(Get_socketid()!=-1)//rigg
	{
		attributess.movmentRate=2;
	}
	if(Get_socketid()==-1)
	{

		if(computerId==0)
		{
			if(t2==npcmovmenttime2)	//undate
			{
				if(state== 1)
				{

					
					if (loding == 0)
					{

						t2=0;
						t1=0;
						inboxbuffer +="0|"+NumberToString(0)+"|002|"+NumberToString(Get_id())+ ','+ NumberToString(Get_X()+(input(0,50) - input(0,100))) +','+ NumberToString(Get_Y()+(input(0,50) - input(0,100))) +
						','+NumberToString(Get_X())+','+NumberToString(Get_Y())+
						','+NumberToString(Get_direction() )+','+NumberToString(Get_frame())+','
							+NumberToString(Get_inarea())+"`~";
					}
				}
			}
			t2++;
		}
		if(t1==npcmovmenttime)	
		{
			t1=0;
			if(state== 1)
			{
				//Set_destx(Get_X()+(input(0,50) - input(0,100)));//rig glich get out of cyinck

				//	Set_desty(Get_Y()+(input(0,50) - input(0,100)));//rig glich get out of cyinck
				
		

			}
		}
		t1++;
	}
	
		


	if(attributess.Gethp()<1) //check to see it dead
	{
		Set_inarea(1);
		state=3;

		//delel npc charitor
		if(name=="nim")
		{
			if(computerId==0)//only server
			{
				inboxbuffer += "0|0|005|"+NumberToString(id)+"~";//delet char from screens
			}
		}

		if(charnetId==Get_id())
		{
			ch_current_area = 1;//rig

		}
	}
	else
	{
		if((x!=destx ||y!=desty) && state != 4)
		{
			state = 2; //walking
		}
		else if ((x==destx && y==desty)&& state != 4)
		{
			state = 1; //idel
			commanded=0;
		}

	}


	if(state!=3)//if not dead
	{
		if(onesecpased==attributess.regentime)
		{

			if(attributess.attackstamana < attributess.attackstamana_max) 
			{
				attributess.attackstamana=attributess.attackstamana +(attributess.attackstamana_regen + attributess.bonce_attackstamana_regen);
  		
				if(attributess.attackstamana>attributess.attackstamana_max)
				{
					attributess.attackstamana=attributess.attackstamana_max;
				}
			}
			//if(attributess.regentime==true)
			//{
			if(attributess.Gethp()<attributess.Gethp_max()) 
			{
				attributess.Sethp(attributess.Gethp()+(attributess.hp_regen + attributess.bonce_hp_regen));
  			}

			if(attributess.Getmp()<attributess.Getmp_max()) 
			{
				attributess.Setmp(attributess.Getmp()+(attributess.mp_regen + attributess.bonce_mp_regen));
 			}
			attributess.regentime=(!attributess.regentime);	
			//}
		}
	}
}



void Character::fromString()
{	
	int j= 0;
	int i = 0;
	//maybey i acn make the conver for sring to char* right in here
	string temps[22]; //arry to hold data
	
	while (character[i]!= '`') //stay in untl you fing the ` at the end
	{
		if (character[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += character[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}
	type = StringToNumber(temps[0]);
	name = temps[1];
	//race = temps[2];
	//sex = temps[3];
	//npcs.insert("ghgh,dfhg,hjh,2060,1594,32,32,3,32,32,0,1,6,4,1,5`");
	x = StringToNumber(temps[4]);  //move data from temp holder 

	y = StringToNumber(temps[5]);
	
	w = StringToNumber(temps[6]);
	
	h = StringToNumber(temps[7]);
	
	layer = StringToNumber(temps[8]);
	
	source_X = StringToNumber(temps[9]);

	source_Y = StringToNumber(temps[10]);
	 //bool collision= StringToNumber(temps[11]);
	
	frame = StringToNumber(temps[12]);
	direction= StringToNumber(temps[13]);
	
	source = StringToNumber(temps[14]);
	
	//dest= StringToNumber(temps[15]);
	id = StringToNumber(temps[16]);
	
	//	textprintf_centre(screen, font, 40, 200, makecol(255, 255, 255),"test source:%d", id);
	inarea = StringToNumber(temps[17]);
	//Sleep(5000);
	socketid = StringToNumber(temps[18]);// scoketId

}



string Character::toString()
{
	
		character = NumberToString(type) + ","+ name + "," + race + "," + "sex" +
	"," + NumberToString(x) + 
	"," + NumberToString(y) + "," + NumberToString(w) +"," +NumberToString(h) +
	"," + NumberToString(layer) + "," + NumberToString(source_X) + 
	"," + NumberToString(source_Y) + "," + NumberToString(collision) + 
	"," + NumberToString(frame) + "," + NumberToString(direction) +
	"," + NumberToString(source) + "," + "dest" + 
	"," + NumberToString(id) + ","+ NumberToString(inarea) + "," + NumberToString(socketid) +"`";
//	textout(screen,font,character.c_str(),100,50*id,makecol(255,255,255));
	//Sleep(10000);
	return character;
}

void Character::SendStats()
{
	/*
		string hold = id +",";
		hold += attributess.toString();
		outboxbuffer += d+"|0|009|"+ hold +'~';
	*/
}



void Character::runRangeScript(Unit *onewhotiggeredme,DATAFILE *music)
{
	
   if( Get_name()== "NimMaster" || onewhotiggeredme->Get_name()== "NimMaster")
   {
		if( Get_name()== "NimMaster" )
		{
			inboxbuffer +="0|" + NumberToString(0) + "|003|" + NumberToString(id) + "," +"Hey wait up" +"`~";
			//speak
		}
	   /*

						if(socketid==-1)
						{
							startgameofnim = true;
							Set_X(420);
							Set_Y(100);
						Set_destx(Get_X());
						Set_desty(Get_Y());
				


							onewhotiggeredme->Set_X(940);
							onewhotiggeredme->Set_Y(100);
						onewhotiggeredme->Set_destx(onewhotiggeredme->Get_X());
						onewhotiggeredme->Set_desty(onewhotiggeredme->Get_Y());



						}
						else
						{
						startgameofnim = true;
							Set_X(940);
							Set_Y(100);
						Set_destx(Get_X());
						Set_desty(Get_Y());

							onewhotiggeredme->Set_X(420);
							onewhotiggeredme->Set_Y(100);
						onewhotiggeredme->Set_destx(onewhotiggeredme->Get_X());
						onewhotiggeredme->Set_desty(onewhotiggeredme->Get_Y());
						}
						*/

   }
   else
   {



		if(attributess.attackstamana >= 100)/// do we have enough stamna
		{	
			if(state!=3 && onewhotiggeredme->state !=3 && (onewhotiggeredme->type==1 && type==1))//bouth or one dead
			{			
				if(state == 1)
				{
						if (socketid == -1)
						{
							//outboxbuffer += "0|"+NumberToString(computerId)+"|008|"+NumberToString(id)+","+NumberToString(targget)+"`~";
							//targget =-1	;
						}
						//Set_destx(Get_X());
						//Set_desty(Get_Y());

						if(commanded!=1) //if not commande to walk
						{
							commanded=3;// some glichs
							/*
							//////////////////////////////////////////////////////////needs to be fixed
							int min=99;

							if((onewhotiggeredme->Get_X() +(onewhotiggeredme->Get_W() *.5))  == (Get_X()+(Get_W() *.5))	)
							{

							}
							else if((onewhotiggeredme->Get_X() +(onewhotiggeredme->Get_W() *.5))  > (Get_X()+(Get_W() *.5))	)// true = X right  false = X left
							{

								min=abs((onewhotiggeredme->Get_X() +(onewhotiggeredme->Get_W() *.5))  - (Get_X()+(Get_W() *.5)));
								// true = X right
									Set_direction(1);
								if(( onewhotiggeredme->Get_Y()+(onewhotiggeredme->Get_H() *.5)) > (Get_Y()+(Get_H() *.5) ))
								{
									if(( onewhotiggeredme->Get_Y()+(onewhotiggeredme->Get_H() *.5)) > (Get_Y()+(Get_H() *.5) ))// true = Y UP  false = Y DOWN
									{
										//true = Y UP
										Set_direction(0);

									}
									else
									{
										//false = Y DOWN
										Set_direction(2);
									}
								}

							}
							else
							{
								min=abs((onewhotiggeredme->Get_X() +(onewhotiggeredme->Get_W() *.5))  - (Get_X()+(Get_W() *.5)));
								//false = X left
								Set_direction(3);
								if(( onewhotiggeredme->Get_Y()+(onewhotiggeredme->Get_H() *.5)) > (Get_Y()+(Get_H() *.5) ))
								{
									if(( onewhotiggeredme->Get_Y()+(onewhotiggeredme->Get_H() *.5)) > (Get_Y()+(Get_H() *.5) ))// true = Y UP  false = Y DOWN
									{
										//true = Y UP
										Set_direction(0);

									}
									else
									{
										//false = Y DOWN
										Set_direction(2);
									}
								}
							}

				//////////////////////////////////////////////////////////////////////////////

*/
/*
int min=99;
int tempmis=99;
 tempmis=abs((onewhotiggeredme->Get_X() +(onewhotiggeredme->Get_W() *.5))  - (Get_X()+(Get_W() *.5)));
	if (tempmis<min	)
	{
	min=tempmis;
		Set_direction(1);
	}
 tempmis=abs( (onewhotiggeredme->Get_X() +(onewhotiggeredme->Get_W() *.5)) +(Get_X()+(Get_W() *.5) ));
	if (tempmis<min	) 
	{
	min=tempmis;
		Set_direction(3);
	}
tempmis=abs((onewhotiggeredme->Get_Y() + (onewhotiggeredme->Get_H() *.5))  - (Get_Y()+(Get_H() *.5)));  

	if( tempmis<min	)
	{
	min=tempmis;	
	Set_direction(0);
	}
tempmis=abs(( onewhotiggeredme->Get_Y()+(onewhotiggeredme->Get_H() *.5)) + (Get_Y()+(Get_H() *.5) ) ) ;
    if(tempmis<min	)
	{
	min=tempmis;
		Set_direction(2);
	}
     */
/*
if(id==charnetId)
{
listofProjectile =  new Projectile();
			netId++;
			listofProjectile->Set_name("projectile");
									p1->Set_id(netId);
			listofProjectile->Set_X(((Character*)logonUnits.Get_Unit( charnetId ))->Get_X() +50);
			listofProjectile->Set_Y(((Character*)logonUnits.Get_Unit( charnetId ))->Get_Y()+50 );
			listofProjectile->Set_inarea(	((Character*)logonUnits.Get_Unit( charnetId ))->Get_inarea() ) ;




			listofProjectile->Set_destx(((Character*)logonUnits.Get_Unit( charnetId ))->Get_X()+500);
			listofProjectile->Set_desty(((Character*)logonUnits.Get_Unit( charnetId ))->Get_Y()+500);

						inboxbuffer += "0|"+NumberToString(computerId)+"|001|"+listofProjectile->toString()+"~";//mite need to send stats here


			inboxbuffer +="0|"+NumberToString(charnetId)+"|002|"+NumberToString(netId)+ ','+ NumberToString(((Character*)logonUnits.Get_Unit( netId ))->Get_X()+500) +','+ NumberToString(((Character*)logonUnits.Get_Unit( netId ))->Get_Y()+500) +
			','+NumberToString(logonUnits.Get_Unit( charnetId )->Get_X())+','+NumberToString(logonUnits.Get_Unit( charnetId )->Get_Y())+
			','+NumberToString(logonUnits.Get_Unit( charnetId )->Get_direction() )+','+NumberToString(logonUnits.Get_Unit( charnetId )->Get_frame())+','+NumberToString(logonUnits.Get_Unit( charnetId )->Get_inarea())+"`~";
				
}
*/
					
							//attack((Character*)onewhotiggeredme ,music);
						}
				}
			}

		}
		else
		{
			/*
			if(onewhotiggeredme->Get_X() < Get_X())
			{
				Set_destx(onewhotiggeredme->Get_X()+50);

			}
			else
			{
				Set_destx(onewhotiggeredme->Get_X()-50);
			}


			if(onewhotiggeredme->Get_Y() < Get_Y())
			{
				Set_desty( onewhotiggeredme->Get_Y()+50);//rig

			}
			else
			{
				
				Set_desty( onewhotiggeredme->Get_Y()-50);//rig
			}
*/

/*
			if((type==1 && b->type==1) && (Get_inarea() == b->Get_inarea()))
			{
				if(target==-1)
				{
					target=b->Get_id();
				}


				if(target!=-1)
				{
					if(state==1&& b->state!=3 && Get_destx()==Get_X() &&Get_desty()==Get_Y() )//if idel
					{
						if(is_collision(b)==false)
						{
							Set_destx(b->Get_X());//rig glich get out of cyinck

							Set_desty(b->Get_Y());//rig glich get out of cyinck
						}
						else
						{
							Set_destx(Get_X());//rig glich get out of cyinck

						Set_desty(Get_Y());//rig glich get out of cyinck
						}

					}
				
				}

			
			}


*/
		}

		if(Get_socketid()==-1)
		{

		
		}
   }
			
}


void Character::runSiteScript(Unit *onewhotiggeredme,DATAFILE *music)
{
if( Get_name()!= "NimMaster" )
		{

						if(commanded!=1&& commanded!=3) //if not commande to walk and not attack
						{

	if((type==1 && onewhotiggeredme->type==1) && (Get_inarea() == onewhotiggeredme->Get_inarea()))
	{
		if(target==-1)
		{
			target=onewhotiggeredme->Get_id();
		}


		if(target!=-1) //if you dont have taget aredy
		{
if (!targetinrange(onewhotiggeredme))
{

			if(state==1&& onewhotiggeredme->state!=3 && Get_destx()==Get_X() &&Get_desty()==Get_Y() )//if idel
			{
				if(is_collision(onewhotiggeredme)==false)
				{
					Set_destx(onewhotiggeredme->Get_X());//rig glich get out of cyinck

					Set_desty(onewhotiggeredme->Get_Y());//rig glich get out of cyinck
				}
				else
				{
					Set_destx(Get_X());//rig glich get out of cyinck

					Set_desty(Get_Y());//rig glich get out of cyinck
				}

			}
}
				
		}

			
	}
}

}
}



void Character::runScript(Unit *onewhotiggeredme,DATAFILE *music)
{
		////////////////////////////////////////// no passing though
	if(onewhotiggeredme->Get_X() < Get_X())
	{
		Set_X(Get_X()+1);

	}
	else
	{
		Set_X(Get_X()-1);
	}

	Set_destx(Get_X());
	if(onewhotiggeredme->Get_Y() < Get_Y())
	{
		Set_Y(Get_Y()+1);

	}
	else
	{
				
		Set_Y(Get_Y()-1);
	}
	Set_desty(Get_Y());
	////////////////////////////////////////



	if(attributess.attackstamana >= 100)/// do we have enough stamna
	{	
		if(state!=3 && onewhotiggeredme->state !=3 && (onewhotiggeredme->type==1 && type==1))//bouth or one dead
		{			
			if(state == 1)
			{

				//attack((Character*)onewhotiggeredme ,music);
			}
		}
	}
	else
	{
		/*	
		if(onewhotiggeredme->Get_X() < Get_X())
		{
				Set_destx(onewhotiggeredme->Get_X()+50);

		}
		else
		{
			Set_destx(onewhotiggeredme->Get_X()-50);
		}


		if(onewhotiggeredme->Get_Y() < Get_Y())
		{
			Set_desty( onewhotiggeredme->Get_Y()+50);//rig

		}
		else
		{
				
			Set_desty( onewhotiggeredme->Get_Y()-50);//rig
		}
		*/



	}

	if(Get_socketid()==-1)
	{

		
	}
			
}
void Character::animate()// need do animation as 2darry headarry bodyarray armarray for each anmation
{
	//need to put min and max frame in charitor class
	//that way there will be only one if stament do incrementing
	if(connte == frspeed)
	{
		connte=0;
		switch(state)
		{
			case 1://idel
			
			Set_W(32);
			Set_H(32);
			Set_sourcebitmap( 4);
			source_X = direction;
			if (Get_source_Y() < 1)
			{
				//do idel animation
			//	Set_source_Y(Get_source_Y() + 1);

			}
			else if(Get_source_Y() >= 1)//max frame
			{
				Set_source_Y(0); // min frame need to make dinamic 
			}
			//states = state;
			break;
			case 2: //walking
			frspeed =10;///glitch with animate when set to 1
			Set_W(32);
			Set_H(32);
			Set_sourcebitmap( 4);
			source_X = 4 + direction;
			
			if (Get_source_Y() < 5)// remeber frames start at 0
			{
				//do walk animation
				Set_source_Y(Get_source_Y() + 1);

			}
			else //if(Get_source_Y() >= 6)//max frame
			{
				Set_source_Y(0); // min frame need to make dinamic 
			}
			//states = state;
			break;
			case 3: //dead
			
			Set_W(32);
			Set_H(32);
			Set_sourcebitmap( 4);
			source_X = 0 ;//+ direction;
			Set_source_Y(1);
			
			//if (Get_source_Y() < 5)// remeber frames start at 0
			//{
				//do walk animation
			//	Set_source_Y(Get_source_Y() + 1);

			//}
			//else //if(Get_source_Y() >= 6)//max frame
			//{
			//	Set_source_Y(0); // min frame need to make dinamic 
			//}
			//states = state;
			break;
			case 4: //milly
			frspeed =7;///glitch with animate when set to 1
			Set_W(32);
			Set_H(32);
			Set_sourcebitmap( 4);
			source_X = 8+ direction;
			if (Get_source_Y() < 5)// remeber frames start at 0
			{
				//do milli animation
				Set_source_Y(Get_source_Y() + 1);

			}
			else //if(Get_source_Y() >= 6)//max frame
			{
				Set_source_Y(0); // min frame need to make dinamic 
				state = 1;
			}
			//states = state;
			break;
		}

	}
	else
	{
		connte++;
	}
}

void Character::do_moveing(Layer* map,View v)
{

	if(state!=3)//if not dead
	{

		/*
		if (!Astar->ReachedGoal() && (destx==x) && (desty==y))
		{
         	Astar->PathNextNode();
		}
		//// i changed all 8's to 1's
		*/

		///animation seed is walk_count
		//character::walk_count=1;
		/*
		if (ABS( x -character::pointx)<8 && ABS( y -character::pointy)<8)
		{
			character::	walking = false;
		}
		*/
	
	
		
		
		/*
		if (ABS(destx- x )<p)
		{
			p=ABS(destx- x );
		}
		*/


	
		if (destx > x ) 
		{
			direction = 1;//RIGHT;
			if (hit_wall( 1,map,v)==false)
			{
				if(attributess.movmentRate > (destx - x)  )
				{
					x +=(destx - x);
				}
				else
				{
					x +=attributess.movmentRate;
				}
			}
			else
			{
				x -=attributess.movmentRate;
				Set_destx(x);
				Set_desty(y);
			}
		}
	
		if(destx < x ) 
		{
			direction = 3;//LEFT;
			if(hit_wall( 2 ,map,v)==false)
			{

				if(attributess.movmentRate > ( x - destx)  )
				{
					x -=(destx - x);
				}
				else
				{
					x -=attributess.movmentRate;
				}
			}
			else
			{
				x +=attributess.movmentRate;
				Set_destx(x);
				Set_desty(y);
			}

		}
	
		/*
		if (ABS(desty- y )<p)
		{
			p=ABS(desty- y );
		}
		*/
	
		if (desty > y ) 
		{
			direction = 2;//DOWN;
			if (hit_wall( 3,map,v)==false)
			{
				if(attributess.movmentRate > (desty - y) )
				{
					y +=(desty - y);
				}
				else
				{
					y +=attributess.movmentRate;
				}
			}
			else
			{
				y -=attributess.movmentRate;
				Set_destx(x);
				Set_desty(y);
			}
		}
		if(desty < y) 
		{
			direction = 0;//UP;
			if(hit_wall( 4,map,v)==false)
			{

				if(attributess.movmentRate > ( y - desty)  )
				{
					y -=(desty - y);
				}
				else
				{
					y -=attributess.movmentRate;
				}
			}
			else
			{
				y +=attributess.movmentRate;
				Set_destx(x);
				Set_desty(y);
			}
		}
	

		//this take care of the fog of war movement
		//set_trans_blender(45, 45, 45, 100);
		//makecol(45,45,45)
		//makeacol(255,0,255,50)
		//floodfill(world, x /32, y /32,2);
		//circlefill(world, x /32, y /32,7,1);
		// our world bitmap tells us which tile to blit
		// 0 is a black tile
		// 1 is a darkenened tile
		// 2 is normal
	}	//state = 2;

}

void Character::talk(BITMAP* n,View v)
{
 

	//add a conter for life of message then ereas dieolog string
	// need to atach a txt raper for the hieght of the rect
	if (dieolog.length()> 0) //we have to make this if statement say if on screen too
	{


		if(msglife == 200)
		{
			char *ansistr = (char *)dieolog.c_str();
			int lenA = lstrlenA(ansistr);
			int lenW;
			BSTR unicodestr;

			lenW = ::MultiByteToWideChar(CP_ACP, 0, ansistr, lenA, 0, 0);
			if(lenW > 0)
			{
				// Check whether conversion was successful
				unicodestr = ::SysAllocStringLen(0, lenW);
				::MultiByteToWideChar(CP_ACP, 0, ansistr, lenA, unicodestr, lenW);
			}
			else
			{
				// handle the error
			}

			// when done, free the BSTR
			::SysFreeString(unicodestr);

			// Initialize COM
			CoInitialize ( NULL );

			// Create the voice interface object
			CoCreateInstance ( CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&Voice );

			// Our text to be spoken
			//WCHAR* TextBuffer = "Hello World";
			//int Long;
			//Long = ISpeechAudioStatus.NonBlockingIO ;
			// Use our voice interface to speak the contents of the buffer
			Voice -> Speak ( unicodestr,  0  , NULL );

			// Shutdown the voice
			if ( Voice != NULL ) Voice -> Release (); Voice = NULL;

			// Shutdown COM
			CoUninitialize ();




			dieolog="";
		}
		else
		{
			msglife++;
		}
		//max chars per line = 30
		if(dieolog.length()<=30)
		{
			//void drawRectangle(BITMAP *bmp,int x, int y, int w,int h,int color, int style, int bgcolor)
			drawRectangle(n,45+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h-10,text_length(font,dieolog.c_str())+10,text_height(font)+20,makecol(0,0,0),2,makeacol(255,0,255,100));
			textout(n,font,dieolog.c_str(),50+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h,makecol(255,255,255));//makecol(255,0,255)is trans
	
		}
		else if(dieolog.length()>30)
		{
			drawRectangle(n,45+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h-10,text_length(font,dieolog.substr(0,30).c_str())+10,text_height(font)+20*2,makecol(0,0,0),2,makeacol(255,0,255,100));
			textout(n,font,dieolog.substr(0,30).c_str(),50+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h,makecol(255,255,255));//makecol(255,0,255)is trans
			textout(n,font,dieolog.substr(31,dieolog.length()).c_str(),50+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,(10+y)-v.get_screen_pos_y()*32-v.get_scroll_y()-h,makecol(255,255,255));//makecol(255,0,255)is trans
		}
	}
}


void Character::showlife(BITMAP* n,View v,DATAFILE *data)
{
	if( damagetaken > -1)
	{

		textprintf(n, (FONT*)data[FONT1].dat, 15+x-v.get_screen_pos_x()*32-v.get_scroll_x()-w, y-v.get_screen_pos_y()*32-v.get_scroll_y()-h-20, makecol(255, 0, 0),"- %d" ,damagetaken);
		if(dmcon==80)
		{

			damagetaken = -1;
			dmcon=0;
		}
		if(dmcon<80)
		{
			if(predamagetaken!=damagetaken)
			{
				predamagetaken=damagetaken;
				dmcon=0;
			}

			dmcon++;

		}
		//Sleep(787);
	}

	if(mouse_x >= x-v.get_screen_pos_x()*32-v.get_scroll_x()-w && mouse_y >= y-v.get_screen_pos_y()*32-v.get_scroll_y()-h)
	{
		if(mouse_x <= x-v.get_screen_pos_x()*32-v.get_scroll_x()+w && mouse_y <= y-v.get_screen_pos_y()*32-v.get_scroll_y()+h)
		{
			//void draw_bar(BITMAP *buf, int x, int y, int w, int h, int value, int range, int backcol, int startcol, int endcol)
			if(debugmode==true)
			{
				textout(n,font,attributess.Get_statholder().c_str(),10,400,makecol(255,255,255));

			}
			//textout(n,font,NumberToString(Get_id()).c_str(),10,400,makecol(255,255,255));
			draw_bar2(n, x-v.get_screen_pos_x()*32-v.get_scroll_x()-w, 10+y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, 100, 2, attributess.Gethp_bar(), 100,makecol(255,0,0), makecol(0,255,0),  makecol(0,0,255));
			draw_bar2(n, x-v.get_screen_pos_x()*32-v.get_scroll_x()-w, 13+y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, 100, 2, attributess.Getmp_bar(), 100,makecol(255,0,0), makecol(0,0,255),  makecol(0,0,255));
			draw_bar2(n, x-v.get_screen_pos_x()*32-v.get_scroll_x()-w, 15+y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, 100, 2, attributess.Getattackstamana_bar(), 100,makecol(255,0,0), makecol(255,255,255),  makecol(0,0,255));
		
			if(mouse_b & 1 && clicked==false)
			{
				clicked= true;
				if(name=="none")
				{
					if(unitpickedleft > 0)
					{
						unitpickedleft--;
						unitpicked=Get_id();

				
				
					}
				}
			}
			else if(!(mouse_b &2)&&!(mouse_b &1))
			{
				clicked=false;
			}
		
			//targget= id;

		}	

	}

}

int Character::attack(Character *victim,DATAFILE *music)
{
	double load =((double)attributess.Getattack_power()/victim->attributess.Getdefense());
	///victim.attributess.Sethp(10);
	state = 4;
	if( load >1)
	{
		load = 1;
	}
	if( input(0,100) <= attributess.crtprocent)
	{
		//play_sound(SAMPLE_HIT);
		victim->damagetaken =(int)(((int)((input(attributess.Getminattack_power(),attributess.Getattack_power())+ attributess.Getbonceattack_power())*attributess.crt))*load);
	
	}
	else
	{

		victim->damagetaken =(int)((input(attributess.Getminattack_power(),attributess.Getattack_power())+ attributess.Getbonceattack_power())*load);
	}

	//victim->damagetaken =(input(attributess.Getminattack_power(),attributess.Getattack_power())+ attributess.Getbonceattack_power())*load;
	//victim->Set_X(victim->Get_X()+100);//knocked back
	//victim->Set_destx(victim->Get_X());// so they dont walk back to spot
	//victim.Set_Y(victim.Get_Y()+100);//knocked back
	//	basic_message1("You were killed","Game Over","Ok");
	victim->attributess.Sethp(victim->attributess.Gethp()-victim->damagetaken);
	play_sound(SAMPLE_HIT,music);
	//textout(screen,font,NumberToString(attributess.Getattack_power()).c_str(),100,410,makecol(6,255,255));
	//textout(screen,font,NumberToString(attributess.Getbonceattack_power()).c_str(),200,410,makecol(6,255,255));
	//textout(screen,font,NumberToString(victim->damagetaken).c_str(),300,410,makecol(6,255,255));
	//if(socketid==-1)
	//{
	//Sleep(9999999);
	//}

	attributess.attackstamana = attributess.attackstamana - 100; //attack take 50 stamana points to do
	if(attributess.attackstamana < 0)
	{
		attributess.attackstamana=0;
	}
		//get exp
	if(victim->attributess.Gethp()<=0)
	{
 
		play_sound(SAMPLE_MDIE,music);
		attributess.Setexp(attributess.Getexp()+victim->attributess.Getlevel_max());
			attributess.Setgold(attributess.Getgold()+ (victim->attributess.Getgold()*attributess.Getgoldpercent()));
			//victim->attributess.Setgold(victim->attributess.Getgold()- (victim->attributess.Getgold()*attributess.Getgoldpercent()));

		//check to see if to levle up
		if(attributess.Getexp()>=101) //exp max = 100
		{
			attributess.Setexp(1);
			attributess.levelup();
			play_sound(SAMPLE_LEVELUP,music);
		}
	}


	return 0;
}

int Character::die()
{
	return 0;
}
