Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add various hooks #676

Closed
wants to merge 10 commits into from
Closed
3 changes: 2 additions & 1 deletion autogen/convert_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"src/pc/djui/djui_console.h",
"src/game/player_palette.h",
"src/pc/network/lag_compensation.h",
"src/pc/djui/djui_panel_menu.h"
"src/pc/djui/djui_panel_menu.h",
"src/game/mario.h"
]

exclude_constants = {
Expand Down
37 changes: 34 additions & 3 deletions autogen/lua_definitions/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4163,6 +4163,28 @@ MARIO_SPAWN_UNUSED_38 = ((MARIO_SPAWN_PAINTING_STAR_COLLECT ) + 6)
--- @type MarioSpawnType
MARIO_SPAWN_FADE_FROM_BLACK = ((MARIO_SPAWN_PAINTING_STAR_COLLECT ) + 7)

--- @class MarioHealType

--- @type MarioHealType
HEAL_NORMAL = 0

--- @type MarioHealType
HEAL_WATER = 1

--- @class MarioHurtType

--- @type MarioHurtType
HURT_NORMAL = 0

--- @type MarioHurtType
HURT_WATER = 1

--- @type MarioHurtType
HURT_TOXIC_GAS = 2

--- @type MarioHurtType
HURT_BURNING = 3

--- @class CharacterAnimID

--- @type CharacterAnimID
Expand Down Expand Up @@ -9190,10 +9212,19 @@ HOOK_ON_GEO_PROCESS_CHILDREN = 50
HOOK_MARIO_OVERRIDE_GEOMETRY_INPUTS = 51

--- @type LuaHookedEventType
HOOK_ON_INTERACTIONS = 52
HOOK_HEALED_MARIO = 52

--- @type LuaHookedEventType
HOOK_HURT_MARIO = 53

--- @type LuaHookedEventType
HOOK_ON_INTERACTIONS = 54

--- @type LuaHookedEventType
HOOK_AFTER_QUARTER_STEP = 55

--- @type LuaHookedEventType
HOOK_MAX = 53
HOOK_MAX = 56

--- @class LuaModMenuElementType

Expand Down Expand Up @@ -12526,7 +12557,7 @@ MINOR_VERSION_NUMBER = 1
SM64COOPDX_VERSION = "v1.2.1"

--- @type integer
VERSION_NUMBER = 38
VERSION_NUMBER = 39

--- @type string
VERSION_TEXT = "v"
5 changes: 5 additions & 0 deletions autogen/lua_definitions/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5197,6 +5197,11 @@ function transition_submerged_to_walking(m)
-- ...
end

--- @param m MarioState
function update_burning_health_common(m)
-- ...
end

--- @param m MarioState
--- Applies the translation from Mario's current animation to his world position. Considers animation flags (horizontal/vertical translation)
function update_mario_pos_for_anim(m)
Expand Down
30 changes: 28 additions & 2 deletions docs/lua/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
- [level_update.h](#level_updateh)
- [enum HUDDisplayFlag](#enum-HUDDisplayFlag)
- [enum MarioSpawnType](#enum-MarioSpawnType)
- [mario.h](#marioh)
- [enum MarioHealType](#enum-MarioHealType)
- [enum MarioHurtType](#enum-MarioHurtType)
- [mario_animation_ids.h](#mario_animation_idsh)
- [enum CharacterAnimID](#enum-CharacterAnimID)
- [enum MarioAnimID](#enum-MarioAnimID)
Expand Down Expand Up @@ -1533,6 +1536,26 @@

<br />

## [mario.h](#mario.h)

### [enum MarioHealType](#MarioHealType)
| Identifier | Value |
| :--------- | :---- |
| HEAL_NORMAL | 0 |
| HEAL_WATER | 1 |

### [enum MarioHurtType](#MarioHurtType)
| Identifier | Value |
| :--------- | :---- |
| HURT_NORMAL | 0 |
| HURT_WATER | 1 |
| HURT_TOXIC_GAS | 2 |
| HURT_BURNING | 3 |

[:arrow_up_small:](#)

<br />

## [mario_animation_ids.h](#mario_animation_ids.h)

### [enum CharacterAnimID](#CharacterAnimID)
Expand Down Expand Up @@ -3350,8 +3373,11 @@
| HOOK_BEFORE_GEO_PROCESS | 49 |
| HOOK_ON_GEO_PROCESS_CHILDREN | 50 |
| HOOK_MARIO_OVERRIDE_GEOMETRY_INPUTS | 51 |
| HOOK_ON_INTERACTIONS | 52 |
| HOOK_MAX | 53 |
| HOOK_HEALED_MARIO | 52 |
| HOOK_HURT_MARIO | 53 |
| HOOK_ON_INTERACTIONS | 54 |
| HOOK_AFTER_QUARTER_STEP | 55 |
| HOOK_MAX | 56 |

### [enum LuaModMenuElementType](#LuaModMenuElementType)
| Identifier | Value |
Expand Down
20 changes: 20 additions & 0 deletions docs/lua/functions-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,26 @@ Transitions Mario from being underwater to a walking state. Resets camera to the

<br />

## [update_burning_health_common](#update_burning_health_common)

### Lua Example
`update_burning_health_common(m)`

### Parameters
| Field | Type |
| ----- | ---- |
| m | [MarioState](structs.md#MarioState) |

### Returns
- None

### C Prototype
`void update_burning_health_common(struct MarioState* m);`

[:arrow_up_small:](#)

<br />

## [update_mario_pos_for_anim](#update_mario_pos_for_anim)

### Description
Expand Down
1 change: 1 addition & 0 deletions docs/lua/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@
- [set_steep_jump_action](functions-4.md#set_steep_jump_action)
- [set_water_plunge_action](functions-4.md#set_water_plunge_action)
- [transition_submerged_to_walking](functions-4.md#transition_submerged_to_walking)
- [update_burning_health_common](functions-4.md#update_burning_health_common)
- [update_mario_pos_for_anim](functions-4.md#update_mario_pos_for_anim)
- [update_mario_sound_and_camera](functions-4.md#update_mario_sound_and_camera)

Expand Down
44 changes: 35 additions & 9 deletions docs/lua/guides/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,38 @@
Hooks are a way for SM64 to trigger Lua code, whereas the functions listed in [functions](../functions.md) allow Lua to trigger SM64 code.

# Supported Hooks
- [hook_behavior](#hook_behavior)
- [hook_chat_command](#hook_chat_command)
- [hook_event](#hook_event)
- [hook_mario_action](#hook_mario_action)
- [hook_on_sync_table_change](#hook_on_sync_table_change)
- [hook_mod_menu_button](#hook_mod_menu_button)
- [hook_mod_menu_checkbox](#hook_mod_menu_checkbox)
- [hook_mod_menu_slider](#hook_mod_menu_slider)
- [hook_mod_menu_inputbox](#hook_mod_menu_inputbox)
- [Hooks](#hooks)
- [Supported Hooks](#supported-hooks)
- [hook\_behavior](#hook_behavior)
- [Parameters](#parameters)
- [Returns](#returns)
- [Lua Example](#lua-example)
- [hook\_chat\_command](#hook_chat_command)
- [Parameters](#parameters-1)
- [Lua Example](#lua-example-1)
- [hook\_event](#hook_event)
- [Hook Event Types](#hook-event-types)
- [Parameters](#parameters-2)
- [Lua Example](#lua-example-2)
- [hook\_mario\_action](#hook_mario_action)
- [Parameters](#parameters-3)
- [Action Hook Types](#action-hook-types)
- [Lua Example](#lua-example-3)
- [hook\_on\_sync\_table\_change](#hook_on_sync_table_change)
- [Parameters](#parameters-4)
- [Lua Example](#lua-example-4)
- [hook\_mod\_menu\_button](#hook_mod_menu_button)
- [Parameters](#parameters-5)
- [Lua Example](#lua-example-5)
- [hook\_mod\_menu\_checkbox](#hook_mod_menu_checkbox)
- [Parameters](#parameters-6)
- [Lua Example](#lua-example-6)
- [hook\_mod\_menu\_slider](#hook_mod_menu_slider)
- [Parameters](#parameters-7)
- [Lua Example](#lua-example-7)
- [hook\_mod\_menu\_inputbox](#hook_mod_menu_inputbox)
- [Parameters](#parameters-8)
- [Lua Example](#lua-example-8)

<br />

Expand Down Expand Up @@ -142,7 +165,10 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh
| HOOK_ON_GEO_PROCESS | Called when a GeoLayout is processed **Note:** You must set the `hookProcess` field of the graph node to a non-zero value | [GraphNode](../structs.md#GraphNode) graphNode, `integer` matStackIndex |
| HOOK_BEFORE_GEO_PROCESS | Called before a GeoLayout is processed **Note:** You must set the `hookProcess` field of the graph node to a non-zero value | [GraphNode](../structs.md#GraphNode) graphNode, `integer` matStackIndex |
| HOOK_ON_GEO_PROCESS_CHILDREN | Called when the children of a GeoLayout node is processed **Note:** You must set the `hookProcess` field of the parent graph node to a non-zero value | [GraphNode](../structs.md#GraphNode) graphNode, `integer` matStackIndex |
| HOOK_HEALED_MARIO | Called when any player has just healed from any source. Called once per affected player. | MarioState mario, `integer` healType |
| HOOK_HURT_MARIO | Called when any player has just taken damage from any source. Called once per affected player. | MarioState mario, `integer` hurtType |
| HOOK_ON_INTERACTIONS | Called when the Mario interactions are processed | [MarioState](../structs.md#MarioState) mario |
| HOOK_AFTER_QUARTER_STEP | Called after a single quarter step has been processed. Return a step result to override the current step result. | [MarioState](../structs.md#MarioState) mario, `integer` stepType, `integer` stepResult, `integer` stepNumber |

### Parameters

Expand Down
16 changes: 16 additions & 0 deletions src/game/mario.c
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,17 @@ void set_submerged_cam_preset_and_spawn_bubbles(struct MarioState *m) {
}
}

void update_burning_health_common(struct MarioState* m) {
m->health -= 10;
smlua_call_event_hooks_mario_param_and_int_ret_bool(HOOK_HURT_MARIO, m, HURT_BURNING, NULL);
if (m->health < 0x100) {
if (m == &gMarioStates[0]) {
// never kill remote marios
set_mario_action(m, ACT_STANDING_DEATH, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the previous way of killing mario is still needed, because they were specifically for airborne actions. it would set mario as dead, but the death action doesn't play until mario reaches the floor. setting mario's action while in the air will teleport mario to the ground even if he's not there yet.

}
}
}

/**
* Both increments and decrements Mario's HP.
*/
Expand All @@ -1699,6 +1710,7 @@ void update_mario_health(struct MarioState *m) {
if ((m->input & INPUT_IN_POISON_GAS) && !(m->action & ACT_FLAG_INTANGIBLE)) {
if (!(m->flags & MARIO_METAL_CAP) && !gDebugLevelSelect) {
m->health -= 4;
smlua_call_event_hooks_mario_param_and_int_ret_bool(HOOK_HURT_MARIO, m, HURT_TOXIC_GAS, NULL);
}
} else {
if ((m->action & ACT_FLAG_SWIMMING) && !(m->action & ACT_FLAG_INTANGIBLE)) {
Expand All @@ -1709,8 +1721,10 @@ void update_mario_health(struct MarioState *m) {
// If using the debug level select, do not lose any HP to water.
if ((m->pos[1] >= (m->waterLevel - 140)) && !terrainIsSnow) {
m->health += 0x1A;
smlua_call_event_hooks_mario_param_and_int_ret_bool(HOOK_HEALED_MARIO, m, HEAL_WATER, NULL);
} else if (!gDebugLevelSelect) {
m->health -= (terrainIsSnow ? 3 : 1);
smlua_call_event_hooks_mario_param_and_int_ret_bool(HOOK_HURT_MARIO, m, HURT_WATER, NULL);
}
}
}
Expand All @@ -1719,10 +1733,12 @@ void update_mario_health(struct MarioState *m) {
if (m->healCounter > 0) {
m->health += 0x40;
m->healCounter--;
smlua_call_event_hooks_mario_param_and_int_ret_bool(HOOK_HEALED_MARIO, m, HEAL_NORMAL, NULL);
}
if (m->hurtCounter > 0) {
m->health -= 0x40;
m->hurtCounter--;
smlua_call_event_hooks_mario_param_and_int_ret_bool(HOOK_HURT_MARIO, m, HURT_NORMAL, NULL);
}

if (m->health > 0x880) {
Expand Down
13 changes: 13 additions & 0 deletions src/game/mario.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
extern u16 gLocalBubbleCounter;
struct WallCollisionData;

enum MarioHealType {
HEAL_NORMAL,
HEAL_WATER,
};

enum MarioHurtType {
HURT_NORMAL,
HURT_WATER,
HURT_TOXIC_GAS,
HURT_BURNING,
};

/* |description|
Checks if Mario's current animation has reached its final frame (i.e., the last valid frame in the animation).
Useful for deciding when to transition out of an animation-driven action
Expand Down Expand Up @@ -299,5 +311,6 @@ void set_mario_particle_flags(struct MarioState* m, u32 flags, u8 clear);
Updates Mario's wall information based on wall collisions (`WallCollisionData`). Chooses the most relevant wall depending on the level’s collision fix settings
|descriptionEnd| */
void mario_update_wall(struct MarioState* m, struct WallCollisionData* wcd);
void update_burning_health_common(struct MarioState* m);

#endif // MARIO_H
20 changes: 2 additions & 18 deletions src/game/mario_actions_airborne.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,15 +1116,7 @@ s32 act_burning_jump(struct MarioState *m) {

m->marioObj->oMarioBurnTimer += 3;

m->health -= 10;
if (m->health < 0x100) {
if (m != &gMarioStates[0]) {
// never kill remote marios
m->health = 0x100;
} else {
m->health = 0xFF;
}
}
update_burning_health_common(m);

reset_rumble_timers(m);
return FALSE;
Expand All @@ -1143,15 +1135,7 @@ s32 act_burning_fall(struct MarioState *m) {
set_mario_particle_flags(m, PARTICLE_FIRE, FALSE);
m->marioObj->oMarioBurnTimer += 3;

m->health -= 10;
if (m->health < 0x100) {
if (m != &gMarioStates[0]) {
// never kill remote marios
m->health = 0x100;
} else {
m->health = 0xFF;
}
}
update_burning_health_common(m);

reset_rumble_timers(m);
return FALSE;
Expand Down
33 changes: 13 additions & 20 deletions src/game/mario_actions_automatic.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,26 +365,19 @@ s32 perform_hanging_step(struct MarioState *m, Vec3f nextPos) {
floorHeight = find_floor(nextPos[0], nextPos[1], nextPos[2], &floor);
ceilHeight = vec3f_mario_ceil(nextPos, floorHeight, &ceil);

if (floor == NULL) {
return HANG_HIT_CEIL_OR_OOB;
}
if (ceil == NULL) {
return HANG_LEFT_CEIL;
}
if (ceilHeight - floorHeight <= 160.0f) {
return HANG_HIT_CEIL_OR_OOB;
}
if (ceil->type != SURFACE_HANGABLE) {
return HANG_LEFT_CEIL;
}

ceilOffset = ceilHeight - (nextPos[1] + 160.0f);
if (ceilOffset < -30.0f) {
return HANG_HIT_CEIL_OR_OOB;
}
if (ceilOffset > 30.0f) {
return HANG_LEFT_CEIL;
}
s32 stepResult = HANG_NONE;
if (floor == NULL) { stepResult = HANG_HIT_CEIL_OR_OOB; }
else if (ceil == NULL) { stepResult = HANG_LEFT_CEIL; }
else if (ceilHeight - floorHeight <= 160.0f) { stepResult = HANG_HIT_CEIL_OR_OOB; }
else if (ceil->type != SURFACE_HANGABLE) { stepResult = HANG_LEFT_CEIL; }
else {
ceilOffset = ceilHeight - (nextPos[1] + 160.0f);
if (ceilOffset < -30.0f) { stepResult = HANG_HIT_CEIL_OR_OOB; }
if (ceilOffset > 30.0f) { stepResult = HANG_LEFT_CEIL; }
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this code? it was much more readable before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code was rewritten to be like this because all of the returns. although made the code easier to read, made it pretty incompatible with the hook.


smlua_call_event_hooks_after_quarter_step(HOOK_AFTER_QUARTER_STEP, m, STEP_TYPE_HANG, stepResult, 0, &stepResult);
if (stepResult != HANG_NONE) { return stepResult; }

nextPos[1] = m->ceilHeight - 160.0f;
vec3f_copy(m->pos, nextPos);
Expand Down
9 changes: 1 addition & 8 deletions src/game/mario_actions_moving.c
Original file line number Diff line number Diff line change
Expand Up @@ -1485,14 +1485,7 @@ s32 act_burning_ground(struct MarioState *m) {
set_mario_particle_flags(m, PARTICLE_FIRE, FALSE);
play_sound(SOUND_MOVING_LAVA_BURN, m->marioObj->header.gfx.cameraToObject);

m->health -= 10;
if (m->health < 0x100) {
extern struct MarioState gMarioStates[];
if (m == &gMarioStates[0]) {
// never kill remote marios
set_mario_action(m, ACT_STANDING_DEATH, 0);
}
}
update_burning_health_common(m);

m->marioBodyState->eyeState = MARIO_EYES_DEAD;

Expand Down
Loading