How to Add a Second Digit to the Lifecounter in Sonic 2 SMS

Discussion in 'Approved' started by Ravenfreak, Oct 27, 2011.

Thread Status:
Not open for further replies.
  1. Ravenfreak

    Ravenfreak Incineroooaaarr! Member

    Joined:
    Feb 10, 2010
    Messages:
    360
    Location:
    O'Fallon, MO
    The 8-bit games need more love. :V Anyway copy-paste from SR:


    Alright I always hated that you couldn't have more than 9 lives in Sonic 2, so I decided to change that. I didn't use the updated version on


    the SVN, this is from the wiki version of the disassembly. First look for CapLifeCounterValue in S2.asm. It should look like this:



    Code:
    LABEL_25AC:
    
    	ld	a, ($D292)
    
    	or	a
    
    	ret	nz
    
    	ld	a, (LifeCounter)
    
    	cp	$09 ;cap the life display at 9
    
    	jr	c, +
    
    	ld	a, $09
    
    +:	rlca
    
    	and	$1E
    
    	add	a, $10
    
    	ld	($DBA9), a
    
    	ret


    Now, delete every line after ret nz and add this:





    Code:
    
    	ld	a, (LifeCounter)
    
    	and	$0F
    
    	rlca
    
    	and	$1E
    
    	add	a, $10
    
    	ld	($DBA9), a
    
    	ret


    This sets up the last nibble of the life counter. This is what your code should look like at this:





    Code:
    	
    
    	ld	a, ($D292)
    
    	or	a
    
    	ret	nz
    
    	ld	a, ($D292)
    
    	or	a
    
    	ret	nz
    
    	ld	a, (LifeCounter)
    
    	and	$0F
    
    	rlca
    
    	and	$1E
    
    	add	a, $10
    
    	ld	($DBA9), a


    Alright time for a brief explination, the rlca operand allows the display to be updated, while the other parts of the code updates the display.

    We need to change it to where it can set another nibble for the counter. Right after "ld ($DBA9), a" add this:





    Code:
    
    	ld	a, (LifeCounter)
    
    	and	$F0
    
    	rrca
    
    	rrca
    
    	rrca
    
    	and	$1E
    
    	add	a, $10
    
    	ld	($DBAF), a
    
    	ret


    This will allow the "x" on the counter to become the other nibble. That does allow the counter to display the proper numbers, but when Sonic gains

    a life after 9, it still displays the wrong value in the wrong nibble. So we need to change that.

    Find Collision_Monitor_Life in the source code, it should look like this:





    Code:
    	
    
    	res	1, (hl)
    
    	ld	a, ($D298)
    
    	inc	a
    
    	ld	(LifeCounter), a
    
    	ld	a, SFX_ExtraLife
    
    	ld	($DD04), a
    
    	jp	LABEL_25AC


    First, bellow res 1,(hl) add these lines:





    Code:
    	
    
    	ld	a, (LifeCounter)
    
    	cp	$99
    
    	ret	z


    This will allow the game to cap the life counter after 99 lives. Next we need to change these lines:





    Code:
    		
    
    	ld	a, ($D298)
    
    	inc	a
    
    	ld	(LifeCounter), a


    To this:





    Code:
    	
    
    	ld	a, (LifeCounter)
    
    	add	a, $01
    
    	daa	(LifeCounter),a


    The "daa" operand is used for counters, which is why the "inc" operand wouldn't work here. After that, save build and enjoy.

    This is what the routine looks like after everything is added:





    Code:
    	
    
    CapLifeCounterValue:
    
    LABEL_25AC:
    
    	ld	a, ($D292)
    
    	or	a
    
    	ret	nz
    
    	ld	a, (LifeCounter)
    
    	and	$0F
    
    	rlca
    
    	and	$1E
    
    	add	a, $10
    
    	ld	($DBA9), a
    
    	ld	a, (LifeCounter)
    
    	and	$F0
    
    	rrca
    
    	rrca
    
    	rrca
    
    	and	$1E
    
    	add	a, $10
    
    	ld	($DBAF), a
    
    	ret
    
    
    
    Collision_Monitor_Life:
    
    	res	1, (hl)
    
    	ld	a, (LifeCounter)
    
    	cp	$99
    
    	ret	z
    
    	add	a, $01
    
    	daa
    
    	ld	(LifeCounter), a
    
    	ld	a, SFX_ExtraLife
    
    	ld	($DD04), a
    
    	jp	LABEL_25AC
     
    Last edited by a moderator: Oct 27, 2011
  2. Selbi

    Selbi Plug in, baby. Retired Staff

    Joined:
    Jul 20, 2008
    Messages:
    2,394
    Location:
    Northern Germany
    Please use code boxes in the future. Added them for you.


    Apart from that, awesome work. :)
     
  3. Ravenfreak

    Ravenfreak Incineroooaaarr! Member

    Joined:
    Feb 10, 2010
    Messages:
    360
    Location:
    O'Fallon, MO
    Oops sorry Selbi, i'll remember that in the future. ^^' And thank you. :)
     
Thread Status:
Not open for further replies.