

#include "generic.h"
#include "lfptypes.h"
#include "define.h"
#include "ml.h"

#include "ctrl.h"


extern s8 ShowTrails;
static void ((*special_rous[])(playert *plyr))={

//standing/walking
crossover_start,
std_x_start,

//running
crossover_start,
spin_start,

//shuffling
crossover_start,
crossover_start,

//turbo
crossover_start,
spin_start,
};


global int  special_time;


void special_start(playert *plyr, int index)
{
	if (current_ctrlr->mctrlr & CTRL_CROSSOVER)
	{																				 
		jmask(CTRL_CROSSOVER);
		index += 0;
	}
	else if (current_ctrlr->mctrlr & CTRL_DRIBBLE)
	{
		if (turbo_test(plyr, TURBO_DRIBBLE)==FALSE)	return;

		special_time = lp_ctr;

		index += 1;
	}

	(special_rous[index])(plyr);
}



void crossover_start(playert *plyr)
{
	plyr->cross_ctr += 1*ONE_SEC;

	if (plyr->control)
	{
		if (plyr->cross_ctr > MIN_MAX(plyr->plyrROM->ub___pAttrDribble, 2*ONE_SEC, 2.5*ONE_SEC))
		{
			//drop_ball(plyr);
			//return;
		}
	}


	switch (abs(plyr->top_anim))
	{
	case TOP_PRT_DRIBL:
	case TOP_PRT_DRIB:
	case TOP_WALK_DRIB:
	case TOP_JOG_DRIB:
	case TOP_RUN_DRIB:
	case TOP_RUN_DRIB2:
	case TOP_TURBO_DRIB:
	case TOP_STIFF_DRIB:
	case TOP_LOW_DRIB:
	case TOP_HIGH_DRIB:

		SetMain(crossover_start,plyr,crossover_wait);
		crossover_wait(plyr);
		plyr->top_routine  = NULL;
		break;

	case TOP_STD_DRIB:
		
		plyr->top_routine  = NULL;

		special_time = lp_ctr;
		plyr->xvel=0; //LFPCos(plyr->angle);
		plyr->yvel=0; //LFPSin(plyr->angle);
		if (plyr->top_anim>0)
		{
			start_blend_anim(plyr, TOP_STDCROSS);
			plyr->top_rate+=plyr->top_rate>>2;
			SetMain(crossover_start,plyr,standing_crossover);
		}
		else
		{
			start_blend_anim(plyr, -TOP_STDCROSS);
			plyr->top_rate+=plyr->top_rate>>2;
			SetMain(crossover_start,plyr,standing_crossover_flip);
		}
		break;
	}
}



void std_x_left_start(playert *plyr)
{
	if (plyr->top_anim > 0)
	{
		start_blend_anim(plyr, TOP_STDXL1);

		plyr->top_rate = (frames_to_go(plyr)<<8)/10;

		start_vels(plyr, 38, (SW)(plyr->angle+0x1555));

		plyr->angle_vel = 0x2000/10;

		SetMain(std_x_left_start,plyr,std_x_left1);
		plyr->top_routine = NULL;
	}
	else
	{
		start_blend_anim(plyr, -TOP_STDXR1);

		plyr->top_rate = (frames_to_go(plyr)<<8)/10;

		start_vels(plyr, 38, (SW)(plyr->angle-0x1555));

		plyr->angle_vel = -0x2000/10;

		SetMain(std_x_left_start,plyr,std_x_right1);
		plyr->top_routine  = NULL;
	}
}





void std_x_right_start(playert *plyr)
{
	if (plyr->top_anim < 0)
	{
		start_blend_anim(plyr, -TOP_STDXL1);

		plyr->top_rate = (frames_to_go(plyr)<<8)/10;

		start_vels(plyr, 38, (SW)(plyr->angle-0x2000));

		plyr->angle_vel = -0x2000/10;

		SetMain(std_x_right_start,plyr,std_x_left1);
		plyr->top_routine = NULL;
	}
	else
	{
		start_blend_anim(plyr, TOP_STDXR1);

		plyr->top_rate = (frames_to_go(plyr)<<8)/10;

		start_vels(plyr, 38, (SW)(plyr->angle+0x2000));

		plyr->angle_vel = 0x2000/10;

		SetMain(std_x_right_start,plyr,std_x_right1);
		plyr->top_routine  = NULL;
	}
}




