/*============================================================*/
/*============================================================*/
/*==														==*/
/*==				Sound Handling Routines					==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


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

#include "Globals.h"
#include "UnivUtilities.h"
#include "SoundUtils.h"
#include "SMS.h"


/*========================================================  Functions  */
/*========================================================  TurnSMSOn  */

void TurnSMSOn (void)

{
	if (!smsActivated)
	{
		SMSINIT();
		SMSSETMODE(kFourChannel);
		smsActivated = TRUE;
	}
}


/*========================================================  TurnSMSOff  */

void TurnSMSOff (void)

{
	long		dummyLong;
	if (smsActivated)
	{
		SMSSTOP();
		SMSEXIT();
		Delay(2, &dummyLong);
		smsActivated = FALSE;
	}
}


/*========================================================  PlaySoundSMS  */

void PlaySoundSMS (short theSoundID)

{
	if ((netGameInSession) && (imTheMaster) && (theSoundID < kMaxNumberOfSounds) && 
			(soundPriorities[theSoundID] > lastSoundPriority))
	{
		wasLastSound = theSoundID;
		lastSoundPriority = soundPriorities[theSoundID];
	}
	
	if (smsActivated && soundLoaded[theSoundID] && soundOn)
	{
		switch (theSoundID)
		{
			case 0:
				break;
			case kClashSound:
			case kRicochetSound:
			case kBallPickUpSound:
			case kBallDropSound:
			case kBrakeSound:
				if (collisionSoundOn)
				{
					SMSSTART(theSoundID);
				}
				break;
			case kBallFiringSound:
			case kBeamInSound:
			case kBeamOutSound:
				if (beamSoundOn)
				{
					SMSSTART(theSoundID);
				}
				break;
			case kCrowdSound:
			case kCrowdSwellSound:
			case kApplauseSound:
			case kCrowdFadeSound:
			case kMobSwellSound:
			case kMobSound:
			case kMobFadeSound:
				if (crowdSoundOn)
				{
					SMSSTART(theSoundID);
				}
				break;
			default:
				SMSSTART(theSoundID);
				break;
		}
	}
}


/*========================================================  PlayIncidentSoundSMS  */

void PlayIncidentSoundSMS (short theSoundID)

{
	if ((smsActivated) && (soundOn) && (incidentSoundOn) && 
			(incidentSoundLoaded[theSoundID - kMaxNumberOfSounds]))
	{
		if ((netGameInSession) && (imTheMaster) && (lastSoundPriority < 10))
		{
			wasLastSound = theSoundID;
			lastSoundPriority = 10;
		}
		
		switch (theSoundID)
		{
			case 0:
				break;
			case kBeerManSound:
			case kHotDogSound:
			case kProgramSound:
				if (soundFileRefNum != -1)
				{
					SMSSTART(theSoundID);
				}
				break;
			default:
				break;
		}
	}
}


/*========================================================  FlushSoundQueues  */

void FlushSoundQueues (void)

{
	crowdQueue.number = 0;
	crowdQueue.when = 0;
	crowdQueue.priority = 0;
	incidentalQueue.number = 0;
	incidentalQueue.when = 0;
	incidentalQueue.priority = 0;
}


/*========================================================  BeginGamePointSound  */

void BeginGamePointSound (void)

{
	incidentalQueue.number = kGameSound;
	incidentalQueue.when = 180;
	incidentalQueue.priority = 100;
}


/*========================================================  BeginAllTiedSound  */

void BeginAllTiedSound (void)

{
	incidentalQueue.number = kAllSound;
	incidentalQueue.when = 180;
	incidentalQueue.priority = 100;
}


/*========================================================  StartCrowdSound  */

void StartCrowdSound (void)

{
	if ((smsActivated) && (crowdSoundOn) && (soundOn) && (whichGame > kPracticeScoring))
	{
		SMSSTARTCHAN(kCrowdSound, 3);
		crowdQueue.number = 0;
		crowdQueue.when = 0;
	}
}


/*========================================================  StartApplauseSound  */

void StartApplauseSound (void)

{
	if ((smsActivated) && (crowdSoundOn) && (soundOn) && (whichGame > kPracticeScoring))
	{
		SMSSTARTCHAN(kCrowdSwellSound, 3);
		crowdQueue.number = kApplauseSound;
		crowdQueue.when = kApplauseSwellTicks;
	}
}


/*========================================================  Applaud  */

void Applaud (void)

{
	if ((smsActivated) && (crowdSoundOn) && (soundOn) && (whichGame > kPracticeScoring))
	{
		SMSSTARTCHAN(kApplauseSound, 3);
		crowdQueue.number = kCrowdFadeSound;
		crowdQueue.when = 40;
	}
}


