
#include "generic.h"
#include "lfptypes.h"

#include "define.h"
#include "ml.h"

#include "audio.h"
#include "playgfx.h"

#include "ctrl.h"


#include "gamefx.h"

extern u8
	*GameBank;


global signed char pivot_flag;			//
global signed char pivot_flag2;


global int last_squeek_ctr;		//lp_ctr val last squeek was played
global int last_grunt_ctr;		//lp_ctr val last time grunt fx was played (to stop playing agin too soon)
static int last_grunt;			//which grunt fx was played last (to stop being played again immediately afterwards)

static int coast(playert *plyr, SI speed);


#define SQUEEK_VOL(_v) (_v*2/3)
#define GRUNT_VOL(_v) (_v/2)


void do_squeek(void)
{
	u32
		aid;
int r;

	if (project_flag)	return;

	if (lp_ctr>last_squeek_ctr)
	{
		r = ReadRnd250();

		last_squeek_ctr = lp_ctr+(r/8)+ONE_SEC/2;

		if (r&1)	r +=128;


//old		AudioPlaySfx(SFX_ID_SQUEEK0+(r&1), 25050+((r-128)<<5), SQUEEK_VOL, 7);
		aid = SFX_SQUEEK1 + random( 3 );
		Audio_SfxPlay( GameBank, aid, SQUEEK_VOL( SfxVolume ) );
		Replay_SaveSfx( aid, SQUEEK_VOL( AUDIO_MAX_VOLUME ) );
	}
}


void do_grunt(void)
{
SI grunt_no;

	if (lp_ctr > last_grunt_ctr)
	{
		last_grunt_ctr = lp_ctr+HALF_SEC;

		do{
			grunt_no       = (ReadRnd250()*3)>>8;
		}while(grunt_no != last_grunt);

		last_grunt = grunt_no;

//old		AudioPlaySfx(SFX_ID_GRUNT6 + grunt_no, 22050, GRUNT_VOL, 7);
		Audio_SfxPlay( GameBank, SFX_GRUNT1, GRUNT_VOL( SfxVolume ) );
	}
}


void turn_and_burn(playert *plyr)
{
int time;
SW  diff;

	CallChainP(turn_and_burn);

	plyr->last_speed  = JOG_THRESHOLD;

	if ((current_ctrlr->force <= 0) || ((plyr->no == ball_handler) && (pass_flag)))
	{
		plyr->leg_routine = leg_deflt;
		start_blend_leg_anim(plyr, LEG_RDY);
		return;
	}



	switch(plyr->leg_anim)
	{
	case LEG_RUN90:
	case -LEG_RUN90:
	case LEG_SPIN180:
	case -LEG_SPIN180:
		break;

	default:
		set_default(plyr);
		return;
	}


	diff = plyr->angle2 - plyr->angle;

	if (plyr->leg_rate)
	{
		time = ((leg_frames_to_go(plyr)-1)<<8)/plyr->leg_rate;

//		plyr->xco+=plyr->xvel;
//		plyr->yco+=plyr->yvel;

		run_collision(plyr, plyr->xvel, plyr->yvel);

		if (time)
		{
			plyr->angle += diff/time;
		}
		else
		{
			plyr->leg_routine = running;

			switch(plyr->leg_anim)
			{
			case LEG_RUN90:
				start_leg_anim(plyr, LEG_RUN);
				plyr->leg_frame = 0x200;
				break;

			case -LEG_RUN90:
				start_leg_anim(plyr, LEG_RUN);
				plyr->leg_frame = 0x1600;
				break;

			case LEG_SPIN180:
				plyr->last_speed  = TURBO_THRESHOLD+0x200;
				start_leg_anim(plyr, LEG_TURBO);
				plyr->leg_frame = (15<<8);
				break;

			case -LEG_SPIN180:
				plyr->last_speed  = TURBO_THRESHOLD+0x200;
				start_leg_anim(plyr, LEG_TURBO);
				plyr->leg_frame = (35<<8);
				break;


			
			}
		}
	}
	else
	{
		if (plyr->pick_ctr)
			plyr->pick_ctr--;
		else
			plyr->leg_rate = 0x6000/0x20;
	}
}


