

#include "generic.h"
#include "lfptypes.h"

#include "define.h"
#include "ml.h"
#include "playgfx.h"
#include "speech.h"
#include "camera.h"


#define TO_TIME 4


bool tip_off_won;
bool tip_off_flag;


void to_init(void)
{
playert *plyr;

	tip_off_flag = TRUE;


	ball.xco = ball.yco = 0;
	ball.zco = 0x100000;
	ball.xvel = 0;
	ball.yvel = 0;
	ball.zvel = 0;

	SetBallHandler(get_oobs_sector,4);
	bhp			 = player+4;

	SetPossession(to_init,FALSE);

	for (plyr=player; plyr<&ball; plyr++)
	{
		SetMain(to_init,plyr,to_wait);
		plyr->leg_routine  = NULL;
		plyr->top_routine  = NULL;

		start_anim(plyr, TOP_RDY);
		start_leg_anim(plyr, LEG_RDY);

		plyr->leg_rate = 0x100;

		plyr->angle = LFPAtn(plyr->xco, plyr->yco)+0x8000;

		plyr->look_at_ball_ctr = 4*ONE_SEC;
	}

	player[4].angle = 0x0;
	player[9].angle = 0x8000;

	SetMain(to_init,&player[4],to_waitc);
	SetMain(to_init,&player[9],to_waitc);

	player[4].look_at_ball_ctr = 0;
	player[9].look_at_ball_ctr = 0;

	start_anim(player+4, -TOP_TIPOFF0);
	start_anim(player+9, -TOP_TIPOFF0);

	ball.main_routine = to_ball_wait;
	ball.ctr2 = ONE_SEC;
}


void to_ball_up(playert *bl)
{
	ball.zco  += ball.zvel;
	ball.zvel -= LITTLE_G;

	if (ball.zco < 0)	BRK;

	if ((ball.zco < 0x300000) && (ball.zvel < 0))
	{
		//if no one jumped yet start
		if ((player[4].main_routine == to_waitc) && (player[9].main_routine == to_waitc))
		{
			start_anim2(player+4, -TOP_TIPOFF1, TO_TIME);
			player[4].ctr2 = can_reach_yet(player+4, TO_TIME, J_ZVEL) - TO_TIME;
			SetMain(to_ball_up,&player[4],to_to);

			start_anim2(player+9, -TOP_TIPOFF1, TO_TIME);
			player[9].ctr2 = can_reach_yet(player+9, TO_TIME, J_ZVEL) - TO_TIME;
			SetMain(to_ball_up,&player[9],to_to);
		}
	}
}



void to_ball_wait(playert *plyr)
{
	s32
		i;

	CallChainP(to_ball_wait);

//	if ((ball.ctr2--) <= 0 && !(Speech_IsBusy()||Speech_IsQueued()))
	if (!(Speech_IsBusy() || Speech_IsQueued()))
	{
		if (Cam_TipOff_Active())
		{
			for (i=1; i<=4; i++)
			{
				if (CTL_DOWN( &ctrlrs[i], CTRL_START | CTRL_FIRE_A | CTRL_FIRE_B ))
					Cam_TipOff_Deinit();
			}
		}
		else
		{
			if (ball.ctr2)
				ball.ctr2--;
			else
			{
				ball.main_routine = to_ball_up;
				ball.zvel		  = (LITTLE_G*ONE_SEC*2)/3;

				project_ball(ONE_SEC);
			}
		}
	}
}



void to_wait(playert *plyr)
{
	CallChainP(to_wait);

	if (possession_flag)
	{
		SetMain(to_wait,plyr,deflt);
		plyr->top_routine  = top_deflt;
		plyr->leg_routine  = leg_deflt;
	}
}


int can_reach_yet(playert *plyr, int to_time, int z_vel)
{
int pz;
int bz,bx,by;
int time;
int j_time;

	j_time = z_vel/LITTLE_G;

	pz = plyr->scale*2030*4;
	for (time = to_time; time<(to_time+j_time); time++)
	{
		get_future_ball_cos(time+to_time, &bx, &by, &bz);

		pz    += z_vel;
		z_vel -= LITTLE_G;

		if (pz > bz)	return time;
	}
	return 0;
}




