
#include "generic.h"
#include "lfptypes.h"

#include "define.h"
#include "ml.h"

#include "audio.h"
#include "playgfx.h"

#include "ctrl.h"

#include "res/playerid.h"

int do_sync=1;

int play_cel;
int top_cel;
int cel_ctr;

int cels[]={
	TOP_CEL1,
	TOP_CEL2,
	TOP_CEL3,
	TOP_CEL4,
	TOP_CELB,
};

void sync_top_to_leg(playert *plyr, int tf, int lf);
int call_open(playert *plyr);


void start_celebration(playert *plyr)
{
	CallChainP(start_celebration);

	cel_ctr = 4*ONE_SEC;

#if 0
	if (plyr->plyrROM->uw___pIndx == 0xe0)	//shaq
	{
		top_cel = cels[RANDOM(8)];
	}
	else
	{
		top_cel = cels[RANDOM(4)];
	}
#else

	if (IS_PLYR_ONLY(LARRY_JOHNSON_NY))
		top_cel=TOP_CEL5;
	else if (IS_PLYR_ONLY(SHAQUILLE_ONEAL_LAL) || IS_PLYR_ONLY(NICK_ANDERSON_SAC))
		top_cel=TOP_CEL9;
	else if (IS_PLYR_ONLY(MARK_JACKSON_IND))
		top_cel=TOP_CEL8;
	else
		top_cel=cels[RANDOM(sizeof(cels)/sizeof(cels[0]))];
#endif

	play_cel = plyr->no;
}


void reset_celebration(void)
{
	play_cel = -1;
	cel_ctr  = 0;
}


void process_celebration(void)
{
	if (!cel_ctr)	return;

	if (player[play_cel].main_routine==deflt)	cel_ctr--;

	if ((cel_ctr==0)||(in_play_flag))	reset_celebration();

	if (player[play_cel].control)
	{
		ctrlrs[player[play_cel].control].ctrlr&=~CTRL_TURBO;
		ctrlrs[player[play_cel].control].mctrlr&=~CTRL_TURBO;
		if (CCTL_DOWN(CTRL_FIRE_A))
		{
			jmask(CTRL_FIRE_A);
			reset_celebration();
		}

		if (CCTL_DOWN(CTRL_FIRE_B))
		{
			jmask(CTRL_FIRE_B);
			reset_celebration();
		}
	}
}


void dribble_animate(playert *plyr)
{
int anim;

	CallChainP(dribble_animate);

	if (!project_flag)
	{
		if (possession_flag)	ball.main_routine = follow_player;
		else					BRK;
	}

	//if bringing ball down
	if (abs(plyr->top_anim) == TOP_BALL_DOWN)
	{
		if (end_animation_test(plyr))
		{
			start_blend_anim(plyr, TOP_RUN_DRIB);
		}
		return;
	}

	anim = abs(plyr->leg_anim);

	//start run?

	switch (anim)
	{
	case LEG_RUN90:
		anim = TOP_RUN_DRIB;
		break;

	case LEG_RUN180:
		anim = TOP_180TURN;
		break;

	case LEG_SPIN180:
		anim = (plyr->leg_anim>0) ? TOP_SPIN180: -TOP_SPIN180;
		plyr->top_frame = plyr->leg_frame;
		plyr->top_rate  = plyr->leg_rate;
		goto got_anim;

	case LEG_STTURBO:
	case LEG_STWALK:
	case LEG_STJOG:
	case LEG_STRUN:
	case LEG_STRUN2:

		//start run
		anim += TOP_RUN_DRIB - LEG_STRUN;
		break;

	case LEG_TURBO:
		plyr->top_rate = plyr->leg_rate;
		anim = TOP_TURBO_DRIB;
		sync_top_to_leg(plyr,1,1);

		break;

	case LEG_WALK:
		plyr->top_rate = plyr->leg_rate/2;
		anim = TOP_WALK_DRIB;
		sync_top_to_leg(plyr,1,2);
		break;

	case LEG_JOG:
	case LEG_RUN:
	case LEG_RUN2:
			if (plyr->leg_rate < 0x140)			plyr->top_rate = 0x140;
			else if (plyr->leg_rate > 0x1c0)	plyr->top_rate = 0x1c0;
			else								plyr->top_rate = plyr->leg_rate;
			anim += TOP_RUN_DRIB - LEG_RUN;
			sync_top_to_leg(plyr,1,1);
			break;

	default:
		if ((anim >= LEG_RDY) & (anim <= LEG_SKIPN_END))
		{
			plyr->top_rate = 0x180;
			if (current_ctrlr->ctrlr & CTRL_SHUFFLE)
			{
				SW a_diff;

				anim = TOP_PRT_DRIB;

				a_diff = plyr->shuffle_target_angle-plyr->angle;
				if (abs(a_diff)<0x1000)
				{
					if ((lp_ctr&0x20) == 0)
					{
						//anim+=1;
					}
				}

			}
			else
			{
				anim = TOP_STD_DRIB;
			}
		}
		break;

	}

	if (abs(plyr->top_anim)==TOP_SPIN180)
	{
		anim = -anim;
	}

	if (plyr->top_anim < 0)
	{
		anim = -anim;
	}


	if ((anim == TOP_RUN_DRIB) || (anim == TOP_RUN_DRIB2))
	{
		//plyr->top_frame = plyr->leg_frame;
		//anim = TOP_RUN;
		if (plyr->plyrROM->ub___pAttrDribble > 0x47)
		{
			anim = TOP_LOW_DRIB;
		}
		else if (plyr->plyrROM->ub___pAttrDribble < 0x39)
		{
			anim = TOP_HIGH_DRIB;
		}
	}

got_anim:

	//pick calling
	if ((pick_ctr) && (abs(anim) < TOP_STD_DRIBC) && plyr->pick_arm)
	{
		if (anim < 0)  anim -= TOP_STD_DRIBC-TOP_STD_DRIB;
		else		   anim += TOP_STD_DRIBC-TOP_STD_DRIB;
	}

	if ((possession_ticks > 5) || (plyr->top_blend_ctr == 0))
	{
		if (anim != plyr->top_anim)
		{
			plyr->last_top_anim  = plyr->top_anim;
			plyr->last_top_frame = plyr->top_frame;
			plyr->top_anim       = anim;

			switch (abs(anim))
			{
			case TOP_WALK_DRIB:
			case TOP_WALK_DRIBC:
				plyr->top_frame	= plyr->top_frame/2;
				break;

			case TOP_DRIB180:
				plyr->top_frame = 0;
				break;
			}

			if (plyr->top_blend_ctr == 0)
			{
				plyr->top_blend_speed= 0x40;
				plyr->top_blend_ctr  = 0x200;
			}
		}
	}
}



