Showing posts with label Levels. Show all posts
Showing posts with label Levels. Show all posts

Sunday, April 1, 2018

The many tendrils of a Sonic 3 level, part 3: hiding in plain sight

Have you ever been on an egg hunt, and ended up walking past the damn things multiple times without actually seeing them?

Shortly after the code for the animal object, at $2CA7C we run across the code for the title card object. The first thing it does in its init routine is check if the current zone is one of the Competition levels, and if so set byte $44 of its own SST.
Obj_TitleCardInit:
        cmpi.b  #$E,(Current_zone).w
        bcs.s   loc_2CA96
        cmpi.b  #$12,(Current_zone).w
        bhi.s   loc_2CA96
        st      $44(a0)
        jmp     (Delete_Current_Sprite).l
When this flag is set, various aspects of the object's behavior are changed in order to display a unique set of title cards in Competition mode. However, these are ultimately never shown, because the object calls the Delete_Current_Sprite function immediately after setting the flag.
loc_2CA96:
        ...
        lea     TitleCard_LevelGfx,a1
        moveq   #0,d0
        move.b  (Apparent_zone).w,d0
        lsl.w   #2,d0
        movea.l (a1,d0.w),a1
        move.w  #$A9A0,d2
        jsr     (Queue_Kos_Module).l
Shortly after, we find the code which loads the title card graphics. It uses the value of the apparent zone as an index to the TitleCard_LevelGfx array, which is a list of pointers to KosM-compressed archives containing the letters that spell out each zone's name:
TitleCard_LevelGfx:     dc.l ArtKosM_AIZTitleCard
                        dc.l ArtKosM_HCZTitleCard
                        dc.l ArtKosM_MGZTitleCard
                        dc.l ArtKosM_CNZTitleCard
                        dc.l ArtKosM_FBZTitleCard
                        dc.l ArtKosM_ICZTitleCard
                        dc.l ArtKosM_LBZTitleCard
                        dc.l ArtKosM_AIZTitleCard       ; MHZ
                        dc.l ArtKosM_AIZTitleCard       ; SOZ
                        dc.l ArtKosM_AIZTitleCard       ; LRZ
                        dc.l ArtKosM_AIZTitleCard       ; SSZ
                        dc.l ArtKosM_AIZTitleCard       ; DEZ
                        dc.l ArtKosM_AIZTitleCard       ; DDZ
                        dc.l ArtKosM_AIZTitleCard       ; HPZ
                        dc.l ArtKosM_ALZTitleCard
                        dc.l ArtKosM_BPZTitleCard
                        dc.l ArtKosM_DPZTitleCard
                        dc.l ArtKosM_CGZTitleCard
                        dc.l ArtKosM_EMZTitleCard
                        dc.l ArtKosM_BonusTitleCard
                        dc.l ArtKosM_BonusTitleCard
                        dc.l ArtKosM_BonusTitleCard
Then, at $2CC62, the apparent zone is used again, this time to inform the mapping frame used by the "name" portion of the title card:
Obj_TitleCardName:
        move.b  (Apparent_zone).w,d0
        add.b   d0,$22(a0)
The sprite mappings used by the title card object can be found at $2D90C. These mappings have the same layout as their Sonic & Knuckles counterparts, except the S&K stages all point at a blank mapping frame, and there's a mapping frame for the Competition mode title cards which was removed in Sonic & Knuckles.
Map_TitleCard:  dc.w Map_TitleCard_Blank-Map_TitleCard
                dc.w Map_TitleCard_Banner-Map_TitleCard
                dc.w Map_TitleCard_Act-Map_TitleCard
                dc.w Map_TitleCard_Zone-Map_TitleCard
                dc.w Map_TitleCard_AIZ-Map_TitleCard
                dc.w Map_TitleCard_HCZ-Map_TitleCard
                dc.w Map_TitleCard_MGZ-Map_TitleCard
                dc.w Map_TitleCard_CNZ-Map_TitleCard
                dc.w Map_TitleCard_FBZ-Map_TitleCard
                dc.w Map_TitleCard_ICZ-Map_TitleCard
                dc.w Map_TitleCard_LBZ-Map_TitleCard
                dc.w Map_TitleCard_Blank-Map_TitleCard  ; MHZ
                dc.w Map_TitleCard_Blank-Map_TitleCard  ; SOZ
                dc.w Map_TitleCard_Blank-Map_TitleCard  ; LRZ
                dc.w Map_TitleCard_Blank-Map_TitleCard  ; SSZ
                dc.w Map_TitleCard_Blank-Map_TitleCard  ; DEZ
                dc.w Map_TitleCard_Blank-Map_TitleCard  ; DDZ
                dc.w Map_TitleCard_Blank-Map_TitleCard  ; HPZ
                dc.w Map_TitleCard_2PMode-Map_TitleCard
                dc.w Map_TitleCard_Bonus-Map_TitleCard
                dc.w Map_TitleCard_Stage-Map_TitleCard
Note that both TitleCard_LevelGfx and Map_TitleCard contain valid entries for Flying Battery Zone, which is why we can get as far as displaying its title card in Sonic 3.

That's not why we're here, though. At $2CC08, right before being dismissed, the title card is responsible for loading the current level's KosM PLCs. It does this by calling the rather appropriately named LoadEnemyArt function:
LoadEnemyArt:
        lea     off_2DF60,a6
        move.w  (Apparent_zone_and_act).w,d0
        ror.b   #1,d0
        lsr.w   #6,d0
        adda.w  (a6,d0.w),a6
        move.w  (a6)+,d6
        bmi.s   locret_2DF5E

loc_2DF50:
        movea.l (a6)+,a1
        move.w  (a6)+,d2
        jsr     (Queue_Kos_Module).l
        dbf     d6,loc_2DF50

locret_2DF5E:
        rts
Once again, it uses the value of the apparent zone (and act) as an index to a pointer array, this time containing pointers to the KosM PLCs for each act in the game.

The thing is, much like TitleCard_LevelGfx and Map_TitleCard before it, this pointer array also contains valid entries for Flying Battery Zone:
off_2DF60:      dc.w PLCKosM_AIZ-off_2DF60
                dc.w PLCKosM_AIZ-off_2DF60
                dc.w PLCKosM_HCZ1-off_2DF60
                dc.w PLCKosM_HCZ2-off_2DF60
                dc.w PLCKosM_MGZ1-off_2DF60
                dc.w PLCKosM_MGZ2-off_2DF60
                dc.w PLCKosM_CNZ-off_2DF60
                dc.w PLCKosM_CNZ-off_2DF60
                dc.w PLCKosM_FBZ-off_2DF60
                dc.w PLCKosM_FBZ-off_2DF60
                dc.w PLCKosM_ICZ-off_2DF60
                dc.w PLCKosM_ICZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
                dc.w PLCKosM_LBZ-off_2DF60
