#include "unit.h"
#include "Stats.h"

//Unit.cpp
void drawRectangle(BITMAP *bmp,int x, int y, int w,int h,int color, int style, int bgcolor);


void Unit::play_sound(int s,DATAFILE *music)
{
//	if (snd==0)
	play_sample((SAMPLE*)music[s].dat, 255, 128, 1000,FALSE);
}

void Unit::play_song(int s,DATAFILE *music)
{
//	if (snd==0)
		play_midi((MIDI*)music[s].dat, TRUE);
}
/*
void Game::play_sound(string f)
{
	int s;
	if()
	{ }
	else if()
	{

	}
//	if (snd==0)
	play_sample((SAMPLE*)music[s].dat, 255, 128, 1000,FALSE);
}
*/
bool Unit::hit_wall(int n,Layer* map,View v)
{
	
	int x1,y1;
	for (int i=8; i<24; i++)
	{
		for (int j=20; j<32; j++)
		{

			if(n==1)
			{

			x1=((x+attributess->movmentRate+i)/32);
			y1=((y+j)/32)-1;
			}
			else if(n==2)
			{
			x1=((x-attributess->movmentRate+i)/32)-1;
			y1=((y+j)/32)-1;
			}
			else if(n==3)
			{
			x1=((x+i)/32)-1;
			y1=((y+attributess->movmentRate+j)/32);
			}
			else if(n==4)
			{
			x1=((x+i)/32)-1;
			y1=((y-attributess->movmentRate+j)/32)-1;
			}



/*
			if(n==1)
			{

			x1=((x+movmentRate+i+v.get_scroll_x())/32)+v.get_screen_pos_x()-1;
			y1=((y+j+v.get_scroll_y())/32)+v.get_screen_pos_y()-1;
			}
			else if(n==2)
			{
			x1=((x-movmentRate+i+v.get_scroll_x())/32)+v.get_screen_pos_x()-1;
			y1=((y+j+v.get_scroll_y())/32)+v.get_screen_pos_y()-1;
			}
			else if(n==3)
			{
			x1=((x+i+v.get_scroll_x())/32)+v.get_screen_pos_x()-1;
			y1=((y+movmentRate+j+v.get_scroll_y())/32)+v.get_screen_pos_y()-1;
			}
			else if(n==4)
			{
			x1=((x+i+v.get_scroll_x())/32)+v.get_screen_pos_x()-1;
			y1=((y-movmentRate+j+v.get_scroll_y())/32)+v.get_screen_pos_y()-1;
			}
*/

			if (map->cells[y1][x1].get_passable(0) ==1)
				 return true;
		}
	}

	return false;
}
void Unit::stop_song()
{
	play_midi(NULL,FALSE);
} 

bool Unit::targetinrange( Unit* b)
{

		if ( ( ( b->x +(b->w *.5) -1 >= x+(w *.5)-attributess->range) && (b->x +(b->w *.5)-1 <= x+(w *.5) + attributess->range) )// is a on the right side
                  && 
			(  ( b->y + (b->h *.5) -1 >=y+(h *.5)-attributess->range  )&& ( b->y+(b->h *.5) -1<= y+(h *.5) + attributess->range )  ))
          {
				


    

				
				return true; // means colide
			}
			else
			{
			
				if( target==b->Get_id())
				{
					target=-1;
					//	Set_destx(Get_X());//rig glich get out of cyinck

					//	Set_desty(Get_Y());//rig glich get out of cyinck
				}
				// this means that there is no collision
				 return false; // means no colide


			}
}
bool Unit::targetinsite( Unit* b)
{
		if ( ( ( b->x +(b->w *.5) -1 >= x+(w *.5)-attributess->site) && (b->x +(b->w *.5)-1 <= x+(w *.5) + attributess->site) )// is a on the right side
                  && 
			(  ( b->y + (b->h *.5) -1 >=y+(h *.5)-attributess->site  )&& ( b->y+(b->h *.5) -1<= y+(h *.5) + attributess->site )  ))
             {
				


	
    


				return true; // means colide
			}
			else
			{

			if( target==b->Get_id())
				{
					target=-1;
					//	Set_destx(Get_X());//rig glich get out of cyinck

					//	Set_desty(Get_Y());//rig glich get out of cyinck
				}
			 // this means that there is no collision
				 return false; // means no colide


			}
}
bool Unit::is_collision( Unit* b)
{
    if ((x > b->x + b->w -1) || // is a on the right side
        (y > b->y + b->h -1) || // is a under
        (b->x > x + w -1) || // is b on the right side
        (b->y > y + h -1))   // is b under
    {
        // this means that there is no collision
        return false; // means no colide
    }

    return true; // means colide
}
	//gets
string Unit::Get_name()
{

	return name;
}
int Unit::Get_inarea()
{
	return inarea;
}
int Unit::Get_id()
{
	return id;
}
int Unit::Get_owner_id()
{
	return owner_id;
}
int Unit::Get_socketid()
{
	return socketid;
}

int Unit::Get_X()
{
	return x;
}
int Unit::Get_Y()
{
	return y;
}
	//gets