SW spin_test(playert *plyr1, playert *plyr2)
{
int pd;
			  
	if (plyr2->control)
	{
		return (plyr2->hand_check_flag);
	}
	else
	{
		pd =  plyr1->plyrROM->ub___pAttrStrength
			- plyr2->plyrROM->ub___pAttrStrength
			+ (plyr1->plyrROM->ub___pAttrSpeed - plyr2->plyrROM->ub___pAttrSpeed)>>2;
			+ ((plyr1->plyrRAM->sw___pEnergy - plyr2->plyrRAM->sw___pEnergy) >> 8);

		if (DIFFICULTY==0)
		{
			if ((plyr1->control!=0) && (plyr2->control==0))
			{
				pd -= 0x40;
			}
		}

		return (ReadRnd250() > (pd+0x80));
	}

//	return 1;
}



void leg_deflt(playert *plyr)
{
SW diff;
bool last_shuf_flag;

	CallChainP(leg_deflt);

	last_shuf_flag   = plyr->shuffle;
	plyr->zco        =						
	plyr->last_speed = 0;
	plyr->shuffle    = FALSE;

	plyr->zco = 0;

	//anyone pushing me
	push_back(plyr,plyr->bumped_into_me);
	if (shuffle_stand_test(plyr))
		return;

	if ((current_ctrlr->force) || (current_ctrlr->ctrlr & CTRL_SHUFFLE))
	{
		if (!(current_ctrlr->ctrlr & CTRL_SHUFFLE))
		{
			plyr->angle	= plyr->angle;
			diff    = current_ctrlr->d_angle - plyr->angle;

			//facing right way and force = 1
			if ((diff == 0) & (current_ctrlr->force <= 0x10))	goto deflt;

			//if difference between joystick and plyr->angle is small, start running
			if ( (abs(diff) < 0x1000) & (current_ctrlr->force > 0x10))
			{
				plyr->angle       = current_ctrlr->d_angle;
				plyr->leg_routine = running;
				running(plyr);
				return;
			}
			else
			{
				if ((current_ctrlr->force > 0x50)
					//&& (plyr->control != 0)
					&& (abs(diff) > 0x3000)
					&& ( (plyr->no != ball_handler) || (!possession_flag) || (!(pivot_flag | pass_flag))) )
				{
					SW angle;
					playert *plyr2;

					plyr->leg_rate = 0x6000/0x20;

					//stop turn for hand-checked players
					if ((BALL_HANDLER) && (last_shuf_flag))
					{
						SW b_diff;

						b_diff = current_ctrlr->d_angle - plyr->ball_angle;

						if (abs(b_diff) > 0x6000)
						{
							for (plyr2 = player+(plyr->base^5); plyr2<player+((plyr->base^5)+5); plyr2++)
							{
								if (covered2(plyr, plyr2))
								{
									//if (plyr2->hand_check_flag)
									{
										if (MANHATTAN(plyr2->xco-plyr->xco, plyr2->yco-plyr->yco) < 0x100000)
										{
											if (spin_test(plyr, plyr2))
											{
												diff = plyr2->basket_angle-plyr->basket_angle;
												if (plyr->top_anim>0)	diff = 0x7000;
												else					diff = -0x7000;

//												if  (diff<0)	diff = 0x7000;
//												else			diff = -0x7000;

												plyr->angle2 = plyr->ball_angle+diff;

												start_leg_anim(plyr, (diff > 0) ? -LEG_SPIN180: LEG_SPIN180);

												plyr->leg_rate = (0x3800/0x20)/TIME_FACTOR;

												angle = plyr->ball_angle + (diff*5)/6;
												start_vels(plyr, 60, angle);

												plyr->leg_routine = turn_and_burn;
												turn_and_burn(plyr);
												return;
											}
											else
											{
												plyr->freeze_ctr = 5;
												if		(diff>0x2000)	diff = 0x2000;
												else if (diff<-0x2000)	diff = -0x2000;
												plyr->leg_rate = 0x100/TIME_FACTOR;
											}
										}
									}
								}
							}
						}
					}
					do_squeek();

					start_leg_anim(plyr, (diff > 0) ? -LEG_RUN90: LEG_RUN90);
					angle = plyr->angle + (diff*7)/8;
					start_vels(plyr, 40, angle);
					plyr->angle2 = plyr->angle+diff;

					plyr->leg_routine = turn_and_burn;
					turn_and_burn(plyr);
					return;
				}
				else
				{
					int pivot_anim;

					//do turning moves
					pivot_anim = pivot(plyr, diff);

					//don't animate for small turns
					if (abs (diff) >= TURN_SPEED)
					{
						start_blend_leg_anim(plyr, pivot_anim);
					}
				}

				return;
			}
		}
		else
		{
			start_shuffle(plyr);
			return;
		}
	}
	else
	{
	deflt:

		plyr->xvel       =
		plyr->yvel       =
		plyr->last_speed = 0;

		switch (abs(plyr->top_anim))
		{
			case TOP_OP1:
			case TOP_OP2:
			case TOP_OPR1:
			case TOP_OPR2:
			case TOP_CATCH_OH:
			case TOP_OVER_BALL:
			case TOP_PVT2F:
			case TOP_PVT2B:
			case TOP_LOW_TO_HI:
			case TOP_GRDH:

			start_blend_leg_anim(plyr, LEG_STAND);
			break;


			case TOP_STAND:
			case TOP_TIRED:
			case TOP_ONHIPS:
			case TOP_RDY:

			start_blend_leg_anim(plyr, abs(plyr->top_anim)+LEG_TIRED-TOP_TIRED);
			plyr->leg_frame = plyr->top_frame;
			plyr->leg_rate  = plyr->top_rate = 0x80/TIME_FACTOR;
			break;

			default:
			start_blend_leg_anim(plyr, LEG_RDY);
			break;
		}
	}
}