Remember when I said the graphics for Flying Battery's enemies went out the door with the rest of the level load block?
PLCKosM_FBZ:    dc.w 1
                dc.l ArtKosM_Blaster
                dc.w $A000
                dc.l ArtKosM_Technosqueek
                dc.w $A500
Uh... April Fools!

The PAR code 02DF46:7010 will force every level to load Flying Battery Zone's KosM PLC. You can use this alongside the codes 05B58C:7010 and 05B5C2:7010 from my previous post in order to place the FBZ enemies in any level using debug mode. FBZ's palette is long gone (I checked), but luckily Carnival Night Zone's is a suitable replacement.

Friday, March 23, 2018

The many tendrils of a Sonic 3 level, part 2.5

Before we proceed any further in our analysis, a brief digression. On the subject of the Animate_Tiles function, reader Silver Sonic 1992 commented:
Lava Reef zone has some type of dynamically reloading tiles. Is it just garbage data?
I completely missed this the first time around. Amidst all the pointers to null routines, the Offs_AniFunc table actually contains a pointer to a properly-defined animation routine for Lava Reef Zone 1:
                 dc.w AnimateTiles_NULL-Offs_AniFunc
                 dc.w AniPLC_ALZ-Offs_AniFunc
                 dc.w AnimateTiles_LRZ1-Offs_AniFunc
                 dc.w AniPLC_ALZ-Offs_AniFunc
                 dc.w AnimateTiles_NULL-Offs_AniFunc
                 dc.w AniPLC_ALZ-Offs_AniFunc
Notably, apart from some tweaks to the target VRAM offsets to make the routine work also for act 2, as well as the fact that ArtUnc_AniLRZ__BG2 seemingly grew to 1.5x its size sometime after Sonic 3's release, the bulk of the routine is quite similar to the code found in Sonic & Knuckles:
AnimateTiles_LRZ1:                              AnimateTiles_LRZ1:
                                                    move.w  #$6400,d4
                                                    move.w  #$6880,d6
                                                    bra.s   loc_282D0
                                                ; ---------------------------------------------

                                                AnimateTiles_LRZ2:
                                                    move.w  #$6400,d4
                                                    move.w  #$6880,d6

                                                loc_282D0:
    lea     (Anim_Counters).w,a3                    lea     (Anim_Counters).w,a3
    moveq   #0,d0                                   moveq   #0,d0
    move.w  ($FFFFEEE4).w,d0                        move.w  ($FFFFEEE4).w,d0
    sub.w   (Camera_X_pos_BG_copy).w,d0             sub.w   (Camera_X_pos_BG_copy).w,d0
                                                    subq.w  #1,d0
    divu.w  #$30,d0                                 divu.w  #$30,d0
    swap    d0                                      swap    d0
    cmp.b   1(a3),d0                                cmp.b   1(a3),d0
    beq.s   loc_27440                               beq.s   loc_2833C
    move.b  d0,1(a3)                                move.b  d0,1(a3)
    moveq   #0,d1                                   moveq   #0,d1
    move.w  d0,d2                                   move.w  d0,d2
    andi.w  #7,d0                                   andi.w  #7,d0
    lsl.w   #7,d0                                   lsl.w   #7,d0
    move.w  d0,d1                                   move.w  d0,d1
    lsl.w   #3,d0                                   lsl.w   #3,d0
    add.w   d0,d1                                   add.w   d0,d1
    move.l  d1,d5                                   move.l  d1,d5
    andi.w  #$38,d2                                 andi.w  #$38,d2
    move.w  d2,d0                                   move.w  d2,d0
    lsl.w   #3,d2                                   lsl.w   #3,d2
    add.w   d2,d1                                   add.w   d2,d1
    add.w   d2,d2                                   add.w   d2,d2
    add.w   d2,d1                                   add.w   d2,d1
    lsr.w   #1,d0                                   lsr.w   #1,d0
    lea     word_27446(pc,d0.w),a4                  lea     word_2834C(pc,d0.w),a4
    lea     (ArtUnc_AniALZ).l,a0                    lea     (ArtUnc_AniLRZ__BG).l,a0
    move.w  #$6020,d4
    add.l   a0,d1                                   add.l   a0,d1
    move.w  d4,d2                                   move.w  d4,d2
    move.w  (a4)+,d3                                move.w  (a4)+,d3
    add.w   d3,d4                                   add.w   d3,d4
    add.w   d3,d4                                   add.w   d3,d4
    jsr     (Add_To_DMA_Queue).l                    jsr     (Add_To_DMA_Queue).l
    move.l  d5,d1                                   move.l  d5,d1
    add.l   a0,d1                                   add.l   a0,d1
    move.w  d4,d2                                   move.w  d4,d2
    move.w  (a4)+,d3                                move.w  (a4)+,d3
    beq.s   loc_27440                               beq.s   loc_2833C
    jsr     (Add_To_DMA_Queue).l                    jsr     (Add_To_DMA_Queue).l

loc_27440:                                      loc_2833C:
                                                    cmpi.b  #$16,(Current_zone).w
                                                    beq.s   locret_2834A
    addq.w  #2,a3                                   addq.w  #2,a3
    bra.w   loc_2745E                               bra.w   loc_28364
; --------------------------------------------- ; ---------------------------------------------

                                                locret_2834A:
                                                    rts
                                                ; ---------------------------------------------
word_27446:                                     word_2834C:
    dc.w  $240,     0                               dc.w  $240,     0
    dc.w  $1E0,   $60                               dc.w  $1E0,   $60
    dc.w  $180,   $C0                               dc.w  $180,   $C0
    dc.w  $120,  $120                               dc.w  $120,  $120
    dc.w   $C0,  $180                               dc.w   $C0,  $180
    dc.w   $60,  $1E0                               dc.w   $60,  $1E0
; --------------------------------------------- ; ---------------------------------------------

