1- /* Fan-out events: the framework installs the anchors and poll threads, and every callback
2- * registered for an event runs, higher priority first (mhfu_hook_event in hooks.h). */
1+ /* Events: one registry for every event the framework raises. A callback belongs to an owner,
2+ * the mod id, and the framework drops an owner's callbacks when that mod shuts down. Higher
3+ * priority runs first, ties in registration order. An event's hook is installed for its
4+ * first subscriber; a code patch then lands at the next title or menu screen. */
35#ifndef MHFU_EVENTS_H
46#define MHFU_EVENTS_H
57
68#include <stdint.h>
79#include "types.h"
10+ #include "hooks.h"
811
912#ifdef __cplusplus
1013extern "C" {
1114#endif
1215
1316typedef enum {
14- MHFU_EVENT_QUEST_BEGINNING = 0 , /* the player commits to a quest */
15- MHFU_EVENT_QUEST_ENTERED = 1 , /* area index becomes 98, the snowy-mountains camp */
16- MHFU_EVENT_MAP_SECTION_ENTERED = 2 , /* the area index changed */
17- MHFU_EVENT_MONSTER_SPAWNED = 3 , /* a new entity in registry slot 1 or above */
18- MHFU_EVENT_QUEST_TARGETS_BUILDING = 4 , /* edit the quest's monster list here (quest.h) */
17+ MHFU_EVENT_QUEST_BEGINNING , /* the player commits to a quest */
18+ MHFU_EVENT_QUEST_ENTERED , /* the first area after QUEST_BEGINNING */
19+ MHFU_EVENT_MAP_SECTION_ENTERED , /* the area index changed */
20+ MHFU_EVENT_MONSTER_SPAWNED , /* a new entity in registry slot 1 or above */
21+ MHFU_EVENT_QUEST_TARGETS_BUILDING , /* edit the quest's monster list here (quest.h) */
22+ /* big monsters, ai.h */
23+ MHFU_EVENT_AI_OVERLAY_LOADED ,
24+ MHFU_EVENT_BIGMONSTER_SLOT_PICKED ,
25+ MHFU_EVENT_BIGMONSTER_ACTION_INPUT ,
26+ MHFU_EVENT_BIGMONSTER_ACTION_DECIDED ,
27+ MHFU_EVENT_BIGMONSTER_AI_STEP ,
28+ MHFU_EVENT_BIGMONSTER_ACTION ,
29+ MHFU_EVENT_BIGMONSTER_SPAWN ,
30+ MHFU_EVENT_BIGMONSTER_DEATH ,
31+ MHFU_EVENT_BIGMONSTER_DAMAGED ,
1932
2033 MHFU_EVENT_COUNT_
2134} mhfu_event_id_t ;
2235
23- /* Quest events: the registers at the anchor instruction. */
36+ /* A callback of any event, stored untyped; the event calls it through its own type. */
37+ typedef void (* mhfu_event_fn_t )(void );
38+
39+ /* BADARG on a bad id, callback or owner; NOSPACE when the event is full; CONFLICT when its
40+ * hook cannot be installed. The same owner and callback again is OK and adds nothing. The
41+ * owner string is kept, not copied: a mod id or another literal. */
42+ mhfu_hook_rc_t mhfu_event_on (mhfu_event_id_t id , mhfu_event_fn_t cb , int priority ,
43+ const char * owner );
44+
45+ /* Drop every callback owner registered; the framework calls it on mod shutdown. */
46+ void mhfu_event_release (const char * owner );
47+
48+ /* QUEST_BEGINNING and QUEST_ENTERED: the registers at the anchor instruction. */
2449typedef struct {
2550 mhfu_event_id_t event_id ;
2651 uint32_t cell_value ;
@@ -42,45 +67,44 @@ typedef struct {
4267
4368typedef struct {
4469 mhfu_event_id_t event_id ;
45- int slot ; /* registry slot, 1..20 */
70+ int slot ; /* registry slot, 1..MHFU_ENTITY_REGISTRY_COUNT-1 */
4671 uint32_t entity_ptr ;
4772 uint8_t monster_type ; /* ENTITY.SPECIES */
4873 uint8_t entity_id ; /* ENTITY.ID */
4974 uint16_t hp ; /* ENTITY.HP */
5075 float size_scale ; /* ENTITY.SIZE_SCALE */
5176} mhfu_monster_spawn_ctx_t ;
5277
53- /* Context of MHFU_EVENT_QUEST_TARGETS_BUILDING. */
5478typedef struct {
5579 mhfu_event_id_t event_id ;
5680 mhfu_quest_t quest ;
5781} mhfu_quest_ctx_t ;
5882
59- /* The registry stores this; the typed aliases below check signatures at the caller. */
60- typedef void (* mhfu_event_cb_t )(const void * ctx );
83+ typedef void (* mhfu_quest_cb_t ) (const mhfu_event_ctx_t * ctx );
84+ typedef void (* mhfu_map_section_cb_t ) (const mhfu_map_section_ctx_t * ctx );
85+ typedef void (* mhfu_monster_spawn_cb_t ) (const mhfu_monster_spawn_ctx_t * ctx );
86+ typedef void (* mhfu_quest_targets_cb_t ) (const mhfu_quest_ctx_t * ctx );
6187
62- typedef void (* mhfu_quest_cb_t ) (const mhfu_event_ctx_t * ctx );
63- typedef void (* mhfu_map_section_cb_t ) (const mhfu_map_section_ctx_t * ctx );
64- typedef void (* mhfu_monster_spawn_cb_t )(const mhfu_monster_spawn_ctx_t * ctx );
65-
66- /* Priority 0. Returns 0, or negative on a bad id or a full event. */
67- int mhfu_event_register (mhfu_event_id_t event_id , mhfu_event_cb_t cb );
68-
69- static inline int mhfu_on_quest_beginning (mhfu_quest_cb_t cb ) {
70- return mhfu_event_register (MHFU_EVENT_QUEST_BEGINNING ,
71- (mhfu_event_cb_t )(void * )cb );
88+ static inline mhfu_hook_rc_t mhfu_on_quest_beginning (mhfu_quest_cb_t cb , int priority ,
89+ const char * owner ) {
90+ return mhfu_event_on (MHFU_EVENT_QUEST_BEGINNING , (mhfu_event_fn_t )cb , priority , owner );
91+ }
92+ static inline mhfu_hook_rc_t mhfu_on_quest_entered (mhfu_quest_cb_t cb , int priority ,
93+ const char * owner ) {
94+ return mhfu_event_on (MHFU_EVENT_QUEST_ENTERED , (mhfu_event_fn_t )cb , priority , owner );
7295}
73- static inline int mhfu_on_quest_entered ( mhfu_quest_cb_t cb ) {
74- return mhfu_event_register ( MHFU_EVENT_QUEST_ENTERED ,
75- ( mhfu_event_cb_t )( void * ) cb );
96+ static inline mhfu_hook_rc_t mhfu_on_map_section_entered ( mhfu_map_section_cb_t cb ,
97+ int priority , const char * owner ) {
98+ return mhfu_event_on ( MHFU_EVENT_MAP_SECTION_ENTERED , ( mhfu_event_fn_t ) cb , priority , owner );
7699}
77- static inline int mhfu_on_map_section_entered ( mhfu_map_section_cb_t cb ) {
78- return mhfu_event_register ( MHFU_EVENT_MAP_SECTION_ENTERED ,
79- ( mhfu_event_cb_t )( void * ) cb );
100+ static inline mhfu_hook_rc_t mhfu_on_monster_spawned ( mhfu_monster_spawn_cb_t cb , int priority ,
101+ const char * owner ) {
102+ return mhfu_event_on ( MHFU_EVENT_MONSTER_SPAWNED , ( mhfu_event_fn_t ) cb , priority , owner );
80103}
81- static inline int mhfu_on_monster_spawned (mhfu_monster_spawn_cb_t cb ) {
82- return mhfu_event_register (MHFU_EVENT_MONSTER_SPAWNED ,
83- (mhfu_event_cb_t )(void * )cb );
104+ static inline mhfu_hook_rc_t mhfu_on_quest_targets_building (mhfu_quest_targets_cb_t cb ,
105+ int priority , const char * owner ) {
106+ return mhfu_event_on (MHFU_EVENT_QUEST_TARGETS_BUILDING , (mhfu_event_fn_t )cb , priority ,
107+ owner );
84108}
85109
86110#ifdef __cplusplus
0 commit comments