Actions

SCHG How-to

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.

SCHG How-To Guide: Sonic the Hedgehog 2 (16-bit)
Fixing Bugs
Fix Demo Playback | Fix a Race Condition with Pattern Load Cues | Fix Super Sonic Bugs | Use Correct Height When Roll Jumping | Fix Jump Height Bug When Exiting Water | Fix Spin Dash Code and Add Spin Dash Speeds | Fix Screen Boundary Spin Dash Bug | Correct Drowning Bugs | Fix Camera Y Position for Tails | Fix Tails Subanimation Error | Fix Tails' Respawn Speeds | Fix Accidental Deletion of Scattered Rings | Fix Ring Timers | Fix Rexon Crash | Fix Monitor Collision Bug | Fix EHZ Deformation Bug | Correct CPZ Boss Attack Behavior | Fix Bug in ARZ Boss Arrow's Platform Behavior | Fix ARZ Boss Walking on Air Glitch | Fix ARZ Boss Sprite Behavior | Fix Multiple CNZ Boss Bugs | Fix HTZ Background Scrolling Mountains | Fix OOZ Launcher Speed Up Glitch | Fix DEZ Giant Mech Collision Glitch | Fix Boss Deconstruction Behavior | Fix Speed Bugs
Design Choices
Remove the Air Speed Cap | Disable Floor Collision While Dying | Modify Super Sonic Transformation Methods & Behavior | Enable/Disable Tails in Certain Levels | Collide with Water After Being Hurt | Retain Rings When Returning at a Star Post | Improve the Fade In\Fade Out Progression Routines | Fix Scattered Rings' Underwater Physics | Insert LZ Water Ripple Effect | Restore Lost CPZ Boss Feature | Prevent SCZ Tornado Spin Dash Death | Improve ObjectMove Subroutines | Port S3K Rings Manager | Port S3K Object Manager | Port S3K Priority Manager | Edit Level Order with ASM‎ | Alter Ring Requirements in Special Stages | Make Special Stage Characters Use Normal DPLCs | Speed Up Ring Loss Process | Add beta spindash to Sonic 2 | Change spike behaviour in Sonic 2
Adding Features
Create Insta-kill and High Jump Monitors | Create Clone and Special Stage Monitors | Port Knuckles
Sound Features
Port Sonic 1 Sound Driver | Port Sonic 2 Clone Driver | Port Sonic 3 Sound Driver | Expand the Music Index to Start at $00 (Sonic 2 Clone Driver Version)
Extending the Game
Extend the Level Index Past $10 | Extend the Level Select | Extend Water Tables | Add Extra Characters | Free Up 2 Universal SSTs