PhloxBingo_MapScriptHeader ;trigger count
	db 0
 ;callback count
	db 0

PhloxBingoGranny:
	copybytetovar wBingoCurrentCard
	iffalse .firstcard
	callasm GetNextPendingBingoLine
	sif true
		jumptextfaceplayer PhloxBingoGranny_Text_PrizeWaiting
	callasm GetBingoCardStatus
	anonjumptable
	dw .notdone
	dw .nextcard
	dw .gameover
.notdone
	jumptextfaceplayer PhloxBingoGranny_Text_Instructions
.gameover
	faceplayer
	checkevent EVENT_PHLOX_BINGO_GOT_CARD
	sif true
		scall .completedcard
	jumptext PhloxBingoGranny_Text_NoMoreCards
.nextcard
	faceplayer
	checkevent EVENT_PHLOX_BINGO_GOT_CARD
	sif true
		scall .completedcard
	opentext
	writetext PhloxBingoGranny_Text_WantAnotherCard
	yesorno
	iftrue .givecard
.wontplay
	writetext PhloxBingoGranny_Text_WontPlay
	endtext
.firstcard
	faceplayer
	opentext
	writetext PhloxBingoGranny_Text_NoCard
	yesorno
	iffalse .wontplay
.givecard
	writetext PhloxBingoGranny_Text_GiveCard
	setevent EVENT_PHLOX_BINGO_GOT_CARD
	playsound SFX_DEX_FANFARE_50_79
	appear 4
	scriptstartasm
	ld hl, wBingoCurrentCard
	inc [hl]
	inc hl
	ld bc, 5 ; 2 for wBingoAwardedPrizes and 3 for wBingoMarkedCells
	xor a
	call ByteFill
	scriptstopasm
	waitsfx
	writetext PhloxBingoGranny_Text_Instructions
	endtext
.completedcard
	showemote EMOTE_SHOCK, 2, 40
	opentext
	writetext PhloxBingoGranny_Text_CompletedCard
	closetext
	clearevent EVENT_PHLOX_BINGO_GOT_CARD
	disappear 4
	pause 15
	end

PhloxBingoAttendant:
	checkevent EVENT_PHLOX_BINGO_GOT_CARD
	sif false
		jumptextfaceplayer PhloxBingoAttendant_Text_NoCard
	callasm GetNextPendingBingoLine
	copyvartobyte wScriptBuffer
	copybytetovar wBingoCurrentCard
	addvar -1
	anonjumptable
	dw .first_card
	dw .second_card
	dw .third_card

.first_card
	copybytetovar wScriptBuffer
	anonjumptable
	dw .noprize
	dw .get_item ;1st line
	dw .get_TM ;2nd line
	dw .get_gold_token ;3rd line
	dw .get_item ;4th line
	dw .get_item ;5th line
	dw .get_TM ;1st column
	dw .get_item ;2nd column
	dw .get_item ;3rd column
	dw .get_item ;4th column
	dw .get_item ;5th column
	dw .get_item ;down diagonal
	dw .get_item ;up diagonal
	dw .get_item ;full card

.prizes_1
	db AMULET_COIN ;1st line
	db 40 ;2nd line
	db 0 ;3rd line
	db RARE_CANDY ;4th line
	db MAX_ELIXIR ;5th line
	db 18 ;1st column
	db PP_UP ;2nd column
	db WHITE_FLUTE ;3rd column
	db HEART_SCALE ;4th column
	db SUN_STONE ;5th column
	db MAX_REVIVE ;down diagonal
	db EXP_SHARE ;up diagonal
	db SHINY_BALL ;full card

.second_card
	copybytetovar wScriptBuffer
	anonjumptable
	dw .noprize
	dw .get_item ;1st line
	dw .get_item ;2nd line
	dw .get_gold_token ;3rd line
	dw .get_TM ;4th line
	dw .get_item ;5th line
	dw .get_TM ;1st column
	dw .get_item ;2nd column
	dw .get_TM ;3rd column
	dw .get_item ;4th column
	dw .get_item ;5th column
	dw .get_item ;down diagonal
	dw .get_item ;up diagonal
	dw .get_item ;full card

.prizes_2
	db RARE_CANDY ;1st line
	db PINK_BOW ;2nd line
	db 1 ;3rd line
	db 75 ;4th line
	db YELLOW_FLUTE ;5th line
	db 96 ;1st column
	db CAGE_KEY ;2nd column
	db 36 ;3rd column
	db BIG_NUGGET ;4th column
	db TRADE_STONE ;5th column
	db MAX_ELIXIR ;down diagonal
	db PP_UP ;up diagonal
	db GRAPPLE_HOOK ;full card

.third_card
	copybytetovar wScriptBuffer
	anonjumptable
	dw .noprize
	dw .get_TM ;1st line
	dw .get_item ;2nd line
	dw .get_TM ;3rd line
	dw .get_item ;4th line
	dw .get_item ;5th line
	dw .get_gold_token ;1st column
	dw .get_item ;2nd column
	dw .get_TM ;3rd column
	dw .get_item ;4th column
	dw .get_item ;5th column
	dw .get_item ;down diagonal
	dw .get_item ;up diagonal
	dw .get_item ;full card

.prizes_3
	db 64 ;1st line
	db SACRED_ASH ;2nd line
	db 7 ;3rd line
	db PURPLE_FLUTE ;4th line
	db PP_UP ;5th line
	db 2 ;1st column
	db SHELL_BELL ;2nd column
	db 13 ;3rd column
	db SAFE_GOGGLES ;4th column
	db MAX_ELIXIR ;5th column
	db BLACK_FLUTE ;down diagonal
	db ORANGE_FLUTE ;up diagonal
	db MASTER_BALL ;full card

.noprize
	copybytetovar wBingoAwardedPrizes + 1
	sif >=, $10
		jumptextfaceplayer PhloxBingoAttendant_Text_Congratulations
	jumptextfaceplayer PhloxBingoAttendant_Text_NoPrize

.get_item
	scall .get_prize
	itemtotext 0, 0
	writetext PhloxBingoAttendant_Text_GotItem
	playsound SFX_ITEM
	giveitem ITEM_FROM_MEM, 1
	waitsfx
	waitbutton
	anonjumptable
	dw .noRoomForPrize
	dw .got_prize

.get_TM
	scall .get_prize
	writetext PhloxBingoAttendant_Text_GotTM
	playsound SFX_ITEM
	givetmnomessage 0
	waitsfx
	waitbutton
	sif false
.noRoomForPrize
		jumptext PhloxBingoAttendant_Text_NoRoomForPrize
.got_prize
	copybytetovar wScriptBuffer
	addvar -1
	callasm MarkBingoPrizeAwarded
	sif =, 12, then
		writetext PhloxBingoAttendant_Text_Congratulations
	selse
		writetext PhloxBingoAttendant_Text_EndPrize
	sendif
	endtext

.get_gold_token
	scall .get_prize
	pushvar
	writetext PhloxBingoAttendant_Text_GotGoldToken
	playsound SFX_ITEM
	giveitem GOLD_TOKEN, 1
	waitsfx
	waitbutton
	sif false, then
		popvar
		jumptext PhloxBingoAttendant_Text_NoRoomForPrize
	sendif
	popvar
	loadarray .gold_token_event_flags
	readarrayhalfword 0
	setevent -1
	jump .got_prize

.gold_token_event_flags
	dw EVENT_FIRST_BINGO_CARD_GOLD_TOKEN
.gold_token_event_flagsEntrySizeEnd:
	dw EVENT_SECOND_BINGO_CARD_GOLD_TOKEN
	dw EVENT_THIRD_BINGO_CARD_GOLD_TOKEN

.get_prize
	addvar -1
	pushvar
	copybytetovar wBingoCurrentCard
	scall .get_prize_list
	popvar
	loadarray -1, 1
	faceplayer
	opentext
	writetext PhloxBingoAttendant_Text_GotPrize
	waitbutton
	readarray 0
	end

.get_prize_list
	writehalfword .prizes_1
	sif =, 1
		end
	writehalfword .prizes_2
	sif =, 2
		end
	writehalfword .prizes_3
	end

PhloxBingoCard:
	checkevent EVENT_PHLOX_BINGO_GOT_CARD
	sif false
		end
	callasm PlayBingo
	reloadmap
	end

PhloxBingoGranny_Text_NoCard:
	db TX_COMPRESSED, $12, $19, $39, $0f, $18, $24, $32, $72, $1e
	db $2f, $09, $3a, $07, $b0, $9d, $f1

	db $00, $ea, $f1, $4e, $56, $7b, $72
	db $c2, $f0, $8e, $e8, $28, $80, $be, $71, $58, $c9, $c8

	db $7c, $01, $d5, $ca, $82, $d7, $73, $1f, $3d, $24, $b4
	db $9b, $d5, $05, $7d, $cc, $b0, $c7, $ce, $0f, $08, $08, $2d
	db $00, $11, $dd, $04, $79, $9b, $05, $9b, $43, $65
	db $49, $e5, $85, $a2, $66, $07, $08, $ac, $64, $e4
	db $2a, $aa, $1d, $a6, $3e, $7c

	db $40, $39, $42, $d4, $67, $49, $1f, $d5, $a9, $4d
	db $79, $02, $cd, $ad, $59, $cc, $02, $b8, $60, $c6, $b0
	db $40, $35, $96, $42, $f8, $8e, $8d, $09, $7c, $c9
	db $6f, $02, $0b, $53, $06, $bb, $80, $fb, $df, $4a, $c9
	db $90, $19, $09, $29, $92, $d2, $32, $29, $40, $41
	db $69, $e6, $74, $93, $a3, $39, $61, $8f, $9d, $3c

	db $00, $1d, $16, $4d, $8f, $86, $d2, $6b, $fc
	db $d7, $b9, $5e, $93, $2f, $86, $d2, $6b, $fc, $d2
	db $64, $06, $ce, $56, $7b, $60
	db $5e, $e5, $7a, $4c, $be, $1b, $49, $ae, $58, $43
	db $f5, $49, $34, $9a, $4e, $9e

	db $00, $14, $f0, $20, $31, $d4, $67, $49, $2c, $fb
	db $d7, $aa, $49, $92, $02, $0b, $44, $e6, $29, $0f, $28
	db $01, $d1, $a9, $1c, $04, $16, $a6, $ac, $e7, $41, $0c, $9c
	db $bd, $aa, $c2, $d5, $c4, $05, $f3, $b4, $8c, $9c, $f1

	db $00, $a0, $3f, $99, $0c, $04, $16, $a5, $5a, $4b
	db $7b, $63, $3a, $bc, $d2
	db $d3, $40

PhloxBingoGranny_Text_GiveCard:
	db TX_COMPRESSED, $29, $3a, $45, $24, $4c, $02, $b8, $23, $ba, $08, $64
	db $e5, $ea, $87, $69, $8f, $9f, $18, $c4, $d6, $70, $68
	db $54, $08, $2d, $2e, $33, $ab, $cf
	db $1a, $68

PhloxBingoGranny_Text_Instructions:
	db TX_COMPRESSED, $6e, $93, $24, $04, $16, $a3, $21, $7c, $47, $4e
	db $cd, $ed, $9d, $19, $0f, $f2, $f7, $aa, $52
	db $c2, $01, $f0, $51, $2d, $21, $5e, $93, $31, $59, $a4, $64
	db $cb, $d6, $93, $8c, $85, $a2, $59, $ae, $58
	db $55, $72, $77, $01, $05, $a5, $c1, $0b, $18, $9b, $3f
	db $53, $40

PhloxBingoGranny_Text_WontPlay:
	db TX_COMPRESSED, $6e, $12, $53, $20, $20, $b5, $04, $77, $41, $4a, $87, $69
	db $79, $f3, $a5, $8e, $a2, $74, $18, $e1, $03, $69, $19, $39
	db $54, $7d, $41, $cb, $0c, $c8, $69, $e0
	db $a6, $80

PhloxBingoGranny_Text_PrizeWaiting:
	db TX_COMPRESSED, $72, $85, $a8, $fe, $e4, $e6, $ce, $8c, $87
	db $c5, $e6, $d0, $93, $95, $ed, $b5, $dd, $b1, $84, $88

	db $e2, $01, $89, $ee, $65, $86, $5a, $4e, $32, $16, $88
	db $05, $eb, $35, $c0, $83, $1a, $e5, $a4, $d6, $72
	db $c2, $a9, $9e, $e8, $df, $80, $82, $d2, $e0, $85, $8c, $4d, $9f
	db $1a, $68

