MersonGoldTokenExchange_MapScriptHeader;trigger count
	db 0
 ;callback count
	db 0

MersonGoldTokenExchangeNPC:
	faceplayer
	opentext
	checkevent EVENT_GOLDTOKENMAN_INTRO
	sif false, then
		writetext MersonGoldTokenExchange_Text_Intro
		setevent EVENT_GOLDTOKENMAN_INTRO
	sendif
	scriptstartasm
	ld hl, MersonGoldTokenExchange_EventFlags
	call CountEventFlagsFromList
	ld a, e
	cp 8
	ld hl, MersonGoldTokenExchange_CheckFinishedDex
	ret z
	ld hl, MersonGoldTokenExchange_AfterFillingDex
	ret nc
	ld [hScriptVar], a
	ld hl, PokedexCaught
	ld b, EndPokedexCaught - PokedexCaught
	call CountSetBits
	push af
	ld a, [hScriptVar]
	inc a
	ld b, a
	swap a
	sub b
	add a, a
	ld b, a
	pop af
	cp b
	ld hl, MersonGoldTokenExchange_RequirementsForMilestone
	ret c
	scriptstopasm
	writetext MersonGoldTokenExchange_Text_PassedMilestone
	waitbutton
	verbosegiveitem GOLD_TOKEN, 4
	sif false
		jumptext MersonGoldTokenExchange_Text_NeedMoreSpace
	closetext
	scriptstartasm
	ld hl, MersonGoldTokenExchange_EventFlags
.loop
	ld a, [hli]
	ld d, [hl]
	ld e, a
	inc hl
	push hl
	push de
	ld b, CHECK_FLAG
	predef EventFlagAction
	pop de
	pop hl
	jr nz, .loop
	ld b, 1
	predef EventFlagAction
	ld hl, 0
	ret

MersonGoldTokenExchange_EventFlags:
	dw EVENT_GOLDTOKENMAN_MILESTONE_1
	dw EVENT_GOLDTOKENMAN_MILESTONE_2
	dw EVENT_GOLDTOKENMAN_MILESTONE_3
	dw EVENT_GOLDTOKENMAN_MILESTONE_4
	dw EVENT_GOLDTOKENMAN_MILESTONE_5
	dw EVENT_GOLDTOKENMAN_MILESTONE_6
	dw EVENT_GOLDTOKENMAN_MILESTONE_7
	dw EVENT_GOLDTOKENMAN_MILESTONE_8
	dw EVENT_GOLDTOKENMAN_MILESTONE_9
	dw -1

MersonGoldTokenExchange_RequirementsForMilestone:
	jumptext MersonGoldTokenExchange_Text_RequirementsForMilestone

MersonGoldTokenExchange_CheckFinishedDex:
	checkcode VAR_DEXCAUGHT
	sif <, 253
		jumptext MersonGoldTokenExchange_Text_RequirementsForSpecialGift
	writetext MersonGoldTokenExchange_Text_FinishedDex
	waitbutton
	verbosegiveitem TOKENFINDER, 1
	sif false
		jumptext MersonGoldTokenExchange_Text_NeedMoreSpace
	setevent EVENT_GOLDTOKENMAN_MILESTONE_9
	closetext
	end

MersonGoldTokenExchange_AfterFillingDex:
	jumptext MersonGoldTokenExchange_Text_AfterFillingDex

MersonGoldTokenExchange_Text_Intro:
	db TX_COMPRESSED, $62, $7d, $e8, $7f, $1d, $4d, $7e, $6c, $4b, $5c
	db $9b, $d8, $69, $20, $70, $80, $e5, $7c, $24

	db $e2, $01, $4c, $2b, $86, $0c, $6b, $05, $05, $90
	db $b0, $f0, $39, $5f, $09, $20, $20, $b5, $03, $51, $3f, $1d

	db $00, $a8, $7d, $ec, $e4, $60, $c7, $01, $05, $a9, $4f
	db $de, $61, $07, $b9, $85, $05, $48, $c8, $8f

	db $00, $61, $2c, $f6, $e6, $4f, $7b, $12, $d2, $69
	db $69, $e2

MersonGoldTokenExchange_Text_NeedMoreSpace:
	db TX_COMPRESSED, $72, $85, $a8, $43, $3c, $c7, $c2, $b3
	db $79, $02, $35, $93, $98, $05, $70, $10, $5a
	db $5a, $ae, $47, $85, $f1
	db $a6, $80

MersonGoldTokenExchange_Text_PassedMilestone:
	start_asm
	call MersonGoldTextExchange_GetMilestoneNumber
	ld hl, .text
	ret
.text
	db TX_COMPRESSED, $72, $85, $a8, $23, $50, $83, $cc, $b4
	db $9b, $c0, $e4, $47, $c7, $d3, $45, $84, $87
	db $c6, $9a

MersonGoldTokenExchange_Text_RequirementsForMilestone:
	start_asm
	call MersonGoldTextExchange_GetMilestoneNumber
	ld a, [hScriptVar]
	inc a
	ld l, a
	swap a
	sub l
	add a, a
	ld [hTemp], a
	ld hl, .text
	ret
.text
	db TX_COMPRESSED, $72, $85, $a8, $43, $3c, $cb, $0c, $0d, $44
	db $79, $cc, $00
	deciram hTemp, 1, 3
	db TX_COMPRESSED, $80, $e5, $7c, $24, $83
	db $22, $a0, $af, $26, $b9, $61, $82, $35, $08
	db $55, $69, $38, $0e
	db $42, $a7, $c7, $d3, $45, $84, $87
	db $c6, $9a

MersonGoldTextExchange_GetMilestoneNumber:
	push bc
	ld a, [hScriptVar]
	ld hl, MersonGoldTokenExchange_Text_OrdinalNumbers
	call GetNthString
	ld d, h
	ld e, l
	ld hl, wStringBuffer1
	ld bc, 8
	ld a, "@"
	push hl
	call ByteFill
	pop hl
	call PlaceString
	pop bc
	ret

MersonGoldTokenExchange_Text_OrdinalNumbers:
	db "first@"
	db "second@"
	db "third@"
	db "fourth@"
	db "fifth@"
	db "sixth@"
	db "seventh@"
	db "eighth@"

MersonGoldTokenExchange_Text_FinishedDex:
	db TX_COMPRESSED, $74, $f8, $85, $9a, $10, $30, $63

	db $e2, $01, $ca, $16, $a6, $03, $20, $d0, $93, $cc, $b4
	db $9b, $c0, $fd, $ed, $38, $1c, $9d, $3b

	db $c4, $03, $94, $2d, $30, $1c, $7c, $20, $c7, $98, $f9
	db $fd, $ed, $26, $62, $06, $46, $4e, $02, $0b

	db $40, $0c, $94, $e0, $41, $4b, $48, $50, $71, $84, $1e
	db $e5, $e2, $82, $a4, $64, $47, $e3, $a7, $0f, $67, $4e

	db $c0, $02, $0b, $51, $3a, $0c, $73, $49, $07, $66
	db $fb, $d6, $9d, $b9, $50, $fb, $d8, $93, $e8
	db $85, $40, $82, $d4, $c0, $64, $cc, $7c, $2b, $3e
	db $34, $d0

MersonGoldTokenExchange_Text_RequirementsForSpecialGift:
	db TX_COMPRESSED, $75, $13, $a0, $c7, $36, $03, $1a, $c1
	db $3c, $81, $06, $c8, $f7, $b3, $91, $e1, $73, $00
	db $ad, $55, $a4, $28, $39, $52
	db $42, $a9, $90, $be, $23, $a7, $67, $2d, $26, $35
	db $aa, $07, $ef, $69, $c0, $c0, $72, $e4, $e9, $de

	db $20, $1b, $42, $f9, $c5, $7a, $ca, $94, $a9, $26
	db $47, $81, $05, $a8, $c8, $5f, $11, $d3, $b3, $83
	db $b5, $56, $19, $c9, $dc, $d8, $0c, $6b
	db $04, $a9, $02, $0d, $91, $ef, $67, $23, $c2
	db $fd, $4d

MersonGoldTokenExchange_Text_AfterFillingDex:
	db TX_COMPRESSED, $74, $59, $11, $63, $ef, $60, $c9, $10, $90, $b2
	db $a4, $f6, $93, $33, $6a, $38, $10, $63, $5c, $04, $16
	db $96, $ab, $59, $64, $2f, $88, $e8, $d0, $97, $cc, $97

	db $e0, $0e, $50, $b4, $c0, $71, $fd, $c9, $cd
	db $78, $1c, $af, $84, $93, $0d, $24, $06, $35, $82
	db $54, $9d, $10, $20, $17
	db $a9, $a0

MersonGoldTokenExchange_MapEventHeader ;filler
	db 0, 0

;warps
	db 2
	warp_def $5, $4, 2, MERSON_CITY
	warp_def $5, $5, 2, MERSON_CITY

	;xy triggers
	db 0

	;signposts
	db 0

	;people-events
	db 1
	person_event SPRITE_GENTLEMAN, 3, 2, SPRITEMOVEDATA_00, 0, 0, -1, -1, 8 + PAL_OW_YELLOW, PERSONTYPE_SCRIPT, 0, MersonGoldTokenExchangeNPC, -1

