Hi guys,
I am sure some of you remember that a while ago I made some "damage functions" for e2, getting attacker, inflictor, damage etc. Today I decided, with some encouragement from "Dav1d" that I would remake them with a nicer layout and a few more functions, as well as a new datatype.
The functions included in this version are:
runOnDamage(N) -- Runs every time damage is inflicted on anything
dmgClk() -- Returns 1 every time damage is inflicted on anything
entity:dmginfo() -- Returns the "damage" datatype, this is used for the rest of the functions, you retrieve the damage data from it
damage:getDamage() -- Returns the damage inflicted on the entity from the damage datatype
damage:getAttacker() -- Returns the entity attacking the entity from the damage datatype
damage:getInflictor() -- Returns the entity inflicting the damage on the entity from the damage datatype
damage:getVictim() -- Basically just returns the entity from the entity:dmginfo()
getVictim() -- Returns the entity of ANYTHING that has damage inflicted on it
damage:dmgPos() -- Returns the position that the entity was hit
damage:dmgForce() -- Returns the force inflicted on the entity as a directional vector
damage:dmgNormal() -- Returns a normalized vector, denoting the direction in which the entity was hit
I hope these functions are better than my old ones and I WILL accept suggestions and requests, I also appreciate bug reports. I plan to also create a table function to receive all of the data in a table and also the type of damage. Anyway, here's the code:
Code:
local registered_chips = {}
local dmgrun = 0
local damageData = nil
local damagedEnt = nil
registerType("damage", "dmg", nil,
nil,
nil,
function(retval)
if retval == nil then return end
if type(retval) ~= "table" then error("Return value is neither nil nor a table, but a "..type(retval).."!",0) end
end,
function(v)
return type(v) ~= "table" or not v.HitPos
end
)
e2function damage operator=(damage lhs, damage rhs)
self.vars[lhs] = rhs
self.vclk[lhs] = true
return damage
end
registerCallback("destruct",function(self)
registered_chips[self.entity] = nil
end)
__e2setcost(1)
e2function void runOnDamage(activate)
if activate ~= 0 then
registered_chips[self.entity] = true
else
registered_chips[self.entity] = nil
end
end
e2function number dmgClk()
return dmgrun
end
e2function damage entity:dmginfo()
if damagedEnt ~= this then return nil end
return damageData
end
e2function number damage:getDamage()
if this == nil then return 0 end
return this:GetDamage()
end
e2function entity damage:getAttacker()
if this == nil then return end
return this:GetAttacker()
end
e2function entity damage:getInflictor()
if this == nil then return end
return this:GetInflictor()
end
e2function entity damage:getVictim()
if this == nil then return end
return damagedEnt
end
e2function entity getVictim()
return damagedEnt
end
e2function vector damage:dmgPos()
if this == nil then return {0,0,0} end
return this:GetDamagePosition()
end
e2function vector damage:dmgForce()
if this == nil then return {0,0,0} end
return this:GetDamageForce()
end
e2function vector damage:dmgNormal()
if this == nil then return {0,0,0} end
local force = this:GetDamageForce()
return force:Normalize()
end
local function DamageClock(ent, inflictor, attacker, amount, dmginfo)
local ents = {}
-- this additional step is needed because we cant modify registered_chips while it is being iterated.
for entity,_ in pairs(registered_chips) do
if entity:IsValid() then table.insert(ents, entity) end
end
dmgrun = 1
damagedEnt = ent
damageData = dmginfo
for _,entity in ipairs(ents) do
entity:Execute()
end
dmgrun = 0
damagedEnt = nil
damageData = nil
end
hook.Add("EntityTakeDamage", "DamageClock", DamageClock)
__e2setcost(nil) ENJOY!
Bookmarks