GameCornerBlackjack::
	opentext
	special Special_DisplayCoinCaseBalance
	writetext BlackjackWelcome
	yesorno
	sif false
		closetextend
.choseplayblackjack
	writetext BetAmountText
	loadmenudata BlackjackBetMenu
	verticalmenu
	closewindow
	sif false
		closetextend
	callasm SetBetType
	callasm BlackjackPlaceBet
	sif =, 2
		jumptext BlackjackNotEnoughCoinsText
.blackjackprepare
	playsound $22
	special Special_DisplayCoinCaseBalance
	waitsfx
	callasm BlackjackInit

.blackjackloop
	writetext BlackjackWhatToDo
	menuanonjumptable BlackjackMenu
	dw .blackjackloop
	dw .hit
	dw .stand
	dw .doubledown
	dw .yourhand
	dw .dealerhand

.yourhand
	callasm DisplayCards
	writetext DisplayCardHand
	waitbutton
	jump .blackjackloop

.dealerhand
	callasm DisplayDealerCards
	writetext BlackjackDealersHandText
	waitbutton
	jump .blackjackloop

.hit
	callasm BlackjackHit
	callasm CountPlayerCards
	writetext DisplayCount
	waitbutton
	iffalse .blackjackloop
.playerbust
	writetext BlackjackBustedText
	playwaitsfx SFX_WRONG
	writetext CardGamePlayAgain
	yesorno
	iftrue .choseplayblackjack
	closetextend

.doubledown
	callasm BlackjackBetMain
	sif =, 2, then
		writetext CardGameNotEnoughCoinsText
		waitbutton
		jump .blackjackloop
	sendif
	playsound $22
	special Special_DisplayCoinCaseBalance
	waitsfx
	callasm DoubleDown
	callasm BlackjackHit
	callasm CountPlayerCards
	writetext DisplayCount
	waitbutton
	if_equal 1, .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
	anonjumptable
	dw .lose
	dw .push
	dw .win

.lose
	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
	closetextend

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

BlackjackGetBetAmount:
	ld a, [wTempNumber + 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 [wTempNumber + 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
	ld a, 1
	jr z, .load_and_take_coins
	xor a
.load_and_take_coins
	ld [hScriptVar], a
	jpba TakeCoins

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

BlackjackWonCoins:
	call BlackjackGetBetAmount
	add a, a
	jr BlackjackGiveCoins

BlackjackPushCoins:
	call BlackjackGetBetAmount
BlackjackGiveCoins:
	ld l, a
	ld h, 0
	push hl
	ld hl, wTempNumber + 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

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

CheckIfDealerBusted:
	xor a
	ld [hScriptVar], a
	ld a, [wTempNumber]
	cp 22
	ret c
	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
	jp c, BlackjackGetDealerCard
	ld a, 1
	ld [hScriptVar], a
	ret

DetermineBlackjackWinner:
	ld a, [wTempNumber]
	ld b, a
	push bc
	call CountDealerCards
	pop bc
	ld a, [wTempNumber]  ;a: dealer b: yours
	cp b
	ld a, 1
	jr z, .load
	sbc a
	and 2
.load
	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:
	lb bc, 0, 0 ;aces and sum
.loop
	ld a, [hli]
	inc a
	jr z, .done
	and $f
	inc a
	cp 10 ;face card
	jr c, .addcard
	cp 14 ;ace
	ld a, 10
	jr nz, .addcard
	ld a, 1
	inc b
.addcard
	add a, c
	ld c, a
	jr .loop

.done
	ld a, b
	and a ;check if an ace was encountered
	ld a, c
	jr z, .load
	cp 12 ;check if the ace bonus will exceed 21
	jr nc, .load
	add a, 10
.load
	ld [wTempNumber], a
	ret

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

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 wTempNumber, 1, 0
	db TX_COMPRESSED
	db $a6, $80

DisplayDealerCount:
	db TX_COMPRESSED, $e0, $3d, $e9, $ae, $58, $6f, $7a, $e3, $1c, $c0
	deciram wTempNumber, 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:
	text "? <STRBF1>"
	done

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 0, 0 ; start coords
	db 11, 15 ; end coords
	dw .options
	db 1 ; default option

.options
	db $80
	db 5
	db "Hit@"
	db "Stand@"
	db "Double Down@"
	db "Your hand@"
	db "Dealer's hand@"

BlackjackBetMenu:
	db $40 ; flags
	db 0, 0 ; start coords
	db 9, 7 ; end coords
	dw .options
	db 1 ; default option

.options
	db $80
	db 4
	db "10@"
	db "25@"
	db "50@"
	db "100@"

BlackjackBets:
	db 10
	db 25
	db 50
	db 100