SW	sshuf_tops_tbl[4]= { TOP_GUARD1, TOP_GUARD2, TOP_GUARD3, TOP_GUARD4 };

SW	sshu1_tops[16]=
		{
		-TOP_SSHU1S_000,	//	LEG_SKIPS_LP,
		-TOP_SSHU1S_022,	//	LEG_SKIPSSE_LP,
		-TOP_SSHU1S_045,	//	LEG_SKIPSE_LP,
		-TOP_SSHU1S_067,	//	LEG_SKIPESE_LP,
		-TOP_SSHU1S_090,	//	LEG_SKIPE_LP,
		-TOP_SSHU1S_112,	//	LEG_SKIPENE_LP,
		-TOP_SSHU1S_135,	//	LEG_SKIPNE_LP,
		-TOP_SSHU1S_157,	//	LEG_SKIPNNE_LP,
		-TOP_SSHU1S_180,	//	LEG_SKIPN_LP,
		TOP_SSHU1S_157,	//	LEG_SKIPENE_LP,
		TOP_SSHU1S_135,	//	LEG_SKIPNE_LP,
		TOP_SSHU1S_112,	//	LEG_SKIPNNE_LP,
		TOP_SSHU1S_090,	//	LEG_SKIPE_LP,
		TOP_SSHU1S_067,	//	LEG_SKIPSSE_LP,
		TOP_SSHU1S_045,	//	LEG_SKIPSE_LP,
		TOP_SSHU1S_022,	//	LEG_SKIPESE_LP,
		};

SW	sshu2_tops[16]=
		{
		-TOP_SSHU2S_000,	//	LEG_SKIPS_LP,
		-TOP_SSHU2S_022,	//	LEG_SKIPSSE_LP,
		-TOP_SSHU2S_045,	//	LEG_SKIPSE_LP,
		-TOP_SSHU2S_067,	//	LEG_SKIPESE_LP,
		-TOP_SSHU2S_090,	//	LEG_SKIPE_LP,
		-TOP_SSHU2S_112,	//	LEG_SKIPENE_LP,
		-TOP_SSHU2S_135,	//	LEG_SKIPNE_LP,
		-TOP_SSHU2S_157,	//	LEG_SKIPNNE_LP,
		-TOP_SSHU2S_180,	//	LEG_SKIPN_LP,
		TOP_SSHU2S_157,	//	LEG_SKIPENE_LP,
		TOP_SSHU2S_135,	//	LEG_SKIPNE_LP,
		TOP_SSHU2S_112,	//	LEG_SKIPNNE_LP,
		TOP_SSHU2S_090,	//	LEG_SKIPE_LP,
		TOP_SSHU2S_067,	//	LEG_SKIPSSE_LP,
		TOP_SSHU2S_045,	//	LEG_SKIPSE_LP,
		TOP_SSHU2S_022,	//	LEG_SKIPESE_LP,
		};