int coast(playert *plyr, SI speed)
{
	//if (plyr->control)
	{
			speed -= COAST_BRAKE;
			if (speed < 0)	speed = 0;
			return speed;
	}


	/*
	if (speed >= JOG_THRESHOLD)
	{
		speed -= COAST_BRAKE;
		return speed;
	}

	anim = abs(plyr->leg_anim);
	if ((anim < LEG_JOG) | (anim > LEG_TURBO))
	{
		speed -= COAST_BRAKE;
		if (speed < 0)  return 0;
		else			return speed;
	}

// at what point in the animation will i stop at assuming const decel
	frames_per_speed = (plyr->speed * 0x10000)/((SI)(31.9*8)*plyr->scale);
	frames =  (speed*speed)/(2*COAST_BRAKE);
	frames *= frames_per_speed;

	end_frame = ((frames>>16) + (plyr->leg_frame>>8) + 4) % 20;

// if over 8 frames into stride go to next
	if (end_frame > 4)		frames += 20<<16;

	frames -= end_frame<<16;
	frames /= frames_per_speed;

	time = frames*2/speed;
	if (time > 10)  return speed;

	if (frames<=0)	return 0;
	return (speed - (speed*speed)/(2*frames));
	*/
}




void turn180(playert *plyr)
{
SW diff;
int time;

	CallChainP(turn180);

	diff = plyr->angle-plyr->angle2;

	if ((abs(plyr->leg_anim) == LEG_RUN180) && (plyr->leg_routine == NULL))
	{
		plyr->leg_rate = 0x200;
		time = ((leg_frames_to_go(plyr)-1)<<8)/plyr->leg_rate;
		run_collision(plyr, plyr->xvel, plyr->yvel);
	}
	else
	{
		time = 0;
	}

	if (time)
	{
		plyr->angle -= diff/time;
	}
	else
	{
		//start running or dribbling again
		plyr->leg_routine = running;

		if (BALL_HANDLER)
		{
			plyr->top_routine  = dribble_animate;
			SetMain(turn180,plyr,dribble);
		}
		else
		{
			plyr->top_routine  = top_deflt;
			SetMain(turn180,plyr,deflt);
		}

		start_blend_leg_anim(plyr, LEG_RUN);
		if (plyr->last_leg_anim < 0)			plyr->leg_frame = 20<<8;
		plyr->last_speed  = 0x1000;
	}
}





