Fixed rare errors.

Consider possibility of nil or NULL being a value in the tables returned
from util.FindInSphere, util.FindInBox, and ents.GetAll.
This commit is contained in:
JetBoom 2014-11-19 09:07:50 -05:00
parent 15d3347371
commit b9e182385f
15 changed files with 49 additions and 31 deletions

View file

@ -23,7 +23,7 @@ function ENT:SetProp(ent)
local teamid = self:GetTeam() local teamid = self:GetTeam()
local inrad = false local inrad = false
for _, pl in pairs(ents.FindInSphere(ent:LocalToWorld(ent:OBBCenter()), ent:BoundingRadius() / 2 + self:GetExtraRadius())) do for _, pl in pairs(ents.FindInSphere(ent:LocalToWorld(ent:OBBCenter()), ent:BoundingRadius() / 2 + self:GetExtraRadius())) do
if pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then if pl and pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then
inrad = true inrad = true
break break
end end
@ -65,7 +65,7 @@ function ENT:Think()
local center = ent:LocalToWorld(ent:OBBCenter()) local center = ent:LocalToWorld(ent:OBBCenter())
for _, pl in pairs(ents.FindInSphere(center, ent:BoundingRadius() / 2 + self:GetExtraRadius())) do for _, pl in pairs(ents.FindInSphere(center, ent:BoundingRadius() / 2 + self:GetExtraRadius())) do
if pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then if pl and pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then
pushout = true pushout = true
if timeout then if timeout then

View file

@ -13,7 +13,7 @@ end
function ENT:IsInsideProp() function ENT:IsInsideProp()
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end if ent and ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
end end
return false return false
@ -41,7 +41,7 @@ function ENT:RecalculateValidity()
valid = false valid = false
else else
for _, ent in pairs(ents.FindInSphere(tr.HitPos, 128)) do for _, ent in pairs(ents.FindInSphere(tr.HitPos, 128)) do
if ent:IsValid() and ent:GetClass() == "prop_arsenalcrate" then if ent and ent:IsValid() and ent:GetClass() == "prop_arsenalcrate" then
valid = false valid = false
break break
end end

View file

@ -19,7 +19,7 @@ end
function ENT:IsInsideProp() function ENT:IsInsideProp()
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end if ent and ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
end end
return false return false
@ -56,7 +56,7 @@ function ENT:RecalculateValidity()
valid = false valid = false
elseif self.GhostDistance then elseif self.GhostDistance then
for _, ent in pairs(ents.FindInSphere(tr.HitPos, self.GhostDistance)) do for _, ent in pairs(ents.FindInSphere(tr.HitPos, self.GhostDistance)) do
if ent:IsValid() and ent:GetClass() == self.GhostEntity then if ent and ent:IsValid() and ent:GetClass() == self.GhostEntity then
valid = false valid = false
break break
end end

View file

@ -14,7 +14,7 @@ end
function ENT:IsInsideProp() function ENT:IsInsideProp()
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end if ent and ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
end end
return false return false
@ -37,7 +37,7 @@ function ENT:RecalculateValidity()
valid = false valid = false
else else
for _, ent in pairs(ents.FindInSphere(tr.HitPos, 48)) do for _, ent in pairs(ents.FindInSphere(tr.HitPos, 48)) do
if ent:IsValid() and ent:GetClass() == "prop_messagebeacon" then if ent and ent:IsValid() and ent:GetClass() == "prop_messagebeacon" then
valid = false valid = false
break break
end end

View file

