poker_reward: MACRO
	dw \1 ;Text
	dw \2 ;Payout
	dw \3 ;Sound FX
	ENDM

GameCornerPoker::
	opentext
	special Special_DisplayCoinCaseBalance
	writetext PokerWelcome
	yesorno
	sif false
		closetextend
.choseToPlayPoker
	checkcoins 10
	sif =, 2
		jumptext CardGameNotEnoughCoinsText
	takecoins 10
	playsound SFX_TRANSACTION
	special Special_DisplayCoinCaseBalance
	waitsfx
	callasm InitializeDeck
	callasm GetPokerHand
	writetext PokerHoldWhichText

.displayCardMenu
	callasm GetCardMenu
	loadmenudata wPokerMenu
	verticalmenu
	closewindow
	sif true, then
		callasm HoldCard
		jump .displayCardMenu
	sendif
	callasm GetPokerHand
	callasm DisplayCards
	writetext DisplayCardHand
	waitbutton
	callasm CheckPokerHand
	callasm GivePokerReward
	waitsfx
	sif true, then
		playsound SFX_TRANSACTION
		special Special_DisplayCoinCaseBalance
	sendif
	writetext CardGamePlayAgain
	yesorno
	iftrue .choseToPlayPoker
	closetextend

GivePokerReward:
	ld a, [hScriptVar]
	ld hl, PokerRewards
	ld bc, 6
	rst AddNTimes
	ld e, l
	ld d, h
	ld a, [de]
	inc de
	ld l, a
	ld a, [de]
	inc de
	ld h, a
	ld b, BANK(GivePokerReward)

	push de
	call MapTextbox
	pop hl

	;Give coins
	ld a, [hli]
	ld bc, hMoneyTemp + 1
	ld [c], a ;faster than ld [bc], a, and it works because HRAM
	dec c
	ld a, [hli]
	ld [c], a

	;Play sfx
	ld e, [hl]
	ld d, 0
	call PlaySFX
	jpba GiveCoins

CheckPokerHand:
	xor a ;Clear hash table
	ld [hScriptVar], a
	ld bc, 17
	ld hl, wYourCardHand + 5 ;Hash table
	call ByteFill

	;Set first suit
	ld hl, wYourCardHand + 18 ;Flags
	set 0, [hl] ;Default flush

	ld de, wYourCardHand
	ld a, [de]
	and $30
	ld b, a
	ld c, 5

.loop
	ld a, [de]
	inc de

;Check for flush
	push af
	and $30
	cp b
	jr z, .matchingsuit
	ld hl, wYourCardHand + 18 ;Flags
	res 0, [hl] ;Flush failed
.matchingsuit ;Create hash table
	pop af
	and $f
	push bc
	ld hl, wYourCardHand + 5
	ld c, a
	ld b, 0
	add hl, bc
	inc [hl]
	ld a, [hl]
	pop bc
	dec c
	jr nz, .loop

.checkforpairs
	ld bc, 13
	ld hl, wYourCardHand + 5

.pairloop
	ld a, [hli]
	push hl
	ld hl, wYourCardHand + 18
	cp 4
	jp z, .fourOfAKind
	cp 3
	jp z, .threeOfAKind
	cp 2
	jp z, .twoOfAKind
.paircheckend
	pop hl
	dec c
	jr nz, .pairloop

;Check for straight
	ld hl, wYourCardHand + 5
	ld c, 10
	ld a, [wYourCardHand + 17]
	and a
	jr nz, .acestraight

	ld c, 10

.findfirstcardloop
	dec c
	jr z, .checkPokerFlags
	ld a, [hli]
	and a
	jr z, .findfirstcardloop

	ld a, 2
	cp c
	jr nz, .notRoyal
	push hl
	ld hl, wYourCardHand + 18
	set 7, [hl]
	pop hl

.notRoyal

;Found first card in hash table, check if any succeed it.
	ld c, 4
.straightLoop
	ld a, [hli]
	and a
	jr z, .checkPokerFlags
	dec c
	jr nz, .straightLoop

	ld b, 5
	call .changeReward

	;It's a straight
	ld hl, wYourCardHand + 18
	bit 0, [hl]
	jr nz, .straightflush

.checkPokerFlags
	ld hl, wYourCardHand + 18
	bit 0, [hl]
	ret z
	ld b, 4
	jp .changeReward

.acestraight ;Check ace low straight
	ld a, [hli]
	and a
	jr z, .findfirstcardloop ;2 isn't in hand so you can't do an ace low straight
	ld c, 3
	jr .straightLoop

.straightflush
	ld b, 8
	call .changeReward
	bit 7, [hl]
	ret z
	ld b, 9

.changeReward ;takes b
	ld a, [hScriptVar]
	cp b
	ret nc
	ld a, b
	ld [hScriptVar], a
	ret