PhloxBingoGranny_Text_CompletedCard:
	db TX_COMPRESSED, $6e, $1a, $b8, $c7, $28, $5a, $8c, $85, $f1, $1d, $3b, $3c
	db $bd, $69, $39, $52, $43, $d3, $8c, $ea, $f3, $34, $99
	db $00, $cf, $74, $6f, $9e, $66, $f7, $b2, $d2
	db $6f, $08, $58, $c4, $d9, $a3, $8c, $6e, $7d, $ec, $c8, $48, $7c

	db $40, $63, $9d, $c7, $ce, $08, $6a, $e5, $a4, $68
	db $79, $9d, $5e, $66, $d5, $a0, $a1, $03, $57
	db $81, $cc

PhloxBingoGranny_Text_WantAnotherCard:
	db TX_COMPRESSED, $75, $19, $d2, $4e, $46, $0c, $70, $10, $5a
	db $7b, $48, $1b, $49, $ae, $33, $ab, $cc, $a8, $76
	db $90, $0f, $85, $67, $39, $0e, $f5, $13, $00, $ae, $02, $0b
	db $4f, $58, $63, $21, $7c, $47, $4e, $cf

	db $c0, $38, $03, $01, $05, $a9, $56, $92
	db $de, $d2, $06, $d2, $6b, $8c, $ea, $f3
	db $4b, $4d

PhloxBingoGranny_Text_NoMoreCards:
	db TX_COMPRESSED, $74, $59, $37, $bd, $81, $6a, $e0, $70
	db $79, $9d, $5e, $51

	db $c4, $01, $49, $d2, $29, $22, $60, $15, $c1, $1d, $d0, $43, $27
	db $2f, $2b, $19, $39, $0c, $a8, $76, $98, $f9, $f1
	db $a6, $80

PhloxBingoAttendant_Text_NoCard:
	db TX_COMPRESSED, $6e, $7d, $64, $2f, $9c, $b0, $cb, $49
	db $bc, $24, $32, $72, $18, $c7, $7b, $de

	db $20, $18, $4b, $d5, $70, $10, $5a, $5a, $0f, $b8, $4d
	db $79, $9d, $5e, $7f, $34, $99, $95, $0c, $91, $01, $7c
	db $d5, $61, $aa, $da, $e8, $5a, $44, $10, $b1, $89, $b3, $47

	db $10, $05, $24, $e7, $77, $20, $a2, $07, $76, $32
	db $72, $f0, $41, $8d, $72, $d2, $6b, $39, $ca, $f5, $5a, $b3
	db $c8, $00, $b4, $b9, $dd, $45, $c6, $75, $79, $45, $3c
	db $0f, $2b, $6a, $e2, $12, $6e, $1e, $c1, $0b, $0a, $bd, $57, $d0
	db $4a, $90, $9d, $59, $cb, $49, $bf
	db $ea, $68

PhloxBingoAttendant_Text_GotPrize:
	start_asm
	ld a, [hScriptVar]
	cp 12
	ld hl, .full_card_text
	ret z
	push bc
	ld bc, wStringBuffer3 - wStringBuffer1
	ld hl, wStringBuffer1
	push hl
	push af
	ld a, "@"
	call ByteFill
	pop af
	ld c, 5
	call SimpleDivide
	bit 1, b
	ld hl, .ordinals
	jr z, .selected_list
	ld hl, .diagonal_names
.selected_list
	call GetNthString
	ld e, l
	ld d, h
	pop hl
	push bc
	call PlaceString
	pop af
	ld hl, .kinds
	call GetNthString
	ld e, l
	ld d, h
	ld hl, wStringBuffer2
	call PlaceString
	pop bc
	ld hl, .text
	ret
.text
	db TX_COMPRESSED, $6d, $09, $39, $5e, $db, $5d, $db, $18, $48, $8e
	db $2f, $39, $42, $d4, $64, $2f, $88, $e9, $d9, $e6, $5a
	db $4c, $00, $39, $10, $1c, $9e
	db $34, $d0
.full_card_text
	db TX_COMPRESSED, $72, $85, $a9, $80, $c8, $34, $24, $f3, $2d
	db $26, $f5, $49, $0f, $4e, $33, $ab, $cf, $18, $dc, $fb
	db $d5, $59, $09, $0f
	db $8d, $34
.ordinals
	db "first@"
	db "second@"
	db "third@"
	db "fourth@"
	db "fifth@"
.kinds
	db "row@"
	db "column@"
	db "diagonal@"
.diagonal_names
	db "down@"
	db "up@"