@ -37,7 +37,7 @@ function ENT:AcceptInput(name, activator, caller, arg)
local vPos = self:GetPos() local vPos = self:GetPos()
for _, ent in pairs(ents.FindInSphere(vPos, self:GetRadius())) do for _, ent in pairs(ents.FindInSphere(vPos, self:GetRadius())) do
if ent:IsPlayer() and ent:Alive() and WorldVisible(vPos, ent:NearestPoint(vPos)) then if ent and ent:IsValid() and ent:IsPlayer() and ent:Alive() and WorldVisible(vPos, ent:NearestPoint(vPos)) then
if ent:Team() == TEAM_UNDEAD then if ent:Team() == TEAM_UNDEAD then
--[[if ent:Health() < ent:GetMaxHealth() and not ent:GetZombieClassTable().Boss then --[[if ent:Health() < ent:GetMaxHealth() and not ent:GetZombieClassTable().Boss then
ent:SetHealth(math.min(ent:GetMaxZombieHealth(), ent:Health() + self.Heal)) ent:SetHealth(math.min(ent:GetMaxZombieHealth(), ent:Health() + self.Heal))

View file

@ -15,7 +15,7 @@ function SWEP:Think()
local origin = self.Owner:LocalToWorld(self.Owner:OBBCenter()) local origin = self.Owner:LocalToWorld(self.Owner:OBBCenter())
for _, ent in pairs(ents.FindInSphere(origin, 40)) do for _, ent in pairs(ents.FindInSphere(origin, 40)) do
if ent:IsPlayer() and ent:Team() ~= TEAM_UNDEAD and ent:Alive() and TrueVisible(origin, ent:NearestPoint(origin)) then if ent and ent:IsValid() and ent:IsPlayer() and ent:Team() ~= TEAM_UNDEAD and ent:Alive() and TrueVisible(origin, ent:NearestPoint(origin)) then
ent:PoisonDamage(1, self.Owner, self) ent:PoisonDamage(1, self.Owner, self)
end end
end end

View file

@ -93,7 +93,7 @@ function SWEP:DoAlert()
local mouthpos = owner:EyePos() + owner:GetUp() * -3 local mouthpos = owner:EyePos() + owner:GetUp() * -3
local screampos = mouthpos + owner:GetAimVector() * 16 local screampos = mouthpos + owner:GetAimVector() * 16
for _, ent in pairs(ents.FindInSphere(screampos, 92)) do for _, ent in pairs(ents.FindInSphere(screampos, 92)) do
if ent:IsPlayer() and ent:Team() ~= owner:Team() then if ent and ent:IsValid() and ent:IsPlayer() and ent:Team() ~= owner:Team() then
local entearpos = ent:EyePos() local entearpos = ent:EyePos()
local dist = screampos:Distance(entearpos) local dist = screampos:Distance(entearpos)
if dist <= 92 and TrueVisible(entearpos, screampos) then if dist <= 92 and TrueVisible(entearpos, screampos) then

View file

@ -787,7 +787,7 @@ function GM:PlayerSelectSpawn(pl)
local blocked local blocked
local spawnpos = spawn:GetPos() local spawnpos = spawn:GetPos()
for _, ent in pairs(ents.FindInBox(spawnpos + playermins, spawnpos + playermaxs)) do for _, ent in pairs(ents.FindInBox(spawnpos + playermins, spawnpos + playermaxs)) do
if ent:IsPlayer() and not spawninplayer or string.sub(ent:GetClass(), 1, 5) == "prop_" then if ent and ent:IsValid() and ent:IsPlayer() and not spawninplayer or string.sub(ent:GetClass(), 1, 5) == "prop_" then
blocked = true blocked = true
break break
end end
@ -1737,7 +1737,7 @@ end
function GM:EvaluatePropFreeze(ent, neighbors) function GM:EvaluatePropFreeze(ent, neighbors)
if not ent then if not ent then
for _, e in pairs(ents.GetAll()) do for _, e in pairs(ents.GetAll()) do
if e:IsValid() then if e and e:IsValid() then
self:EvaluatePropFreeze(e) self:EvaluatePropFreeze(e)
end end
end end

View file

@ -153,7 +153,7 @@ end
local function barricadetimer(self, timername) local function barricadetimer(self, timername)
if self:IsValid() then if self:IsValid() then
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e:Alive() then if e and e:IsValid() and e:IsPlayer() and e:Alive() then
return return
end end
end end
@ -169,7 +169,7 @@ function meta:TemporaryBarricadeObject()
if self.IsBarricadeObject then return end if self.IsBarricadeObject then return end
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e:Alive() then if e and e:IsValid() and e:IsPlayer() and e:Alive() then
self.IsBarricadeObject = true self.IsBarricadeObject = true
self:CollisionRulesChanged() self:CollisionRulesChanged()

View file

@ -367,7 +367,7 @@ end
local function GetNailOwner(nail, filter) local function GetNailOwner(nail, filter)
for _, ent in pairs(ents.GetAll()) do for _, ent in pairs(ents.GetAll()) do
if ent ~= filter and ent.Nails then if ent and ent ~= filter and ent.Nails and ent:isValid() then
for __, n in pairs(ent.Nails) do for __, n in pairs(ent.Nails) do
if n == nail then if n == nail then
return ent return ent

View file

@ -444,7 +444,7 @@ end
local function nocollidetimer(self, timername) local function nocollidetimer(self, timername)
if self:IsValid() then if self:IsValid() then
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then if e and e:IsValid() and e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then
return return
end end
end end
@ -459,7 +459,7 @@ function meta:TemporaryNoCollide(force)
if self:GetCollisionGroup() ~= COLLISION_GROUP_PLAYER and not force then return end if self:GetCollisionGroup() ~= COLLISION_GROUP_PLAYER and not force then return end
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then if e and e:IsValid() and e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then
self:SetCollisionGroup(COLLISION_GROUP_DEBRIS_TRIGGER) self:SetCollisionGroup(COLLISION_GROUP_DEBRIS_TRIGGER)
local timername = "TemporaryNoCollide"..self:UniqueID() local timername = "TemporaryNoCollide"..self:UniqueID()
@ -547,7 +547,7 @@ function meta:ActiveBarricadeGhosting(override)
if self:Team() ~= TEAM_HUMAN and not override or not self:GetBarricadeGhosting() then return false end if self:Team() ~= TEAM_HUMAN and not override or not self:GetBarricadeGhosting() then return false end
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if self:ShouldBarricadeGhostWith(ent) then return true end if ent and ent:IsValid() and self:ShouldBarricadeGhostWith(ent) then return true end
end end
return false return false

View file

@ -182,7 +182,7 @@ function GM:CanCraft(pl, craft)
if not pl:IsValid() or not pl:Alive() or pl:Team() ~= TEAM_HUMAN then return false end if not pl:IsValid() or not pl:Alive() or pl:Team() ~= TEAM_HUMAN then return false end
local plpos = pl:EyePos() local plpos = pl:EyePos()
local entities = ents.FindInSphere(plpos, self.CraftingRange) local entities = ents.FindValidInSphere(plpos, self.CraftingRange)
if craft.CanCraft and not craft:CanCraft(pl, entities, plpos) then return false end if craft.CanCraft and not craft:CanCraft(pl, entities, plpos) then return false end

View file

@ -118,9 +118,11 @@ end
function util.BlastDamageEx(inflictor, attacker, epicenter, radius, damage, damagetype) function util.BlastDamageEx(inflictor, attacker, epicenter, radius, damage, damagetype)
local filter = inflictor local filter = inflictor
for _, ent in pairs(ents.FindInSphere(epicenter, radius)) do for _, ent in pairs(ents.FindInSphere(epicenter, radius)) do
local nearest = ent:NearestPoint(epicenter) if ent and ent:IsValid() then
if TrueVisibleFilters(epicenter, nearest, inflictor, ent) then local nearest = ent:NearestPoint(epicenter)
ent:TakeSpecialDamage(((radius - nearest:Distance(epicenter)) / radius) * damage, damagetype, attacker, inflictor, nearest) if TrueVisibleFilters(epicenter, nearest, inflictor, ent) then
ent:TakeSpecialDamage(((radius - nearest:Distance(epicenter)) / radius) * damage, damagetype, attacker, inflictor, nearest)
end
end end
end end
end end
@ -129,12 +131,26 @@ function util.BlastDamage2(inflictor, attacker, epicenter, radius, damage)
util.BlastDamageEx(inflictor, attacker, epicenter, radius, damage, DMG_BLAST) util.BlastDamageEx(inflictor, attacker, epicenter, radius, damage, DMG_BLAST)
end end
function util.FindValidInSphere(pos, radius)
local ret = {}
for _, ent in pairs(util.FindInSphere(pos, radius)) do
if ent and ent:IsValid() then
ret[#ret + 1] = ent
end
end
return ret
end
function util.PoisonBlastDamage(inflictor, attacker, epicenter, radius, damage, noreduce) function util.PoisonBlastDamage(inflictor, attacker, epicenter, radius, damage, noreduce)
local filter = inflictor local filter = inflictor
for _, ent in pairs(ents.FindInSphere(epicenter, radius)) do for _, ent in pairs(ents.FindInSphere(epicenter, radius)) do
local nearest = ent:NearestPoint(epicenter) if ent and ent:IsValid() then
if TrueVisibleFilters(epicenter, nearest, inflictor, ent) then local nearest = ent:NearestPoint(epicenter)
ent:PoisonDamage(((radius - nearest:Distance(epicenter)) / radius) * damage, attacker, inflictor, nil, noreduce) if TrueVisibleFilters(epicenter, nearest, inflictor, ent) then
ent:PoisonDamage(((radius - nearest:Distance(epicenter)) / radius) * damage, attacker, inflictor, nil, noreduce)
end
end end
end end
end end

View file

@ -128,10 +128,12 @@ function GM:ProfilerPlayerValid(pl)
-- Are they near a trigger hurt? -- Are they near a trigger hurt?
for _, ent in pairs(ents.FindInSphere(plcenter, 256)) do for _, ent in pairs(ents.FindInSphere(plcenter, 256)) do
local entclass = ent:GetClass() if ent and ent:IsValid() then
if entclass == "trigger_hurt" then local entclass = ent:GetClass()
--print('trigger hurt') if entclass == "trigger_hurt" then
return false --print('trigger hurt')
return false
end
end end
end end

View file

@ -37,7 +37,7 @@ hook.Add("InitPostEntityMap", "zombieescape", function(fromze)
end end
for _, ent in pairs(ents.GetAll()) do for _, ent in pairs(ents.GetAll()) do
if ent.ZEDelete then if ent and ent.ZEDelete and ent:IsValid() then
ent:Remove() ent:Remove()
end end
end end