SW	sshu3_tops[16]=
		{
		-TOP_SSHU3S_000,	//	LEG_SKIPS_LP,
		-TOP_SSHU3S_022,	//	LEG_SKIPSSE_LP,
		-TOP_SSHU3S_045,	//	LEG_SKIPSE_LP,
		-TOP_SSHU3S_067,	//	LEG_SKIPESE_LP,
		-TOP_SSHU3S_090,	//	LEG_SKIPE_LP,
		-TOP_SSHU3S_112,	//	LEG_SKIPENE_LP,
		-TOP_SSHU3S_135,	//	LEG_SKIPNE_LP,
		-TOP_SSHU3S_157,	//	LEG_SKIPNNE_LP,
		-TOP_SSHU3S_180,	//	LEG_SKIPN_LP,
		TOP_SSHU3S_157,	//	LEG_SKIPENE_LP,
		TOP_SSHU3S_135,	//	LEG_SKIPNE_LP,
		TOP_SSHU3S_112,	//	LEG_SKIPNNE_LP,
		TOP_SSHU3S_090,	//	LEG_SKIPE_LP,
		TOP_SSHU3S_067,	//	LEG_SKIPSSE_LP,
		TOP_SSHU3S_045,	//	LEG_SKIPSE_LP,
		TOP_SSHU3S_022,	//	LEG_SKIPESE_LP,
		};

SW	shuf1_tops[16]=
		{
		-TOP_SHUF1_000,	//	LEG_SKIPS_LP,
		-TOP_SHUF1_022,	//	LEG_SKIPSSE_LP,
		-TOP_SHUF1_045,	//	LEG_SKIPSE_LP,
		-TOP_SHUF1_067,	//	LEG_SKIPESE_LP,
		-TOP_SHUF1_090,	//	LEG_SKIPE_LP,
		-TOP_SHUF1_112,	//	LEG_SKIPENE_LP,
		-TOP_SHUF1_135,	//	LEG_SKIPNE_LP,
		-TOP_SHUF1_157,	//	LEG_SKIPNNE_LP,
		-TOP_SHUF1_180,	//	LEG_SKIPN_LP,
		TOP_SHUF1_157,	//	LEG_SKIPENE_LP,
		TOP_SHUF1_135,	//	LEG_SKIPNE_LP,
		TOP_SHUF1_112,	//	LEG_SKIPNNE_LP,
		TOP_SHUF1_090,	//	LEG_SKIPE_LP,
		TOP_SHUF1_067,	//	LEG_SKIPSSE_LP,
		TOP_SHUF1_045,	//	LEG_SKIPSE_LP,
		TOP_SHUF1_022,	//	LEG_SKIPESE_LP,
		};

SW	shuf2_tops[16]=
		{
		-TOP_SHUF2_000,	//	LEG_SKIPS_LP,
		-TOP_SHUF2_022,	//	LEG_SKIPSSE_LP,
		-TOP_SHUF2_045,	//	LEG_SKIPSE_LP,
		-TOP_SHUF2_067,	//	LEG_SKIPESE_LP,
		-TOP_SHUF2_090,	//	LEG_SKIPE_LP,
		-TOP_SHUF2_112,	//	LEG_SKIPENE_LP,
		-TOP_SHUF2_135,	//	LEG_SKIPNE_LP,
		-TOP_SHUF2_157,	//	LEG_SKIPNNE_LP,
		-TOP_SHUF2_180,	//	LEG_SKIPN_LP,
		TOP_SHUF2_157,	//	LEG_SKIPENE_LP,
		TOP_SHUF2_135,	//	LEG_SKIPNE_LP,
		TOP_SHUF2_112,	//	LEG_SKIPNNE_LP,
		TOP_SHUF2_090,	//	LEG_SKIPE_LP,
		TOP_SHUF2_067,	//	LEG_SKIPSSE_LP,
		TOP_SHUF2_045,	//	LEG_SKIPSE_LP,
		TOP_SHUF2_022,	//	LEG_SKIPESE_LP,
		};

