GameCornerBlackjack::
	opentext
	special Special_DisplayCoinCaseBalance
	writetext BlackjackWelcome
	yesorno
	iffalse BlackjackDontPlay

.choseplayblackjack
	writetext BetAmountText
	loadmenudata BlackjackBetMenu
	verticalmenu
	closewindow
	iffalse BlackjackDontPlay
	callasm SetBetType
	callasm BlackjackPlaceBet
	if_equal 2, BlackjackNotEnoughCoins

.blackjackprepare
	playsound $22
	special Special_DisplayCoinCaseBalance
	waitsfx
	callasm BlackjackInit

.blackjackloop
	writetext BlackjackWhatToDo
	loadmenudata BlackjackMenu
	verticalmenu
	closewindow
	if_equal 1, .hit
	if_equal 2, .stand
	if_equal 3, .doubledown
	if_equal 4, .yourhand
	if_equal 5, .dealerhand
	jump .blackjackloop
	end

.cantdoubledown
	writetext CardGameNotEnoughCoinsText
	waitbutton
	jump .blackjackloop

.doubledown
	callasm BlackjackBetMain
	if_equal 2, .cantdoubledown
	playsound $22
	special Special_DisplayCoinCaseBalance
	waitsfx
	callasm DoubleDown
	callasm BlackjackHit
	callasm CountPlayerCards
	writetext DisplayCount
	waitbutton
	if_equal 1, .playerbust
	jump .stand

.yourhand
	callasm DisplayCards
	writetext DisplayCardHand
	waitbutton
	jump .blackjackloop

.dealerhand
	callasm DisplayDealerCards
	writetext BlackjackDealersHandText
	waitbutton
	jump .blackjackloop

.hit
	callasm BlackjackHit
	callasm CountPlayerCards
	writetext DisplayCount
	waitbutton
	if_equal 0, .blackjackloop
	jump .playerbust

.stand
	callasm BlackjackStand
	writetext DealerDisplaysWholeDeck
	waitbutton
	callasm GetFullDealerHand
	writetext DisplayCardHand
	waitbutton
	writetext DisplayDealerCount
	waitbutton
	callasm CheckIfDealerBusted
	if_equal 1, .dealerbusted
	callasm CountPlayerCards
	writetext DisplayCount
	waitbutton
	callasm DetermineBlackjackWinner
	if_equal 1, .win
	if_equal 2, .push
	writetext BadHandText
	playwaitsfx SFX_WRONG
	special Special_DisplayCoinCaseBalance
	waitbutton
	jump .blackjackaskplayagain

.dealerbusted
	writetext DealerBustedText
	waitbutton
.win
	writetext YouWonText
	playwaitsfx SFX_DEX_FANFARE_170_199
	playsound SFX_TRANSACTION
	callasm BlackjackWonCoins
	special Special_DisplayCoinCaseBalance
	waitbutton
	jump .blackjackaskplayagain

.push
	writetext PushText
	playsound SFX_TRANSACTION
	callasm BlackjackPushCoins
	special Special_DisplayCoinCaseBalance
	waitbutton

.blackjackaskplayagain
	writetext CardGamePlayAgain
	yesorno
	iftrue .choseplayblackjack
	closetext
	end

.playerbust
	writetext BlackjackBustedText
	playwaitsfx SFX_WRONG
	writetext CardGamePlayAgain
	yesorno
	iftrue .choseplayblackjack
	closetext
	end

DoubleDown:
	ld hl, TempNumber + 1
	set 2, [hl]
	ret

BlackjackGetBetAmount:
	ld a, [TempNumber + 1]
	and 3
	ld hl, BlackjackBets
	ld c, a
	ld b, 0
	add hl, bc
	ld a, [hl]
	ret

BlackjackPlaceBet:
	ld a, [hScriptVar]
	dec a
	ld [TempNumber + 1], a
BlackjackBetMain:
	call BlackjackGetBetAmount
	ld c, a
	ld [hMoneyTemp + 1], a
	xor a
	ld [hMoneyTemp], a

	ld bc, hMoneyTemp
	callba CheckCoins

	jr c, .two
	jr z, .one
	xor a
	ld [hScriptVar], a
	jpba TakeCoins
	ret
.one
	ld a, 1
	ld [hScriptVar], a
	jpba TakeCoins
	ret
.two
	ld a, 2
	ld [hScriptVar], a
	ret

BlackjackWonCoins:
	call BlackjackGetBetAmount
	sla a
	jr BlackjackGiveCoins

BlackjackPushCoins:
	call BlackjackGetBetAmount
BlackjackGiveCoins:
	ld l, a
	ld h, 0
	push hl
	ld hl, TempNumber + 1
	bit 2, [hl]
	pop hl
	jr z, .nodoubledown
	add hl, hl
.nodoubledown
	ld a, l
	ld [hMoneyTemp + 1], a
	ld a, h
	ld [hMoneyTemp], a

	ld bc, hMoneyTemp
	jpba GiveCoins
	ret

SetBetType:
	ld a, [hScriptVar]
	dec a
	ld [TempNumber + 1], a
	ret

CheckIfDealerBusted:
	xor a
	ld [hScriptVar], a
	ld a, [TempNumber]
	cp 22
	jr nc, .dealerbusted
	ret

.dealerbusted
	ld a, 1
	ld [hScriptVar], a
	ret

BlackjackStand:
.drawto17
	call BlackjackGetDealerCard
	jr nc, .drawto17
	ret

GetFullDealerHand:
	ld hl, wDealerCardHand
	jp DisplayCardsMain

BlackjackHit:
	xor a
	ld [hScriptVar], a
	call BlackjackGetPlayerCard
	call CountPlayerCards
	cp 22
	jr nc, .playerbust
	jp BlackjackGetDealerCard