static int run_speeds[]={
	0x0,0x0,0x500,0x500,
	0x500,0x500,0x500,0x600,		//walk
	0x700,0x980,0xa00,0xa80,
	0xb00,0x1200,0x1200,0x1200,
	TURBO_SPEED
};


static int drib_speeds[]={
	0x0,0x0,0x500,0x500,
	0x500,0x500,0x500,0x600,		//walk
	0x680,0x700,0x800,0x900,
	0xa00,0xf00,0xf00,0xf00,
	0x1080
};



static int turn_speeds[]={
	0x800,0x800,0x300,0x300,
	0x300,0x300,0x300,0x900,		//walk
	0x900,0x900,0x900,0x900,
	0xc00,0xc00,0xc00,0xc00,
	0xc00,0xc00,0xc00,0xc00,
	0x600,0x600,0x600,0x600,
	0x600,0x600,0x600,0x600
};





void running(playert *plyr)
{
SI speed;
SW p_angle;
SW diff;

SW turn_speed;

SI run_start;
SI brake;
int top_speed;

SI xv,yv;
SW last_anim;
SW new_anim;
int cf;

	CallChainP(running);

/*
	if (plyr->no == 3)
	{
		printf("angle %x\n",current_ctrlr->d_angle);
		printf("force %x\n",current_ctrlr->force);
	}
*/

	plyr->shuffle = FALSE;
	/* different speeds depending upon whether dribbling or not */
	if (BALL_HANDLER)
	{
		brake		= DRIBBLE_BRAKE;
		run_start	= DRIBBLE_START;
	}
	else
	{
		brake 		= RUN_BRAKE;
		run_start	= RUN_START;
	}

	/* the player wants to go into shuffle mode? */
	if (current_ctrlr->ctrlr & CTRL_SHUFFLE)
	{
		do_squeek();
		plyr->leg_routine = shuffle_from_run_init;
		shuffle_from_run_init(plyr);
		return;
	}

	//fatigue
	cf = current_ctrlr->force>>4;

#if 0
	//slow down celebrations
	if (plyr->top_anim==TOP_CELG)
	{
		if (cf>12)	cf=12;
	}
#endif

#ifdef N64
	#if 0
	//#ifndef DEBUG
		if (plyr->plyrRAM->sw___pEnergy < (50<<8))
		{
			if (cf>12)	cf = 12;
		}
		else if (plyr->plyrRAM->sw___pEnergy < (75<<8))
		{
			if (cf>14)	cf = 14;
		}
	#endif
#endif

	/* top speed dependent upon controller force */
	if (!BALL_HANDLER)	 top_speed = run_speeds[cf];
	else				 top_speed = drib_speeds[cf];


	p_angle = plyr->angle;
	diff    = p_angle - current_ctrlr->d_angle;

	speed   = plyr->last_speed;


	if (top_speed == 0)
	{
		if (speed)	speed = coast(plyr, speed);
	}
	else
	{
		//turn speed dependent on running speed
		turn_speed = speed;
		if (speed<top_speed)	turn_speed=top_speed;
		turn_speed = turn_speeds[turn_speed>>8];

		//turn ball handler more slowly
		if (BALL_HANDLER)
	 	{
	 		turn_speed	= (turn_speed>>2)*3;
		}

		/* am I facing roughly the same direction as the controller */
		if ((diff < turn_speed) && (diff > -turn_speed))
		{
			p_angle	= current_ctrlr->d_angle;

			if (speed < run_start)
			{
				speed = run_start;
			}
			else
			{
				if (speed < top_speed)	speed += (top_speed - speed)>>3;
				else
				{
					speed	-= (RUN_ACCEL*2/3);
					if (speed < top_speed)	speed = top_speed;
				}
			}
		}
		/* there a large diffence in controller and player angles */
		else if	(abs(diff)>0x6000) //(UW)(diff+0x5000) > 0xa000)
		{
			if ((speed > WALK_THRESHOLD) && (current_ctrlr->ctrlr & CTRL_TURBO))
			{
				if (((plyr->control) && (!pass_flag)) && (plyr->ikBlend==0 && plyr->ikBlendSpeed<=0))
				{
					plyr->angle2	   = current_ctrlr->d_angle;
					plyr->leg_routine  = NULL;
					SetMain(running,plyr,turn180);

					if (diff > 0)
					{
						new_anim = LEG_RUN180;
						diff = -0x6000;
					}
					else
					{
						new_anim = -LEG_RUN180;
						diff = 0x6000;
					}

					start_blend_leg_anim(plyr, new_anim);

					plyr->leg_rate = 0x200;

					start_vels(plyr, 18, (SW)(plyr->angle+diff) );

					return;
				}
			}
			/* stop quickly */
			speed -= brake*2;
		}
		else
		{
			/* turn whilst on the run */
			if (diff < 0)	turn_speed = -turn_speed;
			p_angle -= turn_speed;
			/* do pitch for turning */
			if (speed > WALK_THRESHOLD)
			{
				if (abs(plyr->pitch) < 10)	plyr->pitch += (turn_speed>0) ? 3:-3;
			}
		}
	}

	plyr->angle	= p_angle;

	if (speed <= 0)
	{
		plyr->last_speed  =
		speed             = 0;
		plyr->leg_routine = leg_deflt;
		start_blend_leg_anim(plyr, LEG_RDY);
	}
	else
	{
		plyr->last_speed = speed;

		// if not running then start legs
		last_anim = abs(plyr->leg_anim);

		if ((last_anim > LEG_TURBO) | (last_anim < LEG_WALK))
		{
			//get right start legs
			if (top_speed < WALK_THRESHOLD)
			{
				new_anim = LEG_STWALK;
			}
			else if (top_speed < JOG_THRESHOLD)
			{
				new_anim = LEG_STJOG;
			}
			else if (top_speed < TURBO_THRESHOLD)
			{
				new_anim = LEG_STRUN;
			}
			else
			{
				new_anim = LEG_STTURBO;
			}

			//if pivoting right start with left foot
			if (plyr->leg_anim == -LEG_PVTF)
			{
				new_anim = -new_anim;
			}


			if ((last_anim <= LEG_STTURBO) && (last_anim >= LEG_STWALK))
			{
				if (end_leg_animation_test(plyr))
				{
					new_anim = LEG_JOG;
					if (last_anim == LEG_STWALK)	new_anim = LEG_WALK;

					start_leg_anim(plyr, new_anim);

					if (plyr->last_leg_anim < 0)
					{
						//if starting from other left foot advance
						//run 1/2 way through loop
						plyr->leg_frame = 0<<8;
					}
					else
					{
						plyr->leg_frame = 20<<8;
					}

					//LEG_STWALK ends on a different frame
					if (last_anim == LEG_STWALK)
					{
						plyr->leg_frame += 10<<8;
					}

				}
				else if (last_anim != new_anim)
				{
					plyr->last_leg_anim  = plyr->leg_anim;
					plyr->leg_anim       = new_anim;
					plyr->last_leg_frame = plyr->leg_frame;
					plyr->leg_blend_ctr  = 0x200;
				}
			}
			else
			{
				//change to blend by steve...
				start_blend_leg_anim(plyr, new_anim);
			}

			//if pivoting into run start a fraction later in start run anim
			if ((last_anim == LEG_PVTF) && (abs(new_anim) != LEG_STWALK))
			{
				speed = RUN_ACCEL*3;
				plyr->leg_frame = 0x680;
			}
		}
		else
		{
			//get desired animation from actual speed

			//don't go through walk in stopping, (unless walking)
			if ( (speed < WALK_THRESHOLD) && ((top_speed != 0) || (last_anim == LEG_WALK)) )
			{
				new_anim = LEG_WALK;
			}
			else if (speed < JOG_THRESHOLD)
			{
				new_anim = LEG_JOG;
			}
			else if ((speed<TURBO_THRESHOLD) && (current_ctrlr->force <= 0xf0))
			{
				new_anim = (plyr->plyrROM->pcz__pSName[0]>'L') ? LEG_RUN2: LEG_RUN;
			}
			else
			{
				new_anim = LEG_TURBO;
			}

			// if already running then move to desired animation cycle
			if ((last_anim >= LEG_WALK) && (last_anim <= LEG_TURBO))
			{
				if (last_anim != new_anim)
				{
					plyr->last_leg_anim  = plyr->leg_anim;
					plyr->leg_anim       = new_anim;
					plyr->last_leg_frame = plyr->leg_frame;
					plyr->leg_blend_ctr  = 0x200;
					plyr->leg_blend_speed= 0x80;
				}
			}
		}

		speed = (speed * plyr->speed);

		//slow down CPU according to difficulty
		if ((ai_team[plyr->team].controllers[0]==0) && (ai_team[plyr->team^1].controllers[0]!=0))
		{
			switch (DIFFICULTY)
			{
			case 1:
				speed -=speed>>4;
				break;

			case 0:
				speed -=speed>>3;
				break;
			}
		}

		running_animate(plyr,speed);
	}


	if (speed)
	{
		plyr->xvel =
		xv         = (LFPCos(p_angle) * (speed>>6)) >> 8;      /* x */
		plyr->yvel =
		yv         = (LFPSin(p_angle) * (speed>>6)) >> 8;      /* y */

		plyr->xvel-=plyr->xvel*((MAX_PLAYER_ENERGY-plyr->plyrRAM->sw___pEnergy)>>8)/(MAX_PLAYER_ENERGY>>8)/4;
		plyr->yvel-=plyr->yvel*((MAX_PLAYER_ENERGY-plyr->plyrRAM->sw___pEnergy)>>8)/(MAX_PLAYER_ENERGY>>8)/4;

		xv=plyr->xvel;
		yv=plyr->yvel;

		//update x,y and handle collisions
		plyr->col_flags = run_collision(plyr, xv, yv);
		if (plyr->col_flags != -1)
		{
			//AudioPlaySfx(SFX_ID_CLOCKTIK,22050,255,7);
			plyr->flags |= COLLIDE;
			collision_react(plyr, plyr->col_flags);

			if ((plyr->force == 0x100) && (BALL_HANDLER) && ( ((plyr->col_flags-5)^(plyr->no-5))<0) )
			{
				do_grunt();
				//pickup_dribble(plyr);
			}
		}
	}
}



