Credits::
	ld a, b
	and $40
	ld [wJumptableIndex], a

	ld a, [rSVBK]
	push af
	wbk BANK(wOriginalBGPals)

	call ClearBGPalettes
	call ClearTileMap
	call ClearSprites

	ld hl, wCreditsFaux2bpp
	ld c, $80
	ld de, $ff00

.load_loop
	ld a, e
	ld [hli], a
	ld a, d
	ld [hli], a
	dec c
	jr nz, .load_loop

	ld de, CreditsBorderGFX
	ld hl, vBGTiles tile $20
	lb bc, BANK(CreditsBorderGFX), $09
	call Request2bpp

	ld de, CopyrightGFX
	ld hl, vBGTiles tile $60
	lb bc, BANK(CopyrightGFX), $1d
	call Request2bpp

	ld de, TheEndGFX
	ld hl, vBGTiles tile $40
	lb bc, BANK(TheEndGFX), $10
	call Request2bpp

	ld a, $ff
	ld [wCreditsBorderFrame], a
	xor a
	ld [wCreditsBorderMon], a

	call Credits_LoadBorderGFX
	ld e, l
	ld d, h
	ld hl, vBGTiles
	lb bc, BANK(CreditsMonsGFX), 16
	call Request2bpp

	call ConstructCreditsTilemap
	xor a
	ld [wCreditsLYOverride], a

	ld hl, wLYOverrides
	ld bc, $100
	xor a
	call ByteFill

	ld a, rSCX & $ff
	ld [wLCDCPointer], a
	ld hl, rIE
	set LCD_STAT, [hl]
	call GetCreditsPalette
	call SetPalettes
	ld a, [hVBlank]
	push af
	ld a, $5
	ld [hVBlank], a
	xor a
	ld [hBGMapMode], a
	ld [wCreditsPos], a
	ld [wCreditsPos + 1], a
	ld [wCreditsTimer], a
	inc a
	ld [hInMenu], a

.execution_loop
	call Credits_HandleBButton
	call Credits_HandleAButton
	jr nz, .exit_credits

	call Credits_Jumptable
	call DelayFrame
	jr .execution_loop

.exit_credits
	call ClearBGPalettes
	xor a
	ld [wLCDCPointer], a
	ld [hBGMapAddress], a
	ld hl, rIE
	res LCD_STAT, [hl]
	pop af
	ld [hVBlank], a
	pop af
	ld [rSVBK], a
	ret

Credits_HandleAButton:
	ld a, [hJoypadDown]
	and A_BUTTON
	ret z
	ld a, [wJumptableIndex]
	bit 7, a
	ret

Credits_HandleBButton:
	ld a, [hJoypadDown]
	and B_BUTTON
	ret z
	ld a, [wJumptableIndex]
	bit 6, a
	ret z
	ld hl, wCreditsPos
	ld a, [hli]
	cp $d
	jr nc, .okay
	ld a, [hli]
	and a
	ret z
.okay
	ld hl, wCreditsTimer
	ld a, [hl]
	and a
	ret z
	dec [hl]
	ret

Credits_Jumptable:
	ld a, [wJumptableIndex]
	and $f
	jumptable

.Jumptable

	dw ParseCredits
	dw Credits_Next
	dw Credits_Next
	dw Credits_PrepBGMapUpdate
	dw Credits_UpdateGFXRequestPath
	dw Credits_RequestGFX
	dw Credits_LYOverride
	dw Credits_Next
	dw Credits_Next
	dw Credits_Next
	dw Credits_UpdateGFXRequestPath
	dw Credits_RequestGFX
	dw Credits_LoopBack


Credits_Next:
	ld hl, wJumptableIndex
	inc [hl]
	ret

Credits_LoopBack:
	ld hl, wJumptableIndex
	ld a, [hl]
	and $f0
	ld [hl], a
	ret

Credits_PrepBGMapUpdate:
	xor a
	ld [hBGMapMode], a
	jp Credits_Next

