Skip to content

Lua: OnPickup and OnSlap triggers#4805

Open
Loobinex wants to merge 5 commits into
masterfrom
LuaOnPickupOnSlap
Open

Lua: OnPickup and OnSlap triggers#4805
Loobinex wants to merge 5 commits into
masterfrom
LuaOnPickupOnSlap

Conversation

@Loobinex
Copy link
Copy Markdown
Member

No description provided.

@Loobinex Loobinex requested a review from PieterVdc May 21, 2026 21:05
@Loobinex Loobinex marked this pull request as draft May 23, 2026 23:30
@Loobinex Loobinex marked this pull request as ready for review May 23, 2026 23:30
@PieterVdc PieterVdc requested a review from Copilot May 24, 2026 09:28
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds new Lua trigger entry points so level scripts can react to player interactions (picking up things into the hand and slapping things), integrating them into the existing TriggerSystem/Builtins event pipeline.

Changes:

  • Invoke new Lua triggers from hand pickup and slap power usage (lua_on_pick_up, lua_on_slap).
  • Add C trigger wrappers and header declarations for OnPickUp and OnSlap.
  • Add Builtins.lua handlers to forward PickUp/Slap events into ProcessEvent.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/magic_powers.c Calls Lua triggers when using Hand pickup and Slap powers
src/lua_triggers.h Declares new trigger functions for pickup/slap
src/lua_triggers.c Implements Lua global dispatch for OnPickUp / OnSlap
config/fxdata/lua/triggers/Builtins.lua Adds builtin Lua handlers emitting PickUp and Slap events
Comments suppressed due to low confidence (1)

src/magic_powers.c:1806

  • lua_on_slap is invoked before verifying thing_exists(thing) and before checking whether the slap will actually be applied (instance already PI_Whip / recent drop early-return). This can fire OnSlap for a non-existent thing (Lua gets nil) or for slaps that are ignored, which is likely inconsistent with the trigger semantics. Consider moving lua_on_slap to after the thing_exists check and only on the Lb_SUCCESS path (when the slap is actually performed and counted).
static TbResult magic_use_power_slap_thing(PowerKind power_kind, PlayerNumber plyr_idx, struct Thing *thing, MapSubtlCoord stl_x, MapSubtlCoord stl_y, KeepPwrLevel power_level, unsigned long mod_flags)
{
    struct PlayerInfo *player;
    struct Dungeon *dungeon;
    lua_on_slap(thing, plyr_idx);
    if (!thing_exists(thing)) {
        return Lb_FAIL;
    }
    player = get_player(plyr_idx);
    dungeon = get_dungeon(player->id_number);
    if ((player->instance_num == PI_Whip) || (get_gameturn() - dungeon->last_creature_dropped_gameturn <= 10)) {
        return Lb_OK;
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lua_triggers.c
Comment on lines +285 to +291
lua_getglobal(Lvl_script, "OnPickUp");
if (lua_isfunction(Lvl_script, -1))
{
lua_pushThing(Lvl_script, thing);
lua_pushPlayer(Lvl_script, plyr_idx);
CheckLua(Lvl_script, lua_pcall(Lvl_script, 1, 0, 0), "OnPickUp");
}
Comment thread src/lua_triggers.c
Comment on lines +301 to +307
lua_getglobal(Lvl_script, "OnSlap");
if (lua_isfunction(Lvl_script, -1))
{
lua_pushThing(Lvl_script, thing);
lua_pushPlayer(Lvl_script, plyr_idx);
CheckLua(Lvl_script, lua_pcall(Lvl_script, 1, 0, 0), "OnSlap");
}
Comment thread src/magic_powers.c
if (place_thing_in_power_hand(thing, plyr_idx))
else if (place_thing_in_power_hand(thing, plyr_idx))
{
lua_on_pick_up(thing, plyr_idx);
Comment on lines +103 to +113
function OnPickUp(thing)
local eventData = {}
eventData.thing = thing
ProcessEvent("PickUp",eventData)
end

--- Called when a thing is slapped. Can be Creatures, Traps, Shots, Objects, etc.
---@param thing Thing
function OnSlap(thing)
local eventData = {}
eventData.thing = thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants