//=============================================================================
// ChallengeVoicePack.
//=============================================================================
class ChallengeVoicePack expands VoicePack
	abstract;

var() Sound NameSound[16]; // sounds to play for call signs (string from Orders Menu)
var() float NameTime[16];

var() Sound AckSound[16]; // acknowledgement sounds
var() localized string[64] AckString[16];
var() float AckTime[16];
var() int numAcks;

var() Sound FFireSound[16];
var() localized string[64] FFireString[16];
var() float FFireTime[16];
var() int numFFires;

var() Sound TauntSound[32];
var() localized string[64] TauntString[32];
var() float TauntTime[32];
var() int numTaunts;

/* Orders (in same order as in Orders Menu 
	0 = Defend, 
	1 = Hold, 
	2 = Attack, 
	3 = Follow, 
	4 = FreeLance
*/
var() Sound OrderSound[16];
var() localized string[64] OrderString[16];
var() float OrderTime[16];

/* Other messages - use passed messageIndex
	0 = Base Undefended
	1 = Get Flag
	2 = Got Flag
	3 = Back up
	4 = Im Hit
	5 = Under Attack
	6 = Man Down
*/
var() Sound OtherSound[32];
var() localized string[64] OtherString[32];
var() float OtherTime[32];

var Sound Phrase[8];
var float PhraseTime[8];
var int PhraseNum;
var string[200] DelayedResponse;
var bool bDelayedResponse;
var Actor DelayedLoc;
var PlayerReplicationInfo DelayedSender;

function string[32] GetCallSign(byte ID)
{
	if ( ID > 10 )
		return class'TeamGamePlus'.Default.TeamColor[ID-11]$class'ChallengeOrdersMenu'.Static.GetCallSigns(0);
	else
		return class'ChallengeOrdersMenu'.Static.GetCallSigns(ID);
}

function Initialize(PlayerReplicationInfo Sender, PlayerReplicationInfo Recipient, name messagetype, byte messageIndex, Actor Location)
{
	local int n, m;
	local Sound MessageSound;
	local float MessageTime;

	//log("Voice message from "$Sender.PlayerName$" to "$Recipient.PlayerName$" of type "$MessageType);
	DelayedSender = Sender;
	DelayedLoc = Location;
	bDelayedResponse = true;

	if ( messagetype == 'ACK' )
		SetAckMessage(Rand(numAcks), Recipient, MessageSound, MessageTime);
	else
	{
		messageIndex = Clamp(messageIndex, 0, 15);
		SetTimer(0.1, false);
		if ( recipient != None )
		{
			if ( recipient.TeamID > 0 )
			{
				Phrase[0] = NameSound[recipient.TeamID];
				PhraseTime[0] = NameTime[recipient.TeamID];
			}
			else
			{
				Phrase[0] = NameSound[recipient.Team + 11 ];
				PhraseTime[0] = NameTime[recipient.Team + 11];
			}
			m = 1;
			DelayedResponse = GetCallSign(Recipient.TeamID)$", ";
		}	
		else
			m = 0;
		if ( messagetype == 'FRIENDLYFIRE' )
			SetFFireMessage(Rand(numFFires), Recipient, MessageSound, MessageTime);
		else if ( messagetype == 'TAUNT' )
			SetTauntMessage(Rand(numTaunts), Recipient, MessageSound, MessageTime);
		else if ( messagetype == 'ORDER' )
			SetOrderMessage(messageIndex, Recipient, MessageSound, MessageTime);
		else // messagetype == Other
			SetOtherMessage(messageIndex, Recipient, MessageSound, MessageTime);

		Phrase[m] = MessageSound;
		PhraseTime[m] = MessageTime;
	}
}

function ClientInitialize(PlayerReplicationInfo Sender, PlayerReplicationInfo Recipient, name messagetype, byte messageIndex, Actor Location)
{
	local int n, m;
	local Sound MessageSound;
	local float MessageTime;

	DelayedSender = Sender;
	DelayedLoc = Location;
	bDelayedResponse = true;
	SetTimer(0.1, false);

	if ( messagetype == 'ACK' )
		SetClientAckMessage(messageIndex, Recipient, MessageSound, MessageTime);
	else if ( messagetype == 'FRIENDLYFIRE' )
		SetClientFFireMessage(messageIndex, Recipient, MessageSound, MessageTime);
	else if ( messagetype == 'TAUNT' )
		SetClientTauntMessage(messageIndex, Recipient, MessageSound, MessageTime);
	else if ( messagetype == 'ORDER' )
		SetClientOrderMessage(messageIndex, Recipient, MessageSound, MessageTime);
	else // messagetype == Other
		SetClientOtherMessage(messageIndex, Recipient, MessageSound, MessageTime);

	Phrase[0] = MessageSound;
	PhraseTime[0] = MessageTime;
}

function SetClientAckMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	messageIndex = Clamp(messageIndex, 0, numAcks-1);

	if (Recipient != None)
		DelayedResponse = AckString[messageIndex]$", "$GetCallsign(Recipient.TeamID);
	else
		DelayedResponse = AckString[messageIndex];

	MessageSound = AckSound[messageIndex];
	MessageTime = AckTime[messageIndex];

	if (Recipient != None)
	{
		if ( recipient.TeamID > 0 )
		{
			Phrase[1] = NameSound[Recipient.TeamID];
			PhraseTime[1] = NameTime[Recipient.TeamID];
		}
		else
		{
			Phrase[1] = NameSound[Recipient.Team + 11];
			PhraseTime[1] = NameTime[Recipient.Team + 11];
		}
	}
}

function SetAckMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	local int r;

	r = Rand(numAcks);
	DelayedResponse = AckString[r]$", "$GetCallSign(recipient.TeamID);
	SetTimer(3 + FRand(), false); // wait for initial order to be spoken
	Phrase[0] = AckSound[r];
	PhraseTime[0] = AckTime[r];
	if ( recipient.TeamID > 0 )
	{
		Phrase[1] = NameSound[recipient.TeamID];
		PhraseTime[1] = NameTime[recipient.TeamID];
	}
	else
	{
		Phrase[1] = NameSound[recipient.Team + 11 ];
		PhraseTime[1] = NameTime[recipient.Team + 11];
	}
}

function SetClientFFireMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	messageIndex = Clamp(messageIndex, 0, numFFires-1);

	DelayedResponse = DelayedResponse$FFireString[messageIndex];
	MessageSound = FFireSound[messageIndex];
	MessageTime = FFireTime[messageIndex];
}

function SetFFireMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	local int r;

	r = Rand(numFFires);
	DelayedResponse = DelayedResponse$FFireString[r];
	MessageSound = FFireSound[r];
	MessageTime = FFireTime[r];
}

function SetClientTauntMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	messageIndex = Clamp(messageIndex, 0, numTaunts-1);

	DelayedResponse = DelayedResponse$TauntString[messageIndex];
	MessageSound = TauntSound[messageIndex];
	MessageTime = TauntTime[messageIndex];
}

function SetTauntMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	local int r;

	r = Rand(numTaunts);
	DelayedResponse = DelayedResponse$TauntString[r];
	MessageSound = TauntSound[r];
	MessageTime = TauntTime[r];
	SetTimer(1.0, false);
}

function SetClientOrderMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	DelayedResponse = DelayedResponse$OrderString[messageIndex];
	MessageSound = OrderSound[messageIndex];
	MessageTime = OrderTime[messageIndex];
}

function SetOrderMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	DelayedResponse = DelayedResponse$OrderString[messageIndex];
	MessageSound = OrderSound[messageIndex];
	MessageTime = OrderTime[messageIndex];
}

function SetClientOtherMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	DelayedResponse = DelayedResponse$OtherString[messageIndex];
	MessageSound = OtherSound[messageIndex];
	MessageTime = OtherTime[messageIndex];
}

function SetOtherMessage(int messageIndex, PlayerReplicationInfo Recipient, out Sound MessageSound, out Float MessageTime)
{
	DelayedResponse = DelayedResponse$OtherString[messageIndex];
	MessageSound = OtherSound[messageIndex];
	MessageTime = OtherTime[messageIndex];
}

function Timer()
{
	local name MessageType;

	if ( bDelayedResponse )
	{
		bDelayedResponse = false;
		if ( Owner.IsA('PlayerPawn') )
		{
			if ( PlayerPawn(Owner).PlayerReplicationInfo.Team == DelayedSender.Team )
				MessageType = 'TeamSay';
			else
				MessageType = 'Say';
			PlayerPawn(Owner).TeamMessage(DelayedSender, DelayedResponse, ZoneInfo(DelayedLoc), MessageType);
		}
	}
	if ( Phrase[PhraseNum] != None )
	{
		if ( Owner.IsA('PlayerPawn') && !PlayerPawn(Owner).bNoVoices ) {
			//PlayerPawn(Owner).ClientPlaySound(Phrase[PhraseNum]);
			//PlayerPawn(Owner).PlaySound(Phrase[PhraseNum], SLOT_None, 255, true);
		}
		if ( PhraseTime[PhraseNum] == 0 )
			Destroy();
		else
		{
			SetTimer(PhraseTime[PhraseNum], false);
			PhraseNum++;
		}
	}
	else 
		Destroy();
}

function PlayerSpeech( int Type, int Index, int Callsign )
{
	local name SendType;
	local PlayerReplicationInfo Recipient, SearchPRI;
	local string[32] ReCallsign;

	// Types:
	//	0 - Acknowledgement
	//	1 - Friendly Fire
	//	2 - Orders
	//	3 - Taunt
	//	4 - Other
	//  Log("PlayerSpeech, Type: "$Type$" Index: "$Index$" Callsign: "$Callsign);
	if (Type == 0) {
		SendType = 'ACK';
	} else if (Type == 1) {
		SendType = 'FRIENDLYFIRE';
	} else if (Type == 2) {
		SendType = 'ORDER';
	} else if (Type == 3) {
		SendType = 'TAUNT';
	} else if (Type == 4) {
		SendType = 'OTHER';
	}

	foreach AllActors(class'PlayerReplicationInfo', SearchPRI)
		if (SearchPRI.TeamID == Callsign - 1)
			Recipient = SearchPRI;

	if (SendType == 'TAUNT')
		Recipient = none;

	if (Recipient == None)
		Pawn(Owner).SendVoiceMessage( Pawn(Owner).PlayerReplicationInfo, None, SendType, Index, 'GLOBAL' );
	else
		Pawn(Owner).SendVoiceMessage( Pawn(Owner).PlayerReplicationInfo, Recipient, SendType, Index, 'GLOBAL' );
}