loc_2745E:                                      loc_28364:
    moveq   #0,d0                                   moveq   #0,d0
    move.w  ($FFFFEEE2).w,d0                        move.w  ($FFFFEEE2).w,d0
    sub.w   (Camera_X_pos_BG_copy).w,d0             sub.w   (Camera_X_pos_BG_copy).w,d0
    andi.w  #$1F,d0                                 andi.w  #$1F,d0
    cmp.b   1(a3),d0                                cmp.b   1(a3),d0
    beq.s   locret_274BE                            beq.s   loc_283CC
    move.b  d0,1(a3)                                move.b  d0,1(a3)
    moveq   #0,d1                                   moveq   #0,d1
    move.w  d0,d2                                   move.w  d0,d2
    andi.w  #7,d0                                   andi.w  #7,d0
    lsl.w   #8,d0                                   lsl.w   #7,d0
                                                    move.w  d0,d1
                                                    add.w   d0,d0
                                                    add.w   d1,d0
    move.w  d0,d1                                   move.w  d0,d1
    move.l  d1,d5                                   move.l  d1,d5
    andi.w  #$18,d2                                 andi.w  #$18,d2
    move.w  d2,d0                                   move.w  d2,d0
    lsl.w   #3,d2                                   lsl.w   #2,d2
                                                    add.w   d2,d1
                                                    add.w   d2,d2
    add.w   d2,d1                                   add.w   d2,d1
    lsr.w   #1,d0                                   lsr.w   #1,d0
    lea     word_274C0(pc,d0.w),a4                  lea     word_283D2(pc,d0.w),a4
    lea     (ArtUnc_AniALZ).l,a0                    lea     (ArtUnc_AniLRZ__BG2).l,a0
    move.w  #$64A0,d4                               move.w  d6,d4
    add.l   a0,d1                                   add.l   a0,d1
    move.w  d4,d2                                   move.w  d4,d2
    move.w  (a4)+,d3                                move.w  (a4)+,d3
    add.w   d3,d4                                   add.w   d3,d4
    add.w   d3,d4                                   add.w   d3,d4
    jsr     (Add_To_DMA_Queue).l                    jsr     (Add_To_DMA_Queue).l
    move.l  d5,d1                                   move.l  d5,d1
    add.l   a0,d1                                   add.l   a0,d1
    move.w  d4,d2                                   move.w  d4,d2
    move.w  (a4)+,d3                                move.w  (a4)+,d3
    beq.s   locret_274BE                            beq.s   loc_283CC
    jsr     (Add_To_DMA_Queue).l                    jsr     (Add_To_DMA_Queue).l

locret_274BE:                                   loc_283CC:
                                                    addq.w  #2,a3
    rts                                             bra.w   loc_286E8
; --------------------------------------------- ; ---------------------------------------------
word_274C0:                                     word_283D2:
    dc.w   $80,     0                               dc.w   $C0,     0
    dc.w   $60,   $20                               dc.w   $90,   $30
    dc.w   $40,   $40                               dc.w   $60,   $60
    dc.w   $20,   $60                               dc.w   $30,   $90
; --------------------------------------------- ; ---------------------------------------------
Much like what happened with the level load block though, the uncompressed art used by this routine was completely wiped from the Sonic 3 ROM, leaving the code pointing at whatever data came next. In this case it's ArtUnc_AniALZ, which is uncompressed art normally used by Azure Lake's animation routine.

Friday, March 16, 2018

The many tendrils of a Sonic 3 level, part 2: the animal exchange

Picking up from where we left off, at $2BD98 we come across the Obj_Animal object, which as we learned before, is the small animal that spawns both from a defeated enemy and from an act 2 end capsule. And right at the start of its code, we find this seemingly innocuous byte array:
byte_2BDDA: dc.b 5, 1
            dc.b 0, 3
            dc.b 5, 1
            dc.b 0, 5
            dc.b 6, 5
            dc.b 2, 3
            dc.b 6, 1
            dc.b 6, 5
            dc.b 6, 5
            dc.b 6, 5
            dc.b 6, 5
            dc.b 6, 5
            dc.b 6, 5
This is actually a very dangerous byte array! When the animal object is first initialized, it uses the value of the current zone, plus the value in register d0 (which is randomly set to either 0 or 1), to index the byte_2BDDA array, and put the resulting byte into offset $30 of its own SST:
loc_2BF4A:
    moveq   #0,d1
    move.b  (Current_zone).w,d1
    add.w   d1,d1
    add.w   d0,d1
    lea     byte_2BDDA,a1
    move.b  (a1,d1.w),d0
    move.b  d0,$30(a0)
Then at loc_2BFEA, after the animal first lands on the floor, it feeds this value into the routine counter at offset 5 of its own SST, in order to determine whether it should hop or fly away:
    move.b  $30(a0),d0
    add.b   d0,d0
    addq.b  #4,d0
    move.b  d0,5(a0)
It is therefore vital that the byte_2BDDA array contain two entries for each level in the game, as these determine which two animals may spawn in any given stage. With Sonic 3 in particular, every stage past Launch Base Zone reuses the same animal combination as Flying Battery Zone. In Sonic & Knuckles, only Doomsday Zone retains this property.
byte_2BDDA: dc.b 5, 1  ; AIZ                    byte_2C7BA: dc.b 5, 1  ; AIZ
            dc.b 0, 3  ; HCZ                                dc.b 0, 3  ; HCZ
            dc.b 5, 1  ; MGZ                                dc.b 5, 1  ; MGZ
            dc.b 0, 5  ; CNZ                                dc.b 0, 5  ; CNZ
            dc.b 6, 5  ; FBZ                                dc.b 6, 5  ; FBZ
            dc.b 2, 3  ; ICZ                                dc.b 2, 3  ; ICZ
            dc.b 6, 1  ; LBZ                                dc.b 5, 1  ; LBZ
            dc.b 6, 5  ; MHZ                                dc.b 6, 1  ; MHZ
            dc.b 6, 5  ; SOZ                                dc.b 0, 1  ; SOZ
            dc.b 6, 5  ; LRZ                                dc.b 5, 1  ; LRZ
            dc.b 6, 5  ; SSZ                                dc.b 0, 5  ; SSZ
            dc.b 6, 5  ; DEZ                                dc.b 6, 1  ; DEZ
            dc.b 6, 5  ; DDZ                                dc.b 6, 5  ; DDZ
                                                            dc.b 5, 1  ; Ending
                                                            dc.b 5, 1  ; ALZ
                                                            dc.b 5, 1  ; BPZ
                                                            dc.b 5, 1  ; DPZ
                                                            dc.b 5, 1  ; CGZ
                                                            dc.b 5, 1  ; EMZ
                                                            dc.b 5, 1  ; Gumball
                                                            dc.b 5, 1  ; Pachinko
                                                            dc.b 5, 1  ; Slots
                                                            dc.b 5, 1  ; LRZ Boss
                                                            dc.b 5, 1  ; DEZ Boss
Interestingly, in Sonic & Knuckles, the array was extended to cover every level slot, rather than stopping at Doomsday Zone. This was probably done for the sake of the Lava Reef Zone boss act, in which animals spawn from the capsule at the very end of the stage. Indeed, if we look at the numbers, all of the new entries in the array use the same animal combination as Lava Reef Zone.

The length of this array in Sonic 3 suggests that attempting to load any level slot beyond Doomsday Zone is inherently dangerous. But in reality, it only becomes a problem if an animal is spawned while playing the stage; it shouldn't affect whether the stage can load or not.

What does affect it are the animals' graphics, which are loaded by the PLCLoad_AnimalsAndExplosion function at $543F4 when the level starts. Much like the animal object, it uses the value of the current zone as an offset to an array, which contains pointers to the PLCs for each stage's combination of animals.

