#include	"global.h"

#define KEY_MOVE	4
#define BAR_MOVE	5

int		game(int key);
void	game_setup(void);
int		ball_move(void);
void	reflect_check(void);
int		ship_move_check(int move_x,int move_y);
int		crash_check(void);
void	enemy_move(void);

void	game_wait(int key);

struct CHARA	ship;
struct CHARA	bar;
struct CHARA	ball;
struct CHARA	ball_ex;
struct CHARA	b_dir={5,5};
struct CHARA	poti;
struct CHARA	mike;

struct CHARA	poti_dir;
struct CHARA	mike_dir;

int	ball_direct_level=3;
unsigned char stage[9][14];

int	enemy_flag=0;
int ship_up_flag=0;

int muteki_count;
int speed_counter=0;
int	master_speed;

int enemy_speed_flag;

/*メイン処理*/
int	game(int key)
{
	if(ship_up_flag==1&&ship.x<24){
		ship.x+=2;
		if(ship.x>=24)	ship_up_flag=0;
	}

	if(key&KEY_X1)	if(bar.y-BAR_MOVE>=15)	bar.y-=BAR_MOVE;
	if(key&KEY_X3)	if(bar.y+BAR_MOVE<=81)	bar.y+=BAR_MOVE;

	if(key&KEY_Y1)	i=ship_move_check(0,-KEY_MOVE);
	if(key&KEY_Y3)	i=ship_move_check(0,KEY_MOVE);
	if(key&KEY_Y4)	i=ship_move_check(-KEY_MOVE,0);
	if(key&KEY_Y2)	i=ship_move_check(KEY_MOVE,0);

	if(i==1)			return	1;

	enemy_move();
	
	if(ball_move())		return	2;
/*	reflect_check();*/
	
	if(crash_check())	return	2;
	
	if(speed_counter>=13)
	{
		speed_counter=0;
	/*	if(b_dir.x>0)	b_dir.x+=1;
		else			b_dir.x-=1;
		if(b_dir.y>0)	b_dir.y+=1;
		else			b_dir.y-=1;*/

		if(master_speed<12)	master_speed++;
	}

	return	0;
}

void	game_wait(int key)
{
	if(ship_up_flag==1&&ship.x<24){
		ship.x+=2;
		if(ship.x>=24)	ship_up_flag=0;
	}

	switch(key)
	{
		case	KEY_X1:
			if(bar.y-BAR_MOVE>16)
			{
				bar.y-=BAR_MOVE;
				ball.y-=BAR_MOVE;
				ball_ex.y-=BAR_MOVE*100;
			}
			break;
		case	KEY_X3:
			if(bar.y+BAR_MOVE<80)
			{
				bar.y+=BAR_MOVE;
				ball.y+=BAR_MOVE;
				ball_ex.y+=BAR_MOVE*100;
			}
			break;
	}

}