Credits_UpdateGFXRequestPath:
	call Credits_LoadBorderGFX
	ld a, l
	ld [hRequestedVTileSource], a
	ld a, h
	ld [hRequestedVTileSource + 1], a
	ld a, vBGTiles % $100
	ld [hRequestedVTileDest], a
	ld a, vBGTiles / $100
	ld [hRequestedVTileDest + 1], a
	; fallthrough

Credits_RequestGFX:
	xor a
	ld [hBGMapMode], a
	ld a, $8
	ld [hRequested2bpp], a
	jr Credits_Next

Credits_LYOverride:
	ld a, [rLY]
	cp $30
	jr c, Credits_LYOverride
	ld a, [wCreditsLYOverride]
	sub 2
	ld [wCreditsLYOverride], a
	ld hl, wLYOverrides + $1f
	call .Fill
	ld hl, wLYOverrides + $87
	call .Fill
	jr Credits_Next

.Fill
	ld c, $8
.loop
	ld [hli], a
	dec c
	jr nz, .loop
	ret

ParseCredits:
	ld hl, wJumptableIndex
	bit 7, [hl]
	jr nz, Credits_Next

; Wait until the timer has run out to parse the next command.
	ld hl, wCreditsTimer
	ld a, [hl]
	and a
	jr z, .parse

; One tick has passed.
	dec [hl]
	jr Credits_Next

.parse
; First, let's clear the current text display,
; starting from line 5.
	xor a
	ld [hBGMapMode], a
	hlcoord 0, 5
	ld bc, 20 * 12
	ld a, " "
	call ByteFill

; Then read the script.

.loop
	call .get

; Commands:
	cp CREDITS_END
	jp z, .end
	cp CREDITS_WAIT
	jr z, .wait
	cp CREDITS_SCENE
	jr z, .scene
	cp CREDITS_CLEAR
	jr z, .clear
	cp CREDITS_MUSIC
	jr z, .music
	cp CREDITS_WAIT2
	jr z, .wait2
	cp CREDITS_THEEND
	jr z, .theend

; If it's not a command, it's a string identifier.

	push af
	ld e, a
	ld d, 0
	ld hl, CreditsStrings
	add hl, de
	add hl, de
	ld a, [hli]
	ld d, [hl]
	ld e, a
	pop af

; Strings spanning multiple lines have special cases.

	cp CREDITS_STAFF
	jr c, .staff

; The rest start from line 6.

	hlcoord 1, 6
	jr .print

.copyright
	hlcoord 2, 6
	jr .print

.staff
	hlcoord 1, 6

.print
; Print strings spaced every two lines.
	call .get
	ld bc, 20 * 2
	rst AddNTimes
	call PlaceText
	jr .loop

.theend
; Display "The End" graphic.
	call Credits_TheEnd
	jr .loop

.scene
; Update the scene number and corresponding palette.
	call .get
	ld [wCreditsBorderMon], a ; scene
	xor a
	ld [wCreditsBorderFrame], a ; frame
	call GetCreditsPalette
	call SetPalettes ; update hw pal registers
	jr .loop

.clear
; Clear the banner.
	ld a, $ff
	ld [wCreditsBorderFrame], a ; frame
	jr .loop

.music
; Play the credits music.
	ld de, MUSIC_CREDITS
	push de
	ld de, MUSIC_NONE
	call PlayMusic
	call DelayFrame
	pop de
	call PlayMusic
	jr .loop

.wait2
; Wait for some amount of ticks.
	call .get
	ld [wCreditsTimer], a
	jr .done

.wait
; Wait for some amount of ticks, and do something else.
	call .get
	ld [wCreditsTimer], a

	xor a
	ld [hBGMapHalf], a
	inc a
	ld [hBGMapMode], a

.done
	jp Credits_Next