void std_x_left(playert *plyr)
{
	run_collision(plyr, plyr->xvel, plyr->yvel);
	plyr->angle += plyr->angle_vel;

	if (blend_to_end(plyr, -TOP_TURBO_DRIB, LEG_JOG, 3))
	{
		SetMain(std_x_left,plyr,dribble);
		plyr->top_routine  = dribble_animate;

		plyr->leg_routine = running;
		plyr->last_speed  = TURBO_THRESHOLD+0x200;
	}

	if (plyr->top_blend_ctr == 0x200)	plyr->leg_frame = 15<<8;


	dribble_inbounds(plyr);
}


void std_x_left_flip(playert *plyr)
{
	run_collision(plyr, plyr->xvel, plyr->yvel);

	plyr->angle += plyr->angle_vel;

	if (blend_to_end(plyr, TOP_TURBO_DRIB, LEG_RUN, 3))
	{
		SetMain(std_x_left_flip,plyr,dribble);
		plyr->top_routine  = dribble_animate;

		plyr->leg_routine = running;
		plyr->last_speed  = TURBO_THRESHOLD;
	}

//	if (plyr->top_blend_ctr == 0x200)	plyr->leg_frame = 15<<8;
	if (plyr->top_blend_ctr == 0x200)	plyr->leg_frame = 35<<8;

	dribble_inbounds(plyr);
}









void std_x_right1(playert *plyr)
{
	run_collision(plyr, plyr->xvel, plyr->yvel);
	
	plyr->angle += plyr->angle_vel;

	if (end_animation_test(plyr))
	{
		int r;

		plyr->angle_vel = -13*plyr->angle_vel/10;

		r = plyr->top_rate;
		start_next_anim(plyr);
		plyr->top_rate = r;

		start_vels(plyr, 83, (SW)(plyr->angle + ((plyr->top_anim>0) ? -0x2ccc:0x2ccc)));
		SetMain(std_x_right1,plyr,std_x_right2);
	}

	dribble_inbounds(plyr);
}


void std_x_right2(playert *plyr)
{
int top_anim;

	run_collision(plyr, plyr->xvel, plyr->yvel);
	
	plyr->angle += plyr->angle_vel;

	if (plyr->top_anim>0)		top_anim = TOP_RUN_DRIB;
	else						top_anim = -TOP_RUN_DRIB;

	if (blend_to_end(plyr, top_anim, LEG_RUN, 3))
	{
		SetMain(std_x_right2,plyr,dribble);
		plyr->top_routine  = dribble_animate;

		plyr->leg_routine = running;
		plyr->last_speed  = TURBO_THRESHOLD+0x200;
	}

	if (plyr->top_blend_ctr == 0x200)
	{
		if (top_anim<0)		plyr->leg_frame += 0x20<<8;
	}

	dribble_inbounds(plyr);
}




void std_x_left1(playert *plyr)
{
	run_collision(plyr, plyr->xvel, plyr->yvel);
	
	plyr->angle += plyr->angle_vel;

	if (plyr->control)
	{
		if (frames_to_go(plyr) > (plyr->top_frame>>8))
		{
			if (current_ctrlr->ctrlr & CTRL_DRIBBLE)
			{
				plyr->last_top_anim  = plyr->top_anim;
				plyr->last_top_frame = plyr->top_frame;
				plyr->top_blend_ctr   = 0x200;
				plyr->top_blend_speed = 0x80;

				plyr->top_anim = (plyr->top_anim>0) ? TOP_STDXR1: -TOP_STDXR1;
				SetMain(std_x_left1,plyr,std_x_right1);
			}
		}
	}

	if (end_animation_test(plyr))
	{
		int r;

		r = plyr->top_rate;
		start_next_anim(plyr);
		plyr->top_rate = r;

		start_vels(plyr, 83, (SW)plyr->angle);
		SetMain(std_x_left1,plyr,std_x_left2);
	}

	dribble_inbounds(plyr);
}



