Fix Hill Top's background scrolling mountains
From Sonic Retro
(Original guide by Flamewing)
If you pay close attention to the blue mountains in the far background part of Hill Top zone, you will notice it scrolls independently from the larger, nearer brown mountains. This is achieved by dynamically reloading the tiles corresponding to those mountains, using one of eight different versions of the mountain art, each shifted by one pixel relative to the previous.
If you watch the mountains close enough -- preferably in frame advance -- you will notice that every once in a while the mountains will scroll in the wrong direction by one pixel, then scroll right back to the right place on the subsequent frame. This guide is about fixing that, because no bugs are too small to fix.
Fixing the bug
Open s2.asm and locate the Dynamic_HTZ label. You will be greeted by this block of code:
Dynamic_HTZ: lea (Anim_Counters).w,a3 moveq #0,d0 move.w (Camera_X_pos).w,d1 neg.w d1 asr.w #3,d1 move.w (Camera_X_pos).w,d0 lsr.w #4,d0 add.w d1,d0 subi.w #$10,d0 divu.w #$30,d0 swap d0 cmp.b 1(a3),d0 beq.s BranchTo_loc_3FE5C move.b d0,1(a3) move.w d0,d2 andi.w #7,d0 add.w d0,d0 add.w d0,d0 add.w d0,d0 move.w d0,d1 add.w d0,d0 add.w d1,d0 andi.w #$38,d2 lsr.w #2,d2 add.w d2,d0 lea word_3FD9C(pc,d0.w),a4 moveq #5,d5 move.w #tiles_to_bytes(ArtTile_ArtUnc_HTZMountains),d4
What this does is compute the position in an array (word_3FD9C) that tells the game what pieces of art need to be loaded. You want to go to this block of code:
move.w (Camera_X_pos).w,d0 lsr.w #4,d0 add.w d1,d0
and change it to this:
move.w (Camera_X_pos).w,d0 move.w d0,d2 ; Copy to d2 andi.w #$F,d2 ; Is the lower nibble zero? seq.b d2 ; If yes, set low byte of d2 to $FF ext.w d2 ; Low word of d2 = -1 lsr.w #4,d0 add.w d1,d0 ; (*) See notes add.w d2,d0 ; Shift the parallax to the correct value
When you build the game, you will see that the blue mountains no longer shift in the wrong direction.
The bug explained
The block of code from Dynamic_HTZ quoted above groups the camera position in 48 groups of 16 pixels (meaning a period of 768 pixels), in a way that maps the camera value uniquely determines the parallax value to use for the background mountains; this parallax value then feeds into a lookup table that determines which columns of tiles from the art file are to be loaded.
There is a problem in that parallax calculation, though: every time the camera position is a multiple of 16 pixels, it gives the wrong result, the end result being that d0 is 1 higher than it should after the line marked with an asterisk. This off-by-one error then propagates and becomes an off-by-one pixel error for the background mountains.
The added lines compensate for this by subtracting one whenever the camera value is divisible by 16.
|Fix Hill Top's background scrolling mountains]]