SW	shuf3_tops[16]=
		{
		-TOP_SHUF3_000,	//	LEG_SKIPS_LP,
		-TOP_SHUF3_022,	//	LEG_SKIPSSE_LP,
		-TOP_SHUF3_045,	//	LEG_SKIPSE_LP,
		-TOP_SHUF3_067,	//	LEG_SKIPESE_LP,
		-TOP_SHUF3_090,	//	LEG_SKIPE_LP,
		-TOP_SHUF3_112,	//	LEG_SKIPENE_LP,
		-TOP_SHUF3_135,	//	LEG_SKIPNE_LP,
		-TOP_SHUF3_157,	//	LEG_SKIPNNE_LP,
		-TOP_SHUF3_180,	//	LEG_SKIPN_LP,
		TOP_SHUF3_157,	//	LEG_SKIPENE_LP,
		TOP_SHUF3_135,	//	LEG_SKIPNE_LP,
		TOP_SHUF3_112,	//	LEG_SKIPNNE_LP,
		TOP_SHUF3_090,	//	LEG_SKIPE_LP,
		TOP_SHUF3_067,	//	LEG_SKIPSSE_LP,
		TOP_SHUF3_045,	//	LEG_SKIPSE_LP,
		TOP_SHUF3_022,	//	LEG_SKIPESE_LP,
		};

SW	*shuf_tops_tbl[3][3]=
		{
			{ sshu1_tops, shuf1_tops, sshu1_tops },
			{ sshu2_tops, shuf2_tops, sshu2_tops },
			{ sshu3_tops, shuf3_tops, sshu3_tops },
		};

SW	guard_tops_tbl[4]=
		{
		TOP_GUARD1,
		TOP_GUARD2,
		TOP_GUARD3,
		TOP_GUARD4
		};

SW	offense_tops_tbl[16]=
	{
	-TOP_SHUR000,	//	LEG_SKIPS_LP,
	-TOP_SHUR045,	//	LEG_SKIPSSE_LP,
	-TOP_SHUR045,	//	LEG_SKIPSE_LP,
	-TOP_SHUR045,	//	LEG_SKIPESE_LP,
	-TOP_SHUR090,	//	LEG_SKIPE_LP,
	-TOP_SHUR135,	//	LEG_SKIPENE_LP,
	-TOP_SHUR135,	//	LEG_SKIPNE_LP,
	-TOP_SHUR135,	//	LEG_SKIPNNE_LP,
	-TOP_SHUR180,	//	LEG_SKIPN_LP,
	TOP_SHUR135,	//	LEG_SKIPENE_LP,
	TOP_SHUR135,	//	LEG_SKIPNE_LP,
	TOP_SHUR135,	//	LEG_SKIPNNE_LP,
	TOP_SHUR090,	//	LEG_SKIPE_LP,
	TOP_SHUR045,	//	LEG_SKIPSSE_LP,
	TOP_SHUR045,	//	LEG_SKIPSE_LP,
	TOP_SHUR045,	//	LEG_SKIPESE_LP,
	};

//SW	*shuf_tops_tbls[]={ shuf1_tops_tbl, shuf2_tops_tbl, shuf3_tops_tbl };
//SW	*sshu_tops_tbls[]={ sshu1_tops_tbl, sshu2_tops_tbl, sshu3_tops_tbl };

#define GUARD_CONE	(16384+8094)
#define	GUARD_DIST	(0x100000)
int is_boxing(playert *plyr)
{
	SW	angle;
	int	dx,dy;

	dx=player[plyr->markee].xco-plyr->xco;
	dy=player[plyr->markee].yco-plyr->yco;
	angle=LFPAtn(dx,dy);
	angle=plyr->shuffle_target_angle-angle;

	if (angle>GUARD_CONE || angle<-GUARD_CONE)
		if (MANHATTAN(dx,dy)<GUARD_DIST)
			return 1;

	return 0;
}


#define	FLIP_CONE	1024

#if 1
int flip_for_angle(playert *plyr, int an)
{
	int	anim;

	if (plyr->top_anim*an>0)
		anim=an;
	else
		anim=-an;

	if ((!(plyr->xvel||plyr->yvel)) || plyr->shuffle_angle==0 || plyr->shuffle_angle==8)
	{
		return anim;
	}

	return -an;
}
#else
int flip_for_angle(playert *plyr, int an)
{
	int	anim;

	if (plyr->top_anim>0)
		anim=an;
	else
		anim=-an;

	if (plyr->xvel||plyr->yvel)
	{
		if (plyr->shuffle_angle)
			return -an;
		else
			return an;
	}

	return anim;
}
#endif

#define	BS (0x40+0x20)

