Skip to content

Commit

Permalink
Merge pull request #2003 from Vurv78/master
Browse files Browse the repository at this point in the history
Add Death+Respawn detection functions (player.lua)
  • Loading branch information
Sparky authored Jan 4, 2020
2 parents d0b693a + 58b868b commit 40d22d8
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
136 changes: 136 additions & 0 deletions lua/entities/gmod_wire_expression2/core/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,139 @@ end
e2function entity lastDisconnectedPlayer()
return lastLeft
end

----- Deaths+Spawns, Dev: Vurv, 12/28/19 -----
local DeathAlert = {} -- table of e2s that have runOnDeath(1)
local RespawnAlert = {}
local DeathList = { last = {} }
local RespawnList = { last = {} }

hook.Add("PlayerDeath","Exp2PlayerDetDead",function(victim,inflictor,attacker)
local entry = {} -- default table
entry.victim = victim
entry.inflictor = inflictor
entry.timestamp = CurTime()
entry.attacker = attacker
DeathList[victim:EntIndex()] = entry -- victim's death is saved in victims death list.
DeathList.last = entry -- the most recent death's table is stored here for later use.
for ex,_ in pairs(DeathAlert) do -- loops over all chips in deathalert, ignores key.
if IsValid(ex) then
ex.context.data.runByDeath = entry
ex:Execute()
ex.context.data.runByDeath = nil
end
end
end)

hook.Add("PlayerSpawn","Exp2PlayerDetRespn",function(player,transition)
local entry = {}
entry.ply = player
entry.timestamp = CurTime()
RespawnList[player:EntIndex()] = entry
RespawnList.last = entry
for ex,_ in pairs(RespawnAlert) do
if IsValid(ex) then
ex.context.data.runByRespawned = entry
ex:Execute()
ex.context.data.runByRespawned = nil
end
end
end)

__e2setcost(5)

--- If active is 0, the chip will no longer run on death.
e2function void runOnDeath(number activate)
if activate ~= 0 then
DeathAlert[self.entity] = true
else
DeathAlert[self.entity] = nil
end
end

--If ran by death, (defined in e2 data), gives 1 or 0 (ternary)
e2function number deathClk()
return self.data.runByDeath and 1 or 0
end

e2function number lastDeathTime() -- returns when the last death happened
local Timestamp = DeathList.last.timestamp
if not IsValid(Timestamp) then return 0 end
return Timestamp -- Checks if num is valid, if so then returns the timestamp from the table, else returns 0.
end

e2function number lastDeathTime(entity ply) -- returns when the player provided last died
if not IsValid(ply) then return NULL end
if not ply:IsPlayer() then return NULL end
local Timestamp = DeathList[ply:EntIndex()].timestamp
if not IsValid(Timestamp) then return 0 end
return Timestamp -- Checks if num is valid, if so then returns the timestamp from the table, else returns 0. (also checks if ply is player and valid)
end

e2function entity lastDeathVictim() -- Gives Death Victim
local Victim = DeathList.last.victim
if not IsValid(Victim) then return NULL end
return Victim
end

e2function entity lastDeathInflictor()
local Inflictor = DeathList.last.inflictor
if not IsValid(Inflictor) then return NULL end
return Inflictor
end

e2function entity lastDeathInflictor(entity ply)
if not IsValid(ply) then return NULL end
if not ply:IsPlayer() then return NULL end
local Inflictor = DeathList[ply:EntIndex()].inflictor
if not IsValid(Inflictor) then return NULL end
return Inflictor
end

e2function entity lastDeathAttacker()
local Attacker = DeathList.last.attacker
if not IsValid(Attacker) then return NULL end
return Attacker
end

e2function entity lastDeathAttacker(entity ply)
if not IsValid(ply) then return NULL end
if not ply:IsPlayer() then return NULL end
local Attacker = DeathList[ply:EntIndex()].attacker
if not IsValid(Attacker) then return NULL end
return Attacker
end

-- Spawn Functions
e2function number spawnClk()
return self.data.runByRespawned and 1 or 0
end

e2function void runOnSpawn(number activate)
if activate ~= 0 then
RespawnAlert[self.entity] = true
else
RespawnAlert[self.entity] = nil
end
end

e2function number lastSpawnTime()
local Timestamp = RespawnList.last.timestamp
if not IsValid(Timestamp) then return 0 end
return Timestamp
end

e2function number lastSpawnTime(entity ply) -- returns the last time player provided spawned.
if not IsValid(ply) then return 0 end
if not ply:IsPlayer() then return 0 end
local Timestamp = SpawnList[ply:EntIndex()].timestamp
if not IsValid(Timestamp) then return 0 end
return Timestamp
end

e2function entity lastSpawnedPlayer()
local Ply = RespawnList.last.ply
if not IsValid(Ply) then return NULL end
return Ply
end
--******************************************--
17 changes: 17 additions & 0 deletions lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,23 @@ E2Helper.Descriptions["toUnicodeChar(r)"] = "Returns the UTF-8 string from the g
E2Helper.Descriptions["toUnicodeByte(s:nn)"] = "Returns the Unicode code-points from the given UTF-8 string"
E2Helper.Descriptions["unicodeLength(s:nn)"] = "Returns the length of the given UTF-8 string"

-- Damage
E2Helper.Descriptions["runOnDeath(n)"] = "If set to 0, chip won't run on players dying"
E2Helper.Descriptions["deathClk()"] = "Returns if the E2 was triggered by a death"
E2Helper.Descriptions["lastDeathTime()"] = "Returns the last time a player died"
E2Helper.Descriptions["lastDeathTime(e)"] = "Returns the last time given player died"
E2Helper.Descriptions["lastDeathInflictor()"] = "Returns the entity that inflicted the last death"
E2Helper.Descriptions["lastDeathInflictor(e)"] = "Returns the entity that inflicted the given player's last death"
E2Helper.Descriptions["lastDeathVictim()"] = "Returns the last player to die"
E2Helper.Descriptions["lastDeathAttacker()"] = "Returns the attacker who killed the last player to die"
E2Helper.Descriptions["lastDeathAttacker(e)"] = "Returns the attacker who killed the player provided in their last death"
--
E2Helper.Descriptions["runOnSpawn(n)"] = "If set to 0, chip won't run on players spawning"
E2Helper.Descriptions["spawnClk()"] = "Returns if the E2 was triggered by a player spawning"
E2Helper.Descriptions["lastSpawnTime()"] = "Returns the last time a player spawned"
E2Helper.Descriptions["lastSpawnTime(e)"] = "Returns the last time the given player spawned"
E2Helper.Descriptions["lastSpawnedPlayer()"] = "Returns the last player to spawn"

---- Custom ----
-- Effect
E2Helper.Descriptions["effect()"] = "Creates and returns new effect"
Expand Down

0 comments on commit 40d22d8

Please sign in to comment.