And therein lies the rub, because in Sonic 3, this array only goes up to Launch Base Zone:
off_54414:  dc.w PLC_54422-off_54414  ; AIZ     off_85FFE:  dc.w PLC_8602E-off_85FFE  ; AIZ
            dc.w PLC_54430-off_54414  ; HCZ                 dc.w PLC_8603C-off_85FFE  ; HCZ
            dc.w PLC_5443E-off_54414  ; MGZ                 dc.w PLC_8604A-off_85FFE  ; MGZ
            dc.w PLC_5444C-off_54414  ; CNZ                 dc.w PLC_86058-off_85FFE  ; CNZ
            dc.w PLC_5445A-off_54414  ; FBZ                 dc.w PLC_86066-off_85FFE  ; FBZ
            dc.w PLC_54468-off_54414  ; ICZ                 dc.w PLC_86074-off_85FFE  ; ICZ
            dc.w PLC_54476-off_54414  ; LBZ                 dc.w PLC_86082-off_85FFE  ; LBZ
                                                            dc.w PLC_86090-off_85FFE  ; MHZ
                                                            dc.w PLC_8609E-off_85FFE  ; SOZ
                                                            dc.w PLC_860AC-off_85FFE  ; LRZ
                                                            dc.w PLC_860BA-off_85FFE  ; SSZ
                                                            dc.w PLC_860C8-off_85FFE  ; DEZ
                                                            dc.w PLC_860D6-off_85FFE  ; DDZ
                                                            dc.w PLC_860E4-off_85FFE  ; Ending
                                                            dc.w PLC_860E4-off_85FFE  ; ALZ
                                                            dc.w PLC_860E4-off_85FFE  ; BPZ
                                                            dc.w PLC_860E4-off_85FFE  ; DPZ
                                                            dc.w PLC_860E4-off_85FFE  ; CGZ
                                                            dc.w PLC_860E4-off_85FFE  ; EMZ
                                                            dc.w PLC_860E4-off_85FFE  ; Gumball
                                                            dc.w PLC_860E4-off_85FFE  ; Pachinko
                                                            dc.w PLC_860E4-off_85FFE  ; Slots
                                                            dc.w PLC_860E4-off_85FFE  ; LRZ Boss
                                                            dc.w PLC_860E4-off_85FFE  ; DEZ Boss
PLC_54422:  dc.w 1                              PLC_8602E:  dc.w 1
            dc.l ArtNem_BlueFlicky                          dc.l ArtNem_BlueFlicky
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_Chicken                             dc.l ArtNem_Chicken
            dc.w $B240                                      dc.w $B240
PLC_54430:  dc.w 1                              PLC_8603C:  dc.w 1
            dc.l ArtNem_Rabbit                              dc.l ArtNem_Rabbit
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_Seal                                dc.l ArtNem_Seal
            dc.w $B240                                      dc.w $B240
PLC_5443E:  dc.w 1                              PLC_8604A:  dc.w 1
            dc.l ArtNem_BlueFlicky                          dc.l ArtNem_BlueFlicky
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_Chicken                             dc.l ArtNem_Chicken
            dc.w $B240                                      dc.w $B240
PLC_5444C:  dc.w 1                              PLC_86058:  dc.w 1
            dc.l ArtNem_Rabbit                              dc.l ArtNem_Rabbit
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_BlueFlicky                          dc.l ArtNem_BlueFlicky
            dc.w $B240                                      dc.w $B240
PLC_5445A:  dc.w 1                              PLC_86066:  dc.w 1
            dc.l ArtNem_Squirrel                            dc.l ArtNem_Squirrel
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_BlueFlicky                          dc.l ArtNem_BlueFlicky
            dc.w $B240                                      dc.w $B240
PLC_54468:  dc.w 1                              PLC_86074:  dc.w 1
            dc.l ArtNem_Penguin                             dc.l ArtNem_Penguin
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_Seal                                dc.l ArtNem_Seal
            dc.w $B240                                      dc.w $B240
PLC_54476:  dc.w 1                              PLC_86082:  dc.w 1
            dc.l ArtNem_Squirrel                            dc.l ArtNem_BlueFlicky
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_Chicken                             dc.l ArtNem_Chicken
            dc.w $B240                                      dc.w $B240
                                                PLC_86090:  dc.w 1
                                                            dc.l ArtNem_Squirrel
                                                            dc.w $B000
                                                            dc.l ArtNem_Chicken
                                                            dc.w $B240
                                                PLC_8609E:  dc.w 1
                                                            dc.l ArtNem_Rabbit
                                                            dc.w $B000
                                                            dc.l ArtNem_Chicken
                                                            dc.w $B240
                                                PLC_860AC:  dc.w 1
                                                            dc.l ArtNem_BlueFlicky
                                                            dc.w $B000
                                                            dc.l ArtNem_Chicken
                                                            dc.w $B240
                                                PLC_860BA:  dc.w 1
                                                            dc.l ArtNem_Rabbit
                                                            dc.w $B000
                                                            dc.l ArtNem_Chicken
                                                            dc.w $B240
                                                PLC_860C8:  dc.w 1
                                                            dc.l ArtNem_Squirrel
                                                            dc.w $B000
                                                            dc.l ArtNem_Chicken
                                                            dc.w $B240
                                                PLC_860D6:  dc.w 1
                                                            dc.l ArtNem_Squirrel
                                                            dc.w $B000
                                                            dc.l ArtNem_Chicken
                                                            dc.w $B240
                                                PLC_860E4:  dc.w 1
                                                            dc.l ArtNem_BlueFlicky
                                                            dc.w $B000
                                                            dc.l ArtNem_Chicken
                                                            dc.w $B240
So even if we were to fix up the level load block for the Sonic & Knuckles stages, the PLCLoad_AnimalsAndExplosion function would then take the value of the current zone, go past the end of the pointer array straight into the actual PLC definitions, and eventually attempt to load a nonsensical PLC, which would most definitely crash the game. Huzzah!
PLC_54476:  dc.w 1                              PLC_86082:  dc.w 1
            dc.l ArtNem_Squirrel                            dc.l ArtNem_BlueFlicky
            dc.w $B000                                      dc.w $B000
            dc.l ArtNem_Chicken                             dc.l ArtNem_Chicken
            dc.w $B240                                      dc.w $B240
It was only by looking at the animal data side-by-side like this that I stumbled upon one of the most obscure differences between Sonic 3 and Sonic & Knuckles that I know of. In Sonic 3, the animals which appear in Launch Base Zone are Ricky (the squirrel) and Cucky (the chicken). In Sonic & Knuckles however, Ricky was replaced by Flicky (the flicky).

This wasn't an accident; if you check the byte_2BDDA array at the top of this post, you'll see that the animal type was changed from 6 to 5 there, too.


Finally, on the subject of animal types, here's something that always bugged me. In Sonic 1, there were seven different animals. From left to right, they are: Pocky, Cucky, Pecky, Rocky, Picky, Flicky and Ricky.


Sonic 2 then added five more, whose names I can't find a source for: an eagle, a mouse, a monkey, a turtle and a bear.


When it came time to make Sonic 3, the developers decided to scale back to the original cast of animals. Indeed, if you look at the byte_2BDDA array, you'll find that they're numbered 0 through 6... but animal 4 is nowhere to be seen.


