
#include "generic.h"
#include "lfptypes.h"
#include "define.h"
#include "ml.h"
#include "playgfx.h"

#include "ctrl.h"


/**/
/**/
/**/

bool turbo_test(playert *plyr, int turbo)
{
	if (plyr->turbo >= turbo)
	{
		plyr->turbo -= turbo;
		return TRUE;
	}

	return FALSE;
}


void free_move (playert *plyr)
{
	CallChainP(free_move);

	if (!project_flag)
	{
		if (plyr->top_routine == NULL)	plyr->top_routine = top_deflt;
		if (plyr->leg_routine == NULL)	plyr->leg_routine = leg_deflt;
	}

	/*steal test*/
	if (DEFENDING)
	{
		if (current_ctrlr->mctrlr & CTRL_STEAL)
		{
			if (in_play_flag && stealable_flag)
			{
				if (possession_flag)
				{
					#ifndef DEBUG
					if (turbo_test(plyr, TURBO_STEAL))
					#endif
					{
						jmask(CTRL_STEAL);
						steal_start(plyr);
						return;
					}
				}
				else if (ball.main_routine == pass_ball_move)
				{
					jmask(CTRL_STEAL);
					current_ctrlr->mctrlr &= ~CTRL_STEAL;
					steal_pass(plyr, ball.ctr2);
				}
			}
		}



		if ((!ArcadeMode || foul_cheat_flag) && CCTL_DOWN(CTRL_FOUL))
		{
			if ((in_play_flag) && (DEFENSIVE_FOULS || plyr->control))
			{
				foul_start(plyr);
			}
		}
	}
	else
	{
		if (current_ctrlr->mctrlr & CTRL_PICK)
		{
			if (in_play_flag)
			{
				jmask(CTRL_PICK);

				if (lp_ctr-last_pick>ONE_SEC)
				{
					plyr->pick_arm=TRUE;
					start_set_pick(plyr);
				}
				//set pick here
				//foul_start(plyr);
			}
		}
	}

	if (	CTL_DOWN(current_ctrlr,CTRL_SHOOT) )
	{
		//can't jump if someone on your team has the ball
		if ( (!possession_flag) || (bhp->team != plyr->team)) 
		{
			//can't jump if ball is being passed to you
			if (! (((ball.main_routine == pass_ball_move)||(pass_flag)) && (plyr->no == int_recvr)) )
			{
				if (!(current_ctrlr->roleplay!=CPL_NOLOCK && possession_flag && bhp->team==plyr->team && bhp->control==0) )
				{
					jmask(CTRL_SHOOT);
					jump_start(plyr);
				}
			}
		}
	}
}






void jump_start(playert *plyr)
{
	CallChainP(jump_start);
	
	{
		//can I rebound ball
		if (rebound_test(plyr))	
		{
			{
				teamt *t;

				t = ai_team+plyr->team;
				rebound_start(t->reb_plyr,t->reb_lt,t->reb_t,t->reb_to,t->reb_low_flag);
				return;
			}
		}
	}


	if (DEFENDING)
	{
		if (block_dunk_test(plyr))	return;
	}

	block_start(plyr);
}





void chase_loose_ball(playert *plyr)
{
int dist;
int	time;
int x, y, z;

SW		a_diff;
teamt	*tm;

	CallChainP(chase_loose_ball);

	//call off chase if ball caught or out of play
	if ((possession_flag)||(!in_play_flag)||(ai_off_flag))
	{
		new_main(plyr, deflt);
		new_top(plyr, top_deflt);
		return;
	}

	if (ball.zco < 0x140000)
	{
		if (!picked_up_flag)
		{
			if (plyr->ball_dist < 0x100000)
			{
				a_diff = plyr->angle - plyr->ball_angle;
				if (a_diff < 0x2000)
				{
					check_rebound(plyr);

					get_new_controls(&ai_team[plyr->team], plyr);
					catch_ball(plyr);

					plyr->top_routine  = dribble_animate;
					new_main(plyr, dribble);
					if (plyr->main_routine==dribble)
					{
						SetMain(chase_loose_ball,plyr,dribble);
						SetPossession(chase_loose_ball,TRUE);
					}
					return;
				}
			}
		}
	}
	//can I rebound ball
	if (!plyr->control && rebound_test(plyr))
	{
		tm=&ai_team[plyr->team];
		rebound_start(tm->reb_plyr,tm->reb_lt,tm->reb_t,tm->reb_to,tm->reb_low_flag);
		return;
	}

	dist = plyr->ball_dist;
	time = (dist >> 18);
	if (time >= ONE_SEC)  	time = ONE_SEC-1;

	do{
		get_future_ball_cos(time, &x, &y, &z);
		time += 1;
	}while(z>0x140000);

	plyr->target_x = x;
	plyr->target_y = y;


	if (!plyr->control)
	{
		current_ctrlr->d_angle = LFPAtn(x - plyr->xco, y - plyr->yco);
		current_ctrlr->force   = 0xf0;
	}

}