.end
; Stop execution.
	ld hl, wJumptableIndex
	set 7, [hl]
	ld a, 32
	ld [wMusicFade], a
	ld a, MUSIC_POST_CREDITS % $100
	ld [wMusicFadeID], a
	ld a, MUSIC_POST_CREDITS / $100
	ld [wMusicFadeIDHi], a
	ret

.get
; Get byte wCreditsPos from CreditsScript
	push hl
	push de
	ld hl, wCreditsPos
	push hl
	ld a, [hli]
	ld e, a
	ld d, [hl]
	ld hl, CreditsScript
	add hl, de

	ld a, [hl]
	pop hl
	inc [hl]
	jr nz, .no_carry_on_increment
	inc hl
	inc [hl]
.no_carry_on_increment
	pop de
	pop hl
	ret

ConstructCreditsTilemap:
	xor a
	ld [hBGMapMode], a
	ld a, $c
	ld [hBGMapAddress], a

	ld a, $28
	hlcoord 0, 0
	ld bc, SCREEN_HEIGHT * SCREEN_WIDTH
	call ByteFill

	ld a, $7f
	hlcoord 0, 4
	ld bc, (SCREEN_HEIGHT - 4) * SCREEN_WIDTH
	call ByteFill

	hlcoord 0, 4
	ld a, $24
	call DrawCreditsBorder

	hlcoord 0, 17
	ld a, $20
	call DrawCreditsBorder

	hlcoord 0, 0, wAttrMap
	ld bc, 4 * SCREEN_WIDTH
	xor a
	call ByteFill

	hlcoord 0, 4, wAttrMap
	ld bc, SCREEN_WIDTH
	ld a, $1
	call ByteFill

	hlcoord 0, 5, wAttrMap
	ld bc, 12 * SCREEN_WIDTH
	ld a, $2
	call ByteFill

	hlcoord 0, 17, wAttrMap
	ld bc, SCREEN_WIDTH
	ld a, $1
	call ByteFill

	call ApplyAttrAndTilemapInVBlank
	xor a
	ld [hBGMapMode], a
	ld [hBGMapAddress], a
	hlcoord 0, 0

	ld b, 5
.outer_loop
	push hl
	ld de, SCREEN_WIDTH - 3
	ld c, 4
	xor a
.inner_loop
rept 3
	ld [hli], a
	inc a
endr
	ld [hl], a
	inc a
	add hl, de
	dec c
	jr nz, .inner_loop
	pop hl
rept 4
	inc hl
endr
	dec b
	jr nz, .outer_loop
	jp ApplyAttrAndTilemapInVBlank

DrawCreditsBorder:
	ld c, SCREEN_WIDTH / 4
.loop
	push af
rept 3
	ld [hli], a
	inc a
endr
	ld [hli], a
	pop af
	dec c
	jr nz, .loop
	ret

GetCreditsPalette:
	call .GetPalAddress

	push hl
	ld a, 0
	call .UpdatePals
	pop hl
	ret

.GetPalAddress
; Each set of palette data is 24 bytes long.
	ld a, [wCreditsBorderMon] ; scene
	and 3
	add a
	add a ; * 8
	add a
	ld e, a
	ld d, 0
	ld hl, CreditsPalettes
	add hl, de
	add hl, de ; * 3
	add hl, de
	ret

.UpdatePals
; Update the first three colors in both palette buffers.

	push af
	push hl
	add wOriginalBGPals % $100
	ld e, a
	ld a, 0
	adc wOriginalBGPals / $100
	ld d, a
	ld bc, 24
	rst CopyBytes

	pop hl
	pop af
	add wBGPals % $100
	ld e, a
	ld a, 0
	adc wBGPals / $100
	ld d, a
	ld bc, 24
	rst CopyBytes
	ret


CreditsPalettes:

; Pichu
	RGB 31, 00, 31
	RGB 31, 25, 00
	RGB 11, 14, 31
	RGB 07, 07, 07

	RGB 31, 05, 05
	RGB 11, 14, 31
	RGB 11, 14, 31
	RGB 31, 31, 31

	RGB 31, 05, 05
	RGB 00, 00, 00
	RGB 31, 31, 31
	RGB 31, 31, 31