int Unit::Get_W()
{
	return w;
}
int Unit::Get_H()
{
	return h;
}
int Unit::Get_frame()
{
	return frame;
}
int Unit::Get_source_X()
{
	return source_X;
}
int Unit::Get_source_Y()
{
	return source_Y;
}
int Unit::Get_layer()
{
	return layer;
}

int Unit::Is_in_collision()
{
	return collision;
}
int Unit::Get_direction()
{
	return direction;
}
int Unit::Get_sourcebitmap()
{
	return source;
}
int Unit::Get_destbitmap()
{
	return dest;
}
	//sets
void Unit::Set_name(string New_name)
{
	name =  New_name ;
}
void Unit::Set_inarea(int new_area)
{
	inarea = new_area;
}
void Unit::Set_id(int new_id)
{
	id = new_id;
}
void Unit::Set_owner_id(int new_id)
{
	owner_id = new_id;
}
void Unit::Set_socketid(int new_id)
{
	socketid = new_id;
}

void Unit::Set_X(int new_x)
{
	x=new_x;
}
void Unit::Set_Y(int new_y)
{
	y=new_y;
}
void Unit::Set_W(int new_W)
{
	w=new_W;
}
void Unit::Set_H(int new_H)
{
	h=new_H;
}
void Unit::Set_source_X(int new_x)
{
	source_X=new_x;
}
void Unit::Set_source_Y(int new_y)
{
	source_Y=new_y;
}
void Unit::Set_frame(int new_frame)
{
	frame=new_frame;
}
void Unit::Set_layer(int new_layer)
{
	layer=new_layer;
}
void Unit::Set_collision(int new_state)
{
	collision = new_state;
}
void Unit::Set_direction(int new_direction)
{
	direction = new_direction;
}
void Unit::Set_sourcebitmap(int new_bitmap)
{
	source = new_bitmap;
}
void Unit::Set_destbitmap(int new_bitmap)
{
	dest = new_bitmap;
}
	//constutors
Unit::Unit()
{

selected=false;
trading=false;
owner_id = -1;
commanded=0;
secon=false;
target=-1;
 controlltype=0;
Voice = NULL;

 frspeed =0;///glitch with animate when set to 1
		 connte =0;

socketid = -1;
name = "none";
	
	 x = 2060;
	 y = 1594;
	 w = 32;
	 h = 32;
	 layer = 3;
	 source_X = 32;
	 source_Y = 32;
	 collision = 0;
	 frame = 1;
	direction = 0;
	source = 4;
	id = 1;
	spunits=0;
	spunits_max=0;
}

Unit::Unit(int s,BITMAP* d)
{
selected=false;
trading=false;
owner_id = -1;
commanded=0;
controlltype=0;
Voice = NULL;

frspeed =0;///glitch with animate when set to 1
	connte =0;
target=-1;
socketid = -1;
name = "none";
	
	source = s;
	//dest = d;
	 x = 2060;
	 y = 1594;
	 w = 32;
	 h = 32;
	 layer = 3;
	 source_X = 0;
	 source_Y = 0;
	 collision = 0;
	 frame = 1;
	direction = 0;
	id = 1;
	spunits=0;
	spunits_max=0;

}

void Unit::draw_select(BITMAP* n,View v,int &sid)
{
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)
		{
			if (mouse_b & 1)
			{
				selected=true;
				sid=id;
			}
		}
	}


	if(selected==false)
	{
	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)
		{

			drawRectangle(n,x-v.get_screen_pos_x()*32-v.get_scroll_x()-w+15, 40+y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, w,h,makecol(255,255,0), 1, makecol(255,0,255));


		}
	}
	}
	else
	{
if(sid!=id)
{
selected=false;
}
	drawRectangle(n,x-v.get_screen_pos_x()*32-v.get_scroll_x()-w+15, 40+y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, w,h,makecol(0,255,0), 1, makecol(255,0,255));

	}

}

void Unit::draw(BITMAP* n,View v,DATAFILE * data)
{
	if ((x>=v.get_screen_pos_x()*32 && x<=(v.get_screen_pos_x()+v.get_GRID_WIDTH())*32) &&
		(y>=v.get_screen_pos_y()*32 && y<=(v.get_screen_pos_y()+v.get_GRID_HEIGHT())*32))
	{								
		//masked_blit(source,dest, source_X,source_Y,x,  y, w, h);
		
		masked_stretch_blit((BITMAP*)data[source].dat, n, source_X*w,source_Y*h, w, h,x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, w*2, h*2);

		//masked_blit((BITMAP*)data[source].dat,n,source_X*w,source_Y*h,x-v.get_screen_pos_x()*32-v.get_scroll_x()-w,y-v.get_screen_pos_y()*32-v.get_scroll_y()-h, w,h);
	}
}

Unit::~Unit()
{
	//unload_datafile(data);
//delete Astar;
}
void Unit::showname(BITMAP* n,View v)
{

	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)
		{

			textout(n,font,name.c_str(),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
		}	

	}

}

