


#include "generic.h"
#include "lfptypes.h"

#include "main.h"
#include "audio.h"

#include "define.h"
#include "ml.h"
#include "comment.h"

#ifdef N64
#include "camera.h"
#include "cammodes.h"
#endif


#include "gamefx.h"

extern u8
	*GameBank;


extern long
	cont[];


int time_left;
int shot_clock;
int ten_sec_clock;
int five_sec_clock;

int gameOver;

bool shot_clock_flag;


int celebration_flag;




//extern void end_of_game_setup(void);
//extern void celebration_check(void);



void new_shot_clock(void)
{

	mask_shoot();

	shot_clock_flag=(SEC24_CLOCK);
	if (time_left > 24*ONE_SEC)
	{
		shot_clock = 24*ONE_SEC;
	}
	else
	{
		shot_clock_flag=FALSE;
		shot_clock = time_left+2;
		if (shot_clock<ONE_SEC)	shot_clock = ONE_SEC;
	}
}


void clock_update(void)
{
int i,c;

#if DEBUG
//	return;
#endif

#ifdef DUNK
	return;
#endif

#ifdef PLAY
	return;
#endif

#ifdef MOVE
	return;
#endif

#ifdef PC
//	shot_clock--;
//	if (shot_clock<0)							shot_clock=24*ONE_SEC;
//	if ((time_flag) & (!half_flag))				ten_sec_clock++;
//	if ((!in_play_flag) && (possession_flag))	five_sec_clock++;
//	return;
#endif


	if (celebration_flag)
	{
		time_left += 1;
		// click ticks up from ONE_MIN...
		if (time_left>ONE_MIN+(8*ONE_SEC))
		{
			gameOver=1;
		}
		return;
	}



	if ((time_flag) || (time_left <= 0))
	{
		time_left -= 1;

		if (PracticeMode)
			time_left++;

		if (time_left==2*ONE_MIN)
		{
			//say TWO Minutes
//phil			CommentQueue(PHRASE_2MINUTES, 0, 0);

			if (ai_team[0].fouls_left>1)	ai_team[0].fouls_left = 1;
			if (ai_team[1].fouls_left>1)	ai_team[1].fouls_left = 1;
		}


		if (time_left <= 0)
		{
			if (time_left == 0)
			{
//old				AudioPlaySfx(SFX_ID_HORN,22050,255,7);
				Audio_SfxPlay( GameBank, SFX_HORN, SfxVolume );

				stopAllMotors = TRUE;	// fixes bug #703 !?!  (mjk 9/6/99)

			}

			//stop moving CPU players
			ai_off_flag = TRUE;

			if (ball.main_routine != shot_ball_move &&
					time_left < -(2*ONE_SEC) && !(ft_flag || free_throws) )
			{
				time_left++;

				// Hopefully, these checks will wait until the ball is idle...
				if (ball.main_routine == ball_rolling ||
						ball.main_routine == follow_player)
				{
					if (!ScoreBoardCam)
					{
						CamScoreBoard( TRUE );
						Comment( COMMENT_QUARTER, NULL, 0 );
					}
				}

				if (card_ctr <= 0 && ScoreBoardTime >= (5*ONE_SEC) && !Speech_IsQueued())
				{
					c = 0;

					for (i=0; i<4; i++)
						c |= cont[i];

					if ((c & (CTRL_START | CTRL_FIRE_A | CTRL_FIRE_B))||
						(!(ai_team[0].controllers[0] || ai_team[1].controllers[0]) && ScoreBoardTime>(10*ONE_SEC)))
					{
						CamScoreBoard( FALSE );

						//celebration_check();

						if (celebration_flag)
						{
							//end_of_game_setup();
						}
						else
						{
							gameOver=1;
						}
					}
				}
			}

			return;

		}
		else
		{
			//update player's time on court
			for (i=0;i<10;i++)
			{
				player[i].plyrRAM->si___pTimeOnCourt++;
				if (player[i].plyrRAM->si___pTimeOnCourt > max_mins)
				{
					player[i].plyrRAM->si___pTimeOnCourt=max_mins;
				}
			}

			if (time_left <= (5*ONE_SEC))
			{
				if ((time_left % ONE_SEC) == 0)
				{
					//beep
//old					AudioPlaySfx(SFX_ID_CLOCKTIK,22050,255,7);
//old					AudioPlaySfx(SFX_ID_CLOCKTIK,22050,255,7);
//old					AudioPlaySfx(SFX_ID_CLOCKTIK,22050,255,7);
//old					sfx_vol = 255*3;
				}
			}
		}
	}


	if (!in_play_flag)
	{
		if (possession_flag)
		{
			if ((free_throws==0))
			{
				five_sec_clock++;
				if ( (five_sec_clock > (5*ONE_SEC)) && (SEC5_CLOCK) && !PracticeMode)
				{
					//call five sec violation here
					SetMain(clock_update,bhp,deflt);
					bhp->top_routine  = top_deflt;
					bhp->leg_routine  = leg_deflt;
					FoulSet(clock_update,bhp, FIVE_SECOND);

//phil					CommentQueue(PHRASE_5SECV, 0, ONE_SEC);
				}

				if (five_sec_clock==2.5*ONE_SEC)
				{
					playert *p;
					SW angle;

					for (p=player+bhp->base; p<player+bhp->base+5; p++)
					{
						if (p != bhp)
						{
							angle		= LFPAtn(p->xco, p->yco);
							p->target_x = p->xco - (0x100*LFPCos(angle));
							p->target_y = p->yco - (0x100*LFPSin(angle));
						}
					}
				}
			}
		}
	}
	else
	{
		shot_clock_flag = FALSE;

		if ((shot_clock<time_left) && (SEC24_CLOCK))	shot_clock_flag = TRUE;


		if (time_flag)
		{
			if (shot_clock == 0)
			{
				if ((SEC24_CLOCK))
				{
					//buzzer
					shot_clock--;
//old					AudioPlaySfx(SFX_ID_BUZZER,32010,255,7);
					Audio_SfxPlay( GameBank, SFX_BUZZER, SfxVolume/2 );

					if (ball.main_routine != shot_ball_move)
					{
						FoulSet(clock_update,bhp, SHOT_CLOCK);
//phil						CommentQueue(PHRASE_24SECV, 0, 2*ONE_SEC);
						new_shot_clock();
					}
				}
			}
			else if (shot_clock < 0)
			{
				if (SEC24_CLOCK)
				{
					if (ball.main_routine != shot_ball_move)
					{
						if (ball.yco<0)		FoulSet(clock_update,player+5, SHOT_CLOCK);
						else				FoulSet(clock_update,player, SHOT_CLOCK);
//phil						CommentQueue(PHRASE_24SECV, 0, 2*ONE_SEC);
					}
				}
			}
			else if (SEC24_CLOCK)
			{
				shot_clock--;

				if (shot_clock <= (5*ONE_SEC))
				{
					if ((shot_clock % ONE_SEC) == 0)
					{
						//beep
						if (shot_clock_flag)
						{
//old							AudioPlaySfx(SFX_ID_CLOCKTIK,22050,255,7);
//old							AudioPlaySfx(SFX_ID_CLOCKTIK,22050,255,7);
//old							AudioPlaySfx(SFX_ID_CLOCKTIK,22050,255,7);
//old							sfx_vol = 255*3;
						}
					}
				}
			}
		}

		if ((time_flag) && (!half_flag))
		{
			ten_sec_clock++;
			if ((ten_sec_clock > (10*ONE_SEC)) && (SEC10_CLOCK))
			{
					if ((possession_flag) && (bhp->zco==0) && !PracticeMode)
					{
						//drop ball
						ball.yco = 0;
						SetMain(clock_update,bhp,deflt);
						bhp->top_routine  = top_deflt;
						bhp->leg_routine  = leg_deflt;
						FoulSet(update_clock,bhp, TEN_SECOND);
	//phil					CommentQueue(PHRASE_10SECV, 0, ONE_SEC);
					}
			}
		}
	}
}