int		crash_check(void)
{
	if(muteki_count==0)
		if(ship.x<ball.x+8&&ship.x+16>ball.x)
			if(ship.y<ball.y+8&&ship.y+16>ball.y&&bomb_flag==0)
			{
				ship.flag--;
				if(ship.flag==0)	return	1;
				bomb_flag=1;
				muteki_count=30;
				se_start(3);
			}


	if(poti.flag&&poti_dir.flag==0&&muteki_count==0)
	{
		if(ship.x<poti.x+16&&ship.x+16>poti.x)
			if(ship.y<poti.y+16&&ship.y+16>poti.y&&bomb_flag==0)
			{
				ship.flag--;
				if(ship.flag==0)	return	1;
				bomb_flag=1;
				muteki_count=30;
				se_start(3);
			}
		
		if(poti.x<ball.x+8&&poti.x+16>ball.x)
			if(poti.y<ball.y+8&&poti.y+16>ball.y)
			{
				poti.flag--;
				poti_dir.flag=10;
				if(poti.flag==0)	enemy_bomb_flag1=1;
				
				if(
					((ball.x+3<poti.x+3)||(ball.x+3>poti.x+12))&&
					((ball.y+3>poti.y+3)&&(ball.y+3<poti.y+12))
				)	b_dir.x*=-1;
				
				else if(
					((ball.y+3<poti.y+3)||(ball.y+3>poti.y+12))&&
					((ball.x+3>poti.x+3)&&(ball.x+3<poti.x+12))
				)	b_dir.y*=-1;
			
				else	b_dir.x*=-1,b_dir.y*=-1;
			
			}
	}
	if(mike.flag&&mike_dir.flag==0&&muteki_count==0)
	{
		if(ship.x<mike.x+16&&ship.x+16>mike.x)
			if(ship.y<mike.y+16&&ship.y+16>mike.y&&bomb_flag==0)
			{
				ship.flag--;
				if(ship.flag==0)	return	1;
				bomb_flag=1;
				muteki_count=30;
				se_start(3);
			}

		if(mike.x<ball.x+8&&mike.x+16>ball.x)
			if(mike.y<ball.y+8&&mike.y+16>ball.y)
			{
				mike.flag--;
				mike_dir.flag=10;
				if(mike.flag==0)	enemy_bomb_flag2=1;
				
				if(
					((ball.x+3<mike.x+3)||(ball.x+3>mike.x+12))&&
					((ball.y+3>mike.y+3)&&(ball.y+3<mike.y+12))
				)	b_dir.x*=-1;
				
				else if(
					((ball.y+3<mike.y+3)||(ball.y+3>mike.y+12))&&
					((ball.x+3>mike.x+3)&&(ball.x+3<mike.x+12))
				)	b_dir.y*=-1;
			
				else	b_dir.x*=-1,b_dir.y*=-1;
			}
	}

	if(poti_dir.flag)	poti_dir.flag--;
	if(mike_dir.flag)	mike_dir.flag--;

	return	0;

}