void to_waitc(playert *plyr)
{
int z_vel;
int j_time;
int time;

	CallChainP(to_waitc);
	
	if (ball.zvel < 0)
	{
		z_vel   = MIN_MAX(plyr->plyrROM->ub___pAttrJump, J_ZVEL-LITTLE_G, J_ZVEL+LITTLE_G);

		if (plyr->control == 0)
		{
			time = can_reach_yet(plyr, TO_TIME, z_vel);
			if (time)
			{
				plyr->ctr2 = time-TO_TIME;
				SetMain(to_waitc,plyr,to_to);
				start_anim2(plyr, -TOP_TIPOFF1, TO_TIME);
				plyr->zvel = z_vel;
			}
		}
		else
		{
			if (current_ctrlr->db_ctrlr & CTRL_SHOOT)
			{
				jmask(CTRL_SHOOT);

				//human bonus
				z_vel += LITTLE_G;
				if (DIFFICULTY==0)		z_vel += LITTLE_G;
			

				time = can_reach_yet(plyr, TO_TIME, z_vel);
				if (time)
				{
					plyr->ctr2 = time-TO_TIME;
					SetMain(to_waitc,plyr,to_to);
					start_anim2(plyr, -TOP_TIPOFF1, TO_TIME);
					plyr->zvel = z_vel;
				}
			}
		}
	}


	if (ball.main_routine == pass_ball_move)
	{
		SetMain(to_waitc,plyr,deflt);
		plyr->top_routine  = top_deflt;
		plyr->leg_routine  = leg_deflt;
	}

}


void to_to(playert *plyr)
{
	CallChainP(to_to);

	if (end_animation_test(plyr))
	{
		SetMain(to_to,plyr,to_up);
		start_next_anim2(plyr, plyr->ctr2);
	}
}


void tip_test(playert *plyr)
{
	int	mz,hi,hz;

	// If the other guy is up higher, give it to him


	mz=plyr->zco+(plyr->scale*2430*4);
	hi=plyr->no-5;
	if (hi<0)
		hi+=10;
	
	//make sure he is off the ground
	if (player[hi].zco)	
	{
		hz=player[hi].zco+(player[hi].scale*2430*4);
		if (hz>mz)
			return;
	}

	if (ball.zco < mz)
	{
		if (ball.main_routine == to_ball_up)
		{
			SetBallHandler(get_oobs_sector,plyr->no);
			bhp = plyr;

			int_recvr = (plyr->no > 4) ? 7:1;
			release_tipoff(plyr, CH_PASS_SPD/4, 0);

			if (plyr->control)	CtrlMotorOn(plyr->control-1, 2);

			tip_off_flag = FALSE;

			tip_off_won = (plyr->no > 4);

			time_flag = TRUE;
			in_play_flag = TRUE;
			fast_break_flag = FALSE;
			SetHalfFlag(tip_test,FALSE);
		}
	}
}


void to_up(playert *plyr)
{
	CallChainP(to_up);

	plyr->zco  += plyr->zvel;
	plyr->zvel -= LITTLE_G;

	tip_test(plyr);

	if (end_animation_test(plyr))
	{
		SetMain(to_up,plyr,to_down);
		start_next_anim2(plyr, time_till_land(plyr));

	}
}


void to_down(playert *plyr)
{
	CallChainP(to_down);

	plyr->zco  += plyr->zvel;
	plyr->zvel -= LITTLE_G;

	tip_test(plyr);

	if (end_animation_test(plyr))
	{
		SetMain(to_down,plyr,to_land);
		start_next_anim(plyr);
	}
}


void to_land(playert *plyr)
{
	CallChainP(to_land);

	plyr->zco=0;

	if (end_animation_test(plyr))
	{
		SetMain(to_land,plyr,deflt);
		plyr->top_routine  = top_deflt;
		plyr->leg_routine  = leg_deflt;
	}
}



















