pret / pokecrystal

Disassembly of Pokémon Crystal
https://pret.github.io/pokecrystal/
2.07k stars 778 forks source link

Further define relationship between the `map_object` struct and `object_events` #1031

Closed vulcandth closed 1 year ago

vulcandth commented 1 year ago

The relationship between the map_object struct and object_event's should be further defined.

object_event's are loaded into the map_object struct by means of CopyMapObjectEvents. There is a clear and corresponding relationship between these two. The only items in the map_object struct that isn't copied from the Map's object_event's is MAPOBJECT_OBJECT_STRUCT_ID which is initially set to -1 until the object appears on screen (_in which case it is then loaded with the index of the corresponding object_struct_), and the two extra bytes at the end of the struct represented by the rb_skip 2.

https://github.com/pret/pokecrystal/blob/c01409be5a9930d6f0687ce53c1c898e1855c884/constants/map_object_constants.asm#L99-L118

https://github.com/pret/pokecrystal/blob/c01409be5a9930d6f0687ce53c1c898e1855c884/macros/scripts/maps.asm#L105-L132

https://github.com/pret/pokecrystal/blob/c01409be5a9930d6f0687ce53c1c898e1855c884/home/map.asm#L610-L634

https://github.com/pret/pokecrystal/blob/41d5ea0482f4ef577df600d2d8b5cad70f74a396/constants/script_constants.asm#L115

Recommendation: OBJECT_EVENT_SIZE should be defined by the map_object struct. Example:

 ; map_object struct members (see macros/ram.asm)
 rsreset
 DEF MAPOBJECT_OBJECT_STRUCT_ID rb ; 0
 DEF MAPOBJECT_SPRITE           rb ; 1
 DEF MAPOBJECT_Y_COORD          rb ; 2
 DEF MAPOBJECT_X_COORD          rb ; 3
 DEF MAPOBJECT_MOVEMENT         rb ; 4
 DEF MAPOBJECT_RADIUS           rb ; 5
 DEF MAPOBJECT_HOUR_1           rb ; 6
 DEF MAPOBJECT_HOUR_2           rb ; 7
 rsset MAPOBJECT_HOUR_2
 DEF MAPOBJECT_TIMEOFDAY        rb ; 7
 DEF MAPOBJECT_PALETTE          rb ; 8
 rsset MAPOBJECT_PALETTE
 DEF MAPOBJECT_TYPE             rb ; 8
 DEF MAPOBJECT_SIGHT_RANGE      rb ; 9
 DEF MAPOBJECT_SCRIPT_POINTER   rw ; a
 DEF MAPOBJECT_EVENT_FLAG       rw ; c
+DEF OBJECT_EVENT_SIZE EQU _RS - 1 ; object_event's begin with MAPOBJECT_SPRITE
                                rb_skip 2
 DEF MAPOBJECT_LENGTH EQU _RS

EDIT: To re-iterate the importance of this, you can't reorder/add/remove items from the object_event macro without also modifying the map_object struct.