void	enemy_move(void)
{
	int check_flag[8];
	
	int jx;
	int	jy;

	if(poti.flag&&poti_dir.flag==0)
	{
		if(poti.x<32&&poti_dir.x<0)	poti_dir.x*=-1;

		jx=poti.x+poti_dir.x+7;
		jy=poti.y+poti_dir.y+7;

		check_flag[0]=stage[(jy-7)/16][(jx-7)/16];
		check_flag[1]=stage[(jy-7)/16][(jx+8)/16];
		check_flag[2]=stage[(jy+8)/16][(jx-7)/16];
		check_flag[3]=stage[(jy+8)/16][(jx+8)/16];

		check_flag[4]=stage[(jy-7)/16][(jx)/16];
		check_flag[5]=stage[(jy)/16][(jx+8)/16];
		check_flag[6]=stage[(jy+8)/16][(jx)/16];
		check_flag[7]=stage[(jy)/16][(jx-7)/16];

		if((check_flag[0]+check_flag[1]+check_flag[2]+check_flag[3])!=0)
		{
			if(check_flag[0]!=0)
			{
				if(check_flag[4]!=0&&check_flag[7]!=0)		poti_dir.x*=-1,poti_dir.y*=-1;
				else if(check_flag[4]!=0)					poti_dir.y*=-1;
				else if(check_flag[7]!=0)					poti_dir.x*=-1;
				else										poti_dir.x*=-1,poti_dir.y*=-1;

			}
			else if(check_flag[1]!=0)
			{
				if(check_flag[4]!=0&&check_flag[5]!=0)		poti_dir.x*=-1,poti_dir.y*=-1;
				else if(check_flag[4]!=0)					poti_dir.y*=-1;
				else if(check_flag[5]!=0)					poti_dir.x*=-1;
				else										poti_dir.x*=-1,poti_dir.y*=-1;

			}
			else if(check_flag[2]!=0)
			{
				if(check_flag[6]!=0&&check_flag[7]!=0)		poti_dir.x*=-1,poti_dir.y*=-1;
				else if(check_flag[6]!=0)					poti_dir.y*=-1;
				else if(check_flag[7]!=0)					poti_dir.x*=-1;
				else										poti_dir.x*=-1,poti_dir.y*=-1;

			}
			else if(check_flag[3]!=0)
			{
				if(check_flag[6]!=0&&check_flag[5]!=0)		poti_dir.x*=-1,poti_dir.y*=-1;
				else if(check_flag[6]!=0)					poti_dir.y*=-1;
				else if(check_flag[5]!=0)					poti_dir.x*=-1;
				else										poti_dir.x*=-1,poti_dir.y*=-1;

			}

		}
	
		poti.x+=poti_dir.x;
		poti.y+=poti_dir.y;
	}


	if(mike.flag&&mike_dir.flag==0)
	{
		if(mike.x<32&&mike_dir.x<0)	mike_dir.x*=-1;

		jx=mike.x+mike_dir.x+7;
		jy=mike.y+mike_dir.y+7;

		check_flag[0]=stage[(jy-7)/16][(jx-7)/16];
		check_flag[1]=stage[(jy-7)/16][(jx+8)/16];
		check_flag[2]=stage[(jy+8)/16][(jx-7)/16];
		check_flag[3]=stage[(jy+8)/16][(jx+8)/16];

		check_flag[4]=stage[(jy-7)/16][(jx)/16];
		check_flag[5]=stage[(jy)/16][(jx+8)/16];
		check_flag[6]=stage[(jy+8)/16][(jx)/16];
		check_flag[7]=stage[(jy)/16][(jx-7)/16];

		if((check_flag[0]+check_flag[1]+check_flag[2]+check_flag[3])!=0)
		{
			if(check_flag[0]!=0)
			{
				if(check_flag[4]!=0&&check_flag[7]!=0)		mike_dir.x*=-1,mike_dir.y*=-1;
				else if(check_flag[4]!=0)					mike_dir.y*=-1;
				else if(check_flag[7]!=0)					mike_dir.x*=-1;
				else										mike_dir.x*=-1,mike_dir.y*=-1;

			}
			else if(check_flag[1]!=0)
			{
				if(check_flag[4]!=0&&check_flag[5]!=0)		mike_dir.x*=-1,mike_dir.y*=-1;
				else if(check_flag[4]!=0)					mike_dir.y*=-1;
				else if(check_flag[5]!=0)					mike_dir.x*=-1;
				else										mike_dir.x*=-1,mike_dir.y*=-1;

			}
			else if(check_flag[2]!=0)
			{
				if(check_flag[6]!=0&&check_flag[7]!=0)		mike_dir.x*=-1,mike_dir.y*=-1;
				else if(check_flag[6]!=0)					mike_dir.y*=-1;
				else if(check_flag[7]!=0)					mike_dir.x*=-1;
				else										mike_dir.x*=-1,mike_dir.y*=-1;

			}
			else if(check_flag[3]!=0)
			{
				if(check_flag[6]!=0&&check_flag[5]!=0)		mike_dir.x*=-1,mike_dir.y*=-1;
				else if(check_flag[6]!=0)					mike_dir.y*=-1;
				else if(check_flag[5]!=0)					mike_dir.x*=-1;
				else										mike_dir.x*=-1,mike_dir.y*=-1;

			}

		}
	
		mike.x+=mike_dir.x;
		mike.y+=mike_dir.y;
	
	}

}


