Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/fxdata/lua/aliases.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
---@alias actionpoint integer
---@alias location playersingle|actionpoint|"LAST_EVENT"|"COMBAT"|Pos3d
---@alias thing_class "Object"|"Shot"|"EffectElem"|"DeadCreature"|"Creature"|"Effect"|"EffectGen"|"Trap"|"Door"|"AmbientSnd"|"CaveIn"
---@alias effect_or_effelem_type integer|string|effect_type|effect_element_type -- I allow string here because there's to many entries for, the language server to handle
---@alias effect_or_effelem_type integer|string|effect_type|effect_element_type -- I allow string here because there's to many entries for, the language server to handle
---@alias damage_source_kind "UNKNOWN"|"LAVA"|"POWER_SLAP"|"POWER"|"DOT_SPELL"|"CREATURE"|"TRAP"|"OBJECT"|"DOOR"

14 changes: 9 additions & 5 deletions config/fxdata/lua/triggers/Builtins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ function OnDungeonDestroyed(player)
ProcessEvent("DungeonDestroyed",eventData)
end

--- Called when a thing taked damage
---@param thing Thing
---@param damage integer
---@param dealing_player Player
function OnApplyDamage(thing, damage, dealing_player)
--- Called when a thing takes damage
---@param thing Thing The thing receiving damage
---@param damage integer Amount of damage dealt
---@param dealing_player Player Player responsible for the damage
---@param source_thing Thing|nil The thing that caused the damage (e.g., attacking creature, trap)
---@param source_kind damage_source_kind String identifying the damage source (e.g., "LAVA", "CREATURE", "TRAP")
function OnApplyDamage(thing, damage, dealing_player, source_thing, source_kind)
local eventData = {}
eventData.thing = thing
eventData.damage = damage
eventData.dealing_player = dealing_player
eventData.source_thing = source_thing
eventData.source_kind = source_kind
ProcessEvent("ApplyDamage",eventData)
end

Expand Down
12 changes: 10 additions & 2 deletions config/fxdata/lua/triggers/Events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,22 @@ end
---Triggers when a thing takes damage
---@param action function|string the function to call when the event happens
---@param thing? Thing the unit that triggers the event
---@param source_thing? Thing the thing that cause the trigger
---@param source_kind? damage_source_kind kind of damage
---@return table
function RegisterThingDamageEvent(action, thing)
local trigData = {thing = thing}
function RegisterThingDamageEvent(action, thing, source_thing, source_kind)
local trigData = {thing = thing,source_thing = source_thing, source_kind = source_kind}

local trigger = CreateTrigger("ApplyDamage",action,trigData)
if thing then
TriggerAddCondition(trigger, function(eventData,triggerData) return eventData.thing == triggerData.thing end)
end
if source_thing then
TriggerAddCondition(trigger, function(eventData,triggerData) return eventData.source_thing == triggerData.source_thing end)
end
if source_kind then
TriggerAddCondition(trigger, function(eventData,triggerData) return eventData.source_kind == triggerData.source_kind end)
end
return trigger
end

Expand Down
5 changes: 3 additions & 2 deletions src/console_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "thing_objects.h"
#include "thing_navigate.h"
#include "thing_physics.h"
#include "thing_creature.h"
#include "version.h"
#include "frontmenu_ingame_map.h"
#include <string.h>
Expand Down Expand Up @@ -1839,7 +1840,7 @@ TbBool cmd_freeze_creature(PlayerNumber plyr_idx, char * args)
}
thing_play_sample(thing, 50, NORMAL_PITCH, 0, 3, 0, 4, FULL_LOUDNESS);
// Not sure how to handle this yet, for now simply hardcode the intended spell kind with a number.
apply_spell_effect_to_thing(thing, 3, 8, plyr_idx); // 3 was 'SplK_Freeze' in the enum.
apply_spell_effect_to_thing(thing, 3, 8, plyr_idx, INVALID_THING, DSK_Power); // 3 was 'SplK_Freeze' in the enum.
return true;
}

Expand All @@ -1857,7 +1858,7 @@ TbBool cmd_slow_creature(PlayerNumber plyr_idx, char * args)
}
thing_play_sample(thing, 50, NORMAL_PITCH, 0, 3, 0, 4, FULL_LOUDNESS);
// Not sure how to handle this yet, for now simply hardcode the intended spell kind with a number.
apply_spell_effect_to_thing(thing, 12, 8, plyr_idx); // 12 was 'SplK_Slow' in the enum.
apply_spell_effect_to_thing(thing, 12, 8, plyr_idx, INVALID_THING, DSK_Power); // 12 was 'SplK_Slow' in the enum.
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/creature_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct CastedSpellData {
GameTurnDelta duration;
CrtrExpLevel caster_level;
PlayerNumber caster_owner;
ThingIndex caster_thing_idx;
};

