#ifndef UNITLIST_H
#define UNITLIST_H
#define maxchars 50
#include "Trigger.h"
//#include "Projectile.h"
#include <STRING>
#include <fstream>
#include <iostream>

 using namespace std;
class ItemList;
#define maxUnits 50

void swap( Unit& item1, Unit& item2 )
{
	
 Unit temp;
 temp = item1;
 item1 = item2;
 item2 = temp;

 
}

int partition( Unit* arr[], int first, int last )
{
// cout << "partition called on (" << first << ", " << last << " )" << endl;
 int pivot = arr[first]->Get_Y();
 int i = first - 1;
 int j = last + 1;
 
 while( i < j )
 {
  do
  {
   j = j - 1;
  } while( arr[j]->Get_Y() > pivot );

  do
  {
   i = i + 1;
  } while( arr[i]->Get_Y()< pivot );

  if ( i < j )
	  swap( arr[i], arr[j] );
 }
 return j;
}

void quickSort( Unit* arr[], int first, int last )
{
	//we want mege sort instean for nlogn every time
if(debugmode==true)
{
	//textout(screen,font,"quickSort called ",100,220,makecol(6,255,255));
}
// cout << "quickSort called on (" << first << ", " << last << " )" << endl;
  //displayArray( arr, number_of_items );
	if ( first < last )
 {
  int part = partition( arr, first, last );
  quickSort( arr, first, part );
  quickSort( arr, part + 1, last );
 }
 // else do nothing; array of 1 element is sorted
	if(debugmode==true)
	{
		//textout(screen,font,"quickSort done ",100,250,makecol(255,6,255));
	}
}



class UnitList  
{
	private:
		//Unit* head;
		
		int numItems ;
	public:
Unit* listofUnits[maxUnits];
		UnitList();
		//insert at beginning
		int findUnitTpye(string);
		void load(string);
		void save(string );
		void traverse2( );
		void draw( BITMAP*,int,View,DATAFILE *,Layer*);
		bool isEmpty( );
		int GetnumItems( );
		bool IsInList( int );
		int IndexOf( int );
		bool IsInList2( int );
		void insert(string); 
		void setdest();// change to bool
		void setdieolog();
		bool Delete(int d);
		bool Deletec(int d);
		bool doTrade(int ,int,ItemList*);
		int battle(string,DATAFILE *);
		bool upDateStats(string);
		bool upDateTradeInfo(string);
		bool startTrade(string);