int	ship_move_check(int move_x,int move_y)
{
	
	int check_flag[8];
	
	int jx=ship.x+move_x+7;
	int	jy=ship.y+move_y+7;

	if(ship_up_flag||bomb_flag)	return	0;
	if(jx<20)					return	0;

	check_flag[0]=stage[(jy-6)/16][(jx-6)/16];
	check_flag[1]=stage[(jy-6)/16][(jx+6)/16];
	check_flag[2]=stage[(jy+6)/16][(jx-6)/16];
	check_flag[3]=stage[(jy+6)/16][(jx+6)/16];
	
	check_flag[4]=stage[(jy-5)/16][(jx-5)/16];
	check_flag[5]=stage[(jy-5)/16][(jx+5)/16];
	check_flag[6]=stage[(jy+5)/16][(jx-5)/16];
	check_flag[7]=stage[(jy+5)/16][(jx+5)/16];


	if((check_flag[4]+check_flag[5]+check_flag[6]+check_flag[7])==32)
	{
		return	1;
	}	
	else if((check_flag[0]+check_flag[1]+check_flag[2]+check_flag[3])==0)
	{
		ship.x+=move_x;
		ship.y+=move_y;		
	}
	else if(
		(check_flag[4]==10||
		check_flag[5]==10||
		check_flag[6]==10||
		check_flag[7]==10)&&
		enemy_speed_flag==0)
	{
		if(check_flag[4]==10)		goal_draw((jy-5)/16,(jx-5)/16);
		else if(check_flag[5]==10)	goal_draw((jy-5)/16,(jx+5)/16);
		else if(check_flag[6]==10)	goal_draw((jy+5)/16,(jx-5)/16);
		else if(check_flag[7]==10)	goal_draw((jy+5)/16,(jx+5)/16);
		
		ship.x+=move_x;
		ship.y+=move_y;		
	
		enemy_speed_flag=1;

		if(poti_dir.x>0)	poti_dir.x++;
		else				poti_dir.x--;
		
		if(poti_dir.y>0)	poti_dir.y++;
		else				poti_dir.y--;

		if(mike_dir.x>0)	mike_dir.x++;
		else				mike_dir.x--;
		
		if(mike_dir.y>0)	mike_dir.y++;
		else				mike_dir.y--;
		
		se_start(2);
	}
	
	else if(	(check_flag[0]==0||check_flag[0]==8||check_flag[0]==10)&&
				(check_flag[1]==0||check_flag[1]==8||check_flag[1]==10)&&
				(check_flag[2]==0||check_flag[2]==8||check_flag[2]==10)&&
				(check_flag[3]==0||check_flag[3]==8||check_flag[3]==10)	)
	{

		ship.x+=move_x;
		ship.y+=move_y;		
	}

	return	0;
}


int	ball_move(void)
{
		
	if(ball_ex.x<2100&&ball_ex.x>1000)
	{
		
		if(ball.y+4>bar.y&&ball.y+4<bar.y+48)
		{
			i=bar.y+24-ball.y+4;
			ball_ex.x=2100;

			speed_counter++;
			se_start(1);
			
			if(i<-12)
			{
				if(b_dir.y<0)	
				{
					ball_direct_level-=2;
					if(ball_direct_level==-1)		ball_direct_level=0,b_dir.y*=-1;
					else if(ball_direct_level==-2)	ball_direct_level=1,b_dir.y*=-1;
				}
				else
				{
					ball_direct_level+=2;
					if(ball_direct_level>5)	ball_direct_level=4;
				}
			}
			
			else if(i<=0)
			{
				if(b_dir.y<0)	
				{
					ball_direct_level-=1;
					if(ball_direct_level<0)	ball_direct_level=0;
				}
				else
				{
					ball_direct_level+=1;
					if(ball_direct_level>5)	ball_direct_level=4;
				}

			}
			else if(i>12)
			{
				if(b_dir.y<0)	
				{
					ball_direct_level+=2;
					if(ball_direct_level>5)	ball_direct_level=4;
				}
				else
				{
					ball_direct_level-=2;
					if(ball_direct_level==-1)		ball_direct_level=0,b_dir.y*=-1;
					else if(ball_direct_level==-2)	ball_direct_level=1,b_dir.y*=-1;
				}
			}
			else
			{
				if(b_dir.y<0)	
				{
					ball_direct_level+=1;
					if(ball_direct_level>5)	ball_direct_level=4;
				}
				else
				{
					ball_direct_level-=1;
					if(ball_direct_level<0)	ball_direct_level=0;
				}
			}

			if(b_dir.x*-1>0)	b_dir.x*=-1;
		
		}
	}

	if(ball_ex.x<0)
	{
		ship.flag--;
		if(ship.flag==0)	return	1;
		bomb_flag=1;
		ball_ex.x=3000;
		ball_ex.y=3000;
		muteki_count=30;

		se_start(3);

		if(b_dir.x<0)	b_dir.x*=-1;
		if(b_dir.y<0)	b_dir.y*=-1;
		ball_direct_level=1;
	}

/*	if(ball_ex.y<1600&&b_dir.y<0)	b_dir.y*=-1;
	if(ball_ex.y>12000&&b_dir.y>0)	b_dir.y*=-1;
	if(ball_ex.x>20200&&b_dir.x>0)	b_dir.x*=-1;
*/


	for(i=0;i<master_speed;i++)
	{

		ball_ex.x+=direction[ball_direct_level].x*b_dir.x;
		ball_ex.y+=direction[ball_direct_level].y*b_dir.y;


		ball.x=ball_ex.x/100;
		ball.y=ball_ex.y/100;
	
		reflect_check();
	}
	return	0;
}



