///////////////////////////////////////////////////////////////////////////////////////
// -- VOICE SCRIPT AND CLASS SWITCH SCRIPTS, WILL BE ADDED SOON -- //
///////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////
// - VOICE CHANGE SCRIPT ------- //
/////////////////////////////////////////////////////////////
// - Credit to TMC and ZzZombo for helping with this //
/////////////////////////////////////////////////////////////

#library "VOICES"
#include "zcommon.acs"
#import "zt2functions.acs"

#define VOICE_COUNT 5

//-- SCOPES IN USE: ----------------
// 1-17
// 20-27
// 30-43
// 44-49 <-- VOICES
// 50-60

#define MAX_VOICES 100

// Names of the voicepacks
str voiceNames[MAX_VOICES];
// Reloading sounds
str voiceSoundReload[MAX_VOICES];
// Taunt
str voiceSoundTaunt[MAX_VOICES];
// Pain
str voiceSoundPain[MAX_VOICES];
// How long each taunt takes
int voiceTauntTimes[MAX_VOICES];
// How many voicepacks we actually have
int voicePackCount;

// -- VARIABLES --

#define VOICE_ID			385
#define SECTION_ID			390
#define SECTION_BG_ID		400

// How many voices on either side of the selection to display
#define SECTION_PADDING		2

// -- VOICE SYSTEM FUNCTIONS -----------------------------------------------------

// Add a new voice
// Name, Reload Sound, Taunt Sound, Pain Sound, Taunt Time
function void AddVoicepack(str vName, str sReload, str sTaunt, str sPain, int tTime)
{
	voiceNames[voicePackCount] = vName;
	voiceSoundReload[voicePackCount] = sReload;
	voiceSoundTaunt[voicePackCount] = sTaunt;
	voiceSoundPain[voicePackCount] = sPain;
	voicePackCount ++;
}

// Remove all voicepacks
function void ClearVoicepacks(void)
{
	voicePackCount = 0;
}

function void CompileSection(int Selected)
{
	// str sectionString = "\cDAvailable Voicepacks:\n\n";
	str color = "\cM";
	str pic = "VOICEBLK";
	int startX, startY;
	int sectionHeight;
	int counter;
	
	int adder;
	int totalHeight;
	
	totalHeight = (SECTION_PADDING*2)+1;
	
	counter = 0;
	
	startX = 0.0;
	startY = FixedMul(CheckInventory("ResolutionY")*1.0,0.25);
	
	sectionHeight = 26.0 * totalHeight;
	startY = startY - FixedDiv(sectionHeight,2.0);
	
	sectionHeight = 26.0;
	
	if (Selected == 0)
		adder = totalHeight-1;
	else if (Selected == 1)
		adder = totalHeight-2;
	else
		adder = SECTION_PADDING;
	
	for (int l=Selected-SECTION_PADDING; l<=Selected+adder; l++)
	{
		SetFont(pic);
		HMUSRedux(startX, startY+(sectionHeight*counter), "", 0, 1, SECTION_BG_ID+counter, 2.0, 0);
		SetFont("BIGFONT");
		HMUSRedux(startX+16.0, startY+(sectionHeight*counter), "", 0, 1, SECTION_ID+counter, 2.0, 0);
		SetFont("SMALLFONT");
		
		if (l >= voicePackCount)
			break;
		
		if (l < 0)
			continue;
			
		if (l == Selected)
		{
			color = "\cK";
			pic = "VOICERED";
		}
		else
		{
			color = "\cM";
			pic = "VOICEBLK";
		}
		
		SetFont(pic);
		HMUSRedux(startX, startY+(sectionHeight*counter), "a", 0, 1, SECTION_BG_ID+counter, 2.0, 0);
		SetFont("BIGFONT");
		HMUSRedux(startX+16.0, startY+(sectionHeight*counter), StrParam(s:color,d:l,s:".) ",s:voiceNames[l]), 0, 1, SECTION_ID+counter, 2.0, 0);
		SetFont("SMALLFONT");
		
		counter ++;
	}
}

//----------------------------------

