/*============================================================*/
/*============================================================*/
/*==														==*/
/*==				Play Utility Routines					==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


/*========================================================  Includes  */

#include "AppleTalkDDP.h"
#include "Globals.h"
#include "UnivUtilities.h"
#include "PlayUtils.h"
#include "Render.h"
#include "Ball.h"
#include "Menu.h"
#include "SoundUtils.h"
#include "CommonPerson.h"
#include "NetOpponent.h"
#include "TeamSetUp.h"
#include "MainWindow.h"
#include "IdleRoutines.h"
#include "Replay.h"
#include "DissBits.h"


/*========================================================  Functions  */
/*========================================================  DoEndGame  */

void DoEndGame (void)

{
	pausing = FALSE;
	primaryMode = kIdleMode;
	SetMBarToIdle();
}


/*========================================================  UpdateWorldRecords  */

void UpdateWorldRecords (void)

{
	if (playerScore > mostPoints)
	{
		mostPoints = playerScore;
		GetDateTime((unsigned long *)&mostPointsDate);
		PasStringCopy((StringPtr)theNames[whichHumanNumber - 1], (StringPtr)mostPointsName);
	}
	
	if ((opponentScore > mostPoints) &&
			(theOpponent.persona >= kSimpleGeorge) && 
			(theOpponent.persona <= kMissTeak))
	{
		mostPoints = opponentScore;
		GetDateTime((unsigned long *)&mostPointsDate);
		GetIndString((StringPtr)mostPointsName, rNamesStringID, theOpponent.persona + 1);
	}
	
	if (playerTotalFouls > mostFouls)
	{
		mostFouls = playerTotalFouls;
		GetDateTime((unsigned long *)&mostFoulsDate);
		PasStringCopy((StringPtr)theNames[whichHumanNumber - 1], (StringPtr)mostFoulsName);
	}
	if ((opponentTotalFouls > mostFouls) &&
			(theOpponent.persona >= kSimpleGeorge) && 
			(theOpponent.persona <= kMissTeak))
	{
		mostFouls = opponentTotalFouls;
		GetDateTime((unsigned long *)&mostFoulsDate);
		GetIndString((StringPtr)mostFoulsName, rNamesStringID, theOpponent.persona + 1);
	}
	
	if (playerTotalCrits > mostCrits)
	{
		mostCrits = playerTotalCrits;
		GetDateTime((unsigned long *)&mostCritsDate);
		PasStringCopy((StringPtr)theNames[whichHumanNumber - 1], (StringPtr)mostCritsName);
	}
	if ((opponentTotalCrits > mostCrits) &&
			(theOpponent.persona >= kSimpleGeorge) && 
			(theOpponent.persona <= kMissTeak))
	{
		mostCrits = opponentTotalCrits;
		GetDateTime((unsigned long *)&mostCritsDate);
		GetIndString((StringPtr)mostCritsName, rNamesStringID, theOpponent.persona + 1);
	}
}


/*========================================================  UpdateStats  */

void UpdateStats (void)