Picky appears to have disappeared; not even his graphics can be found within the ROM. Maybe he's still hiding in the Casino Night Zone?

Sunday, March 11, 2018

The many tendrils of a Sonic 3 level, part 1

Apologies for the radio silence during the past month; I've been absolutely swamped with work and haven't had time for either the blog or the hack.

On the subject of loading Sonic & Knuckles stages in Sonic 3, an anonymous commenter wondered whether the bogus entries in the S3A level load block are enough to make the game crash while loading those levels, to which I answered that yes, that should be quite enough.

However, are they the sole cause of errors in this scenario? Assuming we patch up the level load block to point at valid Kosinski data where appropriate, would those stages then boot up properly? Not necessarily.

First off, as we saw before, the level load block itself contains byte pointers which are used as an index to the PalPoint and Offs_PLC arrays. Obviously, these bytes must correspond to valid entries within those arrays, otherwise we'll once again be attempting to decompress garbage data, or trying to copy nonsensical color values to a random location in the Mega Drive's address space.

Beyond this though, there are several other points in the game where code is executed and data is loaded conditionally depending on which level is being played. These points are scattered throughout the ROM without any structure, which is partially why Sonic 3 has a reputation of being a harder game to hack than previous Sonic titles.

Let's start from the top. At $23CA, we have the AnPal_Load function, which is called once every frame in order to run all of the animated palettes within a stage. To accomplish this, it takes the value of the current zone and act and uses it as an index to the OffsAnPal array, which is itself a list of pointers to smaller routines that then handle all the palette animations specific to that stage:
OffsAnPal:      dc.w AnPal_AIZ1-OffsAnPal
                dc.w AnPal_AIZ2-OffsAnPal
                dc.w AnPal_HCZ1-OffsAnPal
                dc.w AnPal_HCZ2-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_CNZ-OffsAnPal
                dc.w AnPal_CNZ-OffsAnPal
                dc.w AnPal_FBZ-OffsAnPal
                dc.w AnPal_FBZ-OffsAnPal
                dc.w AnPal_ICZ-OffsAnPal
                dc.w AnPal_ICZ-OffsAnPal
                dc.w AnPal_LBZ1-OffsAnPal
                dc.w AnPal_LBZ2-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_LRZ1-OffsAnPal
                dc.w AnPal_LRZ2-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_BPZ-OffsAnPal
                dc.w AnPal_BPZ-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_CGZ-OffsAnPal
                dc.w AnPal_CGZ-OffsAnPal
                dc.w AnPal_EMZ-OffsAnPal
                dc.w AnPal_EMZ-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
                dc.w AnPal_None-OffsAnPal
Interestingly, Flying Battery Zone has its own routine just like in Sonic & Knuckles, except that here it does absolutely nothing. More interestingly, there are also routines defined for both acts of Lava Reef Zone, and although the routine for act 2 is blank, the routine for act 1 is fully functional, and even references some otherwise unused palette data!


The presence of this data, which is identical to the corresponding data in the Sonic & Knuckles ROM, ties squarely into the notion that Lava Reef Zone had already entered production by the time of Sonic 3's release.

Moving right along, at $4680 we have the LevelMusic_Playlist, which as we saw before, dictates which track is played at level load based on the current zone and act. Sonic 3's version of the playlist is identical to the one found in Sonic & Knuckles, except for the following points:

  • The Rolling Jump bonus stage reuses the track for the Gumball bonus stage.
  • Both the ending level slot and the four acts at the end of the level list make use of the special stage track.
  • Sky Sanctuary Zone and Death Egg Zone are bugged. Instead of using the same song for both acts, act 2 of Sky Sanctuary Zone uses the song meant for Death Egg Zone 1, and then both acts of Death Egg Zone use the song meant for Death Egg Zone 2.

At $1A1F4, we find the LevelSizes structure. All of the levels unused in Sonic 3 have the default size of $1000 pixels down and $6000 pixels across. Shortly after, at $1A8A8 we find the LevelResizeArray. Like in Sonic & Knuckles, every stage past Launch Base Zone 2 points at an empty dynamic resize routine.

Then at $26A92, we find the Animate_Tiles function, which is also called once every frame in order to process all the animated elements within a level. Much like the AnPal_Load function, it uses the current zone and act as an index to an array of pointers to smaller routines, which then handle the tile-based animations specific to that level. Interleaved with this array however, is another array containing pointers to the level's "animated PLC", which consists of the ROM address of the uncompressed art, the destination VRAM address, and the duration of each frame of animation.
Offs_AniFunc:   dc.w AnimateTiles_AIZ1-Offs_AniFunc
Offs_AniPLC:    dc.w AniPLC_AIZ1-Offs_AniFunc
                dc.w AnimateTiles_AIZ2-Offs_AniFunc
                dc.w AniPLC_AIZ2-Offs_AniFunc
                dc.w AnimateTiles_HCZ1-Offs_AniFunc
                dc.w AniPLC_HCZ1-Offs_AniFunc
                dc.w AnimateTiles_HCZ2-Offs_AniFunc
                dc.w AniPLC_HCZ2-Offs_AniFunc
                dc.w AnimateTiles_MGZ-Offs_AniFunc
                dc.w AniPLC_MGZ-Offs_AniFunc
                dc.w AnimateTiles_MGZ-Offs_AniFunc
                dc.w AniPLC_MGZ-Offs_AniFunc
                dc.w AnimateTiles_CNZ-Offs_AniFunc
                dc.w AniPLC_CNZ-Offs_AniFunc
                dc.w AnimateTiles_CNZ-Offs_AniFunc
                dc.w AniPLC_CNZ-Offs_AniFunc
                dc.w AnimateTiles_NULL-Offs_AniFunc
                dc.w AniPLC_ICZ-Offs_AniFunc
                dc.w AnimateTiles_NULL-Offs_AniFunc
                dc.w AniPLC_ICZ-Offs_AniFunc
                dc.w AnimateTiles_ICZ-Offs_AniFunc
                dc.w AniPLC_ICZ-Offs_AniFunc
                dc.w AnimateTiles_ICZ-Offs_AniFunc
                dc.w AniPLC_ICZ-Offs_AniFunc
                dc.w AnimateTiles_LBZ1-Offs_AniFunc
                dc.w AniPLC_LBZ1-Offs_AniFunc
                dc.w AnimateTiles_LBZ2-Offs_AniFunc
                dc.w AniPLC_LBZ2-Offs_AniFunc
                dc.w AnimateTiles_NULL-Offs_AniFunc
                dc.w AniPLC_ALZ-Offs_AniFunc
                dc.w AnimateTiles_NULL-Offs_AniFunc
                dc.w AniPLC_ALZ-Offs_AniFunc
                dc.w AnimateTiles_NULL-Offs_AniFunc
                dc.w AniPLC_ALZ-Offs_AniFunc
                dc.w AnimateTiles_NULL-Offs_AniFunc
                dc.w AniPLC_ALZ-Offs_AniFunc
                ...
