don't click here

Sonic 1 SolidObject routine

Discussion in 'Engineering & Reverse Engineering' started by Hayate, Apr 9, 2008.

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

    Hayate

    Tech Member
    I decided I needed to comment this subroutine, so I could understand it. IIRC, nothing here has been changed from the original disasm, so I figured I might as well post it before butchering it in SATSEE to allow for the whole "big Sonic can break down almost anything in his way" thing.

    Note on conventions: I use first person plural when referring to the object in a0.

    Code (ASM):
    1. ; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
    2.  
    3.  
    4. SolidObject:
    5.         tst.b   $25(a0)         ; is Sonic standing on us?
    6.         beq.w   SolidObject_CheckEnter  ; if not, branch
    7.         move.w  d1,d2           ; copy width/2
    8.         add.w   d2,d2           ; get whole width
    9.         lea ($FFFFD000).w,a1    ; get Sonic's SST
    10.         btst    #1,$22(a1)      ; is Sonic in the air?
    11.         bne.b   SolidObject_Leave   ; if so, branch
    12.         move.w  8(a1),d0        ; get Sonic's x-position
    13.         sub.w   8(a0),d0        ; subtract our x-position
    14.         add.w   d1,d0           ; add our width
    15.         bmi.b   SolidObject_Leave   ; if Sonic's to the left, branch
    16.         cmp.w   d2,d0           ; is Sonic to the right?
    17.         blt.b   SolidObject_Stand   ; if not, branch
    18.  
    19. SolidObject_Leave:
    20.         bclr    #3,$22(a1)      ; clear Sonic's standing flag
    21.         bclr    #3,$22(a0)      ; clear our being-stood-on flag
    22.         clr.b   $25(a0)         ; clear our other being-stood-on flag
    23.         moveq   #0,d4           ; return no collision
    24.         rts
    25.  
    26. SolidObject_Stand:
    27.         move.w  d4,d2
    28.         jsr MoveSonicOnPlatform
    29.         moveq   #0,d4           ; return no collision
    30.         rts
    31.  
    32. SolidObject71:
    33.         tst.b   $25(a0)
    34.         beq.w   loc_FAD0
    35.         move.w  d1,d2
    36.         add.w   d2,d2
    37.         lea ($FFFFD000).w,a1
    38.         btst    #1,$22(a1)
    39.         bne.s   loc_FA44
    40.         move.w  8(a1),d0
    41.         sub.w   8(a0),d0
    42.         add.w   d1,d0
    43.         bmi.s   loc_FA44
    44.         cmp.w   d2,d0
    45.         bcs.s   loc_FA58
    46.  
    47. loc_FA44:
    48.         bclr    #3,$22(a1)
    49.         bclr    #3,$22(a0)
    50.         clr.b   $25(a0)
    51.         moveq   #0,d4
    52.         rts
    53. ; ===========================================================================
    54.  
    55. loc_FA58:
    56.         move.w  d4,d2
    57.         jsr MoveSonicOnPlatform
    58.         moveq   #0,d4
    59.         rts
    60. ; ===========================================================================
    61.  
    62. SolidObject2F:
    63.         lea ($FFFFD000).w,a1
    64.         tst.b   1(a0)
    65.         bpl.w   SolidObject_Ignore
    66.         move.w  8(a1),d0
    67.         sub.w   8(a0),d0
    68.         add.w   d1,d0
    69.         bmi.w   SolidObject_Ignore
    70.         move.w  d1,d3
    71.         add.w   d3,d3
    72.         cmp.w   d3,d0
    73.         bhi.w   SolidObject_Ignore
    74.         move.w  d0,d5
    75.         btst    #0,1(a0)
    76.         beq.s   loc_FA94
    77.         not.w   d5
    78.         add.w   d3,d5
    79.  
    80. loc_FA94:
    81.         lsr.w   #1,d5
    82.         moveq   #0,d3
    83.         move.b  (a2,d5.w),d3
    84.         sub.b   (a2),d3
    85.         move.w  $C(a0),d5
    86.         sub.w   d3,d5
    87.         move.b  $16(a1),d3
    88.         ext.w   d3
    89.         add.w   d3,d2
    90.         move.w  $C(a1),d3
    91.         sub.w   d5,d3
    92.         addq.w  #4,d3
    93.         add.w   d2,d3
    94.         bmi.w   SolidObject_Ignore
    95.         move.w  d2,d4
    96.         add.w   d4,d4
    97.         cmp.w   d4,d3
    98.         bcc.w   SolidObject_Ignore
    99.         bra.w   loc_FB0E
    100. ; ===========================================================================
    101.  
    102. SolidObject_CheckEnter:
    103.         tst.b   1(a0)           ; should we be drawn?
    104.         bpl.w   SolidObject_Ignore  ; if not, branch
    105.  
    106. loc_FAD0:
    107.         lea ($FFFFD000).w,a1    ; get Sonic's SST
    108.         move.w  8(a1),d0        ; get Sonic's x-position
    109.         sub.w   8(a0),d0        ; subtract our x-position
    110.         add.w   d1,d0           ; add our width
    111.         bmi.w   SolidObject_Ignore  ; if Sonic's to the left, branch
    112.         move.w  d1,d3           ; copy width
    113.         add.w   d3,d3           ; double width
    114.         cmp.w   d3,d0           ; is Sonic to the right?
    115.         bhi.w   SolidObject_Ignore  ; if so, branch
    116.         move.b  $16(a1),d3      ; get Sonic's height
    117.         ext.w   d3
    118.         add.w   d3,d2           ; add to our height
    119.         move.w  $C(a1),d3       ; get Sonic's y-position
    120.         sub.w   $C(a0),d3       ; subtract our y-position
    121.         addq.w  #4,d3
    122.         add.w   d2,d3           ; add our height
    123.         bmi.w   SolidObject_Ignore  ; if Sonic's above, branch
    124.         move.w  d2,d4           ; copy height
    125.         add.w   d4,d4           ; double height
    126.         cmp.w   d4,d3           ; is Sonic below?
    127.         bge.w   SolidObject_Ignore  ; if so, branch
    128.  
    129. loc_FB0E:
    130.         tst.b   ($FFFFF7C8).w       ; are the controls locked?
    131.         bmi.w   SolidObject_Ignore  ; if so, branch
    132.         cmpi.b  #6,($FFFFD024).w    ; is Sonic dying/restarting/etc?
    133.         bge.w   SolidObject_Ignore  ; if so, branch
    134.         tst.w   ($FFFFFE08).w       ; is debug mode on?
    135.         bne.w   SolidObject_Debug   ; if so, branch
    136.         move.w  d0,d5           ; (= width+dx)
    137.         cmp.w   d0,d1           ; is Sonic to the right of the center of the object?
    138.         bcc.s   +           ; if so, branch
    139.         add.w   d1,d1
    140.         sub.w   d1,d0
    141.         move.w  d0,d5
    142.         neg.w   d5          ; (= width-dx)
    143. +       move.w  d3,d1           ; (= height+dy) ; d5 is now width+|dx|
    144.         cmp.w   d3,d2           ; is Sonic below the center of the object?
    145.         bcc.s   +           ; if so, branch
    146.         subq.w  #4,d3
    147.         sub.w   d4,d3
    148.         move.w  d3,d1
    149.         neg.w   d1          ; (=height-dy)
    150. +       cmp.w   d1,d5           ; do the 45 degree check! ; d1 is now height+|dy|
    151.         bhi.w   SolidObject_TopBottom   ; if Sonic hit the top or bottom, branch
    152.         cmpi.w  #4,d1
    153.         bls.s   SolidObject_SideAir
    154.         tst.w   d0          ; where is Sonic?
    155.         beq.s   SolidObject_Center  ; if in the center, branch
    156.         bmi.s   SolidObject_Right   ; if to the right, branch
    157.         tst.w   $10(a1)         ; is Sonic moving left?
    158.         bmi.s   SolidObject_Center  ; if so, branch
    159.         bra.s   SolidObject_Left
    160. ; ===========================================================================
    161.  
    162. SolidObject_Right:
    163.         tst.w   $10(a1)         ; is Sonic moving right?
    164.         bpl.s   SolidObject_Center  ; if so, branch
    165.  
    166. SolidObject_Left:
    167.         move.w  #0,$14(a1)      ; stop Sonic moving horizontally
    168.         move.w  #0,$10(a1)
    169.  
    170. SolidObject_Center:
    171.         sub.w   d0,8(a1)        ; correct Sonic's x-position
    172.         btst    #1,$22(a1)      ; is Sonic in the air?
    173.         bne.s   SolidObject_SideAir ; if so, branch
    174.         bset    #5,$22(a1)      ; make Sonic push the object
    175.         bset    #5,$22(a0)      ; make the object be pushed
    176.         moveq   #1,d4           ; return side collision
    177.         rts
    178. ; ===========================================================================
    179.  
    180. SolidObject_SideAir:
    181.         bsr.s   SolidObject_NotPushing
    182.         moveq   #1,d4           ; return side collision
    183.         rts
    184. ; ===========================================================================
    185.  
    186. SolidObject_Ignore:
    187.         btst    #5,$22(a0)      ; is Sonic pushing us?
    188.         beq.s   SolidObject_Debug   ; if not, branch
    189.         move.w  #1,$1C(a1)      ; use walking animation
    190.  
    191. SolidObject_NotPushing:
    192.         bclr    #5,$22(a0)      ; clear our being-pushed flag
    193.         bclr    #5,$22(a1)      ; clear Sonic's pushing flag
    194.  
    195. SolidObject_Debug:
    196.         moveq   #0,d4           ; return no collision
    197.         rts
    198. ; ===========================================================================
    199.  
    200. SolidObject_TopBottom:
    201.         tst.w   d3          ; is Sonic below the object?
    202.         bmi.s   SolidObject_Below   ; if so, branch
    203.         cmpi.w  #$10,d3         ; has Sonic landed on the object?
    204.         bcs.s   SolidObject_Land    ; if so, branch
    205.         bra.s   SolidObject_Ignore
    206. ; ===========================================================================
    207.  
    208. SolidObject_Below:
    209.         tst.w   $12(a1)         ; is Sonic moving vertically?
    210.         beq.s   loc_FBD6        ; if not, branch
    211.         bpl.s   SolidObject_TopBotmAir  ; if downwards, branch
    212.         tst.w   d3          ; is Sonic above the object?
    213.         bpl.s   SolidObject_TopBotmAir  ; if so, branch
    214.         sub.w   d3,$C(a1)       ; correct Sonic's y-position
    215.         move.w  #0,$12(a1)      ; stop Sonic moving vertically
    216.  
    217. SolidObject_TopBotmAir:
    218.         moveq   #-1,d4          ; return top/bottom collision
    219.         rts
    220. ; ===========================================================================
    221.  
    222. loc_FBD6:
    223.         btst    #1,$22(a1)      ; is Sonic in the air?
    224.         bne.s   SolidObject_TopBotmAir  ; if so, branch
    225.         move.l  a0,-(sp)        ; backup a0 to the stack
    226.         movea.l a1,a0
    227.         jsr KillSonic       ; kill Sonic, because he got squashed
    228.         movea.l (sp)+,a0        ; restore a0 from the stack
    229.         moveq   #-1,d4          ; return top/bottom collision
    230.         rts
    231. ; ===========================================================================
    232.  
    233. SolidObject_Land:
    234.         subq.w  #4,d3
    235.         moveq   #0,d1
    236.         move.b  $19(a0),d1      ; get our width
    237.         move.w  d1,d2
    238.         add.w   d2,d2
    239.         add.w   8(a1),d1        ; add our x-position
    240.         sub.w   8(a0),d1        ; subtract Sonic's x-position
    241.         bmi.s   SolidObject_Miss    ; if Sonic is to the right, branch
    242.         cmp.w   d2,d1           ; is Sonic to the left?
    243.         bcc.s   SolidObject_Miss    ; if so, branch
    244.         tst.w   $12(a1)         ; is Sonic moving upwards?
    245.         bmi.s   SolidObject_Miss    ; if so, branch
    246.         sub.w   d3,$C(a1)       ; fix Sonic's y-position
    247.         subq.w  #1,$C(a1)
    248.         bsr.s   SolidObject_ResetFloor
    249.         move.b  #2,$25(a0)      ; set our other being-stood-on flag
    250.         bset    #3,$22(a0)      ; set our being-stood-on flag
    251.         moveq   #-1,d4          ; return top/bottom collision
    252.         rts
    253. ; ===========================================================================
    254.  
    255. SolidObject_Miss:
    256.         moveq   #0,d4           ; return no collision
    257.         rts
    258. ; End of function SolidObject
    259.  
    260.  
    261. ; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
    262.  
    263.  
    264. SolidObject_ResetFloor:
    265.         btst    #3,$22(a1)      ; is Sonic standing on something?
    266.         beq.s   +           ; if not, branch
    267.         moveq   #0,d0
    268.         move.b  $3D(a1),d0      ; get the object being stood on
    269.         lsl.w   #6,d0
    270.         addi.l  #$FFD000,d0
    271.         movea.l d0,a2
    272.         bclr    #3,$22(a2)      ; clear its being-stood-on flag
    273.         clr.b   $25(a2)         ; clear its other being-stood-on flag
    274. +       move.w  a0,d0
    275.         subi.w  #-$3000,d0
    276.         lsr.w   #6,d0
    277.         andi.w  #$7F,d0
    278.         move.b  d0,$3D(a1)      ; set the object being stood on
    279.         move.b  #0,$26(a1)      ; clear Sonic's angle
    280.         move.w  #0,$12(a1)      ; stop Sonic moving vertically
    281.         move.w  $10(a1),$14(a1)     ; copy Sonic's horizontal speed to his inertia
    282.         btst    #1,$22(a1)      ; is Sonic in the air?
    283.         beq.s   +           ; if not, branch
    284.         move.l  a0,-(sp)        ; backup a0 to the stack
    285.         movea.l a1,a0
    286.         jsr Sonic_ResetOnFloor  ; call the ordinary floor reset subroutine
    287.         movea.l (sp)+,a0        ; restore a0 from the stack
    288. +       bset    #3,$22(a1)      ; set our being-stood-on flag
    289.         bset    #3,$22(a0)      ; set Sonic's standing flag
    290.         rts
    291. ; End of function SolidObject_ResetFloor
    292.  
     
  2. Hivebrain

    Hivebrain

    Administrator
    3,048
    160
    43
    53.4N, 1.5W
    Github
    Brilliant, this saves me some time. :)
     
Thread Status:
Not open for further replies.