Scriptname _SAG_Activ_DiceBlackJackMainsScript extends Quest  

_00E_TavernGames_GamlingTableSC objGamblingTable

;=====================================================================================
;              							 FUNCTIONS                   					 
;=====================================================================================

Function InitiateGame(_00E_TavernGames_GamlingTableSC objGamblingTableRef)

	objGamblingTable = objGamblingTableRef

	; _SAG_Activ_BlackJackDiceQuest

	s_chargeninitialtext = Game.GetGameSettingString("sCharGenControlsDisabled")
	Game.SetGameSettingString("sCharGenControlsDisabled", "")
	Commentator.GetActorBase().SetVoiceType(Opponent.GetActorReference().GetVoiceType())
	Commentator.SetName(Opponent.GetReference().GetDisplayName())
	Commentator.SetDisplayName(Opponent.GetReference().GetDisplayName(), true)
	initialFurn = PlayerRef.GetFurnitureReference()
	game.DisablePlayerControls(true, true, true, true, true, false, true, false, 0)
	game.ForceFirstPerson()
	game.SetInChargen(true, true, true)
	PlayerPosition.MoveTo(PlayerRef as objectreference, 0.000000, 0.000000, 0.000000, true)
	utility.Wait(0.300000)
	PlayerRef.MoveTo(Marker, 0.000000, 0.000000, 0.000000, true)
	iStartGold = Opponent.GetReference().GetItemcount(Gold001)
	
	if Opponent.GetReference().IsDisabled()
		Opponent.GetReference().Enable(false)
	endIf
	
	Commentator.Say(GenericDialogue_Gambling_StartGameTopic)
	Utility.Wait(5.0)
	
	Step01()
	
EndFunction

Function Step01()

	if Opponent.GetReference().GetItemcount(Gold001) >= iStartGold*3
		Opponent.GetReference().RemoveItem(Gold001, 30000)
		Opponent.GetReference().AddItem(Gold001, iStartGold*3)
	EndIf

	Message.ResetHelpMessage("TossCoinTailsEvent")
	Message.ResetHelpMessage("TossCoinHeadEvent")
	PlayerThrowing = 0
	OpponentTHrowing = 0
	PlayerScore = 0
	OpponentScore = 0
		
	if PlayerRef.GetItemcount(gold001) < Opponent.GetReference().GetItemCount(gold001)
		MaxBet = PlayerRef.GetItemcount(gold001) / 2
	elseif PlayerRef.GetItemcount(gold001) >= Opponent.GetReference().GetItemCount(gold001)
		MaxBet = Opponent.GetReference().GetItemcount(gold001) / 2
	endif
	
	if EnderalFunctions.TextInputEnabled()
		PlayerBet = (((self as QUEST) as FORM) as UILIB_1).ShowTextInput(PlaceYourBet .GetName()+MaxBet, MaxBet) as int
	else
		float fPrevValue = PlayerRef.GetActorValue("Variable08")
		PlayerRef.SetActorValue("Variable08", MaxBet)
		int iBet = _SAG_SelectBetMessage.Show()
		PlayerRef.SetActorValue("Variable08", fPrevValue)
		
		if iBet == 0
			PlayerBet = 50
		elseif iBet == 1
			PlayerBet = 100
		elseif iBet == 2
			PlayerBet = 200
		elseif iBet == 3
			PlayerBet = 350
		elseif iBet == 4
			PlayerBet = 500
		elseif iBet == 5
			PlayerBet = 750
		elseif iBet == 6
			PlayerBet = 1000
		else
			PlayerBet = 0
		endif
	endif
	
	if PlayerBet <= 0 
		QuitGame()
	elseif PlayerBet > MaxBet
		Step01()
		Return
	else
		Step02()
	endif
		
EndFunction

Function Step02()

	MaxDice01 = Utility.RandomInt(0,5)
	MaxDice02 = Utility.RandomInt(0,5)
	MaxScore = (MaxDice01+MaxDice02+2) *3
	_00E_TavernGames_DiceRollM.Play(PlayerREF)
	Utility.Wait(1.5)
	MaxDices[MaxDice01].Enable(true)
	MaxDices2[MaxDice02].Enable(true)	
	Utility.Wait(2.0)
	MaxScoreMessage.Show(MaxDice01+MaxDice02+2,MaxScore)
	CoinToss()
	