; Smoochum
	RGB 31, 31, 31
	RGB 31, 27, 00
	RGB 26, 06, 31
	RGB 07, 07, 07

	RGB 03, 13, 31
	RGB 20, 00, 24
	RGB 26, 06, 31
	RGB 31, 31, 31

	RGB 03, 13, 31
	RGB 00, 00, 00
	RGB 31, 31, 31
	RGB 31, 31, 31

; Ditto
	RGB 31, 31, 31
	RGB 23, 12, 28
	RGB 31, 22, 00
	RGB 07, 07, 07

	RGB 03, 20, 00
	RGB 31, 22, 00
	RGB 31, 22, 00
	RGB 31, 31, 31

	RGB 03, 20, 00
	RGB 00, 00, 00
	RGB 31, 31, 31
	RGB 31, 31, 31

; Igglybuff
	RGB 31, 31, 31
	RGB 31, 10, 31
	RGB 31, 00, 09
	RGB 07, 07, 07

	RGB 31, 14, 00
	RGB 31, 00, 09
	RGB 31, 00, 09
	RGB 31, 31, 31

	RGB 31, 14, 00
	RGB 31, 31, 31
	RGB 31, 31, 31
	RGB 31, 31, 31

Credits_LoadBorderGFX:
	ld hl, wCreditsBorderFrame
	ld a, [hl]
	cp $ff
	jr z, .init

	and 3
	ld e, a
	inc a
	and 3
	ld [hl], a
	ld a, [wCreditsBorderMon]
	and 3
	add a
	add a
	add e
	add a
	ld e, a
	ld d, 0
	ld hl, .Frames
	add hl, de
	ld a, [hli]
	ld h, [hl]
	ld l, a
	ret

.init
	ld hl, wCreditsFaux2bpp
	ret

.Frames
	dw CreditsPichuGFX
	dw CreditsPichuGFX     + 16 tiles
	dw CreditsPichuGFX     + 32 tiles
	dw CreditsPichuGFX     + 48 tiles
	dw CreditsSmoochumGFX
	dw CreditsSmoochumGFX  + 16 tiles
	dw CreditsSmoochumGFX  + 32 tiles
	dw CreditsSmoochumGFX  + 48 tiles
	dw CreditsDittoGFX
	dw CreditsDittoGFX     + 16 tiles
	dw CreditsDittoGFX     + 32 tiles
	dw CreditsDittoGFX     + 48 tiles
	dw CreditsIgglybuffGFX
	dw CreditsIgglybuffGFX + 16 tiles
	dw CreditsIgglybuffGFX + 32 tiles
	dw CreditsIgglybuffGFX + 48 tiles

Credits_TheEnd:
	ld a, $40
	hlcoord 6, 9
	call .Load
	hlcoord 6, 10
.Load
	ld c, 8
.loop
	ld [hli], a
	inc a
	dec c
	jr nz, .loop
	ret

CreditsBorderGFX:    INCBIN "gfx/credits/border.2bpp"

CreditsMonsGFX:
CreditsPichuGFX:     INCBIN "gfx/credits/pichu.2bpp"
CreditsSmoochumGFX:  INCBIN "gfx/credits/smoochum.2bpp"
CreditsDittoGFX:     INCBIN "gfx/credits/ditto.2bpp"
CreditsIgglybuffGFX: INCBIN "gfx/credits/igglybuff.2bpp"


CreditsScript:

; Clear the banner.
	db CREDITS_CLEAR

; Pokemon Crystal Version Staff
	db   CREDITS_STAFF, 1

	db 	 CREDITS_WAIT, 8

; Play the credits music.
	db CREDITS_MUSIC

	db CREDITS_WAIT2, 10

	db CREDITS_WAIT, 1