PhloxBingoAttendant_Text_NoPrize:
	db TX_COMPRESSED, $75, $84, $04, $16, $a3, $21, $7c, $47, $4e, $ce
	db $6b, $dd, $19, $0e, $0c, $90, $10, $5a, $5c, $67, $57, $9f
	db $aa, $64, $2f, $9c, $49, $ac, $e5, $86, $72
	db $76, $a8, $10, $5a, $5c, $10, $b1, $89, $b3
	db $e3, $4d

PhloxBingoAttendant_Text_GotItem:
	start_asm
	push bc
	ld hl, wStringBuffer1
	ld [hl], "a"
	inc hl
	ld a, "@"
	ld [hli], a
	ld [hld], a
	push hl
	ld a, [wStringBuffer3]
	ld hl, .vowels
	call IsInSingularArray
	pop hl
	jr nc, .not_vowel
	ld [hl], "n"
.not_vowel
	pop bc
	ld hl, .text
	ret
.vowels
	db "AEIOUaeiou", -1
.text
	db TX_COMPRESSED, $72, $85, $a9, $50, $49, $01, $c8
	db $7a, $9e, $5c
	db $69, $a0

PhloxBingoAttendant_Text_EndPrize:
	db TX_COMPRESSED, $6d, $0b, $e7, $15, $eb, $2a, $52, $a4, $99, $20, $20
	db $b4, $f3, $21, $7c, $47, $4e, $ce, $69, $03, $69, $35
	db $aa, $e8, $c8, $7c
	db $69, $a0

PhloxBingoAttendant_Text_NoRoomForPrize:
	db TX_COMPRESSED, $12, $6a, $e0, $20, $b5, $13, $a0, $c7, $08
	db $17, $96, $10, $bf, $30, $0a, $e0, $20, $b4
	db $b5, $42, $16, $31, $36, $74, $f0

	db $00, $74, $e1, $ec, $52, $33, $11, $07, $73, $00
	db $ad, $e0, $41, $6a, $60, $15, $cd, $95, $24, $7d, $3f
	db $aa, $40, $63, $21, $7c, $e2, $bd, $65, $4a, $6e, $16, $6b
	db $54, $fe, $a9, $0c, $9c, $c4, $05, $f3, $8b, $08, $5f
	db $f5, $34

PhloxBingoAttendant_Text_GotTM:
	start_asm
	push bc
	ld a, [hScriptVar]
	ld c, 10
	call SimpleDivide
	push af
	ld a, b
	add a, "0"
	ld hl, wStringBuffer1
	ld [hli], a
	pop af
	add a, "0"
	ld [hli], a
	ld [hl], "@"
	ld hl, .text
	pop bc
	ret
.text
	db TX_COMPRESSED, $72, $85, $a9, $50, $49, $36, $14, $2a, $0e, $47
	db $1a, $68

PhloxBingoAttendant_Text_GotGoldToken:
	db TX_COMPRESSED, $72, $85, $a9, $50, $49
	db $35, $e6, $10, $7b, $98, $50, $54, $8c, $9c
	db $69, $a0

PhloxBingoAttendant_Text_Congratulations:
	db TX_COMPRESSED, $6d, $09, $39, $5e, $db, $5d, $db, $18, $48, $88, $12
	db $3d, $80, $c8, $34, $24, $64, $e6, $02, $0b, $4b
	db $54, $ac, $64, $e4, $31, $9d, $5e, $78
	db $d3, $40

PhloxBingo_MapEventHeader ;filler
	db 0, 0

;warps
	db 2
	warp_def $9, $6, 5, PHLOX_TOWN
	warp_def $9, $7, 5, PHLOX_TOWN

	;xy triggers
	db 0

	;signposts
	db 0

	;people-events
	db 3
	person_event SPRITE_GRANNY, 5, 9, SPRITEMOVEDATA_STANDING_LEFT, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_SCRIPT, 0, PhloxBingoGranny, -1
	person_event SPRITE_GRAMPS, 6, 0, SPRITEMOVEDATA_STANDING_RIGHT, 0, 0, -1, -1, PAL_OW_RED, PERSONTYPE_SCRIPT, 0, PhloxBingoAttendant, -1
	person_event SPRITE_POKEDEX, 5, 8, SPRITEMOVEDATA_ITEM_TREE, 0, 0, -1, -1, PAL_OW_YELLOW, PERSONTYPE_SCRIPT, 0, PhloxBingoCard, EVENT_PHLOX_BINGO_GOT_CARD | $8000