// Wow, we should probably get the player's stored voice eh?
// That might be a good idea you know
script "FindMyVoice" ENTER
{
	str StoredVoice = GetUserCVarString(PlayerNumber(),"cl_zt2voice");
	
	for (int l=0; l<voicePackCount; l++)
	{
		if (voiceNames[l] == StoredVoice)
			ACS_NamedExecuteAlways("AssignCertainVoice",0,l,0,0);
	}
}

// We found a voice and we need to assign it
script "AssignCertainVoice" (int voiceNum)
{
	TakeInventory("PlayerVoice",999);
	GiveInventory("PlayerVoice",voiceNum);
}

// Add the actual voices
// GIT: Changed to named script to be more unique
script "ZT2_AddBaseVoices" OPEN
{
	// BO2 Merc
	AddVoicepack("CoD:BO2 (Merc)","merc/reload","merc/taunt","merc/pain",70);
	// KF Male
	AddVoicepack("Killing Floor (Human)","kfmale/reload","kfmale/taunt","kfmale/pain",70);
	// KF D.A.R.
	AddVoicepack("Killing Floor (D.A.R.)","dar/reload","dar/taunt","dar/pain",70);
	// Cod:MW American
	AddVoicepack("CoD:MW (American)","usa/reload","usa/taunt","rga/pain",70);
	// CoD:WAW German
	AddVoicepack("CoD:WAW (German)","german/reload","german/taunt","german/pain",140);
}

// Can be puked via the console, what is this even
// YIKES MUCHACHO, WHY IS THIS CLIENTSIDE???
script 818 (int casing) NET CLIENTSIDE
{
	switch (casing)
	{
		case 0:
		NamedRequestScriptPuke(819,0,0,0);
		break;

		case 1:
		ACS_ExecuteAlways(820,0,0,0,0);
		break;

		case 2:
		ACS_ExecuteAlways(820,0,1,0,0);
		break;

		case 3:
		ACS_ExecuteAlways(820,0,2,0,0);
		break;
	}
}

script "SetVoicePreference" (int pref) //CLIENTSIDE
{
	LocalAmbientSound(voiceSoundReload[CheckInventory("PlayerVoice")],127);
	
	// Show a message
	SetFont("BIGWRITR");
	HudMessage(s:"\cKVoice changed:\n\cD",s:voiceNames[CheckInventory("PlayerVoice")];HUDMSG_FADEOUT,VOICE_ID,CR_WHITE,1.5,0.8,2.0,0.5);
	SetFont("BIGFONT");
	
	CompileSection(CheckInventory("PlayerVoice"));
}

// -- CHANGE VOICES --
script 819 (void) NET
{
	// Actually control the junk
	if (CheckInventory("PlayerVoice") < voicePackCount-1)
		GiveInventory("PlayerVoice",1);
	else
		TakeInventory("PlayerVoice",99);
		
	SetUserCVarString(PlayerNumber(),"cl_zt2voice",voiceNames[CheckInventory("PlayerVoice")]);
	ACS_NamedExecuteAlways("SetVoicePreference",0,CheckInventory("PlayerVoice"),0,0);	
}

// -- CALLED FROM TAUNTING AND THINGS --
// What's the point of using a separate script, I mean really
script 820 (int args) NET CLIENTSIDE
{
	int useReload = GetCVar("cl_noreloadsound");
	NamedRequestScriptPuke("PlayVoiceSound",args,useReload,ActivatorTID());
}

//-- PLAY THE ACTUAL SOUND --
script "PlayVoiceSound" (int soundmode, int useReload, int originalPlayer) NET
{
	int voice;
	int value;

	value = GetCVar("cl_noreloadsound");
	voice = CheckInventory("PlayerVoice");

	switch (soundmode)
	{
		//-- RELOAD --
		case 0:
			// If cl_noreloadsound is 0...
			if (!useReload)
				ThingSound(originalPlayer,voiceSoundReload[voice],127);
		break;

		//-- TAUNT --
		case 1:
			if (!CheckInventory("TauntTimer"))
			{
				ThingSound(originalPlayer,voiceSoundTaunt[voice],127);
				GiveInventory("TauntTimer",voiceTauntTimes[voice]);
			}
		break;

		// -- PAIN --
		case 2:
				ThingSound(originalPlayer,voiceSoundPain[voice],127);
		break;
	}
}