From 447c6bee7e4e58df23f0aedb0dbf0939a6ee7bdc Mon Sep 17 00:00:00 2001 From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54> Date: Fri, 11 Nov 2011 17:17:59 +0000 Subject: [PATCH] Hooks system added so taht addons do their addon things git-svn-id: http://svn.net-core.org/repos/t-engine4@4643 51575b47-30f0-44d4-a5cc-537603b46e54 --- game/engines/default/engine/class.lua | 31 ++++++++++++++++++++ game/modules/tome/class/Actor.lua | 3 ++ game/modules/tome/class/interface/Combat.lua | 3 ++ game/modules/tome/data/damage_types.lua | 10 +++++-- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/game/engines/default/engine/class.lua b/game/engines/default/engine/class.lua index 9623079c6d..48fe9c27d6 100644 --- a/game/engines/default/engine/class.lua +++ b/game/engines/default/engine/class.lua @@ -167,6 +167,36 @@ function _M:replaceWith(t) setmetatable(self, getmetatable(t)) end +-- --------------------------------------------------------------------- +-- --------------------------------------------------------------------- +-- Hooks & Events system +-- --------------------------------------------------------------------- +-- --------------------------------------------------------------------- + +local _hooks = {hooks={}, list={}} + +function _M:bindHook(hook, fct) + _hooks.list[hook] = _hooks.list[hook] or {} + table.insert(_hooks.list[hook], fct) + + local sfct = [[return function(l, self, data) local ok=false]] + + for i, fct in ipairs(_hooks.list[hook]) do + sfct = sfct..([[ if l[%d](self, data) then ok=true end]]):format(i) + end + + sfct = sfct..[[ return ok end]] + local f, err = loadstring(sfct) + if not f then error(err) end + _hooks.hooks[hook] = f() +end + +function _M:triggerHook(hook) + local h = hook[1] + if not _hooks.hooks[h] then return end + return _hooks.hooks[h](_hooks.list[h], self, hook) +end + -- --------------------------------------------------------------------- -- --------------------------------------------------------------------- -- LOAD & SAVE @@ -181,6 +211,7 @@ function _M:save(filter, allow) filter._no_save_fields = true filter._mo = true filter._mo_final = true + filter._hooks = true else filter.__CLASSNAME = true end diff --git a/game/modules/tome/class/Actor.lua b/game/modules/tome/class/Actor.lua index 087d91943a..0c2994ac5d 100644 --- a/game/modules/tome/class/Actor.lua +++ b/game/modules/tome/class/Actor.lua @@ -1446,6 +1446,9 @@ function _M:onTakeHit(value, src) end end + local hd = {"Actor:takeHit", value=value, src=src} + if self:triggerHook(hd) then value = hd.value end + -- Resource leech if value > 0 and src and src:attr("resource_leech_chance") and rng.percent(src.resource_leech_chance) then local leech = src.resource_leech_value diff --git a/game/modules/tome/class/interface/Combat.lua b/game/modules/tome/class/interface/Combat.lua index e8f090f523..daf8824059 100644 --- a/game/modules/tome/class/interface/Combat.lua +++ b/game/modules/tome/class/interface/Combat.lua @@ -601,6 +601,9 @@ function _M:attackTargetWith(target, weapon, damtype, mult, force_dam) end end + local hd = {"Combat:attackTargetWith", hitted=hitted, target=target, weapon=weapon, damtype=damtype, mult=mult} + if self:triggerHook(hd) then hitted = hd.hitted end + -- Visual feedback if hitted then game.level.map:particleEmitter(target.x, target.y, 1, "melee_attack", {color=target.blood_color}) end diff --git a/game/modules/tome/data/damage_types.lua b/game/modules/tome/data/damage_types.lua index f369e3111b..8d56bf30ae 100644 --- a/game/modules/tome/data/damage_types.lua +++ b/game/modules/tome/data/damage_types.lua @@ -30,6 +30,9 @@ setDefaultProjector(function(src, x, y, type, dam, tmp, no_martyr) print("[PROJECTOR] starting dam", dam) + local hd = {"DamageProjector:base", src=src, x=x, y=y, type=type, dam=dam} + if self:triggerHook(hd) then dam = hd.dam end + -- Difficulty settings if game.difficulty == game.DIFFICULTY_EASY and rtarget.player then dam = dam * 0.7 @@ -64,7 +67,7 @@ setDefaultProjector(function(src, x, y, type, dam, tmp, no_martyr) dam = dam + dam * target:attr("inc_necrotic_minions") / 100 print("[PROJECTOR] after necrotic increase dam", dam) end - + if target.isTalentActive and target:isTalentActive(target.T_GESTURE_OF_GUARDING) then local t = target:getTalentFromId(target.T_GESTURE_OF_GUARDING) dam = t.on_damageInflicted(target, t, type, dam, src) @@ -184,7 +187,7 @@ setDefaultProjector(function(src, x, y, type, dam, tmp, no_martyr) local t = target:getTalentFromId(target.T_ENERGY_DECOMPOSITION) dam = t.on_damage(target, t, type, dam) end - + if target.isTalentActive and target:isTalentActive(target.T_GESTURE_OF_GUARDING) then local t = target:getTalentFromId(target.T_GESTURE_OF_GUARDING) dam = t.on_damageReceived(target, t, type, dam, src) @@ -212,6 +215,9 @@ setDefaultProjector(function(src, x, y, type, dam, tmp, no_martyr) print("[PROJECTOR] final dam", dam) + local hd = {"DamageProjector:final", src=src, x=x, y=y, type=type, dam=dam} + if self:triggerHook(hd) then dam = hd.dam end + local source_talent = src.__projecting_for and src.__projecting_for.project_type and (src.__projecting_for.project_type.talent_id or src.__projecting_for.project_type.talent) and src.getTalentFromId and src:getTalentFromId(src.__projecting_for.project_type.talent or src.__projecting_for.project_type.talent_id) local dead dead, dam = target:takeHit(dam, src, {damtype=type, source_talent=source_talent}) -- GitLab