begin sltw_KA_cardgame_script

;script to be place on an misc item

short button
short bet
short winner
short cardsuit1
short cardsuit2
short cardnum1
short cardnum2
short stage

float timer

if ( MenuMode == 1 )
	return
endif

if ( timer > 0 )
	set timer to timer - GetSecondsPassed
	return
endif

if ( stage == 0 )
	if ( OnActivate == 1 )
		MessageBox "Would you like to play?" "Play game" "Pick up deck"
		set stage to 1
	endif
endif

if ( stage == 1 )
	set button to GetButtonPressed
	if ( button == -1 )
		return
	elseif ( button == 0 )
		MessageBox "Make a bet" "1" "100" "500" "1000"
		set stage to 2
	elseif ( button == 1 )
		set stage to 0
		activate
		return
	endif
endif

if ( stage == 2 )
	set button to GetButtonPressed
	if ( button == -1 )
		return
	elseif ( button == 0 )
		if ( Player->GetItemCount, "Gold_001" > 0 )
			set bet to 1
			set stage to 3
			set timer to 3
			return
		else
			MessageBox "You do not have enough gold"
			;MessageBox "Would you like to play?" "Play game" "Pick up deck"
			set stage to 0
			return
		endif
	elseif ( button == 1 )
		if ( Player->GetItemCount, "Gold_001" > 100 )
			set bet to 100
			set stage to 3
			set timer to 3
			return
		else
			;MessageBox "You do not have enough gold"
			MessageBox "Would you like to play?" "Play game" "Pick up deck"
			set stage to 0
			return
		endif
	elseif ( button == 2 )
		if ( Player->GetItemCount, "Gold_001" > 500 )
			set bet to 500
			set stage to 3
			set timer to 3
			return
		else
			;MessageBox "You do not have enough gold"
			MessageBox "Would you like to play?" "Play game" "Pick up deck"
			set stage to 0
			return
		endif
	elseif ( button == 3 )
		if ( Player->GetItemCount, "Gold_001" > 1000 )
			set bet to 1000
			set stage to 3
			set timer to 3
			return
		else
			;MessageBox "You do not have enough gold"
			MessageBox "Would you like to play?" "Play game" "Pick up deck"
			set stage to 0
			return
		endif
	endif
endif


if ( stage == 3 )
	set cardsuit1 to Random, 4
	set cardsuit2 to Random, 4
	set cardnum1 to Random, 13
	set cardnum2 to Random, 13
	
	if ( cardsuit1 == cardsuit2 )
		if ( cardnum1 == cardnum2 )
			return
		endif
	endif
	
	if ( cardnum1 > cardnum2 )
		set winner to 1
	elseif ( cardnum1 < cardnum2 )
		set winner to 0
	elseif ( cardnum1 == cardnum2 )
		if ( cardsuit1 > cardsuit2 )
			set winner to 1
		elseif ( cardsuit1 < cardsuit2 )
			set winner to 0
		endif
	endif

	set stage to 4
		
endif

if ( stage == 4 )

	set stage to 5

	if ( cardsuit1 == 0 )
		if ( cardnum1 == 0 )
			MessageBox "You drew an Ace of Clubs"
		elseif ( cardnum1 == 10 )
			MessageBox "You drew a Jack of Clubs"
		elseif ( cardnum1 == 11 )
			MessageBox "You drew a Queen of Clubs"
		elseif ( cardnum1 == 12 )
			MessageBox "You drew a King of Clubs"
		else
			set cardnum1 to ( cardnum1 + 1 )
			MessageBox "You drew a %g of Clubs" cardnum1
		endif
	elseif ( cardsuit1 == 1 )
		if ( cardnum1 == 0 )
			MessageBox "You drew an Ace of Diamonds"
		elseif ( cardnum1 == 10 )
			MessageBox "You drew a Jack of Diamonds"
		elseif ( cardnum1 == 11 )
			MessageBox "You drew a Queen of Diamonds"
		elseif ( cardnum1 == 12 )
			MessageBox "You drew a King of Diamonds"
		else
			set cardnum1 to ( cardnum1 + 1 )
			MessageBox "You drew a %g of Diamonds" cardnum1
		endif
	elseif ( cardsuit1 == 2 )
		if ( cardnum1 == 0 )
			MessageBox "You drew an Ace of Spades"
		elseif ( cardnum1 == 10 )
			MessageBox "You drew a Jack of Spades"
		elseif ( cardnum1 == 11 )
			MessageBox "You drew a Queen of Spades"
		elseif ( cardnum1 == 12 )
			MessageBox "You drew a King of Spades"
		else
			set cardnum1 to ( cardnum1 + 1 )
			MessageBox "You drew a %g of Spades" cardnum1
		endif
	elseif ( cardsuit1 == 3 )
		if ( cardnum1 == 0 )
			MessageBox "You drew an Ace of Hearts"
		elseif ( cardnum1 == 10 )
			MessageBox "You drew a Jack of Hearts"
		elseif ( cardnum1 == 11 )
			MessageBox "You drew a Queen of Hearts"
		elseif ( cardnum1 == 12 )
			MessageBox "You drew a King of Hearts"
		else
			set cardnum1 to ( cardnum1 + 1 )
			MessageBox "You drew a %g of Hearts" cardnum1
		endif
	endif

	set timer to 3
	return

endif


if  ( stage  == 5 )
		
	set stage to 6

	if ( cardsuit2 == 0 )
		if ( cardnum2 == 0 )
			MessageBox "Opponent drew an Ace of Clubs"
		elseif ( cardnum2 == 10 )
			MessageBox "Opponent drew a Jack of Clubs"
		elseif ( cardnum2 == 11 )
			MessageBox "Opponent drew a Queen of Clubs"
		elseif ( cardnum2 == 12 )
			MessageBox "Opponent drew a King of Clubs"
		else
			set cardnum2 to ( cardnum2 + 1 )
			MessageBox "Opponent drew a %g of Clubs" cardnum2
		endif
	elseif ( cardsuit2 == 1 )
		if ( cardnum2 == 0 )
			MessageBox "Opponent drew an Ace of Diamonds"
		elseif ( cardnum2 == 10 )
			MessageBox "Opponent drew a Jack of Diamonds"
		elseif ( cardnum2 == 11 )
			MessageBox "Opponent drew a Queen of Diamonds"
		elseif ( cardnum2 == 12 )
			MessageBox "Opponent drew a King of Diamonds"
		else
			set cardnum2 to ( cardnum2 + 1 )
			MessageBox "Opponent drew a %g of Diamonds" cardnum2
		endif
	elseif ( cardsuit2 == 2 )
		if ( cardnum2 == 0 )
			MessageBox "Opponent drew an Ace of Spades"
		elseif ( cardnum2 == 10 )
			MessageBox "Opponent drew a Jack of Spades"
		elseif ( cardnum2 == 11 )
			MessageBox "Opponent drew a Queen of Spades"
		elseif ( cardnum2 == 12 )
			MessageBox "Opponent drew a King of Spades"
		else
			set cardnum2 to ( cardnum2 + 1 )
			MessageBox "Opponent drew a %g of Spades" cardnum2
		endif
	elseif ( cardsuit2 == 3 )
		if ( cardnum2 == 0 )
			MessageBox "Opponent drew an Ace of Hearts"
		elseif ( cardnum2 == 10 )
			MessageBox "Opponent drew a Jack of Hearts"
		elseif ( cardnum2 == 11 )
			MessageBox "Opponent drew a Queen of Hearts"
		elseif ( cardnum2 == 12 )
			MessageBox "Opponent drew a King of Hearts"
		else
			set cardnum2 to ( cardnum2 + 1 )
			MessageBox "Opponent drew a %g of Hearts" cardnum2
		endif
	endif

	set timer to 3
	return

endif


if ( stage == 6 )
	
	if ( winner == 1 )
		MessageBox, "You win", "ok"
		set stage to 7
	else
		MessageBox, "You lose", "ok"
		set stage to 7
	endif

endif


if ( stage == 7 )
	set button to GetButtonPressed
	if ( button == -1 )
		return
	elseif ( button == 0 )
		set stage to 8
	endif
endif


if ( stage == 8 )
	if ( winner == 1 )
		MessageBox "collected %g gold" bet
		if ( bet == 1 )
			Player->AddItem, "Gold_001", 1
		elseif ( bet == 100 )
			Player->AddItem, "Gold_001", 100
		elseif ( bet == 500 )
			Player->AddItem, "Gold_001", 500
		elseif ( bet == 1000 )
			Player->AddItem, "Gold_001", 1000
		endif
		;MessageBox "Would you like to play?" "Play game" "Pick up deck"
		set stage to 0
		set winner to 0
	else
		MessageBox "%g gold taken from your inventory" bet
		if ( bet == 1 )
			Player->RemoveItem, "Gold_001", 1
		elseif ( bet == 100 )
			Player->RemoveItem, "Gold_001", 100
		elseif ( bet == 500 )
			Player->RemoveItem, "Gold_001", 500
		elseif ( bet == 1000 )
			Player->RemoveItem, "Gold_001", 1000
		endif
		;MessageBox "Would you like to play?" "Play game" "Pick up deck"
		set stage to 0
		set winner to 0
	endif
endif

end

