//===================================================
// Z O M B I E   T O R R E N T   2
// SLOT MACHINE SYSTEM
//
// Coded by Zedek the Plague Doctor
// Inspired by Marco's slot machines for KF
//===================================================

#library "SLOTSYSTEM"
#import "zt2functions.acs"
#import "slotcards.acs"
#include "zcommon.acs"

// Initial delay between selecting icons
#define SLOT_DELAY 1.0
// How many ticks we have to use before we actually roll a slot
#define SLOT_MAXTICKS 55
// How much to increase the delay by each roll
#define SLOT_INCREASER 1.05
// Chance of always winning (0.8 is 20% chance)
//#define SLOT_WINCHANCE 0.5
// How much time we have to wait after we roll
#define SLOT_ENDDELAY 35

//--MESSAGE IDS------------------------------------
#define ID_DENIED 19
#define ID_ICON1 20
#define ID_ICON2 21
#define ID_ICON3 22
#define ID_CARDTITLE 23
#define ID_CARDINFO 24

// Size of the card icons
#define CARD_SIZE 64.0

script 477 (int resX, int resY) NET
{
	TakeInventory("ResolutionX",9999);
	TakeInventory("ResolutionY",9999);
	
	GiveInventory("ResolutionX",resX);
	GiveInventory("ResolutionY",resY);
	
	//Print(s:"Resolution set: ",d:CheckInventory("ResolutionX"),s:"x",d:CheckInventory("ResolutionY"));
}

// Clientsided script - responsible for resolution
script 478 (void) CLIENTSIDE
{
	//Log(s:"Sending res to server: ",d:GetCVar("vid_defwidth"),s:"x",d:GetCVar("vid_defheight"));
	delay(1);
	RequestScriptPuke(477,GetCVar("vid_defwidth"),GetCVar("vid_defheight"),0);
}

// 479 - CHECK FOR SLOT ROLLS (SERVERSIDE)
// CALL THIS SCRIPT FIRST (Works on each player)
script 479 ENTER
{
	delay(5);
	
	while (1)
	{
		if (GetCVar("sv_slotenabled"))
		{
			if (CheckInventory("SlotKills") >= GetCVar("sv_slotmonsters"))
			{
				TakeInventory("SlotKills",999);
				ACS_Execute(480,0,GetCVar("sv_slotwinchance"), GetCVar("sv_slotbadchance")*1.0, GetCVar("sv_slotgoodchance")*1.0);
			}
		}
		
		delay(1);
	}
}