EndFunction

Function CoinToss()

	CoinTossResult = Utility.RandomInt(0,1)
	Coin[CoinTossResult].Enable(true)
	
	if CoinTossResult == 0
		Head.ShowAsHelpMessage("TossCoinHeadEvent", 5.0, 1.0,1)
		Utility.Wait(3.0)
		PlayerTurn()
	else
		Tails.ShowAsHelpMessage("TossCoinTailsEvent", 5.0, 1.0,1)
		Utility.Wait(3.0)
		OpponentTurn()
	endif

EndFunction

Function PlayerTurn()

	PlayerPlayed = TRUE
	PlayerDice1[PlayerDice01].Disable()
	PlayerDice2[PlayerDice02].Disable()
	PlayerDice01 = Utility.RandomInt(0,5)
	PlayerDice02 = Utility.RandomInt(0,5)
	_00E_TavernGames_DiceRollM.Play(PlayerREF)
	Utility.Wait(1.5)
	PlayerDice1[PlayerDice01].Enable(true)
	PlayerDice2[PlayerDice02].Enable(true)
	PlayerScore += (PlayerDice01+PlayerDice02+2)
	PlayerThrowing += 1
		
	if PlayerScore > MaxScore
		_SAG_Activ_BJ_Fails.Value += 1
		_SAG_Activ_BJ_GoldLost.Value += PlayerBet *2
		PlayerDiceFail.Show(PlayerScore, MaxScore, PlayerBet*2)
		PlayerRef.RemoveItem(Gold001, PlayerBet*2, false, Opponent.GetReference())
		PlayAgain()
	elseif PlayerScore == MaxScore
		_SAG_Activ_BJ_Victories.Value += 1
		If _SAG_Activ_BJ_Victories.GetValueInt() == 10
			Steam.UnlockAchievement("END_DICEGOD_01")
		EndIf
		_SAG_Activ_BJ_GoldWon.Value += PLayerBet *2
		PlayerPerfectDice.Show(MaxScore, PlayerBet*2)
		Opponent.GetReference().RemoveItem(Gold001, PLayerBet*2)
		PlayerRef.AddItem(Gold001, PlayerBet*2)
		PlayAgain()
	elseif PlayerScore < MaxScore
	
	int ThrowAgainChoice = ThrowAgain.Show(PlayerScore, PlayerThrowing, OpponentScore, OpponentTHrowing, MaxScore)
			if ThrowAgainChoice == 0
				PlayerTurn()
			elseif THrowAgainCHoice == 1 && !OpponentPlayed
				OpponentTurn()
			elseif OpponentPlayed && ThrowAgainChoice == 1
				if OpponentScore == PlayerScore
					ExAequo.Show(PlayerBet)
					_SAG_Activ_BJ_ExAequo.Value += 1
					Commentator.Say(GenericDialogue_Gambling_PlayerWonTopic)
					Utility.Wait(4)
					PlayAgain()
				elseif OpponentScore > PlayerScore
					_SAG_Activ_BJ_Fails.Value += 1
					_SAG_Activ_BJ_GoldLost.Value += PlayerBet
					OpponentWon.Show(OpponentScore, MaxScore, PlayerScore, PlayerBet)
					PlayerRef.RemoveItem(Gold001, PlayerBet, false, Opponent.GetReference())
					Commentator.Say(GenericDialogue_Gambling_PlayerLostTopic)
					Utility.Wait(4)
					PlayAgain()
				elseif OpponentScore < PlayerScore
						_SAG_Activ_BJ_Victories.Value += 1
						If _SAG_Activ_BJ_Victories.GetValueInt() == 10
							Steam.UnlockAchievement("END_DICEGOD_01")
						EndIf
						_SAG_Activ_BJ_GoldWon.Value += PLayerBet
						PlayerWon.Show(PlayerScore, MaxScore, OpponentScore, PlayerBet)
						Opponent.GetReference().RemoveItem(Gold001, PLayerBet)
						PlayerRef.AddItem(Gold001, PlayerBet)
						Commentator.Say(GenericDialogue_Gambling_PlayerWonTopic)
						Utility.Wait(4)
						PlayAgain()
				endif
			endif
	endif

EndFunction

