don't click here

Walking function in sonic 2

Discussion in 'Engineering & Reverse Engineering' started by snkenjoi, May 30, 2007.

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

    snkenjoi

    Tech Member
    89
    7
    8
    I'm trying to add a walking function to sonic 2, so that when A is held, the variable for speed is changed to a lower value to effectivly make sonic walk, and when you let go of A, the value is reset. I've already disabled A from jumping.

    The only problem I have is with the joypad support for it, I've commented out my current 'solution' which got A to enable walking and up to disable it. As you can imagine, this isn't exacly the best way to go about it.

    Code (Text):
    1. loc_1A2B8:
    2.         bsr.w   loc_1AC3E
    3.         bsr.w   loc_1AA38
    4.         bsr.w   loc_1AD96
    5.         bsr.w   loc_1A35A
    6.         bsr.w   loc_1A9D2
    7.         bsr.w   loc_1A974
    8.     ;cmpi.b #$40,($FFFFF603).w; check if A is pressed
    9.     ;beq.s  walk
    10.     ;cmpi.b #$1,($FFFFF603).w; check if up is pressed
    11.     ;beq.s  run
    12.         jsr sub_163AC
    13.         bsr.w   loc_1E234
    14.         bsr.w   loc_1AE08
    15.  
    16. locret_1A2DE:
    17.         rts
    18.  
    19. walk:
    20.         move.w  #$400,(Sonic_top_speed).w; new speed
    21.         cmpi.w  #0,$14(a0); check sonic is moving
    22.         beq.s   nwalk
    23.         btst    #0,$22(a0); check direction
    24.         bne.s   walkl
    25.         move.w  #$300,$14(a0); right; if sonic is running, slow him down
    26.         rts
    27.  
    28. walkl:
    29.         move.w  #-$300,$14(a0); left
    30.         rts
    31.  
    32. nwalk:
    33.         rts
    34.  
    35. run:
    36.         move.w  #$600,(Sonic_top_speed).w ; reset speed
    37.     ;walkwalkwalk
    38.         rts
     
  2. Puto

    Puto

    Shin'ichi Kudō, detective. Tech Member
    2,013
    0
    16
    Portugal, Oeiras
    Part of Team Megamix, but haven't done any actual work in ages.
    Try something like this:

    Code (Text):
    1. loc_1A2B8:
    2.         bsr.w   loc_1AC3E
    3.         bsr.w   loc_1AA38
    4.         bsr.w   loc_1AD96
    5.         bsr.w   loc_1A35A
    6.         bsr.w   loc_1A9D2
    7.         bsr.w   loc_1A974
    8.         cmpi.b #$40,($FFFFF603).w; check if A is pressed
    9.         beq.s   walk
    10.         bra.s    run
    11.    ;cmpi.b #$1,($FFFFF603).w; check if up is pressed
    12.    ;beq.s   run
    13.         jsr sub_163AC
    14.         bsr.w   loc_1E234
    15.         bsr.w   loc_1AE08
    16.  
    17. locret_1A2DE:
    18.         rts
    19.  
    20. walk:
    21.         move.w  #$400,(Sonic_top_speed).w; new speed
    22.         cmpi.w  #0,$14(a0); check sonic is moving
    23.         beq.s   nwalk
    24.         btst    #0,$22(a0); check direction
    25.         bne.s   walkl
    26.         move.w  #$300,$14(a0); right; if sonic is running, slow him down
    27.         rts
    28.  
    29. walkl:
    30.         move.w  #-$300,$14(a0); left
    31.         rts
    32.  
    33. nwalk:
    34.         rts
    35.  
    36. run:
    37.         move.w  #$600,(Sonic_top_speed).w; reset speed
    38.    ;walkwalkwalk
    39.         rts
     
  3. snkenjoi

    snkenjoi

    Tech Member
    89
    7
    8
    Hmm, that doesn't work, sonic stays rooted to the spot.

    Also, shouldn't ($FFFFF602).w be used instead?
     
  4. Tweaker

    Tweaker

    Banned
    12,387
    3
    0
    Try using $FFFFF605 - it's specifically for buttons being held down.

    You're probably also better off doing a btst or something, since you're working with a single buttonpress.
     
  5. snkenjoi

    snkenjoi

    Tech Member
    89
    7
    8
    Took me a while, but I got it working.

    Now, sonic walks when you hold A :(

    Code (Text):
    1. loc_1A2B8:
    2.         bsr.w   loc_1AC3E
    3.         bsr.w   loc_1AA38
    4.         bsr.w   loc_1AD96
    5.         bsr.w   loc_1A35A
    6.         bsr.w   loc_1A9D2
    7.         bsr.w   loc_1A974
    8.         btst #6,($FFFFF605).w; check if A is pressed
    9.         bne.s   walk
    10.         bsr.w run
    11.         jsr sub_163AC
    12.         bsr.w   loc_1E234
    13.         bsr.w   loc_1AE08
    14.  
    15. locret_1A2DE:
    16.         rts
    17.  
    18. walk:
    19.         move.w  #$400,(Sonic_top_speed).w; slow him down
    20.         cmpi.w  #0,$14(a0); check sonic is moving
    21.         beq.s   nwalk
    22.         btst    #0,$22(a0); check direction
    23.         bne.s   walkl
    24.         move.w  #$300,$14(a0); right
    25.         rts
    26.  
    27. walkl:
    28.         move.w  #-$300,$14(a0); left
    29. nwalk:
    30.         rts
    31.  
    32. run:
    33.         btst #6,($FFFFF602).w; check if A is still held
    34.         bne.s   rwalk; if so, don't reset the speed
    35.         move.w  #$600,(Sonic_top_speed).w;reset speed
    36. rwalk:
    37.         rts
    38. ; End of subroutine loc_1A26E
    To disable A from jumping, replace;

    Code (Text):
    1. andi.b  #$70,d0
    with;

    Code (Text):
    1. andi.b  #$30,d0
    This works, because;

    A = $40
    B = $10
    C = $20

    A + B + C = $70

    B + C = $30

    Thanks to upthorn for that little gem.
     
  6. Ritz

    Ritz

    Subhedgehog Member
    4,094
    118
    43
    What use would anyone have for that?
     
  7. snkenjoi

    snkenjoi

    Tech Member
    89
    7
    8
    Well, if sonic's speed is modified to $F00 or similar, it makes it quite hard to do certain things with precision.

    I'm going to do that in my hack, so I found it useful.
     
  8. Upthorn

    Upthorn

    TAS Tech Member
    239
    0
    0
    Actually, $FFFFF603 is also specifically for buttons being held down, but it only updates when the main game is running, and controlls aren't locked out (IE: game is paused, a cutscene is running, or a signpost is forcing you offscreen).
    Actually, the guide tweaker got his info from had $F605 and $F604 reversed. (also F603 and F602, and F607 and F606)
    F605 is for buttons pressed new, F604 is for buttons held down. F602 is F604, but only updates when the main game is running, and controls aren't locked out. F603 is F605, but only updates when the main game is running, and controls aren't locked out.
     
  9. Aurochs

    Aurochs

    Единый, могучий Советский Союз! Tech Member
    2,343
    0
    0
    Whatever catches my fancy
    Damnit, I've had those addresses wrong for over a year. They're wrong in the disassembly now too. =(
     
Thread Status:
Not open for further replies.