; Update the banner.
	db CREDITS_SCENE, 0 ;Pichu
	db CREDITS_INITIAL_1, 1
	db CREDITS_WAIT, 40

	db CREDITS_SCENE, 1 ;Smoochum
	db CREDITS_INITIAL_2, 1
	db CREDITS_WAIT, 40
	
	db CREDITS_SCENE, 2 ;Ditto
	db CREDITS_KBM_1, 1
	db CREDITS_WAIT, 25
	
	db CREDITS_KBM_2, 1
	db CREDITS_WAIT, 35
	
	db CREDITS_SCENE, 3 ;Igglybuff
	db CREDITS_ORIGINAL_DEVS_1, 1
	db CREDITS_WAIT, 25
	
	db CREDITS_ORIGINAL_DEVS_2, 1
	db CREDITS_WAIT, 35
	
	db CREDITS_SCENE, 0 ;Pichu
	db CREDITS_LEAKERS_1, 1
	db CREDITS_WAIT, 25
	
	db CREDITS_LEAKERS_2, 1
	db CREDITS_WAIT, 35
	
	db CREDITS_SCENE, 1 ;Smoochum
	db CREDITS_GAME_FREAK_1, 1
	db CREDITS_WAIT, 25
	
	db CREDITS_GAME_FREAK_2, 1
	db CREDITS_WAIT, 25
	
	db CREDITS_GAME_FREAK_3, 1
	db CREDITS_WAIT, 35
	
	db CREDITS_SCENE, 2 ;Ditto
	db CREDITS_YOU, 1
	db CREDITS_WAIT, 40
	
	db CREDITS_SCENE, 3 ;Igglybuff
	db CREDITS_CLOSING, 1
	db CREDITS_WAIT, 70
	
; Display "The End" graphic.
	db CREDITS_SCENE, 0 ;Pichu
	db CREDITS_THEEND

	db CREDITS_WAIT, 30

	db CREDITS_END

CreditsStrings:
	dw .Staff
	dw .initial_1
	dw .initial_2
	dw .kbm_1
	dw .kbm_2
	dw .original_devs_1
	dw .original_devs_2
	dw .leakers_1
	dw .leakers_2
	dw .game_freak_1
	dw .game_freak_2
	dw .game_freak_3
	dw .you
	dw .closing

.Staff
	db TX_COMPRESSED, $92, $49, $01, $ca, $f8
	db $49, $4f, $0a, $49, $24, $9c, $2b, $1a, $1f
	db $a7, $85, $24, $92, $36, $ac, $f2, $3b
	db $45, $34

.initial_1
	db TX_COMPRESSED, $c7, $0d, $a2, $07, $08, $20, $c2, $23
	db $a7, $4f, $0b, $23, $cc, $d9, $d0, $dc, $0e, $12, $a0, $ad, $4b
	db $d4, $f0, $85, $42, $8b, $e8, $29, $a4, $09, $01, $3e, $16, $91
	db $ea, $68

.initial_2
	db TX_COMPRESSED, $12, $6a, $e5, $47, $8d, $17, $b6, $93, $5c, $69
	db $07, $4f, $0a, $d2, $68, $38, $ca, $cf, $23, $b4, $4b
	db $0d, $3c, $2b, $4e, $91, $4a, $20, $2f, $9c, $10, $61, $11, $d3
	db $f5, $34

.kbm_1
	db TX_COMPRESSED, $29, $3a, $45, $24, $4b
	db $0d, $3c, $21, $30, $10, $f5, $58, $41, $3f, $a4, $fc, $c0, $2b
	db $a7, $84, $9d, $06, $19, $39, $8f, $ee, $4e, $5a
	db $4e, $9a

.kbm_2
	db TX_COMPRESSED, $0a, $c7, $c8, $c9, $bd, $80, $c6, $b4, $0c, $24
	db $a7, $84, $1c, $25, $a4, $68, $9c, $eb, $e7, $f9, $50, $56
	db $06, $9e, $12, $90, $c9, $cc, $c0, $2b, $94, $fa, $02, $3d, $5a
	db $3d, $4d