void	reflect_check(void)
{
/*	int check_flag[8];
	
	int jx=ball.x+3;
	int	jy=ball.y+3;
	
	check_flag[0]=stage[(jy-3)/16][(jx-3)/16];
	check_flag[1]=stage[(jy-3)/16][(jx+4)/16];
	check_flag[2]=stage[(jy+4)/16][(jx-3)/16];
	check_flag[3]=stage[(jy+4)/16][(jx+4)/16];
	
	check_flag[4]=stage[(jy-3)/16][(jx)/16];
	check_flag[5]=stage[(jy)/16][(jx+4)/16];
	check_flag[6]=stage[(jy+4)/16][(jx)/16];
	check_flag[7]=stage[(jy)/16][(jx-3)/16];

	if((check_flag[0]+check_flag[1]+check_flag[2]+check_flag[3])!=0)
	{
		se_start(0);
		
		if(check_flag[0]!=0)
		{
			if(check_flag[4]!=0&&check_flag[7]!=0&&stage[jy/16][jx/16]==0)		b_dir.x*=-1,b_dir.y*=-1;
			else if(check_flag[4]!=0)					b_dir.y*=-1;
			else if(check_flag[7]!=0)					b_dir.x*=-1;
			else										b_dir.x*=-1,b_dir.y*=-1;

			if(check_flag[0]!=9&&check_flag[0]!=8&&check_flag[0]!=7)	erase_block((jx-3)/16,(jy-3)/16);
		}
		else if(check_flag[1]!=0)
		{
			if(check_flag[4]!=0&&check_flag[5]!=0&&stage[jy/16][jx/16]==0)		b_dir.x*=-1,b_dir.y*=-1;
			else if(check_flag[4]!=0)					b_dir.y*=-1;
			else if(check_flag[5]!=0)					b_dir.x*=-1;
			else										b_dir.x*=-1,b_dir.y*=-1;

			if(check_flag[1]!=9&&check_flag[1]!=8&&check_flag[1]!=7)	erase_block((jx+4)/16,(jy-3)/16);
		}
		else if(check_flag[2]!=0)
		{
			if(check_flag[6]!=0&&check_flag[7]!=0&&stage[jy/16][jx/16]==0)		b_dir.x*=-1,b_dir.y*=-1;
			else if(check_flag[6]!=0)					b_dir.y*=-1;
			else if(check_flag[7]!=0)					b_dir.x*=-1;
			else										b_dir.x*=-1,b_dir.y*=-1;

			if(check_flag[2]!=9&&check_flag[2]!=8&&check_flag[2]!=7)	erase_block((jx-3)/16,(jy+4)/16);
		}
		else if(check_flag[3]!=0)
		{
			if(check_flag[6]!=0&&check_flag[5]!=0&&stage[jy/16][jx/16]==0)		b_dir.x*=-1,b_dir.y*=-1;
			else if(check_flag[6]!=0)					b_dir.y*=-1;
			else if(check_flag[5]!=0)					b_dir.x*=-1;
			else										b_dir.x*=-1,b_dir.y*=-1;

			if(check_flag[3]!=9&&check_flag[3]!=8&&check_flag[3]!=7)	erase_block((jx+4)/16,(jy+4)/16);
		}

	}
*/

	
	int jx=ball.x+3;
	int	jy=ball.y+3;

	if(stage[jy/16][jx/16]==9)
	{
		se_start(0);

		if(jx>=200)	b_dir.x*=-1;
		if(jy<=24)	b_dir.y*=-1;
		if(jy>=120)	b_dir.y*=-1;

	}
	
	else if(stage[jy/16][jx/16]!=0)
	{
		se_start(0);
		
		if(stage[jy/16][jx/16]!=8&&stage[jy/16][jx/16]!=7)	erase_block((jx/16),(jy/16));		
		
		jx=jx%16;
		jy=jy%16;

		if(jx==0)						b_dir.x*=-1;
		if(jx==15)						b_dir.x*=-1;
		if(jy==0&&jx!=0&&jx!=15)		b_dir.y*=-1;
		if(jy==15&&jx!=0&&jx!=15)		b_dir.y*=-1;
	}


/*
	if(ball.x<=0||ball.x>=224)	b_dir.x*=-1;
	if(ball.y<=0||ball.y>=144)	b_dir.y*=-1;
*/

}