.fourOfAKind
	ld hl, wYourCardHand + 18
	set 5, [hl]
	ld b, 7
	call .changeReward
	jp .paircheckend

.twoOfAKind
	ld hl, wYourCardHand + 18
	bit 3, [hl]
	jr nz, .fullhouse
	bit 1, [hl]
	jr nz, .twopair
	set 1, [hl]
	ld b, 1
	call .changeReward
	jp .paircheckend

.threeOfAKind
	ld hl, wYourCardHand + 18
	set 3, [hl]
	bit 1, [hl]
	jr nz, .fullhouse
	ld b, 3
	call .changeReward
	jp .paircheckend

.fullhouse
	set 4, [hl]
	ld b, 6
	call .changeReward
	jp .paircheckend

.twopair
	set 2, [hl]
	ld b, 2
	call .changeReward
	jp .paircheckend

;Flags:
;0 - Flush
;1 - Pair
;2 - Two Pair
;3 - Three of a Kind
;4 - Full House
;5 - Four of a Kind
;7 - Royal Potential

HoldCard:
	ld a, [hScriptVar]
	dec a
	ld c, a
	ld b, 0
	ld hl, wYourCardHand
	add hl, bc
	ld a, [hl]
	xor $80
	ld [hl], a
	ret

GetPokerHand:
.loop
	lb bc, 0, 5
	ld hl, wYourCardHand

	call GetCardFromDeck
	jr nc, .loop
	ret

GetCardMenu: ;Prepares a card menu
	ld hl, wYourCardHand
	ld de, wStringBuffer1
	ld a, $80
	ld [de], a
	inc de
	ld a, 5
	ld [de], a
	inc de

.loop
	call DisplayCard
	jr nc, .setMenuHeader
	inc de
	jr .loop

.setMenuHeader
	ld hl, PokerMenu
	ld de, wPokerMenu
	ld bc, $7
	rst CopyBytes
	ld a, [hScriptVar]
	ld [wPokerMenu + 7], a
	ret

PokerMenu:
	db $40 ; flags
	db 00, 00 ; start coords
	db 11, 8 ; end coords
	dw wStringBuffer1
	db 1 ; default option

PokerWelcome:
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0, $ce, $10, $a9, $1a, $f8

	db $80, $6f, $a4, $b9, $61, $82, $3b, $a0, $a9
	db $69, $a0

PokerHoldWhichText:
	db TX_COMPRESSED, $e1, $59, $a1, $12, $b9, $61
	db $7b, $23, $43, $3a, $bc, $cd, $8c, $ea, $f3

	db $e0, $1c, $2b, $34, $22, $09, $4b, $0c, $ca, $f6, $af
	db $53, $40

PairText:
	db TX_COMPRESSED, $e1, $d1
	db $ae, $9a

TwoPairText:
	db TX_COMPRESSED, $2a, $a0, $ce, $1d, $1a
	db $e9, $a0

ThreeofaKindText:
	db TX_COMPRESSED, $29, $2b, $33, $81, $c2, $6c, $52, $19
	db $33, $4d

FourofaKindText:
	db TX_COMPRESSED, $53, $0b, $4b, $81, $c2, $6c, $52, $19
	db $33, $4d

StraightText:
	db TX_COMPRESSED, $51, $6b, $d1, $f2, $9b
	db $a6, $80

FlushText:
	db TX_COMPRESSED, $53, $eb, $48
	db $4d, $34

FullHouseText:
	db TX_COMPRESSED, $53, $6b, $bd, $8c, $42, $d2
	db $0e, $9a

StraightFlushText:
	db TX_COMPRESSED, $51, $6b, $d1, $f2, $9b, $8a, $7d, $69, $09
	db $a6, $80

RoyalFlushText:
	db TX_COMPRESSED, $72, $3b, $72, $d7, $8f, $14, $f1, $dc, $ea, $18, $f1
	db $a6, $80

PokerRewards:
	poker_reward BadHandText, 0, SFX_WRONG
	poker_reward PairText, 10, SFX_ITEM
	poker_reward TwoPairText, 20, SFX_FANFARE_2
	poker_reward ThreeofaKindText, 30, SFX_DEX_FANFARE_LESS_THAN_20
	poker_reward FlushText, 65, SFX_DEX_FANFARE_170_199
	poker_reward StraightText, 40, SFX_DEX_FANFARE_140_169
	poker_reward FullHouseText, 100, SFX_DEX_FANFARE_200_229
	poker_reward FourofaKindText, 250, SFX_DEX_FANFARE_230_PLUS
	poker_reward StraightFlushText, 1000, SFX_DEX_FANFARE_230_PLUS
	poker_reward RoyalFlushText, 2500, SFX_DEX_FANFARE_230_PLUS