As with the OffsAnPal array, all of the empty slots in the Offs_AniFunc array point to blank, "null" routines. However, much like the level load block, empty slots in the Offs_AniPLC array are filled with pointers to whatever data follows the previous PLC entry. As a result, Flying Battery Zone points to the PLC for Icecap Zone, all of the Sonic & Knuckles levels point to the PLC for Azure Lake, and every level past Desert Palace (the last stage with animated PLCs) points at the end of the animated PLC block.

Okay, but unlike the data mismatch in the level load block, the Azure Lake PLC is obviously valid PLC data, so loading Sonic & Knuckles levels shouldn't cause the game to crash. And even if the PLC data is invalid, such as with the levels that point at the end of the animated PLC block, the fact that the animation routine is blank means the game doesn't do anything with the invalid PLC data.

So far we haven't found anything that might crash the game. In the next post, we'll look at one aspect which could, and in the process, stumble upon an obscure version difference.

Thursday, February 1, 2018

Trouble at the border

A while ago, we saw how in Sonic 3, Knuckles' boss area in Launch Base Zone 1 was originally a bit different, and that Sonic 3 & Knuckles modifies it by altering the level layout directly in RAM. Logically, the same changes are also applied to the matching area at the start of act 2, except that this time, the twin boxes are already open.


Interestingly, in Sonic 3 the checkered tube is still present in the act 2 layout, despite normally being removed when the boss is defeated. It's possible that the act transition was originally planned to happen up there, and the tube would only explode at the start of act 2.

However, this theory is challenged by the presence of hidden monitors, buried in the floor at the bottom of the room, as early as the Sonic 3 object layout.


On the other hand, assuming the tube was meant to explode in act 1, it would then suddenly reappear upon loading the act 2 layout, which wcould lead to this curious scenario.

This isn't the only level layout adjustment performed in Launch Base Zone 2, though: directly after Knuckles' act 2 boss area, the Sonic 3 layout cuts off rather abruptly. In Sonic 3 & Knuckles, a couple of extra chunks were added to end the pipe floor more naturally.


This change is purely cosmetic however, because Knuckles' end of level cutscene forces him to jump at the exact point where the floor would cut off, regardless.


If it's weird you're looking for, though, look no further than Angel Island Zone 2. For whatever reason, there are actually hidden monitors buried in the floor of Knuckles' boss area. In act 2.


What on earth could the developers have been planning here?

Tuesday, January 2, 2018

Level Load Block in S3A

Reader kathy asks:
is the emerald shrine that you're warped to for the super emerald special stages the same stage as hidden palace? how's that work?
Well, as I previously mentioned, they're completely separate levels. They just happen to have nearly identical level load blocks and level layouts, only really differing in their object layout and level size, as well as more esoteric aspects such as their background event routines.

Surprisingly enough, entries for these stages exist within S3A's level load block, hinting that their addition wasn't as late as one might think. Which brings me to this question by an anonymous commenter:
I'd be more interested in knowing what happens when a Sonic & Knuckles stage gets loaded in Sonic 3. For example, what tells the game to use Azure Lake graphics in Mushroom Valley Zone?
Well, since the data for the Sonic & Knuckles stages isn't included in the Sonic 3 cartridge, their entries in the level load block had to be replaced with placeholder values. For example, Flying Battery Zone, which occupies the slots between Carnival Night Zone and Icecap Zone, has all of its data pointers replaced with pointers to Icecap Zone's primary 16x16 block definitions:
; ---------------------------------------------------------------------------
    dc.l ICZ_16x16_Primary_Kos+$1A000000
    dc.l ICZ_16x16_Primary_Kos+$1A000000
    dc.l ICZ_16x16_Primary_Kos+$12000000
    dc.l ICZ_16x16_Primary_Kos+$12000000
    dc.l ICZ_16x16_Primary_Kos
    dc.l ICZ_16x16_Primary_Kos
; ---------------------------------------------------------------------------
    dc.l ICZ_16x16_Primary_Kos+$1C000000
    dc.l ICZ_16x16_Primary_Kos+$1C000000
    dc.l ICZ_16x16_Primary_Kos+$13000000
    dc.l ICZ_16x16_Primary_Kos+$13000000
    dc.l ICZ_16x16_Primary_Kos
    dc.l ICZ_16x16_Primary_Kos
; ---------------------------------------------------------------------------
Mushroom Valley Zone and Lava Reef Zone are both pointing at Azure Lake's primary 16x16 block definitions, which is what tells the game to use Azure Lake graphics in Mushroom Valley Zone. Meanwhile, Sandopolis Zone and the rest of the Sonic & Knuckles levels instead contain an unaltered copy of Angel Island Zone's level load block:
; ---------------------------------------------------------------------------
    dc.l ALZ_16x16_Kos+$26000000
    dc.l ALZ_16x16_Kos+$26000000
    dc.l ALZ_16x16_Kos+$18000000
    dc.l ALZ_16x16_Kos+$18000000
    dc.l ALZ_16x16_Kos
    dc.l ALZ_16x16_Kos
; ---------------------------------------------------------------------------
    dc.l ALZ_16x16_Kos+$28000000
    dc.l ALZ_16x16_Kos+$28000000
    dc.l ALZ_16x16_Kos+$19000000
    dc.l ALZ_16x16_Kos+$19000000
    dc.l ALZ_16x16_Kos
    dc.l ALZ_16x16_Kos
; ---------------------------------------------------------------------------
    dc.l AIZ1_8x8_Primary_KosM+$B000000
    dc.l AIZ1_8x8_Secondary_KosM+$B000000
    dc.l AIZ1_16x16_Secondary_Kos+$A000000
    dc.l AIZ1_16x16_Secondary_Kos+$A000000
    dc.l AIZ1_128x128_Kos
    dc.l AIZ1_128x128_Kos
; ---------------------------------------------------------------------------
    dc.l AIZ1_8x8_Primary_KosM+$C000000
    dc.l AIZ1_8x8_Secondary_KosM+$C000000
    dc.l AIZ1_16x16_Secondary_Kos+$B000000
    dc.l AIZ1_16x16_Secondary_Kos+$B000000
    dc.l AIZ1_128x128_Kos
    dc.l AIZ1_128x128_Kos
; ---------------------------------------------------------------------------
    dc.l ALZ_16x16_Kos+$2E000000
    dc.l ALZ_16x16_Kos+$2E000000
    dc.l ALZ_16x16_Kos+$1C000000
    dc.l ALZ_16x16_Kos+$1C000000
    dc.l ALZ_16x16_Kos
    dc.l ALZ_16x16_Kos
; ---------------------------------------------------------------------------
    dc.l ALZ_16x16_Kos+$30000000
    dc.l ALZ_16x16_Kos+$30000000
    dc.l ALZ_16x16_Kos+$1D000000
    dc.l ALZ_16x16_Kos+$1D000000
    dc.l ALZ_16x16_Kos
    dc.l ALZ_16x16_Kos