// 480 - Clientside slot roll effects
script 480 (int winchance, int badchance, int goodchance)
{
	// Imagine ticks on a wheel, this is how long the slot lasts
	int slotTicks;
	// Time between selecting icons
	int slotDelay = SLOT_DELAY;
	// Which icons we have selected
	int icon1;
	int icon2;
	int icon3;
	// Whether or not we're forcing a winner
	int winForced;
	// Used in forcing
	int finale = -1;
	// The last icons we selected
	int last1;
	int last2;
	int last3;
	// Used for good and bad
	int percentage;
	// Slowly slide the bar down from the top of the screen
	int slotOffset = -100.0;
	
	winForced = 0;
	
	// Force the slots to pick a winning combination
	if (Random(0.0,1.0) >= 1.0-(winchance*0.01) )
	{
		winForced = 1;
		
		if (totalCards > 1)
			finale = Random(0,totalCards-1);
		else
			finale = 0;
	}
	
	// Cycle through our slots
	while (slotTicks < SLOT_MAXTICKS)
	{
		if (slotOffset < 0)
		{
			slotOffset += 2.0;
		}
		
		// Should we force the slot machines to win?
		// Make sure we're on the last tick, plus we're forcing it already
		if (slotTicks == SLOT_MAXTICKS-1 && winForced)
		{
			// Set all icons to a single value
			icon1 = finale;
			icon2 = finale;
			icon3 = finale;
		}
		else
		{
			percentage = Random(0.0,1.0);
			// Randomize the icons
			if (totalCards > 1)
			{
				until (icon1 != last1 && ( /* Bad chance */ (cardType[icon1] == 3 && percentage >= 1.0-badchance) || /* Good chance */ (cardType[icon1] == 2 && percentage >= 1.0-goodchance) || /* Normal */ cardType[icon1] == 0 || cardType[icon1] == 1 ))
				{
					icon1 = Random(0,totalCards-1);
				}
				
				percentage = Random(0.0,1.0);
				until (icon2 != last2 && ( /* Bad chance */ (cardType[icon2] == 3 && percentage >= 1.0-badchance) || /* Good chance */ (cardType[icon2] == 2 && percentage >= 1.0-goodchance) || /* Normal */ cardType[icon2] == 0 || cardType[icon2] == 1 ))
				{
					icon2 = Random(0,totalCards-1);
				}
				
				percentage = Random(0.0,1.0);
				until (icon3 != last3 && ( /* Bad chance */ (cardType[icon2] == 3 && percentage >= 1.0-badchance) || /* Good chance */ (cardType[icon3] == 2 && percentage >= 1.0-goodchance) || /* Normal */ cardType[icon3] == 0 || cardType[icon3] == 1 ))
				{
					icon3 = Random(0,totalCards-1);
				}
				
				if (icon3 > totalCards-1)
					icon3 = totalCards-1;
				if (icon2 > totalCards-1)
					icon2 = totalCards-1;
				if (icon1 > totalCards-1)
					icon1 = totalCards-1;
			}
			else
			{
				icon1 = 0;
				icon2 = 0;
				icon3 = 0;
			}
			
			LocalAmbientSound("c4/click",128);
			
			if (GetCVar("cl_slotdebug"))
				Print(d:icon1,s:",",d:icon2,s:",",d:icon3);
		}
		
		last1 = icon1;
		last2 = icon2;
		last3 = icon3;
		
		//Print(d:icon1,s:",",d:icon2,s:",",d:icon3);
		
		//--SHOW THE ICONS ON THE SCREEN-----------------------------
		int SW;
		int SH;

		// SW = FixedDiv(GetCVar("vid_defwidth")*1.0,2.0);
		// SH = GetCVar("vid_defheight")*1.0;
		SW = FixedDiv(CheckInventory("ResolutionX")*1.0,2.0);
		SH = CheckInventory("ResolutionY")*1.0;
		
		//Print(d:GetUserCVar(PlayerNumber(),"vid_defwidth"),s:",",d:GetUserCVar(PlayerNumber(),"vid_defheight"));
		
		SetFont(cardIcon[icon1]);
		HMUSRedux(SW, 100.0+slotOffset, "a", 1, 1, ID_ICON1, 10.0,0);
		SetFont(cardIcon[icon2]);
		HMUSRedux(SW-CARD_SIZE, 100.0+slotOffset, "a", 1, 1, ID_ICON2, 10.0,0);
		SetFont(cardIcon[icon3]);
		HMUSRedux(SW+CARD_SIZE, 100.0+slotOffset, "a", 1, 1, ID_ICON3, 10.0,0);
		SetFont("SMALLFONT");

		// Delay a little bit for the next slot
		slotTicks ++;
		slotDelay = FixedMul(slotDelay,SLOT_INCREASER);
		
		delay(round(slotDelay));
	}
	
	delay(SLOT_ENDDELAY);
	
	// Did we win?
	// All icons must match in order to do something
	if (icon1 == icon2 && icon2 == icon3)
	{
		LocalAmbientSound(cardSound[icon1],128);
		// Flash all of the boxes white and fade them
		SetFont("SLOTWHIT");
		HMUSRedux(SW, 100.0+slotOffset, "a", 1, 1, ID_ICON1, 0.1,0);
		HMUSRedux(SW-CARD_SIZE, 100.0+slotOffset, "a", 1, 1, ID_ICON2, 0.1,0);
		HMUSRedux(SW+CARD_SIZE, 100.0+slotOffset, "a", 1, 1, ID_ICON3, 0.1,0);
		SetFont("SMALLFONT");
		// Actually call our script
		
		ACS_ExecuteAlways(cardScript[icon1],0,cardCaseID[icon1],0,0);
		
		SetFont("typewrit");
		HMUSRedux(SW, 200.0+slotOffset, cardDisplayName[icon1], 1, 1, ID_DENIED, 2.0,0);
		SetFont("bigfont");
		HMUSRedux(SW, 200.0+slotOffset+24.0, cardDescription[icon1], 1, 1, ID_DENIED-1, 2.0,0);
		
		//Print(s:cardDisplayName[icon1],s:" - ",s:cardDescription[icon1]);
	}
	// We lost, oh shit
	else
	{
		LocalAmbientSound("slot/failed",128);
		SetFont(cardIcon[icon1]);
		HMUSRedux(SW, 100.0+slotOffset, "a", 1, 1, ID_ICON1, 2.0,0);
		SetFont(cardIcon[icon2]);
		HMUSRedux(SW-CARD_SIZE, 100.0+slotOffset, "a", 1, 1, ID_ICON2, 2.0,0);
		SetFont(cardIcon[icon3]);
		HMUSRedux(SW+CARD_SIZE, 100.0+slotOffset, "a", 1, 1, ID_ICON3, 2.0,0);
		
		SetFont("SLOTFAIL");
		HMUSRedux(SW, 100.0+slotOffset, "a", 1, 1, ID_DENIED, 2.0,0);
		SetFont("SMALLFONT");
	}
	
	terminate;
}