{
	short			statusFlag, didWon, skunked;
	
	if (theStats[whichHumanNumber - 1].played < kMaxShort)
		theStats[whichHumanNumber - 1].played++;	/* increment # of games played */
	
	if ((theStats[whichHumanNumber - 1].goals + playerTotalGoals) < kMaxShort)
		theStats[whichHumanNumber - 1].goals += playerTotalGoals;
	else
		theStats[whichHumanNumber - 1].goals = kMaxShort;
	
	if ((theStats[whichHumanNumber - 1].oppGoals + opponentTotalGoals) < kMaxShort)
		theStats[whichHumanNumber - 1].oppGoals += opponentTotalGoals;
	else
		theStats[whichHumanNumber - 1].oppGoals = kMaxShort;
	
	if ((theStats[whichHumanNumber - 1].fouls + playerTotalFouls) < kMaxShort)
		theStats[whichHumanNumber - 1].fouls += playerTotalFouls;
	else
		theStats[whichHumanNumber - 1].fouls = kMaxShort;
	
	if ((theStats[whichHumanNumber - 1].oppFouls + opponentTotalFouls) < kMaxShort)
		theStats[whichHumanNumber - 1].oppFouls += opponentTotalFouls;
	else
		theStats[whichHumanNumber - 1].oppFouls = kMaxShort;
	
	if ((theStats[whichHumanNumber - 1].criticals + playerTotalCrits) < kMaxShort)
		theStats[whichHumanNumber - 1].criticals += playerTotalCrits;
	else
		theStats[whichHumanNumber - 1].criticals = kMaxShort;
	
	if ((theStats[whichHumanNumber - 1].oppCriticals + opponentTotalCrits) < kMaxShort)
		theStats[whichHumanNumber - 1].oppCriticals += opponentTotalCrits;
	else
		theStats[whichHumanNumber - 1].oppCriticals = kMaxShort;
	
	if ((playerWonTheGame == kPlayerWon) && (theStats[whichHumanNumber - 1].won < 
			kMaxShort))
		theStats[whichHumanNumber - 1].won++;	/* increment # of games won */
	
	if (whichGame == kTournament)
	{
		if (playerWonTheGame == kPlayerWon)
		{
			if (opponentTotalGoals == 0)
			{
				statusFlag = kSkunkedState;
			}
			else
			{
				statusFlag = kWonState;
			}
		}
		else
		{
			statusFlag = kLostState;
		}
		
		switch (theOpponent.persona)
		{
			case kSimpleGeorge:
				theStats[whichHumanNumber - 1].georgeState = statusFlag;
				break;
			case kMadMara:
				theStats[whichHumanNumber - 1].maraState = statusFlag;
				break;
			case kHeavyOtto:
				theStats[whichHumanNumber - 1].ottoState = statusFlag;
				break;
			case kCleverClaire:
				theStats[whichHumanNumber - 1].claireState = statusFlag;
				break;
			case kMisterEaze:
				theStats[whichHumanNumber - 1].eazeState = statusFlag;
				break;
			case kMissTeak:
				theStats[whichHumanNumber - 1].teakState = statusFlag;
				break;
			default:
				break;
		}
			
		if ((theStats[whichHumanNumber - 1].georgeState != kUnplayedState) && 
				(theStats[whichHumanNumber - 1].maraState != kUnplayedState) &&
				(theStats[whichHumanNumber - 1].ottoState != kUnplayedState) &&
				(theStats[whichHumanNumber - 1].claireState != kUnplayedState) &&
				(theStats[whichHumanNumber - 1].eazeState != kUnplayedState) &&
				(theStats[whichHumanNumber - 1].teakState != kUnplayedState))
		{
			didWon = 0;
			
			if (theStats[whichHumanNumber - 1].georgeState >= kWonState)
				didWon++;
			if (theStats[whichHumanNumber - 1].maraState >= kWonState)
				didWon++;
			if (theStats[whichHumanNumber - 1].ottoState >= kWonState)
				didWon++;
			if (theStats[whichHumanNumber - 1].claireState >= kWonState)
				didWon++;
			if (theStats[whichHumanNumber - 1].eazeState >= kWonState)
				didWon++;
			if (theStats[whichHumanNumber - 1].teakState >= kWonState)
				didWon++;
			
			skunked = 0;
			
			if (theStats[whichHumanNumber - 1].georgeState == kSkunkedState)
				skunked++;
			if (theStats[whichHumanNumber - 1].maraState == kSkunkedState)
				skunked++;
			if (theStats[whichHumanNumber - 1].ottoState == kSkunkedState)
				skunked++;
			if (theStats[whichHumanNumber - 1].claireState == kSkunkedState)
				skunked++;
			if (theStats[whichHumanNumber - 1].eazeState == kSkunkedState)
				skunked++;
			if (theStats[whichHumanNumber - 1].teakState == kSkunkedState)
				skunked++;
			
			switch (didWon)
			{
				case 3:
					if (theStats[whichHumanNumber - 1].copperTitles < kMaxShort)
						theStats[whichHumanNumber - 1].copperTitles++;
					break;
				case 4:
					if (theStats[whichHumanNumber - 1].bronzeTitles < kMaxShort)
						theStats[whichHumanNumber - 1].bronzeTitles++;
					break;
				case 5:
					if (theStats[whichHumanNumber - 1].silverTitles < kMaxShort)
						theStats[whichHumanNumber - 1].silverTitles++;
					break;
				case 6:
					if (skunked == 6)
					{
						if (theStats[whichHumanNumber - 1].platinumTitles < kMaxShort)
							theStats[whichHumanNumber - 1].platinumTitles++;
					}
					else
					{
						if (theStats[whichHumanNumber - 1].goldTitles < kMaxShort)
							theStats[whichHumanNumber - 1].goldTitles++;
					}
					break;
				default:
					break;
			}
			
			if (theStats[whichHumanNumber - 1].goldTitles > mostTitles)
			{
				mostTitles = theStats[whichHumanNumber - 1].goldTitles;
				GetDateTime((unsigned long *)&mostTitlesDate);
				PasStringCopy((StringPtr)theNames[whichHumanNumber - 1], 
						(StringPtr)mostTitlesName);
			}
			
			theStats[whichHumanNumber - 1].georgeState = kUnplayedState;
			theStats[whichHumanNumber - 1].maraState = kUnplayedState;
			theStats[whichHumanNumber - 1].ottoState = kUnplayedState;
			theStats[whichHumanNumber - 1].claireState = kUnplayedState;
			theStats[whichHumanNumber - 1].eazeState = kUnplayedState;
			theStats[whichHumanNumber - 1].teakState = kUnplayedState;
		}
	}
}