void top_deflt(playert *plyr)
{
SW top_anim;
SW leg_anim;

	CallChainP(top_deflt);

	if ((plyr->ikBlend) && (plyr->ikBlendSpeed==0))
	{
		plyr->ikBlendSpeed=-0x40;
	}


	if (plyr->no == int_recvr)
	{
		if (ball.main_routine == pass_ball_move)
		{
			return;
		}
	}

	if (plyr->leg_routine == NULL)
	{
		switch(plyr->force>>4)
		{
			case 0:
			break;

			default:
			if (plyr->main_routine != turn180)
			{
				plyr->leg_routine  = leg_deflt;
				start_blend_leg_anim(plyr, LEG_RDY);
				start_blend_anim(plyr, TOP_RDY);
				break;
			}
		}
	}

	//in place ?
	if (plyr->force == 0)
	{
		if (free_throws)
		{
			start_blend_anim(plyr, TOP_TIRED);
			return;
		}

		if (ATTACKING)
		{
			if (MANHATTAN(plyr->target_x-plyr->xco, plyr->target_y-plyr->yco) < 0x100000)
			{
				if ((plyr->last_speed==0) && (plyr->leg_routine == leg_deflt))
				{

					if (plyr->ball_dist>0x500000)
					{
						int a;

						if (a=call_open(plyr))
							a-=TOP_RDY;
						else
							a = (plyr->no % 4);
						start_blend_anim(plyr, TOP_RDY+a);
						return;
					}
				}


				if (plyr->top_blend_ctr == 0)
				{
					switch (abs(plyr->top_anim))
					{
					case TOP_CATCH_HP1:

						if (!project_flag)
						{
							//must be int_recvr and ball in possession, or on it's way to me to keep hand up
							if (!((plyr->no == int_recvr) & ((possession_flag) || (ball.main_routine == pass_ball_move))))   //|| (dunk_flag))
							{
								start_blend_anim(plyr, TOP_RDY);
								start_blend_leg_anim(plyr, LEG_RDY);
								plyr->leg_routine = leg_deflt;
								break;
							}
						}

						if (plyr->ball_dist < 0x480000)
						{
							start_blend_anim(plyr, (plyr->top_anim>0) ? TOP_CATCH_LP1: -TOP_CATCH_LP1);
							plyr->top_frame = (frames_to_go(plyr)-1) << 8;
						}
						break;

					case TOP_CATCH_LP1:

						if (!project_flag)
						{
							//must be int_recvr and ball in possession, or on it's way to me to keep hand up
							if (!((plyr->no == int_recvr) & ((possession_flag) | (ball.main_routine == pass_ball_move))))	// || (dunk_flag))
							{
								start_blend_anim(plyr, TOP_RDY);
								start_blend_leg_anim(plyr, LEG_RDY);
								plyr->leg_routine = leg_deflt;
								break;
							}
						}

						if (plyr->ball_dist > 0x580000)
						{
							start_blend_anim(plyr, (plyr->top_anim>0) ? TOP_CATCH_HP1: -TOP_CATCH_HP1);
							plyr->top_frame = (frames_to_go(plyr)-1) << 8;
						}
						break;


					case TOP_RDY:

						if ((plyr->basket_dist < 0x400000) &&
							(plyr->no == int_recvr) &&
							(possession_flag) &&
							(possession_ticks > 2) &&
							(!dunk_flag) )
						{
							SW anim;

							anim = (plyr->ball_dist > 0x500000) ? TOP_CATCH_HP1:TOP_CATCH_LP1;

							//catch ball away from basket
							if ((plyr->angle - plyr->basket_angle) & 0x8000)	start_anim(plyr, anim);
							else												start_anim(plyr, -anim);
						}
						else
						{
							int	an;

							if (an=call_open(plyr))
								start_blend_anim(plyr,an);
						}

						break;

					case TOP_CATCH_FRONT:
					case TOP_CATCH_OH:
					case TOP_STOLEN:
						break;


					default:
						{
							int	an;

							if (an=call_open(plyr))
								start_blend_anim(plyr,an);
							else
								start_blend_anim(plyr, TOP_RDY);
						}
						if (plyr->leg_anim == 0)
						{
							start_blend_leg_anim(plyr, LEG_RDY);
							plyr->leg_routine = leg_deflt;
						}
						break;

					}
				}

				return;
			}
		}
	}


	leg_anim = abs(plyr->leg_anim);


	if (ATTACKING)
		top_anim = TOP_RDY;
	else
	{
		top_anim = TOP_GRD;

		if ((plyr->shuffle) || (plyr->last_speed == 0))
		{
			if ((plyr->markee == ball_handler) && (possession_flag))
			{
				if (bhp->basket_dist < 0xa00000)
				{
					if (plyr->last_speed == 0)
					{
						if (plyr->ball_dist < 0x280000)
						{
							if (ball.zco > (0x1800*bhp->scale))
							{
								switch (abs(plyr->top_anim))
								{
									case TOP_LOW_TO_HI:
									if (!end_animation_test(plyr))	return;
									start_blend_anim(plyr, TOP_GRDH);
									return;

									case TOP_GRDH:
									plyr->top_rate = 0x40;
									return;

									case TOP_LP_GRD:
									if (shoot_stage == 1)	break;

									default:
									start_blend_anim(plyr, TOP_LOW_TO_HI);
									return;
								}
							}
						}
					}
				}

				if (DEFENDING)
				{
					if ((plyr->control) && (plyr->shuffle))
					{
						if (covered2(bhp, plyr) == 2)
						{
							SW a_diff;

							a_diff = bhp->angle-bhp->basket_angle;

							if (abs(a_diff)>0x4000)
							{
								top_anim = TOP_LP_GRD;
								goto got_top;
							}
						}
					}
					else
					{
						if (plyr->hand_check_flag)
						{
							top_anim = TOP_LP_GRD;
							goto got_top;
						}
					}
				}
			}
		}
	}



	if (plyr->shuffle)
	{
		if (ATTACKING)
		{
			if (!(top_anim=call_open(plyr)))
				top_anim=offense_tops_tbl[plyr->shuffle_angle];
		}
		else	// Defenders
		{

			if (plyr->leg_routine==shuffle_stand)
			{
				if (plyr->markee==ball_handler)
					top_anim=flip_for_angle(plyr,sshuf_tops_tbl[2]);
				else
					top_anim=sshuf_tops_tbl[is_boxing(plyr)];
			}

			else
			{
				if (leg_anim==LEG_BACK)
					top_anim=TOP_BACK;
				else if ((leg_anim >= LEG_SKIPS_ST) && (leg_anim <= LEG_SKIPN_END))
				{
					if (plyr->markee==ball_handler)
						top_anim=flip_for_angle(plyr,shuf_tops_tbl[2][plyr->shuffle_stage][plyr->shuffle_angle]);
					else
						top_anim=(shuf_tops_tbl[is_boxing(plyr)][plyr->shuffle_stage])[plyr->shuffle_angle];
				}
				else
				{
					if (plyr->markee==ball_handler)
						top_anim=flip_for_angle(plyr,guard_tops_tbl[2]);
					else
						top_anim=guard_tops_tbl[is_boxing(plyr)];
				}
			}
		}

		start_blend_anim(plyr, top_anim);
		plyr->top_blend_speed = BS;

		if (plyr->shuffle_stage == 1 || plyr->leg_routine==shuffle_stand)
		{
			plyr->top_rate  = plyr->leg_rate;
			plyr->top_frame = plyr->leg_frame;
		}
		else
		{
			plyr->top_frame = 0;
		}

		return;
	}


	if (leg_anim <= LEG_BACK)
	{
		if (!(top_anim=call_open(plyr)))
		{
			top_anim = leg_anim + TOP_JOG-LEG_JOG;
			plyr->top_rate = plyr->leg_rate;

			/*
			//calling for pass
			if (plyr->no == play_pass)
			{
				SW a_diff;

				top_anim = leg_anim+TOP_JOG_CALL-LEG_JOG;

				a_diff = plyr->ball_angle - plyr->angle;
				if (a_diff<0)	top_anim = -top_anim;
			}
			*/

			if (plyr->no == play_cel)
			{
				top_anim = top_cel;
			}

			if (abs(plyr->top_anim) == top_anim)
			{
				if (leg_anim==LEG_BACK)
					plyr->top_rate=plyr->leg_rate*1/4;
				else
					plyr->top_frame = plyr->leg_frame;
			}
		}
	}
	else if (leg_anim == LEG_RUN90)
	{
		top_anim = (plyr->leg_anim > 0) ? TOP_RUN90: -TOP_RUN90;
		plyr->top_frame = plyr->leg_frame;
		plyr->top_rate  = plyr->leg_rate;

	}
	else if (leg_anim == LEG_RUN180)
	{
		top_anim = (plyr->leg_anim > 0) ? TOP_RUN180: -TOP_RUN180;
		plyr->top_frame = plyr->leg_frame;
		plyr->top_rate = plyr->leg_rate;
	}

got_top:
	if (top_anim != plyr->top_anim)
	{
		plyr->last_top_anim  = plyr->top_anim;
		plyr->last_top_frame = plyr->top_frame;
		plyr->top_anim       = top_anim;

		//if not blending already set up
		if (plyr->top_blend_ctr==0)
		{
			plyr->top_blend_speed= 0x40;
			plyr->top_blend_ctr  = 0xff;
		}

	}

	if (leg_anim == LEG_RDY)
	{
		plyr->top_rate = 0x100;
		plyr->leg_rate = 0x100;
		//plyr->top_frame = plyr->leg_frame;
	}
}



