Load Collision Data from ROM in Sonic 2

Discussion in 'Tutorials Archive' started by Devon, Oct 14, 2014.

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

    Devon I'm a loser, baby, so why don't you kill me? Member

    Joined:
    Aug 26, 2013
    Messages:
    1,377
    Location:
    your mom
    In Sonic 2, there are a couple variables entitled "Primary_Collision" and "Secondary_Collision" and both of them each take up $300 bytes of RAM.

    In this tutorial, we'll load the collision data from ROM, so we can save $600 bytes of RAM.

    Note that this guide is for the Xenowhirl disassembly. It should also apply to the GitHub one as well.

    First this you have to do is decompress your collision files from Kosinski, or it won't work!

    Next, what we are going to do is revamp "LoadCollisionIndexes". When you search for that label, you should see this:


    ; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||

    ; sub_49BC:
    LoadCollisionIndexes:
    moveq #0,d0
    move.b (Current_Zone).w,d0
    lsl.w #2,d0
    move.l #Primary_Collision,(Collision_addr).w
    move.w d0,-(sp)
    movea.l Off_ColP(pc,d0.w),a0
    lea (Primary_Collision).w,a1
    bsr.w KosDec
    move.w (sp)+,d0
    movea.l Off_ColS(pc,d0.w),a0
    lea (Secondary_Collision).w,a1
    bra.w KosDec

    Replace is with:


    ; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||

    LoadCollisionData:
        moveq    #0,d4
        moveq    #0,d5
        move.b    (Current_Zone).w,d5
        lsl.w    #2,d5
        lea    (Off_ColP).l,a2
        adda.l    d5,a2
        move.l    (a2),d4
        lea    (Off_ColS).l,a2
        adda.l    d5,a2
        move.l    (a2),d5
        rts

    ; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||

    ; sub_49BC:
    LoadCollisionIndexes:
    bsr.s LoadCollisionData
    move.l d4,(Collision_addr).w
    rts

    You will notice that I split the routine into 2. This will be improtant for this next step.

    Now, search for:


    #Primary_Collision

    For each result you get, you should see something like:


    move.l #Primary_Collision,(Collision_addr).w
    cmpi.b #$C,layer(a0)
    beq.s +
    move.l #Secondary_Collision,(Collision_addr).w
    +

    Note that a couple results will be slightly different, but you should still apply the changes.

    What you do is first add this line above the code above:


    jsr LoadCollisionData

    And then replace each:


    #Primary_Collision

    With a:


    d4

    And each:


    #Secondary_Collision

    With a:


    d5

    Do this for each result.

    There! Now you can remove the "Primary_Collision" and "Secondary_Collision" variables. Wasn't that pretty simple? Enjoy your free $600 bytes of RAM!
     
    Last edited by a moderator: Oct 19, 2014
  2. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    I don't see any mention of the collision files needing to be decompressed from Kosinski. That is actually the main purpose of this: how to make S2 read collision indexes from ROM.


    You can, with a little work, continue using two separate tables; and you should be able to eliminate the need for those two RAM addresses entirely. You just need to use LoadCollisionIndexes' calculations instead of reading the addresses in RAM.
     
  3. ThomasThePencil

    ThomasThePencil resident psycho Member

    Joined:
    Jan 29, 2013
    Messages:
    910
    Location:
    the united states. where else?
    This particular bit does not seem logical to me. Each act has a ColP entry and a ColS entry, but If this table alone is implemented without expanding the table to work with 2 acts (or however many acts the system you have supports), then you can most likely crash the game (see: CPZ).

    Two fixes for this:

    1. Expand the table as I previously mentioned, or:
    2. Use only Current_Zone as opposed to Current_ZoneAndAct.
    That aside, this is a very useful tutorial because it lets you free up a shitload of RAM (so much, in fact, that the resulting $600 bytes of free space could fit the S2 Clone Driver. either version, and still have $40 or so bytes left over =O ). Seems very useful if someone's trying to port Moduled Kosinski from S3K (see: title cards).
     
    Last edited by a moderator: Oct 15, 2014
  4. redhotsonic

    redhotsonic Also known as RHS Member

    Joined:
    Aug 10, 2007
    Messages:
    2,969
    Location:
    England
    I swear someone already made a guide of this somewhere...
     
  5. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
  6. Devon

    Devon I'm a loser, baby, so why don't you kill me? Member

    Joined:
    Aug 26, 2013
    Messages:
    1,377
    Location:
    your mom
    This guide already exists? I didn't know that. What should I do now?
     
    Last edited by a moderator: Oct 15, 2014
  7. rika_chou

    rika_chou Adopt Member

    Joined:
    Aug 11, 2007
    Messages:
    689
    It only exists in a place most people cannot access. So more people will be able to see and use your guide.
     
  8. Devon

    Devon I'm a loser, baby, so why don't you kill me? Member

    Joined:
    Aug 26, 2013
    Messages:
    1,377
    Location:
    your mom
    Alright, I fixed the guide, so it now works. Now you don't need to combine the collision tables.
     
  9. DanielHall

    DanielHall Well-Known Member Member

    Joined:
    Jan 18, 2010
    Messages:
    860
    Location:
    North Wales
    I'd do nothing, assuming the guide works. It's your own work, and it isn't exactly redundant, as not many people can see the other.
     
  10. Devon

    Devon I'm a loser, baby, so why don't you kill me? Member

    Joined:
    Aug 26, 2013
    Messages:
    1,377
    Location:
    your mom
    Guide updated! No longer need to use Primary_collision_addr and Secondary_collision_addr. Now $600 bytes are saved instead of $5F8.

    Also:

    The thing is, those RAM addresses are exactly the same ($FFFFFE10), so there would be no point in changing them. Moving #$101 into Current_Zone won't be any different than moving #$101 into Current_ZoneAndAct.
     
    Last edited by a moderator: Oct 16, 2014
  11. JoenickROS

    JoenickROS ROS (bug fixing in progress) Member

    Joined:
    Feb 5, 2012
    Messages:
    929
    Question, did you test it on walls, it messes up wall collision, I triple checked the guide, and I don't see any errors I could have made.
     
    Last edited by a moderator: Oct 16, 2014
  12. redhotsonic

    redhotsonic Also known as RHS Member

    Joined:
    Aug 10, 2007
    Messages:
    2,969
    Location:
    England
    I knew it. And I was sure it was Alriightyman that did it, but I couldn't find it anywhere, making me think I went insane (don't comment on that =P)
     
  13. Devon

    Devon I'm a loser, baby, so why don't you kill me? Member

    Joined:
    Aug 26, 2013
    Messages:
    1,377
    Location:
    your mom
    Hm...just checked it again. Yeah, it messes up walls. I'll see into it.

    EDIT: Done! It turns out that LoadCollisionData was messing with a couple data registers. Changed them so it would now work.
     
    Last edited by a moderator: Oct 16, 2014
  14. JoenickROS

    JoenickROS ROS (bug fixing in progress) Member

    Joined:
    Feb 5, 2012
    Messages:
    929
    Well the collision is all good, but it seems the path swappers are messed up now.

    edit: Trash, forgot to change a a1 oops, sorry dude. Guess that's what I get for trying to change it manually lol

    edit2: Works great!
     
    Last edited by a moderator: Oct 16, 2014
  15. Pokepunch

    Pokepunch That guy who posts on occasion Member

    Joined:
    Aug 7, 2009
    Messages:
    270
    Location:
    UK
    After following this guide it seems Knuckles can't climb up walls anymore. He just clips into the wall then gets thrown out when you move. 
     
  16. Clownacy

    Clownacy Retired Staff lolololo Member

    Joined:
    Aug 15, 2014
    Messages:
    1,020
    You're sure you did this for the Knuckles object? Namely Knuckles_Climbing_Wall?
     
  17. Devon

    Devon I'm a loser, baby, so why don't you kill me? Member

    Joined:
    Aug 26, 2013
    Messages:
    1,377
    Location:
    your mom
    I know the problem. Knuckles doesn't use Primary_Collision and Secondary_Collision, but their respective RAM addresses.

    What you do is search $FFFFD600 and apply the changes above for each result.

    Primary_Collision = $FFFFD600

    Secondary_Collision = $FFFFD900

    So when replacing the variables with d4 and d5, you're replacing $FFFD600 and $FFFFD900.
     
    Last edited by a moderator: Oct 19, 2014
  18. Pokepunch

    Pokepunch That guy who posts on occasion Member

    Joined:
    Aug 7, 2009
    Messages:
    270
    Location:
    UK
    Now I feel stupid... Nevermind. 
     
Thread Status:
Not open for further replies.