
#ifndef TRIGGER_H
#define TRIGGER_H

#include "character.h"
#include "UnitList.h"
class Trigger:public Unit
{
	//data members
	protected:
	public:

		string race;
		string dieolog;
		gender sex;
		
		
		//int	msglife;
		int destx, desty ;
	public:
		int damagetaken;
		
		int	msglife;

		//gets
		string Get_dieolog();
		int Get_state();
		int Get_destx();
		int Get_desty();
		string Get_unit();
		string Get_race();
		gender Get_sex();
		bool sp;
		
		//sets
		Character *listofUnit;

		int spuntime;
		int spuntime_max;
		void Set_dieolog(string);
		void Set_state(int);
		void Set_destx(int);
		void Set_desty(int);
		void Set_unit(string );
		void set_mesageLife(int );
		void Set_race(string );
		void Set_sex(gender);
		void animate();
		void talk(BITMAP* ,View );
		void runStatScript();
		void runRangeScript(Unit *,DATAFILE *);
		void do_moveing(Layer* map,View v);
		void showlife(BITMAP* ,View ,DATAFILE *);
		//void battel();
		int attack(Character*);
		int die();
		void SendStats();
int Get_scriptId();
int get_type();
		
		
//void set_onewhotiggeredme(Unit k){onewhotiggeredme=&k;};
//Unit *get_onewhotiggeredme(){return &onewhotiggeredme;};//UnitObject;
		 Statss *get_attributess(){return attributess;} ;
string trigger;
		
	Trigger();
	void Set_scriptId(int);
	
	void runScript(Unit*,DATAFILE *);
void fromString();
string toString();
};




Trigger::Trigger()
{
	sp=false;
listofUnit= new Character();
spunits_max=0;
spunits=0;
	damagetaken = -1;
	attributess =  new Statss();
	msglife=0;
	dieolog="";
	race = "none";
	trigger = "none";
	sex = none;
	state = 1;
scriptId = -1;
triggered = false;
type=2;

 spuntime=0;
	 spuntime_max=30;
}
void Trigger::Set_scriptId(int s)
{
	scriptId=s; 
}




void Trigger::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 (trigger[i]!= '`') //stay in untl you find the ` at the end
	{
		if (trigger[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += trigger[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
scriptId = StringToNumber(temps[19]);// 
}



string Trigger::toString()
{
	
		trigger = 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) +"," + NumberToString(scriptId) +"`";
textout(screen,font,trigger.c_str(),100,50*id,makecol(255,255,255));
//Sleep(10000);
return trigger;
}



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

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

string Trigger::Get_dieolog()
{

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

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

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

	return state;
}


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

void Trigger::Set_race(string New_race)
{
	race =  New_race ;
}
void Trigger::Set_sex(gender New_sex)
{
	sex = New_sex;
}

	int Trigger::Get_scriptId()
	{
		return scriptId;
	}

void Trigger::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
			frspeed =10;///glitch with animate when set to 1
			Set_W(32);
			Set_H(32);
			Set_sourcebitmap( 4);
			source_X = 17;
			if (Get_source_Y() < 5)
			{
				//do idel animation
				Set_source_Y(Get_source_Y() + 1);

			}
			else //if(Get_source_Y() < 5)//max frame
			{
				Set_source_Y(0); // min frame need to make dinamic 
			}
			//states = state;
		break;
		case 2: //walking
			
			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;
	}

}
else
{
connte++;
}
}