.playerbust
	ld a, 1
	ld [hScriptVar], a
	ret

DetermineBlackjackWinner:
	ld a, [TempNumber]
	ld b, a
	push bc
	call CountDealerCards
	pop bc
	ld a, [TempNumber]  ;a: dealer b: yours
	cp b
	jr z, .push
	jr c, .win
	xor a
	ld [hScriptVar], a
	ret

.push
	ld a, 2
	ld [hScriptVar], a
	ret

.win
	ld a, 1
	ld [hScriptVar], a
	ret

BlackjackInit:
	call InitializeDeck
	call BlackjackGetPlayerCard
	call BlackjackGetPlayerCard
	call BlackjackGetDealerCard
	jp BlackjackGetDealerCardFaceDown

CountDealerCards:
	ld hl, wDealerCardHand
	jp CountCards

CountPlayerCards:
	ld hl, wYourCardHand

CountCards:
	ld b, 0 ;Ace
	ld c, 0 ;Sum
.loop
	ld a, [hli]
	cp $ff
	jr z, .checkAce
	and $f
	inc a
	inc a

	cp 10 ;Face card
	jr nc, .facecard

.addcard
	add c
	ld c, a
	jr .loop

.checkAce
	ld a, c
	cp 12 ;Dont add ace bonus if it'll exceed 21
	jr nc, .end

	ld a, 1 ;Check if an ace was encountered
	cp b
	jr nz, .end

	ld a, c ;If all checks out, add 10
	add 10
	ld [TempNumber], a
	ret

.end
	ld a, c
	ld [TempNumber], a
	ret

.facecard
	cp 14
	jr z, .blackjackace
	ld a, 10
	jr .addcard

.blackjackace
	ld a, 1
	ld b, 1
	jr .addcard

BlackjackGetPlayerCard:
	ld hl, wYourCardHand
	lb bc, 0, 11
	jp GetCardFromDeck

BlackjackGetDealerCard:
	ld hl, wDealerCardHand
	call CountCards
	cp 17
	jr nc, .stand ;Stand on 17
	ld hl, wDealerCardHand
	lb bc, 1, 11
	call GetCardFromDeck
	xor a
	ret

.stand
	ld hl, DealerStandsText
	ld b, BANK(DealerStandsText)
	call MapTextbox
	call WaitButton
	scf
	ret

BlackjackGetDealerCardFaceDown:
	ld hl, wDealerCardHand
	lb bc, 2, 11
	jp GetCardFromDeck


BlackjackNotEnoughCoins:
	jumptext BlackjackNotEnoughCoinsText

BlackjackDontPlay:
	closetext
	end

BlackjackWelcome:
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0
	db $bc, $25, $dd, $65, $4a, $9c, $d6, $54, $b8

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

BlackjackNotEnoughCoinsText:
	db TX_COMPRESSED, $72, $85, $a9, $90, $92, $99, $13, $a0
	db $c6, $f1, $90, $2d, $72, $98, $c8, $46, $44
	db $7a, $9a

DisplayCount:
	db TX_COMPRESSED, $72, $85, $a5, $cb, $0d, $ef, $5c, $63, $98, $00
	deciram TempNumber, 1, 0
	db TX_COMPRESSED
	db $a6, $80

DisplayDealerCount:
	db TX_COMPRESSED, $e0, $3d, $e9, $ae, $58, $6f, $7a, $e3, $1c, $c0
	deciram TempNumber, 1, 0
	db TX_COMPRESSED
	db $a6, $80


BlackjackBustedText:
	db TX_COMPRESSED, $72, $85, $a8, $ad, $a4, $59, $e7
	db $8d, $34

BlackjackWhatToDo:
	db TX_COMPRESSED, $6e, $9d, $b9, $90, $c0, $41, $6a, $55, $a4
	db $b7, $ac, $33, $21, $a5
	db $a6, $80

BlackjackDealersHandText:
	db TX_COMPRESSED, $a5, $80, $e4
	db $53, $40

DealerStandsText:
	db TX_COMPRESSED, $e0, $3d, $e9, $ae, $22, $f4, $99, $47
	db $a9, $a0

PushText:
	db TX_COMPRESSED, $e1, $69, $09
	db $e3, $4d

YouWonText:
	db TX_COMPRESSED, $72, $85, $a9, $50, $49
	db $c6, $9a

DealerDisplaysWholeDeck:
	db TX_COMPRESSED, $e0, $3d, $e9, $ae, $2c, $c1, $8f, $7a, $89, $69
	db $37, $b0, $d6, $4e, $64, $35, $12, $33, $ab, $cf
	db $a9, $a0

DealerBustedText:
	db TX_COMPRESSED, $29, $27, $32, $7b, $d3, $5c, $56, $d2, $2c, $f3
	db $c6, $9a

BetAmountText:
	db TX_COMPRESSED, $62, $1a, $a3, $ed, $32, $99, $90, $c0, $41
	db $69, $ea, $b4, $97, $2c, $31, $59, $dd
	db $2d, $34

BlackjackMenu:
	db $40 ; flags
	db 00, 00 ; start coords
	db 11, 15 ; end coords
	dw BlackjackOptions
	db 1 ; default option

BlackjackOptions:
	db $80
	db $5
	db "Hit@"
	db "Stand@"
	db "Double Down@"
	db "Your hand@"
	db "Dealer's hand@"

BlackjackBetMenu:
	db $40 ; flags
	db 00, 00 ; start coords
	db 9, 7 ; end coords
	dw BlackjackBetOptions
	db 1 ; default option

BlackjackBetOptions:
	db $80
	db $4
	db "10@"
	db "25@"
	db "50@"
	db "100@"

BlackjackBets:
	db 10
	db 25
	db 50
	db 100

