Monday, June 12, 2017

Object and ring layouts

Data for object and ring placement is read directly from ROM as you move through a level. Objects and rings are kept in separate layouts, with the format for ring layouts being fairly straightforward: each ring definition is composed of two words, which correspond to the ring's horizontal (X) and vertical (Y) coordinates within the level.

Object layouts are slightly more complex. Some additional flags are stored in the upper bits of the Y coordinate, such as horizontal and vertical flip: this is possible because there's a maximum level height of $1000 pixels. Two extra bytes define which object to spawn at those coordinates, which correspond to the object's ID and its subtype.

The game only loads objects inside the same "vertical slice" of the level as the player. To that effect, object definitions appear ordered by their X coordinate, so that parsing of the layout can stop as soon as the first out-of-range object is encountered. An additional optimization in Sonic 3 prevents objects from being allocated until they're also in the same "horizontal slice" as the player; this behavior can be disabled by setting the high bit on the Y coordinate.

The ID byte is used as an index to a list of pointers to the actual object code. Sonic 3 & Knuckles actually has two of these lists, one for the Sonic 3 levels and another for the Sonic & Knuckles levels. Competition stages, bonus stages and Flying Battery Zone all use the Sonic 3 list.
Sprite_Listing3:
    dc.l    Obj_Ring                    ; 0
    dc.l    Obj_Monitor                 ; 1
    dc.l    Obj_PathSwap                ; 2
    dc.l    Obj_AIZHollowTree           ; 3
    dc.l    Obj_CollapsingPlatform      ; 4
    dc.l    Obj_AIZLRZEMZRock           ; 5
    dc.l    Obj_AIZRideVine             ; 6
    dc.l    Obj_Spring                  ; 7
    dc.l    Obj_Spikes                  ; 8
    ...
When the object comes into range, its code pointer, X/Y coordinates, horizontal/vertical flip flags and subtype are all copied to a data structure in RAM, the sprite status table. From there, the object is ready to start fending for itself.

Next, we'll begin our look at the conventions of Sonic 3 object programming.

No comments:

Post a Comment