void running_animate
		(
		playert	*plyr,
		int		speed
		)
{
	//match leg rate to leg animation, speed and player scale
	switch (abs(plyr->leg_anim))
	{
		case LEG_STWALK:
		case LEG_WALK:
		plyr->leg_rate = TEMPO((speed * 0x100)/((SI)(21.6*8)*plyr->scale));
		break;

		case LEG_JOG:
		plyr->leg_rate = TEMPO((speed * 0x100)/((SI)(35*8)*plyr->scale));
		break;

		case LEG_STJOG:
		case LEG_STTURBO:
		case LEG_STRUN:
		case LEG_STRUN2:
		case LEG_RUN:
		case LEG_RUN2:
		plyr->leg_rate = TEMPO((speed * 0x100)/((SI)(89*4)*plyr->scale));
		break;

		case LEG_TURBO:
		plyr->leg_rate = TEMPO((speed * 0x100)/((SI)(99*4)*plyr->scale));
		break;
	}
}



int pivot(playert *plyr, SW diff)
{
SW pivot_angle;
int pivot_dist;
int pivot_anim;
SW turn_speed;

	//cap turn_speed
	if		(diff > TURN_SPEED)		turn_speed = TURN_SPEED;
	else if (diff < -TURN_SPEED)	turn_speed = -TURN_SPEED;
	else							turn_speed = diff;

	//have i picked up my dribble
	if ((plyr->main_routine == stand_with_ball) ||
		(plyr->main_routine == stand_with_ball2) ||
		(plyr->main_routine==return_pass) )
	{
		//have i pivoted yet
		switch (pivot_flag)
		{
			case NO_PIVOT:
				if (turn_speed > 0)
				{
					pivot_anim = LEG_PVTF;
					pivot_dist = LFPSin(turn_speed/2);
					pivot_flag = LEFT_PIVOT;
					pivot_angle= -0x1000;		//angle of ball of pivot foot from c of player
				}
				else
				{
					pivot_anim = -LEG_PVTF;
					pivot_dist = -LFPSin(turn_speed/2);
					pivot_flag =  RIGHT_PIVOT;
					pivot_angle= 0x1000;		//angle of ball of pivot foot from c of player
				}
				break;

			case LEFT_PIVOT:
				pivot_anim = (turn_speed > 0) ? LEG_PVTF: LEG_PVTB;
				pivot_dist = LFPSin(turn_speed/2);
				pivot_angle= -0x1000;			//angle of ball of pivot foot from c of player
				break;

			case RIGHT_PIVOT:
				pivot_anim = (turn_speed < 0) ? -LEG_PVTF: -LEG_PVTB;
				pivot_dist = -LFPSin(turn_speed/2);
				pivot_angle= 0x1000;			//angle of ball of pivot foot from c of player
				break;

			default:
				BRK;
		}
	}
	else
	{
		//non-ball handling pivots

		//pivot flag2 set by AI
		if (pivot_flag2)
		{
			if (turn_speed < 0)
			{
				pivot_anim = LEG_PVTB;
				pivot_dist = LFPSin(turn_speed/2);
				pivot_angle= -0x1000;		//angle of ball of pivot foot from c of player
			}
			else
			{
				pivot_anim = -LEG_PVTB;
				pivot_dist = -LFPSin(turn_speed/2);
				pivot_angle= 0x1000;		//angle of ball of pivot foot from c of player
			}
		}
		else
		{
			/* do pivot */
			if (turn_speed > 0)
			{
				pivot_anim = LEG_PVTF;
				pivot_dist = LFPSin(turn_speed/2);
				pivot_angle= -0x1000;		//angle of ball of pivot foot from c of player
			}
			else
			{
				pivot_anim = -LEG_PVTF;
				pivot_dist = -LFPSin(turn_speed/2);
				pivot_angle= 0x1000;		//angle of ball of pivot foot from c of player
			}
		}
	}


	pivot_dist  *= (340*plyr->scale)>>(11+4);	//dist by centre during pivot
	pivot_angle += plyr->angle + (turn_speed/2);

#if 0
	run_collision(plyr,
					 (pivot_dist*LFPCos(pivot_angle)) >> 10,
					 (pivot_dist*LFPSin(pivot_angle)) >> 10);
#else
	plyr->xco+=(pivot_dist*LFPCos(pivot_angle)) >> 10;
	plyr->yco+=(pivot_dist*LFPSin(pivot_angle)) >> 10;
#endif

	plyr->angle += turn_speed;

	return pivot_anim;
}