.original_devs_1
	db TX_COMPRESSED, $29, $3a, $45, $24, $4b, $0c, $b4
	db $9d, $3c, $20, $ac, $7c, $8c, $9b, $d9, $93, $06, $96, $7a, $ff
	db $e9, $e1, $54, $90, $c7, $f7, $27, $2d, $23, $44, $e7, $5f
	db $3a, $68

.original_devs_2
	db TX_COMPRESSED, $10, $14, $20, $6a, $fa, $7f, $9a, $4c
	db $d3, $c2, $de, $be, $14, $5c, $c0, $64, $1a, $12, $79
	db $a7, $84, $3b
	db $f5, $34

.leakers_1
	db TX_COMPRESSED, $28, $32, $d2, $73, $a7, $aa, $46, $b4
	db $7e, $9e, $15, $a7, $48, $a4, $89, $61, $95, $24, $2f
	db $d3, $c2, $30, $63, $58, $20, $90, $e3, $3a, $48, $8b, $1f
	db $7b, $4d

.leakers_2
	db TX_COMPRESSED, $11, $dd, $05, $2d, $27, $39, $d7, $cf, $f3, $49
	db $9a, $78, $56, $9d, $22, $92, $25, $86, $54, $90, $bf, $2a
	db $3a, $78, $4c, $e9, $26, $4c, $18, $fa, $11, $10, $24, $83, $bf
	db $53, $40

.game_freak_1
	db TX_COMPRESSED, $ac, $99, $96, $9d, $22, $92, $25, $86, $30, $b5, $f3
	db $a7, $84, $a6, $b3, $d5, $29, $a4, $cc, $b4, $98
	db $d7, $4f, $0b, $50, $80, $b2, $3d, $b3, $47, $e6, $01
	db $5d, $34

.game_freak_2
	db TX_COMPRESSED, $65, $67, $b6, $32, $73, $2d, $23
	db $45, $3c, $2d, $7f, $44, $d8, $c9, $cc, $c1, $7a, $46, $52, $34
	db $1d, $3c, $2b, $4e, $dc, $45, $ea, $ec, $f3, $07
	db $74, $d0

.game_freak_3
	db TX_COMPRESSED, $de, $f7
	db $d4, $f0, $b8, $7a, $7a, $83, $8a, $da, $05, $2d, $26, $35
	db $d3, $c2, $e5, $67, $b7, $39, $d7, $cd, $1e
	db $a6, $80

.you
	db TX_COMPRESSED, $53, $19, $37, $bd, $05, $f9, $69, $d2, $29, $40, $41
	db $6a, $9e, $16, $01, $5c, $11, $dd, $04, $32, $73, $2d, $23
	db $45, $3c, $2e, $75, $f3
	db $e3, $4d

.closing
	db TX_COMPRESSED, $6d, $09, $39, $5e, $db, $5d, $db, $18, $48, $88, $12
	db $53, $c2, $04, $16, $97, $01, $86, $cd, $85, $60, $b8
	db $d3, $c2, $02, $e4, $d1, $91, $58, $6a, $e0, $30, $64, $7e, $39, $a2, $c0, $b5, $4f
	db $69, $a0

.Copyright
	;    (C) 1  9  9  5 - 2  0  0  1     N  i  n  t  e  n  d  o
	text $60,$61,$62,$63,$64,$65,$66, $67, $68, $69, $6a, $6b, $6c
	;    (C) 1  9  9  5 - 2  0  0  1    C  r  e  a  t  u  r  e  s      i  n  c .
	next $60,$61,$62,$63,$64,$65,$66, $6d, $6e, $6f, $70, $71, $72,  $7a, $7b, $7c
	;    (C) 1  9  9  5 - 2  0  0  1  G   A   M   E   F   R   E   A   K     i  n  c .
	next $60,$61,$62,$63,$64,$65,$66, $73, $74, $75, $76, $77, $78, $79,  $7a, $7b, $7c
	done