; ---------------------------------------------------------------------------
That is, except for the Sonic & Knuckles bonus stages, which instead contain pointers to the end of ROM marker!
; ---------------------------------------------------------------------------
    dc.l Gumball_8x8_KosM+$47000000
    dc.l Gumball_8x8_KosM+$47000000
    dc.l Gumball_16x16_Kos+$33000000
    dc.l Gumball_16x16_Kos+$33000000
    dc.l Gumball_128x128_Kos
    dc.l Gumball_128x128_Kos
; ---------------------------------------------------------------------------
    dc.l Gumball_8x8_KosM+$47000000
    dc.l Gumball_8x8_KosM+$47000000
    dc.l Gumball_16x16_Kos+$33000000
    dc.l Gumball_16x16_Kos+$33000000
    dc.l Gumball_128x128_Kos
    dc.l Gumball_128x128_Kos
; ---------------------------------------------------------------------------
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$37000000
    dc.l EndOfROM+$37000000
    dc.l EndOfROM
    dc.l EndOfROM
; ---------------------------------------------------------------------------
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$37000000
    dc.l EndOfROM+$37000000
    dc.l EndOfROM
    dc.l EndOfROM
; ---------------------------------------------------------------------------
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$38000000
    dc.l EndOfROM+$38000000
    dc.l EndOfROM
    dc.l EndOfROM
; ---------------------------------------------------------------------------
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$47000000
    dc.l EndOfROM+$38000000
    dc.l EndOfROM+$38000000
    dc.l EndOfROM
    dc.l EndOfROM
; ---------------------------------------------------------------------------
So what exactly is going on? Let's take Flying Battery Zone as an example again. What would happen if the developers simply removed all the FBZ level data from the build, without actually touching the level load block? Since the FBZ data now takes up zero bytes, all labels to it should now point at whatever followed FBZ's data when it was still in the ROM.

Assuming that FBZ's data originally came between the data for CNZ and ICZ, what do you suppose follows CNZ's data in the final ROM layout?

NameAddress
CNZ_16x16_Kos1CCC34
CNZ_8x8_KosM1CDC74
CNZ_128x128_Kos1D0E96
ICZ_16x16_Primary_Kos1D3FB6
ICZ_8x8_Primary_KosM1D4296
ICZ_128x128_Primary_Kos1D56A8

Mushroom Valley Zone, Lava Reef Zone, and the Sonic & Knuckles bonus stages follow the same pattern:

NameAddress
LBZ2_16x16_Secondary_Kos1E5F70
LBZ2_8x8_Secondary_KosM1E77D0
LBZ2_128x128_Kos1EAF04
ALZ_16x16_Kos1EE0C4
ALZ_8x8_KosM1EEB84
ALZ_128x128_Kos1F1936
......
Gumball_16x16_Kos1FEA0E
Gumball_8x8_KosM1FEE2E
Gumball_128x128_Kos1FFB80
EndOfROM1FFF20

Okay, but what about Sandopolis Zone and the remaining Sonic & Knuckles level slots? Why are they filled with copies of Angel Island Zone's level load block?

Here's my guess: the developers originally padded the entire table with copies of the AIZ data, before filling in each slot with the actual data as the respective level entered production. The presence of AIZ data in the slots for Sandopolis Zone and the remaining S&K stages is indication that those stages hadn't yet entered production at the time of Sonic 3's release.

Indeed, by digging around in the ROM, we can find unused object layouts for Mushroom Valley Zone as well as unused palette data for Lava Reef Zone and the S&K bonus stages, but seemingly nothing for the remaining levels.

Monday, October 30, 2017

The transformation glitch

A while ago, I mentioned how you can go through the rising lava in Lava Reef Zone 1 by transforming into Super Sonic right before you land on it. More generally, characters go through all solid objects while they're hanging in the air during their transformation sequence. In his glitches and oversights series, GoldS dubbed this the "transformation glitch".


So what's special about the transformation sequence that causes you to phase through solid objects? Something which I glossed over back when I talked about the Super monitor is how as part of the sequence, the object_control attribute at offset $2E of the player's SST is set to a particular value:
Sonic_Transform:
    move.b  #1,(Super_Hyper_palette_status).w   ; set Super/Hyper palette status to 'fading'
    move.b  #$F,(Palette_timer).w
    move.w  #60,(Super_Hyper_frame_count).w
    move.l  #Map_SuperSonic,mappings(a0)
    move.b  #$81,object_control(a0)
    move.b  #$1F,anim(a0)                       ; enter 'transformation' animation
    ...
The object_control attribute is a bitfield, whose flags instruct the player object to skip key routines such as movement, collision and animation. They're typically set when another object is controlling the player, hence the attribute's name.

In this case, a value of $81 means that bits 7 and 0 are set. Bit 7 causes object collision to be skipped, while bit 0 skips over the player's movement routines, removing the effects of gravity. This is different from the "standing on object" flag, which also essentially disables gravity, but leaves players in full control of their movement.


When the player's movement routines don't run, level collision effectively becomes disabled, due to the assumption that the player can't touch the level unless they're traveling toward it. This quirk is actually quite important, because it allows objects to pull the player straight through solid ground, such as the hooks and teacups in Launch Base Zone.


Okay, so the transformation sequence temporarily disables gravity by setting bit 0 of the object_control bitfield, so that the player hangs in the air a bit while the animation plays. But why does it also disable object collision by setting bit 7?

Because the alternative is worse.


When bit 0 is set, the player object assumes that it must be completely stationary, so it doesn't bother checking for level collisions. However, unless bit 7 is also set, other objects may break this assumption by pushing the player themselves, which will then send the player straight through solid ground.

Better safe than sorry.

Friday, October 20, 2017

Implicit unboxing conversions

Reader Silver Sonic 1992 asked:
The Twin hammer boxes had their art changed slightly between Sonic 3 and Sonic 3 & Knuckles?
Specifically, they're referring to my dissection of Knuckles' boss area in Launch Base Zone 1, and the changes made to it between Sonic 3 and Sonic 3 & Knuckles, which I illustrated using these two screenshots:


Let's take a step back. In act 2, these boxes are present alongside each of these ascending/descending hook objects:


Here we can see these boxes are clearly using the "Sonic 3" design, even though the screenshot was obviously taken in Sonic & Knuckles. Can you understand what is going on now?

Let's go back to the Sonic and Tails act 1 boss area:


This is an instance where sprites don't use background art. The sprite-based object that Eggman carries around, which then opens up to reveal the boss, uses a different set of graphics and mappings from the solid, chunk-based boxes that are permanent fixtures of the level.


It just so happens that the 16x16 blocks along the center of those chunks got swapped vertically somehow, resulting in a weird pattern which doesn't look like it could actually open. Would've been interesting if it did, though.

Thursday, October 19, 2017

The ghosts are already loose

When playing as Knuckles in Sandopolis Zone 2, the ghosts don't wait for Last_star_post_hit to have a non-zero value before spawning. And later, when you reach the capsule, you find that it's already been opened.