Function OpponentTurn()

	Utility.Wait(0.5)
	OpponentPlayed = TRUE
	OpponentDice1[OpponentDice01].Disable()
	OpponentDice2[OpponentDice02].Disable()
	OpponentDice01 = Utility.RandomInt(0,5)
	OpponentDice02 = Utility.RandomInt(0,5)
	_00E_TavernGames_DiceRollM.Play(PlayerREF)
	Utility.Wait(1.5)
	OpponentDice1[OpponentDice01].Enable(true)
	OpponentDice2[OpponentDice02].Enable(true)
	OpponentScore += (OpponentDice01+OpponentDice02+2)
	OpponentThrowing += 1
		
	if OpponentScore > MaxScore
		_SAG_Activ_BJ_Victories.Value += 1
		If _SAG_Activ_BJ_Victories.GetValueInt() == 10
			Steam.UnlockAchievement("END_DICEGOD_01")
		EndIf
		_SAG_Activ_BJ_GoldWon.Value += PlayerBet *2
		OpponentDiceFail.Show(OpponentScore, MaxScore, PlayerBet*2)
		Opponent.GetReference().RemoveItem(Gold001, PlayerBet*2)
		PlayerRef.AddItem(Gold001, PlayerBet*2)
		PlayAgain()
	elseif OpponentScore == MaxScore
		_SAG_Activ_BJ_Fails.Value += 1
		_SAG_Activ_BJ_GoldLost.Value += PlayerBet *2
		OpponentPerfectDice.Show(OpponentScore,PlayerBet*2)
		PlayerRef.RemoveItem(Gold001, PlayerBet*2, false, Opponent.GetReference())
		PlayAgain()
	elseif OpponentScore < MaxScore
		int RiskTreshold = Utility.RandomInt(5,9)
		if PlayerScore >= OpponentScore && MaxScore - OpponentScore >= RiskTreshold
			OpponentTurn()
		elseif PlayerScore <= OpponentScore && PlayerPlayed == FALSE && MaxScore - OpponentScore >= RiskTreshold
			OpponentTurn()
		elseif PlayerScore <= OpponentScore && PlayerPLayed == TRUE && MaxScore - OpponentScore >= RiskTreshold+5 
			OpponentTurn()
		elseif PlayerScore < OpponentScore && PlayerPlayed == TRUE && MaxScore - OpponentScore < RiskTreshold+5
			_SAG_Activ_BJ_Fails.Value += 1
			_SAG_Activ_BJ_GoldLost.Value += PlayerBet 
			OpponentWon.Show(OpponentScore, MaxScore, PlayerScore, PlayerBet)
			PlayerRef.RemoveItem(Gold001, PlayerBet, false, Opponent.GetReference())
			PlayAgain()
		elseif PlayerScore == OpponentScore && PlayerPlayed == TRUE && MaxScore - OpponentScore < RiskTreshold - 3
			ExAequo.Show(PlayerBet)
			_SAG_Activ_BJ_ExAequo.Value += 1
			PlayAgain()
		else
			if !PlayerPlayed
				PlayerTurn()
			else
				if PlayerPlayed && PlayerScore > OpponentScore
					_SAG_Activ_BJ_Victories.Value += 1
					If _SAG_Activ_BJ_Victories.GetValueInt() == 10
						Steam.UnlockAchievement("END_DICEGOD_01")
					EndIf
					_SAG_Activ_BJ_GoldWon.Value += PlayerBet 
					PlayerWon.Show(PlayerScore, MaxScore, OpponentScore, PlayerBet)
					PLayerRef.AddItem(Gold001, PlayerBet)
					Opponent.GetReference().RemoveItem(Gold001, PlayerBet)
					PlayAgain()
				elseif PlayerPlayed && PlayerScore == OpponentScore
					ExAequo.Show(PlayerBet)
					_SAG_Activ_BJ_ExAequo.Value += 1
					PlayAgain()	
				elseif PlayerPlayed && PlayerScore < OpponentScore
					_SAG_Activ_BJ_Fails.Value += 1
					_SAG_Activ_BJ_GoldLost.Value += PlayerBet 
					OpponentWon.Show(OpponentScore, MaxScore, PlayerScore, PlayerBet)
					PlayerRef.RemoveItem(Gold001, PlayerBet, false)
					Opponent.GetReference().AddItem(Gold001, PlayerBet)
					PlayAgain()						
				endif
			endif
		endif

	endif
	
EndFunction