#if 0

int eogs[]={

//winners circle
0x180000, 0,
0x180000 * 0.3, 0x180000 * 0.95,
0x180000 * -0.81, 0x180000 * 0.59,
0x180000 * -0.81, 0x180000 * -0.59,
0x180000 * 0.3, 0x180000 * -0.95,

//who gives a fuck about where the losers go

0x200000, 0 ,
0x200000 * 0.3,   (0x200000 * 0.95) ,
0x200000 * -0.81, (0x200000 * 0.59) ,
0x200000 * -0.81, (0x200000 * -0.59),
0x200000 * 0.3,  (0x200000 * -0.95),

};



void celebration_check(void)
{
#ifdef N64
	// return if not done yet...
	if(pcl__gMatch->sb___mQuarter<3) return;
	if(pcl__gMatch->apcl_mTeamRam[0]->uw___tPoints==
	   pcl__gMatch->apcl_mTeamRam[1]->uw___tPoints &&
		(pcl__gMatch->sb___mQuarter < 12)) return;
	
	celebration_flag=IsPlayoffWinner();

	if(celebration_flag)
	{
		extern int trophyPlayer;
		CamStartSpecial(CAM_SPECIAL_CIRCLE,-2,0,0);
		trophyPlayer=0+((celebration_flag-1)*5);
	}
#endif
}



void end_of_game_setup(void)
{
int *tp;
playert *p, *plyr1, *plyr2;


	if (celebration_flag)
	{
		if (celebration_flag==1)
		{
			plyr1 = &player[0];
			plyr2 = &player[5];
		}
		else
		{
			plyr1 = &player[5];
			plyr2 = &player[0];
		}


		tp = eogs;

		for (p=plyr1; p<plyr1+5; p++)
		{
			p->target_x = *tp++;
			p->target_y = *tp++;
		}

		for (p=plyr2; p<plyr2+5; p++)
		{
			p->target_x = *tp++;
			p->target_y = *tp++ + ((ball.yco > 0) ? BASKET_Y/2: -BASKET_Y/2);
		}

		in_play_flag = FALSE;
		not_there_yet= TRUE;

		if (possession_flag)
		{
			drop_ball(bhp);
		}

		ball.zco=0;
		SetMain(end_of_game_setup,&ball,nothing);
		SetBallHandler(10);

		fast_break_flag=FALSE;

		time_left = ONE_MIN;
	}
}

#endif