void std_x_left2(playert *plyr)
{
int top_anim;

	run_collision(plyr, plyr->xvel, plyr->yvel);

	if (end_animation_test(plyr))
	{
		SetMain(std_x_left2,plyr,dribble);
		plyr->top_routine  = dribble_animate;

		plyr->leg_routine = running;
		plyr->last_speed  = TURBO_THRESHOLD+0x200;

		start_leg_anim(plyr, LEG_RUN);

		if (plyr->top_anim>0)
		{
			top_anim  = -TOP_RUN_DRIB;
			plyr->leg_frame = 0;
		}
		else
		{
			top_anim = TOP_RUN_DRIB;
			plyr->leg_frame = 20<<8;
		}
		start_anim(plyr, top_anim);
	}

	dribble_inbounds(plyr);
}












void std_x_start(playert *plyr)
{
SW a_diff;

	if (plyr->control==0)
	{
		a_diff = plyr->angle - plyr->basket_angle;
		if (a_diff<0)	std_x_left_start(plyr);
		else			std_x_right_start(plyr);
	}
	else
	{
		SetMain(std_x_start,plyr,std_wait);
		plyr->ctr2         = 3;
		jmask(CTRL_DRIBBLE);
	}
}



void std_wait(playert *plyr)
{
SW a_diff;

	plyr->ctr2--;
	if (plyr->ctr2!=0)	return;

	if (plyr->top_anim > 0)
	{
		start_blend_anim(plyr, TOP_STDXL1);
		start_vels(plyr, 38, (SW)(plyr->angle+0x1555));
		plyr->angle_vel = 0x2000/10;
	}
	else
	{
		start_blend_anim(plyr, -TOP_STDXL1);
		start_vels(plyr, 38, (SW)(plyr->angle-0x1555));
		plyr->angle_vel = -0x2000/10;
	}

	plyr->top_rate = (frames_to_go(plyr)<<8)/10;
	SetMain(std_wait,plyr,std_x_left1);
	plyr->top_routine = NULL;
}




void crossover_wait(playert *plyr)
{
//	gap = plyr->top_frame - (12<<8);

//	if ( (gap-plyr->top_rate<0) && (gap > 0))
	{
		plyr->last_top_anim   = plyr->top_anim;
		plyr->last_top_frame  = plyr->top_frame = 12<<8;
		plyr->top_anim		  = -plyr->top_anim;
		plyr->top_rate		  = 0x300;
		plyr->top_blend_ctr	  = 0x1ff; 
		plyr->top_blend_speed = (plyr->top_rate)/20;

		SetMain(crossover_wait,plyr,crossover);
	}
}



bool dribble_inbounds(playert *plyr)
{
	if (BOUNDARIES)
	{
		if (!project_flag)
		{
			if (player_inbounds_check(plyr))
			{
				assister = -1;
				FoulSet(dribble_inbounds,plyr, OUT_OF_BOUNDS);
				return TRUE;
			}
		}
	}
	else
	{
		shove_back_inbound(plyr);
	}
	return FALSE;
}







void crossover(playert *plyr)
{
	if (plyr->top_blend_ctr == 0)
	{
		SetMain(crossover,plyr,dribble);
		plyr->top_routine  = dribble_animate;
	}

	dribble_inbounds(plyr);
}



void bb_start(playert *plyr)
{
	jmask(CTRL_DRIBBLE);

	if (plyr->force>0xc0)	plyr->force=0xc0;

	if (plyr->top_anim > 0)
	{
		start_blend_anim(plyr, TOP_DRIB_BB);
		SetMain(bb_start,plyr,bb_dribble);
	}
	else
	{
		start_blend_anim(plyr, -TOP_DRIB_BB);
		SetMain(bb_start,plyr,bb_dribble_flip);
	}

	plyr->top_rate  = 0x160;
	plyr->top_routine  = NULL;
	return;
}