Function PlayAgain()
if PlayerRef.GetItemCount(Gold001) >= 10 && Opponent.GetReference().GetItemCount(Gold001) >= 10
	int PlayAgainChoice = PlayAgainMessage.Show(PlayerRef.GetItemCount(gold001), Opponent.GetReference().GetItemCount(gold001))
	if PlayAgainChoice == 0
		MaxDices[MaxDice01].Disable()
		MaxDices2[MaxDice02].Disable()
		PlayerDice1[PlayerDice01].Disable()
		PlayerDice2[PlayerDice02].Disable()
		OpponentDice1[OpponentDice01].Disable()
		OpponentDice2[OpponentDice02].Disable()
		PlayerPlayed = FALSE
		OpponentPlayed = FALSE
		Coin[CoinTOssResult].Disable()
		Step01()
	else
		QuitGame()
	endif
else
	QuitGame()
endif
EndFunction

Function QuitGame()
	Game.SetGameSettingString("sCharGenControlsDisabled", s_chargeninitialtext)
if initialfurn
	PlayerRef.MoveTo(initialfurn)
else
	PlayerRef.MoveTo(Playerposition)
endif
	Game.EnablePlayerControls()
	Game.SetInChargen(abDisableSaving = false, abDisableWaiting = true, abShowControlsDisabledMessage = false)
	Opponent.Clear()
	MaxDices[MaxDice01].Disable()
	MaxDices2[MaxDice02].Disable()
	PlayerDice1[PlayerDice01].Disable()
	PlayerDice2[PlayerDice02].Disable()
	OpponentDice1[OpponentDice01].Disable()
	OpponentDice2[OpponentDice02].Disable()
	PlayerPlayed = FALSE
	OpponentPlayed = FALSE
	Coin[CoinTOssResult].Disable()
	objGamblingTable.SendGameFinishedEvent()
	
EndFunction

;=====================================================================================
;              							 PROPERTIES                   					 
;=====================================================================================

int iStartGold 

Topic Property GenericDialogue_Gambling_StartGameTopic Auto
Topic Property GenericDialogue_Gambling_PlayerLostTopic Auto
Topic Property GenericDialogue_Gambling_PlayerWonTopic Auto
Topic Property GenericDialogue_Gambling_PlayerCheatTopic Auto

Message Property Head Auto
Message Property Tails Auto
Message Property ThrowAgain Auto
Message Property OpponentWon Auto
Message Property PlayerWon Auto
Message Property ExAequo Auto
Message Property PlayAgainMessage Auto
Message Property PlayerPerfectDice Auto
Message Property OpponentPerfectDice Auto
Message Property PlayerDiceFail Auto
Message Property OpponentDiceFail Auto

GlobalVariable Property _SAG_Activ_BJ_ExAequo Auto
GlobalVariable Property _SAG_Activ_BJ_Fails Auto
GlobalVariable Property _SAG_Activ_BJ_GoldLost Auto
GlobalVariable Property _SAG_Activ_BJ_GoldWon Auto
GlobalVariable Property _SAG_Activ_BJ_Victories Auto

Message Property PlaceYourBet Auto
Message Property _SAG_SelectBetMessage  Auto  
String s_chargeninitialtext
Int OpponentTHrowing = 0
Int PlayerTHrowing = 0
Message Property MaxScoreMessage Auto
MiscObject Property Gold001 Auto
Sound Property _00E_TavernGames_DiceRollM Auto
Int MaxBet
Int PlayerBet
Int PlayerScore
Int OpponentScore
Int MaxScore
Actor Property PlayerRef Auto
ObjectReference Property PlayerPosition Auto
ReferenceAlias Property Opponent Auto
Actor Property Commentator Auto
ObjectReference initialFurn
ObjectReference Property Marker Auto
Objectreference[] Property MaxDices Auto
Objectreference[] Property MaxDices2 Auto
Objectreference[] Property PlayerDice1 Auto
Objectreference[] Property PlayerDice2 Auto
Objectreference[] Property OpponentDice1 Auto
Objectreference[] Property OpponentDice2 Auto
ObjectReference[] Property Coin Auto
Int CoinTossResult
Int OpponentDice01
Int OpponentDice02
Int PlayerDice01
Int PlayerDice02
int MaxDice02
int MaxDice01
Bool OpponentPlayed = FALSE
Bool PlayerPlayed = FALSE