struct CreatureControl {
Expand Down
2 changes: 1 addition & 1 deletion src/creature_states_pray.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void apply_spell_effect_to_players_creatures(PlayerNumber plyr_idx, ThingModel c
// Thing list loop body
if (creature_matches_model(thing,crmodel))
{
apply_spell_effect_to_thing(thing, spl_idx, overchrg, plyr_idx);
apply_spell_effect_to_thing(thing, spl_idx, overchrg, plyr_idx, INVALID_THING, DSK_None);
}
// Thing list loop body ends
k++;
Expand Down
1 change: 1 addition & 0 deletions src/lua_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "thing_effects.h"
#include "magic_powers.h"

#include "lua_api.h"
#include "lua_base.h"
#include "lua_params.h"
#include "lua_api_lens.h"
Expand Down
7 changes: 4 additions & 3 deletions src/lua_api_things.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static int thing_set_field(lua_State *L) {
} else if (strcmp(key, "health") == 0)
{
thing->health = luaL_checkinteger(L, 3);
} else if (strcmp(key, "pos") == 0)
} else if (strcmp(key, "pos") == 0)
{
struct Coord3d pos;
luaL_checkCoord3d(L, 3, &pos);
Expand Down Expand Up @@ -569,7 +569,7 @@ static const struct luaL_Reg thing_methods[] = {
{"stun", lua_stun_creature},
{"delete", lua_delete_thing},
{"isValid", lua_is_valid},

{"transfer" ,lua_Transfer_creature },
{"level_up" ,lua_Level_up_creature },
{"teleport" ,lua_Teleport_creature },
Expand Down Expand Up @@ -614,4 +614,5 @@ void Thing_register(lua_State *L) {

// Pop the metatable
lua_pop(L, 1);
}
}

6 changes: 4 additions & 2 deletions src/lua_triggers.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void lua_on_creature_rebirth(struct Thing* crtng)
}


void lua_on_apply_damage_to_thing(struct Thing *thing, HitPoints dmg, PlayerNumber dealing_plyr_idx)
void lua_on_apply_damage_to_thing(struct Thing *thing, HitPoints dmg, PlayerNumber dealing_plyr_idx, struct Thing *tngsrc, DamageSourceKind source_kind)
{
SYNCDBG(6,"Starting");
lua_getglobal(Lvl_script, "OnApplyDamage");
Expand All @@ -193,8 +193,10 @@ void lua_on_apply_damage_to_thing(struct Thing *thing, HitPoints dmg, PlayerNumb
lua_pushThing(Lvl_script, thing);
lua_pushinteger(Lvl_script, dmg);
lua_pushPlayer(Lvl_script, dealing_plyr_idx);
lua_pushThing(Lvl_script, tngsrc);
lua_pushstring(Lvl_script, damage_source_kind_name(source_kind));

CheckLua(Lvl_script, lua_pcall(Lvl_script, 3, 0, 0),"OnApplyDamage");
CheckLua(Lvl_script, lua_pcall(Lvl_script, 5, 0, 0),"OnApplyDamage");
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/lua_triggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "globals.h"
#include "bflib_basics.h"
#include "thing_stats.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -36,7 +37,7 @@ void lua_on_dungeon_destroyed(PlayerNumber plyr_idx);
void lua_on_creature_death(struct Thing *crtng);
void lua_on_creature_rebirth(struct Thing* crtng);
void lua_on_trap_placed(struct Thing *traptng);
void lua_on_apply_damage_to_thing(struct Thing *thing, HitPoints dmg, PlayerNumber dealing_plyr_idx);
void lua_on_apply_damage_to_thing(struct Thing *thing, HitPoints dmg, PlayerNumber dealing_plyr_idx, struct Thing *tngsrc, DamageSourceKind source_kind);
void lua_on_level_up(struct Thing *thing);
void lua_on_slab_kind_change(MapSlabCoord slb_x, MapSlabCoord slb_y, SlabKind old_slab);
void lua_on_slab_owner_change(MapSlabCoord slb_x, MapSlabCoord slb_y, PlayerNumber old_owner);
Expand Down
4 changes: 2 additions & 2 deletions src/magic_powers.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ void slap_creature(struct PlayerInfo *player, struct Thing *thing)
if (crconf->slaps_to_kill > 0)
{
HitPoints slap_damage = calculate_correct_creature_max_health(thing) / crconf->slaps_to_kill;
apply_damage_to_thing_and_display_health(thing, slap_damage, player->id_number);
apply_damage_to_thing_and_display_health(thing, slap_damage, player->id_number, INVALID_THING, DSK_PowerSlap);
}
powerst = get_power_model_stats(PwrK_SLAP);
i = cctrl->slap_turns;
Expand Down Expand Up @@ -1386,7 +1386,7 @@ static TbResult magic_use_power_apply_spell(PowerKind power_kind, PlayerNumber p
create_used_effect_or_element(&effpos, powerst->effect_id, thing->owner, 0);
}
thing_play_sample(thing, powerst->select_sound_idx, NORMAL_PITCH, 0, 3, 0, 2, FULL_LOUDNESS);
apply_spell_effect_to_thing(thing, powerst->spell_idx, power_level, plyr_idx);
apply_spell_effect_to_thing(thing, powerst->spell_idx, power_level, plyr_idx, INVALID_THING, DSK_None);
// Special cases.
if (flag_is_set(spconf->spell_flags, CSAfF_Disease))
{ // Set disease_caster_plyridx if spell_idx has 'CSAfF_Disease'.
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,7 @@ TngUpdateRet damage_creatures_with_physical_force(struct Thing *thing, ModTngFil
}
if (thing_is_creature(thing))
{
apply_damage_to_thing_and_display_health(thing, param->secondary_number, param->primary_number);
apply_damage_to_thing_and_display_health(thing, param->secondary_number, param->primary_number, INVALID_THING, DSK_Power);
if ((thing->health >= 0) && !creature_is_leaving_and_cannot_be_stopped(thing))
{
if (((thing->alloc_flags & TAlF_IsControlled) == 0) && !creature_is_kept_in_custody(thing))
Expand All @@ -2514,7 +2514,7 @@ TngUpdateRet damage_creatures_with_physical_force(struct Thing *thing, ModTngFil
}
else if (thing_is_destructible_trap(thing) > 0)
{
apply_damage_to_thing(thing, param->secondary_number, param->primary_number);
apply_damage_to_thing(thing, param->secondary_number, param->primary_number, INVALID_THING, DSK_Power);
return TUFRet_Modified;
}
return TUFRet_Unchanged;
Expand Down Expand Up @@ -3529,7 +3529,7 @@ void gameplay_loop_timestep()
exit_keeper = 1;
}
}

frametime_end_measurement(Frametime_Sleep);
}

Expand Down
7 changes: 4 additions & 3 deletions src/power_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "thing_objects.h"
#include "thing_physics.h"
#include "thing_effects.h"
#include "thing_creature.h"
#include "thing_navigate.h"
#include "creature_instances.h"
#include "creature_states.h"
Expand Down Expand Up @@ -214,7 +215,7 @@ void process_disease(struct Thing *creatng)
&& !creature_is_immune_to_spell_effect(thing, CSAfF_Disease)
&& (cctrl->disease_caster_plyridx != game.neutral_player_num))
{ // Apply the spell kind stored in 'active_disease_spell'.
apply_spell_effect_to_thing(thing, cctrl->active_disease_spell, cctrl->exp_level, creatng->owner);
apply_spell_effect_to_thing(thing, cctrl->active_disease_spell, cctrl->exp_level, creatng->owner, creatng, DSK_Power);
tngcctrl->disease_caster_plyridx = cctrl->disease_caster_plyridx;
}
// Per thing code ends.
Expand All @@ -230,7 +231,7 @@ void process_disease(struct Thing *creatng)
}
if (((game.play_gameturn - cctrl->disease_start_turn) % game.conf.rules[creatng->owner].magic.disease_lose_health_time) == 0)
{
apply_damage_to_thing_and_display_health(creatng, game.conf.rules[creatng->owner].magic.disease_lose_percentage_health * cctrl->max_health / 100, cctrl->disease_caster_plyridx);
apply_damage_to_thing_and_display_health(creatng, game.conf.rules[creatng->owner].magic.disease_lose_percentage_health * cctrl->max_health / 100, cctrl->disease_caster_plyridx, INVALID_THING, DSK_Power);
}
}

Expand Down Expand Up @@ -303,7 +304,7 @@ void update_god_lightning_ball(struct Thing *thing)
if (!thing_exists(target))
break;
shotst = get_shot_model_stats(thing->model);
apply_damage_to_thing_and_display_health(target, shotst->damage, thing->owner);
apply_damage_to_thing_and_display_health(target, shotst->damage, thing->owner, thing, DSK_Power);
if (target->health < 0)
{
struct CreatureControl* cctrl = creature_control_get_from_thing(target);
Expand Down
Loading