/*========================================================  InitiateGameOver  */

void InitiateGameOver (void)

{
	if (gameIsOver)
		return;
	
	PlaySoundSMS(kBellSound);
	
	if (playerScore > opponentScore)
		playerWonTheGame = kPlayerWon;
	else if (playerScore < opponentScore)
		playerWonTheGame = kOpponentWon;
	else
		playerWonTheGame = kNoOneWon;
	
	if (playerWonTheGame == kPlayerWon)
	{
		StartPersonBeamOut(&theOpponent);
		lengthOfApplause = kCrowdClosingHoopla;
		StartApplauseSound();
	}
	else if (playerWonTheGame == kOpponentWon)
	{
		StartPersonBeamOut(&thePlayer);
		lengthOfMob = kCrowdClosingHoopla;
		StartMobSound();
	}
	else
	{
		StartPersonBeamOut(&thePlayer);
		StartPersonBeamOut(&theOpponent);
		lengthOfMob = kCrowdClosingHoopla;
		StartMobSound();
	}
	
	if ((theOpponent.persona == kMissTeak) && (thePlayer.persona == kHumanPlayer))
		teaksThreshholds[whichHumanNumber - 1] = theOpponent.teaksThresh;
	
	if ((whichGame >= kStandardGame) && (thePlayer.persona == kHumanPlayer))
	{
		UpdateStats();
		UpdateWorldRecords();
	}
	
	incidentalQueue.number = kGameSound;
	incidentalQueue.when = 180;
	incidentalQueue.priority = 100;
	
	gameIsOver = TRUE;
}


/*========================================================  DoSlaveGameOver  */

void DoSlaveGameOver (void)

{
	if ((gameIsOver) || (imTheMaster))
		return;
	
	gameIsOver = TRUE;
	if ((whichGame >= kStandardGame) && (thePlayer.persona == kHumanPlayer))
	{
		UpdateStats();
		UpdateWorldRecords();
	}
}


/*========================================================  DrawNames  */

void DrawNames (void)

{
	#define 	rShortNameStrID		1002
	#define		kNoOnesName			7
	Str255		theName1, theName2, tempStr;
	Rect		name1Rect, name2Rect;
	GrafPtr		wasPort;
	short		i, charsToDisplay, offset;
	
	GetPort(&wasPort);
	SetPort((GrafPtr)mainWndo);
	
	switch (displayMode)
	{
		case kDisplay9Inch:
			SetRect(&name1Rect, 0, 0, 64, 6);
			SetRect(&name2Rect, 0, 0, 64, 6);
			OffsetRect(&name1Rect, 25, 301);
			OffsetRect(&name2Rect, 425, 301);
			TextFont(202);
			TextSize(7);
			break;
		case kDisplay12Inch:
			SetRect(&name1Rect, 0, 0, 69, 9);
			SetRect(&name2Rect, 0, 0, 69, 9);
			OffsetRect(&name1Rect, 25, 319);
			OffsetRect(&name2Rect, 425, 319);
			TextFont(systemFont);
			TextSize(12);
			break;
		case kDisplay13Inch:
			SetRect(&name1Rect, 0, 0, 69, 9);
			SetRect(&name2Rect, 0, 0, 69, 9);
			OffsetRect(&name1Rect, 25, 426);
			OffsetRect(&name2Rect, 553, 426);
			TextFont(systemFont);
			TextSize(12);
			break;
		default:
			break;
	}
	
	if (leftGoalIsPlayers)
	{
		if (thePlayer.persona == kHumanPlayer)
			PasStringCopy((StringPtr)theNames[whichHumanNumber - 1], (StringPtr)theName1);
		else
		{
			if (whichGame >= kStandardGame)
				GetIndString(theName1, rShortNameStrID, thePlayer.persona);
			else
				GetIndString(theName1, rShortNameStrID, kNoOnesName);
		}
	}
	else
	{
		if (theOpponent.persona == kNetHuman)
		{
			PasStringCopy((StringPtr)opponentsName, (StringPtr)theName1);
		}
		else
		{
			if (whichGame >= kStandardGame)
				GetIndString(theName1, rShortNameStrID, theOpponent.persona);
			else
				GetIndString(theName1, rShortNameStrID, kNoOnesName);
		}
	}
	
	UprString(theName1, TRUE);
	charsToDisplay = theName1[0];
	for (i = 0; i < theName1[0]; i++)
	{
		PasStringCopyNum((StringPtr)theName1, (StringPtr)tempStr, i + 1);
		if ((StringWidth(tempStr)) < (name1Rect.right - name1Rect.left))
			charsToDisplay = i + 1;
	}
	theName1[0] = charsToDisplay;
	
	if (leftGoalIsPlayers)
	{
		if (theOpponent.persona == kNetHuman)
		{
			PasStringCopy((StringPtr)opponentsName, (StringPtr)theName2);
		}
		else
		{
			if (whichGame >= kStandardGame)
				GetIndString(theName2, rShortNameStrID, theOpponent.persona);
			else
				GetIndString(theName2, rShortNameStrID, kNoOnesName);
		}
	}
	else
	{
		if (thePlayer.persona == kHumanPlayer)
			PasStringCopy((StringPtr)theNames[whichHumanNumber - 1], (StringPtr)theName2);
		else
		{
			if (whichGame >= kStandardGame)
				GetIndString(theName2, rShortNameStrID, thePlayer.persona);
			else
				GetIndString(theName2, rShortNameStrID, kNoOnesName);
		}
	}
	
	UprString(theName2, TRUE);
	charsToDisplay = theName2[0];
	for (i = 0; i < theName2[0]; i++)
	{
		PasStringCopyNum((StringPtr)theName2, (StringPtr)tempStr, i + 1);
		if ((StringWidth(tempStr)) < (name2Rect.right - name2Rect.left))
			charsToDisplay = i + 1;
	}
	theName2[0] = charsToDisplay;
	
	FillRect(&name1Rect, black);
	FillRect(&name2Rect, black);
	
	TextMode(srcXor);
	
	offset = ((name1Rect.right - name1Rect.left) - StringWidth(theName1)) / 2;
	MoveTo(name1Rect.left + offset, name1Rect.bottom);
	DrawString(theName1);
	
	offset = ((name2Rect.right - name2Rect.left) - StringWidth(theName2)) / 2;
	MoveTo(name2Rect.left + offset, name2Rect.bottom);
	DrawString(theName2);
	
	if (isColor)
	{
		CopyBits(&(((GrafPtr)mainWndo)->portBits), &((GrafPtr)offCWorkPtr)->portBits, 
				&name1Rect, &name1Rect, srcCopy, nil);
		CopyBits(&(((GrafPtr)mainWndo)->portBits), &((GrafPtr)offCBackPtr)->portBits, 
				&name1Rect, &name1Rect, srcCopy, nil);
		CopyBits(&(((GrafPtr)mainWndo)->portBits), &((GrafPtr)offCWorkPtr)->portBits, 
				&name2Rect, &name2Rect, srcCopy, nil);
		CopyBits(&(((GrafPtr)mainWndo)->portBits), &((GrafPtr)offCBackPtr)->portBits, 
				&name2Rect, &name2Rect, srcCopy, nil);
	}
	else
	{
		CopyBits(&(mainWndo->portBits), &offWorkBits, &name1Rect, &name1Rect, srcCopy, 
				nil);
		CopyBits(&(mainWndo->portBits), &offBackBits, &name1Rect, &name1Rect, srcCopy, 
				nil);
		CopyBits(&(mainWndo->portBits), &offWorkBits, &name2Rect, &name2Rect, srcCopy, 
				nil);
		CopyBits(&(mainWndo->portBits), &offBackBits, &name2Rect, &name2Rect, srcCopy, 
				nil);
	}
	
	SetPort((GrafPtr)wasPort);
}


/*========================================================  UpdatePlayerScore  */

void UpdatePlayerScore (void)

{
	short		isOnes, isTens, isHundreds, selector;
	
	playerJustScored = TRUE;
	
	if (leftGoalIsPlayers)
		selector = kLeftScoreSelector;
	else
		selector = kRightScoreSelector;
	
	isHundreds = playerScore / 100;
	if (isHundreds == 0)
		DisplayScoreDigit(kHundredsPlace, kBlankCharacter, selector);
	else
		DisplayScoreDigit(kHundredsPlace, isHundreds, selector);
	
	isTens = (playerScore - isHundreds * 100) / 10;
	if ((isTens == 0) && (isHundreds == 0))
		DisplayScoreDigit(kTensPlace, kBlankCharacter, selector);
	else
		DisplayScoreDigit(kTensPlace, isTens, selector);
	
	isOnes = playerScore % 10;
	DisplayScoreDigit(kOnesPlace, isOnes, selector);
}


/*========================================================  UpdateOpponentScore  */

void UpdateOpponentScore (void)

{
	short		isOnes, isTens, isHundreds, selector;
	
	playerJustScored = FALSE;
	
	if (leftGoalIsPlayers)
		selector = kRightScoreSelector;
	else
		selector = kLeftScoreSelector;
	
	isHundreds = opponentScore / 100;
	if (isHundreds == 0)
		DisplayScoreDigit(kHundredsPlace, kBlankCharacter, selector);
	else
		DisplayScoreDigit(kHundredsPlace, isHundreds, selector);
	
	isTens = (opponentScore - isHundreds * 100) / 10;
	if ((isTens == 0) && (isHundreds == 0))
		DisplayScoreDigit(kTensPlace, kBlankCharacter, selector);
	else
		DisplayScoreDigit(kTensPlace, isTens, selector);
	
	isOnes = opponentScore % 10;
	DisplayScoreDigit(kOnesPlace, isOnes, selector);
}


/*========================================================  DoPlayerScores  */

void DoPlayerScores (void)

{
	lengthOfApplause = 120;
	StartApplauseSound();
	PlaySoundSMS(kScoreSound);
	
	playerScore++;
	playerTotalGoals++;
	if (thePlayer.whichGoal == kLeftGoal)
		leftScoredLast = TRUE;
	else
		leftScoredLast = FALSE;
	
	if (whichGame == kStandardGame)
	{
		if (playerScore - opponentScore == 4)
		{
			BeginGamePointSound();
		}
		else if ((playerScore >= 10) && (playerScore - opponentScore >= 1))
		{
			BeginGamePointSound();
		}
		
		if (playerScore == opponentScore)
		{
			BeginAllTiedSound();
		}
	}
	else if (whichGame == kTournament)
	{
		if (playerScore - opponentScore == 6)
		{
			BeginGamePointSound();
		}
		else if ((playerScore >= 16) && (playerScore - opponentScore >= 2))
		{
			BeginGamePointSound();
		}
		
		if (playerScore == opponentScore)
		{
			BeginAllTiedSound();
		}
	}
	
	UpdatePlayerScore();
	DisplayPlayerFouls(playerFouls);
	
	if (thePlayer.persona == kMissTeak)
	{
		if (thePlayer.strategy == kRunDiagonal)
			thePlayer.teaksThresh++;
		else if (thePlayer.strategy == kRunCircle)
			thePlayer.teaksThresh--;
		if (thePlayer.teaksThresh < 4)
			thePlayer.teaksThresh = 4;
		if (thePlayer.teaksThresh > 95)
			thePlayer.teaksThresh = 95;
	}
	
	if ((!netGameInSession) && (replayGoals))
		replayFlag = -50;
	
	switch (whichGame)
	{
		case kStandardGame:
			if (playerScore >= 11)
			{
				if (playerScore >= (opponentScore + 2))
				{
					InitiateGameOver();
				}
			}
			else
			{
				if (playerScore >= (opponentScore + 5))
				{
					InitiateGameOver();
				}
			}
			break;
		case kTournament:
			if (playerScore >= 17)
			{
				if (playerScore >= (opponentScore + 3))
				{
					InitiateGameOver();
				}
			}
			else
			{
				if (playerScore >= (opponentScore + 7))
				{
					InitiateGameOver();
				}
			}
			break;
		default:
			break;
	}
}


/*========================================================  DoOpponentScores  */

void DoOpponentScores (void)

{
	lengthOfMob = 120;
	StartMobSound();
	PlaySoundSMS(kScoreSound);
	
	opponentScore++;
	opponentTotalGoals++;
	if (theOpponent.whichGoal == kLeftGoal)
		leftScoredLast = TRUE;
	else
		leftScoredLast = FALSE;
	
	if (whichGame == kStandardGame)
	{
		if (opponentScore - playerScore == 4)
		{
			BeginGamePointSound();
		}
		else if ((opponentScore >= 10) && (opponentScore - playerScore >= 1))
		{
			BeginGamePointSound();
		}
		
		if (playerScore == opponentScore)
		{
			BeginAllTiedSound();
		}
	}
	else if (whichGame == kTournament)
	{
		if (opponentScore - playerScore == 6)
		{
			BeginGamePointSound();
		}
		else if ((opponentScore >= 16) && (opponentScore - playerScore >= 2))
		{
			BeginGamePointSound();
		}
		
		if (playerScore == opponentScore)
		{
			BeginAllTiedSound();
		}
	}
	
	UpdateOpponentScore();
	DisplayOpponentFouls(opponentFouls);
	
	if (theOpponent.persona == kMissTeak)
	{
		if (theOpponent.strategy == kRunDiagonal)
			theOpponent.teaksThresh++;
		else if (theOpponent.strategy == kRunCircle)
			theOpponent.teaksThresh--;
		if (theOpponent.teaksThresh < 4)
			theOpponent.teaksThresh = 4;
		if (theOpponent.teaksThresh > 95)
			theOpponent.teaksThresh = 95;
	}
	
	if ((!netGameInSession) && (replayGoals))
		replayFlag = -50;
	
	switch (whichGame)
	{
		case kStandardGame:
			if (opponentScore >= 11)
			{
				if (opponentScore >= (playerScore + 2))
				{
					InitiateGameOver();
				}
			}
			else
			{
				if (opponentScore >= (playerScore + 5))
				{
					InitiateGameOver();
				}
			}
			break;
		case kTournament:
			if (opponentScore >= 17)
			{
				if (opponentScore >= (playerScore + 3))
				{
					InitiateGameOver();
				}
			}
			else
			{
				if (opponentScore >= (playerScore + 7))
				{
					InitiateGameOver();
				}
			}
			break;
		default:
			break;
	}
}


/*========================================================  HandlePlayerFoul  */

void HandlePlayerFoul (void)

{
	lengthOfMob = 90;
	StartMobSound();
	if ((whichGame == kPracticeBoardin) || (whichGame == kPracticeScoring))
		PlaySoundSMS(kFoulSound);
	else
		QueueUpIncidental(kFoulSound, 6, 100);
	
	playerFouls++;
	playerTotalFouls++;
	if (playerFouls == kCriticalFoul)
	{
		playerTotalCrits++;
		playerFouls = 0;
		DoOpponentScores();
	}
	
	DisplayPlayerFouls(playerFouls);
	if ((!netGameInSession) && (replayFouls))
		replayFlag = -90;
}


/*========================================================  HandleOpponentFoul  */

void HandleOpponentFoul (void)

{
	lengthOfApplause = 90;
	StartApplauseSound();
	QueueUpIncidental(kFoulSound, 6, 100);
	
	opponentFouls++;
	opponentTotalFouls++;
	if (opponentFouls == kCriticalFoul)
	{
		opponentTotalCrits++;
		opponentFouls = 0;
		DoPlayerScores();
	}
	
	DisplayOpponentFouls(opponentFouls);
	if ((!netGameInSession) && (replayFouls))
		replayFlag = -90;
}


/*========================================================  DisplayHoopla  */

void DisplayHoopla (void)

{
	#define			kDisplayHooplaLoops	5
	#define			kDisplayHooplaDelay	2
	long			nextTime;
	short			i, miscShort1;
	
	nextTime = Ticks + kDisplayHooplaDelay;
	
	DisplayPlayerFouls(1);
	for (i = 0; i < kDisplayHooplaLoops; i++)	/* Blink --> 0:00     X00 000 */
	{
		miscShort1 = RandomInt(10);
		DisplayScoreDigit(0, miscShort1, 0);
		while (Ticks < nextTime)
		{
		}
		nextTime = Ticks + kDisplayHooplaDelay;
	}
	DisplayScoreDigit(0, 10, 0);
	
	DisplayPlayerFouls(2);
	for (i = 0; i < kDisplayHooplaLoops; i++)	/* Blink --> 0:00     0X0 000 */
	{
		miscShort1 = RandomInt(10);
		DisplayScoreDigit(1, miscShort1, 0);
		while (Ticks < nextTime)
		{
		}
		nextTime = Ticks + kDisplayHooplaDelay;
	}
	DisplayScoreDigit(1, 10, 0);
	
	DisplayPlayerFouls(0);
	for (i = 0; i < kDisplayHooplaLoops; i++)	/* Blink --> 0:00     00X 000 */
	{
		miscShort1 = RandomInt(10);
		DisplayScoreDigit(2, miscShort1, 0);
		while (Ticks < nextTime)
		{
		}
		nextTime = Ticks + kDisplayHooplaDelay;
	}
	DisplayScoreDigit(2, 10, 0);
	
	DisplayOpponentFouls(1);
	for (i = 0; i < kDisplayHooplaLoops; i++)	/* Blink --> 0:00     000 X00 */
	{
		miscShort1 = RandomInt(10);
		DisplayScoreDigit(0, miscShort1, 1);
		while (Ticks < nextTime)
		{
		}
		nextTime = Ticks + kDisplayHooplaDelay;
	}
	DisplayScoreDigit(0, 10, 1);
	
	DisplayOpponentFouls(2);
	for (i = 0; i < kDisplayHooplaLoops; i++)	/* Blink --> 0:00     000 0X0 */
	{
		miscShort1 = RandomInt(10);
		DisplayScoreDigit(1, miscShort1, 1);
		while (Ticks < nextTime)
		{
		}
		nextTime = Ticks + kDisplayHooplaDelay;
	}
	DisplayScoreDigit(1, 10, 1);
	
	DisplayOpponentFouls(0);
	for (i = 0; i < kDisplayHooplaLoops; i++)	/* Blink --> 0:00     000 00X */
	{
		miscShort1 = RandomInt(10);
		DisplayScoreDigit(2, miscShort1, 1);
		while (Ticks < nextTime)
		{
		}
		nextTime = Ticks + kDisplayHooplaDelay;
	}
	DisplayScoreDigit(2, 10, 1);
}


/*========================================================  DoNetGameSetUp  */

Boolean DoNetGameSetUp (void)

{
	OSErr			theErr;
	Boolean			itWorked;
	
	masterSendSize = (short)sizeof(masterSendType);
	slaveSendSize = (short)sizeof(slaveSendType);
	spotPoints = 0;
	spottedPoints = 0;
	
	theErr = RegisterNameOnNet();
	if (theErr != noErr)
	{
		MinorError(kMinorRegisterFailed);
		return(FALSE);
	}
	
	itWorked = SelectNetOpponentAsynch();
	if (!itWorked)
	{
		theErr = DeRegisterName();
		return(FALSE);
	}
	
	itWorked = SpotPoints();

	itWorked = WaitForSynch(3600);
	if (!itWorked)
	{
		MinorError(kMinorSynchFailed);
		theErr = DeRegisterName();
		return(FALSE);
	}
	
	itWorked = ConfirmEnvironmentMatch();
	if (!itWorked)
	{
		theErr = DeRegisterName();
		return(FALSE);
	}
	
	theErr = DeRegisterName();
	netGameInSession = TRUE;
	madeThePickUp = FALSE;
	loopDelay = 1;	
	frameCounter = 0;
	newFrameCount = 0;
	lastFrameCount = 0;
	droppedPackets = 0;
	sentPackets = 0;
	wasLastSound = 0;
	lastSoundPriority = 0;
	
	return (TRUE);
}


/*========================================================  CantRepeatTournament  */

Boolean CantRepeatTournament (void)

{
	Str255		theText;
	short		sheWants;
	
	GetIndString(theText, rMiscStrings, 23);
	ParamText(theText, "\p", "\p", "\p");
	
	CenterAlert(rCantRepeatAlertID);
	sheWants = Alert(rCantRepeatAlertID, kNilPointer);
	
	if (sheWants == 1)
		return (TRUE);
	else
		return (FALSE);
}


/*========================================================  MustProLeagueTournament  */

Boolean MustProLeagueTournament (void)

{
	Str255		theText;
	short		sheWants;
	
	GetIndString(theText, rMiscStrings, 24);
	ParamText(theText, "\p", "\p", "\p");
	
	CenterAlert(rCantRepeatAlertID);
	sheWants = Alert(rCantRepeatAlertID, kNilPointer);
	
	if (sheWants == 1)
		return (TRUE);
	else
		return (FALSE);
}


/*========================================================  CanWePlay  */

Boolean CanWePlay (void)

{
	short		statusFlag;
	Boolean		sheSaid;
	
	if ((netOnly) && (theOpponent.persona != kNetHuman))
	{
		MinorError(kMinorNetworkOnlyRestrict);
		primaryMode = kIdleMode;
		return (FALSE);
	}
	
	if ((thePlayer.persona == kHumanPlayer) && (theOpponent.persona != kNetHuman) && 
			(whichGame == kTournament))
	{
		switch (theOpponent.persona)
		{
			case kSimpleGeorge:
				statusFlag = theStats[whichHumanNumber - 1].georgeState;
				break;
			case kMadMara:
				statusFlag = theStats[whichHumanNumber - 1].maraState;
				break;
			case kHeavyOtto:
				statusFlag = theStats[whichHumanNumber - 1].ottoState;
				break;
			case kCleverClaire:
				statusFlag = theStats[whichHumanNumber - 1].claireState;
				break;
			case kMisterEaze:
				statusFlag = theStats[whichHumanNumber - 1].eazeState;
				break;
			case kMissTeak:
				statusFlag = theStats[whichHumanNumber - 1].teakState;
				break;
			default:
				break;
		}
		
		if (statusFlag != kUnplayedState)
		{
			sheSaid = CantRepeatTournament();
			if (sheSaid)
			{
				whichGame = kStandardGame;
				CheckGameMenu();
			}
			else
			{
				primaryMode = kIdleMode;
				return (FALSE);
			}
		}
	}
	
	if ((thePlayer.persona == kHumanPlayer) && (theOpponent.persona != kNetHuman) && 
			(whichGame == kTournament) && (isLeague != kProfessional))
	{
		sheSaid = MustProLeagueTournament();
		if (sheSaid)
		{
			whichGame = kStandardGame;
			CheckGameMenu();
		}
		else
		{
			primaryMode = kIdleMode;
			return (FALSE);
		}
	}
	
	if ((theOpponent.persona == kNetHuman) && (whichGame == kTournament))
	{
		MinorError(kMinorNetCantTournament);
		whichGame = kStandardGame;
		CheckGameMenu();
	}
	
	if ((thePlayer.persona != kHumanPlayer) && (theOpponent.persona != kHumanPlayer) && 
			(whichGame != kStandardGame))
	{
		MinorError(kMinorNetCantPractice);
		whichGame = kStandardGame;
		CheckGameMenu();
	}
	
	return (TRUE);
}


/*========================================================  PrepareNewGame  */

void PrepareNewGame (void)

{
	long			dummyLong;
	OSErr			theErr;
	short			i;
	Boolean			itWorked;
	
	if (splashIsUp)
	{
		DissolveWorkToMain();
		RefreshMainWindow();
		splashIsUp = FALSE;
	}
	
	if ((autoTeamsDialog) && (whichGame > kPracticeScoring))
	{
		itWorked = DoTeamsSetUp();
		if (itWorked)
		{
			WhosOnFirst();
			UpdateGoalPicts(FALSE);
		}
		else
		{
			primaryMode = kIdleMode;
			DrawMenuBar();
			return;
		}
	}
	
	if (!CanWePlay())
	{
		DrawMenuBar();
		return;
	}
	
	leftScoredLast = RandomCoin();
	netGameInSession = FALSE;
	
	if ((theOpponent.persona == kNetHuman) && (whichGame > kPracticeScoring))
	{
		itWorked = DoNetGameSetUp();
		if (!itWorked)
		{
			primaryMode = kIdleMode;
			DrawMenuBar();
			return;
		}
		WhosOnFirst();
	}
	
	ResetBall();
	ResetPerson(&thePlayer);
	ResetPerson(&theOpponent);
	thePlayer.wasRect = thePlayer.isRect;
	theOpponent.wasRect = theOpponent.isRect;
	theBall.wasRect = theBall.isRect;
	SetMBarToPlaying();
	HideMenuBar((GrafPtr)mainWndo);
	EraseMenuBarRect();
	UpdateGoalPicts(FALSE);
	RefreshMainWindow();
	
	TurnSMSOn();
	FlushSoundQueues();
	StartCrowdSound();
	
	if ((enableAnnouncer) && (whichGame > kPracticeScoring))
	{
		DoOpeningAnnouncer();
	}
	else
	{
		lengthOfApplause = 300;
		StartApplauseSound();
		Delay(kApplauseSwellTicks, &dummyLong);
		crowdQueue.when = 1;
		HandleCrowdSound();
	}
	
	DisplayHoopla();
	
	if (netGameInSession)
	{
		playerScore = spottedPoints;
		opponentScore = spotPoints;
	}
	else
	{
		playerScore = 0;
		opponentScore = 0;
	}
	playerFouls = 0;
	opponentFouls = 0;
	playerTotalGoals = 0;
	opponentTotalGoals = 0;
	playerTotalFouls = 0;
	opponentTotalFouls = 0;
	playerTotalCrits = 0;
	opponentTotalCrits = 0;
	
	if (thePlayer.persona == kHumanPlayer)
		theOpponent.teaksThresh = teaksThreshholds[whichHumanNumber - 1];
	
	replayFlag = 0;
	if (canReplay)
	{
		replayData->frame = 0;
		replayData->lastFrame = 0;
	}
	
	UpdatePlayerScore();
	UpdateOpponentScore();
	
	for (i = 0; i < kMaxReplayFrames; i++)
			AmassReplayData();
	
	newGame = FALSE;
	gameIsOver = FALSE;
	wasBrakeOn = FALSE;
	drawThisFrame = TRUE;
	baseTime = Ticks / 60;
	timeElapsed = 0;
	wasTime = 0;
	DrawNames();
	
	if (netGameInSession)
	{
		InitializeMessages();
		itWorked = WaitForSynch(3600);
		if (!itWorked)
		{
			MinorError(kMinorSynchFailed);
			primaryMode = kIdleMode;
			ShowMenuBar((GrafPtr)mainWndo);
			FixVisRegion();
			InitCursor();
			return;
		}
	}
	
	HideCursor();
}