/*========================================================  StartMobSound  */

void StartMobSound (void)

{
	if ((smsActivated) && (crowdSoundOn) && (soundOn) && (whichGame > kPracticeScoring))
	{
		SMSSTARTCHAN(kMobSwellSound, 3);
		crowdQueue.number = kMobSound;
		crowdQueue.when = kMobSwellTicks;
	}
}


/*========================================================  HandleCrowdSound  */

void HandleCrowdSound (void)

{
	if (crowdQueue.when == 0)
		return;
	
	crowdQueue.when--;
	
	if ((smsActivated) && (crowdQueue.when == 0) && (crowdSoundOn) && (soundOn) && 
			(whichGame > kPracticeScoring))
	{
		switch (crowdQueue.number)
		{
			case kApplauseSound:
				SMSSTARTCHAN(kApplauseSound, 3);
				crowdQueue.number = kCrowdFadeSound;
				crowdQueue.when = lengthOfApplause;
				break;
			case kMobSound:
				SMSSTARTCHAN(kMobSound, 3);
				crowdQueue.number = kMobFadeSound;
				crowdQueue.when = lengthOfMob;
				break;
			case kCrowdFadeSound:
				SMSSTARTCHAN(kCrowdFadeSound, 3);
				crowdQueue.number = kCrowdSound;
				crowdQueue.when = kApplauseFadeTicks;
				break;
			case kMobFadeSound:
				SMSSTARTCHAN(kMobFadeSound, 3);
				crowdQueue.number = kCrowdSound;
				crowdQueue.when = kMobFadeTicks;
				break;
			case kCrowdSound:
				SMSSTARTCHAN(kCrowdSound, 3);
				crowdQueue.number = 0;
				crowdQueue.when = 0;
				break;
		}
	}
}


/*========================================================  StopCrowdSound  */

void StopCrowdSound (void)

{
	if (smsActivated)
		SMSSTOPCHAN(3);
}


/*========================================================  ResumeCrowdSound  */

void ResumeCrowdSound (void)

{
	if ((smsActivated) && (crowdSoundOn) && (soundOn) && (whichGame > kPracticeScoring))
		SMSSTARTCHAN(kCrowdSound, 3);
}


/*========================================================  QueueUpIncidental  */

void QueueUpIncidental (short soundIDIs, short whenTo, short priorityIs)

{
	if (priorityIs >= incidentalQueue.priority)
	{
		incidentalQueue.number = soundIDIs;
		incidentalQueue.when = whenTo;
		incidentalQueue.priority = priorityIs;
	}
}


/*========================================================  HandleIncidentalQueue  */

void HandleIncidentalQueue (void)

{
	if (incidentalQueue.when == 0)
	{
		if (netGameInSession && !imTheMaster)
		{
			return;
		}
		else
		{
			incidentalQueue.number = RandomInt(kMaxIncidentalSounds) + kMaxNumberOfSounds;
			incidentalQueue.when = RandomInt(480) + 480;
			incidentalQueue.priority = 10;
			return;
		}
	}
	
	incidentalQueue.when--;
	
	if (incidentalQueue.when == 0)
	{
		switch (incidentalQueue.number)
		{
			case 0:
				break;
			case kGameSound:
				PlaySoundSMS(kGameSound);
				if (gameIsOver)
					incidentalQueue.number = kOverSound;
				else
					incidentalQueue.number = kPointSound;
				incidentalQueue.when = 90;
				incidentalQueue.priority = 100;
				break;
			case kAllSound:
				PlaySoundSMS(kAllSound);
				incidentalQueue.number = kTiedSound;
				incidentalQueue.when = 90;
				incidentalQueue.priority = 100;
				break;
			case kPointSound:
			case kTiedSound:
			case kOverSound:
				PlaySoundSMS(incidentalQueue.number);
				incidentalQueue.number = 0;
				incidentalQueue.when = 0;
				incidentalQueue.priority = 0;
				break;
			case kBeerManSound:
			case kHotDogSound:
			case kProgramSound:
				if (soundFileRefNum != -1)
				{
					PlayIncidentSoundSMS(incidentalQueue.number);
					incidentalQueue.number = 0;
					incidentalQueue.when = 0;
					incidentalQueue.priority = 0;
				}
				break;
			default:
				PlaySoundSMS(incidentalQueue.number);
				incidentalQueue.number = 0;
				incidentalQueue.when = 0;
				incidentalQueue.priority = 0;
				break;
		}
	}
}