void Trigger::do_moveing(Layer* map,View v)
{
/*
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)
	{
			x +=attributess->movmentRate;
		}
		}
	
		if (destx < x ) 
		{
			direction = 3;//LEFT;
if (hit_wall( 2 ,map,v)==false)
	{
			x -=attributess->movmentRate;
		}

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

		//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 Trigger::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)
		{
			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 Trigger::showlife(BITMAP* n,View v,DATAFILE *data)
{
	if( damagetaken > -1)
	{
textprintf(n, font, 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-5, makecol(255, 50, 50),"- %d" ,damagetaken);
	damagetaken = -1;
	}

	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));
}		
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->Getattackstamana_bar(), 100,makecol(255,0,0), makecol(255,255,255),  makecol(0,0,255));
	
//targget= id;

		}	

	}

}




void Trigger::runStatScript()
{



	if (computerId == 0)
	{
		//if(sp==true)//if(spawunit==true)
		//{

			//sp=false;//spawunit=false;
			//while(spunits < tospawunit)
			//{

if (scriptId==7)
{
					if(spunits < spunits_max)
					{
						netId++;

						listofUnit->Set_id(netId);
						listofUnit->Set_name("nim");

						listofUnit->Set_X(input(10,100)+Get_X());
						listofUnit->Set_Y(input(20,100)+Get_Y());
						listofUnit->owner_id = Get_id();

						//	listofUnit->Set_X(listofUnit->Get_X()+45);
						//	if(listofUnit->Get_X()>Get_X())
						//	{
						//		listofUnit->Set_X(530);
						//		listofUnit->Set_Y(listofUnit->Get_Y()+50);
						//	}
					inboxbuffer += "0|"+NumberToString(computerId)+"|001|"+listofUnit->toString()+"~";//mite need to send stats here
					spunits++;
					}
}
			//}
		//}
	}
	if(attributess->Gethp()<1) //check to see it dead
	{

		Set_inarea(1);
		if(computerId==Get_id())
		{
			ch_current_area = 1;//rig
		}
	}



}

void Trigger::runRangeScript(Unit *onewhotiggeredme,DATAFILE *music)
{
	
   
  



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




				if(triggered== true)
				{

					//if(attributess->attackstamana >= 50) //each telaport takes 50
					//{
						//attributess->attackstamana = attributess->attackstamana - 50; //attack take 50 stamana points to do
						//onewhotiggeredme->Set_inarea(2);
						//if(attributess->attackstamana < 0)
						//{
							//attributess->attackstamana=0;
						//}

					switch(scriptId)
					{
						case 1:
						//do this
						onewhotiggeredme->Set_inarea(1);
						if(charnetId==onewhotiggeredme->Get_id())
						{
							play_sound(SAMPLE_TELLY,music);
							ch_current_area = 1;//rig
							stop_song();
							play_song(MIDI_SPIKKO,music);

						}
						break;
						case 2:
						//do this
						onewhotiggeredme->Set_inarea(2);
						if(charnetId==onewhotiggeredme->Get_id())
						{
							play_sound(SAMPLE_TELLY,music);
							ch_current_area = 2;//rig
							stop_song();
							play_song(MIDI_BATTLE,music);
						}
						break;
						case 3:
						//do this
						onewhotiggeredme->Set_inarea(3);
						if(charnetId==onewhotiggeredme->Get_id())
						{
							play_sound(SAMPLE_TELLY,music);
							ch_current_area = 3;//rig
							stop_song();
							play_song(MIDI_FROG,music);
						}

						break;
						case 4:
						//do this
						onewhotiggeredme->Set_inarea(4);
						if(charnetId==onewhotiggeredme->Get_id())
						{
							play_sound(SAMPLE_TELLY,music);
							ch_current_area = 4;//rig
							stop_song();
							play_song(MIDI_GOTA,music);
						}
						break;
						case 5:
						//do this
						onewhotiggeredme->Set_inarea(2);
						play_sound(SAMPLE_MAGIC,music);
						nimitems--;
						if(charnetId==onewhotiggeredme->Get_id())
						{

							//play_sound(SAMPLE_MAGIC,music);
							//ch_current_area = 2;//rig
							//stop_song();
							//play_song(MIDI_GOTA,music);
						}
						break;
						case 6:
						//do this
						onewhotiggeredme->Set_inarea(2);
						play_sound(SAMPLE_MAGIC,music);
						nimitems--;
						if(charnetId==onewhotiggeredme->Get_id())
						{
							//play_sound(SAMPLE_MAGIC,music);
							//ch_current_area = 2;//rig
							//	stop_song();
							//	play_song(MIDI_GOTA,music);
						}
						break;
						case 7:
						//do this

						//onewhotiggeredme->Set_inarea(2);

						if(onesecpased==true)
						{
							spuntime++;

							if( spuntime==spuntime_max)
							{
								sp=true;
								spuntime=0;
								spunits_max=3;
							}

							//spawunit=true;

						}
						if(computerId==onewhotiggeredme->Get_id())
						{
							//play_sound(SAMPLE_MAGIC,music);
							//	ch_current_area = 4;//rig
							//stop_song();
							//play_song(MIDI_GOTA,music);


						}
						break;
						case 8:
						//do this
						onewhotiggeredme->Set_layer(3);
						if(charnetId==onewhotiggeredme->Get_id())
						{
							//play_sound(SAMPLE_TELLY,music);
						//	ch_current_area = 4;//rig
							//stop_song();
							//play_song(MIDI_GOTA,music);
						}
						break;
						case 9:
						//do this
						onewhotiggeredme->Set_inarea(5);
						onewhotiggeredme->Set_X(300);
						onewhotiggeredme->Set_Y(400);
						onewhotiggeredme->Set_destx(300);
						onewhotiggeredme->Set_desty(400);

						if(charnetId==onewhotiggeredme->Get_id())
						{

							play_sound(SAMPLE_TELLY,music);
							ch_current_area = 5;//rig
							stop_song();
							//play_song(MIDI_GOTA,music);
						}
						break;

					}	
		

				//}
				
				}
			}

		}
		else
		{
		
		}

		if(Get_socketid()==-1)
		{

		
		}
   
			
}


void Trigger::runScript(Unit *onewhotiggeredme,DATAFILE *music)
{


	

}

#endif