/* *************************************************************************/
/* head_turn(playert *plyr)*/
/* *************************************************************************/
/* Inputs   - 	ptr to plyr data*/
/* *************************************************************************/

void head_turn(playert *plyr)
{
SW new_twist;
SW new_tilt;
SW head_angle;
SW diff;
int z_diff;
int xy_diff;

	CallChainP(head_turn);

//look at ball if

	new_tilt  =
	new_twist = 0;

	if (plyr->look_at_ball_ctr)
	{
		if (!BALL_HANDLER)
		{
			plyr->look_at_ball_ctr--;

			new_twist = LFPAtn(ball.xco - plyr->xco, ball.yco - plyr->yco) - plyr->angle;
			if (new_twist >= 0x1f00)	new_twist = 0x1f00;
			if (new_twist < -0x1f00)	new_twist = -0x1f00;

			xy_diff = MANHATTAN(ball.xco - plyr->xco, ball.yco - plyr->yco);
			z_diff  = ball.zco - (plyr->scale*1770*4) - plyr->zco;

			//don't look down
			if (z_diff > 0)
			{
				new_tilt = LFPAtn(xy_diff, z_diff);
				if (new_tilt > 0x1000)	new_tilt = 0x1000;
				if (new_tilt < -0x1000)	new_tilt = -0x1000;
			}
		}
		else
		{
			plyr->look_at_ball_ctr--;
			new_twist = plyr->basket_angle-plyr->angle;
		}
	}


	/*
	//move head towards target
	head_angle = plyr->head_twist;
	diff	   = new_twist - head_angle;
	if (diff > 0x800)		head_angle += 0x800;
	else if (diff < -0x800)	head_angle -= 0x800;
	else					head_angle = new_twist;
	plyr->head_twist = head_angle;

	//move head towards target
	head_angle = plyr->head_tilt;
	diff	   = new_tilt - head_angle;
	if (diff > 0x800)		head_angle += 0x800;
	else if (diff < -0x800)	head_angle -= 0x800;
	else					head_angle = new_tilt;
	plyr->head_tilt = head_angle;
	*/

}