void	game_setup(void)
{

	ship.x=0,ship.y=24;

	bar.x=5,bar.y=50;

	ball.x=22,ball.y=72;
	ball_ex.x=2200,ball_ex.y=7200;

	ball_direct_level=(get_rand()%4);

	ship_up_flag=1;

	speed_counter=0;
	muteki_count=0;

	enemy_speed_flag=0;

	
	switch(stage_counter)
	{
		case	0:
			enemy_flag=2;
			poti.x=150,poti.y=30,poti.flag=0;
			mike.x=150,mike.y=100,mike.flag=0;
			poti_dir.x=1,poti_dir.y=1;
			mike_dir.x=2,mike_dir.y=2;
			b_dir.x=4,b_dir.y=4;
			break;
		case	1:
			enemy_flag=0;
			poti.x=150,poti.y=30,poti.flag=0;
			mike.x=150,mike.y=100,mike.flag=0;
			poti_dir.x=1,poti_dir.y=1;
			mike_dir.x=2,mike_dir.y=2;
			b_dir.x=5,b_dir.y=5;
			break;
		case	2:
			enemy_flag=1;
			poti.x=160,poti.y=64,poti.flag=3;
			mike.x=150,mike.y=100,mike.flag=0;
			poti_dir.x=1,poti_dir.y=1;
			mike_dir.x=2,mike_dir.y=2;
			b_dir.x=5,b_dir.y=5;
			break;
		case	3:
			enemy_flag=2;
			poti.x=120,poti.y=24,poti.flag=3;
			mike.x=120,mike.y=104,mike.flag=3;
			poti_dir.x=1,poti_dir.y=1;
			mike_dir.x=2,mike_dir.y=2;
			b_dir.x=5,b_dir.y=5;
			break;
		case	4:
			enemy_flag=2;
			poti.x=120,poti.y=16,poti.flag=5;
			mike.x=120,mike.y=112,mike.flag=5;
			poti_dir.x=1,poti_dir.y=1;
			mike_dir.x=2,mike_dir.y=2;
			b_dir.x=5,b_dir.y=5;
			break;
	}
	
	direction[0].x=98;direction[0].y=17;
	direction[1].x=93;direction[1].y=34;
	direction[2].x=86;direction[2].y=50;
	direction[3].x=76;direction[3].y=64;
	direction[4].x=64;direction[4].y=76;
	direction[5].x=34;direction[5].y=86;
	direction[6].x=17;direction[6].y=98;

	b_dir.x=1,b_dir.y=1;
	master_speed=5;
}