void spin_wait(playert *plyr)
{
SB left_flag;

	left_flag =	(threat_angle>0) ? 1:-1;

fail:

	switch (left_flag)
	{
	case 1:
		//spin left

		start_blend_anim(plyr, -TOP_DRIB360);
		plyr->top_rate  = 0x160;
		plyr->angle_vel = (0x100*plyr->top_rate)/frames_to_go(plyr);
		break;

	case -1:
		//spin right
		
		start_blend_anim(plyr, TOP_DRIB360);
		plyr->top_rate  = 0x160;
		plyr->angle_vel = -(0x100*plyr->top_rate)/frames_to_go(plyr);
		break;

	case 0:
		bb_start(plyr);
		return;
	}

	jmask(CTRL_DRIBBLE);

	special_time = lp_ctr;

	plyr->angle     -= plyr->angle_vel;
	start_vels(plyr, 80, (SW)plyr->angle);
	SetMain(spin_wait,plyr,spin_dribble);
	plyr->top_routine  = NULL;
	plyr->leg_routine  = NULL;
}






void spin_start(playert *plyr)
{
SB left_flag;

	plyr->angle2=ctrlrs[plyr->control].d_angle;

	SetMain(spin_start,plyr,spin_wait);

	plyr->ctr2 = HALF_SEC/2;


}



void standing_crossover(playert *plyr)
{
	//run_collision(plyr,plyr->xvel,plyr->yvel);
	if (blend_to_end(plyr, -TOP_STD_DRIB, LEG_RDY, 4))
	{
		SetMain(standing_crossover,plyr,dribble);
		plyr->top_routine = dribble_animate;
		plyr->leg_routine = leg_deflt;
	}
}

void standing_crossover_flip(playert *plyr)
{
	//run_collision(plyr,plyr->xvel,plyr->yvel);
	if (blend_to_end2(plyr, TOP_STD_DRIB, LEG_RDY, 4))
	{
		SetMain(standing_crossover_flip,plyr,dribble);
		plyr->top_routine = dribble_animate;
		plyr->leg_routine = leg_deflt;
	}
}


void bb_dribble(playert *plyr)
{
	current_ctrlr->force   = plyr->force;
	current_ctrlr->d_angle = plyr->d_angle;

	if (blend_to_end2(plyr, -TOP_RUN_DRIB, 0, 4))
	{
		SetMain(bb_dribble,plyr,dribble);
		plyr->top_routine = dribble_animate;
	}

	if (plyr->bumped_into)
	{
		pickup_dribble(plyr);
	}

	dribble_inbounds(plyr);
}


void bb_dribble_flip(playert *plyr)
{
	current_ctrlr->force   = plyr->force;
	current_ctrlr->d_angle = plyr->d_angle;

	if (blend_to_end2(plyr, TOP_RUN_DRIB, 0, 4))
	{
		SetMain(bb_dribble_flip,plyr,dribble);
		plyr->top_routine = dribble_animate;
	}

	if (plyr->bumped_into)
	{
		pickup_dribble(plyr);
	}

	dribble_inbounds(plyr);
}



void spin_dribble(playert *plyr)
{
int xv, yv;
	
	xv = plyr->xvel;
	yv = plyr->yvel;
	run_collision(plyr, plyr->xvel, plyr->yvel);
	plyr->xvel = xv;
	plyr->yvel = yv;

	plyr->angle += plyr->angle_vel;

	if (current_ctrlr->mctrlr & CTRL_SHOOT)
	{
		shoot(plyr,select_shot(plyr));
		return;
	}

	if (blend_to_end(plyr, ((plyr->angle_vel>0) ? TOP_RUN_DRIB:-TOP_RUN_DRIB), LEG_RUN, 4))
	{
		plyr->top_routine = dribble_animate;
		plyr->leg_routine = running;
		SetMain(spin_dribble,plyr,dribble);
		plyr->last_speed = 0xa00;
		ShowTrails=FALSE;
	}

	dribble_inbounds(plyr);
}