/* incidental animations for stood still when in position */

void top_in_place(playert *plyr)
{
	CallChainP(top_in_place);

	//check to make sure I'm still in place
	//dist   = MANHATTAN(plyr->target_x - plyr->xco, plyr->target_y - plyr->yco) >= SHUFFLE_STOP_ZONE;
	//a_diff =

	if (plyr->no == int_recvr)
	{
		if ((ball.main_routine == pass_ball_move) || (project_flag))
		{
			return;
		}
	}


	switch(plyr->force>>4)
	{

	case 0:
		break;

	case 1:
		//don't turn for ball being passed
		if ((plyr->leg_routine == NULL) && (possession_flag))
		{
			pivot(plyr, (SW)(current_ctrlr->d_angle - plyr->angle));
		}
		break;

	default:


		plyr->leg_routine  = leg_deflt;
		plyr->top_routine  = top_deflt;

		start_blend_leg_anim(plyr, LEG_RDY);
		start_blend_anim(plyr, TOP_RDY);
		break;
	}



	if (plyr->top_blend_ctr == 0)
	{
		switch (abs(plyr->top_anim))
		{
		case TOP_CATCH_HP1:
			//must be int_recvr and ball in possession, or on it's way to me to keep hand up
			if (!((plyr->no == int_recvr) & ((possession_flag) | (ball.main_routine == pass_ball_move))))   //|| (dunk_flag))
			{
				start_blend_anim(plyr, TOP_RDY);
				start_blend_leg_anim(plyr, LEG_RDY);
				plyr->leg_routine = leg_deflt;
				break;
			}

			if (plyr->ball_dist < 0x500000)
			{
				start_blend_anim(plyr, (plyr->top_anim>0) ? TOP_CATCH_LP1: -TOP_CATCH_LP1);
				plyr->top_frame = (frames_to_go(plyr)-1) << 8;
			}
			break;

		case TOP_CATCH_LP1:

			//must be int_recvr and ball in possession, or on it's way to me to keep hand up
			if (!((plyr->no == int_recvr) & ((possession_flag) | (ball.main_routine == pass_ball_move))))	// || (dunk_flag))
			{
				start_blend_anim(plyr, TOP_RDY);
				start_blend_leg_anim(plyr, LEG_RDY);
				plyr->leg_routine = leg_deflt;
				break;
			}

			if (plyr->ball_dist > 0x600000)
			{
				start_blend_anim(plyr, (plyr->top_anim>0) ? TOP_CATCH_HP1: -TOP_CATCH_HP1);
				plyr->top_frame = (frames_to_go(plyr)-1) << 8;
			}
			break;


		case TOP_RDY:

			if ((plyr->basket_dist < 0x400000) &&
				(plyr->no == int_recvr) &&
				(possession_flag) &&
				(possession_ticks > 2) &&
				(!dunk_flag) )
			{
				SW anim;

				anim = (plyr->ball_dist > 0x580000) ? TOP_CATCH_HP1:TOP_CATCH_LP1;

				//catch ball away from basket
				if ((plyr->angle - plyr->basket_angle) & 0x8000)	start_anim(plyr, anim);
				else												start_anim(plyr, -anim);
			}

		case TOP_CATCH_FRONT:
		case TOP_CATCH_OH:
			break;


		default:
			start_blend_anim(plyr, TOP_RDY);
			if (plyr->leg_anim == 0)	start_blend_leg_anim(plyr, LEG_RDY);
			plyr->leg_routine = leg_deflt;
			break;

		}
	}
}



void sync_top_to_leg
		(
		playert	*plyr,			// Player to synchronize
		int		lf,				// Frequency of top loops in a full cycle
		int		tf				// Frequency of bottom loops in a full cycle
		)
{
	int	period;		// Framecount of the animation loop

	int	tphase;		// Which period of the master period the top animation is in
	int	tperiod;	// Size of the top period
	int	tpos;		// Position inside the current top period

	int	lphase;
	int	lperiod;
	int	lpos;

	int	shift;		// Phase shift between the top and legs
	int	frame;		// Resulting frame

	if (!do_sync)
		return;

	// Initialize variables
	frame=plyr->top_frame;
	period=playerAnims[abs(plyr->top_anim)].a->numFrames*256;

	tperiod=period/tf;
	lperiod=period/lf;

	tphase=plyr->top_frame/tperiod;
	tpos=(plyr->top_frame-tphase*tperiod)*tf;

	lphase=plyr->leg_frame/lperiod;
	lpos=(plyr->leg_frame-lphase*lperiod)*lf;

	// Determine phase shift between top and bottom
	shift=tpos-lpos;
	if (tpos>lpos)
	{
		if (shift>period/2)
			shift=shift-period;
	}
	else	// tpos less than lpos
	{
		if (shift<-period/2)
			shift=shift+period;
	}

	// Offset animation
	frame-=shift/8;
	if (frame<0)
		frame+=period;
	else if (frame>=period)
		frame-=period;
	plyr->top_frame=frame;
}

#define	OPEN_SCORE	0xC00000
#define	OPEN_DIST	0x140000

int call_open(playert *plyr)
{
	int	an;
	int	ls,hs;

	if (C3PT || PracticeMode)		// Don't call for pass during 3pt contest or practice
		return 0;

	if (!bhp->control)
		return 0;

	if (!ICL_ROOKIE(&ctrlrs[bhp->control]))
		return 0;

	if (PLY(plyr)<0)
		return 0;

	if (PLY(bhp)<0)
		return 0;

	get_free_pass_scores2(plyr,&ls,&hs);

	if (pass_scores[plyr->no-plyr->base]<OPEN_SCORE)
		if (MANHATTAN(plyr->xco-player[plyr->marker].xco,plyr->yco-player[plyr->marker].yco)>OPEN_DIST)
		{
			switch(abs(plyr->leg_anim))
			{
#if 0
				case LEG_RDY:
					if (abs(plyr->top_anim)>=TOP_ST2H_CALL && abs(plyr->top_anim)<=TOP_TURBO_CALL)
						an=plyr->top_anim;
					else
						an=(ReadRnd250()&0x10)? TOP_ST2H_CALL : TOP_ST1H_CALL;
					break;
#endif
				case LEG_STWALK:
				case LEG_WALK:
					an=TOP_WALK_CALL;
					break;

				case LEG_STJOG:
				case LEG_JOG:
					an=TOP_JOG_CALL;
					break;

				case LEG_STRUN:
				case LEG_RUN:
					an=TOP_RUN_CALL;

				default:
#if 0
					an=TOP_ST1H_CALL;
#else
					an=TOP_WALK_CALL;
#endif
					break;
			}

			return an;
		}

	return 0;
}