		int Find_Unit_id( int d,int c );
		void setup_collision(DATAFILE *);
		void sort();
		string listToString(string);
	Unit *Get_Unit( int d );
	Unit *Get_Unit( string d );
	Unit *Get_Unit( string,int*,int  );
	Unit *Get_Unit( string,int  );
		virtual ~UnitList();
bool upDateUnitItems(string strings);
};


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UnitList::UnitList()
{

	numItems = 0;
}
int UnitList::findUnitTpye(string uit)
{
int j= 0;
	int i = 0;
	//maybey i acn make the conver for sring to char* right in here
	string  temps[2]; //arry to hold data
	
	while (uit[i]!= ',')//'`') //stay in untl you fing the ` at the end riged to find first comma we just want the type
	{
		if (uit[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += uit[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}

return StringToNumber(temps[0]);
}

bool UnitList::upDateUnitItems(string strings)
{
		
	int j= 0;
	int i = 0;
int comcont= 0;
	//maybey i acn make the conver for sring to char* right in here
	string  temps[41]; //arry to hold data
	
	while (strings[i]!= '`') //stay in untl you fing the ` at the end riged to find first comma we just want the id
	{
		if (strings[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += strings[i]; //add char to temp spot
			i++;
		}
		else
		{
if(comcont!=1)
{
			i++;
			j++;
comcont= 1;
}
else
{
temps[j] += strings[i];
i++;
comcont= 1;

}
		}

	}

	///////////////////////////////////////////////////////////////
	for(i=0; i<numItems;i++)
	{
		if( listofUnits[i]->Get_id() == StringToNumber(temps[0]))//Id
		{

		
			//textout(screen,font,temps[1].c_str(),1,350,makecol(255,255,255));

					
			((Character*)listofUnits[i])->itembag.unitItems=temps[1]+'`';

			//textout(screen,font,((Character*)listofUnits[i])->get_attributess()->Get_statholder().c_str(),1,310,makecol(255,0,255));
			//Sleep(99990);	
			((Character*)listofUnits[i])->itembag.fromString();

			//textout(screen,font,"fixed",100,400,makecol(255,0,255));
			//Sleep(7877);

		//nd->UnitObject.Set_desty(StringToNumber(temps[2]));//desty
		//nd->UnitObject.Set_inarea(StringToNumber(temps[3])) //inarea
		}

	}
	/////////////////////////////////////////////////////////////
	
	return 1;
}

bool UnitList::doTrade(int id1,int id2,ItemList *ItemList5)
{

	int i,j;	bool id1_found= false;
	bool id2_found= false;


	for(  i = 0; i < numItems; i++ )
	{
		
		if( listofUnits[i]->Get_id() == id1 )
		{
			
			id1_found = true;
			break;
		}

	}

	for(  j= 0; j < numItems; j++ )
	{
		
		if( listofUnits[j]->Get_id() ==id2 )
		{
		
			id2_found = true;
			break;
		}
	}

	if(id1_found==true)
	{
		if(id2_found==true)
		{
			//do swaping

			//gold
			if(((Character*)listofUnits[j])->get_attributess()->goldtotrade>0)
			{
				((Character*)listofUnits[i])->get_attributess()->Setgold(((Character*)listofUnits[i])->get_attributess()->Getgold()+((Character*)listofUnits[j])->get_attributess()->goldtotrade);
				((Character*)listofUnits[j])->get_attributess()->goldtotrade=0;
			}

			if(((Character*)listofUnits[i])->get_attributess()->goldtotrade>0)
			{
				((Character*)listofUnits[j])->get_attributess()->Setgold(((Character*)listofUnits[j])->get_attributess()->Getgold()+((Character*)listofUnits[i])->get_attributess()->goldtotrade);
				((Character*)listofUnits[i])->get_attributess()->goldtotrade=0;
			}

			//items
			for(int r=0;r<((Character*)listofUnits[i])->itembagtotrade.slotsperpage; r++)
			{

				if(((Character*)listofUnits[i])->itembagtotrade.items[r].something_in_slot==true)
				{
					((Character*)listofUnits[j])->itembag.additem(((Character*)listofUnits[i])->itembagtotrade.items[r].itemid,((Character*)listofUnits[i])->itembagtotrade.items[r].nuberofitems);
					((Character*)listofUnits[i])->itembagtotrade.items[r].itemid,((Character*)listofUnits[i])->itembagtotrade.items[r].nuberofitems=0;
				}
				if(((Character*)listofUnits[j])->itembagtotrade.items[r].something_in_slot==true)
				{
					((Character*)listofUnits[i])->itembag.additem(((Character*)listofUnits[j])->itembagtotrade.items[r].itemid,((Character*)listofUnits[j])->itembagtotrade.items[r].nuberofitems);
					((Character*)listofUnits[j])->itembagtotrade.items[r].itemid,((Character*)listofUnits[j])->itembagtotrade.items[r].nuberofitems=0;
				}
			}
			if(((Character*)listofUnits[i])->itembag.Get_Value(ItemList5 )+((Character*)listofUnits[i])->attributess->goldtotrade==((Character*)listofUnits[j])->itembag.Get_Value(ItemList5 )+((Character*)listofUnits[j])->attributess->goldtotrade)
			{

outboxbuffer += "0|0|013|"+ NumberToString(id1_found)+","+((Character*)listofUnits[i])->itembag.toString()+'~';
outboxbuffer += "0|0|013|"+ NumberToString(id2_found) +","+((Character*)listofUnits[j])->itembag.toString()+'~';
			}			
return true;

		}
		else
		{
				
			//cant find id2
			return false;
		}



	}
	else
	{	
		//cant find id1
		return false;
		
	}

		
	return false;

}

bool UnitList::upDateTradeInfo(string strings)
{
	//need to code
		
	int j= 0;
	int i = 0;
	int comcont= 0;
	//maybey i acn make the conver for sring to char* right in here
	string  temps[20]; //arry to hold data
	
	while (strings[i]!= '`') //stay in untl you fing the ` at the end riged to find first comma we just want the id
	{
		if (strings[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += strings[i]; //add char to temp spot
			i++;
		}
		else
		{
			if(comcont!=1)
			{
				i++;
				j++;
				comcont= 1;
			}
			else
			{
				temps[j] += strings[i];
				i++;
				comcont= 1;

			}
		}

	}

	///////////////////////////////////////////////////////////////
	for(i=0; i<numItems;i++)
	{
		if( listofUnits[i]->Get_id() == StringToNumber(temps[0]))//Id
		{

		break;
			
		}

	}


for(j=0; j<numItems;j++)
	{
		if( listofUnits[j]->Get_id() == StringToNumber(temps[1]))//Id
		{

		
		break;
		}

	}
//((Character*)listofUnits[i])->itembag.unitItems=;
//((Character*)listofUnits[i])->itembagtotrade.unitItems=;
//((Character*)listofUnits[i])->itembag.fromString();
//((Character*)listofUnits[i])->itembagtotrade.fromString();
//((Character*)listofUnits[i])->get_attributess()->Setgold();
//((Character*)listofUnits[i])->get_attributess()->goldtotrade;

//((Character*)listofUnits[j])->itembag.unitItems=;
//((Character*)listofUnits[j])->itembagtotrade.unitItems=;
//((Character*)listofUnits[j])->itembag.fromString();
//((Character*)listofUnits[j])->itembagtotrade.fromString();

//((Character*)listofUnits[j])->get_attributess()->Setgold();
//((Character*)listofUnits[j])->get_attributess()->goldtotrade;
	/////////////////////////////////////////////////////////////

return 1;
}


bool UnitList::upDateStats(string strings)
{
		
	int j= 0;
	int i = 0;
	int comcont= 0;
	//maybey i acn make the conver for sring to char* right in here
	string  temps[2]; //arry to hold data
	
	while (strings[i]!= '`') //stay in untl you fing the ` at the end riged to find first comma we just want the id
	{
		if (strings[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += strings[i]; //add char to temp spot
			i++;
		}
		else
		{
			if(comcont!=1)
			{
				i++;
				j++;
				comcont= 1;
			}
			else
			{
				temps[j] += strings[i];
				i++;
				comcont= 1;

			}
		}

	}

	///////////////////////////////////////////////////////////////
	for(i=0; i<numItems;i++)
	{
		if( listofUnits[i]->Get_id() == StringToNumber(temps[0]))//Id
		{

		
			textout(screen,font,temps[1].c_str(),1,350,makecol(255,255,255));

					
			listofUnits[i]->get_attributess()->Set_statholder(temps[1]+'`');

			//textout(screen,font,((Character*)listofUnits[i])->get_attributess()->Get_statholder().c_str(),1,310,makecol(255,0,255));
			//Sleep(99990);	
			listofUnits[i]->get_attributess()->fromString();

			textout(screen,font,"fixed",100,400,makecol(255,0,255));
			//Sleep(7877);

			//nd->UnitObject.Set_desty(StringToNumber(temps[2]));//desty
			//nd->UnitObject.Set_inarea(StringToNumber(temps[3])) //inarea
		}

	}
	/////////////////////////////////////////////////////////////
	
return 1;
}


void UnitList::insert(string chs )
{
	int typ = findUnitTpye(chs);


	//Unit* nd;
	//	nd = new Unit;




	if(typ == 1)
	{
		textout(screen,font,chs.c_str(),100,400,makecol(255,0,255));
		//Sleep(987700);	 
		//	textout(screen,font,chs.c_str(),100,400,makecol(255,0,255));
		//Sleep(7877);

		listofUnits[numItems] = new Character();

		//nd=(Character*)listofUnits[numItems]   ;
	
		//nd->Set_unit(chs);
		//nd->fromString();
		//if (nd->get_type() == 1)
		//{

		
		((Character*)listofUnits[numItems])->Set_unit(chs);
		((Character*)listofUnits[numItems])->fromString();
	
	
		((Character*)listofUnits[numItems])->Set_destx(((Character*)listofUnits[numItems])->Get_X());
		((Character*)listofUnits[numItems])->Set_desty(((Character*)listofUnits[numItems])->Get_Y());
	}
	else if(typ == 2)
	{
		textout(screen,font,chs.c_str(),100,400,makecol(255,0,255));
		//Sleep(987700);		 
	
		//	textout(screen,font,chs.c_str(),100,400,makecol(255,0,255));
		//Sleep(7877);
		listofUnits[numItems] = new Trigger();

		//nd=(Trigger*)listofUnits[numItems]   ;
	
		//nd->Set_unit(chs);
		//nd->fromString();
		//if (nd->get_type() == 1)
		//{

		
		((Trigger*)listofUnits[numItems])->Set_unit(chs);
		((Trigger*)listofUnits[numItems])->fromString();
	
	
		((Trigger*)listofUnits[numItems])->Set_destx(((Trigger*)listofUnits[numItems])->Get_X());
		((Trigger*)listofUnits[numItems])->Set_desty(((Trigger*)listofUnits[numItems])->Get_Y());

	}
	else if(typ == 3)
	{
		textout(screen,font,chs.c_str(),100,400,makecol(255,0,255));
		//Sleep(987700);		 
	
		//	textout(screen,font,chs.c_str(),100,400,makecol(255,0,255));
		//Sleep(7877);
		listofUnits[numItems] = new Projectile();

		//nd=(Trigger*)listofUnits[numItems]   ;
	
		//nd->Set_unit(chs);
		//nd->fromString();
		//if (nd->get_type() == 1)
		//{

		
		((Projectile*)listofUnits[numItems])->Set_unit(chs);
		((Projectile*)listofUnits[numItems])->fromString();
	
	
		((Projectile*)listofUnits[numItems])->Set_destx(((Projectile*)listofUnits[numItems])->Get_X());
		((Projectile*)listofUnits[numItems])->Set_desty(((Projectile*)listofUnits[numItems])->Get_Y());

	}
	//nd->type =typ;
	///////////////////////////////////////////////////////////////////
	//if (numItems < maxchars)
	//	{
	//listofUnits[numItems]=nd;

	numItems++;	
	//	}
	////////////////////////////////////////////////////////////////////

	
	//}
}

///and load from file
void UnitList::load(string filename )
{

	ifstream inData;
	inData.open(filename.c_str() );
	string namer ;
	char trash ;
	int num = 0;
	int num2 = 0;
		string num4;	 
	inData >> num;
	inData >> trash;	

	while( 	numItems < num)
  	{

		//Unit* nd;
	//	nd = new Unit;

		 
		inData >> num2;
		inData >> trash;
		if (num2 == 1)
		{

		listofUnits[numItems] = new Character();


inData >>num4;
((Character*)listofUnits[numItems])->Set_unit(num4);
((Character*)listofUnits[numItems])->fromString();

	netId ++;
		((Trigger*)listofUnits[numItems])->Set_id(netId);
((Character*)listofUnits[numItems])->Set_destx(((Character*)listofUnits[numItems])->Get_X());
((Character*)listofUnits[numItems])->Set_desty(((Character*)listofUnits[numItems])->Get_Y());
((Character*)listofUnits[numItems])->toString();
	num4="";
inData >> num4;
	/*
	for(char num5;num5!='`';inData >> num5)
{
	num4 +=num5;
}
*/
((Character*)listofUnits[numItems])->attributess->Set_statholder(num4);
		((Character*)listofUnits[numItems])->attributess->fromString();

textout(screen,font,((Character*)listofUnits[numItems])->Get_unit().c_str(),100,400,makecol(255,255,255));
inData >>num2;
if(num2==1)
{
	num4="";
	inData >> num4;

	((Character*)listofUnits[numItems])->itembag.unitItems=num4;
	((Character*)listofUnits[numItems])->itembag.fromString();
}

		//listofUnits[numItems]   = nd;
	
		numItems++;
//textout(screen,font,listofUnits[numItems-1]->Get_unit().c_str(),100,450,makecol(255,0,255));

		}
		else if(num2== 2)//load target
		{



listofUnits[numItems] = new Trigger();
inData >>num4;
((Trigger*)listofUnits[numItems])->Set_unit(num4);
((Trigger*)listofUnits[numItems])->fromString();
//nd=(Trigger*)listofUnits[numItems];

	
			//nd.TriggerObject.//load trigger stuff


	num4="";
inData >> num4;
	/*
	for(char num5;num5!='`';inData >> num5)
{
	num4 +=num5;
}
*/
((Trigger*)listofUnits[numItems])->attributess->Set_statholder(num4);
		((Trigger*)listofUnits[numItems])->attributess->fromString();
	netId ++;
		((Trigger*)listofUnits[numItems])->Set_id(netId);
((Trigger*)listofUnits[numItems])->Set_destx(((Trigger*)listofUnits[numItems])->Get_X());
((Trigger*)listofUnits[numItems])->Set_desty(((Trigger*)listofUnits[numItems])->Get_Y());
((Trigger*)listofUnits[numItems])->toString();

textout(screen,font,((Trigger*)listofUnits[numItems])->Get_unit().c_str(),10,450,makecol(255,255,255));
//Sleep(987700);



	//listofUnits[numItems]= nd;
			numItems++;

		}
	
	}

//

	inData.close();

	//Sleep(9000);

}


bool UnitList::startTrade(string newstring)
{

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

	}
	//temp[0]==Id of unit you want to trade with
	//temp[1]==Id of unit that started trade
	if(Get_Unit(StringToNumber(temps[0]))->trading==false)
	{
		//send yes /no messas if not an NPC
		if(Get_Unit(StringToNumber(temps[0]))->Get_socketid()!=-1)
		{
			outboxbuffer += "0|0|014|"+ newstring +",1"+'~'; //one means menu obout trade
		}
		else
		{
			//return yes from the yes no menu
			outboxbuffer += "0|0|015|"+ newstring +",1,1"+'~'; //2nd one mean yes

		}

	}
	else
	{
			//send diolog saying im bizzy
		outboxbuffer +="9999|0|003|" + temps[0] + ", dude im am bizzy`~";
									
	}


return false;
}



	



///and save to file
void UnitList::save( string filename)
{
	Unit* nd;
//	nd = head;
	
	ofstream outData;
	outData.open(filename.c_str());
	
	outData << numItems;
	outData << ",";
	outData << endl;
	
	//while( nd != NULL ) 
	//{
	for(int i=0; i<numItems;i++)
	{
		nd=listofUnits[i];
		outData << nd->get_type();
		outData << ",";
		outData << endl;
		outData << nd->toString();
		outData << endl;
		
		
/*
		if(nd->get_type()==2)
		{
			outData << ",";
			outData <<((Trigger*)nd)->Get_scriptId();
			
		}

		outData << endl;*/
		outData << nd->attributess->toString();
		outData << endl;
		outData <<"1";
		outData << endl;
		outData << ((Character*)nd)->itembag.toString();
	}
	
	
	outData.close();
	
}
////


void UnitList::setup_collision(DATAFILE *music)
{
	if(debugmode==true)
	{
		//textout(screen,font,"setup_collision called ",100,320,makecol(6,255,255));
	}
	
	int i,j;
	
	for(  i = 0; i < numItems; i++ )
	{
	 
		for(  j = 0; j < numItems; j++ )
		{
			if(listofUnits[i]->Get_inarea()==listofUnits[j]->Get_inarea())// are both objects in same area
			{
				if(listofUnits[i]->Get_id() != listofUnits[j]->Get_id()) // make sure object both are not the same
				{
					if(listofUnits[i]->type ==1&&listofUnits[j]->type ==1) // check to see if they are both charitor objcets
					{


						//if(listofUnits[i]->Get_socketid()!=listofUnits[j]->Get_socketid()) // check to see if the are both computer controled objects
						//{
							if(listofUnits[i]->targetinsite( listofUnits[j]))
							{
								
								((Character*)listofUnits[i])->runSiteScript(((Character*)listofUnits[j]),music);

							}
							if(listofUnits[i]->targetinrange( listofUnits[j]))
							{
								((Character*)listofUnits[i])->runRangeScript(((Character*)listofUnits[j]),music);
							}

						//}
					}
					else if(listofUnits[i]->type ==2)
					{
						if(listofUnits[i]->targetinrange( listofUnits[j]))
						{
							listofUnits[i]->triggered=true;
							((Trigger*)listofUnits[i])->runRangeScript(((Character*)listofUnits[j]),music);
							listofUnits[i]->triggered=false;
						}				
					}
					else if(listofUnits[i]->type ==3)
					{
						if(listofUnits[i]->targetinrange( listofUnits[j]))
						{
							listofUnits[i]->triggered=true;
							((Projectile*)listofUnits[i])->runRangeScript((listofUnits[j]),music);
							listofUnits[i]->triggered=false;
						}				
					}




				}
				
				if(listofUnits[i]->is_collision(listofUnits[j]) )
				{
					
					if(listofUnits[i]->Get_id() != listofUnits[j]->Get_id())
					{
					
						textout(screen,font," collision detected ",400,320,makecol(6,255,255));

						listofUnits[i]->Set_collision(true);
						listofUnits[j]->Set_collision(true);/// this has the sam bug from project 3 setting point useable

						
						if(listofUnits[j]->Get_id()!=computerId)
						{
							targget=listofUnits[j]->Get_id();
						}
						else
						{
							targget=listofUnits[i]->Get_id();
						}

						if(listofUnits[i]->type ==1)
						{


							listofUnits[i]->triggered=true;
											
							((Character*)listofUnits[i])->runScript(((Character*)listofUnits[j]),music);
							listofUnits[i]->triggered=false;

							if(listofUnits[i]->type ==1&&listofUnits[j]->type ==1)
							{



								if(listofUnits[i]->Get_X() < listofUnits[j]->Get_X())
								{
									////////	listofUnits[i]->Set_X(listofUnits[i]->Get_X() -1);
									//listofUnits[j]->Set_X(listofUnits[j]->Get_X() +1);

								}
								else if(listofUnits[i]->Get_X() > listofUnits[j]->Get_X())
								{
									////////////	listofUnits[i]->Set_X(listofUnits[i]->Get_X()+ 1);
									//	listofUnits[j]->Set_X(listofUnits[j]->Get_X()-1);

								}


								if(listofUnits[i]->Get_Y() < listofUnits[j]->Get_Y())
								{
									/////////////	listofUnits[i]->Set_Y(listofUnits[i]->Get_Y() -1);
									//	listofUnits[j]->Set_Y(listofUnits[j]->Get_Y() +1);

								}
								else if(listofUnits[i]->Get_Y() > listofUnits[j]->Get_Y())
								{
									//////////	listofUnits[i]->Set_Y(listofUnits[i]->Get_Y()+ 1);
									//	listofUnits[j]->Set_Y(listofUnits[j]->Get_Y() -1);
								}
								if(listofUnits[i]->Get_name()!="none" &&listofUnits[j]->Get_name()!="none")
								{
									//////////	listofUnits[i]->Set_destx(listofUnits[i]->Get_X());
									//listofUnits[j]->Set_destx(listofUnits[j]->Get_X());
									/////////////	listofUnits[i]->Set_desty(listofUnits[i]->Get_Y());
									//	listofUnits[j]->Set_desty(listofUnits[j]->Get_Y());
								}

							}

						}
						if(listofUnits[i]->type ==2)
						{
							

							((Trigger*)listofUnits[i])->runScript(((Character*)listofUnits[j]),music);
							
							
							
							//listofUnits[i]->set_onewhotiggeredme(*(listofUnits[j]));
							/*
							if(listofUnits[i]->scriptId==1)
							{
								listofUnits[j]->Set_inarea(1);//rig
								if(computerId==listofUnits[j]->Get_id())
								{
									ch_current_area = 1;//rig
								}
							}	
							else if(listofUnits[i]->scriptId==2)
							{
								listofUnits[j]->Set_inarea(2);//rig
								if(computerId==listofUnits[j]->Get_id())
								{
									ch_current_area = 2;//rig
								}
							}
							else if(listofUnits[i]->scriptId==3)
							{
								listofUnits[j]->Set_inarea(3);//rig
								if(computerId==listofUnits[j]->Get_id())
								{
									ch_current_area = 3;//rig
								}
							}
							else if(listofUnits[i]->scriptId==4)
							{
								listofUnits[j]->Set_inarea(4);//rig
								if(computerId==listofUnits[j]->Get_id())
								{
									ch_current_area = 4;//rig
								}
							}
							*/

				
							//	listofUnits[i]->runScript();
							

							//	listofUnits[i]->set_onewhotiggeredme(NULL);
						}
						break;//list of ele
					}
					else
					{
						listofUnits[i]->Set_collision(false);
			
					}
				}
				else
				{
					listofUnits[i]->Set_collision(false);

				}
			}
		
		}	
	
	}
	if(debugmode==true)
	{
		//textout(screen,font,"setup_collision done ",100,350,makecol(255,6,255));
	}
}




void UnitList::traverse2( )
{
	Unit *nd;
	
	
	for( int i = 0; i < numItems; i++ )
	{
		nd = listofUnits[i];
		
	}
	
}

void UnitList::sort()
{
	//first we need to sorut by x lesest to greatest
	quickSort(listofUnits,0,numItems-1);
}


void UnitList::draw( BITMAP* bmp,int lay,View v ,DATAFILE * data, Layer* map )
{


	//textout(screen,font,NumberToString(numItems).c_str(),100,400,makecol(255,255,255));


	for( int i = 0; i < numItems; i++ )
	{

		if (listofUnits[i]->Get_layer()== lay)
		{
			listofUnits[i]->runStatScript();
			listofUnits[i]->animate();
			listofUnits[i]->do_moveing(map,v);

			if(listofUnits[i]->Get_inarea() == current_area)
			{

		
				listofUnits[i]->draw_select(bmp,v,selected_Id);
				if(listofUnits[i]->get_type()==1)
				{
			
					// listofUnits[i]->do_moveing(map,v);
			
					listofUnits[i]->draw(bmp,v,data);
					listofUnits[i]->showname(bmp,v);
					listofUnits[i]->showlife(bmp,v,data);
					listofUnits[i]->talk(bmp,v);
				}
				else if(listofUnits[i]->get_type()==2)
				{
			
					//listofUnits[i]->do_moveing(map,v);
					//listofUnits[i]->animate();
					listofUnits[i]->draw(bmp,v,data);
					listofUnits[i]->showname(bmp,v);
					listofUnits[i]->showlife(bmp,v,data);
					listofUnits[i]->talk(bmp,v);
				}
				else if(listofUnits[i]->get_type()==3)
				{
			
					//listofUnits[i]->do_moveing(map,v);
					//listofUnits[i]->animate();
					listofUnits[i]->draw(bmp,v,data);
					listofUnits[i]->showname(bmp,v);
					listofUnits[i]->showlife(bmp,v,data);
					listofUnits[i]->talk(bmp,v);
				}			}
		}
	
	}


}

bool UnitList::isEmpty( )
{
	return( numItems == 0 );
}

int UnitList::GetnumItems( )
{
	return numItems;
}

bool UnitList::IsInList( int d )
{
	//Unit* nd;
	//nd = head;
	//	while( nd != NULL )
	for( int i = 0; i < numItems; i++ )
	{
		if( listofUnits[i]->Get_id() == d )
		{
			return true;
		}
			//nd = nd->GetNext( );
	}
	return false;
}

void UnitList::setdest()
{

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

	}

	
//	nd = head;
	
	//while( nd != NULL )
	//{
for(  i = 0; i < numItems; i++ )
	{

		if( listofUnits[i]->Get_id() == StringToNumber(temps[0]) )//Id
		{
		listofUnits[i]->Set_X(StringToNumber(temps[3]));//curent x
		listofUnits[i]->Set_Y(StringToNumber(temps[4]));//curent y
		listofUnits[i]->Set_destx(StringToNumber(temps[1])); //destx
		listofUnits[i]->Set_desty(StringToNumber(temps[2]));//desty
		
		listofUnits[i]->Set_direction(StringToNumber(temps[5]));//curent dirfacing
		listofUnits[i]->Set_frame(StringToNumber(temps[6])); //current frame
		listofUnits[i]->Set_inarea(StringToNumber(temps[7]));//inarea
		listofUnits[i]->commanded=1;
		
		
		break;//if found stop looking
		}
		//	nd = nd->GetNext( );
			
	}

}

void UnitList::setdieolog()
{

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

	}

if (StringToNumber(temps[0]) == 0)
{
serverdiolog = temps[1];
}
else
{

	Unit* nd;
//	nd = head;
	int index = 0;
//	while( nd != NULL )
	//{
for( i=0; i<numItems;i++)
	{
 nd= listofUnits[i];
		if( nd->Get_id() == StringToNumber(temps[0]) )//Id
		{
		
		nd->Set_dieolog(temps[1]); 
nd->set_mesageLife( 0);

//nd->UnitObject.msgdie = xt +5;
		break; //stop looking when found
		}
		//	nd = nd->GetNext( );
			
	}
}
}

int UnitList::IndexOf( int d )
{
	for( int i = 0; i < numItems; i++ )
	{
		
		if( listofUnits[i]->Get_id() == d )
		{
			//nd = nd->GetNext();
			return i;
		}
	}
	
	return -1;
}


Unit *UnitList::Get_Unit( int d )
{

	//notfoun need log error
	for( int i = 0; i < numItems; i++ )
	{
		
		if( listofUnits[i]->Get_id() == d )
		{
		//nd = nd->GetNext();
			return listofUnits[i];
		}
	}
	return &Unit();
}
Unit *UnitList::Get_Unit( string d )
{

	//notfoun need log error
	for( int i = 0; i < numItems; i++ )
	{
		
		if( listofUnits[i]->Get_name() == d )
		{
		//nd = nd->GetNext();
			return listofUnits[i];
		}
	}
	return &Unit();
}

Unit *UnitList::Get_Unit( string d ,int *b,int a)
{

	//notfoun need log error
	for( int i = 0; i < numItems; i++ )
	{
		
		if( listofUnits[i]->Get_name() == d &&listofUnits[i]->Get_inarea()==a)
		{
			if(listofUnits[i]->Get_id()!=b[0]&&listofUnits[i]->Get_id()!=b[1]&&listofUnits[i]->Get_id()!=b[2])
			{
		//nd = nd->GetNext();
			return listofUnits[i];
			}
		}
	}
	return &Unit();
}
Unit *UnitList::Get_Unit( string d ,int b)
{

	//notfoun need log error
	for( int i = 0; i < numItems; i++ )
	{
		
		if( listofUnits[i]->Get_name() == d &&listofUnits[i]->Get_id()!=b)
		{
		//nd = nd->GetNext();
			return listofUnits[i];
		}
	}
	return &Unit();
}


int UnitList::Find_Unit_id( int d ,int c)
{
	/*
	Unit* nd;
	//nd = head;
	int index = 0;
	
	while( nd != NULL )
	{
		
		if( nd->UnitObject.Get_id() == d )
		{

			if (c == 1)
			{
			nd->UnitObject.Set_corrent_frame(nd->UnitObject.Get_frame2());

			}
			else if(c == 2)
			{
			nd->UnitObject.Set_corrent_frame(nd->UnitObject.Get_frame3());

			}
			else if(c == 3)
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame4());

			}
			else if(c == 4)
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame5());

			}
			else 
			{
			nd->animeObject.Set_corrent_frame(nd->animeObject.Get_frame1());

			}

			return nd->UnitObject.Get_corrent_frame();
			break;
			
		}
	//	nd = nd->GetNext( );
		index++;
	}
*/
	return -1;
}


bool UnitList::Delete(int d)
{

	int i,j;
	for( i=0; i<numItems;i++)
	{
		if(listofUnits[i]->Get_id()==d)
		{
			d=listofUnits[i]->Get_owner_id();
			if (d!=-1&& d!=0)//-1 = no owner
			{
				for( j=0; j<numItems;j++)//find owner let know this unit is dead
				{
					if(listofUnits[j]->Get_id()==d)
					{
						listofUnits[j]->spunits--;
					}

				}
			}
			swap(listofUnits[i],listofUnits[numItems-1]);
			numItems--;
	
			return true;
		}
	}

	return false;
}


bool UnitList::Deletec(int d)
{

	int i;
	for( i=0; i<numItems;i++)
	{
		if(listofUnits[i]->Get_socketid()==d)
		{
			swap(listofUnits[i],listofUnits[numItems-1]);
			numItems--;
			return true;
		}
	}


return false;
}

string UnitList::listToString(string d)
{
	Unit* nd;
	//	nd = head;
	string list="";
	loding += (numItems * 2) + 1;
	for( int i = 0; i < numItems; i++ )
	{
		nd=(Character*)listofUnits[i];
		textout(screen,font,(nd->toString()).c_str(),100,300,makecol(255,255,255));
		//Sleep(10000);
		outboxbuffer += d+"|0|004|"+ (nd->toString()) +'~';
		outboxbuffer += d+"|0|009|"+NumberToString((nd->Get_id())) +','+ (nd->get_attributess()->toString()) +'~';
		list += nd->toString() + '~';
		//nd = nd->GetNext();
	}
//textout(screen,font,outboxbuffer.c_str(),-250,300,makecol(255,255,255));
//
	return list;
}

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

	}



	int attacker_id = StringToNumber(temps[0]); 
	int defender_id = StringToNumber(temps[1]);
	
	bool attacker_found = false, defender_found = false;

	int attacker,defender;

	for(  attacker = 0; attacker < numItems; attacker++ )
	{
		
		if( listofUnits[attacker]->Get_id() == attacker_id )
		{
			
			attacker_found = true;
			break;
		}

	}

	for(  defender= 0; defender < numItems; defender++ )
	{
		
		if( listofUnits[defender]->Get_id() ==defender_id )
		{
		
			defender_found = true;
			break;
		}
	}

		if ((attacker_found == true) && (defender_found == true))
		{

			

			if(listofUnits[defender]->is_collision(listofUnits[attacker])==true )//riged
			{
				if(((Character*)listofUnits[attacker])->attributess->attackstamana >= 50)/// do we have enough stamna
				{


					
					((Character*)listofUnits[attacker])->attack(((Character*)listofUnits[defender]),music);//rigged
				}
				else if( attacker_id == computerId)
				{
					
					textprintf(screen, font, 250, 220, makecol(255, 0, 0),"Not enough stamna!!");
				}
			}
			else
			{
				outboxbuffer +=attacker_id +"|0|003|" + NumberToString(computerId) + ",you or your tager is not in collision`~";
			}

		}
		else
		{
		outboxbuffer +=attacker_id +"|0|003|" + NumberToString(computerId) + ",cant find traget`~";
		return -1;
		}


return 0;	
}

UnitList::~UnitList()
{

}

#endif