summaryrefslogtreecommitdiff
path: root/engine/games
diff options
context:
space:
mode:
authorentrpntr <12521136+entrpntr@users.noreply.github.com>2020-06-05 17:54:54 -0400
committerGitHub <noreply@github.com>2020-06-05 17:54:54 -0400
commit41aed88e16a138293971d63e7facfceb8f612c60 (patch)
tree214be900e6128abe76d6d1b599068da02abfc40a /engine/games
parente28325211cfc584ebed4389ab242014cc80ab2c9 (diff)
parent13eb43eb1b14d227b73ccfc6ba000ff982189425 (diff)
Merge pull request #44 from libjet/bank38
Disassemble bank $38
Diffstat (limited to 'engine/games')
-rwxr-xr-xengine/games/card_flip.asm1614
-rwxr-xr-xengine/games/dummy_game.asm590
-rwxr-xr-xengine/games/unown_puzzle.asm846
3 files changed, 3050 insertions, 0 deletions
diff --git a/engine/games/card_flip.asm b/engine/games/card_flip.asm
new file mode 100755
index 00000000..82faf692
--- /dev/null
+++ b/engine/games/card_flip.asm
@@ -0,0 +1,1614 @@
+CARDFLIP_LIGHT_OFF EQU "♂" ; $ef
+CARDFLIP_LIGHT_ON EQU "♀" ; $f5
+
+CARDFLIP_DECK_SIZE EQU 4 * 6
+
+; two labels below called from inside ./dummy_game.asm
+Unknown_e0908:
+; Graphics for an unused Game Corner
+; game were meant to be here.
+ret_e0908:
+ ret
+
+_CardFlip:
+ ld hl, wOptions
+ set NO_TEXT_SCROLL, [hl]
+ call ClearBGPalettes
+ call ClearTilemap
+ call ClearSprites
+ ld de, MUSIC_NONE
+ call PlayMusic
+ call DelayFrame
+ call DisableLCD
+ call LoadStandardFont
+ call LoadFontsExtra
+
+ ld hl, CardFlipLZ01
+ ld de, vTiles2 tile $00
+ call Decompress
+ ld hl, CardFlipLZ02
+ ld de, vTiles2 tile $3e
+ call Decompress
+ ld hl, CardFlipLZ03
+ ld de, vTiles0 tile $00
+ call Decompress
+ ld hl, CardFlipOffButtonGFX
+ ld de, vTiles0 tile CARDFLIP_LIGHT_OFF
+ ld bc, 1 tiles
+ call CopyBytes
+ ld hl, CardFlipOnButtonGFX
+ ld de, vTiles0 tile CARDFLIP_LIGHT_ON
+ ld bc, 1 tiles
+ call CopyBytes
+
+ call CardFlip_InitTilemap
+ call CardFlip_InitAttrPals
+ call EnableLCD
+ call WaitBGMap2
+ ld a, %11100100
+ call DmgToCgbBGPals
+ lb de, %11100100, %11100100
+ call DmgToCgbObjPals
+ call DelayFrame
+ xor a
+ ld [wJumptableIndex], a
+ ld a, $2
+ ld [wCardFlipCursorY], a
+ ld [wCardFlipCursorX], a
+ ld de, MUSIC_GAME_CORNER
+ call PlayMusic
+.MasterLoop:
+ ld a, [wJumptableIndex]
+ bit 7, a
+ jr nz, .leavethegame
+ call .CardFlip
+ jr .MasterLoop
+.leavethegame
+ call WaitSFX
+ ld de, SFX_QUIT_SLOTS
+ call PlaySFX
+ call WaitSFX
+ call ClearBGPalettes
+ ld hl, wOptions
+ res 4, [hl]
+ ret
+
+.CardFlip:
+ ld a, [wJumptableIndex]
+ ld e, a
+ ld d, 0
+ ld hl, .Jumptable
+ add hl, de
+ add hl, de
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ jp hl
+
+.Jumptable:
+ dw .AskPlayWithThree
+ dw .DeductCoins
+ dw .ChooseACard
+ dw .PlaceYourBet
+ dw .CheckTheCard
+ dw .TabulateTheResult
+ dw .PlayAgain
+ dw .Quit
+
+.Increment:
+ ld hl, wJumptableIndex
+ inc [hl]
+ ret
+
+.AskPlayWithThree:
+ ld hl, .CardFlipPlayWithThreeCoinsText
+ call CardFlip_UpdateCoinBalanceDisplay
+ call YesNoBox
+ jr c, .SaidNo
+ call CardFlip_ShuffleDeck
+ call .Increment
+ ret
+
+.SaidNo:
+ ld a, 7
+ ld [wJumptableIndex], a
+ ret
+
+.CardFlipPlayWithThreeCoinsText:
+ text_far _CardFlipPlayWithThreeCoinsText
+ text_end
+
+.DeductCoins:
+ ld a, [wCoins]
+ ld h, a
+ ld a, [wCoins + 1]
+ ld l, a
+ ld a, h
+ and a
+ jr nz, .deduct ; You have at least 256 coins.
+ ld a, l
+ cp 3
+ jr nc, .deduct ; You have at least 3 coins.
+ ld hl, .CardFlipNotEnoughCoinsText
+ call CardFlip_UpdateCoinBalanceDisplay
+ ld a, 7
+ ld [wJumptableIndex], a
+ ret
+
+.deduct
+ ld de, -3
+ add hl, de
+ ld a, h
+ ld [wCoins], a
+ ld a, l
+ ld [wCoins + 1], a
+ ld de, SFX_TRANSACTION
+ call PlaySFX
+ xor a
+ ldh [hBGMapMode], a
+ call CardFlip_PrintCoinBalance
+ ld a, $1
+ ldh [hBGMapMode], a
+ call WaitSFX
+ call .Increment
+ ret
+
+.CardFlipNotEnoughCoinsText:
+ text_far _CardFlipNotEnoughCoinsText
+ text_end
+
+.ChooseACard:
+ xor a
+ ldh [hBGMapMode], a
+ hlcoord 0, 0
+ lb bc, 12, 9
+ call CardFlip_FillGreenBox
+ hlcoord 9, 0
+ ld bc, SCREEN_WIDTH
+ ld a, [wCardFlipNumCardsPlayed]
+ call AddNTimes
+ ld [hl], CARDFLIP_LIGHT_ON
+ ld a, $1
+ ldh [hBGMapMode], a
+ ld c, 20
+ call DelayFrames
+ hlcoord 2, 0
+ call PlaceCardFaceDown
+ ld a, $1
+ ldh [hBGMapMode], a
+ ld c, 20
+ call DelayFrames
+ hlcoord 2, 6
+ call PlaceCardFaceDown
+ call WaitBGMap
+ ld hl, .CardFlipChooseACardText
+ call CardFlip_UpdateCoinBalanceDisplay
+ xor a
+ ld [wCardFlipWhichCard], a
+.loop
+ call JoyTextDelay
+ ldh a, [hJoyLast]
+ and A_BUTTON
+ jr nz, .next
+ ld de, SFX_KINESIS
+ call PlaySFX
+ call PlaceOAMCardBorder
+ ld c, 4
+ call DelayFrames
+ ld hl, wCardFlipWhichCard
+ ld a, [hl]
+ xor $1
+ ld [hl], a
+ jr .loop
+
+.next
+ ld de, SFX_SLOT_MACHINE_START
+ call PlaySFX
+ ld a, $3
+.loop2
+ push af
+ call PlaceOAMCardBorder
+ ld c, 4
+ call DelayFrames
+ call ClearSprites
+ ld c, 4
+ call DelayFrames
+ pop af
+ dec a
+ jr nz, .loop2
+ ld hl, wCardFlipWhichCard
+ ld a, [hl]
+ push af
+ xor $1
+ ld [hl], a
+ call GetCoordsOfChosenCard
+ lb bc, 6, 5
+ call CardFlip_FillGreenBox
+ pop af
+ ld [wCardFlipWhichCard], a
+ call .Increment
+ ret
+
+.CardFlipChooseACardText:
+ text_far _CardFlipChooseACardText
+ text_end
+
+.PlaceYourBet:
+ ld hl, .CardFlipPlaceYourBetText
+ call CardFlip_UpdateCoinBalanceDisplay
+.betloop
+ call JoyTextDelay
+ ldh a, [hJoyLast]
+ and A_BUTTON
+ jr nz, .betdone
+ call ChooseCard_HandleJoypad
+ call CardFlip_UpdateCursorOAM
+ call DelayFrame
+ jr .betloop
+
+.betdone
+ call .Increment
+ ret
+
+.CardFlipPlaceYourBetText:
+ text_far _CardFlipPlaceYourBetText
+ text_end
+
+.CheckTheCard:
+ xor a
+ ldh [hVBlankCounter], a
+ call CardFlip_UpdateCursorOAM
+ call WaitSFX
+ ld de, SFX_CHOOSE_A_CARD
+ call PlaySFX
+ call WaitSFX
+ ld a, [wCardFlipNumCardsPlayed]
+ ld e, a
+ ld d, 0
+ ld hl, wDeck
+ add hl, de
+ add hl, de
+ ld a, [wCardFlipWhichCard]
+ ld e, a
+ add hl, de
+ ld a, [hl]
+ ld [wCardFlipFaceUpCard], a
+ ld e, a
+ ld hl, wDiscardPile
+ add hl, de
+ ld [hl], TRUE
+ call GetCoordsOfChosenCard
+ call CardFlip_DisplayCardFaceUp
+ call WaitBGMap2
+ call .Increment
+ ret
+
+.TabulateTheResult:
+ call CardFlip_CheckWinCondition
+ call WaitPressAorB_BlinkCursor
+ call .Increment
+ ret
+
+.PlayAgain:
+ call ClearSprites
+ ld hl, .CardFlipPlayAgainText
+ call CardFlip_UpdateCoinBalanceDisplay
+ call YesNoBox
+ jr nc, .Continue
+ call .Increment
+ ret
+
+.Continue:
+ ld a, [wCardFlipNumCardsPlayed]
+ inc a
+ ld [wCardFlipNumCardsPlayed], a
+ cp 12
+ jr c, .KeepTheCurrentDeck
+ call CardFlip_InitTilemap
+ ld a, $1
+ ldh [hBGMapMode], a
+ call CardFlip_ShuffleDeck
+ ld hl, .CardFlipShuffledText
+ call PrintText
+ jr .LoopAround
+
+.KeepTheCurrentDeck:
+ call CardFlip_BlankDiscardedCardSlot
+
+.LoopAround:
+ ld a, 1
+ ld [wJumptableIndex], a
+ ret
+
+.CardFlipPlayAgainText:
+ text_far _CardFlipPlayAgainText
+ text_end
+
+.CardFlipShuffledText:
+ text_far _CardFlipShuffledText
+ text_end
+
+.Quit:
+ ld hl, wJumptableIndex
+ set 7, [hl]
+ ret
+
+CardFlip_ShuffleDeck:
+ ld hl, wDeck
+ ld bc, CARDFLIP_DECK_SIZE
+ xor a
+ call ByteFill
+ ld de, wDeck
+ ld c, CARDFLIP_DECK_SIZE - 1
+.loop
+ call Random
+ and $1f
+ cp CARDFLIP_DECK_SIZE
+ jr nc, .loop
+ ld l, a
+ ld h, $0
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .loop
+ ld [hl], c
+ dec c
+ jr nz, .loop
+ xor a
+ ld [wCardFlipNumCardsPlayed], a
+ ld hl, wDiscardPile
+ ld bc, CARDFLIP_DECK_SIZE
+ call ByteFill
+ ret
+
+CollapseCursorPosition:
+ ld hl, 0
+ ld bc, 6
+ ld a, [wCardFlipCursorY]
+ call AddNTimes
+ ld b, $0
+ ld a, [wCardFlipCursorX]
+ ld c, a
+ add hl, bc
+ ret
+
+GetCoordsOfChosenCard:
+ ld a, [wCardFlipWhichCard]
+ and a
+ jr nz, .BottomCard
+ hlcoord 2, 0
+ bcpixel 2, 3
+ jr .done
+
+.BottomCard:
+ hlcoord 2, 6
+ bcpixel 8, 3
+
+.done
+ ret
+
+PlaceCardFaceDown:
+ xor a
+ ldh [hBGMapMode], a
+ ld de, .FaceDownCardTilemap
+ lb bc, 6, 5
+ call CardFlip_CopyToBox
+ ret
+
+.FaceDownCardTilemap:
+ db $08, $09, $09, $09, $0a
+ db $0b, $28, $2b, $28, $0c
+ db $0b, $2c, $2d, $2e, $0c
+ db $0b, $2f, $30, $31, $0c
+ db $0b, $32, $33, $34, $0c
+ db $0d, $0e, $0e, $0e, $0f
+
+CardFlip_DisplayCardFaceUp:
+ xor a
+ ldh [hBGMapMode], a
+ push hl
+ push hl
+ ; Flip the card face up.
+ ld de, .FaceUpCardTilemap
+ lb bc, 6, 5
+ call CardFlip_CopyToBox
+
+ ; Get the level and species of the upturned card.
+ ld a, [wCardFlipFaceUpCard]
+ ld e, a
+ ld d, 0
+ ld hl, .Deck
+ add hl, de
+ add hl, de
+ ld a, [hli]
+ ld e, a
+ ld d, [hl]
+
+ ; Place the level.
+ pop hl
+ ld bc, 3 + SCREEN_WIDTH
+ add hl, bc
+ ld [hl], e
+
+ ; Place the Pokepic.
+ ld bc, SCREEN_HEIGHT
+ add hl, bc
+ ld a, d
+ ld de, SCREEN_WIDTH
+ ld b, 3
+.row
+ push hl
+ ld c, 3
+.col
+ ld [hli], a
+ inc a
+ dec c
+ jr nz, .col
+ pop hl
+ add hl, de
+ dec b
+ jr nz, .row
+ pop hl
+
+ ; Pointless CGB check
+ ldh a, [hCGB]
+ and a
+ ret z
+
+ ; Set the attributes
+ ld de, wAttrmap - wTilemap
+ add hl, de
+ ld a, [wCardFlipFaceUpCard]
+ and 3
+ inc a
+ lb bc, 6, 5
+ call CardFlip_FillBox
+ ret
+
+.FaceUpCardTilemap:
+ db $18, $19, $19, $19, $1a
+ db $1b, $35, $7f, $7f, $1c
+ db $0b, $28, $28, $28, $0c
+ db $0b, $28, $28, $28, $0c
+ db $0b, $28, $28, $28, $0c
+ db $1d, $1e, $1e, $1e, $1f
+
+.Deck:
+ ; level, pic anchor (3x3)
+ db "1", $4e, "1", $57, "1", $69, "1", $60
+ db "2", $4e, "2", $57, "2", $69, "2", $60
+ db "3", $4e, "3", $57, "3", $69, "3", $60
+ db "4", $4e, "4", $57, "4", $69, "4", $60
+ db "5", $4e, "5", $57, "5", $69, "5", $60
+ db "6", $4e, "6", $57, "6", $69, "6", $60
+
+CardFlip_UpdateCoinBalanceDisplay:
+ push hl
+ hlcoord 0, 12
+ ld b, 4
+ ld c, SCREEN_WIDTH - 2
+ call Textbox
+ pop hl
+ call PrintTextboxText
+ call CardFlip_PrintCoinBalance
+ ret
+
+CardFlip_PrintCoinBalance:
+ hlcoord 9, 15
+ lb bc, 1, 9
+ call Textbox
+ hlcoord 10, 16
+ ld de, .CoinStr
+ call PlaceString
+ hlcoord 15, 16
+ ld de, wCoins
+ lb bc, PRINTNUM_LEADINGZEROS | 2, 4
+ call PrintNum
+ ret
+
+.CoinStr:
+ db "COIN@"
+
+CardFlip_InitTilemap:
+ xor a
+ ldh [hBGMapMode], a
+ hlcoord 0, 0
+ ld bc, SCREEN_HEIGHT * SCREEN_WIDTH
+ ld a, $29
+ call ByteFill
+ hlcoord 9, 0
+ ld de, CardFlipTilemap
+ lb bc, 12, 11
+ call CardFlip_CopyToBox
+ hlcoord 0, 12
+ lb bc, 4, 18
+ call Textbox
+ ret
+
+CardFlip_FillGreenBox:
+ ld a, $29
+
+CardFlip_FillBox:
+.row
+ push bc
+ push hl
+.col
+ ld [hli], a
+ dec c
+ jr nz, .col
+ pop hl
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ pop bc
+ dec b
+ jr nz, .row
+ ret
+
+CardFlip_CopyToBox:
+.row
+ push bc
+ push hl
+.col
+ ld a, [de]
+ inc de
+ ld [hli], a
+ dec c
+ jr nz, .col
+ pop hl
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ pop bc
+ dec b
+ jr nz, .row
+ ret
+
+CardFlip_CopyOAM:
+ ld de, wVirtualOAMSprite00
+ ld a, [hli]
+.loop
+ push af
+ ld a, [hli]
+ add b
+ ld [de], a ; y
+ inc de
+ ld a, [hli]
+ add c
+ ld [de], a ; x
+ inc de
+ ld a, [hli]
+ ld [de], a ; tile id
+ inc de
+ ld a, [hli]
+ ld [de], a ; attributes
+ inc de
+ pop af
+ dec a
+ jr nz, .loop
+ ret
+
+CardFlip_ShiftDigitsUpOnePixel:
+; The top rows of digits 1-9 become the bottom rows of 0-8,
+; so this routine relies on the top rows being blank.
+ ld de, vTiles0 tile "0"
+ ld hl, vTiles0 tile "0" + 2
+ ld bc, 10 tiles - 2
+ call CopyBytes
+ ld hl, vTiles0 tile "9" + 1 tiles - 2
+ xor a
+ ld [hli], a
+ ld [hl], a
+ ret
+
+CardFlip_BlankDiscardedCardSlot:
+ xor a
+ ldh [hBGMapMode], a
+ ld a, [wCardFlipFaceUpCard]
+ ld e, a
+ ld d, 0
+
+ and 3 ; get mon
+ ld c, a
+ ld b, 0
+
+ ld a, e
+ and $1c ; get level
+ srl a
+ add LOW(.Jumptable)
+ ld l, a
+ ld a, 0
+ adc HIGH(.Jumptable)
+ ld h, a
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ jp hl
+
+.Jumptable:
+ dw .Level1
+ dw .Level2
+ dw .Level3
+ dw .Level4
+ dw .Level5
+ dw .Level6
+
+.Level1:
+ ld hl, wDiscardPile + 4
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .discarded2
+ hlcoord 13, 3
+ add hl, bc
+ add hl, bc
+ ld [hl], $36
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $37
+ ret
+
+.discarded2
+ hlcoord 13, 3
+ add hl, bc
+ add hl, bc
+ ld [hl], $36
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3d
+ ret
+
+.Level2:
+ ld hl, wDiscardPile - 4
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .discarded1
+ hlcoord 13, 4
+ add hl, bc
+ add hl, bc
+ ld [hl], $3b
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3a
+ ret
+
+.discarded1
+ hlcoord 13, 4
+ add hl, bc
+ add hl, bc
+ ld [hl], $3d
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3a
+ ret
+
+.Level3:
+ ld hl, wDiscardPile + 4
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .discarded4
+ hlcoord 13, 6
+ add hl, bc
+ add hl, bc
+ ld [hl], $36
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $38
+ ret
+
+.discarded4
+ hlcoord 13, 6
+ add hl, bc
+ add hl, bc
+ ld [hl], $36
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3d
+ ret
+
+.Level4:
+ ld hl, wDiscardPile - 4
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .discarded3
+ hlcoord 13, 7
+ add hl, bc
+ add hl, bc
+ ld [hl], $3c
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3a
+ ret
+
+.discarded3
+ hlcoord 13, 7
+ add hl, bc
+ add hl, bc
+ ld [hl], $3d
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3a
+ ret
+
+.Level5:
+ ld hl, wDiscardPile + 4
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .discarded6
+ hlcoord 13, 9
+ add hl, bc
+ add hl, bc
+ ld [hl], $36
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $39
+ ret
+
+.discarded6
+ hlcoord 13, 9
+ add hl, bc
+ add hl, bc
+ ld [hl], $36
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3d
+ ret
+
+.Level6:
+ ld hl, wDiscardPile - 4
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .discarded5
+ hlcoord 13, 10
+ add hl, bc
+ add hl, bc
+ ld [hl], $3c
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3a
+ ret
+
+.discarded5
+ hlcoord 13, 10
+ add hl, bc
+ add hl, bc
+ ld [hl], $3d
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hl], $3a
+ ret
+
+CardFlip_CheckWinCondition:
+ call CollapseCursorPosition
+ add hl, hl
+ ld de, .Jumptable
+ add hl, de
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ jp hl
+
+.Jumptable:
+ dw .Impossible
+ dw .Impossible
+ dw .PikaJiggly
+ dw .PikaJiggly
+ dw .PoliOddish
+ dw .PoliOddish
+
+ dw .Impossible
+ dw .Impossible
+ dw .Pikachu
+ dw .Jigglypuff
+ dw .Poliwag
+ dw .Oddish
+
+ dw .OneTwo
+ dw .One
+ dw .PikaOne
+ dw .JigglyOne
+ dw .PoliOne
+ dw .OddOne
+
+ dw .OneTwo
+ dw .Two
+ dw .PikaTwo
+ dw .JigglyTwo
+ dw .PoliTwo
+ dw .OddTwo
+
+ dw .ThreeFour
+ dw .Three
+ dw .PikaThree
+ dw .JigglyThree
+ dw .PoliThree
+ dw .OddThree
+
+ dw .ThreeFour
+ dw .Four
+ dw .PikaFour
+ dw .JigglyFour
+ dw .PoliFour
+ dw .OddFour
+
+ dw .FiveSix
+ dw .Five
+ dw .PikaFive
+ dw .JigglyFive
+ dw .PoliFive
+ dw .OddFive
+
+ dw .FiveSix
+ dw .Six
+ dw .PikaSix
+ dw .JigglySix
+ dw .PoliSix
+ dw .OddSix
+
+.Impossible:
+ jp .Lose
+
+.PikaJiggly:
+ ld a, [wCardFlipFaceUpCard]
+ and $2
+ jp nz, .Lose
+ jr .WinSix
+
+.PoliOddish:
+ ld a, [wCardFlipFaceUpCard]
+ and $2
+ jr nz, .WinSix
+ jp .Lose
+
+.WinSix:
+ ld c, $6
+ ld de, SFX_2ND_PLACE
+ jp .Payout
+
+.OneTwo:
+ ld a, [wCardFlipFaceUpCard]
+ and $18
+ jr z, .WinNine
+ jp .Lose
+
+.ThreeFour:
+ ld a, [wCardFlipFaceUpCard]
+ and $18
+ cp $8
+ jr z, .WinNine
+ jp .Lose
+
+.FiveSix:
+ ld a, [wCardFlipFaceUpCard]
+ and $18
+ cp $10
+ jr z, .WinNine
+ jp .Lose
+
+.WinNine:
+ ld c, $9
+ ld de, SFX_2ND_PLACE
+ jp .Payout
+
+.Pikachu:
+ ld a, [wCardFlipFaceUpCard]
+ and $3
+ jr z, .WinTwelve
+ jp .Lose
+
+.Jigglypuff:
+ ld a, [wCardFlipFaceUpCard]
+ and $3
+ cp $1
+ jr z, .WinTwelve
+ jp .Lose
+
+.Poliwag:
+ ld a, [wCardFlipFaceUpCard]
+ and $3
+ cp $2
+ jr z, .WinTwelve
+ jp .Lose
+
+.Oddish:
+ ld a, [wCardFlipFaceUpCard]
+ and $3
+ cp $3
+ jr z, .WinTwelve
+ jp .Lose
+
+.WinTwelve:
+ ld c, $c
+ ld de, SFX_2ND_PLACE
+ jp .Payout
+
+.One:
+ ld a, [wCardFlipFaceUpCard]
+ and $1c
+ jr z, .WinEighteen
+ jp .Lose
+
+.Two:
+ ld a, [wCardFlipFaceUpCard]
+ and $1c
+ cp $4
+ jr z, .WinEighteen
+ jp .Lose
+
+.Three:
+ ld a, [wCardFlipFaceUpCard]
+ and $1c
+ cp $8
+ jr z, .WinEighteen
+ jp .Lose
+
+.Four:
+ ld a, [wCardFlipFaceUpCard]
+ and $1c
+ cp $c
+ jr z, .WinEighteen
+ jp .Lose
+
+.Five:
+ ld a, [wCardFlipFaceUpCard]
+ and $1c
+ cp $10
+ jr z, .WinEighteen
+ jp .Lose
+
+.Six:
+ ld a, [wCardFlipFaceUpCard]
+ and $1c
+ cp $14
+ jr z, .WinEighteen
+ jp .Lose
+
+.WinEighteen:
+ ld c, $12
+ ld de, SFX_2ND_PLACE
+ jp .Payout
+
+.PikaOne:
+ ld e, $0
+ jr .CheckWin72
+
+.JigglyOne:
+ ld e, $1
+ jr .CheckWin72
+
+.PoliOne:
+ ld e, $2
+ jr .CheckWin72
+
+.OddOne:
+ ld e, $3
+ jr .CheckWin72
+
+.PikaTwo:
+ ld e, $4
+ jr .CheckWin72
+
+.JigglyTwo:
+ ld e, $5
+ jr .CheckWin72
+
+.PoliTwo:
+ ld e, $6
+ jr .CheckWin72
+
+.OddTwo:
+ ld e, $7
+ jr .CheckWin72
+
+.PikaThree:
+ ld e, $8
+ jr .CheckWin72
+
+.JigglyThree:
+ ld e, $9
+ jr .CheckWin72
+
+.PoliThree:
+ ld e, $a
+ jr .CheckWin72
+
+.OddThree:
+ ld e, $b
+ jr .CheckWin72
+
+.PikaFour:
+ ld e, $c
+ jr .CheckWin72
+
+.JigglyFour:
+ ld e, $d
+ jr .CheckWin72
+
+.PoliFour:
+ ld e, $e
+ jr .CheckWin72
+
+.OddFour:
+ ld e, $f
+ jr .CheckWin72
+
+.PikaFive:
+ ld e, $10
+ jr .CheckWin72
+
+.JigglyFive:
+ ld e, $11
+ jr .CheckWin72
+
+.PoliFive:
+ ld e, $12
+ jr .CheckWin72
+
+.OddFive:
+ ld e, $13
+ jr .CheckWin72
+
+.PikaSix:
+ ld e, $14
+ jr .CheckWin72
+
+.JigglySix:
+ ld e, $15
+ jr .CheckWin72
+
+.PoliSix:
+ ld e, $16
+ jr .CheckWin72
+
+.OddSix:
+ ld e, $17
+
+.CheckWin72:
+ ld a, [wCardFlipFaceUpCard]
+ cp e
+ jr nz, .Lose
+ ld c, 72
+ ld de, SFX_2ND_PLACE
+ jr .Payout
+
+.Lose:
+ ld de, SFX_WRONG
+ call PlaySFX
+ ld hl, .CardFlipDarnText
+ call CardFlip_UpdateCoinBalanceDisplay
+ call WaitSFX
+ ret
+
+.Payout:
+ push bc
+ push de
+ ld hl, .CardFlipYeahText
+ call CardFlip_UpdateCoinBalanceDisplay
+ pop de
+ call PlaySFX
+ call WaitSFX
+ pop bc
+.loop
+ push bc
+ call .IsCoinCaseFull
+ jr c, .full
+ call .AddCoinPlaySFX
+
+.full
+ call CardFlip_PrintCoinBalance
+ ld c, 2
+ call DelayFrames
+ pop bc
+ dec c
+ jr nz, .loop
+ ret
+
+.CardFlipYeahText:
+ text_far _CardFlipYeahText
+ text_end
+
+.CardFlipDarnText:
+ text_far _CardFlipDarnText
+ text_end
+
+.AddCoinPlaySFX:
+ ld a, [wCoins]
+ ld h, a
+ ld a, [wCoins + 1]
+ ld l, a
+ inc hl
+ ld a, h
+ ld [wCoins], a
+ ld a, l
+ ld [wCoins + 1], a
+ ld de, SFX_PAY_DAY
+ call PlaySFX
+ ret
+
+.IsCoinCaseFull:
+ ld a, [wCoins]
+ cp HIGH(MAX_COINS)
+ jr c, .less
+ jr z, .check_low
+ jr .more
+
+.check_low
+ ld a, [wCoins + 1]
+ cp LOW(MAX_COINS)
+ jr c, .less
+
+.more
+ scf
+ ret
+
+.less
+ and a
+ ret
+
+PlaceOAMCardBorder:
+ call GetCoordsOfChosenCard
+ ld hl, .SpriteData
+ call CardFlip_CopyOAM
+ ret
+
+.SpriteData:
+ db 18
+ dbsprite 0, 0, 0, 0, $04, 0
+ dbsprite 1, 0, 0, 0, $06, 0
+ dbsprite 2, 0, 0, 0, $06, 0
+ dbsprite 3, 0, 0, 0, $06, 0
+ dbsprite 4, 0, 0, 0, $04, 0 | X_FLIP
+
+ dbsprite 0, 1, 0, 0, $05, 0
+ dbsprite 4, 1, 0, 0, $05, 0 | X_FLIP
+
+ dbsprite 0, 2, 0, 0, $05, 0
+ dbsprite 4, 2, 0, 0, $05, 0 | X_FLIP
+
+ dbsprite 0, 3, 0, 0, $05, 0
+ dbsprite 4, 3, 0, 0, $05, 0 | X_FLIP
+
+ dbsprite 0, 4, 0, 0, $05, $00
+ dbsprite 4, 4, 0, 0, $05, 0 | X_FLIP
+
+ dbsprite 0, 5, 0, 0, $04, 0 | Y_FLIP
+ dbsprite 1, 5, 0, 0, $06, 0 | Y_FLIP
+ dbsprite 2, 5, 0, 0, $06, 0 | Y_FLIP
+ dbsprite 3, 5, 0, 0, $06, 0 | Y_FLIP
+ dbsprite 4, 5, 0, 0, $04, 0 | X_FLIP | Y_FLIP
+
+ChooseCard_HandleJoypad:
+ ld hl, hJoyLast
+ ld a, [hl]
+ and D_LEFT
+ jp nz, .d_left
+ ld a, [hl]
+ and D_RIGHT
+ jp nz, .d_right
+ ld a, [hl]
+ and D_UP
+ jp nz, .d_up
+ ld a, [hl]
+ and D_DOWN
+ jp nz, .d_down
+ ret
+
+.d_left
+ ld hl, wCardFlipCursorX
+ ld a, [wCardFlipCursorY]
+ and a
+ jr z, .mon_pair_left
+ cp $1
+ jr z, .mon_group_left
+ ld a, [hl]
+ and a
+ ret z
+ dec [hl]
+ jp .play_sound
+
+.mon_group_left
+ ld a, [hl]
+ cp $3
+ jr c, .left_to_number_gp
+ dec [hl]
+ jp .play_sound
+
+.mon_pair_left
+ ld a, [hl]
+ and $e
+ ld [hl], a
+ cp $3
+ jr c, .left_to_number_gp
+ dec [hl]
+ dec [hl]
+ jp .play_sound
+
+.left_to_number_gp
+ ld a, $2
+ ld [wCardFlipCursorY], a
+ ld a, $1
+ ld [wCardFlipCursorX], a
+ jp .play_sound
+
+.d_right
+ ld hl, wCardFlipCursorX
+ ld a, [wCardFlipCursorY]
+ and a
+ jr z, .mon_pair_right
+ ld a, [hl]
+ cp $5
+ ret nc
+ inc [hl]
+ jr .play_sound
+
+.mon_pair_right
+ ld a, [hl]
+ and $e
+ ld [hl], a
+ cp $4
+ ret nc
+ inc [hl]
+ inc [hl]
+ jr .play_sound
+
+.d_up
+ ld hl, wCardFlipCursorY
+ ld a, [wCardFlipCursorX]
+ and a
+ jr z, .num_pair_up
+ cp $1
+ jr z, .num_gp_up
+ ld a, [hl]
+ and a
+ ret z
+ dec [hl]
+ jr .play_sound
+
+.num_gp_up
+ ld a, [hl]
+ cp $3
+ jr c, .up_to_mon_group
+ dec [hl]
+ jr .play_sound
+
+.num_pair_up
+ ld a, [hl]
+ and $e
+ ld [hl], a
+ cp $3
+ jr c, .up_to_mon_group
+ dec [hl]
+ dec [hl]
+ jr .play_sound
+
+.up_to_mon_group
+ ld a, $1
+ ld [wCardFlipCursorY], a
+ ld a, $2
+ ld [wCardFlipCursorX], a
+ jr .play_sound
+
+.d_down
+ ld hl, wCardFlipCursorY
+ ld a, [wCardFlipCursorX]
+ and a
+ jr z, .num_pair_down
+ ld hl, wCardFlipCursorY
+ ld a, [hl]
+ cp $7
+ ret nc
+ inc [hl]
+ jr .play_sound
+
+.num_pair_down
+ ld a, [hl]
+ and $e
+ ld [hl], a
+ cp $6
+ ret nc
+ inc [hl]
+ inc [hl]
+
+.play_sound
+ ld de, SFX_POKEBALLS_PLACED_ON_TABLE
+ call PlaySFX
+ ret
+
+CardFlip_UpdateCursorOAM:
+ call ClearSprites
+ ldh a, [hCGB]
+ and a
+ jr nz, .skip
+ ldh a, [hVBlankCounter]
+ and $4
+ ret nz
+
+.skip
+ call CollapseCursorPosition
+ add hl, hl
+ add hl, hl
+ ld de, .OAMData
+ add hl, de
+ ld a, [hli]
+ ld c, a
+ ld a, [hli]
+ ld b, a
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ call CardFlip_CopyOAM
+ ret
+
+.OAMData:
+cardflip_cursor: MACRO
+if _NARG >= 5
+ dbpixel \1, \2, \3, \4
+ dw \5
+else
+ dbpixel \1, \2
+ dw \3
+endc
+ENDM
+
+ cardflip_cursor 11, 2, .Impossible
+ cardflip_cursor 12, 2, .Impossible
+ cardflip_cursor 13, 2, .PokeGroupPair
+ cardflip_cursor 13, 2, .PokeGroupPair
+ cardflip_cursor 17, 2, .PokeGroupPair
+ cardflip_cursor 17, 2, .PokeGroupPair
+
+ cardflip_cursor 11, 3, .Impossible
+ cardflip_cursor 12, 3, .Impossible
+ cardflip_cursor 13, 3, .PokeGroup
+ cardflip_cursor 15, 3, .PokeGroup
+ cardflip_cursor 17, 3, .PokeGroup
+ cardflip_cursor 19, 3, .PokeGroup
+
+ cardflip_cursor 11, 5, .NumGroupPair
+ cardflip_cursor 12, 5, .NumGroup
+ cardflip_cursor 13, 5, .SingleTile
+ cardflip_cursor 15, 5, .SingleTile
+ cardflip_cursor 17, 5, .SingleTile
+ cardflip_cursor 19, 5, .SingleTile
+
+ cardflip_cursor 11, 5, .NumGroupPair
+ cardflip_cursor 12, 6, 0, 4, .NumGroup
+ cardflip_cursor 13, 6, 0, 4, .SingleTile
+ cardflip_cursor 15, 6, 0, 4, .SingleTile
+ cardflip_cursor 17, 6, 0, 4, .SingleTile
+ cardflip_cursor 19, 6, 0, 4, .SingleTile
+
+ cardflip_cursor 11, 8, .NumGroupPair
+ cardflip_cursor 12, 8, .NumGroup
+ cardflip_cursor 13, 8, .SingleTile
+ cardflip_cursor 15, 8, .SingleTile
+ cardflip_cursor 17, 8, .SingleTile
+ cardflip_cursor 19, 8, .SingleTile
+
+ cardflip_cursor 11, 8, .NumGroupPair
+ cardflip_cursor 12, 9, 0, 4, .NumGroup
+ cardflip_cursor 13, 9, 0, 4, .SingleTile
+ cardflip_cursor 15, 9, 0, 4, .SingleTile
+ cardflip_cursor 17, 9, 0, 4, .SingleTile
+ cardflip_cursor 19, 9, 0, 4, .SingleTile
+
+ cardflip_cursor 11, 11, .NumGroupPair
+ cardflip_cursor 12, 11, .NumGroup
+ cardflip_cursor 13, 11, .SingleTile
+ cardflip_cursor 15, 11, .SingleTile
+ cardflip_cursor 17, 11, .SingleTile
+ cardflip_cursor 19, 11, .SingleTile
+
+ cardflip_cursor 11, 11, .NumGroupPair
+ cardflip_cursor 12, 12, 0, 4, .NumGroup
+ cardflip_cursor 13, 12, 0, 4, .SingleTile
+ cardflip_cursor 15, 12, 0, 4, .SingleTile
+ cardflip_cursor 17, 12, 0, 4, .SingleTile
+ cardflip_cursor 19, 12, 0, 4, .SingleTile
+
+.SingleTile:
+ db 6
+ dbsprite -1, 0, 7, 0, $00, 0 | PRIORITY
+ dbsprite 0, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 1, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 0, 7, 5, $00, 0 | Y_FLIP | PRIORITY
+ dbsprite 0, 0, 0, 5, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 1, 0, 0, 5, $03, 0 | PRIORITY
+
+.PokeGroup:
+ db 26
+ dbsprite -1, 0, 7, 0, $00, 0 | PRIORITY
+ dbsprite 0, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 1, 0, 0, 0, $00, 0 | X_FLIP | PRIORITY
+ dbsprite -1, 1, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 1, 0, 0, $01, 0 | X_FLIP | PRIORITY
+ dbsprite -1, 2, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 2, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 3, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 3, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 4, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 4, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 5, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 5, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 6, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 6, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 7, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 7, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 8, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 8, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 9, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 9, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 10, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 10, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 10, 7, 1, $00, 0 | Y_FLIP | PRIORITY
+ dbsprite 0, 10, 0, 1, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 1, 10, 0, 1, $03, 0 | PRIORITY
+
+.NumGroup:
+ db 20
+ dbsprite -1, 0, 7, 0, $00, 0 | PRIORITY
+ dbsprite 0, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 1, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 2, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 4, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite 5, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 6, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite 7, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 8, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 0, 7, 5, $00, 0 | Y_FLIP | PRIORITY
+ dbsprite 0, 0, 0, 5, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 1, 0, 0, 5, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 2, 0, 0, 5, $03, 0 | PRIORITY
+ dbsprite 3, 0, 0, 5, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 4, 0, 0, 5, $03, 0 | PRIORITY
+ dbsprite 5, 0, 0, 5, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 6, 0, 0, 5, $03, 0 | PRIORITY
+ dbsprite 7, 0, 0, 5, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 8, 0, 0, 5, $03, 0 | PRIORITY
+
+.NumGroupPair:
+ db 30
+ dbsprite 0, 0, 0, 0, $00, 0 | PRIORITY
+ dbsprite 1, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 2, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 3, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite 4, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 5, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite 6, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 7, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite 8, 0, 0, 0, $02, 0 | PRIORITY
+ dbsprite 9, 0, 0, 0, $03, 0 | PRIORITY
+ dbsprite 0, 1, 0, 0, $01, 0 | PRIORITY
+ dbsprite 3, 1, 0, 0, $03, 0 | PRIORITY
+ dbsprite 5, 1, 0, 0, $03, 0 | PRIORITY
+ dbsprite 7, 1, 0, 0, $03, 0 | PRIORITY
+ dbsprite 9, 1, 0, 0, $03, 0 | PRIORITY
+ dbsprite 0, 2, 0, 0, $01, 0 | PRIORITY
+ dbsprite 3, 2, 0, 0, $03, 0 | PRIORITY
+ dbsprite 5, 2, 0, 0, $03, 0 | PRIORITY
+ dbsprite 7, 2, 0, 0, $03, 0 | PRIORITY
+ dbsprite 9, 2, 0, 0, $03, 0 | PRIORITY
+ dbsprite 0, 2, 0, 1, $00, 0 | Y_FLIP | PRIORITY
+ dbsprite 1, 2, 0, 1, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 2, 2, 0, 1, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 3, 2, 0, 1, $03, 0 | PRIORITY
+ dbsprite 4, 2, 0, 1, $03, 0 | PRIORITY
+ dbsprite 5, 2, 0, 1, $03, 0 | PRIORITY
+ dbsprite 6, 2, 0, 1, $03, 0 | PRIORITY
+ dbsprite 7, 2, 0, 1, $03, 0 | PRIORITY
+ dbsprite 8, 2, 0, 1, $03, 0 | PRIORITY
+ dbsprite 9, 2, 0, 1, $03, 0 | PRIORITY
+
+.PokeGroupPair:
+ db 38
+ dbsprite -1, 0, 7, 0, $00, 0 | PRIORITY
+ dbsprite 3, 0, 0, 0, $00, 0 | X_FLIP | PRIORITY
+ dbsprite -1, 1, 7, 0, $01, 0 | PRIORITY
+ dbsprite 3, 1, 0, 0, $01, 0 | X_FLIP | PRIORITY
+ dbsprite -1, 2, 7, 0, $01, 0 | PRIORITY
+ dbsprite 3, 2, 0, 0, $01, 0 | X_FLIP | PRIORITY
+ dbsprite -1, 3, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 3, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 3, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 4, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 4, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 4, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 5, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 5, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 5, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 6, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 6, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 6, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 7, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 7, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 7, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 8, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 8, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 8, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 9, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 9, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 9, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 10, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 10, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 10, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 11, 7, 0, $01, 0 | PRIORITY
+ dbsprite 1, 11, 0, 0, $03, 0 | PRIORITY
+ dbsprite 3, 11, 0, 0, $03, 0 | PRIORITY
+ dbsprite -1, 11, 7, 1, $00, 0 | Y_FLIP | PRIORITY
+ dbsprite 0, 11, 0, 1, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 1, 11, 0, 1, $03, 0 | Y_FLIP | PRIORITY
+ dbsprite 2, 11, 0, 1, $02, 0 | Y_FLIP | PRIORITY
+ dbsprite 3, 11, 0, 1, $03, 0 | X_FLIP | Y_FLIP | PRIORITY
+
+.Impossible:
+ db 4
+ dbsprite 0, 0, 0, 0, $00, 0 | PRIORITY
+ dbsprite 1, 0, 0, 0, $00, 0 | X_FLIP | PRIORITY
+ dbsprite 0, 1, 0, 0, $00, 0 | Y_FLIP | PRIORITY
+ dbsprite 1, 1, 0, 0, $00, 0 | X_FLIP | Y_FLIP | PRIORITY
+
+CardFlip_InitAttrPals:
+ ldh a, [hCGB]
+ and a
+ ret z
+
+ hlcoord 0, 0, wAttrmap
+ ld bc, SCREEN_HEIGHT * SCREEN_WIDTH
+ xor a
+ call ByteFill
+
+ hlcoord 12, 1, wAttrmap
+ lb bc, 2, 2
+ ld a, $1
+ call CardFlip_FillBox
+
+ hlcoord 14, 1, wAttrmap
+ lb bc, 2, 2
+ ld a, $2
+ call CardFlip_FillBox
+
+ hlcoord 16, 1, wAttrmap
+ lb bc, 2, 2
+ ld a, $3
+ call CardFlip_FillBox
+
+ hlcoord 18, 1, wAttrmap
+ lb bc, 2, 2
+ ld a, $4
+ call CardFlip_FillBox
+
+ hlcoord 9, 0, wAttrmap
+ lb bc, 12, 1
+ ld a, $1
+ call CardFlip_FillBox
+
+ ld hl, .palettes
+ ld de, wBGPals1
+ ld bc, 9 palettes
+ call CopyBytes
+ ret
+
+.palettes
+INCLUDE "gfx/card_flip/card_flip.pal"
+
+CardFlipLZ03:
+INCBIN "gfx/card_flip/card_flip_3.2bpp.lz"
+
+CardFlipOffButtonGFX:
+INCBIN "gfx/card_flip/off.2bpp"
+
+CardFlipOnButtonGFX:
+INCBIN "gfx/card_flip/on.2bpp"
+
+CardFlipLZ01:
+INCBIN "gfx/card_flip/card_flip_1.2bpp.lz"
+
+CardFlipLZ02:
+INCBIN "gfx/card_flip/card_flip_2.2bpp.lz"
+
+CardFlipTilemap:
+INCBIN "gfx/card_flip/card_flip.tilemap"
diff --git a/engine/games/dummy_game.asm b/engine/games/dummy_game.asm
new file mode 100755
index 00000000..e12c791b
--- /dev/null
+++ b/engine/games/dummy_game.asm
@@ -0,0 +1,590 @@
+_DummyGame:
+ call .LoadGFXAndPals
+ call DelayFrame
+.loop
+ call .JumptableLoop
+ jr nc, .loop
+ ret
+
+.LoadGFXAndPals:
+ call DisableLCD
+ ld b, SCGB_DIPLOMA
+ call GetSGBLayout
+ callfar ClearSpriteAnims
+ ld hl, DummyGameLZ
+ ld de, vTiles2 tile $00
+ call Decompress
+ ld hl, Unknown_e0908
+ ld de, vTiles0 tile $00
+ ld bc, 4 tiles
+ ld a, BANK(Unknown_e0908)
+ call FarCopyBytes
+ ld a, 8
+ ld hl, wc508
+ ld [hli], a
+ ld [hl], 0
+ hlcoord 0, 0
+ ld bc, SCREEN_HEIGHT * SCREEN_WIDTH
+ xor a
+ call ByteFill
+ xor a
+ ldh [hSCY], a
+ ldh [hSCX], a
+ ldh [rWY], a
+ ld [wJumptableIndex], a
+ ld a, $1
+ ldh [hBGMapMode], a
+ ld a, LCDC_DEFAULT
+ ldh [rLCDC], a
+ ld a, $e4
+ call DmgToCgbBGPals
+ ld a, $e0
+ call DmgToCgbObjPal0
+ ret
+
+.JumptableLoop:
+ ld a, [wJumptableIndex]
+ bit 7, a
+ jr nz, .quit
+ call .ExecuteJumptable
+ callfar PlaySpriteAnimations
+ call DelayFrame
+ and a
+ ret
+
+.quit
+ scf
+ ret
+
+.ExecuteJumptable:
+ jumptable .Jumptable, wJumptableIndex
+
+.Jumptable:
+ dw .RestartGame
+ dw .ResetBoard
+ dw .InitBoardTilemapAndCursorObject
+ dw .CheckTriesRemaining
+ dw .PickCard1
+ dw .PickCard2
+ dw .DelayPickAgain
+ dw .RevealAll
+ dw .AskPlayAgain
+
+.RestartGame:
+ call DummyGame_InitStrings
+ ld hl, wJumptableIndex
+ inc [hl]
+ ret
+
+.ResetBoard:
+ call ret_e0908
+ jr nc, .proceed
+ ld hl, wJumptableIndex
+ set 7, [hl]
+ ret
+
+.proceed
+ call DummyGame_InitBoard
+ ld hl, wJumptableIndex
+ inc [hl]
+ xor a
+ ld [wDummyGameCounter], a
+ ld hl, wDummyGameLastMatches
+rept 4
+ ld [hli], a
+endr
+ ld [hl], a
+ ld [wDummyGameNumCardsMatched], a
+.InitBoardTilemapAndCursorObject:
+ ld hl, wDummyGameCounter
+ ld a, [hl]
+ cp 45
+ jr nc, .spawn_object
+ inc [hl]
+ call DummyGame_Card2Coord
+ xor a
+ ld [wDummyGameLastCardPicked], a
+ call DummyGame_PlaceCard
+ ret
+
+.spawn_object
+ depixel 6, 3, 4, 4
+ ld a, SPRITE_ANIM_INDEX_DUMMY_GAME
+ call InitSpriteAnimStruct
+ ld a, 5
+ ld [wDummyGameNumberTriesRemaining], a
+ ld hl, wJumptableIndex
+ inc [hl]
+ ret
+
+.CheckTriesRemaining:
+ ld a, [wDummyGameNumberTriesRemaining]
+ hlcoord 17, 0
+ add "0"
+ ld [hl], a
+ ld hl, wDummyGameNumberTriesRemaining
+ ld a, [hl]
+ and a
+ jr nz, .next_try
+ ld a, $7
+ ld [wJumptableIndex], a
+ ret
+
+.next_try
+ dec [hl]
+ xor a
+ ld [wce64], a
+ ld hl, wJumptableIndex
+ inc [hl]
+.PickCard1:
+ ld a, [wce64]
+ and a
+ ret z
+ dec a
+ ld e, a
+ ld d, 0
+ ld hl, wDummyGameCards
+ add hl, de
+ ld a, [hl]
+ cp -1
+ ret z
+ ld [wDummyGameLastCardPicked], a
+ ld [wDummyGameCard1], a
+ ld a, e
+ ld [wDummyGameCard1Location], a
+ call DummyGame_Card2Coord
+ call DummyGame_PlaceCard
+ xor a
+ ld [wce64], a
+ ld hl, wJumptableIndex
+ inc [hl]
+ ret
+
+.PickCard2:
+ ld a, [wce64]
+ and a
+ ret z
+ dec a
+ ld hl, wDummyGameCard1Location
+ cp [hl]
+ ret z
+ ld e, a
+ ld d, 0
+ ld hl, wDummyGameCards
+ add hl, de
+ ld a, [hl]
+ cp -1
+ ret z
+ ld [wDummyGameLastCardPicked], a
+ ld [wDummyGameCard2], a
+ ld a, e
+ ld [wDummyGameCard2Location], a
+ call DummyGame_Card2Coord
+ call DummyGame_PlaceCard
+ ld a, 64
+ ld [wDummyGameCounter], a
+ ld hl, wJumptableIndex
+ inc [hl]
+.DelayPickAgain:
+ ld hl, wDummyGameCounter
+ ld a, [hl]
+ and a
+ jr z, .PickAgain
+ dec [hl]
+ ret
+
+.PickAgain:
+ call DummyGame_CheckMatch
+ ld a, $3
+ ld [wJumptableIndex], a
+ ret
+
+.RevealAll:
+ ldh a, [hJoypadPressed]
+ and A_BUTTON
+ ret z
+ xor a
+ ld [wDummyGameCounter], a
+.RevelationLoop:
+ ld hl, wDummyGameCounter
+ ld a, [hl]
+ cp 45
+ jr nc, .finish_round
+ inc [hl]
+ push af
+ call DummyGame_Card2Coord
+ pop af
+ push hl
+ ld e, a
+ ld d, $0
+ ld hl, wDummyGameCards
+ add hl, de
+ ld a, [hl]
+ pop hl
+ cp -1
+ jr z, .RevelationLoop
+ ld [wDummyGameLastCardPicked], a
+ call DummyGame_PlaceCard
+ jr .RevelationLoop
+
+.finish_round
+ call WaitPressAorB_BlinkCursor
+ ld hl, wJumptableIndex
+ inc [hl]
+.AskPlayAgain:
+ call ret_e0908
+ jr nc, .restart
+ ld hl, wJumptableIndex
+ set 7, [hl]
+ ret
+
+.restart
+ xor a
+ ld [wJumptableIndex], a
+ ret
+
+DummyGame_CheckMatch:
+ ld hl, wDummyGameCard1
+ ld a, [hli]
+ cp [hl]
+ jr nz, .no_match
+
+ ld a, [wDummyGameCard1Location]
+ call DummyGame_Card2Coord
+ call DummyGame_DeleteCard
+
+ ld a, [wDummyGameCard2Location]
+ call DummyGame_Card2Coord
+ call DummyGame_DeleteCard
+
+ ld a, [wDummyGameCard1Location]
+ ld e, a
+ ld d, $0
+ ld hl, wDummyGameCards
+ add hl, de
+ ld [hl], -1
+
+ ld a, [wDummyGameCard2Location]
+ ld e, a
+ ld d, 0
+ ld hl, wDummyGameCards
+ add hl, de
+ ld [hl], -1
+
+ ld hl, wDummyGameLastMatches
+.find_empty_slot
+ ld a, [hli]
+ and a
+ jr nz, .find_empty_slot
+ dec hl
+ ld a, [wDummyGameCard1]
+ ld [hl], a
+ ld [wDummyGameLastCardPicked], a
+ ld hl, wDummyGameNumCardsMatched
+ ld e, [hl]
+ inc [hl]
+ inc [hl]
+ ld d, 0
+ hlcoord 5, 0
+ add hl, de
+ call DummyGame_PlaceCard
+ ld hl, .VictoryText
+ call PrintText
+ ret
+
+.no_match
+ xor a
+ ld [wDummyGameLastCardPicked], a
+
+ ld a, [wDummyGameCard1Location]
+ call DummyGame_Card2Coord
+ call DummyGame_PlaceCard
+
+ ld a, [wDummyGameCard2Location]
+ call DummyGame_Card2Coord
+ call DummyGame_PlaceCard
+
+ ld hl, DummyGameDarnText
+ call PrintText
+ ret
+
+.VictoryText:
+ text_asm
+ push bc
+ hlcoord 2, 13
+ call DummyGame_PlaceCard
+ ld hl, DummyGameYeahText
+ pop bc
+ inc bc
+ inc bc
+ inc bc
+ ret
+
+DummyGameYeahText:
+ text_far _DummyGameYeahText
+ text_end
+
+DummyGameDarnText:
+ text_far _DummyGameDarnText
+ text_end
+
+DummyGame_InitBoard:
+ ld hl, wDummyGameCards
+ ld bc, wDummyGameCardsEnd - wDummyGameCards
+ xor a
+ call ByteFill
+ call DummyGame_GetDistributionOfTiles
+
+ ld c, 2
+ ld b, [hl]
+ call DummyGame_SampleTilePlacement
+
+ ld c, 8
+ ld b, [hl]
+ call DummyGame_SampleTilePlacement
+
+ ld c, 4
+ ld b, [hl]
+ call DummyGame_SampleTilePlacement
+
+ ld c, 7
+ ld b, [hl]
+ call DummyGame_SampleTilePlacement
+
+ ld c, 3
+ ld b, [hl]
+ call DummyGame_SampleTilePlacement
+
+ ld c, 6
+ ld b, [hl]
+ call DummyGame_SampleTilePlacement
+
+ ld c, 1
+ ld b, [hl]
+ call DummyGame_SampleTilePlacement
+
+ ld c, 5
+ ld hl, wDummyGameCards
+ ld b, wDummyGameCardsEnd - wDummyGameCards
+.loop
+ ld a, [hl]
+ and a
+ jr nz, .no_load
+ ld [hl], c
+.no_load
+ inc hl
+ dec b
+ jr nz, .loop
+ ret
+
+DummyGame_SampleTilePlacement:
+ push hl
+ ld de, wDummyGameCards
+.loop
+ call Random
+ and %00111111
+ cp 45
+ jr nc, .loop
+ ld l, a
+ ld h, 0
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .loop
+ ld [hl], c
+ dec b
+ jr nz, .loop
+ pop hl
+ inc hl
+ ret
+
+DummyGame_GetDistributionOfTiles:
+ ld a, [wMenuCursorY]
+ dec a
+ ld l, a
+ ld h, 0
+ add hl, hl
+ add hl, hl
+ add hl, hl
+ ld de, .distributions
+ add hl, de
+ ret
+
+.distributions
+ db $02, $03, $06, $06, $06, $08, $08, $06
+ db $02, $02, $04, $06, $06, $08, $08, $09
+ db $02, $02, $02, $04, $07, $08, $08, $0c
+
+DummyGame_PlaceCard:
+ ld a, [wDummyGameLastCardPicked]
+ sla a
+ sla a
+ add 4
+ ld [hli], a
+ inc a
+ ld [hld], a
+ inc a
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hli], a
+ inc a
+ ld [hl], a
+ ld c, 3
+ call DelayFrames
+ ret
+
+DummyGame_DeleteCard:
+ ld a, $1
+ ld [hli], a
+ ld [hld], a
+ ld bc, SCREEN_WIDTH
+ add hl, bc
+ ld [hli], a
+ ld [hl], a
+ ld c, 3
+ call DelayFrames
+ ret
+
+DummyGame_InitStrings:
+ hlcoord 0, 0
+ ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
+ ld a, $1
+ call ByteFill
+ hlcoord 0, 0
+ ld de, .japstr1
+ call PlaceString
+ hlcoord 15, 0
+ ld de, .japstr2
+ call PlaceString
+ ld hl, .dummy_text
+ call PrintText
+ ret
+
+.dummy_text
+ db "@"
+.japstr1
+ db "とったもの@"
+.japstr2
+ db "あと かい@"
+
+DummyGame_Card2Coord:
+ ld d, 0
+.find_row
+ sub 9
+ jr c, .found_row
+ inc d
+ jr .find_row
+
+.found_row
+ add 9
+ ld e, a
+ hlcoord 1, 2
+ ld bc, 2 * SCREEN_WIDTH
+.loop2
+ ld a, d
+ and a
+ jr z, .done
+ add hl, bc
+ dec d
+ jr .loop2
+
+.done
+ sla e
+ add hl, de
+ ret
+
+DummyGame_InterpretJoypad_AnimateCursor:
+ ld a, [wJumptableIndex]
+ cp $7
+ jr nc, .quit
+ call JoyTextDelay
+ ld hl, hJoypadPressed
+ ld a, [hl]
+ and A_BUTTON
+ jr nz, .pressed_a
+ ld a, [hl]
+ and D_LEFT
+ jr nz, .pressed_left
+ ld a, [hl]
+ and D_RIGHT
+ jr nz, .pressed_right
+ ld a, [hl]
+ and D_UP
+ jr nz, .pressed_up
+ ld a, [hl]
+ and D_DOWN
+ jr nz, .pressed_down
+ ret
+
+.quit
+ ld hl, SPRITEANIMSTRUCT_INDEX
+ add hl, bc
+ ld [hl], $0
+ ret
+
+.pressed_a
+ ld hl, SPRITEANIMSTRUCT_0C
+ add hl, bc
+ ld a, [hl]
+ inc a
+ ld [wce64], a
+ ret
+
+.pressed_left
+ ld hl, SPRITEANIMSTRUCT_XOFFSET
+ add hl, bc
+ ld a, [hl]
+ and a
+ ret z
+ sub 1 tiles
+ ld [hl], a
+ ld hl, SPRITEANIMSTRUCT_0C
+ add hl, bc
+ dec [hl]
+ ret
+
+.pressed_right
+ ld hl, SPRITEANIMSTRUCT_XOFFSET
+ add hl, bc
+ ld a, [hl]
+ cp (9 - 1) tiles
+ ret z
+ add 1 tiles
+ ld [hl], a
+ ld hl, SPRITEANIMSTRUCT_0C
+ add hl, bc
+ inc [hl]
+ ret
+
+.pressed_up
+ ld hl, SPRITEANIMSTRUCT_YOFFSET
+ add hl, bc
+ ld a, [hl]
+ and a
+ ret z
+ sub 1 tiles
+ ld [hl], a
+ ld hl, SPRITEANIMSTRUCT_0C
+ add hl, bc
+ ld a, [hl]
+ sub 9
+ ld [hl], a
+ ret
+
+.pressed_down
+ ld hl, SPRITEANIMSTRUCT_YOFFSET
+ add hl, bc
+ ld a, [hl]
+ cp (5 - 1) tiles
+ ret z
+ add 1 tiles
+ ld [hl], a
+ ld hl, SPRITEANIMSTRUCT_0C
+ add hl, bc
+ ld a, [hl]
+ add 9
+ ld [hl], a
+ ret
+
+DummyGameLZ:
+INCBIN "gfx/dummy_game/dummy_game.2bpp.lz"
diff --git a/engine/games/unown_puzzle.asm b/engine/games/unown_puzzle.asm
new file mode 100755
index 00000000..6151107d
--- /dev/null
+++ b/engine/games/unown_puzzle.asm
@@ -0,0 +1,846 @@
+PUZZLE_BORDER EQU $ee
+PUZZLE_VOID EQU $ef
+
+puzcoord EQUS "* 6 +"
+
+_UnownPuzzle:
+ ldh a, [hInMenu]
+ push af
+ ld a, $1
+ ldh [hInMenu], a
+ call ClearBGPalettes
+ call ClearTilemap
+ call ClearSprites
+ xor a
+ ldh [hBGMapMode], a
+ call DisableLCD
+ ld hl, wUnownPuzzle ; includes wPuzzlePieces
+ ld bc, wUnownPuzzleEnd - wUnownPuzzle
+ xor a
+ call ByteFill
+ ld hl, UnownPuzzleCursorGFX
+ ld de, vTiles0 tile $e0
+ ld bc, 4 tiles
+ call CopyBytes
+ ld hl, UnownPuzzleStartCancelLZ
+ ld de, vTiles0 tile $ed
+ call Decompress
+ call LoadUnownPuzzlePiecesGFX
+ hlcoord 0, 0
+ ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
+ ld a, PUZZLE_BORDER
+ call ByteFill
+ hlcoord 4, 3
+ lb bc, 12, 12
+ ld a, PUZZLE_VOID
+ call UnownPuzzle_FillBox
+ call InitUnownPuzzlePiecePositions
+ call UnownPuzzle_UpdateTilemap
+ call PlaceStartCancelBox
+ xor a
+ ldh [hSCY], a
+ ldh [hSCX], a
+ ldh [rWY], a
+ ld [wJumptableIndex], a
+ ld [wHoldingUnownPuzzlePiece], a
+ ld [wUnownPuzzleCursorPosition], a
+ ld [wUnownPuzzleHeldPiece], a
+ ld a, %10010011
+ ldh [rLCDC], a
+ call WaitBGMap
+ ld b, SCGB_UNOWN_PUZZLE
+ call GetSGBLayout
+ ld a, $e4
+ call DmgToCgbBGPals
+ ld a, $24
+ call DmgToCgbObjPal0
+ xor a
+ ld [wSolvedUnownPuzzle], a
+ call DelayFrame
+.loop
+ call JoyTextDelay
+ ld a, [wJumptableIndex]
+ bit 7, a
+ jr nz, .quit
+ call UnownPuzzleJumptable
+ ld a, [wHoldingUnownPuzzlePiece]
+ and a
+ jr nz, .holding_piece
+ ldh a, [hVBlankCounter]
+ and $10
+ jr z, .clear
+.holding_piece
+ call RedrawUnownPuzzlePieces
+ jr .next
+
+.clear
+ call ClearSprites
+.next
+ call DelayFrame
+ jr .loop
+
+.quit
+ pop af
+ ldh [hInMenu], a
+ call ClearBGPalettes
+ call ClearTilemap
+ call ClearSprites
+ ld a, LCDC_DEFAULT
+ ldh [rLCDC], a
+ ret
+
+InitUnownPuzzlePiecePositions:
+ ld c, 1
+ ld b, 16
+.load_loop
+ call Random
+ and $f
+ ld hl, .PuzzlePieceInitialPositions
+ ld e, a
+ ld d, $0
+ add hl, de
+ ld e, [hl]
+ ld hl, wPuzzlePieces
+ add hl, de
+ ld a, [hl]
+ and a
+ jr nz, .load_loop
+ ld [hl], c
+ inc c
+ dec b
+ jr nz, .load_loop
+ ret
+
+.PuzzlePieceInitialPositions:
+initpuzcoord: MACRO
+rept _NARG / 2
+ db \1 puzcoord \2
+ shift 2
+endr
+ENDM
+ initpuzcoord 0,0, 0,1, 0,2, 0,3, 0,4, 0,5
+ initpuzcoord 1,0, 1,5
+ initpuzcoord 2,0, 2,5
+ initpuzcoord 3,0, 3,5
+ initpuzcoord 4,0, 4,5
+ initpuzcoord 5,0, 5,5
+ ; START > CANCEL
+
+PlaceStartCancelBox:
+ call PlaceStartCancelBoxBorder
+ hlcoord 5, 16
+ ld a, $f6
+ ld c, 10
+.loop
+ ld [hli], a
+ inc a
+ dec c
+ jr nz, .loop
+ ret
+
+PlaceStartCancelBoxBorder:
+ hlcoord 4, 15
+ ld a, $f0
+ ld [hli], a
+ ld bc, 10
+ ld a, $f1
+ call ByteFill
+ hlcoord 15, 15
+ ld a, $f2
+ ld [hli], a
+ hlcoord 4, 16
+ ld a, $f3
+ ld [hli], a
+ ld bc, 10
+ ld a, PUZZLE_VOID
+ call ByteFill
+ hlcoord 15, 16
+ ld a, $f3
+ ld [hli], a
+ hlcoord 4, 17
+ ld a, $f4
+ ld [hli], a
+ ld bc, 10
+ ld a, $f1
+ call ByteFill
+ hlcoord 15, 17
+ ld a, $f5
+ ld [hl], a
+ ret
+
+UnownPuzzleJumptable:
+ ld a, [wJumptableIndex]
+ ld e, a
+ ld d, 0
+ ld hl, .Jumptable
+ add hl, de
+ add hl, de
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ jp hl
+
+.Jumptable:
+ dw .Function
+
+.Function:
+ ldh a, [hJoyPressed]
+ and START
+ jp nz, UnownPuzzle_Quit
+ ldh a, [hJoyPressed]
+ and A_BUTTON
+ jp nz, UnownPuzzle_A
+ ld hl, hJoyLast
+ ld a, [hl]
+ and D_UP
+ jr nz, .d_up
+ ld a, [hl]
+ and D_DOWN
+ jr nz, .d_down
+ ld a, [hl]
+ and D_LEFT
+ jr nz, .d_left
+ ld a, [hl]
+ and D_RIGHT
+ jr nz, .d_right
+ ret
+
+.d_up
+ ld hl, wUnownPuzzleCursorPosition
+ ld a, [hl]
+ cp 1 puzcoord 0
+ ret c
+ sub 6
+ ld [hl], a
+ jr .done_joypad
+
+.d_down
+ ld hl, wUnownPuzzleCursorPosition
+ ld a, [hl]
+ cp 4 puzcoord 1
+ ret z
+ cp 4 puzcoord 2
+ ret z
+ cp 4 puzcoord 3
+ ret z
+ cp 4 puzcoord 4
+ ret z
+ cp 5 puzcoord 0
+ ret nc
+ add 6
+ ld [hl], a
+ jr .done_joypad
+
+.d_left
+ ld hl, wUnownPuzzleCursorPosition
+ ld a, [hl]
+ and a
+ ret z
+ cp 1 puzcoord 0
+ ret z
+ cp 2 puzcoord 0
+ ret z
+ cp 3 puzcoord 0
+ ret z
+ cp 4 puzcoord 0
+ ret z
+ cp 5 puzcoord 0
+ ret z
+ cp 5 puzcoord 5
+ jr z, .left_overflow
+ dec [hl]
+ jr .done_joypad
+
+.left_overflow
+ ld [hl], 5 puzcoord 0
+ jr .done_joypad
+
+.d_right
+ ld hl, wUnownPuzzleCursorPosition
+ ld a, [hl]
+ cp 0 puzcoord 5
+ ret z
+ cp 1 puzcoord 5
+ ret z
+ cp 2 puzcoord 5
+ ret z
+ cp 3 puzcoord 5
+ ret z
+ cp 4 puzcoord 5
+ ret z
+ cp 5 puzcoord 5
+ ret z
+ cp 5 puzcoord 0
+ jr z, .right_overflow
+ inc [hl]
+ jr .done_joypad
+
+.right_overflow
+ ld [hl], 5 puzcoord 5
+
+.done_joypad
+ ld a, [wHoldingUnownPuzzlePiece]
+ and a
+ jr nz, .holding_piece
+ ld de, SFX_POUND
+ jr .play_sfx
+
+.holding_piece
+ ld de, SFX_MOVE_PUZZLE_PIECE
+
+.play_sfx
+ call PlaySFX
+ ret
+
+UnownPuzzle_A:
+ ld a, [wHoldingUnownPuzzlePiece]
+ and a
+ jr nz, .TryPlacePiece
+ call UnownPuzzle_CheckCurrentTileOccupancy
+ and a
+ jr z, UnownPuzzle_InvalidAction
+ ld de, SFX_MEGA_KICK
+ call PlaySFX
+ ld [hl], 0
+ ld [wUnownPuzzleHeldPiece], a
+ call RedrawUnownPuzzlePieces
+ call FillUnoccupiedPuzzleSpace
+ call WaitBGMap
+ call WaitSFX
+ ld a, TRUE
+ ld [wHoldingUnownPuzzlePiece], a
+ ret
+
+.TryPlacePiece:
+ call UnownPuzzle_CheckCurrentTileOccupancy
+ and a
+ jr nz, UnownPuzzle_InvalidAction
+ ld de, SFX_PLACE_PUZZLE_PIECE_DOWN
+ call PlaySFX
+ ld a, [wUnownPuzzleHeldPiece]
+ ld [hl], a
+ call PlaceUnownPuzzlePieceGFX
+ call WaitBGMap
+ xor a
+ ld [wUnownPuzzleHeldPiece], a
+ call RedrawUnownPuzzlePieces
+ xor a
+ ld [wHoldingUnownPuzzlePiece], a
+ call WaitSFX
+ call CheckSolvedUnownPuzzle
+ ret nc
+
+; You solved the puzzle!
+ call PlaceStartCancelBoxBorder
+ call ClearSprites
+ ld de, SFX_1ST_PLACE
+ call PlaySFX
+ call WaitSFX
+ call SimpleWaitPressAorB
+ ld a, TRUE
+ ld [wSolvedUnownPuzzle], a
+UnownPuzzle_Quit:
+ ld hl, wJumptableIndex
+ set 7, [hl]
+ ret
+
+UnownPuzzle_InvalidAction:
+ ld de, SFX_WRONG
+ call PlaySFX
+ call WaitSFX
+ ret
+
+UnownPuzzle_FillBox:
+ ld de, SCREEN_WIDTH
+.row
+ push bc
+ push hl
+.col
+ ld [hli], a
+ dec c
+ jr nz, .col
+ pop hl
+ add hl, de
+ pop bc
+ dec b
+ jr nz, .row
+ ret
+
+UnownPuzzle_UpdateTilemap:
+ xor a
+ ld [wUnownPuzzleCursorPosition], a
+ ld c, 6 * 6
+.loop
+ push bc
+ call UnownPuzzle_CheckCurrentTileOccupancy
+ ld [wUnownPuzzleHeldPiece], a
+ and a
+ jr z, .not_holding_piece
+ call PlaceUnownPuzzlePieceGFX
+ jr .next
+
+.not_holding_piece
+ call FillUnoccupiedPuzzleSpace
+
+.next
+ ld hl, wUnownPuzzleCursorPosition
+ inc [hl]
+ pop bc
+ dec c
+ jr nz, .loop
+ ret
+
+PlaceUnownPuzzlePieceGFX:
+ ld a, $2 ; tilemap coords
+ call GetUnownPuzzleCoordData
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ push hl
+ call GetCurrentPuzzlePieceVTileCorner
+ pop hl
+ ld de, SCREEN_WIDTH
+ ld b, 3
+.row
+ ld c, 3
+ push hl
+.col
+ ld [hli], a
+ inc a
+ dec c
+ jr nz, .col
+ add 9
+ pop hl
+ add hl, de
+ dec b
+ jr nz, .row
+ ret
+
+FillUnoccupiedPuzzleSpace:
+ ld a, 2 ; tilemap coords
+ call GetUnownPuzzleCoordData
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ push hl
+ ld a, 4 ; tile
+ call GetUnownPuzzleCoordData
+ ld a, [hl]
+ pop hl
+ ld de, SCREEN_WIDTH
+ ld b, 3
+.row
+ ld c, 3
+ push hl
+.col
+ ld [hli], a
+ dec c
+ jr nz, .col
+ pop hl
+ add hl, de
+ dec b
+ jr nz, .row
+ ret
+
+GetUnownPuzzleCoordData:
+ ld e, a
+ ld d, 0
+ ld hl, UnownPuzzleCoordData
+ add hl, de
+ ld a, [wUnownPuzzleCursorPosition]
+ ld e, a
+rept 6
+ add hl, de
+endr
+ ret
+
+UnownPuzzle_CheckCurrentTileOccupancy:
+ ld hl, wPuzzlePieces
+ ld a, [wUnownPuzzleCursorPosition]
+ ld e, a
+ ld d, $0
+ add hl, de
+ ld a, [hl]
+ ret
+
+GetCurrentPuzzlePieceVTileCorner:
+ ld a, [wUnownPuzzleHeldPiece]
+ ld hl, .Corners
+ add l
+ ld l, a
+ ld a, $0
+ adc h
+ ld h, a
+ ld a, [hl]
+ ret
+
+.Corners:
+; 00, 01, 02
+; 0c, 0d, 0e
+; 18, 19, 1a
+ db $e0 ; no piece selected
+ db $00, $03, $06, $09
+ db $24, $27, $2a, $2d
+ db $48, $4b, $4e, $51
+ db $6c, $6f, $72, $75
+
+CheckSolvedUnownPuzzle:
+ ld hl, .SolvedPuzzleConfiguration
+ ld de, wPuzzlePieces
+ ld c, 6 * 6
+.loop
+ ld a, [de]
+ cp [hl]
+ jr nz, .not_solved
+ inc de
+ inc hl
+ dec c
+ jr nz, .loop
+ scf
+ ret
+
+.not_solved
+ and a
+ ret
+
+.SolvedPuzzleConfiguration:
+ db $00, $00, $00, $00, $00, $00
+ db $00, $01, $02, $03, $04, $00
+ db $00, $05, $06, $07, $08, $00
+ db $00, $09, $0a, $0b, $0c, $00
+ db $00, $0d, $0e, $0f, $10, $00
+ db $00, $00, $00, $00, $00, $00
+
+RedrawUnownPuzzlePieces:
+ call GetCurrentPuzzlePieceVTileCorner
+ ld [wceed], a
+ xor a
+ call GetUnownPuzzleCoordData ; get pixel positions
+ ld a, [hli]
+ ld b, [hl]
+ ld c, a
+ ld a, [wceed]
+ cp $e0
+ jr z, .NoPiece
+ ld hl, .OAM_HoldingPiece
+ jr .load
+
+.NoPiece:
+ ld hl, .OAM_NotHoldingPiece
+
+.load
+ ld de, wVirtualOAMSprite00
+.loop
+ ld a, [hli]
+ cp -1
+ ret z
+ add b
+ ld [de], a ; y
+ inc de
+ ld a, [hli]
+ add c
+ ld [de], a ; x
+ inc de
+ ld a, [wceed]
+ add [hl]
+ ld [de], a ; tile id
+ inc hl
+ inc de
+ ld a, [hli]
+ ld [de], a ; attributes
+ inc de
+ jr .loop
+
+.OAM_HoldingPiece:
+ dbsprite -1, -1, -4, -4, $00, 0
+ dbsprite 0, -1, -4, -4, $01, 0
+ dbsprite 0, -1, 4, -4, $02, 0
+ dbsprite -1, 0, -4, -4, $0c, 0
+ dbsprite 0, 0, -4, -4, $0d, 0
+ dbsprite 0, 0, 4, -4, $0e, 0
+ dbsprite -1, 0, -4, 4, $18, 0
+ dbsprite 0, 0, -4, 4, $19, 0
+ dbsprite 0, 0, 4, 4, $1a, 0
+ db -1
+
+.OAM_NotHoldingPiece:
+ dbsprite -1, -1, -4, -4, $00, 0
+ dbsprite 0, -1, -4, -4, $01, 0
+ dbsprite 0, -1, 4, -4, $00, 0 | X_FLIP
+ dbsprite -1, 0, -4, -4, $02, 0
+ dbsprite 0, 0, -4, -4, $03, 0
+ dbsprite 0, 0, 4, -4, $02, 0 | X_FLIP
+ dbsprite -1, 0, -4, 4, $00, 0 | Y_FLIP
+ dbsprite 0, 0, -4, 4, $01, 0 | Y_FLIP
+ dbsprite 0, 0, 4, 4, $00, 0 | X_FLIP | Y_FLIP
+ db -1
+
+UnownPuzzleCoordData:
+
+puzzle_coords: MACRO
+ dbpixel \1, \2, \3, \4
+ dwcoord \5, \6
+ db \7, \8
+ENDM
+; OAM coords, tilemap coords, vacant tile, filler
+ puzzle_coords 3, 3, 4, 4, 1, 0, PUZZLE_BORDER, 0
+ puzzle_coords 6, 3, 4, 4, 4, 0, PUZZLE_BORDER, 0
+ puzzle_coords 9, 3, 4, 4, 7, 0, PUZZLE_BORDER, 0
+ puzzle_coords 12, 3, 4, 4, 10, 0, PUZZLE_BORDER, 0
+ puzzle_coords 15, 3, 4, 4, 13, 0, PUZZLE_BORDER, 0
+ puzzle_coords 18, 3, 4, 4, 16, 0, PUZZLE_BORDER, 0
+
+ puzzle_coords 3, 6, 4, 4, 1, 3, PUZZLE_BORDER, 0
+ puzzle_coords 6, 6, 4, 4, 4, 3, PUZZLE_VOID, 0
+ puzzle_coords 9, 6, 4, 4, 7, 3, PUZZLE_VOID, 0
+ puzzle_coords 12, 6, 4, 4, 10, 3, PUZZLE_VOID, 0
+ puzzle_coords 15, 6, 4, 4, 13, 3, PUZZLE_VOID, 0
+ puzzle_coords 18, 6, 4, 4, 16, 3, PUZZLE_BORDER, 0
+
+ puzzle_coords 3, 9, 4, 4, 1, 6, PUZZLE_BORDER, 0
+ puzzle_coords 6, 9, 4, 4, 4, 6, PUZZLE_VOID, 0
+ puzzle_coords 9, 9, 4, 4, 7, 6, PUZZLE_VOID, 0
+ puzzle_coords 12, 9, 4, 4, 10, 6, PUZZLE_VOID, 0
+ puzzle_coords 15, 9, 4, 4, 13, 6, PUZZLE_VOID, 0
+ puzzle_coords 18, 9, 4, 4, 16, 6, PUZZLE_BORDER, 0
+
+ puzzle_coords 3, 12, 4, 4, 1, 9, PUZZLE_BORDER, 0
+ puzzle_coords 6, 12, 4, 4, 4, 9, PUZZLE_VOID, 0
+ puzzle_coords 9, 12, 4, 4, 7, 9, PUZZLE_VOID, 0
+ puzzle_coords 12, 12, 4, 4, 10, 9, PUZZLE_VOID, 0
+ puzzle_coords 15, 12, 4, 4, 13, 9, PUZZLE_VOID, 0
+ puzzle_coords 18, 12, 4, 4, 16, 9, PUZZLE_BORDER, 0
+
+ puzzle_coords 3, 15, 4, 4, 1, 12, PUZZLE_BORDER, 0
+ puzzle_coords 6, 15, 4, 4, 4, 12, PUZZLE_VOID, 0
+ puzzle_coords 9, 15, 4, 4, 7, 12, PUZZLE_VOID, 0
+ puzzle_coords 12, 15, 4, 4, 10, 12, PUZZLE_VOID, 0
+ puzzle_coords 15, 15, 4, 4, 13, 12, PUZZLE_VOID, 0
+ puzzle_coords 18, 15, 4, 4, 16, 12, PUZZLE_BORDER, 0
+
+ puzzle_coords 3, 18, 4, 4, 1, 15, PUZZLE_BORDER, 0
+ puzzle_coords 6, 18, 4, 4, 4, 15, PUZZLE_BORDER, 0
+ puzzle_coords 9, 18, 4, 4, 7, 15, PUZZLE_BORDER, 0
+ puzzle_coords 12, 18, 4, 4, 10, 15, PUZZLE_BORDER, 0
+ puzzle_coords 15, 18, 4, 4, 13, 15, PUZZLE_BORDER, 0
+ puzzle_coords 18, 18, 4, 4, 16, 15, PUZZLE_BORDER, 0
+
+ConvertLoadedPuzzlePieces:
+ ld hl, vTiles2
+ ld de, vTiles0
+ ld b, 6
+.loop
+ push bc
+ push hl
+ push hl
+ call .EnlargePuzzlePieceTiles
+ pop hl
+ ld bc, 1 tiles / 2
+ add hl, bc
+ call .EnlargePuzzlePieceTiles
+ pop hl
+ ld bc, 6 tiles
+ add hl, bc
+ pop bc
+ dec b
+ jr nz, .loop
+ call UnownPuzzle_AddPuzzlePieceBorders
+ ret
+
+.EnlargePuzzlePieceTiles:
+; double size
+ ld c, 6
+.loop1
+ push bc
+ push hl
+ push hl
+ ld c, 4
+.loop2
+ push bc
+ ld a, [hli]
+ and $f0
+ swap a
+ call .GetEnlargedTile
+ ld c, a
+ ld a, [hli]
+ and $f0
+ swap a
+ call .GetEnlargedTile
+ ld b, a
+ ld a, c
+ ld [de], a
+ inc de
+ ld a, b
+ ld [de], a
+ inc de
+ ld a, c
+ ld [de], a
+ inc de
+ ld a, b
+ ld [de], a
+ inc de
+ pop bc
+ dec c
+ jr nz, .loop2
+ pop hl
+ ld c, 4
+.loop3
+ push bc
+ ld a, [hli]
+ and $f
+ call .GetEnlargedTile
+ ld c, a
+ ld a, [hli]
+ and $f
+ call .GetEnlargedTile
+ ld b, a
+ ld a, c
+ ld [de], a
+ inc de
+ ld a, b
+ ld [de], a
+ inc de
+ ld a, c
+ ld [de], a
+ inc de
+ ld a, b
+ ld [de], a
+ inc de
+ pop bc
+ dec c
+ jr nz, .loop3
+ pop hl
+ ld bc, 1 tiles
+ add hl, bc
+ pop bc
+ dec c
+ jr nz, .loop1
+ ret
+
+.GetEnlargedTile:
+ push hl
+ ld hl, .EnlargedTiles
+ add l
+ ld l, a
+ ld a, 0
+ adc h
+ ld h, a
+ ld a, [hl]
+ pop hl
+ ret
+
+.EnlargedTiles:
+x = 0
+rept 16
+ db ((x & %1000) * %11000) + ((x & %0100) * %1100) + ((x & %0010) * %110) + ((x & %0001) * %11)
+x = x + 1
+endr
+
+UnownPuzzle_AddPuzzlePieceBorders:
+ ld hl, PuzzlePieceBorderData
+ ld a, 8
+.loop
+ push af
+ push hl
+ ld a, [hli]
+ ld e, a
+ ld a, [hli]
+ ld d, a
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ call .LoadGFX
+ pop hl
+rept 4
+ inc hl
+endr
+ pop af
+ dec a
+ jr nz, .loop
+ ret
+
+.LoadGFX:
+ lb bc, 4, 4
+.loop1
+ push bc
+
+.loop2
+ push de
+ push hl
+
+ ld b, 1 tiles
+.loop3
+ ld a, [de]
+ or [hl]
+ ld [hli], a
+ inc de
+ dec b
+ jr nz, .loop3
+
+ pop hl
+ ld de, 3 tiles
+ add hl, de
+ pop de
+ dec c
+ jr nz, .loop2
+
+ ld bc, 24 tiles
+ add hl, bc
+ pop bc
+ dec b
+ jr nz, .loop1
+ ret
+
+PuzzlePieceBorderData:
+ dw .TileBordersGFX + 0 tiles, vTiles0 tile $00
+ dw .TileBordersGFX + 1 tiles, vTiles0 tile $01
+ dw .TileBordersGFX + 2 tiles, vTiles0 tile $02
+ dw .TileBordersGFX + 3 tiles, vTiles0 tile $0c
+ dw .TileBordersGFX + 4 tiles, vTiles0 tile $0e
+ dw .TileBordersGFX + 5 tiles, vTiles0 tile $18
+ dw .TileBordersGFX + 6 tiles, vTiles0 tile $19
+ dw .TileBordersGFX + 7 tiles, vTiles0 tile $1a
+
+.TileBordersGFX:
+INCBIN "gfx/unown_puzzle/tile_borders.2bpp"
+
+LoadUnownPuzzlePiecesGFX:
+ ld a, [wScriptVar]
+ maskbits NUM_UNOWN_PUZZLES
+ ld e, a
+ ld d, 0
+ ld hl, .LZPointers
+ add hl, de
+ add hl, de
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ ld de, vTiles2
+ call Decompress
+ call ConvertLoadedPuzzlePieces
+ ret
+
+.LZPointers:
+; entries correspond to UNOWNPUZZLE_* constants
+ dw KabutoPuzzleLZ
+ dw OmanytePuzzleLZ
+ dw AerodactylPuzzleLZ
+ dw HoOhPuzzleLZ
+
+UnownPuzzleCursorGFX:
+INCBIN "gfx/unown_puzzle/cursor.2bpp"
+
+UnownPuzzleStartCancelLZ:
+INCBIN "gfx/unown_puzzle/start_cancel.2bpp.lz"
+
+HoOhPuzzleLZ:
+INCBIN "gfx/unown_puzzle/hooh.2bpp.lz"
+
+AerodactylPuzzleLZ:
+INCBIN "gfx/unown_puzzle/aerodactyl.2bpp.lz"
+
+KabutoPuzzleLZ:
+INCBIN "gfx/unown_puzzle/kabuto.2bpp.lz"
+
+OmanytePuzzleLZ:
+INCBIN "gfx/unown_puzzle/omanyte.2bpp.lz"