Continuing my point from last time, this is because Knuckles' story takes place after Sonic's, and no one re-trapped the ghosts in the capsule after Sonic and Tails freed them.

This isn't the only instance of level events in Sonic's story having a tangible effect on Knuckles' playthrough of the level.


When playing as Sonic or Tails, there's a cutscene at the end of Launch Base Zone 1 in which Knuckles throws a bomb into a building, causing it to collapse. Later, when Knuckles plays through the stage, the building has sunk into the floor, revealing the alternate path hidden behind a wall in Sonic and Tails' version of the stage.


In fact, the very same function is called during both Sonic and Knuckles' stories in order to remove the building from the level layout. That should cement any doubts on the issue, concretely proving that my claims are rock solid.

*bricked*

Wednesday, October 11, 2017

Knuckles in Sonic 2: object layouts

Just as with Sonic 3 before it, Knuckles in Sonic 2 includes its own object layouts for all the Sonic 2 levels. The reasons for this are varied. Sega made sure to flaunt the extra 1-ups in Chemical Plant Zone, but sometimes the additional item boxes are a little bit more subtle.


Sonic 2's levels weren't designed with Knuckles' climbing ability in mind, and as a result, some walls only extend as far as Sonic can reach. Knuckles in Sonic 2 occasionally patches these through clever use of a level's existing obstacles.


Other solutions aren't as elegant. The underside of Hill Top Zone 1's starting area cuts off abruptly, so they just stacked a bunch of invisible blocks on either side of the bottomless pit. Meanwhile, in Wing Fortress Zone, invisible blocks were placed at the end of the level to prevent Knuckles from gliding over the boss area.


Finally, sometimes less is more. In Sonic 2, there's a breakable column right near the start of Aquatic Ruin Zone. It was removed in the Knuckles in Sonic 2 layout because it'd be a bit stupid to start off the level with an obstacle that's too tall to clear with Knuckles' reduced jump height.


Hm, that water looks kind of funky... Oh yeah, we haven't even talked about the consequences of trying to put Knuckles and his palette into a game that wasn't designed for it. That'll be our subject for the next couple of posts.

Friday, October 6, 2017

Sonic 3 stages in S3&K: level layouts

Okay, so object and ring layouts can't be reused because they're not amenable to being patched, and they have to get patched for Knuckles. What about level layouts? Are they amenable to being patched?

Well, as we saw before, a level layout is pretty much just a flat array of chunk IDs, which is copied to RAM on level load to allow crazy stuff like copying chunks from one part of the layout to another in the middle of gameplay. They are most definitely amenable to being patched, so the ones in the Sonic 3 cartridge do just fine.

Do they require patching? Yes. For example, Launch Base Zone 1 has the following code at the start of its background init function. Note that during execution of screen and background event functions, the a3 register contains the address of the first row pointer for the respective level layout plane.
LBZ1_BackgroundInit:
    cmpi.w  #3,(Player_mode).w
    bne.s   loc_541C6
    movea.w (a3),a1
    addq.w  #2,a1
    move.b  #$E4,(a1)+
    move.b  #$E6,(a1)+
    move.b  #$E0,(a1)+
    movea.w 4(a3),a1
    addq.w  #2,a1
    move.b  #$EC,(a1)+
    move.b  #$EE,(a1)+
    move.b  #$E8,(a1)+
    ...
So what this code does is load the pointer to the first row of background chunks into the a1 register, skip over the first two chunks in the row, and then replace the next three chunks with different chunk IDs. Then it loads the pointer to the second row of chunks and does the same thing.

In practice, the chunks being replaced are the ones that put the Death Egg on the background plane. Since Knuckles' story takes place after Sonic and Tails', during the course of which the Death Egg is launched back into outer space, it wouldn't make sense for it to appear in the background when playing as Knuckles.

As such, this:


becomes this:


Something similar happens at the very end of the level, after defeating Knuckles' boss. Here's the code, this time from the screen event function:
loc_53F50:
    movea.w $4C(a3),a1
    move.b  $78(a1),d0
    movea.w $50(a3),a1
    lea     $78(a1),a1
    move.b  d0,(a1)+
    move.b  d0,(a1)+
    move.b  d0,(a1)+
    jmp     Refresh_PlaneScreenDirect(pc)
This one is cute. First, it loads the pointer to a specific chunk row to register a1, then copies a specific chunk ID from that row into register d0. It's this one:


Next, it loads a different chunk row into a1, skips over the first 120 chunks, and pastes the previously copied chunk into the three slots that follow, erasing the checkerboard pipe from the level layout. Note how the background pattern isn't a perfect match; it's that way in-game as well:


Something doesn't seem right about the above two images, though. The twin boxes appear way too low. Well, if you go and check, they're actually like that in the Sonic 3 layout. It was Sonic & Knuckles that patched them.


The code responsible for doing so is in the screen init function for LBZ1:
LBZ1_ScreenInit:
    ...
    movea.w $48(a3),a1
    movea.w $4C(a3),a5
    move.b  $79(a1),$79(a5)
    move.b  #$DB,$79(a1)
    jsr     LBZ1_RotateChunks(pc)
    ...
Okay, so the first thing this code does is load the pointers to two chunk rows into registers a1 and a5. Then, it copies a chunk from the first row...


...and pastes it over the same chunk on the second row, also replacing the one on the first row with a chunk ID of $DB:


We seem to have placed a strange chunk up there. This is actually correct: the boss object displays the outside of the boxes using sprites, and the chunk is only used to show the inside of the boxes once they open.

Astute viewers will notice that the box is now too high. Argh. Best look inside that LBZ1_RotateChunks function:
LBZ1_RotateChunks:
    moveq   #$17,d2

loc_54148:
    lea     ($FF6E00).l,a1
    lea     -2(a1),a5
    move.w  (a5),d0
    moveq   #$3E,d1

loc_54156:
    move.w  -(a5),-(a1)
    dbf     d1,loc_54156
    move.w  d0,(a5)
    dbf     d2,loc_54148
    rts
Holy goodness, what is happening here?

What this function is actually doing is patching the chunk definition for chunk $DB. That is, it is changing the pattern of 16x16 blocks which make up the 128x128 chunk. More specifically, it's taking the bottom three rows of blocks...


...and throwing them on top of the chunk, bringing the remaining rows down. It's "rotating" the chunk in the sense that it's sliding all the blocks down by three rows:


This isn't the only instance of chunk definition patching in the game, though. In Angel Island Zone 1, it's used to remove the horizon line from the background while playing as Knuckles. Again, that's because Knuckles' story takes place after Sonic and Tails', in which the Master Emerald is returned to Angel Island, causing it to resume floating in the sky.

Note also how once again, some of the replacement blocks aren't a perfect match:


That's it for our brief look into Sonic 3 & Knuckles. Next up is the game born out of circumstance: Knuckles in Sonic 2.